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