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/ast/ast-types.h"
8 #include "src/handles-inl.h"
9 #include "src/ostreams.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 // static
None()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
Any()22 FieldType* FieldType::Any() {
23   return reinterpret_cast<FieldType*>(Smi::FromInt(1));
24 }
25 
26 // static
None(Isolate * isolate)27 Handle<FieldType> FieldType::None(Isolate* isolate) {
28   return handle(None(), isolate);
29 }
30 
31 // static
Any(Isolate * isolate)32 Handle<FieldType> FieldType::Any(Isolate* isolate) {
33   return handle(Any(), isolate);
34 }
35 
36 // static
Class(i::Map * map)37 FieldType* FieldType::Class(i::Map* map) { return FieldType::cast(map); }
38 
39 // static
Class(i::Handle<i::Map> map,Isolate * isolate)40 Handle<FieldType> FieldType::Class(i::Handle<i::Map> map, Isolate* isolate) {
41   return handle(Class(*map), isolate);
42 }
43 
44 // static
cast(Object * object)45 FieldType* FieldType::cast(Object* object) {
46   DCHECK(object == None() || object == Any() || object->IsMap());
47   return reinterpret_cast<FieldType*>(object);
48 }
49 
IsClass()50 bool FieldType::IsClass() { return this->IsMap(); }
51 
AsClass()52 Handle<i::Map> FieldType::AsClass() {
53   DCHECK(IsClass());
54   i::Map* map = Map::cast(this);
55   return handle(map, map->GetIsolate());
56 }
57 
NowStable()58 bool FieldType::NowStable() {
59   return !this->IsClass() || this->AsClass()->is_stable();
60 }
61 
NowIs(FieldType * other)62 bool FieldType::NowIs(FieldType* other) {
63   if (other->IsAny()) return true;
64   if (IsNone()) return true;
65   if (other->IsNone()) return false;
66   if (IsAny()) return false;
67   DCHECK(IsClass());
68   DCHECK(other->IsClass());
69   return this == other;
70 }
71 
NowIs(Handle<FieldType> other)72 bool FieldType::NowIs(Handle<FieldType> other) { return NowIs(*other); }
73 
Convert(Zone * zone)74 AstType* FieldType::Convert(Zone* zone) {
75   if (IsAny()) return AstType::NonInternal();
76   if (IsNone()) return AstType::None();
77   DCHECK(IsClass());
78   return AstType::Class(AsClass(), zone);
79 }
80 
PrintTo(std::ostream & os)81 void FieldType::PrintTo(std::ostream& os) {
82   if (IsAny()) {
83     os << "Any";
84   } else if (IsNone()) {
85     os << "None";
86   } else {
87     DCHECK(IsClass());
88     os << "Class(" << static_cast<void*>(*AsClass()) << ")";
89   }
90 }
91 
92 }  // namespace internal
93 }  // namespace v8
94