src/compiler/NameBinding.cpp
changeset 256 3c184218462d
parent 251 ca99a81745fe
child 257 9db4cc815ebc
     1.1 --- a/src/compiler/NameBinding.cpp	Tue Jan 01 20:08:34 2013 -0800
     1.2 +++ b/src/compiler/NameBinding.cpp	Sun Jan 06 13:52:21 2013 -0800
     1.3 @@ -197,6 +197,7 @@
     1.4          if (node->initialization())
     1.5              node->initialization()->accept(this);
     1.6      }
     1.7 +
     1.8      void visitEnumStatement(EnumStatement *node) {
     1.9          Local<String> name(zone_, node->name());
    1.10          Local<Type> type(zone_);
    1.11 @@ -239,6 +240,7 @@
    1.12              value++;
    1.13          }
    1.14      }
    1.15 +
    1.16      void visitFunctionStatement(FunctionStatement *node) {
    1.17          // We create function types ahead of time, not for any particular
    1.18          // reason. It makes things consistent with FunctionTypeStatement,
    1.19 @@ -260,9 +262,6 @@
    1.20          {
    1.21              // We're in the global scope, for sure.
    1.22              assert(env_->scope()->kind() == Scope::GLOBAL);
    1.23 -            GlobalScope *globals = (GlobalScope *)env_->scope();
    1.24 -            if (!globals->addPublic(sym))
    1.25 -                return;
    1.26          }
    1.27  
    1.28          node->setSymbol(sym);
    1.29 @@ -298,9 +297,11 @@
    1.30  
    1.31          node->setScopes(argEnv.scope(), localEnv.scope());
    1.32      }
    1.33 +
    1.34      void visitStructureStatement(StructureStatement *node) {
    1.35          assert(false);
    1.36      }
    1.37 +
    1.38      void visiFunctionTypeStatementt(FunctionTypeStatement *node) {
    1.39          assert(false);
    1.40      }
    1.41 @@ -492,73 +493,23 @@
    1.42  static const int EVAL_ARRAY_SIZE = -1;
    1.43  static const int DYNAMIC_ARRAY_SIZE = -2;
    1.44  
    1.45 -class NameBinder : public AstVisitor
    1.46 +class NameBinderBase : public AstVisitor
    1.47  {
    1.48    public:
    1.49 -    class AutoLinkScope
    1.50 -    {
    1.51 -      public:
    1.52 -        AutoLinkScope(AutoLinkScope **prevp, Scope *scope)
    1.53 -          : prevp_(prevp),
    1.54 -            prev_(*prevp),
    1.55 -            scope_(scope)
    1.56 -        {
    1.57 -            if (scope_) {
    1.58 -                *prevp_ = this;
    1.59 -                if (prev_)
    1.60 -                    scope_->setParent(prev_->scope());
    1.61 -            }
    1.62 -        }
    1.63 -        ~AutoLinkScope() {
    1.64 -            if (scope_) {
    1.65 -                assert(*prevp_ == this);
    1.66 -                *prevp_ = prev_;
    1.67 -            }
    1.68 -        }
    1.69 -        Scope *scope() const {
    1.70 -            return scope_;
    1.71 -        }
    1.72 -
    1.73 -      private:
    1.74 -        AutoLinkScope **prevp_;
    1.75 -        AutoLinkScope *prev_;
    1.76 -        Scope *scope_;
    1.77 -    };
    1.78 -
    1.79 -  public:
    1.80 -    NameBinder(Zone *zone, CompileContext &cc, TranslationUnit *unit)
    1.81 +    NameBinderBase(Zone *zone, CompileContext &cc, TranslationUnit *unit)
    1.82        : zone_(zone),
    1.83          pool_(zone->pool()),
    1.84          cc_(cc),
    1.85 -        unit_(unit),
    1.86 -        link_(NULL)
    1.87 +        unit_(unit)
    1.88      {
    1.89      }
    1.90  
    1.91 -    void bind() {
    1.92 -        // Start with the import scope, and collect all imported names.
    1.93 -        AutoLinkScope importScope(&link_, unit_->importScope());
    1.94  
    1.95 -        for (size_t i = 0; i < unit_->tree()->imports()->length(); i++)
    1.96 -            unit_->tree()->imports()->at(i)->accept(this);
    1.97 -
    1.98 -        // Next, enter the global scope and walk all statements.
    1.99 -        AutoLinkScope globalScope(&link_, unit_->globalScope());
   1.100 -        for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
   1.101 -            Statement *stmt = unit_->tree()->statements()->at(i);
   1.102 -            stmt->accept(this);
   1.103 -        }
   1.104 -    }
   1.105 -
   1.106 -    void visitVariableDeclaration(VariableDeclaration *node) {
   1.107 -        // Bind the initializer before registering the declaration, so that we
   1.108 -        // can error on bogus initializers (new x = x).
   1.109 -        if (node->initialization())
   1.110 -            node->initialization()->accept(this);
   1.111 -
   1.112 +  protected:
   1.113 +    Type *buildVariableType(VariableDeclaration *node) {
   1.114          Local<Type> type(zone_, bindType(node->type(), node->quals()));
   1.115          if (!type)
   1.116 -            return;
   1.117 +            return NULL;
   1.118  
   1.119          // We don't allow array typedefs yet.
   1.120          assert(!type->isArray());
   1.121 @@ -567,7 +518,7 @@
   1.122              int dims[MAX_ARRAY_DEPTH];
   1.123              int levels = evaluateDimensions(node->pos(), type, node->dims(), dims);
   1.124              if (levels < 0)
   1.125 -                return;
   1.126 +                return NULL;
   1.127  
   1.128              // Do some extra inference based on the initializer, if present.
   1.129              Expression *init = node->initialization();
   1.130 @@ -577,7 +528,7 @@
   1.131                                                   ? ArrayType::cast(type)->contained()
   1.132                                                   : type);
   1.133                      if (!inferFixedArrayDimensions(init->toArrayLiteral(), contained, dims, levels))
   1.134 -                        return;
   1.135 +                        return NULL;
   1.136                  } else if (init->isStringLiteral()) {
   1.137                      dims[levels - 1] = init->toStringLiteral()->string()->length() + 1;
   1.138                  }
   1.139 @@ -586,180 +537,13 @@
   1.140              // If we got extra dimensions, we need to build a new type.
   1.141              if (node->dims()) {
   1.142                  if ((type = buildArrayType(type, node->quals(), dims, node->dims()->length())) == NULL)
   1.143 -                    return;
   1.144 +                    return NULL;
   1.145              }
   1.146          }
   1.147  
   1.148 -        // If we're in global scope, and we received a |const| qualifier, then
   1.149 -        // we extend this to array references as well. This is to sort of mimic
   1.150 -        // SP1 functionality, but also means that you cannot have changeable
   1.151 -        // const pointers in global scope.
   1.152 -        if ((node->quals() & TypeQual_Const) &&
   1.153 -            type->isArray() &&
   1.154 -            link_->scope()->kind() == Scope::GLOBAL)
   1.155 -        {
   1.156 -            if ((type = zone_->types()->qualify(type, TypeQual_Const)) == NULL)
   1.157 -                return;
   1.158 -        }
   1.159 -
   1.160 -        // If the node already has a symbol (meaning it was a global), then
   1.161 -        // we don't have to do anything more.
   1.162 -        if (node->sym()) {
   1.163 -            assert(node->sym()->scope()->kind() == Scope::GLOBAL);
   1.164 -            node->sym()->setType(type);
   1.165 -            return;
   1.166 -        }
   1.167 -
   1.168 -        VariableSymbol *sym = new (pool_) VariableSymbol(link_->scope(), node->name(), node->pos(), type);
   1.169 -        node->setSymbol(sym);
   1.170 -       
   1.171 -        if (Symbol *other = link_->scope()->localLookup(sym->name())) {
   1.172 -            // Report, but allow errors to continue.
   1.173 -            cc_.reportError(sym->pos(), Message_RedeclaredName,
   1.174 -                            sym->name()->chars(),
   1.175 -                            other->pos().line,
   1.176 -                            other->pos().col);
   1.177 -            return;
   1.178 -        }
   1.179 -        
   1.180 -        link_->scope()->addSymbol(sym);
   1.181 -    }
   1.182 -    void visitEnumStatement(EnumStatement *node) {
   1.183 -    }
   1.184 -    void visitFunctionStatement(FunctionStatement *node) {
   1.185 -        AutoLinkScope argScope(&link_, node->argScope());
   1.186 -        AutoLinkScope varScope(&link_, node->varScope());
   1.187 -
   1.188 -        Local<FunctionType> type(zone_, node->sym()->type());
   1.189 -        if (!fillFunctionType(type, node->signature()))
   1.190 -            return;
   1.191 -
   1.192 -        if (node->body())
   1.193 -            node->body()->accept(this);
   1.194 -    }
   1.195 -    void visitStructureStatement(StructureStatement *node) {
   1.196 -        assert(false);
   1.197 -    }
   1.198 -    void visitFunctionTypeStatement(FunctionTypeStatement *node) {
   1.199 -        assert(false);
   1.200 +        return type;
   1.201      }
   1.202  
   1.203 -    void visitNameProxy(NameProxy *proxy) {
   1.204 -        Scope *scope = link_->scope();
   1.205 -        Symbol *sym = scope->lookup(proxy->name());
   1.206 -        if (!sym)
   1.207 -            cc_.reportError(proxy->pos(), Message_IdentifierNotFound, proxy->name()->chars());
   1.208 -        proxy->bind(sym);
   1.209 -    }
   1.210 -    void visitAssignment(Assignment *node) {
   1.211 -        node->lvalue()->accept(this);
   1.212 -        node->expression()->accept(this);
   1.213 -    }
   1.214 -    void visitBinaryExpression(BinaryExpression *node) {
   1.215 -        node->left()->accept(this);
   1.216 -        node->right()->accept(this);
   1.217 -    }
   1.218 -    void visitReturnStatement(ReturnStatement *node) {
   1.219 -        node->expression()->accept(this);
   1.220 -    }
   1.221 -    void visitForStatement(ForStatement *node) {
   1.222 -        AutoLinkScope scope(&link_, node->scope());
   1.223 -
   1.224 -        if (node->initialization())
   1.225 -            node->initialization()->accept(this);
   1.226 -        if (node->condition())
   1.227 -            node->condition()->accept(this);
   1.228 -        if (node->update())
   1.229 -            node->update()->accept(this);
   1.230 -        node->body()->accept(this);
   1.231 -    }
   1.232 -    void visitBlockStatement(BlockStatement *node) {
   1.233 -        AutoLinkScope scope(&link_, node->scope());
   1.234 -        for (size_t i = 0; i < node->statements()->length(); i++)
   1.235 -            node->statements()->at(i)->accept(this);
   1.236 -    }
   1.237 -    void visitIntegerLiteral(IntegerLiteral *node) {
   1.238 -    }
   1.239 -    void visitExpressionStatement(ExpressionStatement *node) {
   1.240 -        node->expression()->accept(this);
   1.241 -    }
   1.242 -    void visitCallExpression(CallExpression *node) {
   1.243 -        node->callee()->accept(this);
   1.244 -        for (size_t i = 0; i < node->arguments()->length(); i++)
   1.245 -            node->arguments()->at(i)->accept(this);
   1.246 -    }
   1.247 -    void visitIfStatement(IfStatement *node) {
   1.248 -        node->condition()->accept(this);
   1.249 -        node->ifTrue()->accept(this);
   1.250 -        if (node->ifFalse())
   1.251 -            node->ifFalse()->accept(this);
   1.252 -    }
   1.253 -    void visitIndexExpression(IndexExpression *node) {
   1.254 -        node->left()->accept(this);
   1.255 -        node->right()->accept(this);
   1.256 -    }
   1.257 -    void visitFloatLiteral(FloatLiteral *node) {
   1.258 -    }
   1.259 -    void visitWhileStatement(WhileStatement *node) {
   1.260 -        node->condition()->accept(this);
   1.261 -        node->body()->accept(this);
   1.262 -    }
   1.263 -    void visitBreakStatement(BreakStatement *node) {
   1.264 -    }
   1.265 -    void visitContinueStatement(ContinueStatement *node) {
   1.266 -    }
   1.267 -    void visitStringLiteral(StringLiteral *node) {
   1.268 -    }
   1.269 -    void visitIncDecExpression(IncDecExpression *node) {
   1.270 -        node->expression()->accept(this);
   1.271 -    }
   1.272 -    void visitUnaryExpression(UnaryExpression *node) {
   1.273 -        if (node->tag())
   1.274 -            node->tag()->accept(this);
   1.275 -        node->expression()->accept(this);
   1.276 -    }
   1.277 -    void visitTernaryExpression(TernaryExpression *node) {
   1.278 -        node->condition()->accept(this);
   1.279 -        node->left()->accept(this);
   1.280 -        node->right()->accept(this);
   1.281 -    }
   1.282 -    void visitBooleanLiteral(BooleanLiteral *node) {
   1.283 -    }
   1.284 -    void visitFieldExpression(FieldExpression *node) {
   1.285 -        node->left()->accept(this);
   1.286 -    }
   1.287 -    void visitSwitchStatement(SwitchStatement *node) {
   1.288 -        node->expression()->accept(this);
   1.289 -        node->defaultCase()->accept(this);
   1.290 -        for (size_t i = 0; i < node->cases()->length(); i++) {
   1.291 -            // We don't test case expressions because they are literals.
   1.292 -            Case *c = node->cases()->at(i);
   1.293 -            c->statement()->accept(this);
   1.294 -        }
   1.295 -    }
   1.296 -    void visitArrayLiteral(ArrayLiteral *node) {
   1.297 -        for (size_t i = 0; i < node->expressions()->length(); i++)
   1.298 -            node->expressions()->at(i)->accept(this);
   1.299 -    }
   1.300 -    void visitImportStatement(ImportStatement *node) {
   1.301 -        if (node->suffix() == TOK_STAR) {
   1.302 -            // Import every name.
   1.303 -            assert(false);
   1.304 -        } else {
   1.305 -            assert(false);
   1.306 -#if 0
   1.307 -            GlobalScope *other = node->source()->globalScope();
   1.308 -            Symbol *sym = other->findPublic(node->name());
   1.309 -            if (!sym) {
   1.310 -                cc_.reportError(node->pos(), Message_CannotResolveImport, node->name()->chars());
   1.311 -                return;
   1.312 -            }
   1.313 -            link_->scope()->addSymbol(sym);
   1.314 -#endif
   1.315 -        }
   1.316 -    }
   1.317 -
   1.318 -  private:
   1.319      int evaluateDimensions(const SourcePosition &pos, Handle<Type> typeArg, ExpressionList *exprs, int dims[MAX_ARRAY_DEPTH]) {
   1.320          unsigned level = 0;
   1.321          Local<Type> type(zone_, typeArg);
   1.322 @@ -967,11 +751,344 @@
   1.323          return zone_->types()->qualify(type, quals);
   1.324      }
   1.325  
   1.326 -  private:
   1.327 +  protected:
   1.328      Zone *zone_;
   1.329      PoolAllocator &pool_;
   1.330      CompileContext &cc_;
   1.331      TranslationUnit *unit_;
   1.332 +};
   1.333 +
   1.334 +class GlobalBuilder : public NameBinderBase
   1.335 +{
   1.336 +  public:
   1.337 +    GlobalBuilder(Zone *zone, CompileContext &cc, TranslationUnit *unit)
   1.338 +      : NameBinderBase(zone, cc, unit)
   1.339 +    {
   1.340 +    }
   1.341 +
   1.342 +    Struct *build() {
   1.343 +        for (size_t i = 0; i < unit_->tree()->imports()->length(); i++)
   1.344 +            unit_->tree()->imports()->at(i)->accept(this);
   1.345 +
   1.346 +        // Next, enter the global scope and walk all statements.
   1.347 +        for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
   1.348 +            Statement *stmt = unit_->tree()->statements()->at(i);
   1.349 +            stmt->accept(this);
   1.350 +        }
   1.351 +
   1.352 +        Local<FixedArray> descs(zone_, FixedArray::New(zone_, descriptors_.length(), Heap::Tenure_Old));
   1.353 +        if (!descs)
   1.354 +            return NULL;
   1.355 +
   1.356 +        for (unsigned i = 0; i < descriptors_.length(); i++)
   1.357 +            descs->set(zone_, i, *descriptors_[i]);
   1.358 +
   1.359 +        Local<String> name(zone_, unit_->module()->name());
   1.360 +        Local<StructType> type(zone_, StructType::New(zone_, name, descs));
   1.361 +        if (!type)
   1.362 +            return NULL;
   1.363 +
   1.364 +        Local<StructMap> map(zone_, type->newMap());
   1.365 +        return Struct::New(zone_, map, Heap::Tenure_Old);
   1.366 +    }
   1.367 +
   1.368 +    void visitEnumStatement(EnumStatement *stmt) {
   1.369 +    }
   1.370 +
   1.371 +    void visitNameProxy(NameProxy *proxy) {
   1.372 +        // Don't report errors here, since we'll report them later.
   1.373 +        Scope *scope = unit_->globalScope();
   1.374 +        Symbol *sym = scope->lookup(proxy->name());
   1.375 +        proxy->bind(sym);
   1.376 +    }
   1.377 +
   1.378 +    void visitFunctionStatement(FunctionStatement *node) {
   1.379 +        Local<FunctionType> type(zone_, node->sym()->type());
   1.380 +        if (!fillFunctionType(type, node->signature()))
   1.381 +            return;
   1.382 +
   1.383 +        unsigned offset;
   1.384 +        if (!builder_.add(type, &offset))
   1.385 +            return;
   1.386 +
   1.387 +        Descriptor::Visibility visibility = (node->token() == TOK_NATIVE || node->token() == TOK_FORWARD)
   1.388 +                                            ? Descriptor::Public
   1.389 +                                            : Descriptor::Private;
   1.390 +
   1.391 +        Descriptor *desc = Descriptor::New(zone_, node->name(), type, visibility);
   1.392 +        if (!desc)
   1.393 +            return;
   1.394 +        if (!descriptors_.append(new (pool_) ScopedRoot<Descriptor>(desc)))
   1.395 +            return;
   1.396 +    }
   1.397 +
   1.398 +    void visitVariableDeclaration(VariableDeclaration *node) {
   1.399 +        Local<Type> type(zone_, buildVariableType(node));
   1.400 +        if (!type)
   1.401 +            return;
   1.402 +
   1.403 +        // If we're in global scope, and we received a |const| qualifier, then
   1.404 +        // we extend this to array references as well. This is to sort of mimic
   1.405 +        // SP1 functionality, but also means that you cannot have changeable
   1.406 +        // const pointers in global scope.
   1.407 +        if ((node->quals() & TypeQual_Const) && type->isArray()) {
   1.408 +            if ((type = zone_->types()->qualify(type, TypeQual_Const)) == NULL)
   1.409 +                return;
   1.410 +        }
   1.411 +
   1.412 +        node->sym()->setType(type);
   1.413 +
   1.414 +        unsigned offset;
   1.415 +        if (!builder_.add(type, &offset))
   1.416 +            return;
   1.417 +
   1.418 +        Descriptor *desc = Descriptor::New(zone_, node->name(), type, Descriptor::Private);
   1.419 +        if (!desc)
   1.420 +            return;
   1.421 +        if (!descriptors_.append(new (pool_) ScopedRoot<Descriptor>(desc)))
   1.422 +            return;
   1.423 +    }
   1.424 +
   1.425 +    void visitImportStatement(ImportStatement *node) {
   1.426 +        // Eventually we'll handle |from| style imports as well.
   1.427 +        {
   1.428 +            PathComponent *path = node->path();
   1.429 +            while (path->next)
   1.430 +                path = path->next;
   1.431 +
   1.432 +            // Imports automatically go into the importScope. For now, we
   1.433 +            // error if there are any duplicate symbols entered into the
   1.434 +            // importScope.
   1.435 +            if (Symbol *sym = unit_->importScope()->localLookup(path->name)) {
   1.436 +                cc_.reportError(node->pos(), Message_DuplicateImport, path->name->chars());
   1.437 +                return;
   1.438 +            }
   1.439 +
   1.440 +            Local<Type> type(zone_, zone_->types()->getImportable());
   1.441 +            ImportSymbol *sym = new (pool_) ImportSymbol(unit_->importScope(), path->name, node->pos(), type, node->importIndex());
   1.442 +            if (!unit_->importScope()->addSymbol(sym))
   1.443 +                return;
   1.444 +        }
   1.445 +    }
   1.446 +
   1.447 +  private:
   1.448 +    StructureBufferBuilder builder_;
   1.449 +    Vector<ScopedRoot<Descriptor> *> descriptors_;
   1.450 +};
   1.451 +    
   1.452 +class NameBinder : public NameBinderBase
   1.453 +{
   1.454 +  public:
   1.455 +    class AutoLinkScope
   1.456 +    {
   1.457 +      public:
   1.458 +        AutoLinkScope(AutoLinkScope **prevp, Scope *scope)
   1.459 +          : prevp_(prevp),
   1.460 +            prev_(*prevp),
   1.461 +            scope_(scope)
   1.462 +        {
   1.463 +            if (scope_) {
   1.464 +                *prevp_ = this;
   1.465 +                if (prev_)
   1.466 +                    scope_->setParent(prev_->scope());
   1.467 +            }
   1.468 +        }
   1.469 +        ~AutoLinkScope() {
   1.470 +            if (scope_) {
   1.471 +                assert(*prevp_ == this);
   1.472 +                *prevp_ = prev_;
   1.473 +            }
   1.474 +        }
   1.475 +        Scope *scope() const {
   1.476 +            return scope_;
   1.477 +        }
   1.478 +
   1.479 +      private:
   1.480 +        AutoLinkScope **prevp_;
   1.481 +        AutoLinkScope *prev_;
   1.482 +        Scope *scope_;
   1.483 +    };
   1.484 +
   1.485 +  public:
   1.486 +    NameBinder(Zone *zone, CompileContext &cc, TranslationUnit *unit)
   1.487 +      : NameBinderBase(zone, cc, unit),
   1.488 +        link_(NULL)
   1.489 +    {
   1.490 +    }
   1.491 +
   1.492 +    void bind() {
   1.493 +        // Start with the import scope, and collect all imported names.
   1.494 +        AutoLinkScope importScope(&link_, unit_->importScope());
   1.495 +
   1.496 +        for (size_t i = 0; i < unit_->tree()->imports()->length(); i++)
   1.497 +            unit_->tree()->imports()->at(i)->accept(this);
   1.498 +
   1.499 +        // Next, enter the global scope and walk all statements.
   1.500 +        AutoLinkScope globalScope(&link_, unit_->globalScope());
   1.501 +        for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
   1.502 +            Statement *stmt = unit_->tree()->statements()->at(i);
   1.503 +            stmt->accept(this);
   1.504 +        }
   1.505 +    }
   1.506 +
   1.507 +    void visitVariableDeclaration(VariableDeclaration *node) {
   1.508 +        // Bind the initializer before registering the declaration, so that we
   1.509 +        // can error on bogus initializers (new x = x).
   1.510 +        if (node->initialization())
   1.511 +            node->initialization()->accept(this);
   1.512 +
   1.513 +        // If the node already has a symbol (meaning it was a global), then
   1.514 +        // we don't have to do anything more.
   1.515 +        if (node->sym())
   1.516 +            return;
   1.517 +
   1.518 +        Local<Type> type(zone_, buildVariableType(node));
   1.519 +        if (!type)
   1.520 +            return;
   1.521 +
   1.522 +        VariableSymbol *sym = new (pool_) VariableSymbol(link_->scope(), node->name(), node->pos(), type);
   1.523 +        node->setSymbol(sym);
   1.524 +       
   1.525 +        if (Symbol *other = link_->scope()->localLookup(sym->name())) {
   1.526 +            // Report, but allow errors to continue.
   1.527 +            cc_.reportError(sym->pos(), Message_RedeclaredName,
   1.528 +                            sym->name()->chars(),
   1.529 +                            other->pos().line,
   1.530 +                            other->pos().col);
   1.531 +            return;
   1.532 +        }
   1.533 +        
   1.534 +        link_->scope()->addSymbol(sym);
   1.535 +    }
   1.536 +    void visitEnumStatement(EnumStatement *node) {
   1.537 +    }
   1.538 +    void visitFunctionStatement(FunctionStatement *node) {
   1.539 +        AutoLinkScope argScope(&link_, node->argScope());
   1.540 +        AutoLinkScope varScope(&link_, node->varScope());
   1.541 +
   1.542 +        // If the function is in the global scope, we've already computed its
   1.543 +        // type.
   1.544 +        if (node->sym()->scope()->kind() != Scope::GLOBAL) {
   1.545 +            Local<FunctionType> type(zone_, node->sym()->type());
   1.546 +            if (!fillFunctionType(type, node->signature()))
   1.547 +                return;
   1.548 +        }
   1.549 +
   1.550 +        if (node->body())
   1.551 +            node->body()->accept(this);
   1.552 +    }
   1.553 +    void visitStructureStatement(StructureStatement *node) {
   1.554 +        assert(false);
   1.555 +    }
   1.556 +    void visitFunctionTypeStatement(FunctionTypeStatement *node) {
   1.557 +        assert(false);
   1.558 +    }
   1.559 +
   1.560 +    void visitNameProxy(NameProxy *proxy) {
   1.561 +        // We could have already bound globals.
   1.562 +        if (proxy->sym())
   1.563 +            return;
   1.564 +
   1.565 +        Scope *scope = link_->scope();
   1.566 +        Symbol *sym = scope->lookup(proxy->name());
   1.567 +        if (!sym)
   1.568 +            cc_.reportError(proxy->pos(), Message_IdentifierNotFound, proxy->name()->chars());
   1.569 +        proxy->bind(sym);
   1.570 +    }
   1.571 +    void visitAssignment(Assignment *node) {
   1.572 +        node->lvalue()->accept(this);
   1.573 +        node->expression()->accept(this);
   1.574 +    }
   1.575 +    void visitBinaryExpression(BinaryExpression *node) {
   1.576 +        node->left()->accept(this);
   1.577 +        node->right()->accept(this);
   1.578 +    }
   1.579 +    void visitReturnStatement(ReturnStatement *node) {
   1.580 +        node->expression()->accept(this);
   1.581 +    }
   1.582 +    void visitForStatement(ForStatement *node) {
   1.583 +        AutoLinkScope scope(&link_, node->scope());
   1.584 +
   1.585 +        if (node->initialization())
   1.586 +            node->initialization()->accept(this);
   1.587 +        if (node->condition())
   1.588 +            node->condition()->accept(this);
   1.589 +        if (node->update())
   1.590 +            node->update()->accept(this);
   1.591 +        node->body()->accept(this);
   1.592 +    }
   1.593 +    void visitBlockStatement(BlockStatement *node) {
   1.594 +        AutoLinkScope scope(&link_, node->scope());
   1.595 +        for (size_t i = 0; i < node->statements()->length(); i++)
   1.596 +            node->statements()->at(i)->accept(this);
   1.597 +    }
   1.598 +    void visitIntegerLiteral(IntegerLiteral *node) {
   1.599 +    }
   1.600 +    void visitExpressionStatement(ExpressionStatement *node) {
   1.601 +        node->expression()->accept(this);
   1.602 +    }
   1.603 +    void visitCallExpression(CallExpression *node) {
   1.604 +        node->callee()->accept(this);
   1.605 +        for (size_t i = 0; i < node->arguments()->length(); i++)
   1.606 +            node->arguments()->at(i)->accept(this);
   1.607 +    }
   1.608 +    void visitIfStatement(IfStatement *node) {
   1.609 +        node->condition()->accept(this);
   1.610 +        node->ifTrue()->accept(this);
   1.611 +        if (node->ifFalse())
   1.612 +            node->ifFalse()->accept(this);
   1.613 +    }
   1.614 +    void visitIndexExpression(IndexExpression *node) {
   1.615 +        node->left()->accept(this);
   1.616 +        node->right()->accept(this);
   1.617 +    }
   1.618 +    void visitFloatLiteral(FloatLiteral *node) {
   1.619 +    }
   1.620 +    void visitWhileStatement(WhileStatement *node) {
   1.621 +        node->condition()->accept(this);
   1.622 +        node->body()->accept(this);
   1.623 +    }
   1.624 +    void visitBreakStatement(BreakStatement *node) {
   1.625 +    }
   1.626 +    void visitContinueStatement(ContinueStatement *node) {
   1.627 +    }
   1.628 +    void visitStringLiteral(StringLiteral *node) {
   1.629 +    }
   1.630 +    void visitIncDecExpression(IncDecExpression *node) {
   1.631 +        node->expression()->accept(this);
   1.632 +    }
   1.633 +    void visitUnaryExpression(UnaryExpression *node) {
   1.634 +        if (node->tag())
   1.635 +            node->tag()->accept(this);
   1.636 +        node->expression()->accept(this);
   1.637 +    }
   1.638 +    void visitTernaryExpression(TernaryExpression *node) {
   1.639 +        node->condition()->accept(this);
   1.640 +        node->left()->accept(this);
   1.641 +        node->right()->accept(this);
   1.642 +    }
   1.643 +    void visitBooleanLiteral(BooleanLiteral *node) {
   1.644 +    }
   1.645 +    void visitFieldExpression(FieldExpression *node) {
   1.646 +        node->left()->accept(this);
   1.647 +    }
   1.648 +    void visitSwitchStatement(SwitchStatement *node) {
   1.649 +        node->expression()->accept(this);
   1.650 +        node->defaultCase()->accept(this);
   1.651 +        for (size_t i = 0; i < node->cases()->length(); i++) {
   1.652 +            // We don't test case expressions because they are literals.
   1.653 +            Case *c = node->cases()->at(i);
   1.654 +            c->statement()->accept(this);
   1.655 +        }
   1.656 +    }
   1.657 +    void visitArrayLiteral(ArrayLiteral *node) {
   1.658 +        for (size_t i = 0; i < node->expressions()->length(); i++)
   1.659 +            node->expressions()->at(i)->accept(this);
   1.660 +    }
   1.661 +    void visitImportStatement(ImportStatement *node) {
   1.662 +    }
   1.663 +
   1.664 +  private:
   1.665      AutoLinkScope *link_;
   1.666  };
   1.667  
   1.668 @@ -979,6 +1096,18 @@
   1.669  ke::BindNamesAndTypes(Zone *zone, CompileContext &cc, TranslationUnit *unit)
   1.670  {
   1.671      AutoEnterCompileContext enter(cc, unit);
   1.672 +
   1.673 +    // Because of the way globals work in Pawn, we need a precursor step to
   1.674 +    // actually enter global symbols into the global struct. They're already
   1.675 +    // in the global symbol table, so we know they won't clash. This process
   1.676 +    // also binds any remaining types on global variables.
   1.677 +    GlobalBuilder builder(zone, cc, unit);
   1.678 +    Local<Struct> globals(zone, builder.build());
   1.679 +    if (!globals || unit->failed())
   1.680 +        return;
   1.681 +
   1.682 +    unit->module()->setGlobals(globals);
   1.683 +
   1.684      NameBinder binder(zone, cc, unit);
   1.685      binder.bind();
   1.686  }