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