/
typing_defs_flags.rs
415 lines (373 loc) · 13.7 KB
/
typing_defs_flags.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the "hack" directory of this source tree.
use bitflags::bitflags;
use eq_modulo_pos::EqModuloPos;
use crate::xhp_attribute;
use crate::xhp_attribute::XhpAttribute;
// NB: Keep the values of these flags in sync with typing_defs_flags.ml.
#[derive(EqModuloPos, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
pub struct FunTypeFlags(u16);
bitflags! {
impl FunTypeFlags: u16 {
const RETURN_DISPOSABLE = 1 << 0;
const IS_COROUTINE = 1 << 3;
const ASYNC = 1 << 4;
const GENERATOR = 1 << 5;
const INSTANTIATED_TARGS = 1 << 8;
const IS_FUNCTION_POINTER = 1 << 9;
const RETURNS_READONLY = 1 << 10;
const READONLY_THIS = 1 << 11;
const SUPPORT_DYNAMIC_TYPE = 1 << 12;
const IS_MEMOIZED = 1 << 13;
const VARIADIC = 1 << 14;
}
}
#[derive(EqModuloPos, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
pub struct FunParamFlags(u16);
bitflags! {
impl FunParamFlags: u16 {
const ACCEPT_DISPOSABLE = 1 << 0;
const INOUT = 1 << 1;
const HAS_DEFAULT = 1 << 2;
const READONLY = 1 << 8;
}
}
#[derive(EqModuloPos, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
pub struct ClassEltFlags(u16);
bitflags! {
impl ClassEltFlags: u16 {
const ABSTRACT = 1 << 0;
const FINAL = 1 << 1;
const SUPERFLUOUS_OVERRIDE = 1 << 2;
//Whether the __Override attribute is erroneous, i.e. there is
// nothing in parents to override. This is set during decling
// (because that's the easiest place to spot this error) so
// that an error can be emitted later during typing.
const LSB = 1 << 3;
const SYNTHESIZED = 1 << 4;
const CONST = 1 << 5;
const LATEINIT = 1 << 6;
const DYNAMICALLYCALLABLE = 1 << 7;
const SUPPORT_DYNAMIC_TYPE = 1 << 8;
const XA_HAS_DEFAULT = 1 << 9;
const XA_TAG_REQUIRED = 1 << 10;
const XA_TAG_LATEINIT = 1 << 11;
const READONLY_PROP = 1 << 12;
const NEEDS_INIT = 1 << 13;
const SAFE_GLOBAL_VARIABLE = 1 << 14;
const XA_FLAGS_MASK = Self::XA_HAS_DEFAULT.bits() | Self::XA_TAG_REQUIRED.bits() | Self::XA_TAG_LATEINIT.bits();
}
}
#[derive(Copy, Clone, Debug)]
pub struct ClassEltFlagsArgs {
pub xhp_attr: Option<XhpAttribute>,
pub is_abstract: bool,
pub is_final: bool,
pub is_superfluous_override: bool,
pub is_lsb: bool,
pub is_synthesized: bool,
pub is_const: bool,
pub is_lateinit: bool,
pub is_dynamicallycallable: bool,
pub is_readonly_prop: bool,
pub supports_dynamic_type: bool,
pub needs_init: bool,
pub safe_global_variable: bool,
}
impl From<xhp_attribute::Tag> for ClassEltFlags {
fn from(tag: xhp_attribute::Tag) -> Self {
use xhp_attribute::Tag;
match tag {
Tag::Required => Self::XA_TAG_REQUIRED,
Tag::LateInit => Self::XA_TAG_LATEINIT,
}
}
}
impl From<Option<XhpAttribute>> for ClassEltFlags {
fn from(xhp_attr: Option<XhpAttribute>) -> Self {
let mut flags = Self::empty();
if let Some(XhpAttribute { tag, has_default }) = xhp_attr {
if let Some(tag) = tag {
flags.insert(Self::from(tag));
} else {
flags.insert(Self::XA_TAG_REQUIRED | Self::XA_TAG_LATEINIT);
}
if has_default {
flags.insert(Self::XA_HAS_DEFAULT);
}
}
flags
}
}
impl ClassEltFlags {
pub fn new(args: ClassEltFlagsArgs) -> Self {
let ClassEltFlagsArgs {
xhp_attr,
is_abstract,
is_final,
is_superfluous_override,
is_lsb,
is_synthesized,
is_const,
is_lateinit,
is_dynamicallycallable,
is_readonly_prop,
supports_dynamic_type,
needs_init,
safe_global_variable,
} = args;
let mut flags = Self::empty();
flags.set(Self::ABSTRACT, is_abstract);
flags.set(Self::FINAL, is_final);
flags.set(Self::SUPERFLUOUS_OVERRIDE, is_superfluous_override);
flags.set(Self::LSB, is_lsb);
flags.set(Self::SYNTHESIZED, is_synthesized);
flags.set(Self::CONST, is_const);
flags.set(Self::LATEINIT, is_lateinit);
flags.set(Self::DYNAMICALLYCALLABLE, is_dynamicallycallable);
flags.set_xhp_attr(xhp_attr);
flags.set(Self::READONLY_PROP, is_readonly_prop);
flags.set(Self::SUPPORT_DYNAMIC_TYPE, supports_dynamic_type);
flags.set(Self::NEEDS_INIT, needs_init);
flags.set(Self::SAFE_GLOBAL_VARIABLE, safe_global_variable);
flags
}
pub fn get_xhp_attr(&self) -> Option<XhpAttribute> {
use xhp_attribute::Tag;
if self.intersection(Self::XA_FLAGS_MASK).is_empty() {
return None;
}
let has_default = self.contains(Self::XA_HAS_DEFAULT);
let tag = match (
self.contains(Self::XA_TAG_REQUIRED),
self.contains(Self::XA_TAG_LATEINIT),
) {
(true, false) => Some(Tag::Required),
(false, true) => Some(Tag::LateInit),
// If both `XaTagRequired` and `XaTagLateinit` bits are set then
// that's code for tag = None.
(true, true) | (false, false) => None,
};
Some(XhpAttribute { tag, has_default })
}
fn set_xhp_attr(&mut self, xa: Option<XhpAttribute>) {
self.remove(Self::XA_FLAGS_MASK);
self.insert(Self::from(xa));
}
}
// This alias is only necessary because of how typing_defs.rs is
// generated (T111144107). TODO(SF, 2022-02-01): Change
// `typing_defs_flags.ml` so that this is no longer necessary.
pub mod class_elt {
use super::ClassEltFlags;
pub type ClassElt = ClassEltFlags;
}
pub mod fun {
use super::FunTypeFlags;
pub type Fun = FunTypeFlags;
}
pub mod fun_param {
use super::FunParamFlags;
pub type FunParam = FunParamFlags;
}
impl ocamlrep::ToOcamlRep for ClassEltFlags {
fn to_ocamlrep<'a, A: ocamlrep::Allocator>(&'a self, _alloc: &'a A) -> ocamlrep::Value<'a> {
ocamlrep::Value::int(self.bits() as isize)
}
}
impl ocamlrep::FromOcamlRep for ClassEltFlags {
fn from_ocamlrep(value: ocamlrep::Value<'_>) -> Result<Self, ocamlrep::FromError> {
let int_value = ocamlrep::from::expect_int(value)?;
Ok(Self::from_bits_truncate(int_value.try_into()?))
}
}
impl<'a> ocamlrep::FromOcamlRepIn<'a> for ClassEltFlags {
fn from_ocamlrep_in(
value: ocamlrep::Value<'_>,
_alloc: &'a bumpalo::Bump,
) -> Result<Self, ocamlrep::FromError> {
use ocamlrep::FromOcamlRep;
Self::from_ocamlrep(value)
}
}
impl no_pos_hash::NoPosHash for ClassEltFlags {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(self, state);
}
}
impl serde::Serialize for ClassEltFlags {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u16(self.bits())
}
}
impl<'de> serde::Deserialize<'de> for ClassEltFlags {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = ClassEltFlags;
fn expecting(&self, formatter: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(formatter, "a u16 for ClassEltFlags")
}
fn visit_u16<E: serde::de::Error>(self, value: u16) -> Result<Self::Value, E> {
Ok(Self::Value::from_bits_truncate(value))
}
fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<Self::Value, E> {
Ok(Self::Value::from_bits_truncate(
u16::try_from(value).expect("expect an u16, but got u64"),
))
}
}
deserializer.deserialize_u16(Visitor)
}
}
impl FunTypeFlags {
pub fn return_disposable(&self) -> bool {
self.contains(Self::RETURN_DISPOSABLE)
}
pub fn is_coroutine(&self) -> bool {
self.contains(Self::IS_COROUTINE)
}
pub fn is_async(&self) -> bool {
self.contains(Self::ASYNC)
}
pub fn is_generator(&self) -> bool {
self.contains(Self::GENERATOR)
}
pub fn instantiated_targs(&self) -> bool {
self.contains(Self::INSTANTIATED_TARGS)
}
pub fn is_function_pointer(&self) -> bool {
self.contains(Self::IS_FUNCTION_POINTER)
}
pub fn returns_readonly(&self) -> bool {
self.contains(Self::RETURNS_READONLY)
}
pub fn readonly_this(&self) -> bool {
self.contains(Self::READONLY_THIS)
}
pub fn support_dynamic_type(&self) -> bool {
self.contains(Self::SUPPORT_DYNAMIC_TYPE)
}
pub fn is_memoized(&self) -> bool {
self.contains(Self::IS_MEMOIZED)
}
pub fn variadic(&self) -> bool {
self.contains(Self::VARIADIC)
}
}
impl ocamlrep::ToOcamlRep for FunTypeFlags {
fn to_ocamlrep<'a, A: ocamlrep::Allocator>(&'a self, _alloc: &'a A) -> ocamlrep::Value<'a> {
ocamlrep::Value::int(self.bits() as isize)
}
}
impl ocamlrep::FromOcamlRep for FunTypeFlags {
fn from_ocamlrep(value: ocamlrep::Value<'_>) -> Result<Self, ocamlrep::FromError> {
let int_value = ocamlrep::from::expect_int(value)?;
Ok(Self::from_bits_truncate(int_value.try_into()?))
}
}
impl<'a> ocamlrep::FromOcamlRepIn<'a> for FunTypeFlags {
fn from_ocamlrep_in(
value: ocamlrep::Value<'_>,
_alloc: &'a bumpalo::Bump,
) -> Result<Self, ocamlrep::FromError> {
use ocamlrep::FromOcamlRep;
Self::from_ocamlrep(value)
}
}
impl no_pos_hash::NoPosHash for FunTypeFlags {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(self, state);
}
}
impl serde::Serialize for FunTypeFlags {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u16(self.bits())
}
}
impl<'de> serde::Deserialize<'de> for FunTypeFlags {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = FunTypeFlags;
fn expecting(&self, formatter: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(formatter, "a u16 for FunTypeFlags")
}
fn visit_u16<E: serde::de::Error>(self, value: u16) -> Result<Self::Value, E> {
Ok(Self::Value::from_bits_truncate(value))
}
fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<Self::Value, E> {
Ok(Self::Value::from_bits_truncate(
u16::try_from(value).expect("expect an u16, but got u64"),
))
}
}
deserializer.deserialize_u16(Visitor)
}
}
impl FunParamFlags {
pub fn accepts_disposable(&self) -> bool {
self.contains(Self::ACCEPT_DISPOSABLE)
}
pub fn is_inout(&self) -> bool {
self.contains(Self::INOUT)
}
pub fn has_default(&self) -> bool {
self.contains(Self::HAS_DEFAULT)
}
pub fn is_readonly(&self) -> bool {
self.contains(Self::READONLY)
}
}
impl ocamlrep::ToOcamlRep for FunParamFlags {
fn to_ocamlrep<'a, A: ocamlrep::Allocator>(&'a self, _alloc: &'a A) -> ocamlrep::Value<'a> {
ocamlrep::Value::int(self.bits() as isize)
}
}
impl ocamlrep::FromOcamlRep for FunParamFlags {
fn from_ocamlrep(value: ocamlrep::Value<'_>) -> Result<Self, ocamlrep::FromError> {
let int_value = ocamlrep::from::expect_int(value)?;
Ok(Self::from_bits_truncate(int_value.try_into()?))
}
}
impl<'a> ocamlrep::FromOcamlRepIn<'a> for FunParamFlags {
fn from_ocamlrep_in(
value: ocamlrep::Value<'_>,
_alloc: &'a bumpalo::Bump,
) -> Result<Self, ocamlrep::FromError> {
use ocamlrep::FromOcamlRep;
Self::from_ocamlrep(value)
}
}
impl no_pos_hash::NoPosHash for FunParamFlags {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(self, state);
}
}
impl serde::Serialize for FunParamFlags {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u16(self.bits())
}
}
impl<'de> serde::Deserialize<'de> for FunParamFlags {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = FunParamFlags;
fn expecting(&self, formatter: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(formatter, "a u16 for FunParamFlags")
}
fn visit_u16<E: serde::de::Error>(self, value: u16) -> Result<Self::Value, E> {
Ok(Self::Value::from_bits_truncate(value))
}
fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<Self::Value, E> {
Ok(Self::Value::from_bits_truncate(
u16::try_from(value).expect("expect an u16, but got u64"),
))
}
}
deserializer.deserialize_u16(Visitor)
}
}