codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / runtime / vm / type-alias.h
blob6d746920c0ccc6489ddd6817cfdf6aa1c5b28ea7
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_TYPE_ALIAS_H_
18 #define incl_HPHP_TYPE_ALIAS_H_
20 #include "hphp/runtime/base/annot-type.h"
21 #include "hphp/runtime/base/array-data.h"
22 #include "hphp/runtime/base/attr.h"
23 #include "hphp/runtime/base/datatype.h"
24 #include "hphp/runtime/base/type-array.h"
25 #include "hphp/runtime/base/typed-value.h"
26 #include "hphp/runtime/base/types.h"
27 #include "hphp/runtime/base/user-attributes.h"
29 namespace HPHP {
30 ///////////////////////////////////////////////////////////////////////////////
32 struct Class;
33 struct StringData;
34 struct ArrayData;
36 ///////////////////////////////////////////////////////////////////////////////
39 * This is the runtime representation of a type alias. Type aliases are only
40 * allowed when HipHop extensions are enabled.
42 * The `type' field is Object whenever the type alias is basically just a
43 * name. At runtime we still might resolve this name to another type alias,
44 * becoming a type alias for KindOfArray or something in that request.
46 * For the per-request struct, see TypeAliasReq below.
48 struct TypeAlias {
49 LowStringPtr name;
50 LowStringPtr value;
51 Attr attrs;
52 AnnotType type;
53 bool nullable; // null is allowed; for ?Foo aliases
54 UserAttributeMap userAttrs;
55 Array typeStructure{Array::Create()};
57 template<class SerDe>
58 typename std::enable_if<!SerDe::deserializing>::type
59 serde(SerDe& sd) {
60 sd(name)
61 (value)
62 (type)
63 (nullable)
64 (userAttrs)
65 (attrs)
67 TypedValue tv = make_tv<KindOfArray>(typeStructure.get());
68 sd(tv);
71 template<class SerDe>
72 typename std::enable_if<SerDe::deserializing>::type
73 serde(SerDe& sd) {
74 sd(name)
75 (value)
76 (type)
77 (nullable)
78 (userAttrs)
79 (attrs)
82 TypedValue tv;
83 sd(tv);
84 assert(isArrayType(tv.m_type));
85 typeStructure = tv.m_data.parr;
91 ///////////////////////////////////////////////////////////////////////////////
94 * In a given request, a defined type alias is turned into a TypeAliasReq
95 * struct. This contains the information needed to validate parameter type
96 * hints for a type alias at runtime.
98 struct TypeAliasReq {
100 /////////////////////////////////////////////////////////////////////////////
101 // Static constructors.
103 static TypeAliasReq Invalid();
104 static TypeAliasReq From(const TypeAlias& alias);
105 static TypeAliasReq From(TypeAliasReq req, const TypeAlias& alias);
108 /////////////////////////////////////////////////////////////////////////////
109 // Comparison.
111 bool same(const TypeAliasReq& req) const;
112 bool compat(const TypeAlias& alias) const;
115 /////////////////////////////////////////////////////////////////////////////
116 // Data members.
118 // The aliased type.
119 AnnotType type{AnnotType::Uninit};
120 // Overrides `type' if the alias is invalid (e.g., for a nonexistent class).
121 bool invalid{false};
122 // For option types, like ?Foo.
123 bool nullable{false};
124 // Aliased Class; nullptr if type != Object.
125 LowPtr<Class> klass{nullptr};
126 // Needed for error messages; nullptr if not defined.
127 LowStringPtr name{nullptr};
128 Array typeStructure{Array::Create()};
129 UserAttributeMap userAttrs;
132 bool operator==(const TypeAliasReq& l, const TypeAliasReq& r);
133 bool operator!=(const TypeAliasReq& l, const TypeAliasReq& r);
135 ///////////////////////////////////////////////////////////////////////////////
138 #define incl_HPHP_TYPE_ALIAS_INL_H_
139 #include "hphp/runtime/vm/type-alias-inl.h"
140 #undef incl_HPHP_TYPE_ALIAS_INL_H_
142 #endif