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