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