source: src/SymTab/Indexer.h@ 43e0949

no_list
Last change on this file since 43e0949 was 43e0949, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

more lists converted to vectors

  • Property mode set to 100644
File size: 6.5 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 : 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#include <functional> // for function
22
23#include "SynTree/Type.h" // for AST nodes
24#include "SynTree/SynTree.h" // for AST nodes
25#include "SynTree/Visitor.h" // for Visitor
26
27namespace ResolvExpr {
28class Cost;
29}
30
31namespace SymTab {
32 class Indexer {
33 public:
34 explicit Indexer();
35
36 Indexer( const Indexer &that );
37 Indexer( Indexer &&that );
38 virtual ~Indexer();
39 Indexer& operator= ( const Indexer &that );
40 Indexer& operator= ( Indexer &&that );
41
42 // when using an indexer manually (e.g., within a mutator traversal), it is necessary to tell the indexer
43 // explicitly when scopes begin and end
44 void enterScope();
45 void leaveScope();
46
47 struct IdData {
48 DeclarationWithType * id = nullptr;
49 Expression * baseExpr = nullptr; // WithExpr
50
51 /// non-null if this declaration is deleted
52 BaseSyntaxNode * deleteStmt = nullptr;
53
54 // NOTE: shouldn't need either of these constructors, but gcc-4 does not properly support initializer lists with default members.
55 IdData() = default;
56 IdData( DeclarationWithType * id, Expression * baseExpr, BaseSyntaxNode * deleteStmt ) : id( id ), baseExpr( baseExpr ), deleteStmt( deleteStmt ) {}
57
58 Expression * combine( ResolvExpr::Cost & cost ) const;
59 };
60
61 /// Gets all declarations with the given ID
62 void lookupId( const std::string &id, std::vector< IdData > &out ) const;
63 /// Gets the top-most type declaration with the given ID
64 NamedTypeDecl *lookupType( const std::string &id ) const;
65 /// Gets the top-most struct declaration with the given ID
66 StructDecl *lookupStruct( const std::string &id ) const;
67 /// Gets the top-most enum declaration with the given ID
68 EnumDecl *lookupEnum( const std::string &id ) const;
69 /// Gets the top-most union declaration with the given ID
70 UnionDecl *lookupUnion( const std::string &id ) const;
71 /// Gets the top-most trait declaration with the given ID
72 TraitDecl *lookupTrait( const std::string &id ) const;
73
74 /// Gets the type declaration with the given ID at global scope
75 NamedTypeDecl *globalLookupType( const std::string &id ) const;
76 /// Gets the struct declaration with the given ID at global scope
77 StructDecl *globalLookupStruct( const std::string &id ) const;
78 /// Gets the union declaration with the given ID at global scope
79 UnionDecl *globalLookupUnion( const std::string &id ) const;
80 /// Gets the enum declaration with the given ID at global scope
81 EnumDecl *globalLookupEnum( const std::string &id ) const;
82
83 void print( std::ostream &os, int indent = 0 ) const;
84
85 /// looks up a specific mangled ID at the given scope
86 IdData * lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope );
87 const IdData * lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const;
88 /// returns true if there exists a declaration with C linkage and the given name with a different mangled name
89 bool hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const;
90 /// returns true if there exists a declaration with C linkage and the given name with the same mangled name
91 bool hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const;
92 // equivalents to lookup functions that only look at tables at scope `scope` (which should be >= tables->scope)
93 NamedTypeDecl *lookupTypeAtScope( const std::string &id, unsigned long scope ) const;
94 StructDecl *lookupStructAtScope( const std::string &id, unsigned long scope ) const;
95 EnumDecl *lookupEnumAtScope( const std::string &id, unsigned long scope ) const;
96 UnionDecl *lookupUnionAtScope( const std::string &id, unsigned long scope ) const;
97 TraitDecl *lookupTraitAtScope( const std::string &id, unsigned long scope ) const;
98
99 typedef std::function<bool(IdData &, const std::string &)> ConflictFunction;
100
101 void addId( DeclarationWithType * decl, Expression * baseExpr = nullptr );
102 void addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt );
103
104 void addType( NamedTypeDecl *decl );
105 void addStruct( const std::string &id );
106 void addStruct( StructDecl *decl );
107 void addEnum( EnumDecl *decl );
108 void addUnion( const std::string &id );
109 void addUnion( UnionDecl *decl );
110 void addTrait( TraitDecl *decl );
111
112 /// adds all of the IDs from WithStmt exprs
113 void addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt );
114
115 /// adds all of the members of the Aggregate (addWith helper)
116 void addMembers( AggregateDecl * aggr, Expression * expr, ConflictFunction );
117
118 /// convenience function for adding a list of Ids to the indexer
119 void addIds( const std::list< DeclarationWithType * > & decls );
120
121 /// convenience function for adding a list of forall parameters to the indexer
122 void addTypes( const Type::ForallList & tds );
123
124 /// convenience function for adding all of the declarations in a function type to the indexer
125 void addFunctionType( FunctionType * ftype );
126
127 bool doDebug = false; ///< Display debugging trace?
128 private:
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
134 /// Takes a new ref to a table (returns null if null)
135 static Impl *newRef( Impl *toClone );
136 /// Clears a ref to a table (does nothing if null)
137 static void deleteRef( Impl *toFree );
138
139 // Removes matching autogenerated constructors and destructors
140 // so that they will not be selected
141 // void removeSpecialOverrides( FunctionDecl *decl );
142 void removeSpecialOverrides( const std::string &id, std::vector< IdData > & out ) const;
143
144 /// Ensures that tables variable is writable (i.e. allocated, uniquely owned by this Indexer, and at the current scope)
145 void makeWritable();
146
147 /// common code for addId, addDeletedId, etc.
148 void addId( DeclarationWithType * decl, ConflictFunction, Expression * baseExpr = nullptr, BaseSyntaxNode * deleteStmt = nullptr );
149 };
150} // namespace SymTab
151
152// Local Variables: //
153// tab-width: 4 //
154// mode: c++ //
155// compile-command: "make install" //
156// End: //
Note: See TracBrowser for help on using the repository browser.