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