src/compiler/SemanticAnalysis.cpp
changeset 233 bbb498ccb4ac
parent 232 2b8512340180
child 234 26e3035cdffc
     1.1 --- a/src/compiler/SemanticAnalysis.cpp	Sat Dec 22 19:48:43 2012 -0800
     1.2 +++ b/src/compiler/SemanticAnalysis.cpp	Sat Dec 22 23:18:57 2012 -0800
     1.3 @@ -214,6 +214,11 @@
     1.4  }
     1.5  
     1.6  void
     1.7 +SemanticAnalysis::visitEnumStatement(EnumStatement *node)
     1.8 +{
     1.9 +}
    1.10 +
    1.11 +void
    1.12  SemanticAnalysis::visitExpressionStatement(ExpressionStatement *node)
    1.13  {
    1.14      // While building HIR, we enter new pool and root scopes, since HIR
    1.15 @@ -545,24 +550,51 @@
    1.16  SemanticAnalysis::visitNameProxy(NameProxy *proxy)
    1.17  {
    1.18      Symbol *sym = proxy->sym();
    1.19 -    VariableSymbol *var = sym->asVariable();
    1.20  
    1.21 -    Scope *in = sym->scope();
    1.22 -    switch (in->kind()) {
    1.23 -      case Scope::GLOBAL:
    1.24 -        hir_ = new (pool_) HGlobal(proxy, sym);
    1.25 -        return;
    1.26 +    switch (sym->kind()) {
    1.27 +      case Symbol::kConstant:
    1.28 +      {
    1.29 +        ConstantSymbol *val = sym->toConstant();
    1.30 +        hir_ = new (pool_) HInteger(proxy, sym->type(), val->value().toInt());
    1.31 +        break;
    1.32 +      }
    1.33 +      
    1.34 +      // This is kind of screwy. Symbol::kFunction is like a constant we can't
    1.35 +      // evaluate yet. Maybe we should extract the GLOBAL case and say that
    1.36 +      // kFunction is only used for global functions (and use const vars for
    1.37 +      // local functions).
    1.38 +      case Symbol::kFunction:
    1.39 +      case Symbol::kVariable:
    1.40 +      {
    1.41 +        Scope *in = sym->scope();
    1.42 +        switch (in->kind()) {
    1.43 +          case Scope::GLOBAL:
    1.44 +            hir_ = new (pool_) HGlobal(proxy, sym);
    1.45 +            break;
    1.46  
    1.47 -      case Scope::FUNCTION:
    1.48 -        assert(var->storage() == VariableSymbol::Arg);
    1.49 -        hir_ = new (pool_) HLocal(proxy, var);
    1.50 -        return;
    1.51 +          case Scope::FUNCTION:
    1.52 +          {
    1.53 +            VariableSymbol *var = sym->toVariable();
    1.54 +            assert(var->storage() == VariableSymbol::Arg);
    1.55 +            hir_ = new (pool_) HLocal(proxy, var);
    1.56 +            break;
    1.57 +          }
    1.58 +
    1.59 +          default:
    1.60 +          {
    1.61 +            VariableSymbol *var = sym->toVariable();
    1.62 +            assert(in->kind() == Scope::BLOCK);
    1.63 +            assert(var->storage() == VariableSymbol::Local);
    1.64 +            hir_ = new (pool_) HLocal(proxy, var);
    1.65 +            break;
    1.66 +          }
    1.67 +        }
    1.68 +        break;
    1.69 +      }
    1.70  
    1.71        default:
    1.72 -        assert(in->kind() == Scope::BLOCK);
    1.73 -        assert(var->storage() == VariableSymbol::Local);
    1.74 -        hir_ = new (pool_) HLocal(proxy, var);
    1.75 -        return;
    1.76 +        // :TODO:
    1.77 +        assert(false);
    1.78      }
    1.79  }
    1.80  
    1.81 @@ -854,6 +886,15 @@
    1.82  }
    1.83  
    1.84  HIR *
    1.85 +SemanticAnalysis::typeFailure(const SourcePosition &pos, Handle<Type> from, Handle<Type> to)
    1.86 +{
    1.87 +    cc_.reportError(pos, Message_InvalidCoercion,
    1.88 +                    GetTypeName(from),
    1.89 +                    GetTypeName(to));
    1.90 +    return NULL;
    1.91 +}
    1.92 +
    1.93 +HIR *
    1.94  SemanticAnalysis::coerce(HIR *hir, Handle<Type> to, CoercionKind kind)
    1.95  {
    1.96      Handle<Type> from = hir->type();
    1.97 @@ -862,6 +903,9 @@
    1.98          return new (pool_) HConvert(hir->node(), to, OP_CVT_I2F, hir);
    1.99      if (from->isInt32() && to->isBool())
   1.100          return new (pool_) HConvert(hir->node(), to, OP_CVT_I2B, hir);
   1.101 +
   1.102 +    if (to->isEnum() && from != to)
   1.103 +        return typeFailure(hir->node()->pos(), from, to);
   1.104      
   1.105      if (to->isArray()) {
   1.106          if (!typeCheckArray(SourcePosition(), from, to, kind))