aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/dmd/dclass.c
blob: c7dbbbea5d38ea757cb0641f62f6ce8a8897e300 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

/* Compiler implementation of the D programming language
 * Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
 * written by Walter Bright
 * http://www.digitalmars.com
 * Distributed under the Boost Software License, Version 1.0.
 * http://www.boost.org/LICENSE_1_0.txt
 * https://github.com/D-Programming-Language/dmd/blob/master/src/class.c
 */

#include "root/dsystem.h"               // mem{cpy|set}()
#include "root/root.h"
#include "root/rmem.h"

#include "errors.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"
#include "template.h"
#include "target.h"
#include "objc.h"

bool symbolIsVisible(Dsymbol *origin, Dsymbol *s);
Objc *objc();


/********************************* ClassDeclaration ****************************/

ClassDeclaration *ClassDeclaration::object;
ClassDeclaration *ClassDeclaration::throwable;
ClassDeclaration *ClassDeclaration::exception;
ClassDeclaration *ClassDeclaration::errorException;
ClassDeclaration *ClassDeclaration::cpp_type_info_ptr;   // Object.__cpp_type_info_ptr

ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses, Dsymbols *members, bool inObject)
    : AggregateDeclaration(loc, id ? id : Identifier::generateId("__anonclass"))
{
    static const 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();

    this->members = members;

    baseClass = NULL;

    interfaces.length = 0;
    interfaces.ptr = NULL;

    vtblInterfaces = NULL;

    //printf("ClassDeclaration(%s), dim = %d\n", id->toChars(), this->baseclasses->length);

    // For forward references
    type = new TypeClass(this);

    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 (!inObject)
                    error("%s", msg);
                Type::dtypeinfo = this;
            }

            if (id == Id::TypeInfo_Class)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfoclass = this;
            }

            if (id == Id::TypeInfo_Interface)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfointerface = this;
            }

            if (id == Id::TypeInfo_Struct)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfostruct = this;
            }

            if (id == Id::TypeInfo_Pointer)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfopointer = this;
            }

            if (id == Id::TypeInfo_Array)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfoarray = this;
            }

            if (id == Id::TypeInfo_StaticArray)
            {
                //if (!inObject)
                //    Type::typeinfostaticarray->error("%s", msg);
                Type::typeinfostaticarray = this;
            }

            if (id == Id::TypeInfo_AssociativeArray)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfoassociativearray = this;
            }

            if (id == Id::TypeInfo_Enum)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfoenum = this;
            }

            if (id == Id::TypeInfo_Function)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfofunction = this;
            }

            if (id == Id::TypeInfo_Delegate)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfodelegate = this;
            }

            if (id == Id::TypeInfo_Tuple)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfotypelist = this;
            }

            if (id == Id::TypeInfo_Const)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfoconst = this;
            }

            if (id == Id::TypeInfo_Invariant)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfoinvariant = this;
            }

            if (id == Id::TypeInfo_Shared)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfoshared = this;
            }

            if (id == Id::TypeInfo_Wild)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfowild = this;
            }

            if (id == Id::TypeInfo_Vector)
            {
                if (!inObject)
                    error("%s", msg);
                Type::typeinfovector = this;
            }
        }

        if (id == Id::Object)
        {
            if (!inObject)
                error("%s", msg);
            object = this;
        }

        if (id == Id::Throwable)
        {
            if (!inObject)
                error("%s", msg);
            throwable = this;
        }

        if (id == Id::Exception)
        {
            if (!inObject)
                error("%s", msg);
            exception = this;
        }

        if (id == Id::Error)
        {
            if (!inObject)
                error("%s", msg);
            errorException = this;
        }

        if (id == Id::cpp_type_info_ptr)
        {
            if (!inObject)
                error("%s", msg);
            cpp_type_info_ptr = this;
        }
    }

    com = false;
    isscope = false;
    isabstract = ABSfwdref;
    inuse = 0;
    baseok = BASEOKnone;
    cpp_type_info_ptr_sym = NULL;
}

