src/compiler/Scopes.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 195 c1ba166f3fc4
child 259 99ab8b19f0b9
permissions -rw-r--r--
Initial implementation of module imports.
[email protected]
     1
/* vim: set ts=4 sw=4 tw=99 et:
[email protected]
     2
 *
[email protected]
     3
 * Copyright (C) 2012 David Anderson
[email protected]
     4
 *
[email protected]
     5
 * This file is part of JITCraft.
[email protected]
     6
 *
[email protected]
     7
 * JITCraft is free software: you can redistribute it and/or modify it under
[email protected]
     8
 * the terms of the GNU General Public License as published by the Free
[email protected]
     9
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
    10
 * any later version.
[email protected]
    11
 * 
[email protected]
    12
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
[email protected]
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    15
 *
[email protected]
    16
 * You should have received a copy of the GNU General Public License along with
[email protected]
    17
 * JITCraft. If not, see http://www.gnu.org/licenses/.
[email protected]
    18
 */
[email protected]
    19
#include "../Zone.h"
[email protected]
    20
#include "../Types.h"
[email protected]
    21
#include "../TypeManager.h"
[email protected]
    22
#include "../Strings.h"
[email protected]
    23
#include "../Environments.h"
[email protected]
    24
#include "../Structures.h"
[email protected]
    25
#include "CompileContext.h"
[email protected]
    26
#include "Scopes.h"
[email protected]
    27
#include "../Spaces-inl.h"
[email protected]
    28
#include "../Heap-inl.h"
[email protected]
    29
[email protected]
    30
using namespace ke;
[email protected]
    31
[email protected]
    32
Scope::Scope(PoolAllocator &pool, Kind kind, Scope *enclosing)
[email protected]
    33
  : pool_(pool),
[email protected]
    34
    kind_(kind),
[email protected]
    35
    enclosing_(enclosing)
[email protected]
    36
{
[email protected]
    37
}
[email protected]
    38
[email protected]
    39
bool
[email protected]
    40
Scope::initialize()
[email protected]
    41
{
[email protected]
    42
    if (enclosing_ && !enclosing_->children_.append(this))
[email protected]
    43
        return false;
[email protected]
    44
    return true;
[email protected]
    45
}
[email protected]
    46
[email protected]
    47
void
[email protected]
    48
Scope::unlink()
[email protected]
    49
{
[email protected]
    50
    assert(empty());
[email protected]
    51
[email protected]
    52
    assert(enclosing_->children_[enclosing_->children_.length() - 1] == this);
[email protected]
    53
    enclosing_->children_.pop();
[email protected]
    54
[email protected]
    55
    // OOM here is swallowed and checked after compilation.
[email protected]
    56
    // :TODO: make the above true.
[email protected]
    57
    for (size_t i = 0; i < children_.length(); i++)
[email protected]
    58
        enclosing_->children_.append(children_[i]);
[email protected]
    59
}
[email protected]
    60
[email protected]
    61
Scope *
[email protected]
    62
Scope::unlinkIfEmpty()
[email protected]
    63
{
[email protected]
    64
    if (empty()) {
[email protected]
    65
        unlink();
[email protected]
    66
        return NULL;
[email protected]
    67
    }
[email protected]
    68
    return this;
[email protected]
    69
}
[email protected]
    70
[email protected]
    71
Symbol *
[email protected]
    72
Scope::localLookup(Handle<String> name)
[email protected]
    73
{
[email protected]
    74
    for (size_t i = 0; i < names_.length(); i++) {
[email protected]
    75
        if (names_[i]->name() == name)
[email protected]
    76
            return names_[i];
[email protected]
    77
    }
[email protected]
    78
    return NULL;
[email protected]
    79
}
[email protected]
    80
[email protected]
    81
Symbol  *
[email protected]
    82
Scope::lookup(Handle<String> name)
[email protected]
    83
{
[email protected]
    84
    for (Scope *scope = this; scope; scope = scope->enclosing()) {
[email protected]
    85
        if (Symbol *bound = scope->localLookup(name))
[email protected]
    86
            return bound;
[email protected]
    87
    }
[email protected]
    88
    return NULL;
[email protected]
    89
}
[email protected]
    90
[email protected]
    91
#if 0
[email protected]
    92
void
[email protected]
    93
