Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 "rust-session-manager.h"
20 : : #include "rust-collect-lang-items.h"
21 : : #include "rust-desugar-for-loops.h"
22 : : #include "rust-desugar-question-mark.h"
23 : : #include "rust-desugar-apit.h"
24 : : #include "rust-diagnostics.h"
25 : : #include "rust-expression-yeast.h"
26 : : #include "rust-hir-pattern-analysis.h"
27 : : #include "rust-immutable-name-resolution-context.h"
28 : : #include "rust-unsafe-checker.h"
29 : : #include "rust-lex.h"
30 : : #include "rust-parse.h"
31 : : #include "rust-macro-expand.h"
32 : : #include "rust-ast-lower.h"
33 : : #include "rust-hir-type-check.h"
34 : : #include "rust-privacy-check.h"
35 : : #include "rust-const-checker.h"
36 : : #include "rust-feature-gate.h"
37 : : #include "rust-compile.h"
38 : : #include "rust-cfg-parser.h"
39 : : #include "rust-lint-scan-deadcode.h"
40 : : #include "rust-lint-unused-var.h"
41 : : #include "rust-readonly-check.h"
42 : : #include "rust-hir-dump.h"
43 : : #include "rust-ast-dump.h"
44 : : #include "rust-export-metadata.h"
45 : : #include "rust-imports.h"
46 : : #include "rust-extern-crate.h"
47 : : #include "rust-attributes.h"
48 : : #include "rust-name-resolution-context.h"
49 : : #include "rust-early-name-resolver-2.0.h"
50 : : #include "rust-late-name-resolver-2.0.h"
51 : : #include "rust-resolve-builtins.h"
52 : : #include "rust-cfg-strip.h"
53 : : #include "rust-expand-visitor.h"
54 : : #include "rust-unicode.h"
55 : : #include "rust-attribute-values.h"
56 : : #include "rust-borrow-checker.h"
57 : : #include "rust-ast-validation.h"
58 : : #include "rust-tyty-variance-analysis.h"
59 : :
60 : : #include "input.h"
61 : : #include "selftest.h"
62 : : #include "tm.h"
63 : : #include "rust-target.h"
64 : :
65 : : extern bool saw_errors (void);
66 : :
67 : : extern Linemap *rust_get_linemap ();
68 : :
69 : : namespace Rust {
70 : :
71 : : const char *kLexDumpFile = "gccrs.lex.dump";
72 : : const char *kASTDumpFile = "gccrs.ast.dump";
73 : : const char *kASTPrettyDumpFile = "gccrs.ast-pretty.dump";
74 : : const char *kASTPrettyDumpFileExpanded = "gccrs.ast-pretty-expanded.dump";
75 : : const char *kASTExpandedDumpFile = "gccrs.ast-expanded.dump";
76 : : const char *kASTmacroResolutionDumpFile = "gccrs.ast-macro-resolution.dump";
77 : : const char *kASTlabelResolutionDumpFile = "gccrs.ast-label-resolution.dump";
78 : : const char *kASTtypeResolutionDumpFile = "gccrs.ast-type-resolution.dump";
79 : : const char *kASTvalueResolutionDumpFile = "gccrs.ast-value-resolution.dump";
80 : : const char *kHIRDumpFile = "gccrs.hir.dump";
81 : : const char *kHIRPrettyDumpFile = "gccrs.hir-pretty.dump";
82 : : const char *kHIRTypeResolutionDumpFile = "gccrs.type-resolution.dump";
83 : : const char *kTargetOptionsDumpFile = "gccrs.target-options.dump";
84 : :
85 : : const std::string kDefaultCrateName = "rust_out";
86 : : const size_t kMaxNameLength = 64;
87 : :
88 : : Session &
89 : 129534630 : Session::get_instance ()
90 : : {
91 : 129539199 : static Session instance{};
92 : 129534630 : return instance;
93 : : }
94 : :
95 : : static std::string
96 : 4552 : infer_crate_name (const std::string &filename)
97 : :
98 : : {
99 : 4552 : if (filename == "-")
100 : 0 : return kDefaultCrateName;
101 : :
102 : 4552 : std::string crate = std::string (filename);
103 : 4552 : size_t path_sep = crate.find_last_of (file_separator);
104 : :
105 : : // find the base filename
106 : 4552 : if (path_sep != std::string::npos)
107 : 4548 : crate.erase (0, path_sep + 1);
108 : :
109 : : // find the file stem name (remove file extension)
110 : 4552 : size_t ext_position = crate.find_last_of ('.');
111 : 4552 : if (ext_position != std::string::npos)
112 : 4551 : crate.erase (ext_position);
113 : :
114 : : // Replace all the '-' symbols with '_' per Rust rules
115 : 59425 : for (auto &c : crate)
116 : : {
117 : 54873 : if (c == '-')
118 : 1661 : c = '_';
119 : : }
120 : 4552 : return crate;
121 : 4552 : }
122 : :
123 : : /* Validate the crate name using the ASCII rules */
124 : :
125 : : static bool
126 : 4581 : validate_crate_name (const std::string &crate_name, Error &error)
127 : : {
128 : 4581 : tl::optional<Utf8String> utf8_name_opt
129 : 4581 : = Utf8String::make_utf8_string (crate_name);
130 : 4581 : if (!utf8_name_opt.has_value ())
131 : : {
132 : 0 : error = Error (UNDEF_LOCATION, "crate name is not a valid UTF-8 string");
133 : 0 : return false;
134 : : }
135 : :
136 : 4581 : std::vector<Codepoint> uchars = utf8_name_opt->get_chars ();
137 : 4581 : if (uchars.empty ())
138 : : {
139 : 0 : error = Error (UNDEF_LOCATION, "crate name cannot be empty");
140 : 0 : return false;
141 : : }
142 : 4581 : if (uchars.size () > kMaxNameLength)
143 : : {
144 : 0 : error = Error (UNDEF_LOCATION, "crate name cannot exceed %lu characters",
145 : 0 : (unsigned long) kMaxNameLength);
146 : 0 : return false;
147 : : }
148 : 59630 : for (Codepoint &c : uchars)
149 : : {
150 : 55057 : if (!(is_alphabetic (c.value) || is_numeric (c.value) || c.value == '_'))
151 : : {
152 : 8 : error
153 : 8 : = Error (UNDEF_LOCATION, "invalid character %qs in crate name: %qs",
154 : 8 : c.as_string ().c_str (), crate_name.c_str ());
155 : 8 : return false;
156 : : }
157 : : }
158 : : return true;
159 : 4581 : }
160 : :
161 : : void
162 : 4568 : Session::init ()
163 : : {
164 : : // initialize target hooks
165 : 4568 : targetrustm.rust_cpu_info ();
166 : 4568 : targetrustm.rust_os_info ();
167 : :
168 : : // target-independent values that should exist in all targets
169 : 4568 : options.target_data.insert_key_value_pair ("target_pointer_width",
170 : 4568 : std::to_string (POINTER_SIZE));
171 : 4568 : options.target_data.insert_key_value_pair ("target_endian", BYTES_BIG_ENDIAN
172 : 4568 : ? "big"
173 : : : "little");
174 : :
175 : : // setup singleton linemap
176 : 4568 : linemap = rust_get_linemap ();
177 : :
178 : : // setup backend to GCC GIMPLE
179 : 4568 : Backend::init ();
180 : :
181 : : // setup mappings class
182 : 4568 : mappings = Analysis::Mappings::get ();
183 : 4568 : }
184 : :
185 : : /* Initialise default options. Actually called before handle_option, unlike init
186 : : * itself. */
187 : : void
188 : 4569 : Session::init_options ()
189 : 4569 : {}
190 : :
191 : : // Handle option selection.
192 : : bool
193 : 41299 : Session::handle_option (
194 : : enum opt_code code, const char *arg, HOST_WIDE_INT value ATTRIBUTE_UNUSED,
195 : : int kind ATTRIBUTE_UNUSED, location_t loc ATTRIBUTE_UNUSED,
196 : : const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
197 : : {
198 : : // used to store whether results of various stuff are successful
199 : 41299 : bool ret = true;
200 : :
201 : : // Handles options as listed in lang.opt.
202 : 41299 : switch (code)
203 : : {
204 : 31981 : case OPT_I:
205 : 31981 : case OPT_L:
206 : 31981 : {
207 : : // TODO: add search path
208 : 31981 : const std::string p = std::string (arg);
209 : 31981 : add_search_path (p);
210 : 31981 : }
211 : 31981 : break;
212 : :
213 : 0 : case OPT_frust_extern_:
214 : 0 : {
215 : 0 : std::string input (arg);
216 : 0 : ret = handle_extern_option (input);
217 : 0 : }
218 : 0 : break;
219 : 13 : case OPT_frust_crate_:
220 : : // set the crate name
221 : 13 : if (arg != nullptr)
222 : : {
223 : 13 : auto error = Error (UNDEF_LOCATION, std::string ());
224 : 13 : if ((ret = validate_crate_name (arg, error)))
225 : : {
226 : 12 : options.set_crate_name (arg);
227 : 12 : options.crate_name_set_manually = true;
228 : : }
229 : : else
230 : : {
231 : 1 : rust_assert (!error.message.empty ());
232 : 1 : error.emit ();
233 : : }
234 : 13 : }
235 : : else
236 : : ret = false;
237 : : break;
238 : :
239 : 0 : case OPT_frust_dump_:
240 : : // enable dump and return whether this was successful
241 : 0 : if (arg != nullptr)
242 : : {
243 : 0 : ret = enable_dump (std::string (arg));
244 : : }
245 : : else
246 : : {
247 : : ret = false;
248 : : }
249 : : break;
250 : :
251 : 2 : case OPT_frust_mangling_:
252 : 2 : Compile::Mangler::set_mangling (flag_rust_mangling);
253 : 2 : break;
254 : :
255 : 68 : case OPT_frust_cfg_:
256 : 68 : {
257 : 68 : auto string_arg = std::string (arg);
258 : 68 : ret = handle_cfg_option (string_arg);
259 : 68 : break;
260 : 68 : }
261 : 21 : case OPT_frust_crate_type_:
262 : 21 : options.set_crate_type (flag_rust_crate_type);
263 : 21 : break;
264 : 8 : case OPT_frust_edition_:
265 : 8 : options.set_edition (flag_rust_edition);
266 : 8 : break;
267 : 46 : case OPT_frust_compile_until_:
268 : 46 : options.set_compile_step (flag_rust_compile_until);
269 : 46 : break;
270 : 0 : case OPT_frust_metadata_output_:
271 : 0 : options.set_metadata_output (arg);
272 : 0 : break;
273 : 0 : case OPT_frust_panic_:
274 : 0 : options.set_panic_strategy (flag_rust_panic);
275 : 0 : break;
276 : :
277 : : default:
278 : : break;
279 : : }
280 : :
281 : 41299 : return ret;
282 : : }
283 : :
284 : : bool
285 : 0 : Session::handle_extern_option (std::string &input)
286 : : {
287 : 0 : auto pos = input.find ('=');
288 : 0 : if (std::string::npos == pos)
289 : : return false;
290 : :
291 : 0 : std::string libname = input.substr (0, pos);
292 : 0 : std::string path = input.substr (pos + 1);
293 : :
294 : 0 : extern_crates.insert ({libname, path});
295 : 0 : return true;
296 : 0 : }
297 : :
298 : : bool
299 : 68 : Session::handle_cfg_option (std::string &input)
300 : : {
301 : 68 : std::string key;
302 : 68 : std::string value;
303 : :
304 : : // Refactor this if needed
305 : 68 : if (!parse_cfg_option (input, key, value))
306 : : {
307 : 0 : rust_error_at (
308 : : UNDEF_LOCATION,
309 : : "invalid argument to %<-frust-cfg%>: Accepted formats are "
310 : : "%<-frust-cfg=key%> or %<-frust-cfg=key=\"value\"%> (quoted)");
311 : 0 : return false;
312 : : }
313 : :
314 : 68 : if (value.empty ())
315 : : // rustc does not seem to error on dup key
316 : 132 : options.target_data.insert_key (key);
317 : : else
318 : 6 : options.target_data.insert_key_value_pair (key, value);
319 : :
320 : : return true;
321 : 68 : }
322 : :
323 : : /* Enables a certain dump depending on the name passed in. Returns true if
324 : : * name is valid, false otherwise. */
325 : : bool
326 : 0 : Session::enable_dump (std::string arg)
327 : : {
328 : 0 : if (arg.empty ())
329 : : {
330 : 0 : rust_error_at (
331 : : UNDEF_LOCATION,
332 : : "dump option was not given a name. choose %<lex%>, %<ast-pretty%>, "
333 : : "%<register_plugins%>, %<injection%>, "
334 : : "%<expansion%>, %<resolution%>, %<target_options%>, %<hir%>, "
335 : : "%<hir-pretty%>, %<bir%> or %<all%>");
336 : 0 : return false;
337 : : }
338 : :
339 : 0 : if (arg == "all")
340 : : {
341 : 0 : options.enable_all_dump_options ();
342 : : }
343 : 0 : else if (arg == "lex")
344 : : {
345 : 0 : options.enable_dump_option (CompileOptions::LEXER_DUMP);
346 : : }
347 : 0 : else if (arg == "ast-pretty")
348 : : {
349 : 0 : options.enable_dump_option (CompileOptions::AST_DUMP_PRETTY);
350 : : }
351 : 0 : else if (arg == "register_plugins")
352 : : {
353 : 0 : options.enable_dump_option (CompileOptions::REGISTER_PLUGINS_DUMP);
354 : : }
355 : 0 : else if (arg == "injection")
356 : : {
357 : 0 : options.enable_dump_option (CompileOptions::INJECTION_DUMP);
358 : : }
359 : 0 : else if (arg == "expansion")
360 : : {
361 : 0 : options.enable_dump_option (CompileOptions::EXPANSION_DUMP);
362 : : }
363 : 0 : else if (arg == "resolution")
364 : : {
365 : 0 : options.enable_dump_option (CompileOptions::RESOLUTION_DUMP);
366 : : }
367 : 0 : else if (arg == "target_options")
368 : : {
369 : 0 : options.enable_dump_option (CompileOptions::TARGET_OPTION_DUMP);
370 : : }
371 : 0 : else if (arg == "hir")
372 : : {
373 : 0 : options.enable_dump_option (CompileOptions::HIR_DUMP);
374 : : }
375 : 0 : else if (arg == "hir-pretty")
376 : : {
377 : 0 : options.enable_dump_option (CompileOptions::HIR_DUMP_PRETTY);
378 : : }
379 : 0 : else if (arg == "bir")
380 : : {
381 : 0 : options.enable_dump_option (CompileOptions::BIR_DUMP);
382 : : }
383 : : else
384 : : {
385 : 0 : rust_error_at (
386 : : UNDEF_LOCATION,
387 : : "dump option %qs was unrecognised. choose %<lex%>, %<ast-pretty%>, "
388 : : "%<register_plugins%>, %<injection%>, "
389 : : "%<expansion%>, %<resolution%>, %<target_options%>, %<hir%>, "
390 : : "%<hir-pretty%>, or %<all%>",
391 : : arg.c_str ());
392 : 0 : return false;
393 : : }
394 : : return true;
395 : : }
396 : :
397 : : /* Actual main entry point for front-end. Called from langhook to parse files.
398 : : */
399 : : void
400 : 4568 : Session::handle_input_files (int num_files, const char **files)
401 : : {
402 : 4568 : if (num_files != 1)
403 : 0 : rust_fatal_error (UNDEF_LOCATION,
404 : : "only one file may be specified on the command line");
405 : :
406 : 4568 : const auto &file = files[0];
407 : :
408 : 4568 : rust_debug ("Attempting to parse file: %s", file);
409 : 4568 : compile_crate (file);
410 : 4566 : }
411 : :
412 : : void
413 : 4567 : Session::handle_crate_name (const char *filename,
414 : : const AST::Crate &parsed_crate)
415 : : {
416 : 4567 : auto &mappings = Analysis::Mappings::get ();
417 : 4567 : auto crate_name_found = false;
418 : 4567 : auto error = Error (UNDEF_LOCATION, std::string ());
419 : :
420 : 5481 : for (const auto &attr : parsed_crate.inner_attrs)
421 : : {
422 : 914 : if (attr.get_path () != "crate_name")
423 : 905 : continue;
424 : :
425 : 10 : auto msg_str = Analysis::Attributes::extract_string_literal (attr);
426 : 10 : if (!msg_str.has_value ())
427 : : {
428 : 0 : rust_error_at (attr.get_locus (),
429 : : "malformed %<crate_name%> attribute input");
430 : 0 : continue;
431 : : }
432 : :
433 : 10 : if (!validate_crate_name (*msg_str, error))
434 : : {
435 : 1 : error.locus = attr.get_locus ();
436 : 1 : error.emit ();
437 : 1 : continue;
438 : : }
439 : :
440 : 9 : if (options.crate_name_set_manually && (options.crate_name != *msg_str))
441 : : {
442 : 1 : rust_error_at (attr.get_locus (),
443 : : "%<-frust-crate-name%> and %<#[crate_name]%> are "
444 : : "required to match, but %qs does not match %qs",
445 : : options.crate_name.c_str (), msg_str->c_str ());
446 : : }
447 : 9 : crate_name_found = true;
448 : 18 : options.set_crate_name (*msg_str);
449 : 10 : }
450 : :
451 : 4567 : options.crate_name_set_manually |= crate_name_found;
452 : 4567 : if (!options.crate_name_set_manually)
453 : : {
454 : 4547 : auto crate_name = infer_crate_name (filename);
455 : 4547 : if (crate_name.empty ())
456 : : {
457 : 0 : rust_error_at (UNDEF_LOCATION, "crate name is empty");
458 : 0 : rust_inform (linemap_position_for_column (line_table, 0),
459 : : "crate name inferred from this file");
460 : 0 : return;
461 : : }
462 : :
463 : 4547 : rust_debug ("inferred crate name: %s", crate_name.c_str ());
464 : 9094 : options.set_crate_name (crate_name);
465 : :
466 : 4547 : if (!validate_crate_name (options.get_crate_name (), error))
467 : : {
468 : 1 : error.emit ();
469 : 1 : rust_inform (linemap_position_for_column (line_table, 0),
470 : : "crate name inferred from this file");
471 : 1 : return;
472 : : }
473 : 4547 : }
474 : :
475 : 4566 : if (saw_errors ())
476 : : return;
477 : :
478 : 4474 : CrateNum crate_num = mappings.get_next_crate_num (options.get_crate_name ());
479 : 4474 : mappings.set_current_crate (crate_num);
480 : 4567 : }
481 : :
482 : : // Parses a single file with filename filename.
483 : : void
484 : 4568 : Session::compile_crate (const char *filename)
485 : : {
486 : 4568 : if (!flag_rust_experimental
487 : 4568 : && !std::getenv ("GCCRS_INCOMPLETE_AND_EXPERIMENTAL_COMPILER_DO_NOT_USE"))
488 : 0 : rust_fatal_error (
489 : : UNDEF_LOCATION, "%s",
490 : : "gccrs is not yet able to compile Rust code "
491 : : "properly. Most of the errors produced will be the fault of gccrs and "
492 : : "not the crate you are trying to compile. Because of this, please report "
493 : : "errors directly to us instead of opening issues on said crate's "
494 : : "repository.\n\n"
495 : : "Our github repository: "
496 : : "https://github.com/rust-gcc/gccrs\nOur bugzilla tracker: "
497 : : "https://gcc.gnu.org/bugzilla/"
498 : : "buglist.cgi?bug_status=__open__&component=rust&product=gcc\n\n"
499 : : "If you understand this, and understand that the binaries produced might "
500 : : "not behave accordingly, you may attempt to use gccrs in an experimental "
501 : : "manner by passing the following flag:\n\n"
502 : : "`-frust-incomplete-and-experimental-compiler-do-not-use`\n\nor by "
503 : : "defining the following environment variable (any value will "
504 : : "do)\n\nGCCRS_INCOMPLETE_AND_EXPERIMENTAL_COMPILER_DO_NOT_USE\n\nFor "
505 : : "cargo-gccrs, this means passing\n\n"
506 : : "GCCRS_EXTRA_ARGS=\"-frust-incomplete-and-experimental-compiler-do-not-"
507 : : "use\"\n\nas an environment variable.");
508 : :
509 : 4568 : RAIIFile file_wrap (filename);
510 : 4568 : if (!file_wrap.ok ())
511 : : {
512 : 0 : rust_error_at (UNDEF_LOCATION, "cannot open filename %s: %m", filename);
513 : 0 : return;
514 : : }
515 : :
516 : 4568 : auto last_step = options.get_compile_until ();
517 : :
518 : : // parse file here
519 : : /* create lexer and parser - these are file-specific and so aren't instance
520 : : * variables */
521 : 4568 : tl::optional<std::ofstream &> dump_lex_opt = tl::nullopt;
522 : 4568 : std::ofstream dump_lex_stream;
523 : 4568 : if (options.dump_option_enabled (CompileOptions::LEXER_DUMP))
524 : : {
525 : 0 : dump_lex_stream.open (kLexDumpFile);
526 : 0 : if (dump_lex_stream.fail ())
527 : 0 : rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored",
528 : : kLexDumpFile);
529 : :
530 : 0 : dump_lex_opt = dump_lex_stream;
531 : : }
532 : :
533 : 4568 : Lexer lex (filename, std::move (file_wrap), linemap, dump_lex_opt);
534 : :
535 : 4568 : if (!lex.input_source_is_valid_utf8 ())
536 : : {
537 : 1 : rust_error_at (UNKNOWN_LOCATION,
538 : : "cannot read %s; stream did not contain valid UTF-8",
539 : : filename);
540 : 1 : return;
541 : : }
542 : :
543 : 4567 : Parser<Lexer> parser (lex);
544 : :
545 : : // generate crate from parser
546 : 4567 : std::unique_ptr<AST::Crate> ast_crate = parser.parse_crate ();
547 : :
548 : : // handle crate name
549 : 4567 : handle_crate_name (filename, *ast_crate.get ());
550 : :
551 : : // dump options except lexer dump
552 : 4567 : if (options.dump_option_enabled (CompileOptions::TARGET_OPTION_DUMP))
553 : : {
554 : 0 : options.target_data.dump_target_options ();
555 : : }
556 : :
557 : 4567 : if (saw_errors ())
558 : : return;
559 : :
560 : 4474 : if (options.dump_option_enabled (CompileOptions::AST_DUMP_PRETTY))
561 : : {
562 : 0 : dump_ast_pretty (*ast_crate.get ());
563 : : }
564 : :
565 : : // setup the mappings for this AST
566 : 4474 : CrateNum current_crate = mappings.get_current_crate ();
567 : 4474 : AST::Crate &parsed_crate
568 : 4474 : = mappings.insert_ast_crate (std::move (ast_crate), current_crate);
569 : :
570 : : /* basic pipeline:
571 : : * - lex
572 : : * - parse
573 : : * - register plugins (dummy stage for now) - attribute injection? what is
574 : : * this? (attribute injection is injecting attributes specified in command
575 : : * line into crate root)
576 : : * - injection (some lint checks or dummy, register builtin macros, crate
577 : : * injection)
578 : : * - expansion (expands all macros, maybe build test harness, AST
579 : : * validation, maybe macro crate)
580 : : * - resolution (name resolution, type resolution, maybe feature checking,
581 : : * maybe buffered lints)
582 : : * TODO not done */
583 : :
584 : 4474 : rust_debug ("\033[0;31mSUCCESSFULLY PARSED CRATE \033[0m");
585 : :
586 : : // If -fsyntax-only was passed, we can just skip the remaining passes.
587 : : // Parsing errors are already emitted in `parse_crate()`
588 : 4474 : if (flag_syntax_only || last_step == CompileOptions::CompileStep::Ast)
589 : : return;
590 : :
591 : : // register plugins pipeline stage
592 : 4449 : register_plugins (parsed_crate);
593 : 4449 : rust_debug ("\033[0;31mSUCCESSFULLY REGISTERED PLUGINS \033[0m");
594 : 4449 : if (options.dump_option_enabled (CompileOptions::REGISTER_PLUGINS_DUMP))
595 : : {
596 : : // TODO: what do I dump here?
597 : : }
598 : :
599 : : // injection pipeline stage
600 : 4449 : injection (parsed_crate);
601 : 4449 : rust_debug ("\033[0;31mSUCCESSFULLY FINISHED INJECTION \033[0m");
602 : 4449 : if (options.dump_option_enabled (CompileOptions::INJECTION_DUMP))
603 : : {
604 : : // TODO: what do I dump here? injected crate names?
605 : : }
606 : :
607 : 4449 : if (last_step == CompileOptions::CompileStep::AttributeCheck)
608 : : return;
609 : :
610 : 4449 : Analysis::AttributeChecker ().go (parsed_crate);
611 : :
612 : 4449 : if (last_step == CompileOptions::CompileStep::Expansion)
613 : : return;
614 : :
615 : 4449 : auto name_resolution_ctx = Resolver2_0::NameResolutionContext ();
616 : : // expansion pipeline stage
617 : :
618 : 4449 : expansion (parsed_crate, name_resolution_ctx);
619 : :
620 : 4449 : AST::CollectLangItems ().go (parsed_crate);
621 : :
622 : 4449 : rust_debug ("\033[0;31mSUCCESSFULLY FINISHED EXPANSION \033[0m");
623 : 4449 : if (options.dump_option_enabled (CompileOptions::EXPANSION_DUMP))
624 : : {
625 : : // dump AST with expanded stuff
626 : 0 : rust_debug ("BEGIN POST-EXPANSION AST DUMP");
627 : 0 : dump_ast_pretty (parsed_crate, true);
628 : 0 : rust_debug ("END POST-EXPANSION AST DUMP");
629 : : }
630 : :
631 : : // AST Validation pass
632 : 4449 : if (last_step == CompileOptions::CompileStep::ASTValidation)
633 : : return;
634 : :
635 : 4447 : ASTValidation ().check (parsed_crate);
636 : :
637 : : // feature gating
638 : 4447 : if (last_step == CompileOptions::CompileStep::FeatureGating)
639 : : return;
640 : 4447 : FeatureGate ().check (parsed_crate);
641 : :
642 : 4447 : if (last_step == CompileOptions::CompileStep::NameResolution)
643 : : return;
644 : :
645 : : // resolution pipeline stage
646 : 4444 : Resolver2_0::Late (name_resolution_ctx).go (parsed_crate);
647 : :
648 : 4443 : if (options.dump_option_enabled (CompileOptions::RESOLUTION_DUMP))
649 : 0 : dump_name_resolution (name_resolution_ctx);
650 : :
651 : 4443 : if (saw_errors ())
652 : : return;
653 : :
654 : 4285 : if (last_step == CompileOptions::CompileStep::Lowering)
655 : : return;
656 : :
657 : : // lower AST to HIR
658 : 4275 : std::unique_ptr<HIR::Crate> lowered
659 : 4275 : = HIR::ASTLowering::Resolve (parsed_crate);
660 : 4275 : if (saw_errors ())
661 : : return;
662 : :
663 : : // add the mappings to it
664 : 4266 : HIR::Crate &hir = mappings.insert_hir_crate (std::move (lowered));
665 : 4266 : if (options.dump_option_enabled (CompileOptions::HIR_DUMP))
666 : : {
667 : 0 : dump_hir (hir);
668 : : }
669 : 4266 : if (options.dump_option_enabled (CompileOptions::HIR_DUMP_PRETTY))
670 : : {
671 : 0 : dump_hir_pretty (hir);
672 : : }
673 : :
674 : 4266 : if (last_step == CompileOptions::CompileStep::TypeCheck)
675 : : return;
676 : :
677 : : // name resolution is done, we now freeze the name resolver for type checking
678 : 4260 : Resolver2_0::ImmutableNameResolutionContext::init (name_resolution_ctx);
679 : :
680 : : // type resolve
681 : 4260 : Compile::Context *ctx = Compile::Context::get ();
682 : 4260 : Resolver::TypeResolution::Resolve (hir);
683 : :
684 : 4259 : Resolver::TypeCheckContext::get ()->get_variance_analysis_ctx ().solve ();
685 : :
686 : 4259 : if (saw_errors ())
687 : : return;
688 : :
689 : 4084 : Analysis::PatternChecker ().go (hir);
690 : :
691 : 4084 : if (saw_errors ())
692 : : return;
693 : :
694 : 4081 : if (last_step == CompileOptions::CompileStep::Privacy)
695 : : return;
696 : :
697 : : // Various HIR error passes. The privacy pass happens before the unsafe checks
698 : 4081 : Privacy::Resolver::resolve (hir);
699 : 4081 : if (saw_errors ())
700 : : return;
701 : :
702 : 4071 : if (last_step == CompileOptions::CompileStep::Unsafety)
703 : : return;
704 : :
705 : 4070 : HIR::UnsafeChecker ().go (hir);
706 : :
707 : 4070 : if (last_step == CompileOptions::CompileStep::Const)
708 : : return;
709 : :
710 : 4070 : HIR::ConstChecker ().go (hir);
711 : :
712 : 4070 : if (last_step == CompileOptions::CompileStep::BorrowCheck)
713 : : return;
714 : :
715 : 4070 : if (flag_borrowcheck)
716 : : {
717 : 11 : const bool dump_bir
718 : 11 : = options.dump_option_enabled (CompileOptions::DumpOption::BIR_DUMP);
719 : 11 : HIR::BorrowChecker (dump_bir).go (hir);
720 : : }
721 : :
722 : 4070 : if (saw_errors ())
723 : : return;
724 : :
725 : 4034 : if (last_step == CompileOptions::CompileStep::Compilation)
726 : : return;
727 : :
728 : : // do compile to gcc generic
729 : 4032 : Compile::CompileCrate::Compile (hir, ctx);
730 : :
731 : : // we can't do static analysis if there are errors to worry about
732 : 4032 : if (!saw_errors ())
733 : : {
734 : : // lints
735 : 3990 : Analysis::ScanDeadcode::Scan (hir);
736 : 3990 : Analysis::UnusedVariables::Lint (*ctx);
737 : 3990 : HIR::ReadonlyChecker ().go (hir);
738 : :
739 : : // metadata
740 : 3990 : bool specified_emit_metadata
741 : 3990 : = flag_rust_embed_metadata || options.metadata_output_path_set ();
742 : 3990 : if (!specified_emit_metadata)
743 : : {
744 : 3990 : Metadata::PublicInterface::ExportTo (
745 : 7980 : hir, Metadata::PublicInterface::expected_metadata_filename ());
746 : : }
747 : : else
748 : : {
749 : 0 : if (flag_rust_embed_metadata)
750 : 0 : Metadata::PublicInterface::Export (hir);
751 : 0 : if (options.metadata_output_path_set ())
752 : 0 : Metadata::PublicInterface::ExportTo (
753 : : hir, options.get_metadata_output ());
754 : : }
755 : : }
756 : :
757 : 4032 : if (saw_errors ())
758 : : return;
759 : :
760 : : // pass to GCC middle-end
761 : 3986 : ctx->write_to_backend ();
762 : 9711 : }
763 : :
764 : : void
765 : 4449 : Session::register_plugins (AST::Crate &crate ATTRIBUTE_UNUSED)
766 : : {
767 : 4449 : rust_debug ("ran register_plugins (with no body)");
768 : 4449 : }
769 : :
770 : : // TODO: move somewhere else
771 : : bool
772 : 8895 : contains_name (const AST::AttrVec &attrs, std::string name)
773 : : {
774 : 10446 : for (const auto &attr : attrs)
775 : : {
776 : 1554 : if (attr.get_path () == name)
777 : 8895 : return true;
778 : : }
779 : :
780 : : return false;
781 : : }
782 : :
783 : : void
784 : 4449 : Session::injection (AST::Crate &crate)
785 : : {
786 : 4449 : rust_debug ("started injection");
787 : :
788 : : // lint checks in future maybe?
789 : :
790 : : // register builtin macros
791 : : /* In rustc, builtin macros are divided into 3 categories depending on use -
792 : : * "bang" macros, "attr" macros, and "derive" macros. I think the meanings
793 : : * of these categories should be fairly obvious to anyone who has used rust.
794 : : * Builtin macro list by category: Bang
795 : : * - asm
796 : : * - assert
797 : : * - cfg
798 : : * - column
799 : : * - compile_error
800 : : * - concat_idents
801 : : * - concat
802 : : * - env
803 : : * - file
804 : : * - format_args_nl
805 : : * - format_args
806 : : * - global_asm
807 : : * - include_bytes
808 : : * - include_str
809 : : * - include
810 : : * - line
811 : : * - log_syntax
812 : : * - module_path
813 : : * - option_env
814 : : * - stringify
815 : : * - trace_macros
816 : : * Attr
817 : : * - bench
818 : : * - global_allocator
819 : : * - test
820 : : * - test_case
821 : : * Derive
822 : : * - Clone
823 : : * - Copy
824 : : * - Debug
825 : : * - Default
826 : : * - Eq
827 : : * - Hash
828 : : * - Ord
829 : : * - PartialEq
830 : : * - PartialOrd
831 : : * - RustcDecodable
832 : : * - RustcEncodable
833 : : * rustc also has a "quote" macro that is defined differently and is
834 : : * supposedly not stable so eh. */
835 : : /* TODO: actually implement injection of these macros. In particular, derive
836 : : * macros, cfg, and test should be prioritised since they seem to be used
837 : : * the most. */
838 : :
839 : : // crate injection
840 : 4449 : std::vector<std::string> names;
841 : 4449 : if (contains_name (crate.inner_attrs, "no_core"))
842 : : {
843 : : // no prelude
844 : 3 : injected_crate_name = "";
845 : : }
846 : 4446 : else if (contains_name (crate.inner_attrs, "no_std"))
847 : : {
848 : 0 : names.push_back ("core");
849 : :
850 : 0 : if (!contains_name (crate.inner_attrs, "compiler_builtins"))
851 : : {
852 : 0 : names.push_back ("compiler_builtins");
853 : : }
854 : :
855 : 0 : injected_crate_name = "core";
856 : : }
857 : : else
858 : : {
859 : 4446 : names.push_back ("std");
860 : :
861 : 4446 : injected_crate_name = "std";
862 : : }
863 : :
864 : : // reverse iterate through names to insert crate items in "forward" order at
865 : : // beginning of crate
866 : 8895 : for (auto it = names.rbegin (); it != names.rend (); ++it)
867 : : {
868 : : // create "macro use" attribute for use on extern crate item to enable
869 : : // loading macros from it
870 : 4446 : AST::Attribute attr (AST::SimplePath::from_str (
871 : 4446 : Values::Attributes::MACRO_USE, UNDEF_LOCATION),
872 : 13338 : nullptr);
873 : :
874 : : // create "extern crate" item with the name
875 : 4446 : std::unique_ptr<AST::ExternCrate> extern_crate (
876 : 4446 : new AST::ExternCrate (*it, AST::Visibility::create_error (),
877 : 13338 : {std::move (attr)}, UNKNOWN_LOCATION));
878 : :
879 : : // insert at beginning
880 : : // crate.items.insert (crate.items.begin (), std::move (extern_crate));
881 : 8892 : }
882 : :
883 : : // create use tree path
884 : : // prelude is injected_crate_name
885 : : // FIXME: Once we do want to include the standard library, add the prelude
886 : : // use item
887 : : // std::vector<AST::SimplePathSegment> segments
888 : : // = {AST::SimplePathSegment (injected_crate_name, UNDEF_LOCATION),
889 : : // AST::SimplePathSegment ("prelude", UNDEF_LOCATION),
890 : : // AST::SimplePathSegment ("v1", UNDEF_LOCATION)};
891 : : // // create use tree and decl
892 : : // std::unique_ptr<AST::UseTreeGlob> use_tree (
893 : : // new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
894 : : // AST::SimplePath (std::move (segments)),
895 : : // UNDEF_LOCATION));
896 : : // AST::Attribute prelude_attr (AST::SimplePath::from_str ("prelude_import",
897 : : // UNDEF_LOCATION),
898 : : // nullptr);
899 : : // std::unique_ptr<AST::UseDeclaration> use_decl (
900 : : // new AST::UseDeclaration (std::move (use_tree),
901 : : // AST::Visibility::create_error (),
902 : : // {std::move (prelude_attr)}, UNDEF_LOCATION));
903 : :
904 : : // crate.items.insert (crate.items.begin (), std::move (use_decl));
905 : :
906 : : /* TODO: potentially add checking attribute crate type? I can't figure out
907 : : * what this does currently comment says "Unconditionally collect crate
908 : : * types from attributes to make them used", which presumably refers to
909 : : * checking the linkage info by "crate_type". It also seems to ensure that
910 : : * an invalid crate type is not specified, so maybe just do that. Valid
911 : : * crate types: bin lib dylib staticlib cdylib rlib proc-macro */
912 : :
913 : : // this crate type will have options affecting the metadata ouput
914 : :
915 : 4449 : rust_debug ("finished injection");
916 : 4449 : }
917 : :
918 : : void
919 : 4449 : Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx)
920 : : {
921 : 4449 : rust_debug ("started expansion");
922 : :
923 : : /* rustc has a modification to windows PATH temporarily here, which may end
924 : : * up being required */
925 : :
926 : : // create macro expansion config?
927 : : // if not, would at least have to configure recursion_limit
928 : 4449 : ExpansionCfg cfg;
929 : :
930 : 4449 : auto fixed_point_reached = false;
931 : 4449 : unsigned iterations = 0;
932 : :
933 : : // create extctxt? from parse session, cfg, and resolver?
934 : : /* expand by calling cxtctxt object's monotonic_expander's expand_crate
935 : : * method. */
936 : 4449 : MacroExpander expander (crate, cfg, *this);
937 : 4449 : std::vector<Error> macro_errors;
938 : :
939 : 4449 : Resolver2_0::Builtins::setup_lang_prelude (ctx);
940 : :
941 : 19154 : while (!fixed_point_reached && iterations < cfg.recursion_limit)
942 : : {
943 : 10340 : CfgStrip (cfg).go (crate);
944 : : // Errors might happen during cfg strip pass
945 : :
946 : 10340 : Resolver2_0::Early early (ctx);
947 : 10340 : early.go (crate);
948 : 10340 : macro_errors = early.get_macro_resolve_errors ();
949 : :
950 : 10340 : if (saw_errors ())
951 : : break;
952 : :
953 : 10288 : ExpandVisitor (expander).go (crate);
954 : :
955 : 10288 : fixed_point_reached = !expander.has_changed () && !early.is_dirty ();
956 : 10288 : expander.reset_changed_state ();
957 : 10288 : iterations++;
958 : :
959 : 10288 : if (saw_errors ())
960 : : break;
961 : 10340 : }
962 : :
963 : : // Fixed point reached: Emit unresolved macros error
964 : 4474 : for (auto &error : macro_errors)
965 : 25 : error.emit ();
966 : :
967 : 4449 : if (iterations == cfg.recursion_limit)
968 : : {
969 : 1 : auto &last_invoc = expander.get_last_invocation ();
970 : 1 : auto &last_def = expander.get_last_definition ();
971 : :
972 : 1 : rust_assert (last_def.has_value () && last_invoc.has_value ());
973 : :
974 : 1 : rich_location range (line_table, last_invoc->get_locus ());
975 : 1 : range.add_range (last_def->get_locus ());
976 : :
977 : 1 : rust_error_at (range, "reached recursion limit");
978 : 1 : }
979 : :
980 : : // handle AST desugaring
981 : 4449 : if (!saw_errors ())
982 : : {
983 : 4354 : AST::ExpressionYeast ().go (crate);
984 : :
985 : 4354 : AST::DesugarApit ().go (crate);
986 : :
987 : : // HACK: we may need a final TopLevel pass
988 : : // however, this should not count towards the recursion limit
989 : : // and we don't need a full Early pass
990 : 4354 : Resolver2_0::TopLevel (ctx).go (crate);
991 : : }
992 : :
993 : : // error reporting - check unused macros, get missing fragment specifiers
994 : :
995 : : // build test harness
996 : :
997 : : // ast validation (also with proc macro decls)
998 : :
999 : : // maybe create macro crate if not rustdoc
1000 : :
1001 : 4449 : rust_debug ("finished expansion");
1002 : 4449 : }
1003 : :
1004 : : void
1005 : 0 : Session::dump_ast_pretty (AST::Crate &crate, bool expanded) const
1006 : : {
1007 : 0 : std::ofstream out;
1008 : 0 : if (expanded)
1009 : 0 : out.open (kASTPrettyDumpFileExpanded);
1010 : : else
1011 : 0 : out.open (kASTPrettyDumpFile);
1012 : :
1013 : 0 : if (out.fail ())
1014 : : {
1015 : 0 : rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored",
1016 : : kASTDumpFile);
1017 : 0 : return;
1018 : : }
1019 : :
1020 : 0 : AST::Dump (out).go (crate);
1021 : :
1022 : 0 : out.close ();
1023 : 0 : }
1024 : :
1025 : : void
1026 : 0 : Session::dump_name_resolution (Resolver2_0::NameResolutionContext &ctx) const
1027 : : {
1028 : : // YES this is ugly but NO GCC 4.8 does not allow us to make it fancier :(
1029 : 0 : std::string types_content = ctx.types.as_debug_string ();
1030 : 0 : std::ofstream types_stream{kASTtypeResolutionDumpFile};
1031 : 0 : types_stream << types_content;
1032 : :
1033 : 0 : std::string macros_content = ctx.macros.as_debug_string ();
1034 : 0 : std::ofstream macros_stream{kASTmacroResolutionDumpFile};
1035 : 0 : macros_stream << macros_content;
1036 : :
1037 : 0 : std::string labels_content = ctx.labels.as_debug_string ();
1038 : 0 : std::ofstream labels_stream{kASTlabelResolutionDumpFile};
1039 : 0 : labels_stream << labels_content;
1040 : :
1041 : 0 : std::string values_content = ctx.values.as_debug_string ();
1042 : 0 : std::ofstream values_stream{kASTvalueResolutionDumpFile};
1043 : 0 : values_stream << values_content;
1044 : 0 : }
1045 : :
1046 : : void
1047 : 0 : Session::dump_hir (HIR::Crate &crate) const
1048 : : {
1049 : 0 : std::ofstream out;
1050 : 0 : out.open (kHIRDumpFile);
1051 : 0 : if (out.fail ())
1052 : : {
1053 : 0 : rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored",
1054 : : kHIRDumpFile);
1055 : 0 : return;
1056 : : }
1057 : :
1058 : 0 : out << crate.as_string ();
1059 : 0 : out.close ();
1060 : 0 : }
1061 : :
1062 : : void
1063 : 0 : Session::dump_hir_pretty (HIR::Crate &crate) const
1064 : : {
1065 : 0 : std::ofstream out;
1066 : 0 : out.open (kHIRPrettyDumpFile);
1067 : 0 : if (out.fail ())
1068 : : {
1069 : 0 : rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored",
1070 : : kHIRPrettyDumpFile);
1071 : 0 : return;
1072 : : }
1073 : :
1074 : 0 : HIR::Dump (out).go (crate);
1075 : 0 : out.close ();
1076 : 0 : }
1077 : :
1078 : : // imports
1079 : :
1080 : : NodeId
1081 : 51 : Session::load_extern_crate (const std::string &crate_name, location_t locus)
1082 : : {
1083 : : // has it already been loaded?
1084 : 51 : if (auto crate_num = mappings.lookup_crate_name (crate_name))
1085 : : {
1086 : 24 : auto resolved_node_id = mappings.crate_num_to_nodeid (*crate_num);
1087 : 24 : rust_assert (resolved_node_id);
1088 : :
1089 : 24 : return *resolved_node_id;
1090 : : }
1091 : :
1092 : 27 : std::string relative_import_path = "";
1093 : 27 : std::string import_name = crate_name;
1094 : :
1095 : : // The path to the extern crate might have been specified by the user using
1096 : : // -frust-extern
1097 : 27 : auto cli_extern_crate = extern_crates.find (crate_name);
1098 : :
1099 : 27 : std::pair<std::unique_ptr<Import::Stream>, std::vector<ProcMacro::Procmacro>>
1100 : 27 : package_result;
1101 : 27 : if (cli_extern_crate != extern_crates.end ())
1102 : : {
1103 : 0 : auto path = cli_extern_crate->second;
1104 : 0 : package_result = Import::try_package_in_directory (path, locus);
1105 : 0 : }
1106 : : else
1107 : : {
1108 : 27 : package_result
1109 : 54 : = Import::open_package (import_name, locus, relative_import_path);
1110 : : }
1111 : :
1112 : 27 : auto stream = std::move (package_result.first);
1113 : 27 : auto proc_macros = std::move (package_result.second);
1114 : :
1115 : 27 : if (stream == NULL // No stream and
1116 : 27 : && proc_macros.empty ()) // no proc macros
1117 : : {
1118 : 3 : rust_error_at (locus, "failed to locate crate %qs", import_name.c_str ());
1119 : 3 : return UNKNOWN_NODEID;
1120 : : }
1121 : :
1122 : 24 : auto extern_crate
1123 : 24 : = stream == nullptr
1124 : 24 : ? Imports::ExternCrate (crate_name,
1125 : 0 : proc_macros) // Import proc macros
1126 : 24 : : Imports::ExternCrate (*stream); // Import from stream
1127 : 24 : if (stream != nullptr)
1128 : : {
1129 : 24 : bool ok = extern_crate.load (locus);
1130 : 24 : if (!ok)
1131 : : {
1132 : 0 : rust_error_at (locus, "failed to load crate metadata");
1133 : 0 : return UNKNOWN_NODEID;
1134 : : }
1135 : : }
1136 : :
1137 : : // ensure the current vs this crate name don't collide
1138 : 24 : const std::string current_crate_name = mappings.get_current_crate_name ();
1139 : 24 : if (current_crate_name.compare (extern_crate.get_crate_name ()) == 0)
1140 : : {
1141 : 0 : rust_error_at (locus, "current crate name %qs collides with this",
1142 : : current_crate_name.c_str ());
1143 : 0 : return UNKNOWN_NODEID;
1144 : : }
1145 : :
1146 : : // setup mappings
1147 : 24 : CrateNum saved_crate_num = mappings.get_current_crate ();
1148 : 24 : CrateNum crate_num
1149 : 24 : = mappings.get_next_crate_num (extern_crate.get_crate_name ());
1150 : 24 : mappings.set_current_crate (crate_num);
1151 : :
1152 : : // then lets parse this as a 2nd crate
1153 : 24 : Lexer lex (extern_crate.get_metadata (), linemap);
1154 : 24 : Parser<Lexer> parser (lex);
1155 : 24 : std::unique_ptr<AST::Crate> metadata_crate = parser.parse_crate ();
1156 : :
1157 : 24 : AST::Crate &parsed_crate
1158 : 24 : = mappings.insert_ast_crate (std::move (metadata_crate), crate_num);
1159 : :
1160 : 24 : std::vector<AttributeProcMacro> attribute_macros;
1161 : 24 : std::vector<CustomDeriveProcMacro> derive_macros;
1162 : 24 : std::vector<BangProcMacro> bang_macros;
1163 : :
1164 : 24 : for (auto ¯o : extern_crate.get_proc_macros ())
1165 : : {
1166 : 0 : switch (macro.tag)
1167 : : {
1168 : 0 : case ProcMacro::CUSTOM_DERIVE:
1169 : 0 : derive_macros.push_back (macro.payload.custom_derive);
1170 : 0 : break;
1171 : 0 : case ProcMacro::ATTR:
1172 : 0 : attribute_macros.push_back (macro.payload.attribute);
1173 : 0 : break;
1174 : 0 : case ProcMacro::BANG:
1175 : 0 : bang_macros.push_back (macro.payload.bang);
1176 : 0 : break;
1177 : 0 : default:
1178 : 0 : gcc_unreachable ();
1179 : : }
1180 : : }
1181 : :
1182 : 24 : mappings.insert_attribute_proc_macros (crate_num, attribute_macros);
1183 : 24 : mappings.insert_bang_proc_macros (crate_num, bang_macros);
1184 : 24 : mappings.insert_derive_proc_macros (crate_num, derive_macros);
1185 : :
1186 : : // always restore the crate_num
1187 : 24 : mappings.set_current_crate (saved_crate_num);
1188 : :
1189 : 24 : return parsed_crate.get_node_id ();
1190 : 126 : }
1191 : : //
1192 : :
1193 : : void
1194 : 0 : TargetOptions::dump_target_options () const
1195 : : {
1196 : 0 : std::ofstream out;
1197 : 0 : out.open (kTargetOptionsDumpFile);
1198 : 0 : if (out.fail ())
1199 : : {
1200 : 0 : rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored",
1201 : : kTargetOptionsDumpFile);
1202 : 0 : return;
1203 : : }
1204 : :
1205 : 0 : if (features.empty ())
1206 : : {
1207 : 0 : out << "No target options available!\n";
1208 : : }
1209 : :
1210 : 0 : for (const auto &pairs : features)
1211 : : {
1212 : 0 : for (const auto &value : pairs.second)
1213 : : {
1214 : 0 : if (value.has_value ())
1215 : 0 : out << pairs.first + ": \"" + value.value () + "\"\n";
1216 : : else
1217 : 0 : out << pairs.first + "\n";
1218 : : }
1219 : : }
1220 : :
1221 : 0 : out.close ();
1222 : 0 : }
1223 : :
1224 : : void
1225 : 0 : TargetOptions::init_derived_values ()
1226 : : {
1227 : : // enable derived values based on target families
1228 : 0 : if (has_key_value_pair ("target_family", "unix"))
1229 : 0 : insert_key ("unix");
1230 : 0 : if (has_key_value_pair ("target_family", "windows"))
1231 : 0 : insert_key ("windows");
1232 : :
1233 : : // implicitly enable features - this should not be required in general
1234 : 0 : if (has_key_value_pair ("target_feature", "aes"))
1235 : 0 : enable_implicit_feature_reqs ("aes");
1236 : 0 : if (has_key_value_pair ("target_feature", "avx"))
1237 : 0 : enable_implicit_feature_reqs ("sse4.2");
1238 : 0 : if (has_key_value_pair ("target_feature", "avx2"))
1239 : 0 : enable_implicit_feature_reqs ("avx");
1240 : 0 : if (has_key_value_pair ("target_feature", "pclmulqdq"))
1241 : 0 : enable_implicit_feature_reqs ("sse2");
1242 : 0 : if (has_key_value_pair ("target_feature", "sha"))
1243 : 0 : enable_implicit_feature_reqs ("sse2");
1244 : 0 : if (has_key_value_pair ("target_feature", "sse2"))
1245 : 0 : enable_implicit_feature_reqs ("sse");
1246 : 0 : if (has_key_value_pair ("target_feature", "sse3"))
1247 : 0 : enable_implicit_feature_reqs ("sse2");
1248 : 0 : if (has_key_value_pair ("target_feature", "sse4.1"))
1249 : 0 : enable_implicit_feature_reqs ("sse3");
1250 : 0 : if (has_key_value_pair ("target_feature", "sse4.2"))
1251 : 0 : enable_implicit_feature_reqs ("sse4.1");
1252 : 0 : if (has_key_value_pair ("target_feature", "ssse3"))
1253 : 0 : enable_implicit_feature_reqs ("sse3");
1254 : 0 : }
1255 : :
1256 : : void
1257 : 0 : TargetOptions::enable_implicit_feature_reqs (std::string feature)
1258 : : {
1259 : 0 : if (feature == "aes")
1260 : 0 : enable_implicit_feature_reqs ("sse2");
1261 : 0 : else if (feature == "avx")
1262 : 0 : enable_implicit_feature_reqs ("sse4.2");
1263 : 0 : else if (feature == "avx2")
1264 : 0 : enable_implicit_feature_reqs ("avx");
1265 : 0 : else if (feature == "fma")
1266 : 0 : enable_implicit_feature_reqs ("avx");
1267 : 0 : else if (feature == "pclmulqdq")
1268 : 0 : enable_implicit_feature_reqs ("sse2");
1269 : 0 : else if (feature == "sha")
1270 : 0 : enable_implicit_feature_reqs ("sse2");
1271 : 0 : else if (feature == "sse2")
1272 : 0 : enable_implicit_feature_reqs ("sse");
1273 : 0 : else if (feature == "sse3")
1274 : 0 : enable_implicit_feature_reqs ("sse2");
1275 : 0 : else if (feature == "sse4.1")
1276 : 0 : enable_implicit_feature_reqs ("sse3");
1277 : 0 : else if (feature == "sse4.2")
1278 : 0 : enable_implicit_feature_reqs ("sse4.1");
1279 : 0 : else if (feature == "ssse3")
1280 : 0 : enable_implicit_feature_reqs ("sse3");
1281 : :
1282 : 0 : if (!has_key_value_pair ("target_feature", feature))
1283 : : {
1284 : 0 : insert_key_value_pair ("target_feature", feature);
1285 : :
1286 : 0 : rust_debug ("had to implicitly enable feature '%s'!", feature.c_str ());
1287 : : }
1288 : 0 : }
1289 : :
1290 : : // NOTEs:
1291 : : /* mrustc compile pipeline:
1292 : : * - target load (pass target spec to parser?)
1293 : : * - parse (convert source to AST)
1294 : : * - load crates (load any explicitly mentioned extern crates [not all of
1295 : : * them])
1296 : : * - expand (AST transformations from attributes and macros, loads remaining
1297 : : * extern crates [std/core and any triggered by macro expansion])
1298 : : * - implicit crates (test harness, allocator crate, panic crate)
1299 : : * - resolve use (annotate every 'use' item with source [supposedly handles
1300 : : * nasty recursion])
1301 : : * - resolve index (generate index of visible items for every module [avoids
1302 : : * recursion in next pass])
1303 : : * - resolve absolute (resolve all paths into either variable names
1304 : : * [types/values] or absolute paths)
1305 : : * - HIR lower (convert modified AST to simpler HIR [both expressions and
1306 : : * module tree])
1307 : : * - resolve type aliases (replace any usages of type aliases with actual
1308 : : * type [except associated types])
1309 : : * - resolve bind (iterate HIR tree and set binding annotations on all
1310 : : * concrete types [avoids path lookups later])
1311 : : * - resolve HIR markings (generate "markings" [e.g. for Copy/Send/Sync/...]
1312 : : * for all types
1313 : : * - sort impls (small pass - sort impls into groups)
1314 : : * - resolve UFCS outer (determine source trait for all top-level <T>::Type
1315 : : * [qualified] paths)
1316 : : * - resolve UFCS paths (do the same, but include for exprs this time. also
1317 : : * normalises results of previous pass [expanding known associated types])
1318 : : * - constant evaluate (evaluate all constants)
1319 : : * - typecheck outer (checks impls are sane)
1320 : : * - typecheck expressions (resolve and check types for all exprs)
1321 : : * - expand HIR annotate (annotate how exprs are used - used for closure
1322 : : * extractions and reborrows)
1323 : : * - expand HIR closures (extract closures into structs implementing Fn*
1324 : : * traits)
1325 : : * - expand HIR vtables (generate vtables for types with dyn dispatch)
1326 : : * - expand HIR calls (converts method and callable calls into explicit
1327 : : * function calls)
1328 : : * - expand HIR reborrows (apply reborrow rules [taking '&mut *v' instead of
1329 : : * 'v'])
1330 : : * - expand HIR erasedtype (replace all erased types 'impl Trait' with the
1331 : : * true type)
1332 : : * - typecheck expressions (validate - double check that previous passes
1333 : : * haven't broke type system rules)
1334 : : * - lower MIR (convert HIR exprs into a control-flow graph [MIR])
1335 : : * - MIR validate (check that the generated MIR is consistent)
1336 : : * - MIR cleanup (perform various transformations on MIR - replace reads of
1337 : : * const items with the item itself; convert casts to unsized types into
1338 : : * 'MakeDst' operations)
1339 : : * - MIR optimise (perform various simple optimisations on the MIR - constant
1340 : : * propagation, dead code elimination, borrow elimination, some inlining)
1341 : : * - MIR validate PO (re-validate the MIR)
1342 : : * - MIR validate full (optionally: perform expensive state-tracking
1343 : : * validation on MIR)
1344 : : * - trans enumerate (enumerate all items needed for code generation,
1345 : : * primarily types used for generics)
1346 : : * - trans auto impls (create magic trait impls as enumerated in previous
1347 : : * pass)
1348 : : * - trans monomorph (generate monomorphised copies of all functions [with
1349 : : * generics replaced with real types])
1350 : : * - MIR optimise inline (run optimisation again, this time with full type
1351 : : * info [primarily for inlining])
1352 : : * - HIR serialise (write out HIR dump [module tree and generic/inline MIR])
1353 : : * - trans codegen (generate final output file: emit C source file and call C
1354 : : * compiler) */
1355 : :
1356 : : /* rustc compile pipeline (basic, in way less detail):
1357 : : * - parse input (parse .rs to AST)
1358 : : * - name resolution, macro expansion, and configuration (process AST
1359 : : * recursively, resolving paths, expanding macros, processing #[cfg] nodes
1360 : : * [i.e. maybe stripping stuff from AST])
1361 : : * - lower to HIR
1362 : : * - type check and other analyses (e.g. privacy checking)
1363 : : * - lower to MIR and post-processing (and do stuff like borrow checking)
1364 : : * - translation to LLVM IR and LLVM optimisations (produce the .o files)
1365 : : * - linking (link together .o files) */
1366 : :
1367 : : /* Pierced-together rustc compile pipeline (from source):
1368 : : * - parse input (parse file to crate)
1369 : : * - register plugins (attributes injection, set various options, register
1370 : : * lints, load plugins)
1371 : : * - expansion/configure and expand (initial 'cfg' processing, 'loading
1372 : : * compiler plugins', syntax expansion, secondary 'cfg' expansion, synthesis
1373 : : * of a test harness if required, injection of any std lib dependency and
1374 : : * prelude, and name resolution) - actually documented inline
1375 : : * - seeming pierced-together order: pre-AST expansion lint checks,
1376 : : * registering builtin macros, crate injection, then expand all macros, then
1377 : : * maybe build test harness, AST validation, maybe create a macro crate (if
1378 : : * not rustdoc), name resolution, complete gated feature checking, add all
1379 : : * buffered lints
1380 : : * - create global context (lower to HIR)
1381 : : * - analysis on global context (HIR optimisations? create MIR?)
1382 : : * - code generation
1383 : : * - link */
1384 : : } // namespace Rust
1385 : :
1386 : : #if CHECKING_P
1387 : : namespace selftest {
1388 : : void
1389 : 1 : rust_crate_name_validation_test (void)
1390 : : {
1391 : 1 : auto error = Rust::Error (UNDEF_LOCATION, std::string ());
1392 : 1 : ASSERT_TRUE (Rust::validate_crate_name ("example", error));
1393 : 1 : ASSERT_TRUE (Rust::validate_crate_name ("abcdefg_1234", error));
1394 : 1 : ASSERT_TRUE (Rust::validate_crate_name ("1", error));
1395 : 1 : ASSERT_TRUE (Rust::validate_crate_name ("クレート", error));
1396 : 1 : ASSERT_TRUE (Rust::validate_crate_name ("Sōkrátēs", error));
1397 : 1 : ASSERT_TRUE (Rust::validate_crate_name ("惊吓", error));
1398 : :
1399 : : // NOTE: - is not allowed in the crate name ...
1400 : :
1401 : 1 : ASSERT_FALSE (Rust::validate_crate_name ("abcdefg-1234", error));
1402 : 1 : ASSERT_FALSE (Rust::validate_crate_name ("a+b", error));
1403 : 1 : ASSERT_FALSE (Rust::validate_crate_name ("/a+b/", error));
1404 : 1 : ASSERT_FALSE (Rust::validate_crate_name ("😸++", error));
1405 : 1 : ASSERT_FALSE (Rust::validate_crate_name ("∀", error));
1406 : :
1407 : : /* Tests for crate name inference */
1408 : 1 : ASSERT_EQ (Rust::infer_crate_name (".rs"), "");
1409 : 1 : ASSERT_EQ (Rust::infer_crate_name ("c.rs"), "c");
1410 : : // NOTE: ... but - is allowed when in the filename
1411 : 1 : ASSERT_EQ (Rust::infer_crate_name ("a-b.rs"), "a_b");
1412 : 1 : ASSERT_EQ (Rust::infer_crate_name ("book.rs.txt"), "book.rs");
1413 : : #if defined(HAVE_DOS_BASED_FILE_SYSTEM)
1414 : : ASSERT_EQ (Rust::infer_crate_name ("a\\c\\a-b.rs"), "a_b");
1415 : : #else
1416 : 1 : ASSERT_EQ (Rust::infer_crate_name ("a/c/a-b.rs"), "a_b");
1417 : : #endif
1418 : 1 : }
1419 : : } // namespace selftest
1420 : : #endif // CHECKING_P
|