Add sub-controls for Hack array compat runtime checks
[hiphop-php.git] / hphp / runtime / base / empty-array.h
blob79d32b4f22cd54b5958b9c286d853a0e2be2df70
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 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_EMPTY_ARRAY_H_
17 #define incl_HPHP_EMPTY_ARRAY_H_
19 #include <cstddef>
20 #include <cstdint>
21 #include <utility>
22 #include <sys/types.h>
24 #include "hphp/runtime/base/array-common.h"
25 #include "hphp/runtime/base/array-data.h"
26 #include "hphp/runtime/base/header-kind.h"
27 #include "hphp/runtime/base/member-val.h"
28 #include "hphp/runtime/base/sort-flags.h"
29 #include "hphp/runtime/base/typed-value.h"
31 #include "hphp/util/type-scan.h"
33 namespace HPHP {
35 //////////////////////////////////////////////////////////////////////
37 struct Variant;
38 struct RefData;
39 struct StringData;
40 struct MArrayIter;
41 struct MixedArray;
43 //////////////////////////////////////////////////////////////////////
46 * Functions relating to the "empty array" kind. These implement
47 * entries in the array dispatch table for the global empty array.
48 * Other arrays may also be empty in the sense that size() == 0, but
49 * this one is dealt with commonly enough to deserve special handlers.
51 struct EmptyArray final : type_scan::MarkCollectable<EmptyArray> {
52 static void Release(ArrayData*);
54 static member_rval::ptr_u NvGetInt(const ArrayData*, int64_t) {
55 return nullptr;
57 static constexpr auto NvTryGetInt = &NvGetInt;
59 static member_rval::ptr_u NvGetStr(const ArrayData*, const StringData*) {
60 return nullptr;
62 static constexpr auto NvTryGetStr = &NvGetStr;
64 static member_rval RvalInt(const ArrayData* ad, int64_t k) {
65 return member_rval { ad, NvGetInt(ad, k) };
67 static member_rval RvalIntStrict(const ArrayData* ad, int64_t k) {
68 return member_rval { ad, NvTryGetInt(ad, k) };
70 static member_rval RvalStr(const ArrayData* ad, const StringData* k) {
71 return member_rval { ad, NvGetStr(ad, k) };
73 static member_rval RvalStrStrict(const ArrayData* ad, const StringData* k) {
74 return member_rval { ad, NvTryGetStr(ad, k) };
76 static member_rval RvalAtPos(const ArrayData* ad, ssize_t pos) {
77 return member_rval { ad, GetValueRef(ad, pos) };
80 static Cell NvGetKey(const ArrayData*, ssize_t pos);
81 static ArrayData* SetInt(ArrayData*, int64_t k, Cell v, bool copy);
82 static ArrayData* SetStr(ArrayData*, StringData* k, Cell v, bool copy);
83 static ArrayData* SetWithRefInt(ArrayData*, int64_t k,
84 TypedValue v, bool copy);
85 static ArrayData* SetWithRefStr(ArrayData*, StringData* k,
86 TypedValue v, bool copy);
87 static ArrayData* RemoveInt(ArrayData* ad, int64_t, bool) {
88 return ad;
90 static ArrayData* RemoveStr(ArrayData* ad, const StringData*, bool) {
91 return ad;
93 static size_t Vsize(const ArrayData*);
94 static member_rval::ptr_u GetValueRef(const ArrayData* ad, ssize_t pos);
95 static bool IsVectorData(const ArrayData*) {
96 return true;
98 static bool ExistsInt(const ArrayData*, int64_t) {
99 return false;
101 static bool ExistsStr(const ArrayData*, const StringData*) {
102 return false;
104 static member_lval LvalInt(ArrayData*, int64_t k, bool copy);
105 static member_lval LvalIntRef(ArrayData*, int64_t k, bool copy);
106 static member_lval LvalStr(ArrayData*, StringData* k, bool copy);
107 static member_lval LvalStrRef(ArrayData*, StringData* k, bool copy);
108 static member_lval LvalNew(ArrayData*, bool copy);
109 static member_lval LvalNewRef(ArrayData*, bool copy);
110 static ArrayData* SetRefInt(ArrayData*, int64_t k,
111 member_lval v, bool copy);
112 static ArrayData* SetRefStr(ArrayData*, StringData* k,
113 member_lval v, bool copy);
114 static constexpr auto AddInt = &SetInt;
115 static constexpr auto AddStr = &SetStr;
116 static constexpr auto IterBegin = &ArrayCommon::ReturnInvalidIndex;
117 static constexpr auto IterLast = &ArrayCommon::ReturnInvalidIndex;
118 static constexpr auto IterEnd = &ArrayCommon::ReturnInvalidIndex;
119 static ssize_t IterAdvance(const ArrayData*, ssize_t prev);
120 static ssize_t IterRewind(const ArrayData*, ssize_t prev);
122 // ValidMArrayIter may be called on this array kind, because Escalate is a
123 // no-op.
124 static bool ValidMArrayIter(const ArrayData*, const MArrayIter&) {
125 return false;
127 static bool AdvanceMArrayIter(ArrayData*, MArrayIter& fp);
128 static ArrayData* EscalateForSort(ArrayData* ad, SortFunction /*sf*/) {
129 return ad;
131 static void Ksort(ArrayData*, int, bool) {}
132 static void Sort(ArrayData*, int, bool) {}
133 static void Asort(ArrayData*, int, bool) {}
134 static bool Uksort(ArrayData*, const Variant&) {
135 return true;
137 static bool Usort(ArrayData*, const Variant&) {
138 return true;
140 static bool Uasort(ArrayData*, const Variant&) {
141 return true;
143 static ArrayData* PopOrDequeue(ArrayData*, Variant&);
144 static constexpr auto Pop = &PopOrDequeue;
145 static constexpr auto Dequeue = &PopOrDequeue;
146 static ArrayData* Copy(const ArrayData* ad);
147 static ArrayData* CopyStatic(const ArrayData*);
148 static ArrayData* Append(ArrayData*, Cell v, bool copy);
149 static ArrayData* AppendRef(ArrayData*, member_lval v, bool copy);
150 static ArrayData* AppendWithRef(ArrayData*, TypedValue v, bool copy);
151 static ArrayData* PlusEq(ArrayData*, const ArrayData* elems);
152 static ArrayData* Merge(ArrayData*, const ArrayData* elems);
153 static ArrayData* Prepend(ArrayData*, Cell v, bool copy);
154 static ArrayData* ToPHPArray(ArrayData* ad, bool) {
155 return ad;
157 static ArrayData* ToVArray(ArrayData*, bool) {
158 return staticEmptyVArray();
160 static ArrayData* ToDArray(ArrayData*, bool) {
161 return staticEmptyDArray();
163 static ArrayData* ToDict(ArrayData*, bool);
164 static ArrayData* ToVec(ArrayData*, bool);
165 static ArrayData* ToKeyset(ArrayData*, bool);
166 static void Renumber(ArrayData*) {}
167 static void OnSetEvalScalar(ArrayData*);
168 static ArrayData* Escalate(const ArrayData* ad) {
169 return const_cast<ArrayData*>(ad);
172 private:
173 static member_lval MakePacked(TypedValue);
174 static member_lval MakePackedInl(TypedValue);
175 static member_lval MakeMixed(StringData*, TypedValue);
176 static member_lval MakeMixed(int64_t, TypedValue);
178 private:
179 struct Initializer;
180 static Initializer s_initializer;
183 //////////////////////////////////////////////////////////////////////
187 #endif