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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * Copyright 2018 The Starlark in Rust Authors.
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::cell::UnsafeCell;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::sync::Arc;

use allocative::Allocative;
use display_container::fmt_container;
use dupe::Dupe;
use either::Either;
use once_cell::unsync::OnceCell;
use starlark_derive::starlark_module;
use starlark_derive::starlark_value;
use starlark_derive::Coerce;
use starlark_derive::NoSerialize;
use starlark_derive::StarlarkDocs;
use starlark_derive::Trace;
use starlark_map::small_map::SmallMap;
use starlark_map::Equivalent;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::environment::Methods;
use crate::environment::MethodsBuilder;
use crate::environment::MethodsStatic;
use crate::eval::Arguments;
use crate::eval::Evaluator;
use crate::typing::starlark_value::TyStarlarkValue;
use crate::typing::user::TyUser;
use crate::typing::user::TyUserIndex;
use crate::typing::user::TyUserParams;
use crate::typing::Param;
use crate::typing::Ty;
use crate::typing::TyFunction;
use crate::values::enumeration::matcher::EnumTypeMatcher;
use crate::values::enumeration::ty_enum_type::TyEnumData;
use crate::values::enumeration::value::EnumValueGen;
use crate::values::enumeration::EnumValue;
use crate::values::function::FUNCTION_TYPE;
use crate::values::index::convert_index;
use crate::values::list::AllocList;
use crate::values::types::type_instance_id::TypeInstanceId;
use crate::values::typing::type_compiled::type_matcher_factory::TypeMatcherFactory;
use crate::values::Freeze;
use crate::values::Freezer;
use crate::values::FrozenValue;
use crate::values::Heap;
use crate::values::StarlarkValue;
use crate::values::StringValue;
use crate::values::Value;
use crate::values::ValueLike;

#[derive(thiserror::Error, Debug)]
enum EnumError {
    #[error("enum values must all be distinct, but repeated `{0}`")]
    DuplicateEnumValue(String),
    #[error("Unknown enum element `{0}`, given to `{1}`")]
    InvalidElement(String, String),
}

#[doc(hidden)]
pub trait EnumCell: Freeze {
    type TyEnumDataOpt: Debug;

    fn get_or_init_ty(
        ty: &Self::TyEnumDataOpt,
        f: impl FnOnce() -> crate::Result<Arc<TyEnumData>>,
    ) -> crate::Result<()>;
    fn get_ty(ty: &Self::TyEnumDataOpt) -> Option<&Arc<TyEnumData>>;
}

impl<'v> EnumCell for Value<'v> {
    type TyEnumDataOpt = OnceCell<Arc<TyEnumData>>;

    fn get_or_init_ty(
        ty: &Self::TyEnumDataOpt,
        f: impl FnOnce() -> crate::Result<Arc<TyEnumData>>,
    ) -> crate::Result<()> {
        ty.get_or_try_init(f)?;
        Ok(())
    }

    fn get_ty(ty: &Self::TyEnumDataOpt) -> Option<&Arc<TyEnumData>> {
        ty.get()
    }
}

impl EnumCell for FrozenValue {
    type TyEnumDataOpt = Option<Arc<TyEnumData>>;

    fn get_or_init_ty(
        ty: &Self::TyEnumDataOpt,
        f: impl FnOnce() -> crate::Result<Arc<TyEnumData>>,
    ) -> crate::Result<()> {
        let _ignore = (ty, f);
        Ok(())
    }

    fn get_ty(ty: &Self::TyEnumDataOpt) -> Option<&Arc<TyEnumData>> {
        ty.as_ref()
    }
}

