2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * https://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * https://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/dlang/dmd/blob/master/src/dmd/aggregate.h
21 class FuncDeclaration
;
22 class CtorDeclaration
;
23 class DtorDeclaration
;
24 class InterfaceDeclaration
;
25 class TypeInfoClassDeclaration
;
28 enum class Sizeok
: uint8_t
30 none
, // size of aggregate is not yet able to compute
31 fwd
, // size of aggregate is ready to compute
32 inProcess
, // in the midst of computing the size
33 done
// size of aggregate is set correctly
36 enum class Baseok
: uint8_t
38 none
, // base classes not computed yet
39 in
, // in process of resolving base classes
40 done
, // all base classes are resolved
41 semanticdone
// all base classes semantic done
46 FuncDeclaration
*search_toString(StructDeclaration
*sd
);
47 void semanticTypeInfoMembers(StructDeclaration
*sd
);
50 enum class ClassKind
: uint8_t
52 /// the aggregate is a d(efault) struct/class/interface
54 /// the aggregate is a C++ struct/class/interface
56 /// the aggregate is an Objective-C class/interface
58 /// the aggregate is a C struct
68 class AggregateDeclaration
: public ScopeDsymbol
72 StorageClass storage_class
;
73 unsigned structsize
; // size of struct
74 unsigned alignsize
; // size of struct for alignment purposes
75 VarDeclarations fields
; // VarDeclaration fields
76 Dsymbol
*deferred
; // any deferred semantic2() or semantic3() symbol
78 ClassKind classKind
; // specifies the linkage type
81 // overridden symbol with pragma(mangle, "...")
82 MangleOverride
*pMangleOverride
;
83 /* !=NULL if is nested
84 * pointing to the dsymbol that directly enclosing it.
85 * 1. The function that enclosing it (nested struct and class)
86 * 2. The class that enclosing it (nested class only)
87 * 3. If enclosing aggregate is template, its enclosing dsymbol.
88 * See AggregateDeclaraton::makeNested for the details.
91 VarDeclaration
*vthis
; // 'this' parameter if this aggregate is nested
92 VarDeclaration
*vthis2
; // 'this' parameter if this aggregate is a template and is nested
93 // Special member functions
94 FuncDeclarations invs
; // Array of invariants
95 FuncDeclaration
*inv
; // invariant
97 Dsymbol
*ctor
; // CtorDeclaration or TemplateDeclaration
99 // default constructor - should have no arguments, because
100 // it would be stored in TypeInfo_Class.defaultConstructor
101 CtorDeclaration
*defaultCtor
;
103 AliasThis
*aliasthis
; // forward unresolved lookups to aliasthis
105 DtorDeclarations userDtors
; // user-defined destructors (`~this()`) - mixins can yield multiple ones
106 DtorDeclaration
*aggrDtor
; // aggregate destructor calling userDtors and fieldDtor (and base class aggregate dtor for C++ classes)
107 DtorDeclaration
*dtor
; // the aggregate destructor exposed as `__xdtor` alias
108 // (same as aggrDtor, except for C++ classes with virtual dtor on Windows)
109 DtorDeclaration
*tidtor
; // aggregate destructor used in TypeInfo (must have extern(D) ABI)
110 DtorDeclaration
*fieldDtor
; // function destructing (non-inherited) fields
112 Expression
*getRTInfo
; // pointer to GC info generated by object.RTInfo(this)
114 Visibility visibility
;
115 d_bool noDefaultCtor
; // no default construction
116 d_bool disableNew
; // disallow allocations using `new`
117 Sizeok sizeok
; // set when structsize contains valid data
119 virtual Scope
*newScope(Scope
*sc
);
120 virtual void finalizeSize() = 0;
121 uinteger_t
size(const Loc
&loc
) override final
;
122 bool fill(const Loc
&loc
, Expressions
&elements
, bool ctorinit
);
123 Type
*getType() override final
;
124 bool isDeprecated() const override final
; // is aggregate deprecated?
125 bool isNested() const;
126 bool isExport() const override final
;
128 Visibility
visible() override final
;
131 Type
*handleType() { return type
; }
138 AggregateDeclaration
*isAggregateDeclaration() override final
{ return this; }
139 void accept(Visitor
*v
) override
{ v
->visit(this); }
147 hasPointers
= 0x1 // NB: should use noPointers as in ClassFlags
151 class StructDeclaration
: public AggregateDeclaration
154 FuncDeclarations postblits
; // Array of postblit functions
155 FuncDeclaration
*postblit
; // aggregate postblit
157 FuncDeclaration
*xeq
; // TypeInfo_Struct.xopEquals
158 FuncDeclaration
*xcmp
; // TypeInfo_Struct.xopCmp
159 FuncDeclaration
*xhash
; // TypeInfo_Struct.xtoHash
160 static FuncDeclaration
*xerreq
; // object.xopEquals
161 static FuncDeclaration
*xerrcmp
; // object.xopCmp
163 // ABI-specific type(s) if the struct can be passed in registers
166 structalign_t alignment
; // alignment applied outside of the struct
167 ThreeState ispod
; // if struct is POD
171 static StructDeclaration
*create(const Loc
&loc
, Identifier
*id
, bool inObject
);
172 StructDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
173 const char *kind() const override
;
174 void finalizeSize() override final
;
176 bool zeroInit() const; // !=0 if initialize with 0 fill
177 bool zeroInit(bool v
);
178 bool hasIdentityAssign() const; // true if has identity opAssign
179 bool hasIdentityAssign(bool v
);
180 bool hasBlitAssign() const; // true if opAssign is a blit
181 bool hasBlitAssign(bool v
);
182 bool hasIdentityEquals() const; // true if has identity opEquals
183 bool hasIdentityEquals(bool v
);
184 bool hasNoFields() const; // has no fields
185 bool hasNoFields(bool v
);
186 bool hasCopyCtor() const; // copy constructor
187 bool hasCopyCtor(bool v
);
188 // Even if struct is defined as non-root symbol, some built-in operations
189 // (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo.
190 // For those, today TypeInfo_Struct is generated in COMDAT.
191 bool requestTypeInfo() const;
192 bool requestTypeInfo(bool v
);
194 StructDeclaration
*isStructDeclaration() override final
{ return this; }
195 void accept(Visitor
*v
) override
{ v
->visit(this); }
197 unsigned numArgTypes() const;
198 Type
*argType(unsigned index
);
199 bool hasRegularCtor(bool checkDisabled
= false);
202 class UnionDeclaration final
: public StructDeclaration
205 UnionDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
206 const char *kind() const override
;
208 UnionDeclaration
*isUnionDeclaration() override
{ return this; }
209 void accept(Visitor
*v
) override
{ v
->visit(this); }
214 Type
*type
; // (before semantic processing)
216 ClassDeclaration
*sym
;
217 unsigned offset
; // 'this' pointer offset
218 // for interfaces: Array of FuncDeclaration's
219 // making up the vtbl[]
220 FuncDeclarations vtbl
;
222 DArray
<BaseClass
> baseInterfaces
; // if BaseClass is an interface, these
223 // are a copy of the InterfaceDeclaration::interfaces
225 bool fillVtbl(ClassDeclaration
*cd
, FuncDeclarations
*vtbl
, int newinstance
);
237 hasGetMembers
= 0x10,
246 class ClassDeclaration
: public AggregateDeclaration
249 static ClassDeclaration
*object
;
250 static ClassDeclaration
*throwable
;
251 static ClassDeclaration
*exception
;
252 static ClassDeclaration
*errorException
;
253 static ClassDeclaration
*cpp_type_info_ptr
;
255 ClassDeclaration
*baseClass
; // NULL only if this is Object
256 FuncDeclaration
*staticCtor
;
257 FuncDeclaration
*staticDtor
;
258 Dsymbols vtbl
; // Array of FuncDeclaration's making up the vtbl[]
259 Dsymbols vtblFinal
; // More FuncDeclaration's that aren't in vtbl[]
261 BaseClasses
*baseclasses
; // Array of BaseClass's; first is super,
262 // rest are Interface's
264 DArray
<BaseClass
*> interfaces
; // interfaces[interfaces_dim] for this class
265 // (does not include baseClass)
267 BaseClasses
*vtblInterfaces
; // array of base interfaces that have
270 TypeInfoClassDeclaration
*vclassinfo
; // the ClassInfo object for this ClassDeclaration
271 d_bool com
; // true if this is a COM class (meaning it derives from IUnknown)
272 d_bool stack
; // true if this is a scope class
273 int cppDtorVtblIndex
; // slot reserved for the virtual destructor [extern(C++)]
274 d_bool inuse
; // to prevent recursive attempts
276 ThreeState isabstract
; // if abstract class
277 Baseok baseok
; // set the progress of base classes resolving
278 ObjcClassDeclaration objc
; // Data for a class declaration that is needed for the Objective-C integration
279 Symbol
*cpp_type_info_ptr_sym
; // cached instance of class Id.cpp_type_info_ptr
281 static ClassDeclaration
*create(const Loc
&loc
, Identifier
*id
, BaseClasses
*baseclasses
, Dsymbols
*members
, bool inObject
);
282 const char *toPrettyChars(bool QualifyTypes
= false) override
;
283 ClassDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
284 Scope
*newScope(Scope
*sc
) override
;
286 #define OFFSET_RUNTIME 0x76543210
287 #define OFFSET_FWDREF 0x76543211
288 virtual bool isBaseOf(ClassDeclaration
*cd
, int *poffset
);
290 bool isBaseInfoComplete();
291 void finalizeSize() override
;
293 bool isFuncHidden(FuncDeclaration
*fd
);
294 bool isCOMclass() const;
295 virtual bool isCOMinterface() const;
296 bool isCPPclass() const;
297 virtual bool isCPPinterface() const;
299 virtual int vtblOffset() const;
300 const char *kind() const override
;
302 void addObjcSymbols(ClassDeclarations
*classes
, ClassDeclarations
*categories
) override final
;
306 Dsymbol
*vtblSymbol();
308 ClassDeclaration
*isClassDeclaration() override final
{ return (ClassDeclaration
*)this; }
309 void accept(Visitor
*v
) override
{ v
->visit(this); }
312 class InterfaceDeclaration final
: public ClassDeclaration
315 InterfaceDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
316 Scope
*newScope(Scope
*sc
) override
;
317 bool isBaseOf(ClassDeclaration
*cd
, int *poffset
) override
;
318 const char *kind() const override
;
319 int vtblOffset() const override
;
320 bool isCPPinterface() const override
;
321 bool isCOMinterface() const override
;
323 InterfaceDeclaration
*isInterfaceDeclaration() override
{ return this; }
324 void accept(Visitor
*v
) override
{ v
->visit(this); }