1// Copyright 2014 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#ifndef HYDROGEN_TYPES_H_
6#define HYDROGEN_TYPES_H_
7
8#include <climits>
9
10#include "src/base/macros.h"
11
12namespace v8 {
13namespace internal {
14
15// Forward declarations.
16template <typename T> class Handle;
17class Object;
18class OStream;
19
20#define HTYPE_LIST(V)                                 \
21  V(Any, 0x0)              /* 0000 0000 0000 0000 */  \
22  V(Tagged, 0x1)           /* 0000 0000 0000 0001 */  \
23  V(TaggedPrimitive, 0x5)  /* 0000 0000 0000 0101 */  \
24  V(TaggedNumber, 0xd)     /* 0000 0000 0000 1101 */  \
25  V(Smi, 0x1d)             /* 0000 0000 0001 1101 */  \
26  V(HeapObject, 0x21)      /* 0000 0000 0010 0001 */  \
27  V(HeapPrimitive, 0x25)   /* 0000 0000 0010 0101 */  \
28  V(Null, 0x27)            /* 0000 0000 0010 0111 */  \
29  V(HeapNumber, 0x2d)      /* 0000 0000 0010 1101 */  \
30  V(String, 0x65)          /* 0000 0000 0110 0101 */  \
31  V(Boolean, 0xa5)         /* 0000 0000 1010 0101 */  \
32  V(Undefined, 0x125)      /* 0000 0001 0010 0101 */  \
33  V(JSObject, 0x221)       /* 0000 0010 0010 0001 */  \
34  V(JSArray, 0x621)        /* 0000 0110 0010 0001 */  \
35  V(None, 0x7ff)           /* 0000 0111 1111 1111 */
36
37class HType FINAL {
38 public:
39  #define DECLARE_CONSTRUCTOR(Name, mask) \
40    static HType Name() WARN_UNUSED_RESULT { return HType(k##Name); }
41  HTYPE_LIST(DECLARE_CONSTRUCTOR)
42  #undef DECLARE_CONSTRUCTOR
43
44  // Return the weakest (least precise) common type.
45  HType Combine(HType other) const WARN_UNUSED_RESULT {
46    return HType(static_cast<Kind>(kind_ & other.kind_));
47  }
48
49  bool Equals(HType other) const WARN_UNUSED_RESULT {
50    return kind_ == other.kind_;
51  }
52
53  bool IsSubtypeOf(HType other) const WARN_UNUSED_RESULT {
54    return Combine(other).Equals(other);
55  }
56
57  #define DECLARE_IS_TYPE(Name, mask)               \
58    bool Is##Name() const WARN_UNUSED_RESULT {   \
59      return IsSubtypeOf(HType::Name());            \
60    }
61  HTYPE_LIST(DECLARE_IS_TYPE)
62  #undef DECLARE_IS_TYPE
63
64  template <class T>
65  static HType FromType(typename T::TypeHandle type) WARN_UNUSED_RESULT;
66  static HType FromValue(Handle<Object> value) WARN_UNUSED_RESULT;
67
68  friend OStream& operator<<(OStream& os, const HType& t);
69
70 private:
71  enum Kind {
72    #define DECLARE_TYPE(Name, mask) k##Name = mask,
73    HTYPE_LIST(DECLARE_TYPE)
74    #undef DECLARE_TYPE
75    LAST_KIND = kNone
76  };
77
78  // Make sure type fits in int16.
79  STATIC_ASSERT(LAST_KIND < (1 << (CHAR_BIT * sizeof(int16_t))));
80
81  explicit HType(Kind kind) : kind_(kind) { }
82
83  int16_t kind_;
84};
85
86
87OStream& operator<<(OStream& os, const HType& t);
88} }  // namespace v8::internal
89
90#endif  // HYDROGEN_TYPES_H_
91