Mercurial > projects > ldc
comparison dmd2/init.c @ 1526:54b3c1394d62
Merged dmdfe 2.031.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 07 Jul 2009 02:26:11 +0100 |
parents | 638d16625da2 |
children | e4f7b5d9c68a |
comparison
equal
deleted
inserted
replaced
1525:d28cd7c45267 | 1526:54b3c1394d62 |
---|---|
394 | 394 |
395 Expression *ArrayInitializer::toExpression() | 395 Expression *ArrayInitializer::toExpression() |
396 { Expressions *elements; | 396 { Expressions *elements; |
397 Expression *e; | 397 Expression *e; |
398 | 398 |
399 //printf("ArrayInitializer::toExpression()\n"); | 399 //printf("ArrayInitializer::toExpression(), dim = %d\n", dim); |
400 //static int i; if (++i == 2) halt(); | 400 //static int i; if (++i == 2) halt(); |
401 | |
402 size_t edim; | |
403 Type *t = NULL; | |
404 if (type) | |
405 { | |
406 t = type->toBasetype(); | |
407 switch (t->ty) | |
408 { | |
409 case Tsarray: | |
410 edim = ((TypeSArray *)t)->dim->toInteger(); | |
411 break; | |
412 | |
413 case Tpointer: | |
414 case Tarray: | |
415 edim = dim; | |
416 break; | |
417 | |
418 default: | |
419 assert(0); | |
420 } | |
421 } | |
422 else | |
423 edim = value.dim; | |
424 | |
401 elements = new Expressions(); | 425 elements = new Expressions(); |
402 for (size_t i = 0; i < value.dim; i++) | 426 elements->setDim(edim); |
427 for (size_t i = 0, j = 0; i < value.dim; i++, j++) | |
403 { | 428 { |
404 if (index.data[i]) | 429 if (index.data[i]) |
405 goto Lno; | 430 j = ((Expression *)index.data[i])->toInteger(); |
431 assert(j < edim); | |
406 Initializer *iz = (Initializer *)value.data[i]; | 432 Initializer *iz = (Initializer *)value.data[i]; |
407 if (!iz) | 433 if (!iz) |
408 goto Lno; | 434 goto Lno; |
409 Expression *ex = iz->toExpression(); | 435 Expression *ex = iz->toExpression(); |
410 if (!ex) | 436 if (!ex) |
411 goto Lno; | 437 { |
412 elements->push(ex); | 438 goto Lno; |
413 } | 439 } |
414 e = new ArrayLiteralExp(loc, elements); | 440 elements->data[j] = ex; |
441 } | |
442 | |
443 /* Fill in any missing elements with the default initializer | |
444 */ | |
445 { | |
446 Expression *init = NULL; | |
447 for (size_t i = 0; i < edim; i++) | |
448 { | |
449 if (!elements->data[i]) | |
450 { | |
451 if (!type) | |
452 goto Lno; | |
453 if (!init) | |
454 init = ((TypeNext *)t)->next->defaultInit(); | |
455 elements->data[i] = init; | |
456 } | |
457 } | |
458 | |
459 Expression *e = new ArrayLiteralExp(loc, elements); | |
415 e->type = type; | 460 e->type = type; |
416 return e; | 461 return e; |
462 } | |
417 | 463 |
418 Lno: | 464 Lno: |
419 delete elements; | 465 delete elements; |
420 error(loc, "array initializers as expressions are not allowed"); | 466 error(loc, "array initializers as expressions are not allowed"); |
421 return NULL; | 467 return NULL; |
464 } | 510 } |
465 | 511 |
466 | 512 |
467 Type *ArrayInitializer::inferType(Scope *sc) | 513 Type *ArrayInitializer::inferType(Scope *sc) |
468 { | 514 { |
515 //printf("ArrayInitializer::inferType() %s\n", toChars()); | |
516 type = Type::terror; | |
469 for (size_t i = 0; i < value.dim; i++) | 517 for (size_t i = 0; i < value.dim; i++) |
470 { | 518 { |
471 if (index.data[i]) | 519 if (index.data[i]) |
472 goto Lno; | 520 goto Lno; |
473 } | 521 } |
474 if (value.dim) | 522 for (size_t i = 0; i < value.dim; i++) |
475 { | 523 { |
476 Initializer *iz = (Initializer *)value.data[0]; | 524 Initializer *iz = (Initializer *)value.data[i]; |
477 if (iz) | 525 if (iz) |
478 { Type *t = iz->inferType(sc); | 526 { Type *t = iz->inferType(sc); |
479 t = new TypeSArray(t, new IntegerExp(value.dim)); | 527 if (i == 0) |
480 t = t->semantic(loc, sc); | 528 { t = new TypeSArray(t, new IntegerExp(value.dim)); |
481 return t; | 529 t = t->semantic(loc, sc); |
482 } | 530 type = t; |
483 } | 531 } |
532 } | |
533 } | |
534 return type; | |
484 | 535 |
485 Lno: | 536 Lno: |
486 error(loc, "cannot infer type from this array initializer"); | 537 error(loc, "cannot infer type from this array initializer"); |
487 return Type::terror; | 538 return Type::terror; |
488 } | 539 } |