-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSemanticAnalyzer.cpp
More file actions
406 lines (395 loc) · 20.9 KB
/
SemanticAnalyzer.cpp
File metadata and controls
406 lines (395 loc) · 20.9 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "SemanticAnalyzer.h"
#include <algorithm>
#include <ranges>
bool SemanticAnalyzer::analyze() {
scope_function = &ast->function_table.equal_range("main").first->second->signature;
bool occur_errors = false;
scoped_symbol_types.emplace_back();
for (auto& [name, value] : ast->constant_table) {
scoped_symbol_types.back().emplace(name, static_cast<SymbolType>(ast->is_variable(name)));
if (!is_constant_expression(value)) {
ast->lex->source_mgr.PrintMessage(value->range.Start, llvm::SourceMgr::DK_Error,
"expression must be constant", value->range);
occur_errors = true;
}
const SymbolType value_type = get_type(value);
if (const int constant_type = ast->is_variable(name); value_type != constant_type) {
ast->lex->source_mgr.PrintMessage(value->range.Start, llvm::SourceMgr::DK_Error,
symbol_type_to_literal(value_type) + " is not " +
symbol_type_to_literal(static_cast<SymbolType>(constant_type)),
value->range);
occur_errors = true;
}
}
for (auto& [name, value] : ast->global_table) {
scoped_symbol_types.back().emplace(name, static_cast<SymbolType>(ast->is_variable(name)));
const SymbolType value_type = get_type(value);
if (const int variable_type = ast->is_variable(name); value_type != variable_type) {
ast->lex->source_mgr.PrintMessage(value->range.Start, llvm::SourceMgr::DK_Error,
symbol_type_to_literal(value_type) + " cannot convert to " +
symbol_type_to_literal(static_cast<SymbolType>(variable_type)),
value->range);
occur_errors = true;
}
}
for (auto& function : ast->function_table | std::views::values) {
scope_function = &function->signature;
scoped_symbol_types.emplace_back();
ast->scoped_symbol_table_layer.emplace_back(&function->signature->symbol_table, SYMBOL_TABLE_TYPE_FUNCTION);
const auto& symbol_table = function->signature->symbol_table;
for (const auto& [name, type] : symbol_table) {
if (is_variable_type(type)) scoped_symbol_types.back().emplace(name, type);
}
for (const auto& arg : function->signature->arguments) {
if (arg->default_value == nullptr) continue;
const SymbolType value_type = get_type(arg->default_value);
if (const int argument_type = arg->type; value_type != argument_type) {
ast->lex->source_mgr.PrintMessage(arg->default_value->range.Start, llvm::SourceMgr::DK_Error,
symbol_type_to_literal(value_type) + " cannot convert to " +
symbol_type_to_literal(static_cast<SymbolType>(argument_type)),
arg->default_value->range);
occur_errors = true;
}
}
for (auto& expr : function->body) {
try {
get_type(expr);
} catch (std::exception&) {
occur_errors = true;
}
}
ast->scoped_symbol_table_layer.pop_back();
scoped_symbol_types.pop_back();
}
return occur_errors;
}
bool SemanticAnalyzer::can_convert_to(const std::unique_ptr<ExprAST>& old_expr, const SymbolType new_type) {
const SymbolType old_type = get_type(old_expr);
if (old_type == new_type) return true;
switch (old_type) {
case SYMBOL_TYPE_VOID:
return false;
case SYMBOL_TYPE_STRING:
return new_type == SYMBOL_TYPE_STRING || new_type == SYMBOL_TYPE_POINTER;
case SYMBOL_TYPE_INT:
return new_type == SYMBOL_TYPE_FLOAT || new_type == SYMBOL_TYPE_STRING;
case SYMBOL_TYPE_FLOAT:
if (new_type == SYMBOL_TYPE_INT) {
ast->lex->source_mgr.PrintMessage(old_expr->range.Start, llvm::SourceMgr::DK_Warning,
"float to int may cause precision loss", old_expr->range);
return true;
}
return new_type == SYMBOL_TYPE_STRING;
default:
return false;
}
}
const std::unique_ptr<FunctionSignatureAST>* SemanticAnalyzer::seek_best_match_function(const CallExprAST& expr) {
auto [begin, end] = ast->function_table.equal_range(expr.name);
const std::unique_ptr<FunctionSignatureAST>* current_candidate = nullptr;
size_t current_mandatory = 0;
for (auto& it = begin; it != end; ++it) {
const size_t mandatory_args = std::ranges::count_if(it->second->signature->arguments,
[](const std::unique_ptr<FunctionArgument>& arg) {
return arg->default_value == nullptr;
});
const size_t optional_args = it->second->signature->arguments.size() - mandatory_args;
if (expr.arguments.size() >= mandatory_args && mandatory_args >= current_mandatory) {
current_candidate = &it->second->signature;
current_mandatory = mandatory_args;
}
if (expr.arguments.size() == mandatory_args + optional_args) return &it->second->signature; // best match
}
if (current_candidate == nullptr && ast->extern_function_table.contains(expr.name)) {
const auto& candidate = ast->extern_function_table.at(expr.name);
const size_t mandatory_args = std::ranges::count_if(candidate->arguments,
[](const std::unique_ptr<FunctionArgument>& arg) {
return arg->default_value == nullptr;
});
if (expr.arguments.size() >= mandatory_args && expr.arguments.size() <= candidate->arguments.size()) {
return &candidate;
}
}
return current_candidate;
}
SymbolType SemanticAnalyzer::get_type(const std::unique_ptr<ExprAST>& expr) {
if (typeid(*expr) == typeid(FloatExprAST)) {
return SYMBOL_TYPE_FLOAT;
}
if (typeid(*expr) == typeid(IntegerExprAST)) {
return SYMBOL_TYPE_INT;
}
if (typeid(*expr) == typeid(StringExprAST)) {
return SYMBOL_TYPE_STRING;
}
if (typeid(*expr) == typeid(UnaryExprAST)) {
const auto& call = dynamic_cast<UnaryExprAST&>(*expr);
switch (call.op) {
case '&': {
const auto& ident = dynamic_cast<VariableExprAST&>(*call.expr);
// TODO variable
if (!ast->scoped_symbol_table_layer.front().symbol_table->contains(ident.name)) {
ast->lex->source_mgr.PrintMessage(ident.range.Start, llvm::SourceMgr::DK_Error,
"unknown function name", ident.range);
throw std::exception();
}
const auto candidates = ast->function_table.equal_range(ident.name);
if (std::distance(candidates.first, candidates.second) > 1)
ast->lex->source_mgr.PrintMessage(call.range.Start, llvm::SourceMgr::DK_Warning,
"retrieving function pointer which has overloading", call.range);
return SYMBOL_TYPE_POINTER;
}
case '-':
case TOKEN_LOGIC_NOT: {
const SymbolType type = get_type(call.expr);
if (type == SYMBOL_TYPE_STRING) {
ast->lex->source_mgr.PrintMessage(call.range.Start, llvm::SourceMgr::DK_Error,
"unary operator cannot apply to the expression", call.range);
throw std::exception();
}
return call.op == TOKEN_LOGIC_NOT ? SYMBOL_TYPE_INT : type;
}
default:
ast->lex->source_mgr.PrintMessage(call.range.Start, llvm::SourceMgr::DK_Error,
"unknown unary operator", call.range);
throw std::exception();
}
}
if (typeid(*expr) == typeid(CallExprAST)) {
const auto& call = dynamic_cast<CallExprAST&>(*expr);
const auto candidate = seek_best_match_function(call);
if (candidate != nullptr) {
for (int i = 0; i < call.arguments.size(); i++) {
const SymbolType new_type = (*candidate)->arguments.at(i)->type;
if (!can_convert_to(call.arguments.at(i), new_type)) {
ast->lex->source_mgr.PrintMessage(call.arguments.at(i)->range.Start, llvm::SourceMgr::DK_Error,
"cannot convert " + symbol_type_to_literal(
get_type(call.arguments.at(i))) + " to " +
symbol_type_to_literal(new_type), call.arguments.at(i)->range);
throw std::exception();
}
}
return (*candidate)->return_value_type;
}
ast->lex->source_mgr.PrintMessage(call.range.Start, llvm::SourceMgr::DK_Error,
"no function that matches the requirement", call.range);
throw std::exception();
}
if (typeid(*expr) == typeid(BinaryExprAST)) {
auto& bi_expr = dynamic_cast<BinaryExprAST&>(*expr);
const SymbolType lhs_type = get_type(bi_expr.lhs);
const SymbolType rhs_type = get_type(bi_expr.rhs);
if (bi_expr.op == '=') {
if (typeid(*bi_expr.lhs) == typeid(VariableExprAST)) {
const auto& var = dynamic_cast<VariableExprAST&>(*bi_expr.lhs);
if (ast->constant_table.contains(var.name)) {
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Error,
"cannot realloc constant value", bi_expr.range);
throw std::exception();
}
}
if (rhs_type != SYMBOL_TYPE_VOID) {
switch (lhs_type) {
case SYMBOL_TYPE_INT:
if (rhs_type == SYMBOL_TYPE_POINTER) {
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Warning,
"assigning pointer to a integer variable is deprecated",
bi_expr.range, {
llvm::SMFixIt(
bi_expr.op_loc, "assigning to a pointer variable")
});
return SYMBOL_TYPE_POINTER;
}
if (rhs_type == SYMBOL_TYPE_FLOAT) {
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Warning,
"float to int may cause precision loss", bi_expr.range);
}
case SYMBOL_TYPE_FLOAT:
if (rhs_type != SYMBOL_TYPE_STRING) return lhs_type;
break;
case SYMBOL_TYPE_STRING:
if (rhs_type == SYMBOL_TYPE_POINTER) {
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Warning,
"assigning a pointer to a string variable, please use @ for pointer type instead",
bi_expr.range);
}
return lhs_type;
case SYMBOL_TYPE_POINTER:
if (rhs_type == SYMBOL_TYPE_POINTER || rhs_type == SYMBOL_TYPE_STRING) {
if (rhs_type == SYMBOL_TYPE_STRING)
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Warning,
"assigning a string to a pointer variable. lifecycle of string is managed by ZiYue4D, the pointer may be a wild pointer",
bi_expr.range);
return lhs_type;
}
}
}
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Error,
"cannot convert " + symbol_type_to_literal(lhs_type) + " to " +
symbol_type_to_literal(rhs_type), bi_expr.range);
throw std::exception();
}
if (lhs_type == SYMBOL_TYPE_STRING || rhs_type == SYMBOL_TYPE_STRING) {
if (bi_expr.op == '+') return SYMBOL_TYPE_STRING;
if (bi_expr.op == TOKEN_EQUALS) return SYMBOL_TYPE_INT;
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Error,
"invalid operator for string", bi_expr.range);
throw std::exception();
}
if (lhs_type == SYMBOL_TYPE_FLOAT || rhs_type == SYMBOL_TYPE_FLOAT) {
if (is_relational_operator(bi_expr.op)) return SYMBOL_TYPE_INT;
if (is_bitwise_or_logic_operator(bi_expr.op)) {
ast->lex->source_mgr.PrintMessage(bi_expr.op_loc, llvm::SourceMgr::DK_Warning,
"float to int may cause precision loss", bi_expr.range);
return SYMBOL_TYPE_INT;
}
return SYMBOL_TYPE_FLOAT;
}
return SYMBOL_TYPE_INT;
}
if (typeid(*expr) == typeid(VariableExprAST)) {
const auto& var = dynamic_cast<VariableExprAST&>(*expr);
for (const auto& scoped_symbol_table : std::ranges::reverse_view(scoped_symbol_types)) {
if (scoped_symbol_table.contains(var.name)) return scoped_symbol_table.at(var.name);
}
ast->lex->source_mgr.PrintMessage(var.range.Start, llvm::SourceMgr::DK_Error,
"unknown variable " + var.name, var.range);
throw std::exception();
}
if (typeid(*expr) == typeid(ReturnExprAST)) {
const auto& ret = dynamic_cast<ReturnExprAST&>(*expr);
if (ret.expr == nullptr) return (*scope_function)->return_value_type;
const SymbolType type = get_type(ret.expr);
if (!can_convert_to(ret.expr, (*scope_function)->return_value_type)) {
ast->lex->source_mgr.PrintMessage(ret.expr->range.Start, llvm::SourceMgr::DK_Error,
"cannot convert " + symbol_type_to_literal(type) + " to " +
symbol_type_to_literal((*scope_function)->return_value_type),
ret.expr->range);
throw std::exception();
}
return type;
}
if (typeid(*expr) == typeid(IfStatementAST)) {
auto& if_statement = dynamic_cast<IfStatementAST&>(*expr);
if (!can_convert_to(if_statement.condition, SYMBOL_TYPE_INT)) {
ast->lex->source_mgr.PrintMessage(if_statement.condition->range.Start, llvm::SourceMgr::DK_Error,
"if condition statement must be integer", if_statement.condition->range);
throw std::exception();
}
// statement true
{
scoped_symbol_types.emplace_back();
auto& symbol_table = if_statement.statement_true_symbol_table;
ast->scoped_symbol_table_layer.emplace_back(&symbol_table, SYMBOL_TABLE_TYPE_BRANCH);
for (const auto& [name, type] : symbol_table) {
if (is_variable_type(type)) scoped_symbol_types.back().emplace(name, type);
}
for (auto& true_expr : if_statement.statement_true) {
get_type(true_expr);
}
ast->scoped_symbol_table_layer.pop_back();
scoped_symbol_types.pop_back();
}
// statement false
{
scoped_symbol_types.emplace_back();
auto& symbol_table = if_statement.statement_false_symbol_table;
ast->scoped_symbol_table_layer.emplace_back(&symbol_table, SYMBOL_TABLE_TYPE_BRANCH);
for (
const auto& [name, type] : symbol_table) {
if (is_variable_type(type)) scoped_symbol_types.back().emplace(name, type);
}
for (auto& false_expr : if_statement.statement_false) {
get_type(false_expr);
}
ast->scoped_symbol_table_layer.pop_back();
scoped_symbol_types.pop_back();
}
return SYMBOL_TYPE_VOID;
}
if (typeid(*expr) == typeid(WhileStatementAST)) {
auto& while_statement = dynamic_cast<WhileStatementAST&>(*expr);
if (!can_convert_to(while_statement.condition, SYMBOL_TYPE_INT)) {
ast->lex->source_mgr.PrintMessage(while_statement.condition->range.Start, llvm::SourceMgr::DK_Error,
"while condition statement must be integer",
while_statement.condition->range);
throw std::exception();
}
scoped_symbol_types.emplace_back();
auto& symbol_table = while_statement.statement_true_symbol_table;
ast->scoped_symbol_table_layer.emplace_back(&symbol_table, SYMBOL_TABLE_TYPE_LOOP);
for (const auto& [name, type] : symbol_table) {
if (is_variable_type(type)) scoped_symbol_types.back().emplace(name, type);
}
for (auto& true_expr : while_statement.statement_true) {
get_type(true_expr);
}
ast->scoped_symbol_table_layer.pop_back();
scoped_symbol_types.pop_back();
return SYMBOL_TYPE_VOID;
}
if (typeid(*expr) == typeid(ExitAST) || typeid(*expr) == typeid(ContinueAST)) return SYMBOL_TYPE_VOID;
ast->lex->source_mgr.PrintMessage(expr->range.Start, llvm::SourceMgr::DK_Error,
"unknown expression! is this a compiler bug?", expr->range);
throw std::exception();
}
bool SemanticAnalyzer::is_constant_expression(const std::unique_ptr<ExprAST>& expr) {
if (typeid(*expr) == typeid(FloatExprAST) || typeid(*expr) == typeid(IntegerExprAST) ||
typeid(*expr) == typeid(StringExprAST)) {
return true;
}
if (typeid(*expr) == typeid(UnaryExprAST)) {
const auto& call = dynamic_cast<UnaryExprAST&>(*expr);
switch (call.op) {
case '&':
return false;
case '-':
case TOKEN_LOGIC_NOT:
return is_constant_expression(call.expr);
}
}
if (typeid(*expr) == typeid(CallExprAST)) {
return false;
}
if (typeid(*expr) == typeid(BinaryExprAST)) {
const auto& biexpr = dynamic_cast<BinaryExprAST&>(*expr);
return is_constant_expression(biexpr.lhs) && is_constant_expression(biexpr.rhs);
}
if (typeid(*expr) == typeid(VariableExprAST)) {
const auto& var = dynamic_cast<VariableExprAST&>(*expr);
return ast->constant_table.contains(var.name);
}
return false;
}
bool SemanticAnalyzer::is_relational_operator(int token) {
return token == TOKEN_NOT_EQUALS || token == TOKEN_EQUALS || token == TOKEN_LESS_THAN ||
token == TOKEN_LESS_THAN_OR_EQUALS || token == TOKEN_GREATER_THAN || token == TOKEN_GREATER_THAN_OR_EQUALS;
}
bool SemanticAnalyzer::is_bitwise_or_logic_operator(int token) {
return token == '&' || token == TOKEN_BITWISE_OR || token == TOKEN_LOGIC_OR ||
token == TOKEN_LOGIC_AND || token == TOKEN_LOGIC_NOT;
}
std::string SemanticAnalyzer::readable_function_signature(const std::unique_ptr<FunctionSignatureAST>& signature) {
std::string result = signature->name + '(';
for (const auto& arg : signature->arguments) {
result += arg->name;
switch (arg->type) {
case SYMBOL_TYPE_INT: result += '%';
break;
case SYMBOL_TYPE_FLOAT: result += '#';
break;
case SYMBOL_TYPE_STRING: result += '$';
break;
case SYMBOL_TYPE_POINTER: result += '@';
break;
default: break;
}
result += ',';
}
if (!signature->arguments.empty()) result.pop_back();
result.push_back(')');
return result;
}
std::string SemanticAnalyzer::readable_function_signature(const std::unique_ptr<FunctionAST>& function) {
return std::move(readable_function_signature(function->signature));
}