ClassDeclaration *ClassDeclaration::create(Loc loc, Identifier *id, BaseClasses *baseclasses, Dsymbols *members, bool inObject)
{
    return new ClassDeclaration(loc, id, baseclasses, members, inObject);
}

Dsymbol *ClassDeclaration::syntaxCopy(Dsymbol *s)
{
    //printf("ClassDeclaration::syntaxCopy('%s')\n", toChars());
    ClassDeclaration *cd =
        s ? (ClassDeclaration *)s
          : new ClassDeclaration(loc, ident, NULL, NULL, false);

    cd->storage_class |= storage_class;

    cd->baseclasses->setDim(this->baseclasses->length);
    for (size_t i = 0; i < cd->baseclasses->length; i++)
    {
        BaseClass *b = (*this->baseclasses)[i];
        BaseClass *b2 = new BaseClass(b->type->syntaxCopy());
        (*cd->baseclasses)[i] = b2;
    }

    return ScopeDsymbol::syntaxCopy(cd);
}

Scope *ClassDeclaration::newScope(Scope *sc)
{
    Scope *sc2 = AggregateDeclaration::newScope(sc);
    if (isCOMclass())
    {
        if (global.params.isWindows)
            sc2->linkage = LINKwindows;
        else
        {
            /* This enables us to use COM objects under Linux and
             * work with things like XPCOM
             */
            sc2->linkage = LINKc;
        }
    }
    return sc2;
}

/*********************************************
 * Determine if 'this' is a base class of cd.
 * This is used to detect circular inheritance only.
 */

bool ClassDeclaration::isBaseOf2(ClassDeclaration *cd)
{
    if (!cd)
        return false;
    //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd->toChars());
    for (size_t i = 0; i < cd->baseclasses->length; i++)
    {
        BaseClass *b = (*cd->baseclasses)[i];
        if (b->sym == this || isBaseOf2(b->sym))
            return true;
    }
    return false;
}

/*******************************************
 * Determine if 'this' is a base class of cd.
 */

bool 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->semanticRun < PASSsemanticdone && !cd->isInterfaceDeclaration())
        {
            dsymbolSemantic(cd, NULL);
            if (!cd->baseClass && cd->semanticRun < PASSsemanticdone)
                cd->error("base class is forward referenced by %s", toChars());
        }

        if (this == cd->baseClass)
            return true;

        cd = cd->baseClass;
    }
    return false;
}

/*********************************************
 * Determine if 'this' has complete base class information.
 * This is used to detect forward references in covariant overloads.
 */

bool ClassDeclaration::isBaseInfoComplete()
{
    return baseok >= BASEOKdone;
}

Dsymbol *ClassDeclaration::search(const Loc &loc, Identifier *ident, int flags)
{
    //printf("%s.ClassDeclaration::search('%s', flags=x%x)\n", toChars(), ident->toChars(), flags);

    //if (_scope) printf("%s baseok = %d\n", toChars(), baseok);
    if (_scope && baseok < BASEOKdone)
    {
        if (!inuse)
        {
            // must semantic on base class/interfaces
            ++inuse;
            dsymbolSemantic(this, NULL);
            --inuse;
        }
    }

    if (!members || !symtab)    // opaque or addMember is not yet done
    {
        error("is forward referenced when looking for `%s`", ident->toChars());
        //*(char*)0=0;
        return NULL;
    }

    Dsymbol *s = ScopeDsymbol::search(loc, ident, flags);

    // don't search imports of base classes
    if (flags & SearchImportsOnly)
        return s;

    if (!s)
    {
        // Search bases classes in depth-first, left to right order

        for (size_t i = 0; i < baseclasses->length; i++)
        {
            BaseClass *b = (*baseclasses)[i];

            if (b->sym)
            {
                if (!b->sym->symtab)
                    error("base %s is forward referenced", b->sym->ident->toChars());
                else
                {
                    s = b->sym->search(loc, ident, flags);
                    if (!s)
                        continue;
                    else if (s == this) // happens if s is nested in this and derives from this
                        s = NULL;
                    else if (!(flags & IgnoreSymbolVisibility) && !(s->prot().kind == Prot::protected_) && !symbolIsVisible(this, s))
                        s = NULL;
                    else
                        break;
                }
            }
        }
    }
    return s;
}

