1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #include "rust-session-manager.h"
20 #include "rust-diagnostics.h"
21 #include "rust-unsafe-checker.h"
23 #include "rust-parse.h"
24 #include "rust-macro-expand.h"
25 #include "rust-ast-resolve.h"
26 #include "rust-ast-lower.h"
27 #include "rust-hir-type-check.h"
28 #include "rust-privacy-check.h"
29 #include "rust-const-checker.h"
30 #include "rust-feature-gate.h"
31 #include "rust-tycheck-dump.h"
32 #include "rust-compile.h"
33 #include "rust-cfg-parser.h"
34 #include "rust-lint-scan-deadcode.h"
35 #include "rust-lint-unused-var.h"
36 #include "rust-hir-dump.h"
37 #include "rust-ast-dump.h"
38 #include "rust-export-metadata.h"
39 #include "rust-imports.h"
40 #include "rust-extern-crate.h"
41 #include "rust-attributes.h"
42 #include "rust-early-name-resolver.h"
44 #include "diagnostic.h"
48 #include "rust-target.h"
61 const char *kLexDumpFile
= "gccrs.lex.dump";
62 const char *kASTDumpFile
= "gccrs.ast.dump";
63 const char *kASTPrettyDumpFile
= "gccrs.ast-pretty.dump";
64 const char *kASTPrettyDumpFileExpanded
= "gccrs.ast-pretty-expanded.dump";
65 const char *kASTExpandedDumpFile
= "gccrs.ast-expanded.dump";
66 const char *kHIRDumpFile
= "gccrs.hir.dump";
67 const char *kHIRPrettyDumpFile
= "gccrs.hir-pretty.dump";
68 const char *kHIRTypeResolutionDumpFile
= "gccrs.type-resolution.dump";
69 const char *kTargetOptionsDumpFile
= "gccrs.target-options.dump";
71 const std::string kDefaultCrateName
= "rust_out";
72 const size_t kMaxNameLength
= 64;
75 Session::get_instance ()
77 static Session instance
;
82 infer_crate_name (const std::string
&filename
)
85 return kDefaultCrateName
;
87 std::string crate
= std::string (filename
);
88 size_t path_sep
= crate
.find_last_of (file_separator
);
90 // find the base filename
91 if (path_sep
!= std::string::npos
)
92 crate
.erase (0, path_sep
+ 1);
94 // find the file stem name (remove file extension)
95 size_t ext_position
= crate
.find_last_of ('.');
96 if (ext_position
!= std::string::npos
)
97 crate
.erase (ext_position
);
99 // Replace all the '-' symbols with '_' per Rust rules
100 for (auto &c
: crate
)
108 /* Validate the crate name using the ASCII rules
109 TODO: Support Unicode version of the rules */
112 validate_crate_name (const std::string
&crate_name
, Error
&error
)
114 if (crate_name
.empty ())
116 error
= Error (Location (), "crate name cannot be empty");
119 if (crate_name
.length () > kMaxNameLength
)
121 error
= Error (Location (), "crate name cannot exceed %lu characters",
122 (unsigned long) kMaxNameLength
);
125 for (auto &c
: crate_name
)
127 if (!(ISALNUM (c
) || c
== '_'))
129 error
= Error (Location (),
130 "invalid character %<%c%> in crate name: %<%s%>", c
,
131 crate_name
.c_str ());
141 // initialize target hooks
142 targetrustm
.rust_cpu_info ();
143 targetrustm
.rust_os_info ();
145 // target-independent values that should exist in all targets
146 options
.target_data
.insert_key_value_pair ("target_pointer_width",
147 std::to_string (POINTER_SIZE
));
148 options
.target_data
.insert_key_value_pair ("target_endian", BYTES_BIG_ENDIAN
152 // setup singleton linemap
153 linemap
= rust_get_linemap ();
155 // setup backend to GCC GIMPLE
156 backend
= rust_get_backend ();
158 // setup mappings class
159 mappings
= Analysis::Mappings::get ();
162 /* Initialise default options. Actually called before handle_option, unlike init
165 Session::init_options ()
168 // Handle option selection.
170 Session::handle_option (
171 enum opt_code code
, const char *arg
, HOST_WIDE_INT value ATTRIBUTE_UNUSED
,
172 int kind ATTRIBUTE_UNUSED
, location_t loc ATTRIBUTE_UNUSED
,
173 const struct cl_option_handlers
*handlers ATTRIBUTE_UNUSED
)
175 // used to store whether results of various stuff are successful
178 // Handles options as listed in lang.opt.
183 // TODO: add search path
184 const std::string p
= std::string (arg
);
189 case OPT_frust_crate_
:
190 // set the crate name
193 auto error
= Error (Location (), std::string ());
194 if ((ret
= validate_crate_name (arg
, error
)))
196 options
.set_crate_name (arg
);
197 options
.crate_name_set_manually
= true;
201 rust_assert (!error
.message
.empty ());
209 case OPT_frust_dump_
:
210 // enable dump and return whether this was successful
213 ret
= enable_dump (std::string (arg
));
221 case OPT_frust_mangling_
:
222 Compile::Mangler::set_mangling (flag_rust_mangling
);
225 case OPT_frust_cfg_
: {
226 auto string_arg
= std::string (arg
);
227 ret
= handle_cfg_option (string_arg
);
231 case OPT_frust_edition_
:
232 options
.set_edition (flag_rust_edition
);
234 case OPT_frust_compile_until_
:
235 options
.set_compile_step (flag_rust_compile_until
);
237 case OPT_frust_metadata_output_
:
238 options
.set_metadata_output (arg
);
249 Session::handle_cfg_option (std::string
&input
)
254 // Refactor this if needed
255 if (!parse_cfg_option (input
, key
, value
))
259 "invalid argument to %<-frust-cfg%>: Accepted formats are "
260 "%<-frust-cfg=key%> or %<-frust-cfg=key=\"value\"%> (quoted)");
265 // rustc does not seem to error on dup key
266 options
.target_data
.insert_key (key
);
268 options
.target_data
.insert_key_value_pair (key
, value
);
273 /* Enables a certain dump depending on the name passed in. Returns true if
274 * name is valid, false otherwise. */
276 Session::enable_dump (std::string arg
)
282 "dump option was not given a name. choose %<lex%>, %<parse%>, "
283 "%<register_plugins%>, %<injection%>, %<expansion%>, %<resolution%>,"
284 " %<target_options%>, %<hir%>, or %<all%>");
290 options
.enable_all_dump_options ();
292 else if (arg
== "lex")
294 options
.enable_dump_option (CompileOptions::LEXER_DUMP
);
296 else if (arg
== "parse")
298 options
.enable_dump_option (CompileOptions::PARSER_AST_DUMP
);
300 else if (arg
== "ast-pretty")
302 options
.enable_dump_option (CompileOptions::AST_DUMP_PRETTY
);
304 else if (arg
== "register_plugins")
306 options
.enable_dump_option (CompileOptions::REGISTER_PLUGINS_DUMP
);
308 else if (arg
== "injection")
310 options
.enable_dump_option (CompileOptions::INJECTION_DUMP
);
312 else if (arg
== "expansion")
314 options
.enable_dump_option (CompileOptions::EXPANSION_DUMP
);
316 else if (arg
== "resolution")
318 options
.enable_dump_option (CompileOptions::RESOLUTION_DUMP
);
320 else if (arg
== "target_options")
322 options
.enable_dump_option (CompileOptions::TARGET_OPTION_DUMP
);
324 else if (arg
== "hir")
326 options
.enable_dump_option (CompileOptions::HIR_DUMP
);
328 else if (arg
== "hir-pretty")
330 options
.enable_dump_option (CompileOptions::HIR_DUMP_PRETTY
);
336 "dump option %qs was unrecognised. choose %<lex%>, %<parse%>, "
337 "%<register_plugins%>, %<injection%>, %<expansion%>, %<resolution%>,"
338 " %<target_options%>, or %<hir%>",
345 /* Actual main entry point for front-end. Called from langhook to parse files.
348 Session::handle_input_files (int num_files
, const char **files
)
351 rust_fatal_error (Location (),
352 "only one file may be specified on the command line");
354 const auto &file
= files
[0];
356 if (options
.crate_name
.empty ())
362 auto crate_name
= infer_crate_name (filename
);
363 rust_debug ("inferred crate name: %s", crate_name
.c_str ());
364 // set the preliminary crate name here
365 // we will figure out the real crate name in `handle_crate_name`
366 options
.set_crate_name (crate_name
);
369 CrateNum crate_num
= mappings
->get_next_crate_num (options
.get_crate_name ());
370 mappings
->set_current_crate (crate_num
);
372 rust_debug ("Attempting to parse file: %s", file
);
373 compile_crate (file
);
377 Session::handle_crate_name (const AST::Crate
&parsed_crate
)
379 auto mappings
= Analysis::Mappings::get ();
380 auto crate_name_changed
= false;
381 auto error
= Error (Location (), std::string ());
383 for (const auto &attr
: parsed_crate
.inner_attrs
)
385 if (attr
.get_path () != "crate_name")
387 if (!attr
.has_attr_input ())
389 rust_error_at (attr
.get_locus (),
390 "%<crate_name%> accepts one argument");
395 = static_cast<AST::AttrInputLiteral
&> (attr
.get_attr_input ());
396 const auto &msg_str
= literal
.get_literal ().as_string ();
397 if (!validate_crate_name (msg_str
, error
))
399 error
.locus
= attr
.get_locus ();
404 auto options
= Session::get_instance ().options
;
405 if (options
.crate_name_set_manually
&& (options
.crate_name
!= msg_str
))
407 rust_error_at (attr
.get_locus (),
408 "%<-frust-crate-name%> and %<#[crate_name]%> are "
409 "required to match, but %qs does not match %qs",
410 options
.crate_name
.c_str (), msg_str
.c_str ());
412 crate_name_changed
= true;
413 options
.set_crate_name (msg_str
);
414 mappings
->set_crate_name (mappings
->get_current_crate (), msg_str
);
417 options
.crate_name_set_manually
|= crate_name_changed
;
418 if (!options
.crate_name_set_manually
419 && !validate_crate_name (options
.crate_name
, error
))
422 rust_inform (linemap
->get_location (0),
423 "crate name inferred from this file");
427 // Parses a single file with filename filename.
429 Session::compile_crate (const char *filename
)
431 if (!flag_rust_experimental
432 && !std::getenv ("GCCRS_INCOMPLETE_AND_EXPERIMENTAL_COMPILER_DO_NOT_USE"))
435 "gccrs is not yet able to compile Rust code "
436 "properly. Most of the errors produced will be gccrs' fault and not the "
437 "crate you are trying to compile. Because of this, please reports issues "
438 "to us directly instead of opening issues on said crate's "
439 "repository.\n\nOur github repository: "
440 "https://github.com/rust-gcc/gccrs\nOur bugzilla tracker: "
441 "https://gcc.gnu.org/bugzilla/"
442 "buglist.cgi?bug_status=__open__&component=rust&product=gcc\n\n"
443 "If you understand this, and understand that the binaries produced might "
444 "not behave accordingly, you may attempt to use gccrs in an experimental "
445 "manner by passing the following flag:\n\n"
446 "`-frust-incomplete-and-experimental-compiler-do-not-use`\n\nor by "
447 "defining the following environment variable (any value will "
448 "do)\n\nGCCRS_INCOMPLETE_AND_EXPERIMENTAL_COMPILER_DO_NOT_USE\n\nFor "
449 "cargo-gccrs, this means passing\n\n"
450 "GCCRS_EXTRA_ARGS=\"-frust-incomplete-and-experimental-compiler-do-not-"
451 "use\"\n\nas an environment variable.");
453 RAIIFile
file_wrap (filename
);
454 if (!file_wrap
.ok ())
456 rust_error_at (Location (), "cannot open filename %s: %m", filename
);
460 auto last_step
= options
.get_compile_until ();
463 /* create lexer and parser - these are file-specific and so aren't instance
465 Optional
<std::ofstream
&> dump_lex_opt
= Optional
<std::ofstream
&>::none ();
466 std::ofstream dump_lex_stream
;
467 if (options
.dump_option_enabled (CompileOptions::LEXER_DUMP
))
469 dump_lex_stream
.open (kLexDumpFile
);
470 if (dump_lex_stream
.fail ())
472 rust_error_at (Linemap::unknown_location (),
473 "cannot open %s:%m; ignored", kLexDumpFile
);
475 auto stream
= Optional
<std::ofstream
&>::some (dump_lex_stream
);
476 dump_lex_opt
= std::move (stream
);
479 Lexer
lex (filename
, std::move (file_wrap
), linemap
, dump_lex_opt
);
481 Parser
<Lexer
> parser (lex
);
483 // generate crate from parser
484 std::unique_ptr
<AST::Crate
> ast_crate
= parser
.parse_crate ();
487 handle_crate_name (*ast_crate
.get ());
489 // dump options except lexer dump
490 if (options
.dump_option_enabled (CompileOptions::PARSER_AST_DUMP
))
492 dump_ast (parser
, *ast_crate
.get ());
494 if (options
.dump_option_enabled (CompileOptions::AST_DUMP_PRETTY
))
496 dump_ast_pretty (*ast_crate
.get ());
498 if (options
.dump_option_enabled (CompileOptions::TARGET_OPTION_DUMP
))
500 options
.target_data
.dump_target_options ();
506 // setup the mappings for this AST
507 CrateNum current_crate
= mappings
->get_current_crate ();
508 AST::Crate
&parsed_crate
509 = mappings
->insert_ast_crate (std::move (ast_crate
), current_crate
);
514 * - register plugins (dummy stage for now) - attribute injection? what is
515 * this? (attribute injection is injecting attributes specified in command
516 * line into crate root)
517 * - injection (some lint checks or dummy, register builtin macros, crate
519 * - expansion (expands all macros, maybe build test harness, AST
520 * validation, maybe macro crate)
521 * - resolution (name resolution, type resolution, maybe feature checking,
522 * maybe buffered lints)
525 rust_debug ("\033[0;31mSUCCESSFULLY PARSED CRATE \033[0m");
527 // If -fsyntax-only was passed, we can just skip the remaining passes.
528 // Parsing errors are already emitted in `parse_crate()`
529 if (flag_syntax_only
|| last_step
== CompileOptions::CompileStep::Ast
)
532 // register plugins pipeline stage
533 register_plugins (parsed_crate
);
534 rust_debug ("\033[0;31mSUCCESSFULLY REGISTERED PLUGINS \033[0m");
535 if (options
.dump_option_enabled (CompileOptions::REGISTER_PLUGINS_DUMP
))
537 // TODO: what do I dump here?
540 // injection pipeline stage
541 injection (parsed_crate
);
542 rust_debug ("\033[0;31mSUCCESSFULLY FINISHED INJECTION \033[0m");
543 if (options
.dump_option_enabled (CompileOptions::INJECTION_DUMP
))
545 // TODO: what do I dump here? injected crate names?
548 if (last_step
== CompileOptions::CompileStep::AttributeCheck
)
551 Analysis::AttributeChecker ().go (parsed_crate
);
553 if (last_step
== CompileOptions::CompileStep::Expansion
)
556 // expansion pipeline stage
557 expansion (parsed_crate
);
558 rust_debug ("\033[0;31mSUCCESSFULLY FINISHED EXPANSION \033[0m");
559 if (options
.dump_option_enabled (CompileOptions::EXPANSION_DUMP
))
561 // dump AST with expanded stuff
562 rust_debug ("BEGIN POST-EXPANSION AST DUMP");
563 dump_ast_expanded (parser
, parsed_crate
);
564 dump_ast_pretty (parsed_crate
, true);
565 rust_debug ("END POST-EXPANSION AST DUMP");
569 FeatureGate ().check (parsed_crate
);
571 if (last_step
== CompileOptions::CompileStep::NameResolution
)
574 // resolution pipeline stage
575 Resolver::NameResolution::Resolve (parsed_crate
);
576 if (options
.dump_option_enabled (CompileOptions::RESOLUTION_DUMP
))
578 // TODO: what do I dump here? resolved names? AST with resolved names?
584 if (last_step
== CompileOptions::CompileStep::Lowering
)
588 std::unique_ptr
<HIR::Crate
> lowered
589 = HIR::ASTLowering::Resolve (parsed_crate
);
593 // add the mappings to it
594 HIR::Crate
&hir
= mappings
->insert_hir_crate (std::move (lowered
));
595 if (options
.dump_option_enabled (CompileOptions::HIR_DUMP
))
599 if (options
.dump_option_enabled (CompileOptions::HIR_DUMP_PRETTY
))
601 dump_hir_pretty (hir
);
604 if (last_step
== CompileOptions::CompileStep::TypeCheck
)
608 Resolver::TypeResolution::Resolve (hir
);
609 if (options
.dump_option_enabled (CompileOptions::TYPE_RESOLUTION_DUMP
))
611 dump_type_resolution (hir
);
617 if (last_step
== CompileOptions::CompileStep::Privacy
)
620 // Various HIR error passes. The privacy pass happens before the unsafe checks
621 Privacy::Resolver::resolve (hir
);
625 if (last_step
== CompileOptions::CompileStep::Unsafety
)
628 HIR::UnsafeChecker ().go (hir
);
630 if (last_step
== CompileOptions::CompileStep::Const
)
633 HIR::ConstChecker ().go (hir
);
638 if (last_step
== CompileOptions::CompileStep::Compilation
)
641 // do compile to gcc generic
642 Compile::Context
ctx (backend
);
643 Compile::CompileCrate::Compile (hir
, &ctx
);
645 // we can't do static analysis if there are errors to worry about
649 Analysis::ScanDeadcode::Scan (hir
);
650 Analysis::UnusedVariables::Lint (ctx
);
653 bool specified_emit_metadata
654 = flag_rust_embed_metadata
|| options
.metadata_output_path_set ();
655 if (!specified_emit_metadata
)
657 Metadata::PublicInterface::ExportTo (
658 hir
, Metadata::PublicInterface::expected_metadata_filename ());
662 if (flag_rust_embed_metadata
)
663 Metadata::PublicInterface::Export (hir
);
664 if (options
.metadata_output_path_set ())
665 Metadata::PublicInterface::ExportTo (
666 hir
, options
.get_metadata_output ());
670 // pass to GCC middle-end
671 ctx
.write_to_backend ();
675 Session::register_plugins (AST::Crate
&crate ATTRIBUTE_UNUSED
)
677 rust_debug ("ran register_plugins (with no body)");
680 // TODO: move somewhere else
682 contains_name (const AST::AttrVec
&attrs
, std::string name
)
684 for (const auto &attr
: attrs
)
686 if (attr
.get_path () == name
)
694 Session::injection (AST::Crate
&crate
)
696 rust_debug ("started injection");
698 // lint checks in future maybe?
700 // register builtin macros
701 /* In rustc, builtin macros are divided into 3 categories depending on use -
702 * "bang" macros, "attr" macros, and "derive" macros. I think the meanings
703 * of these categories should be fairly obvious to anyone who has used rust.
704 * Builtin macro list by category: Bang
743 * rustc also has a "quote" macro that is defined differently and is
744 * supposedly not stable so eh. */
745 /* TODO: actually implement injection of these macros. In particular, derive
746 * macros, cfg, and test should be prioritised since they seem to be used
750 std::vector
<std::string
> names
;
751 if (contains_name (crate
.inner_attrs
, "no_core"))
754 injected_crate_name
= "";
756 else if (contains_name (crate
.inner_attrs
, "no_std"))
758 names
.push_back ("core");
760 if (!contains_name (crate
.inner_attrs
, "compiler_builtins"))
762 names
.push_back ("compiler_builtins");
765 injected_crate_name
= "core";
769 names
.push_back ("std");
771 injected_crate_name
= "std";
774 // reverse iterate through names to insert crate items in "forward" order at
775 // beginning of crate
776 for (auto it
= names
.rbegin (); it
!= names
.rend (); ++it
)
778 // create "macro use" attribute for use on extern crate item to enable
779 // loading macros from it
780 AST::Attribute
attr (AST::SimplePath::from_str ("macro_use", Location ()),
783 // create "extern crate" item with the name
784 std::unique_ptr
<AST::ExternCrate
> extern_crate (
785 new AST::ExternCrate (*it
, AST::Visibility::create_error (),
787 Linemap::unknown_location ()));
789 // insert at beginning
790 // crate.items.insert (crate.items.begin (), std::move (extern_crate));
793 // create use tree path
794 // prelude is injected_crate_name
795 // FIXME: Once we do want to include the standard library, add the prelude
797 // std::vector<AST::SimplePathSegment> segments
798 // = {AST::SimplePathSegment (injected_crate_name, Location ()),
799 // AST::SimplePathSegment ("prelude", Location ()),
800 // AST::SimplePathSegment ("v1", Location ())};
801 // // create use tree and decl
802 // std::unique_ptr<AST::UseTreeGlob> use_tree (
803 // new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
804 // AST::SimplePath (std::move (segments)), Location ()));
805 // AST::Attribute prelude_attr (AST::SimplePath::from_str ("prelude_import",
808 // std::unique_ptr<AST::UseDeclaration> use_decl (
809 // new AST::UseDeclaration (std::move (use_tree),
810 // AST::Visibility::create_error (),
811 // {std::move (prelude_attr)}, Location ()));
813 // crate.items.insert (crate.items.begin (), std::move (use_decl));
815 /* TODO: potentially add checking attribute crate type? I can't figure out
816 * what this does currently comment says "Unconditionally collect crate
817 * types from attributes to make them used", which presumably refers to
818 * checking the linkage info by "crate_type". It also seems to ensure that
819 * an invalid crate type is not specified, so maybe just do that. Valid
820 * crate types: bin lib dylib staticlib cdylib rlib proc-macro */
822 // this crate type will have options affecting the metadata ouput
824 rust_debug ("finished injection");
828 Session::expansion (AST::Crate
&crate
)
830 rust_debug ("started expansion");
832 /* rustc has a modification to windows PATH temporarily here, which may end
833 * up being required */
835 // create macro expansion config?
836 // if not, would at least have to configure recursion_limit
839 auto fixed_point_reached
= false;
840 unsigned iterations
= 0;
842 // create extctxt? from parse session, cfg, and resolver?
843 /* expand by calling cxtctxt object's monotonic_expander's expand_crate
845 MacroExpander
expander (crate
, cfg
, *this);
847 while (!fixed_point_reached
&& iterations
< cfg
.recursion_limit
)
849 /* We need to name resolve macros and imports here */
850 Resolver::EarlyNameResolver ().go (crate
);
852 expander
.expand_crate ();
854 fixed_point_reached
= !expander
.has_changed ();
855 expander
.reset_changed_state ();
862 if (iterations
== cfg
.recursion_limit
)
864 auto last_invoc
= expander
.get_last_invocation ();
865 auto last_def
= expander
.get_last_definition ();
867 rust_assert (last_def
&& last_invoc
);
869 RichLocation
range (last_invoc
->get_locus ());
870 range
.add_range (last_def
->get_locus ());
872 rust_error_at (range
, "reached recursion limit");
875 // error reporting - check unused macros, get missing fragment specifiers
877 // build test harness
879 // ast validation (also with proc macro decls)
881 // maybe create macro crate if not rustdoc
883 rust_debug ("finished expansion");
887 Session::dump_ast (Parser
<Lexer
> &parser
, AST::Crate
&crate
) const
890 out
.open (kASTDumpFile
);
893 rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored",
898 parser
.debug_dump_ast_output (crate
, out
);
903 Session::dump_ast_pretty (AST::Crate
&crate
, bool expanded
) const
907 out
.open (kASTPrettyDumpFileExpanded
);
909 out
.open (kASTPrettyDumpFile
);
913 rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored",
918 AST::Dump (out
).go (crate
);
924 Session::dump_ast_expanded (Parser
<Lexer
> &parser
, AST::Crate
&crate
) const
927 out
.open (kASTExpandedDumpFile
);
930 rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored",
931 kASTExpandedDumpFile
);
935 parser
.debug_dump_ast_output (crate
, out
);
940 Session::dump_hir (HIR::Crate
&crate
) const
943 out
.open (kHIRDumpFile
);
946 rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored",
951 out
<< crate
.as_string ();
956 Session::dump_hir_pretty (HIR::Crate
&crate
) const
959 out
.open (kHIRPrettyDumpFile
);
962 rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored",
967 HIR::Dump (out
).go (crate
);
972 Session::dump_type_resolution (HIR::Crate
&hir
) const
975 out
.open (kHIRTypeResolutionDumpFile
);
978 rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored",
979 kHIRTypeResolutionDumpFile
);
983 Resolver::TypeResolverDump::go (hir
, out
);
990 Session::load_extern_crate (const std::string
&crate_name
, Location locus
)
992 // has it already been loaded?
993 CrateNum found_crate_num
= UNKNOWN_CREATENUM
;
994 bool found
= mappings
->lookup_crate_name (crate_name
, found_crate_num
);
997 NodeId resolved_node_id
= UNKNOWN_NODEID
;
999 = mappings
->crate_num_to_nodeid (found_crate_num
, resolved_node_id
);
1000 rust_assert (resolved
);
1002 return resolved_node_id
;
1005 std::string relative_import_path
= "";
1007 = Import::open_package (crate_name
, locus
, relative_import_path
);
1010 rust_error_at (locus
, "failed to locate crate %<%s%>",
1011 crate_name
.c_str ());
1012 return UNKNOWN_NODEID
;
1015 Imports::ExternCrate
extern_crate (*s
);
1016 bool ok
= extern_crate
.load (locus
);
1019 rust_error_at (locus
, "failed to load crate metadata");
1020 return UNKNOWN_NODEID
;
1023 // ensure the current vs this crate name don't collide
1024 const std::string current_crate_name
= mappings
->get_current_crate_name ();
1025 if (current_crate_name
.compare (extern_crate
.get_crate_name ()) == 0)
1027 rust_error_at (locus
, "current crate name %<%s%> collides with this",
1028 current_crate_name
.c_str ());
1029 return UNKNOWN_NODEID
;
1033 CrateNum saved_crate_num
= mappings
->get_current_crate ();
1035 = mappings
->get_next_crate_num (extern_crate
.get_crate_name ());
1036 mappings
->set_current_crate (crate_num
);
1038 // then lets parse this as a 2nd crate
1039 Lexer
lex (extern_crate
.get_metadata ());
1040 Parser
<Lexer
> parser (lex
);
1041 std::unique_ptr
<AST::Crate
> metadata_crate
= parser
.parse_crate ();
1042 AST::Crate
&parsed_crate
1043 = mappings
->insert_ast_crate (std::move (metadata_crate
), crate_num
);
1046 Resolver::NameResolution::Resolve (parsed_crate
);
1048 // perform hir lowering
1049 std::unique_ptr
<HIR::Crate
> lowered
1050 = HIR::ASTLowering::Resolve (parsed_crate
);
1051 HIR::Crate
&hir
= mappings
->insert_hir_crate (std::move (lowered
));
1053 // perform type resolution
1054 Resolver::TypeResolution::Resolve (hir
);
1056 // always restore the crate_num
1057 mappings
->set_current_crate (saved_crate_num
);
1059 return parsed_crate
.get_node_id ();
1064 TargetOptions::dump_target_options () const
1067 out
.open (kTargetOptionsDumpFile
);
1070 rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored",
1071 kTargetOptionsDumpFile
);
1075 if (features
.empty ())
1077 out
<< "No target options available!\n";
1080 for (const auto &pairs
: features
)
1082 for (const auto &value
: pairs
.second
)
1083 out
<< pairs
.first
+ ": \"" + value
+ "\"\n";
1085 if (pairs
.second
.empty ())
1086 out
<< pairs
.first
+ "\n";
1093 TargetOptions::init_derived_values ()
1095 // enable derived values based on target families
1096 if (has_key_value_pair ("target_family", "unix"))
1097 insert_key ("unix");
1098 if (has_key_value_pair ("target_family", "windows"))
1099 insert_key ("windows");
1101 // implicitly enable features - this should not be required in general
1102 if (has_key_value_pair ("target_feature", "aes"))
1103 enable_implicit_feature_reqs ("aes");
1104 if (has_key_value_pair ("target_feature", "avx"))
1105 enable_implicit_feature_reqs ("sse4.2");
1106 if (has_key_value_pair ("target_feature", "avx2"))
1107 enable_implicit_feature_reqs ("avx");
1108 if (has_key_value_pair ("target_feature", "pclmulqdq"))
1109 enable_implicit_feature_reqs ("sse2");
1110 if (has_key_value_pair ("target_feature", "sha"))
1111 enable_implicit_feature_reqs ("sse2");
1112 if (has_key_value_pair ("target_feature", "sse2"))
1113 enable_implicit_feature_reqs ("sse");
1114 if (has_key_value_pair ("target_feature", "sse3"))
1115 enable_implicit_feature_reqs ("sse2");
1116 if (has_key_value_pair ("target_feature", "sse4.1"))
1117 enable_implicit_feature_reqs ("sse3");
1118 if (has_key_value_pair ("target_feature", "sse4.2"))
1119 enable_implicit_feature_reqs ("sse4.1");
1120 if (has_key_value_pair ("target_feature", "ssse3"))
1121 enable_implicit_feature_reqs ("sse3");
1125 TargetOptions::enable_implicit_feature_reqs (std::string feature
)
1127 if (feature
== "aes")
1128 enable_implicit_feature_reqs ("sse2");
1129 else if (feature
== "avx")
1130 enable_implicit_feature_reqs ("sse4.2");
1131 else if (feature
== "avx2")
1132 enable_implicit_feature_reqs ("avx");
1133 else if (feature
== "fma")
1134 enable_implicit_feature_reqs ("avx");
1135 else if (feature
== "pclmulqdq")
1136 enable_implicit_feature_reqs ("sse2");
1137 else if (feature
== "sha")
1138 enable_implicit_feature_reqs ("sse2");
1139 else if (feature
== "sse2")
1140 enable_implicit_feature_reqs ("sse");
1141 else if (feature
== "sse3")
1142 enable_implicit_feature_reqs ("sse2");
1143 else if (feature
== "sse4.1")
1144 enable_implicit_feature_reqs ("sse3");
1145 else if (feature
== "sse4.2")
1146 enable_implicit_feature_reqs ("sse4.1");
1147 else if (feature
== "ssse3")
1148 enable_implicit_feature_reqs ("sse3");
1150 if (!has_key_value_pair ("target_feature", feature
))
1152 insert_key_value_pair ("target_feature", feature
);
1154 rust_debug ("had to implicitly enable feature '%s'!", feature
.c_str ());
1159 /* mrustc compile pipeline:
1160 * - target load (pass target spec to parser?)
1161 * - parse (convert source to AST)
1162 * - load crates (load any explicitly mentioned extern crates [not all of
1164 * - expand (AST transformations from attributes and macros, loads remaining
1165 * extern crates [std/core and any triggered by macro expansion])
1166 * - implicit crates (test harness, allocator crate, panic crate)
1167 * - resolve use (annotate every 'use' item with source [supposedly handles
1169 * - resolve index (generate index of visible items for every module [avoids
1170 * recursion in next pass])
1171 * - resolve absolute (resolve all paths into either variable names
1172 * [types/values] or absolute paths)
1173 * - HIR lower (convert modified AST to simpler HIR [both expressions and
1175 * - resolve type aliases (replace any usages of type aliases with actual
1176 * type [except associated types])
1177 * - resolve bind (iterate HIR tree and set binding annotations on all
1178 * concrete types [avoids path lookups later])
1179 * - resolve HIR markings (generate "markings" [e.g. for Copy/Send/Sync/...]
1181 * - sort impls (small pass - sort impls into groups)
1182 * - resolve UFCS outer (determine source trait for all top-level <T>::Type
1183 * [qualified] paths)
1184 * - resolve UFCS paths (do the same, but include for exprs this time. also
1185 * normalises results of previous pass [expanding known associated types])
1186 * - constant evaluate (evaluate all constants)
1187 * - typecheck outer (checks impls are sane)
1188 * - typecheck expressions (resolve and check types for all exprs)
1189 * - expand HIR annotate (annotate how exprs are used - used for closure
1190 * extractions and reborrows)
1191 * - expand HIR closures (extract closures into structs implementing Fn*
1193 * - expand HIR vtables (generate vtables for types with dyn dispatch)
1194 * - expand HIR calls (converts method and callable calls into explicit
1196 * - expand HIR reborrows (apply reborrow rules [taking '&mut *v' instead of
1198 * - expand HIR erasedtype (replace all erased types 'impl Trait' with the
1200 * - typecheck expressions (validate - double check that previous passes
1201 * haven't broke type system rules)
1202 * - lower MIR (convert HIR exprs into a control-flow graph [MIR])
1203 * - MIR validate (check that the generated MIR is consistent)
1204 * - MIR cleanup (perform various transformations on MIR - replace reads of
1205 * const items with the item itself; convert casts to unsized types into
1206 * 'MakeDst' operations)
1207 * - MIR optimise (perform various simple optimisations on the MIR - constant
1208 * propagation, dead code elimination, borrow elimination, some inlining)
1209 * - MIR validate PO (re-validate the MIR)
1210 * - MIR validate full (optionally: perform expensive state-tracking
1211 * validation on MIR)
1212 * - trans enumerate (enumerate all items needed for code generation,
1213 * primarily types used for generics)
1214 * - trans auto impls (create magic trait impls as enumerated in previous
1216 * - trans monomorph (generate monomorphised copies of all functions [with
1217 * generics replaced with real types])
1218 * - MIR optimise inline (run optimisation again, this time with full type
1219 * info [primarily for inlining])
1220 * - HIR serialise (write out HIR dump [module tree and generic/inline MIR])
1221 * - trans codegen (generate final output file: emit C source file and call C
1224 /* rustc compile pipeline (basic, in way less detail):
1225 * - parse input (parse .rs to AST)
1226 * - name resolution, macro expansion, and configuration (process AST
1227 * recursively, resolving paths, expanding macros, processing #[cfg] nodes
1228 * [i.e. maybe stripping stuff from AST])
1230 * - type check and other analyses (e.g. privacy checking)
1231 * - lower to MIR and post-processing (and do stuff like borrow checking)
1232 * - translation to LLVM IR and LLVM optimisations (produce the .o files)
1233 * - linking (link together .o files) */
1235 /* Pierced-together rustc compile pipeline (from source):
1236 * - parse input (parse file to crate)
1237 * - register plugins (attributes injection, set various options, register
1238 * lints, load plugins)
1239 * - expansion/configure and expand (initial 'cfg' processing, 'loading
1240 * compiler plugins', syntax expansion, secondary 'cfg' expansion, synthesis
1241 * of a test harness if required, injection of any std lib dependency and
1242 * prelude, and name resolution) - actually documented inline
1243 * - seeming pierced-together order: pre-AST expansion lint checks,
1244 * registering builtin macros, crate injection, then expand all macros, then
1245 * maybe build test harness, AST validation, maybe create a macro crate (if
1246 * not rustdoc), name resolution, complete gated feature checking, add all
1248 * - create global context (lower to HIR)
1249 * - analysis on global context (HIR optimisations? create MIR?)
1255 namespace selftest
{
1257 rust_crate_name_validation_test (void)
1259 auto error
= Rust::Error (Location (), std::string ());
1260 ASSERT_TRUE (Rust::validate_crate_name ("example", error
));
1261 ASSERT_TRUE (Rust::validate_crate_name ("abcdefg_1234", error
));
1262 ASSERT_TRUE (Rust::validate_crate_name ("1", error
));
1263 // FIXME: The next test does not pass as of current implementation
1264 // ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("惊吓"));
1265 // NOTE: - is not allowed in the crate name ...
1267 ASSERT_FALSE (Rust::validate_crate_name ("abcdefg-1234", error
));
1268 ASSERT_FALSE (Rust::validate_crate_name ("a+b", error
));
1269 ASSERT_FALSE (Rust::validate_crate_name ("/a+b/", error
));
1271 /* Tests for crate name inference */
1272 ASSERT_EQ (Rust::infer_crate_name ("c.rs"), "c");
1273 // NOTE: ... but - is allowed when in the filename
1274 ASSERT_EQ (Rust::infer_crate_name ("a-b.rs"), "a_b");
1275 ASSERT_EQ (Rust::infer_crate_name ("book.rs.txt"), "book.rs");
1276 #if defined(HAVE_DOS_BASED_FILE_SYSTEM)
1277 ASSERT_EQ (Rust::infer_crate_name ("a\\c\\a-b.rs"), "a_b");
1279 ASSERT_EQ (Rust::infer_crate_name ("a/c/a-b.rs"), "a_b");
1282 } // namespace selftest
1283 #endif // CHECKING_P