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