/************************************
 * Search base classes in depth-first, left-to-right order for
 * a class or interface named 'ident'.
 * Stops at first found. Does not look for additional matches.
 * Params:
 *  ident = identifier to search for
 * Returns:
 *  ClassDeclaration if found, null if not
 */
ClassDeclaration *ClassDeclaration::searchBase(Identifier *ident)
{
    for (size_t i = 0; i < baseclasses->length; i++)
    {
        BaseClass *b = (*baseclasses)[i];
        ClassDeclaration *cdb = b->type->isClassHandle();
        if (!cdb)   // Bugzilla 10616
            return NULL;
        if (cdb->ident->equals(ident))
            return cdb;
        cdb = cdb->searchBase(ident);
        if (cdb)
            return cdb;
    }
    return NULL;
}

/****
 * Runs through the inheritance graph to set the BaseClass.offset fields.
 * Recursive in order to account for the size of the interface classes, if they are
 * more than just interfaces.
 * Params:
 *      cd = interface to look at
 *      baseOffset = offset of where cd will be placed
 * Returns:
 *      subset of instantiated size used by cd for interfaces
 */
static unsigned membersPlace(BaseClasses *vtblInterfaces, size_t &bi, ClassDeclaration *cd, unsigned baseOffset)
{
    //printf("    membersPlace(%s, %d)\n", cd->toChars(), baseOffset);
    unsigned offset = baseOffset;

    for (size_t i = 0; i < cd->interfaces.length; i++)
    {
        BaseClass *b = cd->interfaces.ptr[i];
        if (b->sym->sizeok != SIZEOKdone)
            b->sym->finalizeSize();
        assert(b->sym->sizeok == SIZEOKdone);

        if (!b->sym->alignsize)
            b->sym->alignsize = target.ptrsize;
        cd->alignmember(b->sym->alignsize, b->sym->alignsize, &offset);
        assert(bi < vtblInterfaces->length);
        BaseClass *bv = (*vtblInterfaces)[bi];
        if (b->sym->interfaces.length == 0)
        {
            //printf("\tvtblInterfaces[%d] b=%p b->sym = %s, offset = %d\n", bi, bv, bv->sym->toChars(), offset);
            bv->offset = offset;
            ++bi;
            // All the base interfaces down the left side share the same offset
            for (BaseClass *b2 = bv; b2->baseInterfaces.length; )
            {
                b2 = &b2->baseInterfaces.ptr[0];
                b2->offset = offset;
                //printf("\tvtblInterfaces[%d] b=%p   sym = %s, offset = %d\n", bi, b2, b2->sym->toChars(), b2->offset);
            }
        }
        membersPlace(vtblInterfaces, bi, b->sym, offset);
        //printf(" %s size = %d\n", b->sym->toChars(), b->sym->structsize);
        offset += b->sym->structsize;
        if (cd->alignsize < b->sym->alignsize)
            cd->alignsize = b->sym->alignsize;
    }
    return offset - baseOffset;
}

