0
|
1 module dmd.ReturnStatement;
|
|
2
|
|
3 import dmd.Loc;
|
|
4 import dmd.Statement;
|
|
5 import dmd.GotoStatement;
|
|
6 import dmd.STC;
|
|
7 import dmd.CompoundStatement;
|
|
8 import dmd.Id;
|
|
9 import dmd.AssignExp;
|
|
10 import dmd.ExpStatement;
|
|
11 import dmd.FuncDeclaration;
|
|
12 import dmd.IntegerExp;
|
|
13 import dmd.ThisExp;
|
|
14 import dmd.StructDeclaration;
|
|
15 import dmd.TypeFunction;
|
|
16 import dmd.CSX;
|
|
17 import dmd.RET;
|
|
18 import dmd.TOK;
|
|
19 import dmd.Type;
|
|
20 import dmd.Expression;
|
|
21 import dmd.StructLiteralExp;
|
|
22 import dmd.TypeStruct;
|
|
23 import dmd.Scope;
|
|
24 import dmd.OutBuffer;
|
|
25 import dmd.HdrGenState;
|
|
26 import dmd.InterState;
|
|
27 import dmd.InlineCostState;
|
|
28 import dmd.InlineDoState;
|
|
29 import dmd.InlineScanState;
|
|
30 import dmd.IRState;
|
|
31 import dmd.TY;
|
|
32 import dmd.WANT;
|
|
33 import dmd.VarExp;
|
|
34 import dmd.VarDeclaration;
|
63
|
35 import dmd.GlobalExpressions;
|
0
|
36 import dmd.BE;
|
|
37 import dmd.codegen.Util;
|
|
38
|
|
39 import dmd.backend.Blockx;
|
|
40 import dmd.backend.elem;
|
|
41 import dmd.backend.TYM;
|
|
42 import dmd.backend.Util;
|
|
43 import dmd.backend.OPER;
|
|
44 import dmd.backend.mTY;
|
|
45 import dmd.backend.BC;
|
|
46
|
|
47 class ReturnStatement : Statement
|
|
48 {
|
|
49 Expression exp;
|
|
50
|
|
51 this(Loc loc, Expression exp)
|
|
52 {
|
|
53 super(loc);
|
|
54 this.exp = exp;
|
|
55 }
|
|
56
|
|
57 Statement syntaxCopy()
|
|
58 {
|
|
59 Expression e = exp ? exp.syntaxCopy() : null;
|
|
60 return new ReturnStatement(loc, e);
|
|
61 }
|
|
62
|
|
63 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
64 {
|
|
65 assert(false);
|
|
66 }
|
|
67
|
|
68 Statement semantic(Scope sc)
|
|
69 {
|
|
70 //printf("ReturnStatement.semantic() %s\n", toChars());
|
|
71
|
|
72 FuncDeclaration fd = sc.parent.isFuncDeclaration();
|
|
73 Scope scx = sc;
|
|
74 int implicit0 = 0;
|
|
75
|
|
76 if (sc.fes)
|
|
77 {
|
|
78 // Find scope of function foreach is in
|
|
79 for (; 1; scx = scx.enclosing)
|
|
80 {
|
|
81 assert(scx);
|
|
82 if (scx.func !is fd)
|
|
83 {
|
|
84 fd = scx.func; // fd is now function enclosing foreach
|
|
85 break;
|
|
86 }
|
|
87 }
|
|
88 }
|
|
89
|
|
90 Type tret = fd.type.nextOf();
|
|
91 if (fd.tintro) {
|
|
92 /* We'll be implicitly casting the return expression to tintro
|
|
93 */
|
|
94 tret = fd.tintro.nextOf();
|
|
95 }
|
|
96
|
|
97 Type tbret = null;
|
|
98
|
|
99 if (tret) {
|
|
100 tbret = tret.toBasetype();
|
|
101 }
|
|
102
|
|
103 // main() returns 0, even if it returns void
|
|
104 if (!exp && (!tbret || tbret.ty == TY.Tvoid) && fd.isMain())
|
|
105 {
|
|
106 implicit0 = 1;
|
|
107 exp = new IntegerExp(0);
|
|
108 }
|
|
109
|
|
110 if (sc.incontract || scx.incontract)
|
|
111 error("return statements cannot be in contracts");
|
|
112
|
|
113 if (sc.tf || scx.tf)
|
|
114 error("return statements cannot be in finally, scope(exit) or scope(success) bodies");
|
|
115
|
|
116 if (fd.isCtorDeclaration())
|
|
117 {
|
|
118 // Constructors implicitly do:
|
|
119 // return this;
|
|
120 if (exp && exp.op != TOK.TOKthis) {
|
|
121 error("cannot return expression from constructor");
|
|
122 }
|
|
123
|
|
124 exp = new ThisExp(Loc(0));
|
|
125 }
|
|
126
|
|
127 if (!exp) {
|
|
128 fd.nrvo_can = 0;
|
|
129 }
|
|
130
|
|
131 if (exp)
|
|
132 {
|
|
133 fd.hasReturnExp |= 1;
|
|
134
|
|
135 exp = exp.semantic(sc);
|
|
136 exp = resolveProperties(sc, exp);
|
|
137 exp = exp.optimize(WANT.WANTvalue);
|
|
138
|
|
139 if (fd.nrvo_can && exp.op == TOK.TOKvar) {
|
|
140 VarExp ve = cast(VarExp)exp;
|
|
141 VarDeclaration v = ve.var.isVarDeclaration();
|
|
142
|
|
143 if ((cast(TypeFunction)fd.type).isref) {
|
|
144 // Function returns a reference
|
|
145 fd.nrvo_can = 0;
|
|
146 } else if (!v || v.isOut() || v.isRef()) {
|
|
147 fd.nrvo_can = 0;
|
|
148 } else if (tbret.ty == TY.Tstruct && (cast(TypeStruct)tbret).sym.dtor) {
|
|
149 // Struct being returned has destructors
|
|
150 fd.nrvo_can = 0;
|
|
151 } else if (fd.nrvo_var is null) {
|
|
152 if (!v.isDataseg() && !v.isParameter() && v.toParent2() == fd) {
|
|
153 //printf("Setting nrvo to %s\n", v.toChars());
|
|
154 fd.nrvo_var = v;
|
|
155 } else {
|
|
156 fd.nrvo_can = 0;
|
|
157 }
|
|
158 } else if (fd.nrvo_var != v) {
|
|
159 fd.nrvo_can = 0;
|
|
160 }
|
|
161 } else {
|
|
162 fd.nrvo_can = 0;
|
|
163 }
|
|
164
|
|
165 if (fd.returnLabel && tbret.ty != TY.Tvoid) {
|
|
166 ;
|
|
167 } else if (fd.inferRetType) {
|
|
168 if (fd.type.nextOf()) {
|
|
169 if (!exp.type.equals(fd.type.nextOf()))
|
|
170 error("mismatched function return type inference of %s and %s", exp.type.toChars(), fd.type.nextOf().toChars());
|
|
171 }
|
|
172 else
|
|
173 {
|
|
174 (cast(TypeFunction)fd.type).next = exp.type;
|
|
175 fd.type = fd.type.semantic(loc, sc);
|
|
176 if (!fd.tintro)
|
|
177 {
|
|
178 tret = fd.type.nextOf();
|
|
179 tbret = tret.toBasetype();
|
|
180 }
|
|
181 }
|
|
182 } else if (tbret.ty != TY.Tvoid)
|
|
183 {
|
|
184 exp = exp.implicitCastTo(sc, tret);
|
|
185 exp = exp.optimize(WANT.WANTvalue);
|
|
186 }
|
|
187 } else if (fd.inferRetType) {
|
|
188 if (fd.type.nextOf())
|
|
189 {
|
|
190 if (fd.type.nextOf().ty != TY.Tvoid) {
|
|
191 error("mismatched function return type inference of void and %s", fd.type.nextOf().toChars());
|
|
192 }
|
|
193 }
|
|
194 else
|
|
195 {
|
|
196 (cast(TypeFunction*)fd.type).next = Type.tvoid;
|
|
197 fd.type = fd.type.semantic(loc, sc);
|
|
198 if (!fd.tintro)
|
|
199 {
|
|
200 tret = Type.tvoid;
|
|
201 tbret = tret;
|
|
202 }
|
|
203 }
|
|
204 }
|
|
205 else if (tbret.ty != TY.Tvoid) {// if non-void return
|
|
206 error("return expression expected");
|
|
207 }
|
|
208
|
|
209 if (sc.fes)
|
|
210 {
|
|
211 Statement s;
|
|
212
|
|
213 if (exp && !implicit0)
|
|
214 {
|
|
215 exp = exp.implicitCastTo(sc, tret);
|
|
216 }
|
|
217 if (!exp || exp.op == TOK.TOKint64 || exp.op == TOK.TOKfloat64 ||
|
|
218 exp.op == TOK.TOKimaginary80 || exp.op == TOK.TOKcomplex80 ||
|
|
219 exp.op == TOK.TOKthis || exp.op == TOK.TOKsuper || exp.op == TOK.TOKnull ||
|
|
220 exp.op == TOK.TOKstring)
|
|
221 {
|
|
222 sc.fes.cases.push(cast(void*)this);
|
|
223 // Construct: return cases.dim+1;
|
|
224 s = new ReturnStatement(Loc(0), new IntegerExp(sc.fes.cases.dim + 1));
|
|
225 }
|
|
226 else if (fd.type.nextOf().toBasetype() == Type.tvoid)
|
|
227 {
|
|
228 s = new ReturnStatement(Loc(0), null);
|
|
229 sc.fes.cases.push(cast(void*)s);
|
|
230
|
|
231 // Construct: { exp; return cases.dim + 1; }
|
|
232 Statement s1 = new ExpStatement(loc, exp);
|
|
233 Statement s2 = new ReturnStatement(Loc(0), new IntegerExp(sc.fes.cases.dim + 1));
|
|
234 s = new CompoundStatement(loc, s1, s2);
|
|
235 }
|
|
236 else
|
|
237 {
|
|
238 // Construct: return vresult;
|
|
239 if (!fd.vresult)
|
|
240 {
|
|
241 // Declare vresult
|
|
242 VarDeclaration v = new VarDeclaration(loc, tret, Id.result, null);
|
|
243 v.noauto = true;
|
|
244 v.semantic(scx);
|
|
245 if (!scx.insert(v)) {
|
|
246 assert(0);
|
|
247 }
|
|
248 v.parent = fd;
|
|
249 fd.vresult = v;
|
|
250 }
|
|
251
|
|
252 s = new ReturnStatement(Loc(0), new VarExp(Loc(0), fd.vresult));
|
|
253 sc.fes.cases.push(cast(void*)s);
|
|
254
|
|
255 // Construct: { vresult = exp; return cases.dim + 1; }
|
|
256 exp = new AssignExp(loc, new VarExp(Loc(0), fd.vresult), exp);
|
|
257 exp.op = TOK.TOKconstruct;
|
|
258 exp = exp.semantic(sc);
|
|
259 Statement s1 = new ExpStatement(loc, exp);
|
|
260 Statement s2 = new ReturnStatement(Loc(0), new IntegerExp(sc.fes.cases.dim + 1));
|
|
261 s = new CompoundStatement(loc, s1, s2);
|
|
262 }
|
|
263 return s;
|
|
264 }
|
|
265
|
|
266 if (exp)
|
|
267 {
|
|
268 if (fd.returnLabel && tbret.ty != TY.Tvoid)
|
|
269 {
|
|
270 assert(fd.vresult);
|
|
271 VarExp v = new VarExp(Loc(0), fd.vresult);
|
|
272
|
|
273 exp = new AssignExp(loc, v, exp);
|
|
274 exp.op = TOK.TOKconstruct;
|
|
275 exp = exp.semantic(sc);
|
|
276 }
|
|
277
|
|
278 if ((cast(TypeFunction)fd.type).isref && !fd.isCtorDeclaration())
|
|
279 { // Function returns a reference
|
|
280 if (tbret.isMutable())
|
|
281 exp = exp.modifiableLvalue(sc, exp);
|
|
282 else
|
|
283 exp = exp.toLvalue(sc, exp);
|
|
284
|
|
285 if (exp.op == TOK.TOKvar)
|
|
286 {
|
|
287 VarExp ve = cast(VarExp)exp;
|
|
288 VarDeclaration v = ve.var.isVarDeclaration();
|
|
289 if (v && !v.isDataseg() && !(v.storage_class & (STC.STCref | STC.STCout))) {
|
|
290 error("escaping reference to local variable %s", v.toChars());
|
|
291 }
|
|
292 }
|
|
293 }
|
|
294
|
|
295 //exp.dump(0);
|
|
296 //exp.print();
|
|
297 exp.checkEscape();
|
|
298 }
|
|
299
|
|
300 /* BUG: need to issue an error on:
|
|
301 * this
|
|
302 * { if (x) return;
|
|
303 * super();
|
|
304 * }
|
|
305 */
|
|
306
|
|
307 if (sc.callSuper & CSX.CSXany_ctor && !(sc.callSuper & (CSX.CSXthis_ctor | CSX.CSXsuper_ctor))) {
|
|
308 error("return without calling constructor");
|
|
309 }
|
|
310
|
|
311 sc.callSuper |= CSX.CSXreturn;
|
|
312
|
|
313 // See if all returns are instead to be replaced with a goto returnLabel;
|
|
314 if (fd.returnLabel)
|
|
315 {
|
|
316 GotoStatement gs = new GotoStatement(loc, Id.returnLabel);
|
|
317
|
|
318 gs.label = fd.returnLabel;
|
|
319 if (exp)
|
|
320 {
|
|
321 /* Replace: return exp;
|
|
322 * with: exp; goto returnLabel;
|
|
323 */
|
|
324 Statement s = new ExpStatement(Loc(0), exp);
|
|
325 return new CompoundStatement(loc, s, gs);
|
|
326 }
|
|
327 return gs;
|
|
328 }
|
|
329
|
|
330 if (exp && tbret.ty == TY.Tvoid && !fd.isMain())
|
|
331 {
|
|
332 /* Replace:
|
|
333 * return exp;
|
|
334 * with:
|
|
335 * exp; return;
|
|
336 */
|
|
337 Statement s = new ExpStatement(loc, exp);
|
|
338 loc = Loc(0);
|
|
339 exp = null;
|
|
340 return new CompoundStatement(loc, s, this);
|
|
341 }
|
|
342
|
|
343 return this;
|
|
344 }
|
|
345
|
|
346 BE blockExit()
|
|
347 {
|
|
348 BE result = BE.BEreturn;
|
|
349 if (exp && exp.canThrow())
|
|
350 result |= BE.BEthrow;
|
|
351
|
|
352 return result;
|
|
353 }
|
|
354
|
63
|
355 Expression interpret(InterState istate)
|
0
|
356 {
|
63
|
357 version (LOG) {
|
|
358 printf("ReturnStatement.interpret(%s)\n", exp ? exp.toChars() : "");
|
|
359 }
|
|
360 mixin(START!());
|
|
361 if (!exp)
|
|
362 return EXP_VOID_INTERPRET;
|
|
363 version (LOG) {
|
|
364 Expression e = exp.interpret(istate);
|
|
365 printf("e = %p\n", e);
|
|
366 return e;
|
|
367 } else {
|
|
368 return exp.interpret(istate);
|
|
369 }
|
0
|
370 }
|
|
371
|
|
372 int inlineCost(InlineCostState* ics)
|
|
373 {
|
|
374 // Can't handle return statements nested in if's
|
|
375 if (ics.nested)
|
|
376 return COST_MAX;
|
|
377 return exp ? exp.inlineCost(ics) : 0;
|
|
378 }
|
|
379
|
|
380 Expression doInline(InlineDoState ids)
|
|
381 {
|
|
382 //printf("ReturnStatement.doInline() '%s'\n", exp ? exp.toChars() : "");
|
|
383 return exp ? exp.doInline(ids) : null;
|
|
384 }
|
|
385
|
|
386 Statement inlineScan(InlineScanState* iss)
|
|
387 {
|
|
388 //printf("ReturnStatement.inlineScan()\n");
|
|
389 if (exp)
|
|
390 {
|
|
391 exp = exp.inlineScan(iss);
|
|
392 }
|
|
393 return this;
|
|
394 }
|
|
395
|
|
396 void toIR(IRState* irs)
|
|
397 {
|
|
398 Blockx* blx = irs.blx;
|
|
399
|
|
400 incUsage(irs, loc);
|
|
401 if (exp)
|
|
402 {
|
|
403 elem *e;
|
|
404
|
|
405 FuncDeclaration func = irs.getFunc();
|
|
406 assert(func);
|
|
407 assert(func.type.ty == TY.Tfunction);
|
|
408 TypeFunction tf = cast(TypeFunction)(func.type);
|
|
409
|
|
410 RET retmethod = tf.retStyle();
|
|
411 if (retmethod == RET.RETstack)
|
|
412 {
|
|
413 elem* es;
|
|
414
|
|
415 /* If returning struct literal, write result
|
|
416 * directly into return value
|
|
417 */
|
|
418 if (exp.op == TOK.TOKstructliteral)
|
|
419 {
|
|
420 assert(false);
|
|
421 /*
|
|
422 StructLiteralExp se = cast(StructLiteralExp)exp;
|
|
423 char save[(StructLiteralExp).sizeof];
|
|
424 memcpy(save, se, sizeof(StructLiteralExp));
|
|
425 se.sym = irs.shidden;
|
|
426 se.soffset = 0;
|
|
427 se.fillHoles = 1;
|
|
428 e = exp.toElem(irs);
|
|
429 memcpy(se, save, sizeof(StructLiteralExp));
|
|
430 */
|
|
431 }
|
|
432 else
|
|
433 e = exp.toElem(irs);
|
|
434
|
|
435 assert(e);
|
|
436
|
|
437 if (exp.op == TOK.TOKstructliteral || (func.nrvo_can && func.nrvo_var))
|
|
438 {
|
|
439 // Return value via hidden pointer passed as parameter
|
|
440 // Write exp; return shidden;
|
|
441 es = e;
|
|
442 }
|
|
443 else
|
|
444 {
|
|
445 // Return value via hidden pointer passed as parameter
|
|
446 // Write *shidden=exp; return shidden;
|
|
447 int op;
|
|
448 tym_t ety;
|
|
449
|
|
450 ety = e.Ety;
|
|
451 es = el_una(OPER.OPind,ety,el_var(irs.shidden));
|
|
452 op = (tybasic(ety) == TYM.TYstruct) ? OPER.OPstreq : OPER.OPeq;
|
|
453 es = el_bin(op, ety, es, e);
|
|
454 if (op == OPER.OPstreq)
|
|
455 es.Enumbytes = cast(uint)exp.type.size();
|
|
456 version (DMDV2) {
|
|
457 /* Call postBlit() on *shidden
|
|
458 */
|
|
459 Type tb = exp.type.toBasetype();
|
|
460 //if (tb.ty == TY.Tstruct) exp.dump(0);
|
|
461 if ((exp.op == TOK.TOKvar || exp.op == TOK.TOKdotvar || exp.op == TOK.TOKstar) &&
|
|
462 tb.ty == TY.Tstruct)
|
|
463 { StructDeclaration sd = (cast(TypeStruct)tb).sym;
|
|
464 if (sd.postblit)
|
|
465 {
|
|
466 FuncDeclaration fd = sd.postblit;
|
|
467 elem* ec = el_var(irs.shidden);
|
|
468 ec = callfunc(loc, irs, 1, Type.tvoid, ec, tb.pointerTo(), fd, fd.type, null, null);
|
|
469 es = el_bin(OPER.OPcomma, ec.Ety, es, ec);
|
|
470 }
|
|
471
|
|
472 static if (false) {
|
|
473 /* It has been moved, so disable destructor
|
|
474 */
|
|
475 if (exp.op == TOK.TOKvar)
|
|
476 {
|
|
477 VarExp ve = cast(VarExp)exp;
|
|
478 VarDeclaration v = ve.var.isVarDeclaration();
|
|
479 if (v && v.rundtor)
|
|
480 {
|
|
481 elem* er = el_var(v.rundtor.toSymbol());
|
|
482 er = el_bin(OPER.OPeq, TYM.TYint, er, el_long(TYM.TYint, 0));
|
|
483 es = el_bin(OPER.OPcomma, TYM.TYint, es, er);
|
|
484 }
|
|
485 }
|
|
486 }
|
|
487 }
|
|
488 }
|
|
489 }
|
|
490 e = el_var(irs.shidden);
|
|
491 e = el_bin(OPER.OPcomma, e.Ety, es, e);
|
|
492 }
|
|
493 ///version (DMDV2) {
|
|
494 else if (tf.isref)
|
|
495 { // Reference return, so convert to a pointer
|
|
496 Expression ae = exp.addressOf(null);
|
|
497 e = ae.toElem(irs);
|
|
498 }
|
|
499 ///}
|
|
500 else
|
|
501 {
|
|
502 e = exp.toElem(irs);
|
|
503 assert(e);
|
|
504 }
|
|
505
|
|
506 block_appendexp(blx.curblock, e);
|
|
507 block_next(blx, BC.BCretexp, null);
|
|
508 }
|
|
509 else
|
|
510 block_next(blx, BC.BCret, null);
|
|
511 }
|
|
512
|
|
513 ReturnStatement isReturnStatement() { return this; }
|
|
514 } |