-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclass.c
1648 lines (1408 loc) · 47 KB
/
class.c
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "root.h"
#include "rmem.h"
#include "enum.h"
#include "init.h"
#include "attrib.h"
#include "declaration.h"
#include "aggregate.h"
#include "id.h"
#include "mtype.h"
#include "scope.h"
#include "module.h"
#include "expression.h"
#include "statement.h"
/********************************* ClassDeclaration ****************************/
ClassDeclaration *ClassDeclaration::classinfo;
ClassDeclaration *ClassDeclaration::object;
ClassDeclaration *ClassDeclaration::throwable;
ClassDeclaration *ClassDeclaration::exception;
ClassDeclaration *ClassDeclaration::errorException;
ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses)
: AggregateDeclaration(loc, id)
{
static char msg[] = "only object.d can define this reserved class name";
if (baseclasses)
// Actually, this is a transfer
this->baseclasses = baseclasses;
else
this->baseclasses = new BaseClasses();
baseClass = NULL;
interfaces_dim = 0;
interfaces = NULL;
vtblInterfaces = NULL;
//printf("ClassDeclaration(%s), dim = %d\n", id->toChars(), this->baseclasses->dim);
// For forward references
type = new TypeClass(this);
handle = type;
staticCtor = NULL;
staticDtor = NULL;
vtblsym = NULL;
vclassinfo = NULL;
if (id)
{ // Look for special class names
if (id == Id::__sizeof || id == Id::__xalignof || id == Id::mangleof)
error("illegal class name");
// BUG: What if this is the wrong TypeInfo, i.e. it is nested?
if (id->toChars()[0] == 'T')
{
if (id == Id::TypeInfo)
{ if (Type::typeinfo)
Type::typeinfo->error("%s", msg);
Type::typeinfo = this;
}
if (id == Id::TypeInfo_Class)
{ if (Type::typeinfoclass)
Type::typeinfoclass->error("%s", msg);
Type::typeinfoclass = this;
}
if (id == Id::TypeInfo_Interface)
{ if (Type::typeinfointerface)
Type::typeinfointerface->error("%s", msg);
Type::typeinfointerface = this;
}
if (id == Id::TypeInfo_Struct)
{ if (Type::typeinfostruct)
Type::typeinfostruct->error("%s", msg);
Type::typeinfostruct = this;
}
if (id == Id::TypeInfo_Typedef)
{ if (Type::typeinfotypedef)
Type::typeinfotypedef->error("%s", msg);
Type::typeinfotypedef = this;
}
if (id == Id::TypeInfo_Pointer)
{ if (Type::typeinfopointer)
Type::typeinfopointer->error("%s", msg);
Type::typeinfopointer = this;
}
if (id == Id::TypeInfo_Array)
{ if (Type::typeinfoarray)
Type::typeinfoarray->error("%s", msg);
Type::typeinfoarray = this;
}
if (id == Id::TypeInfo_StaticArray)
{ //if (Type::typeinfostaticarray)
//Type::typeinfostaticarray->error("%s", msg);
Type::typeinfostaticarray = this;
}
if (id == Id::TypeInfo_AssociativeArray)
{ if (Type::typeinfoassociativearray)
Type::typeinfoassociativearray->error("%s", msg);
Type::typeinfoassociativearray = this;
}
if (id == Id::TypeInfo_Enum)
{ if (Type::typeinfoenum)
Type::typeinfoenum->error("%s", msg);
Type::typeinfoenum = this;
}
if (id == Id::TypeInfo_Function)
{ if (Type::typeinfofunction)
Type::typeinfofunction->error("%s", msg);
Type::typeinfofunction = this;
}
if (id == Id::TypeInfo_Delegate)
{ if (Type::typeinfodelegate)
Type::typeinfodelegate->error("%s", msg);
Type::typeinfodelegate = this;
}
if (id == Id::TypeInfo_Tuple)
{ if (Type::typeinfotypelist)
Type::typeinfotypelist->error("%s", msg);
Type::typeinfotypelist = this;
}
#if DMDV2
if (id == Id::TypeInfo_Const)
{ if (Type::typeinfoconst)
Type::typeinfoconst->error("%s", msg);
Type::typeinfoconst = this;
}
if (id == Id::TypeInfo_Invariant)
{ if (Type::typeinfoinvariant)
Type::typeinfoinvariant->error("%s", msg);
Type::typeinfoinvariant = this;
}
if (id == Id::TypeInfo_Shared)
{ if (Type::typeinfoshared)
Type::typeinfoshared->error("%s", msg);
Type::typeinfoshared = this;
}
if (id == Id::TypeInfo_Wild)
{ if (Type::typeinfowild)
Type::typeinfowild->error("%s", msg);
Type::typeinfowild = this;
}
#endif
}
if (id == Id::Object)
{ if (object)
object->error("%s", msg);
object = this;
}
if (id == Id::Throwable)
{ if (throwable)
throwable->error("%s", msg);
throwable = this;
}
if (id == Id::Exception)
{ if (exception)
exception->error("%s", msg);
exception = this;
}
if (id == Id::Error)
{ if (errorException)
errorException->error("%s", msg);
errorException = this;
}
//if (id == Id::ClassInfo)
if (id == Id::TypeInfo_Class)
{ if (classinfo)
classinfo->error("%s", msg);
classinfo = this;
}
if (id == Id::ModuleInfo)
{ if (Module::moduleinfo)
Module::moduleinfo->error("%s", msg);
Module::moduleinfo = this;
}
}
com = 0;
isscope = 0;
isabstract = 0;
inuse = 0;
}
Dsymbol *ClassDeclaration::syntaxCopy(Dsymbol *s)
{
ClassDeclaration *cd;
//printf("ClassDeclaration::syntaxCopy('%s')\n", toChars());
if (s)
cd = (ClassDeclaration *)s;
else
cd = new ClassDeclaration(loc, ident, NULL);
cd->storage_class |= storage_class;
cd->baseclasses->setDim(this->baseclasses->dim);
for (size_t i = 0; i < cd->baseclasses->dim; i++)
{
BaseClass *b = this->baseclasses->tdata()[i];
BaseClass *b2 = new BaseClass(b->type->syntaxCopy(), b->protection);
cd->baseclasses->tdata()[i] = b2;
}
ScopeDsymbol::syntaxCopy(cd);
return cd;
}
void ClassDeclaration::semantic(Scope *sc)
{
//printf("ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", toChars(), type, sizeok, this);
//printf("\tparent = %p, '%s'\n", sc->parent, sc->parent ? sc->parent->toChars() : "");
//printf("sc->stc = %x\n", sc->stc);
//{ static int n; if (++n == 20) *(char*)0=0; }
if (!ident) // if anonymous class
{ const char *id = "__anonclass";
ident = Identifier::generateId(id);
}
if (!sc)
sc = scope;
if (!parent && sc->parent && !sc->parent->isModule())
parent = sc->parent;
type = type->semantic(loc, sc);
handle = type;
if (!members) // if forward reference
{ //printf("\tclass '%s' is forward referenced\n", toChars());
return;
}
if (symtab)
{ if (sizeok == 1 || !scope)
{ //printf("\tsemantic for '%s' is already completed\n", toChars());
return; // semantic() already completed
}
}
else
symtab = new DsymbolTable();
Scope *scx = NULL;
if (scope)
{ sc = scope;
scx = scope; // save so we don't make redundant copies
scope = NULL;
}
unsigned dprogress_save = Module::dprogress;
#ifdef IN_GCC
methods.setDim(0);
#endif
int errors = global.gaggedErrors;
if (sc->stc & STCdeprecated)
{
isdeprecated = true;
}
if (sc->linkage == LINKcpp)
error("cannot create C++ classes");
// Expand any tuples in baseclasses[]
for (size_t i = 0; i < baseclasses->dim; )
{ BaseClass *b = baseclasses->tdata()[i];
b->type = b->type->semantic(loc, sc);
Type *tb = b->type->toBasetype();
if (tb->ty == Ttuple)
{ TypeTuple *tup = (TypeTuple *)tb;
enum PROT protection = b->protection;
baseclasses->remove(i);
size_t dim = Parameter::dim(tup->arguments);
for (size_t j = 0; j < dim; j++)
{ Parameter *arg = Parameter::getNth(tup->arguments, j);
b = new BaseClass(arg->type, protection);
baseclasses->insert(i + j, b);
}
}
else
i++;
}
// See if there's a base class as first in baseclasses[]
if (baseclasses->dim)
{ TypeClass *tc;
BaseClass *b;
Type *tb;
b = baseclasses->tdata()[0];
//b->type = b->type->semantic(loc, sc);
tb = b->type->toBasetype();
if (tb->ty != Tclass)
{ error("base type must be class or interface, not %s", b->type->toChars());
baseclasses->remove(0);
}
else
{
tc = (TypeClass *)(tb);
if (tc->sym->isDeprecated())
{
if (!isDeprecated())
{
// Deriving from deprecated class makes this one deprecated too
isdeprecated = true;
tc->checkDeprecated(loc, sc);
}
}
if (tc->sym->isInterfaceDeclaration())
;
else
{
for (ClassDeclaration *cdb = tc->sym; cdb; cdb = cdb->baseClass)
{
if (cdb == this)
{
error("circular inheritance");
baseclasses->remove(0);
goto L7;
}
}
if (!tc->sym->symtab || tc->sym->sizeok == 0)
{ // Try to resolve forward reference
if (/*sc->mustsemantic &&*/ tc->sym->scope)
tc->sym->semantic(NULL);
}
if (!tc->sym->symtab || tc->sym->scope || tc->sym->sizeok == 0)
{
//printf("%s: forward reference of base class %s\n", toChars(), tc->sym->toChars());
//error("forward reference of base class %s", baseClass->toChars());
// Forward reference of base class, try again later
//printf("\ttry later, forward reference of base class %s\n", tc->sym->toChars());
scope = scx ? scx : new Scope(*sc);
scope->setNoFree();
if (tc->sym->scope)
tc->sym->scope->module->addDeferredSemantic(tc->sym);
scope->module->addDeferredSemantic(this);
return;
}
else
{ baseClass = tc->sym;
b->base = baseClass;
}
L7: ;
}
}
}
// Treat the remaining entries in baseclasses as interfaces
// Check for errors, handle forward references
for (size_t i = (baseClass ? 1 : 0); i < baseclasses->dim; )
{ TypeClass *tc;
BaseClass *b;
Type *tb;
b = baseclasses->tdata()[i];
b->type = b->type->semantic(loc, sc);
tb = b->type->toBasetype();
if (tb->ty == Tclass)
tc = (TypeClass *)tb;
else
tc = NULL;
if (!tc || !tc->sym->isInterfaceDeclaration())
{
error("base type must be interface, not %s", b->type->toChars());
baseclasses->remove(i);
continue;
}
else
{
if (tc->sym->isDeprecated())
{
if (!isDeprecated())
{
// Deriving from deprecated class makes this one deprecated too
isdeprecated = true;
tc->checkDeprecated(loc, sc);
}
}
// Check for duplicate interfaces
for (size_t j = (baseClass ? 1 : 0); j < i; j++)
{
BaseClass *b2 = baseclasses->tdata()[j];
if (b2->base == tc->sym)
error("inherits from duplicate interface %s", b2->base->toChars());
}
if (!tc->sym->symtab)
{ // Try to resolve forward reference
if (/*sc->mustsemantic &&*/ tc->sym->scope)
tc->sym->semantic(NULL);
}
b->base = tc->sym;
if (!b->base->symtab || b->base->scope)
{
//error("forward reference of base class %s", baseClass->toChars());
// Forward reference of base, try again later
//printf("\ttry later, forward reference of base %s\n", baseClass->toChars());
scope = scx ? scx : new Scope(*sc);
scope->setNoFree();
if (tc->sym->scope)
tc->sym->scope->module->addDeferredSemantic(tc->sym);
scope->module->addDeferredSemantic(this);
return;
}
}
i++;
}
// If no base class, and this is not an Object, use Object as base class
if (!baseClass && ident != Id::Object)
{
// BUG: what if Object is redefined in an inner scope?
Type *tbase = new TypeIdentifier(0, Id::Object);
BaseClass *b;
TypeClass *tc;
Type *bt;
if (!object)
{
error("missing or corrupt object.d");
fatal();
}
bt = tbase->semantic(loc, sc)->toBasetype();
b = new BaseClass(bt, PROTpublic);
baseclasses->shift(b);
assert(b->type->ty == Tclass);
tc = (TypeClass *)(b->type);
baseClass = tc->sym;
assert(!baseClass->isInterfaceDeclaration());
b->base = baseClass;
}
interfaces_dim = baseclasses->dim;
interfaces = baseclasses->tdata();
if (baseClass)
{
if (baseClass->storage_class & STCfinal)
error("cannot inherit from final class %s", baseClass->toChars());
interfaces_dim--;
interfaces++;
// Copy vtbl[] from base class
vtbl.setDim(baseClass->vtbl.dim);
memcpy(vtbl.tdata(), baseClass->vtbl.tdata(), sizeof(void *) * vtbl.dim);
// Inherit properties from base class
com = baseClass->isCOMclass();
isscope = baseClass->isscope;
vthis = baseClass->vthis;
storage_class |= baseClass->storage_class & STC_TYPECTOR;
}
else
{
// No base class, so this is the root of the class hierarchy
vtbl.setDim(0);
vtbl.push(this); // leave room for classinfo as first member
}
protection = sc->protection;
storage_class |= sc->stc;
if (sizeok == 0)
{
interfaceSemantic(sc);
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = members->tdata()[i];
s->addMember(sc, this, 1);
}
/* If this is a nested class, add the hidden 'this'
* member which is a pointer to the enclosing scope.
*/
if (vthis) // if inheriting from nested class
{ // Use the base class's 'this' member
isnested = 1;
if (storage_class & STCstatic)
error("static class cannot inherit from nested class %s", baseClass->toChars());
if (toParent2() != baseClass->toParent2())
{
if (toParent2())
{
error("is nested within %s, but super class %s is nested within %s",
toParent2()->toChars(),
baseClass->toChars(),
baseClass->toParent2()->toChars());
}
else
{
error("is not nested, but super class %s is nested within %s",
baseClass->toChars(),
baseClass->toParent2()->toChars());
}
isnested = 0;
}
}
else if (!(storage_class & STCstatic))
{ Dsymbol *s = toParent2();
if (s)
{
AggregateDeclaration *ad = s->isClassDeclaration();
FuncDeclaration *fd = s->isFuncDeclaration();
if (ad || fd)
{ isnested = 1;
Type *t;
if (ad)
t = ad->handle;
else if (fd)
{ AggregateDeclaration *ad2 = fd->isMember2();
if (ad2)
t = ad2->handle;
else
{
t = Type::tvoidptr;
}
}
else
assert(0);
if (t->ty == Tstruct) // ref to struct
t = Type::tvoidptr;
assert(!vthis);
vthis = new ThisDeclaration(loc, t);
members->push(vthis);
}
}
}
}
if (storage_class & STCauto)
error("storage class 'auto' is invalid when declaring a class, did you mean to use 'scope'?");
if (storage_class & STCscope)
isscope = 1;
if (storage_class & STCabstract)
isabstract = 1;
if (storage_class & STCimmutable)
type = type->addMod(MODimmutable);
if (storage_class & STCconst)
type = type->addMod(MODconst);
if (storage_class & STCshared)
type = type->addMod(MODshared);
sc = sc->push(this);
//sc->stc &= ~(STCfinal | STCauto | STCscope | STCstatic | STCabstract | STCdeprecated | STC_TYPECTOR | STCtls | STCgshared);
//sc->stc |= storage_class & STC_TYPECTOR;
sc->stc &= STCsafe | STCtrusted | STCsystem;
sc->parent = this;
sc->inunion = 0;
if (isCOMclass())
{
#if _WIN32
sc->linkage = LINKwindows;
#else
/* This enables us to use COM objects under Linux and
* work with things like XPCOM
*/
sc->linkage = LINKc;
#endif
}
sc->protection = PROTpublic;
sc->explicitProtection = 0;
sc->structalign = 8;
structalign = sc->structalign;
if (baseClass)
{ sc->offset = baseClass->structsize;
alignsize = baseClass->alignsize;
// if (isnested)
// sc->offset += PTRSIZE; // room for uplevel context pointer
}
else
{ sc->offset = PTRSIZE * 2; // allow room for __vptr and __monitor
alignsize = PTRSIZE;
}
structsize = sc->offset;
Scope scsave = *sc;
size_t members_dim = members->dim;
sizeok = 0;
/* Set scope so if there are forward references, we still might be able to
* resolve individual members like enums.
*/
for (size_t i = 0; i < members_dim; i++)
{ Dsymbol *s = members->tdata()[i];
/* There are problems doing this in the general case because
* Scope keeps track of things like 'offset'
*/
if (s->isEnumDeclaration() || (s->isAggregateDeclaration() && s->ident))
{
//printf("setScope %s %s\n", s->kind(), s->toChars());
s->setScope(sc);
}
}
for (size_t i = 0; i < members_dim; i++)
{ Dsymbol *s = members->tdata()[i];
s->semantic(sc);
}
if (global.gag && global.gaggedErrors != errors)
{ // The type is no good, yet the error messages were gagged.
type = Type::terror;
}
if (sizeok == 2) // failed due to forward references
{ // semantic() failed due to forward references
// Unwind what we did, and defer it for later
fields.setDim(0);
structsize = 0;
alignsize = 0;
structalign = 0;
sc = sc->pop();
scope = scx ? scx : new Scope(*sc);
scope->setNoFree();
scope->module->addDeferredSemantic(this);
Module::dprogress = dprogress_save;
//printf("\tsemantic('%s') failed due to forward references\n", toChars());
return;
}
//printf("\tsemantic('%s') successful\n", toChars());
structsize = sc->offset;
//members->print();
/* Look for special member functions.
* They must be in this class, not in a base class.
*/
ctor = (CtorDeclaration *)search(0, Id::ctor, 0);
if (ctor && (ctor->toParent() != this || !ctor->isCtorDeclaration()))
ctor = NULL;
// dtor = (DtorDeclaration *)search(Id::dtor, 0);
// if (dtor && dtor->toParent() != this)
// dtor = NULL;
// inv = (InvariantDeclaration *)search(Id::classInvariant, 0);
// if (inv && inv->toParent() != this)
// inv = NULL;
// Can be in base class
aggNew = (NewDeclaration *)search(0, Id::classNew, 0);
aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0);
// If this class has no constructor, but base class does, create
// a constructor:
// this() { }
if (!ctor && baseClass && baseClass->ctor)
{
//printf("Creating default this(){} for class %s\n", toChars());
Type *tf = new TypeFunction(NULL, NULL, 0, LINKd, 0);
CtorDeclaration *ctor = new CtorDeclaration(loc, 0, 0, tf);
ctor->fbody = new CompoundStatement(0, new Statements());
members->push(ctor);
ctor->addMember(sc, this, 1);
*sc = scsave; // why? What about sc->nofree?
sc->offset = structsize;
ctor->semantic(sc);
this->ctor = ctor;
defaultCtor = ctor;
}
#if 0
if (baseClass)
{ if (!aggDelete)
aggDelete = baseClass->aggDelete;
if (!aggNew)
aggNew = baseClass->aggNew;
}
#endif
// Allocate instance of each new interface
for (size_t i = 0; i < vtblInterfaces->dim; i++)
{
BaseClass *b = vtblInterfaces->tdata()[i];
unsigned thissize = PTRSIZE;
alignmember(structalign, thissize, &sc->offset);
assert(b->offset == 0);
b->offset = sc->offset;
// Take care of single inheritance offsets
while (b->baseInterfaces_dim)
{
b = &b->baseInterfaces[0];
b->offset = sc->offset;
}
sc->offset += thissize;
if (alignsize < thissize)
alignsize = thissize;
}
structsize = sc->offset;
sizeok = 1;
Module::dprogress++;
dtor = buildDtor(sc);
sc->pop();
#if 0 // Do not call until toObjfile() because of forward references
// Fill in base class vtbl[]s
for (i = 0; i < vtblInterfaces->dim; i++)
{
BaseClass *b = vtblInterfaces->tdata()[i];
//b->fillVtbl(this, &b->vtbl, 1);
}
#endif
//printf("-ClassDeclaration::semantic(%s), type = %p\n", toChars(), type);
if (deferred && !global.gag)
{
deferred->semantic2(sc);
deferred->semantic3(sc);
}
}
void ClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
if (!isAnonymous())
{
buf->printf("%s ", kind());
buf->writestring(toChars());
if (baseclasses->dim)
buf->writestring(" : ");
}
for (size_t i = 0; i < baseclasses->dim; i++)
{
BaseClass *b = baseclasses->tdata()[i];
if (i)
buf->writeByte(',');
//buf->writestring(b->base->ident->toChars());
b->type->toCBuffer(buf, NULL, hgs);
}
if (members)
{
buf->writenl();
buf->writeByte('{');
buf->writenl();
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = members->tdata()[i];
buf->writestring(" ");
s->toCBuffer(buf, hgs);
}
buf->writestring("}");
}
else
buf->writeByte(';');
buf->writenl();
}
#if 0
void ClassDeclaration::defineRef(Dsymbol *s)
{
ClassDeclaration *cd;
AggregateDeclaration::defineRef(s);
cd = s->isClassDeclaration();
baseType = cd->baseType;
cd->baseType = NULL;
}
#endif
/*********************************************
* Determine if 'this' is a base class of cd.
* This is used to detect circular inheritance only.
*/
int ClassDeclaration::isBaseOf2(ClassDeclaration *cd)
{
if (!cd)
return 0;
//printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd->toChars());
for (size_t i = 0; i < cd->baseclasses->dim; i++)
{ BaseClass *b = cd->baseclasses->tdata()[i];
if (b->base == this || isBaseOf2(b->base))
return 1;
}
return 0;
}
/*******************************************
* Determine if 'this' is a base class of cd.
*/
int ClassDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset)
{
//printf("ClassDeclaration::isBaseOf(this = '%s', cd = '%s')\n", toChars(), cd->toChars());
if (poffset)
*poffset = 0;
while (cd)
{
/* cd->baseClass might not be set if cd is forward referenced.
*/
if (!cd->baseClass && cd->baseclasses->dim && !cd->isInterfaceDeclaration())
{
cd->semantic(NULL);
if (!cd->baseClass)
cd->error("base class is forward referenced by %s", toChars());
}
if (this == cd->baseClass)
return 1;
cd = cd->baseClass;
}
return 0;
}
/*********************************************
* Determine if 'this' has complete base class information.
* This is used to detect forward references in covariant overloads.
*/
int ClassDeclaration::isBaseInfoComplete()
{
if (!baseClass)
return ident == Id::Object;
for (size_t i = 0; i < baseclasses->dim; i++)
{ BaseClass *b = baseclasses->tdata()[i];
if (!b->base || !b->base->isBaseInfoComplete())
return 0;
}
return 1;
}
Dsymbol *ClassDeclaration::search(Loc loc, Identifier *ident, int flags)
{
Dsymbol *s;
//printf("%s.ClassDeclaration::search('%s')\n", toChars(), ident->toChars());
if (scope && !symtab)
{ Scope *sc = scope;
sc->mustsemantic++;
semantic(sc);
sc->mustsemantic--;
}
if (!members || !symtab)
{
error("is forward referenced when looking for '%s'", ident->toChars());
//*(char*)0=0;
return NULL;
}
s = ScopeDsymbol::search(loc, ident, flags);
if (!s)
{
// Search bases classes in depth-first, left to right order
for (size_t i = 0; i < baseclasses->dim; i++)
{
BaseClass *b = baseclasses->tdata()[i];
if (b->base)
{
if (!b->base->symtab)
error("base %s is forward referenced", b->base->ident->toChars());
else
{
s = b->base->search(loc, ident, flags);
if (s == this) // happens if s is nested in this and derives from this
s = NULL;
else if (s)
break;
}
}
}
}
return s;
}
Dsymbol *ClassDeclaration::searchBase(Loc loc, Identifier *ident)
{
// Search bases classes in depth-first, left to right order
for (size_t i = 0; i < baseclasses->dim; i++)
{
BaseClass *b = (*baseclasses)[i];
Dsymbol *cdb = b->type->isClassHandle();
if (cdb->ident->equals(ident))
return cdb;
cdb = ((ClassDeclaration *)cdb)->searchBase(loc, ident);
if (cdb)
return cdb;
}
return NULL;
}
/**********************************************************
* fd is in the vtbl[] for this class.
* Return 1 if function is hidden (not findable through search).
*/
#if DMDV2
int isf(void *param, FuncDeclaration *fd)
{
//printf("param = %p, fd = %p %s\n", param, fd, fd->toChars());
return param == fd;
}
int ClassDeclaration::isFuncHidden(FuncDeclaration *fd)
{
//printf("ClassDeclaration::isFuncHidden(class = %s, fd = %s)\n", toChars(), fd->toChars());
Dsymbol *s = search(0, fd->ident, 4|2);
if (!s)
{ //printf("not found\n");
/* Because, due to a hack, if there are multiple definitions
* of fd->ident, NULL is returned.
*/
return 0;
}
s = s->toAlias();
OverloadSet *os = s->isOverloadSet();
if (os)
{
for (size_t i = 0; i < os->a.dim; i++)
{ Dsymbol *s2 = os->a.tdata()[i];
FuncDeclaration *f2 = s2->isFuncDeclaration();
if (f2 && overloadApply(f2, &isf, fd))
return 0;
}
return 1;
}
else
{
FuncDeclaration *fdstart = s->isFuncDeclaration();
//printf("%s fdstart = %p\n", s->kind(), fdstart);
if (overloadApply(fdstart, &isf, fd))
return 0;
return !fd->parent->isTemplateMixin();
}
}