void ClassDeclaration::finalizeSize()
{
    assert(sizeok != SIZEOKdone);

    // Set the offsets of the fields and determine the size of the class
    if (baseClass)
    {
        assert(baseClass->sizeok == SIZEOKdone);

        alignsize = baseClass->alignsize;
        structsize = baseClass->structsize;
        if (isCPPclass() && global.params.isWindows)
            structsize = (structsize + alignsize - 1) & ~(alignsize - 1);
    }
    else if (isInterfaceDeclaration())
    {
        if (interfaces.length == 0)
        {
            alignsize = target.ptrsize;
            structsize = target.ptrsize;      // allow room for __vptr
        }
    }
    else
    {
        alignsize = target.ptrsize;
        structsize = target.ptrsize;      // allow room for __vptr
        if (hasMonitor())
            structsize += target.ptrsize; // allow room for __monitor
    }

    //printf("finalizeSize() %s, sizeok = %d\n", toChars(), sizeok);
    size_t bi = 0;                  // index into vtblInterfaces[]

    // Add vptr's for any interfaces implemented by this class
    structsize += membersPlace(vtblInterfaces, bi, this, structsize);

    if (isInterfaceDeclaration())
    {
        sizeok = SIZEOKdone;
        return;
    }

    // FIXME: Currently setFieldOffset functions need to increase fields
    // to calculate each variable offsets. It can be improved later.
    fields.setDim(0);

    unsigned offset = structsize;
    for (size_t i = 0; i < members->length; i++)
    {
        Dsymbol *s = (*members)[i];
        s->setFieldOffset(this, &offset, false);
    }

    sizeok = SIZEOKdone;

    // Calculate fields[i]->overlapped
    checkOverlappedFields();
}

/**************
 * Returns: true if there's a __monitor field
 */
bool ClassDeclaration::hasMonitor()
{
    return classKind == ClassKind::d;
}

/**********************************************************
 * fd is in the vtbl[] for this class.
 * Return 1 if function is hidden (not findable through search).
 */

int isf(void *param, Dsymbol *s)
{
    FuncDeclaration *fd = s->isFuncDeclaration();
    if (!fd)
        return 0;
    //printf("param = %p, fd = %p %s\n", param, fd, fd->toChars());
    return (RootObject *)param == fd;
}

bool ClassDeclaration::isFuncHidden(FuncDeclaration *fd)
{
    //printf("ClassDeclaration::isFuncHidden(class = %s, fd = %s)\n", toChars(), fd->toPrettyChars());
    Dsymbol *s = search(Loc(), fd->ident, IgnoreAmbiguous | IgnoreErrors);
    if (!s)
    {
        //printf("not found\n");
        /* Because, due to a hack, if there are multiple definitions
         * of fd->ident, NULL is returned.
         */
        return false;
    }
    s = s->toAlias();
    OverloadSet *os = s->isOverloadSet();
    if (os)
    {
        for (size_t i = 0; i < os->a.length; i++)
        {
            Dsymbol *s2 = os->a[i];
            FuncDeclaration *f2 = s2->isFuncDeclaration();
            if (f2 && overloadApply(f2, (void *)fd, &isf))
                return false;
        }
        return true;
    }
    else
    {
        FuncDeclaration *fdstart = s->isFuncDeclaration();
        //printf("%s fdstart = %p\n", s->kind(), fdstart);
        if (overloadApply(fdstart, (void *)fd, &isf))
            return false;

        return !fd->parent->isTemplateMixin();
    }
}

/****************
 * Find virtual function matching identifier and type.
 * Used to build virtual function tables for interface implementations.
 */

