Home | History | Annotate | Download | only in src
      1 // Copyright 2016 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/field-type.h"
      6 
      7 #include "src/handles-inl.h"
      8 #include "src/objects-inl.h"
      9 #include "src/ostreams.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 // static
     15 FieldType* FieldType::None() {
     16   // Do not Smi::kZero here or for Any(), as that may translate
     17   // as `nullptr` which is not a valid value for `this`.
     18   return reinterpret_cast<FieldType*>(Smi::FromInt(2));
     19 }
     20 
     21 // static
     22 FieldType* FieldType::Any() {
     23   return reinterpret_cast<FieldType*>(Smi::FromInt(1));
     24 }
     25 
     26 // static
     27 Handle<FieldType> FieldType::None(Isolate* isolate) {
     28   return handle(None(), isolate);
     29 }
     30 
     31 // static
     32 Handle<FieldType> FieldType::Any(Isolate* isolate) {
     33   return handle(Any(), isolate);
     34 }
     35 
     36 // static
     37 FieldType* FieldType::Class(i::Map* map) { return FieldType::cast(map); }
     38 
     39 // static
     40 Handle<FieldType> FieldType::Class(i::Handle<i::Map> map, Isolate* isolate) {
     41   return handle(Class(*map), isolate);
     42 }
     43 
     44 // static
     45 FieldType* FieldType::cast(Object* object) {
     46   DCHECK(object == None() || object == Any() || object->IsMap());
     47   return reinterpret_cast<FieldType*>(object);
     48 }
     49 
     50 bool FieldType::IsClass() { return this->IsMap(); }
     51 
     52 Map* FieldType::AsClass() {
     53   DCHECK(IsClass());
     54   return Map::cast(this);
     55 }
     56 
     57 bool FieldType::NowStable() {
     58   return !this->IsClass() || AsClass()->is_stable();
     59 }
     60 
     61 bool FieldType::NowIs(FieldType* other) {
     62   if (other->IsAny()) return true;
     63   if (IsNone()) return true;
     64   if (other->IsNone()) return false;
     65   if (IsAny()) return false;
     66   DCHECK(IsClass());
     67   DCHECK(other->IsClass());
     68   return this == other;
     69 }
     70 
     71 bool FieldType::NowIs(Handle<FieldType> other) { return NowIs(*other); }
     72 
     73 void FieldType::PrintTo(std::ostream& os) {
     74   if (IsAny()) {
     75     os << "Any";
     76   } else if (IsNone()) {
     77     os << "None";
     78   } else {
     79     DCHECK(IsClass());
     80     os << "Class(" << static_cast<void*>(AsClass()) << ")";
     81   }
     82 }
     83 
     84 bool FieldType::NowContains(Object* value) {
     85   if (this == Any()) return true;
     86   if (this == None()) return false;
     87   if (!value->IsHeapObject()) return false;
     88   return HeapObject::cast(value)->map() == Map::cast(this);
     89 }
     90 
     91 }  // namespace internal
     92 }  // namespace v8
     93