-
Notifications
You must be signed in to change notification settings - Fork 40
/
ClassDeclWriter.ooc
374 lines (283 loc) · 14.1 KB
/
ClassDeclWriter.ooc
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
import structs/[List, ArrayList, HashMap]
import ../../middle/[ClassDecl, FunctionDecl, VariableDecl, TypeDecl,
Type, Node, InterfaceDecl, InterfaceImpl, CoverDecl]
import Skeleton, FunctionDeclWriter, VersionWriter
ClassDeclWriter: abstract class extends Skeleton {
LANG_PREFIX := static const "lang_types__"
CLASS_NAME := static const This LANG_PREFIX + "Class"
write: static func ~_class (this: Skeleton, cDecl: ClassDecl) {
//printf(" << Writing class decl %s with version %s\n", cDecl toString(), cDecl getVersion() ? cDecl getVersion() toString() : "(nil)")
if(cDecl isMeta) {
current = hw
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
writeObjectStruct(this, cDecl)
if(cDecl getVersion()) VersionWriter writeEnd(this)
//TODO: split into InterfaceImplWriter ?
if(!cDecl getNonMeta() instanceOf(InterfaceImpl)) {
current = fw
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
writeMemberFuncPrototypes(this, cDecl)
if(cDecl getVersion()) VersionWriter writeEnd(this)
current = cw
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
writeInstanceImplFuncs(this, cDecl)
writeInstanceVirtualFuncs(this, cDecl)
writeStaticFuncs(this, cDecl)
} else {
current = cw
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
}
// don't write class-getting functions of extern covers - it hurts
if(cDecl getNonMeta() == null || !cDecl getNonMeta() instanceOf(CoverDecl) || !(cDecl getNonMeta() as CoverDecl isExtern() || cDecl getNonMeta() as CoverDecl isAddon())) {
writeClassGettingFunction(this, cDecl)
}
if(cDecl getVersion()) VersionWriter writeEnd(this)
for(interfaceDecl in cDecl getNonMeta() getInterfaceDecls()) {
write(this, interfaceDecl getMeta())
}
} else {
current = hw
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
writeObjectStruct(this, cDecl)
if(cDecl getVersion()) VersionWriter writeEnd(this)
for(interfaceDecl in cDecl getInterfaceDecls()) {
write(this, interfaceDecl)
}
}
}
writeObjectStruct: static func (this: Skeleton, cDecl: ClassDecl) {
current nl(). app("struct _"). app(cDecl underName()). app(' '). openBlock()
if (cDecl name != "Object" && cDecl getSuperRef() != null) {
current nl(). app("struct _"). app(cDecl getSuperRef() underName()). app(" __super__;")
}
for(vDecl in cDecl variables) {
// ignore extern and virtual variables (usually properties)
if(vDecl isExtern() || vDecl isVirtual()) continue;
current nl(). app(vDecl getType()). app(" "). app(vDecl getName()). app(';')
}
// Now write all virtual functions prototypes in the class struct
for (fDecl in cDecl functions) {
if(fDecl isExtern()) continue
if(cDecl getSuperRef() != null) {
superDecl : FunctionDecl = null
superDecl = cDecl getSuperRef() lookupFunction(fDecl name, fDecl suffix)
// don't write the function if it was declared in the parent
if(superDecl != null) {
//printf("Already declared in super %s, skipping (superDecl = %s)\n", cDecl getSuperRef() toString(), superDecl toString())
continue
}
}
current nl()
writeFunctionDeclPointer(this, fDecl, true)
current app(';')
}
current closeBlock(). app(';'). nl(). nl()
}
/** Write a function declaration's pointer */
writeFunctionDeclPointer: static func (this: Skeleton, fDecl: FunctionDecl, doName: Bool) {
current app((fDecl hasReturn() ? fDecl getReturnType() : voidType) as Node)
current app(" (*")
if(doName) FunctionDeclWriter writeSuffixedName(this, fDecl)
current app(")")
FunctionDeclWriter writeFuncArgs(this, fDecl, ArgsWriteModes TYPES_ONLY, null);
}
/** Write the prototypes of member functions */
writeMemberFuncPrototypes: static func (this: Skeleton, cDecl: ClassDecl) {
current nl(). app(cDecl underName()). app(" *"). app(cDecl getNonMeta() getFullName()). app("_class();")
for(fDecl: FunctionDecl in cDecl functions) {
if(fDecl isExternWithName()) {
continue
}
current nl()
FunctionDeclWriter writeFuncPrototype(this, fDecl, null)
current app(';')
if(!fDecl isStatic() && !fDecl isAbstract() && !fDecl isFinal()) {
current nl()
FunctionDeclWriter writeFuncPrototype(this, fDecl, "_impl")
current app(';')
}
}
}
writeStaticFuncs: static func (this: Skeleton, cDecl: ClassDecl) {
for (decl: FunctionDecl in cDecl functions) {
if (!decl isStatic() || decl isExternWithName()) {
if(decl isExternWithName()) {
FunctionDeclWriter write(this, decl)
}
continue
}
current nl()
FunctionDeclWriter writeFuncPrototype(this, decl);
current app(' '). openBlock(). nl()
if(decl getName() == ClassDecl LOAD_FUNC_NAME) {
superRef := cDecl getSuperRef()
finalScore: Int
superLoad := superRef getFunction(ClassDecl LOAD_FUNC_NAME, null, null, finalScore&)
if(superLoad) {
FunctionDeclWriter writeFullName(this, superLoad)
current app("();")
}
for(vDecl in cDecl variables) {
if(vDecl getExpr() == null) continue
current nl(). app(cDecl getNonMeta() underName()). app("_class()->"). app(vDecl getName()). app(" = "). app(vDecl getExpr()). app(';')
}
}
for(stat in decl body) {
writeLine(stat)
}
current closeBlock()
}
}
writeInstanceVirtualFuncs: static func (this: Skeleton, cDecl: ClassDecl) {
for(fDecl: FunctionDecl in cDecl functions) {
if (fDecl isStatic() || fDecl isFinal() || fDecl isExternWithName()) {
continue
}
current nl(). nl()
FunctionDeclWriter writeFuncPrototype(this, fDecl)
current app(' '). openBlock(). nl()
baseClass := cDecl getBaseClass(fDecl)
if (fDecl hasReturn()) {
current app("return ("). app(fDecl returnType). app(") ")
}
if(cDecl getNonMeta() instanceOf(InterfaceDecl)) {
current app("this.impl->")
} else {
current app("(("). app(baseClass underName()). app(" *)"). app("((lang_types__Object *)this)->class)->")
}
FunctionDeclWriter writeSuffixedName(this, fDecl)
FunctionDeclWriter writeFuncArgs(this, fDecl, ArgsWriteModes NAMES_ONLY, baseClass)
current app(";"). closeBlock()
}
}
writeInstanceImplFuncs: static func (this: Skeleton, cDecl: ClassDecl) {
// Non-static (ie instance) functions
for (decl: FunctionDecl in cDecl functions) {
if (decl isStatic() || decl isAbstract() || decl isExternWithName()) {
continue
}
current nl(). nl()
FunctionDeclWriter writeFuncPrototype(this, decl, (decl isFinal()) ? null : "_impl")
current app(' '). openBlock()
if(decl getName() == ClassDecl DEFAULTS_FUNC_NAME) {
superRef := cDecl getSuperRef()
finalScore: Int
superDefaults := superRef getFunction(ClassDecl DEFAULTS_FUNC_NAME, null, null, finalScore&)
if(superDefaults) {
FunctionDeclWriter writeFullName(this, superDefaults)
current app("_impl(("). app(superDefaults owner getInstanceType()). app(") this);")
}
nonMeta := cDecl getNonMeta()
for(vDecl in nonMeta variables) {
if(vDecl getExpr() == null) continue
current nl(). app("this->"). app(vDecl getName()). app(" = "). app(vDecl getExpr()). app(';')
}
}
for(stat in decl body) {
writeLine(stat)
}
current closeBlock()
}
}
writeClassGettingFunction: static func (this: Skeleton, cDecl: ClassDecl) {
isInterface := (cDecl getNonMeta() != null && cDecl getNonMeta() instanceOf(InterfaceImpl)) as Bool
underName := isInterface ? cDecl getSuperRef() underName() : cDecl underName()
current nl(). nl(). app(underName). app(" *"). app(cDecl getNonMeta() getFullName()). app("_class()"). openBlock(). nl()
if (cDecl getNonMeta() getSuperRef()) {
current app("static "). app(This LANG_PREFIX). app("Bool __done__ = false;"). nl()
}
current app("static "). app(underName). app(" class = "). nl()
writeClassStructInitializers(this, isInterface ? cDecl getSuperRef() as ClassDecl : cDecl, cDecl, ArrayList<FunctionDecl> new(), true)
current app(';')
if (cDecl getNonMeta() getSuperRef()) {
current nl(). app(This CLASS_NAME). app(" *classPtr = ("). app(This CLASS_NAME). app(" *) &class;")
current nl(). app("if(!__done__)"). openBlock().
nl(). app("classPtr->super = ("). app(This CLASS_NAME). app("*) "). app(cDecl getNonMeta() getSuperRef() getFullName()). app("_class();").
nl(). app("__done__ = true;").
closeBlock()
}
current nl(). app("return &class;"). closeBlock()
}
/**
* Write class initializers
* @param parentClass
*/
writeClassStructInitializers: static func (this: Skeleton, parentClass: ClassDecl,
realClass: ClassDecl, done: List<FunctionDecl>, root: Bool) {
current openBlock(). nl()
if (parentClass name equals("Class")) {
current app(".instanceSize = ")
realClass getNonMeta() writeSize(current, true) // instance = true
current app(','). nl(). app(".size = ")
realClass getNonMeta() writeSize(current, false) // instance = false
current app(','). nl(). app(".name = "). app('"'). app(realClass getNonMeta() name). app("\",")
} else {
writeClassStructInitializers(this, parentClass getSuperRef() as ClassDecl, realClass, done, false)
}
if(parentClass != realClass ||
realClass getNonMeta() == null ||
!realClass getNonMeta() instanceOf(InterfaceDecl)) {
for (parentDecl: FunctionDecl in parentClass functions) {
if(done contains(parentDecl)) {
continue
}
realDecl : FunctionDecl = null
if(realClass != parentClass) {
finalScore: Int
realDecl = realClass getFunction(parentDecl name, parentDecl suffix ? parentDecl suffix : "", null, true, finalScore&)
if(realDecl != parentDecl) {
if(done contains(realDecl)) {
continue
}
done add(realDecl)
}
}
if (parentDecl isFinal() || parentDecl isExtern() || (realDecl != null && realDecl isExtern())) {
continue // skip it.
}
if (parentDecl isStatic() || (realDecl == null && parentDecl isAbstract())) {
writeDesignatedInit(this, parentDecl, realDecl, false)
} else {
writeDesignatedInit(this, parentDecl, realDecl, true)
}
}
}
if (parentClass != realClass &&
parentClass getNonMeta() != null &&
parentClass getNonMeta() instanceOf(InterfaceDecl) &&
realClass getNonMeta() instanceOf(InterfaceImpl)) {
interfaceImpl := realClass getNonMeta() as InterfaceImpl
for(alias: FunctionAlias in interfaceImpl getAliases()) {
current nl(). app('.'). app(alias key getName()). app(" = (void*) ")
FunctionDeclWriter writeFullName(this, alias value)
if(!alias value isFinal() && !alias value isAbstract()) current app("_impl")
current app(",")
}
}
current closeBlock()
if (!root)
current app(',')
}
writeDesignatedInit: static func (this: Skeleton, parentDecl, realDecl: FunctionDecl, impl: Bool) {
if(realDecl != null && realDecl isAbstract) return
current nl(). app('.')
FunctionDeclWriter writeSuffixedName(this, parentDecl)
current app(" = ")
if(realDecl != null || parentDecl isExtern()) {
current app("(")
writeFunctionDeclPointer(this, parentDecl, false)
current app(") ")
}
decl := realDecl ? realDecl : parentDecl
FunctionDeclWriter writeFullName(this, decl)
if(!decl isExternWithName() && impl) current app("_impl")
current app(',')
}
writeStructTypedef: static func (this: Skeleton, cDecl: ClassDecl) {
structName := cDecl underName()
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
current nl(). app("struct _"). app(structName). app(";")
current nl(). app("typedef struct _"). app(structName). app(" "). app(structName). app(";")
if(cDecl getVersion()) VersionWriter writeEnd(this)
}
}