[0dd3a2f] | 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 | // |
---|
[1e1e15b] | 7 | // Indexer.h -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 21:38:55 2015 |
---|
[b8665e3] | 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Fri Mar 8 13:55:00 2019 |
---|
| 13 | // Update Count : 9 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[b8665e3] | 18 | #include <functional> // for function |
---|
| 19 | #include <list> // for list |
---|
| 20 | #include <memory> // for shared_ptr, enable_shared_from_this |
---|
| 21 | #include <string> // for string |
---|
[51b7345] | 22 | |
---|
[b8665e3] | 23 | #include "Common/PersistentMap.h" // for PersistentMap |
---|
| 24 | #include "SynTree/SynTree.h" // for AST nodes |
---|
[51b7345] | 25 | |
---|
[a181494] | 26 | namespace ResolvExpr { |
---|
[b8665e3] | 27 | class Cost; |
---|
[a181494] | 28 | } |
---|
| 29 | |
---|
[51b7345] | 30 | namespace SymTab { |
---|
[b8665e3] | 31 | class Indexer : public std::enable_shared_from_this<SymTab::Indexer> { |
---|
| 32 | public: |
---|
[6e50a6b] | 33 | explicit Indexer( bool trackIdentifiers = true ); |
---|
[a08ba92] | 34 | virtual ~Indexer(); |
---|
[17cd4eb] | 35 | |
---|
[ef5b828] | 36 | // when using an indexer manually (e.g., within a mutator traversal), it is necessary to |
---|
[b419abb] | 37 | // tell the indexer explicitly when scopes begin and end |
---|
[a08ba92] | 38 | void enterScope(); |
---|
| 39 | void leaveScope(); |
---|
[17cd4eb] | 40 | |
---|
[a40d503] | 41 | struct IdData { |
---|
[6f096d2] | 42 | const DeclarationWithType * id = nullptr; |
---|
| 43 | const Expression * baseExpr = nullptr; // WithExpr |
---|
[490ff5c3] | 44 | |
---|
| 45 | /// non-null if this declaration is deleted |
---|
[e67991f] | 46 | const Declaration * deleteStmt = nullptr; |
---|
[b8665e3] | 47 | /// scope of identifier |
---|
| 48 | unsigned long scope = 0; |
---|
[a40d503] | 49 | |
---|
[78d69da7] | 50 | // NOTE: shouldn't need either of these constructors, but gcc-4 does not properly support initializer lists with default members. |
---|
| 51 | IdData() = default; |
---|
[ef5b828] | 52 | IdData( |
---|
[e67991f] | 53 | const DeclarationWithType * id, const Expression * baseExpr, const Declaration * deleteStmt, |
---|
[ef5b828] | 54 | unsigned long scope ) |
---|
[b8665e3] | 55 | : id( id ), baseExpr( baseExpr ), deleteStmt( deleteStmt ), scope( scope ) {} |
---|
[e67991f] | 56 | IdData( const IdData& o, const Declaration * deleteStmt ) |
---|
[b8665e3] | 57 | : id( o.id ), baseExpr( o.baseExpr ), deleteStmt( deleteStmt ), scope( o.scope ) {} |
---|
[78d69da7] | 58 | |
---|
[a181494] | 59 | Expression * combine( ResolvExpr::Cost & cost ) const; |
---|
[a40d503] | 60 | }; |
---|
| 61 | |
---|
[bed4c63e] | 62 | /// Gets all declarations with the given ID |
---|
[ef5b828] | 63 | void lookupId( const std::string & id, std::list< IdData > &out ) const; |
---|
[bed4c63e] | 64 | /// Gets the top-most type declaration with the given ID |
---|
[ef5b828] | 65 | const NamedTypeDecl * lookupType( const std::string & id ) const; |
---|
[bed4c63e] | 66 | /// Gets the top-most struct declaration with the given ID |
---|
[ef5b828] | 67 | const StructDecl * lookupStruct( const std::string & id ) const; |
---|
[bed4c63e] | 68 | /// Gets the top-most enum declaration with the given ID |
---|
[ef5b828] | 69 | const EnumDecl * lookupEnum( const std::string & id ) const; |
---|
[bed4c63e] | 70 | /// Gets the top-most union declaration with the given ID |
---|
[ef5b828] | 71 | const UnionDecl * lookupUnion( const std::string & id ) const; |
---|
[bed4c63e] | 72 | /// Gets the top-most trait declaration with the given ID |
---|
[ef5b828] | 73 | const TraitDecl * lookupTrait( const std::string & id ) const; |
---|
[1e1e15b] | 74 | |
---|
[ed34540] | 75 | /// Gets the type declaration with the given ID at global scope |
---|
[ef5b828] | 76 | const NamedTypeDecl * globalLookupType( const std::string & id ) const; |
---|
[9a7a3b6] | 77 | /// Gets the struct declaration with the given ID at global scope |
---|
[ef5b828] | 78 | const StructDecl * globalLookupStruct( const std::string & id ) const; |
---|
[9a7a3b6] | 79 | /// Gets the union declaration with the given ID at global scope |
---|
[ef5b828] | 80 | const UnionDecl * globalLookupUnion( const std::string & id ) const; |
---|
[9a7a3b6] | 81 | /// Gets the enum declaration with the given ID at global scope |
---|
[ef5b828] | 82 | const EnumDecl * globalLookupEnum( const std::string & id ) const; |
---|
[9a7a3b6] | 83 | |
---|
[6f096d2] | 84 | void addId( const DeclarationWithType * decl, const Expression * baseExpr = nullptr ); |
---|
[e67991f] | 85 | void addDeletedId( const DeclarationWithType * decl, const Declaration * deleteStmt ); |
---|
[0ac366b] | 86 | |
---|
[6f096d2] | 87 | void addType( const NamedTypeDecl * decl ); |
---|
[ef5b828] | 88 | void addStruct( const std::string & id ); |
---|
[6f096d2] | 89 | void addStruct( const StructDecl * decl ); |
---|
| 90 | void addEnum( const EnumDecl * decl ); |
---|
[ef5b828] | 91 | void addUnion( const std::string & id ); |
---|
[6f096d2] | 92 | void addUnion( const UnionDecl * decl ); |
---|
| 93 | void addTrait( const TraitDecl * decl ); |
---|
[1e1e15b] | 94 | |
---|
[81644e0] | 95 | /// adds all of the IDs from WithStmt exprs |
---|
[e67991f] | 96 | void addWith( const std::list< Expression * > & withExprs, const Declaration * withStmt ); |
---|
[81644e0] | 97 | |
---|
[5fe35d6] | 98 | /// convenience function for adding a list of Ids to the indexer |
---|
| 99 | void addIds( const std::list< DeclarationWithType * > & decls ); |
---|
| 100 | |
---|
| 101 | /// convenience function for adding a list of forall parameters to the indexer |
---|
| 102 | void addTypes( const std::list< TypeDecl * > & tds ); |
---|
| 103 | |
---|
| 104 | /// convenience function for adding all of the declarations in a function type to the indexer |
---|
[6f096d2] | 105 | void addFunctionType( const FunctionType * ftype ); |
---|
[5fe35d6] | 106 | |
---|
[522363e] | 107 | private: |
---|
[ef5b828] | 108 | /// Wraps a Decl * with a scope |
---|
[b8665e3] | 109 | template<typename Decl> |
---|
| 110 | struct Scoped { |
---|
[6f096d2] | 111 | const Decl * decl; ///< declaration |
---|
[b8665e3] | 112 | unsigned long scope; ///< scope of this declaration |
---|
[30f9072] | 113 | |
---|
[6f096d2] | 114 | Scoped(const Decl * d, unsigned long s) : decl(d), scope(s) {} |
---|
[b8665e3] | 115 | }; |
---|
| 116 | |
---|
| 117 | using Ptr = std::shared_ptr<const Indexer>; |
---|
| 118 | |
---|
| 119 | using MangleTable = PersistentMap< std::string, IdData >; |
---|
| 120 | using IdTable = PersistentMap< std::string, MangleTable::Ptr >; |
---|
| 121 | using TypeTable = PersistentMap< std::string, Scoped<NamedTypeDecl> >; |
---|
| 122 | using StructTable = PersistentMap< std::string, Scoped<StructDecl> >; |
---|
| 123 | using EnumTable = PersistentMap< std::string, Scoped<EnumDecl> >; |
---|
| 124 | using UnionTable = PersistentMap< std::string, Scoped<UnionDecl> >; |
---|
| 125 | using TraitTable = PersistentMap< std::string, Scoped<TraitDecl> >; |
---|
[e8032b0] | 126 | |
---|
[b8665e3] | 127 | IdTable::Ptr idTable; ///< identifier namespace |
---|
| 128 | TypeTable::Ptr typeTable; ///< type namespace |
---|
| 129 | StructTable::Ptr structTable; ///< struct namespace |
---|
| 130 | EnumTable::Ptr enumTable; ///< enum namespace |
---|
| 131 | UnionTable::Ptr unionTable; ///< union namespace |
---|
| 132 | TraitTable::Ptr traitTable; ///< trait namespace |
---|
[e8032b0] | 133 | |
---|
[b8665e3] | 134 | Ptr prevScope; ///< reference to indexer for parent scope |
---|
| 135 | unsigned long scope; ///< Scope index of this indexer |
---|
[b419abb] | 136 | unsigned long repScope; ///< Scope index of currently represented scope |
---|
| 137 | |
---|
| 138 | /// Ensures that a proper backtracking scope exists before a mutation |
---|
| 139 | void lazyInitScope(); |
---|
[b8665e3] | 140 | |
---|
| 141 | /// Gets the indexer at the given scope |
---|
[ef5b828] | 142 | const Indexer * atScope( unsigned long scope ) const; |
---|
[b8665e3] | 143 | |
---|
[ef5b828] | 144 | /// Removes matching autogenerated constructors and destructors so that they will not be |
---|
[42f1279c] | 145 | /// selected. If returns false, passed decl should not be added. |
---|
[6f096d2] | 146 | bool removeSpecialOverrides( IdData & decl, MangleTable::Ptr & mangleTable ); |
---|
[42f1279c] | 147 | |
---|
[b8665e3] | 148 | /// Options for handling identifier conflicts |
---|
| 149 | struct OnConflict { |
---|
| 150 | enum { |
---|
| 151 | Error, ///< Throw a semantic error |
---|
| 152 | Delete ///< Delete the earlier version with the delete statement |
---|
| 153 | } mode; |
---|
[e67991f] | 154 | const Declaration * deleteStmt; ///< Statement that deletes this expression |
---|
[b8665e3] | 155 | |
---|
| 156 | private: |
---|
| 157 | OnConflict() : mode(Error), deleteStmt(nullptr) {} |
---|
[e67991f] | 158 | OnConflict( const Declaration * d ) : mode(Delete), deleteStmt(d) {} |
---|
[b8665e3] | 159 | public: |
---|
| 160 | OnConflict( const OnConflict& ) = default; |
---|
| 161 | |
---|
| 162 | static OnConflict error() { return {}; } |
---|
[e67991f] | 163 | static OnConflict deleteWith( const Declaration * d ) { return { d }; } |
---|
[b8665e3] | 164 | }; |
---|
| 165 | |
---|
| 166 | /// true if the existing identifier conflicts with the added identifier |
---|
| 167 | bool addedIdConflicts( |
---|
[6f096d2] | 168 | const IdData & existing, const DeclarationWithType * added, OnConflict handleConflicts, |
---|
[e67991f] | 169 | const Declaration * deleteStmt ); |
---|
[0ac366b] | 170 | |
---|
| 171 | /// common code for addId, addDeletedId, etc. |
---|
[6f096d2] | 172 | void addId(const DeclarationWithType * decl, OnConflict handleConflicts, |
---|
[e67991f] | 173 | const Expression * baseExpr = nullptr, const Declaration * deleteStmt = nullptr ); |
---|
[b8665e3] | 174 | |
---|
| 175 | /// adds all of the members of the Aggregate (addWith helper) |
---|
[6f096d2] | 176 | void addMembers( const AggregateDecl * aggr, const Expression * expr, OnConflict handleConflicts ); |
---|
[b8665e3] | 177 | |
---|
| 178 | /// returns true if there exists a declaration with C linkage and the given name with the same mangled name |
---|
[6f096d2] | 179 | bool hasCompatibleCDecl( const std::string & id, const std::string & mangleName ) const; |
---|
[b8665e3] | 180 | /// returns true if there exists a declaration with C linkage and the given name with a different mangled name |
---|
[6f096d2] | 181 | bool hasIncompatibleCDecl( const std::string & id, const std::string & mangleName ) const; |
---|
[6e50a6b] | 182 | |
---|
| 183 | bool trackIdentifiers; |
---|
[a08ba92] | 184 | }; |
---|
[51b7345] | 185 | } // namespace SymTab |
---|
| 186 | |
---|
[0dd3a2f] | 187 | // Local Variables: // |
---|
| 188 | // tab-width: 4 // |
---|
| 189 | // mode: c++ // |
---|
| 190 | // compile-command: "make install" // |
---|
| 191 | // End: // |
---|