FuncDeclaration *ClassDeclaration::findFunc(Identifier *ident, TypeFunction *tf)
{
    //printf("ClassDeclaration::findFunc(%s, %s) %s\n", ident->toChars(), tf->toChars(), toChars());
    FuncDeclaration *fdmatch = NULL;
    FuncDeclaration *fdambig = NULL;

    ClassDeclaration *cd = this;
    Dsymbols *vtbl = &cd->vtbl;
    while (1)
    {
        for (size_t i = 0; i < vtbl->length; i++)
        {
            FuncDeclaration *fd = (*vtbl)[i]->isFuncDeclaration();
            if (!fd)
                continue;               // the first entry might be a ClassInfo

            //printf("\t[%d] = %s\n", i, fd->toChars());
            if (ident == fd->ident &&
                fd->type->covariant(tf) == 1)
            {
                //printf("fd->parent->isClassDeclaration() = %p\n", fd->parent->isClassDeclaration());
                if (!fdmatch)
                    goto Lfd;
                if (fd == fdmatch)
                    goto Lfdmatch;

                {
                // Function type matcing: exact > covariant
                MATCH m1 = tf->equals(fd     ->type) ? MATCHexact : MATCHnomatch;
                MATCH m2 = tf->equals(fdmatch->type) ? MATCHexact : MATCHnomatch;
                if (m1 > m2)
                    goto Lfd;
                else if (m1 < m2)
                    goto Lfdmatch;
                }

                {
                MATCH m1 = (tf->mod == fd     ->type->mod) ? MATCHexact : MATCHnomatch;
                MATCH m2 = (tf->mod == fdmatch->type->mod) ? MATCHexact : MATCHnomatch;
                if (m1 > m2)
                    goto Lfd;
                else if (m1 < m2)
                    goto Lfdmatch;
                }

                {
                // The way of definition: non-mixin > mixin
                MATCH m1 = fd     ->parent->isClassDeclaration() ? MATCHexact : MATCHnomatch;
                MATCH m2 = fdmatch->parent->isClassDeclaration() ? MATCHexact : MATCHnomatch;
                if (m1 > m2)
                    goto Lfd;
                else if (m1 < m2)
                    goto Lfdmatch;
                }

                fdambig = fd;
                //printf("Lambig fdambig = %s %s [%s]\n", fdambig->toChars(), fdambig->type->toChars(), fdambig->loc.toChars());
                continue;

            Lfd:
                fdmatch = fd;
                fdambig = NULL;
                //printf("Lfd fdmatch = %s %s [%s]\n", fdmatch->toChars(), fdmatch->type->toChars(), fdmatch->loc.toChars());
                continue;

            Lfdmatch:
                continue;
            }
            //else printf("\t\t%d\n", fd->type->covariant(tf));
        }
        if (!cd)
            break;
        vtbl = &cd->vtblFinal;
        cd = cd->baseClass;
    }

    if (fdambig)
        error("ambiguous virtual function %s", fdambig->toChars());
    return fdmatch;
}

/****************************************
 */

bool ClassDeclaration::isCOMclass() const
{
    return com;
}

bool ClassDeclaration::isCOMinterface() const
{
    return false;
}

bool ClassDeclaration::isCPPclass() const
{
    return classKind == ClassKind::cpp;
}

bool ClassDeclaration::isCPPinterface() const
{
    return false;
}


/****************************************
 */

bool ClassDeclaration::isAbstract()
{
    if (isabstract != ABSfwdref)
        return isabstract == ABSyes;

    /* Bugzilla 11169: Resolve forward references to all class member functions,
     * and determine whether this class is abstract.
     */
    struct SearchAbstract
    {
        static int fp(Dsymbol *s, void *)
        {
            FuncDeclaration *fd = s->isFuncDeclaration();
            if (!fd)
                return 0;
            if (fd->storage_class & STCstatic)
                return 0;

            if (fd->_scope)
                dsymbolSemantic(fd, NULL);

            if (fd->isAbstract())
                return 1;
            return 0;
        }
    };

    for (size_t i = 0; i < members->length; i++)
    {
        Dsymbol *s = (*members)[i];
        if (s->apply(&SearchAbstract::fp, this))
        {
            isabstract = ABSyes;
            return true;
        }
    }

    /* Iterate inherited member functions and check their abstract attribute.
     */
    for (size_t i = 1; i < vtbl.length; i++)
    {
        FuncDeclaration *fd = vtbl[i]->isFuncDeclaration();
        //if (fd) printf("\tvtbl[%d] = [%s] %s\n", i, fd->loc.toChars(), fd->toChars());
        if (!fd || fd->isAbstract())
        {
            isabstract = ABSyes;
            return true;
        }
    }

    isabstract = ABSno;
    return false;
}


/****************************************
 * Determine if slot 0 of the vtbl[] is reserved for something else.
 * For class objects, yes, this is where the classinfo ptr goes.
 * For COM interfaces, no.
 * For non-COM interfaces, yes, this is where the Interface ptr goes.
 * Returns:
 *      0       vtbl[0] is first virtual function pointer
 *      1       vtbl[0] is classinfo/interfaceinfo pointer
 */

