typing.cc 22.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "typing.h"

#include "parser.h"  // for CompileTimeValue; TODO(rossberg): should move
#include "scopes.h"

namespace v8 {
namespace internal {


AstTyper::AstTyper(CompilationInfo* info)
    : info_(info),
      oracle_(
          Handle<Code>(info->closure()->shared()->code()),
          Handle<Context>(info->closure()->context()->native_context()),
          info->isolate(),
43 44
          info->zone()),
      store_(info->zone()) {
45
  InitializeAstVisitor(info->isolate());
46 47 48
}


49
#define RECURSE(call)                         \
50
  do {                                        \
51
    ASSERT(!visitor->HasStackOverflow());     \
52 53 54 55
    call;                                     \
    if (visitor->HasStackOverflow()) return;  \
  } while (false)

56
void AstTyper::Run(CompilationInfo* info) {
57 58 59 60 61 62
  AstTyper* visitor = new(info->zone()) AstTyper(info);
  Scope* scope = info->scope();

  // Handle implicit declaration of the function name in named function
  // expressions before other declarations.
  if (scope->is_function_scope() && scope->function() != NULL) {
63
    RECURSE(visitor->VisitVariableDeclaration(scope->function()));
64
  }
65 66
  RECURSE(visitor->VisitDeclarations(scope->declarations()));
  RECURSE(visitor->VisitStatements(info->function()->body()));
67 68
}

69
#undef RECURSE
70

71
#define RECURSE(call)                \
72
  do {                               \
73
    ASSERT(!HasStackOverflow());     \
74 75 76 77 78 79 80 81
    call;                            \
    if (HasStackOverflow()) return;  \
  } while (false)


void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) {
  for (int i = 0; i < stmts->length(); ++i) {
    Statement* stmt = stmts->at(i);
82
    RECURSE(Visit(stmt));
83
    if (stmt->IsJump()) break;
84 85 86 87 88
  }
}


void AstTyper::VisitBlock(Block* stmt) {
89
  RECURSE(VisitStatements(stmt->statements()));
90 91 92
  if (stmt->labels() != NULL) {
    store_.Forget();  // Control may transfer here via 'break l'.
  }
93 94 95 96
}


void AstTyper::VisitExpressionStatement(ExpressionStatement* stmt) {
97
  RECURSE(Visit(stmt->expression()));
98 99 100 101 102 103 104 105
}


void AstTyper::VisitEmptyStatement(EmptyStatement* stmt) {
}


void AstTyper::VisitIfStatement(IfStatement* stmt) {
106
  // Collect type feedback.
107 108 109 110
  if (!stmt->condition()->ToBooleanIsTrue() &&
      !stmt->condition()->ToBooleanIsFalse()) {
    stmt->condition()->RecordToBooleanTypeFeedback(oracle());
  }
111 112 113 114 115 116 117 118 119 120

  RECURSE(Visit(stmt->condition()));
  Effects then_effects = EnterEffects();
  RECURSE(Visit(stmt->then_statement()));
  ExitEffects();
  Effects else_effects = EnterEffects();
  RECURSE(Visit(stmt->else_statement()));
  ExitEffects();
  then_effects.Alt(else_effects);
  store_.Seq(then_effects);
121 122 123 124
}


void AstTyper::VisitContinueStatement(ContinueStatement* stmt) {
125
  // TODO(rossberg): is it worth having a non-termination effect?
126 127 128 129
}


void AstTyper::VisitBreakStatement(BreakStatement* stmt) {
130
  // TODO(rossberg): is it worth having a non-termination effect?
131 132 133 134
}


void AstTyper::VisitReturnStatement(ReturnStatement* stmt) {
135
  // Collect type feedback.
136 137
  // TODO(rossberg): we only need this for inlining into test contexts...
  stmt->expression()->RecordToBooleanTypeFeedback(oracle());
138 139 140

  RECURSE(Visit(stmt->expression()));
  // TODO(rossberg): is it worth having a non-termination effect?
141 142 143 144
}


void AstTyper::VisitWithStatement(WithStatement* stmt) {
145 146
  RECURSE(stmt->expression());
  RECURSE(stmt->statement());
147 148 149 150
}


void AstTyper::VisitSwitchStatement(SwitchStatement* stmt) {
151
  RECURSE(Visit(stmt->tag()));
152

153 154
  ZoneList<CaseClause*>* clauses = stmt->cases();
  SwitchStatement::SwitchType switch_type = stmt->switch_type();
155 156 157
  Effects local_effects(zone());
  bool complex_effects = false;  // True for label effects or fall-through.

158 159
  for (int i = 0; i < clauses->length(); ++i) {
    CaseClause* clause = clauses->at(i);
160 161
    Effects clause_effects = EnterEffects();

162 163 164 165 166 167 168 169 170 171
    if (!clause->is_default()) {
      Expression* label = clause->label();
      SwitchStatement::SwitchType label_switch_type =
          label->IsSmiLiteral() ? SwitchStatement::SMI_SWITCH :
          label->IsStringLiteral() ? SwitchStatement::STRING_SWITCH :
              SwitchStatement::GENERIC_SWITCH;
      if (switch_type == SwitchStatement::UNKNOWN_SWITCH)
        switch_type = label_switch_type;
      else if (switch_type != label_switch_type)
        switch_type = SwitchStatement::GENERIC_SWITCH;
172 173 174 175 176 177 178 179 180 181 182 183

      RECURSE(Visit(label));
      if (!clause_effects.IsEmpty()) complex_effects = true;
    }

    ZoneList<Statement*>* stmts = clause->statements();
    RECURSE(VisitStatements(stmts));
    ExitEffects();
    if (stmts->is_empty() || stmts->last()->IsJump()) {
      local_effects.Alt(clause_effects);
    } else {
      complex_effects = true;
184 185
    }
  }
186 187 188 189 190 191 192

  if (complex_effects) {
    store_.Forget();  // Reached this in unknown state.
  } else {
    store_.Seq(local_effects);
  }

193 194 195 196
  if (switch_type == SwitchStatement::UNKNOWN_SWITCH)
    switch_type = SwitchStatement::GENERIC_SWITCH;
  stmt->set_switch_type(switch_type);

197
  // Collect type feedback.
198 199 200 201 202
  // TODO(rossberg): can we eliminate this special case and extra loop?
  if (switch_type == SwitchStatement::SMI_SWITCH) {
    for (int i = 0; i < clauses->length(); ++i) {
      CaseClause* clause = clauses->at(i);
      if (!clause->is_default())
203
        clause->set_compare_type(oracle()->ClauseType(clause->CompareId()));
204 205 206 207 208
    }
  }
}


209 210 211 212 213
void AstTyper::VisitCaseClause(CaseClause* clause) {
  UNREACHABLE();
}


214
void AstTyper::VisitDoWhileStatement(DoWhileStatement* stmt) {
215
  // Collect type feedback.
216 217 218
  if (!stmt->cond()->ToBooleanIsTrue()) {
    stmt->cond()->RecordToBooleanTypeFeedback(oracle());
  }
219 220 221 222 223 224 225 226

  // TODO(rossberg): refine the unconditional Forget (here and elsewhere) by
  // computing the set of variables assigned in only some of the origins of the
  // control transfer (such as the loop body here).
  store_.Forget();  // Control may transfer here via looping or 'continue'.
  RECURSE(Visit(stmt->body()));
  RECURSE(Visit(stmt->cond()));
  store_.Forget();  // Control may transfer here via 'break'.
227 228 229 230
}


void AstTyper::VisitWhileStatement(WhileStatement* stmt) {
231
  // Collect type feedback.
232 233 234
  if (!stmt->cond()->ToBooleanIsTrue()) {
    stmt->cond()->RecordToBooleanTypeFeedback(oracle());
  }
235 236 237 238 239

  store_.Forget();  // Control may transfer here via looping or 'continue'.
  RECURSE(Visit(stmt->cond()));
  RECURSE(Visit(stmt->body()));
  store_.Forget();  // Control may transfer here via termination or 'break'.
240 241 242 243 244
}


void AstTyper::VisitForStatement(ForStatement* stmt) {
  if (stmt->init() != NULL) {
245
    RECURSE(Visit(stmt->init()));
246
  }
247
  store_.Forget();  // Control may transfer here via looping.
248
  if (stmt->cond() != NULL) {
249
    // Collect type feedback.
250
    stmt->cond()->RecordToBooleanTypeFeedback(oracle());
251 252

    RECURSE(Visit(stmt->cond()));
253
  }
254
  RECURSE(Visit(stmt->body()));
255
  if (stmt->next() != NULL) {
256
    store_.Forget();  // Control may transfer here via 'continue'.
257
    RECURSE(Visit(stmt->next()));
258
  }
259
  store_.Forget();  // Control may transfer here via termination or 'break'.
260 261 262 263
}


void AstTyper::VisitForInStatement(ForInStatement* stmt) {
264
  // Collect type feedback.
265 266
  stmt->set_for_in_type(static_cast<ForInStatement::ForInType>(
      oracle()->ForInType(stmt->ForInFeedbackId())));
267

268
  RECURSE(Visit(stmt->enumerable()));
269
  store_.Forget();  // Control may transfer here via looping or 'continue'.
270
  RECURSE(Visit(stmt->body()));
271
  store_.Forget();  // Control may transfer here via 'break'.
272 273 274
}


275
void AstTyper::VisitForOfStatement(ForOfStatement* stmt) {
276
  RECURSE(Visit(stmt->iterable()));
277
  store_.Forget();  // Control may transfer here via looping or 'continue'.
278
  RECURSE(Visit(stmt->body()));
279
  store_.Forget();  // Control may transfer here via 'break'.
280 281 282
}


283
void AstTyper::VisitTryCatchStatement(TryCatchStatement* stmt) {
284
  Effects try_effects = EnterEffects();
285
  RECURSE(Visit(stmt->try_block()));
286 287 288
  ExitEffects();
  Effects catch_effects = EnterEffects();
  store_.Forget();  // Control may transfer here via 'throw'.
289
  RECURSE(Visit(stmt->catch_block()));
290 291 292 293 294
  ExitEffects();
  try_effects.Alt(catch_effects);
  store_.Seq(try_effects);
  // At this point, only variables that were reassigned in the catch block are
  // still remembered.
295 296 297 298
}


void AstTyper::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
299
  RECURSE(Visit(stmt->try_block()));
300
  store_.Forget();  // Control may transfer here via 'throw'.
301
  RECURSE(Visit(stmt->finally_block()));
302 303 304 305
}


