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 : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Aug 17 16:09:12 2017 |
---|
13 | // Update Count : 8 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <iosfwd> // for ostream |
---|
19 | #include <list> // for list |
---|
20 | #include <string> // for string |
---|
21 | |
---|
22 | #include "SynTree/Visitor.h" // for Visitor |
---|
23 | #include "SynTree/SynTree.h" // for AST nodes |
---|
24 | |
---|
25 | namespace SymTab { |
---|
26 | class Indexer : public Visitor { |
---|
27 | public: |
---|
28 | explicit Indexer( bool useDebug = false ); |
---|
29 | |
---|
30 | Indexer( const Indexer &that ); |
---|
31 | Indexer( Indexer &&that ); |
---|
32 | virtual ~Indexer(); |
---|
33 | Indexer& operator= ( const Indexer &that ); |
---|
34 | Indexer& operator= ( Indexer &&that ); |
---|
35 | |
---|
36 | using Visitor::visit; |
---|
37 | virtual void visit( ObjectDecl *objectDecl ); |
---|
38 | virtual void visit( FunctionDecl *functionDecl ); |
---|
39 | virtual void visit( TypeDecl *typeDecl ); |
---|
40 | virtual void visit( TypedefDecl *typeDecl ); |
---|
41 | virtual void visit( StructDecl *aggregateDecl ); |
---|
42 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
43 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
44 | virtual void visit( TraitDecl *aggregateDecl ); |
---|
45 | |
---|
46 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
47 | virtual void visit( IfStmt *ifStmt ); |
---|
48 | virtual void visit( ForStmt *forStmt ); |
---|
49 | virtual void visit( CatchStmt *catchStmt ); |
---|
50 | |
---|
51 | virtual void visit( ApplicationExpr *applicationExpr ); |
---|
52 | virtual void visit( UntypedExpr *untypedExpr ); |
---|
53 | virtual void visit( NameExpr *nameExpr ); |
---|
54 | virtual void visit( CastExpr *castExpr ); |
---|
55 | virtual void visit( AddressExpr *addressExpr ); |
---|
56 | virtual void visit( LabelAddressExpr *labAddressExpr ); |
---|
57 | virtual void visit( UntypedMemberExpr *memberExpr ); |
---|
58 | virtual void visit( MemberExpr *memberExpr ); |
---|
59 | virtual void visit( VariableExpr *variableExpr ); |
---|
60 | virtual void visit( ConstantExpr *constantExpr ); |
---|
61 | virtual void visit( SizeofExpr *sizeofExpr ); |
---|
62 | virtual void visit( AlignofExpr *alignofExpr ); |
---|
63 | virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
64 | virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
65 | virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
66 | virtual void visit( AttrExpr *attrExpr ); |
---|
67 | virtual void visit( LogicalExpr *logicalExpr ); |
---|
68 | virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
69 | virtual void visit( CommaExpr *commaExpr ); |
---|
70 | virtual void visit( TypeExpr *typeExpr ); |
---|
71 | virtual void visit( AsmExpr *asmExpr ); |
---|
72 | virtual void visit( ImplicitCopyCtorExpr *impCpCtorExpr ); |
---|
73 | virtual void visit( ConstructorExpr * ctorExpr ); |
---|
74 | virtual void visit( CompoundLiteralExpr *compLitExpr ); |
---|
75 | virtual void visit( RangeExpr *rangeExpr ); |
---|
76 | virtual void visit( UntypedTupleExpr *tupleExpr ); |
---|
77 | virtual void visit( TupleExpr *tupleExpr ); |
---|
78 | virtual void visit( TupleIndexExpr *tupleExpr ); |
---|
79 | virtual void visit( TupleAssignExpr *tupleExpr ); |
---|
80 | virtual void visit( StmtExpr * stmtExpr ); |
---|
81 | virtual void visit( UniqueExpr * uniqueExpr ); |
---|
82 | |
---|
83 | virtual void visit( TraitInstType *contextInst ); |
---|
84 | virtual void visit( StructInstType *contextInst ); |
---|
85 | virtual void visit( UnionInstType *contextInst ); |
---|
86 | |
---|
87 | // when using an indexer manually (e.g., within a mutator traversal), it is necessary to tell the indexer |
---|
88 | // explicitly when scopes begin and end |
---|
89 | void enterScope(); |
---|
90 | void leaveScope(); |
---|
91 | |
---|
92 | /// Gets all declarations with the given ID |
---|
93 | void lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const; |
---|
94 | /// Gets the top-most type declaration with the given ID |
---|
95 | NamedTypeDecl *lookupType( const std::string &id ) const; |
---|
96 | /// Gets the top-most struct declaration with the given ID |
---|
97 | StructDecl *lookupStruct( const std::string &id ) const; |
---|
98 | /// Gets the top-most enum declaration with the given ID |
---|
99 | EnumDecl *lookupEnum( const std::string &id ) const; |
---|
100 | /// Gets the top-most union declaration with the given ID |
---|
101 | UnionDecl *lookupUnion( const std::string &id ) const; |
---|
102 | /// Gets the top-most trait declaration with the given ID |
---|
103 | TraitDecl *lookupTrait( const std::string &id ) const; |
---|
104 | |
---|
105 | void print( std::ostream &os, int indent = 0 ) const; |
---|
106 | private: |
---|
107 | /// looks up a specific mangled ID at the given scope |
---|
108 | DeclarationWithType *lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const; |
---|
109 | /// returns true if there exists a declaration with C linkage and the given name with a different mangled name |
---|
110 | bool hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const; |
---|
111 | /// returns true if there exists a declaration with C linkage and the given name with the same mangled name |
---|
112 | bool hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const; |
---|
113 | // equivalents to lookup functions that only look at tables at scope `scope` (which should be >= tables->scope) |
---|
114 | NamedTypeDecl *lookupTypeAtScope( const std::string &id, unsigned long scope ) const; |
---|
115 | StructDecl *lookupStructAtScope( const std::string &id, unsigned long scope ) const; |
---|
116 | EnumDecl *lookupEnumAtScope( const std::string &id, unsigned long scope ) const; |
---|
117 | UnionDecl *lookupUnionAtScope( const std::string &id, unsigned long scope ) const; |
---|
118 | TraitDecl *lookupTraitAtScope( const std::string &id, unsigned long scope ) const; |
---|
119 | |
---|
120 | void addId( DeclarationWithType *decl ); |
---|
121 | void addType( NamedTypeDecl *decl ); |
---|
122 | void addStruct( const std::string &id ); |
---|
123 | void addStruct( StructDecl *decl ); |
---|
124 | void addEnum( EnumDecl *decl ); |
---|
125 | void addUnion( const std::string &id ); |
---|
126 | void addUnion( UnionDecl *decl ); |
---|
127 | void addTrait( TraitDecl *decl ); |
---|
128 | |
---|
129 | struct Impl; |
---|
130 | |
---|
131 | Impl *tables; ///< Copy-on-write instance of table data structure |
---|
132 | unsigned long scope; ///< Scope index of this pointer |
---|
133 | bool doDebug; ///< Display debugging trace? |
---|
134 | |
---|
135 | /// Takes a new ref to a table (returns null if null) |
---|
136 | static Impl *newRef( Impl *toClone ); |
---|
137 | /// Clears a ref to a table (does nothing if null) |
---|
138 | static void deleteRef( Impl *toFree ); |
---|
139 | |
---|
140 | // Removes matching autogenerated constructors and destructors |
---|
141 | // so that they will not be selected |
---|
142 | // void removeSpecialOverrides( FunctionDecl *decl ); |
---|
143 | void removeSpecialOverrides( const std::string &id, std::list< DeclarationWithType * > & out ) const; |
---|
144 | |
---|
145 | /// Ensures that tables variable is writable (i.e. allocated, uniquely owned by this Indexer, and at the current scope) |
---|
146 | void makeWritable(); |
---|
147 | }; |
---|
148 | } // namespace SymTab |
---|
149 | |
---|
150 | // Local Variables: // |
---|
151 | // tab-width: 4 // |
---|
152 | // mode: c++ // |
---|
153 | // compile-command: "make install" // |
---|
154 | // End: // |
---|