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