src/compiler/SemanticAnalysis.cpp
changeset 241 18f3010a358c
parent 238 e9bec7549c28
child 244 3c57bbf22aa2
     1.1 --- a/src/compiler/SemanticAnalysis.cpp	Tue Dec 25 18:13:02 2012 -0800
     1.2 +++ b/src/compiler/SemanticAnalysis.cpp	Wed Dec 26 17:14:12 2012 -0800
     1.3 @@ -731,6 +731,11 @@
     1.4      if (!lval)
     1.5          return;
     1.6  
     1.7 +    if (lval->type()->quals() & TypeQual_Const) {
     1.8 +        cc_.reportError(node->pos(), Message_CannotModifyConst);
     1.9 +        return;
    1.10 +    }
    1.11 +
    1.12      HIR *hir = eval(node->expression());
    1.13      if ((hir = coerce(hir, lval->type(), Coerce_Assign)) == NULL)
    1.14          return;
    1.15 @@ -838,7 +843,7 @@
    1.16  HIR *
    1.17  SemanticAnalysis::typeCheckArray(const SourcePosition &pos,
    1.18                                   HIR *from,
    1.19 -                                 Handle<Type> argActual,
    1.20 +                                 Handle<ArrayType> argActual,
    1.21                                   CoercionKind kind)
    1.22  {
    1.23      Local<Type> argGiven(zone_, from->type());
    1.24 @@ -888,7 +893,7 @@
    1.25          // We treat declaration separately from assignment, since for
    1.26          // declaration we can just immediately use the cloned version.
    1.27          bool shouldDuplicate = true;
    1.28 -        if (0 /*actual->isConst() :TODO */ ||
    1.29 +        if ((actual->quals() & TypeQual_Const) ||
    1.30              (actual->isFixedLength() && kind == Coerce_Assign))
    1.31          {
    1.32              shouldDuplicate = false;
    1.33 @@ -959,11 +964,18 @@
    1.34          actual = ArrayType::cast(actual->contained());
    1.35      }
    1.36  
    1.37 +    bool givenConst = !!(given->contained()->quals() & TypeQual_Const);
    1.38 +    bool actualConst = !!(actual->contained()->quals() & TypeQual_Const);
    1.39 +
    1.40      // These arrays must contain the same innermost type.
    1.41 -    if (given->contained() != actual->contained()) {
    1.42 +    if (given->contained()->unqualified() != actual->contained()->unqualified() ||
    1.43 +        (givenConst && !actualConst))
    1.44 +    {
    1.45 +        char aname[255];
    1.46 +        char gname[255];
    1.47          cc_.reportError(pos, Message_TypeMismatch,
    1.48 -                        GetTypeName(actual->contained()),
    1.49 -                        GetTypeName(given->contained()));
    1.50 +                        GetTypeName(argActual, aname, sizeof(aname)),
    1.51 +                        GetTypeName(argGiven, gname, sizeof(gname)));
    1.52          return false;
    1.53      }
    1.54  
    1.55 @@ -973,9 +985,11 @@
    1.56  HIR *
    1.57  SemanticAnalysis::typeFailure(const SourcePosition &pos, Handle<Type> from, Handle<Type> to)
    1.58  {
    1.59 +    char fname[255];
    1.60 +    char tname[255];
    1.61      cc_.reportError(pos, Message_InvalidCoercion,
    1.62 -                    GetTypeName(from),
    1.63 -                    GetTypeName(to));
    1.64 +                    GetTypeName(from, fname, sizeof(fname)),
    1.65 +                    GetTypeName(to, tname, sizeof(tname)));
    1.66      return NULL;
    1.67  }
    1.68  
    1.69 @@ -992,8 +1006,10 @@
    1.70      if (to->isEnum() && *from != to)
    1.71          return typeFailure(hir->node()->pos(), from, to);
    1.72      
    1.73 -    if (to->isArray())
    1.74 -        return typeCheckArray(SourcePosition(), hir, to, kind);
    1.75 +    if (to->isArray()) {
    1.76 +        Local<ArrayType> atype(zone_, ArrayType::cast(to));
    1.77 +        return typeCheckArray(hir->node()->pos(), hir, atype, kind);
    1.78 +    }
    1.79      
    1.80      return hir;
    1.81  }