]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_macros/src/lib.rs
Rollup merge of #104622 - nicholasbishop:bishop-uefi-clang, r=Mark-Simulacrum
[rust.git] / compiler / rustc_macros / src / lib.rs
1 #![feature(allow_internal_unstable)]
2 #![feature(if_let_guard)]
3 #![feature(never_type)]
4 #![feature(proc_macro_diagnostic)]
5 #![feature(proc_macro_span)]
6 #![allow(rustc::default_hash_types)]
7 #![deny(rustc::untranslatable_diagnostic)]
8 #![deny(rustc::diagnostic_outside_of_impl)]
9 #![recursion_limit = "128"]
10
11 use synstructure::decl_derive;
12
13 use proc_macro::TokenStream;
14
15 mod diagnostics;
16 mod hash_stable;
17 mod lift;
18 mod newtype;
19 mod query;
20 mod serialize;
21 mod symbols;
22 mod type_foldable;
23 mod type_visitable;
24
25 #[proc_macro]
26 pub fn rustc_queries(input: TokenStream) -> TokenStream {
27     query::rustc_queries(input)
28 }
29
30 #[proc_macro]
31 pub fn symbols(input: TokenStream) -> TokenStream {
32     symbols::symbols(input.into()).into()
33 }
34
35 /// Creates a struct type `S` that can be used as an index with
36 /// `IndexVec` and so on.
37 ///
38 /// There are two ways of interacting with these indices:
39 ///
40 /// - The `From` impls are the preferred way. So you can do
41 ///   `S::from(v)` with a `usize` or `u32`. And you can convert back
42 ///   to an integer with `u32::from(s)`.
43 ///
44 /// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
45 ///   to create/return a value.
46 ///
47 /// Internally, the index uses a u32, so the index must not exceed
48 /// `u32::MAX`. You can also customize things like the `Debug` impl,
49 /// what traits are derived, and so forth via the macro.
50 #[proc_macro]
51 #[allow_internal_unstable(step_trait, rustc_attrs, trusted_step)]
52 pub fn newtype_index(input: TokenStream) -> TokenStream {
53     newtype::newtype(input)
54 }
55
56 /// Implements the `fluent_messages` macro, which performs compile-time validation of the
57 /// compiler's Fluent resources (i.e. that the resources parse and don't multiply define the same
58 /// messages) and generates constants that make using those messages in diagnostics more ergonomic.
59 ///
60 /// For example, given the following invocation of the macro..
61 ///
62 /// ```ignore (rust)
63 /// fluent_messages! {
64 ///     typeck => "./typeck.ftl",
65 /// }
66 /// ```
67 /// ..where `typeck.ftl` has the following contents..
68 ///
69 /// ```fluent
70 /// typeck_field_multiply_specified_in_initializer =
71 ///     field `{$ident}` specified more than once
72 ///     .label = used more than once
73 ///     .label_previous_use = first use of `{$ident}`
74 /// ```
75 /// ...then the macro parse the Fluent resource, emitting a diagnostic if it fails to do so, and
76 /// will generate the following code:
77 ///
78 /// ```ignore (rust)
79 /// pub static DEFAULT_LOCALE_RESOURCES: &'static [&'static str] = &[
80 ///     include_str!("./typeck.ftl"),
81 /// ];
82 ///
83 /// mod fluent_generated {
84 ///     mod typeck {
85 ///         pub const field_multiply_specified_in_initializer: DiagnosticMessage =
86 ///             DiagnosticMessage::fluent("typeck_field_multiply_specified_in_initializer");
87 ///         pub const field_multiply_specified_in_initializer_label_previous_use: DiagnosticMessage =
88 ///             DiagnosticMessage::fluent_attr(
89 ///                 "typeck_field_multiply_specified_in_initializer",
90 ///                 "previous_use_label"
91 ///             );
92 ///     }
93 /// }
94 /// ```
95 /// When emitting a diagnostic, the generated constants can be used as follows:
96 ///
97 /// ```ignore (rust)
98 /// let mut err = sess.struct_span_err(
99 ///     span,
100 ///     fluent::typeck::field_multiply_specified_in_initializer
101 /// );
102 /// err.span_default_label(span);
103 /// err.span_label(
104 ///     previous_use_span,
105 ///     fluent::typeck::field_multiply_specified_in_initializer_label_previous_use
106 /// );
107 /// err.emit();
108 /// ```
109 #[proc_macro]
110 pub fn fluent_messages(input: TokenStream) -> TokenStream {
111     diagnostics::fluent_messages(input)
112 }
113
114 decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
115 decl_derive!(
116     [HashStable_Generic, attributes(stable_hasher)] =>
117     hash_stable::hash_stable_generic_derive
118 );
119
120 decl_derive!([Decodable] => serialize::decodable_derive);
121 decl_derive!([Encodable] => serialize::encodable_derive);
122 decl_derive!([TyDecodable] => serialize::type_decodable_derive);
123 decl_derive!([TyEncodable] => serialize::type_encodable_derive);
124 decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
125 decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
126 decl_derive!([TypeFoldable, attributes(type_foldable)] => type_foldable::type_foldable_derive);
127 decl_derive!([TypeVisitable, attributes(type_visitable)] => type_visitable::type_visitable_derive);
128 decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
129 decl_derive!(
130     [Diagnostic, attributes(
131         // struct attributes
132         diag,
133         help,
134         note,
135         warning,
136         // field attributes
137         skip_arg,
138         primary_span,
139         label,
140         subdiagnostic,
141         suggestion,
142         suggestion_short,
143         suggestion_hidden,
144         suggestion_verbose)] => diagnostics::session_diagnostic_derive
145 );
146 decl_derive!(
147     [LintDiagnostic, attributes(
148         // struct attributes
149         diag,
150         help,
151         note,
152         warning,
153         // field attributes
154         skip_arg,
155         primary_span,
156         label,
157         subdiagnostic,
158         suggestion,
159         suggestion_short,
160         suggestion_hidden,
161         suggestion_verbose)] => diagnostics::lint_diagnostic_derive
162 );
163 decl_derive!(
164     [Subdiagnostic, attributes(
165         // struct/variant attributes
166         label,
167         help,
168         note,
169         warning,
170         suggestion,
171         suggestion_short,
172         suggestion_hidden,
173         suggestion_verbose,
174         multipart_suggestion,
175         multipart_suggestion_short,
176         multipart_suggestion_hidden,
177         multipart_suggestion_verbose,
178         // field attributes
179         skip_arg,
180         primary_span,
181         suggestion_part,
182         applicability)] => diagnostics::session_subdiagnostic_derive
183 );