Line data Source code
1 : // Copyright (C) 2020-2026 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 : // #include "rust-session-manager.h"
19 :
20 : #ifndef RUST_SESSION_MANAGER_H
21 : #define RUST_SESSION_MANAGER_H
22 :
23 : #include "rust-linemap.h"
24 : #include "rust-backend.h"
25 : #include "rust-hir-map.h"
26 : #include "safe-ctype.h"
27 : #include "rust-name-resolution-context.h"
28 :
29 : #include "config.h"
30 : #include "rust-system.h"
31 : #include "coretypes.h"
32 : #include "options.h"
33 :
34 : #include "optional.h"
35 :
36 : namespace Rust {
37 : // parser forward decl
38 : template <typename ManagedTokenSource> class Parser;
39 : class Lexer;
40 : // crate forward decl
41 : namespace AST {
42 : struct Crate;
43 : }
44 : // crate forward decl
45 : namespace HIR {
46 : class Crate;
47 : }
48 :
49 : /* Data related to target, most useful for conditional compilation and
50 : * whatever. */
51 : struct TargetOptions
52 : {
53 : /* TODO: maybe make private and access through helpers to allow changes to
54 : * impl */
55 : std::unordered_map<std::string, std::unordered_set<tl::optional<std::string>>>
56 : features;
57 :
58 : enum class CrateType
59 : {
60 : BIN = 0,
61 : LIB,
62 : RLIB,
63 : DYLIB,
64 : CDYLIB,
65 : STATICLIB,
66 : PROC_MACRO
67 : } crate_type
68 : = CrateType::BIN;
69 :
70 : public:
71 21 : void set_crate_type (int raw_type)
72 : {
73 21 : crate_type = static_cast<CrateType> (raw_type);
74 : }
75 :
76 : const CrateType &get_crate_type () const { return crate_type; }
77 :
78 : // Returns whether a key is defined in the feature set.
79 320 : bool has_key (std::string key) const
80 : {
81 320 : auto it = features.find (key);
82 320 : return it != features.end ()
83 517 : && it->second.find (tl::nullopt) != it->second.end ();
84 : }
85 :
86 : // Returns whether a key exists with the given value in the feature set.
87 3766 : bool has_key_value_pair (std::string key, std::string value) const
88 : {
89 3766 : auto it = features.find (key);
90 3766 : if (it != features.end ())
91 : {
92 3738 : auto set = it->second;
93 7476 : auto it2 = set.find (value);
94 3738 : if (it2 != set.end ())
95 3704 : return true;
96 3738 : }
97 : return false;
98 : }
99 :
100 : /* Returns the singular value from the key, or if the key has multiple, an
101 : * empty string. */
102 : std::string get_singular_value (std::string key) const
103 : {
104 : auto it = features.find (key);
105 : if (it != features.end ())
106 : {
107 : auto set = it->second;
108 : if (set.size () == 1 && set.begin ()->has_value ())
109 : return set.begin ()->value ();
110 : }
111 : return "";
112 : }
113 :
114 : /* Returns all values associated with a key (including none), or an empty
115 : * set if no key is found. */
116 : std::unordered_set<std::string> get_values_for_key (std::string key) const
117 : {
118 : std::unordered_set<std::string> ret;
119 :
120 : auto it = features.find (key);
121 : if (it == features.end ())
122 : return {};
123 :
124 : for (auto &val : it->second)
125 : if (val.has_value ())
126 : ret.insert (val.value ());
127 :
128 : return ret;
129 : }
130 :
131 : /* Inserts a key (no value) into the feature set. This will do nothing if
132 : * the key already exists. This returns whether the insertion was successful
133 : * (i.e. whether key already existed). */
134 66 : bool insert_key (std::string key)
135 : {
136 66 : auto it = features.find (key);
137 :
138 66 : if (it == features.end ())
139 66 : it
140 66 : = features
141 66 : .insert (
142 132 : std::make_pair (std::move (key),
143 132 : std::unordered_set<tl::optional<std::string>> ()))
144 : .first;
145 :
146 66 : return it->second.insert (tl::nullopt).second;
147 : }
148 :
149 : // Inserts a key-value pair into the feature set.
150 51174 : void insert_key_value_pair (std::string key, std::string value)
151 : {
152 51174 : auto it = features.find (key);
153 :
154 51174 : if (it == features.end ())
155 37218 : it
156 37218 : = features
157 37218 : .insert (
158 74436 : std::make_pair (std::move (key),
159 74436 : std::unordered_set<tl::optional<std::string>> ()))
160 : .first;
161 :
162 51174 : it->second.insert (std::move (value));
163 51174 : }
164 :
165 : // Dump all target options to stderr.
166 : void dump_target_options () const;
167 :
168 : /* Creates derived values and implicit enables after all target info is
169 : * added (e.g. "unix"). */
170 : void init_derived_values ();
171 :
172 : /* Enables all requirements for the feature given, and will enable feature
173 : * itself if not enabled. */
174 : void enable_implicit_feature_reqs (std::string feature);
175 :
176 : /* According to reference, Rust uses either multi-map key-values or just
177 : * values (although values may be aliases for a key-value value). This seems
178 : * like overkill. Thus, depending on whether the attributes used in cfg are
179 : * fixed or not, I think I'll either put each non-multimap "key-value" as a
180 : * separate field and have the multimap "key-values" in a regular map for
181 : * that one key, or actually use a multimap.
182 : *
183 : * rustc itself uses a set of key-value tuples where the second tuple
184 : * element is optional. This gets rid of the requirement to make a
185 : * multi-map, I guess, but seems like it might make search slow (unless all
186 : * "is defined"-only ones have empty string as second element). */
187 : /* cfg attributes:
188 : * - target_arch: single value
189 : * - target_feature: multiple values possible
190 : * - target_os: single value
191 : * - target_family: single value (or no value?)
192 : * - unix: set when target_family = "unix"
193 : * - windows: set when target_family = "windows"
194 : * - if these are just syntactic sugar, then maybe have a separate set or
195 : * map for this kind of stuff
196 : * - target_env: set when needed for disambiguation about ABI - usually
197 : * empty string for GNU, complicated
198 : * - seems to be a single value (if any)
199 : * - target_endian: single value; "little" or "big"
200 : * - target_pointer_width: single value, "32" for 32-bit pointers, etc.
201 : * - target_vendor, single value
202 : * - test: set when testing is being done
203 : * - again, seems similar to a "is defined" rather than "is equal to" like
204 : * unix
205 : * - debug_assertions: seems to "is defined"
206 : * - proc_macro: no idea, bad docs. seems to be boolean, so maybe "is
207 : * defined"
208 : */
209 : };
210 :
211 : // Defines compiler options (e.g. dump, etc.).
212 : struct CompileOptions
213 : {
214 : enum DumpOption
215 : {
216 : LEXER_DUMP,
217 : AST_DUMP_PRETTY,
218 : REGISTER_PLUGINS_DUMP,
219 : INJECTION_DUMP,
220 : EXPANSION_DUMP,
221 : RESOLUTION_DUMP,
222 : TARGET_OPTION_DUMP,
223 : HIR_DUMP,
224 : HIR_DUMP_PRETTY,
225 : BIR_DUMP,
226 : INTERNAL_DUMP,
227 : };
228 :
229 : std::set<DumpOption> dump_options;
230 :
231 : /* List of node that is not print during the dump of the ast with internal
232 : * comment */
233 : std::set<std::string> excluded_node;
234 :
235 : /* configuration options - actually useful for conditional compilation and
236 : * whatever data related to target arch, features, os, family, env, endian,
237 : * pointer width, vendor */
238 : TargetOptions target_data;
239 : std::string crate_name;
240 : bool crate_name_set_manually = false;
241 : bool enable_test = false;
242 : bool debug_assertions = false;
243 : std::string metadata_output_path;
244 :
245 : /** Structure containing additional attributes to be injected within the
246 : * compiled crate from the command line instead of the source code.
247 : *
248 : * To make things a bit easier with rustc, we're trying to use the same
249 : * format accepted by -Zcrate-attr. This means the CLI accepts the text
250 : * content of the attribute and not text of the whole attribute itself.
251 : */
252 3 : struct CliAttributeContent
253 : {
254 1 : CliAttributeContent (std::string content, location_t locus)
255 2 : : content (content), locus (locus)
256 : {}
257 : /* Content of the attribute*/
258 : std::string content;
259 : location_t locus;
260 : };
261 : std::vector<CliAttributeContent> addional_attributes;
262 :
263 : enum class Edition
264 : {
265 : E2015 = 0,
266 : E2018,
267 : E2021,
268 : } edition
269 : = Edition::E2015;
270 :
271 : enum class CompileStep
272 : {
273 : Ast,
274 : AttributeCheck,
275 : Expansion,
276 : ASTValidation,
277 : FeatureGating,
278 : NameResolution,
279 : Lowering,
280 : TypeCheck,
281 : Privacy,
282 : Unsafety,
283 : Const,
284 : BorrowCheck,
285 : Compilation,
286 : End,
287 : } compile_until
288 : = CompileStep::End;
289 :
290 : enum class PanicStrategy
291 : {
292 : Unwind,
293 : Abort,
294 : } panic_strategy
295 : = PanicStrategy::Unwind;
296 :
297 45119 : bool dump_option_enabled (DumpOption option) const
298 : {
299 36259 : return dump_options.find (option) != dump_options.end ();
300 : }
301 :
302 0 : void enable_dump_option (DumpOption option) { dump_options.insert (option); }
303 :
304 0 : void enable_all_dump_options ()
305 : {
306 0 : enable_dump_option (DumpOption::LEXER_DUMP);
307 0 : enable_dump_option (DumpOption::AST_DUMP_PRETTY);
308 0 : enable_dump_option (DumpOption::REGISTER_PLUGINS_DUMP);
309 0 : enable_dump_option (DumpOption::INJECTION_DUMP);
310 0 : enable_dump_option (DumpOption::EXPANSION_DUMP);
311 0 : enable_dump_option (DumpOption::RESOLUTION_DUMP);
312 0 : enable_dump_option (DumpOption::TARGET_OPTION_DUMP);
313 0 : enable_dump_option (DumpOption::HIR_DUMP);
314 0 : enable_dump_option (DumpOption::HIR_DUMP_PRETTY);
315 0 : enable_dump_option (DumpOption::BIR_DUMP);
316 0 : enable_dump_option (DumpOption::INTERNAL_DUMP);
317 0 : }
318 :
319 0 : void add_excluded (std::string node)
320 : {
321 0 : rust_assert (!node.empty ());
322 0 : excluded_node.insert (node);
323 0 : }
324 :
325 0 : const std::set<std::string> get_excluded () const { return excluded_node; }
326 :
327 4652 : void set_crate_name (std::string name)
328 : {
329 4652 : rust_assert (!name.empty ());
330 :
331 4652 : crate_name = std::move (name);
332 4652 : }
333 :
334 9179 : const std::string &get_crate_name () const
335 : {
336 9179 : rust_assert (!crate_name.empty ());
337 9179 : return crate_name;
338 : }
339 :
340 8 : void set_edition (int raw_edition)
341 : {
342 8 : edition = static_cast<Edition> (raw_edition);
343 8 : }
344 :
345 : const Edition &get_edition () const { return edition; }
346 :
347 21 : void set_crate_type (int raw_type) { target_data.set_crate_type (raw_type); }
348 :
349 17058 : bool is_proc_macro () const
350 : {
351 17058 : return target_data.get_crate_type ()
352 : == TargetOptions::CrateType::PROC_MACRO;
353 : }
354 :
355 46 : void set_compile_step (int raw_step)
356 : {
357 46 : compile_until = static_cast<CompileStep> (raw_step);
358 46 : }
359 :
360 : const CompileStep &get_compile_until () const { return compile_until; }
361 :
362 0 : void set_panic_strategy (int strategy)
363 : {
364 0 : panic_strategy = static_cast<PanicStrategy> (strategy);
365 0 : }
366 :
367 : const PanicStrategy &get_panic_strategy () const { return panic_strategy; }
368 :
369 0 : void set_metadata_output (const std::string &path)
370 : {
371 0 : metadata_output_path = path;
372 : }
373 :
374 0 : const std::string &get_metadata_output () const
375 : {
376 0 : return metadata_output_path;
377 : }
378 :
379 4032 : bool metadata_output_path_set () const
380 : {
381 4032 : return !metadata_output_path.empty ();
382 : }
383 : };
384 :
385 : /* Defines a compiler session. This is for a single compiler invocation, so
386 : * potentially includes parsing multiple crates. */
387 : struct Session
388 : {
389 : CompileOptions options;
390 : /* This should really be in a per-crate storage area but it is wiped with
391 : * every file so eh. */
392 : std::string injected_crate_name;
393 : std::map<std::string, std::string> extern_crates;
394 :
395 : /* extra files get included during late stages of compilation (e.g. macro
396 : * expansion) */
397 : std::vector<std::string> extra_files;
398 :
399 : // backend linemap
400 : Linemap *linemap;
401 :
402 : // mappings
403 : Analysis::Mappings &mappings;
404 :
405 : public:
406 : /* Get a reference to the static session instance */
407 : static Session &get_instance ();
408 :
409 4653 : ~Session () = default;
410 :
411 : /* This initializes the compiler session. Corresponds to langhook
412 : * grs_langhook_init(). Note that this is called after option handling. */
413 : void init ();
414 :
415 : // delete those constructors so we don't access the singleton in any
416 : // other way than via `get_instance()`
417 : Session (Session const &) = delete;
418 : void operator= (Session const &) = delete;
419 :
420 : bool handle_option (enum opt_code code, const char *arg, HOST_WIDE_INT value,
421 : int kind, location_t loc,
422 : const struct cl_option_handlers *handlers);
423 : void handle_input_files (int num_files, const char **files);
424 : void init_options ();
425 : void handle_crate_name (const char *filename, const AST::Crate &parsed_crate);
426 :
427 : /* This function saves the filename data into the session manager using the
428 : * `move` semantics, and returns a C-style string referencing the input
429 : * std::string */
430 5 : inline const char *include_extra_file (std::string filename)
431 : {
432 5 : extra_files.push_back (std::move (filename));
433 5 : return extra_files.back ().c_str ();
434 : }
435 :
436 : NodeId load_extern_crate (const std::string &crate_name, location_t locus);
437 :
438 : private:
439 4653 : Session () : mappings (Analysis::Mappings::get ()) {}
440 : void compile_crate (const char *filename);
441 : bool enable_dump (std::string arg);
442 :
443 : void dump_lex (Parser<Lexer> &parser) const;
444 : void dump_ast_pretty (AST::Crate &crate, bool expanded = false) const;
445 : void dump_ast_pretty_internal (AST::Crate &crate) const;
446 : void dump_name_resolution (Resolver2_0::NameResolutionContext &ctx) const;
447 : void dump_hir (HIR::Crate &crate) const;
448 : void dump_hir_pretty (HIR::Crate &crate) const;
449 :
450 : void handle_excluded_node (std::string arg);
451 :
452 : // pipeline stages - TODO maybe move?
453 : /* Register plugins pipeline stage. TODO maybe move to another object?
454 : * Currently dummy stage. In future will handle attribute injection
455 : * (top-level inner attribute creation from command line arguments), setting
456 : * options maybe, registering lints maybe, loading plugins maybe. */
457 : void register_plugins (AST::Crate &crate);
458 :
459 : /* Injection pipeline stage. TODO maybe move to another object? Maybe have
460 : * some lint checks (in future, obviously), register builtin macros, crate
461 : * injection. */
462 : void injection (AST::Crate &crate, AST::AttrVec cli_attributes);
463 :
464 : /* Expansion pipeline stage. TODO maybe move to another object? Expands all
465 : * macros, maybe build test harness in future, AST validation, maybe create
466 : * macro crate (if not rustdoc).*/
467 : void expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx);
468 :
469 : // handle cfg_option
470 : bool handle_cfg_option (std::string &data);
471 :
472 : bool handle_extern_option (std::string &data);
473 : };
474 :
475 : } // namespace Rust
476 :
477 : #if CHECKING_P
478 : namespace selftest {
479 : extern void rust_crate_name_validation_test (void);
480 : }
481 : #endif // CHECKING_P
482 :
483 : #endif
|