Implement re-tag unary expression.
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 EvaluateForConstant(Expression *expression, BoxedPrimitive *out)
64 switch (expression->kind()) {
65 case AstNode::kBinaryExpression:
69 BinaryExpression *b = expression->toBinaryExpression();
70 if (!EvaluateForConstant(b->left(), &left) ||
71 !EvaluateForConstant(b->right(), &right))
75 return EvaluateBinaryOperation(b->token(), left, right, out);
78 case AstNode::kNameProxy:
80 NameProxy *proxy = expression->toNameProxy();
81 if (proxy->binding()->kind() != BoundName::CONSTANT)
84 NamedConstant *c = proxy->binding()->toNamedConstant();
85 if (!IsPrimitiveType(c->type(), PrimitiveType_Int32))
92 case AstNode::kIntegerLiteral:
93 *out = BoxedPrimitive::Int(expression->toIntegerLiteral()->value());
101 SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, ParseTree *tree)
111 SemanticAnalysis::analyze()
116 scope_ = tree_->scope();
118 for (size_t i = 0; i < tree_->statements()->length(); i++) {
119 Statement *statement = tree_->statements()->at(i);
120 if (statement->isFunctionStatement()) {
121 FunctionStatement *funstmt = statement->toFunctionStatement();
122 if (!preanalyze(funstmt))
127 for (size_t i = 0; i < tree_->statements()->length(); i++) {
128 Statement *statement = tree_->statements()->at(i);
129 statement->accept(this);
132 if (!determineLiveFunctions())
135 return !cc_.failures();
139 SemanticAnalysis::determineLiveFunctions()
141 Vector<NamedFunction *> worklist;
144 for (size_t i = 0; i < tree_->statements()->length(); i++) {
145 Statement *statement = tree_->statements()->at(i);
146 if (!statement->isFunctionStatement())
149 FunctionStatement *stmt = statement->toFunctionStatement();
150 NamedFunction *fun = stmt->fun();
154 for (size_t i = 0; i < fun->usedGlobals()->length(); i++) {
155 NamedFunction *name = fun->usedGlobals()->at(i)->toNamedFunction();
157 // Names in these lists are always unused, however, we mark them
158 // as used while inserting into the work queue, to avoid double-
163 if (!worklist.append(name))
170 while (!worklist.empty()) {
171 NamedFunction *fun = worklist.popCopy();
173 for (size_t i = 0; i < fun->usedGlobals()->length(); i++) {
174 NamedFunction *name = fun->usedGlobals()->at(i)->toNamedFunction();
177 if (!worklist.append(name))
188 SemanticAnalysis::visitAndApplyType(Expression *expression)
190 expression->accept(this);
191 return expression->type();
195 SemanticAnalysis::visitForLoad(Expression *expression)
197 Local<Type> type(zone_, visitAndApplyType(expression));
201 if (type->isReference()) {
202 expression->setType(type->contained());
203 } else if (expression->isIndexExpression()) {
205 expression->setType(type->contained());
206 } else if (expression->isCallExpression()) {
207 if (type->isVoid()) {
208 CallExpression *call = expression->toCallExpression();
209 cc_.reportError(call->callee()->pos(), Message_FunctionDoesNotReturnValue);
214 return expression->type();
217 // :TODO: this leaks an interior pointer.
218 static inline const char *
219 GetTypeName(Type *type)
223 if (type->isFunction())
227 if (type->isReference())
228 type = type->contained();
230 return type->tag()->chars();
235 SemanticAnalysis::unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type)
237 const char *op = TokenNames[token];
238 const char *name = GetTypeName(type);
240 cc_.reportError(node->pos(), Message_UnaryTypeError, op, name);
244 SemanticAnalysis::binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
246 const char *op = TokenNames[token];
247 const char *leftName = GetTypeName(left);
248 const char *rightName = GetTypeName(right);
250 cc_.reportError(node->pos(), Message_BinaryTypeError, op, leftName, rightName);
254 SemanticAnalysis::typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
256 if ((token >= TOK_PLUS && token <= TOK_SLASH) ||
257 (token >= TOK_ASSIGN_ADD && token <= TOK_ASSIGN_DIV))
259 // If either side is a float, the other side must be a float or int.
260 if (left->isFloat() || right->isFloat()) {
261 Type *floatType = left->isFloat() ? left : right;
262 Type *other = left->isFloat() ? right : left;
263 if (other->isFloat() || (other->isInt32() && !other->tag()))
268 if (token == TOK_OR || token == TOK_AND) {
269 if (!canTest(node, left) || !canTest(node, right))
271 return zone_->types()->getPrimitive(PrimitiveType_Int32);
274 if (token >= TOK_EQUALS && token <= TOK_GE) {
275 if ((left->isFloat() || (left->isInt32() && !left->tag())) &&
276 (right->isFloat() || (right->isInt32() && !right->tag())))
278 return zone_->types()->getPrimitive(PrimitiveType_Int32);
280 binaryTypeError(node, token, left, right);
284 if ((!left->isInt32() || left->tag()) ||
285 (!right->isInt32() || right->tag()))
287 binaryTypeError(node, token, left, right);
295 SemanticAnalysis::canTest(AstNode *node, Handle<Type> type)
297 if (type->isArray() || (type->isInt32() && type->tag())) {
298 cc_.reportError(node->pos(), Message_ArrayIsNotBoolean);
305 SemanticAnalysis::visitForLValue(Expression *expression)
307 if (expression->isIndexExpression()) {
308 Type *type = visitAndApplyType(expression);
312 if (type->isConst()) {
313 cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
317 return type->contained();
320 if (!expression->isNameProxy()) {
321 cc_.reportError(expression->pos(), Message_ExpectedLValue);
325 NameProxy *proxy = expression->toNameProxy();
326 if (!proxy->binding()->kind() == BoundName::VARIABLE) {
327 cc_.reportError(expression->pos(), Message_ExpectedLValue);
331 Variable *var = proxy->binding()->toVariable();
335 if (var->type()->isReference()) {
336 if (var->type()->isConst()) {
337 cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
340 return var->type()->contained();
345 SemanticAnalysis::ControlStatus
346 SemanticAnalysis::visitForControl(Statement *statement)
348 ControlScope scope(&control_);
349 statement->accept(this);
350 return scope.status();
354 SemanticAnalysis::visit(NameProxy *name)
356 BoundName *binding = name->binding();
358 if (binding->scope()->kind() == Scope::GLOBAL) {
359 if (!function_ || function_->isPublic()) {
360 // Whatever we're referencing must be kept alive no matter what.
361 // As such, we simply mark it used, and don't do anything special.
363 } else if (!binding->used() && binding->kind() == BoundName::FUNCTION) {
364 // Otherwise, we're inside a normal stock or function. Since we
365 // don't really know if we've been used yet, we shouldn't
366 // prematurely references as alive. Instead, we keep a list of
367 // which names have been used, so we can mark them later if needed.
369 // Note that we skip this step if the binding is already in the
371 if (!function_->addUsedGlobal(binding))
376 name->setType(binding->type());
380 SemanticAnalysis::visit(Assignment *node)
382 Local<Type> exprType(zone_, visitForLoad(node->expression()));
386 Local<Type> type(zone_, visitForLValue(node->lvalue()));
390 if (node->token() != TOK_ASSIGN) {
391 if (!typeCheckBinary(node, node->token(), type, exprType))
395 if (!typeCheckAssignment(node, exprType, type, true))
402 SemanticAnalysis::visit(IncDecExpression *node)
404 Local<Type> type(zone_, visitForLValue(node->expression()));
408 assert(!type->isReference());
410 if ((!type->isInt32() || (type->isInt32() && type->tag())) && !type->isFloat()) {
411 unaryTypeError(node, node->token(), type);
419 SemanticAnalysis::visit(UnaryExpression *node)
421 Local<Type> type(zone_, visitForLoad(node->expression()));
425 switch (node->token())
428 if (!canTest(node->expression(), type))
430 type = zone_->types()->getPrimitive(PrimitiveType_Int32);
437 if (!type->isFloat() && (!type->isInt32() || type->tag())) {
438 unaryTypeError(node, node->token(), type);
446 Local<String> tag(zone_, node->tag());
447 Local<Type> to(zone_, zone_->types()->getPrimitiveForTag(tag));
451 if ((!type->isFloat() && !type->isInt32()) ||
452 (!to->isFloat() && !to->isInt32()))
454 unaryTypeError(node, node->token(), type);
463 assert(node->token() == TOK_TILDE);
464 if (!type->isInt32() || type->tag()) {
465 unaryTypeError(node, node->token(), type);
474 SemanticAnalysis::visit(TernaryExpression *node)
476 Local<Type> type(zone_, visitForLoad(node->condition()));
480 if (!canTest(node->condition(), type))
483 Local<Type> left(zone_, visitForLoad(node->left()));
484 Local<Type> right(zone_, visitForLoad(node->right()));
489 binaryTypeError(node, TOK_QMARK, left, right);
495 SemanticAnalysis::visit(BinaryExpression *node)
497 Local<Type> left(zone_, visitForLoad(node->left()));
498 Local<Type> right(zone_, visitForLoad(node->right()));
502 Local<Type> result(zone_, typeCheckBinary(node, node->token(), left, right));
506 node->setType(result);
510 SemanticAnalysis::visit(ReturnStatement *node)
512 // Set this early, so if we error we don't get more errors.
513 control_->returns(Returns_Always);
515 if (!node->expression()) {
516 if (!function_->type()->returnType()->isVoid())
517 cc_.reportError(node->pos(), Message_NotAllPathsReturnValue);
521 Local<Type> type(zone_, visitForLoad(node->expression()));
525 Local<Type> returnType(zone_, function_->type()->returnType());
526 if (!typeCheckAssignment(node, type, returnType, true))
531 SemanticAnalysis::visit(ForStatement *node)
533 LoopScope loop(&loop_);
536 scope_ = node->scope();
538 if (node->initialization())
539 node->initialization()->accept(this);
541 if (node->condition()) {
542 Local<Type> type(zone_, visitForLoad(node->condition()));
543 if (!type || !canTest(node->condition(), type)) {
549 node->update()->accept(this);
551 ControlStatus status = visitForControl(node->body());
553 // If there is a condition, we treat this like a while loop. Otherwise,
554 // join point is only reachable if the loop contains a break. The
555 // termination status doesn't tell us anything yet.
556 if (node->condition() || loop.breaks())
557 control_->merge(status, ControlStatus(false, Returns_Never));
559 control_->terminate(status.returns());
562 scope_ = scope_->enclosing();
566 SemanticAnalysis::visit(WhileStatement *node)
568 LoopScope loop(&loop_);
570 Local<Type> type(zone_, visitForLoad(node->condition()));
571 if (!type || !canTest(node->condition(), type)) {
575 ControlStatus status = visitForControl(node->body());
577 // Detect loops that will always have a fork in the control-flow graph.
578 // This will be a |while| loop, or a |do-while| loop whose body does
581 // For now, we treat these as an |if| regardless of the direction the
582 // branch can take. That means the control-flow is always conditional.
583 if (node->token() == TOK_WHILE ||
584 !status.terminates() ||
587 control_->merge(status, ControlStatus(false, Returns_Never));
589 // At this point, we've got a do-while loop with a |return| or
590 // |continue|, meaning that the rest of the block is unreachable.
591 control_->terminate(status.returns());
596 SemanticAnalysis::visit(BlockStatement *node)
599 scope_ = node->scope();
600 for (size_t i = 0; i < node->statements()->length(); i++) {
601 Statement *statement = node->statements()->at(i);
602 statement->accept(this);
605 scope_ = scope_->enclosing();
609 SemanticAnalysis::visit(VariableDeclaration *node)
611 Local<String> tag(zone_, node->tag());
612 Local<Type> type(zone_, zone_->types()->getPrimitiveForTag(tag));
616 if (node->postDimensions()) {
617 Local<Type> init(zone_);
618 if (node->initialization()) {
619 if ((init = visitForLoad(node->initialization())) == NULL)
623 if (node->postDimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
624 cc_.reportError(node->pos(), Message_MaximumArrayDepth);
628 // If we have an initializer, make sure it's the same type.
629 if (init && node->postDimensions()->length() != init->levels()) {
630 cc_.reportError(node->pos(), Message_ArrayInitializerDepth);
634 Local<Type> check(zone_, init);
635 for (size_t i = node->postDimensions()->length() - 1; i < node->postDimensions()->length(); i--) {
636 Expression *expr = node->postDimensions()->at(i);
640 Local<Type> exprType(zone_, visitForLoad(expr));
644 if (!IsPrimitiveType(exprType, PrimitiveType_Int32)) {
645 cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
650 if (EvaluateForConstant(expr, &box) && box.type() == PrimitiveType_Int32) {
653 cc_.reportError(expr->pos(), Message_BadArraySize);
656 if (size >= int(INT_MAX / sizeof(int))) {
657 cc_.reportError(expr->pos(), Message_ArraySizeTooBig);
662 if (check->elements() == -1) {
663 cc_.reportError(expr->pos(), Message_ArraySizeCannotBeDetermined);
667 if (check->isCharArray()) {
668 if (check->elements() > size) {
669 cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
672 } else if (check->elements() != size) {
673 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, unsigned(i));
679 cc_.reportError(node->pos(), Message_DynamicArrayHasInitializer);
683 // here until we get dynamic array support.
684 cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
688 // Infer the size from the initializer.
690 cc_.reportError(node->pos(), Message_ArraySizeNotSpecified);
693 if (init->elements() == -1) {
694 cc_.reportError(node->pos(), Message_ArraySizeCannotBeDetermined);
697 size = init->elements();
700 type = zone_->types()->unsafeArray(type, size, false);
705 check = check->contained();
708 // If this variable is stack allocated, we could use the LIFO heap
709 // to allocate it. If that's the case, make sure to reserve a slot
710 // so we can unwind the LIFO heap at the end of this scope.
711 if (!node->variable()->needsHeapAllocation())
712 scope_->setUsesLifoHeap();
714 if (node->initialization()) {
715 Local<Type> right(zone_, visitForLoad(node->initialization()));
718 if (!typeCheckAssignment(node, type, right, true))
721 if (type->primitive() == PrimitiveType_Char) {
722 cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
727 node->variable()->setType(type);
731 SemanticAnalysis::visit(IntegerLiteral *node)
733 node->setType(zone_->types()->getPrimitive(PrimitiveType_Int32));
737 SemanticAnalysis::visit(ExpressionStatement *node)
739 visitAndApplyType(node->expression());
743 SemanticAnalysis::preanalyze(FunctionStatement *node)
745 Local<FixedArray> args(zone_, FixedArray::New(zone_, node->parameters()->length(), Heap::Tenure_Old));
749 // Apply types to arguments.
750 for (size_t i = 0; i < node->parameters()->length(); i++) {
751 Parameter *parameter = node->parameters()->at(i);
753 Local<String> tag(zone_, parameter->tag());
754 Local<Type> type(zone_, zone_->types()->getPrimitiveForTag(tag));
758 if (parameter->reference()) {
759 if (parameter->dimensions()) {
760 cc_.reportError(parameter->variable()->pos(), Message_ArrayCantBeReference);
764 type = zone_->types()->getReference(type, parameter->isConst());
767 } else if (parameter->dimensions()) {
768 if (parameter->dimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
769 cc_.reportError(parameter->variable()->pos(), Message_MaximumArrayDepth);
773 if (node->fun()->isNative() && parameter->dimensions()->length() > 1) {
774 cc_.reportError(parameter->variable()->pos(), Message_MultiArrayInNative);
778 for (size_t i = parameter->dimensions()->length() - 1;
779 i < parameter->dimensions()->length();
782 int size = Type::UNKNOWN_SIZE;
783 if (Expression *expr = parameter->dimensions()->at(i)) {
785 if (!EvaluateForConstant(expr, &box)) {
786 cc_.reportError(expr->pos(), Message_ArraySizeMustBeConstant);
792 type = zone_->types()->unsafeArray(type, size, parameter->isConst());
798 if ((type->isPrimitive() && type->primitive() == PrimitiveType_Char) ||
799 (type->isReference() && type->contained()->primitive() == PrimitiveType_Char))
801 cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
805 args->set(zone_, i, type);
806 parameter->variable()->setType(type);
809 Local<Type> returnType(zone_);
810 if (node->returnTag()) {
811 Local<String> tag(zone_, node->returnTag());
812 returnType = zone_->types()->getPrimitiveForTag(tag);
813 } else if (node->hasExplicitReturn() || node->fun()->isNative()) {
814 returnType = zone_->types()->getPrimitive(PrimitiveType_Int32);
816 returnType = zone_->types()->getVoid();
821 Local<Type> funtype(zone_);
822 if (node->fun()->isNative())
823 funtype = zone_->types()->newNative(returnType, args, node->fun()->isNativeVariadic());
825 funtype = zone_->types()->newFunction(returnType, args);
829 // If the function already has a type, it was forward declared before, and
830 // now this is our chance to type check the second declaration.
831 if (node->fun()->type()) {
832 Local<Type> forward(zone_, node->fun()->type());
833 if (!Type::Compare(funtype, forward)) {
834 cc_.reportError(node->pos(), Message_ForwardDeclareMismatches);
837 node->fun()->setForwardType(forward);
840 node->fun()->setType(funtype);
846 SemanticAnalysis::visit(FunctionStatement *node)
848 function_ = node->fun();
849 scope_ = node->scope();
851 ControlScope control(&control_);
853 node->body()->accept(this);
855 ControlStatus status = control.status();
856 if (status.returns() == Returns_Sometimes) {
857 cc_.reportError(node->pos(), Message_NotAllPathsReturnValue);
858 } else if (status.returns() == Returns_Never) {
859 if (!function_->type()->returnType()->isVoid())
860 cc_.reportError(node->pos(), Message_MissingReturnValue);
869 SemanticAnalysis::typeCheckArrayAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
871 Local<Type> actual(zone_, argActual);
872 Local<Type> given(zone_, argGiven);
874 if (!given->isArray()) {
875 cc_.reportError(node->pos(), Message_ExpectedArray);
878 if (given->levels() != actual->levels()) {
879 cc_.reportError(node->pos(), Message_ArrayDimensionsMismatch);
884 while (given->isArray()) {
885 if (actual->elements() != -1) {
887 if (given->elements() == -1 || actual->elements() == -1) {
888 cc_.reportError(node->pos(), Message_ArraySizeNotKnown);
892 if (actual->isCharArray()) {
893 // String assignment can have a truncated |given|.
894 if (given->elements() > actual->elements()) {
895 cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
899 if (given->elements() != actual->elements()) {
900 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
905 if (actual->elements() != -1 && given->elements() != actual->elements()) {
906 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
911 given = given->contained();
912 actual = actual->contained();
915 if (given->primitive() != actual->primitive()) {
916 // :TODO: leaks an interior pointer.
917 const char *givenTag = given->tag() ? given->tag()->chars() : "_";
918 const char *actualTag = given->tag() ? given->tag()->chars() : "_";
919 cc_.reportError(node->pos(), Message_TagMismatch, actualTag, givenTag);
927 SemanticAnalysis::typeCheckAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
929 Local<Type> actual(zone_, argActual);
930 Local<Type> given(zone_, argGiven);
932 if (actual->isArray())
933 return typeCheckArrayAssignment(node, given, actual, forAssign);
935 if (given->isArray()) {
936 cc_.reportError(node->pos(), Message_CannotAssignArrayToScalar);
940 // The given type may not have been dereferenced.
941 if (given->isReference())
942 given = given->contained();
943 if (actual->isReference())
944 actual = actual->contained();
946 if (!given->isPrimitive() || !actual->isPrimitive()) {
947 cc_.reportError(node->pos(), Message_UnsupportedType);
951 if (given->primitive() != actual->primitive()) {
952 // :TODO: leaks an interior pointer
953 const char *givenTag = given->tag() ? given->tag()->chars() : "_";
954 const char *actualTag = actual->tag() ? actual->tag()->chars() : "_";
955 cc_.reportError(node->pos(), Message_TagMismatch, actualTag, givenTag);
963 SemanticAnalysis::typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual)
965 return typeCheckAssignment(node, given, actual, false);
969 SemanticAnalysis::visit(CallExpression *node)
971 Local<Type> type(zone_, visitForLoad(node->callee()));
975 if (!type->isFunction()) {
976 cc_.reportError(node->pos(), Message_CalleeNotFunction);
979 if (node->arguments()->length() != type->parameters()->length()) {
980 if (!type->isNative() ||
981 !type->isNativeVariadic() ||
982 node->arguments()->length() < type->parameters()->length())
984 cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
989 // Currently, only calls from a NameProxy are possible.
990 NamedFunction *fun = node->callee()->toNameProxy()->binding()->toNamedFunction();
991 if (fun->isForward()) {
992 cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
996 for (unsigned i = 0; i < node->arguments()->length(); i++) {
997 // Note: we don't compute references unless the callee needs a
999 Expression *expression = node->arguments()->at(i);
1000 Local<Type> given(zone_, visitAndApplyType(expression));
1004 // Since we skip a load, check for a call expression that returns void.
1005 if (given->isVoid()) {
1006 assert(expression->isCallExpression());
1007 CallExpression *call = expression->toCallExpression();
1008 cc_.reportError(call->callee()->pos(), Message_FunctionDoesNotReturnValue);
1012 // If it's an IndexExpression, we special case the ability to compute
1014 if (expression->isIndexExpression())
1015 given = given->contained();
1017 bool mustComputeReference;
1018 if (i >= type->parameters()->length()) {
1019 // Special handling for natives. Arguments past the formal must
1020 // always have references computed. However, arrays are already
1021 // references for natives.
1022 assert(type->isNative() && type->isNativeVariadic());
1023 mustComputeReference = !given->isReference() && !given->isArray();
1025 Local<Type> actual(zone_, type->parameterAt(i));
1026 if (!typeCheckParameter(expression, given, actual))
1028 mustComputeReference = actual->isReference() && !given->isReference();
1031 // If the actual is a reference, and the given is not, we'll need to
1032 // compute a reference, which may be allocated on the stack.
1033 if (mustComputeReference)
1034 scope_->setUsesLifoHeap();
1037 node->setType(fun->type()->returnType());
1041 SemanticAnalysis::ControlScope::merge(const ControlStatus &left, const ControlStatus &right)
1043 // If both sides terminate in a return, we consider this a return as well.
1044 if (left.returns() == Returns_Always && right.returns() == Returns_Always) {
1045 terminate(Returns_Always);
1049 ReturnStatus status = (left.returns() == Returns_Never && right.returns() == Returns_Never)
1051 : Returns_Sometimes;
1053 if (left.terminates() && right.terminates())
1060 SemanticAnalysis::visit(IfStatement *node)
1062 Local<Type> type(zone_, visitForLoad(node->condition()));
1066 if (!canTest(node, type))
1069 ControlStatus trueBranch = visitForControl(node->ifTrue());
1070 ControlStatus falseBranch = ControlStatus(false, Returns_Never);
1072 if (node->ifFalse())
1073 falseBranch = visitForControl(node->ifFalse());
1075 control_->merge(trueBranch, falseBranch);
1079 SemanticAnalysis::visit(IndexExpression *node)
1081 Local<Type> left(zone_, visitForLoad(node->left()));
1082 Local<Type> right(zone_, visitForLoad(node->right()));
1084 if (!left || !right)
1087 if (!right->isInt32()) {
1088 cc_.reportError(node->right()->pos(), Message_IndexMustBeInteger);
1091 if (!left->isArray()) {
1092 cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
1095 node->setType(left);
1099 SemanticAnalysis::visit(EnumStatement *node)
1101 // Forbid the blessed types.
1102 Local<String> tag(zone_, node->name());
1103 if (!strcmp(tag->chars(), "Float") ||
1104 !strcmp(tag->chars(), "String") ||
1105 !strcmp(tag->chars(), "any") ||
1106 !strcmp(tag->chars(), "bool"))
1108 cc_.reportError(node->pos(), Message_InvalidEnumName);
1112 Local<Type> type(zone_, zone_->types()->getPrimitiveForTag(tag));
1117 for (size_t i = 0; i < node->entries()->length(); i++) {
1118 EnumStatement::Entry &entry = node->entries()->at(i);
1120 BoxedPrimitive box = BoxedPrimitive::Int(value);
1122 entry.expr->accept(this);
1123 if (!EvaluateForConstant(entry.expr, &box)) {
1124 cc_.reportError(entry.expr->pos(), Message_EnumFieldNotConstant);
1128 assert(box.type() == PrimitiveType_Int32);
1129 value = box.toInt();
1132 entry.name->set(type, box);
1138 SemanticAnalysis::visit(StringLiteral *node)
1140 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Char));
1144 type = Type::NewArray(zone_, type, node->string()->length() + 1, true);
1148 node->setType(type);
1152 SemanticAnalysis::visit(FloatLiteral *node)
1154 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
1158 node->setType(type);
1162 SemanticAnalysis::visit(BreakStatement *node)
1165 cc_.reportError(node->pos(), Message_BreakOutsideLoop);
1169 // Note: ignore breaks that occur in unreachable control-flow.
1170 if (!control_->status().terminates())
1173 control_->terminate(Returns_Never);
1177 SemanticAnalysis::visit(ContinueStatement *node)
1180 cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
1184 // Note: ignore continues that occur in unreachable control-flow.
1185 if (!control_->status().terminates())
1186 loop_->addContinue();
1188 control_->terminate(Returns_Never);