LCOV - code coverage report
Current view: top level - gcc/rust - rust-session-manager.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 58.1 % 639 371
Test Date: 2026-04-20 14:57:17 Functions: 63.3 % 30 19
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.