source: src/SymTab/Indexer.h @ 42f1279c

arm-ehcleanup-dtorsjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexer
Last change on this file since 42f1279c was 42f1279c, checked in by Aaron Moss <a3moss@…>, 3 years ago

Eagerly remove over-ridden generated functions

  • Property mode set to 100644
File size: 7.7 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                NamedTypeDecl *lookupType( const std::string &id ) const;
66                /// Gets the top-most struct declaration with the given ID
67                StructDecl *lookupStruct( const std::string &id ) const;
68                /// Gets the top-most enum declaration with the given ID
69                EnumDecl *lookupEnum( const std::string &id ) const;
70                /// Gets the top-most union declaration with the given ID
71                UnionDecl *lookupUnion( const std::string &id ) const;
72                /// Gets the top-most trait declaration with the given ID
73                TraitDecl *lookupTrait( const std::string &id ) const;
74
75                /// Gets the type declaration with the given ID at global scope
76                NamedTypeDecl *globalLookupType( const std::string &id ) const;
77                /// Gets the struct declaration with the given ID at global scope
78                StructDecl *globalLookupStruct( const std::string &id ) const;
79                /// Gets the union declaration with the given ID at global scope
80                UnionDecl *globalLookupUnion( const std::string &id ) const;
81                /// Gets the enum declaration with the given ID at global scope
82                EnumDecl *globalLookupEnum( const std::string &id ) const;
83
84                void addId( DeclarationWithType * decl, Expression * baseExpr = nullptr );
85                void addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt );
86
87                void addType( NamedTypeDecl *decl );
88                void addStruct( const std::string &id );
89                void addStruct( StructDecl *decl );
90                void addEnum( EnumDecl *decl );
91                void addUnion( const std::string &id );
92                void addUnion( UnionDecl *decl );
93                void addTrait( TraitDecl *decl );
94
95                /// adds all of the IDs from WithStmt exprs
96                void addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt );
97
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
105                void addFunctionType( FunctionType * ftype );
106
107          private:
108                /// Wraps a Decl* with a scope
109                template<typename Decl>
110                struct Scoped {
111                        Decl* decl;           ///< declaration
112                        unsigned long scope;  ///< scope of this declaration
113
114                        Scoped(Decl* d, unsigned long s) : decl(d), scope(s) {}
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> >;
126
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
133
134                Ptr prevScope;                 ///< reference to indexer for parent scope
135                unsigned long scope;           ///< Scope index of this indexer
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();
140
141                /// Gets the indexer at the given scope
142                const Indexer* atScope( unsigned long scope ) const;
143
144                /// Removes matching autogenerated constructors and destructors
145                /// so that they will not be selected
146                void removeSpecialOverrides( const std::string &id, std::list< IdData > & out ) const;
147
148                /// Removes matching autogenerated constructors and destructors so that they will not be
149                /// selected. If returns false, passed decl should not be added.
150                bool removeSpecialOverrides( IdData& decl, MangleTable::Ptr& mangleTable );
151
152                /// Options for handling identifier conflicts
153                struct OnConflict {
154                        enum {
155                                Error,  ///< Throw a semantic error
156                                Delete  ///< Delete the earlier version with the delete statement
157                        } mode;
158                        BaseSyntaxNode * deleteStmt;  ///< Statement that deletes this expression
159
160                private:
161                        OnConflict() : mode(Error), deleteStmt(nullptr) {}
162                        OnConflict( BaseSyntaxNode * d ) : mode(Delete), deleteStmt(d) {}
163                public:
164                        OnConflict( const OnConflict& ) = default;
165
166                        static OnConflict error() { return {}; }
167                        static OnConflict deleteWith( BaseSyntaxNode * d ) { return { d }; }
168                };
169
170                /// true if the existing identifier conflicts with the added identifier
171                bool addedIdConflicts(
172                        const IdData& existing, DeclarationWithType * added, OnConflict handleConflicts, 
173                        BaseSyntaxNode * deleteStmt );
174
175                /// common code for addId, addDeletedId, etc.
176                void addId( 
177                        DeclarationWithType * decl, OnConflict handleConflicts, 
178                        Expression * baseExpr = nullptr, BaseSyntaxNode * deleteStmt = nullptr );
179
180                /// adds all of the members of the Aggregate (addWith helper)
181                void addMembers( AggregateDecl * aggr, Expression * expr, OnConflict handleConflicts );
182
183                /// returns true if there exists a declaration with C linkage and the given name with the same mangled name
184                bool hasCompatibleCDecl( const std::string &id, const std::string &mangleName ) const;
185                /// returns true if there exists a declaration with C linkage and the given name with a different mangled name
186                bool hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const;
187        };
188} // namespace SymTab
189
190// Local Variables: //
191// tab-width: 4 //
192// mode: c++ //
193// compile-command: "make install" //
194// End: //
Note: See TracBrowser for help on using the repository browser.