src/compiler/SemanticAnalysis.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 251 ca99a81745fe
child 258 241d082d6d89
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 SourcePawn.
[email protected]
     6
 *
[email protected]
     7
 * SourcePawn 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
 * SourcePawn 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
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
[email protected]
    18
 */
[email protected]
    19
#include <assert.h>
[email protected]
    20
#include "../Zone.h"
[email protected]
    21
#include "../Strings.h"
[email protected]
    22
#include "../NativeTable.h"
[email protected]
    23
#include "../TypeManager.h"
[email protected]
    24
#include "../Modules.h"
[email protected]
    25
#include "../Code.h"
[email protected]
    26
#include "SemanticAnalysis.h"
[email protected]
    27
#include "../Heap-inl.h"
[email protected]
    28
[email protected]
    29
using namespace ke;
[email protected]
    30
[email protected]
    31
#define __ emitter_.
[email protected]
    32
[email protected]
    33
typedef BytecodeEmitter::Label Label;
[email protected]
    34
[email protected]
    35
class AutoEnterScope
[email protected]
    36
{
[email protected]
    37
  public:
[email protected]
    38
    AutoEnterScope(Scope **prevp, Scope *scope)
[email protected]
    39
      : prevp_(prevp),
[email protected]
    40
        prev_(*prevp),
[email protected]
    41
        scope_(scope)
[email protected]
    42
    {
[email protected]
    43
        if (!scope_)
[email protected]
    44
            return;
[email protected]
    45
        prev_ = *prevp_;
[email protected]
    46
        *prevp_ = scope;
[email protected]
    47
        assert(prev_ == scope->enclosing());
[email protected]
    48
    }
[email protected]
    49
    ~AutoEnterScope()
[email protected]
    50
    {
[email protected]
    51
        if (scope_)
[email protected]
    52
            *prevp_ = prev_;
[email protected]
    53
    }
[email protected]
    54
  private:
[email protected]
    55
    Scope **prevp_;
[email protected]
    56
    Scope *prev_;
[email protected]
    57
    Scope *scope_;
[email protected]
    58
};
[email protected]
    59
[email protected]
    60
SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit)
[email protected]
    61
  : zone_(zone),
[email protected]
    62
    pool_(zone->pool()),
[email protected]
    63
    cc_(cc),
[email protected]
    64
    unit_(unit),
[email protected]
    65
    hir_(NULL),
[email protected]
    66
    emitter_(pool_),
[email protected]
    67
    loop_(NULL)
[email protected]
    68
{
[email protected]
    69
}
[email protected]
    70
[email protected]
    71
SemanticAnalysis::SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun)
[email protected]
    72
  : zone_(parent->zone_),
[email protected]
    73
    pool_(parent->pool_),
[email protected]
    74
    cc_(parent->cc_),
[email protected]
    75
    unit_(parent->unit_),
[email protected]
    76
    hir_(NULL),
[email protected]
    77
    scope_(parent->scope_),
[email protected]
    78
    emitter_(pool_),
[email protected]
    79
    fun_(fun),
[email protected]
    80
    loop_(NULL),
[email protected]
    81
    control_(NULL)
[email protected]
    82
{
[email protected]
    83
[email protected]
    84
}
[email protected]
    85
[email protected]
    86
void
[email protected]
    87
SemanticAnalysis::analyze()
[email protected]
    88
{
[email protected]
    89
    scope_ = unit_->globalScope();
[email protected]
    90
[email protected]
    91
    for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
[email protected]
    92
        Statement *stmt = unit_->tree()->statements()->at(i);
[email protected]
    93
        stmt->accept(this);
[email protected]
    94
    }
[email protected]
    95
[email protected]
    96
    Local<Code> code(zone_, __ finish());
[email protected]
    97
    if (!code)
[email protected]
    98
        return;
[email protected]
    99
    code->setModule(unit_->module());
[email protected]
   100
    unit_->module()->setCode(code);
[email protected]
   101
[email protected]
   102
#ifndef NDEBUG
[email protected]
   103
//    Disassemble(code, stdout);
[email protected]
   104
#endif
[email protected]
   105
}
[email protected]
   106
[email protected]
   107
void
[email protected]
   108
SemanticAnalysis::visitFunctionStatement(FunctionStatement *stmt)
[email protected]
   109
{
[email protected]
   110
    FunctionSymbol *sym = stmt->sym();
[email protected]
   111
    Local<FunctionType> type(zone_, sym->type());
[email protected]
   112
[email protected]
   113
    assert(scope_->kind() == Scope::GLOBAL);
[email protected]
   114
[email protected]
   115
    if (stmt->token() == TOK_NATIVE) {
[email protected]
   116
        // Define the native now.
[email protected]
   117
        size_t index;
[email protected]
   118
        if (!zone_->nativeTable()->add(stmt->name(), &index))
[email protected]
   119
            return;
[email protected]
   120
[email protected]
   121
        Local<Native> native(zone_, Native::New(zone_, type, index));
[email protected]
   122
        if (!native)
[email protected]
   123
            return;
[email protected]
   124
[email protected]
   125
        __ object(native);
[email protected]
   126
        __ setglobal(sym);
[email protected]
   127
        __ pop();
[email protected]
   128
    }
[email protected]
   129
[email protected]
   130
    // If the function is a forward or native, we don't have to do anything.
[email protected]
   131
    if (stmt->token() == TOK_NATIVE || stmt->token() == TOK_FORWARD)
[email protected]
   132
        return;
[email protected]
   133
[email protected]
   134
    SemanticAnalysis child(this, type);
[email protected]
   135
    Local<Function> funobj(zone_, child.compile(stmt));
[email protected]
   136
    if (!funobj)
[email protected]
   137
        return;
[email protected]
   138
[email protected]
   139
    __ object(funobj);
[email protected]
   140
    __ setglobal(sym);
[email protected]
   141
    if (stmt->token() == TOK_PUBLIC)
[email protected]
   142
        __ public_();
[email protected]
   143
    else
[email protected]
   144
        __ pop();
[email protected]
   145
}
[email protected]
   146
[email protected]
   147
Function *
[email protected]
   148
SemanticAnalysis::compile(FunctionStatement *stmt)
[email protected]
   149
{
[email protected]
   150
    __ setFirstLine(stmt->pos().line);
[email protected]
   151
[email protected]
   152
    AutoEnterScope argScope(&scope_, stmt->argScope());
[email protected]
   153
[email protected]
   154
    for (size_t i = 0; i < stmt->signature().parameters()->length(); i++) {
[email protected]
   155
        Parameter *param = stmt->signature().parameters()->at(i);
[email protected]
   156
        param->sym()->allocate(VariableSymbol::Arg, i);
[email protected]
   157
    }
[email protected]
   158
[email protected]
   159
    AutoEnterScope varScope(&scope_, stmt->varScope());
[email protected]
   160
    ControlStatus status = visitForControl(stmt->body());
[email protected]
   161
[email protected]
   162
    if (!fun_->returnType()->isVoid()) {
[email protected]
   163
        // If the function is not void, report if not all paths return a value.
[email protected]
   164
        // All other incorrect return cases have been caught already.
[email protected]
   165
        if (status.returns() == Returns_Sometimes) {
[email protected]
   166
            cc_.reportError(stmt->pos(), Message_NotAllPathsReturnValue);
[email protected]
   167
            return NULL;
[email protected]
   168
        }
[email protected]
   169
[email protected]
   170
        if (status.returns() == Returns_Never) {
[email protected]
   171
            cc_.reportError(stmt->pos(), Message_MissingReturnValue);
[email protected]
   172
            return NULL;
[email protected]
   173
        }
[email protected]
   174
    }
[email protected]
   175
[email protected]
   176
    Local<Code> code(zone_, __ finish());
[email protected]
   177
    if (!code)
[email protected]
   178
        return NULL;
[email protected]
   179
    code->setModule(unit_->module());
[email protected]
   180
[email protected]
   181
    Local<Function> fun(zone_, Function::New(zone_, code, fun_, stmt->name(), stmt->pos()));
[email protected]
   182
    if (!fun)
[email protected]
   183
        return NULL;
[email protected]
   184
[email protected]
   185
#ifndef NDEBUG
[email protected]
   186
//    Disassemble(code, stdout);
[email protected]
   187
#endif
[email protected]
   188
[email protected]
   189
    return fun;
[email protected]
   190
}
[email protected]
   191
[email protected]
   192
void 
[email protected]
   193
SemanticAnalysis::visitBlockStatement(BlockStatement *node)
[email protected]
   194
{
[email protected]
   195
    AutoEnterScope scope(&scope_, node->scope());
[email protected]
   196
[email protected]
   197
    for (size_t i = 0; i < node->statements()->length(); i++) {
[email protected]
   198
        Statement *stmt = node->statements()->at(i);
[email protected]
   199
        __ note_position(stmt->pos());
[email protected]
   200
        stmt->accept(this);
[email protected]
   201
        assert(__ stackDepth() == 0);
[email protected]
   202
    }
[email protected]
   203
}
[email protected]
   204
