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 None()15FieldType* 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 Any()22FieldType* FieldType::Any() { 23 return reinterpret_cast<FieldType*>(Smi::FromInt(1)); 24 } 25 26 // static None(Isolate * isolate)27Handle<FieldType> FieldType::None(Isolate* isolate) { 28 return handle(None(), isolate); 29 } 30 31 // static Any(Isolate * isolate)32Handle<FieldType> FieldType::Any(Isolate* isolate) { 33 return handle(Any(), isolate); 34 } 35 36 // static Class(i::Map * map)37FieldType* FieldType::Class(i::Map* map) { return FieldType::cast(map); } 38 39 // static Class(i::Handle<i::Map> map,Isolate * isolate)40Handle<FieldType> FieldType::Class(i::Handle<i::Map> map, Isolate* isolate) { 41 return handle(Class(*map), isolate); 42 } 43 44 // static cast(Object * object)45FieldType* FieldType::cast(Object* object) { 46 DCHECK(object == None() || object == Any() || object->IsMap()); 47 return reinterpret_cast<FieldType*>(object); 48 } 49 IsClass()50bool FieldType::IsClass() { return this->IsMap(); } 51 AsClass()52Map* FieldType::AsClass() { 53 DCHECK(IsClass()); 54 return Map::cast(this); 55 } 56 NowStable()57bool FieldType::NowStable() { 58 return !this->IsClass() || AsClass()->is_stable(); 59 } 60 NowIs(FieldType * other)61bool 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 NowIs(Handle<FieldType> other)71bool FieldType::NowIs(Handle<FieldType> other) { return NowIs(*other); } 72 PrintTo(std::ostream & os)73void 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 NowContains(Object * value)84bool 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