comparison dmd2/expression.c @ 1526:54b3c1394d62

Merged dmdfe 2.031.
author Robert Clipsham <robert@octarineparrot.com>
date Tue, 07 Jul 2009 02:26:11 +0100
parents 833337c65fd3
children e4f7b5d9c68a
comparison
equal deleted inserted replaced
1525:d28cd7c45267 1526:54b3c1394d62
377 else if (e->op == TOKdotexp) 377 else if (e->op == TOKdotexp)
378 { 378 {
379 e->error("expression has no value"); 379 e->error("expression has no value");
380 } 380 }
381 381
382 }
383 else if (e->op == TOKdottd)
384 {
385 e = new CallExp(e->loc, e);
386 e = e->semantic(sc);
382 } 387 }
383 return e; 388 return e;
384 } 389 }
385 390
386 /****************************** 391 /******************************
1000 { error("expression %s is void and has no value", toChars()); 1005 { error("expression %s is void and has no value", toChars());
1001 #if 0 1006 #if 0
1002 dump(0); 1007 dump(0);
1003 halt(); 1008 halt();
1004 #endif 1009 #endif
1005 type = Type::tint32; 1010 type = Type::terror;
1006 } 1011 }
1007 } 1012 }
1008 1013
1009 Expression *Expression::combine(Expression *e1, Expression *e2) 1014 Expression *Expression::combine(Expression *e1, Expression *e2)
1010 { 1015 {
1062 } 1067 }
1063 1068
1064 void Expression::toMangleBuffer(OutBuffer *buf) 1069 void Expression::toMangleBuffer(OutBuffer *buf)
1065 { 1070 {
1066 error("expression %s is not a valid template value argument", toChars()); 1071 error("expression %s is not a valid template value argument", toChars());
1072 #ifdef DEBUG
1073 dump(0);
1074 #endif
1067 } 1075 }
1068 1076
1069 /*************************************** 1077 /***************************************
1070 * Return !=0 if expression is an lvalue. 1078 * Return !=0 if expression is an lvalue.
1071 */ 1079 */
1114 1122
1115 void Expression::checkScalar() 1123 void Expression::checkScalar()
1116 { 1124 {
1117 if (!type->isscalar()) 1125 if (!type->isscalar())
1118 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); 1126 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars());
1127 rvalue();
1119 } 1128 }
1120 1129
1121 void Expression::checkNoBool() 1130 void Expression::checkNoBool()
1122 { 1131 {
1123 if (type->toBasetype()->ty == Tbool) 1132 if (type->toBasetype()->ty == Tbool)
1128 { 1137 {
1129 if (!type->isintegral()) 1138 if (!type->isintegral())
1130 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); 1139 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars());
1131 return new ErrorExp(); 1140 return new ErrorExp();
1132 } 1141 }
1142 rvalue();
1133 return this; 1143 return this;
1134 } 1144 }
1135 1145
1136 Expression *Expression::checkArithmetic() 1146 Expression *Expression::checkArithmetic()
1137 { 1147 {
1138 if (!type->isintegral() && !type->isfloating()) 1148 if (!type->isintegral() && !type->isfloating())
1139 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars()); 1149 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars());
1140 return new ErrorExp(); 1150 return new ErrorExp();
1141 } 1151 }
1152 rvalue();
1142 return this; 1153 return this;
1143 } 1154 }
1144 1155
1145 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) 1156 void Expression::checkDeprecated(Scope *sc, Dsymbol *s)
1146 { 1157 {
1994 /* See if the symbol was a member of an enclosing 'with' 2005 /* See if the symbol was a member of an enclosing 'with'
1995 */ 2006 */
1996 withsym = scopesym->isWithScopeSymbol(); 2007 withsym = scopesym->isWithScopeSymbol();
1997 if (withsym) 2008 if (withsym)
1998 { 2009 {
2010 #if DMDV2
2011 /* Disallow shadowing
2012 */
2013 // First find the scope of the with
2014 Scope *scwith = sc;
2015 while (scwith->scopesym != scopesym)
2016 { scwith = scwith->enclosing;
2017 assert(scwith);
2018 }
2019 // Look at enclosing scopes for symbols with the same name,
2020 // in the same function
2021 for (Scope *scx = scwith; scx && scx->func == scwith->func; scx = scx->enclosing)
2022 { Dsymbol *s2;
2023
2024 if (scx->scopesym && scx->scopesym->symtab &&
2025 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL &&
2026 s != s2)
2027 {
2028 error("with symbol %s is shadowing local symbol %s", s->toPrettyChars(), s2->toPrettyChars());
2029 }
2030 }
2031 #endif
1999 s = s->toAlias(); 2032 s = s->toAlias();
2000 2033
2001 // Same as wthis.ident 2034 // Same as wthis.ident
2002 if (s->needThis() || s->isTemplateDeclaration()) 2035 if (s->needThis() || s->isTemplateDeclaration())
2003 { 2036 {
2158 { 2191 {
2159 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); 2192 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars());
2160 if (!type) 2193 if (!type)
2161 { type = v->type; 2194 { type = v->type;
2162 if (!v->type) 2195 if (!v->type)
2163 { error("forward reference of %s", v->toChars()); 2196 { error("forward reference of %s %s", v->kind(), v->toChars());
2164 type = Type::terror; 2197 type = Type::terror;
2165 } 2198 }
2166 } 2199 }
2167 e = new VarExp(loc, v); 2200 e = new VarExp(loc, v);
2168 e->type = type; 2201 e->type = type;
2176 return e->semantic(sc); 2209 return e->semantic(sc);
2177 } 2210 }
2178 f = s->isFuncDeclaration(); 2211 f = s->isFuncDeclaration();
2179 if (f) 2212 if (f)
2180 { //printf("'%s' is a function\n", f->toChars()); 2213 { //printf("'%s' is a function\n", f->toChars());
2214
2215 if (!f->type->deco)
2216 {
2217 error("forward reference to %s", toChars());
2218 }
2181 return new VarExp(loc, f, hasOverloads); 2219 return new VarExp(loc, f, hasOverloads);
2182 } 2220 }
2183 o = s->isOverloadSet(); 2221 o = s->isOverloadSet();
2184 if (o) 2222 if (o)
2185 { //printf("'%s' is an overload set\n", o->toChars()); 2223 { //printf("'%s' is an overload set\n", o->toChars());
2235 return e; 2273 return e;
2236 } 2274 }
2237 2275
2238 TemplateInstance *ti = s->isTemplateInstance(); 2276 TemplateInstance *ti = s->isTemplateInstance();
2239 if (ti && !global.errors) 2277 if (ti && !global.errors)
2240 { if (!ti->semanticdone) 2278 { if (!ti->semanticRun)
2241 ti->semantic(sc); 2279 ti->semantic(sc);
2242 s = ti->inst->toAlias(); 2280 s = ti->inst->toAlias();
2243 if (!s->isTemplateInstance()) 2281 if (!s->isTemplateInstance())
2244 goto Lagain; 2282 goto Lagain;
2245 e = new ScopeExp(loc, ti); 2283 e = new ScopeExp(loc, ti);
2924 2962
2925 // Run semantic() on each element 2963 // Run semantic() on each element
2926 for (int i = 0; i < elements->dim; i++) 2964 for (int i = 0; i < elements->dim; i++)
2927 { e = (Expression *)elements->data[i]; 2965 { e = (Expression *)elements->data[i];
2928 e = e->semantic(sc); 2966 e = e->semantic(sc);
2967 assert(e->type);
2929 elements->data[i] = (void *)e; 2968 elements->data[i] = (void *)e;
2930 } 2969 }
2931 expandTuples(elements); 2970 expandTuples(elements);
2932 for (int i = 0; i < elements->dim; i++) 2971 for (int i = 0; i < elements->dim; i++)
2933 { e = (Expression *)elements->data[i]; 2972 { e = (Expression *)elements->data[i];
3403 //printf("TypeExp::semantic(%s)\n", type->toChars()); 3442 //printf("TypeExp::semantic(%s)\n", type->toChars());
3404 type = type->semantic(loc, sc); 3443 type = type->semantic(loc, sc);
3405 return this; 3444 return this;
3406 } 3445 }
3407 3446
3447 void TypeExp::rvalue()
3448 {
3449 error("type %s has no value", toChars());
3450 }
3451
3408 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) 3452 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3409 { 3453 {
3410 type->toCBuffer(buf, NULL, hgs); 3454 type->toCBuffer(buf, NULL, hgs);
3411 } 3455 }
3412 3456
3438 #endif 3482 #endif
3439 Lagain: 3483 Lagain:
3440 ti = sds->isTemplateInstance(); 3484 ti = sds->isTemplateInstance();
3441 if (ti && !global.errors) 3485 if (ti && !global.errors)
3442 { Dsymbol *s; 3486 { Dsymbol *s;
3443 if (!ti->semanticdone) 3487 if (!ti->semanticRun)
3444 ti->semantic(sc); 3488 ti->semantic(sc);
3445 s = ti->inst->toAlias(); 3489 s = ti->inst->toAlias();
3446 sds2 = s->isScopeDsymbol(); 3490 sds2 = s->isScopeDsymbol();
3447 if (!sds2) 3491 if (!sds2)
3448 { Expression *e; 3492 { Expression *e;
3653 break; 3697 break;
3654 } 3698 }
3655 } 3699 }
3656 #endif 3700 #endif
3657 } 3701 }
3658 else if (fdn) // Possible problems here, no obvious solution when merging 3702 #if 1
3703 else if (thisexp)
3704 error("e.new is only for allocating nested classes");
3705 else if (fdn)
3706 {
3707 // make sure the parent context fdn of cd is reachable from sc
3708 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent)
3709 {
3710 if (fdn == sp)
3711 break;
3712 FuncDeclaration *fsp = sp ? sp->isFuncDeclaration() : NULL;
3713 if (!sp || (fsp && fsp->isStatic()))
3714 {
3715 error("outer function context of %s is needed to 'new' nested class %s", fdn->toPrettyChars(), cd->toPrettyChars());
3716 break;
3717 }
3718 }
3719 }
3720 #else
3721 else if (fdn)
3659 { /* The nested class cd is nested inside a function, 3722 { /* The nested class cd is nested inside a function,
3660 * we'll let getEthis() look for errors. 3723 * we'll let getEthis() look for errors.
3661 */ 3724 */
3662 //printf("nested class %s is nested inside function %s, we're in %s\n", cd->toChars(), fdn->toChars(), sc->func->toChars()); 3725 //printf("nested class %s is nested inside function %s, we're in %s\n", cd->toChars(), fdn->toChars(), sc->func->toChars());
3663 if (thisexp) 3726 if (thisexp)
3664 // Because thisexp cannot be a function frame pointer 3727 // Because thisexp cannot be a function frame pointer
3665 error("e.new is only for allocating nested classes"); 3728 error("e.new is only for allocating nested classes");
3666 } 3729 }
3730 #endif
3667 else 3731 else
3668 assert(0); 3732 assert(0);
3669 } 3733 }
3670 else if (thisexp) 3734 else if (thisexp)
3671 error("e.new is only for allocating nested classes"); 3735 error("e.new is only for allocating nested classes");
4026 type = new TypeDelegate(tf); 4090 type = new TypeDelegate(tf);
4027 type = type->semantic(loc, sc); 4091 type = type->semantic(loc, sc);
4028 } 4092 }
4029 #endif 4093 #endif
4030 } 4094 }
4095
4031 /* Fix for 1161 doesn't work because it causes protection 4096 /* Fix for 1161 doesn't work because it causes protection
4032 * problems when instantiating imported templates passing private 4097 * problems when instantiating imported templates passing private
4033 * variables as alias template parameters. 4098 * variables as alias template parameters.
4034 */ 4099 */
4035 //accessCheck(loc, sc, NULL, var); 4100 //accessCheck(loc, sc, NULL, var);
4107 e = new FuncExp(loc, fd); 4172 e = new FuncExp(loc, fd);
4108 e->type = type; 4173 e->type = type;
4109 return e; 4174 return e;
4110 } 4175 }
4111 #endif 4176 #endif
4177
4112 return this; 4178 return this;
4113 } 4179 }
4114 4180
4115 char *VarExp::toChars() 4181 char *VarExp::toChars()
4116 { 4182 {
4293 if (0 && exps->dim == 1) 4359 if (0 && exps->dim == 1)
4294 { 4360 {
4295 return (Expression *)exps->data[0]; 4361 return (Expression *)exps->data[0];
4296 } 4362 }
4297 type = new TypeTuple(exps); 4363 type = new TypeTuple(exps);
4364 type = type->semantic(loc, sc);
4298 //printf("-TupleExp::semantic(%s)\n", toChars()); 4365 //printf("-TupleExp::semantic(%s)\n", toChars());
4299 return this; 4366 return this;
4300 } 4367 }
4301 4368
4302 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) 4369 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6157 * foo!(tiargs)(funcargs) 6224 * foo!(tiargs)(funcargs)
6158 */ 6225 */
6159 if (e1->op == TOKimport && !e1->type) 6226 if (e1->op == TOKimport && !e1->type)
6160 { ScopeExp *se = (ScopeExp *)e1; 6227 { ScopeExp *se = (ScopeExp *)e1;
6161 TemplateInstance *ti = se->sds->isTemplateInstance(); 6228 TemplateInstance *ti = se->sds->isTemplateInstance();
6162 if (ti && !ti->semanticdone) 6229 if (ti && !ti->semanticRun)
6163 { 6230 {
6164 /* Attempt to instantiate ti. If that works, go with it. 6231 /* Attempt to instantiate ti. If that works, go with it.
6165 * If not, go with partial explicit specialization. 6232 * If not, go with partial explicit specialization.
6166 */ 6233 */
6167 ti->semanticTiargs(sc); 6234 ti->semanticTiargs(sc);
6185 * expr.foo!(tiargs)(funcargs) 6252 * expr.foo!(tiargs)(funcargs)
6186 */ 6253 */
6187 if (e1->op == TOKdotti && !e1->type) 6254 if (e1->op == TOKdotti && !e1->type)
6188 { DotTemplateInstanceExp *se = (DotTemplateInstanceExp *)e1; 6255 { DotTemplateInstanceExp *se = (DotTemplateInstanceExp *)e1;
6189 TemplateInstance *ti = se->ti; 6256 TemplateInstance *ti = se->ti;
6190 if (!ti->semanticdone) 6257 if (!ti->semanticRun)
6191 { 6258 {
6192 /* Attempt to instantiate ti. If that works, go with it. 6259 /* Attempt to instantiate ti. If that works, go with it.
6193 * If not, go with partial explicit specialization. 6260 * If not, go with partial explicit specialization.
6194 */ 6261 */
6195 ti->semanticTiargs(sc); 6262 ti->semanticTiargs(sc);
6793 /************************************************************/ 6860 /************************************************************/
6794 6861
6795 AddrExp::AddrExp(Loc loc, Expression *e) 6862 AddrExp::AddrExp(Loc loc, Expression *e)
6796 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) 6863 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e)
6797 { 6864 {
6865 #if IN_LLVM
6798 m = NULL; 6866 m = NULL;
6867 #endif
6799 } 6868 }
6800 6869
6801 Expression *AddrExp::semantic(Scope *sc) 6870 Expression *AddrExp::semantic(Scope *sc)
6802 { 6871 {
6803 #if LOGSEMANTIC 6872 #if LOGSEMANTIC
6804 printf("AddrExp::semantic('%s')\n", toChars()); 6873 printf("AddrExp::semantic('%s')\n", toChars());
6805 #endif 6874 #endif
6806 if (!type) 6875 if (!type)
6807 { 6876 {
6877 #if IN_LLVM
6808 m = sc->module; 6878 m = sc->module;
6879 #endif
6809 UnaExp::semantic(sc); 6880 UnaExp::semantic(sc);
6810 e1 = e1->toLvalue(sc, NULL); 6881 e1 = e1->toLvalue(sc, NULL);
6811 if (!e1->type) 6882 if (!e1->type)
6812 { 6883 {
6813 error("cannot take address of %s", e1->toChars()); 6884 error("cannot take address of %s", e1->toChars());
6814 type = Type::tint32; 6885 return new ErrorExp();
6815 return this; 6886 }
6816 } 6887 if (!e1->type->deco)
6888 {
6889 /* No deco means semantic() was not run on the type.
6890 * We have to run semantic() on the symbol to get the right type:
6891 * auto x = &bar;
6892 * pure: int bar() { return 1;}
6893 * otherwise the 'pure' is missing from the type assigned to x.
6894 */
6895
6896 error("forward reference to %s", e1->toChars());
6897 return new ErrorExp();
6898 }
6899
6900 //printf("test3 deco = %p\n", e1->type->deco);
6817 type = e1->type->pointerTo(); 6901 type = e1->type->pointerTo();
6818 6902
6819 // See if this should really be a delegate 6903 // See if this should really be a delegate
6820 if (e1->op == TOKdotvar) 6904 if (e1->op == TOKdotvar)
6821 { 6905 {
6876 /************************************************************/ 6960 /************************************************************/
6877 6961
6878 PtrExp::PtrExp(Loc loc, Expression *e) 6962 PtrExp::PtrExp(Loc loc, Expression *e)
6879 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) 6963 : UnaExp(loc, TOKstar, sizeof(PtrExp), e)
6880 { 6964 {
6881 if (e->type) 6965 // if (e->type)
6882 type = ((TypePointer *)e->type)->next; 6966 // type = ((TypePointer *)e->type)->next;
6883 } 6967 }
6884 6968
6885 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) 6969 PtrExp::PtrExp(Loc loc, Expression *e, Type *t)
6886 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) 6970 : UnaExp(loc, TOKstar, sizeof(PtrExp), e)
6887 { 6971 {
6888 type = t; 6972 type = t;
6889 } 6973 }
6890 6974
6891 Expression *PtrExp::semantic(Scope *sc) 6975 Expression *PtrExp::semantic(Scope *sc)
6892 { Type *tb; 6976 {
6893
6894 #if LOGSEMANTIC 6977 #if LOGSEMANTIC
6895 printf("PtrExp::semantic('%s')\n", toChars()); 6978 printf("PtrExp::semantic('%s')\n", toChars());
6896 #endif 6979 #endif
6897 if (!type) 6980 if (!type)
6898 { 6981 {
6901 if (!e1->type) 6984 if (!e1->type)
6902 printf("PtrExp::semantic('%s')\n", toChars()); 6985 printf("PtrExp::semantic('%s')\n", toChars());
6903 Expression *e = op_overload(sc); 6986 Expression *e = op_overload(sc);
6904 if (e) 6987 if (e)
6905 return e; 6988 return e;
6906 tb = e1->type->toBasetype(); 6989 Type *tb = e1->type->toBasetype();
6907 switch (tb->ty) 6990 switch (tb->ty)
6908 { 6991 {
6909 case Tpointer: 6992 case Tpointer:
6910 type = ((TypePointer *)tb)->next; 6993 type = ((TypePointer *)tb)->next;
6911 break; 6994 break;
6916 e1 = e1->castTo(sc, type->pointerTo()); 6999 e1 = e1->castTo(sc, type->pointerTo());
6917 break; 7000 break;
6918 7001
6919 default: 7002 default:
6920 error("can only * a pointer, not a '%s'", e1->type->toChars()); 7003 error("can only * a pointer, not a '%s'", e1->type->toChars());
6921 type = Type::tint32; 7004 return new ErrorExp();
6922 break;
6923 } 7005 }
6924 rvalue(); 7006 rvalue();
6925 } 7007 }
6926 return this; 7008 return this;
6927 } 7009 }
9742 error("compare not defined for complex operands"); 9824 error("compare not defined for complex operands");
9743 e = new ErrorExp(); 9825 e = new ErrorExp();
9744 } 9826 }
9745 #endif 9827 #endif
9746 else 9828 else
9829 { e1->rvalue();
9830 e2->rvalue();
9747 e = this; 9831 e = this;
9748 //printf("CmpExp: %s\n", e->toChars()); 9832 }
9833 //printf("CmpExp: %s, type = %s\n", e->toChars(), e->type->toChars());
9749 return e; 9834 return e;
9750 } 9835 }
9751 9836
9752 int CmpExp::isBit() 9837 int CmpExp::isBit()
9753 { 9838 {