void AstTyper::VisitDebuggerStatement(DebuggerStatement* stmt) {
306
  store_.Forget();  // May do whatever.
307 308 309 310 311 312 313
}


void AstTyper::VisitFunctionLiteral(FunctionLiteral* expr) {
}


314
void AstTyper::VisitNativeFunctionLiteral(NativeFunctionLiteral* expr) {
315 316 317 318
}


void AstTyper::VisitConditional(Conditional* expr) {
319 320 321
  // Collect type feedback.
  expr->condition()->RecordToBooleanTypeFeedback(oracle());

322
  RECURSE(Visit(expr->condition()));
323
  Effects then_effects = EnterEffects();
324
  RECURSE(Visit(expr->then_expression()));
325 326
  ExitEffects();
  Effects else_effects = EnterEffects();
327
  RECURSE(Visit(expr->else_expression()));
328 329 330
  ExitEffects();
  then_effects.Alt(else_effects);
  store_.Seq(then_effects);
331

332 333 334
  NarrowType(expr, Bounds::Either(
      expr->then_expression()->bounds(),
      expr->else_expression()->bounds(), isolate_));
335 336 337 338
}


void AstTyper::VisitVariableProxy(VariableProxy* expr) {
339 340 341 342
  Variable* var = expr->var();
  if (var->IsStackAllocated()) {
    NarrowType(expr, store_.LookupBounds(variable_index(var)));
  }
343 344 345 346
}


