]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/dstruct.c
2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/D-Programming-Language/dmd/blob/master/src/struct.c
11 #include "root/dsystem.h"
12 #include "root/root.h"
15 #include "aggregate.h"
19 #include "declaration.h"
22 #include "statement.h"
27 Type
*getTypeInfoType(Loc loc
, Type
*t
, Scope
*sc
);
28 void unSpeculative(Scope
*sc
, RootObject
*o
);
29 bool MODimplicitConv(MOD modfrom
, MOD modto
);
30 Expression
*resolve(Loc loc
, Scope
*sc
, Dsymbol
*s
, bool hasOverloads
);
32 FuncDeclaration
*StructDeclaration::xerreq
; // object.xopEquals
33 FuncDeclaration
*StructDeclaration::xerrcmp
; // object.xopCmp
35 /***************************************
36 * Search toString member function for TypeInfo_Struct.
39 FuncDeclaration
*search_toString(StructDeclaration
*sd
)
41 Dsymbol
*s
= search_function(sd
, Id::tostring
);
42 FuncDeclaration
*fd
= s
? s
->isFuncDeclaration() : NULL
;
45 static TypeFunction
*tftostring
;
48 tftostring
= new TypeFunction(NULL
, Type::tstring
, 0, LINKd
);
49 tftostring
= tftostring
->merge()->toTypeFunction();
52 fd
= fd
->overloadExactMatch(tftostring
);
57 /***************************************
58 * Request additonal semantic analysis for TypeInfo generation.
60 void semanticTypeInfo(Scope
*sc
, Type
*t
)
62 class FullTypeInfoVisitor
: public Visitor
69 Type
*tb
= t
->toBasetype();
73 void visit(TypeNext
*t
)
76 t
->next
->accept(this);
78 void visit(TypeBasic
*) { }
79 void visit(TypeVector
*t
)
81 t
->basetype
->accept(this);
83 void visit(TypeAArray
*t
)
85 t
->index
->accept(this);
88 void visit(TypeFunction
*t
)
91 // Currently TypeInfo_Function doesn't store parameter types.
93 void visit(TypeStruct
*t
)
95 //printf("semanticTypeInfo::visit(TypeStruct = %s)\n", t->toChars());
96 StructDeclaration
*sd
= t
->sym
;
98 /* Step 1: create TypeInfoDeclaration
100 if (!sc
) // inline may request TypeInfo.
103 scx
._module
= sd
->getModule();
104 getTypeInfoType(sd
->loc
, t
, &scx
);
105 sd
->requestTypeInfo
= true;
109 // don't yet have to generate TypeInfo instance if
110 // the typeid(T) expression exists in speculative scope.
114 getTypeInfoType(sd
->loc
, t
, sc
);
115 sd
->requestTypeInfo
= true;
117 // Bugzilla 15149, if the typeid operand type comes from a
118 // result of auto function, it may be yet speculative.
119 // unSpeculative(sc, sd);
122 /* Step 2: If the TypeInfo generation requires sd.semantic3, run it later.
123 * This should be done even if typeid(T) exists in speculative scope.
124 * Because it may appear later in non-speculative scope.
127 return; // opaque struct
128 if (!sd
->xeq
&& !sd
->xcmp
&& !sd
->postblit
&&
129 !sd
->dtor
&& !sd
->xhash
&& !search_toString(sd
))
130 return; // none of TypeInfo-specific members
132 // If the struct is in a non-root module, run semantic3 to get
133 // correct symbols for the member function.
134 if (sd
->semanticRun
>= PASSsemantic3
)
136 // semantic3 is already done
138 else if (TemplateInstance
*ti
= sd
->isInstantiated())
140 if (ti
->minst
&& !ti
->minst
->isRoot())
141 Module::addDeferredSemantic3(sd
);
147 //printf("deferred sem3 for TypeInfo - sd = %s, inNonRoot = %d\n", sd->toChars(), sd->inNonRoot());
148 Module::addDeferredSemantic3(sd
);
152 void visit(TypeClass
*) { }
153 void visit(TypeTuple
*t
)
157 for (size_t i
= 0; i
< t
->arguments
->length
; i
++)
159 Type
*tprm
= (*t
->arguments
)[i
]->type
;
173 if (sc
->flags
& (SCOPEctfe
| SCOPEcompile
))
177 FullTypeInfoVisitor v
;
182 /********************************* AggregateDeclaration ****************************/
184 AggregateDeclaration::AggregateDeclaration(Loc loc
, Identifier
*id
)
190 protection
= Prot(PROTpublic
);
192 structsize
= 0; // size of struct
193 alignsize
= 0; // size of struct for alignment purposes
194 sizeok
= SIZEOKnone
; // size not determined yet
196 isdeprecated
= false;
197 classKind
= ClassKind::d
;
210 noDefaultCtor
= false;
215 Prot
AggregateDeclaration::prot()
220 /***************************************
221 * Create a new scope from sc.
222 * semantic, semantic2 and semantic3 will use this for aggregate members.
224 Scope
*AggregateDeclaration::newScope(Scope
*sc
)
226 Scope
*sc2
= sc
->push(this);
227 sc2
->stc
&= STCsafe
| STCtrusted
| STCsystem
;
229 if (isUnionDeclaration())
231 sc2
->protection
= Prot(PROTpublic
);
232 sc2
->explicitProtection
= 0;
233 sc2
->aligndecl
= NULL
;
234 sc2
->userAttribDecl
= NULL
;
238 void AggregateDeclaration::setScope(Scope
*sc
)
240 // Might need a scope to resolve forward references. The check for
241 // semanticRun prevents unnecessary setting of _scope during deferred
242 // setScope phases for aggregates which already finished semantic().
243 // Also see https://issues.dlang.org/show_bug.cgi?id=16607
244 if (semanticRun
< PASSsemanticdone
)
245 ScopeDsymbol::setScope(sc
);
248 void AggregateDeclaration::semantic2(Scope
*sc
)
250 //printf("AggregateDeclaration::semantic2(%s) type = %s, errors = %d\n", toChars(), type->toChars(), errors);
256 error("has forward references");
260 Scope
*sc2
= newScope(sc
);
264 for (size_t i
= 0; i
< members
->length
; i
++)
266 Dsymbol
*s
= (*members
)[i
];
267 //printf("\t[%d] %s\n", i, s->toChars());
274 void AggregateDeclaration::semantic3(Scope
*sc
)
276 //printf("AggregateDeclaration::semantic3(%s) type = %s, errors = %d\n", toChars(), type->toChars(), errors);
280 StructDeclaration
*sd
= isStructDeclaration();
281 if (!sc
) // from runDeferredSemantic3 for TypeInfo generation
284 sd
->semanticTypeInfoMembers();
288 Scope
*sc2
= newScope(sc
);
290 for (size_t i
= 0; i
< members
->length
; i
++)
292 Dsymbol
*s
= (*members
)[i
];
298 // don't do it for unused deprecated types
300 if (!getRTInfo
&& Type::rtinfo
&&
301 (!isDeprecated() || global
.params
.useDeprecated
!= DIAGNOSTICerror
) &&
302 (type
&& type
->ty
!= Terror
))
304 // Evaluate: RTinfo!type
305 Objects
*tiargs
= new Objects();
307 TemplateInstance
*ti
= new TemplateInstance(loc
, Type::rtinfo
, tiargs
);
309 Scope
*sc3
= ti
->tempdecl
->_scope
->startCTFE();
310 sc3
->tinst
= sc
->tinst
;
311 sc3
->minst
= sc
->minst
;
313 sc3
->stc
|= STCdeprecated
;
318 Expression
*e
= resolve(Loc(), sc3
, ti
->toAlias(), false);
322 e
= e
->ctfeInterpret();
327 sd
->semanticTypeInfoMembers();
328 semanticRun
= PASSsemantic3done
;
331 /***************************************
332 * Find all instance fields, then push them into `fields`.
334 * Runs semantic() for all instance field variables, but also
335 * the field types can reamin yet not resolved forward references,
336 * except direct recursive definitions.
337 * After the process sizeok is set to SIZEOKfwd.
340 * false if any errors occur.
342 bool AggregateDeclaration::determineFields()
344 if (sizeok
!= SIZEOKnone
)
347 //printf("determineFields() %s, fields.length = %d\n", toChars(), fields.length);
352 AggregateDeclaration
*agg
;
354 static int func(Dsymbol
*s
, void *param
)
356 VarDeclaration
*v
= s
->isVarDeclaration();
359 if (v
->storage_class
& STCmanifest
)
362 AggregateDeclaration
*ad
= ((SV
*)param
)->agg
;
364 if (v
->semanticRun
< PASSsemanticdone
)
366 // Note: Aggregate fields or size could have determined during v->semantic.
367 if (ad
->sizeok
!= SIZEOKnone
)
371 return 0; // If this variable was really a tuple, skip it.
373 if (v
->storage_class
& (STCstatic
| STCextern
| STCtls
| STCgshared
| STCmanifest
| STCctfe
| STCtemplateparameter
))
375 if (!v
->isField() || v
->semanticRun
< PASSsemanticdone
)
376 return 1; // unresolvable forward reference
380 if (v
->storage_class
& STCref
)
382 Type
*tv
= v
->type
->baseElemOf();
383 if (tv
->ty
!= Tstruct
)
385 if (ad
== ((TypeStruct
*)tv
)->sym
)
387 const char *psz
= (v
->type
->toBasetype()->ty
== Tsarray
) ? "static array of " : "";
388 ad
->error("cannot have field %s with %ssame struct type", v
->toChars(), psz
);
389 ad
->type
= Type::terror
;
399 for (size_t i
= 0; i
< members
->length
; i
++)
401 Dsymbol
*s
= (*members
)[i
];
402 if (s
->apply(&SV::func
, &sv
))
404 if (sizeok
!= SIZEOKnone
)
410 if (sizeok
!= SIZEOKdone
)
416 /***************************************
417 * Collect all instance fields, then determine instance size.
419 * false if failed to determine the size.
421 bool AggregateDeclaration::determineSize(Loc loc
)
423 //printf("AggregateDeclaration::determineSize() %s, sizeok = %d\n", toChars(), sizeok);
425 // The previous instance size finalizing had:
426 if (type
->ty
== Terror
)
427 return false; // failed already
428 if (sizeok
== SIZEOKdone
)
429 return true; // succeeded
433 error(loc
, "unknown size");
440 // Determine the instance size of base class first.
441 if (ClassDeclaration
*cd
= isClassDeclaration())
444 if (cd
&& !cd
->determineSize(loc
))
448 // Determine instance fields when sizeok == SIZEOKnone
449 if (!determineFields())
451 if (sizeok
!= SIZEOKdone
)
454 // this aggregate type has:
455 if (type
->ty
== Terror
)
456 return false; // marked as invalid during the finalizing.
457 if (sizeok
== SIZEOKdone
)
458 return true; // succeeded to calculate instance size.
461 // There's unresolvable forward reference.
462 if (type
!= Type::terror
)
463 error(loc
, "no size because of forward reference");
464 // Don't cache errors from speculative semantic, might be resolvable later.
465 // https://issues.dlang.org/show_bug.cgi?id=16574
474 void StructDeclaration::semanticTypeInfoMembers()
478 xeq
->semanticRun
< PASSsemantic3done
)
480 unsigned errors
= global
.startGagging();
481 xeq
->semantic3(xeq
->_scope
);
482 if (global
.endGagging(errors
))
488 xcmp
->semanticRun
< PASSsemantic3done
)
490 unsigned errors
= global
.startGagging();
491 xcmp
->semantic3(xcmp
->_scope
);
492 if (global
.endGagging(errors
))
496 FuncDeclaration
*ftostr
= search_toString(this);
499 ftostr
->semanticRun
< PASSsemantic3done
)
501 ftostr
->semantic3(ftostr
->_scope
);
506 xhash
->semanticRun
< PASSsemantic3done
)
508 xhash
->semantic3(xhash
->_scope
);
513 postblit
->semanticRun
< PASSsemantic3done
)
515 postblit
->semantic3(postblit
->_scope
);
520 dtor
->semanticRun
< PASSsemantic3done
)
522 dtor
->semantic3(dtor
->_scope
);
526 d_uns64
AggregateDeclaration::size(Loc loc
)
528 //printf("+AggregateDeclaration::size() %s, scope = %p, sizeok = %d\n", toChars(), _scope, sizeok);
529 bool ok
= determineSize(loc
);
530 //printf("-AggregateDeclaration::size() %s, scope = %p, sizeok = %d\n", toChars(), _scope, sizeok);
531 return ok
? structsize
: SIZE_INVALID
;
534 Type
*AggregateDeclaration::getType()
539 bool AggregateDeclaration::isDeprecated()
544 bool AggregateDeclaration::isExport() const
546 return protection
.kind
== PROTexport
;
549 /***************************************
550 * Calculate field[i].overlapped and overlapUnsafe, and check that all of explicit
551 * field initializers have unique memory space on instance.
553 * true if any errors happen.
556 bool AggregateDeclaration::checkOverlappedFields()
558 //printf("AggregateDeclaration::checkOverlappedFields() %s\n", toChars());
559 assert(sizeok
== SIZEOKdone
);
560 size_t nfields
= fields
.length
;
563 ClassDeclaration
*cd
= isClassDeclaration();
564 if (!cd
|| !cd
->baseClass
|| !cd
->baseClass
->isNested())
569 // Fill in missing any elements with default initializers
570 for (size_t i
= 0; i
< nfields
; i
++)
572 VarDeclaration
*vd
= fields
[i
];
579 VarDeclaration
*vx
= vd
;
580 if (vd
->_init
&& vd
->_init
->isVoidInitializer())
583 // Find overlapped fields with the hole [vd->offset .. vd->offset->size()].
584 for (size_t j
= 0; j
< nfields
; j
++)
588 VarDeclaration
*v2
= fields
[j
];
594 if (!vd
->isOverlappedWith(v2
))
597 // vd and v2 are overlapping.
598 vd
->overlapped
= true;
599 v2
->overlapped
= true;
601 if (!MODimplicitConv(vd
->type
->mod
, v2
->type
->mod
))
602 v2
->overlapUnsafe
= true;
603 if (!MODimplicitConv(v2
->type
->mod
, vd
->type
->mod
))
604 vd
->overlapUnsafe
= true;
608 if (v2
->_init
&& v2
->_init
->isVoidInitializer())
611 if (vx
->_init
&& v2
->_init
)
613 ::error(loc
, "overlapping default initialization for field %s and %s", v2
->toChars(), vd
->toChars());
621 /***************************************
622 * Fill out remainder of elements[] with default initializers for fields[].
625 * elements: explicit arguments which given to construct object.
626 * ctorinit: true if the elements will be used for default initialization.
628 * false if any errors occur.
629 * Otherwise, returns true and the missing arguments will be pushed in elements[].
631 bool AggregateDeclaration::fill(Loc loc
, Expressions
*elements
, bool ctorinit
)
633 //printf("AggregateDeclaration::fill() %s\n", toChars());
634 assert(sizeok
== SIZEOKdone
);
636 size_t nfields
= fields
.length
- isNested();
639 size_t dim
= elements
->length
;
640 elements
->setDim(nfields
);
641 for (size_t i
= dim
; i
< nfields
; i
++)
642 (*elements
)[i
] = NULL
;
644 // Fill in missing any elements with default initializers
645 for (size_t i
= 0; i
< nfields
; i
++)
650 VarDeclaration
*vd
= fields
[i
];
651 VarDeclaration
*vx
= vd
;
652 if (vd
->_init
&& vd
->_init
->isVoidInitializer())
655 // Find overlapped fields with the hole [vd->offset .. vd->offset->size()].
657 for (size_t j
= 0; j
< nfields
; j
++)
661 VarDeclaration
*v2
= fields
[j
];
662 if (!vd
->isOverlappedWith(v2
))
670 if (v2
->_init
&& v2
->_init
->isVoidInitializer())
675 /* Prefer first found non-void-initialized field
676 * union U { int a; int b = 2; }
677 * U u; // Error: overlapping initialization for field a and b
686 ::error(loc
, "overlapping initialization for field %s and %s",
687 v2
->toChars(), vd
->toChars());
693 // Will fix Bugzilla 1432 by enabling this path always
695 /* Prefer explicitly initialized field
696 * union U { int a; int b = 2; }
697 * U u; // OK (u.b == 2)
699 if (!vx
|| (!vx
->_init
&& v2
->_init
))
704 else if (vx
!= vd
&& !vx
->isOverlappedWith(v2
))
706 // Both vx and v2 fills vd, but vx and v2 does not overlap
708 else if (vx
->_init
&& v2
->_init
)
710 ::error(loc
, "overlapping default initialization for field %s and %s",
711 v2
->toChars(), vd
->toChars());
715 assert(vx
->_init
|| (!vx
->_init
&& !v2
->_init
));
721 if (vx
->type
->size() == 0)
727 assert(!vx
->_init
->isVoidInitializer());
728 if (vx
->inuse
) // https://issues.dlang.org/show_bug.cgi?id=18057
730 vx
->error(loc
, "recursive initialization of field");
735 e
= vx
->getConstInitializer(false);
739 if ((vx
->storage_class
& STCnodefaultctor
) && !ctorinit
)
741 ::error(loc
, "field %s.%s must be initialized because it has no default constructor",
742 type
->toChars(), vx
->toChars());
746 /* Bugzilla 12509: Get the element of static array type.
748 Type
*telem
= vx
->type
;
749 if (telem
->ty
== Tsarray
)
751 /* We cannot use Type::baseElemOf() here.
752 * If the bottom of the Tsarray is an enum type, baseElemOf()
753 * will return the base of the enum, and its default initializer
754 * would be different from the enum's.
756 while (telem
->toBasetype()->ty
== Tsarray
)
757 telem
= ((TypeSArray
*)telem
->toBasetype())->next
;
759 if (telem
->ty
== Tvoid
)
760 telem
= Type::tuns8
->addMod(telem
->mod
);
762 if (telem
->needsNested() && ctorinit
)
763 e
= telem
->defaultInit(loc
);
765 e
= telem
->defaultInitLiteral(loc
);
767 (*elements
)[fieldi
] = e
;
771 for (size_t i
= 0; i
< elements
->length
; i
++)
773 Expression
*e
= (*elements
)[i
];
774 if (e
&& e
->op
== TOKerror
)
781 /****************************
782 * Do byte or word alignment as necessary.
783 * Align sizes of 0, as we may not know array sizes yet.
785 * alignment: struct alignment that is in effect
786 * size: alignment requirement of field
789 void AggregateDeclaration::alignmember(
790 structalign_t alignment
,
794 //printf("alignment = %d, size = %d, offset = %d\n",alignment,size,offset);
797 case (structalign_t
) 1:
801 case (structalign_t
) STRUCTALIGN_DEFAULT
:
802 // Alignment in Target::fieldalignsize must match what the
803 // corresponding C compiler's default alignment behavior is.
804 assert(size
> 0 && !(size
& (size
- 1)));
805 *poffset
= (*poffset
+ size
- 1) & ~(size
- 1);
809 // Align on alignment boundary, which must be a positive power of 2
810 assert(alignment
> 0 && !(alignment
& (alignment
- 1)));
811 *poffset
= (*poffset
+ alignment
- 1) & ~(alignment
- 1);
816 /****************************************
817 * Place a member (mem) into an aggregate (agg), which can be a struct, union or class
819 * offset to place field at
821 * nextoffset: next location in aggregate
822 * memsize: size of member
823 * memalignsize: natural alignment of member
824 * alignment: alignment in effect for this member
825 * paggsize: size of aggregate (updated)
826 * paggalignsize: alignment of aggregate (updated)
827 * isunion: the aggregate is a union
829 unsigned AggregateDeclaration::placeField(
830 unsigned *nextoffset
,
832 unsigned memalignsize
,
833 structalign_t alignment
,
835 unsigned *paggalignsize
,
839 unsigned ofs
= *nextoffset
;
841 const unsigned actualAlignment
=
842 alignment
== STRUCTALIGN_DEFAULT
? memalignsize
: alignment
;
844 alignmember(alignment
, memalignsize
, &ofs
);
845 unsigned memoffset
= ofs
;
852 if (*paggalignsize
< actualAlignment
)
853 *paggalignsize
= actualAlignment
;
859 /****************************************
860 * Returns true if there's an extra member which is the 'this'
861 * pointer to the enclosing context (enclosing aggregate or function)
864 bool AggregateDeclaration::isNested()
866 return enclosing
!= NULL
;
869 /* Append vthis field (this->tupleof[$-1]) to make this aggregate type nested.
871 void AggregateDeclaration::makeNested()
873 if (enclosing
) // if already nested
875 if (sizeok
== SIZEOKdone
)
877 if (isUnionDeclaration() || isInterfaceDeclaration())
879 if (storage_class
& STCstatic
)
882 // If nested struct, add in hidden 'this' pointer to outer scope
883 Dsymbol
*s
= toParent2();
887 if (FuncDeclaration
*fd
= s
->isFuncDeclaration())
891 /* Bugzilla 14422: If a nested class parent is a function, its
892 * context pointer (== `outer`) should be void* always.
896 else if (AggregateDeclaration
*ad
= s
->isAggregateDeclaration())
898 if (isClassDeclaration() && ad
->isClassDeclaration())
902 else if (isStructDeclaration())
904 if (TemplateInstance
*ti
= ad
->parent
->isTemplateInstance())
906 enclosing
= ti
->enclosing
;
910 t
= ad
->handleType();
914 //printf("makeNested %s, enclosing = %s\n", toChars(), enclosing->toChars());
916 if (t
->ty
== Tstruct
)
917 t
= Type::tvoidptr
; // t should not be a ref type
919 vthis
= new ThisDeclaration(loc
, t
);
920 //vthis->storage_class |= STCref;
922 // Emulate vthis->addMember()
923 members
->push(vthis
);
925 // Emulate vthis->semantic()
926 vthis
->storage_class
|= STCfield
;
927 vthis
->parent
= this;
928 vthis
->protection
= Prot(PROTpublic
);
929 vthis
->alignment
= t
->alignment();
930 vthis
->semanticRun
= PASSsemanticdone
;
932 if (sizeok
== SIZEOKfwd
)
937 /*******************************************
938 * Look for constructor declaration.
940 Dsymbol
*AggregateDeclaration::searchCtor()
942 Dsymbol
*s
= search(Loc(), Id::ctor
);
945 if (!(s
->isCtorDeclaration() ||
946 s
->isTemplateDeclaration() ||
949 s
->error("is not a constructor; identifiers starting with __ are reserved for the implementation");
954 if (s
&& s
->toParent() != this)
955 s
= NULL
; // search() looks through ancestor classes
958 // Finish all constructors semantics to determine this->noDefaultCtor.
961 static int fp(Dsymbol
*s
, void *)
963 CtorDeclaration
*f
= s
->isCtorDeclaration();
964 if (f
&& f
->semanticRun
== PASSinit
)
970 for (size_t i
= 0; i
< members
->length
; i
++)
972 Dsymbol
*sm
= (*members
)[i
];
973 sm
->apply(&SearchCtor::fp
, NULL
);
979 /********************************* StructDeclaration ****************************/
981 StructDeclaration::StructDeclaration(Loc loc
, Identifier
*id
, bool inObject
)
982 : AggregateDeclaration(loc
, id
)
984 zeroInit
= 0; // assume false until we do semantic processing
985 hasIdentityAssign
= false;
986 hasIdentityEquals
= false;
996 requestTypeInfo
= false;
998 // For forward references
999 type
= new TypeStruct(this);
1003 if (id
== Id::ModuleInfo
&& !Module::moduleinfo
)
1004 Module::moduleinfo
= this;
1008 StructDeclaration
*StructDeclaration::create(Loc loc
, Identifier
*id
, bool inObject
)
1010 return new StructDeclaration(loc
, id
, inObject
);
1013 Dsymbol
*StructDeclaration::syntaxCopy(Dsymbol
*s
)
1015 StructDeclaration
*sd
=
1016 s
? (StructDeclaration
*)s
1017 : new StructDeclaration(loc
, ident
, false);
1018 return ScopeDsymbol::syntaxCopy(sd
);
1021 void StructDeclaration::semantic(Scope
*sc
)
1023 //printf("StructDeclaration::semantic(this=%p, %s '%s', sizeok = %d)\n", this, parent->toChars(), toChars(), sizeok);
1025 //static int count; if (++count == 20) halt();
1027 if (semanticRun
>= PASSsemanticdone
)
1029 unsigned errors
= global
.errors
;
1031 //printf("+StructDeclaration::semantic(this=%p, %s '%s', sizeok = %d)\n", this, parent->toChars(), toChars(), sizeok);
1036 scx
= _scope
; // save so we don't make redundant copies
1042 assert(sc
->parent
&& sc
->func
);
1043 parent
= sc
->parent
;
1045 assert(parent
&& !isAnonymous());
1048 type
= Type::terror
;
1049 if (semanticRun
== PASSinit
)
1050 type
= type
->addSTC(sc
->stc
| storage_class
);
1051 type
= type
->semantic(loc
, sc
);
1053 if (type
->ty
== Tstruct
&& ((TypeStruct
*)type
)->sym
!= this)
1055 TemplateInstance
*ti
= ((TypeStruct
*)type
)->sym
->isInstantiated();
1056 if (ti
&& isError(ti
))
1057 ((TypeStruct
*)type
)->sym
= this;
1060 // Ungag errors when not speculative
1061 Ungag ungag
= ungagSpeculative();
1063 if (semanticRun
== PASSinit
)
1065 protection
= sc
->protection
;
1067 alignment
= sc
->alignment();
1069 storage_class
|= sc
->stc
;
1070 if (storage_class
& STCdeprecated
)
1071 isdeprecated
= true;
1072 if (storage_class
& STCabstract
)
1073 error("structs, unions cannot be abstract");
1074 userAttribDecl
= sc
->userAttribDecl
;
1076 if (sc
->linkage
== LINKcpp
)
1077 classKind
= ClassKind::cpp
;
1079 else if (symtab
&& !scx
)
1083 semanticRun
= PASSsemantic
;
1085 if (!members
) // if opaque declaration
1087 semanticRun
= PASSsemanticdone
;
1092 symtab
= new DsymbolTable();
1094 for (size_t i
= 0; i
< members
->length
; i
++)
1096 Dsymbol
*s
= (*members
)[i
];
1097 //printf("adding member '%s' to '%s'\n", s->toChars(), this->toChars());
1098 s
->addMember(sc
, this);
1102 Scope
*sc2
= newScope(sc
);
1104 /* Set scope so if there are forward references, we still might be able to
1105 * resolve individual members like enums.
1107 for (size_t i
= 0; i
< members
->length
; i
++)
1109 Dsymbol
*s
= (*members
)[i
];
1110 //printf("struct: setScope %s %s\n", s->kind(), s->toChars());
1114 for (size_t i
= 0; i
< members
->length
; i
++)
1116 Dsymbol
*s
= (*members
)[i
];
1120 for (size_t i
= 0; i
< members
->length
; i
++)
1122 Dsymbol
*s
= (*members
)[i
];
1126 if (!determineFields())
1128 assert(type
->ty
== Terror
);
1130 semanticRun
= PASSsemanticdone
;
1134 /* Following special member functions creation needs semantic analysis
1135 * completion of sub-structs in each field types. For example, buildDtor
1136 * needs to check existence of elaborate dtor in type of each fields.
1137 * See the case in compilable/test14838.d
1139 for (size_t i
= 0; i
< fields
.length
; i
++)
1141 VarDeclaration
*v
= fields
[i
];
1142 Type
*tb
= v
->type
->baseElemOf();
1143 if (tb
->ty
!= Tstruct
)
1145 StructDeclaration
*sd
= ((TypeStruct
*)tb
)->sym
;
1146 if (sd
->semanticRun
>= PASSsemanticdone
)
1151 _scope
= scx
? scx
: sc
->copy();
1152 _scope
->setNoFree();
1153 _scope
->_module
->addDeferredSemantic(this);
1155 //printf("\tdeferring %s\n", toChars());
1159 /* Look for special member functions.
1161 aggNew
= (NewDeclaration
*)search(Loc(), Id::classNew
);
1162 aggDelete
= (DeleteDeclaration
*)search(Loc(), Id::classDelete
);
1164 // Look for the constructor
1165 ctor
= searchCtor();
1167 dtor
= buildDtor(this, sc2
);
1168 postblit
= buildPostBlit(this, sc2
);
1170 buildOpAssign(this, sc2
);
1171 buildOpEquals(this, sc2
);
1173 if (global
.params
.useTypeInfo
&& Type::dtypeinfo
) // these functions are used for TypeInfo
1175 xeq
= buildXopEquals(this, sc2
);
1176 xcmp
= buildXopCmp(this, sc2
);
1177 xhash
= buildXtoHash(this, sc2
);
1180 inv
= buildInv(this, sc2
);
1182 Module::dprogress
++;
1183 semanticRun
= PASSsemanticdone
;
1184 //printf("-StructDeclaration::semantic(this=%p, '%s')\n", this, toChars());
1190 Dsymbol
*scall
= search(Loc(), Id::call
);
1193 unsigned xerrors
= global
.startGagging();
1197 FuncDeclaration
*fcall
= resolveFuncCall(loc
, sc
, scall
, NULL
, NULL
, NULL
, 1);
1199 global
.endGagging(xerrors
);
1201 if (fcall
&& fcall
->isStatic())
1203 error(fcall
->loc
, "static opCall is hidden by constructors and can never be called");
1204 errorSupplemental(fcall
->loc
, "Please use a factory method instead, or replace all constructors with static opCall.");
1209 if (global
.errors
!= errors
)
1211 // The type is no good.
1212 type
= Type::terror
;
1213 this->errors
= true;
1215 deferred
->errors
= true;
1218 if (deferred
&& !global
.gag
)
1220 deferred
->semantic2(sc
);
1221 deferred
->semantic3(sc
);
1224 assert(type
->ty
!= Tstruct
|| ((TypeStruct
*)type
)->sym
== this);
1227 Dsymbol
*StructDeclaration::search(const Loc
&loc
, Identifier
*ident
, int flags
)
1229 //printf("%s.StructDeclaration::search('%s', flags = x%x)\n", toChars(), ident->toChars(), flags);
1231 if (_scope
&& !symtab
)
1234 if (!members
|| !symtab
) // opaque or semantic() is not yet called
1236 error("is forward referenced when looking for '%s'", ident
->toChars());
1240 return ScopeDsymbol::search(loc
, ident
, flags
);
1243 void StructDeclaration::finalizeSize()
1245 //printf("StructDeclaration::finalizeSize() %s, sizeok = %d\n", toChars(), sizeok);
1246 assert(sizeok
!= SIZEOKdone
);
1248 //printf("+StructDeclaration::finalizeSize() %s, fields.length = %d, sizeok = %d\n", toChars(), fields.length, sizeok);
1250 fields
.setDim(0); // workaround
1252 // Set the offsets of the fields and determine the size of the struct
1253 unsigned offset
= 0;
1254 bool isunion
= isUnionDeclaration() != NULL
;
1255 for (size_t i
= 0; i
< members
->length
; i
++)
1257 Dsymbol
*s
= (*members
)[i
];
1258 s
->setFieldOffset(this, &offset
, isunion
);
1260 if (type
->ty
== Terror
)
1263 // 0 sized struct's are set to 1 byte
1264 if (structsize
== 0)
1270 // Round struct size up to next alignsize boundary.
1271 // This will ensure that arrays of structs will get their internals
1272 // aligned properly.
1273 if (alignment
== STRUCTALIGN_DEFAULT
)
1274 structsize
= (structsize
+ alignsize
- 1) & ~(alignsize
- 1);
1276 structsize
= (structsize
+ alignment
- 1) & ~(alignment
- 1);
1278 sizeok
= SIZEOKdone
;
1280 //printf("-StructDeclaration::finalizeSize() %s, fields.length = %d, structsize = %d\n", toChars(), fields.length, structsize);
1285 // Calculate fields[i]->overlapped
1286 if (checkOverlappedFields())
1292 // Determine if struct is all zeros or not
1294 for (size_t i
= 0; i
< fields
.length
; i
++)
1296 VarDeclaration
*vd
= fields
[i
];
1299 // Should examine init to see if it is really all 0's
1303 else if (!vd
->type
->isZeroInit(loc
))
1310 TypeTuple
*tt
= Target::toArgTypes(type
);
1311 size_t dim
= tt
? tt
->arguments
->length
: 0;
1315 arg1type
= (*tt
->arguments
)[0]->type
;
1317 arg2type
= (*tt
->arguments
)[1]->type
;
1321 /***************************************
1322 * Fit elements[] to the corresponding type of field[].
1326 * elements The explicit arguments that given to construct object.
1327 * stype The constructed object type.
1328 * Returns false if any errors occur.
1329 * Otherwise, returns true and elements[] are rewritten for the output.
1331 bool StructDeclaration::fit(Loc loc
, Scope
*sc
, Expressions
*elements
, Type
*stype
)
1336 size_t nfields
= fields
.length
- isNested();
1338 for (size_t i
= 0; i
< elements
->length
; i
++)
1340 Expression
*e
= (*elements
)[i
];
1344 e
= resolveProperties(sc
, e
);
1347 if (i
== fields
.length
- 1 && isNested() && e
->op
== TOKnull
)
1349 // CTFE sometimes creates null as hidden pointer; we'll allow this.
1352 ::error(loc
, "more initializers than fields (%d) of %s", (int)nfields
, toChars());
1355 VarDeclaration
*v
= fields
[i
];
1356 if (v
->offset
< offset
)
1358 ::error(loc
, "overlapping initialization for %s", v
->toChars());
1361 offset
= (unsigned)(v
->offset
+ v
->type
->size());
1365 t
= t
->addMod(stype
->mod
);
1367 Type
*tb
= t
->toBasetype();
1369 /* Look for case of initializing a static array with a too-short
1370 * string literal, such as:
1371 * char[5] foo = "abc";
1372 * Allow this by doing an explicit cast, which will lengthen the string
1375 if (e
->op
== TOKstring
&& tb
->ty
== Tsarray
)
1377 StringExp
*se
= (StringExp
*)e
;
1378 Type
*typeb
= se
->type
->toBasetype();
1379 TY tynto
= tb
->nextOf()->ty
;
1380 if (!se
->committed
&&
1381 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
) &&
1382 (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
) &&
1383 se
->numberOfCodeUnits(tynto
) < ((TypeSArray
*)tb
)->dim
->toInteger())
1385 e
= se
->castTo(sc
, t
);
1390 while (!e
->implicitConvTo(t
) && tb
->ty
== Tsarray
)
1392 /* Static array initialization, as in:
1396 tb
= t
->toBasetype();
1398 if (!e
->implicitConvTo(t
))
1399 t
= origType
; // restore type for better diagnostic
1401 e
= e
->implicitCastTo(sc
, t
);
1403 if (e
->op
== TOKerror
)
1406 (*elements
)[i
] = doCopyOrMove(sc
, e
);
1411 /***************************************
1412 * Return true if struct is POD (Plain Old Data).
1413 * This is defined as:
1415 * no postblits, destructors, or assignment operators
1416 * no 'ref' fields or fields that are themselves non-POD
1417 * The idea being these are compatible with C structs.
1419 bool StructDeclaration::isPOD()
1421 // If we've already determined whether this struct is POD.
1422 if (ispod
!= ISPODfwd
)
1423 return (ispod
== ISPODyes
);
1427 if (enclosing
|| postblit
|| dtor
)
1430 // Recursively check all fields are POD.
1431 for (size_t i
= 0; i
< fields
.length
; i
++)
1433 VarDeclaration
*v
= fields
[i
];
1434 if (v
->storage_class
& STCref
)
1440 Type
*tv
= v
->type
->baseElemOf();
1441 if (tv
->ty
== Tstruct
)
1443 TypeStruct
*ts
= (TypeStruct
*)tv
;
1444 StructDeclaration
*sd
= ts
->sym
;
1453 return (ispod
== ISPODyes
);
1456 const char *StructDeclaration::kind() const
1461 /********************************* UnionDeclaration ****************************/
1463 UnionDeclaration::UnionDeclaration(Loc loc
, Identifier
*id
)
1464 : StructDeclaration(loc
, id
, false)
1468 Dsymbol
*UnionDeclaration::syntaxCopy(Dsymbol
*s
)
1471 UnionDeclaration
*ud
= new UnionDeclaration(loc
, ident
);
1472 return StructDeclaration::syntaxCopy(ud
);
1475 const char *UnionDeclaration::kind() const