comparison dmd/init.c @ 1587:def7a1d494fd

Merge DMD 1.051
author Christian Kamm <kamm incasoftware de>
date Fri, 06 Nov 2009 23:58:01 +0100
parents 8026319762be
children 207a8a438dea
comparison
equal deleted inserted replaced
1586:7f728c52e63c 1587:def7a1d494fd
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 if (type == Type::terror)
407 return new ErrorExp();
408
409 t = type->toBasetype();
410 switch (t->ty)
411 {
412 case Tsarray:
413 edim = ((TypeSArray *)t)->dim->toInteger();
414 break;
415
416 case Tpointer:
417 case Tarray:
418 edim = dim;
419 break;
420
421 default:
422 assert(0);
423 }
424 }
425 else
426 {
427 edim = value.dim;
428 for (size_t i = 0, j = 0; i < value.dim; i++, j++)
429 {
430 if (index.data[i])
431 j = ((Expression *)index.data[i])->toInteger();
432 if (j >= edim)
433 edim = j + 1;
434 }
435 }
436
401 elements = new Expressions(); 437 elements = new Expressions();
402 for (size_t i = 0; i < value.dim; i++) 438 elements->setDim(edim);
439 for (size_t i = 0, j = 0; i < value.dim; i++, j++)
403 { 440 {
404 if (index.data[i]) 441 if (index.data[i])
405 goto Lno; 442 j = ((Expression *)index.data[i])->toInteger();
443 assert(j < edim);
406 Initializer *iz = (Initializer *)value.data[i]; 444 Initializer *iz = (Initializer *)value.data[i];
407 if (!iz) 445 if (!iz)
408 goto Lno; 446 goto Lno;
409 Expression *ex = iz->toExpression(); 447 Expression *ex = iz->toExpression();
410 if (!ex) 448 if (!ex)
411 goto Lno; 449 {
412 elements->push(ex); 450 goto Lno;
413 } 451 }
414 e = new ArrayLiteralExp(loc, elements); 452 elements->data[j] = ex;
453 }
454
455 /* Fill in any missing elements with the default initializer
456 */
457 {
458 Expression *init = NULL;
459 for (size_t i = 0; i < edim; i++)
460 {
461 if (!elements->data[i])
462 {
463 if (!type)
464 goto Lno;
465 if (!init)
466 init = t->next->defaultInit();
467 elements->data[i] = init;
468 }
469 }
470
471 Expression *e = new ArrayLiteralExp(loc, elements);
415 e->type = type; 472 e->type = type;
416 return e; 473 return e;
474 }
417 475
418 Lno: 476 Lno:
419 delete elements; 477 delete elements;
420 error(loc, "array initializers as expressions are not allowed"); 478 error(loc, "array initializers as expressions are not allowed");
421 return NULL; 479 return new ErrorExp();
422 } 480 }
423
424 481
425 /******************************** 482 /********************************
426 * If possible, convert array initializer to associative array initializer. 483 * If possible, convert array initializer to associative array initializer.
427 */ 484 */
428 485
467 Type *ArrayInitializer::inferType(Scope *sc) 524 Type *ArrayInitializer::inferType(Scope *sc)
468 { 525 {
469 for (size_t i = 0; i < value.dim; i++) 526 for (size_t i = 0; i < value.dim; i++)
470 { 527 {
471 if (index.data[i]) 528 if (index.data[i])
472 goto Lno; 529 goto Laa;
473 } 530 }
474 if (value.dim) 531 if (value.dim)
475 { 532 {
476 Initializer *iz = (Initializer *)value.data[0]; 533 Initializer *iz = (Initializer *)value.data[0];
477 if (iz) 534 if (iz)
480 t = t->semantic(loc, sc); 537 t = t->semantic(loc, sc);
481 return t; 538 return t;
482 } 539 }
483 } 540 }
484 541
485 Lno: 542 Laa:
486 error(loc, "cannot infer type from this array initializer"); 543 /* It's possibly an associative array initializer
487 return Type::terror; 544 */
545 Initializer *iz = (Initializer *)value.data[0];
546 Expression *indexinit = (Expression *)index.data[0];
547 if (iz && indexinit)
548 { Type *t = iz->inferType(sc);
549 indexinit = indexinit->semantic(sc);
550 Type *indext = indexinit->type;
551 t = new TypeAArray(t, indext);
552 type = t->semantic(loc, sc);
553 }
554 else
555 error(loc, "cannot infer type from this array initializer");
556 return type;
488 } 557 }
489 558
490 559
491 void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs) 560 void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
492 { 561 {
567 { 636 {
568 //printf("ExpInitializer::inferType() %s\n", toChars()); 637 //printf("ExpInitializer::inferType() %s\n", toChars());
569 exp = exp->semantic(sc); 638 exp = exp->semantic(sc);
570 exp = resolveProperties(sc, exp); 639 exp = resolveProperties(sc, exp);
571 640
572 #if DMDV2
573 // Give error for overloaded function addresses 641 // Give error for overloaded function addresses
574 if (exp->op == TOKsymoff) 642 if (exp->op == TOKsymoff)
575 { SymOffExp *se = (SymOffExp *)exp; 643 { SymOffExp *se = (SymOffExp *)exp;
576 if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique()) 644 if (
645 #if DMDV2
646 se->hasOverloads &&
647 #else
648 se->var->isFuncDeclaration() &&
649 #endif
650 !se->var->isFuncDeclaration()->isUnique())
577 exp->error("cannot infer type from overloaded function symbol %s", exp->toChars()); 651 exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
578 } 652 }
579 #endif 653
654 // Give error for overloaded function addresses
655 if (exp->op == TOKdelegate)
656 { DelegateExp *se = (DelegateExp *)exp;
657 if (
658 se->func->isFuncDeclaration() &&
659 !se->func->isFuncDeclaration()->isUnique())
660 exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
661 }
580 662
581 Type *t = exp->type; 663 Type *t = exp->type;
582 if (!t) 664 if (!t)
583 t = Initializer::inferType(sc); 665 t = Initializer::inferType(sc);
584 return t; 666 return t;