Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 Free Software Foundation, Inc.
2 : :
3 : : // This file is part of GCC.
4 : :
5 : : // GCC is free software; you can redistribute it and/or modify it under
6 : : // the terms of the GNU General Public License as published by the Free
7 : : // Software Foundation; either version 3, or (at your option) any later
8 : : // version.
9 : :
10 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : : // for more details.
14 : :
15 : : // You should have received a copy of the GNU General Public License
16 : : // along with GCC; see the file COPYING3. If not see
17 : : // <http://www.gnu.org/licenses/>.
18 : :
19 : : #include "expected.h"
20 : : #include "libproc_macro_internal/tokenstream.h"
21 : : #include "optional.h"
22 : : #include "rust-ast-full-decls.h"
23 : : #include "rust-builtin-ast-nodes.h"
24 : : #include "rust-expand-format-args.h"
25 : : #include "rust-token-converter.h"
26 : : #include "rust-system.h"
27 : : #include "rust-macro-builtins.h"
28 : : #include "rust-ast-fragment.h"
29 : : #include "rust-ast.h"
30 : : #include "rust-cfg-strip.h"
31 : : #include "rust-diagnostics.h"
32 : : #include "rust-early-name-resolver.h"
33 : : #include "rust-expr.h"
34 : : #include "rust-lex.h"
35 : : #include "rust-macro-invoc-lexer.h"
36 : : #include "rust-macro.h"
37 : : #include "rust-parse.h"
38 : : #include "rust-session-manager.h"
39 : : #include "rust-attribute-values.h"
40 : : #include "rust-fmt.h"
41 : : #include "rust-token.h"
42 : :
43 : : #include "rust-macro-builtins-helpers.h"
44 : : namespace Rust {
45 : :
46 : : const BiMap<std::string, BuiltinMacro> MacroBuiltin::builtins = {{
47 : : {"assert", BuiltinMacro::Assert},
48 : : {"file", BuiltinMacro::File},
49 : : {"line", BuiltinMacro::Line},
50 : : {"column", BuiltinMacro::Column},
51 : : {"include_bytes", BuiltinMacro::IncludeBytes},
52 : : {"include_str", BuiltinMacro::IncludeStr},
53 : : {"stringify", BuiltinMacro::Stringify},
54 : : {"compile_error", BuiltinMacro::CompileError},
55 : : {"concat", BuiltinMacro::Concat},
56 : : {"env", BuiltinMacro::Env},
57 : : {"option_env", BuiltinMacro::OptionEnv},
58 : : {"cfg", BuiltinMacro::Cfg},
59 : : {"include", BuiltinMacro::Include},
60 : : {"format_args", BuiltinMacro::FormatArgs},
61 : : {"format_args_nl", BuiltinMacro::FormatArgsNl},
62 : : {"concat_idents", BuiltinMacro::ConcatIdents},
63 : : {"module_path", BuiltinMacro::ModulePath},
64 : : {"asm", BuiltinMacro::Asm},
65 : : {"llvm_asm", BuiltinMacro::LlvmAsm},
66 : : {"global_asm", BuiltinMacro::GlobalAsm},
67 : : {"log_syntax", BuiltinMacro::LogSyntax},
68 : : {"trace_macros", BuiltinMacro::TraceMacros},
69 : : {"test", BuiltinMacro::Test},
70 : : {"bench", BuiltinMacro::Bench},
71 : : {"test_case", BuiltinMacro::TestCase},
72 : : {"global_allocator", BuiltinMacro::GlobalAllocator},
73 : : {"cfg_accessible", BuiltinMacro::CfgAccessible},
74 : : {"RustcEncodable", BuiltinMacro::RustcDecodable},
75 : : {"RustcDecodable", BuiltinMacro::RustcEncodable},
76 : : {"Clone", BuiltinMacro::Clone},
77 : : {"Copy", BuiltinMacro::Copy},
78 : : {"Debug", BuiltinMacro::Debug},
79 : : {"Default", BuiltinMacro::Default},
80 : : {"Eq", BuiltinMacro::Eq},
81 : : {"PartialEq", BuiltinMacro::PartialEq},
82 : : {"Ord", BuiltinMacro::Ord},
83 : : {"PartialOrd", BuiltinMacro::PartialOrd},
84 : : {"Hash", BuiltinMacro::Hash},
85 : :
86 : : }};
87 : :
88 : : AST::MacroTranscriberFunc
89 : 7436 : format_args_maker (AST::FormatArgs::Newline nl)
90 : : {
91 : 7437 : return [nl] (location_t loc, AST::MacroInvocData &invoc) {
92 : 1 : return MacroBuiltin::format_args_handler (loc, invoc, nl);
93 : 7436 : };
94 : : }
95 : :
96 : : std::unordered_map<std::string, AST::MacroTranscriberFunc>
97 : : MacroBuiltin::builtin_transcribers = {
98 : : {"assert", MacroBuiltin::assert_handler},
99 : : {"file", MacroBuiltin::file_handler},
100 : : {"line", MacroBuiltin::line_handler},
101 : : {"column", MacroBuiltin::column_handler},
102 : : {"include_bytes", MacroBuiltin::include_bytes_handler},
103 : : {"include_str", MacroBuiltin::include_str_handler},
104 : : {"stringify", MacroBuiltin::stringify_handler},
105 : : {"compile_error", MacroBuiltin::compile_error_handler},
106 : : {"concat", MacroBuiltin::concat_handler},
107 : : {"env", MacroBuiltin::env_handler},
108 : : {"cfg", MacroBuiltin::cfg_handler},
109 : : {"include", MacroBuiltin::include_handler},
110 : : {"format_args", format_args_maker (AST::FormatArgs::Newline::No)},
111 : : {"format_args_nl", format_args_maker (AST::FormatArgs::Newline::Yes)},
112 : : /* Unimplemented macro builtins */
113 : : {"option_env", MacroBuiltin::sorry},
114 : : {"concat_idents", MacroBuiltin::sorry},
115 : : {"module_path", MacroBuiltin::sorry},
116 : : {"asm", MacroBuiltin::sorry},
117 : : {"llvm_asm", MacroBuiltin::sorry},
118 : : {"global_asm", MacroBuiltin::sorry},
119 : : {"log_syntax", MacroBuiltin::sorry},
120 : : {"trace_macros", MacroBuiltin::sorry},
121 : : {"test", MacroBuiltin::sorry},
122 : : {"bench", MacroBuiltin::sorry},
123 : : {"test_case", MacroBuiltin::sorry},
124 : : {"global_allocator", MacroBuiltin::sorry},
125 : : {"cfg_accessible", MacroBuiltin::sorry},
126 : : /* Derive builtins do not need a real transcriber, but still need one. It
127 : : should however never be called since builtin derive macros get expanded
128 : : differently, and benefit from knowing on what kind of items they are
129 : : applied (struct, enums, unions) rather than receiving a list of tokens
130 : : like regular builtin macros */
131 : : {"RustcEncodable", MacroBuiltin::proc_macro_builtin},
132 : : {"RustcDecodable", MacroBuiltin::proc_macro_builtin},
133 : : {"Clone", MacroBuiltin::proc_macro_builtin},
134 : : {"Copy", MacroBuiltin::proc_macro_builtin},
135 : : {"Debug", MacroBuiltin::proc_macro_builtin},
136 : : {"Default", MacroBuiltin::proc_macro_builtin},
137 : : {"Eq", MacroBuiltin::proc_macro_builtin},
138 : : {"PartialEq", MacroBuiltin::proc_macro_builtin},
139 : : {"Ord", MacroBuiltin::proc_macro_builtin},
140 : : {"PartialOrd", MacroBuiltin::proc_macro_builtin},
141 : : {"Hash", MacroBuiltin::proc_macro_builtin},
142 : : };
143 : :
144 : : tl::optional<BuiltinMacro>
145 : 218 : builtin_macro_from_string (const std::string &identifier)
146 : : {
147 : 218 : auto macro = MacroBuiltin::builtins.lookup (identifier);
148 : 218 : rust_assert (macro.has_value ());
149 : :
150 : 218 : return macro;
151 : : }
152 : :
153 : : tl::optional<AST::Fragment>
154 : 0 : MacroBuiltin::sorry (location_t invoc_locus, AST::MacroInvocData &invoc)
155 : : {
156 : 0 : rust_sorry_at (invoc_locus, "unimplemented builtin macro: %qs",
157 : : invoc.get_path ().as_string ().c_str ());
158 : :
159 : 0 : return AST::Fragment::create_error ();
160 : : }
161 : :
162 : : tl::optional<AST::Fragment>
163 : 1 : MacroBuiltin::proc_macro_builtin (location_t invoc_locus,
164 : : AST::MacroInvocData &invoc)
165 : : {
166 : 1 : rust_error_at (invoc_locus, "cannot invoke derive macro: %qs",
167 : 1 : invoc.get_path ().as_string ().c_str ());
168 : :
169 : 1 : return AST::Fragment::create_error ();
170 : : }
171 : :
172 : : } // namespace Rust
|