comparison dmd2/mtype.c @ 1526:54b3c1394d62

Merged dmdfe 2.031.
author Robert Clipsham <robert@octarineparrot.com>
date Tue, 07 Jul 2009 02:26:11 +0100
parents a5526b7a5ae6
children e4f7b5d9c68a
comparison
equal deleted inserted replaced
1525:d28cd7c45267 1526:54b3c1394d62
1169 //next = next->merge(); 1169 //next = next->merge();
1170 toDecoBuffer(&buf, false); 1170 toDecoBuffer(&buf, false);
1171 sv = stringtable.update((char *)buf.data, buf.offset); 1171 sv = stringtable.update((char *)buf.data, buf.offset);
1172 if (sv->ptrvalue) 1172 if (sv->ptrvalue)
1173 { t = (Type *) sv->ptrvalue; 1173 { t = (Type *) sv->ptrvalue;
1174 #ifdef DEBUG
1175 if (!t->deco)
1176 printf("t = %s\n", t->toChars());
1177 #endif
1174 assert(t->deco); 1178 assert(t->deco);
1175 //printf("old value, deco = '%s' %p\n", t->deco, t->deco); 1179 //printf("old value, deco = '%s' %p\n", t->deco, t->deco);
1176 } 1180 }
1177 else 1181 else
1178 { 1182 {
1322 1326
1323 /******************************** 1327 /********************************
1324 * Determine if 'this' can be implicitly converted 1328 * Determine if 'this' can be implicitly converted
1325 * to type 'to'. 1329 * to type 'to'.
1326 * Returns: 1330 * Returns:
1327 * 0 can't convert 1331 * MATCHnomatch, MATCHconvert, MATCHconst, MATCHexact
1328 * 1 can convert using implicit conversions
1329 * 2 this and to are the same type
1330 */ 1332 */
1331 1333
1332 MATCH Type::implicitConvTo(Type *to) 1334 MATCH Type::implicitConvTo(Type *to)
1333 { 1335 {
1334 //printf("Type::implicitConvTo(this=%p, to=%p)\n", this, to); 1336 //printf("Type::implicitConvTo(this=%p, to=%p)\n", this, to);
1337 //printf("from: %s\n", toChars());
1338 //printf("to : %s\n", to->toChars());
1335 if (this == to) 1339 if (this == to)
1336 return MATCHexact; 1340 return MATCHexact;
1337 return MATCHnomatch; 1341 return MATCHnomatch;
1338 } 1342 }
1339 1343
1589 Type *Type::nextOf() 1593 Type *Type::nextOf()
1590 { 1594 {
1591 return NULL; 1595 return NULL;
1592 } 1596 }
1593 1597
1598 /****************************************
1599 * Return the mask that an integral type will
1600 * fit into.
1601 */
1602 uinteger_t Type::sizemask()
1603 { uinteger_t m;
1604
1605 switch (toBasetype()->ty)
1606 {
1607 case Tbool: m = 1; break;
1608 case Tchar:
1609 case Tint8:
1610 case Tuns8: m = 0xFF; break;
1611 case Twchar:
1612 case Tint16:
1613 case Tuns16: m = 0xFFFFUL; break;
1614 case Tdchar:
1615 case Tint32:
1616 case Tuns32: m = 0xFFFFFFFFUL; break;
1617 case Tint64:
1618 case Tuns64: m = 0xFFFFFFFFFFFFFFFFULL; break;
1619 default:
1620 assert(0);
1621 }
1622 return m;
1623 }
1624
1594 /* ============================= TypeNext =========================== */ 1625 /* ============================= TypeNext =========================== */
1595 1626
1596 TypeNext::TypeNext(TY ty, Type *next) 1627 TypeNext::TypeNext(TY ty, Type *next)
1597 : Type(ty) 1628 : Type(ty)
1598 { 1629 {
1608 } 1639 }
1609 1640
1610 void TypeNext::checkDeprecated(Loc loc, Scope *sc) 1641 void TypeNext::checkDeprecated(Loc loc, Scope *sc)
1611 { 1642 {
1612 Type::checkDeprecated(loc, sc); 1643 Type::checkDeprecated(loc, sc);
1613 next->checkDeprecated(loc, sc); 1644 if (next) // next can be NULL if TypeFunction and auto return type
1645 next->checkDeprecated(loc, sc);
1614 } 1646 }
1615 1647
1616 1648
1617 Type *TypeNext::reliesOnTident() 1649 Type *TypeNext::reliesOnTident()
1618 { 1650 {
1630 if (cto) 1662 if (cto)
1631 { assert(cto->mod == MODconst); 1663 { assert(cto->mod == MODconst);
1632 return cto; 1664 return cto;
1633 } 1665 }
1634 TypeNext *t = (TypeNext *)Type::makeConst(); 1666 TypeNext *t = (TypeNext *)Type::makeConst();
1635 if (ty != Tfunction && ty != Tdelegate && next->deco && 1667 if (ty != Tfunction && ty != Tdelegate &&
1668 (next->deco || next->ty == Tfunction) &&
1636 !next->isInvariant() && !next->isConst()) 1669 !next->isInvariant() && !next->isConst())
1637 { if (next->isShared()) 1670 { if (next->isShared())
1638 t->next = next->sharedConstOf(); 1671 t->next = next->sharedConstOf();
1639 else 1672 else
1640 t->next = next->constOf(); 1673 t->next = next->constOf();
1649 if (ito) 1682 if (ito)
1650 { assert(ito->isInvariant()); 1683 { assert(ito->isInvariant());
1651 return ito; 1684 return ito;
1652 } 1685 }
1653 TypeNext *t = (TypeNext *)Type::makeInvariant(); 1686 TypeNext *t = (TypeNext *)Type::makeInvariant();
1654 if (ty != Tfunction && ty != Tdelegate && next->deco && 1687 if (ty != Tfunction && ty != Tdelegate &&
1688 (next->deco || next->ty == Tfunction) &&
1655 !next->isInvariant()) 1689 !next->isInvariant())
1656 { t->next = next->invariantOf(); 1690 { t->next = next->invariantOf();
1657 } 1691 }
1658 return t; 1692 return t;
1659 } 1693 }
1664 if (sto) 1698 if (sto)
1665 { assert(sto->mod == MODshared); 1699 { assert(sto->mod == MODshared);
1666 return sto; 1700 return sto;
1667 } 1701 }
1668 TypeNext *t = (TypeNext *)Type::makeShared(); 1702 TypeNext *t = (TypeNext *)Type::makeShared();
1669 if (ty != Tfunction && ty != Tdelegate && next->deco && 1703 if (ty != Tfunction && ty != Tdelegate &&
1704 (next->deco || next->ty == Tfunction) &&
1670 !next->isInvariant() && !next->isShared()) 1705 !next->isInvariant() && !next->isShared())
1671 { 1706 {
1672 if (next->isConst()) 1707 if (next->isConst())
1673 t->next = next->sharedConstOf(); 1708 t->next = next->sharedConstOf();
1674 else 1709 else
1684 if (scto) 1719 if (scto)
1685 { assert(scto->mod == (MODshared | MODconst)); 1720 { assert(scto->mod == (MODshared | MODconst));
1686 return scto; 1721 return scto;
1687 } 1722 }
1688 TypeNext *t = (TypeNext *)Type::makeSharedConst(); 1723 TypeNext *t = (TypeNext *)Type::makeSharedConst();
1689 if (ty != Tfunction && ty != Tdelegate && next->deco && 1724 if (ty != Tfunction && ty != Tdelegate &&
1725 (next->deco || next->ty == Tfunction) &&
1690 !next->isInvariant() && !next->isSharedConst()) 1726 !next->isInvariant() && !next->isSharedConst())
1691 { 1727 {
1692 t->next = next->sharedConstOf(); 1728 t->next = next->sharedConstOf();
1693 } 1729 }
1694 //printf("TypeNext::makeSharedConst() returns %p, %s\n", t, t->toChars()); 1730 //printf("TypeNext::makeSharedConst() returns %p, %s\n", t, t->toChars());
2359 { 2395 {
2360 //printf("TypeBasic::implicitConvTo(%s) from %s\n", to->toChars(), toChars()); 2396 //printf("TypeBasic::implicitConvTo(%s) from %s\n", to->toChars(), toChars());
2361 if (this == to) 2397 if (this == to)
2362 return MATCHexact; 2398 return MATCHexact;
2363 2399
2400 #if DMDV2
2364 if (ty == to->ty) 2401 if (ty == to->ty)
2365 { 2402 {
2366 return (mod == to->mod) ? MATCHexact : MATCHconst; 2403 return (mod == to->mod) ? MATCHexact : MATCHconst;
2367 } 2404 }
2405 #endif
2368 2406
2369 if (ty == Tvoid || to->ty == Tvoid) 2407 if (ty == Tvoid || to->ty == Tvoid)
2370 return MATCHnomatch; 2408 return MATCHnomatch;
2371 if (1 || global.params.Dversion == 1) 2409 if (to->ty == Tbool)
2372 { 2410 return MATCHnomatch;
2373 if (to->ty == Tbool)
2374 return MATCHnomatch;
2375 }
2376 else
2377 {
2378 if (ty == Tbool || to->ty == Tbool)
2379 return MATCHnomatch;
2380 }
2381 if (!to->isTypeBasic()) 2411 if (!to->isTypeBasic())
2382 return MATCHnomatch; 2412 return MATCHnomatch;
2383 2413
2384 TypeBasic *tob = (TypeBasic *)to; 2414 TypeBasic *tob = (TypeBasic *)to;
2385 if (flags & TFLAGSintegral) 2415 if (flags & TFLAGSintegral)
2386 { 2416 {
2387 // Disallow implicit conversion of integers to imaginary or complex 2417 // Disallow implicit conversion of integers to imaginary or complex
2388 if (tob->flags & (TFLAGSimaginary | TFLAGScomplex)) 2418 if (tob->flags & (TFLAGSimaginary | TFLAGScomplex))
2389 return MATCHnomatch; 2419 return MATCHnomatch;
2390 2420
2391 // If converting to integral 2421 #if DMDV2
2392 if (0 && global.params.Dversion > 1 && tob->flags & TFLAGSintegral) 2422 // If converting from integral to integral
2423 if (1 && tob->flags & TFLAGSintegral)
2393 { d_uns64 sz = size(0); 2424 { d_uns64 sz = size(0);
2394 d_uns64 tosz = tob->size(0); 2425 d_uns64 tosz = tob->size(0);
2395 2426
2396 /* Can't convert to smaller size or, if same size, change sign 2427 /* Can't convert to smaller size
2397 */ 2428 */
2398 if (sz > tosz) 2429 if (sz > tosz)
2399 return MATCHnomatch; 2430 return MATCHnomatch;
2400 2431
2432 /* Can't change sign if same size
2433 */
2401 /*if (sz == tosz && (flags ^ tob->flags) & TFLAGSunsigned) 2434 /*if (sz == tosz && (flags ^ tob->flags) & TFLAGSunsigned)
2402 return MATCHnomatch;*/ 2435 return MATCHnomatch;*/
2403 } 2436 }
2437 #endif
2404 } 2438 }
2405 else if (flags & TFLAGSfloating) 2439 else if (flags & TFLAGSfloating)
2406 { 2440 {
2407 // Disallow implicit conversion of floating point to integer 2441 // Disallow implicit conversion of floating point to integer
2408 if (tob->flags & TFLAGSintegral) 2442 if (tob->flags & TFLAGSintegral)
3550 } 3584 }
3551 3585
3552 Type *TypePointer::semantic(Loc loc, Scope *sc) 3586 Type *TypePointer::semantic(Loc loc, Scope *sc)
3553 { 3587 {
3554 //printf("TypePointer::semantic()\n"); 3588 //printf("TypePointer::semantic()\n");
3589 if (deco)
3590 return this;
3555 Type *n = next->semantic(loc, sc); 3591 Type *n = next->semantic(loc, sc);
3556 switch (n->toBasetype()->ty) 3592 switch (n->toBasetype()->ty)
3557 { 3593 {
3558 case Ttuple: 3594 case Ttuple:
3559 error(loc, "can't have pointer to %s", n->toChars()); 3595 error(loc, "can't have pointer to %s", n->toChars());
3560 n = tint32; 3596 n = tint32;
3561 break; 3597 break;
3562 } 3598 }
3563 if (n != next) 3599 if (n != next)
3600 {
3564 deco = NULL; 3601 deco = NULL;
3602 }
3565 next = n; 3603 next = n;
3566 transitive(); 3604 transitive();
3567 return merge(); 3605 return merge();
3568 } 3606 }
3569 3607
3799 for (size_t i = 0; i < dim; i++) 3837 for (size_t i = 0; i < dim; i++)
3800 { Argument *arg1 = Argument::getNth(t1->parameters, i); 3838 { Argument *arg1 = Argument::getNth(t1->parameters, i);
3801 Argument *arg2 = Argument::getNth(t2->parameters, i); 3839 Argument *arg2 = Argument::getNth(t2->parameters, i);
3802 3840
3803 if (!arg1->type->equals(arg2->type)) 3841 if (!arg1->type->equals(arg2->type))
3804 goto Ldistinct; 3842 {
3843 #if 0 // turn on this for contravariant argument types, see bugzilla 3075
3844 // We can add const, but not subtract it
3845 if (arg2->type->implicitConvTo(arg1->type) < MATCHconst)
3846 #endif
3847 goto Ldistinct;
3848 }
3805 if ((arg1->storageClass & ~STCscope) != (arg2->storageClass & ~STCscope)) 3849 if ((arg1->storageClass & ~STCscope) != (arg2->storageClass & ~STCscope))
3806 inoutmismatch = 1; 3850 inoutmismatch = 1;
3807 // We can add scope, but not subtract it 3851 // We can add scope, but not subtract it
3808 if (!(arg1->storageClass & STCscope) && (arg2->storageClass & STCscope)) 3852 if (!(arg1->storageClass & STCscope) && (arg2->storageClass & STCscope))
3809 inoutmismatch = 1; 3853 inoutmismatch = 1;
4055 if (mod != this->mod) 4099 if (mod != this->mod)
4056 { 4100 {
4057 if (mod & MODconst) 4101 if (mod & MODconst)
4058 buf->writestring(" const"); 4102 buf->writestring(" const");
4059 if (mod & MODinvariant) 4103 if (mod & MODinvariant)
4060 buf->writestring(" invariant"); 4104 buf->writestring(" immutable");
4061 if (mod & MODshared) 4105 if (mod & MODshared)
4062 buf->writestring(" shared"); 4106 buf->writestring(" shared");
4063 } 4107 }
4064 if (ispure) 4108 if (ispure)
4065 buf->writestring(" pure"); 4109 buf->writestring(" pure");
4079 return this; 4123 return this;
4080 } 4124 }
4081 //printf("TypeFunction::semantic() this = %p\n", this); 4125 //printf("TypeFunction::semantic() this = %p\n", this);
4082 //printf("TypeFunction::semantic() %s, sc->stc = %x\n", toChars(), sc->stc); 4126 //printf("TypeFunction::semantic() %s, sc->stc = %x\n", toChars(), sc->stc);
4083 4127
4128 /* Copy in order to not mess up original.
4129 * This can produce redundant copies if inferring return type,
4130 * as semantic() will get called again on this.
4131 */
4084 TypeFunction *tf = (TypeFunction *)mem.malloc(sizeof(TypeFunction)); 4132 TypeFunction *tf = (TypeFunction *)mem.malloc(sizeof(TypeFunction));
4085 memcpy(tf, this, sizeof(TypeFunction)); 4133 memcpy(tf, this, sizeof(TypeFunction));
4086 if (parameters) 4134 if (parameters)
4087 { tf->parameters = (Arguments *)parameters->copy(); 4135 { tf->parameters = (Arguments *)parameters->copy();
4088 for (size_t i = 0; i < parameters->dim; i++) 4136 for (size_t i = 0; i < parameters->dim; i++)
4099 tf->isnothrow = TRUE; 4147 tf->isnothrow = TRUE;
4100 if (sc->stc & STCref) 4148 if (sc->stc & STCref)
4101 tf->isref = TRUE; 4149 tf->isref = TRUE;
4102 4150
4103 tf->linkage = sc->linkage; 4151 tf->linkage = sc->linkage;
4104 if (!tf->next) 4152 if (tf->next)
4105 { 4153 {
4106 assert(global.errors); 4154 tf->next = tf->next->semantic(loc,sc);
4107 tf->next = tvoid; 4155 if (tf->next->toBasetype()->ty == Tsarray)
4108 } 4156 { error(loc, "functions cannot return static array %s", tf->next->toChars());
4109 tf->next = tf->next->semantic(loc,sc); 4157 tf->next = Type::terror;
4110 if (tf->next->toBasetype()->ty == Tsarray) 4158 }
4111 { error(loc, "functions cannot return static array %s", tf->next->toChars()); 4159 if (tf->next->toBasetype()->ty == Tfunction)
4112 tf->next = Type::terror; 4160 { error(loc, "functions cannot return a function");
4113 } 4161 tf->next = Type::terror;
4114 if (tf->next->toBasetype()->ty == Tfunction) 4162 }
4115 { error(loc, "functions cannot return a function"); 4163 if (tf->next->toBasetype()->ty == Ttuple)
4116 tf->next = Type::terror; 4164 { error(loc, "functions cannot return a tuple");
4117 } 4165 tf->next = Type::terror;
4118 if (tf->next->toBasetype()->ty == Ttuple) 4166 }
4119 { error(loc, "functions cannot return a tuple"); 4167 if (tf->next->isauto() && !(sc->flags & SCOPEctor))
4120 tf->next = Type::terror; 4168 error(loc, "functions cannot return scope %s", tf->next->toChars());
4121 } 4169 }
4122 if (tf->next->isauto() && !(sc->flags & SCOPEctor))
4123 error(loc, "functions cannot return scope %s", tf->next->toChars());
4124 4170
4125 if (tf->parameters) 4171 if (tf->parameters)
4126 { size_t dim = Argument::dim(tf->parameters); 4172 { size_t dim = Argument::dim(tf->parameters);
4127 4173
4128 for (size_t i = 0; i < dim; i++) 4174 for (size_t i = 0; i < dim; i++)
4166 { dim = Argument::dim(tf->parameters); 4212 { dim = Argument::dim(tf->parameters);
4167 i--; 4213 i--;
4168 } 4214 }
4169 } 4215 }
4170 } 4216 }
4171 tf->deco = tf->merge()->deco; 4217 if (tf->next)
4218 tf->deco = tf->merge()->deco;
4172 4219
4173 if (tf->inuse) 4220 if (tf->inuse)
4174 { error(loc, "recursive type"); 4221 { error(loc, "recursive type");
4175 tf->inuse = 0; 4222 tf->inuse = 0;
4176 return terror; 4223 return terror;
4768 * if expression, *pe is set 4815 * if expression, *pe is set
4769 * if type, *pt is set 4816 * if type, *pt is set
4770 */ 4817 */
4771 4818
4772 void TypeIdentifier::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) 4819 void TypeIdentifier::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps)
4773 { Dsymbol *s; 4820 {
4774 Dsymbol *scopesym; 4821 Dsymbol *scopesym;
4775 4822
4776 //printf("TypeIdentifier::resolve(sc = %p, idents = '%s')\n", sc, toChars()); 4823 //printf("TypeIdentifier::resolve(sc = %p, idents = '%s')\n", sc, toChars());
4777 s = sc->search(loc, ident, &scopesym); 4824 Dsymbol *s = sc->search(loc, ident, &scopesym);
4778 resolveHelper(loc, sc, s, scopesym, pe, pt, ps); 4825 resolveHelper(loc, sc, s, scopesym, pe, pt, ps);
4779 if (*pt) 4826 if (*pt)
4780 (*pt) = (*pt)->addMod(mod); 4827 (*pt) = (*pt)->addMod(mod);
4781 } 4828 }
4782 4829
5239 } 5286 }
5240 5287
5241 Type *TypeEnum::semantic(Loc loc, Scope *sc) 5288 Type *TypeEnum::semantic(Loc loc, Scope *sc)
5242 { 5289 {
5243 //printf("TypeEnum::semantic() %s\n", toChars()); 5290 //printf("TypeEnum::semantic() %s\n", toChars());
5244 sym->semantic(sc); 5291 //sym->semantic(sc);
5245 return merge(); 5292 return merge();
5246 } 5293 }
5247 5294
5248 d_uns64 TypeEnum::size(Loc loc) 5295 d_uns64 TypeEnum::size(Loc loc)
5249 { 5296 {
5893 return e; 5940 return e;
5894 } 5941 }
5895 5942
5896 TemplateInstance *ti = s->isTemplateInstance(); 5943 TemplateInstance *ti = s->isTemplateInstance();
5897 if (ti) 5944 if (ti)
5898 { if (!ti->semanticdone) 5945 { if (!ti->semanticRun)
5899 ti->semantic(sc); 5946 ti->semantic(sc);
5900 s = ti->inst->toAlias(); 5947 s = ti->inst->toAlias();
5901 if (!s->isTemplateInstance()) 5948 if (!s->isTemplateInstance())
5902 goto L1; 5949 goto L1;
5903 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti)); 5950 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti));
6132 } 6179 }
6133 6180
6134 Type *TypeClass::semantic(Loc loc, Scope *sc) 6181 Type *TypeClass::semantic(Loc loc, Scope *sc)
6135 { 6182 {
6136 //printf("TypeClass::semantic(%s)\n", sym->toChars()); 6183 //printf("TypeClass::semantic(%s)\n", sym->toChars());
6137 if (sym->scope) 6184 if (deco)
6138 sym->semantic(sym->scope); 6185 return this;
6186 //printf("\t%s\n", merge()->deco);
6139 return merge(); 6187 return merge();
6140 } 6188 }
6141 6189
6142 d_uns64 TypeClass::size(Loc loc) 6190 d_uns64 TypeClass::size(Loc loc)
6143 { 6191 {
6424 return e; 6472 return e;
6425 } 6473 }
6426 6474
6427 TemplateInstance *ti = s->isTemplateInstance(); 6475 TemplateInstance *ti = s->isTemplateInstance();
6428 if (ti) 6476 if (ti)
6429 { if (!ti->semanticdone) 6477 { if (!ti->semanticRun)
6430 ti->semantic(sc); 6478 ti->semantic(sc);
6431 s = ti->inst->toAlias(); 6479 s = ti->inst->toAlias();
6432 if (!s->isTemplateInstance()) 6480 if (!s->isTemplateInstance())
6433 goto L1; 6481 goto L1;
6434 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti)); 6482 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti));