/
init.d
336 lines (297 loc) · 8.5 KB
/
init.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/init.d, _init.d)
* Documentation: https://dlang.org/phobos/dmd_init.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/init.d
*/
module dmd.init;
import core.stdc.stdio;
import core.checkedint;
import dmd.arraytypes;
import dmd.ast_node;
import dmd.dsymbol;
import dmd.expression;
import dmd.globals;
import dmd.hdrgen;
import dmd.identifier;
import dmd.mtype;
import dmd.root.outbuffer;
import dmd.root.rootobject;
import dmd.tokens;
import dmd.visitor;
enum NeedInterpret : int
{
INITnointerpret,
INITinterpret,
}
alias INITnointerpret = NeedInterpret.INITnointerpret;
alias INITinterpret = NeedInterpret.INITinterpret;
/*************
* Discriminant for which kind of initializer
*/
enum InitKind : ubyte
{
void_,
error,
struct_,
array,
exp,
}
/***********************************************************
*/
extern (C++) class Initializer : ASTNode
{
Loc loc;
InitKind kind;
extern (D) this(const ref Loc loc, InitKind kind)
{
this.loc = loc;
this.kind = kind;
}
override final const(char)* toChars() const
{
OutBuffer buf;
HdrGenState hgs;
.toCBuffer(this, &buf, &hgs);
return buf.extractChars();
}
final inout(ErrorInitializer) isErrorInitializer() inout pure
{
// Use void* cast to skip dynamic casting call
return kind == InitKind.error ? cast(inout ErrorInitializer)cast(void*)this : null;
}
final inout(VoidInitializer) isVoidInitializer() inout pure
{
return kind == InitKind.void_ ? cast(inout VoidInitializer)cast(void*)this : null;
}
final inout(StructInitializer) isStructInitializer() inout pure
{
return kind == InitKind.struct_ ? cast(inout StructInitializer)cast(void*)this : null;
}
final inout(ArrayInitializer) isArrayInitializer() inout pure
{
return kind == InitKind.array ? cast(inout ArrayInitializer)cast(void*)this : null;
}
final inout(ExpInitializer) isExpInitializer() inout pure
{
return kind == InitKind.exp ? cast(inout ExpInitializer)cast(void*)this : null;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class VoidInitializer : Initializer
{
Type type; // type that this will initialize to
extern (D) this(const ref Loc loc)
{
super(loc, InitKind.void_);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class ErrorInitializer : Initializer
{
extern (D) this()
{
super(Loc.initial, InitKind.error);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class StructInitializer : Initializer
{
Identifiers field; // of Identifier *'s
Initializers value; // parallel array of Initializer *'s
extern (D) this(const ref Loc loc)
{
super(loc, InitKind.struct_);
}
extern (D) void addInit(Identifier field, Initializer value)
{
//printf("StructInitializer::addInit(field = %p, value = %p)\n", field, value);
this.field.push(field);
this.value.push(value);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class ArrayInitializer : Initializer
{
Expressions index; // indices
Initializers value; // of Initializer *'s
uint dim; // length of array being initialized
Type type; // type that array will be used to initialize
bool sem; // true if semantic() is run
extern (D) this(const ref Loc loc)
{
super(loc, InitKind.array);
}
extern (D) void addInit(Expression index, Initializer value)
{
this.index.push(index);
this.value.push(value);
dim = 0;
type = null;
}
bool isAssociativeArray() const pure
{
foreach (idx; index)
{
if (idx)
return true;
}
return false;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class ExpInitializer : Initializer
{
bool expandTuples;
Expression exp;
extern (D) this(const ref Loc loc, Expression exp)
{
super(loc, InitKind.exp);
this.exp = exp;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
version (all)
{
extern (C++) bool hasNonConstPointers(Expression e)
{
static bool checkArray(Expressions* elems)
{
foreach (e; *elems)
{
if (e && hasNonConstPointers(e))
return true;
}
return false;
}
if (e.type.ty == Terror)
return false;
if (e.op == TOK.null_)
return false;
if (auto se = e.isStructLiteralExp())
{
return checkArray(se.elements);
}
if (auto ae = e.isArrayLiteralExp())
{
if (!ae.type.nextOf().hasPointers())
return false;
return checkArray(ae.elements);
}
if (auto ae = e.isAssocArrayLiteralExp())
{
if (ae.type.nextOf().hasPointers() && checkArray(ae.values))
return true;
if ((cast(TypeAArray)ae.type).index.hasPointers())
return checkArray(ae.keys);
return false;
}
if (auto ae = e.isAddrExp())
{
if (auto se = ae.e1.isStructLiteralExp())
{
if (!(se.stageflags & stageSearchPointers))
{
const old = se.stageflags;
se.stageflags |= stageSearchPointers;
bool ret = checkArray(se.elements);
se.stageflags = old;
return ret;
}
else
{
return false;
}
}
return true;
}
if (e.type.ty == Tpointer && e.type.nextOf().ty != Tfunction)
{
if (e.op == TOK.symbolOffset) // address of a global is OK
return false;
if (e.op == TOK.int64) // cast(void *)int is OK
return false;
if (e.op == TOK.string_) // "abc".ptr is OK
return false;
return true;
}
return false;
}
}
/****************************************
* Copy the AST for Initializer.
* Params:
* inx = Initializer AST to copy
* Returns:
* the copy
*/
Initializer syntaxCopy(Initializer inx)
{
static Initializer copyStruct(StructInitializer vi)
{
auto si = new StructInitializer(vi.loc);
assert(vi.field.dim == vi.value.dim);
si.field.setDim(vi.field.dim);
si.value.setDim(vi.value.dim);
foreach (const i; 0 .. vi.field.dim)
{
si.field[i] = vi.field[i];
si.value[i] = vi.value[i].syntaxCopy();
}
return si;
}
static Initializer copyArray(ArrayInitializer vi)
{
auto ai = new ArrayInitializer(vi.loc);
assert(vi.index.dim == vi.value.dim);
ai.index.setDim(vi.index.dim);
ai.value.setDim(vi.value.dim);
foreach (const i; 0 .. vi.value.dim)
{
ai.index[i] = vi.index[i] ? vi.index[i].syntaxCopy() : null;
ai.value[i] = vi.value[i].syntaxCopy();
}
return ai;
}
final switch (inx.kind)
{
case InitKind.void_: return new VoidInitializer(inx.loc);
case InitKind.error: return inx;
case InitKind.struct_: return copyStruct(cast(StructInitializer)inx);
case InitKind.array: return copyArray(cast(ArrayInitializer)inx);
case InitKind.exp: return new ExpInitializer(inx.loc, (cast(ExpInitializer)inx).exp.syntaxCopy());
}
}