Changeset 523
- Timestamp:
- 04/19/07 18:22:23 (1 year ago)
- Files:
-
- branches/dmdfe/cast.c (modified) (2 diffs)
- branches/dmdfe/constfold.c (modified) (2 diffs)
- branches/dmdfe/declaration.c (modified) (1 diff)
- branches/dmdfe/expression.c (modified) (5 diffs)
- branches/dmdfe/expression.h (modified) (1 diff)
- branches/dmdfe/interpret.c (modified) (8 diffs)
- branches/dmdfe/link.c (modified) (4 diffs)
- branches/dmdfe/mars.c (modified) (1 diff)
- branches/dmdfe/mtype.c (modified) (4 diffs)
- branches/dmdfe/opover.c (modified) (2 diffs)
- branches/dmdfe/optimize.c (modified) (3 diffs)
- branches/dmdfe/parse.c (modified) (7 diffs)
- branches/dmdfe/port.h (modified) (1 diff)
- branches/dmdfe/statement.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/dmdfe/cast.c
r458 r523 404 404 } 405 405 406 int AssocArrayLiteralExp::implicitConvTo(Type *t) 407 { MATCH result = MATCHexact; 408 409 Type *typeb = type->toBasetype(); 410 Type *tb = t->toBasetype(); 411 if (tb->ty == Taarray && typeb->ty == Taarray) 412 { 413 for (size_t i = 0; i < keys->dim; i++) 414 { Expression *e = (Expression *)keys->data[i]; 415 MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->key); 416 if (m < result) 417 result = m; // remember worst match 418 if (result == MATCHnomatch) 419 break; // no need to check for worse 420 e = (Expression *)values->data[i]; 421 m = (MATCH)e->implicitConvTo(tb->next); 422 if (m < result) 423 result = m; // remember worst match 424 if (result == MATCHnomatch) 425 break; // no need to check for worse 426 } 427 return result; 428 } 429 else 430 return Expression::implicitConvTo(t); 431 } 432 406 433 int AddrExp::implicitConvTo(Type *t) 407 434 { … … 919 946 { 920 947 type = typeb->next->pointerTo(); 948 } 949 L1: 950 return Expression::castTo(sc, t); 951 } 952 953 Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t) 954 { 955 Type *typeb = type->toBasetype(); 956 Type *tb = t->toBasetype(); 957 if (tb->ty == Taarray && typeb->ty == Taarray && 958 tb->next->toBasetype()->ty != Tvoid) 959 { 960 assert(keys->dim == values->dim); 961 for (size_t i = 0; i < keys->dim; i++) 962 { Expression *e = (Expression *)values->data[i]; 963 e = e->castTo(sc, tb->next); 964 values->data[i] = (void *)e; 965 966 e = (Expression *)keys->data[i]; 967 e = e->castTo(sc, ((TypeAArray *)tb)->key); 968 keys->data[i] = (void *)e; 969 } 970 type = t; 971 return this; 921 972 } 922 973 L1: branches/dmdfe/constfold.c
r458 r523 1019 1019 e = new IntegerExp(loc, dim, type); 1020 1020 } 1021 else if (e1->op == TOKassocarrayliteral) 1022 { AssocArrayLiteralExp *ale = (AssocArrayLiteralExp *)e1; 1023 size_t dim = ale->keys->dim; 1024 1025 e = new IntegerExp(loc, dim, type); 1026 } 1021 1027 else 1022 1028 e = EXP_CANT_INTERPRET; … … 1088 1094 { e = (Expression *)ale->elements->data[i]; 1089 1095 e->type = type; 1096 } 1097 } 1098 } 1099 else if (e1->op == TOKassocarrayliteral && !e1->checkSideEffect(2)) 1100 { 1101 AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e1; 1102 /* Search the keys backwards, in case there are duplicate keys 1103 */ 1104 for (size_t i = ae->keys->dim; i;) 1105 { 1106 i--; 1107 Expression *ekey = (Expression *)ae->keys->data[i]; 1108 Expression *ex = Equal(TOKequal, Type::tbool, ekey, e2); 1109 if (ex == EXP_CANT_INTERPRET) 1110 return ex; 1111 if (ex->isBool(TRUE)) 1112 { e = (Expression *)ae->values->data[i]; 1113 e->type = type; 1114 break; 1090 1115 } 1091 1116 } branches/dmdfe/declaration.c
r460 r523 597 597 { 598 598 //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars()); 599 //printf("type = %s\n", type->toChars()); 600 //printf("linkage = %d\n", sc->linkage); 599 601 //if (strcmp(toChars(), "mul") == 0) halt(); 600 602 branches/dmdfe/expression.c
r460 r523 43 43 #endif 44 44 45 #include "port.h"45 //#include "port.h" 46 46 #include "mtype.h" 47 47 #include "init.h" … … 3415 3415 exps->push(e); 3416 3416 } 3417 else if (o->dyncast() == DYNCAST_TYPE) 3418 { 3419 Type *t = (Type *)o; 3420 Expression *e = new TypeExp(loc, t); 3421 exps->push(e); 3422 } 3417 3423 else 3418 3424 { … … 4174 4180 Expression *CompileExp::semantic(Scope *sc) 4175 4181 { 4176 #if 1 ||LOGSEMANTIC4182 #if LOGSEMANTIC 4177 4183 printf("CompileExp::semantic('%s')\n", toChars()); 4178 4184 #endif … … 4180 4186 e1 = resolveProperties(sc, e1); 4181 4187 e1 = e1->optimize(WANTvalue | WANTinterpret); 4182 e1->print();4183 4188 if (e1->op != TOKstring) 4184 4189 { error("argument to mixin must be a string, not (%s)", e1->toChars()); … … 5923 5928 } 5924 5929 } 5925 return e1->castTo(sc, to); 5930 e = e1->castTo(sc, to); 5931 return e; 5926 5932 } 5927 5933 branches/dmdfe/expression.h
r460 r523 368 368 Expression *optimize(int result); 369 369 Expression *interpret(InterState *istate); 370 int implicitConvTo(Type *t); 371 Expression *castTo(Scope *sc, Type *t); 370 372 371 373 int inlineCost(InlineCostState *ics); branches/dmdfe/interpret.c
r460 r523 992 992 993 993 Expression *AssocArrayLiteralExp::interpret(InterState *istate) 994 { Expressions *keysx = NULL;995 Expressions *valuesx = NULL;994 { Expressions *keysx = keys; 995 Expressions *valuesx = values; 996 996 997 997 #if LOG … … 1005 1005 ex = ekey->interpret(istate); 1006 1006 if (ex == EXP_CANT_INTERPRET) 1007 { delete keysx; 1008 delete valuesx; 1009 return EXP_CANT_INTERPRET; 1010 } 1007 goto Lerr; 1011 1008 1012 1009 /* If any changes, do Copy On Write … … 1014 1011 if (ex != ekey) 1015 1012 { 1016 if (!keysx) 1017 { keysx = new Expressions(); 1018 keysx->setDim(keys->dim); 1019 for (size_t j = 0; j < i; j++) 1020 { 1021 keysx->data[j] = keys->data[j]; 1022 } 1023 } 1013 if (keysx == keys) 1014 keysx = (Expressions *)keys->copy(); 1024 1015 keysx->data[i] = (void *)ex; 1025 1016 } … … 1027 1018 ex = evalue->interpret(istate); 1028 1019 if (ex == EXP_CANT_INTERPRET) 1029 { delete keysx; 1030 delete valuesx; 1031 return EXP_CANT_INTERPRET; 1032 } 1020 goto Lerr; 1033 1021 1034 1022 /* If any changes, do Copy On Write … … 1036 1024 if (ex != evalue) 1037 1025 { 1038 if (!valuesx) 1039 { valuesx = new Expressions(); 1040 valuesx->setDim(values->dim); 1041 for (size_t j = 0; j < i; j++) 1042 { 1043 valuesx->data[j] = values->data[j]; 1044 } 1045 } 1026 if (valuesx == values) 1027 valuesx = (Expressions *)values->copy(); 1046 1028 valuesx->data[i] = (void *)ex; 1047 1029 } 1048 1030 } 1049 if (keysx || valuesx) 1050 { 1051 if (keysx) 1052 expandTuples(keysx); 1053 if (valuesx) 1054 expandTuples(valuesx); 1055 if ((keysx && keysx->dim != keys->dim) || 1056 (valuesx && valuesx->dim != values->dim)) 1057 { delete keysx; 1058 delete valuesx; 1059 return EXP_CANT_INTERPRET; 1060 } 1061 AssocArrayLiteralExp *ae = new AssocArrayLiteralExp(loc, 1062 keysx ? keysx : keys, valuesx ? valuesx : values); 1031 if (keysx != keys) 1032 expandTuples(keysx); 1033 if (valuesx != values) 1034 expandTuples(valuesx); 1035 if (keysx->dim != valuesx->dim) 1036 goto Lerr; 1037 1038 /* Remove duplicate keys 1039 */ 1040 for (size_t i = 1; i < keysx->dim; i++) 1041 { Expression *ekey = (Expression *)keysx->data[i - 1]; 1042 1043 for (size_t j = i; j < keysx->dim; j++) 1044 { Expression *ekey2 = (Expression *)keysx->data[j]; 1045 Expression *ex = Equal(TOKequal, Type::tbool, ekey, ekey2); 1046 if (ex == EXP_CANT_INTERPRET) 1047 goto Lerr; 1048 if (ex->isBool(TRUE)) // if a match 1049 { 1050 // Remove ekey 1051 if (keysx == keys) 1052 keysx = (Expressions *)keys->copy(); 1053 if (valuesx == values) 1054 valuesx = (Expressions *)values->copy(); 1055 keysx->remove(i - 1); 1056 valuesx->remove(i - 1); 1057 i -= 1; // redo the i'th iteration 1058 break; 1059 } 1060 } 1061 } 1062 1063 if (keysx != keys || valuesx != values) 1064 { 1065 AssocArrayLiteralExp *ae; 1066 ae = new AssocArrayLiteralExp(loc, keysx, valuesx); 1063 1067 ae->type = type; 1064 1068 return ae; 1065 1069 } 1066 1070 return this; 1071 1072 Lerr: 1073 if (keysx != keys) 1074 delete keysx; 1075 if (valuesx != values) 1076 delete values; 1077 return EXP_CANT_INTERPRET; 1067 1078 } 1068 1079 … … 1201 1212 } 1202 1213 } 1203 if (e1 != EXP_CANT_INTERPRET && e1->op == TOKvar) 1214 if (e1 == EXP_CANT_INTERPRET) 1215 return e1; 1216 if (e1->op == TOKvar) 1204 1217 { 1205 1218 VarExp *ve = (VarExp *)e1; … … 1460 1473 if (e1 == EXP_CANT_INTERPRET) 1461 1474 goto Lcant; 1462 if (e1->op == TOKstring || e1->op == TOKarrayliteral )1475 if (e1->op == TOKstring || e1->op == TOKarrayliteral || e1->op == TOKassocarrayliteral) 1463 1476 { 1464 1477 e = ArrayLength(type, e1); … … 1484 1497 goto Lcant; 1485 1498 1486 /* Set the $ variable 1487 */ 1488 e = ArrayLength(Type::tsize_t, e1); 1489 if (e == EXP_CANT_INTERPRET) 1490 goto Lcant; 1491 if (lengthVar) 1492 lengthVar->value = e; 1499 if (op == TOKstring || op == TOKarrayliteral) 1500 { 1501 /* Set the $ variable 1502 */ 1503 e = ArrayLength(Type::tsize_t, e1); 1504 if (e == EXP_CANT_INTERPRET) 1505 goto Lcant; 1506 if (lengthVar) 1507 lengthVar->value = e; 1508 } 1493 1509 1494 1510 e2 = this->e2->interpret(istate); branches/dmdfe/link.c
r458 r523 1 1 2 2 3 // Copyright (c) 1999-200 6by Digital Mars3 // Copyright (c) 1999-2007 by Digital Mars 4 4 // All Rights Reserved 5 5 // written by Walter Bright … … 144 144 p = cmdbuf.toChars(); 145 145 146 FileName *lnkfilename = NULL; 147 size_t plen = strlen(p); 148 if (plen > 7000) 149 { 150 lnkfilename = FileName::forceExt(global.params.exefile, "lnk"); 151 File flnk(lnkfilename); 152 flnk.setbuffer(p, plen); 153 flnk.ref = 1; 154 if (flnk.write()) 155 error("error writing file %s", lnkfilename); 156 if (lnkfilename->len() < plen) 157 sprintf(p, "@%s", lnkfilename->toChars()); 158 } 159 146 160 char *linkcmd = getenv("LINKCMD"); 147 161 if (!linkcmd) 148 162 linkcmd = "link"; 149 163 status = executecmd(linkcmd, p, 1); 164 if (lnkfilename) 165 { 166 remove(lnkfilename->toChars()); 167 delete lnkfilename; 168 } 150 169 return status; 151 170 #else … … 204 223 argv.push((void *)"-m32"); 205 224 206 argv.push((void *)"-lphobos"); // turns into /usr/lib/libphobos.a207 argv.push((void *)"-lpthread");208 argv.push((void *)"-lm");209 210 225 if (0 && global.params.exefile) 211 226 { … … 224 239 225 240 for (i = 0; i < global.params.linkswitches->dim; i++) 226 { 227 argv.push((void *)"-Xlinker"); 228 argv.push((void *) global.params.linkswitches->data[i]); 229 } 241 { char *p = (char *)global.params.linkswitches->data[i]; 242 if (!p || !p[0] || !(p[0] == '-' && p[1] == 'l')) 243 // Don't need -Xlinker if switch starts with -l 244 argv.push((void *)"-Xlinker"); 245 argv.push((void *) p); 246 } 247 248 /* Standard libraries must go after user specified libraries 249 * passed with -l. 250 */ 251 argv.push((void *)"-lphobos"); // turns into /usr/lib/libphobos.a 252 argv.push((void *)"-lpthread"); 253 argv.push((void *)"-lm"); 230 254 231 255 if (!global.params.quiet) branches/dmdfe/mars.c
r460 r523 56 56 copyright = "Copyright (c) 1999-2007 by Digital Mars"; 57 57 written = "written by Walter Bright"; 58 version = "v1.01 2";58 version = "v1.013"; 59 59 global.structalign = 8; 60 60 branches/dmdfe/mtype.c
r458 r523 1680 1680 } 1681 1681 1682 Expression *semanticLength(Scope *sc, TupleDeclaration *s, Expression *exp) 1683 { 1684 ScopeDsymbol *sym = new ArrayScopeSymbol(s); 1685 sym->parent = sc->scopesym; 1686 sc = sc->push(sym); 1687 1688 exp = exp->semantic(sc); 1689 1690 sc->pop(); 1691 return exp; 1692 } 1693 1682 1694 void TypeSArray::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) 1683 1695 { … … 1748 1760 { 1749 1761 //printf("TypeSArray::semantic() %s\n", toChars()); 1762 1763 Type *t; 1764 Expression *e; 1765 Dsymbol *s; 1766 next->resolve(loc, sc, &e, &t, &s); 1767 if (dim && s && s->isTupleDeclaration()) 1768 { TupleDeclaration *sd = s->isTupleDeclaration(); 1769 1770 dim = semanticLength(sc, sd, dim); 1771 dim = dim->optimize(WANTvalue | WANTinterpret); 1772 uinteger_t d = dim->toUInteger(); 1773 1774 if (d >= sd->objects->dim) 1775 { error(loc, "tuple index %ju exceeds %u", d, sd->objects->dim); 1776 return Type::terror; 1777 } 1778 Object *o = (Object *)sd->objects->data[(size_t)d]; 1779 if (o->dyncast() != DYNCAST_TYPE) 1780 { error(loc, "%s is not a type", toChars()); 1781 return Type::terror; 1782 } 1783 t = (Type *)o; 1784 return t; 1785 } 1786 1750 1787 next = next->semantic(loc,sc); 1751 1788 Type *tbn = next->toBasetype(); … … 1765 1802 goto Loverflow; 1766 1803 1767 if (tbn->ty == Tbit && (d2 + 31) < d2) 1768 goto Loverflow; 1769 else if (tbn->isintegral() || 1804 if (tbn->isintegral() || 1770 1805 tbn->isfloating() || 1771 1806 tbn->ty == Tpointer || … … 2207 2242 FuncDeclaration *fd; 2208 2243 Expressions *arguments; 2209 char aakeys[7+3*sizeof(int)+1];2210 2244 int size = key->size(e->loc); 2211 2245 2212 2246 assert(size); 2213 #if 0 2214 if (size == 1 || size == 2 || size == 4 || size == 8) 2215 { 2216 sprintf(aakeys, "_aaKeys%d", size); 2217 size = 0; 2218 } 2219 else 2220 #endif 2221 strcpy(aakeys, "_aaKeys"); 2222 fd = FuncDeclaration::genCfunc(Type::tindex, aakeys); 2247 fd = FuncDeclaration::genCfunc(Type::tindex, "_aaKeys"); 2223 2248 ec = new VarExp(0, fd); 2224 2249 arguments = new Expressions(); 2225 2250 arguments->push(e); 2226 if (size) 2227 arguments->push(new IntegerExp(0, size, Type::tsize_t)); 2251 arguments->push(new IntegerExp(0, size, Type::tsize_t)); 2228 2252 e = new CallExp(e->loc, ec, arguments); 2229 2253 e->type = index->arrayOf(); branches/dmdfe/opover.c
r458 r523 1 1 2 2 // Compiler implementation of the D programming language 3 // Copyright (c) 1999-200 6by Digital Mars3 // Copyright (c) 1999-2007 by Digital Mars 4 4 // All Rights Reserved 5 5 // written by Walter Bright … … 27 27 #endif 28 28 29 #include "port.h"29 //#include "port.h" 30 30 #include "mtype.h" 31 31 #include "init.h" branches/dmdfe/optimize.c
r460 r523 265 265 //printf("CastExp::optimize(result = %d) %s\n", result, toChars()); 266 266 //printf("from %s to %s\n", type->toChars(), to->toChars()); 267 //printf("from %s\n", type->toChars()); 267 268 //printf("type = %p\n", type); 268 269 assert(type); 269 270 e1 = e1->optimize(result); 270 enum TOK op1 = e1->op; 271 272 e1 = e1->optimize(result); 273 if (result & WANTinterpret) 274 e1 = fromConstInitializer(e1); 275 271 276 if ((e1->op == TOKstring || e1->op == TOKarrayliteral) && 272 277 (type->ty == Tpointer || type->ty == Tarray) && … … 277 282 return e1; 278 283 } 284 /* The first test here is to prevent infinite loops 285 */ 286 if (op1 != TOKarrayliteral && e1->op == TOKarrayliteral) 287 return e1->castTo(NULL, to); 279 288 if (e1->op == TOKnull && 280 289 (type->ty == Tpointer || type->ty == Tclass)) … … 520 529 e1 = e1->optimize(WANTvalue | (result & WANTinterpret)); 521 530 e = this; 522 if (e1->op == TOKstring || e1->op == TOKarrayliteral )531 if (e1->op == TOKstring || e1->op == TOKarrayliteral || e1->op == TOKassocarrayliteral) 523 532 { 524 533 e = ArrayLength(type, e1); branches/dmdfe/parse.c
r458 r523 192 192 193 193 case TOKinvariant: 194 #if 1 195 s = parseInvariant(); 196 #else 194 197 if (peek(&token)->value == TOKlcurly) 195 198 s = parseInvariant(); … … 199 202 goto Lstc; 200 203 } 204 #endif 201 205 break; 202 206 … … 268 272 case TOKsynchronized: stc |= STCsynchronized; goto Lstc; 269 273 case TOKdeprecated: stc |= STCdeprecated; goto Lstc; 270 case TOKinvariant: stc |= STCinvariant; goto Lstc;274 //case TOKinvariant: stc |= STCinvariant; goto Lstc; 271 275 default: 272 276 break; … … 2002 2006 } 2003 2007 v->storage_class = storage_class; 2004 a->push(v); 2008 if (link == linkage) 2009 a->push(v); 2010 else 2011 { 2012 Array *ax = new Array(); 2013 ax->push(v); 2014 Dsymbol *s = new LinkDeclaration(link, ax); 2015 a->push(s); 2016 } 2005 2017 switch (token.value) 2006 2018 { case TOKsemicolon: … … 2062 2074 v = new VarDeclaration(loc, t, ident, init); 2063 2075 v->storage_class = storage_class; 2064 a->push(v); 2076 if (link == linkage) 2077 a->push(v); 2078 else 2079 { 2080 Array *ax = new Array(); 2081 ax->push(v); 2082 Dsymbol *s = new LinkDeclaration(link, ax); 2083 a->push(s); 2084 } 2065 2085 switch (token.value) 2066 2086 { case TOKsemicolon: … … 2328 2348 case TOKvoid: 2329 2349 t = peek(&token); 2330 if (t->value == TOKsemicolon )2350 if (t->value == TOKsemicolon || t->value == TOKcomma) 2331 2351 { 2332 2352 nextToken(); … … 3962 3982 3963 3983 case TOKlbracket: 3964 { Expressions *elements = parseArguments(); 3965 3966 e = new ArrayLiteralExp(loc, elements); 3984 { /* Parse array literals and associative array literals: 3985 * [ value, value, value ... ] 3986 * [ key:value, key:value, key:value ... ] 3987 */ 3988 Expressions *values = new Expressions(); 3989 Expressions *keys = NULL; 3990 3991 nextToken(); 3992 if (token.value != TOKrbracket) 3993 { 3994 while (1) 3995 { 3996 Expression *e = parseAssignExp(); 3997 if (token.value == TOKcolon && (keys || values->dim == 0)) 3998 { nextToken(); 3999 if (!keys) 4000 keys = new Expressions(); 4001 keys->push(e); 4002 e = parseAssignExp(); 4003 } 4004 else if (keys) 4005 { error("'key:value' expected for associative array literal"); 4006 delete keys; 4007 keys = NULL; 4008 } 4009 values->push(e); 4010 if (token.value == TOKrbracket) 4011 break; 4012 check(TOKcomma); 4013 } 4014 } 4015 check(TOKrbracket); 4016 4017 if (keys) 4018 e = new AssocArrayLiteralExp(loc, keys, values); 4019 else 4020 e = new ArrayLiteralExp(loc, values); 3967 4021 break; 3968 4022 } branches/dmdfe/port.h
r360 r523 35 35 static double dbl_min; 36 36 37 static int isnan(double); 38 static int isfinite(double); 39 static int isinfinity(double); 40 static int signbit(double); 37 #if __GNUC__ 38 // These conflict with macros in math.h, should rename them 39 #undef isnan 40 #undef isfinite 41 #undef isinfinity 42 #undef signbit 43 #endif 44 static int isNan(double); 45 static int isFinite(double); 46 static int isInfinity(double); 47 static int Signbit(double); 41 48 42 49 static double floor(double); branches/dmdfe/statement.c
r458 r523 2429 2429 fd->nrvo_can = 0; 2430 2430 } 2431 else 2432 fd->nrvo_can = 0; 2431 2433 2432 2434 if (fd->returnLabel && tbret->ty != Tvoid)
