Structure declaration support.
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);
162 } else if (statement->isStructureStatement()) {
163 StructureStatement *node = statement->toStructureStatement();
165 if (!node->decl()->type())
170 for (size_t i = 0; i < tree_->statements()->length(); i++) {
171 Statement *statement = tree_->statements()->at(i);
172 statement->accept(this);
175 if (!determineLiveFunctions())
178 return !cc_.failures();
182 SemanticAnalysis::determineLiveFunctions()
184 Vector<NamedFunction *> worklist;
187 for (size_t i = 0; i < tree_->statements()->length(); i++) {
188 Statement *statement = tree_->statements()->at(i);
189 if (!statement->isFunctionStatement())
192 FunctionStatement *stmt = statement->toFunctionStatement();
193 NamedFunction *fun = stmt->fun();
197 for (size_t i = 0; i < fun->usedGlobals()->length(); i++) {
198 NamedFunction *name = fun->usedGlobals()->at(i)->toNamedFunction();
200 // Names in these lists are always unused, however, we mark them
201 // as used while inserting into the work queue, to avoid double-
206 if (!worklist.append(name))
213 while (!worklist.empty()) {
214 NamedFunction *fun = worklist.popCopy();
216 for (size_t i = 0; i < fun->usedGlobals()->length(); i++) {
217 NamedFunction *name = fun->usedGlobals()->at(i)->toNamedFunction();
220 if (!worklist.append(name))
230 SemanticAnalysis::bindType(const SourcePosition &pos, BoundName *name)
233 return zone_->types()->getPrimitive(PrimitiveType_Int32);
235 if (name->kind() == BoundName::UNBOUND) {
236 cc_.reportError(pos, Message_TypeNotFound, name->name()->chars());
240 if (name->kind() != BoundName::TYPE) {
241 cc_.reportError(pos, Message_IdentifierIsNotAType, name->name()->chars());
252 SemanticAnalysis::visitAndApplyType(Expression *expression)
254 expression->accept(this);
255 return expression->type();
259 SemanticAnalysis::visitForLoad(Expression *expression)
261 Local<Type> type(zone_, visitAndApplyType(expression));
265 if (type->isReference()) {
266 expression->setType(type->contained());
267 } else if (expression->isIndexExpression()) {
269 expression->setType(type->contained());
270 } else if (expression->isCallExpression()) {
271 if (type->isVoid()) {
272 CallExpression *call = expression->toCallExpression();
273 cc_.reportError(call->callee()->pos(), Message_FunctionDoesNotReturnValue);
278 return expression->type();
281 // :TODO: this leaks an interior pointer.
283 ke::GetTypeName(Type *type)
287 if (type->isFunction())
291 if (type->isEnum() || type->isStruct())
292 return type->name()->chars();
293 if (type->isReference())
294 type = type->contained();
295 switch (type->primitive()) {
296 case PrimitiveType_Bool:
298 case PrimitiveType_Char:
300 case PrimitiveType_Int32:
302 case PrimitiveType_Float:
310 SemanticAnalysis::unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type)
312 const char *op = TokenNames[token];
313 const char *name = GetTypeName(type);
315 cc_.reportError(node->pos(), Message_UnaryTypeError, op, name);
319 SemanticAnalysis::binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
321 const char *op = TokenNames[token];
322 const char *leftName = GetTypeName(left);
323 const char *rightName = GetTypeName(right);
325 cc_.reportError(node->pos(), Message_BinaryTypeError, op, leftName, rightName);
329 SemanticAnalysis::typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right)
331 if ((token >= TOK_PLUS && token <= TOK_SLASH) ||
332 (token >= TOK_ASSIGN_ADD && token <= TOK_ASSIGN_DIV))
334 // If either side is a float, the other side must be a float or int.
335 if (left->isFloat() || right->isFloat()) {
336 Type *floatType = left->isFloat() ? left : right;
337 Type *other = left->isFloat() ? right : left;
338 if (other->isFloat() || other->isInt32())
343 if (token == TOK_OR || token == TOK_AND) {
344 if (!canTest(node, left) || !canTest(node, right))
346 return zone_->types()->getPrimitive(PrimitiveType_Bool);
349 if (token >= TOK_EQUALS && token <= TOK_GE) {
350 if ((left->isFloat() || left->isInt32()) &&
351 (right->isFloat() || right->isInt32()))
353 return zone_->types()->getPrimitive(PrimitiveType_Bool);
355 binaryTypeError(node, token, left, right);
359 if (!left->isInt32() || !right->isInt32()) {
360 binaryTypeError(node, token, left, right);
368 SemanticAnalysis::canTest(AstNode *node, Handle<Type> type)
370 if (!type->isPrimitive() && !type->isEnum()) {
371 cc_.reportError(node->pos(), Message_ArrayIsNotBoolean);
378 SemanticAnalysis::visitForLValue(Expression *expression)
380 if (expression->isIndexExpression()) {
381 Type *type = visitAndApplyType(expression);
385 if (type->isConst()) {
386 cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
390 return type->contained();
393 if (!expression->isNameProxy()) {
394 cc_.reportError(expression->pos(), Message_ExpectedLValue);
398 NameProxy *proxy = expression->toNameProxy();
399 if (!proxy->binding()->kind() == BoundName::VARIABLE) {
400 cc_.reportError(expression->pos(), Message_ExpectedLValue);
404 Variable *var = proxy->binding()->toVariable();
408 if (var->type()->isReference()) {
409 if (var->type()->isConst()) {
410 cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
413 return var->type()->contained();
418 SemanticAnalysis::ControlStatus
419 SemanticAnalysis::visitForControl(Statement *statement)
421 ControlScope scope(&control_);
422 statement->accept(this);
423 return scope.status();
427 SemanticAnalysis::visit(NameProxy *name)
429 BoundName *binding = name->binding();
431 if (binding->scope()->kind() == Scope::GLOBAL) {
432 if (!function_ || function_->isPublic()) {
433 // Whatever we're referencing must be kept alive no matter what.
434 // As such, we simply mark it used, and don't do anything special.
436 } else if (!binding->used() && binding->kind() == BoundName::FUNCTION) {
437 // Otherwise, we're inside a normal stock or function. Since we
438 // don't really know if we've been used yet, we shouldn't
439 // prematurely references as alive. Instead, we keep a list of
440 // which names have been used, so we can mark them later if needed.
442 // Note that we skip this step if the binding is already in the
444 if (!function_->addUsedGlobal(binding))
449 name->setType(binding->type());
453 SemanticAnalysis::visit(Assignment *node)
455 Local<Type> exprType(zone_, visitForLoad(node->expression()));
459 Local<Type> type(zone_, visitForLValue(node->lvalue()));
463 if (node->token() != TOK_ASSIGN) {
464 if (!typeCheckBinary(node, node->token(), type, exprType))
468 if (!typeCheckAssignment(node, exprType, type, true))
475 SemanticAnalysis::visit(IncDecExpression *node)
477 Local<Type> type(zone_, visitForLValue(node->expression()));
481 assert(!type->isReference());
483 if (!type->isInt32() && !type->isFloat()) {
484 unaryTypeError(node, node->token(), type);
492 SemanticAnalysis::visit(UnaryExpression *node)
494 Local<Type> type(zone_, visitForLoad(node->expression()));
498 switch (node->token())
501 if (!canTest(node->expression(), type))
503 type = zone_->types()->getPrimitive(PrimitiveType_Int32);
510 if (!type->isFloat() && !type->isInt32()) {
511 unaryTypeError(node, node->token(), type);
519 Local<Type> to(zone_, bindType(node->pos(), node->type()));
523 if ((!type->isFloat() && !type->isInt32OrEnum()) ||
524 (!to->isFloat() && !to->isInt32OrEnum()))
526 unaryTypeError(node, node->token(), type);
535 assert(node->token() == TOK_TILDE);
536 if (!type->isInt32()) {
537 unaryTypeError(node, node->token(), type);
546 SemanticAnalysis::visit(TernaryExpression *node)
548 Local<Type> type(zone_, visitForLoad(node->condition()));
552 if (!canTest(node->condition(), type))
555 Local<Type> left(zone_, visitForLoad(node->left()));
556 Local<Type> right(zone_, visitForLoad(node->right()));
561 binaryTypeError(node, TOK_QMARK, left, right);
567 SemanticAnalysis::visit(BinaryExpression *node)
569 Local<Type> left(zone_, visitForLoad(node->left()));
570 Local<Type> right(zone_, visitForLoad(node->right()));
574 Local<Type> result(zone_, typeCheckBinary(node, node->token(), left, right));
578 node->setType(result);
582 SemanticAnalysis::visit(ReturnStatement *node)
584 // Set this early, so if we error we don't get more errors.
585 control_->returns(Returns_Always);
587 if (!node->expression()) {
588 if (!function_->type()->returnType()->isVoid())
589 cc_.reportError(node->pos(), Message_NotAllPathsReturnValue);
593 Local<Type> type(zone_, visitForLoad(node->expression()));
597 Local<Type> returnType(zone_, function_->type()->returnType());
598 if (!typeCheckAssignment(node, type, returnType, true))
603 SemanticAnalysis::visit(ForStatement *node)
605 LoopScope loop(&loop_);
608 scope_ = node->scope();
610 if (node->initialization())
611 node->initialization()->accept(this);
613 if (node->condition()) {
614 Local<Type> type(zone_, visitForLoad(node->condition()));
615 if (!type || !canTest(node->condition(), type)) {
621 node->update()->accept(this);
623 ControlStatus status = visitForControl(node->body());
625 // If there is a condition, we treat this like a while loop. Otherwise,
626 // join point is only reachable if the loop contains a break. The
627 // termination status doesn't tell us anything yet.
628 if (node->condition() || loop.breaks())
629 control_->merge(status, ControlStatus(false, Returns_Never));
631 control_->terminate(status.returns());
634 scope_ = scope_->enclosing();
638 SemanticAnalysis::visit(WhileStatement *node)
640 LoopScope loop(&loop_);
642 Local<Type> type(zone_, visitForLoad(node->condition()));
643 if (!type || !canTest(node->condition(), type)) {
647 ControlStatus status = visitForControl(node->body());
649 // Detect loops that will always have a fork in the control-flow graph.
650 // This will be a |while| loop, or a |do-while| loop whose body does
653 // For now, we treat these as an |if| regardless of the direction the
654 // branch can take. That means the control-flow is always conditional.
655 if (node->token() == TOK_WHILE ||
656 !status.terminates() ||
659 control_->merge(status, ControlStatus(false, Returns_Never));
661 // At this point, we've got a do-while loop with a |return| or
662 // |continue|, meaning that the rest of the block is unreachable.
663 control_->terminate(status.returns());
668 SemanticAnalysis::visit(BlockStatement *node)
671 scope_ = node->scope();
672 for (size_t i = 0; i < node->statements()->length(); i++) {
673 Statement *statement = node->statements()->at(i);
674 statement->accept(this);
677 scope_ = scope_->enclosing();
681 IsScalarString(Type *type)
683 if (type->isPrimitive() && type->primitive() == PrimitiveType_Char)
685 if (type->isReference())
686 return IsScalarString(type->contained());
691 SemanticAnalysis::visit(VariableDeclaration *node)
693 Local<Type> type(zone_, bindType(node->pos(), node->type()));
697 if (node->postDimensions()) {
698 Local<Type> init(zone_);
699 if (node->initialization()) {
700 if ((init = visitForLoad(node->initialization())) == NULL)
704 if (node->postDimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
705 cc_.reportError(node->pos(), Message_MaximumArrayDepth);
709 // If we have an initializer, make sure it's the same type.
710 if (init && node->postDimensions()->length() != init->levels()) {
711 cc_.reportError(node->pos(), Message_ArrayInitializerDepth);
715 Local<Type> check(zone_, init);
716 for (size_t i = node->postDimensions()->length() - 1; i < node->postDimensions()->length(); i--) {
717 Expression *expr = node->postDimensions()->at(i);
721 Local<Type> exprType(zone_, visitForLoad(expr));
725 if (!IsPrimitiveType(exprType, PrimitiveType_Int32)) {
726 cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
731 if (EvaluateForConstant(expr, &box) && box.type() == PrimitiveType_Int32) {
734 cc_.reportError(expr->pos(), Message_BadArraySize);
737 if (size >= int(INT_MAX / sizeof(int))) {
738 cc_.reportError(expr->pos(), Message_ArraySizeTooBig);
743 if (check->elements() == -1) {
744 cc_.reportError(expr->pos(), Message_ArraySizeCannotBeDetermined);
748 if (check->isCharArray()) {
749 if (check->elements() > size) {
750 cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
753 } else if (check->elements() != size) {
754 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, unsigned(i));
760 cc_.reportError(node->pos(), Message_DynamicArrayHasInitializer);
764 // here until we get dynamic array support.
765 cc_.reportError(expr->pos(), Message_ArrayDimensionNotInteger);
769 // Infer the size from the initializer.
771 cc_.reportError(node->pos(), Message_ArraySizeNotSpecified);
774 if (init->elements() == -1) {
775 cc_.reportError(node->pos(), Message_ArraySizeCannotBeDetermined);
778 size = init->elements();
781 type = zone_->types()->unsafeArray(type, size, false);
786 check = check->contained();
789 if (node->initialization()) {
790 Local<Type> right(zone_, visitForLoad(node->initialization()));
793 if (!typeCheckAssignment(node, type, right, true))
797 if (type->isPrimitive() && type->primitive() == PrimitiveType_Char) {
798 cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
803 // If this variable is stack allocated, we could use the LIFO heap
804 // to allocate it. If that's the case, make sure to reserve a slot
805 // so we can unwind the LIFO heap at the end of this scope.
806 if (!node->variable()->needsHeapAllocation()) {
807 if (type->isArray() || type->isStruct())
808 scope_->setUsesLifoHeap();
811 node->variable()->setType(type);
815 SemanticAnalysis::visit(IntegerLiteral *node)
817 node->setType(zone_->types()->getPrimitive(PrimitiveType_Int32));
821 SemanticAnalysis::visit(BooleanLiteral *node)
823 node->setType(zone_->types()->getPrimitive(PrimitiveType_Bool));
827 SemanticAnalysis::visit(ExpressionStatement *node)
829 visitAndApplyType(node->expression());
833 SemanticAnalysis::preanalyze(FunctionStatement *node)
835 Local<FixedArray> args(zone_, FixedArray::New(zone_, node->parameters()->length(), Heap::Tenure_Old));
839 // Apply types to arguments.
840 for (size_t i = 0; i < node->parameters()->length(); i++) {
841 Parameter *parameter = node->parameters()->at(i);
843 Local<Type> type(zone_, bindType(parameter->variable()->pos(), parameter->type()));
847 if (parameter->reference()) {
848 if (parameter->dimensions()) {
849 cc_.reportError(parameter->variable()->pos(), Message_ArrayCantBeReference);
853 type = zone_->types()->getReference(type, parameter->isConst());
856 } else if (parameter->dimensions()) {
857 if (parameter->dimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
858 cc_.reportError(parameter->variable()->pos(), Message_MaximumArrayDepth);
862 if (node->fun()->isNative() && parameter->dimensions()->length() > 1) {
863 cc_.reportError(parameter->variable()->pos(), Message_MultiArrayInNative);
867 for (size_t i = parameter->dimensions()->length() - 1;
868 i < parameter->dimensions()->length();
871 int size = Type::UNKNOWN_SIZE;
872 if (Expression *expr = parameter->dimensions()->at(i)) {
874 if (!EvaluateForConstant(expr, &box)) {
875 cc_.reportError(expr->pos(), Message_ArraySizeMustBeConstant);
881 type = zone_->types()->unsafeArray(type, size, parameter->isConst());
887 if (IsScalarString(type)) {
888 cc_.reportError(node->pos(), Message_CannotDeclareStringCell);
892 args->set(zone_, i, type);
893 parameter->variable()->setType(type);
896 Local<Type> returnType(zone_);
897 if (node->returnType() ||
898 node->hasExplicitReturn() ||
899 node->fun()->isNative())
901 returnType = bindType(node->pos(), node->returnType());
903 returnType = zone_->types()->getVoid();
908 Local<Type> funtype(zone_);
909 if (node->fun()->isNative())
910 funtype = zone_->types()->newNative(returnType, args, node->fun()->isNativeVariadic());
912 funtype = zone_->types()->newFunction(returnType, args);
916 // If the function already has a type, it was forward declared before, and
917 // now this is our chance to type check the second declaration.
918 if (node->fun()->type()) {
919 Local<Type> forward(zone_, node->fun()->type());
920 if (!Type::Compare(funtype, forward)) {
921 cc_.reportError(node->pos(), Message_ForwardDeclareMismatches);
924 node->fun()->setForwardType(forward);
927 node->fun()->setType(funtype);
933 SemanticAnalysis::visit(FunctionStatement *node)
935 function_ = node->fun();
936 scope_ = node->scope();
938 ControlScope control(&control_);
940 node->body()->accept(this);
942 ControlStatus status = control.status();
943 if (status.returns() == Returns_Sometimes) {
944 cc_.reportError(node->pos(), Message_NotAllPathsReturnValue);
945 } else if (status.returns() == Returns_Never) {
946 if (!function_->type()->returnType()->isVoid())
947 cc_.reportError(node->pos(), Message_MissingReturnValue);
956 SemanticAnalysis::typeMismatch(AstNode *node, Handle<Type> actual, Handle<Type> given)
958 cc_.reportError(node->pos(),Message_TypeMismatch,
964 SemanticAnalysis::typeCheckArrayAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
966 Local<Type> actual(zone_, argActual);
967 Local<Type> given(zone_, argGiven);
969 if (!given->isArray()) {
970 cc_.reportError(node->pos(), Message_ExpectedArray);
973 if (given->levels() != actual->levels()) {
974 cc_.reportError(node->pos(), Message_ArrayDimensionsMismatch);
979 while (given->isArray()) {
980 if (actual->elements() != -1) {
982 if (given->elements() == -1 || actual->elements() == -1) {
983 cc_.reportError(node->pos(), Message_ArraySizeNotKnown);
987 if (actual->isCharArray()) {
988 // String assignment can have a truncated |given|.
989 if (given->elements() > actual->elements()) {
990 cc_.reportError(node->pos(), Message_ArrayAssignmentTooBig);
994 if (given->elements() != actual->elements()) {
995 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
1000 if (actual->elements() != -1 && given->elements() != actual->elements()) {
1001 cc_.reportError(node->pos(), Message_ArrayDimensionSizeMismatch, level);
1006 given = given->contained();
1007 actual = actual->contained();
1010 return typeCheckAssignment(node, given, actual, forAssign);
1014 SemanticAnalysis::typeCheckAssignment(AstNode *node, Handle<Type> argGiven, Handle<Type> argActual, bool forAssign)
1016 Local<Type> actual(zone_, argActual);
1017 Local<Type> given(zone_, argGiven);
1019 if (actual->isArray())
1020 return typeCheckArrayAssignment(node, given, actual, forAssign);
1022 if (given->isArray()) {
1023 cc_.reportError(node->pos(), Message_CannotAssignArrayToScalar);
1027 // The given type may not have been dereferenced.
1028 if (given->isReference())
1029 given = given->contained();
1030 if (actual->isReference())
1031 actual = actual->contained();
1033 if (actual->isEnum()) {
1034 if (!given->isEnum() || actual != given) {
1035 typeMismatch(node, actual, given);
1041 if (given->isEnum() || given->isFunction()) {
1042 typeMismatch(node, actual, given);
1046 if (given->primitive() != actual->primitive()) {
1047 typeMismatch(node, actual, given);
1055 SemanticAnalysis::typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual)
1057 return typeCheckAssignment(node, given, actual, false);
1061 SemanticAnalysis::visit(CallExpression *node)
1063 Local<Type> type(zone_, visitForLoad(node->callee()));
1067 if (!type->isFunction()) {
1068 cc_.reportError(node->pos(), Message_CalleeNotFunction);
1071 if (node->arguments()->length() != type->parameters()->length()) {
1072 if (!type->isNative() ||
1073 !type->isNativeVariadic() ||
1074 node->arguments()->length() < type->parameters()->length())
1076 cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
1081 // Currently, only calls from a NameProxy are possible.
1082 NamedFunction *fun = node->callee()->toNameProxy()->binding()->toNamedFunction();
1083 if (fun->isForward()) {
1084 cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
1088 for (unsigned i = 0; i < node->arguments()->length(); i++) {
1089 // Note: we don't compute references unless the callee needs a
1091 Expression *expression = node->arguments()->at(i);
1092 Local<Type> given(zone_, visitAndApplyType(expression));
1096 // Since we skip a load, check for a call expression that returns void.
1097 if (given->isVoid()) {
1098 assert(expression->isCallExpression());
1099 CallExpression *call = expression->toCallExpression();
1100 cc_.reportError(call->callee()->pos(), Message_FunctionDoesNotReturnValue);
1104 // If it's an IndexExpression, we special case the ability to compute
1106 if (expression->isIndexExpression())
1107 given = given->contained();
1109 bool mustComputeReference;
1110 if (i >= type->parameters()->length()) {
1111 // Special handling for natives. Arguments past the formal must
1112 // always have references computed. However, arrays are already
1113 // references for natives.
1114 assert(type->isNative() && type->isNativeVariadic());
1115 mustComputeReference = !given->isReference() && !given->isArray();
1117 Local<Type> actual(zone_, type->parameterAt(i));
1118 if (!typeCheckParameter(expression, given, actual))
1120 mustComputeReference = actual->isReference() && !given->isReference();
1123 // If the actual is a reference, and the given is not, we'll need to
1124 // compute a reference, which may be allocated on the stack.
1125 if (mustComputeReference)
1126 scope_->setUsesLifoHeap();
1129 node->setType(fun->type()->returnType());
1133 SemanticAnalysis::ControlScope::merge(const ControlStatus &left, const ControlStatus &right)
1135 // If both sides terminate in a return, we consider this a return as well.
1136 if (left.returns() == Returns_Always && right.returns() == Returns_Always) {
1137 terminate(Returns_Always);
1141 ReturnStatus status = (left.returns() == Returns_Never && right.returns() == Returns_Never)
1143 : Returns_Sometimes;
1145 if (left.terminates() && right.terminates())
1152 SemanticAnalysis::visit(IfStatement *node)
1154 Local<Type> type(zone_, visitForLoad(node->condition()));
1158 if (!canTest(node, type))
1161 ControlStatus trueBranch = visitForControl(node->ifTrue());
1162 ControlStatus falseBranch = ControlStatus(false, Returns_Never);
1164 if (node->ifFalse())
1165 falseBranch = visitForControl(node->ifFalse());
1167 control_->merge(trueBranch, falseBranch);
1171 SemanticAnalysis::visit(IndexExpression *node)
1173 Local<Type> left(zone_, visitForLoad(node->left()));
1174 Local<Type> right(zone_, visitForLoad(node->right()));
1176 if (!left || !right)
1179 if (!right->isInt32() && !right->isEnum()) {
1180 cc_.reportError(node->right()->pos(), Message_IndexMustBeInteger);
1183 if (!left->isArray()) {
1184 cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
1187 node->setType(left);
1191 SemanticAnalysis::visit(EnumStatement *node)
1193 Local<Type> type(zone_, node->decl()->type());
1198 for (size_t i = 0; i < node->entries()->length(); i++) {
1199 EnumStatement::Entry &entry = node->entries()->at(i);
1201 BoxedPrimitive box = BoxedPrimitive::Int(value);
1203 entry.expr->accept(this);
1204 if (!EvaluateForConstant(entry.expr, &box)) {
1205 cc_.reportError(entry.expr->pos(), Message_EnumFieldNotConstant);
1209 assert(box.type() == PrimitiveType_Int32);
1210 value = box.toInt();
1213 entry.name->set(type, box);
1219 SemanticAnalysis::visit(StructureStatement *node)
1221 // If this node already has a type, it was pre-analyzed, so skip it.
1222 if (node->decl()->type())
1225 FieldList *fields = node->fields();
1226 Local<FixedArray> desctable(zone_, FixedArray::New(zone_, fields->length(), Heap::Tenure_Old));
1230 StructureBufferBuilder builder;
1232 Local<Type> type(zone_);
1233 Local<String> name(zone_);
1234 Local<Descriptor> desc(zone_);
1235 for (size_t i = 0; i < fields->length(); i++) {
1236 StructureStatement::Field *field = fields->at(i);
1238 type = bindType(field->pos(), field->type());
1243 if (!builder.add(type, &offset))
1246 name = field->name();
1247 desc = Descriptor::New(zone_, name, type, offset);
1251 desctable->set(zone_, i, desc);
1254 Local<StructMap> map(zone_, StructMap::New(zone_, builder));
1258 name = node->decl()->name();
1259 type = zone_->types()->newStruct(name, desctable, map);
1264 node->decl()->setType(type);
1268 SemanticAnalysis::visit(StringLiteral *node)
1270 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Char));
1274 type = Type::NewArray(zone_, type, node->string()->length() + 1, true);
1278 node->setType(type);
1282 SemanticAnalysis::visit(FloatLiteral *node)
1284 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
1288 node->setType(type);
1292 SemanticAnalysis::visit(BreakStatement *node)
1295 cc_.reportError(node->pos(), Message_BreakOutsideLoop);
1299 // Note: ignore breaks that occur in unreachable control-flow.
1300 if (!control_->status().terminates())
1303 control_->terminate(Returns_Never);
1307 SemanticAnalysis::visit(ContinueStatement *node)
1310 cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
1314 // Note: ignore continues that occur in unreachable control-flow.
1315 if (!control_->status().terminates())
1316 loop_->addContinue();
1318 control_->terminate(Returns_Never);