d: Merge dmd, druntime d8e3976a58, phobos 7a6e95688
[official-gcc.git] / gcc / d / dmd / aggregate.h
blob9abdd094b3f21561565ce931043caaa5a40f3c03
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
9 */
11 #pragma once
13 #include "dsymbol.h"
14 #include "objc.h"
16 class AliasThis;
17 class Identifier;
18 class Type;
19 class TypeFunction;
20 class Expression;
21 class FuncDeclaration;
22 class CtorDeclaration;
23 class DtorDeclaration;
24 class InterfaceDeclaration;
25 class TypeInfoClassDeclaration;
26 class VarDeclaration;
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
51 cpp,
52 /// the aggregate is an Objective-C class/interface
53 objc,
54 /// the aggregate is a C struct
58 struct MangleOverride
60 Dsymbol *agg;
61 Identifier *id;
64 class AggregateDeclaration : public ScopeDsymbol
66 public:
67 Type *type;
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
75 CPPMANGLE cppmangle;
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.
86 Dsymbol *enclosing;
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 virtual void finalizeSize() = 0;
117 uinteger_t size(const Loc &loc) override final;
118 bool fill(const Loc &loc, Expressions &elements, bool ctorinit);
119 Type *getType() override final;
120 bool isDeprecated() const override final; // is aggregate deprecated?
121 bool isNested() const;
122 bool isExport() const override final;
124 Visibility visible() override final;
126 // 'this' type
127 Type *handleType() { return type; }
129 bool hasInvariant();
131 // Back end
132 void *sinit;
134 AggregateDeclaration *isAggregateDeclaration() override final { return this; }
135 void accept(Visitor *v) override { v->visit(this); }
138 struct StructFlags
140 enum Type
142 none = 0x0,
143 hasPointers = 0x1 // NB: should use noPointers as in ClassFlags
147 class StructDeclaration : public AggregateDeclaration
149 public:
150 FuncDeclarations postblits; // Array of postblit functions
151 FuncDeclaration *postblit; // aggregate postblit
153 FuncDeclaration *xeq; // TypeInfo_Struct.xopEquals
154 FuncDeclaration *xcmp; // TypeInfo_Struct.xopCmp
155 FuncDeclaration *xhash; // TypeInfo_Struct.xtoHash
156 static FuncDeclaration *xerreq; // object.xopEquals
157 static FuncDeclaration *xerrcmp; // object.xopCmp
159 // ABI-specific type(s) if the struct can be passed in registers
160 TypeTuple *argTypes;
162 structalign_t alignment; // alignment applied outside of the struct
163 ThreeState ispod; // if struct is POD
164 private:
165 uint16_t bitFields;
166 public:
167 static StructDeclaration *create(const Loc &loc, Identifier *id, bool inObject);
168 StructDeclaration *syntaxCopy(Dsymbol *s) override;
169 const char *kind() const override;
170 void finalizeSize() override final;
171 bool isPOD();
172 bool zeroInit() const; // !=0 if initialize with 0 fill
173 bool zeroInit(bool v);
174 bool hasIdentityAssign() const; // true if has identity opAssign
175 bool hasIdentityAssign(bool v);
176 bool hasBlitAssign() const; // true if opAssign is a blit
177 bool hasBlitAssign(bool v);
178 bool hasIdentityEquals() const; // true if has identity opEquals
179 bool hasIdentityEquals(bool v);
180 bool hasNoFields() const; // has no fields
181 bool hasNoFields(bool v);
182 bool hasCopyCtor() const; // copy constructor
183 bool hasCopyCtor(bool v);
184 // Even if struct is defined as non-root symbol, some built-in operations
185 // (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo.
186 // For those, today TypeInfo_Struct is generated in COMDAT.
187 bool requestTypeInfo() const;
188 bool requestTypeInfo(bool v);
190 StructDeclaration *isStructDeclaration() override final { return this; }
191 void accept(Visitor *v) override { v->visit(this); }
193 unsigned numArgTypes() const;
194 Type *argType(unsigned index);
195 bool hasRegularCtor(bool checkDisabled = false);
198 class UnionDeclaration final : public StructDeclaration
200 public:
201 UnionDeclaration *syntaxCopy(Dsymbol *s) override;
202 const char *kind() const override;
204 UnionDeclaration *isUnionDeclaration() override { return this; }
205 void accept(Visitor *v) override { v->visit(this); }
208 struct BaseClass
210 Type *type; // (before semantic processing)
212 ClassDeclaration *sym;
213 unsigned offset; // 'this' pointer offset
214 // for interfaces: Array of FuncDeclaration's
215 // making up the vtbl[]
216 FuncDeclarations vtbl;
218 DArray<BaseClass> baseInterfaces; // if BaseClass is an interface, these
219 // are a copy of the InterfaceDeclaration::interfaces
221 bool fillVtbl(ClassDeclaration *cd, FuncDeclarations *vtbl, int newinstance);
224 struct ClassFlags
226 enum Type
228 none = 0x0,
229 isCOMclass = 0x1,
230 noPointers = 0x2,
231 hasOffTi = 0x4,
232 hasCtor = 0x8,
233 hasGetMembers = 0x10,
234 hasTypeInfo = 0x20,
235 isAbstract = 0x40,
236 isCPPclass = 0x80,
237 hasDtor = 0x100
241 class ClassDeclaration : public AggregateDeclaration
243 public:
244 static ClassDeclaration *object;
245 static ClassDeclaration *throwable;
246 static ClassDeclaration *exception;
247 static ClassDeclaration *errorException;
248 static ClassDeclaration *cpp_type_info_ptr;
250 ClassDeclaration *baseClass; // NULL only if this is Object
251 FuncDeclaration *staticCtor;
252 FuncDeclaration *staticDtor;
253 Dsymbols vtbl; // Array of FuncDeclaration's making up the vtbl[]
254 Dsymbols vtblFinal; // More FuncDeclaration's that aren't in vtbl[]
256 BaseClasses *baseclasses; // Array of BaseClass's; first is super,
257 // rest are Interface's
259 DArray<BaseClass*> interfaces; // interfaces[interfaces_dim] for this class
260 // (does not include baseClass)
262 BaseClasses *vtblInterfaces; // array of base interfaces that have
263 // their own vtbl[]
265 TypeInfoClassDeclaration *vclassinfo; // the ClassInfo object for this ClassDeclaration
266 d_bool com; // true if this is a COM class (meaning it derives from IUnknown)
267 d_bool stack; // true if this is a scope class
268 int cppDtorVtblIndex; // slot reserved for the virtual destructor [extern(C++)]
269 d_bool inuse; // to prevent recursive attempts
271 ThreeState isabstract; // if abstract class
272 Baseok baseok; // set the progress of base classes resolving
273 ObjcClassDeclaration objc; // Data for a class declaration that is needed for the Objective-C integration
274 Symbol *cpp_type_info_ptr_sym; // cached instance of class Id.cpp_type_info_ptr
276 static ClassDeclaration *create(const Loc &loc, Identifier *id, BaseClasses *baseclasses, Dsymbols *members, bool inObject);
277 const char *toPrettyChars(bool QualifyTypes = false) override;
278 ClassDeclaration *syntaxCopy(Dsymbol *s) override;
279 Scope *newScope(Scope *sc) override;
281 #define OFFSET_RUNTIME 0x76543210
282 #define OFFSET_FWDREF 0x76543211
283 virtual bool isBaseOf(ClassDeclaration *cd, int *poffset);
285 bool isBaseInfoComplete();
286 void finalizeSize() override;
287 bool hasMonitor();
288 bool isFuncHidden(FuncDeclaration *fd);
289 bool isCOMclass() const;
290 virtual bool isCOMinterface() const;
291 bool isCPPclass() const;
292 virtual bool isCPPinterface() const;
293 bool isAbstract();
294 virtual int vtblOffset() const;
295 const char *kind() const override;
297 void addObjcSymbols(ClassDeclarations *classes, ClassDeclarations *categories) override final;
299 // Back end
300 Dsymbol *vtblsym;
301 Dsymbol *vtblSymbol();
303 ClassDeclaration *isClassDeclaration() override final { return (ClassDeclaration *)this; }
304 void accept(Visitor *v) override { v->visit(this); }
307 class InterfaceDeclaration final : public ClassDeclaration
309 public:
310 InterfaceDeclaration *syntaxCopy(Dsymbol *s) override;
311 Scope *newScope(Scope *sc) override;
312 bool isBaseOf(ClassDeclaration *cd, int *poffset) override;
313 const char *kind() const override;
314 int vtblOffset() const override;
315 bool isCPPinterface() const override;
316 bool isCOMinterface() const override;
318 InterfaceDeclaration *isInterfaceDeclaration() override { return this; }
319 void accept(Visitor *v) override { v->visit(this); }