-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethodWithoutParametersNode.cpp
49 lines (41 loc) · 1.6 KB
/
MethodWithoutParametersNode.cpp
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
#include "MethodWithoutParametersNode.hpp"
bool MethodWithoutParametersNode::buildTable(SymbolTable &st) const {
if (st.lookupMethod(id->value)) {
std::cerr << "Error: (line " << lineno << ") Method '" << id->value
<< "' already declared.\n";
return false;
}
st.addMethod(type->value, id->value);
auto *currentMethod = st.lookupMethod(id->value);
auto *currentClass = dynamic_cast<Class *>(st.getCurrentRecord());
currentClass->addMethod(currentMethod);
st.enterMethodScope(currentMethod);
body->buildTable(st);
st.exitScope();
return true;
}
std::string MethodWithoutParametersNode::checkTypes(SymbolTable &st) const {
st.enterMethodScope(id->value);
const auto signatureReturnType = type->checkTypes(st);
const auto bodyReturnType = body->checkTypes(st);
st.exitScope();
if (bodyReturnType.empty()) {
return "";
}
if (signatureReturnType != bodyReturnType) {
std::cerr << "Error: (line " << lineno << ") Return type '"
<< signatureReturnType << "' in method '" << id->value
<< "' does not match returned type '" << bodyReturnType
<< "'.\n";
}
return type->value;
}
Operand MethodWithoutParametersNode::generateIR(CFG &graph, SymbolTable &st) {
auto *currentClass = dynamic_cast<Class *>(st.getCurrentRecord());
st.enterMethodScope(id->value);
graph.setCurrentBlock(
graph.addMethodRootBlock(currentClass->getID(), id->value));
body->generateIR(graph, st);
st.exitScope();
return graph.getCurrentBlock()->getName();
}