comparison gen/llvmhelpers.cpp @ 719:7261ff0f95ff

Implemented first class delegates. closes #101
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Wed, 22 Oct 2008 21:50:08 +0200
parents 30b42a283c8e
children 46d0755451a4
comparison
equal deleted inserted replaced
718:72ee105be27b 719:7261ff0f95ff
450 } else { 450 } else {
451 assert(0 && "Unimplemented static array assign!"); 451 assert(0 && "Unimplemented static array assign!");
452 } 452 }
453 } 453 }
454 else if (t->ty == Tdelegate) { 454 else if (t->ty == Tdelegate) {
455 if (rhs->isNull()) 455 LLValue* l = lhs->getLVal();
456 DtoAggrZeroInit(lhs->getLVal()); 456 LLValue* r = rhs->getRVal();
457 else { 457 if (Logger::enabled())
458 LLValue* l = lhs->getLVal(); 458 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n';
459 LLValue* r = rhs->getRVal(); 459 DtoStore(r, l);
460 if (Logger::enabled())
461 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n';
462 DtoAggrCopy(l, r);
463 }
464 } 460 }
465 else if (t->ty == Tclass) { 461 else if (t->ty == Tclass) {
466 assert(t2->ty == Tclass); 462 assert(t2->ty == Tclass);
467 LLValue* l = lhs->getLVal(); 463 LLValue* l = lhs->getLVal();
468 LLValue* r = rhs->getRVal(); 464 LLValue* r = rhs->getRVal();
692 return new DImValue(to, rval); 688 return new DImValue(to, rval);
693 } 689 }
694 690
695 DValue* DtoCastDelegate(Loc& loc, DValue* val, Type* to) 691 DValue* DtoCastDelegate(Loc& loc, DValue* val, Type* to)
696 { 692 {
697 LLValue* res = 0; 693 if (to->toBasetype()->ty == Tdelegate)
698 to = to->toBasetype(); 694 {
699 695 return DtoPaintType(loc, val, to);
700 if (to->ty == Tdelegate)
701 {
702 const LLType* toll = getPtrToType(DtoType(to));
703 res = DtoBitCast(val->getRVal(), toll);
704 } 696 }
705 else 697 else
706 { 698 {
707 error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars()); 699 error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars());
708 fatal(); 700 fatal();
709 } 701 }
710
711 return new DImValue(to, res);
712 } 702 }
713 703
714 DValue* DtoCast(Loc& loc, DValue* val, Type* to) 704 DValue* DtoCast(Loc& loc, DValue* val, Type* to)
715 { 705 {
716 Type* fromtype = val->getType()->toBasetype(); 706 Type* fromtype = val->getType()->toBasetype();
769 LLValue *len, *ptr; 759 LLValue *len, *ptr;
770 len = DtoArrayLen(val); 760 len = DtoArrayLen(val);
771 ptr = DtoArrayPtr(val); 761 ptr = DtoArrayPtr(val);
772 ptr = DtoBitCast(ptr, DtoType(elem)); 762 ptr = DtoBitCast(ptr, DtoType(elem));
773 return new DImValue(to, DtoAggrPair(len, ptr, "tmp")); 763 return new DImValue(to, DtoAggrPair(len, ptr, "tmp"));
764 }
765 }
766 else if (from->ty == Tdelegate)
767 {
768 Type* dgty = to->toBasetype();
769 assert(dgty->ty == Tdelegate);
770 if (val->isLVal())
771 {
772 LLValue* ptr = val->getLVal();
773 assert(isaPointer(ptr));
774 ptr = DtoBitCast(ptr, getPtrToType(DtoType(dgty)));
775 if (Logger::enabled())
776 Logger::cout() << "dg ptr: " << *ptr << '\n';
777 return new DVarValue(to, ptr);
778 }
779 else
780 {
781 LLValue* dg = val->getRVal();
782 LLValue* context = gIR->ir->CreateExtractValue(dg, 0, ".context");
783 LLValue* funcptr = gIR->ir->CreateExtractValue(dg, 1, ".funcptr");
784 funcptr = DtoBitCast(funcptr, DtoType(dgty)->getContainedType(1));
785 LLValue* aggr = DtoAggrPair(context, funcptr, "tmp");
786 if (Logger::enabled())
787 Logger::cout() << "dg: " << *aggr << '\n';
788 return new DImValue(to, aggr);
774 } 789 }
775 } 790 }
776 else if (from->ty == Tpointer || from->ty == Tclass || from->ty == Taarray) 791 else if (from->ty == Tpointer || from->ty == Tclass || from->ty == Taarray)
777 { 792 {
778 Type* b = to->toBasetype(); 793 Type* b = to->toBasetype();