1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/**
** \file type/type-checker.hxx
** \brief Inline methods of type::TypeChecker.
*/
#pragma once
#include <ast/all.hh>
#include <type/pretty-printer.hh>
#include <type/type-checker.hh>
#include <type/types.hh>
namespace type
{
namespace
{
const Nil nil_error_instance{};
}
/*----------------.
| Setting types. |
`----------------*/
template <typename NodeType>
void TypeChecker::type_default(NodeType& e, const type::Type* type)
{
// FIXME DONE: Some code was deleted here.
// We can check here to be sure that the type we use really is a native Tiger type
const auto casted = dynamic_cast<ast::Typable*>(&e);
assertion(casted != nullptr);
if (casted->type_get() != nullptr)
{
return;
}
type_set(e, type);
}
template <typename NodeType>
void TypeChecker::created_type_default(NodeType& e, const type::Type* type)
{
// FIXME DONE: Some code was deleted here.
const auto casted = dynamic_cast<ast::TypeConstructor*>(&e);
assertion(casted != nullptr);
if (casted->created_type_get() != nullptr)
{
return;
}
casted->created_type_set(type);
}
template <typename NodeType>
void TypeChecker::type_set(NodeType& e, const type::Type* type)
{
// FIXME DONE: Some code was deleted here (Basically e.type_set(type)).
const auto casted = dynamic_cast<ast::Typable*>(&e);
assertion(casted != nullptr);
casted->type_set(type);
}
/*-----------------.
| Error handling. |
`-----------------*/
template <typename T>
void
TypeChecker::error(const ast::Ast& ast, const std::string& msg, const T& exp)
{
error_ << misc::error::error_type::type << ast.location_get() << ": " << msg
<< ": " << exp << std::endl;
}
template <typename T, typename U>
void
TypeChecker::error_and_recover(T& loc, const std::string& msg, const U& exp)
{
error(loc, msg, exp);
loc.type_set(&nil_error_instance);
}
template <typename NodeType>
void TypeChecker::check_type(NodeType& e,
const std::string& s,
const Type& t)
{
// FIXME DONE: Some code was deleted here.
const auto casted = dynamic_cast<ast::Typable*>(&e);
assertion(casted != nullptr);
if (type(*casted)->actual() == t)
return;
type_mismatch(e, s, *casted->type_get(), "expected", t);
}
/*------------.
| Functions. |
`------------*/
template <typename Routine_Type, typename Routine_Node>
void TypeChecker::visit_routine_body(Routine_Node& e)
{
// FIXME DONE: Some code was deleted here.
if (e.body_get() == nullptr)
return;
type(*e.body_get());
if (e.type_get())
{
auto rt = dynamic_cast<const Routine_Type*>(e.type_get());
check_types(e, "routine", rt->result_get(), "body", *e.body_get()->type_get());
}
else
{
created_type_default(e, e.body_get()->type_get());
}
}
} // namespace type
|