Mercurial > projects > ldc
comparison dmd/optimize.c @ 1630:44b145be2ef5
Merge dmd 1.056.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sat, 06 Feb 2010 15:53:52 +0000 |
parents | a542ef277a84 |
children |
comparison
equal
deleted
inserted
replaced
1629:b07d683ba4d0 | 1630:44b145be2ef5 |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | 2 // Compiler implementation of the D programming language |
3 // Copyright (c) 1999-2009 by Digital Mars | 3 // Copyright (c) 1999-2010 by Digital Mars |
4 // All Rights Reserved | 4 // All Rights Reserved |
5 // written by Walter Bright | 5 // written by Walter Bright |
6 // http://www.digitalmars.com | 6 // http://www.digitalmars.com |
7 // License for redistribution is by either the Artistic License | 7 // License for redistribution is by either the Artistic License |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | 8 // in artistic.txt, or the GNU General Public License in gnu.txt. |
342 } | 342 } |
343 return this; | 343 return this; |
344 } | 344 } |
345 | 345 |
346 Expression *CallExp::optimize(int result) | 346 Expression *CallExp::optimize(int result) |
347 { Expression *e = this; | 347 { |
348 //printf("CallExp::optimize(result = %d) %s\n", result, toChars()); | |
349 Expression *e = this; | |
348 | 350 |
349 // Optimize parameters | 351 // Optimize parameters |
350 if (arguments) | 352 if (arguments) |
351 { | 353 { |
352 for (size_t i = 0; i < arguments->dim; i++) | 354 for (size_t i = 0; i < arguments->dim; i++) |
356 arguments->data[i] = (void *)e; | 358 arguments->data[i] = (void *)e; |
357 } | 359 } |
358 } | 360 } |
359 | 361 |
360 e1 = e1->optimize(result); | 362 e1 = e1->optimize(result); |
361 if (e1->op == TOKvar && result & WANTinterpret) | 363 if (result & WANTinterpret) |
362 { | 364 { |
363 FuncDeclaration *fd = ((VarExp *)e1)->var->isFuncDeclaration(); | 365 Expression *eresult = interpret(NULL); |
364 if (fd) | 366 if (eresult == EXP_CANT_INTERPRET) |
365 { | 367 return e; |
366 Expression *eresult = fd->interpret(NULL, arguments); | 368 if (eresult && eresult != EXP_VOID_INTERPRET) |
367 if (eresult && eresult != EXP_VOID_INTERPRET) | 369 e = eresult; |
368 e = eresult; | 370 else |
369 else if (result & WANTinterpret) | 371 error("cannot evaluate %s at compile time", toChars()); |
370 error("cannot evaluate %s at compile time", toChars()); | |
371 } | |
372 } | 372 } |
373 return e; | 373 return e; |
374 } | 374 } |
375 | 375 |
376 | 376 |
622 | 622 |
623 Expression *CommaExp::optimize(int result) | 623 Expression *CommaExp::optimize(int result) |
624 { Expression *e; | 624 { Expression *e; |
625 | 625 |
626 //printf("CommaExp::optimize(result = %d) %s\n", result, toChars()); | 626 //printf("CommaExp::optimize(result = %d) %s\n", result, toChars()); |
627 // Comma needs special treatment, because it may | |
628 // contain compiler-generated declarations. We can interpret them, but | |
629 // otherwise we must NOT attempt to constant-fold them. | |
630 // In particular, if the comma returns a temporary variable, it needs | |
631 // to be an lvalue (this is particularly important for struct constructors) | |
632 | |
633 if (result & WANTinterpret) | |
634 { // Interpreting comma needs special treatment, because it may | |
635 // contain compiler-generated declarations. | |
636 e = interpret(NULL); | |
637 return (e == EXP_CANT_INTERPRET) ? this : e; | |
638 } | |
639 // Don't constant fold if it is a compiler-generated temporary. | |
640 if (e1->op == TOKdeclaration) | |
641 return this; | |
642 | |
627 e1 = e1->optimize(result & WANTinterpret); | 643 e1 = e1->optimize(result & WANTinterpret); |
628 e2 = e2->optimize(result); | 644 e2 = e2->optimize(result); |
629 if (!e1 || e1->op == TOKint64 || e1->op == TOKfloat64 || !e1->checkSideEffect(2)) | 645 if (!e1 || e1->op == TOKint64 || e1->op == TOKfloat64 || !e1->checkSideEffect(2)) |
630 { | 646 { |
631 e = e2; | 647 e = e2; |
707 e1 = e1->optimize(WANTvalue | (result & WANTinterpret)); | 723 e1 = e1->optimize(WANTvalue | (result & WANTinterpret)); |
708 if (!lwr) | 724 if (!lwr) |
709 { if (e1->op == TOKstring) | 725 { if (e1->op == TOKstring) |
710 { // Convert slice of string literal into dynamic array | 726 { // Convert slice of string literal into dynamic array |
711 Type *t = e1->type->toBasetype(); | 727 Type *t = e1->type->toBasetype(); |
712 if (t->next) | 728 if (t->nextOf()) |
713 e = e1->castTo(NULL, t->next->arrayOf()); | 729 e = e1->castTo(NULL, t->nextOf()->arrayOf()); |
714 } | 730 } |
715 return e; | 731 return e; |
716 } | 732 } |
717 if (result & WANTinterpret) | 733 if (result & WANTinterpret) |
718 e1 = fromConstInitializer(e1); | 734 e1 = fromConstInitializer(e1); |