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