/// The type of an enumeration, created by `enum()`.
#[derive(
    Debug,
    Trace,
    Coerce,
    NoSerialize,
    ProvidesStaticType,
    StarlarkDocs,
    Allocative
)]
#[starlark_docs(builtin = "extension")]
#[repr(C)]
// Deliberately store fully populated values
// for each entry, so we can produce enum values with zero allocation.
pub struct EnumTypeGen<V: EnumCell> {
    pub(crate) id: TypeInstanceId,
    #[allocative(skip)] // TODO(nga): do not skip.
    // TODO(nga): teach derive to do something like `#[trace(static)]`.
    #[trace(unsafe_ignore)]
    pub(crate) ty_enum_data: V::TyEnumDataOpt,
    // The key is the value of the enumeration
    // The value is a value of type EnumValue
    #[allocative(skip)] // TODO(nga): do not skip.
    elements: UnsafeCell<SmallMap<V, V>>,
}

impl<'v> Freeze for EnumTypeGen<Value<'v>> {
    type Frozen = EnumTypeGen<FrozenValue>;

    fn freeze(self, freezer: &Freezer) -> anyhow::Result<Self::Frozen> {
        let EnumTypeGen {
            id,
            ty_enum_data: ty_enum_type,
            elements,
        } = self;
        let ty_enum_type = ty_enum_type.into_inner();
        let elements = elements.freeze(freezer)?;
        Ok(EnumTypeGen {
            id,
            ty_enum_data: ty_enum_type,
            elements,
        })
    }
}

unsafe impl<V: EnumCell + Freeze> Send for EnumTypeGen<V> {}
unsafe impl<V: EnumCell + Freeze> Sync for EnumTypeGen<V> {}

impl<'v, V: EnumCell + ValueLike<'v>> Display for EnumTypeGen<V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt_container(f, "enum(", ")", self.elements().iter().map(|(k, _v)| k))
    }
}

/// Unfrozen enum type.
pub type EnumType<'v> = EnumTypeGen<Value<'v>>;
/// Frozen enum type.
pub type FrozenEnumType = EnumTypeGen<FrozenValue>;

impl<'v> EnumType<'v> {
    pub(crate) fn new(elements: Vec<StringValue<'v>>, heap: &'v Heap) -> crate::Result<Value<'v>> {
        // We are constructing the enum and all elements in one go.
        // They both point at each other, which adds to the complexity.
        let id = TypeInstanceId::gen();
        let typ = heap.alloc(EnumType {
            id,
            ty_enum_data: OnceCell::new(),
            elements: UnsafeCell::new(SmallMap::new()),
        });

        let mut res = SmallMap::with_capacity(elements.len());
        for (i, x) in elements.iter().enumerate() {
            let v = heap.alloc(EnumValue {
                id,
                typ,
                index: i as i32,
                value: x.to_value(),
            });
            if res.insert_hashed(x.to_value().get_hashed()?, v).is_some() {
                return Err(crate::Error::new_other(EnumError::DuplicateEnumValue(
                    x.to_string(),
                )));
            }
        }

        // Here we tie the cycle
        let t = typ.downcast_ref::<EnumType>().unwrap();
        unsafe {
            // SAFETY: we own unique reference to `t`.
            *t.elements.get() = res;
        }
        Ok(typ)
    }
}

impl<V: EnumCell + Freeze> EnumTypeGen<V> {
    pub(crate) fn elements(&self) -> &SmallMap<V, V> {
        // Safe because we never mutate the elements after construction.
        unsafe { &*self.elements.get() }
    }
}

impl<'v, V> EnumTypeGen<V>
where
    Value<'v>: Equivalent<V>,
    V: ValueLike<'v> + 'v + EnumCell,
{
    pub(crate) fn ty_enum_data(&self) -> Option<&Arc<TyEnumData>> {
        V::get_ty(&self.ty_enum_data)
    }

    pub(crate) fn construct(&self, val: Value<'v>) -> crate::Result<V> {
        match self.elements().get_hashed_by_value(val.get_hashed()?) {
            Some(v) => Ok(*v),
            None => Err(crate::Error::new_other(EnumError::InvalidElement(
                val.to_str(),
                self.to_string(),
            ))),
        }
    }
}