[email protected]
   205
void
[email protected]
   206
SemanticAnalysis::visitEnumStatement(EnumStatement *node)
[email protected]
   207
{
[email protected]
   208
}
[email protected]
   209
[email protected]
   210
void
[email protected]
   211
SemanticAnalysis::visitExpressionStatement(ExpressionStatement *node)
[email protected]
   212
{
[email protected]
   213
    // While building HIR, we enter new pool and root scopes, since HIR
[email protected]
   214
    // creation is purely temporary.
[email protected]
   215
    PoolScope poolScope(zone_->pool());
[email protected]
   216
    RootScope rootScope(zone_);
[email protected]
   217
[email protected]
   218
    HIR *hir = eval(node->expression());
[email protected]
   219
    if (!hir)
[email protected]
   220
        return;
[email protected]
   221
[email protected]
   222
    discharge(hir);
[email protected]
   223
    if (!hir->type()->isVoid())
[email protected]
   224
        __ pop();
[email protected]
   225
}
[email protected]
   226
[email protected]
   227
void
[email protected]
   228
SemanticAnalysis::ControlScope::merge(const ControlStatus &left, const ControlStatus &right)
[email protected]
   229
{
[email protected]
   230
    // If both sides terminate in a return, we consider this a return as well.
[email protected]
   231
    if (left.returns() == Returns_Always && right.returns() == Returns_Always) {
[email protected]
   232
        terminate(Returns_Always);
[email protected]
   233
        return;
[email protected]
   234
    }
[email protected]
   235
[email protected]
   236
    ReturnStatus status = (left.returns() == Returns_Never && right.returns() == Returns_Never)
[email protected]
   237
                          ? Returns_Never
[email protected]
   238
                          : Returns_Sometimes;
[email protected]
   239
[email protected]
   240
    if (left.terminates() && right.terminates())
[email protected]
   241
        terminate(status);
[email protected]
   242
    else
[email protected]
   243
        returns(status);
[email protected]
   244
}
[email protected]
   245
[email protected]
   246
SemanticAnalysis::ControlStatus
[email protected]
   247
SemanticAnalysis::visitForControl(Statement *statement)
[email protected]
   248
{
[email protected]
   249
    ControlScope scope(&control_);
[email protected]
   250
    statement->accept(this);
[email protected]
   251
    return scope.status();
[email protected]
   252
}
[email protected]
   253
[email protected]
   254
void
[email protected]
   255
SemanticAnalysis::visitIfStatement(IfStatement *node)
[email protected]
   256
{
[email protected]
   257
    Label join;
[email protected]
   258
[email protected]
   259
    ControlStatus trueBranch;
[email protected]
   260
    ControlStatus falseBranch(false, Returns_Never);
[email protected]
   261
[email protected]
   262
    if (!node->ifFalse()) {
[email protected]
   263
        Label success;
[email protected]
   264
[email protected]
   265
        visitForTest(node->condition(), &success, &join, &success);
[email protected]
   266
        __ bind(&success);
[email protected]
   267
        trueBranch = visitForControl(node->ifTrue());
[email protected]
   268
    } else {
[email protected]
   269
        Label success, next;
[email protected]
   270
[email protected]
   271
        visitForTest(node->condition(), &success, &next, &success);
[email protected]
   272
        __ bind(&success);
[email protected]
   273
        trueBranch = visitForControl(node->ifTrue());
[email protected]
   274
        __ jump(&join);
[email protected]
   275
        __ bind(&next);
[email protected]
   276
        falseBranch = visitForControl(node->ifFalse());
[email protected]
   277
    }
[email protected]
   278
[email protected]
   279
    __ bind(&join);
[email protected]
   280
[email protected]
   281
    control_->merge(trueBranch, falseBranch);
[email protected]
   282
}
[email protected]
   283
[email protected]
   284
void
[email protected]
   285
SemanticAnalysis::requireLifoStack()
[email protected]
   286
{
[email protected]
   287
    if (!loop_)
[email protected]
   288
        return;
[email protected]
   289
[email protected]
   290
    loop_->setUsesLifoStack();
[email protected]
   291
}
[email protected]
   292
[email protected]
   293
SemanticAnalysis::ControlStatus
[email protected]
   294
SemanticAnalysis::visitLoopBody(Statement *stmt)
[email protected]
   295
{
[email protected]
   296
    // We always mark off a loop body, so on each edge, we can balance the
[email protected]
   297
    // LIFO stack. Right now, we don't have the ability to budget the LIFO
[email protected]
   298
    // stack ahead of time. If we turn out to never need the LIFO stack, we
[email protected]
   299
    // mark it as unnecessary. (The interpreter can't really make use of this,
[email protected]
   300
    // but the JIT can).
[email protected]
   301
    //
[email protected]
   302
    // Note that balancing the LIFO stack isn't even necessary, so this
[email protected]
   303
    // feature may be removed. All this mechanism does is mitigate the
[email protected]
   304
    // possibility that a long-running loop will overflow the LIFO region
[email protected]
   305
    // and therefore start requiring nursery allocations.
[email protected]
   306
    int pos = __ enterloop();
[email protected]
   307
    ControlStatus status = visitForControl(stmt);
[email protected]
   308
    __ leaveloop();
[email protected]
   309
[email protected]
   310
    if (loop_->usesLifoStack())
[email protected]
   311
        __ setLoopUsesLifoStack(pos);
[email protected]
   312
[email protected]
   313
    return status;
[email protected]
   314
}
[email protected]
   315
[email protected]
   316
void
[email protected]
   317
SemanticAnalysis::visitForStatement(ForStatement *node)
[email protected]
   318
{
[email protected]
   319
    AutoEnterScope scope(&scope_, node->scope());
[email protected]
   320
    PoolScope poolScope(zone_->pool());
[email protected]
   321
    RootScope rootScope(zone_);
[email protected]
   322
[email protected]
   323
    Label join, update;
[email protected]
   324
    LoopScope loop(scope_, &join, &update, &loop_);
[email protected]
   325
[email protected]
   326
    if (node->initialization())
[email protected]
   327
        node->initialization()->accept(this);
[email protected]
   328
[email protected]
   329
    Label header, test;
[email protected]
   330
    if (node->condition())
[email protected]
   331
        __ jump(&test);
[email protected]
   332
[email protected]
   333
    __ bind(&header);
[email protected]
   334
    ControlStatus status = visitLoopBody(node->body());
[email protected]
   335
[email protected]
   336
    __ bind(&update);
[email protected]
   337
    if (node->update())
[email protected]
   338
        node->update()->accept(this);
[email protected]
   339
[email protected]
   340
    if (node->condition()) {
[email protected]
   341
        __ bind(&test);
[email protected]
   342
        visitForTest(node->condition(), &header, &join, &join);
[email protected]
   343
    } else {
[email protected]
   344
        __ jump(&header);
[email protected]
   345
    }
[email protected]
   346
[email protected]
   347
    __ bind(&join);
[email protected]
   348
    
[email protected]
   349
    // If there was a condition, we treat this like a while loop. Otherwise,
[email protected]
   350
    // the join point is only reachable if the loop contains a break. The
[email protected]
   351
    // termination status doesn't tell us anything yet.
[email protected]
   352
    if (node->condition() || loop.breaks())
[email protected]
   353
        control_->merge(status, ControlStatus(false, Returns_Never));
[email protected]
   354
    else
[email protected]
   355
        control_->terminate(status.returns());
[email protected]
   356
[email protected]
   357
    assert(__ stackDepth() == 0);
[email protected]
   358
}
[email protected]
   359
[email protected]
   360
void
[email protected]
   361