Scope::setUsesLifoHeap()
[email protected]
    94
{
[email protected]
    95
    // Only one lifo slot is needed per scope.
[email protected]
    96
#if 0
[email protected]
    97
    if (lifoSlot_)
[email protected]
    98
        return;
[email protected]
    99
#endif
[email protected]
   100
[email protected]
   101
    // If the parent of this scope is the function scope, then this is the
[email protected]
   102
    // function's block scope, and the lifo slot is part of the Frame instead.
[email protected]
   103
    // Note we can get a function scope here if we're making a temporary in a
[email protected]
   104
    // function with no local variables.
[email protected]
   105
    if (kind() == FUNCTION || enclosing()->kind() == FUNCTION)
[email protected]
   106
        return;
[email protected]
   107
[email protected]
   108
    Local<Type> type(ZONE(), ZONE()->types()->getPrimitive(PrimitiveType_Native));
[email protected]
   109
    if (!type)
[email protected]
   110
        return;
[email protected]
   111
[email protected]
   112
    Local<String> name(ZONE());
[email protected]
   113
#if 0
[email protected]
   114
    lifoSlot_ = new (pool_) Variable(this, name, SourcePosition());
[email protected]
   115
    lifoSlot_->setType(type);
[email protected]
   116
#endif
[email protected]
   117
}
[email protected]
   118
#endif
[email protected]
   119
[email protected]
   120
bool
[email protected]
   121
Scope::addSymbol(Symbol *sym)
[email protected]
   122
{
[email protected]
   123
    return names_.append(sym);
[email protected]
   124
}
[email protected]
   125
[email protected]
   126
LocalScope::LocalScope(PoolAllocator &pool)
[email protected]
   127
  : Scope(pool, Scope::BLOCK, NULL)
[email protected]
   128
{
[email protected]
   129
}
[email protected]
   130
[email protected]
   131
LocalScope *
[email protected]
   132
LocalScope::New(PoolAllocator &pool)
[email protected]
   133
{
[email protected]
   134
    LocalScope *scope = new (pool) LocalScope(pool);
[email protected]
   135
    if (!scope->initialize())
[email protected]
   136
        return NULL;
[email protected]
   137
[email protected]
   138
    return scope;
[email protected]
   139
}
[email protected]
   140
[email protected]
   141
ArgumentScope::ArgumentScope(PoolAllocator &pool)
[email protected]
   142
  : Scope(pool, Scope::FUNCTION, NULL)
[email protected]
   143
{
[email protected]
   144
}
[email protected]
   145
[email protected]
   146
ArgumentScope *
[email protected]
   147
ArgumentScope::New(PoolAllocator &pool)
[email protected]
   148
{
[email protected]
   149
    ArgumentScope *scope = new (pool) ArgumentScope(pool);
[email protected]
   150
    if (!scope->initialize())
[email protected]
   151
        return NULL;
[email protected]
   152
[email protected]
   153
    return scope;
[email protected]
   154
}
[email protected]
   155
[email protected]
   156
GlobalScope::GlobalScope(PoolAllocator &pool, ImportScope *parent)
[email protected]
   157
  : Scope(pool, Scope::GLOBAL, parent)
[email protected]
   158
{
[email protected]
   159
}
[email protected]
   160
[email protected]
   161
GlobalScope *
[email protected]
   162
GlobalScope::New(PoolAllocator &pool, ImportScope *parent)
[email protected]
   163
{
[email protected]
   164
    GlobalScope *scope = new (pool) GlobalScope(pool, parent);
[email protected]
   165
    if (!scope->initialize())
[email protected]
   166
        return NULL;
[email protected]
   167
[email protected]
   168
    return scope;
[email protected]
   169
}
[email protected]
   170
[email protected]
   171
ImportScope::ImportScope(PoolAllocator &pool)
[email protected]
   172
  : Scope(pool, Scope::IMPORT, NULL)
[email protected]
   173
{
[email protected]
   174
}
[email protected]
   175
[email protected]
   176
ImportScope *
[email protected]
   177
ImportScope::New(PoolAllocator &pool)
[email protected]
   178
{
[email protected]
   179
    ImportScope *scope = new (pool) ImportScope(pool);
[email protected]
   180
    if (!scope->initialize())
[email protected]
   181
        return NULL;
[email protected]
   182
dvande[email protected]
   183
    return scope;
[email protected]
   184
}