#[starlark_value(type = FUNCTION_TYPE)]
impl<'v, V> StarlarkValue<'v> for EnumTypeGen<V>
where
    Self: ProvidesStaticType<'v>,
    Value<'v>: Equivalent<V>,
    V: ValueLike<'v> + 'v + EnumCell,
{
    type Canonical = FrozenEnumType;

    fn invoke(
        &self,
        _me: Value<'v>,
        args: &Arguments<'v, '_>,
        eval: &mut Evaluator<'v, '_>,
    ) -> crate::Result<Value<'v>> {
        args.no_named_args()?;
        let val = args.positional1(eval.heap())?;
        Ok(self.construct(val)?.to_value())
    }

    fn length(&self) -> crate::Result<i32> {
        Ok(self.elements().len() as i32)
    }

    fn at(&self, index: Value, _heap: &'v Heap) -> crate::Result<Value<'v>> {
        let i = convert_index(index, self.elements().len() as i32)? as usize;
        // Must be in the valid range since convert_index checks that, so just unwrap
        Ok(self
            .elements()
            .get_index(i)
            .map(|x| *x.1)
            .unwrap()
            .to_value())
    }

    unsafe fn iterate(&self, me: Value<'v>, _heap: &'v Heap) -> crate::Result<Value<'v>> {
        Ok(me)
    }

    unsafe fn iter_size_hint(&self, index: usize) -> (usize, Option<usize>) {
        debug_assert!(index <= self.elements().len());
        let rem = self.elements().len() - index;
        (rem, Some(rem))
    }

    unsafe fn iter_next(&self, index: usize, _heap: &'v Heap) -> Option<Value<'v>> {
        self.elements().values().nth(index).map(|v| v.to_value())
    }

    unsafe fn iter_stop(&self) {}

    fn get_methods() -> Option<&'static Methods> {
        static RES: MethodsStatic = MethodsStatic::new();
        RES.methods(enum_type_methods)
    }

    fn eval_type(&self) -> Option<Ty> {
        self.ty_enum_data().map(|t| t.ty_enum_value.dupe())
    }

    fn typechecker_ty(&self) -> Option<Ty> {
        self.ty_enum_data().map(|t| t.ty_enum_type.dupe())
    }

    fn export_as(&self, variable_name: &str, _eval: &mut Evaluator<'v, '_>) -> crate::Result<()> {
        V::get_or_init_ty(&self.ty_enum_data, || {
            let ty_enum_value = Ty::custom(TyUser::new(
                variable_name.to_owned(),
                TyStarlarkValue::new::<EnumValue>(),
                self.id,
                TyUserParams {
                    matcher: Some(TypeMatcherFactory::new(EnumTypeMatcher { id: self.id })),
                    ..TyUserParams::default()
                },
            )?);
            let ty_enum_type = Ty::custom(TyUser::new(
                format!("enum[{}]", variable_name),
                TyStarlarkValue::new::<EnumType>(),
                TypeInstanceId::gen(),
                TyUserParams {
                    index: Some(TyUserIndex {
                        index: Ty::int(),
                        result: ty_enum_value.dupe(),
                    }),
                    iter_item: Some(ty_enum_value.dupe()),
                    callable: Some(TyFunction::new(
                        vec![Param::pos_only(
                            // TODO(nga): we can do better parameter type.
                            Ty::any(),
                        )],
                        ty_enum_value.dupe(),
                    )),
                    ..TyUserParams::default()
                },
            )?);
            Ok(Arc::new(TyEnumData {
                name: variable_name.to_owned(),
                variants: self
                    .elements()
                    .iter()
                    .map(|(_, enum_value)| {
                        let enum_value: &EnumValueGen<_> =
                            EnumValue::from_value(enum_value.to_value())
                                .expect("known to be enum value");
                        Ty::of_value(enum_value.value)
                    })
                    .collect(),
                id: self.id,
                ty_enum_value,
                ty_enum_type,
            }))
        })
    }
}