void AstTyper::VisitLiteral(Literal* expr) {
347
  Type* type = Type::Constant(expr->value(), isolate_);
348
  NarrowType(expr, Bounds(type, isolate_));
349 350 351 352
}


void AstTyper::VisitRegExpLiteral(RegExpLiteral* expr) {
353
  NarrowType(expr, Bounds(Type::RegExp(), isolate_));
354 355 356 357 358 359 360 361
}


void AstTyper::VisitObjectLiteral(ObjectLiteral* expr) {
  ZoneList<ObjectLiteral::Property*>* properties = expr->properties();
  for (int i = 0; i < properties->length(); ++i) {
    ObjectLiteral::Property* prop = properties->at(i);

362
    // Collect type feedback.
363 364 365
    if ((prop->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL &&
        !CompileTimeValue::IsCompileTimeValue(prop->value())) ||
        prop->kind() == ObjectLiteral::Property::COMPUTED) {
366
      if (prop->key()->value()->IsInternalizedString() && prop->emit_store()) {
367
        prop->RecordTypeFeedback(oracle());
368
      }
369
    }
370 371

    RECURSE(Visit(prop->value()));
372
  }
373

374
  NarrowType(expr, Bounds(Type::Object(), isolate_));
375 376 377 378 379 380 381
}


