Tree Decoration¶

When traversing the tree for type checking, we may need to store information about a node in the tree that will be used later, either in type cheking, or in code generation.

Associating information to certaing nodes in the parse tree is called decorating the tree.

For example, an aritmethic expression (e.g. a sum) will have a type that will depend on the type of its operands (that may be other expressions, and in turn depend on the type of their operands…). Since expressions are not symbols, their type is not stored in the Symbol Table. Thus, when we climb the tree, we need to compute the type for each subexpression to allow nodes higher in the tree perform the checks they may need.

Class TreeDecoration stores information associated to tree nodes, and offers methods to retrieve it. Information that our compiler will need to keep about nodes is:

  • The type of the node (e.g. for nodes corresponding to expressions).

  • Whether the node is an l-value (e.g. it is at the left-hand side of an assignment).

  • The scope to which the node belongs.

(note than not all nodes will be decorated with all this information. Some node will not be decorated at all)

Nodes are represented as antlr4::ParserRuleContext *, that is, pointers to nodes in the ParseTree built by ANTLR using our grammar rules.

 1//////////////////////////////////////////////////////////////////////
 2// Class TreeDecoration: the nodes of the parser tree generated
 3// by the antlr4 parser, whose base type is
 4// antlr4::ParserRuleContext *, can have different attributes.
 5// TreeDecoration groups all of them, and uses different
 6// ParseTreeProperty to save this information.
 7// Currently three kinds of attributes may be present:
 8//   - scope, for nodes like the program, or functions
 9//   - type, for expressions or type especification
10//   - isLValue, for expressions
11// Different visitors set and access these attributes:
12//   - SymbolsVisitor     [TypeCheck phase 1]
13//       * set and access the scope attribute
14//       * set and access the type attribute (in type declarations)
15//   - TypeCheckVisitor   [TypeCheck phase 2]
16//       * access the scope attribute
17//       * set and access the type attribute (in expressions)
18//       * set and access the isLValue attribute (in expressions)
19//   - CodeGenVisitor     [Code Generation]
20//       * access the scope attribute
21//       * access the type attribute
22
23class TreeDecoration {
24
25public:
26  TreeDecoration() = default;
27
28  // Getters:
29  SymTable::ScopeId getScope    (antlr4::ParserRuleContext *ctx);
30  TypesMgr::TypeId  getType     (antlr4::ParserRuleContext *ctx);
31  bool              getIsLValue (antlr4::ParserRuleContext *ctx);
32
33  // Setters:
34  void putScope    (antlr4::ParserRuleContext *ctx, SymTable::ScopeId s);
35  void putType     (antlr4::ParserRuleContext *ctx, TypesMgr::TypeId t);
36  void putIsLValue (antlr4::ParserRuleContext *ctx, bool b);
37
38};  // class TreeDecoration