#[starlark_module]
fn enum_type_methods(builder: &mut MethodsBuilder) {
    #[starlark(attribute)]
    fn r#type<'v>(this: Value, heap: &Heap) -> anyhow::Result<Value<'v>> {
        let this = EnumType::from_value(this).unwrap();
        let ty_enum_type = match this {
            Either::Left(x) => x.ty_enum_data(),
            Either::Right(x) => x.ty_enum_data(),
        };
        match ty_enum_type {
            Some(ty_enum_type) => Ok(heap.alloc(ty_enum_type.name.as_str())),
            None => Ok(heap.alloc(EnumValue::TYPE)),
        }
    }

    fn values<'v>(this: Value<'v>) -> anyhow::Result<AllocList<impl Iterator<Item = Value<'v>>>> {
        let this = EnumType::from_value(this).unwrap();
        match this {
            Either::Left(x) => Ok(AllocList(Either::Left(x.elements().keys().copied()))),
            Either::Right(x) => Ok(AllocList(Either::Right(
                x.elements().keys().map(|x| x.to_value()),
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::assert;

    #[test]
    fn test_enum_type_as_type_pass() {
        assert::pass(
            r#"
Color = enum("RED", "GREEN", "BLUE")

def f_pass(x: Color):
    pass

def g_pass(x: Color):
    f_pass(x)
"#,
        );
    }

    #[test]
    fn test_enum_type_fail_runtime() {
        assert::fail(
            r#"
Color = enum("RED", "GREEN", "BLUE")
Season = enum("SPRING", "SUMMER", "AUTUMN", "WINTER")

def f(x: Color):
    pass

def g(x):
    f(x)

g(Season[0])
"#,
            r#"Value `Season("SPRING")` of type `enum` does not match the type annotation `Color` for argument `x`"#,
        );
    }

    #[test]
    fn test_enum_type_fail_compile_time() {
        assert::fail(
            r#"
Color = enum("RED", "GREEN", "BLUE")
Season = enum("SPRING", "SUMMER", "AUTUMN", "WINTER")

def f(x: Color):
    pass

def g(x: Season):
    f(x)
"#,
            r#"Expected type `Color` but got `Season`"#,
        );
    }

    #[test]
    fn test_enum_is_callable() {
        assert::pass(
            r#"
Color = enum("RED", "GREEN", "BLUE")

def foo(x: typing.Callable):
    pass

def bar():
    foo(Color)
"#,
        );
    }

    #[test]
    fn test_enum_value_index() {
        // Test `.index` is available at both compile and runtime.
        assert::pass(
            r#"
Color = enum("RED", "GREEN", "BLUE")

def test():
    for c in Color:
        if c.index == 1:
            pass

test()
"#,
        );
    }

    #[test]
    fn test_enum_value_index_correct_type() {
        assert::fail(
            r#"
Fruit = enum("APPLE", "BANANA", "ORANGE")

def expect_str(s: str):
    pass

def test():
    for f in Fruit:
        expect_str(f.index)
"#,
            "Expected type `str` but got `int`",
        );
    }

    #[test]
    fn test_enum_index() {
        assert::pass(
            r#"
Mood = enum("HAPPY", "SAD")

def test() -> Mood:
    return Mood[0]

test()
"#,
        );
    }

    #[test]
    fn test_enum_index_fail() {
        assert::fail(
            r#"
Shape = enum("SQUARE", "CIRCLE")

def accept_str(s: str):
    pass

def test():
    accept_str(Shape[0])
"#,
            "Expected type `str` but got `Shape`",
        );
    }

    #[test]
    fn test_enum_call() {
        assert::fail(
            r#"
Currency = enum("GBP", "USD", "EUR")

def accept_str(s: str):
    pass

def test():
    accept_str(Currency("GBP"))
"#,
            "Expected type `str` but got `Currency`",
        );
    }
}