Skip to content
Browse files

merge D2 pull #961

  • Loading branch information...
1 parent 25d1034 commit bcc6ee4284bbf8f9876cbef02cf6acae34358835 @WalterBright WalterBright committed May 21, 2012
Showing with 71 additions and 34 deletions.
  1. +45 −31 src/expression.c
  2. +14 −0 src/expression.h
  3. +12 −3 src/mtype.c
View
76 src/expression.c
@@ -1088,12 +1088,12 @@ dump(0);
/***************************************
* Return !=0 if expression is an lvalue.
*/
-#if DMDV2
+
int Expression::isLvalue()
{
return 0;
}
-#endif
+
/*******************************
* Give error if we're not an lvalue.
@@ -2089,12 +2089,12 @@ void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
buf->writestring(ident->toChars());
}
-#if DMDV2
+
int IdentifierExp::isLvalue()
{
return 1;
}
-#endif
+
Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e)
{
@@ -2358,12 +2358,12 @@ void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
buf->writestring(s->toChars());
}
-#if DMDV2
+
int DsymbolExp::isLvalue()
{
return 1;
}
-#endif
+
Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e)
{
@@ -2456,12 +2456,12 @@ void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
buf->writestring("this");
}
-#if DMDV2
+
int ThisExp::isLvalue()
{
return 1;
}
-#endif
+
Expression *ThisExp::toLvalue(Scope *sc, Expression *e)
{
@@ -2880,12 +2880,15 @@ int StringExp::isBool(int result)
return result ? TRUE : FALSE;
}
-#if DMDV2
+
int StringExp::isLvalue()
{
- return 1;
+ /* string literal is rvalue in default, but
+ * conversion to reference of static array is only allowed.
+ */
+ return 0;
}
-#endif
+
unsigned StringExp::charAt(size_t i)
{ unsigned value;
@@ -4310,14 +4313,14 @@ void VarExp::checkEscapeRef()
}
}
-#if DMDV2
+
int VarExp::isLvalue()
{
if (var->storage_class & STClazy)
return 0;
return 1;
}
-#endif
+
Expression *VarExp::toLvalue(Scope *sc, Expression *e)
{
@@ -5455,6 +5458,11 @@ Expression *BinAssignExp::semantic(Scope *sc)
return this;
}
+int BinAssignExp::isLvalue()
+{
+ return 1;
+}
+
/************************************************************/
CompileExp::CompileExp(Loc loc, Expression *e)
@@ -6116,12 +6124,12 @@ Expression *DotVarExp::semantic(Scope *sc)
return this;
}
-#if DMDV2
+
int DotVarExp::isLvalue()
{
return 1;
}
-#endif
+
Expression *DotVarExp::toLvalue(Scope *sc, Expression *e)
{
@@ -7219,17 +7227,23 @@ int CallExp::canThrow()
}
#endif
-#if DMDV2
int CallExp::isLvalue()
{
-// if (type->toBasetype()->ty == Tstruct)
-// return 1;
Type *tb = e1->type->toBasetype();
+ if (tb->ty == Tdelegate || tb->ty == Tpointer)
+ tb = tb->nextOf();
+#if DMDV2
if (tb->ty == Tfunction && ((TypeFunction *)tb)->isref)
+ {
+ if (e1->op == TOKdotvar)
+ if (((DotVarExp *)e1)->var->isCtorDeclaration())
+ return 0;
return 1; // function returns a reference
+ }
+#endif
return 0;
}
-#endif
+
Expression *CallExp::toLvalue(Scope *sc, Expression *e)
{
@@ -7415,12 +7429,12 @@ Expression *PtrExp::semantic(Scope *sc)
return this;
}
-#if DMDV2
+
int PtrExp::isLvalue()
{
return 1;
}
-#endif
+
void PtrExp::checkEscapeRef()
{
@@ -8035,12 +8049,12 @@ void SliceExp::checkEscapeRef()
e1->checkEscapeRef();
}
-#if DMDV2
+
int SliceExp::isLvalue()
{
return 1;
}
-#endif
+
Expression *SliceExp::toLvalue(Scope *sc, Expression *e)
{
@@ -8178,14 +8192,14 @@ Expression *ArrayExp::semantic(Scope *sc)
return new ErrorExp();
}
-#if DMDV2
+
int ArrayExp::isLvalue()
{
if (type && type->toBasetype()->ty == Tvoid)
return 0;
return 1;
}
-#endif
+
Expression *ArrayExp::toLvalue(Scope *sc, Expression *e)
{
@@ -8267,12 +8281,12 @@ void CommaExp::checkEscapeRef()
e2->checkEscapeRef();
}
-#if DMDV2
+
int CommaExp::isLvalue()
{
return e2->isLvalue();
}
-#endif
+
Expression *CommaExp::toLvalue(Scope *sc, Expression *e)
{
@@ -8449,12 +8463,12 @@ Expression *IndexExp::semantic(Scope *sc)
return new ErrorExp();
}
-#if DMDV2
+
int IndexExp::isLvalue()
{
return 1;
}
-#endif
+
Expression *IndexExp::toLvalue(Scope *sc, Expression *e)
{
@@ -10426,12 +10440,12 @@ Expression *CondExp::semantic(Scope *sc)
return this;
}
-#if DMDV2
+
int CondExp::isLvalue()
{
return e1->isLvalue() && e2->isLvalue();
}
-#endif
+
Expression *CondExp::toLvalue(Scope *sc, Expression *ex)
{
View
14 src/expression.h
@@ -124,6 +124,7 @@ struct Expression : Object
virtual StringExp *toString();
virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
virtual void toMangleBuffer(OutBuffer *buf);
+ virtual int isLvalue();
virtual Expression *toLvalue(Scope *sc, Expression *e);
virtual Expression *modifiableLvalue(Scope *sc, Expression *e);
virtual Expression *implicitCastTo(Scope *sc, Type *t);
@@ -273,6 +274,7 @@ struct IdentifierExp : Expression
char *toChars();
void dump(int indent);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
};
@@ -291,6 +293,7 @@ struct DsymbolExp : Expression
char *toChars();
void dump(int indent);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
};
@@ -303,6 +306,7 @@ struct ThisExp : Expression
Expression *interpret(InterState *istate, CtfeGoal goal = ctfeNeedRvalue);
int isBool(int result);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
int inlineCost3(InlineCostState *ics);
@@ -364,6 +368,7 @@ struct StringExp : Expression
Expression *castTo(Scope *sc, Type *t);
int compare(Object *obj);
int isBool(int result);
+ int isLvalue();
unsigned charAt(size_t i);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toMangleBuffer(OutBuffer *buf);
@@ -788,6 +793,7 @@ struct BinAssignExp : BinExp
}
Expression *semantic(Scope *sc);
+ int isLvalue();
};
/****************************************************************/
@@ -848,6 +854,7 @@ struct DotVarExp : UnaExp
DotVarExp(Loc loc, Expression *e, Declaration *var);
Expression *semantic(Scope *sc);
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
Expression *modifiableLvalue(Scope *sc, Expression *e);
Expression *optimize(int result);
@@ -913,6 +920,7 @@ struct CallExp : UnaExp
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void dump(int indent);
elem *toElem(IRState *irs);
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
Expression *modifiableLvalue(Scope *sc, Expression *e);
@@ -938,6 +946,7 @@ struct PtrExp : UnaExp
PtrExp(Loc loc, Expression *e);
PtrExp(Loc loc, Expression *e, Type *t);
Expression *semantic(Scope *sc);
+ int isLvalue();
void checkEscapeRef();
Expression *toLvalue(Scope *sc, Expression *e);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
@@ -1049,6 +1058,7 @@ struct SliceExp : UnaExp
Expression *semantic(Scope *sc);
void checkEscape();
void checkEscapeRef();
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
Expression *modifiableLvalue(Scope *sc, Expression *e);
int isBool(int result);
@@ -1085,6 +1095,7 @@ struct ArrayExp : UnaExp
Expression *syntaxCopy();
int apply(apply_fp_t fp, void *param);
Expression *semantic(Scope *sc);
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
@@ -1110,6 +1121,7 @@ struct CommaExp : BinExp
Expression *semantic(Scope *sc);
void checkEscape();
void checkEscapeRef();
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
Expression *modifiableLvalue(Scope *sc, Expression *e);
int isBool(int result);
@@ -1126,6 +1138,7 @@ struct IndexExp : BinExp
IndexExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
Expression *modifiableLvalue(Scope *sc, Expression *e);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
@@ -1520,6 +1533,7 @@ struct CondExp : BinExp
Expression *interpret(InterState *istate, CtfeGoal goal = ctfeNeedRvalue);
void checkEscape();
void checkEscapeRef();
+ int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
Expression *modifiableLvalue(Scope *sc, Expression *e);
Expression *checkToBoolean();
View
15 src/mtype.c
@@ -3313,6 +3313,15 @@ Expression *TypeDelegate::dotExp(Scope *sc, Expression *e, Identifier *ident)
}
else if (ident == Id::funcptr)
{
+ if (!e->isLvalue())
+ {
+ Identifier *idtmp = Lexer::uniqueId("__dgtmp");
+ VarDeclaration *tmp = new VarDeclaration(e->loc, this, idtmp, new ExpInitializer(0, e));
+ tmp->storage_class |= STCctfe;
+ e = new DeclarationExp(e->loc, tmp);
+ e = new CommaExp(e->loc, e, new VarExp(e->loc, tmp));
+ e = e->semantic(sc);
+ }
e = e->addressOf(sc);
e->type = tvoidptr;
e = new AddExp(e->loc, e, new IntegerExp(PTRSIZE));
@@ -3349,15 +3358,15 @@ void TypeQualified::syntaxCopyHelper(TypeQualified *t)
idents.setDim(t->idents.dim);
for (size_t i = 0; i < idents.dim; i++)
{
- Identifier *id = (Identifier *)t->idents.data[i];
+ Identifier *id = t->idents[i];
if (id->dyncast() == DYNCAST_DSYMBOL)
{
TemplateInstance *ti = (TemplateInstance *)id;
ti = (TemplateInstance *)ti->syntaxCopy(NULL);
id = (Identifier *)ti;
}
- idents.data[i] = id;
+ idents[i] = id;
}
}
@@ -3370,7 +3379,7 @@ void TypeQualified::addIdent(Identifier *ident)
void TypeQualified::toCBuffer2Helper(OutBuffer *buf, HdrGenState *hgs)
{
for (size_t i = 0; i < idents.dim; i++)
- { Identifier *id = (Identifier *)idents.data[i];
+ { Identifier *id = idents[i];
buf->writeByte('.');

0 comments on commit bcc6ee4

Please sign in to comment.
Something went wrong with that request. Please try again.