| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // SymbolTable.hpp --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Aaron B. Moss
|
|---|
| 10 | // Created On : Wed May 29 11:00:00 2019
|
|---|
| 11 | // Last Modified By : Aaron B. Moss
|
|---|
| 12 | // Last Modified On : Wed May 29 11:00:00 2019
|
|---|
| 13 | // Update Count : 1
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <memory> // for shared_ptr, enable_shared_from_this
|
|---|
| 19 | #include <vector>
|
|---|
| 20 |
|
|---|
| 21 | #include "Fwd.hpp"
|
|---|
| 22 | #include "Node.hpp" // for ptr, readonly
|
|---|
| 23 | #include "Common/CodeLocation.h"
|
|---|
| 24 | #include "Common/PersistentMap.h"
|
|---|
| 25 |
|
|---|
| 26 | namespace ResolvExpr {
|
|---|
| 27 | class Cost;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | namespace ast {
|
|---|
| 31 |
|
|---|
| 32 | /// Builds and stores the symbol table, mapping identifiers to declarations.
|
|---|
| 33 | class SymbolTable final : public std::enable_shared_from_this<ast::SymbolTable> {
|
|---|
| 34 | public:
|
|---|
| 35 | /// Stored information about a declaration
|
|---|
| 36 | struct IdData {
|
|---|
| 37 | readonly<DeclWithType> id = nullptr; ///< Identifier of declaration
|
|---|
| 38 | readonly<Expr> baseExpr = nullptr; ///< Implied containing aggregate (from WithExpr)
|
|---|
| 39 | readonly<Decl> deleter = nullptr; ///< Node deleting this declaration (if non-null)
|
|---|
| 40 | unsigned long scope = 0; ///< Scope of identifier
|
|---|
| 41 |
|
|---|
| 42 | IdData() = default;
|
|---|
| 43 | IdData( const DeclWithType * i, const Expr * base, const Decl * del, unsigned long s )
|
|---|
| 44 | : id( i ), baseExpr( base ), deleter( del ), scope( s ) {}
|
|---|
| 45 |
|
|---|
| 46 | /// Modify an existing node with a new deleter
|
|---|
| 47 | IdData( const IdData & o, const Decl * del )
|
|---|
| 48 | : id( o.id ), baseExpr( o.baseExpr ), deleter( del ), scope( o.scope ) {}
|
|---|
| 49 |
|
|---|
| 50 | /// Constructs an expression referring to this identifier.
|
|---|
| 51 | /// Increments `cost` by cost of reference conversion
|
|---|
| 52 | Expr * combine( const CodeLocation & loc, ResolvExpr::Cost & cost ) const;
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | private:
|
|---|
| 56 | /// wraps a reference to D with a scope
|
|---|
| 57 | template<typename D>
|
|---|
| 58 | struct scoped {
|
|---|
| 59 | readonly<D> decl; ///< wrapped declaration
|
|---|
| 60 | unsigned long scope; ///< scope of this declaration
|
|---|
| 61 |
|
|---|
| 62 | scoped(const D * d, unsigned long s) : decl(d), scope(s) {}
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | using MangleTable = PersistentMap< std::string, IdData >;
|
|---|
| 66 | using IdTable = PersistentMap< std::string, MangleTable::Ptr >;
|
|---|
| 67 | using TypeTable = PersistentMap< std::string, scoped<NamedTypeDecl> >;
|
|---|
| 68 | using StructTable = PersistentMap< std::string, scoped<StructDecl> >;
|
|---|
| 69 | using EnumTable = PersistentMap< std::string, scoped<EnumDecl> >;
|
|---|
| 70 | using UnionTable = PersistentMap< std::string, scoped<UnionDecl> >;
|
|---|
| 71 | using TraitTable = PersistentMap< std::string, scoped<TraitDecl> >;
|
|---|
| 72 |
|
|---|
| 73 | IdTable::Ptr idTable; ///< identifier namespace
|
|---|
| 74 | TypeTable::Ptr typeTable; ///< type namespace
|
|---|
| 75 | StructTable::Ptr structTable; ///< struct namespace
|
|---|
| 76 | EnumTable::Ptr enumTable; ///< enum namespace
|
|---|
| 77 | UnionTable::Ptr unionTable; ///< union namespace
|
|---|
| 78 | TraitTable::Ptr traitTable; ///< trait namespace
|
|---|
| 79 |
|
|---|
| 80 | using Ptr = std::shared_ptr<const SymbolTable>;
|
|---|
| 81 |
|
|---|
| 82 | Ptr prevScope; ///< Indexer for parent scope
|
|---|
| 83 | unsigned long scope; ///< Scope index of this indexer
|
|---|
| 84 | unsigned long repScope; ///< Scope index of currently represented scope
|
|---|
| 85 |
|
|---|
| 86 | public:
|
|---|
| 87 | SymbolTable();
|
|---|
| 88 | ~SymbolTable();
|
|---|
| 89 |
|
|---|
| 90 | // when using an indexer manually (e.g., within a mutator traversal), it is necessary to
|
|---|
| 91 | // tell the indexer explicitly when scopes begin and end
|
|---|
| 92 | void enterScope();
|
|---|
| 93 | void leaveScope();
|
|---|
| 94 |
|
|---|
| 95 | /// Gets all declarations with the given ID
|
|---|
| 96 | std::vector<IdData> lookupId( const std::string &id ) const;
|
|---|
| 97 | /// Gets the top-most type declaration with the given ID
|
|---|
| 98 | const NamedTypeDecl * lookupType( const std::string &id ) const;
|
|---|
| 99 | /// Gets the top-most struct declaration with the given ID
|
|---|
| 100 | const StructDecl * lookupStruct( const std::string &id ) const;
|
|---|
| 101 | /// Gets the top-most enum declaration with the given ID
|
|---|
| 102 | const EnumDecl * lookupEnum( const std::string &id ) const;
|
|---|
| 103 | /// Gets the top-most union declaration with the given ID
|
|---|
| 104 | const UnionDecl * lookupUnion( const std::string &id ) const;
|
|---|
| 105 | /// Gets the top-most trait declaration with the given ID
|
|---|
| 106 | const TraitDecl * lookupTrait( const std::string &id ) const;
|
|---|
| 107 |
|
|---|
| 108 | /// Gets the type declaration with the given ID at global scope
|
|---|
| 109 | const NamedTypeDecl * globalLookupType( const std::string &id ) const;
|
|---|
| 110 | /// Gets the struct declaration with the given ID at global scope
|
|---|
| 111 | const StructDecl * globalLookupStruct( const std::string &id ) const;
|
|---|
| 112 | /// Gets the union declaration with the given ID at global scope
|
|---|
| 113 | const UnionDecl * globalLookupUnion( const std::string &id ) const;
|
|---|
| 114 | /// Gets the enum declaration with the given ID at global scope
|
|---|
| 115 | const EnumDecl * globalLookupEnum( const std::string &id ) const;
|
|---|
| 116 |
|
|---|
| 117 | /// Adds an identifier declaration to the symbol table
|
|---|
| 118 | void addId( const DeclWithType * decl, const Expr * baseExpr = nullptr );
|
|---|
| 119 | /// Adds a deleted identifier declaration to the symbol table
|
|---|
| 120 | void addDeletedId( const DeclWithType * decl, const Decl * deleter );
|
|---|
| 121 |
|
|---|
| 122 | /// Adds a type to the symbol table
|
|---|
| 123 | void addType( const NamedTypeDecl * decl );
|
|---|
| 124 | /// Adds a struct declaration to the symbol table by name
|
|---|
| 125 | void addStruct( const std::string & id );
|
|---|
| 126 | /// Adds a struct declaration to the symbol table
|
|---|
| 127 | void addStruct( const StructDecl * decl );
|
|---|
| 128 | /// Adds an enum declaration to the symbol table
|
|---|
| 129 | void addEnum( const EnumDecl * decl );
|
|---|
| 130 | /// Adds a union declaration to the symbol table by name
|
|---|
| 131 | void addUnion( const std::string & id );
|
|---|
| 132 | /// Adds a union declaration to the symbol table
|
|---|
| 133 | void addUnion( const UnionDecl * decl );
|
|---|
| 134 | /// Adds a trait declaration to the symbol table
|
|---|
| 135 | void addTrait( const TraitDecl * decl );
|
|---|
| 136 |
|
|---|
| 137 | /// adds all of the IDs from WithStmt exprs
|
|---|
| 138 | void addWith( const std::vector< ptr<Expr> > & withExprs, const Decl * withStmt );
|
|---|
| 139 |
|
|---|
| 140 | /// convenience function for adding a list of Ids to the indexer
|
|---|
| 141 | void addIds( const std::vector< ptr<DeclWithType> > & decls );
|
|---|
| 142 |
|
|---|
| 143 | /// convenience function for adding a list of forall parameters to the indexer
|
|---|
| 144 | void addTypes( const std::vector< ptr<TypeDecl> > & tds );
|
|---|
| 145 |
|
|---|
| 146 | /// convenience function for adding all of the declarations in a function type to the indexer
|
|---|
| 147 | void addFunction( const FunctionDecl * );
|
|---|
| 148 |
|
|---|
| 149 | private:
|
|---|
| 150 | /// Ensures that a proper backtracking scope exists before a mutation
|
|---|
| 151 | void lazyInitScope();
|
|---|
| 152 |
|
|---|
| 153 | /// Gets the symbol table at a given scope
|
|---|
| 154 | const SymbolTable * atScope( unsigned long i ) const;
|
|---|
| 155 |
|
|---|
| 156 | /// Removes matching autogenerated constructors and destructors so that they will not be
|
|---|
| 157 | /// selected. If returns false, passed decl should not be added.
|
|---|
| 158 | bool removeSpecialOverrides( IdData & decl, MangleTable::Ptr & mangleTable );
|
|---|
| 159 |
|
|---|
| 160 | /// Options for handling identifier conflicts
|
|---|
| 161 | struct OnConflict {
|
|---|
| 162 | enum {
|
|---|
| 163 | Error, ///< Throw a semantic error
|
|---|
| 164 | Delete ///< Delete the earlier version with the delete statement
|
|---|
| 165 | } mode;
|
|---|
| 166 | const Decl * deleter; ///< Statement that deletes this expression
|
|---|
| 167 |
|
|---|
| 168 | private:
|
|---|
| 169 | OnConflict() : mode(Error), deleter(nullptr) {}
|
|---|
| 170 | OnConflict( const Decl * d ) : mode(Delete), deleter(d) {}
|
|---|
| 171 | public:
|
|---|
| 172 | OnConflict( const OnConflict& ) = default;
|
|---|
| 173 |
|
|---|
| 174 | static OnConflict error() { return {}; }
|
|---|
| 175 | static OnConflict deleteWith( const Decl * d ) { return { d }; }
|
|---|
| 176 | };
|
|---|
| 177 |
|
|---|
| 178 | /// true if the existing identifier conflicts with the added identifier
|
|---|
| 179 | bool addedIdConflicts(
|
|---|
| 180 | const IdData & existing, const DeclWithType * added, OnConflict handleConflicts,
|
|---|
| 181 | const Decl * deleter );
|
|---|
| 182 |
|
|---|
| 183 | /// common code for addId, addDeletedId, etc.
|
|---|
| 184 | void addId(
|
|---|
| 185 | const DeclWithType * decl, OnConflict handleConflicts, const Expr * baseExpr = nullptr,
|
|---|
| 186 | const Decl * deleter = nullptr );
|
|---|
| 187 |
|
|---|
| 188 | /// adds all of the members of the Aggregate (addWith helper)
|
|---|
| 189 | void addMembers( const AggregateDecl * aggr, const Expr * expr, OnConflict handleConflicts );
|
|---|
| 190 |
|
|---|
| 191 | /// returns true if there exists a declaration with C linkage and the given name with the same mangled name
|
|---|
| 192 | bool hasCompatibleCDecl( const std::string &id, const std::string &mangleName ) const;
|
|---|
| 193 | /// returns true if there exists a declaration with C linkage and the given name with a different mangled name
|
|---|
| 194 | bool hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const;
|
|---|
| 195 | };
|
|---|
| 196 |
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | // Local Variables: //
|
|---|
| 200 | // tab-width: 4 //
|
|---|
| 201 | // mode: c++ //
|
|---|
| 202 | // compile-command: "make install" //
|
|---|
| 203 | // End: //
|
|---|