annotate dmd/Optimize.d @ 137:09c858522d55

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