Remove positive is_typechecker guards
[hiphop-php.git] / hphp / hack / src / hhbc / emitter / label.rs
blob98aab1fff15b27cfb81cc9611fefd09667198d17
1 // Copyright (c) Facebook, Inc. and its affiliates.
2 //
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 pub type Id = usize;
8 #[derive(Debug, Clone, Copy, PartialEq, Eq, std::cmp::Ord, std::cmp::PartialOrd)]
9 #[repr(C)]
10 pub enum Label {
11     Regular(Id),
12     DefaultArg(Id),
14 impl Label {
15     pub fn id(&self) -> &Id {
16         match self {
17             Label::Regular(id) => id,
18             Label::DefaultArg(id) => id,
19         }
20     }
22     pub fn map<F: FnOnce(&Id) -> Id>(&self, f: F) -> Label {
23         match self {
24             Label::Regular(id) => Label::Regular(f(id)),
25             Label::DefaultArg(id) => Label::DefaultArg(f(id)),
26         }
27     }
30 #[derive(Debug)]
31 pub enum Error {
32     Id,
33     OptionMap,
34     Map,
36 impl std::fmt::Display for Error {
37     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38         let mut msg = "Label should be rewritten before this point".to_owned();
39         msg.push_str(match self {
40             Error::Id => " (id)",
41             Error::OptionMap => " (option_map)",
42             Error::Map => " (map)",
43         });
44         write!(f, "{}", msg)
45     }
48 #[derive(Default, Debug)]
49 pub struct Gen {
50     next_id: Id,
53 impl Gen {
54     pub fn next_regular(&mut self) -> Label {
55         Label::Regular(self.get_next())
56     }
57     pub fn next_default_arg(&mut self) -> Label {
58         Label::DefaultArg(self.get_next())
59     }
61     fn get_next(&mut self) -> Id {
62         let curr_id = self.next_id;
63         self.next_id = curr_id + 1;
64         curr_id
65     }
67     pub fn reset(&mut self) {
68         self.next_id = 0;
69     }