Mercurial > projects > ldc
diff 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 |
line wrap: on
line diff
--- a/dmd/optimize.c Wed Jan 06 19:53:35 2010 +0100 +++ b/dmd/optimize.c Sat Feb 06 15:53:52 2010 +0000 @@ -1,6 +1,6 @@ // Compiler implementation of the D programming language -// Copyright (c) 1999-2009 by Digital Mars +// Copyright (c) 1999-2010 by Digital Mars // All Rights Reserved // written by Walter Bright // http://www.digitalmars.com @@ -344,7 +344,9 @@ } Expression *CallExp::optimize(int result) -{ Expression *e = this; +{ + //printf("CallExp::optimize(result = %d) %s\n", result, toChars()); + Expression *e = this; // Optimize parameters if (arguments) @@ -358,17 +360,15 @@ } e1 = e1->optimize(result); - if (e1->op == TOKvar && result & WANTinterpret) + if (result & WANTinterpret) { - FuncDeclaration *fd = ((VarExp *)e1)->var->isFuncDeclaration(); - if (fd) - { - Expression *eresult = fd->interpret(NULL, arguments); - if (eresult && eresult != EXP_VOID_INTERPRET) - e = eresult; - else if (result & WANTinterpret) - error("cannot evaluate %s at compile time", toChars()); - } + Expression *eresult = interpret(NULL); + if (eresult == EXP_CANT_INTERPRET) + return e; + if (eresult && eresult != EXP_VOID_INTERPRET) + e = eresult; + else + error("cannot evaluate %s at compile time", toChars()); } return e; } @@ -624,6 +624,22 @@ { Expression *e; //printf("CommaExp::optimize(result = %d) %s\n", result, toChars()); + // Comma needs special treatment, because it may + // contain compiler-generated declarations. We can interpret them, but + // otherwise we must NOT attempt to constant-fold them. + // In particular, if the comma returns a temporary variable, it needs + // to be an lvalue (this is particularly important for struct constructors) + + if (result & WANTinterpret) + { // Interpreting comma needs special treatment, because it may + // contain compiler-generated declarations. + e = interpret(NULL); + return (e == EXP_CANT_INTERPRET) ? this : e; + } + // Don't constant fold if it is a compiler-generated temporary. + if (e1->op == TOKdeclaration) + return this; + e1 = e1->optimize(result & WANTinterpret); e2 = e2->optimize(result); if (!e1 || e1->op == TOKint64 || e1->op == TOKfloat64 || !e1->checkSideEffect(2)) @@ -709,8 +725,8 @@ { if (e1->op == TOKstring) { // Convert slice of string literal into dynamic array Type *t = e1->type->toBasetype(); - if (t->next) - e = e1->castTo(NULL, t->next->arrayOf()); + if (t->nextOf()) + e = e1->castTo(NULL, t->nextOf()->arrayOf()); } return e; }