int ClassDeclaration::vtblOffset() const
{
    return classKind == ClassKind::cpp ? 0 : 1;
}

/****************************************
 */

const char *ClassDeclaration::kind() const
{
    return "class";
}

/****************************************
 */

void ClassDeclaration::addLocalClass(ClassDeclarations *aclasses)
{
    aclasses->push(this);
}

/********************************* InterfaceDeclaration ****************************/

InterfaceDeclaration::InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses)
    : ClassDeclaration(loc, id, baseclasses, NULL, false)
{
    if (id == Id::IUnknown)     // IUnknown is the root of all COM interfaces
    {
        com = true;
        classKind = ClassKind::cpp; // IUnknown is also a C++ interface
    }
}

Dsymbol *InterfaceDeclaration::syntaxCopy(Dsymbol *s)
{
    InterfaceDeclaration *id =
        s ? (InterfaceDeclaration *)s
          : new InterfaceDeclaration(loc, ident, NULL);
    return ClassDeclaration::syntaxCopy(id);
}

Scope *InterfaceDeclaration::newScope(Scope *sc)
{
    Scope *sc2 = ClassDeclaration::newScope(sc);
    if (com)
        sc2->linkage = LINKwindows;
    else if (classKind == ClassKind::cpp)
        sc2->linkage = LINKcpp;
    else if (classKind == ClassKind::objc)
        sc2->linkage = LINKobjc;
    return sc2;
}

/*******************************************
 * Determine if 'this' is a base class of cd.
 * (Actually, if it is an interface supported by cd)
 * Output:
 *      *poffset        offset to start of class
 *                      OFFSET_RUNTIME  must determine offset at runtime
 * Returns:
 *      false   not a base
 *      true    is a base
 */

bool InterfaceDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset)
{
    //printf("%s.InterfaceDeclaration::isBaseOf(cd = '%s')\n", toChars(), cd->toChars());
    assert(!baseClass);
    for (size_t j = 0; j < cd->interfaces.length; j++)
    {
        BaseClass *b = cd->interfaces.ptr[j];

        //printf("\tX base %s\n", b->sym->toChars());
        if (this == b->sym)
        {
            //printf("\tfound at offset %d\n", b->offset);
            if (poffset)
            {
                // don't return incorrect offsets https://issues.dlang.org/show_bug.cgi?id=16980
                *poffset = cd->sizeok == SIZEOKdone ? b->offset : OFFSET_FWDREF;
            }
            //printf("\tfound at offset %d\n", b->offset);
            return true;
        }
        if (isBaseOf(b, poffset))
            return true;
    }

    if (cd->baseClass && isBaseOf(cd->baseClass, poffset))
        return true;

    if (poffset)
        *poffset = 0;
    return false;
}

bool InterfaceDeclaration::isBaseOf(BaseClass *bc, int *poffset)
{
    //printf("%s.InterfaceDeclaration::isBaseOf(bc = '%s')\n", toChars(), bc->sym->toChars());
    for (size_t j = 0; j < bc->baseInterfaces.length; j++)
    {
        BaseClass *b = &bc->baseInterfaces.ptr[j];

        //printf("\tY base %s\n", b->sym->toChars());
        if (this == b->sym)
        {
            //printf("\tfound at offset %d\n", b->offset);
            if (poffset)
            {
                *poffset = b->offset;
            }
            return true;
        }
        if (isBaseOf(b, poffset))
        {
            return true;
        }
    }
    if (poffset)
        *poffset = 0;
    return false;
}

/****************************************
 * Determine if slot 0 of the vtbl[] is reserved for something else.
 * For class objects, yes, this is where the ClassInfo ptr goes.
 * For COM interfaces, no.
 * For non-COM interfaces, yes, this is where the Interface ptr goes.
 */

int InterfaceDeclaration::vtblOffset() const
{
    if (isCOMinterface() || isCPPinterface())
        return 0;
    return 1;
}

bool InterfaceDeclaration::isCOMinterface() const
{
    return com;
}

bool InterfaceDeclaration::isCPPinterface() const
{
    return classKind == ClassKind::cpp;
}

