Mercurial > projects > ddmd
comparison dmd/CommaExp.d @ 179:cd48cb899aee
Updated to dmd2.040
author | korDen |
---|---|
date | Sun, 17 Oct 2010 20:56:07 +0400 |
parents | e3afd1303184 |
children | b0d41ff5e0df |
comparison
equal
deleted
inserted
replaced
178:e3afd1303184 | 179:cd48cb899aee |
---|---|
4 import dmd.Loc; | 4 import dmd.Loc; |
5 import dmd.BinExp; | 5 import dmd.BinExp; |
6 import dmd.IRState; | 6 import dmd.IRState; |
7 import dmd.Scope; | 7 import dmd.Scope; |
8 import dmd.IntRange; | 8 import dmd.IntRange; |
9 import dmd.DeclarationExp; | |
10 import dmd.VarExp; | |
11 import dmd.VarDeclaration; | |
9 import dmd.Expression; | 12 import dmd.Expression; |
10 import dmd.GlobalExpressions; | 13 import dmd.GlobalExpressions; |
11 import dmd.MATCH; | 14 import dmd.MATCH; |
12 import dmd.WANT; | 15 import dmd.WANT; |
13 import dmd.TOK; | 16 import dmd.TOK; |
111 override Expression optimize(int result) | 114 override Expression optimize(int result) |
112 { | 115 { |
113 Expression e; | 116 Expression e; |
114 | 117 |
115 //printf("CommaExp.optimize(result = %d) %s\n", result, toChars()); | 118 //printf("CommaExp.optimize(result = %d) %s\n", result, toChars()); |
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 | |
116 e1 = e1.optimize(result & WANTinterpret); | 136 e1 = e1.optimize(result & WANTinterpret); |
117 e2 = e2.optimize(result); | 137 e2 = e2.optimize(result); |
118 if (!e1 || e1.op == TOKint64 || e1.op == TOKfloat64 || !e1.checkSideEffect(2)) | 138 if (!e1 || e1.op == TOKint64 || e1.op == TOKfloat64 || !e1.checkSideEffect(2)) |
119 { | 139 { |
120 e = e2; | 140 e = e2; |
130 override Expression interpret(InterState istate) | 150 override Expression interpret(InterState istate) |
131 { | 151 { |
132 version (LOG) { | 152 version (LOG) { |
133 printf("CommaExp.interpret() %.*s\n", toChars()); | 153 printf("CommaExp.interpret() %.*s\n", toChars()); |
134 } | 154 } |
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 | |
135 Expression e = e1.interpret(istate); | 170 Expression e = e1.interpret(istate); |
136 if (e !is EXP_CANT_INTERPRET) | 171 if (e !is EXP_CANT_INTERPRET) |
137 e = e2.interpret(istate); | 172 e = e2.interpret(istate); |
138 return e; | 173 return e; |
139 } | 174 } |