Changeset 783

Show
Ignore:
Timestamp:
08/26/07 19:13:24 (1 year ago)
Author:
Gregor
Message:

MERGE: DMD 2.003

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/dmdfe-2.0/Makefile

    r740 r783  
    5656  struct.o \ 
    5757  template.o \ 
     58  traits.o \ 
    5859  unialpha.o \ 
    5960  utf.o \ 
  • branches/dmdfe-2.0/cast.c

    r740 r783  
    11 
    2 // Copyright (c) 1999-2006 by Digital Mars 
     2// Copyright (c) 1999-2007 by Digital Mars 
    33// All Rights Reserved 
    44// written by Walter Bright 
     
    3232Expression *Expression::implicitCastTo(Scope *sc, Type *t) 
    3333{ 
    34     //printf("implicitCastTo(%s) => %s\n", type->toChars(), t->toChars()); 
     34    //printf("Expression::implicitCastTo(%s) => %s\n", type->toChars(), t->toChars()); 
     35    //printf("%s\n", toChars()); 
     36 
    3537    if (implicitConvTo(t)) 
    3638    { 
     
    6163t->print(); 
    6264printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco); 
    63 //printf("%p %p %p\n", type->next->arrayOf(), type, t); 
     65//printf("%p %p %p\n", type->nextOf()->arrayOf(), type, t); 
    6466fflush(stdout); 
    6567#endif 
     
    98100    type = Type::terror; 
    99101    } 
    100     if (t->ty == Tbit && isBit()) 
    101     return MATCHconvert; 
    102102    Expression *e = optimize(WANTvalue | WANTflags); 
    103103    if (e != this) 
     
    106106    } 
    107107    MATCH match = type->implicitConvTo(t); 
    108     if (match
     108    if (match != MATCHnomatch
    109109    return match; 
    110110#if 0 
     
    112112    if (tb->ty == Tdelegate) 
    113113    {   TypeDelegate *td = (TypeDelegate *)tb; 
    114     TypeFunction *tf = (TypeFunction *)td->next
     114    TypeFunction *tf = (TypeFunction *)td->nextOf()
    115115 
    116116    if (!tf->varargs && 
     
    118118       ) 
    119119    { 
    120         match = type->implicitConvTo(tf->next); 
     120        match = type->implicitConvTo(tf->nextOf()); 
    121121        if (match) 
    122122        return match; 
    123         if (tf->next->toBasetype()->ty == Tvoid) 
     123        if (tf->nextOf()->toBasetype()->ty == Tvoid) 
    124124        return MATCHconvert; 
    125125    } 
     
    139139    return MATCHexact; 
    140140 
    141     enum TY ty = type->toBasetype()->ty; 
    142     enum TY toty = t->toBasetype()->ty; 
     141    TY ty = type->toBasetype()->ty; 
     142    TY toty = t->toBasetype()->ty; 
    143143 
    144144    if (type->implicitConvTo(t) == MATCHnomatch && t->ty == Tenum) 
    145     { 
    146     return MATCHnomatch; 
    147     } 
     145    goto Lno; 
    148146 
    149147    switch (ty) 
     
    301299        goto Lyes; 
    302300    } 
     301 
     302    case Tpointer: 
     303//printf("type = %s\n", type->toBasetype()->toChars()); 
     304//printf("t = %s\n", t->toBasetype()->toChars()); 
     305        if (ty == Tpointer && 
     306            type->toBasetype()->nextOf()->ty == t->toBasetype()->nextOf()->ty) 
     307        {   /* Allow things like: 
     308         *  const char* P = cast(char *)3; 
     309         *  char* q = P; 
     310         */ 
     311        goto Lyes; 
     312        } 
     313        break; 
    303314    } 
    304315    return Expression::implicitConvTo(t); 
    305316 
    306317Lyes: 
    307     //printf("MATCHconvert\n"); 
    308318    return MATCHconvert; 
    309319 
    310320Lno: 
    311     //printf("MATCHnomatch\n"); 
    312321    return MATCHnomatch; 
    313322} 
     
    322331    return MATCHexact; 
    323332    // NULL implicitly converts to any pointer type or dynamic array 
    324     if (type->ty == Tpointer && type->next->ty == Tvoid) 
     333    if (type->ty == Tpointer && type->nextOf()->ty == Tvoid) 
    325334    { 
    326335    if (t->ty == Ttypedef) 
     
    343352    if (!committed) 
    344353    { 
    345     if (!committed && t->ty == Tpointer && t->next->ty == Tvoid) 
     354    if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid) 
    346355    { 
    347356    return MATCHnomatch; 
     
    349358    if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer) 
    350359    { 
    351     if (type->next->ty == Tchar) 
    352     { 
     360    TY tyn = type->nextOf()->ty; 
     361    if (tyn == Tchar || tyn == Twchar || tyn == Tdchar) 
     362    {   Type *tn; 
     363        MATCH m; 
     364 
    353365        switch (t->ty) 
    354366        { 
    355367        case Tsarray: 
    356             if (type->ty == Tsarray && 
    357             ((TypeSArray *)type)->dim->toInteger() != 
    358             ((TypeSArray *)t)->dim->toInteger()) 
    359             return MATCHnomatch; 
    360             goto L1; 
     368            if (type->ty == Tsarray) 
     369            { 
     370            if (((TypeSArray *)type)->dim->toInteger() != 
     371                ((TypeSArray *)t)->dim->toInteger()) 
     372                return MATCHnomatch; 
     373            TY tynto = t->nextOf()->ty; 
     374            if (tynto == Tchar || tynto == Twchar || tynto == Tdchar) 
     375                return MATCHexact; 
     376            } 
    361377        case Tarray: 
    362             goto L1; 
    363378        case Tpointer: 
    364         L1: 
    365             if (t->next->ty == Tchar) 
    366             return MATCHexact; 
    367             else if (t->next->ty == Twchar) 
    368             return MATCHexact; 
    369             else if (t->next->ty == Tdchar) 
    370             return MATCHexact; 
     379            tn = t->nextOf(); 
     380            m = MATCHexact; 
     381            if (type->nextOf()->mod != tn->mod) 
     382            {   if (!tn->isConst()) 
     383                return MATCHnomatch; 
     384            m = MATCHconst; 
     385            } 
     386            switch (tn->ty) 
     387            { 
     388            case Tchar: 
     389            case Twchar: 
     390            case Tdchar: 
     391                return m; 
     392            } 
    371393            break; 
    372394        } 
     
    389411{   MATCH result = MATCHexact; 
    390412 
     413#if 0 
     414    printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n", 
     415    toChars(), type->toChars(), t->toChars()); 
     416#endif 
    391417    Type *typeb = type->toBasetype(); 
    392418    Type *tb = t->toBasetype(); 
     
    402428    for (int i = 0; i < elements->dim; i++) 
    403429    {   Expression *e = (Expression *)elements->data[i]; 
    404         MATCH m = (MATCH)e->implicitConvTo(tb->next); 
     430        MATCH m = (MATCH)e->implicitConvTo(tb->nextOf()); 
    405431        if (m < result) 
    406432        result = m;         // remember worst match 
     
    423449    for (size_t i = 0; i < keys->dim; i++) 
    424450    {   Expression *e = (Expression *)keys->data[i]; 
    425         MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->key); 
     451        MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->index); 
    426452        if (m < result) 
    427453        result = m;         // remember worst match 
     
    429455        break;              // no need to check for worse 
    430456        e = (Expression *)values->data[i]; 
    431         m = (MATCH)e->implicitConvTo(tb->next); 
     457        m = (MATCH)e->implicitConvTo(tb->nextOf()); 
    432458        if (m < result) 
    433459        result = m;         // remember worst match 
     
    459485 
    460486    t = t->toBasetype(); 
    461     if (type->ty == Tpointer && type->next->ty == Tfunction && 
    462         t->ty == Tpointer && t->next->ty == Tfunction && 
     487    if (type->ty == Tpointer && type->nextOf()->ty == Tfunction && 
     488        t->ty == Tpointer && t->nextOf()->ty == Tfunction && 
    463489        e1->op == TOKvar) 
    464490    { 
     491        /* I don't think this can ever happen - 
     492         * it should have been 
     493         * converted to a SymOffExp. 
     494         */ 
     495        assert(0); 
    465496        ve = (VarExp *)e1; 
    466497        f = ve->var->isFuncDeclaration(); 
    467         if (f && f->overloadExactMatch(t->next)) 
     498        if (f && f->overloadExactMatch(t->nextOf())) 
    468499        result = MATCHexact; 
    469500    } 
     
    490521 
    491522    t = t->toBasetype(); 
    492     if (type->ty == Tpointer && type->next->ty == Tfunction && 
    493         t->ty == Tpointer && t->next->ty == Tfunction) 
     523    if (type->ty == Tpointer && type->nextOf()->ty == Tfunction && 
     524        (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction) 
    494525    { 
    495526        f = var->isFuncDeclaration(); 
    496         if (f && f->overloadExactMatch(t->next)) 
    497         result = MATCHexact; 
     527        if (f) 
     528        {   f = f->overloadExactMatch(t->nextOf()); 
     529        if (f) 
     530        {   if ((t->ty == Tdelegate && (f->needThis() || f->isNested())) || 
     531            (t->ty == Tpointer && !(f->needThis() || f->isNested()))) 
     532            result = MATCHexact; 
     533        } 
     534        } 
    498535    } 
    499536    } 
     
    512549    result = type->implicitConvTo(t); 
    513550 
    514     if (result == 0
     551    if (result == MATCHnomatch
    515552    { 
    516553    // Look for pointers to functions where the functions are overloaded. 
     
    518555 
    519556    t = t->toBasetype(); 
    520     if (type->ty == Tdelegate && type->next->ty == Tfunction && 
    521         t->ty == Tdelegate && t->next->ty == Tfunction) 
    522     { 
    523         if (func && func->overloadExactMatch(t->next)) 
     557    if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction && 
     558        t->ty == Tdelegate && t->nextOf()->ty == Tfunction) 
     559    { 
     560        if (func && func->overloadExactMatch(t->nextOf())) 
    524561        result = MATCHexact; 
    525562    } 
     
    561598    if (tb != type) 
    562599    { 
    563     if (tb->ty == Tbit && isBit()) 
    564         ; 
    565  
    566600    // Do (type *) cast of (type [dim]) 
    567     else if (tb->ty == Tpointer && 
     601    if (tb->ty == Tpointer && 
    568602        type->ty == Tsarray 
    569603       ) 
     
    580614    { 
    581615        TypeDelegate *td = (TypeDelegate *)tb; 
    582         TypeFunction *tf = (TypeFunction *)td->next
    583         return toDelegate(sc, tf->next); 
     616        TypeFunction *tf = (TypeFunction *)td->nextOf()
     617        return toDelegate(sc, tf->nextOf()); 
    584618    } 
    585619#endif 
     
    629663    { 
    630664    // NULL implicitly converts to any pointer type or dynamic array 
    631     if (type->ty == Tpointer && type->next->ty == Tvoid && 
     665    if (type->ty == Tpointer && type->nextOf()->ty == Tvoid && 
    632666        (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray || 
    633667         tb->ty == Tdelegate)) 
     
    636670        if (tb->ty == Tdelegate) 
    637671        {   TypeDelegate *td = (TypeDelegate *)tb; 
    638         TypeFunction *tf = (TypeFunction *)td->next
     672        TypeFunction *tf = (TypeFunction *)td->nextOf()
    639673 
    640674        if (!tf->varargs && 
     
    665699    //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed); 
    666700 
    667     if (!committed && t->ty == Tpointer && t->next->ty == Tvoid) 
     701    if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid) 
    668702    { 
    669703    error("cannot convert string literal to void*"); 
     
    708742    if (se->committed == 1) 
    709743    { 
    710     if (se->type->next->size() == tb->next->size()) 
     744    if (se->type->nextOf()->size() == tb->nextOf()->size()) 
    711745    {   se->type = t; 
    712746        return se; 
     
    728762    OutBuffer buffer; 
    729763    newlen = 0; 
    730     tfty = se->type->next->toBasetype()->ty; 
    731     ttty = tb->next->toBasetype()->ty; 
     764    tfty = se->type->nextOf()->toBasetype()->ty; 
     765    ttty = tb->nextOf()->toBasetype()->ty; 
    732766    switch (X(tfty, ttty)) 
    733767    { 
     
    820854        se->string = buffer.extractData(); 
    821855        se->len = newlen; 
    822         se->sz = tb->next->size(); 
     856        se->sz = tb->nextOf()->size(); 
    823857        break; 
    824858 
    825859    default: 
    826         if (se->type->next->size() == tb->next->size()) 
     860        if (se->type->nextOf()->size() == tb->nextOf()->size()) 
    827861        {   se->type = t; 
    828862        return se; 
     
    894928    FuncDeclaration *f; 
    895929 
    896     if (type->ty == Tpointer && type->next->ty == Tfunction && 
    897         tb->ty == Tpointer && tb->next->ty == Tfunction && 
     930    if (type->ty == Tpointer && type->nextOf()->ty == Tfunction && 
     931        tb->ty == Tpointer && tb->nextOf()->ty == Tfunction && 
    898932        e1->op == TOKvar) 
    899933    { 
     
    902936        if (f) 
    903937        { 
    904         f = f->overloadExactMatch(tb->next); 
     938        f = f->overloadExactMatch(tb->nextOf()); 
    905939        if (f) 
    906940        { 
     
    937971    if ((tb->ty == Tarray || tb->ty == Tsarray) && 
    938972    (typeb->ty == Tarray || typeb->ty == Tsarray) && 
    939     tb->next->toBasetype()->ty != Tvoid) 
     973    tb->nextOf()->toBasetype()->ty != Tvoid) 
    940974    { 
    941975    if (tb->ty == Tsarray) 
     
    947981    for (int i = 0; i < elements->dim; i++) 
    948982    {   Expression *e = (Expression *)elements->data[i]; 
    949         e = e->castTo(sc, tb->next); 
     983        e = e->castTo(sc, tb->nextOf()); 
    950984        elements->data[i] = (void *)e; 
    951985    } 
     
    955989    if (tb->ty == Tpointer && typeb->ty == Tsarray) 
    956990    { 
    957     type = typeb->next->pointerTo(); 
     991    type = typeb->nextOf()->pointerTo(); 
    958992    } 
    959993L1: 
     
    9661000    Type *tb = t->toBasetype(); 
    9671001    if (tb->ty == Taarray && typeb->ty == Taarray && 
    968     tb->next->toBasetype()->ty != Tvoid) 
     1002    tb->nextOf()->toBasetype()->ty != Tvoid) 
    9691003    { 
    9701004    assert(keys->dim == values->dim); 
    9711005    for (size_t i = 0; i < keys->dim; i++) 
    9721006    {   Expression *e = (Expression *)values->data[i]; 
    973         e = e->castTo(sc, tb->next); 
     1007        e = e->castTo(sc, tb->nextOf()); 
    9741008        values->data[i] = (void *)e; 
    9751009 
    9761010        e = (Expression *)keys->data[i]; 
    977         e = e->castTo(sc, ((TypeAArray *)tb)->key); 
     1011        e = e->castTo(sc, ((TypeAArray *)tb)->index); 
    9781012        keys->data[i] = (void *)e; 
    9791013    } 
     
    10021036    FuncDeclaration *f; 
    10031037 
    1004     if (type->ty == Tpointer && type->next->ty == Tfunction && 
    1005         tb->ty == Tpointer && tb->next->ty == Tfunction) 
     1038    if (hasOverloads && 
     1039        type->ty == Tpointer && type->nextOf()->ty == Tfunction && 
     1040        (tb->ty == Tpointer || tb->ty == Tdelegate) && tb->nextOf()->ty == Tfunction) 
    10061041    { 
    10071042        f = var->isFuncDeclaration(); 
    10081043        if (f) 
    10091044        { 
    1010         f = f->overloadExactMatch(tb->next); 
     1045        f = f->overloadExactMatch(tb->nextOf()); 
    10111046        if (f) 
    10121047        { 
    1013             e = new SymOffExp(loc, f, 0); 
    1014             e->type = t; 
     1048            if (tb->ty == Tdelegate && f->needThis() && hasThis(sc)) 
     1049            { 
     1050            e = new DelegateExp(loc, new ThisExp(loc), f); 
     1051            e = e->semantic(sc); 
     1052            } 
     1053            else if (tb->ty == Tdelegate && f->isNested()) 
     1054            { 
     1055            e = new DelegateExp(loc, new IntegerExp(0), f); 
     1056            e = e->semantic(sc); 
     1057            } 
     1058            else 
     1059            { 
     1060            e = new SymOffExp(loc, f, 0); 
     1061            e->type = t; 
     1062            } 
    10151063            return e; 
    10161064        } 
     
    10181066    } 
    10191067    e = Expression::castTo(sc, t); 
     1068    } 
     1069    if (e == this && hasOverloads) 
     1070    {   e = syntaxCopy(); 
     1071    ((SymOffExp *)e)->hasOverloads = 0; 
    10201072    } 
    10211073    e->type = t; 
     
    10401092    FuncDeclaration *f; 
    10411093 
    1042     if (type->ty == Tdelegate && type->next->ty == Tfunction && 
    1043         tb->ty == Tdelegate && tb->next->ty == Tfunction) 
     1094    if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction && 
     1095        tb->ty == Tdelegate && tb->nextOf()->ty == Tfunction) 
    10441096    { 
    10451097        if (func) 
    10461098        { 
    1047         f = func->overloadExactMatch(tb->next); 
     1099        f = func->overloadExactMatch(tb->nextOf()); 
    10481100        if (f) 
    10491101        {   int offset; 
    1050             if (f->tintro && f->tintro->next->isBaseOf(f->type->next, &offset) && offset) 
     1102            if (f->tintro && f->tintro->nextOf()->isBaseOf(f->type->nextOf(), &offset) && offset) 
    10511103            error("%s", msg); 
    10521104            e = new DelegateExp(loc, e1, f); 
     
    10631115    {   int offset; 
    10641116 
    1065     if (func->tintro && func->tintro->next->isBaseOf(func->type->next, &offset) && offset) 
     1117    if (func->tintro && func->tintro->nextOf()->isBaseOf(func->type->nextOf(), &offset) && offset) 
    10661118        error("%s", msg); 
    10671119    } 
     
    11021154    Type *t = Type::tptrdiff_t; 
    11031155 
    1104     stride = t1b->next->size(); 
     1156    stride = t1b->nextOf()->size(); 
    11051157    if (!t->equals(t2b)) 
    11061158        e2 = e2->castTo(sc, t); 
    1107     if (t1b->next->isbit()) 
    1108         // BUG: should add runtime check for misaligned offsets 
    1109         // This perhaps should be done by rewriting as &p[i] 
    1110         // and letting back end do it. 
    1111         e2 = new UshrExp(loc, e2, new IntegerExp(0, 3, t)); 
    1112     else 
    1113         e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t)); 
     1159    e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t)); 
    11141160    e2->type = t; 
    11151161    type = e1->type; 
     
    11211167    Expression *e; 
    11221168 
    1123     stride = t2b->next->size(); 
     1169    stride = t2b->nextOf()->size(); 
    11241170    if (!t->equals(t1b)) 
    11251171        e = e1->castTo(sc, t); 
    11261172    else 
    11271173        e = e1; 
    1128     if (t2b->next->isbit()) 
    1129         // BUG: should add runtime check for misaligned offsets 
    1130         e = new UshrExp(loc, e, new IntegerExp(0, 3, t)); 
    1131     else 
    1132         e = new MulExp(loc, e, new IntegerExp(0, stride, t)); 
     1174    e = new MulExp(loc, e, new IntegerExp(0, stride, t)); 
    11331175    e->type = t; 
    11341176    type = e2->type; 
     
    12161258    } 
    12171259 
     1260    if (op == TOKcat) 
     1261    { 
     1262    if ((t1->ty == Tsarray || t1->ty == Tarray) && 
     1263        (t2->ty == Tsarray || t2->ty == Tarray) && 
     1264        (t1->nextOf()->mod || t2->nextOf()->mod)) 
     1265    { 
     1266        t1 = t1->constOf(); 
     1267        t2 = t2->constOf(); 
     1268    } 
     1269    } 
     1270 
     1271Lagain: 
    12181272    t = t1; 
    12191273    if (t1 == t2) 
     
    12231277        goto Lincompatible; 
    12241278    } 
    1225     else if (t1->isintegral() && t2->isintegral()) 
    1226     { 
    1227     printf("t1 = %s, t2 = %s\n", t1->toChars(), t2->toChars()); 
    1228     int sz1 = t1->size(); 
    1229     int sz2 = t2->size(); 
    1230     int sign1 = t1->isunsigned() == 0; 
    1231     int sign2 = t2->isunsigned() == 0; 
    1232  
    1233     if (sign1 == sign2) 
    1234     { 
    1235         if (sz1 < sz2) 
    1236         goto Lt2; 
    1237         else 
    1238         goto Lt1; 
    1239     } 
    1240     if (!sign1) 
    1241     { 
    1242         if (sz1 >= sz2) 
    1243         goto Lt1; 
    1244         else 
    1245         goto Lt2; 
    1246     } 
    1247     else 
    1248     { 
    1249         if (sz2 >= sz1) 
    1250         goto Lt2; 
    1251         else 
    1252         goto Lt1; 
    1253     } 
    1254     } 
    12551279    else if (t1->ty == Tpointer && t2->ty == Tpointer) 
    12561280    { 
    12571281    // Bring pointers to compatible type 
    1258     Type *t1n = t1->next; 
    1259     Type *t2n = t2->next; 
    1260  
    1261 //t1->print(); 
    1262 //t2->print(); 
    1263 //if (t1n == t2n) *(char *)0 = 0; 
    1264     assert(t1n != t2n); 
    1265     if (t1n->ty == Tvoid)       // pointers to void are always compatible 
     1282    Type *t1n = t1->nextOf(); 
     1283    Type *t2n = t2->nextOf(); 
     1284 
     1285    if (t1n == t2n) 
     1286        ; 
     1287    else if (t1n->ty == Tvoid)  // pointers to void are always compatible 
    12661288        t = t2; 
    12671289    else if (t2n->ty == Tvoid) 
    12681290        ; 
     1291    else if (t1n->mod != t2n->mod) 
     1292    { 
     1293        t1 = t1->constOf(); 
     1294        t2 = t2->constOf(); 
     1295        goto Lagain; 
     1296    } 
    12691297    else if (t1n->ty == Tclass && t2n->ty == Tclass) 
    12701298    {   ClassDeclaration *cd1 = t1n->isClassHandle(); 
     
    12901318    } 
    12911319    else if ((t1->ty == Tsarray || t1->ty == Tarray) && 
    1292          e2->op == TOKnull && t2->ty == Tpointer && t2->next->ty == Tvoid) 
    1293     { 
     1320         e2->op == TOKnull && t2->ty == Tpointer && t2->nextOf()->ty == Tvoid) 
     1321    {   /*  (T[n] op void*) 
     1322     *  (T[] op void*) 
     1323     */ 
    12941324    goto Lx1; 
    12951325    } 
    12961326    else if ((t2->ty == Tsarray || t2->ty == Tarray) && 
    1297          e1->op == TOKnull && t1->ty == Tpointer && t1->next->ty == Tvoid) 
    1298     { 
     1327         e1->op == TOKnull && t1->ty == Tpointer && t1->nextOf()->ty == Tvoid) 
     1328    {   /*  (void* op T[n]) 
     1329     *  (void* op T[]) 
     1330     */ 
    12991331    goto Lx2; 
    13001332    } 
     
    13061338    { 
    13071339    goto Lt1; 
     1340    } 
     1341    /* If one is mutable and the other invariant, then retry 
     1342     * with both of them as const 
     1343     */ 
     1344    else if ((t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Tpointer) && 
     1345         (t2->ty == Tsarray || t2->ty == Tarray || t2->ty == Tpointer) && 
     1346         t1->nextOf()->mod != t2->nextOf()->mod 
     1347        ) 
     1348    { 
     1349    t1 = t1->constOf(); 
     1350    t2 = t2->constOf(); 
     1351    goto Lagain; 
    13081352    } 
    13091353    else if (t1->ty == Tclass || t2->ty == Tclass) 
     
    13341378        goto Lincompatible; 
    13351379    } 
     1380    else if (t1->ty == Tstruct && t2->ty == Tstruct) 
     1381    { 
     1382    if (((TypeStruct *)t1)->sym != ((TypeStruct *)t2)->sym) 
     1383        goto Lincompatible; 
     1384    } 
    13361385    else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2)) 
    13371386    { 
     
    13441393    } 
    13451394    else if (t1->ty == Tsarray && t2->ty == Tsarray && 
    1346          e2->implicitConvTo(t1->next->arrayOf())) 
     1395         e2->implicitConvTo(t1->nextOf()->arrayOf())) 
    13471396    { 
    13481397     Lx1: 
    1349     t = t1->next->arrayOf(); 
     1398    t = t1->nextOf()->arrayOf(); 
    13501399    e1 = e1->castTo(sc, t); 
    13511400    e2 = e2->castTo(sc, t); 
    13521401    } 
    13531402    else if (t1->ty == Tsarray && t2->ty == Tsarray && 
    1354          e1->implicitConvTo(t2->next->arrayOf())) 
     1403         e1->implicitConvTo(t2->nextOf()->arrayOf())) 
    13551404    { 
    13561405     Lx2: 
    1357     t = t2->next->arrayOf(); 
     1406    t = t2->nextOf()->arrayOf(); 
    13581407    e1 = e1->castTo(sc, t); 
    13591408    e2 = e2->castTo(sc, t); 
     1409    } 
     1410    else if (t1->isintegral() && t2->isintegral()) 
     1411    { 
     1412    assert(0); 
    13601413    } 
    13611414    else 
  • branches/dmdfe-2.0/class.c

    r659 r783  
    146146            Type::typeinfotypelist->error("%s", msg); 
    147147        Type::typeinfotypelist = this; 
     148        } 
     149 
     150        if (id == Id::TypeInfo_Const) 
     151        {   if (Type::typeinfoconst) 
     152            Type::typeinfoconst->error("%s", msg); 
     153        Type::typeinfoconst = this; 
     154        } 
     155 
     156        if (id == Id::TypeInfo_Invariant) 
     157        {   if (Type::typeinfoinvariant) 
     158            Type::typeinfoinvariant->error("%s", msg); 
     159        Type::typeinfoinvariant = this; 
    148160        } 
    149161    } 
     
    568580    members->push(ctor); 
    569581    ctor->addMember(sc, this, 1); 
    570     *sc = scsave; 
     582    *sc = scsave;  // why? What about sc->nofree? 
    571583    sc->offset = structsize; 
    572584    ctor->semantic(sc); 
  • branches/dmdfe-2.0/constfold.c

    r740 r783  
    972972 
    973973    //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars()); 
    974     //printf("e1->type = %s\n", e1->type->toChars()); 
    975     if (type->equals(e1->type) && to->equals(type)) 
     974    //printf("\te1->type = %s\n", e1->type->toChars()); 
     975    if (e1->type->equals(type) && type->equals(to)) 
    976976    return e1; 
     977    if (e1->type->implicitConvTo(to) >= MATCHconst || 
     978    to->implicitConvTo(e1->type) >= MATCHconst) 
     979    return expType(to, e1); 
    977980 
    978981    if (e1->isConst() != 1) 
     
    12431246    Loc loc = e1->loc; 
    12441247    Type *t; 
     1248    Type *t1 = e1->type->toBasetype(); 
     1249    Type *t2 = e2->type->toBasetype(); 
    12451250 
    12461251    //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); 
     1252    //printf("\tt1 = %s, t2 = %s\n", t1->toChars(), t2->toChars()); 
    12471253 
    12481254    if (e1->op == TOKnull && e2->op == TOKint64) 
     
    13631369    } 
    13641370    else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral && 
    1365     e1->type->equals(e2->type)) 
     1371    t1->nextOf()->equals(t2->nextOf())) 
    13661372    { 
    13671373    // Concatenate the arrays 
     
    13751381    if (type->toBasetype()->ty == Tsarray) 
    13761382    { 
    1377         e->type = new TypeSArray(e1->type->toBasetype()->next, new IntegerExp(0, es1->elements->dim, Type::tindex)); 
     1383        e->type = new TypeSArray(t1->nextOf(), new IntegerExp(0, es1->elements->dim, Type::tindex)); 
    13781384        e->type = e->type->semantic(loc, NULL); 
    13791385    } 
     
    13821388    } 
    13831389    else if (e1->op == TOKarrayliteral && 
    1384     e1->type->toBasetype()->next->equals(e2->type)) 
     1390    e1->type->toBasetype()->nextOf()->equals(e2->type)) 
    13851391    { 
    13861392    ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; 
     
    13991405    } 
    14001406    else if (e2->op == TOKarrayliteral && 
    1401     e2->type->toBasetype()->next->equals(e1->type)) 
     1407    e2->type->toBasetype()->nextOf()->equals(e1->type)) 
    14021408    { 
    14031409    ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; 
     
    14261432      L1: 
    14271433    Type *tb = t->toBasetype(); 
    1428     if (tb->ty == Tarray && tb->next->equals(e->type)) 
     1434    if (tb->ty == Tarray && tb->nextOf()->equals(e->type)) 
    14291435    {   Expressions *expressions = new Expressions(); 
    14301436        expressions->push(e); 
  • branches/dmdfe-2.0/declaration.c

    r657 r783  
    8181} 
    8282 
     83/************************************* 
     84 * Check to see if declaration can be modified in this context (sc). 
     85 * Issue error if not. 
     86 */ 
     87 
     88void Declaration::checkModify(Loc loc, Scope *sc) 
     89{ 
     90    if (sc->incontract && isParameter()) 
     91    error(loc, "cannot modify parameter '%s' in contract", toChars()); 
     92 
     93    VarDeclaration *v = isVarDeclaration(); 
     94    if (v && v->canassign == 0 && 
     95        (isConst() || isInvariant() || (isFinal() && !isCtorinit()))) 
     96    error(loc, "cannot modify final/const/invariant variable '%s'", toChars()); 
     97 
     98    if (isCtorinit()) 
     99    {   // It's only modifiable if inside the right constructor 
     100    Dsymbol *s = sc->func; 
     101    while (1) 
     102    { 
     103        FuncDeclaration *fd = NULL; 
     104        if (s) 
     105        fd = s->isFuncDeclaration(); 
     106        if (fd && 
     107        ((fd->isCtorDeclaration() && storage_class & STCfield) || 
     108         (fd->isStaticCtorDeclaration() && !(storage_class & STCfield))) && 
     109        fd->toParent() == toParent() 
     110           ) 
     111        { 
     112        assert(v); 
     113        v->ctorinit = 1; 
     114        //printf("setting ctorinit\n"); 
     115        } 
     116        else 
     117        { 
     118        if (s) 
     119        {   s = s->toParent2(); 
     120            continue; 
     121        } 
     122        else 
     123        { 
     124            const char *p = isStatic() ? "static " : ""; 
     125            error(loc, "can only initialize %sconst %s inside %sconstructor", 
     126            p, toChars(), p); 
     127        } 
     128        } 
     129        break; 
     130    } 
     131    } 
     132} 
     133 
     134 
    83135/********************************* TupleDeclaration ****************************/ 
    84136 
     
    140192        Argument *arg = new Argument(STCin, t, id, NULL); 
    141193#else 
    142         Argument *arg = new Argument(STCin, t, NULL, NULL); 
     194        Argument *arg = new Argument(0, t, NULL, NULL); 
    143195#endif 
    144196        args->data[i] = (void *)arg; 
     
    628680    protection = sc->protection; 
    629681    //printf("sc->stc = %x\n", sc->stc); 
    630     //printf("storage_class = %x\n", storage_class); 
     682    //printf("storage_class = x%x\n", storage_class); 
    631683 
    632684    Dsymbol *parent = toParent(); 
     
    690742    } 
    691743 
    692     if (storage_class & STCconst && !init && !fd) 
    693     // Initialize by constructor only 
    694     storage_class = (storage_class & ~STCconst) | STCctorinit; 
     744    if (storage_class & STCfinal && !init && !fd) 
     745    {   // Initialize by constructor only 
     746    storage_class |= STCctorinit; 
     747    } 
    695748 
    696749    if (isConst()) 
    697750    { 
    698     } 
    699     else if (isStatic()) 
     751    type = type->constOf(); 
     752    if (isParameter()) 
     753    {   storage_class |= STCfinal; 
     754        storage_class &= ~STCconst; 
     755    } 
     756    } 
     757    else if (storage_class & STCinvariant) 
     758    { 
     759    type = type->invariantOf(); 
     760    if (isParameter()) 
     761    {   storage_class |= STCfinal; 
     762        storage_class &= ~STCinvariant; 
     763    } 
     764    } 
     765    type = type->toCanonConst(); 
     766 
     767    if (storage_class & (STCconst | STCinvariant | STCstatic)) 
    700768    { 
    701769    } 
     
    721789        aad = parent->isAggregateDeclaration(); 
    722790    if (aad) 
    723     { 
     791    {   assert(!(storage_class & (STCconst | STCinvariant | STCstatic))); 
    724792        aad->addField(sc, this); 
    725793    } 
     
    826894        // If local variable, use AssignExp to handle all the various 
    827895        // possibilities. 
    828         if (fd && !isStatic() && !isConst() && !init->isVoidInitializer()) 
     896        if (fd && !isStatic() && !isConst() && !isInvariant() && 
     897        !init->isVoidInitializer()) 
    829898        { 
    830899        Expression *e1; 
     
    858927            while (1) 
    859928            { 
    860             t = t->next->toBasetype(); 
     929            t = t->nextOf()->toBasetype(); 
    861930            if (t->ty != Tsarray) 
    862931                break; 
    863             if (t->next->toBasetype()->ty == Tbit) 
    864                 // t->size() gives size in bytes, convert to bits 
    865                 dim *= t->size() * 8; 
    866             else 
    867                 dim *= ((TypeSArray *)t)->dim->toInteger(); 
    868             e1->type = new TypeSArray(t->next, new IntegerExp(0, dim, Type::tindex)); 
     932            dim *= ((TypeSArray *)t)->dim->toInteger(); 
     933            e1->type = new TypeSArray(t->nextOf(), new IntegerExp(0, dim, Type::tindex)); 
    869934            } 
    870935            e1 = new SliceExp(loc, e1, NULL, NULL); 
     
    886951        { 
    887952        init = init->semantic(sc, type); 
    888         if (fd && isConst() && !isStatic()) 
     953        if (fd && (isConst() || isInvariant()) && !isStatic()) 
    889954        {   // Make it static 
    890955            storage_class |= STCstatic; 
     
    892957        } 
    893958    } 
    894     else if (isConst() || isFinal()) 
     959    else if (isConst() || isInvariant() || isFinal()) 
    895960    { 
    896961        /* Because we may need the results of a const declaration in a 
     
    11591224} 
    11601225 
     1226/***************************** TypeInfoConstDeclaration **********************/ 
     1227 
     1228TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type *tinfo) 
     1229    : TypeInfoDeclaration(tinfo, 0) 
     1230{ 
     1231} 
     1232 
     1233/***************************** TypeInfoInvariantDeclaration **********************/ 
     1234 
     1235TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type *tinfo) 
     1236    : TypeInfoDeclaration(tinfo, 0) 
     1237{ 
     1238} 
     1239 
    11611240/***************************** TypeInfoStructDeclaration **********************/ 
    11621241