jitcraft/SemanticAnalysis.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jul 09 21:40:20 2012 -0700 (2012-07-09)
changeset 66 cbba1997dc71
parent 63 dd57743d220e
child 67 b765a6383849
permissions -rw-r--r--
Add support for passing arrays.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include "SemanticAnalysis.h"
    20 #include "Types.h"
    21 #include "Zone.h"
    22 #include "TypeManager.h"
    23 #include "Scopes.h"
    24 #include "Heap-inl.h"
    25 #include "Strings.h"
    26 
    27 using namespace ke;
    28 
    29 static inline bool
    30 IsPrimitiveType(Type *type, PrimitiveType primitive)
    31 {
    32     if (!type)
    33         return false;
    34 
    35     if (!type->isPrimitive())
    36         return false;
    37 
    38     return primitive == type->primitive();
    39 }
    40 
    41 static inline bool
    42 EvaluateBinaryOperation(TokenKind kind, const BoxedPrimitive &left, const BoxedPrimitive &right,
    43                         BoxedPrimitive *out)
    44 {
    45     switch (kind) {
    46       case TOK_PLUS:
    47         *out = BoxedPrimitive::Int(left.toInt() + right.toInt());
    48         return true;
    49 
    50       case TOK_SHL:
    51         *out = BoxedPrimitive::Int(left.toInt() << right.toInt());
    52         return true;
    53 
    54       default:
    55         return false;
    56     }
    57 }
    58 
    59 static bool
    60 EvaluateForConstant(Expression *expression, BoxedPrimitive *out)
    61 {
    62     switch (expression->kind()) {
    63       case AstNode::kBinaryExpression:
    64       {
    65         BoxedPrimitive left;
    66         BoxedPrimitive right;
    67         BinaryExpression *b = expression->toBinaryExpression();
    68         if (!EvaluateForConstant(b->left(), &left) ||
    69             !EvaluateForConstant(b->right(), &right))
    70         {
    71             return false;
    72         }
    73         return EvaluateBinaryOperation(b->token(), left, right, out);
    74       }
    75 
    76       case AstNode::kNameProxy:
    77       {
    78         NameProxy *proxy = expression->toNameProxy();
    79         if (proxy->binding()->kind() != BoundName::CONSTANT)
    80             return false;
    81 
    82         NamedConstant *c = proxy->binding()->toNamedConstant();
    83         if (!IsPrimitiveType(c->type(), PrimitiveType_Int32))
    84             return false;
    85 
    86         *out = c->value();
    87         return true;
    88       }
    89 
    90       case AstNode::kIntegerLiteral:
    91         *out = BoxedPrimitive::Int(expression->toIntegerLiteral()->value());
    92         return true;
    93 
    94       default:
    95         return false;
    96     }
    97 }
    98 
    99 SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, ParseTree *tree)
   100   : zone_(zone),
   101     cc_(cc),
   102     tree_(tree),
   103     loopDepth_(0),
   104     scope_(NULL)
   105 {
   106 }
   107 
   108 bool
   109 SemanticAnalysis::analyze()
   110 {
   111     if (cc_.failures())
   112         return false;
   113 
   114     for (size_t i = 0; i < tree_->statements()->length(); i++) {
   115         Statement *statement = tree_->statements()->at(i);
   116         if (statement->isFunctionStatement()) {
   117             FunctionStatement *funstmt = statement->toFunctionStatement();
   118             if (!preanalyze(funstmt))
   119                 return false;
   120         }
   121     }
   122 
   123     for (size_t i = 0; i < tree_->statements()->length(); i++) {
   124         Statement *statement = tree_->statements()->at(i);
   125         statement->accept(this);
   126     }
   127 
   128     return !cc_.failures();
   129 }
   130 
   131 Type *
   132 SemanticAnalysis::visitAndApplyType(Expression *expression)
   133 {
   134     expression->accept(this);
   135     return expression->type();
   136 }
   137 
   138 Type *
   139 SemanticAnalysis::visitForLoad(Expression *expression)
   140 {
   141     Local<Type> type(zone_, visitAndApplyType(expression));
   142     if (!type)
   143         return NULL;
   144 
   145     if (type->isReference()) {
   146         expression->setType(type->contained());
   147     } else if (expression->isIndexExpression()) {
   148         if (type->isArray())
   149             expression->setType(type->contained());
   150     }
   151 
   152     return expression->type();
   153 }
   154 
   155 Type *
   156 SemanticAnalysis::visitForRef(Expression *expression)
   157 {
   158     expression->accept(this);
   159     return expression->type();
   160 }
   161 
   162 // :TODO: this leaks an interior pointer.
   163 static inline const char *
   164 GetTypeName(Type *type)
   165 {
   166     if (type->isArray())
   167         return "array";
   168     if (type->isFunction())
   169         return "function";
   170     if (type->isReference())
   171         type = type->contained();
   172     if (type->tag())
   173         return type->tag()->chars();
   174     return "int";
   175 }
   176 
   177 void
   178 SemanticAnalysis::unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type)
   179 {
   180     const char *op = TokenNames[token];
   181     const char *name = GetTypeName(type);
   182 
   183     cc_.reportError(node->pos(), Message_UnaryTypeError, op, name);
   184 }
   185 
   186 void
   187 SemanticAnalysis::binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
   188 {
   189     const char *op = TokenNames[token];
   190     const char *leftName = GetTypeName(left);
   191     const char *rightName = GetTypeName(right);
   192 
   193     cc_.reportError(node->pos(), Message_BinaryTypeError, op, leftName, rightName);
   194 }
   195 
   196 Type *
   197 SemanticAnalysis::typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
   198 {
   199     if ((token >= TOK_PLUS && token <= TOK_SLASH) ||
   200         (token >= TOK_ASSIGN_ADD && token <= TOK_ASSIGN_DIV))
   201     {
   202         // If either side is a float, the other side must be a float or int.
   203         if (left->isFloat() || right->isFloat()) {
   204             Type *floatType = left->isFloat() ? left : right;
   205             Type *other = left->isFloat() ? right : left;
   206             if (other->isFloat() || (other->isInt32() && !other->tag()))
   207                 return floatType;
   208         }
   209     }
   210 
   211     if ((!left->isInt32() || left->tag()) ||
   212         (!right->isInt32() || right->tag()))
   213     {
   214         binaryTypeError(node, token, left, right);
   215         return NULL;
   216     }
   217 
   218     return left;
   219 }
   220 
   221 bool
   222 SemanticAnalysis::canTest(AstNode *node, Handle<Type> type)
   223 {
   224     if (type->isArray()) {
   225         cc_.reportError(node->pos(), Message_ArrayIsNotBoolean);
   226         return false;
   227     }
   228     return true;
   229 }
   230 
   231 Type *
   232 SemanticAnalysis::visitForLValue(Expression *expression)
   233 {
   234     if (expression->isIndexExpression()) {
   235         Type *type = visitAndApplyType(expression);
   236         if (!type)
   237             return NULL;
   238 
   239         return type->contained();
   240     }
   241 
   242     if (!expression->isNameProxy()) {
   243         cc_.reportError(expression->pos(), Message_ExpectedLValue);
   244         return NULL;
   245     }
   246 
   247     NameProxy *proxy = expression->toNameProxy();
   248     if (!proxy->binding()->kind() == BoundName::VARIABLE) {
   249         cc_.reportError(expression->pos(), Message_ExpectedLValue);
   250         return NULL;
   251     }
   252 
   253     Variable *var = proxy->binding()->toVariable();
   254     if (!var->type())
   255         return NULL;
   256 
   257     if  (var->type()->isReference())
   258         return var->type()->contained();
   259     return var->type();
   260 }
   261 
   262 void
   263 SemanticAnalysis::visit(NameProxy *name)
   264 {
   265     name->setType(name->binding()->type());
   266 }
   267 
   268 void
   269 SemanticAnalysis::visit(Assignment *node)
   270 {
   271     Local<Type> exprType(zone_, visitForLoad(node->expression()));
   272     if (!exprType)
   273         return ;
   274 
   275     Local<Type> type(zone_, visitForLValue(node->lvalue()));
   276     if (!type)
   277         return;
   278 
   279     if (node->token() != TOK_ASSIGN) {
   280         if (!typeCheckBinary(node, node->token(), type, exprType))
   281             return;
   282     }
   283 
   284     if (!typeCheckAssignment(node, exprType, type, true))
   285         return;
   286 
   287     node->setType(type);
   288 }
   289 
   290 void
   291 SemanticAnalysis::visit(IncDecExpression *node)
   292 {
   293     Local<Type> type(zone_, visitForLValue(node->expression()));
   294     if (!type)
   295         return;
   296 
   297     assert(!type->isReference());
   298 
   299     if ((!type->isInt32() || (type->isInt32() && type->tag())) && !type->isFloat()) {
   300         unaryTypeError(node, node->token(), type);
   301         return;
   302     }
   303 
   304     node->setType(type);
   305 }
   306 
   307 void
   308 SemanticAnalysis::visit(BinaryExpression *node)
   309 {
   310     Local<Type> left(zone_, visitForLoad(node->left()));
   311     Local<Type> right(zone_, visitForLoad(node->right()));
   312     if (!left || !right)
   313         return;
   314 
   315     Local<Type> result(zone_, typeCheckBinary(node, node->token(), left, right));
   316     if (!result)
   317         return;
   318 
   319     node->setType(result);
   320 }
   321 
   322 void
   323 SemanticAnalysis::visit(ReturnStatement *node)
   324 {
   325     Local<Type> type(zone_, visitForLoad(node->expression()));
   326     if (!type)
   327         return;
   328 
   329     Local<Type> returnType(zone_, function_->returnType());
   330     if (!typeCheckAssignment(node, type, returnType, true))
   331         return;
   332 }
   333 
   334 void
   335 SemanticAnalysis::visit(ForStatement *node)
   336 {
   337     loopDepth_++;
   338     if (node->scope())
   339         scope_ = node->scope();
   340 
   341     if (node->initialization())
   342         node->initialization()->accept(this);
   343 
   344     if (node->condition()) {
   345         Local<Type> type(zone_, visitForLoad(node->condition()));
   346         if (!type || !canTest(node->condition(), type)) {
   347             loopDepth_--;
   348             return;
   349         }
   350     }
   351 
   352     if (node->update())
   353         node->update()->accept(this);
   354 
   355     node->body()->accept(this);
   356 
   357     if (node->scope())
   358         scope_ = scope_->enclosing();
   359     loopDepth_--;
   360 }
   361 
   362 void 
   363 SemanticAnalysis::visit(WhileStatement *node)
   364 {
   365     loopDepth_++;
   366 
   367     Local<Type> type(zone_, visitForLoad(node->condition()));
   368     if (!type || !canTest(node->condition(), type)) {
   369         loopDepth_--;
   370         return;
   371     }
   372 
   373     node->body()->accept(this);
   374     loopDepth_--;
   375 }
   376 
   377 void
   378 SemanticAnalysis::visit(BlockStatement *node)
   379 {
   380     if (node->scope())
   381         scope_ = node->scope();
   382     for (size_t i = 0; i < node->statements()->length(); i++) {
   383         Statement *statement = node->statements()->at(i);
   384         statement->accept(this);
   385     }
   386     if (node->scope())
   387         scope_ = scope_->enclosing();
   388 }
   389 
   390 void
   391 SemanticAnalysis::visit(VariableDeclaration *node)
   392 {
   393     Local<String> tag(zone_, node->tag());
   394     Local<Type> type(zone_, zone_->types()->getPrimitiveForTag(tag));
   395     if (!type)
   396         return;
   397 
   398     if (node->postDimensions()) {
   399         Local<Type> init(zone_);
   400         if (node->initialization()) {
   401             if ((init = visitAndApplyType(node->initialization())) == NULL)
   402                 return;
   403         }
   404 
   405         if (node->postDimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
   406             cc_.reportError(node->pos(), Message_MaximumArrayDepth);
   407             return;
   408         }
   409 
   410         // If we have an initializer, make sure it's the same type.
   411         if (init && node->postDimensions()->length() != init->levels()) {
   412             cc_.reportError(node->pos(), Message_ArrayInitializerDepth);
   413             return;
   414         }
   415 
   416         Local<Type> check(zone_, init);
   417         for (size_t i = node->postDimensions()->length() - 1; i < node->postDimensions()->length(); i--) {
   418             Expression *expr = node->postDimensions()->at(i);
   419             int size = -1;
   420 
   421             if (expr) {
   422                 Local<Type> exprType(zone_, visitForLoad(expr));
   423                 if (!exprType)
   424                     return;
   425 
   426                 if (!IsPrimitiveType(exprType, PrimitiveType_Int32)) {
   427                     cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
   428                     return;
   429                 }
   430 
   431                 BoxedPrimitive box;
   432                 if (EvaluateForConstant(expr, &box) && box.type() == PrimitiveType_Int32) {
   433                     size = box.toInt();
   434                     if (size <= 0) {
   435                         cc_.reportError(expr->pos(), Message_BadArraySize);
   436                         return;
   437                     }
   438                     if (size >= INT_MAX / sizeof(int)) {
   439                         cc_.reportError(expr->pos(), Message_ArraySizeTooBig);
   440                         return;
   441                     }
   442 
   443                     if (check) {
   444                         if (check->elements() == -1) {
   445                             cc_.reportError(expr->pos(), Message_ArraySizeCannotBeDetermined);
   446                             return;
   447                         }
   448 
   449                         if (check->isCharArray()) {
   450                             if (check->elements() > size) {
   451                                 cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
   452                                 return;
   453                             }
   454                         } else if (check->elements() != size) {
   455                             cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, unsigned(i));
   456                             return;
   457                         }
   458                     }
   459                 } else {
   460                     if (init) {
   461                         cc_.reportError(node->pos(), Message_DynamicArrayHasInitializer);
   462                         return;
   463                     }
   464 
   465                     // here until we get dynamic array support.
   466                     cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
   467                     return;
   468                 }
   469             } else {
   470                 // Infer the size from the initializer.
   471                 if (!init) {
   472                     cc_.reportError(node->pos(), Message_ArraySizeNotSpecified);
   473                     return;
   474                 }
   475                 if (init->elements() == -1) {
   476                     cc_.reportError(node->pos(), Message_ArraySizeCannotBeDetermined);
   477                     return;
   478                 }
   479                 size = init->elements();
   480             }
   481 
   482             type = zone_->types()->unsafeArray(type, size);
   483             if (!type)
   484                 return;
   485 
   486             if (check)
   487                 check = check->contained();
   488         }
   489 
   490         // Right now, any array is allowed on the LIFO heap. In case the
   491         // runtime actually decides to use the LIFO heap, we must reserve
   492         // a slot so we can unwind the heap when the current scope leaves.
   493         scope_->setUsesLifoHeap();
   494     } else {
   495         if (node->initialization()) {
   496             Local<Type> right(zone_, visitForLoad(node->initialization()));
   497             if (!right)
   498                 return;
   499             if (!typeCheckAssignment(node, type, right, true))
   500                 return;
   501         }
   502         if (type->primitive() == PrimitiveType_Char) {
   503             cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
   504             return;
   505         }
   506     }
   507 
   508     node->variable()->setType(type);
   509 }
   510 
   511 void
   512 SemanticAnalysis::visit(IntegerLiteral *node)
   513 {
   514     node->setType(zone_->types()->getPrimitive(PrimitiveType_Int32));
   515 }
   516 
   517 void
   518 SemanticAnalysis::visit(ExpressionStatement *node)
   519 {
   520     node->expression()->accept(this);
   521 }
   522 
   523 bool
   524 SemanticAnalysis::preanalyze(FunctionStatement *node)
   525 {
   526     Local<FixedArray> args(zone_, FixedArray::New(zone_, node->parameters()->length(), Heap::Tenure_Old));
   527     if (!args)
   528         return false;
   529 
   530     // Apply types to arguments.
   531     for (size_t i = 0; i < node->parameters()->length(); i++) {
   532         Parameter *parameter = node->parameters()->at(i);
   533         
   534         Local<String> tag(zone_, parameter->tag());
   535         Local<Type> type(zone_, zone_->types()->getPrimitiveForTag(tag));
   536         if (!type)
   537             return false;
   538 
   539         if (type->primitive() == PrimitiveType_Char) {
   540             cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
   541             return false;
   542         }
   543 
   544         if (parameter->reference()) {
   545             if (parameter->dimensions()) {
   546                 cc_.reportError(parameter->variable()->pos(), Message_ArrayCantBeReference);
   547                 return NULL;
   548             }
   549 
   550             type = zone_->types()->getReference(type);
   551             if (!type)
   552                 return false;
   553         } else if (parameter->dimensions()) {
   554             if (parameter->dimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
   555                 cc_.reportError(node->pos(), Message_MaximumArrayDepth);
   556                 return false;
   557             }
   558 
   559             for (size_t i = parameter->dimensions()->length() - 1;
   560                  i < parameter->dimensions()->length();
   561                  i++)
   562             {
   563                 int size = Type::UNKNOWN_SIZE;
   564                 if (Expression *expr = parameter->dimensions()->at(i)) {
   565                     BoxedPrimitive box;
   566                     if (!EvaluateForConstant(expr, &box)) {
   567                         cc_.reportError(expr->pos(), Message_ArraySizeMustBeConstant);
   568                         return false;
   569                     }
   570                     size = box.toInt();
   571                 }
   572 
   573                 type = Type::NewArray(zone_, type, size, false);
   574                 if (!type)
   575                     return false;
   576             }
   577         }
   578 
   579         args->set(zone_, i, type);
   580         parameter->variable()->setType(type);
   581     }
   582 
   583     Local<Type> returnType(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
   584     if (!returnType)
   585         return false;
   586 
   587     Local<Type> funtype(zone_, zone_->types()->newFunction(returnType, args));
   588     if (!funtype)
   589         return false;
   590     
   591     // If the function already has a type, it was forward declared before, and
   592     // now this is our chance to type check the second declaration.
   593     if (node->fun()->type()) {
   594         Local<Type> forward(zone_, node->fun()->type());
   595         if (!Type::Compare(funtype, forward)) {
   596             cc_.reportError(node->pos(), Message_ForwardDeclareMismatches);
   597             return false;
   598         }
   599         node->fun()->setForwardType(forward);
   600     }
   601 
   602     node->fun()->setType(funtype);
   603 
   604     return true;
   605 }
   606 
   607 void
   608 SemanticAnalysis::visit(FunctionStatement *node)
   609 {
   610     function_ = node->fun()->type();
   611     scope_ = node->scope();
   612     if (node->body())
   613         node->body()->accept(this);
   614     scope_ = NULL;
   615     function_ = NULL;
   616 }
   617 
   618 bool
   619 SemanticAnalysis::typeCheckArrayAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
   620 {
   621     Local<Type> actual(zone_, argActual);
   622     Local<Type> given(zone_, argGiven);
   623 
   624     if (!given->isArray()) {
   625         cc_.reportError(node->pos(), Message_ExpectedArray);
   626         return false;
   627     }
   628     if (given->levels() != actual->levels()) {
   629         cc_.reportError(node->pos(), Message_ArrayDimensionsMismatch);
   630         return false;
   631     }
   632 
   633     unsigned level = 0;
   634     while (given->isArray()) {
   635         if (actual->elements() != -1) {
   636             if (forAssign) {
   637                 if (given->elements() == -1 || actual->elements() == -1) {
   638                     cc_.reportError(node->pos(), Message_ArraySizeNotKnown);
   639                     return false;
   640                 }
   641 
   642                 if (actual->isCharArray()) {
   643                     // String assignment can have a truncated |given|.
   644                     if (given->elements() > actual->elements()) {
   645                         cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
   646                         return false;
   647                     }
   648                 } else {
   649                     if (given->elements() != actual->elements()) {
   650                         cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
   651                         return false;
   652                     }
   653                 }
   654             } else {
   655                 if (actual->elements() != -1 && given->elements() != actual->elements()) {
   656                     cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
   657                     return false;
   658                 }
   659             }
   660         }
   661         given = given->contained();
   662         actual = actual->contained();
   663     }
   664 
   665     if (given->primitive() != actual->primitive()) {
   666         // :TODO: leaks an interior pointer.
   667         const char *givenTag = given->tag() ? given->tag()->chars() : "_";
   668         const char *actualTag = given->tag() ? given->tag()->chars() : "_";
   669         cc_.reportError(node->pos(), Message_TagMismatch, actualTag, givenTag);
   670         return false;
   671     }
   672 
   673     return true;
   674 }
   675 
   676 bool
   677 SemanticAnalysis::typeCheckAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
   678 {
   679     Local<Type> actual(zone_, argActual);
   680     Local<Type> given(zone_, argGiven);
   681 
   682     if (actual->isArray())
   683         return typeCheckArrayAssignment(node, given, actual, forAssign);
   684 
   685     if (given->isArray()) {
   686         cc_.reportError(node->pos(), Message_CannotAssignArrayToScalar);
   687         return false;
   688     }
   689 
   690     // The given type may not have been dereferenced.
   691     if (given->isReference())
   692         given = given->contained();
   693     if (actual->isReference())
   694         actual = actual->contained();
   695 
   696     if (!given->isPrimitive() || !actual->isPrimitive()) {
   697         cc_.reportError(node->pos(), Message_UnsupportedType);
   698         return false;
   699     }
   700     
   701     if (given->primitive() != actual->primitive()) {
   702         // :TODO: leaks an interior pointer
   703         const char *givenTag = given->tag() ? given->tag()->chars() : "_";
   704         const char *actualTag = given->tag() ? given->tag()->chars() : "_";
   705         cc_.reportError(node->pos(), Message_TagMismatch, actualTag, givenTag);
   706         return false;
   707     }
   708 
   709     return true;
   710 }
   711 
   712 bool
   713 SemanticAnalysis::typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual)
   714 {
   715     return typeCheckAssignment(node, given, actual, false);
   716 }
   717 
   718 void
   719 SemanticAnalysis::visit(CallExpression *node)
   720 {
   721     Local<Type> type(zone_, visitForLoad(node->callee()));
   722     if (!type)
   723         return;
   724 
   725     if (!type->isFunction()) {
   726         cc_.reportError(node->pos(), Message_CalleeNotFunction);
   727         return;
   728     }
   729     if (node->arguments()->length() != type->parameters()->length()) {
   730         cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
   731         return;
   732     }
   733 
   734     // Currently, only calls from a NameProxy are possible.
   735     NamedFunction *fun = node->callee()->toNameProxy()->binding()->toNamedFunction();
   736     if (fun->isForward()) {
   737         cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
   738         return;
   739     }
   740 
   741     for (unsigned i = 0; i < node->arguments()->length(); i++) {
   742         // Note: we don't compute references unless the callee needs a
   743         // reference type.
   744         Expression *expression = node->arguments()->at(i);
   745         Local<Type> given(zone_, visitAndApplyType(expression));
   746         if (!given)
   747             return;
   748 
   749         // If it's an IndexExpression, we special case the ability to compute
   750         // a reference.
   751         if (expression->isIndexExpression())
   752             given = given->contained();
   753 
   754         Local<Type> actual(zone_, type->parameterAt(i));
   755         if (!typeCheckParameter(expression, given, actual))
   756             return;
   757 
   758         // If the actual is a reference, and the given is not, we'll need to
   759         // compute a reference, which may be allocated on the stack.
   760         if (actual->isReference() && !given->isReference())
   761             scope_->setUsesLifoHeap();
   762     }
   763 
   764     node->setType(fun->type()->returnType());
   765 }
   766 
   767 void
   768 SemanticAnalysis::visit(IfStatement *node)
   769 {
   770     Local<Type> type(zone_, visitForLoad(node->condition()));
   771     if (!type)
   772         return;
   773 
   774     if (!canTest(node, type))
   775         return;
   776 
   777     node->ifTrue()->accept(this);
   778     if (node->ifFalse())
   779         node->ifFalse()->accept(this);
   780 }
   781 
   782 void
   783 SemanticAnalysis::visit(IndexExpression *node)
   784 {
   785     Local<Type> left(zone_, visitForRef(node->left()));
   786     Local<Type> right(zone_, visitForLoad(node->right()));
   787 
   788     if (!left || !right)
   789         return;
   790 
   791     if (!right->isInt32()) {
   792         cc_.reportError(node->right()->pos(), Message_IndexMustBeInteger);
   793         return;
   794     }
   795     if (!left->isArray()) {
   796         cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
   797         return;
   798     }
   799     node->setType(left);
   800 }
   801 
   802 void
   803 SemanticAnalysis::visit(EnumStatement *node)
   804 {
   805     // Forbid the blessed types.
   806     Local<String> tag(zone_, node->name());
   807     if (!strcmp(tag->chars(), "Float") ||
   808         !strcmp(tag->chars(), "String") ||
   809         !strcmp(tag->chars(), "any") ||
   810         !strcmp(tag->chars(), "bool"))
   811     {
   812         cc_.reportError(node->pos(), Message_InvalidEnumName);
   813         return;
   814     }
   815 
   816     Local<Type> type(zone_, zone_->types()->getPrimitiveForTag(tag));
   817     if (!type)
   818         return;
   819 
   820     int value = 0;
   821     for (size_t i = 0; i < node->entries()->length(); i++) {
   822         EnumStatement::Entry &entry = node->entries()->at(i);
   823 
   824         BoxedPrimitive box = BoxedPrimitive::Int(value);
   825         if (entry.expr) {
   826             entry.expr->accept(this);
   827             if (!EvaluateForConstant(entry.expr, &box)) {
   828                 cc_.reportError(entry.expr->pos(), Message_EnumFieldNotConstant);
   829                 return;
   830             }
   831 
   832             assert(box.type() == PrimitiveType_Int32);
   833             value = box.toInt();
   834         }
   835 
   836         entry.name->set(type, box);
   837         value++;
   838     }
   839 }
   840 
   841 void
   842 SemanticAnalysis::visit(StringLiteral *node)
   843 {
   844     Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Char));
   845     if (!type)
   846         return;
   847 
   848     type = Type::NewArray(zone_, type, node->string()->length() + 1, true);
   849     if (!type)
   850         return;
   851 
   852     node->setType(type);
   853 }
   854 
   855 void
   856 SemanticAnalysis::visit(FloatLiteral *node)
   857 {
   858     Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
   859     if (!type)
   860         return;
   861 
   862     node->setType(type);
   863 }
   864 
   865 void
   866 SemanticAnalysis::visit(BreakStatement *node)
   867 {
   868     if (loopDepth_ == 0)
   869         cc_.reportError(node->pos(), Message_BreakOutsideLoop);
   870 }
   871 
   872 void
   873 SemanticAnalysis::visit(ContinueStatement *node)
   874 {
   875     if (loopDepth_ == 0)
   876         cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
   877 }