2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2023 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
44 FuncDeclaration
*search_toString(StructDeclaration
*sd
);
46 enum class ClassKind
: uint8_t
48 /// the aggregate is a d(efault) struct/class/interface
50 /// the aggregate is a C++ struct/class/interface
52 /// the aggregate is an Objective-C class/interface
54 /// the aggregate is a C struct
64 class AggregateDeclaration
: public ScopeDsymbol
68 StorageClass storage_class
;
69 unsigned structsize
; // size of struct
70 unsigned alignsize
; // size of struct for alignment purposes
71 VarDeclarations fields
; // VarDeclaration fields
72 Dsymbol
*deferred
; // any deferred semantic2() or semantic3() symbol
74 ClassKind classKind
; // specifies the linkage type
77 // overridden symbol with pragma(mangle, "...")
78 MangleOverride
*pMangleOverride
;
79 /* !=NULL if is nested
80 * pointing to the dsymbol that directly enclosing it.
81 * 1. The function that enclosing it (nested struct and class)
82 * 2. The class that enclosing it (nested class only)
83 * 3. If enclosing aggregate is template, its enclosing dsymbol.
84 * See AggregateDeclaraton::makeNested for the details.
87 VarDeclaration
*vthis
; // 'this' parameter if this aggregate is nested
88 VarDeclaration
*vthis2
; // 'this' parameter if this aggregate is a template and is nested
89 // Special member functions
90 FuncDeclarations invs
; // Array of invariants
91 FuncDeclaration
*inv
; // invariant
93 Dsymbol
*ctor
; // CtorDeclaration or TemplateDeclaration
95 // default constructor - should have no arguments, because
96 // it would be stored in TypeInfo_Class.defaultConstructor
97 CtorDeclaration
*defaultCtor
;
99 AliasThis
*aliasthis
; // forward unresolved lookups to aliasthis
101 DtorDeclarations userDtors
; // user-defined destructors (`~this()`) - mixins can yield multiple ones
102 DtorDeclaration
*aggrDtor
; // aggregate destructor calling userDtors and fieldDtor (and base class aggregate dtor for C++ classes)
103 DtorDeclaration
*dtor
; // the aggregate destructor exposed as `__xdtor` alias
104 // (same as aggrDtor, except for C++ classes with virtual dtor on Windows)
105 DtorDeclaration
*tidtor
; // aggregate destructor used in TypeInfo (must have extern(D) ABI)
106 DtorDeclaration
*fieldDtor
; // function destructing (non-inherited) fields
108 Expression
*getRTInfo
; // pointer to GC info generated by object.RTInfo(this)
110 Visibility visibility
;
111 d_bool noDefaultCtor
; // no default construction
112 d_bool disableNew
; // disallow allocations using `new`
113 Sizeok sizeok
; // set when structsize contains valid data
115 virtual Scope
*newScope(Scope
*sc
);
116 void setScope(Scope
*sc
) override final
;
117 virtual void finalizeSize() = 0;
118 uinteger_t
size(const Loc
&loc
) override final
;
119 bool fill(const Loc
&loc
, Expressions
&elements
, bool ctorinit
);
120 Type
*getType() override final
;
121 bool isDeprecated() const override final
; // is aggregate deprecated?
122 bool isNested() const;
123 bool isExport() const override final
;
125 Visibility
visible() override final
;
128 Type
*handleType() { return type
; }
135 AggregateDeclaration
*isAggregateDeclaration() override final
{ return this; }
136 void accept(Visitor
*v
) override
{ v
->visit(this); }
144 hasPointers
= 0x1 // NB: should use noPointers as in ClassFlags
148 class StructDeclaration
: public AggregateDeclaration
151 FuncDeclarations postblits
; // Array of postblit functions
152 FuncDeclaration
*postblit
; // aggregate postblit
154 FuncDeclaration
*xeq
; // TypeInfo_Struct.xopEquals
155 FuncDeclaration
*xcmp
; // TypeInfo_Struct.xopCmp
156 FuncDeclaration
*xhash
; // TypeInfo_Struct.xtoHash
157 static FuncDeclaration
*xerreq
; // object.xopEquals
158 static FuncDeclaration
*xerrcmp
; // object.xopCmp
160 // ABI-specific type(s) if the struct can be passed in registers
163 structalign_t alignment
; // alignment applied outside of the struct
164 ThreeState ispod
; // if struct is POD
168 static StructDeclaration
*create(const Loc
&loc
, Identifier
*id
, bool inObject
);
169 StructDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
170 const char *kind() const override
;
171 void finalizeSize() override final
;
173 bool zeroInit() const; // !=0 if initialize with 0 fill
174 bool zeroInit(bool v
);
175 bool hasIdentityAssign() const; // true if has identity opAssign
176 bool hasIdentityAssign(bool v
);
177 bool hasBlitAssign() const; // true if opAssign is a blit
178 bool hasBlitAssign(bool v
);
179 bool hasIdentityEquals() const; // true if has identity opEquals
180 bool hasIdentityEquals(bool v
);
181 bool hasNoFields() const; // has no fields
182 bool hasNoFields(bool v
);
183 bool hasCopyCtor() const; // copy constructor
184 bool hasCopyCtor(bool v
);
185 // Even if struct is defined as non-root symbol, some built-in operations
186 // (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo.
187 // For those, today TypeInfo_Struct is generated in COMDAT.
188 bool requestTypeInfo() const;
189 bool requestTypeInfo(bool v
);
191 StructDeclaration
*isStructDeclaration() override final
{ return this; }
192 void accept(Visitor
*v
) override
{ v
->visit(this); }
194 unsigned numArgTypes() const;
195 Type
*argType(unsigned index
);
196 bool hasRegularCtor(bool checkDisabled
= false);
199 class UnionDeclaration final
: public StructDeclaration
202 UnionDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
203 const char *kind() const override
;
205 UnionDeclaration
*isUnionDeclaration() override
{ return this; }
206 void accept(Visitor
*v
) override
{ v
->visit(this); }
211 Type
*type
; // (before semantic processing)
213 ClassDeclaration
*sym
;
214 unsigned offset
; // 'this' pointer offset
215 // for interfaces: Array of FuncDeclaration's
216 // making up the vtbl[]
217 FuncDeclarations vtbl
;
219 DArray
<BaseClass
> baseInterfaces
; // if BaseClass is an interface, these
220 // are a copy of the InterfaceDeclaration::interfaces
222 bool fillVtbl(ClassDeclaration
*cd
, FuncDeclarations
*vtbl
, int newinstance
);
234 hasGetMembers
= 0x10,
242 class ClassDeclaration
: public AggregateDeclaration
245 static ClassDeclaration
*object
;
246 static ClassDeclaration
*throwable
;
247 static ClassDeclaration
*exception
;
248 static ClassDeclaration
*errorException
;
249 static ClassDeclaration
*cpp_type_info_ptr
;
251 ClassDeclaration
*baseClass
; // NULL only if this is Object
252 FuncDeclaration
*staticCtor
;
253 FuncDeclaration
*staticDtor
;
254 Dsymbols vtbl
; // Array of FuncDeclaration's making up the vtbl[]
255 Dsymbols vtblFinal
; // More FuncDeclaration's that aren't in vtbl[]
257 BaseClasses
*baseclasses
; // Array of BaseClass's; first is super,
258 // rest are Interface's
260 DArray
<BaseClass
*> interfaces
; // interfaces[interfaces_dim] for this class
261 // (does not include baseClass)
263 BaseClasses
*vtblInterfaces
; // array of base interfaces that have
266 TypeInfoClassDeclaration
*vclassinfo
; // the ClassInfo object for this ClassDeclaration
267 d_bool com
; // true if this is a COM class (meaning it derives from IUnknown)
268 d_bool stack
; // true if this is a scope class
269 int cppDtorVtblIndex
; // slot reserved for the virtual destructor [extern(C++)]
270 d_bool inuse
; // to prevent recursive attempts
272 ThreeState isabstract
; // if abstract class
273 Baseok baseok
; // set the progress of base classes resolving
274 ObjcClassDeclaration objc
; // Data for a class declaration that is needed for the Objective-C integration
275 Symbol
*cpp_type_info_ptr_sym
; // cached instance of class Id.cpp_type_info_ptr
277 static ClassDeclaration
*create(const Loc
&loc
, Identifier
*id
, BaseClasses
*baseclasses
, Dsymbols
*members
, bool inObject
);
278 const char *toPrettyChars(bool QualifyTypes
= false) override
;
279 ClassDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
280 Scope
*newScope(Scope
*sc
) override
;
282 #define OFFSET_RUNTIME 0x76543210
283 #define OFFSET_FWDREF 0x76543211
284 virtual bool isBaseOf(ClassDeclaration
*cd
, int *poffset
);
286 bool isBaseInfoComplete();
287 void finalizeSize() override
;
289 bool isFuncHidden(FuncDeclaration
*fd
);
290 bool isCOMclass() const;
291 virtual bool isCOMinterface() const;
292 bool isCPPclass() const;
293 virtual bool isCPPinterface() const;
295 virtual int vtblOffset() const;
296 const char *kind() const override
;
298 void addObjcSymbols(ClassDeclarations
*classes
, ClassDeclarations
*categories
) override final
;
302 Dsymbol
*vtblSymbol();
304 ClassDeclaration
*isClassDeclaration() override final
{ return (ClassDeclaration
*)this; }
305 void accept(Visitor
*v
) override
{ v
->visit(this); }
308 class InterfaceDeclaration final
: public ClassDeclaration
311 InterfaceDeclaration
*syntaxCopy(Dsymbol
*s
) override
;
312 Scope
*newScope(Scope
*sc
) override
;
313 bool isBaseOf(ClassDeclaration
*cd
, int *poffset
) override
;
314 const char *kind() const override
;
315 int vtblOffset() const override
;
316 bool isCPPinterface() const override
;
317 bool isCOMinterface() const override
;
319 InterfaceDeclaration
*isInterfaceDeclaration() override
{ return this; }
320 void accept(Visitor
*v
) override
{ v
->visit(this); }