Debian Codegraph @

gcc/d/dmd/aggregate.h

raw
1
2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2025 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 */
10
11 #pragma once
12
13 #include "dsymbol.h"
14 #include "objc.h"
15
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;
27
28 enum class Sizeok : uint8_t
29 {
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
34 };
35
36 enum class Baseok : uint8_t
37 {
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
42 };
43
44 namespace dmd
45 {
46 FuncDeclaration *search_toString(StructDeclaration *sd);
47 void semanticTypeInfoMembers(StructDeclaration *sd);
48 bool fill(StructDeclaration* sd, Loc loc, Expressions &elements, bool ctorinit);
49 bool isFuncHidden(ClassDeclaration* cd, FuncDeclaration* fd);
50 Dsymbol* vtblSymbol(ClassDeclaration *cd);
51 }
52
53 enum class ClassKind : uint8_t
54 {
55 /// the aggregate is a d(efault) struct/class/interface
56 d,
57 /// the aggregate is a C++ struct/class/interface
58 cpp,
59 /// the aggregate is an Objective-C class/interface
60 objc,
61 /// the aggregate is a C struct
62 c,
63 };
64
65 struct MangleOverride
66 {
67 Dsymbol *agg;
68 Identifier *id;
69 };
70
71 class AggregateDeclaration : public ScopeDsymbol
72 {
73 public:
74 Type *type;
75 StorageClass storage_class;
76 unsigned structsize; // size of struct
77 unsigned alignsize; // size of struct for alignment purposes
78 VarDeclarations fields; // VarDeclaration fields
79 Dsymbol *deferred; // any deferred semantic2() or semantic3() symbol
80
81 ClassKind classKind; // specifies the linkage type
82 CPPMANGLE cppmangle;
83
84 // overridden symbol with pragma(mangle, "...")
85 MangleOverride *pMangleOverride;
86 /* !=NULL if is nested
87 * pointing to the dsymbol that directly enclosing it.
88 * 1. The function that enclosing it (nested struct and class)
89 * 2. The class that enclosing it (nested class only)
90 * 3. If enclosing aggregate is template, its enclosing dsymbol.
91 * See AggregateDeclaraton::makeNested for the details.
92 */
93 Dsymbol *enclosing;
94 VarDeclaration *vthis; // 'this' parameter if this aggregate is nested
95 VarDeclaration *vthis2; // 'this' parameter if this aggregate is a template and is nested
96 // Special member functions
97 FuncDeclarations invs; // Array of invariants
98 FuncDeclaration *inv; // invariant
99
100 Dsymbol *ctor; // CtorDeclaration or TemplateDeclaration
101
102 // default constructor - should have no arguments, because
103 // it would be stored in TypeInfo_Class.defaultConstructor
104 CtorDeclaration *defaultCtor;
105
106 AliasThis *aliasthis; // forward unresolved lookups to aliasthis
107
108 DtorDeclarations userDtors; // user-defined destructors (`~this()`) - mixins can yield multiple ones
109 DtorDeclaration *aggrDtor; // aggregate destructor calling userDtors and fieldDtor (and base class aggregate dtor for C++ classes)
110 DtorDeclaration *dtor; // the aggregate destructor exposed as `__xdtor` alias
111 // (same as aggrDtor, except for C++ classes with virtual dtor on Windows)
112 DtorDeclaration *tidtor; // aggregate destructor used in TypeInfo (must have extern(D) ABI)
113 DtorDeclaration *fieldDtor; // function destructing (non-inherited) fields
114
115 Expression *getRTInfo; // pointer to GC info generated by object.RTInfo(this)
116
117 Visibility visibility;
118 d_bool noDefaultCtor; // no default construction
119 d_bool disableNew; // disallow allocations using `new`
120 Sizeok sizeok; // set when structsize contains valid data
121
122 virtual Scope *newScope(Scope *sc);
123 virtual void finalizeSize() = 0;
124 uinteger_t size(Loc loc) override final;
125 Type *getType() override final;
126 bool isDeprecated() const override final; // is aggregate deprecated?
127 bool isNested() const;
128 bool isExport() const override final;
129
130 Visibility visible() override final;
131
132 // 'this' type
133 Type *handleType() { return type; }
134
135 bool hasInvariant();
136
137 // Back end
138 void *sinit;
139
140 void accept(Visitor *v) override { v->visit(this); }
141 };
142
143 struct StructFlags
144 {
145 enum Type
146 {
147 none = 0x0,
148 hasPointers = 0x1 // NB: should use noPointers as in ClassFlags
149 };
150 };
151
152 class StructDeclaration : public AggregateDeclaration
153 {
154 public:
155 FuncDeclarations postblits; // Array of postblit functions
156 FuncDeclaration *postblit; // aggregate postblit
157
158 FuncDeclaration *xeq; // TypeInfo_Struct.xopEquals
159 FuncDeclaration *xcmp; // TypeInfo_Struct.xopCmp
160 FuncDeclaration *xhash; // TypeInfo_Struct.xtoHash
161 static FuncDeclaration *xerreq; // object.xopEquals
162 static FuncDeclaration *xerrcmp; // object.xopCmp
163
164 // ABI-specific type(s) if the struct can be passed in registers
165 TypeTuple *argTypes;
166
167 structalign_t alignment; // alignment applied outside of the struct
168 ThreeState ispod; // if struct is POD
169 private:
170 uint16_t bitFields;
171 public:
172 static StructDeclaration *create(Loc loc, Identifier *id, bool inObject);
173 StructDeclaration *syntaxCopy(Dsymbol *s) override;
174 const char *kind() const override;
175 void finalizeSize() override final;
176 bool isPOD();
177 bool zeroInit() const; // !=0 if initialize with 0 fill
178 bool zeroInit(bool v);
179 bool hasIdentityAssign() const; // true if has identity opAssign
180 bool hasIdentityAssign(bool v);
181 bool hasBlitAssign() const; // true if opAssign is a blit
182 bool hasBlitAssign(bool v);
183 bool hasIdentityEquals() const; // true if has identity opEquals
184 bool hasIdentityEquals(bool v);
185 bool hasNoFields() const; // has no fields
186 bool hasNoFields(bool v);
187 bool hasCopyCtor() const; // copy constructor
188 bool hasCopyCtor(bool v);
189 // Even if struct is defined as non-root symbol, some built-in operations
190 // (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo.
191 // For those, today TypeInfo_Struct is generated in COMDAT.
192 bool requestTypeInfo() const;
193 bool requestTypeInfo(bool v);
194
195 void accept(Visitor *v) override { v->visit(this); }
196
197 unsigned numArgTypes() const;
198 Type *argType(unsigned index);
199 bool hasRegularCtor(bool checkDisabled = false);
200 };
201
202 class UnionDeclaration final : public StructDeclaration
203 {
204 public:
205 UnionDeclaration *syntaxCopy(Dsymbol *s) override;
206 const char *kind() const override;
207
208 void accept(Visitor *v) override { v->visit(this); }
209 };
210
211 struct BaseClass
212 {
213 Type *type; // (before semantic processing)
214
215 ClassDeclaration *sym;
216 unsigned offset; // 'this' pointer offset
217 // for interfaces: Array of FuncDeclaration's
218 // making up the vtbl[]
219 FuncDeclarations vtbl;
220
221 DArray<BaseClass> baseInterfaces; // if BaseClass is an interface, these
222 // are a copy of the InterfaceDeclaration::interfaces
223
224 bool fillVtbl(ClassDeclaration *cd, FuncDeclarations *vtbl, int newinstance);
225 };
226
227 struct ClassFlags
228 {
229 enum Type
230 {
231 none = 0x0,
232 isCOMclass = 0x1,
233 noPointers = 0x2,
234 hasOffTi = 0x4,
235 hasCtor = 0x8,
236 hasGetMembers = 0x10,
237 hasTypeInfo = 0x20,
238 isAbstract = 0x40,
239 isCPPclass = 0x80,
240 hasDtor = 0x100,
241 hasNameSig = 0x200,
242 };
243 };
244
245 class ClassDeclaration : public AggregateDeclaration
246 {
247 public:
248 static ClassDeclaration *object;
249 static ClassDeclaration *throwable;
250 static ClassDeclaration *exception;
251 static ClassDeclaration *errorException;
252 static ClassDeclaration *cpp_type_info_ptr;
253
254 ClassDeclaration *baseClass; // NULL only if this is Object
255 FuncDeclaration *staticCtor;
256 FuncDeclaration *staticDtor;
257 Dsymbols vtbl; // Array of FuncDeclaration's making up the vtbl[]
258 Dsymbols vtblFinal; // More FuncDeclaration's that aren't in vtbl[]
259
260 BaseClasses *baseclasses; // Array of BaseClass's; first is super,
261 // rest are Interface's
262
263 DArray<BaseClass*> interfaces; // interfaces[interfaces_dim] for this class
264 // (does not include baseClass)
265
266 BaseClasses *vtblInterfaces; // array of base interfaces that have
267 // their own vtbl[]
268
269 TypeInfoClassDeclaration *vclassinfo; // the ClassInfo object for this ClassDeclaration
270 d_bool com; // true if this is a COM class (meaning it derives from IUnknown)
271 d_bool stack; // true if this is a scope class
272 int cppDtorVtblIndex; // slot reserved for the virtual destructor [extern(C++)]
273 d_bool inuse; // to prevent recursive attempts
274
275 ThreeState isabstract; // if abstract class
276 Baseok baseok; // set the progress of base classes resolving
277 ObjcClassDeclaration objc; // Data for a class declaration that is needed for the Objective-C integration
278 Symbol *cpp_type_info_ptr_sym; // cached instance of class Id.cpp_type_info_ptr
279
280 static ClassDeclaration *create(Loc loc, Identifier *id, BaseClasses *baseclasses, Dsymbols *members, bool inObject);
281 const char *toPrettyChars(bool QualifyTypes = false) override;
282 ClassDeclaration *syntaxCopy(Dsymbol *s) override;
283 Scope *newScope(Scope *sc) override;
284
285 #define OFFSET_RUNTIME 0x76543210
286 #define OFFSET_FWDREF 0x76543211
287 virtual bool isBaseOf(ClassDeclaration *cd, int *poffset);
288
289 bool isBaseInfoComplete();
290 void finalizeSize() override;
291 bool hasMonitor();
292 bool isCOMclass() const;
293 virtual bool isCOMinterface() const;
294 bool isCPPclass() const;
295 virtual bool isCPPinterface() const;
296 bool isAbstract();
297 virtual int vtblOffset() const;
298 const char *kind() const override;
299
300 void addObjcSymbols(ClassDeclarations *classes, ClassDeclarations *categories) override final;
301
302 // Back end
303 Dsymbol *vtblsym;
304
305 void accept(Visitor *v) override { v->visit(this); }
306 };
307
308 class InterfaceDeclaration final : public ClassDeclaration
309 {
310 public:
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;
318
319 void accept(Visitor *v) override { v->visit(this); }
320 };