Repository navigation
Expand file tree
/
Copy pathFunctionCallIR.cpp
More file actions
123 lines (89 loc) · 4.2 KB
/
Copy pathFunctionCallIR.cpp
File metadata and controls
123 lines (89 loc) · 4.2 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
#include "AST.hpp"
std::optional<Value *> CallFuncExpr::codegen(CompilationContext &ctx) {
vector<Value *> argv;
vector<string> types;
// convert types
for (int i = 0; i != params.size(); ++i)
{
std::optional<Value*> codeGenParam = params[i]->codegen(ctx);
if (codeGenParam) {
argv.push_back(*codeGenParam);
types.push_back(params[i]->return_type());
}
else
throw invalid_argument("Parameter invalid");
}
// check if it is a class
try {
auto cscope = ctx.findClass(functionName);
// allocate temp variable for just the scope
auto alloc = ctx.builder->CreateAlloca(cscope.type);
argv.push_back(alloc);
types.push_back("ptr<"+cscope.name+">");
FunctionScope func = ctx.findFuncName(
"constructor_" + functionName,
types
);
Function *calleeF = ctx.mod->getFunction(func.name);
ctx.builder->CreateCall(calleeF, argv);
retType = cscope.name;
return ctx.builder->CreateLoad(cscope.type, alloc);
} catch (invalid_argument e) {}
// not a class name, just a normal functino call
if (classVar == "") {
try {
FunctionScope func = ctx.findFuncName(functionName, types);
retType = func.returnType;
Function *calleeF = ctx.mod->getFunction(func.name);
return ctx.builder->CreateCall(calleeF, argv);
} catch (invalid_argument e) {
// if that doesn't work, then maybe we are inside a function and we are trying to call there
if (ctx.runningClass == "") throw e; // if there's no class inside of what we are trying to call, then throw e
if (ctx.thisValue == nullptr) throw e;
if (ctx.classesDefined.find(ctx.runningClass) != ctx.classesDefined.end()) {
argv.push_back(ctx.thisValue);
types.push_back("ptr<"+ctx.runningClass+">");
FunctionScope func = ctx.findFuncName(ctx.runningClass + "_" + functionName, types);
// if not actually create call
retType = func.returnType;
Function *calleeF = ctx.mod->getFunction(func.name);
return ctx.builder->CreateCall(calleeF, argv);
}
throw e;
}
}
// class func call from a variable. i.e a.move()
auto result = ctx.findVarName(classVar);
if (const VariableScope* v = get_if<VariableScope>(&result)) {
string className = v->type;
// search through ctx for classes defined
if (ctx.classesDefined.find(className) != ctx.classesDefined.end()) {
argv.push_back(v->value);
types.push_back("ptr<"+className+">");
FunctionScope func = ctx.findFuncName(className + "_" + functionName, types);
// check if its private
if (func.isPrivate && ctx.runningClass != className)
throw invalid_argument("Cannot call private method!");
// if not actually create call
retType = func.returnType;
Function *calleeF = ctx.mod->getFunction(func.name);
return ctx.builder->CreateCall(calleeF, argv);
}
throw invalid_argument("Invalid class name: " + className);
}
if (const ClassScope* v = get_if<ClassScope>(&result)) {
for (int i = 0; i < v->variables.size(); i++) {
if (v->variables[i].expr->name == classVar && ctx.runningClass != "") {
argv.push_back(v->variableValues[i]);
types.push_back("ptr<"+*v->variables[i].expr->typeArg+">");
FunctionScope func = ctx.findFuncName(*v->variables[i].expr->typeArg + "_" + functionName, types);
if (func.isPrivate && ctx.runningClass != v->name)
throw invalid_argument("Cannot call private method!");
retType = func.returnType;
Function *calleeF = ctx.mod->getFunction(func.name);
return ctx.builder->CreateCall(calleeF, argv);
}
}
}
throw invalid_argument("Invalid function call.");
}