Mercurial > projects > ldc
annotate dmd/class.c @ 1607:207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
---
dmd/arrayop.c | 30 ++++----
dmd/arraytypes.h | 2 +-
dmd/class.c | 8 +-
dmd/declaration.c | 10 ++--
dmd/declaration.h | 16 ++--
dmd/doc.c | 12 ++--
dmd/dsymbol.c | 4 +-
dmd/expression.c | 48 +++++++-------
dmd/expression.h | 32 +++++-----
dmd/func.c | 78 +++++++++++-----------
dmd/init.c | 2 +-
dmd/interpret.c | 8 +-
dmd/mtype.c | 190 ++++++++++++++++++++++++++--------------------------
dmd/mtype.h | 32 +++++-----
dmd/opover.c | 34 +++++-----
dmd/parse.c | 40 ++++++------
dmd/parse.h | 2 +-
dmd/statement.c | 90 +++++++++++++-------------
dmd/statement.h | 14 ++--
dmd/struct.c | 8 +-
dmd/template.c | 30 ++++----
gen/functions.cpp | 10 ++--
gen/functions.h | 2 +-
gen/tocall.cpp | 10 ++--
gen/typinf.cpp | 6 +-
25 files changed, 359 insertions(+), 359 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:20 -0300 |
parents | def7a1d494fd |
children | 79f64d5fee9e |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
1587 | 3 // Copyright (c) 1999-2009 by Digital Mars |
159 | 4 // All Rights Reserved |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <assert.h> | |
14 | |
15 #include "root.h" | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
16 #include "rmem.h" |
159 | 17 |
18 #include "enum.h" | |
19 #include "init.h" | |
20 #include "attrib.h" | |
21 #include "declaration.h" | |
22 #include "aggregate.h" | |
23 #include "id.h" | |
24 #include "mtype.h" | |
25 #include "scope.h" | |
26 #include "module.h" | |
27 #include "expression.h" | |
28 #include "statement.h" | |
29 | |
30 /********************************* ClassDeclaration ****************************/ | |
31 | |
32 ClassDeclaration *ClassDeclaration::classinfo; | |
33 ClassDeclaration *ClassDeclaration::object; | |
34 | |
35 ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses) | |
36 : AggregateDeclaration(loc, id) | |
37 { | |
38 static char msg[] = "only object.d can define this reserved class name"; | |
39 | |
40 if (baseclasses) | |
41 this->baseclasses = *baseclasses; | |
42 baseClass = NULL; | |
43 | |
44 interfaces_dim = 0; | |
45 interfaces = NULL; | |
46 | |
47 vtblInterfaces = NULL; | |
48 | |
49 //printf("ClassDeclaration(%s), dim = %d\n", id->toChars(), this->baseclasses.dim); | |
50 | |
51 // For forward references | |
52 type = new TypeClass(this); | |
53 handle = type; | |
54 | |
55 ctor = NULL; | |
56 defaultCtor = NULL; | |
57 staticCtor = NULL; | |
58 staticDtor = NULL; | |
59 | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
60 #if IN_DMD |
159 | 61 vtblsym = NULL; |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
62 #endif |
159 | 63 vclassinfo = NULL; |
64 | |
65 if (id) | |
66 { // Look for special class names | |
67 | |
68 if (id == Id::__sizeof || id == Id::alignof || id == Id::mangleof) | |
69 error("illegal class name"); | |
70 | |
71 // BUG: What if this is the wrong TypeInfo, i.e. it is nested? | |
72 if (id->toChars()[0] == 'T') | |
73 { | |
74 if (id == Id::TypeInfo) | |
75 { if (Type::typeinfo) | |
76 Type::typeinfo->error("%s", msg); | |
77 Type::typeinfo = this; | |
78 } | |
79 | |
80 if (id == Id::TypeInfo_Class) | |
81 { if (Type::typeinfoclass) | |
82 Type::typeinfoclass->error("%s", msg); | |
83 Type::typeinfoclass = this; | |
84 } | |
85 | |
86 if (id == Id::TypeInfo_Interface) | |
87 { if (Type::typeinfointerface) | |
88 Type::typeinfointerface->error("%s", msg); | |
89 Type::typeinfointerface = this; | |
90 } | |
91 | |
92 if (id == Id::TypeInfo_Struct) | |
93 { if (Type::typeinfostruct) | |
94 Type::typeinfostruct->error("%s", msg); | |
95 Type::typeinfostruct = this; | |
96 } | |
97 | |
98 if (id == Id::TypeInfo_Typedef) | |
99 { if (Type::typeinfotypedef) | |
100 Type::typeinfotypedef->error("%s", msg); | |
101 Type::typeinfotypedef = this; | |
102 } | |
103 | |
104 if (id == Id::TypeInfo_Pointer) | |
105 { if (Type::typeinfopointer) | |
106 Type::typeinfopointer->error("%s", msg); | |
107 Type::typeinfopointer = this; | |
108 } | |
109 | |
110 if (id == Id::TypeInfo_Array) | |
111 { if (Type::typeinfoarray) | |
112 Type::typeinfoarray->error("%s", msg); | |
113 Type::typeinfoarray = this; | |
114 } | |
115 | |
116 if (id == Id::TypeInfo_StaticArray) | |
117 { //if (Type::typeinfostaticarray) | |
118 //Type::typeinfostaticarray->error("%s", msg); | |
119 Type::typeinfostaticarray = this; | |
120 } | |
121 | |
122 if (id == Id::TypeInfo_AssociativeArray) | |
123 { if (Type::typeinfoassociativearray) | |
124 Type::typeinfoassociativearray->error("%s", msg); | |
125 Type::typeinfoassociativearray = this; | |
126 } | |
127 | |
128 if (id == Id::TypeInfo_Enum) | |
129 { if (Type::typeinfoenum) | |
130 Type::typeinfoenum->error("%s", msg); | |
131 Type::typeinfoenum = this; | |
132 } | |
133 | |
134 if (id == Id::TypeInfo_Function) | |
135 { if (Type::typeinfofunction) | |
136 Type::typeinfofunction->error("%s", msg); | |
137 Type::typeinfofunction = this; | |
138 } | |
139 | |
140 if (id == Id::TypeInfo_Delegate) | |
141 { if (Type::typeinfodelegate) | |
142 Type::typeinfodelegate->error("%s", msg); | |
143 Type::typeinfodelegate = this; | |
144 } | |
145 | |
146 if (id == Id::TypeInfo_Tuple) | |
147 { if (Type::typeinfotypelist) | |
148 Type::typeinfotypelist->error("%s", msg); | |
149 Type::typeinfotypelist = this; | |
150 } | |
151 | |
336 | 152 #if DMDV2 |
159 | 153 if (id == Id::TypeInfo_Const) |
154 { if (Type::typeinfoconst) | |
155 Type::typeinfoconst->error("%s", msg); | |
156 Type::typeinfoconst = this; | |
157 } | |
158 | |
159 if (id == Id::TypeInfo_Invariant) | |
160 { if (Type::typeinfoinvariant) | |
161 Type::typeinfoinvariant->error("%s", msg); | |
162 Type::typeinfoinvariant = this; | |
163 } | |
1587 | 164 |
165 if (id == Id::TypeInfo_Shared) | |
166 { if (Type::typeinfoshared) | |
167 Type::typeinfoshared->error("%s", msg); | |
168 Type::typeinfoshared = this; | |
169 } | |
159 | 170 #endif |
171 } | |
172 | |
173 if (id == Id::Object) | |
174 { if (object) | |
175 object->error("%s", msg); | |
176 object = this; | |
177 } | |
178 | |
179 if (id == Id::ClassInfo) | |
180 { if (classinfo) | |
181 classinfo->error("%s", msg); | |
182 classinfo = this; | |
183 } | |
184 | |
185 if (id == Id::ModuleInfo) | |
186 { if (Module::moduleinfo) | |
187 Module::moduleinfo->error("%s", msg); | |
188 Module::moduleinfo = this; | |
189 } | |
190 } | |
191 | |
192 com = 0; | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
193 isscope = 0; |
159 | 194 isabstract = 0; |
195 isnested = 0; | |
196 vthis = NULL; | |
197 inuse = 0; | |
198 } | |
199 | |
200 Dsymbol *ClassDeclaration::syntaxCopy(Dsymbol *s) | |
201 { | |
202 ClassDeclaration *cd; | |
203 | |
204 //printf("ClassDeclaration::syntaxCopy('%s')\n", toChars()); | |
205 if (s) | |
206 cd = (ClassDeclaration *)s; | |
207 else | |
208 cd = new ClassDeclaration(loc, ident, NULL); | |
209 | |
210 cd->storage_class |= storage_class; | |
211 | |
212 cd->baseclasses.setDim(this->baseclasses.dim); | |
213 for (int i = 0; i < cd->baseclasses.dim; i++) | |
214 { | |
215 BaseClass *b = (BaseClass *)this->baseclasses.data[i]; | |
216 BaseClass *b2 = new BaseClass(b->type->syntaxCopy(), b->protection); | |
217 cd->baseclasses.data[i] = b2; | |
218 } | |
219 | |
220 ScopeDsymbol::syntaxCopy(cd); | |
221 return cd; | |
222 } | |
223 | |
224 void ClassDeclaration::semantic(Scope *sc) | |
225 { int i; | |
226 unsigned offset; | |
227 | |
228 //printf("ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", toChars(), type, sizeok, this); | |
229 //printf("\tparent = %p, '%s'\n", sc->parent, sc->parent ? sc->parent->toChars() : ""); | |
230 //printf("sc->stc = %x\n", sc->stc); | |
231 | |
232 //{ static int n; if (++n == 20) *(char*)0=0; } | |
233 | |
234 if (!ident) // if anonymous class | |
1587 | 235 { const char *id = "__anonclass"; |
159 | 236 |
237 ident = Identifier::generateId(id); | |
238 } | |
239 | |
1587 | 240 if (!sc) |
241 sc = scope; | |
242 if (!parent && sc->parent && !sc->parent->isModule()) | |
243 parent = sc->parent; | |
159 | 244 |
1587 | 245 type = type->semantic(loc, sc); |
246 handle = type; | |
247 | |
159 | 248 if (!members) // if forward reference |
249 { //printf("\tclass '%s' is forward referenced\n", toChars()); | |
250 return; | |
251 } | |
252 if (symtab) | |
253 { if (!scope) | |
254 { //printf("\tsemantic for '%s' is already completed\n", toChars()); | |
255 return; // semantic() already completed | |
256 } | |
257 } | |
258 else | |
259 symtab = new DsymbolTable(); | |
260 | |
261 Scope *scx = NULL; | |
262 if (scope) | |
263 { sc = scope; | |
264 scx = scope; // save so we don't make redundant copies | |
265 scope = NULL; | |
266 } | |
267 #ifdef IN_GCC | |
268 methods.setDim(0); | |
269 #endif | |
270 | |
271 if (sc->stc & STCdeprecated) | |
336 | 272 { |
159 | 273 isdeprecated = 1; |
274 } | |
275 | |
276 // Expand any tuples in baseclasses[] | |
277 for (i = 0; i < baseclasses.dim; ) | |
278 { BaseClass *b = (BaseClass *)baseclasses.data[i]; | |
279 b->type = b->type->semantic(loc, sc); | |
280 Type *tb = b->type->toBasetype(); | |
281 | |
282 if (tb->ty == Ttuple) | |
283 { TypeTuple *tup = (TypeTuple *)tb; | |
284 enum PROT protection = b->protection; | |
285 baseclasses.remove(i); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
286 size_t dim = Parameter::dim(tup->arguments); |
159 | 287 for (size_t j = 0; j < dim; j++) |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
288 { Parameter *arg = Parameter::getNth(tup->arguments, j); |
159 | 289 b = new BaseClass(arg->type, protection); |
290 baseclasses.insert(i + j, b); | |
291 } | |
292 } | |
293 else | |
294 i++; | |
295 } | |
296 | |
297 // See if there's a base class as first in baseclasses[] | |
298 if (baseclasses.dim) | |
299 { TypeClass *tc; | |
300 BaseClass *b; | |
301 Type *tb; | |
302 | |
303 b = (BaseClass *)baseclasses.data[0]; | |
304 //b->type = b->type->semantic(loc, sc); | |
305 tb = b->type->toBasetype(); | |
306 if (tb->ty != Tclass) | |
307 { error("base type must be class or interface, not %s", b->type->toChars()); | |
308 baseclasses.remove(0); | |
309 } | |
310 else | |
311 { | |
312 tc = (TypeClass *)(tb); | |
336 | 313 |
159 | 314 if (tc->sym->isDeprecated()) |
315 { | |
316 if (!isDeprecated()) | |
317 { | |
318 // Deriving from deprecated class makes this one deprecated too | |
319 isdeprecated = 1; | |
320 | |
321 tc->checkDeprecated(loc, sc); | |
322 } | |
323 } | |
324 | |
325 if (tc->sym->isInterfaceDeclaration()) | |
326 ; | |
327 else | |
328 { | |
329 for (ClassDeclaration *cdb = tc->sym; cdb; cdb = cdb->baseClass) | |
330 { | |
331 if (cdb == this) | |
332 { | |
333 error("circular inheritance"); | |
334 baseclasses.remove(0); | |
335 goto L7; | |
336 } | |
337 } | |
1587 | 338 if (!tc->sym->symtab || tc->sym->sizeok == 0) |
339 { // Try to resolve forward reference | |
340 if (sc->mustsemantic && tc->sym->scope) | |
341 tc->sym->semantic(NULL); | |
342 } | |
159 | 343 if (!tc->sym->symtab || tc->sym->scope || tc->sym->sizeok == 0) |
344 { | |
1587 | 345 //printf("%s: forward reference of base class %s\n", toChars(), tc->sym->toChars()); |
159 | 346 //error("forward reference of base class %s", baseClass->toChars()); |
347 // Forward reference of base class, try again later | |
348 //printf("\ttry later, forward reference of base class %s\n", tc->sym->toChars()); | |
349 scope = scx ? scx : new Scope(*sc); | |
350 scope->setNoFree(); | |
1587 | 351 if (tc->sym->scope) |
352 tc->sym->scope->module->addDeferredSemantic(tc->sym); | |
159 | 353 scope->module->addDeferredSemantic(this); |
354 return; | |
355 } | |
356 else | |
357 { baseClass = tc->sym; | |
358 b->base = baseClass; | |
359 } | |
360 L7: ; | |
361 } | |
362 } | |
363 } | |
364 | |
365 // Treat the remaining entries in baseclasses as interfaces | |
366 // Check for errors, handle forward references | |
367 for (i = (baseClass ? 1 : 0); i < baseclasses.dim; ) | |
368 { TypeClass *tc; | |
369 BaseClass *b; | |
370 Type *tb; | |
371 | |
372 b = (BaseClass *)baseclasses.data[i]; | |
373 b->type = b->type->semantic(loc, sc); | |
374 tb = b->type->toBasetype(); | |
375 if (tb->ty == Tclass) | |
376 tc = (TypeClass *)tb; | |
377 else | |
378 tc = NULL; | |
379 if (!tc || !tc->sym->isInterfaceDeclaration()) | |
380 { | |
381 error("base type must be interface, not %s", b->type->toChars()); | |
382 baseclasses.remove(i); | |
383 continue; | |
384 } | |
385 else | |
386 { | |
387 if (tc->sym->isDeprecated()) | |
388 { | |
389 if (!isDeprecated()) | |
390 { | |
391 // Deriving from deprecated class makes this one deprecated too | |
392 isdeprecated = 1; | |
393 | |
394 tc->checkDeprecated(loc, sc); | |
395 } | |
396 } | |
397 | |
398 // Check for duplicate interfaces | |
399 for (size_t j = (baseClass ? 1 : 0); j < i; j++) | |
400 { | |
401 BaseClass *b2 = (BaseClass *)baseclasses.data[j]; | |
402 if (b2->base == tc->sym) | |
403 error("inherits from duplicate interface %s", b2->base->toChars()); | |
404 } | |
405 | |
1587 | 406 if (!tc->sym->symtab) |
407 { // Try to resolve forward reference | |
408 if (sc->mustsemantic && tc->sym->scope) | |
409 tc->sym->semantic(NULL); | |
410 } | |
411 | |
159 | 412 b->base = tc->sym; |
413 if (!b->base->symtab || b->base->scope) | |
414 { | |
415 //error("forward reference of base class %s", baseClass->toChars()); | |
416 // Forward reference of base, try again later | |
417 //printf("\ttry later, forward reference of base %s\n", baseClass->toChars()); | |
418 scope = scx ? scx : new Scope(*sc); | |
419 scope->setNoFree(); | |
1587 | 420 if (tc->sym->scope) |
421 tc->sym->scope->module->addDeferredSemantic(tc->sym); | |
159 | 422 scope->module->addDeferredSemantic(this); |
423 return; | |
424 } | |
425 } | |
426 i++; | |
427 } | |
428 | |
429 | |
430 // If no base class, and this is not an Object, use Object as base class | |
431 if (!baseClass && ident != Id::Object) | |
432 { | |
433 // BUG: what if Object is redefined in an inner scope? | |
434 Type *tbase = new TypeIdentifier(0, Id::Object); | |
435 BaseClass *b; | |
436 TypeClass *tc; | |
437 Type *bt; | |
438 | |
439 if (!object) | |
440 { | |
441 error("missing or corrupt object.d"); | |
442 fatal(); | |
443 } | |
444 bt = tbase->semantic(loc, sc)->toBasetype(); | |
445 b = new BaseClass(bt, PROTpublic); | |
446 baseclasses.shift(b); | |
447 assert(b->type->ty == Tclass); | |
448 tc = (TypeClass *)(b->type); | |
449 baseClass = tc->sym; | |
450 assert(!baseClass->isInterfaceDeclaration()); | |
451 b->base = baseClass; | |
452 } | |
453 | |
454 interfaces_dim = baseclasses.dim; | |
455 interfaces = (BaseClass **)baseclasses.data; | |
456 | |
457 | |
458 if (baseClass) | |
459 { | |
460 if (baseClass->storage_class & STCfinal) | |
461 error("cannot inherit from final class %s", baseClass->toChars()); | |
462 | |
463 interfaces_dim--; | |
464 interfaces++; | |
465 | |
466 // Copy vtbl[] from base class | |
467 vtbl.setDim(baseClass->vtbl.dim); | |
468 memcpy(vtbl.data, baseClass->vtbl.data, sizeof(void *) * vtbl.dim); | |
469 | |
470 // Inherit properties from base class | |
471 com = baseClass->isCOMclass(); | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
472 isscope = baseClass->isscope; |
159 | 473 vthis = baseClass->vthis; |
474 } | |
475 else | |
476 { | |
477 // No base class, so this is the root of the class hierarchy | |
478 vtbl.setDim(0); | |
479 vtbl.push(this); // leave room for classinfo as first member | |
480 } | |
481 | |
482 protection = sc->protection; | |
483 storage_class |= sc->stc; | |
484 | |
485 if (sizeok == 0) | |
486 { | |
487 interfaceSemantic(sc); | |
488 | |
489 for (i = 0; i < members->dim; i++) | |
490 { | |
491 Dsymbol *s = (Dsymbol *)members->data[i]; | |
492 s->addMember(sc, this, 1); | |
493 } | |
494 | |
495 /* If this is a nested class, add the hidden 'this' | |
496 * member which is a pointer to the enclosing scope. | |
497 */ | |
498 if (vthis) // if inheriting from nested class | |
499 { // Use the base class's 'this' member | |
500 isnested = 1; | |
501 if (storage_class & STCstatic) | |
502 error("static class cannot inherit from nested class %s", baseClass->toChars()); | |
503 if (toParent2() != baseClass->toParent2()) | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
504 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
505 if (toParent2()) |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
506 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
507 error("is nested within %s, but super class %s is nested within %s", |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
508 toParent2()->toChars(), |
159 | 509 baseClass->toChars(), |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
510 baseClass->toParent2()->toChars()); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
511 } |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
512 else |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
513 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
514 error("is not nested, but super class %s is nested within %s", |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
515 baseClass->toChars(), |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
516 baseClass->toParent2()->toChars()); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
517 } |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
518 isnested = 0; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
519 } |
159 | 520 } |
521 else if (!(storage_class & STCstatic)) | |
522 { Dsymbol *s = toParent2(); | |
523 if (s) | |
524 { | |
1587 | 525 AggregateDeclaration *ad = s->isClassDeclaration(); |
159 | 526 FuncDeclaration *fd = s->isFuncDeclaration(); |
527 | |
528 | |
1587 | 529 if (ad || fd) |
159 | 530 { isnested = 1; |
531 Type *t; | |
1587 | 532 if (ad) |
533 t = ad->handle; | |
159 | 534 else if (fd) |
535 { AggregateDeclaration *ad = fd->isMember2(); | |
536 if (ad) | |
537 t = ad->handle; | |
538 else | |
539 { | |
540 t = new TypePointer(Type::tvoid); | |
541 t = t->semantic(0, sc); | |
542 } | |
543 } | |
544 else | |
545 assert(0); | |
546 assert(!vthis); | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
547 vthis = new ThisDeclaration(loc, t); |
159 | 548 members->push(vthis); |
549 } | |
550 } | |
551 } | |
552 } | |
553 | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
554 if (storage_class & STCauto) |
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
555 error("storage class has no effect: auto"); |
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
556 if (storage_class & STCscope) |
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
557 isscope = 1; |
159 | 558 if (storage_class & STCabstract) |
559 isabstract = 1; | |
560 | |
561 sc = sc->push(this); | |
562 sc->stc &= ~(STCfinal | STCauto | STCscope | STCstatic | | |
563 STCabstract | STCdeprecated); | |
564 sc->parent = this; | |
565 sc->inunion = 0; | |
566 | |
567 if (isCOMclass()) | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
568 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
569 #if _WIN32 |
159 | 570 sc->linkage = LINKwindows; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
571 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
572 /* This enables us to use COM objects under Linux and |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
573 * work with things like XPCOM |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
574 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
575 sc->linkage = LINKc; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
576 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
577 } |
159 | 578 sc->protection = PROTpublic; |
579 sc->explicitProtection = 0; | |
580 sc->structalign = 8; | |
581 structalign = sc->structalign; | |
582 if (baseClass) | |
583 { sc->offset = baseClass->structsize; | |
584 alignsize = baseClass->alignsize; | |
585 // if (isnested) | |
586 // sc->offset += PTRSIZE; // room for uplevel context pointer | |
587 } | |
588 else | |
1587 | 589 { sc->offset = PTRSIZE * 2; // allow room for __vptr and __monitor |
809
69a5e4a6fc0f
Changed some hardcoded offset/alignment for classes in DMD, broke offsets for 64bits.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
590 alignsize = PTRSIZE; |
159 | 591 } |
592 structsize = sc->offset; | |
593 Scope scsave = *sc; | |
594 int members_dim = members->dim; | |
595 sizeok = 0; | |
596 for (i = 0; i < members_dim; i++) | |
597 { | |
598 Dsymbol *s = (Dsymbol *)members->data[i]; | |
599 s->semantic(sc); | |
600 } | |
601 | |
602 if (sizeok == 2) | |
603 { // semantic() failed because of forward references. | |
604 // Unwind what we did, and defer it for later | |
605 fields.setDim(0); | |
606 structsize = 0; | |
607 alignsize = 0; | |
608 structalign = 0; | |
609 | |
610 sc = sc->pop(); | |
611 | |
612 scope = scx ? scx : new Scope(*sc); | |
613 scope->setNoFree(); | |
614 scope->module->addDeferredSemantic(this); | |
615 | |
336 | 616 //printf("\tsemantic('%s') failed due to forward references\n", toChars()); |
159 | 617 return; |
618 } | |
619 | |
620 //printf("\tsemantic('%s') successful\n", toChars()); | |
621 | |
622 structsize = sc->offset; | |
623 //members->print(); | |
624 | |
625 /* Look for special member functions. | |
626 * They must be in this class, not in a base class. | |
627 */ | |
628 ctor = (CtorDeclaration *)search(0, Id::ctor, 0); | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
629 if (ctor && (ctor->toParent() != this || !ctor->isCtorDeclaration())) |
159 | 630 ctor = NULL; |
631 | |
632 // dtor = (DtorDeclaration *)search(Id::dtor, 0); | |
633 // if (dtor && dtor->toParent() != this) | |
634 // dtor = NULL; | |
635 | |
636 // inv = (InvariantDeclaration *)search(Id::classInvariant, 0); | |
637 // if (inv && inv->toParent() != this) | |
638 // inv = NULL; | |
639 | |
640 // Can be in base class | |
641 aggNew = (NewDeclaration *)search(0, Id::classNew, 0); | |
642 aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0); | |
643 | |
644 // If this class has no constructor, but base class does, create | |
645 // a constructor: | |
646 // this() { } | |
647 if (!ctor && baseClass && baseClass->ctor) | |
648 { | |
649 //printf("Creating default this(){} for class %s\n", toChars()); | |
336 | 650 ctor = new CtorDeclaration(loc, 0, NULL, 0); |
159 | 651 ctor->fbody = new CompoundStatement(0, new Statements()); |
652 members->push(ctor); | |
653 ctor->addMember(sc, this, 1); | |
654 *sc = scsave; // why? What about sc->nofree? | |
655 sc->offset = structsize; | |
656 ctor->semantic(sc); | |
657 defaultCtor = ctor; | |
658 } | |
659 | |
660 #if 0 | |
661 if (baseClass) | |
662 { if (!aggDelete) | |
663 aggDelete = baseClass->aggDelete; | |
664 if (!aggNew) | |
665 aggNew = baseClass->aggNew; | |
666 } | |
667 #endif | |
668 | |
669 // Allocate instance of each new interface | |
670 for (i = 0; i < vtblInterfaces->dim; i++) | |
671 { | |
672 BaseClass *b = (BaseClass *)vtblInterfaces->data[i]; | |
673 unsigned thissize = PTRSIZE; | |
674 | |
675 alignmember(structalign, thissize, &sc->offset); | |
676 assert(b->offset == 0); | |
677 b->offset = sc->offset; | |
678 | |
679 // Take care of single inheritance offsets | |
680 while (b->baseInterfaces_dim) | |
681 { | |
682 b = &b->baseInterfaces[0]; | |
683 b->offset = sc->offset; | |
684 } | |
685 | |
686 sc->offset += thissize; | |
687 if (alignsize < thissize) | |
688 alignsize = thissize; | |
689 } | |
690 structsize = sc->offset; | |
691 sizeok = 1; | |
692 Module::dprogress++; | |
693 | |
694 dtor = buildDtor(sc); | |
695 | |
696 sc->pop(); | |
697 | |
698 #if 0 // Do not call until toObjfile() because of forward references | |
699 // Fill in base class vtbl[]s | |
700 for (i = 0; i < vtblInterfaces->dim; i++) | |
701 { | |
702 BaseClass *b = (BaseClass *)vtblInterfaces->data[i]; | |
703 | |
704 //b->fillVtbl(this, &b->vtbl, 1); | |
705 } | |
706 #endif | |
707 //printf("-ClassDeclaration::semantic(%s), type = %p\n", toChars(), type); | |
708 } | |
709 | |
710 void ClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
711 { | |
712 if (!isAnonymous()) | |
713 { | |
714 buf->printf("%s ", kind()); | |
715 buf->writestring(toChars()); | |
716 if (baseclasses.dim) | |
717 buf->writestring(" : "); | |
718 } | |
719 for (int i = 0; i < baseclasses.dim; i++) | |
720 { | |
721 BaseClass *b = (BaseClass *)baseclasses.data[i]; | |
722 | |
723 if (i) | |
724 buf->writeByte(','); | |
725 //buf->writestring(b->base->ident->toChars()); | |
726 b->type->toCBuffer(buf, NULL, hgs); | |
727 } | |
1587 | 728 if (members) |
159 | 729 { |
1587 | 730 buf->writenl(); |
731 buf->writeByte('{'); | |
732 buf->writenl(); | |
733 for (int i = 0; i < members->dim; i++) | |
734 { | |
735 Dsymbol *s = (Dsymbol *)members->data[i]; | |
159 | 736 |
1587 | 737 buf->writestring(" "); |
738 s->toCBuffer(buf, hgs); | |
739 } | |
740 buf->writestring("}"); | |
159 | 741 } |
1587 | 742 else |
743 buf->writeByte(';'); | |
159 | 744 buf->writenl(); |
745 } | |
746 | |
747 #if 0 | |
748 void ClassDeclaration::defineRef(Dsymbol *s) | |
749 { | |
750 ClassDeclaration *cd; | |
751 | |
752 AggregateDeclaration::defineRef(s); | |
753 cd = s->isClassDeclaration(); | |
754 baseType = cd->baseType; | |
755 cd->baseType = NULL; | |
756 } | |
757 #endif | |
758 | |
759 /********************************************* | |
760 * Determine if 'this' is a base class of cd. | |
761 * This is used to detect circular inheritance only. | |
762 */ | |
763 | |
764 int ClassDeclaration::isBaseOf2(ClassDeclaration *cd) | |
765 { | |
766 if (!cd) | |
767 return 0; | |
768 //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd->toChars()); | |
769 for (int i = 0; i < cd->baseclasses.dim; i++) | |
770 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
771 | |
772 if (b->base == this || isBaseOf2(b->base)) | |
773 return 1; | |
774 } | |
775 return 0; | |
776 } | |
777 | |
778 /******************************************* | |
779 * Determine if 'this' is a base class of cd. | |
780 */ | |
781 | |
782 int ClassDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset) | |
783 { | |
784 //printf("ClassDeclaration::isBaseOf(this = '%s', cd = '%s')\n", toChars(), cd->toChars()); | |
785 if (poffset) | |
786 *poffset = 0; | |
787 while (cd) | |
788 { | |
789 if (this == cd->baseClass) | |
790 return 1; | |
791 | |
792 /* cd->baseClass might not be set if cd is forward referenced. | |
793 */ | |
794 if (!cd->baseClass && cd->baseclasses.dim && !cd->isInterfaceDeclaration()) | |
795 { | |
796 cd->error("base class is forward referenced by %s", toChars()); | |
797 } | |
798 | |
799 cd = cd->baseClass; | |
800 } | |
801 return 0; | |
802 } | |
803 | |
804 Dsymbol *ClassDeclaration::search(Loc loc, Identifier *ident, int flags) | |
805 { | |
806 Dsymbol *s; | |
1587 | 807 //printf("%s.ClassDeclaration::search('%s')\n", toChars(), ident->toChars()); |
159 | 808 |
809 if (scope) | |
1587 | 810 { Scope *sc = scope; |
811 sc->mustsemantic++; | |
812 semantic(sc); | |
813 sc->mustsemantic--; | |
814 } | |
159 | 815 |
816 if (!members || !symtab || scope) | |
1587 | 817 { |
818 error("is forward referenced when looking for '%s'", ident->toChars()); | |
159 | 819 //*(char*)0=0; |
820 return NULL; | |
821 } | |
822 | |
823 s = ScopeDsymbol::search(loc, ident, flags); | |
824 if (!s) | |
825 { | |
826 // Search bases classes in depth-first, left to right order | |
827 | |
828 int i; | |
829 | |
830 for (i = 0; i < baseclasses.dim; i++) | |
831 { | |
832 BaseClass *b = (BaseClass *)baseclasses.data[i]; | |
833 | |
834 if (b->base) | |
835 { | |
836 if (!b->base->symtab) | |
837 error("base %s is forward referenced", b->base->ident->toChars()); | |
838 else | |
839 { | |
840 s = b->base->search(loc, ident, flags); | |
841 if (s == this) // happens if s is nested in this and derives from this | |
842 s = NULL; | |
843 else if (s) | |
844 break; | |
845 } | |
846 } | |
847 } | |
848 } | |
849 return s; | |
850 } | |
851 | |
852 /********************************************************** | |
853 * fd is in the vtbl[] for this class. | |
854 * Return 1 if function is hidden (not findable through search). | |
855 */ | |
856 | |
336 | 857 #if DMDV2 |
159 | 858 int isf(void *param, FuncDeclaration *fd) |
859 { | |
860 //printf("param = %p, fd = %p %s\n", param, fd, fd->toChars()); | |
861 return param == fd; | |
862 } | |
863 | |
864 int ClassDeclaration::isFuncHidden(FuncDeclaration *fd) | |
865 { | |
1587 | 866 //printf("ClassDeclaration::isFuncHidden(class = %s, fd = %s)\n", toChars(), fd->toChars()); |
159 | 867 Dsymbol *s = search(0, fd->ident, 4|2); |
868 if (!s) | |
869 { //printf("not found\n"); | |
870 /* Because, due to a hack, if there are multiple definitions | |
871 * of fd->ident, NULL is returned. | |
872 */ | |
873 return 0; | |
874 } | |
875 FuncDeclaration *fdstart = s->toAlias()->isFuncDeclaration(); | |
876 //printf("%s fdstart = %p\n", s->kind(), fdstart); | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
877 return !overloadApply(getModule(), fdstart, &isf, fd); |
159 | 878 } |
879 #endif | |
880 | |
881 /**************** | |
882 * Find virtual function matching identifier and type. | |
883 * Used to build virtual function tables for interface implementations. | |
884 */ | |
885 | |
886 FuncDeclaration *ClassDeclaration::findFunc(Identifier *ident, TypeFunction *tf) | |
887 { | |
888 //printf("ClassDeclaration::findFunc(%s, %s) %s\n", ident->toChars(), tf->toChars(), toChars()); | |
889 | |
890 ClassDeclaration *cd = this; | |
891 Array *vtbl = &cd->vtbl; | |
892 while (1) | |
893 { | |
894 for (size_t i = 0; i < vtbl->dim; i++) | |
895 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
896 FuncDeclaration *fd = ((Dsymbol*)vtbl->data[i])->isFuncDeclaration(); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
897 if (!fd) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
898 continue; // the first entry might be a ClassInfo |
159 | 899 |
900 //printf("\t[%d] = %s\n", i, fd->toChars()); | |
901 if (ident == fd->ident && | |
902 //tf->equals(fd->type) | |
903 fd->type->covariant(tf) == 1 | |
904 ) | |
905 { //printf("\t\tfound\n"); | |
906 return fd; | |
907 } | |
908 //else printf("\t\t%d\n", fd->type->covariant(tf)); | |
909 } | |
910 if (!cd) | |
911 break; | |
912 vtbl = &cd->vtblFinal; | |
913 cd = cd->baseClass; | |
914 } | |
915 | |
916 return NULL; | |
917 } | |
918 | |
919 void ClassDeclaration::interfaceSemantic(Scope *sc) | |
336 | 920 { |
159 | 921 vtblInterfaces = new BaseClasses(); |
922 vtblInterfaces->reserve(interfaces_dim); | |
923 | |
336 | 924 for (size_t i = 0; i < interfaces_dim; i++) |
159 | 925 { |
926 BaseClass *b = interfaces[i]; | |
927 | |
928 // If this is an interface, and it derives from a COM interface, | |
929 // then this is a COM interface too. | |
930 if (b->base->isCOMinterface()) | |
931 com = 1; | |
932 | |
933 vtblInterfaces->push(b); | |
934 b->copyBaseInterfaces(vtblInterfaces); | |
935 } | |
936 } | |
937 | |
938 /**************************************** | |
939 */ | |
940 | |
941 int ClassDeclaration::isCOMclass() | |
942 { | |
943 return com; | |
944 } | |
945 | |
946 int ClassDeclaration::isCOMinterface() | |
947 { | |
948 return 0; | |
949 } | |
950 | |
1587 | 951 #if DMDV2 |
952 int ClassDeclaration::isCPPinterface() | |
953 { | |
954 return 0; | |
955 } | |
956 #endif | |
957 | |
159 | 958 |
959 /**************************************** | |
960 */ | |
961 | |
962 int ClassDeclaration::isAbstract() | |
963 { | |
964 if (isabstract) | |
965 return TRUE; | |
966 for (int i = 1; i < vtbl.dim; i++) | |
967 { | |
968 FuncDeclaration *fd = ((Dsymbol *)vtbl.data[i])->isFuncDeclaration(); | |
969 | |
970 //printf("\tvtbl[%d] = %p\n", i, fd); | |
971 if (!fd || fd->isAbstract()) | |
972 { | |
973 isabstract |= 1; | |
974 return TRUE; | |
975 } | |
976 } | |
977 return FALSE; | |
978 } | |
979 | |
336 | 980 |
159 | 981 /**************************************** |
982 * Returns !=0 if there's an extra member which is the 'this' | |
983 * pointer to the enclosing context (enclosing class or function) | |
984 */ | |
985 | |
986 int ClassDeclaration::isNested() | |
987 { | |
988 return isnested; | |
989 } | |
990 | |
991 /**************************************** | |
992 * Determine if slot 0 of the vtbl[] is reserved for something else. | |
993 * For class objects, yes, this is where the classinfo ptr goes. | |
994 * For COM interfaces, no. | |
995 * For non-COM interfaces, yes, this is where the Interface ptr goes. | |
996 */ | |
997 | |
998 int ClassDeclaration::vtblOffset() | |
999 { | |
1000 return 1; | |
1001 } | |
1002 | |
1003 /**************************************** | |
1004 */ | |
1005 | |
336 | 1006 const char *ClassDeclaration::kind() |
159 | 1007 { |
1008 return "class"; | |
1009 } | |
1010 | |
1011 /**************************************** | |
1012 */ | |
1013 | |
1014 void ClassDeclaration::addLocalClass(ClassDeclarations *aclasses) | |
1015 { | |
1016 aclasses->push(this); | |
1017 } | |
1018 | |
1019 /********************************* InterfaceDeclaration ****************************/ | |
1020 | |
1021 InterfaceDeclaration::InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses) | |
1022 : ClassDeclaration(loc, id, baseclasses) | |
1023 { | |
1024 com = 0; | |
1025 if (id == Id::IUnknown) // IUnknown is the root of all COM objects | |
1026 com = 1; | |
1027 } | |
1028 | |
1029 Dsymbol *InterfaceDeclaration::syntaxCopy(Dsymbol *s) | |
1030 { | |
1031 InterfaceDeclaration *id; | |
1032 | |
1033 if (s) | |
1034 id = (InterfaceDeclaration *)s; | |
1035 else | |
1036 id = new InterfaceDeclaration(loc, ident, NULL); | |
1037 | |
1038 ClassDeclaration::syntaxCopy(id); | |
1039 return id; | |
1040 } | |
1041 | |
1042 void InterfaceDeclaration::semantic(Scope *sc) | |
1043 { int i; | |
1044 | |
1045 //printf("InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type); | |
1046 if (inuse) | |
1047 return; | |
1587 | 1048 |
1049 if (!sc) | |
1050 sc = scope; | |
1051 if (!parent && sc->parent && !sc->parent->isModule()) | |
1052 parent = sc->parent; | |
1053 | |
1054 type = type->semantic(loc, sc); | |
1055 handle = type; | |
1056 | |
159 | 1057 if (!members) // if forward reference |
1058 { //printf("\tinterface '%s' is forward referenced\n", toChars()); | |
1059 return; | |
1060 } | |
1061 if (symtab) // if already done | |
1062 { if (!scope) | |
1063 return; | |
1064 } | |
1065 else | |
1066 symtab = new DsymbolTable(); | |
1067 | |
1068 Scope *scx = NULL; | |
1069 if (scope) | |
1070 { sc = scope; | |
1071 scx = scope; // save so we don't make redundant copies | |
1072 scope = NULL; | |
1073 } | |
1074 | |
1075 if (sc->stc & STCdeprecated) | |
1076 { | |
1077 isdeprecated = 1; | |
1078 } | |
1079 | |
1080 // Expand any tuples in baseclasses[] | |
1081 for (i = 0; i < baseclasses.dim; ) | |
1082 { BaseClass *b = (BaseClass *)baseclasses.data[0]; | |
1083 b->type = b->type->semantic(loc, sc); | |
1084 Type *tb = b->type->toBasetype(); | |
1085 | |
1086 if (tb->ty == Ttuple) | |
1087 { TypeTuple *tup = (TypeTuple *)tb; | |
1088 enum PROT protection = b->protection; | |
1089 baseclasses.remove(i); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1090 size_t dim = Parameter::dim(tup->arguments); |
159 | 1091 for (size_t j = 0; j < dim; j++) |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1092 { Parameter *arg = Parameter::getNth(tup->arguments, j); |
159 | 1093 b = new BaseClass(arg->type, protection); |
1094 baseclasses.insert(i + j, b); | |
1095 } | |
1096 } | |
1097 else | |
1098 i++; | |
1099 } | |
1100 | |
1101 // Check for errors, handle forward references | |
1102 for (i = 0; i < baseclasses.dim; ) | |
1103 { TypeClass *tc; | |
1104 BaseClass *b; | |
1105 Type *tb; | |
1106 | |
1107 b = (BaseClass *)baseclasses.data[i]; | |
1108 b->type = b->type->semantic(loc, sc); | |
1109 tb = b->type->toBasetype(); | |
1110 if (tb->ty == Tclass) | |
1111 tc = (TypeClass *)tb; | |
1112 else | |
1113 tc = NULL; | |
1114 if (!tc || !tc->sym->isInterfaceDeclaration()) | |
1115 { | |
1116 error("base type must be interface, not %s", b->type->toChars()); | |
1117 baseclasses.remove(i); | |
1118 continue; | |
1119 } | |
1120 else | |
1121 { | |
1122 // Check for duplicate interfaces | |
1123 for (size_t j = 0; j < i; j++) | |
1124 { | |
1125 BaseClass *b2 = (BaseClass *)baseclasses.data[j]; | |
1126 if (b2->base == tc->sym) | |
1127 error("inherits from duplicate interface %s", b2->base->toChars()); | |
1128 } | |
1129 | |
1130 b->base = tc->sym; | |
1131 if (b->base == this || isBaseOf2(b->base)) | |
1132 { | |
1133 error("circular inheritance of interface"); | |
1134 baseclasses.remove(i); | |
1135 continue; | |
1136 } | |
1587 | 1137 if (!b->base->symtab) |
1138 { // Try to resolve forward reference | |
1139 if (sc->mustsemantic && b->base->scope) | |
1140 b->base->semantic(NULL); | |
1141 } | |
159 | 1142 if (!b->base->symtab || b->base->scope || b->base->inuse) |
1143 { | |
1144 //error("forward reference of base class %s", baseClass->toChars()); | |
1145 // Forward reference of base, try again later | |
1146 //printf("\ttry later, forward reference of base %s\n", b->base->toChars()); | |
1147 scope = scx ? scx : new Scope(*sc); | |
1148 scope->setNoFree(); | |
1149 scope->module->addDeferredSemantic(this); | |
1150 return; | |
1151 } | |
1152 } | |
1153 i++; | |
1154 } | |
1155 | |
1156 interfaces_dim = baseclasses.dim; | |
1157 interfaces = (BaseClass **)baseclasses.data; | |
1158 | |
1159 interfaceSemantic(sc); | |
1160 | |
1161 if (vtblOffset()) | |
1162 vtbl.push(this); // leave room at vtbl[0] for classinfo | |
1163 | |
1164 // Cat together the vtbl[]'s from base interfaces | |
1165 for (i = 0; i < interfaces_dim; i++) | |
1166 { BaseClass *b = interfaces[i]; | |
1167 | |
1168 // Skip if b has already appeared | |
1169 for (int k = 0; k < i; k++) | |
1170 { | |
1171 if (b == interfaces[i]) | |
1172 goto Lcontinue; | |
1173 } | |
1174 | |
1175 // Copy vtbl[] from base class | |
1176 if (b->base->vtblOffset()) | |
1177 { int d = b->base->vtbl.dim; | |
1178 if (d > 1) | |
1179 { | |
1180 vtbl.reserve(d - 1); | |
1181 for (int j = 1; j < d; j++) | |
1182 vtbl.push(b->base->vtbl.data[j]); | |
1183 } | |
1184 } | |
1185 else | |
1186 { | |
1187 vtbl.append(&b->base->vtbl); | |
1188 } | |
1189 | |
1190 Lcontinue: | |
1191 ; | |
1192 } | |
1193 | |
1194 for (i = 0; i < members->dim; i++) | |
1195 { | |
1196 Dsymbol *s = (Dsymbol *)members->data[i]; | |
1197 s->addMember(sc, this, 1); | |
1198 } | |
1199 | |
1200 sc = sc->push(this); | |
1201 sc->parent = this; | |
1202 if (isCOMinterface()) | |
1203 sc->linkage = LINKwindows; | |
1204 sc->structalign = 8; | |
1205 structalign = sc->structalign; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
1206 sc->offset = PTRSIZE * 2; |
159 | 1207 inuse++; |
1208 for (i = 0; i < members->dim; i++) | |
1209 { | |
1210 Dsymbol *s = (Dsymbol *)members->data[i]; | |
1211 s->semantic(sc); | |
1212 } | |
1213 inuse--; | |
1214 //members->print(); | |
1215 sc->pop(); | |
1216 //printf("-InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type); | |
1217 } | |
1218 | |
1219 | |
1220 /******************************************* | |
1221 * Determine if 'this' is a base class of cd. | |
1222 * (Actually, if it is an interface supported by cd) | |
1223 * Output: | |
1224 * *poffset offset to start of class | |
1225 * OFFSET_RUNTIME must determine offset at runtime | |
1226 * Returns: | |
1227 * 0 not a base | |
1228 * 1 is a base | |
1229 */ | |
1230 | |
1231 int InterfaceDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset) | |
1232 { | |
1233 unsigned j; | |
1234 | |
1235 //printf("%s.InterfaceDeclaration::isBaseOf(cd = '%s')\n", toChars(), cd->toChars()); | |
1236 assert(!baseClass); | |
1237 for (j = 0; j < cd->interfaces_dim; j++) | |
1238 { | |
1239 BaseClass *b = cd->interfaces[j]; | |
1240 | |
1241 //printf("\tbase %s\n", b->base->toChars()); | |
1242 if (this == b->base) | |
1243 { | |
1244 //printf("\tfound at offset %d\n", b->offset); | |
1245 if (poffset) | |
1246 { *poffset = b->offset; | |
1247 if (j && cd->isInterfaceDeclaration()) | |
1248 *poffset = OFFSET_RUNTIME; | |
1249 } | |
1250 return 1; | |
1251 } | |
1252 if (isBaseOf(b, poffset)) | |
1253 { if (j && poffset && cd->isInterfaceDeclaration()) | |
1254 *poffset = OFFSET_RUNTIME; | |
1255 return 1; | |
1256 } | |
1257 } | |
1258 | |
1259 if (cd->baseClass && isBaseOf(cd->baseClass, poffset)) | |
1260 return 1; | |
1261 | |
1262 if (poffset) | |
1263 *poffset = 0; | |
1264 return 0; | |
1265 } | |
1266 | |
1267 | |
1268 int InterfaceDeclaration::isBaseOf(BaseClass *bc, int *poffset) | |
1269 { | |
1270 //printf("%s.InterfaceDeclaration::isBaseOf(bc = '%s')\n", toChars(), bc->base->toChars()); | |
1271 for (unsigned j = 0; j < bc->baseInterfaces_dim; j++) | |
1272 { | |
1273 BaseClass *b = &bc->baseInterfaces[j]; | |
1274 | |
1275 if (this == b->base) | |
1276 { | |
1277 if (poffset) | |
1278 { *poffset = b->offset; | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1279 if (j && bc->base->isInterfaceDeclaration()) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1280 *poffset = OFFSET_RUNTIME; |
159 | 1281 } |
1282 return 1; | |
1283 } | |
1284 if (isBaseOf(b, poffset)) | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1285 { if (j && poffset && bc->base->isInterfaceDeclaration()) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1286 *poffset = OFFSET_RUNTIME; |
159 | 1287 return 1; |
1288 } | |
1289 } | |
1290 if (poffset) | |
1291 *poffset = 0; | |
1292 return 0; | |
1293 } | |
1294 | |
1295 /**************************************** | |
1296 * Determine if slot 0 of the vtbl[] is reserved for something else. | |
1297 * For class objects, yes, this is where the ClassInfo ptr goes. | |
1298 * For COM interfaces, no. | |
1299 * For non-COM interfaces, yes, this is where the Interface ptr goes. | |
1300 */ | |
1301 | |
1302 int InterfaceDeclaration::vtblOffset() | |
1303 { | |
1304 if (isCOMinterface()) | |
1305 return 0; | |
1306 return 1; | |
1307 } | |
1308 | |
1309 int InterfaceDeclaration::isCOMinterface() | |
1310 { | |
1311 return com; | |
1312 } | |
1313 | |
1587 | 1314 #if DMDV2 |
1315 int InterfaceDeclaration::isCPPinterface() | |
1316 { | |
1317 return cpp; | |
1318 } | |
1319 #endif | |
1320 | |
159 | 1321 /******************************************* |
1322 */ | |
1323 | |
336 | 1324 const char *InterfaceDeclaration::kind() |
159 | 1325 { |
1326 return "interface"; | |
1327 } | |
1328 | |
1329 | |
1330 /******************************** BaseClass *****************************/ | |
1331 | |
1332 BaseClass::BaseClass() | |
1333 { | |
1334 memset(this, 0, sizeof(BaseClass)); | |
1335 } | |
1336 | |
1337 BaseClass::BaseClass(Type *type, enum PROT protection) | |
1338 { | |
1339 //printf("BaseClass(this = %p, '%s')\n", this, type->toChars()); | |
1340 this->type = type; | |
1341 this->protection = protection; | |
1342 base = NULL; | |
1343 offset = 0; | |
1344 | |
1345 baseInterfaces_dim = 0; | |
1346 baseInterfaces = NULL; | |
1347 } | |
1348 | |
1349 /**************************************** | |
1350 * Fill in vtbl[] for base class based on member functions of class cd. | |
1351 * Input: | |
1352 * vtbl if !=NULL, fill it in | |
1353 * newinstance !=0 means all entries must be filled in by members | |
1354 * of cd, not members of any base classes of cd. | |
1355 * Returns: | |
1356 * !=0 if any entries were filled in by members of cd (not exclusively | |
1357 * by base classes) | |
1358 */ | |
1359 | |
1360 int BaseClass::fillVtbl(ClassDeclaration *cd, Array *vtbl, int newinstance) | |
1361 { | |
1362 ClassDeclaration *id = base; | |
1363 int j; | |
1364 int result = 0; | |
1365 | |
1366 //printf("BaseClass::fillVtbl(this='%s', cd='%s')\n", base->toChars(), cd->toChars()); | |
1367 if (vtbl) | |
1368 vtbl->setDim(base->vtbl.dim); | |
1369 | |
1370 // first entry is ClassInfo reference | |
1371 for (j = base->vtblOffset(); j < base->vtbl.dim; j++) | |
1372 { | |
1373 FuncDeclaration *ifd = ((Dsymbol *)base->vtbl.data[j])->isFuncDeclaration(); | |
1374 FuncDeclaration *fd; | |
1375 TypeFunction *tf; | |
1376 | |
1377 //printf(" vtbl[%d] is '%s'\n", j, ifd ? ifd->toChars() : "null"); | |
1378 | |
1379 assert(ifd); | |
1380 // Find corresponding function in this class | |
1381 tf = (ifd->type->ty == Tfunction) ? (TypeFunction *)(ifd->type) : NULL; | |
1382 fd = cd->findFunc(ifd->ident, tf); | |
1383 if (fd && !fd->isAbstract()) | |
1384 { | |
1385 //printf(" found\n"); | |
1386 // Check that calling conventions match | |
1387 if (fd->linkage != ifd->linkage) | |
1388 fd->error("linkage doesn't match interface function"); | |
1389 | |
1390 // Check that it is current | |
1391 if (newinstance && | |
1392 fd->toParent() != cd && | |
1393 ifd->toParent() == base) | |
1394 cd->error("interface function %s.%s is not implemented", | |
1395 id->toChars(), ifd->ident->toChars()); | |
1396 | |
1397 if (fd->toParent() == cd) | |
1398 result = 1; | |
1399 } | |
1400 else | |
1401 { | |
1402 //printf(" not found\n"); | |
1403 // BUG: should mark this class as abstract? | |
1404 if (!cd->isAbstract()) | |
1405 cd->error("interface function %s.%s isn't implemented", | |
1406 id->toChars(), ifd->ident->toChars()); | |
1407 fd = NULL; | |
1408 } | |
1409 if (vtbl) | |
1410 vtbl->data[j] = fd; | |
1411 } | |
1412 | |
1413 return result; | |
1414 } | |
1415 | |
1416 void BaseClass::copyBaseInterfaces(BaseClasses *vtblInterfaces) | |
1417 { | |
1418 //printf("+copyBaseInterfaces(), %s\n", base->toChars()); | |
1419 // if (baseInterfaces_dim) | |
1420 // return; | |
1421 | |
1422 baseInterfaces_dim = base->interfaces_dim; | |
1423 baseInterfaces = (BaseClass *)mem.calloc(baseInterfaces_dim, sizeof(BaseClass)); | |
1424 | |
1425 //printf("%s.copyBaseInterfaces()\n", base->toChars()); | |
1426 for (int i = 0; i < baseInterfaces_dim; i++) | |
1427 { | |
1428 BaseClass *b = &baseInterfaces[i]; | |
1429 BaseClass *b2 = base->interfaces[i]; | |
1430 | |
1431 assert(b2->vtbl.dim == 0); // should not be filled yet | |
1432 memcpy(b, b2, sizeof(BaseClass)); | |
1433 | |
1434 if (i) // single inheritance is i==0 | |
1435 vtblInterfaces->push(b); // only need for M.I. | |
1436 b->copyBaseInterfaces(vtblInterfaces); | |
1437 } | |
1438 //printf("-copyBaseInterfaces\n"); | |
1439 } |