void AstTyper::VisitArrayLiteral(ArrayLiteral* expr) {
  ZoneList<Expression*>* values = expr->values();
  for (int i = 0; i < values->length(); ++i) {
    Expression* value = values->at(i);
382
    RECURSE(Visit(value));
383
  }
384

385
  NarrowType(expr, Bounds(Type::Array(), isolate_));
386 387 388 389
}


void AstTyper::VisitAssignment(Assignment* expr) {
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  // Collect type feedback.
  Property* prop = expr->target()->AsProperty();
  if (prop != NULL) {
    TypeFeedbackId id = expr->AssignmentFeedbackId();
    expr->set_is_uninitialized(oracle()->StoreIsUninitialized(id));
    if (!expr->IsUninitialized()) {
      expr->set_is_pre_monomorphic(oracle()->StoreIsPreMonomorphic(id));
      expr->set_is_monomorphic(oracle()->StoreIsMonomorphicNormal(id));
      ASSERT(!expr->IsPreMonomorphic() || !expr->IsMonomorphic());
      if (prop->key()->IsPropertyName()) {
        Literal* lit_key = prop->key()->AsLiteral();
        ASSERT(lit_key != NULL && lit_key->value()->IsString());
        Handle<String> name = Handle<String>::cast(lit_key->value());
        oracle()->AssignmentReceiverTypes(id, name, expr->GetReceiverTypes());
      } else {
        KeyedAccessStoreMode store_mode;
        oracle()->KeyedAssignmentReceiverTypes(
            id, expr->GetReceiverTypes(), &store_mode);
        expr->set_store_mode(store_mode);
      }
410
    }
411
  }
412

413 414 415 416 417 418
  Expression* rhs =
      expr->is_compound() ? expr->binary_operation() : expr->value();
  RECURSE(Visit(expr->target()));
  RECURSE(Visit(rhs));
  NarrowType(expr, rhs->bounds());

419 420 421 422
  VariableProxy* proxy = expr->target()->AsVariableProxy();
  if (proxy != NULL && proxy->var()->IsStackAllocated()) {
    store_.Seq(variable_index(proxy->var()), Effect(expr->bounds()));
  }
423 424 425 426
}


void AstTyper::VisitYield(Yield* expr) {
427 428 429
  RECURSE(Visit(expr->generator_object()));
  RECURSE(Visit(expr->expression()));

430
  // We don't know anything about the result type.
431 432 433 434
}


void AstTyper::VisitThrow(Throw* expr) {
435
  RECURSE(Visit(expr->exception()));
436
  // TODO(rossberg): is it worth having a non-termination effect?
437

438
  NarrowType(expr, Bounds(Type::None(), isolate_));
439 440 441 442
}