SemanticAnalysis::visitWhileStatement(WhileStatement *node)
[email protected]
   362
{
[email protected]
   363
    PoolScope poolScope(zone_->pool());
[email protected]
   364
    RootScope rootScope(zone_);
[email protected]
   365
[email protected]
   366
    Label header, join;
[email protected]
   367
[email protected]
   368
    __ bind(&header);
[email protected]
   369
[email protected]
   370
    ControlStatus status;
[email protected]
   371
[email protected]
   372
    LoopScope loop(scope_, &join, &header, &loop_);
[email protected]
   373
    if (node->token() == TOK_WHILE) {
[email protected]
   374
        Label body;
[email protected]
   375
        visitForTest(node->condition(), &body, &join, &body);
[email protected]
   376
        status = visitLoopBody(node->body());
[email protected]
   377
        __ jump(&header);
[email protected]
   378
    } else {
[email protected]
   379
        status = visitLoopBody(node->body());
[email protected]
   380
        visitForTest(node->condition(), &header, &join, &join);
[email protected]
   381
    }
[email protected]
   382
[email protected]
   383
    __ bind(&join);
[email protected]
   384
[email protected]
   385
    // Detect loops that will always have a fork in the contro-flow graph.
[email protected]
   386
    // This will be a |while| loop, or a |do-while| loop whose body does
[email protected]
   387
    // not terminate. For now, we treat these as an |if| regardless of the
[email protected]
   388
    // direction the branch can take. That means the control-flow is always
[email protected]
   389
    // conditional.
[email protected]
   390
    if (node->token() == TOK_WHILE || status.terminates() || loop.breaks()) {
[email protected]
   391
        control_->merge(status, ControlStatus(false, Returns_Never));
[email protected]
   392
    } else {
[email protected]
   393
        // At this point, we've got a do-while loop with a |return| or
[email protected]
   394
        // |continue|, meaning that the rest of the block is unreachable.
[email protected]
   395
        control_->terminate(status.returns());
[email protected]
   396
    }
[email protected]
   397
[email protected]
   398
    assert(__ stackDepth() == 0);
[email protected]
   399
}
[email protected]
   400
[email protected]
   401
static inline Heap::Tenure
[email protected]
   402
InferTenure(Symbol *sym)
[email protected]
   403
{
[email protected]
   404
    if (sym->scope()->kind() == Scope::GLOBAL)
[email protected]
   405
        return Heap::Tenure_Old;
[email protected]
   406
[email protected]
   407
    // SP2's array semantics allow any array kind to escape, so we must use
[email protected]
   408
    // the normal heap. (Hopefully escape analysis can ease this pain later).
[email protected]
   409
    if (sym->type()->isArray())
[email protected]
   410
        return Heap::Tenure_Default;
[email protected]
   411
[email protected]
   412
    return Heap::Tenure_Default;
[email protected]
   413
}
[email protected]
   414
[email protected]
   415
void
[email protected]
   416
