Introduced type semantics, completely replacing tag semantics.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
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)
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.
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/.
20 #include "SemanticAnalysis.h"
23 #include "TypeManager.h"
25 #include "NameBinding.h"
32 IsPrimitiveType(Type *type, PrimitiveType primitive)
37 if (!type->isPrimitive())
40 return primitive == type->primitive();
44 EvaluateBinaryOperation(TokenKind kind, const BoxedPrimitive &left, const BoxedPrimitive &right,
49 *out = BoxedPrimitive::Int(left.toInt() + right.toInt());
53 *out = BoxedPrimitive::Int(left.toInt() << right.toInt());
62 EvaluateUnaryOperation(TokenKind kind, const BoxedPrimitive &inner, BoxedPrimitive *out)
66 *out = BoxedPrimitive::Int(-inner.toInt());
75 EvaluateForConstant(Expression *expression, BoxedPrimitive *out)
77 switch (expression->kind()) {
78 case AstNode::kBinaryExpression:
82 BinaryExpression *b = expression->toBinaryExpression();
83 if (!EvaluateForConstant(b->left(), &left) ||
84 !EvaluateForConstant(b->right(), &right))
88 return EvaluateBinaryOperation(b->token(), left, right, out);
91 case AstNode::kUnaryExpression:
94 UnaryExpression *u = expression->toUnaryExpression();
95 if (!EvaluateForConstant(u->expression(), &inner))
97 return EvaluateUnaryOperation(u->token(), inner, out);
100 case AstNode::kNameProxy:
102 NameProxy *proxy = expression->toNameProxy();
103 if (proxy->binding()->kind() != BoundName::CONSTANT)
106 NamedConstant *c = proxy->binding()->toNamedConstant();
107 if (!IsPrimitiveType(c->type(), PrimitiveType_Int32))
114 case AstNode::kIntegerLiteral:
115 *out = BoxedPrimitive::Int(expression->toIntegerLiteral()->value());
123 SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, ParseTree *tree)
133 SemanticAnalysis::analyze()
138 scope_ = tree_->scope();
140 for (size_t i = 0; i < tree_->statements()->length(); i++) {
141 Statement *statement = tree_->statements()->at(i);
142 if (statement->isFunctionStatement()) {
143 FunctionStatement *funstmt = statement->toFunctionStatement();
144 if (!preanalyze(funstmt))
146 } else if (statement->isEnumStatement()) {
147 EnumStatement *enumstmt = statement->toEnumStatement();
149 // We pre-fill global enum types, since they may occur in function
150 // signatures. This should probably be somewhere else...
151 Local<String> name(zone_, enumstmt->name());
152 Local<Type> type(zone_);
154 type = zone_->types()->newEnum(name);
156 type = zone_->types()->getPrimitive(PrimitiveType_Int32);
161 enumstmt->decl()->setType(type);
165 for (size_t i = 0; i < tree_->statements()->length(); i++) {
166 Statement *statement = tree_->statements()->at(i);
167 statement->accept(this);
170 if (!determineLiveFunctions())
173 return !cc_.failures();
177 SemanticAnalysis::determineLiveFunctions()
179 Vector<NamedFunction *> worklist;
182 for (size_t i = 0; i < tree_->statements()->length(); i++) {
183 Statement *statement = tree_->statements()->at(i);
184 if (!statement->isFunctionStatement())
187 FunctionStatement *stmt = statement->toFunctionStatement();
188 NamedFunction *fun = stmt->fun();
192 for (size_t i = 0; i < fun->usedGlobals()->length(); i++) {
193 NamedFunction *name = fun->usedGlobals()->at(i)->toNamedFunction();
195 // Names in these lists are always unused, however, we mark them
196 // as used while inserting into the work queue, to avoid double-
201 if (!worklist.append(name))
208 while (!worklist.empty()) {
209 NamedFunction *fun = worklist.popCopy();
211 for (size_t i = 0; i < fun->usedGlobals()->length(); i++) {
212 NamedFunction *name = fun->usedGlobals()->at(i)->toNamedFunction();
215 if (!worklist.append(name))
225 SemanticAnalysis::bindType(const SourcePosition &pos, BoundName *name)
228 return zone_->types()->getPrimitive(PrimitiveType_Int32);
230 if (name->kind() == BoundName::UNBOUND) {
231 cc_.reportError(pos, Message_TypeNotFound, name->name()->chars());
235 if (name->kind() != BoundName::TYPE) {
236 cc_.reportError(pos, Message_IdentifierIsNotAType, name->name()->chars());
247 SemanticAnalysis::visitAndApplyType(Expression *expression)
249 expression->accept(this);
250 return expression->type();
254 SemanticAnalysis::visitForLoad(Expression *expression)
256 Local<Type> type(zone_, visitAndApplyType(expression));
260 if (type->isReference()) {
261 expression->setType(type->contained());
262 } else if (expression->isIndexExpression()) {
264 expression->setType(type->contained());
265 } else if (expression->isCallExpression()) {
266 if (type->isVoid()) {
267 CallExpression *call = expression->toCallExpression();
268 cc_.reportError(call->callee()->pos(), Message_FunctionDoesNotReturnValue);
273 return expression->type();
276 // :TODO: this leaks an interior pointer.
278 ke::GetTypeName(Type *type)
282 if (type->isFunction())
287 return type->name()->chars();
288 if (type->isReference())
289 type = type->contained();
290 switch (type->primitive()) {
291 case PrimitiveType_Bool:
293 case PrimitiveType_Char:
295 case PrimitiveType_Int32:
297 case PrimitiveType_Float:
305 SemanticAnalysis::unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type)
307 const char *op = TokenNames[token];
308 const char *name = GetTypeName(type);
310 cc_.reportError(node->pos(), Message_UnaryTypeError, op, name);
314 SemanticAnalysis::binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
316 const char *op = TokenNames[token];
317 const char *leftName = GetTypeName(left);
318 const char *rightName = GetTypeName(right);
320 cc_.reportError(node->pos(), Message_BinaryTypeError, op, leftName, rightName);
324 SemanticAnalysis::typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
326 if ((token >= TOK_PLUS && token <= TOK_SLASH) ||
327 (token >= TOK_ASSIGN_ADD && token <= TOK_ASSIGN_DIV))
329 // If either side is a float, the other side must be a float or int.
330 if (left->isFloat() || right->isFloat()) {
331 Type *floatType = left->isFloat() ? left : right;
332 Type *other = left->isFloat() ? right : left;
333 if (other->isFloat() || other->isInt32())
338 if (token == TOK_OR || token == TOK_AND) {
339 if (!canTest(node, left) || !canTest(node, right))
341 return zone_->types()->getPrimitive(PrimitiveType_Bool);
344 if (token >= TOK_EQUALS && token <= TOK_GE) {
345 if ((left->isFloat() || left->isInt32()) &&
346 (right->isFloat() || right->isInt32()))
348 return zone_->types()->getPrimitive(PrimitiveType_Bool);
350 binaryTypeError(node, token, left, right);
354 if (!left->isInt32() || !right->isInt32()) {
355 binaryTypeError(node, token, left, right);
363 SemanticAnalysis::canTest(AstNode *node, Handle<Type> type)
365 if (!type->isPrimitive() && !type->isEnum()) {
366 cc_.reportError(node->pos(), Message_ArrayIsNotBoolean);
373 SemanticAnalysis::visitForLValue(Expression *expression)
375 if (expression->isIndexExpression()) {
376 Type *type = visitAndApplyType(expression);
380 if (type->isConst()) {
381 cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
385 return type->contained();
388 if (!expression->isNameProxy()) {
389 cc_.reportError(expression->pos(), Message_ExpectedLValue);
393 NameProxy *proxy = expression->toNameProxy();
394 if (!proxy->binding()->kind() == BoundName::VARIABLE) {
395 cc_.reportError(expression->pos(), Message_ExpectedLValue);
399 Variable *var = proxy->binding()->toVariable();
403 if (var->type()->isReference()) {
404 if (var->type()->isConst()) {
405 cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
408 return var->type()->contained();
413 SemanticAnalysis::ControlStatus
414 SemanticAnalysis::visitForControl(Statement *statement)
416 ControlScope scope(&control_);
417 statement->accept(this);
418 return scope.status();
422 SemanticAnalysis::visit(NameProxy *name)
424 BoundName *binding = name->binding();
426 if (binding->scope()->kind() == Scope::GLOBAL) {
427 if (!function_ || function_->isPublic()) {
428 // Whatever we're referencing must be kept alive no matter what.
429 // As such, we simply mark it used, and don't do anything special.
431 } else if (!binding->used() && binding->kind() == BoundName::FUNCTION) {
432 // Otherwise, we're inside a normal stock or function. Since we
433 // don't really know if we've been used yet, we shouldn't
434 // prematurely references as alive. Instead, we keep a list of
435 // which names have been used, so we can mark them later if needed.
437 // Note that we skip this step if the binding is already in the
439 if (!function_->addUsedGlobal(binding))
444 name->setType(binding->type());
448 SemanticAnalysis::visit(Assignment *node)
450 Local<Type> exprType(zone_, visitForLoad(node->expression()));
454 Local<Type> type(zone_, visitForLValue(node->lvalue()));
458 if (node->token() != TOK_ASSIGN) {
459 if (!typeCheckBinary(node, node->token(), type, exprType))
463 if (!typeCheckAssignment(node, exprType, type, true))
470 SemanticAnalysis::visit(IncDecExpression *node)
472 Local<Type> type(zone_, visitForLValue(node->expression()));
476 assert(!type->isReference());
478 if (!type->isInt32() && !type->isFloat()) {
479 unaryTypeError(node, node->token(), type);
487 SemanticAnalysis::visit(UnaryExpression *node)
489 Local<Type> type(zone_, visitForLoad(node->expression()));
493 switch (node->token())
496 if (!canTest(node->expression(), type))
498 type = zone_->types()->getPrimitive(PrimitiveType_Int32);
505 if (!type->isFloat() && !type->isInt32()) {
506 unaryTypeError(node, node->token(), type);
514 Local<Type> to(zone_, bindType(node->pos(), node->type()));
518 if ((!type->isFloat() && !type->isInt32OrEnum()) ||
519 (!to->isFloat() && !to->isInt32OrEnum()))
521 unaryTypeError(node, node->token(), type);
530 assert(node->token() == TOK_TILDE);
531 if (!type->isInt32()) {
532 unaryTypeError(node, node->token(), type);
541 SemanticAnalysis::visit(TernaryExpression *node)
543 Local<Type> type(zone_, visitForLoad(node->condition()));
547 if (!canTest(node->condition(), type))
550 Local<Type> left(zone_, visitForLoad(node->left()));
551 Local<Type> right(zone_, visitForLoad(node->right()));
556 binaryTypeError(node, TOK_QMARK, left, right);
562 SemanticAnalysis::visit(BinaryExpression *node)
564 Local<Type> left(zone_, visitForLoad(node->left()));
565 Local<Type> right(zone_, visitForLoad(node->right()));
569 Local<Type> result(zone_, typeCheckBinary(node, node->token(), left, right));
573 node->setType(result);
577 SemanticAnalysis::visit(ReturnStatement *node)
579 // Set this early, so if we error we don't get more errors.
580 control_->returns(Returns_Always);
582 if (!node->expression()) {
583 if (!function_->type()->returnType()->isVoid())
584 cc_.reportError(node->pos(), Message_NotAllPathsReturnValue);
588 Local<Type> type(zone_, visitForLoad(node->expression()));
592 Local<Type> returnType(zone_, function_->type()->returnType());
593 if (!typeCheckAssignment(node, type, returnType, true))
598 SemanticAnalysis::visit(ForStatement *node)
600 LoopScope loop(&loop_);
603 scope_ = node->scope();
605 if (node->initialization())
606 node->initialization()->accept(this);
608 if (node->condition()) {
609 Local<Type> type(zone_, visitForLoad(node->condition()));
610 if (!type || !canTest(node->condition(), type)) {
616 node->update()->accept(this);
618 ControlStatus status = visitForControl(node->body());
620 // If there is a condition, we treat this like a while loop. Otherwise,
621 // join point is only reachable if the loop contains a break. The
622 // termination status doesn't tell us anything yet.
623 if (node->condition() || loop.breaks())
624 control_->merge(status, ControlStatus(false, Returns_Never));
626 control_->terminate(status.returns());
629 scope_ = scope_->enclosing();
633 SemanticAnalysis::visit(WhileStatement *node)
635 LoopScope loop(&loop_);
637 Local<Type> type(zone_, visitForLoad(node->condition()));
638 if (!type || !canTest(node->condition(), type)) {
642 ControlStatus status = visitForControl(node->body());
644 // Detect loops that will always have a fork in the control-flow graph.
645 // This will be a |while| loop, or a |do-while| loop whose body does
648 // For now, we treat these as an |if| regardless of the direction the
649 // branch can take. That means the control-flow is always conditional.
650 if (node->token() == TOK_WHILE ||
651 !status.terminates() ||
654 control_->merge(status, ControlStatus(false, Returns_Never));
656 // At this point, we've got a do-while loop with a |return| or
657 // |continue|, meaning that the rest of the block is unreachable.
658 control_->terminate(status.returns());
663 SemanticAnalysis::visit(BlockStatement *node)
666 scope_ = node->scope();
667 for (size_t i = 0; i < node->statements()->length(); i++) {
668 Statement *statement = node->statements()->at(i);
669 statement->accept(this);
672 scope_ = scope_->enclosing();
676 IsScalarString(Type *type)
678 if (type->isPrimitive() && type->primitive() == PrimitiveType_Char)
680 if (type->isReference())
681 return IsScalarString(type->contained());
686 SemanticAnalysis::visit(VariableDeclaration *node)
688 Local<Type> type(zone_, bindType(node->pos(), node->type()));
692 if (node->postDimensions()) {
693 Local<Type> init(zone_);
694 if (node->initialization()) {
695 if ((init = visitForLoad(node->initialization())) == NULL)
699 if (node->postDimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
700 cc_.reportError(node->pos(), Message_MaximumArrayDepth);
704 // If we have an initializer, make sure it's the same type.
705 if (init && node->postDimensions()->length() != init->levels()) {
706 cc_.reportError(node->pos(), Message_ArrayInitializerDepth);
710 Local<Type> check(zone_, init);
711 for (size_t i = node->postDimensions()->length() - 1; i < node->postDimensions()->length(); i--) {
712 Expression *expr = node->postDimensions()->at(i);
716 Local<Type> exprType(zone_, visitForLoad(expr));
720 if (!IsPrimitiveType(exprType, PrimitiveType_Int32)) {
721 cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
726 if (EvaluateForConstant(expr, &box) && box.type() == PrimitiveType_Int32) {
729 cc_.reportError(expr->pos(), Message_BadArraySize);
732 if (size >= int(INT_MAX / sizeof(int))) {
733 cc_.reportError(expr->pos(), Message_ArraySizeTooBig);
738 if (check->elements() == -1) {
739 cc_.reportError(expr->pos(), Message_ArraySizeCannotBeDetermined);
743 if (check->isCharArray()) {
744 if (check->elements() > size) {
745 cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
748 } else if (check->elements() != size) {
749 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, unsigned(i));
755 cc_.reportError(node->pos(), Message_DynamicArrayHasInitializer);
759 // here until we get dynamic array support.
760 cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
764 // Infer the size from the initializer.
766 cc_.reportError(node->pos(), Message_ArraySizeNotSpecified);
769 if (init->elements() == -1) {
770 cc_.reportError(node->pos(), Message_ArraySizeCannotBeDetermined);
773 size = init->elements();
776 type = zone_->types()->unsafeArray(type, size, false);
781 check = check->contained();
784 // If this variable is stack allocated, we could use the LIFO heap
785 // to allocate it. If that's the case, make sure to reserve a slot
786 // so we can unwind the LIFO heap at the end of this scope.
787 if (!node->variable()->needsHeapAllocation())
788 scope_->setUsesLifoHeap();
790 if (node->initialization()) {
791 Local<Type> right(zone_, visitForLoad(node->initialization()));
794 if (!typeCheckAssignment(node, type, right, true))
797 if (type->primitive() == PrimitiveType_Char) {
798 cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
803 node->variable()->setType(type);
807 SemanticAnalysis::visit(IntegerLiteral *node)
809 node->setType(zone_->types()->getPrimitive(PrimitiveType_Int32));
813 SemanticAnalysis::visit(BooleanLiteral *node)
815 node->setType(zone_->types()->getPrimitive(PrimitiveType_Bool));
819 SemanticAnalysis::visit(ExpressionStatement *node)
821 visitAndApplyType(node->expression());
825 SemanticAnalysis::preanalyze(FunctionStatement *node)
827 Local<FixedArray> args(zone_, FixedArray::New(zone_, node->parameters()->length(), Heap::Tenure_Old));
831 // Apply types to arguments.
832 for (size_t i = 0; i < node->parameters()->length(); i++) {
833 Parameter *parameter = node->parameters()->at(i);
835 Local<Type> type(zone_, bindType(parameter->variable()->pos(), parameter->type()));
839 if (parameter->reference()) {
840 if (parameter->dimensions()) {
841 cc_.reportError(parameter->variable()->pos(), Message_ArrayCantBeReference);
845 type = zone_->types()->getReference(type, parameter->isConst());
848 } else if (parameter->dimensions()) {
849 if (parameter->dimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
850 cc_.reportError(parameter->variable()->pos(), Message_MaximumArrayDepth);
854 if (node->fun()->isNative() && parameter->dimensions()->length() > 1) {
855 cc_.reportError(parameter->variable()->pos(), Message_MultiArrayInNative);
859 for (size_t i = parameter->dimensions()->length() - 1;
860 i < parameter->dimensions()->length();
863 int size = Type::UNKNOWN_SIZE;
864 if (Expression *expr = parameter->dimensions()->at(i)) {
866 if (!EvaluateForConstant(expr, &box)) {
867 cc_.reportError(expr->pos(), Message_ArraySizeMustBeConstant);
873 type = zone_->types()->unsafeArray(type, size, parameter->isConst());
879 if (IsScalarString(type)) {
880 cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
884 args->set(zone_, i, type);
885 parameter->variable()->setType(type);
888 Local<Type> returnType(zone_);
889 if (node->returnType() ||
890 node->hasExplicitReturn() ||
891 node->fun()->isNative())
893 returnType = bindType(node->pos(), node->returnType());
895 returnType = zone_->types()->getVoid();
900 Local<Type> funtype(zone_);
901 if (node->fun()->isNative())
902 funtype = zone_->types()->newNative(returnType, args, node->fun()->isNativeVariadic());
904 funtype = zone_->types()->newFunction(returnType, args);
908 // If the function already has a type, it was forward declared before, and
909 // now this is our chance to type check the second declaration.
910 if (node->fun()->type()) {
911 Local<Type> forward(zone_, node->fun()->type());
912 if (!Type::Compare(funtype, forward)) {
913 cc_.reportError(node->pos(), Message_ForwardDeclareMismatches);
916 node->fun()->setForwardType(forward);
919 node->fun()->setType(funtype);
925 SemanticAnalysis::visit(FunctionStatement *node)
927 function_ = node->fun();
928 scope_ = node->scope();
930 ControlScope control(&control_);
932 node->body()->accept(this);
934 ControlStatus status = control.status();
935 if (status.returns() == Returns_Sometimes) {
936 cc_.reportError(node->pos(), Message_NotAllPathsReturnValue);
937 } else if (status.returns() == Returns_Never) {
938 if (!function_->type()->returnType()->isVoid())
939 cc_.reportError(node->pos(), Message_MissingReturnValue);
948 SemanticAnalysis::typeMismatch(AstNode *node, Handle<Type> actual, Handle<Type> given)
950 cc_.reportError(node->pos(),Message_TypeMismatch,
956 SemanticAnalysis::typeCheckArrayAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
958 Local<Type> actual(zone_, argActual);
959 Local<Type> given(zone_, argGiven);
961 if (!given->isArray()) {
962 cc_.reportError(node->pos(), Message_ExpectedArray);
965 if (given->levels() != actual->levels()) {
966 cc_.reportError(node->pos(), Message_ArrayDimensionsMismatch);
971 while (given->isArray()) {
972 if (actual->elements() != -1) {
974 if (given->elements() == -1 || actual->elements() == -1) {
975 cc_.reportError(node->pos(), Message_ArraySizeNotKnown);
979 if (actual->isCharArray()) {
980 // String assignment can have a truncated |given|.
981 if (given->elements() > actual->elements()) {
982 cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
986 if (given->elements() != actual->elements()) {
987 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
992 if (actual->elements() != -1 && given->elements() != actual->elements()) {
993 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
998 given = given->contained();
999 actual = actual->contained();
1002 return typeCheckAssignment(node, given, actual, forAssign);
1006 SemanticAnalysis::typeCheckAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
1008 Local<Type> actual(zone_, argActual);
1009 Local<Type> given(zone_, argGiven);
1011 if (actual->isArray())
1012 return typeCheckArrayAssignment(node, given, actual, forAssign);
1014 if (given->isArray()) {
1015 cc_.reportError(node->pos(), Message_CannotAssignArrayToScalar);
1019 // The given type may not have been dereferenced.
1020 if (given->isReference())
1021 given = given->contained();
1022 if (actual->isReference())
1023 actual = actual->contained();
1025 if (actual->isEnum()) {
1026 if (!given->isEnum() || actual != given) {
1027 typeMismatch(node, actual, given);
1033 if (given->isEnum() || given->isFunction()) {
1034 typeMismatch(node, actual, given);
1038 if (given->primitive() != actual->primitive()) {
1039 typeMismatch(node, actual, given);
1047 SemanticAnalysis::typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual)
1049 return typeCheckAssignment(node, given, actual, false);
1053 SemanticAnalysis::visit(CallExpression *node)
1055 Local<Type> type(zone_, visitForLoad(node->callee()));
1059 if (!type->isFunction()) {
1060 cc_.reportError(node->pos(), Message_CalleeNotFunction);
1063 if (node->arguments()->length() != type->parameters()->length()) {
1064 if (!type->isNative() ||
1065 !type->isNativeVariadic() ||
1066 node->arguments()->length() < type->parameters()->length())
1068 cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
1073 // Currently, only calls from a NameProxy are possible.
1074 NamedFunction *fun = node->callee()->toNameProxy()->binding()->toNamedFunction();
1075 if (fun->isForward()) {
1076 cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
1080 for (unsigned i = 0; i < node->arguments()->length(); i++) {
1081 // Note: we don't compute references unless the callee needs a
1083 Expression *expression = node->arguments()->at(i);
1084 Local<Type> given(zone_, visitAndApplyType(expression));
1088 // Since we skip a load, check for a call expression that returns void.
1089 if (given->isVoid()) {
1090 assert(expression->isCallExpression());
1091 CallExpression *call = expression->toCallExpression();
1092 cc_.reportError(call->callee()->pos(), Message_FunctionDoesNotReturnValue);
1096 // If it's an IndexExpression, we special case the ability to compute
1098 if (expression->isIndexExpression())
1099 given = given->contained();
1101 bool mustComputeReference;
1102 if (i >= type->parameters()->length()) {
1103 // Special handling for natives. Arguments past the formal must
1104 // always have references computed. However, arrays are already
1105 // references for natives.
1106 assert(type->isNative() && type->isNativeVariadic());
1107 mustComputeReference = !given->isReference() && !given->isArray();
1109 Local<Type> actual(zone_, type->parameterAt(i));
1110 if (!typeCheckParameter(expression, given, actual))
1112 mustComputeReference = actual->isReference() && !given->isReference();
1115 // If the actual is a reference, and the given is not, we'll need to
1116 // compute a reference, which may be allocated on the stack.
1117 if (mustComputeReference)
1118 scope_->setUsesLifoHeap();
1121 node->setType(fun->type()->returnType());
1125 SemanticAnalysis::ControlScope::merge(const ControlStatus &left, const ControlStatus &right)
1127 // If both sides terminate in a return, we consider this a return as well.
1128 if (left.returns() == Returns_Always && right.returns() == Returns_Always) {
1129 terminate(Returns_Always);
1133 ReturnStatus status = (left.returns() == Returns_Never && right.returns() == Returns_Never)
1135 : Returns_Sometimes;
1137 if (left.terminates() && right.terminates())
1144 SemanticAnalysis::visit(IfStatement *node)
1146 Local<Type> type(zone_, visitForLoad(node->condition()));
1150 if (!canTest(node, type))
1153 ControlStatus trueBranch = visitForControl(node->ifTrue());
1154 ControlStatus falseBranch = ControlStatus(false, Returns_Never);
1156 if (node->ifFalse())
1157 falseBranch = visitForControl(node->ifFalse());
1159 control_->merge(trueBranch, falseBranch);
1163 SemanticAnalysis::visit(IndexExpression *node)
1165 Local<Type> left(zone_, visitForLoad(node->left()));
1166 Local<Type> right(zone_, visitForLoad(node->right()));
1168 if (!left || !right)
1171 if (!right->isInt32() && !right->isEnum()) {
1172 cc_.reportError(node->right()->pos(), Message_IndexMustBeInteger);
1175 if (!left->isArray()) {
1176 cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
1179 node->setType(left);
1183 SemanticAnalysis::visit(EnumStatement *node)
1185 Local<Type> type(zone_, node->decl()->type());
1190 for (size_t i = 0; i < node->entries()->length(); i++) {
1191 EnumStatement::Entry &entry = node->entries()->at(i);
1193 BoxedPrimitive box = BoxedPrimitive::Int(value);
1195 entry.expr->accept(this);
1196 if (!EvaluateForConstant(entry.expr, &box)) {
1197 cc_.reportError(entry.expr->pos(), Message_EnumFieldNotConstant);
1201 assert(box.type() == PrimitiveType_Int32);
1202 value = box.toInt();
1205 entry.name->set(type, box);
1211 SemanticAnalysis::visit(StringLiteral *node)
1213 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Char));
1217 type = Type::NewArray(zone_, type, node->string()->length() + 1, true);
1221 node->setType(type);
1225 SemanticAnalysis::visit(FloatLiteral *node)
1227 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
1231 node->setType(type);
1235 SemanticAnalysis::visit(BreakStatement *node)
1238 cc_.reportError(node->pos(), Message_BreakOutsideLoop);
1242 // Note: ignore breaks that occur in unreachable control-flow.
1243 if (!control_->status().terminates())
1246 control_->terminate(Returns_Never);
1250 SemanticAnalysis::visit(ContinueStatement *node)
1253 cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
1257 // Note: ignore continues that occur in unreachable control-flow.
1258 if (!control_->status().terminates())
1259 loop_->addContinue();
1261 control_->terminate(Returns_Never);