void AstTyper::VisitProperty(Property* expr) {
443
  // Collect type feedback.
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  TypeFeedbackId id = expr->PropertyFeedbackId();
  expr->set_is_uninitialized(oracle()->LoadIsUninitialized(id));
  if (!expr->IsUninitialized()) {
    expr->set_is_pre_monomorphic(oracle()->LoadIsPreMonomorphic(id));
    expr->set_is_monomorphic(oracle()->LoadIsMonomorphicNormal(id));
    ASSERT(!expr->IsPreMonomorphic() || !expr->IsMonomorphic());
    if (expr->key()->IsPropertyName()) {
      Literal* lit_key = expr->key()->AsLiteral();
      ASSERT(lit_key != NULL && lit_key->value()->IsString());
      Handle<String> name = Handle<String>::cast(lit_key->value());
      bool is_prototype;
      oracle()->PropertyReceiverTypes(
          id, name, expr->GetReceiverTypes(), &is_prototype);
      expr->set_is_function_prototype(is_prototype);
    } else {
      bool is_string;
      oracle()->KeyedPropertyReceiverTypes(
          id, expr->GetReceiverTypes(), &is_string);
      expr->set_is_string_access(is_string);
    }
  }
465

466 467
  RECURSE(Visit(expr->obj()));
  RECURSE(Visit(expr->key()));
468

469
  // We don't know anything about the result type.
470 471 472 473
}


void AstTyper::VisitCall(Call* expr) {
474
  // Collect type feedback.
475 476 477
  Expression* callee = expr->expression();
  Property* prop = callee->AsProperty();
  if (prop != NULL) {
478
    expr->RecordTypeFeedback(oracle(), CALL_AS_METHOD);
479 480
  } else {
    expr->RecordTypeFeedback(oracle(), CALL_AS_FUNCTION);
481 482
  }

483 484 485 486 487 488 489 490 491 492 493 494 495
  RECURSE(Visit(expr->expression()));
  ZoneList<Expression*>* args = expr->arguments();
  for (int i = 0; i < args->length(); ++i) {
    Expression* arg = args->at(i);
    RECURSE(Visit(arg));
  }

  VariableProxy* proxy = expr->expression()->AsVariableProxy();
  if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
    store_.Forget();  // Eval could do whatever to local variables.
  }

  // We don't know anything about the result type.
496 497 498 499
}


void AstTyper::VisitCallNew(CallNew* expr) {
500 501 502
  // Collect type feedback.
  expr->RecordTypeFeedback(oracle());

503
  RECURSE(Visit(expr->expression()));
504 505 506
  ZoneList<Expression*>* args = expr->arguments();
  for (int i = 0; i < args->length(); ++i) {
    Expression* arg = args->at(i);
507
    RECURSE(Visit(arg));
508 509
  }

510
  // We don't know anything about the result type.
511 512 513 514 515 516 517
}


void AstTyper::VisitCallRuntime(CallRuntime* expr) {
  ZoneList<Expression*>* args = expr->arguments();
  for (int i = 0; i < args->length(); ++i) {
    Expression* arg = args->at(i);
518
    RECURSE(Visit(arg));
519
  }
520

521
  // We don't know anything about the result type.
522 523 524 525
}


void AstTyper::VisitUnaryOperation(UnaryOperation* expr) {
526
  // Collect type feedback.
527 528 529 530
  if (expr->op() == Token::NOT) {
    // TODO(rossberg): only do in test or value context.
    expr->expression()->RecordToBooleanTypeFeedback(oracle());
  }
531

532 533
  RECURSE(Visit(expr->expression()));

534 535 536
  switch (expr->op()) {
    case Token::NOT:
    case Token::DELETE:
537
      NarrowType(expr, Bounds(Type::Boolean(), isolate_));
538 539
      break;
    case Token::VOID:
540
      NarrowType(expr, Bounds(Type::Undefined(), isolate_));
541 542
      break;
    case Token::TYPEOF:
543
      NarrowType(expr, Bounds(Type::InternalizedString(), isolate_));
544 545 546 547
      break;
    default:
      UNREACHABLE();
  }
548 549 550 551
}


