annotate dmd/CommaExp.d @ 179:cd48cb899aee

Updated to dmd2.040
author korDen
date Sun, 17 Oct 2010 20:56:07 +0400
parents e3afd1303184
children b0d41ff5e0df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.CommaExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 72
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.BinExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.IntRange;
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
9 import dmd.DeclarationExp;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
10 import dmd.VarExp;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
11 import dmd.VarDeclaration;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Expression;
163
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
13 import dmd.GlobalExpressions;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.MATCH;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 class CommaExp : BinExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 this(Loc loc, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 163
diff changeset
27 register();
e3afd1303184 Many small bugs fixed
korDen
parents: 163
diff changeset
28
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 super(loc, TOK.TOKcomma, CommaExp.sizeof, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
32 override Expression semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 BinExp.semanticp(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 type = e2.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
42 override void checkEscape()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 e2.checkEscape();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
47 override void checkEscapeRef()
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
48 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
49 e2.checkEscapeRef();
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
50 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
51
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
52 override IntRange getIntRange()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 version (DMDV2) {
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
58 override int isLvalue()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 return e2.isLvalue();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
63 override Expression toLvalue(Scope sc, Expression e)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 e2 = e2.toLvalue(sc, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
69 override Expression modifiableLvalue(Scope sc, Expression e)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 e2 = e2.modifiableLvalue(sc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
75 override bool isBool(bool result)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 return e2.isBool(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
80 override bool checkSideEffect(int flag)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 if (flag == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 return e1.checkSideEffect(2) || e2.checkSideEffect(2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 // Don't check e1 until we cast(void) the a,b code generation
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 return e2.checkSideEffect(flag);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
91 override MATCH implicitConvTo(Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 {
56
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
93 return e2.implicitConvTo(t);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
96 override Expression castTo(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 {
56
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
98 Expression e2c = e2.castTo(sc, t);
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
99 Expression e;
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
100
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
101 if (e2c != e2)
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
102 {
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
103 e = new CommaExp(loc, e1, e2c);
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
104 e.type = e2c.type;
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
105 }
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
106 else
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
107 {
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
108 e = this;
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
109 e.type = e2.type;
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
110 }
51605de93870 TupleExp.optimize
korDen
parents: 0
diff changeset
111 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
114 override Expression optimize(int result)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 //printf("CommaExp.optimize(result = %d) %s\n", result, toChars());
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
119 // Comma needs special treatment, because it may
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
120 // contain compiler-generated declarations. We can interpret them, but
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
121 // otherwise we must NOT attempt to constant-fold them.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
122 // In particular, if the comma returns a temporary variable, it needs
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
123 // to be an lvalue (this is particularly important for struct constructors)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
124
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
125 if (result & WANTinterpret)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
126 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
127 // Interpreting comma needs special treatment, because it may
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
128 // contain compiler-generated declarations.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
129 e = interpret(null);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
130 return (e is EXP_CANT_INTERPRET) ? this : e;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
131 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
132 // Don't constant fold if it is a compiler-generated temporary.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
133 if (e1.op == TOKdeclaration)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
134 return this;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
135
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 e1 = e1.optimize(result & WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 e2 = e2.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 if (!e1 || e1.op == TOKint64 || e1.op == TOKfloat64 || !e1.checkSideEffect(2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 e = e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 //printf("-CommaExp.optimize(result = %d) %s\n", result, e.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
150 override Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 {
163
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
152 version (LOG) {
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
153 printf("CommaExp.interpret() %.*s\n", toChars());
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
154 }
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
155 // If the comma returns a temporary variable, it needs to be an lvalue
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
156 // (this is particularly important for struct constructors)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
157 if (e1.op == TOKdeclaration && e2.op == TOKvar
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
158 && (cast(DeclarationExp)e1).declaration == (cast(VarExp)e2).var)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
159 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
160 VarExp ve = cast(VarExp)e2;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
161 VarDeclaration v = ve.var.isVarDeclaration();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
162 if (!v.init && !v.value)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
163 v.value = v.type.defaultInitLiteral(Loc(0));
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
164 if (!v.value)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
165 v.value = v.init.toExpression();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
166 v.value = v.value.interpret(istate);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
167 return e2;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
168 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
169
163
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
170 Expression e = e1.interpret(istate);
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
171 if (e !is EXP_CANT_INTERPRET)
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
172 e = e2.interpret(istate);
fe932c1a9563 *.interpret functions implemenation
korDen
parents: 135
diff changeset
173 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
176 override elem* toElem(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 assert(e1 && e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 elem* eleft = e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 elem* eright = e2.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 elem* e = el_combine(eleft, eright);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 el_setLoc(e, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
186 }