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