void AstTyper::VisitCountOperation(CountOperation* expr) {
552
  // Collect type feedback.
553 554 555 556 557 558
  TypeFeedbackId store_id = expr->CountStoreFeedbackId();
  expr->set_is_monomorphic(oracle()->StoreIsMonomorphicNormal(store_id));
  expr->set_store_mode(oracle()->GetStoreMode(store_id));
  oracle()->CountReceiverTypes(store_id, expr->GetReceiverTypes());
  expr->set_type(oracle()->CountType(expr->CountBinOpFeedbackId()));
  // TODO(rossberg): merge the count type with the generic expression type.
559

560 561
  RECURSE(Visit(expr->expression()));

562
  NarrowType(expr, Bounds(Type::Smi(), Type::Number(), isolate_));
563 564 565 566 567

  VariableProxy* proxy = expr->expression()->AsVariableProxy();
  if (proxy != NULL && proxy->var()->IsStackAllocated()) {
    store_.Seq(variable_index(proxy->var()), Effect(expr->bounds()));
  }
568 569 570 571
}


void AstTyper::VisitBinaryOperation(BinaryOperation* expr) {
572
  // Collect type feedback.
573
  Handle<Type> type, left_type, right_type;
574
  Maybe<int> fixed_right_arg;
575
  oracle()->BinaryType(expr->BinaryOperationFeedbackId(),
576
      &left_type, &right_type, &type, &fixed_right_arg, expr->op());
577 578 579
  NarrowLowerType(expr, type);
  NarrowLowerType(expr->left(), left_type);
  NarrowLowerType(expr->right(), right_type);
580
  expr->set_fixed_right_arg(fixed_right_arg);
581 582 583
  if (expr->op() == Token::OR || expr->op() == Token::AND) {
    expr->left()->RecordToBooleanTypeFeedback(oracle());
  }
584 585 586

  switch (expr->op()) {
    case Token::COMMA:
587 588
      RECURSE(Visit(expr->left()));
      RECURSE(Visit(expr->right()));
589
      NarrowType(expr, expr->right()->bounds());
590 591
      break;
    case Token::OR:
592 593 594 595 596 597 598 599 600 601
    case Token::AND: {
      Effects left_effects = EnterEffects();
      RECURSE(Visit(expr->left()));
      ExitEffects();
      Effects right_effects = EnterEffects();
      RECURSE(Visit(expr->right()));
      ExitEffects();
      left_effects.Alt(right_effects);
      store_.Seq(left_effects);

602 603
      NarrowType(expr, Bounds::Either(
          expr->left()->bounds(), expr->right()->bounds(), isolate_));
604
      break;
605
    }
606 607
    case Token::BIT_OR:
    case Token::BIT_AND: {
608 609
      RECURSE(Visit(expr->left()));
      RECURSE(Visit(expr->right()));
610 611 612 613 614 615 616 617 618
      Handle<Type> upper(
          Type::Union(
              expr->left()->bounds().upper, expr->right()->bounds().upper),
          isolate_);
      if (!upper->Is(Type::Signed32()))
        upper = handle(Type::Signed32(), isolate_);
      Handle<Type> lower(Type::Intersect(
          handle(Type::Smi(), isolate_), upper), isolate_);
      NarrowType(expr, Bounds(lower, upper));
619 620 621 622 623
      break;
    }
    case Token::BIT_XOR:
    case Token::SHL:
    case Token::SAR:
624 625
      RECURSE(Visit(expr->left()));
      RECURSE(Visit(expr->right()));
626
      NarrowType(expr, Bounds(Type::Smi(), Type::Signed32(), isolate_));
627 628
      break;
    case Token::SHR:
629 630
      RECURSE(Visit(expr->left()));
      RECURSE(Visit(expr->right()));
631 632 633 634
      // TODO(rossberg): The upper bound would be Unsigned32, but since there
      // is no 'positive Smi' type for the lower bound, we use the smallest
      // union of Smi and Unsigned32 as upper bound instead.
      NarrowType(expr, Bounds(Type::Smi(), Type::Number(), isolate_));
635 636
      break;
    case Token::ADD: {
637 638
      RECURSE(Visit(expr->left()));
      RECURSE(Visit(expr->right()));
639 640 641
      Bounds l = expr->left()->bounds();
      Bounds r = expr->right()->bounds();
      Type* lower =
642 643
          l.lower->Is(Type::None()) || r.lower->Is(Type::None()) ?
              Type::None() :
644
          l.lower->Is(Type::String()) || r.lower->Is(Type::String()) ?
645 646 647
              Type::String() :
          l.lower->Is(Type::Number()) && r.lower->Is(Type::Number()) ?
              Type::Smi() : Type::None();
648
      Type* upper =
649
          l.upper->Is(Type::String()) || r.upper->Is(Type::String()) ?
650 651 652
              Type::String() :
          l.upper->Is(Type::Number()) && r.upper->Is(Type::Number()) ?
              Type::Number() : Type::NumberOrString();
653
      NarrowType(expr, Bounds(lower, upper, isolate_));
654 655 656 657 658 659
      break;
    }
    case Token::SUB:
    case Token::MUL:
    case Token::DIV:
    case Token::MOD:
660 661
      RECURSE(Visit(expr->left()));
      RECURSE(Visit(expr->right()));
662
      NarrowType(expr, Bounds(Type::Smi(), Type::Number(), isolate_));
663 664 665 666
      break;
    default:
      UNREACHABLE();
  }
667 668 669 670
}


