/
CoverDecl.ooc
187 lines (141 loc) · 5.15 KB
/
CoverDecl.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
// sdk stuff
import structs/[HashMap, ArrayList]
import ../io/TabbedWriter
// our stuff
import ../frontend/[Token, BuildParams]
import Expression, Type, Visitor, TypeDecl, Node, FunctionDecl,
FunctionCall, VariableAccess, TemplateDef, BaseType
import tinker/[Response, Resolver, Trail, Errors]
CoverDecl: class extends TypeDecl {
fromType: Type
template: TemplateDef { get set }
templateParent: CoverDecl { get set }
instances := HashMap<String, CoverDecl> new()
init: func ~coverDeclNoSuper(.name, .token) {
super(name, token)
}
init: func ~coverDecl(.name, .superType, .token) {
super(name, superType, token)
}
accept: func (visitor: Visitor) { visitor visitCoverDecl(this) }
setFromType: func (=fromType) {}
getFromType: func -> Type { fromType }
// all functions of a cover are final, because we don't have a 'class' field
addFunction: func (fDecl: FunctionDecl) {
fDecl isFinal = true
super(fDecl)
}
resolve: func (trail: Trail, res: Resolver) -> Response {
if (debugCondition()) {
"Resolving CoverDecl %s, template = %p" printfln(
toString(), template
)
}
if (template) {
response := Response OK
for (instance in instances) {
response = instance resolve(trail, res)
if (!response ok()) {
return response
}
}
} else {
// resolve the body, methods, arguments
response := super(trail, res)
if(!response ok()) return response
if(fromType) {
trail push(this)
response := fromType resolve(trail, res)
if(!response ok()) {
fromType setRef(BuiltinType new(fromType getName(), nullToken))
}
if(fromType getRef() != null) {
fromType checkedDig(res)
}
trail pop(this)
}
}
return Response OK
}
resolveCallInFromType: func (call: FunctionCall, res: Resolver, trail: Trail) -> Int {
if(fromType && fromType getRef() && fromType getRef() instanceOf?(TypeDecl)) {
tDecl := fromType getRef() as TypeDecl
meta := tDecl getMeta()
if(meta) {
meta resolveCall(call, res, trail)
} else {
tDecl resolveCall(call, res, trail)
}
} else {
-1
}
}
resolveAccessInFromType: func (access: VariableAccess, res: Resolver, trail: Trail) -> Int {
if(fromType && fromType getRef() && fromType getRef() instanceOf?(TypeDecl)) {
fromType getRef() as TypeDecl resolveAccess(access, res, trail)
} else {
-1
}
}
hasMeta?: func -> Bool {
if (debugCondition()) {
"hasMeta called, they want %s / %p back" printfln(toString(), template)
}
// templates have no meta-class. Like, none at all.
!template
}
getTemplateInstance: func (spec: BaseType) -> CoverDecl {
"Should get a template instance of %s as per %s" printfln(toString(), spec toString())
fingerprint := "__" + name + "__" + spec typeArgs map(|vAcc| vAcc getName()) join("__")
if (instances contains?(fingerprint)) {
return instances get(fingerprint)
}
"Creating instance with fingerprint: %s" printfln(fingerprint)
instance := This new(fingerprint, token)
instance templateParent = this
instance module = module
instance setVersion(instance getVersion())
for (variable in variables) {
instance addVariable(variable clone())
}
for (oDecl in operators) {
instance addOperator(oDecl)
}
for (fDecl in getMeta() functions) {
if (fDecl oDecl) {
// already been added at last step
continue
}
instance addFunction(fDecl)
}
i := 0
for (typeArg in spec typeArgs) {
if (i >= template typeArgs size) {
Exception new("Too many template args for %s" format(toString())) throw()
}
name := template typeArgs get(i) getName()
ref := typeArg getRef()
"name %s, ref %s" printfln(name, ref ? ref toString() : "(nil)")
instance templateArgs put(name, ref)
i += 1
}
instances put(fingerprint, instance)
instance
}
writeSize: func (w: TabbedWriter, instance: Bool) {
w app("sizeof("). app(underName()). app(")")
}
replace: func (oldie, kiddo: Node) -> Bool { false }
}
AddingVariablesInAddon: class extends Error {
init: func (base: CoverDecl, =token) {
message = base token formatMessage("...while extending cover " + base toString(), "") +
token formatMessage("Attempting to add variables to another cover!", "ERROR")
}
format: func -> String {
message
}
}
CoverDeclLoop: class extends Error {
init: super func ~tokenMessage
}