LRDE Tiger Compiler  1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
type-checker.hh
Go to the documentation of this file.
1 
6 #ifndef TYPE_TYPE_CHECKER_HH
7 # define TYPE_TYPE_CHECKER_HH
8 
9 # include <string>
10 # include <ast/default-visitor.hh>
11 # include <ast/non-object-visitor.hh>
12 
13 # include <misc/set.hh>
14 # include <misc/error.hh>
15 # include <type/fwd.hh>
16 
17 namespace type
18 {
19 
21  {
22  public:
24  using super_type::operator();
25 
26  public:
28  TypeChecker();
30  const misc::error& error_get() const;
31 
32  protected:
38  const Type* type(ast::Typable& e);
39  const Record* type(const ast::fields_type& e);
40  const Record* type(const ast::VarDecs& e);
41 
42 
43  // ------------------ //
44  // Helping routines. //
45  // ------------------ //
46  protected:
49 
51  void error(const ast::Ast& loc, const std::string& msg);
52 
58  template <typename T>
59  void error(const ast::Ast& loc, const std::string& msg, const T& exp);
60 
68  template <typename T, typename U>
69  void error_and_recover(T& loc, const std::string& msg, const U& exp);
70 
77  void type_mismatch(const ast::Ast& loc,
78  const std::string& exp1, const Type& type1,
79  const std::string& exp2, const Type& type2);
81 
82 
85 
94  void check_type(ast::Exp& e, const std::string& s, const Type& t);
95  void check_type(ast::Dec& e, const std::string& s, const Type& t);
96 
108  void check_types(const ast::Ast& loc,
109  const std::string& exp1, const Type& type1,
110  const std::string& exp2, const Type& type2);
111  void check_types(const ast::Ast& loc,
112  const std::string& exp1, ast::Typable& type1,
113  const std::string& exp2, ast::Typable& type2);
115 
116  protected:
119 
125  template <typename NodeType>
126  void type_default(NodeType& e, const type::Type* type);
127 
129  template <typename NodeType>
130  void created_type_default(NodeType& e, const type::Type* type);
131 
136  template <typename NodeType>
137  void type_set(NodeType& e, const type::Type* type);
139 
140  // ------------------------- //
141  // The core of the visitor. //
142  // ------------------------- //
143 
144  // ---------------- //
145  // Visiting /Var/. //
146  // ---------------- //
147 
148  virtual void operator()(ast::SimpleVar& e) override;
149  // FIXME: Some code was deleted here (Other Var nodes).
150 
151 
152 
153  // ---------------- //
154  // Visiting /Exp/. //
155  // ---------------- //
156 
157  // Literals.
158  virtual void operator()(ast::NilExp&) override;
159  virtual void operator()(ast::IntExp&) override;
160  virtual void operator()(ast::StringExp&) override;
161 
162  // Complex values.
163  virtual void operator()(ast::RecordExp& e) override;
164  virtual void operator()(ast::OpExp& e) override;
165  // FIXME: Some code was deleted here (Other Exp nodes).
166 
167  // ---------------- //
168  // Visiting /Dec/. //
169  // ---------------- //
170 
173 
180 
185 
188  template <class D>
189  void decs_visit(ast::AnyDecs<D>& e);
190 
192  template <class D>
193  void visit_dec_header(D& e);
194 
196  template <class D>
197  void visit_dec_body(D& e);
198 
200  template <typename Routine_Type, typename Routine_Node>
201  void visit_routine_body(Routine_Node& e);
202 
204  virtual void operator()(ast::FunctionDecs& e) override;
206  virtual void operator()(ast::FunctionDec&) override;
207 
209  virtual void operator()(ast::TypeDecs& e) override;
211  virtual void operator()(ast::TypeDec&) override;
213 
215  virtual void operator()(ast::VarDec& e) override;
216 
217  // --------------- //
218  // Visiting /Ty/. //
219  // --------------- //
220 
221  virtual void operator()(ast::NameTy& e) override;
222  virtual void operator()(ast::RecordTy& e) override;
223  virtual void operator()(ast::ArrayTy& e) override;
224 
225  protected:
230  };
231 
233  template <>
234  void TypeChecker::visit_dec_header<ast::FunctionDec>(ast::FunctionDec& e);
236  template <>
237  void TypeChecker::visit_dec_body<ast::FunctionDec>(ast::FunctionDec& e);
238 
239 
241  template <>
242  void TypeChecker::visit_dec_header<ast::TypeDec>(ast::TypeDec& e);
244  template <>
245  void TypeChecker::visit_dec_body<ast::TypeDec>(ast::TypeDec& e);
246 }
247 
248 # include <type/type-checker.hxx>
249 
250 #endif // !TYPE_TYPE_CHECKER_HH