-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclasscompilationctx.cpp
More file actions
326 lines (301 loc) · 10.7 KB
/
Copy pathclasscompilationctx.cpp
File metadata and controls
326 lines (301 loc) · 10.7 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
#include "classcompilationctx.h"
#include "bytecodecompilationctx.h"
#include "class.h"
#include "functioncompilationctx.h"
#include "symtab.h"
ClassCompilationContext *
ClassCompilationContext::create(ClassCompilationContext *s, String *n) {
ClassCompilationContext2 ctx = Gc::alloc<ClassCompilationContext>();
ctx->slotCount = 0;
ctx->staticSlotCount = 0;
ctx->isCompiled = false;
ctx->moduleContext = s;
ctx->defaultConstructor = nullptr;
ctx->cctxMap = nullptr;
ctx->isDerived = false;
ctx->public_signatures = nullptr;
ctx->private_signatures = nullptr;
ctx->compilingClass = nullptr;
ctx->fctxMap = nullptr;
ctx->metaclass = nullptr;
ctx->members = nullptr;
// initialize the members
ctx->members = (MemberMap *)Gc_malloc(sizeof(MemberMap));
::new(ctx->members) MemberMap();
ctx->public_signatures = Map::create();
ctx->private_signatures = Map::create();
ctx->compilingClass = Class::create();
ctx->fctxMap = Map::create();
if(s == NULL) {
// it's a module.
// init the module
ctx->compilingClass->init_class(n, Class::ClassType::NORMAL);
// so add a default constructor to initialize
// the class variables
ctx->defaultConstructor = FunctionCompilationContext::create(
String::const_sig_constructor_0, 0);
ctx->add_public_fn(String::const_sig_constructor_0,
ctx->defaultConstructor->get_fn(),
ctx->defaultConstructor);
// add slot for the module
ctx->defaultConstructor->create_slot(String::from("mod "), 0);
// initialize the module
ctx->defaultConstructor->get_codectx()->construct(
Value(ctx->get_class()));
// construct will automatically store it to slot 0
// add the class map
ctx->cctxMap = Map::create();
// a module does not have a metaclass
ctx->metaclass = NULL;
} else {
ctx->compilingClass->module = s->compilingClass;
// if this is a normal class, create a metaclass
// for it
// a metaclass is a copy of the root 'class' class,
// only it has added slots for static methods
// and members of the present class
ctx->metaclass = Classes::get<Class>()->copy();
// link the metaclass first, so it doesn't get
// garbage collected
ctx->compilingClass->obj.setClass(ctx->metaclass);
ctx->metaclass->name = String::append(n, " metaclass");
// init the class with the metaclass
ctx->compilingClass->init_class(n, Class::ClassType::NORMAL,
ctx->metaclass);
}
return ctx;
}
int ClassCompilationContext::add_public_mem(Value name, bool isStatic,
bool declare) {
if(has_mem(name))
return get_mem_slot(name);
if(!isStatic) {
members[0][name] = MemberInfo{slotCount++, false, declare};
} else {
members[0][name] = MemberInfo{staticSlotCount++, true, declare};
}
compilingClass->add_member(name.toString(), isStatic, ValueNil);
return true;
}
int ClassCompilationContext::add_private_mem(Value name, bool isStatic,
bool declare) {
if(has_mem(name))
return get_mem_slot(name);
if(!isStatic) {
members[0][name] = MemberInfo{slotCount++, false, declare};
compilingClass->add_slot();
} else {
members[0][name] = MemberInfo{staticSlotCount++, true, declare};
compilingClass->add_static_slot();
// this is a private static variable, so we don't
// need to add anything to the metaclass
}
return true;
}
bool ClassCompilationContext::has_mem(Value name) {
return members->contains(name) && members[0][name].isDeclared;
}
int ClassCompilationContext::get_mem_slot(Value name) {
return members[0][name].slot;
}
ClassCompilationContext::MemberInfo
ClassCompilationContext::get_mem_info(Value name) {
return members[0][name];
}
bool ClassCompilationContext::is_static_slot(Value name) {
return get_mem_info(name).isStatic;
}
void ClassCompilationContext::reset_default_constructor() {
defaultConstructor =
FunctionCompilationContext::create(String::const_sig_constructor_0, 0);
add_public_signature(String::const_sig_constructor_0,
defaultConstructor->get_fn(), defaultConstructor);
defaultConstructor->create_slot(String::from("mod "), 0);
}
void ClassCompilationContext::add_public_signature(
const String2 &sig, Function *f, FunctionCompilationContext *fctx) {
// TODO: insert token here
public_signatures->vv[Value(sig)] = Value(f);
compilingClass->add_fn(sig, f);
if(fctx)
fctxMap->vv[Value(sig)] = Value(fctx);
if(f->isStatic() && metaclass) {
metaclass->add_fn(sig, f);
}
}
bool ClassCompilationContext::add_public_fn(const String2 &sig, Function *f,
FunctionCompilationContext *fctx) {
if(has_fn((String *)sig))
return false;
add_public_signature(sig, f, fctx);
if(f->isVarArg()) {
// sig contains the base signature, without
// the vararg. so get the base without ')'
String2 base = String::from(sig->strb(), sig->size - 1);
// now starting from 1 upto MAX_VARARG_COUNT, generate
// a signature and register
for(int i = 0; i < MAX_VARARG_COUNT; i++) {
// if base contains only (, i.e. the function
// does not have any necessary arguments, initially
// append it with _
if(i == 0 && (base->str() + (base->len() - 1)) == '(') {
base = String::append(base, "_");
} else {
base = String::append(base, ",_");
}
add_public_signature(String::append(base, ")"), f, fctx);
}
}
return true;
}
void ClassCompilationContext::add_private_signature(
const String2 &sig, Function *f, FunctionCompilationContext *fctx) {
// TODO: insert token here
private_signatures->vv[Value(sig)] = Value(f);
// append the signature with "p " so that it cannot
// be invoked as a method outside of the class
String2 priv_signature = String::append("p ", sig);
compilingClass->add_fn(priv_signature, f);
if(fctx)
fctxMap->vv[Value(sig)] = Value(fctx);
// we don't need to add anything to the metaclass
}
bool ClassCompilationContext::add_private_fn(const String2 &sig, Function *f,
FunctionCompilationContext *fctx) {
if(has_fn((String *)sig))
return false;
add_private_signature(sig, f, fctx);
if(f->isVarArg()) {
// sig contains the base signature, without
// the vararg. so get the base without ')'
String2 base = String::from(sig->str(), sig->size - 1);
// now starting from 1 upto MAX_VARARG_COUNT, generate
// a signature and register
for(int i = 0; i < MAX_VARARG_COUNT; i++) {
// if base contains only (, i.e. the function
// does not have any necessary arguments, initially
// append it with _
if(i == 0 && (base->str() + (base->len() - 1)) == '(') {
base = String::append(base, "_");
} else {
base = String::append(base, ",_");
}
add_private_signature(String::append(base, ")"), f, fctx);
}
}
return true;
}
bool ClassCompilationContext::has_fn(Value sig) {
if(public_signatures->vv.contains(sig))
return true;
if(private_signatures->vv.contains(sig))
return true;
return false;
}
int ClassCompilationContext::get_fn_sym(const String2 &sig) {
String2 finalSig = sig;
if(private_signatures->vv.contains(Value(sig))) {
finalSig = String::append("p ", sig);
}
return SymbolTable2::insert(finalSig);
}
FunctionCompilationContext *ClassCompilationContext::get_func_ctx(Value sig) {
return fctxMap->vv[sig].toFunctionCompilationContext();
}
void ClassCompilationContext::add_public_class(Class * c,
ClassCompilationContext *ctx) {
// mark it as not declared if the class is not builtin
add_public_mem(c->name, false, c->type == Class::ClassType::BUILTIN);
int modSlot = get_mem_slot(c->name);
defaultConstructor->bcc->push(Value(c));
defaultConstructor->bcc->store_object_slot(modSlot);
// if the ctx is null, it is a builtin class,
// and it won't query for its ctx anytime.
// so don't populate the map
if(ctx != NULL)
cctxMap->vv[Value(c->name)] = Value(ctx);
c->module = compilingClass;
}
void ClassCompilationContext::add_private_class(Class * c,
ClassCompilationContext *ctx) {
// mark it as not declared if the class is not builtin
add_private_mem(c->name, false, c->type == Class::ClassType::BUILTIN);
int modSlot = get_mem_slot(c->name);
defaultConstructor->bcc->push(Value(c));
defaultConstructor->bcc->store_object_slot(modSlot);
if(ctx != NULL)
cctxMap->vv[Value(c->name)] = Value(ctx);
c->module = compilingClass;
}
bool ClassCompilationContext::has_class(String *name) {
return has_mem(name) && cctxMap->vv[Value(name)] != ValueNil;
}
ClassCompilationContext *ClassCompilationContext::get_class_ctx(String *name) {
// mark the member as declared
members[0][name].isDeclared = true;
return cctxMap->vv[Value(name)].toClassCompilationContext();
}
Class *ClassCompilationContext::get_class() {
return compilingClass;
}
FunctionCompilationContext *ClassCompilationContext::get_default_constructor() {
return defaultConstructor;
}
void ClassCompilationContext::finalize() {
// add ret to the ()
if(defaultConstructor != NULL && moduleContext == NULL) {
defaultConstructor->bcc->load_slot(0);
defaultConstructor->bcc->ret();
}
}
#ifdef DEBUG
#include "../format.h"
void ClassCompilationContext::disassemble(WritableStream &os) {
if(moduleContext == NULL) {
os.write("Module: ", compilingClass->name->str(), "\n");
} else {
os.write("Class: ", compilingClass->name->str(), "\n");
}
os.write("Members: ");
for(auto &a : *members) {
os.write(a.first.toString()->str(), "(slot=", a.second.slot,
",static=", a.second.isStatic, "), ");
}
os.write("\n");
os.write("Functions: ", fctxMap->vv.size(), "\n");
HashSet<FunctionCompilationContext *> vaFuncs;
size_t i = 0;
for(auto &a : fctxMap->vv) {
FunctionCompilationContext *f = a.second.toFunctionCompilationContext();
if(!vaFuncs.contains(f)) {
os.write("\nFunction #", i++, ": ");
String *name = a.first.toString();
// if this is a vararg function, print the minimum
// signature
if(f->get_fn()->isVarArg()) {
Utf8Source str = name->str();
while(*str != '(') os.write(str++);
os.write("(");
if(f->get_fn()->arity > 0)
os.write("_,");
for(int i = 1; i < f->get_fn()->arity; i++) os.write("_,");
os.write("..)\n");
} else {
os.write(name->str(), "\n");
}
f->disassemble(os);
}
if(f->get_fn()->isVarArg()) {
vaFuncs.insert(f);
}
}
if(cctxMap != NULL) {
os.write("\nClasses: ", cctxMap->vv.size(), "\n");
i = 0;
for(auto &a : cctxMap->vv) {
os.write("\nClass #", i++, ": ", a.first.toString()->str(), "\n");
a.second.toClassCompilationContext()->disassemble(os);
}
}
}
#endif