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