SymbolTableΒΆ

The symbol table stores which symbols have been defined in the program (in ASL, that is just variable and function names) and associates each symbol with a type in TypeMgr.

The symbol table is also able to handle visibility scopes, so we can have separate tables for separate functions, or stacked tables for nested scopes.

Thus, class SymTable stores which symbols have been defined in each scope, and we can query it about which is the type of symbol, whether a symbol is defined in the current function, or (if it is defined twice because there is a nested scope) which is the definition we should use at each moment.

 1////////////////////////////////////////////////////////////////
 2// Class SymTable: stores the symbols declared in the program
 3// along with the information associated with each one:
 4//   - its class (the symbol can be a function, a parameter or
 5//     a local variable)
 6//   - its type (the TypeId returned by the TypesMgr)
 7// The symbols are grouped in scopes. In the current version
 8// of Asl there are two level of scopes: the global and the
 9// local. The former for the symbols of function names, and
10// the latter for symbols declared inside a function:
11// parameters and local variables.
12// The SymTable uses a 'stack' to keep the current available
13// scopes that determines which symbols are visible and
14// which are not. Entering in a function will push a new
15// scope to the stack and exiting will pop the stack.
16
17class SymTable {
18
19public:
20
21  // The ScopeId is an index in a vector
22  typedef std::size_t ScopeId;
23
24  // Name of the Global Scope
25  static const std::string GLOBAL_SCOPE_NAME;
26
27  // Constructor
28  SymTable(TypesMgr & Types);
29  // Destructor
30  ~SymTable() = default;
31
32  // Manage the stack of scopes
33  //   - create a new empty scope and push its ScopeId in the stack
34  ScopeId pushNewScope  (const std::string & name);
35  //   - pop the stack of scopes
36  void    popScope      ();
37  //   - push a previously created scope sc and set it as current scope
38  void    pushThisScope (ScopeId sc);
39  //   - returns the current scope
40  ScopeId topScope      ()                          const;
41
42  // Methods to find an ident
43  //   - in the current scope (top of the stack)
44  bool    findInCurrentScope (const std::string & ident)             const;
45  //   - in the whole stack. Returns the number of scopes skipped to
46                          // find the symbol, or -1 if it is not found
47  int     findInStack        (const std::string & ident)             const;
48
49  // Adds a new symbol in the current scope
50  void addLocalVar  (const std::string & ident, TypesMgr::TypeId type);
51  void addParameter (const std::string & ident, TypesMgr::TypeId type);
52  void addFunction  (const std::string & ident, TypesMgr::TypeId type);
53
54  // Accessors to check the class of the symbol. If not found return false
55  bool isLocalVarClass  (const std::string & ident) const;
56  bool isParameterClass (const std::string & ident) const;
57  bool isFunctionClass  (const std::string & ident) const;
58
59  // Accessor to get the TypeId of a symbol. If not found return type 'error'
60  TypesMgr::TypeId getType (const std::string & ident) const;
61
62  // Check the existence of the "main" function
63  bool noMainProperlyDeclared() const;
64
65  // Given the name of a function, returns its TypeId
66  TypesMgr::TypeId getGlobalFunctionType (const std::string & ident) const;
67  // Given the names of a function and a local symbol, returns its TypeId
68  TypesMgr::TypeId getLocalSymbolType    (const std::string & funcName,
69                                          const std::string & ident) const;
70
71  // Print the symbols of a scope on the standard output
72  //   - the symbols of the current scope (top of the stack)
73  void printCurrentScope () const;
74  //   - the symbols of the whole stack
75  void print             () const;
76
77};  // class SymTable