SemanticAnalysis::visitVariableDeclaration(VariableDeclaration *node)
[email protected]
   417
{
[email protected]
   418
    PoolScope poolScope(zone_->pool());
[email protected]
   419
    RootScope rootScope(zone_);
[email protected]
   420
[email protected]
   421
    Local<Type> type(zone_, node->sym()->type());
[email protected]
   422
[email protected]
   423
    if (node->sym()->type()->isVoid()) {
[email protected]
   424
        cc_.reportError(node->type()->pos(), Message_CannotDeclareVoid);
[email protected]
   425
        return;
[email protected]
   426
    }
[email protected]
   427
[email protected]
   428
    if (node->initialization()) {
[email protected]
   429
        HIR *hir = NULL;
[email protected]
   430
[email protected]
   431
        if ((hir = eval(node->initialization())) == NULL)
[email protected]
   432
            return;
[email protected]
   433
        if ((hir = coerce(hir, type, Coerce_Decl)) == NULL)
[email protected]
   434
            return;
[email protected]
   435
[email protected]
   436
        discharge(hir);
[email protected]
   437
    } else {
[email protected]
   438
        if (type->isInt32() || type->isEnum() ||
[email protected]
   439
            type->isChar() || type->isBool())
[email protected]
   440
        {
[email protected]
   441
            __ int_(0);
[email protected]
   442
        } else if (type->isFloat()) {
[email protected]
   443
            __ float_(0);
[email protected]
   444
        } else if (type->isArray()) {
[email protected]
   445
            Local<ArrayType> atype(zone_, ArrayType::cast(type));
[email protected]
   446
            Local<ArrayMap> map(zone_, atype->newMap());
[email protected]
   447
            if (!map)
[email protected]
   448
                return;
[email protected]
   449
[email protected]
   450
            if (atype->isFixedLength()) {
[email protected]
   451
                __ newarray(OP_NEWFIXED, map, InferTenure(node->sym()));
[email protected]
   452
            } else if (node->dims()->at(0)) {
[email protected]
   453
                // This may not be true if we allow typedefing, so :TODO:
[email protected]
   454
                // figure out proper semantics.
[email protected]
   455
                assert(node->dims()->length() == atype->levels());
[email protected]
   456
[email protected]
   457
                Local<Type> i32(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
[email protected]
   458
                for (size_t i = 0; i < node->dims()->length(); i++) {
[email protected]
   459
                    Expression *expr = node->dims()->at(i);
[email protected]
   460
                    if (expr) {
[email protected]
   461
                        HIR *hir = eval(expr);
[email protected]
   462
                        if (!hir || ((hir = coerce(hir, i32, Coerce_Arg)) == NULL))
[email protected]
   463
                            return;
[email protected]
   464
                        discharge(hir);
[email protected]
   465
                    } else {
[email protected]
   466
                        __ int_(0);
[email protected]
   467
                    }
[email protected]
   468
                }
[email protected]
   469
                __ newarray(OP_NEWSIZED, map, InferTenure(node->sym()));
[email protected]
   470
            } else {
[email protected]
   471
                __ newarray(OP_NEWEMPTY, map, InferTenure(node->sym()));
[email protected]
   472
            }
[email protected]
   473
        } else {
[email protected]
   474
            assert(false);
[email protected]
   475
        }
[email protected]
   476
    }
[email protected]
   477
[email protected]
   478
    if (node->sym()->scope()->kind() != Scope::GLOBAL) {
[email protected]
   479
        if (!__ allocate(node->sym()))
[email protected]
   480
            return;
[email protected]
   481
    }
[email protected]
   482
[email protected]
   483
    set(node->sym());
[email protected]
   484
    __ pop();
[email protected]
   485
}
[email protected]
   486
[email protected]
   487
void
[email protected]
   488
SemanticAnalysis::visitReturnStatement(ReturnStatement *node)
[email protected]
   489
{
[email protected]
   490
    control_->returns(Returns_Always);
[email protected]
   491
[email protected]
   492
    if (!node->expression()) {
[email protected]
   493
        if (!fun_->returnType()->isVoid()) {
[email protected]
   494
            cc_.reportError(node->pos(), Message_MissingReturnValue);
[email protected]
   495
            return;
[email protected]
   496
        }
[email protected]
   497
[email protected]
   498
        __ returnvoid();
[email protected]
   499
        return;
[email protected]
   500
    }
[email protected]
   501
[email protected]
   502
    if (fun_->returnType()->isVoid()) {
[email protected]
   503
        cc_.reportError(node->pos(), Message_UsedVoidReturn);
[email protected]
   504
        return;
[email protected]
   505
    }
[email protected]
   506
[email protected]
   507
    PoolScope poolScope(zone_->pool());
[email protected]
   508
    RootScope rootScope(zone_);
[email protected]
   509
[email protected]
   510
    HIR *hir = eval(node->expression());
[email protected]
   511
    if (!hir)
[email protected]
   512
        return;
[email protected]
   513
[email protected]
   514
    Local<Type> returnType(zone_, fun_->returnType());
[email protected]
   515
    if ((hir = coerce(hir, returnType, Coerce_Return)) == NULL)
[email protected]
   516
        return;
[email protected]
   517
[email protected]
   518
    discharge(hir);
[email protected]
   519
[email protected]
   520
    if (hir->type()->isVoid())
[email protected]
   521
        __ returnvoid();
[email protected]
   522
    else
[email protected]
   523
        __ return_();
[email protected]
   524
}
[email protected]
   525
[email protected]
   526
void
[email protected]
   527
SemanticAnalysis::visitBinaryExpression(BinaryExpression *node)
[email protected]
   528
{
[email protected]
   529
    HIR *left = eval(node->left());
[email protected]
   530
    HIR *right = eval(node->right());
[email protected]
   531
    if (!left || !right)
[email protected]
   532
        return;
[email protected]
   533
[email protected]
   534
    hir_ = binary(node, node->token(), left, right);
[email protected]
   535
}
[email protected]
   536
[email protected]
   537
bool
[email protected]
   538
SemanticAnalysis::checkArgumentCount(Handle<FunctionType> type, unsigned actual)
[email protected]
   539
{
[email protected]
   540
    if (type->parameters()->length() == actual)
[email protected]
   541
        return true;
[email protected]
   542
[email protected]
   543
    if (actual > type->parameters()->length()) {
[email protected]
   544
        if (!type->isNative() || !type->isNativeVariadic())
[email protected]
   545
            return false;
[email protected]
   546
        return true;
[email protected]
   547
    }
[email protected]
   548
[email protected]
   549
    if (!type->defaults())
[email protected]
   550
        return false;
[email protected]
   551
[email protected]
   552
    // Missing arguments. See if there's a default argument for each missing
[email protected]
   553
    // actual.
[email protected]
   554
    for (unsigned i = actual; i < type->parameters()->length(); i++) {
[email protected]
   555
        if (!type->defaults()->at(i))
[email protected]
   556
            return false;
[email protected]
   557
    }
[email protected]
   558
[email protected]
   559
    return true;
[email protected]
   560
}
[email protected]
   561
[email protected]
   562
void
[email protected]
   563
SemanticAnalysis::visitNameProxy(NameProxy *proxy)
[email protected]
   564
{
[email protected]
   565
    Symbol *sym = proxy->sym();
[email protected]
   566
[email protected]
   567
    switch (sym->kind()) {
[email protected]
   568
      case Symbol::kImport:
[email protected]
   569
      {
[email protected]
   570
        ImportSymbol *import = sym->toImport();
[email protected]
   571
        hir_ = new (pool_) HImport(proxy, sym->type(), import->index());
[email protected]
   572
        break;
[email protected]
   573
      }
[email protected]
   574
[email protected]
   575
      case Symbol::kConstant:
[email protected]
   576
      {
[email protected]
   577
        ConstantSymbol *val = sym->toConstant();
[email protected]
   578
        hir_ = new (pool_) HInteger(proxy, sym->type(), val->value().toInt());
[email protected]
   579
        break;
[email protected]
   580
      }
[email protected]
   581
      
[email protected]
   582
      // This is kind of screwy. Symbol::kFunction is like a constant we can't
[email protected]
   583
      // evaluate yet. Maybe we should extract the GLOBAL case and say that
[email protected]
   584
      // kFunction is only used for global functions (and use const vars for
[email protected]
   585
      // local functions).
[email protected]
   586
      case Symbol::kFunction:
[email protected]
   587
      case Symbol::kVariable:
[email protected]
   588
      {
[email protected]
   589
        Scope *in = sym->scope();
[email protected]
   590
        switch (in->kind()) {
[email protected]
   591
          case Scope::GLOBAL:
[email protected]
   592
            hir_ = new (pool_) HGlobal(proxy, sym);
[email protected]
   593
            break;
[email protected]
   594
[email protected]
   595
          case Scope::FUNCTION:
[email protected]
   596
          {
[email protected]
   597
            VariableSymbol *var = sym->toVariable();
[email protected]
   598
            assert(var->storage() == VariableSymbol::Arg);
[email protected]
   599
            hir_ = new (pool_) HLocal(proxy, var);
[email protected]
   600
            break;
[email protected]
   601
          }
[email protected]
   602
[email protected]
   603
          default:
[email protected]
   604
          {
[email protected]
   605
            VariableSymbol *var = sym->toVariable();
[email protected]
   606
            assert(in->kind() == Scope::BLOCK);
[email protected]
   607
            assert(var->storage() == VariableSymbol::Local);
[email protected]
   608
            hir_ = new (pool_) HLocal(proxy, var);
[email protected]
   609
            break;
[email protected]
   610
          }
[email protected]
   611
        }
[email protected]
   612
        break;
[email protected]
   613
      }
[email protected]
   614
[email protected]
   615
      default:
[email protected]
   616
        // :TODO:
[email protected]
   617
        assert(false);
[email protected]
   618
    }
[email protected]
   619
}
[email protected]
   620
[email protected]
   621
void
[email protected]
   622
SemanticAnalysis::visitBooleanLiteral(BooleanLiteral *node)
[email protected]
   623
{
[email protected]
   624
    Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
[email protected]
   625
    hir_ = new (pool_) HBoolean(node, type, node->token() == TOK_TRUE);
[email protected]
   626
}
[email protected]
   627
[email protected]
   628
void
[email protected]
   629
SemanticAnalysis::visitIntegerLiteral(IntegerLiteral *lit)
[email protected]
   630
{
[email protected]
   631
    Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
[email protected]
   632
    hir_ = new (pool_) HInteger(lit, type, lit->value());
[email protected]
   633
}
[email protected]
   634
[email protected]
   635
void
[email protected]
   636
SemanticAnalysis::visitFloatLiteral(FloatLiteral *node)
[email protected]
   637
{
[email protected]
   638
    Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
[email protected]
   639
    hir_ = new (pool_) HFloat(node, type, (float)node->value());
[email protected]
   640
}
[email protected]
   641
[email protected]
   642
void
[email protected]
   643
SemanticAnalysis::visitStringLiteral(StringLiteral *node)
[email protected]
   644
{
[email protected]
   645
    Type *type = zone_->types()->getString();
[email protected]
   646
    hir_ = new (pool_) HString(node, type, node->string());
[email protected]
   647
}
[email protected]
   648
[email protected]
   649
void
[email protected]
   650
SemanticAnalysis::visitCallExpression(CallExpression *node)
[email protected]
   651
{
[email protected]
   652
    HIR *callee = eval(node->callee());
[email protected]
   653
    if (!callee)
[email protected]
   654
        return;
[email protected]
   655
[email protected]
   656
    if (!callee->type()->isFunction()) {
[email protected]
   657
        cc_.reportError(node->pos(), Message_CalleeNotFunction);
[email protected]
   658
        return;
[email protected]
   659
    }
[email protected]
   660
[email protected]
   661
    Local<FunctionType> fun(zone_, FunctionType::cast(callee->type()));
[email protected]
   662
    
[email protected]
   663
    if (!checkArgumentCount(fun, node->arguments()->length())) {
[email protected]
   664
        cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
[email protected]
   665
        return;
[email protected]
   666
    }
[email protected]
   667
[email protected]
   668
    if (fun->isForward()) {
[email protected]
   669
        cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
[email protected]
   670
        return;
[email protected]
   671
    }
[email protected]
   672
[email protected]
   673
    HIRList *args = new (pool_) HIRList;
[email protected]
   674
    for (unsigned i = 0; i < node->arguments()->length(); i++) {
[email protected]
   675
        Expression *expression = node->arguments()->at(i);
[email protected]
   676
        HIR *arg = eval(expression);
[email protected]
   677
        if (!arg)
[email protected]
   678
            return;
[email protected]
   679
[email protected]
   680
        Handle<Type> given = arg->type();
[email protected]
   681
        
[email protected]
   682
        bool mustComputeReference;
[email protected]
   683
        if (i >= fun->parameters()->length()) {
[email protected]
   684
            // Special handling for natives. Arguments past the formal must
[email protected]
   685
            // always have references computed. However, arrays are already
[email protected]
   686
            // references for natives.
[email protected]
   687
            assert(fun->isNative() && fun->isNativeVariadic());
[email protected]
   688
            mustComputeReference = !given->isReference() &&
[email protected]
   689
                                   !given->isArray();
[email protected]
   690
        } else {
[email protected]
   691
            Local<Type> actual(zone_, fun->parameterAt(i));
[email protected]
   692
[email protected]
   693
[email protected]
   694
            if (expression->isIndexExpression() && arg->isIndex()) {
[email protected]
   695
                // If the expression is the form x[y], and this produced a
[email protected]
   696
                // normal array indexing operation, then we apply some 
[email protected]
   697
                // special rules.
[email protected]
   698
[email protected]
   699
                if (actual->isReference()) {
[email protected]
   700
                    // Disabled, since this doesn't have easily definable or
[email protected]
   701
                    // sensible semantics with SP2 arrays, but we don't want
[email protected]
   702
                    // to silently break SP1 semantics.
[email protected]
   703
                    cc_.reportError(expression->pos(), Message_CannotComputeIndexRef);
[email protected]
   704
                    return;
[email protected]
   705
                }
[email protected]
   706
[email protected]
   707
                // As a special rule to account for SP1 semantics, we allow x[y]
[email protected]
   708
                // to automatically compute a sliced, read-only array view if
[email protected]
   709
                // the desired type is an array of one more dimension than x[y]
[email protected]
   710
                // would return.
[email protected]
   711
                //
[email protected]
   712
                // As an additional restriction, this is only allowed for
[email protected]
   713
                // one-level char arrays, to minimize potential issues until
[email protected]
   714
                // we determine whether more compatibility is needed.
[email protected]
   715
                //
[email protected]
   716
                // Internally, for sanity, the dependent array operation does
[email protected]
   717
                // not necessarily create a dependent array. For extensible
[email protected]
   718
                // arrays, we instead create a duplicate arrary of just the
[email protected]
   719
                // sliced portion, rather than deal with the complexity of the
[email protected]
   720
                // source array being resized.
[email protected]
   721
                //
[email protected]
   722
                // Note that we don't really know whether a dependent array
[email protected]
   723
                // will be dependent at compile-time, so the dependent type's
[email protected]
   724
                // just inherits from the source.
[email protected]
   725
                if (actual->isArray()) {
[email protected]
   726
                    HIndex *index = arg->toIndex();
[email protected]
   727
                    Local<ArrayType> basetype(zone_, ArrayType::cast(index->base()->type()));
[email protected]
   728
                    Local<ArrayType> atype(zone_, ArrayType::cast(actual));
[email protected]
   729
[email protected]
   730
                    if (atype->isCharArray() && basetype->isCharArray()) {
[email protected]
   731
                        // There is no possible way this will type check, so
[email protected]
   732
                        // it's safe to just try something completely
[email protected]
   733
                        // different.
[email protected]
   734
                        Local<Type> slicetype(zone_);
[email protected]
   735
                        if (basetype->isFixedLength()) {
[email protected]
   736
                            // If the base type is fixed length, we need to
[email protected]
   737
                            // compute a new dynamic array type based on the
[email protected]
   738
                            // fixed array's contents and qualifiers.
[email protected]
   739
                            Local<Type> contained(zone_, basetype->contained());
[email protected]
   740
                            slicetype = zone_->types()->newArray(contained, ArrayType::DYNAMIC_ARRAY_SIZE);
[email protected]
   741
                            if (!slicetype)
[email protected]
   742
                                return;
[email protected]
   743
                            slicetype = zone_->types()->qualify(slicetype, basetype->quals());
[email protected]
   744
                            if (!slicetype)
[email protected]
   745
                                return;
[email protected]
   746
                        } else {
[email protected]
   747
                            slicetype = basetype;
[email protected]
   748
                        }
[email protected]
   749
                        arg = new (pool_) HNewDependentArray(expression, slicetype, index->base(), index->index());
[email protected]
   750
                        given = arg->type();
[email protected]
   751
                    }
[email protected]
   752
                }
[email protected]
   753
            }
[email protected]
   754
[email protected]
   755
            if (actual->isReference()) {
[email protected]
   756
                // If the actual is a reference, the coercion rules are much
[email protected]
   757
                // stricter; the contained types must be identical.
[email protected]
   758
                // :TODO: make this not an assert
[email protected]
   759
                assert(ReferenceType::cast(actual)->contained() == arg->type());
[email protected]
   760
            } else {
[email protected]
   761
                // When we start overloading, coercion will have to happen later.
[email protected]
   762
                arg = coerce(arg, actual, Coerce_Arg);
[email protected]
   763
                if (!arg)
[email protected]
   764
                    return;
[email protected]
   765
            }
[email protected]
   766
[email protected]
   767
            mustComputeReference = actual->isReference() && !given->isReference();
[email protected]
   768
        }
[email protected]
   769
[email protected]
   770
        if (mustComputeReference) {
[email protected]
   771
            // Refs always use the LIFO stack if they can.
[email protected]
   772
            arg = new (pool_) HToRef(arg->node(), arg->type(), arg);
[email protected]
   773
            requireLifoStack();
[email protected]
   774
        }
[email protected]
   775
[email protected]
   776
        if (!args->append(arg))
[email protected]
   777
            return;
[email protected]
   778
    }
[email protected]
   779
[email protected]
   780
    hir_ = new (pool_) HCall(node, fun->returnType(), callee, args);
[email protected]
   781
}
[email protected]
   782
[email protected]
   783
void
[email protected]
   784
SemanticAnalysis::visitAssignment(Assignment *node)
[email protected]
   785
{
[email protected]
   786
    HLValue *lval = lvalue(node->lvalue());
[email protected]
   787
    if (!lval)
[email protected]
   788
        return;
[email protected]
   789
[email protected]
   790
    if (lval->type()->quals() & TypeQual_Const) {
[email protected]
   791
        cc_.reportError(node->pos(), Message_CannotModifyConst);
[email protected]
   792
        return;
[email protected]
   793
    }
[email protected]
   794
[email protected]
   795
    HIR *hir = eval(node->expression());
[email protected]
   796
    if (!hir)
[email protected]
   797
        return;
[email protected]
   798
    if ((hir = coerce(hir, lval->type(), Coerce_Assign)) == NULL)
[email protected]
   799
        return;
[email protected]
   800
[email protected]
   801
    hir_ = new (pool_) HStore(node, node->token(), lval, hir);
[email protected]
   802
}
[email protected]
   803
[email protected]
   804
void
[email protected]
   805
SemanticAnalysis::visitBreakStatement(BreakStatement *node)
[email protected]
   806
{
[email protected]
   807
    if (!loop_) {
[email protected]
   808
        cc_.reportError(node->pos(), Message_BreakOutsideLoop);
[email protected]
   809
        return;
[email protected]
   810
    }
[email protected]
   811
[email protected]
   812
    loop_->setBreaks();
[email protected]
   813
    __ j(OP_BREAK, loop_->break_());
[email protected]
   814
}
[email protected]
   815
[email protected]
   816
void
[email protected]
   817
SemanticAnalysis::visitContinueStatement(ContinueStatement *node)
[email protected]
   818
{
[email protected]
   819
    if (!loop_) {
[email protected]
   820
        cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
[email protected]
   821
        return;
[email protected]
   822
    }
[email protected]
   823
[email protected]
   824
    __ j(OP_CONTINUE, loop_->continue_());
[email protected]
   825
}
[email protected]
   826
[email protected]
   827
void
[email protected]
   828
SemanticAnalysis::visitIndexExpression(IndexExpression *node)
[email protected]
   829
{
[email protected]
   830
    HIR *left = eval(node->left());
[email protected]
   831
    if (!left)
[email protected]
   832
        return;
[email protected]
   833
[email protected]
   834
    HIR *right = eval(node->right());
[email protected]
   835
    if (!right)
[email protected]
   836
        return;
[email protected]
   837
[email protected]
   838
    // Hrm... yes, for now, let's coerce here.
[email protected]
   839
    Local<Type> i32(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
[email protected]
   840
    if ((right = coerce(right, i32, Coerce_Arg)) == NULL)
[email protected]
   841
        return;
[email protected]
   842
    
[email protected]
   843
    // Do not coerce here, there's no reason to yet.
[email protected]
   844
    if (!left->type()->isArray()) {
[email protected]
   845
        cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
[email protected]
   846
        return;
[email protected]
   847
    }
[email protected]
   848
[email protected]
   849
    hir_ = new (pool_) HIndex(node,
[email protected]
   850
                              ArrayType::cast(left->type())->contained(),
[email protected]
   851
                              left,
[email protected]
   852
                              right);
[email protected]
   853
}
[email protected]
   854
[email protected]
   855
void
[email protected]
   856
SemanticAnalysis::visitIncDecExpression(IncDecExpression *node)
[email protected]
   857
{
[email protected]
   858
    HLValue *lval = lvalue(node->expression());
[email protected]
   859
    if (!lval)
[email protected]
   860
        return;
[email protected]
   861
[email protected]
   862
    if (!lval->type()->isInt32() && !lval->type()->isFloat()) {
[email protected]
   863
        assert(false);
[email protected]
   864
        return;
[email protected]
   865
    }
[email protected]
   866
[email protected]
   867
    HIR *rval;
[email protected]
   868
    if (lval->type()->isInt32())
[email protected]
   869
        rval = new (pool_) HInteger(node, lval->type(), 1);
[email protected]
   870
    else
[email protected]
   871
        rval = new (pool_) HFloat(node, lval->type(), 1.0);
[email protected]
   872
[email protected]
   873
    if (node->postfix())
[email protected]
   874
        hir_ = new (pool_) HPostIncDec(node, node->token(), lval, rval);
[email protected]
   875
    else
[email protected]
   876
        hir_ = new (pool_) HStore(node, node->token(), lval, rval);
[email protected]
   877
}
[email protected]
   878
[email protected]
   879
Type *
[email protected]
   880
SemanticAnalysis::coercionType(TokenKind token, HIR *left, HIR *right)
[email protected]
   881
{
[email protected]
   882
    Local<Type> l(zone_, left->type());
[email protected]
   883
    Local<Type> r(zone_, right->type());
[email protected]
   884
[email protected]
   885
    if (l->isPod())
[email protected]
   886
        l = l->unqualified();
[email protected]
   887
    if (r->isPod())
[email protected]
   888
        r = r->unqualified();
[email protected]
   889
[email protected]
   890
    if ((token >= TOK_PLUS && token <= TOK_SLASH) ||
[email protected]
   891
        (token >= TOK_EQUALS && token <= TOK_GE))
[email protected]
   892
    {
[email protected]
   893
        // We allow: [float|int32] + [float|int32]
[email protected]
   894
        if (l->isFloat() && r->isInt32())
[email protected]
   895
            return l;
[email protected]
   896
        if (l->isInt32() && r->isFloat())
[email protected]
   897
            return r;
[email protected]
   898
        if (l->isInt32() && r->isInt32())
[email protected]
   899
            return l;
[email protected]
   900
[email protected]
   901
        if (l->isChar() && r->isInt32())
[email protected]
   902
             return r;
[email protected]
   903
        if (l->isInt32() && r->isChar())
[email protected]
   904
            return l;
[email protected]
   905
    }
[email protected]
   906
[email protected]
   907
    // :TODO: remove
[email protected]
   908
    assert(*l == *r);
[email protected]
   909
[email protected]
   910
    return left->type();
[email protected]
   911
}
[email protected]
   912
[email protected]
   913
HIR *
[email protected]
   914
SemanticAnalysis::typeCheckArray(const SourcePosition &pos,
[email protected]
   915
                                 HIR *from,
[email protected]
   916
                                 Handle<ArrayType> argActual,
[email protected]
   917
                                 CoercionKind kind)
[email protected]
   918
{
[email protected]
   919
    Local<Type> argGiven(zone_, from->type());
[email protected]
   920
[email protected]
   921
    // Special case: if we receive a string literal, and this is a char array,
[email protected]
   922
    // we allow a coercion.
[email protected]
   923
    if (from->isString() &&
[email protected]
   924
        argGiven->isString() &&
[email protected]
   925
        argActual->isArray() &&
[email protected]
   926
        ArrayType::cast(argActual)->isCharArray())
[email protected]
   927
    {
[email protected]
   928
        HString *hir = from->toString();
[email protected]
   929
        Local<ArrayType> actual(zone_, ArrayType::cast(argActual));
[email protected]
   930
[email protected]
   931
        if (actual->isFixedLength()) {
[email protected]
   932
            if (hir->string()->length() + 1 > actual->fixedLength()) {
[email protected]
   933
                cc_.reportError(pos, Message_ArrayAssignmentTooBig);
[email protected]
   934
                return NULL;
[email protected]
   935
            }
[email protected]
   936
        }
[email protected]
   937
[email protected]
   938
        // Transform the string literal into an array literal. We give it the
[email protected]
   939
        // type of the left-hand side. The constness of the destination type
[email protected]
   940
        // will determine whether HArray duplicates it or not.
[email protected]
   941
        Local<ArrayMap> map(zone_, actual->newMap());
[email protected]
   942
        if (!map)
[email protected]
   943
            return NULL;
[email protected]
   944
[email protected]
   945
        Local<Array> array(zone_);
[email protected]
   946
        array = Array::NewString(zone_,
[email protected]
   947
                                 map,
[email protected]
   948
                                 hir->string()->chars(),
[email protected]
   949
                                 hir->string()->length(),
[email protected]
   950
                                 Heap::Tenure_Old);
[email protected]
   951
        if (!array)
[email protected]
   952
            return NULL;
[email protected]
   953
[email protected]
   954
        // Since we're creating a canonical copy of the char array, we have to
[email protected]
   955
        // be careful that it doesn't get mutated. There are two cases where
[email protected]
   956
        // we know it can't be mutated, and therefore don't need to specify
[email protected]
   957
        // that it be cloned after pushing it onto the stack.
[email protected]
   958
        //
[email protected]
   959
        //  (1) The array type is const.
[email protected]
   960
        //  (2) The array type is fixed-length, and the coercion is for
[email protected]
   961
        //      assignment, meaning it will be immediately copied.
[email protected]
   962
        //
[email protected]
   963
        // We treat declaration separately from assignment, since for
[email protected]
   964
        // declaration we can just immediately use the cloned version.
[email protected]
   965
        bool shouldDuplicate = true;
[email protected]
   966
        if ((actual->quals() & TypeQual_Const) ||
[email protected]
   967
            (actual->isFixedLength() && kind == Coerce_Assign))
[email protected]
   968
        {
[email protected]
   969
            shouldDuplicate = false;
[email protected]
   970
        }
[email protected]
   971
[email protected]
   972
        return new (pool_) HArray(hir->node(), actual, array, shouldDuplicate);
[email protected]
   973
    }
[email protected]
   974
[email protected]
   975
    if (!argGiven->isArray()) {
[email protected]
   976
        cc_.reportError(pos, Message_ExpectedArray);
[email protected]
   977
        return NULL;
[email protected]
   978
    }
[email protected]
   979
[email protected]
   980
    Local<ArrayType> actual(zone_, ArrayType::cast(argActual));
[email protected]
   981
    Local<ArrayType> given(zone_, ArrayType::cast(argGiven));
[email protected]
   982
[email protected]
   983
    if (actual->levels() != given->levels()) {
[email protected]
   984
        cc_.reportError(pos, Message_ArrayDimensionsMismatch);
[email protected]
   985
        return NULL;
[email protected]
   986
    }
[email protected]
   987
[email protected]
   988
    unsigned level = 1;
[email protected]
   989
    while (true) {
[email protected]
   990
        if (actual->isFixedLength()) {
[email protected]
   991
            if (!given->isFixedLength()) {
[email protected]
   992
                cc_.reportError(pos, Message_ArrayDimensionSizeMismatch, level);
[email protected]
   993
                return NULL;
[email protected]
   994
            }
[email protected]
   995
[email protected]
   996
            if (actual->isCharArray()) {
[email protected]
   997
                // String assignment can have a truncated |given|.
[email protected]
   998
                if (given->fixedLength() > actual->fixedLength()) {
[email protected]
   999
                    cc_.reportError(pos, Message_ArrayAssignmentTooBig);
[email protected]
  1000
                    return NULL;
[email protected]
  1001
                }
[email protected]
  1002
            } else {
[email protected]
  1003
                if (given->fixedLength() != actual->fixedLength()) {
[email protected]
  1004
                    cc_.reportError(pos, Message_ArrayDimensionSizeMismatch, level);
[email protected]
  1005
                    return NULL;
[email protected]
  1006
                }
[email protected]
  1007
            }
[email protected]
  1008
        } else if (given->isFixedLength()) {
[email protected]
  1009
            // We do not allow assignments in the form:
[email protected]
  1010
            //  [][] = [][Y]
[email protected]
  1011
            //
[email protected]
  1012
            // Otherwise, the following code could break the size guarantees
[email protected]
  1013
            // of the right-hand side:
[email protected]
  1014
            //
[email protected]
  1015
            //  new x[][5];
[email protected]
  1016
            //  new y[][];
[email protected]
  1017
            //  new z[3];
[email protected]
  1018
            //
[email protected]
  1019
            //
[email protected]
  1020
            //  y = x;
[email protected]
  1021
            //  y[0] = z;
[email protected]
  1022
            //
[email protected]
  1023
            // Now |x[0]| contains a 3-length array.
[email protected]
  1024
            if (level > 1) {
[email protected]
  1025
                cc_.reportError(pos, Message_ArrayDimensionSizeMismatch, level);
[email protected]
  1026
                return NULL;
[email protected]
  1027
            }
[email protected]
  1028
        }
[email protected]
  1029
[email protected]
  1030
        if (!given->contained()->isArray() || !actual->contained()->isArray())
[email protected]
  1031
            break;
[email protected]
  1032
[email protected]
  1033
        given = ArrayType::cast(given->contained());
[email protected]
  1034
        actual = ArrayType::cast(actual->contained());
[email protected]
  1035
    }
[email protected]
  1036
[email protected]
  1037
    bool givenConst = !!(given->contained()->quals() & TypeQual_Const);
[email protected]
  1038
    bool actualConst = !!(actual->contained()->quals() & TypeQual_Const);
dv[email protected]
  1039
[email protected]
  1040
    // These arrays must contain the same innermost type.
[email protected]
  1041
    if (given->contained()->unqualified() != actual->contained()->unqualified() ||
[email protected]
  1042
        (givenConst && !actualConst))
[email protected]
  1043
    {
[email protected]
  1044
        char aname[255];
[email protected]
  1045
        char gname[255];
[email protected]
  1046
        cc_.reportError(pos, Message_TypeMismatch,
[email protected]
  1047
                        GetTypeName(argActual, aname, sizeof(aname)),
[email protected]
  1048
                        GetTypeName(argGiven, gname, sizeof(gname)));
[email protected]
  1049
        return NULL;
[email protected]
  1050
    }
[email protected]
  1051
[email protected]
  1052
    return from;
[email protected]
  1053
}
[email protected]
  1054
[email protected]
  1055
HIR *
[email protected]
  1056
SemanticAnalysis::typeFailure(const SourcePosition &pos, Handle<Type> from, Handle<Type> to)
[email protected]
  1057
{
[email protected]
  1058
    char fname[255];
[email protected]
  1059
    char tname[255];
[email protected]
  1060
    cc_.reportError(pos, Message_InvalidCoercion,
[email protected]
  1061
                    GetTypeName(from, fname, sizeof(fname)),
[email protected]
  1062
                    GetTypeName(to, tname, sizeof(tname)));
[email protected]
  1063
    return NULL;
[email protected]
  1064
}
[email protected]
  1065
[email protected]
  1066
HIR *
[email protected]
  1067
SemanticAnalysis::coerce(HIR *hir, Handle<Type> to, CoercionKind kind)
[email protected]
  1068
{
[email protected]
  1069
    Local<Type> from(zone_, hir->type());
[email protected]
  1070
[email protected]
  1071
    // If the source is *ever* an importable, we just fail.
[email protected]
  1072
    if (from->isImportable())
[email protected]
  1073
        return typeFailure(hir->node()->pos(), from, to);
[email protected]
  1074
[email protected]
  1075
    if (from->isInt32() && to->isFloat())
[email protected]
  1076
        return new (pool_) HConvert(hir->node(), to, OP_CVT_I2F, hir);
[email protected]
  1077
    if (from->isInt32() && to->isBool())
[email protected]
  1078
        return new (pool_) HConvert(hir->node(), to, OP_CVT_I2B, hir);
[email protected]
  1079
[email protected]
  1080
    if (to->isEnum() && *from != to)
[email protected]
  1081
        return typeFailure(hir->node()->pos(), from, to);
[email protected]
  1082
    
[email protected]
  1083
    if (to->isArray()) {
[email protected]
  1084
        Local<ArrayType> atype(zone_, ArrayType::cast(to));
[email protected]
  1085
        return typeCheckArray(hir->node()->pos(), hir, atype, kind);
[email protected]
  1086
    }
[email protected]
  1087
    
[email protected]
  1088
    return hir;
[email protected]
  1089
}
[email protected]
  1090
[email protected]
  1091
void
[email protected]
  1092
SemanticAnalysis::set(VariableSymbol *sym)
[email protected]
  1093
{
[email protected]
  1094
    if (sym->scope()->kind() == Scope::GLOBAL) {
[email protected]
  1095
        __ setglobal(sym);
[email protected]
  1096
    } else {
[email protected]
  1097
        assert(sym->storage() == VariableSymbol::Local);
[email protected]
  1098
        __ setlocal(sym);
[email protected]
  1099
    }
[email protected]
  1100
}
[email protected]
  1101
[email protected]
  1102
HIR *
[email protected]
  1103
SemanticAnalysis::binary(AstNode *node, TokenKind token, HIR *left, HIR *right)
[email protected]
  1104
{
[email protected]
  1105
    Local<Type> coercion(zone_, coercionType(token, left, right));
[email protected]
  1106
    if (!coercion)
[email protected]
  1107
        return NULL;
[email protected]
  1108
    if ((left = coerce(left, coercion, Coerce_Arg)) == NULL)
[email protected]
  1109
        return NULL;
[email protected]
  1110
    if ((right = coerce(right, coercion, Coerce_Arg)) == NULL)
[email protected]
  1111
        return NULL;
[email protected]
  1112
    
[email protected]
  1113
    switch (token) {
[email protected]
  1114
     case TOK_PLUS:
[email protected]
  1115
      case TOK_MINUS:
[email protected]
  1116
      case TOK_STAR:
[email protected]
  1117
      case TOK_SLASH:
[email protected]
  1118
      case TOK_PERCENT:
[email protected]
  1119
      case TOK_AMPERSAND:
[email protected]
  1120
      case TOK_BITOR:
[email protected]
  1121
      case TOK_BITXOR:
[email protected]
  1122
      case TOK_SHR:
[email protected]
  1123
      case TOK_USHR:
[email protected]
  1124
      case TOK_SHL:
[email protected]
  1125
        return new (pool_) HBinary(node, left->type(), token, left, right);
[email protected]
  1126
[email protected]
  1127
      case TOK_EQUALS:
[email protected]
  1128
      case TOK_NOTEQUALS:
[email protected]
  1129
      case TOK_LT:
[email protected]
  1130
      case TOK_LE:
[email protected]
  1131
      case TOK_GT:
[email protected]
  1132
      case TOK_GE:
[email protected]
  1133
      case TOK_AND:
[email protected]
  1134
      case TOK_OR:
[email protected]
  1135
      {
[email protected]
  1136
        Local<Type> result(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
[email protected]
  1137
        return new (pool_) HBinary(node, result, token, left, right);
[email protected]
  1138
      }
[email protected]
  1139
[email protected]
  1140
      default:
[email protected]
  1141
        assert(false);
[email protected]
  1142
        return NULL;
[email protected]
  1143
    }
[email protected]
  1144
}
[email protected]
  1145
[email protected]
  1146
static inline bool
[email protected]
  1147
IsValidUncheckedType(Type *type)
[email protected]
  1148
{
[email protected]
  1149
    return type->isEnum() ||
[email protected]
  1150
           type->isFloat() ||
[email protected]
  1151
           type->isInt32() ||
[email protected]
  1152
           type->isChar() ||
[email protected]
  1153
           type->isUnchecked() ||
[email protected]
  1154
           type->isBool();
[email protected]
  1155
}
[email protected]
  1156
[email protected]
  1157
void
[email protected]
  1158
SemanticAnalysis::visitUnaryExpression(UnaryExpression *node)
[email protected]
  1159
{
[email protected]
  1160
    HIR *hir = eval(node->expression());
[email protected]
  1161
    if (!hir)
[email protected]
  1162
        return;
[email protected]
  1163
[email protected]
  1164
    switch (node->token()) {
[email protected]
  1165
      case TOK_TILDE:
[email protected]
  1166
      {
[email protected]
  1167
        if (!hir->type()->isInt32() && !hir->type()->isEnum()) {
[email protected]
  1168
            char name[255];
[email protected]
  1169
            cc_.reportError(node->pos(), Message_InvalidUnaryType,
[email protected]
  1170
                            "~",
[email protected]
  1171
                            GetTypeName(hir->type(), name, sizeof(name)));
[email protected]
  1172
        }
[email protected]
  1173
        Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
[email protected]
  1174
        hir_ = new (pool_) HUnary(node, hir->type(), TOK_TILDE, hir);
[email protected]
  1175
        return;
[email protected]
  1176
      }
[email protected]
  1177
[email protected]
  1178
      case TOK_MINUS:
[email protected]
  1179
      {
[email protected]
  1180
        if (hir->type()->isInt32() || hir->type()->isEnum()) {
[email protected]
  1181
            Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
[email protected]
  1182
            hir_ = new (pool_) HUnary(node, type, TOK_MINUS, hir);
[email protected]
  1183
            return;
[email protected]
  1184
        }
[email protected]
  1185
        if (hir->type()->isFloat()) {
[email protected]
  1186
            hir_ = new (pool_) HUnary(node, hir->type(), TOK_MINUS, hir);
[email protected]
  1187
            return;
[email protected]
  1188
        }
[email protected]
  1189
[email protected]
  1190
        char name[255];
[email protected]
  1191
        cc_.reportError(node->pos(), Message_InvalidUnaryType,
[email protected]
  1192
                        "-",
[email protected]
  1193
                        GetTypeName(hir->type(), name, sizeof(name)));
[email protected]
  1194
        break;
[email protected]
  1195
      }
[email protected]
  1196
[email protected]
  1197
      default:
[email protected]
  1198
      {
[email protected]
  1199
        assert(node->token() == TOK_LABEL);
[email protected]
  1200
[email protected]
  1201
        TypeSymbol *sym = node->tag()->sym()->asType();
[email protected]
  1202
        if (!sym) {
[email protected]
  1203
            cc_.reportError(node->pos(), Message_IdentifierIsNotAType,
[email protected]
  1204
                            sym->name()->chars());
[email protected]
  1205
            return;
[email protected]
  1206
        }
[email protected]
  1207
[email protected]
  1208
        if (!IsValidUncheckedType(sym->type())) {
[email protected]
  1209
            char name[255];
[email protected]
  1210
            cc_.reportError(node->pos(), Message_InvalidUncheckedCastTo,
[email protected]
  1211
                            GetTypeName(hir->type(), name, sizeof(name)));
[email protected]
  1212
            return;
[email protected]
  1213
        }
[email protected]
  1214
        if (!IsValidUncheckedType(hir->type())) {
[email protected]
  1215
            char name[255];
[email protected]
  1216
            cc_.reportError(node->pos(), Message_InvalidUncheckedCastFrom,
[email protected]
  1217
                            GetTypeName(hir->type(), name, sizeof(name)));
[email protected]
  1218
            return;
[email protected]
  1219
        }
[email protected]
  1220
[email protected]
  1221
        hir_ = new (pool_) HUnary(node, hir->type(), TOK_LABEL, hir);
[email protected]
  1222
        break;
[email protected]
  1223
      }
[email protected]
  1224
    }
[email protected]
  1225
}
[email protected]
  1226
[email protected]
  1227
void
[email protected]
  1228
SemanticAnalysis::import(HIR *hir, FieldExpression *node)
[email protected]
  1229
{
[email protected]
  1230
    // Note that we don't care if the hir type is "importable", since we
[email protected]
  1231
    // can only do anything if the important has a constant binding.
[email protected]
  1232
    unsigned index = hir->toImport()->index();
[email protected]
  1233
    Local<Importable> importable(zone_, unit_->module()->getImportAt(index));
[email protected]
  1234
[email protected]
  1235
    if (importable->isPackage()) {
[email protected]
  1236
        // Look at what's already loaded and see if anything exists. We
[email protected]
  1237
        // can't create stuff on-demand here since it's too late to add
[email protected]
  1238
        // new translation units to the CompileContext.
[email protected]
  1239
        Local<Package> package(zone_, Package::cast(importable));
[email protected]
  1240
        Local<Importable> found(zone_, package->import(node->field()));
[email protected]
  1241
        if (!found) {
[email protected]
  1242
            cc_.reportError(node->pos(), Message_PackageDoesNotHaveMember, node->field()->chars());
[email protected]
  1243
            return;
[email protected]
  1244
        }
[email protected]
  1245
[email protected]
  1246
        // If we did find something, make sure it is in our import list.
[email protected]
  1247
        // If we didn't explicitly import it, we have no right to use it.
[email protected]
  1248
        for (index = 0; index < unit_->module()->imports(); index++) {
[email protected]
  1249
            if (unit_->module()->getImportAt(index) == importable)
[email protected]
  1250
                break;
[email protected]
  1251
        }
[email protected]
  1252
[email protected]
  1253
        if (index == unit_->module()->imports()) {
[email protected]
  1254
            cc_.reportError(node->pos(), Message_ImportWasNeverImported, node->field()->chars());
[email protected]
  1255
            return;
[email protected]
  1256
        }
[email protected]
  1257
[email protected]
  1258
        // Just create a new HImport.
[email protected]
  1259
        hir_ = new (pool_) HImport(node, hir->type(), index);
[email protected]
  1260
    } else {
[email protected]
  1261
        // Modules can never have sub-modules or sub-packages.
[email protected]
  1262
        unsigned index;
[email protected]
  1263
        Local<Module> module(zone_, Module::cast(importable));
[email protected]
  1264
        Local<StructMap> map(zone_, module->globals()->map());
[email protected]
  1265
        Local<Descriptor> desc(zone_, map->type()->lookupField(node->field(), &index));
[email protected]
  1266
        if (!desc) {
[email protected]
  1267
            cc_.reportError(node->pos(), Message_FieldNotFound, node->field()->chars());
[email protected]
  1268
            return;
[email protected]
  1269
        }
[email protected]
  1270
        hir_ = new (pool_) HField(node, hir, desc->type(), index);
[email protected]
  1271
    }
[email protected]
  1272
}
[email protected]
  1273
[email protected]
  1274
void
[email protected]
  1275
SemanticAnalysis::visitFieldExpression(FieldExpression *node)
[email protected]
  1276
{
[email protected]
  1277
    HIR *hir = eval(node->left());
[email protected]
  1278
    if (!hir)
[email protected]
  1279
        return;
[email protected]
  1280
[email protected]
  1281
    if (hir->isImport()) {
[email protected]
  1282
        import(hir, node);
[email protected]
  1283
        return;
[email protected]
  1284
    }
[email protected]
  1285
[email protected]
  1286
    char name[255];
[email protected]
  1287
    cc_.reportError(node->pos(), Message_InvalidFieldExpression,
[email protected]
  1288
                    GetTypeName(hir->type(), name, sizeof(name)));
[email protected]
  1289
}
[email protected]
  1290
[email protected]
  1291
static inline TokenKind
[email protected]
  1292
ToCompare(Expression *expr)
[email protected]
  1293
{
[email protected]
  1294
    if (expr->isBinaryExpression()) {
[email protected]
  1295
        BinaryExpression *b = expr->toBinaryExpression();
[email protected]
  1296
        if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
[email protected]
  1297
            return b->token();
[email protected]
  1298
    } else if (expr->isUnaryExpression()) {
[email protected]
  1299
        UnaryExpression *u = expr->toUnaryExpression();
[email protected]
  1300
        if (u->token() == TOK_NOT)
[email protected]
  1301
            return TOK_NOT;
[email protected]
  1302
    }
[email protected]
  1303
    return TOK_EOF;
[email protected]
  1304
}
[email protected]
  1305
[email protected]
  1306
void
[email protected]
  1307
SemanticAnalysis::visitForTest(Expression *expr,
[email protected]
  1308
                               Label *trueBranch,
[email protected]
  1309
                               Label *falseBranch,
[email protected]
  1310
                               Label *fallthrough)
[email protected]
  1311
{
[email protected]
  1312
    Local<Type> boolType(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
[email protected]
  1313
[email protected]
  1314
    TokenKind token = ToCompare(expr);
[email protected]
  1315
    if (token == TOK_EOF || token == TOK_NOT) {
[email protected]
  1316
        HIR *hir;
[email protected]
  1317
        if (token == TOK_EOF)
[email protected]
  1318
            hir = eval(expr);
[email protected]
  1319
        else
[email protected]
  1320
            hir = eval(expr->toUnaryExpression()->expression());
[email protected]
  1321
        if (!hir)
[email protected]
  1322
            return;
[email protected]
  1323
        if ((hir = coerce(hir, boolType, Coerce_Arg)) == NULL)
[email protected]
  1324
            return;
[email protected]
  1325
[email protected]
  1326
        discharge(hir);
[email protected]
  1327
[email protected]
  1328
        bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
[email protected]
  1329
        Opcode op = jumpOnTrue ? OP_JT : OP_JF;
[email protected]
  1330
        
[email protected]
  1331
        if (fallthrough == falseBranch)
[email protected]
  1332
            __ j(op, trueBranch);
[email protected]
  1333
        else
[email protected]
  1334
            __ j(op, falseBranch);
[email protected]
  1335
        return;
[email protected]
  1336
    }
[email protected]
  1337
[email protected]
  1338
    BinaryExpression *bin = expr->toBinaryExpression();
[email protected]
  1339
    if (token == TOK_AND || token == TOK_OR) {
[email protected]
  1340
        Label next;
[email protected]
  1341
        if (token == TOK_AND)
[email protected]
  1342
            visitForTest(bin->left(), &next, falseBranch, &next);
[email protected]
  1343
        else
[email protected]
  1344
            visitForTest(bin->left(), trueBranch, &next, &next);
[email protected]
  1345
        __ bind(&next);
[email protected]
  1346
        visitForTest(bin->right(), trueBranch, falseBranch, fallthrough);
[email protected]
  1347
        return;
[email protected]
  1348
    }
[email protected]
  1349
[email protected]
  1350
    HIR *left = eval(bin->left());
[email protected]
  1351
    HIR *right = eval(bin->right());
[email protected]
  1352
    if (!left || !right)
[email protected]
  1353
        return;
[email protected]
  1354
[email protected]
  1355
    HIR *hir = binary(bin, bin->token(), left, right);
[email protected]
  1356
    if (!hir)
[email protected]
  1357
        return;
[email protected]
  1358
    if ((hir = coerce(hir, boolType, Coerce_Arg)) == NULL)
[email protected]
  1359
        return;
[email protected]
  1360
[email protected]
  1361
    discharge(hir);
[email protected]
  1362
    if (fallthrough == trueBranch)
[email protected]
  1363
        __ j(OP_JF, falseBranch);
[email protected]
  1364
    else
[email protected]
  1365
        __ j(OP_JT, trueBranch);
[email protected]
  1366
}
[email protected]
  1367
[email protected]
  1368
HLValue *
[email protected]
  1369
SemanticAnalysis::lvalue(Expression *expr)
[email protected]
  1370
{
[email protected]
  1371
    HIR *hir = eval(expr);
[email protected]
  1372
    if (!hir)
[email protected]
  1373
        return NULL;
[email protected]
  1374
    
[email protected]
  1375
    if (!hir->isLValue()) {
[email protected]
  1376
        cc_.reportError(expr->pos(), Message_ExpectedLValue);
[email protected]
  1377
        return NULL;
[email protected]
  1378
    }
[email protected]
  1379
[email protected]
  1380
    return static_cast<HLValue *>(hir);
[email protected]
  1381
}
[email protected]
  1382
[email protected]
  1383
HIR *
[email protected]
  1384
SemanticAnalysis::eval(Expression *expr)
[email protected]
  1385
{
[email protected]
  1386
    assert(!hir_);
[email protected]
  1387
    expr->accept(this);
[email protected]
  1388
[email protected]
  1389
    HIR *hir = hir_;
[email protected]
  1390
    hir_ = NULL;
[email protected]
  1391
[email protected]
  1392
    return hir;
[email protected]
  1393
}
[email protected]
  1394
[email protected]
  1395
void
[email protected]
  1396
SemanticAnalysis::discharge(HIR *hir)
[email protected]
  1397
{
[email protected]
  1398
    EmitHIR(zone_, emitter_, hir);
[email protected]
  1399
}