1.1 --- a/src/compiler/Scopes.cpp Mon Jun 10 00:46:29 2013 +0100
1.2 +++ b/src/compiler/Scopes.cpp Mon Jun 10 01:04:43 2013 +0100
1.3 @@ -1,194 +1,194 @@
1.4 -/* vim: set ts=4 sw=4 tw=99 et:
1.5 - *
1.6 - * Copyright (C) 2012 David Anderson
1.7 - *
1.8 - * This file is part of JITCraft.
1.9 - *
1.10 - * JITCraft is free software: you can redistribute it and/or modify it under
1.11 - * the terms of the GNU General Public License as published by the Free
1.12 - * Software Foundation, either version 3 of the License, or (at your option)
1.13 - * any later version.
1.14 - *
1.15 - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 - *
1.19 - * You should have received a copy of the GNU General Public License along with
1.20 - * JITCraft. If not, see http://www.gnu.org/licenses/.
1.21 - */
1.22 -#include "../Zone.h"
1.23 -#include "../Types.h"
1.24 -#include "../TypeManager.h"
1.25 -#include "../Strings.h"
1.26 -#include "../Environments.h"
1.27 -#include "../Structures.h"
1.28 -#include "CompileContext.h"
1.29 -#include "Scopes.h"
1.30 -#include "../Spaces-inl.h"
1.31 -#include "../Heap-inl.h"
1.32 -
1.33 -using namespace ke;
1.34 -
1.35 -Scope::Scope(PoolAllocator &pool, Kind kind, Scope *enclosing)
1.36 - : pool_(pool),
1.37 - kind_(kind),
1.38 - enclosing_(enclosing)
1.39 -{
1.40 -}
1.41 -
1.42 -bool
1.43 -Scope::initialize()
1.44 -{
1.45 - if (enclosing_ && !enclosing_->children_.append(this))
1.46 - return false;
1.47 - return true;
1.48 -}
1.49 -
1.50 -void
1.51 -Scope::unlink()
1.52 -{
1.53 - assert(empty());
1.54 -
1.55 - assert(enclosing_->children_[enclosing_->children_.length() - 1] == this);
1.56 - enclosing_->children_.pop();
1.57 -
1.58 - // OOM here is swallowed and checked after compilation.
1.59 - // :TODO: make the above true.
1.60 - for (size_t i = 0; i < children_.length(); i++)
1.61 - enclosing_->children_.append(children_[i]);
1.62 -}
1.63 -
1.64 -Scope *
1.65 -Scope::unlinkIfEmpty()
1.66 -{
1.67 - if (empty()) {
1.68 - unlink();
1.69 - return NULL;
1.70 - }
1.71 - return this;
1.72 -}
1.73 -
1.74 -Symbol *
1.75 -Scope::localLookup(Handle<String> name)
1.76 -{
1.77 - for (size_t i = 0; i < names_.length(); i++) {
1.78 - if (names_[i]->name() == name)
1.79 - return names_[i];
1.80 - }
1.81 - return NULL;
1.82 -}
1.83 -
1.84 -Symbol *
1.85 -Scope::lookup(Handle<String> name)
1.86 -{
1.87 - for (Scope *scope = this; scope; scope = scope->enclosing()) {
1.88 - if (Symbol *bound = scope->localLookup(name))
1.89 - return bound;
1.90 - }
1.91 - return NULL;
1.92 -}
1.93 -
1.94 -#if 0
1.95 -void
1.96 -Scope::setUsesLifoHeap()
1.97 -{
1.98 - // Only one lifo slot is needed per scope.
1.99 -#if 0
1.100 - if (lifoSlot_)
1.101 - return;
1.102 -#endif
1.103 -
1.104 - // If the parent of this scope is the function scope, then this is the
1.105 - // function's block scope, and the lifo slot is part of the Frame instead.
1.106 - // Note we can get a function scope here if we're making a temporary in a
1.107 - // function with no local variables.
1.108 - if (kind() == FUNCTION || enclosing()->kind() == FUNCTION)
1.109 - return;
1.110 -
1.111 - Local<Type> type(ZONE(), ZONE()->types()->getPrimitive(PrimitiveType_Native));
1.112 - if (!type)
1.113 - return;
1.114 -
1.115 - Local<String> name(ZONE());
1.116 -#if 0
1.117 - lifoSlot_ = new (pool_) Variable(this, name, SourcePosition());
1.118 - lifoSlot_->setType(type);
1.119 -#endif
1.120 -}
1.121 -#endif
1.122 -
1.123 -bool
1.124 -Scope::addSymbol(Symbol *sym)
1.125 -{
1.126 - return names_.append(sym);
1.127 -}
1.128 -
1.129 -LocalScope::LocalScope(PoolAllocator &pool)
1.130 - : Scope(pool, Scope::BLOCK, NULL)
1.131 -{
1.132 -}
1.133 -
1.134 -LocalScope *
1.135 -LocalScope::New(PoolAllocator &pool)
1.136 -{
1.137 - LocalScope *scope = new (pool) LocalScope(pool);
1.138 - if (!scope->initialize())
1.139 - return NULL;
1.140 -
1.141 - return scope;
1.142 -}
1.143 -
1.144 -ArgumentScope::ArgumentScope(PoolAllocator &pool)
1.145 - : Scope(pool, Scope::FUNCTION, NULL)
1.146 -{
1.147 -}
1.148 -
1.149 -ArgumentScope *
1.150 -ArgumentScope::New(PoolAllocator &pool)
1.151 -{
1.152 - ArgumentScope *scope = new (pool) ArgumentScope(pool);
1.153 - if (!scope->initialize())
1.154 - return NULL;
1.155 -
1.156 - return scope;
1.157 -}
1.158 -
1.159 -GlobalScope::GlobalScope(PoolAllocator &pool, ImportScope *parent)
1.160 - : Scope(pool, Scope::GLOBAL, parent)
1.161 -{
1.162 -}
1.163 -
1.164 -GlobalScope *
1.165 -GlobalScope::New(PoolAllocator &pool, ImportScope *parent)
1.166 -{
1.167 - GlobalScope *scope = new (pool) GlobalScope(pool, parent);
1.168 - if (!scope->initialize())
1.169 - return NULL;
1.170 -
1.171 - return scope;
1.172 -}
1.173 -
1.174 -Symbol *
1.175 -GlobalScope::findExported(Handle<String> name)
1.176 -{
1.177 - for (size_t i = 0; i < exported_.length(); i++) {
1.178 - if (exported_[i]->name() == name)
1.179 - return exported_[i];
1.180 - }
1.181 - return NULL;
1.182 -}
1.183 -
1.184 -ImportScope::ImportScope(PoolAllocator &pool)
1.185 - : Scope(pool, Scope::IMPORT, NULL)
1.186 -{
1.187 -}
1.188 -
1.189 -ImportScope *
1.190 -ImportScope::New(PoolAllocator &pool)
1.191 -{
1.192 - ImportScope *scope = new (pool) ImportScope(pool);
1.193 - if (!scope->initialize())
1.194 - return NULL;
1.195 -
1.196 - return scope;
1.197 -}
1.198 +/* vim: set ts=4 sw=4 tw=99 et:
1.199 + *
1.200 + * Copyright (C) 2012 David Anderson
1.201 + *
1.202 + * This file is part of JITCraft.
1.203 + *
1.204 + * JITCraft is free software: you can redistribute it and/or modify it under
1.205 + * the terms of the GNU General Public License as published by the Free
1.206 + * Software Foundation, either version 3 of the License, or (at your option)
1.207 + * any later version.
1.208 + *
1.209 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.210 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.211 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.212 + *
1.213 + * You should have received a copy of the GNU General Public License along with
1.214 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.215 + */
1.216 +#include "../Zone.h"
1.217 +#include "../Types.h"
1.218 +#include "../TypeManager.h"
1.219 +#include "../Strings.h"
1.220 +#include "../Environments.h"
1.221 +#include "../Structures.h"
1.222 +#include "CompileContext.h"
1.223 +#include "Scopes.h"
1.224 +#include "../Spaces-inl.h"
1.225 +#include "../Heap-inl.h"
1.226 +
1.227 +using namespace ke;
1.228 +
1.229 +Scope::Scope(PoolAllocator &pool, Kind kind, Scope *enclosing)
1.230 + : pool_(pool),
1.231 + kind_(kind),
1.232 + enclosing_(enclosing)
1.233 +{
1.234 +}
1.235 +
1.236 +bool
1.237 +Scope::initialize()
1.238 +{
1.239 + if (enclosing_ && !enclosing_->children_.append(this))
1.240 + return false;
1.241 + return true;
1.242 +}
1.243 +
1.244 +void
1.245 +Scope::unlink()
1.246 +{
1.247 + assert(empty());
1.248 +
1.249 + assert(enclosing_->children_[enclosing_->children_.length() - 1] == this);
1.250 + enclosing_->children_.pop();
1.251 +
1.252 + // OOM here is swallowed and checked after compilation.
1.253 + // :TODO: make the above true.
1.254 + for (size_t i = 0; i < children_.length(); i++)
1.255 + enclosing_->children_.append(children_[i]);
1.256 +}
1.257 +
1.258 +Scope *
1.259 +Scope::unlinkIfEmpty()
1.260 +{
1.261 + if (empty()) {
1.262 + unlink();
1.263 + return NULL;
1.264 + }
1.265 + return this;
1.266 +}
1.267 +
1.268 +Symbol *
1.269 +Scope::localLookup(Handle<String> name)
1.270 +{
1.271 + for (size_t i = 0; i < names_.length(); i++) {
1.272 + if (names_[i]->name() == name)
1.273 + return names_[i];
1.274 + }
1.275 + return NULL;
1.276 +}
1.277 +
1.278 +Symbol *
1.279 +Scope::lookup(Handle<String> name)
1.280 +{
1.281 + for (Scope *scope = this; scope; scope = scope->enclosing()) {
1.282 + if (Symbol *bound = scope->localLookup(name))
1.283 + return bound;
1.284 + }
1.285 + return NULL;
1.286 +}
1.287 +
1.288 +#if 0
1.289 +void
1.290 +Scope::setUsesLifoHeap()
1.291 +{
1.292 + // Only one lifo slot is needed per scope.
1.293 +#if 0
1.294 + if (lifoSlot_)
1.295 + return;
1.296 +#endif
1.297 +
1.298 + // If the parent of this scope is the function scope, then this is the
1.299 + // function's block scope, and the lifo slot is part of the Frame instead.
1.300 + // Note we can get a function scope here if we're making a temporary in a
1.301 + // function with no local variables.
1.302 + if (kind() == FUNCTION || enclosing()->kind() == FUNCTION)
1.303 + return;
1.304 +
1.305 + Local<Type> type(ZONE(), ZONE()->types()->getPrimitive(PrimitiveType_Native));
1.306 + if (!type)
1.307 + return;
1.308 +
1.309 + Local<String> name(ZONE());
1.310 +#if 0
1.311 + lifoSlot_ = new (pool_) Variable(this, name, SourcePosition());
1.312 + lifoSlot_->setType(type);
1.313 +#endif
1.314 +}
1.315 +#endif
1.316 +
1.317 +bool
1.318 +Scope::addSymbol(Symbol *sym)
1.319 +{
1.320 + return names_.append(sym);
1.321 +}
1.322 +
1.323 +LocalScope::LocalScope(PoolAllocator &pool)
1.324 + : Scope(pool, Scope::BLOCK, NULL)
1.325 +{
1.326 +}
1.327 +
1.328 +LocalScope *
1.329 +LocalScope::New(PoolAllocator &pool)
1.330 +{
1.331 + LocalScope *scope = new (pool) LocalScope(pool);
1.332 + if (!scope->initialize())
1.333 + return NULL;
1.334 +
1.335 + return scope;
1.336 +}
1.337 +
1.338 +ArgumentScope::ArgumentScope(PoolAllocator &pool)
1.339 + : Scope(pool, Scope::FUNCTION, NULL)
1.340 +{
1.341 +}
1.342 +
1.343 +ArgumentScope *
1.344 +ArgumentScope::New(PoolAllocator &pool)
1.345 +{
1.346 + ArgumentScope *scope = new (pool) ArgumentScope(pool);
1.347 + if (!scope->initialize())
1.348 + return NULL;
1.349 +
1.350 + return scope;
1.351 +}
1.352 +
1.353 +GlobalScope::GlobalScope(PoolAllocator &pool, ImportScope *parent)
1.354 + : Scope(pool, Scope::GLOBAL, parent)
1.355 +{
1.356 +}
1.357 +
1.358 +GlobalScope *
1.359 +GlobalScope::New(PoolAllocator &pool, ImportScope *parent)
1.360 +{
1.361 + GlobalScope *scope = new (pool) GlobalScope(pool, parent);
1.362 + if (!scope->initialize())
1.363 + return NULL;
1.364 +
1.365 + return scope;
1.366 +}
1.367 +
1.368 +Symbol *
1.369 +GlobalScope::findExported(Handle<String> name)
1.370 +{
1.371 + for (size_t i = 0; i < exported_.length(); i++) {
1.372 + if (exported_[i]->name() == name)
1.373 + return exported_[i];
1.374 + }
1.375 + return NULL;
1.376 +}
1.377 +
1.378 +ImportScope::ImportScope(PoolAllocator &pool)
1.379 + : Scope(pool, Scope::IMPORT, NULL)
1.380 +{
1.381 +}
1.382 +
1.383 +ImportScope *
1.384 +ImportScope::New(PoolAllocator &pool)
1.385 +{
1.386 + ImportScope *scope = new (pool) ImportScope(pool);
1.387 + if (!scope->initialize())
1.388 + return NULL;
1.389 +
1.390 + return scope;
1.391 +}