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