convert HackConstant to use ConstId
[hiphop-php.git] / hphp / hack / src / hackc / ir / ir_core / unit.rs
blob26c288e9121a8ac8a6e309151975103e66d42f3b
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 use std::sync::Arc;
8 use bstr::BString;
9 use ffi::Str;
11 use crate::func::SrcLoc;
12 use crate::string_intern::StringInterner;
13 use crate::Attribute;
14 use crate::Class;
15 use crate::ClassName;
16 use crate::ConstName;
17 use crate::FatalOp;
18 use crate::Function;
19 use crate::FunctionName;
20 use crate::HackConstant;
21 use crate::IncludePath;
22 use crate::Module;
23 use crate::Typedef;
25 #[derive(Default)]
26 pub struct SymbolRefs<'a> {
27     pub classes: Vec<ClassName<'a>>,
28     pub constants: Vec<ConstName<'a>>,
29     pub functions: Vec<FunctionName<'a>>,
30     pub includes: Vec<IncludePath<'a>>,
33 /// Fields used when a unit had compile-time errors that should be reported
34 /// when the unit is loaded.
35 #[derive(Debug)]
36 pub struct Fatal {
37     pub op: FatalOp,
38     pub loc: SrcLoc,
39     pub message: BString,
42 /// Unit represents a single parsed file.
43 #[derive(Default)]
44 pub struct Unit<'a> {
45     /// The list of classes defined in this Unit. This also includes enums which
46     /// are transformed into classes internally.
47     ///
48     /// ```
49     /// class MyClass { ... }
50     /// ```
51     pub classes: Vec<Class<'a>>,
53     /// The list of top-level constants.
54     ///
55     /// ```
56     /// const MAGIC_VALUE: int = 42;
57     /// ```
58     pub constants: Vec<HackConstant>,
60     /// Per-file attributes.
61     ///
62     /// ```
63     /// <<file:__EnableUnstableFeatures('readonly')>>
64     /// ```
65     pub file_attributes: Vec<Attribute<'a>>,
67     /// The list of top-level functions defined in this Unit. This include both
68     /// user-defined functions and "special" compiler-defined functions (like
69     /// static initializers).
70     ///
71     /// ```
72     /// function my_fn(int $a, int $b) { ... }
73     /// ```
74     pub functions: Vec<Function<'a>>,
76     /// If the Unit failed to parse or compile this defines the error that
77     /// should be reported and the rest of the Unit will be empty.
78     //
79     // NB: It could be argued that Unit should be an enum with Fatal/Success
80     // being two separate variants.
81     pub fatal: Option<Fatal>,
83     pub modules: Vec<Module<'a>>,
84     pub module_use: Option<Str<'a>>,
86     /// The unit string interning table.
87     pub strings: Arc<StringInterner>,
89     /// The list of all external symbols referenced by this Unit.
90     //
91     // NB: We really should be able to generate this from the IR itself instead
92     // of relying on a separate table.
93     pub symbol_refs: SymbolRefs<'a>,
95     /// The list of top-level typedefs or aliases defined in this Unit.
96     ///
97     /// ```
98     /// type A = B;
99     /// ```
100     pub typedefs: Vec<Typedef<'a>>,