source: src/SymTab/Indexer.h@ fce4e31

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since fce4e31 was ef5b828, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Indexer now has const lookup by default

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