Mercurial > projects > ldc
comparison dmd/expression.c @ 1604:2afcaab30a6a
Merge DMD r250: harmonize
---
dmd/expression.c | 133 ++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 95 insertions(+), 38 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:19 -0300 |
parents | def7a1d494fd |
children | 1d5721f9ae18 |
comparison
equal
deleted
inserted
replaced
1603:eae495e6ae8d | 1604:2afcaab30a6a |
---|---|
459 return; // empty tuple, no more arguments | 459 return; // empty tuple, no more arguments |
460 arg = (Expression *)exps->data[i]; | 460 arg = (Expression *)exps->data[i]; |
461 } | 461 } |
462 } | 462 } |
463 } | 463 } |
464 } | |
465 | |
466 Expressions *arrayExpressionToCommonType(Scope *sc, Expressions *exps, Type **pt) | |
467 { | |
468 #if DMDV1 | |
469 /* The first element sets the type | |
470 */ | |
471 Type *t0 = NULL; | |
472 for (size_t i = 0; i < exps->dim; i++) | |
473 { Expression *e = (Expression *)exps->data[i]; | |
474 | |
475 if (!e->type) | |
476 { error("%s has no value", e->toChars()); | |
477 e = new ErrorExp(); | |
478 } | |
479 e = resolveProperties(sc, e); | |
480 | |
481 if (!t0) | |
482 t0 = e->type; | |
483 else | |
484 e = e->implicitCastTo(sc, t0); | |
485 exps->data[i] = (void *)e; | |
486 } | |
487 | |
488 if (!t0) | |
489 t0 = Type::tvoid; | |
490 if (pt) | |
491 *pt = t0; | |
492 | |
493 // Eventually, we want to make this copy-on-write | |
494 return exps; | |
495 #endif | |
496 #if DMDV2 | |
497 /* The type is determined by applying ?: to each pair. | |
498 */ | |
499 IntegerExp integerexp(0); | |
500 CondExp condexp(0, &integerexp, NULL, NULL); | |
501 | |
502 Type *t0 = NULL; | |
503 Expression *e0; | |
504 int j0; | |
505 for (size_t i = 0; i < exps->dim; i++) | |
506 { Expression *e = (Expression *)exps->data[i]; | |
507 | |
508 e = resolveProperties(sc, e); | |
509 if (!e->type) | |
510 { error("%s has no value", e->toChars()); | |
511 e = new ErrorExp(); | |
512 } | |
513 | |
514 if (t0) | |
515 { if (t0 != e->type) | |
516 { | |
517 /* This applies ?: to merge the types. It's backwards; | |
518 * ?: should call this function to merge types. | |
519 */ | |
520 condexp.type = NULL; | |
521 condexp.e1 = e0; | |
522 condexp.e2 = e; | |
523 condexp.semantic(sc); | |
524 exps->data[j0] = (void *)condexp.e1; | |
525 e = condexp.e2; | |
526 t0 = e->type; | |
527 } | |
528 } | |
529 else | |
530 { j0 = i; | |
531 e0 = e; | |
532 t0 = e->type; | |
533 } | |
534 exps->data[i] = (void *)e; | |
535 } | |
536 | |
537 if (t0) | |
538 { | |
539 for (size_t i = 0; i < exps->dim; i++) | |
540 { Expression *e = (Expression *)exps->data[i]; | |
541 e = e->implicitCastTo(sc, t0); | |
542 exps->data[i] = (void *)e; | |
543 } | |
544 } | |
545 else | |
546 t0 = Type::tvoid; // [] is typed as void[] | |
547 if (pt) | |
548 *pt = t0; | |
549 | |
550 // Eventually, we want to make this copy-on-write | |
551 return exps; | |
552 #endif | |
464 } | 553 } |
465 | 554 |
466 /**************************************** | 555 /**************************************** |
467 * Preprocess arguments to function. | 556 * Preprocess arguments to function. |
468 */ | 557 */ |
2982 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | 3071 arraySyntaxCopy(keys), arraySyntaxCopy(values)); |
2983 } | 3072 } |
2984 | 3073 |
2985 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | 3074 Expression *AssocArrayLiteralExp::semantic(Scope *sc) |
2986 { Expression *e; | 3075 { Expression *e; |
2987 Type *tkey = NULL; | |
2988 Type *tvalue = NULL; | |
2989 | 3076 |
2990 #if LOGSEMANTIC | 3077 #if LOGSEMANTIC |
2991 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | 3078 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); |
2992 #endif | 3079 #endif |
2993 | 3080 |
2994 // Run semantic() on each element | 3081 // Run semantic() on each element |
2995 for (size_t i = 0; i < keys->dim; i++) | 3082 arrayExpressionSemantic(keys, sc); |
2996 { Expression *key = (Expression *)keys->data[i]; | 3083 arrayExpressionSemantic(values, sc); |
2997 Expression *value = (Expression *)values->data[i]; | |
2998 | |
2999 key = key->semantic(sc); | |
3000 value = value->semantic(sc); | |
3001 | |
3002 keys->data[i] = (void *)key; | |
3003 values->data[i] = (void *)value; | |
3004 } | |
3005 expandTuples(keys); | 3084 expandTuples(keys); |
3006 expandTuples(values); | 3085 expandTuples(values); |
3007 if (keys->dim != values->dim) | 3086 if (keys->dim != values->dim) |
3008 { | 3087 { |
3009 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | 3088 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); |
3010 keys->setDim(0); | 3089 keys->setDim(0); |
3011 values->setDim(0); | 3090 values->setDim(0); |
3012 } | 3091 } |
3013 for (size_t i = 0; i < keys->dim; i++) | 3092 |
3014 { Expression *key = (Expression *)keys->data[i]; | 3093 Type *tkey = NULL; |
3015 Expression *value = (Expression *)values->data[i]; | 3094 Type *tvalue = NULL; |
3016 | 3095 keys = arrayExpressionToCommonType(sc, keys, &tkey); |
3017 if (!key->type) | 3096 values = arrayExpressionToCommonType(sc, values, &tvalue); |
3018 error("%s has no value", key->toChars()); | 3097 |
3019 if (!value->type) | |
3020 error("%s has no value", value->toChars()); | |
3021 key = resolveProperties(sc, key); | |
3022 value = resolveProperties(sc, value); | |
3023 | |
3024 if (!tkey) | |
3025 tkey = key->type; | |
3026 else | |
3027 key = key->implicitCastTo(sc, tkey); | |
3028 keys->data[i] = (void *)key; | |
3029 | |
3030 if (!tvalue) | |
3031 tvalue = value->type; | |
3032 else | |
3033 value = value->implicitCastTo(sc, tvalue); | |
3034 values->data[i] = (void *)value; | |
3035 } | |
3036 | |
3037 if (!tkey) | |
3038 tkey = Type::tvoid; | |
3039 if (!tvalue) | |
3040 tvalue = Type::tvoid; | |
3041 type = new TypeAArray(tvalue, tkey); | 3098 type = new TypeAArray(tvalue, tkey); |
3042 type = type->semantic(loc, sc); | 3099 type = type->semantic(loc, sc); |
3043 return this; | 3100 return this; |
3044 } | 3101 } |
3045 | 3102 |