1 #ifndef SYMBOL_ENVIRONMENT_H 2 #define SYMBOL_ENVIRONMENT_H 3 4 #include <string> 5 #include <vector> 6 //#include <ext/hash_map> 7 #include <map> 8 9 #include <FPG/Symbol.h> 10 11 namespace AST { 12 struct FunctionDefinition; 13 struct Node; 14 } 15 16 struct Collect_variables; 17 18 19 struct SymbolEnvironment { 20 SymbolEnvironment(); 21 22 // corresponds to compound blocks. 23 void enterBlock(); 24 void leaveBlock(); 25 26 // add item to current block. 27 void add( Variable* var ); 28 void add( Type* type ); 29 // only allowed on global level <=> env.size() == 1 30 void add( FunctionType* function ); 31 void add_fun_def( AST::FunctionDefinition* fun_def ); 32 33 // only when leaving a block, symbols are removed. so, no need for remove methods. 34 35 // if symbol not found, nullptr is returned 36 Variable* findVariable( const std::string& id ); 37 // if an alias type is registered that refers to t, t itself is returned (not the alias) 38 // alias types cannot refer to another alias type, so the type returned here is a proper type 39 Type* findType( const std::string& id ); 40 FunctionType* findFunction( const std::string& id, unsigned int arity ); 41 // returns true if there is at least one fun for id 42 bool hasFunction( const std::string& id ); 43 AST::FunctionDefinition* findFunctionDef( const std::string& id, unsigned int arity ); 44 AST::FunctionDefinition* findFunctionDef( FunctionType *fun_type ); numberOfBlocksSymbolEnvironment45 int numberOfBlocks() const { return int(env.size()); } 46 47 Block* getCurrentBlock() ; 48 49 // in AST::FunctionCall* funcall, set called_function to the AST::FunctionDefinition* under given 50 // symbol environment 51 void resolve_function_calls( AST::Node *n ); 52 std::string make_fresh_function_name( const std::string& prefix ); 53 void clear(); 54 // needed for function type declarations, where the variable names can be different 55 // from the actual function definition. overwritten, as soon the definition is seen 56 void replace_function_type( FunctionType* t ); 57 58 void rename_function( FunctionType* t, std::string new_id ); 59 protected: 60 std::vector< Block* > env; 61 Functions functions; 62 63 typedef std::map< FunctionType*, AST::FunctionDefinition*> Function_definitions; 64 Function_definitions function_definitions; 65 }; 66 67 extern SymbolEnvironment symbol_env; 68 extern std::string random_identifier( unsigned int size = 7 ); 69 70 #endif 71