Add sub-controls for Hack array compat runtime checks
[hiphop-php.git] / hphp / runtime / base / tv-conversions.h
blob86597c3d382c5c47eed63a54ecab560b8ae367c4
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_TV_CONVERSIONS_H_
18 #define incl_HPHP_TV_CONVERSIONS_H_
20 #include "hphp/runtime/base/datatype.h"
21 #include "hphp/runtime/base/req-root.h"
22 #include "hphp/runtime/base/type-array.h"
23 #include "hphp/runtime/base/type-object.h"
24 #include "hphp/runtime/base/type-string.h"
25 #include "hphp/runtime/base/typed-value.h"
27 namespace HPHP {
29 struct StringData;
31 ///////////////////////////////////////////////////////////////////////////////
33 * TypedValue conversions that update `tv' in place (decrefing the old value,
34 * if necessary).
36 * We have two kinds of type conversions:
38 * - Cast forcibly changes the value to the new type and will not fail (though
39 * the result may be silly).
40 * - Coerce attempts to convert the type and returns false on failure.
43 #define X(kind) \
44 void tvCastTo##kind##InPlace(TypedValue* tv); \
45 bool tvCoerceParamTo##kind##InPlace(TypedValue* tv, \
46 bool builtin);
47 X(Boolean)
48 X(Int64)
49 X(Double)
50 X(String)
51 X(Vec)
52 X(Dict)
53 X(Keyset)
54 X(Array)
55 X(Object)
56 X(NullableObject)
57 X(Resource)
58 #undef X
60 void tvCastToVArrayInPlace(TypedValue* tv);
61 void tvCastToDArrayInPlace(TypedValue* tv);
63 ALWAYS_INLINE void tvCastInPlace(TypedValue* tv, DataType DType) {
64 #define X(kind) \
65 if (DType == KindOf##kind) { tvCastTo##kind##InPlace(tv); return; }
66 X(Boolean)
67 X(Int64)
68 X(Double)
69 X(String)
70 X(Vec)
71 X(Dict)
72 X(Keyset)
73 X(Array)
74 X(Object)
75 X(Resource)
76 #undef X
77 not_reached();
80 ALWAYS_INLINE bool tvCoerceParamInPlace(TypedValue* tv, DataType DType,
81 bool builtin) {
82 #define X(kind) \
83 if (DType == KindOf##kind) \
84 return tvCoerceParamTo##kind##InPlace(tv, \
85 builtin);
86 X(Boolean)
87 X(Int64)
88 X(Double)
89 X(String)
90 X(Vec)
91 X(Dict)
92 X(Keyset)
93 X(Array)
94 X(Object)
95 X(Resource)
96 #undef X
97 not_reached();
101 * Non-in-place casts.
103 bool tvCastToBoolean(TypedValue tv);
104 int64_t tvCastToInt64(TypedValue tv);
105 double tvCastToDouble(TypedValue tv);
106 String tvCastToString(TypedValue tv);
107 Array tvCastToArrayLike(TypedValue tv);
108 Object tvCastToObject(TypedValue tv);
110 StringData* tvCastToStringData(TypedValue tv);
111 ArrayData* tvCastToArrayLikeData(TypedValue tv);
112 ObjectData* tvCastToObjectData(TypedValue tv);
115 * Convert a cell to various raw data types, without changing the Cell.
117 bool cellToBool(Cell);
118 int64_t cellToInt(Cell);
119 double cellToDouble(Cell);
122 * Convert `tv' or `cell' to a valid array key for `ad', or throw an exception.
124 Cell cellToKey(Cell cell, const ArrayData* ad);
125 Cell tvToKey(TypedValue tv, const ArrayData* ad);
128 * Convert a string to a TypedNum following PHP semantics, allowing strings
129 * that have only a partial number in them (i.e. the string may have junk after
130 * the number).
132 TypedNum stringToNumeric(const StringData*);
134 ///////////////////////////////////////////////////////////////////////////////
138 #include "hphp/runtime/base/tv-conversions-inl.h"
140 #endif