0
|
1 module dmd.Optimize;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Expression;
|
|
5 import dmd.TOK;
|
|
6 import dmd.VarExp;
|
|
7 import dmd.VarDeclaration;
|
|
8 import dmd.STC;
|
|
9 import dmd.AssignExp;
|
|
10 import dmd.Type;
|
|
11 import dmd.WANT;
|
|
12 import dmd.TY;
|
|
13
|
|
14 /*************************************
|
|
15 * If variable has a const initializer,
|
|
16 * return that initializer.
|
|
17 */
|
|
18
|
|
19 Expression expandVar(int result, VarDeclaration v)
|
|
20 {
|
|
21 //printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null");
|
|
22
|
|
23 Expression e = null;
|
|
24 if (!v)
|
136
|
25 return e;
|
0
|
26
|
135
|
27 if (v.isConst() || v.isImmutable() || v.storage_class & STC.STCmanifest)
|
0
|
28 {
|
|
29 if (!v.type)
|
|
30 {
|
|
31 //error("ICE");
|
|
32 return e;
|
|
33 }
|
|
34
|
|
35 Type tb = v.type.toBasetype();
|
|
36 if (result & WANT.WANTinterpret ||
|
|
37 v.storage_class & STC.STCmanifest ||
|
|
38 (tb.ty != TY.Tsarray && tb.ty != TY.Tstruct)
|
|
39 )
|
|
40 {
|
|
41 if (v.init)
|
|
42 {
|
|
43 if (v.inuse)
|
|
44 { if (v.storage_class & STC.STCmanifest)
|
|
45 v.error("recursive initialization of constant");
|
|
46 goto L1;
|
|
47 }
|
|
48 Expression ei = v.init.toExpression();
|
|
49 if (!ei)
|
|
50 goto L1;
|
|
51 if (ei.op == TOK.TOKconstruct || ei.op == TOK.TOKblit)
|
|
52 { AssignExp ae = cast(AssignExp)ei;
|
|
53 ei = ae.e2;
|
|
54 if (ei.isConst() != 1 && ei.op != TOK.TOKstring)
|
|
55 goto L1;
|
|
56 if (ei.type != v.type)
|
|
57 goto L1;
|
|
58 }
|
|
59 if (v.scope_)
|
|
60 {
|
|
61 v.inuse++;
|
|
62 e = ei.syntaxCopy();
|
|
63 e = e.semantic(v.scope_);
|
|
64 e = e.implicitCastTo(v.scope_, v.type);
|
|
65 // enabling this line causes test22 in test suite to fail
|
|
66 //ei.type = e.type;
|
|
67 v.scope_ = null;
|
|
68 v.inuse--;
|
|
69 }
|
|
70 else if (!ei.type)
|
|
71 {
|
|
72 goto L1;
|
|
73 }
|
|
74 else
|
|
75 // Should remove the copy() operation by
|
|
76 // making all mods to expressions copy-on-write
|
|
77 e = ei.copy();
|
|
78 }
|
|
79 else
|
|
80 {
|
|
81 static if (true) {
|
|
82 goto L1;
|
|
83 } else {
|
|
84 // BUG: what if const is initialized in constructor?
|
|
85 e = v.type.defaultInit();
|
|
86 e.loc = e1.loc;
|
|
87 }
|
|
88 }
|
|
89 if (e.type != v.type)
|
|
90 {
|
|
91 e = e.castTo(null, v.type);
|
|
92 }
|
|
93 v.inuse++;
|
|
94 e = e.optimize(result);
|
|
95 v.inuse--;
|
|
96 }
|
|
97 }
|
|
98 L1:
|
|
99 //if (e) printf("\te = %s, e.type = %s\n", e.toChars(), e.type.toChars());
|
|
100 return e;
|
|
101 } |