Nearly CC
An educational compiler skeleton
semantic_analysis.h
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef SEMANTIC_ANALYSIS_H
22 #define SEMANTIC_ANALYSIS_H
23 
24 #include <cstdint>
25 #include <memory>
26 #include <utility>
27 #ifdef SOLUTION
28 #include <vector>
29 #endif
30 #include "type.h"
31 #include "options.h"
32 #include "symtab.h"
33 #include "ast_visitor.h"
34 
35 class SemanticAnalysis : public ASTVisitor {
36 public:
37  typedef std::vector<SymbolTable *> SymbolTableList;
38 
39 private:
40  const Options &m_options;
41  SymbolTable *m_global_symtab, *m_cur_symtab;
42  // Keep track of SymbolTables; as the visitor creates scopes,
43  // it should append each SymbolTable object pointer to this
44  // vector
45  SymbolTableList m_all_symtabs;
46 
47 #ifdef SOLUTION
48  // result of processing a declarator
49  typedef std::pair<std::string, std::shared_ptr<Type>> Declarator;
50 #endif
51 
52 public:
53  SemanticAnalysis(const Options &options);
54  virtual ~SemanticAnalysis();
55 
56  SymbolTable *get_global_symtab() { return m_global_symtab; }
57 
58  // Iterator to access symbol tables
59  SymbolTableList::const_iterator symtab_cbegin() const { return m_all_symtabs.cbegin(); }
60  SymbolTableList::const_iterator symtab_cend() const { return m_all_symtabs.cend(); }
61 
62  virtual void visit_struct_type(Node *n);
63  virtual void visit_union_type(Node *n);
64  virtual void visit_variable_declaration(Node *n);
65  virtual void visit_basic_type(Node *n);
66  virtual void visit_function_definition(Node *n);
67  virtual void visit_function_declaration(Node *n);
68  virtual void visit_function_parameter(Node *n);
69  virtual void visit_statement_list(Node *n);
70  virtual void visit_return_expression_statement(Node *n);
71  virtual void visit_struct_type_definition(Node *n);
72  virtual void visit_binary_expression(Node *n);
73  virtual void visit_unary_expression(Node *n);
74  virtual void visit_postfix_expression(Node *n);
75  virtual void visit_conditional_expression(Node *n);
76  virtual void visit_cast_expression(Node *n);
77  virtual void visit_function_call_expression(Node *n);
78  virtual void visit_field_ref_expression(Node *n);
81  virtual void visit_variable_ref(Node *n);
82  virtual void visit_literal_value(Node *n);
83 
84 private:
85 #ifdef SOLUTION
86  std::shared_ptr<Type> create_basic_type(Node *n);
87  std::shared_ptr<Type> determine_function_type(Node *n);
88  void check_function(Node *n, const std::shared_ptr<Type> &fn_type, Symbol *prev);
89  Declarator process_declarator(Node *n, const std::shared_ptr<Type> &type);
90  void enter_scope();
91  void leave_scope();
92  bool is_assignable_from(const std::shared_ptr<Type> &lhs_type, const std::shared_ptr<Type> &rhs_type);
93  void handle_assignment(Node *n);
94  void determine_binop_result_type(Node *n);
95  bool is_conversion_needed(const std::shared_ptr<Type> &to_type, const std::shared_ptr<Type> &from_type);
96  Node *promote_to_int(Node *n);
97  Node *implicit_conversion(Node *n, const std::shared_ptr<Type> &type);
98 #else
99  // TODO: add helper functions
100 #endif
101 };
102 
103 #endif // SEMANTIC_ANALYSIS_H
Base class for AST visitors.
Definition: ast_visitor.h:7
Definition: node.h:35
Definition: options.h:47
Definition: semantic_analysis.h:35
virtual void visit_literal_value(Node *n)
Visit a Node with the AST_LITERAL_VALUE tag value.
Definition: semantic_analysis.cpp:616
virtual void visit_union_type(Node *n)
Visit a Node with the AST_UNION_TYPE tag value.
Definition: semantic_analysis.cpp:60
virtual void visit_field_ref_expression(Node *n)
Visit a Node with the AST_FIELD_REF_EXPRESSION tag value.
Definition: semantic_analysis.cpp:529
virtual void visit_statement_list(Node *n)
Visit a Node with the AST_STATEMENT_LIST tag value.
Definition: semantic_analysis.cpp:225
virtual void visit_cast_expression(Node *n)
Visit a Node with the AST_CAST_EXPRESSION tag value.
Definition: semantic_analysis.cpp:442
virtual void visit_variable_ref(Node *n)
Visit a Node with the AST_VARIABLE_REF tag value.
Definition: semantic_analysis.cpp:601
virtual void visit_basic_type(Node *n)
Visit a Node with the AST_BASIC_TYPE tag value.
Definition: semantic_analysis.cpp:102
virtual void visit_function_parameter(Node *n)
Visit a Node with the AST_FUNCTION_PARAMETER tag value.
Definition: semantic_analysis.cpp:188
virtual void visit_unary_expression(Node *n)
Visit a Node with the AST_UNARY_EXPRESSION tag value.
Definition: semantic_analysis.cpp:352
virtual void visit_struct_type_definition(Node *n)
Visit a Node with the AST_STRUCT_TYPE_DEFINITION tag value.
Definition: semantic_analysis.cpp:257
virtual void visit_indirect_field_ref_expression(Node *n)
Visit a Node with the AST_INDIRECT_FIELD_REF_EXPRESSION tag value.
Definition: semantic_analysis.cpp:552
virtual void visit_function_call_expression(Node *n)
Visit a Node with the AST_FUNCTION_CALL_EXPRESSION tag value.
Definition: semantic_analysis.cpp:457
virtual void visit_conditional_expression(Node *n)
Visit a Node with the AST_CONDITIONAL_EXPRESSION tag value.
Definition: semantic_analysis.cpp:434
virtual void visit_function_definition(Node *n)
Visit a Node with the AST_FUNCTION_DEFINITION tag value.
Definition: semantic_analysis.cpp:111
virtual void visit_postfix_expression(Node *n)
Visit a Node with the AST_POSTFIX_EXPRESSION tag value.
Definition: semantic_analysis.cpp:426
virtual void visit_array_element_ref_expression(Node *n)
Visit a Node with the AST_ARRAY_ELEMENT_REF_EXPRESSION tag value.
Definition: semantic_analysis.cpp:580
virtual void visit_binary_expression(Node *n)
Visit a Node with the AST_BINARY_EXPRESSION tag value.
Definition: semantic_analysis.cpp:298
virtual void visit_struct_type(Node *n)
Visit a Node with the AST_STRUCT_TYPE tag value.
Definition: semantic_analysis.cpp:48
virtual void visit_function_declaration(Node *n)
Visit a Node with the AST_FUNCTION_DECLARATION tag value.
Definition: semantic_analysis.cpp:167
virtual void visit_variable_declaration(Node *n)
Visit a Node with the AST_VARIABLE_DECLARATION tag value.
Definition: semantic_analysis.cpp:64
virtual void visit_return_expression_statement(Node *n)
Visit a Node with the AST_RETURN_EXPRESSION_STATEMENT tag value.
Definition: semantic_analysis.cpp:236
Definition: symtab.h:93
Definition: symbol.h:28