• Tobias Tebbi's avatar
    [torque] add typeswitch statement · 91ef86f9
    Tobias Tebbi authored
    This adds a typeswitch statement
    
    typeswitch (e)
    case (x1 : Type1) {
      ...
    } case (x2 : Type2) {
    
    } ...
    ... case (xn : TypeN) {
      ...
    }
    
    This checks to which of the given types the result of evaluating e can
    be cast, in the order in which they are listed. So if an earlier
    type matches, a value of this type won't reach a later case.
    
    The type-checks are performed by calling the cast<T>() macro.
    The type of the argument passed to the cast macro is dependent on the
    case and excludes all types checked earlier. For example, in
    
    const x : Object = ...
    typeswitch (x)
    case (x : Smi) {
      ...
    } case (x : HeapNumber) {
      ...
    } case (x : HeapObject) {
      ...
    }
    
    there will be calls to cast<Smi>(Object) and
    cast<HeapNumber>(HeapObject), because after the Smi check we know that
    x has to be a HeapObject. With the refactored base.tq definition of
    cast, this will generate efficient code and avoid repeating the Smi
    check in the second case.
    
    The type system ensures that all cases are reachable and that the type
    given to the last case is safe without a runtime check (in other words,
    the union of all checked types covers the type of e).
    
    The cases can also be written as
    case (Type) { ... }
    , in which case the switched value is not re-bound with the checked
    type.
    
    Bug: v8:7793
    Change-Id: Iea4aed7465d62b445e3ae0d33f52921912e095e3
    Reviewed-on: https://chromium-review.googlesource.com/1156506
    Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
    Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#54958}
    91ef86f9
declaration-visitor.h 5.9 KB