/*******************************************
 */

const char *InterfaceDeclaration::kind() const
{
    return "interface";
}


/******************************** BaseClass *****************************/

BaseClass::BaseClass()
{
    this->type = NULL;
    this->sym = NULL;
    this->offset = 0;

    this->baseInterfaces.length = 0;
    this->baseInterfaces.ptr = NULL;
}

BaseClass::BaseClass(Type *type)
{
    //printf("BaseClass(this = %p, '%s')\n", this, type->toChars());
    this->type = type;
    this->sym = NULL;
    this->offset = 0;

    this->baseInterfaces.length = 0;
    this->baseInterfaces.ptr = NULL;
}

/****************************************
 * Fill in vtbl[] for base class based on member functions of class cd.
 * Input:
 *      vtbl            if !=NULL, fill it in
 *      newinstance     !=0 means all entries must be filled in by members
 *                      of cd, not members of any base classes of cd.
 * Returns:
 *      true if any entries were filled in by members of cd (not exclusively
 *      by base classes)
 */

bool BaseClass::fillVtbl(ClassDeclaration *cd, FuncDeclarations *vtbl, int newinstance)
{
    bool result = false;

    //printf("BaseClass::fillVtbl(this='%s', cd='%s')\n", sym->toChars(), cd->toChars());
    if (vtbl)
        vtbl->setDim(sym->vtbl.length);

    // first entry is ClassInfo reference
    for (size_t j = sym->vtblOffset(); j < sym->vtbl.length; j++)
    {
        FuncDeclaration *ifd = sym->vtbl[j]->isFuncDeclaration();
        FuncDeclaration *fd;
        TypeFunction *tf;

        //printf("        vtbl[%d] is '%s'\n", j, ifd ? ifd->toChars() : "null");

        assert(ifd);
        // Find corresponding function in this class
        tf = ifd->type->toTypeFunction();
        fd = cd->findFunc(ifd->ident, tf);
        if (fd && !fd->isAbstract())
        {
            //printf("            found\n");
            // Check that calling conventions match
            if (fd->linkage != ifd->linkage)
                fd->error("linkage doesn't match interface function");

            // Check that it is current
            //printf("newinstance = %d fd->toParent() = %s ifd->toParent() = %s\n",
                //newinstance, fd->toParent()->toChars(), ifd->toParent()->toChars());
            if (newinstance && fd->toParent() != cd && ifd->toParent() == sym)
                cd->error("interface function `%s` is not implemented", ifd->toFullSignature());

            if (fd->toParent() == cd)
                result = true;
        }
        else
        {
            //printf("            not found %p\n", fd);
            // BUG: should mark this class as abstract?
            if (!cd->isAbstract())
                cd->error("interface function `%s` is not implemented", ifd->toFullSignature());

            fd = NULL;
        }
        if (vtbl)
            (*vtbl)[j] = fd;
    }

    return result;
}

void BaseClass::copyBaseInterfaces(BaseClasses *vtblInterfaces)
{
    //printf("+copyBaseInterfaces(), %s\n", sym->toChars());
//    if (baseInterfaces.length)
//      return;

    baseInterfaces.length = sym->interfaces.length;
    baseInterfaces.ptr = (BaseClass *)mem.xcalloc(baseInterfaces.length, sizeof(BaseClass));

    //printf("%s.copyBaseInterfaces()\n", sym->toChars());
    for (size_t i = 0; i < baseInterfaces.length; i++)
    {
        void *pb = &baseInterfaces.ptr[i];
        BaseClass *b2 = sym->interfaces.ptr[i];

        assert(b2->vtbl.length == 0);      // should not be filled yet
        BaseClass *b = (BaseClass *)memcpy(pb, b2, sizeof(BaseClass));

        if (i)                          // single inheritance is i==0
            vtblInterfaces->push(b);    // only need for M.I.
        b->copyBaseInterfaces(vtblInterfaces);
    }
    //printf("-copyBaseInterfaces\n");
}