void AstTyper::VisitCompareOperation(CompareOperation* expr) {
671 672 673 674
  // Collect type feedback.
  Handle<Type> left_type, right_type, combined_type;
  oracle()->CompareType(expr->CompareOperationFeedbackId(),
      &left_type, &right_type, &combined_type);
675 676
  NarrowLowerType(expr->left(), left_type);
  NarrowLowerType(expr->right(), right_type);
677
  expr->set_combined_type(combined_type);
678

679 680 681
  RECURSE(Visit(expr->left()));
  RECURSE(Visit(expr->right()));

682
  NarrowType(expr, Bounds(Type::Boolean(), isolate_));
683 684 685 686 687 688 689 690 691 692
}


void AstTyper::VisitThisFunction(ThisFunction* expr) {
}


void AstTyper::VisitDeclarations(ZoneList<Declaration*>* decls) {
  for (int i = 0; i < decls->length(); ++i) {
    Declaration* decl = decls->at(i);
693
    RECURSE(Visit(decl));
694 695 696 697 698 699 700 701 702
  }
}


void AstTyper::VisitVariableDeclaration(VariableDeclaration* declaration) {
}


void AstTyper::VisitFunctionDeclaration(FunctionDeclaration* declaration) {
703
  RECURSE(Visit(declaration->fun()));
704 705 706 707
}


void AstTyper::VisitModuleDeclaration(ModuleDeclaration* declaration) {
708
  RECURSE(Visit(declaration->module()));
709 710 711 712
}


void AstTyper::VisitImportDeclaration(ImportDeclaration* declaration) {
713
  RECURSE(Visit(declaration->module()));
714 715 716 717 718 719 720 721
}


void AstTyper::VisitExportDeclaration(ExportDeclaration* declaration) {
}


void AstTyper::VisitModuleLiteral(ModuleLiteral* module) {
722
  RECURSE(Visit(module->body()));
723 724 725 726 727 728 729 730
}


void AstTyper::VisitModuleVariable(ModuleVariable* module) {
}


void AstTyper::VisitModulePath(ModulePath* module) {
731
  RECURSE(Visit(module->module()));
732 733 734 735 736 737 738 739
}


void AstTyper::VisitModuleUrl(ModuleUrl* module) {
}


void AstTyper::VisitModuleStatement(ModuleStatement* stmt) {
740
  RECURSE(Visit(stmt->body()));
741 742 743 744
}


} }  // namespace v8::internal