| 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-ast-visitor.h" |
| 20 |
#include "rust-system.h" |
| 21 |
#include "rust-session-manager.h" |
| 22 |
#include "rust-attributes.h" |
| 23 |
#include "rust-ast.h" |
| 24 |
#include "rust-ast-full.h" |
| 25 |
#include "rust-diagnostics.h" |
| 26 |
#include "rust-unicode.h" |
| 27 |
#include "rust-attribute-values.h" |
| 28 |
|
| 29 |
namespace Rust { |
| 30 |
namespace Analysis { |
| 31 |
|
| 32 |
bool |
| 33 |
Attributes::is_known (const std::string &attribute_path) |
| 34 |
{ |
| 35 |
const auto &lookup |
| 36 |
= BuiltinAttributeMappings::get ()->lookup_builtin (attribute_path); |
| 37 |
|
| 38 |
return !lookup.is_error (); |
| 39 |
} |
| 40 |
|
| 41 |
using Attrs = Values::Attributes; |
| 42 |
|
| 43 |
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248 |
| 44 |
static const BuiltinAttrDefinition __definitions[] |
| 45 |
= {{Attrs::INLINE, CODE_GENERATION}, |
| 46 |
{Attrs::COLD, CODE_GENERATION}, |
| 47 |
{Attrs::CFG, EXPANSION}, |
| 48 |
{Attrs::CFG_ATTR, EXPANSION}, |
| 49 |
{Attrs::DERIVE_ATTR, EXPANSION}, |
| 50 |
{Attrs::DEPRECATED, STATIC_ANALYSIS}, |
| 51 |
{Attrs::ALLOW, STATIC_ANALYSIS}, |
| 52 |
{Attrs::ALLOW_INTERNAL_UNSTABLE, STATIC_ANALYSIS}, |
| 53 |
{Attrs::DOC, HIR_LOWERING}, |
| 54 |
{Attrs::MUST_USE, STATIC_ANALYSIS}, |
| 55 |
{Attrs::LANG, HIR_LOWERING}, |
| 56 |
{Attrs::LINK_SECTION, CODE_GENERATION}, |
| 57 |
{Attrs::NO_MANGLE, CODE_GENERATION}, |
| 58 |
{Attrs::REPR, CODE_GENERATION}, |
| 59 |
{Attrs::RUSTC_BUILTIN_MACRO, EXPANSION}, |
| 60 |
{Attrs::RUSTC_MACRO_TRANSPARENCY, EXPANSION}, |
| 61 |
{Attrs::PATH, EXPANSION}, |
| 62 |
{Attrs::MACRO_USE, NAME_RESOLUTION}, |
| 63 |
{Attrs::MACRO_EXPORT, NAME_RESOLUTION}, |
| 64 |
{Attrs::PROC_MACRO, EXPANSION}, |
| 65 |
{Attrs::PROC_MACRO_DERIVE, EXPANSION}, |
| 66 |
{Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION}, |
| 67 |
// FIXME: This is not implemented yet, see |
| 68 |
// https://github.com/Rust-GCC/gccrs/issues/1475 |
| 69 |
{Attrs::TARGET_FEATURE, CODE_GENERATION}, |
| 70 |
// From now on, these are reserved by the compiler and gated through |
| 71 |
// #![feature(rustc_attrs)] |
| 72 |
{Attrs::RUSTC_DEPRECATED, STATIC_ANALYSIS}, |
| 73 |
{Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION}, |
| 74 |
{Attrs::STABLE, STATIC_ANALYSIS}, |
| 75 |
{Attrs::UNSTABLE, STATIC_ANALYSIS}, |
| 76 |
|
| 77 |
// assuming we keep these for static analysis |
| 78 |
{Attrs::RUSTC_PROMOTABLE, CODE_GENERATION}, |
| 79 |
{Attrs::RUSTC_CONST_STABLE, STATIC_ANALYSIS}, |
| 80 |
{Attrs::RUSTC_CONST_UNSTABLE, STATIC_ANALYSIS}, |
| 81 |
{Attrs::PRELUDE_IMPORT, NAME_RESOLUTION}, |
| 82 |
{Attrs::TRACK_CALLER, CODE_GENERATION}, |
| 83 |
{Attrs::RUSTC_SPECIALIZATION_TRAIT, TYPE_CHECK}, |
| 84 |
{Attrs::RUSTC_UNSAFE_SPECIALIZATION_MARKER, TYPE_CHECK}, |
| 85 |
{Attrs::RUSTC_RESERVATION_IMPL, TYPE_CHECK}, |
| 86 |
{Attrs::RUSTC_PAREN_SUGAR, TYPE_CHECK}, |
| 87 |
{Attrs::RUSTC_NONNULL_OPTIMIZATION_GUARANTEED, TYPE_CHECK}, |
| 88 |
|
| 89 |
{Attrs::RUSTC_LAYOUT_SCALAR_VALID_RANGE_START, CODE_GENERATION}, |
| 90 |
|
| 91 |
{Attrs::PRELUDE_IMPORT, NAME_RESOLUTION}, |
| 92 |
|
| 93 |
{Attrs::RUSTC_DIAGNOSTIC_ITEM, STATIC_ANALYSIS}, |
| 94 |
{Attrs::RUSTC_ON_UNIMPLEMENTED, STATIC_ANALYSIS}, |
| 95 |
|
| 96 |
{Attrs::FUNDAMENTAL, TYPE_CHECK}, |
| 97 |
{Attrs::NON_EXHAUSTIVE, TYPE_CHECK}, |
| 98 |
{Attrs::RUSTFMT, EXTERNAL}}; |
| 99 |
|
| 100 |
BuiltinAttributeMappings * |
| 101 |
BuiltinAttributeMappings::get () |
| 102 |
{ |
| 103 |
static BuiltinAttributeMappings *instance = nullptr; |
| 104 |
if (instance == nullptr) |
| 105 |
instance = new BuiltinAttributeMappings (); |
| 106 |
|
| 107 |
return instance; |
| 108 |
} |
| 109 |
|
| 110 |
const BuiltinAttrDefinition & |
| 111 |
BuiltinAttributeMappings::lookup_builtin (const std::string &attr_name) const |
| 112 |
{ |
| 113 |
auto it = mappings.find (attr_name); |
| 114 |
if (it == mappings.end ()) |
| 115 |
return BuiltinAttrDefinition::error_node (); |
| 116 |
|
| 117 |
return it->second; |
| 118 |
} |
| 119 |
|
| 120 |
BuiltinAttributeMappings::BuiltinAttributeMappings () |
| 121 |
{ |
| 122 |
size_t ndefinitions = sizeof (__definitions) / sizeof (BuiltinAttrDefinition); |
| 123 |
for (size_t i = 0; i < ndefinitions; i++) |
| 124 |
{ |
| 125 |
const BuiltinAttrDefinition &def = __definitions[i]; |
| 126 |
mappings.insert ({def.name, def}); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
AttributeChecker::AttributeChecker () {} |
| 131 |
|
| 132 |
void |
| 133 |
AttributeChecker::go (AST::Crate &crate) |
| 134 |
{ |
| 135 |
visit (crate); |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
AttributeChecker::visit (AST::Crate &crate) |
| 140 |
{ |
| 141 |
check_attributes (crate.get_inner_attrs ()); |
| 142 |
|
| 143 |
for (auto &item : crate.items) |
| 144 |
item->accept_vis (*this); |
| 145 |
} |
| 146 |
|
| 147 |
static bool |
| 148 |
is_builtin (const AST::Attribute &attribute, BuiltinAttrDefinition &builtin) |
| 149 |
{ |
| 150 |
auto &segments = attribute.get_path ().get_segments (); |
| 151 |
|
| 152 |
// Builtin attributes always have a single segment. This avoids us creating |
| 153 |
// strings all over the place and performing a linear search in the builtins |
| 154 |
// map |
| 155 |
if (segments.size () != 1) |
| 156 |
return false; |
| 157 |
|
| 158 |
builtin = BuiltinAttributeMappings::get ()->lookup_builtin ( |
| 159 |
segments.at (0).get_segment_name ()); |
| 160 |
|
| 161 |
return !builtin.is_error (); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Check that the string given to #[doc(alias = ...)] or #[doc(alias(...))] is |
| 166 |
* valid. |
| 167 |
* |
| 168 |
* This means no whitespace characters other than spaces and no quoting |
| 169 |
* characters. |
| 170 |
*/ |
| 171 |
static void |
| 172 |
check_doc_alias (const std::string &alias_input, const location_t locus) |
| 173 |
{ |
| 174 |
// FIXME: The locus here is for the whole attribute. Can we get the locus |
| 175 |
// of the alias input instead? |
| 176 |
for (auto c : alias_input) |
| 177 |
if ((ISSPACE (c) && c != ' ') || c == '\'' || c == '\"') |
| 178 |
{ |
| 179 |
auto to_print = std::string (1, c); |
| 180 |
switch (c) |
| 181 |
{ |
| 182 |
case '\n': |
| 183 |
to_print = "\\n"; |
| 184 |
break; |
| 185 |
case '\t': |
| 186 |
to_print = "\\t"; |
| 187 |
break; |
| 188 |
default: |
| 189 |
break; |
| 190 |
} |
| 191 |
rust_error_at (locus, |
| 192 |
"invalid character used in %<#[doc(alias)]%> input: %qs", |
| 193 |
to_print.c_str ()); |
| 194 |
} |
| 195 |
|
| 196 |
if (alias_input.empty ()) |
| 197 |
return; |
| 198 |
|
| 199 |
if (alias_input.front () == ' ' || alias_input.back () == ' ') |
| 200 |
rust_error_at (locus, |
| 201 |
"%<#[doc(alias)]%> input cannot start or end with a space"); |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
check_doc_attribute (const AST::Attribute &attribute) |
| 206 |
{ |
| 207 |
if (!attribute.has_attr_input ()) |
| 208 |
{ |
| 209 |
rust_error_at ( |
| 210 |
attribute.get_locus (), |
| 211 |
// FIXME: Improve error message here. Rustc has a very good one |
| 212 |
"%<#[doc]%> cannot be an empty attribute"); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
switch (attribute.get_attr_input ().get_attr_input_type ()) |
| 217 |
{ |
| 218 |
case AST::AttrInput::LITERAL: |
| 219 |
case AST::AttrInput::MACRO: |
| 220 |
case AST::AttrInput::META_ITEM: |
| 221 |
break; |
| 222 |
// FIXME: Handle them as well |
| 223 |
|
| 224 |
case AST::AttrInput::TOKEN_TREE: { |
| 225 |
// FIXME: This doesn't check for #[doc(alias(...))] |
| 226 |
const auto &option = static_cast<const AST::DelimTokenTree &> ( |
| 227 |
attribute.get_attr_input ()); |
| 228 |
auto *meta_item = option.parse_to_meta_item (); |
| 229 |
|
| 230 |
for (auto &item : meta_item->get_items ()) |
| 231 |
{ |
| 232 |
if (item->is_key_value_pair ()) |
| 233 |
{ |
| 234 |
auto name_value |
| 235 |
= static_cast<AST::MetaNameValueStr *> (item.get ()) |
| 236 |
->get_name_value_pair (); |
| 237 |
|
| 238 |
// FIXME: Check for other stuff than #[doc(alias = ...)] |
| 239 |
if (name_value.first.as_string () == "alias") |
| 240 |
check_doc_alias (name_value.second, attribute.get_locus ()); |
| 241 |
} |
| 242 |
} |
| 243 |
break; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
static bool |
| 249 |
is_proc_macro_type (const AST::Attribute &attribute) |
| 250 |
{ |
| 251 |
BuiltinAttrDefinition result; |
| 252 |
if (!is_builtin (attribute, result)) |
| 253 |
return false; |
| 254 |
|
| 255 |
auto name = result.name; |
| 256 |
return name == Attrs::PROC_MACRO || name == Attrs::PROC_MACRO_DERIVE |
| 257 |
|| name == Attrs::PROC_MACRO_ATTRIBUTE; |
| 258 |
} |
| 259 |
|
| 260 |
// Emit an error when one encountered attribute is either #[proc_macro], |
| 261 |
// #[proc_macro_attribute] or #[proc_macro_derive] |
| 262 |
static void |
| 263 |
check_proc_macro_non_function (const AST::AttrVec &attributes) |
| 264 |
{ |
| 265 |
for (auto &attr : attributes) |
| 266 |
{ |
| 267 |
if (is_proc_macro_type (attr)) |
| 268 |
rust_error_at ( |
| 269 |
attr.get_locus (), |
| 270 |
"the %<#[%s]%> attribute may only be used on bare functions", |
| 271 |
attr.get_path ().get_segments ()[0].as_string ().c_str ()); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
// Emit an error when one attribute is either proc_macro, proc_macro_attribute |
| 276 |
// or proc_macro_derive |
| 277 |
static void |
| 278 |
check_proc_macro_non_root (AST::AttrVec attributes, location_t loc) |
| 279 |
{ |
| 280 |
for (auto &attr : attributes) |
| 281 |
{ |
| 282 |
if (is_proc_macro_type (attr)) |
| 283 |
{ |
| 284 |
rust_error_at ( |
| 285 |
loc, |
| 286 |
"functions tagged with %<#[%s]%> must currently " |
| 287 |
"reside in the root of the crate", |
| 288 |
attr.get_path ().get_segments ().at (0).as_string ().c_str ()); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
void |
| 294 |
AttributeChecker::check_attribute (const AST::Attribute &attribute) |
| 295 |
{ |
| 296 |
BuiltinAttrDefinition result; |
| 297 |
|
| 298 |
// This checker does not check non-builtin attributes |
| 299 |
if (!is_builtin (attribute, result)) |
| 300 |
return; |
| 301 |
|
| 302 |
// TODO: Add checks here for each builtin attribute |
| 303 |
// TODO: Have an enum of builtins as well, switching on strings is annoying |
| 304 |
// and costly |
| 305 |
if (result.name == Attrs::DOC) |
| 306 |
check_doc_attribute (attribute); |
| 307 |
} |
| 308 |
|
| 309 |
void |
| 310 |
AttributeChecker::check_attributes (const AST::AttrVec &attributes) |
| 311 |
{ |
| 312 |
for (auto &attr : attributes) |
| 313 |
check_attribute (attr); |
| 314 |
} |
| 315 |
|
| 316 |
void |
| 317 |
AttributeChecker::visit (AST::Token &) |
| 318 |
{} |
| 319 |
|
| 320 |
void |
| 321 |
AttributeChecker::visit (AST::DelimTokenTree &) |
| 322 |
{} |
| 323 |
|
| 324 |
void |
| 325 |
AttributeChecker::visit (AST::AttrInputMetaItemContainer &) |
| 326 |
{} |
| 327 |
|
| 328 |
void |
| 329 |
AttributeChecker::visit (AST::IdentifierExpr &) |
| 330 |
{} |
| 331 |
|
| 332 |
void |
| 333 |
AttributeChecker::visit (AST::Lifetime &) |
| 334 |
{} |
| 335 |
|
| 336 |
void |
| 337 |
AttributeChecker::visit (AST::LifetimeParam &) |
| 338 |
{} |
| 339 |
|
| 340 |
void |
| 341 |
AttributeChecker::visit (AST::ConstGenericParam &) |
| 342 |
{} |
| 343 |
|
| 344 |
// rust-path.h |
| 345 |
void |
| 346 |
AttributeChecker::visit (AST::PathInExpression &) |
| 347 |
{} |
| 348 |
|
| 349 |
void |
| 350 |
AttributeChecker::visit (AST::TypePathSegment &) |
| 351 |
{} |
| 352 |
|
| 353 |
void |
| 354 |
AttributeChecker::visit (AST::TypePathSegmentGeneric &) |
| 355 |
{} |
| 356 |
|
| 357 |
void |
| 358 |
AttributeChecker::visit (AST::TypePathSegmentFunction &) |
| 359 |
{} |
| 360 |
|
| 361 |
void |
| 362 |
AttributeChecker::visit (AST::TypePath &) |
| 363 |
{} |
| 364 |
|
| 365 |
void |
| 366 |
AttributeChecker::visit (AST::QualifiedPathInExpression &) |
| 367 |
{} |
| 368 |
|
| 369 |
void |
| 370 |
AttributeChecker::visit (AST::QualifiedPathInType &) |
| 371 |
{} |
| 372 |
|
| 373 |
// rust-expr.h |
| 374 |
void |
| 375 |
AttributeChecker::visit (AST::LiteralExpr &) |
| 376 |
{} |
| 377 |
|
| 378 |
void |
| 379 |
AttributeChecker::visit (AST::AttrInputLiteral &) |
| 380 |
{} |
| 381 |
|
| 382 |
void |
| 383 |
AttributeChecker::visit (AST::AttrInputMacro &) |
| 384 |
{} |
| 385 |
|
| 386 |
void |
| 387 |
AttributeChecker::visit (AST::MetaItemLitExpr &) |
| 388 |
{} |
| 389 |
|
| 390 |
void |
| 391 |
AttributeChecker::visit (AST::MetaItemPathLit &) |
| 392 |
{} |
| 393 |
|
| 394 |
void |
| 395 |
AttributeChecker::visit (AST::BorrowExpr &) |
| 396 |
{} |
| 397 |
|
| 398 |
void |
| 399 |
AttributeChecker::visit (AST::DereferenceExpr &) |
| 400 |
{} |
| 401 |
|
| 402 |
void |
| 403 |
AttributeChecker::visit (AST::ErrorPropagationExpr &) |
| 404 |
{} |
| 405 |
|
| 406 |
void |
| 407 |
AttributeChecker::visit (AST::NegationExpr &) |
| 408 |
{} |
| 409 |
|
| 410 |
void |
| 411 |
AttributeChecker::visit (AST::ArithmeticOrLogicalExpr &) |
| 412 |
{} |
| 413 |
|
| 414 |
void |
| 415 |
AttributeChecker::visit (AST::ComparisonExpr &) |
| 416 |
{} |
| 417 |
|
| 418 |
void |
| 419 |
AttributeChecker::visit (AST::LazyBooleanExpr &) |
| 420 |
{} |
| 421 |
|
| 422 |
void |
| 423 |
AttributeChecker::visit (AST::TypeCastExpr &) |
| 424 |
{} |
| 425 |
|
| 426 |
void |
| 427 |
AttributeChecker::visit (AST::AssignmentExpr &) |
| 428 |
{} |
| 429 |
|
| 430 |
void |
| 431 |
AttributeChecker::visit (AST::CompoundAssignmentExpr &) |
| 432 |
{} |
| 433 |
|
| 434 |
void |
| 435 |
AttributeChecker::visit (AST::GroupedExpr &) |
| 436 |
{} |
| 437 |
|
| 438 |
void |
| 439 |
AttributeChecker::visit (AST::ArrayElemsValues &) |
| 440 |
{} |
| 441 |
|
| 442 |
void |
| 443 |
AttributeChecker::visit (AST::ArrayElemsCopied &) |
| 444 |
{} |
| 445 |
|
| 446 |
void |
| 447 |
AttributeChecker::visit (AST::ArrayExpr &) |
| 448 |
{} |
| 449 |
|
| 450 |
void |
| 451 |
AttributeChecker::visit (AST::ArrayIndexExpr &) |
| 452 |
{} |
| 453 |
|
| 454 |
void |
| 455 |
AttributeChecker::visit (AST::TupleExpr &) |
| 456 |
{} |
| 457 |
|
| 458 |
void |
| 459 |
AttributeChecker::visit (AST::TupleIndexExpr &) |
| 460 |
{} |
| 461 |
|
| 462 |
void |
| 463 |
AttributeChecker::visit (AST::StructExprStruct &) |
| 464 |
{} |
| 465 |
|
| 466 |
void |
| 467 |
AttributeChecker::visit (AST::StructExprFieldIdentifier &) |
| 468 |
{} |
| 469 |
|
| 470 |
void |
| 471 |
AttributeChecker::visit (AST::StructExprFieldIdentifierValue &) |
| 472 |
{} |
| 473 |
|
| 474 |
void |
| 475 |
AttributeChecker::visit (AST::StructExprFieldIndexValue &) |
| 476 |
{} |
| 477 |
|
| 478 |
void |
| 479 |
AttributeChecker::visit (AST::StructExprStructFields &) |
| 480 |
{} |
| 481 |
|
| 482 |
void |
| 483 |
AttributeChecker::visit (AST::StructExprStructBase &) |
| 484 |
{} |
| 485 |
|
| 486 |
void |
| 487 |
AttributeChecker::visit (AST::CallExpr &) |
| 488 |
{} |
| 489 |
|
| 490 |
void |
| 491 |
AttributeChecker::visit (AST::MethodCallExpr &) |
| 492 |
{} |
| 493 |
|
| 494 |
void |
| 495 |
AttributeChecker::visit (AST::FieldAccessExpr &) |
| 496 |
{} |
| 497 |
|
| 498 |
void |
| 499 |
AttributeChecker::visit (AST::ClosureExprInner &) |
| 500 |
{} |
| 501 |
|
| 502 |
void |
| 503 |
AttributeChecker::visit (AST::BlockExpr &expr) |
| 504 |
{ |
| 505 |
for (auto &stmt : expr.get_statements ()) |
| 506 |
{ |
| 507 |
if (stmt->get_stmt_kind () == AST::Stmt::Kind::Item) |
| 508 |
{ |
| 509 |
// Non owning pointer, let it go out of scope |
| 510 |
auto item = static_cast<AST::Item *> (stmt.get ()); |
| 511 |
check_proc_macro_non_root (item->get_outer_attrs (), |
| 512 |
item->get_locus ()); |
| 513 |
} |
| 514 |
} |
| 515 |
AST::DefaultASTVisitor::visit (expr); |
| 516 |
} |
| 517 |
|
| 518 |
void |
| 519 |
AttributeChecker::visit (AST::ClosureExprInnerTyped &) |
| 520 |
{} |
| 521 |
|
| 522 |
void |
| 523 |
AttributeChecker::visit (AST::ContinueExpr &) |
| 524 |
{} |
| 525 |
|
| 526 |
void |
| 527 |
AttributeChecker::visit (AST::BreakExpr &) |
| 528 |
{} |
| 529 |
|
| 530 |
void |
| 531 |
AttributeChecker::visit (AST::RangeFromToExpr &) |
| 532 |
{} |
| 533 |
|
| 534 |
void |
| 535 |
AttributeChecker::visit (AST::RangeFromExpr &) |
| 536 |
{} |
| 537 |
|
| 538 |
void |
| 539 |
AttributeChecker::visit (AST::RangeToExpr &) |
| 540 |
{} |
| 541 |
|
| 542 |
void |
| 543 |
AttributeChecker::visit (AST::RangeFullExpr &) |
| 544 |
{} |
| 545 |
|
| 546 |
void |
| 547 |
AttributeChecker::visit (AST::RangeFromToInclExpr &) |
| 548 |
{} |
| 549 |
|
| 550 |
void |
| 551 |
AttributeChecker::visit (AST::RangeToInclExpr &) |
| 552 |
{} |
| 553 |
|
| 554 |
void |
| 555 |
AttributeChecker::visit (AST::ReturnExpr &) |
| 556 |
{} |
| 557 |
|
| 558 |
void |
| 559 |
AttributeChecker::visit (AST::LoopExpr &) |
| 560 |
{} |
| 561 |
|
| 562 |
void |
| 563 |
AttributeChecker::visit (AST::WhileLoopExpr &) |
| 564 |
{} |
| 565 |
|
| 566 |
void |
| 567 |
AttributeChecker::visit (AST::WhileLetLoopExpr &) |
| 568 |
{} |
| 569 |
|
| 570 |
void |
| 571 |
AttributeChecker::visit (AST::ForLoopExpr &) |
| 572 |
{} |
| 573 |
|
| 574 |
void |
| 575 |
AttributeChecker::visit (AST::IfExpr &) |
| 576 |
{} |
| 577 |
|
| 578 |
void |
| 579 |
AttributeChecker::visit (AST::IfExprConseqElse &) |
| 580 |
{} |
| 581 |
|
| 582 |
void |
| 583 |
AttributeChecker::visit (AST::IfLetExpr &) |
| 584 |
{} |
| 585 |
|
| 586 |
void |
| 587 |
AttributeChecker::visit (AST::IfLetExprConseqElse &) |
| 588 |
{} |
| 589 |
|
| 590 |
void |
| 591 |
AttributeChecker::visit (AST::MatchExpr &) |
| 592 |
{} |
| 593 |
|
| 594 |
void |
| 595 |
AttributeChecker::visit (AST::AwaitExpr &) |
| 596 |
{} |
| 597 |
|
| 598 |
void |
| 599 |
AttributeChecker::visit (AST::AsyncBlockExpr &) |
| 600 |
{} |
| 601 |
|
| 602 |
// rust-item.h |
| 603 |
void |
| 604 |
AttributeChecker::visit (AST::TypeParam &) |
| 605 |
{} |
| 606 |
|
| 607 |
void |
| 608 |
AttributeChecker::visit (AST::LifetimeWhereClauseItem &) |
| 609 |
{} |
| 610 |
|
| 611 |
void |
| 612 |
AttributeChecker::visit (AST::TypeBoundWhereClauseItem &) |
| 613 |
{} |
| 614 |
|
| 615 |
void |
| 616 |
AttributeChecker::visit (AST::Module &module) |
| 617 |
{ |
| 618 |
check_proc_macro_non_function (module.get_outer_attrs ()); |
| 619 |
for (auto &item : module.get_items ()) |
| 620 |
{ |
| 621 |
check_proc_macro_non_root (item->get_outer_attrs (), item->get_locus ()); |
| 622 |
} |
| 623 |
AST::DefaultASTVisitor::visit (module); |
| 624 |
} |
| 625 |
|
| 626 |
void |
| 627 |
AttributeChecker::visit (AST::ExternCrate &crate) |
| 628 |
{ |
| 629 |
check_proc_macro_non_function (crate.get_outer_attrs ()); |
| 630 |
} |
| 631 |
|
| 632 |
void |
| 633 |
AttributeChecker::visit (AST::UseTreeGlob &) |
| 634 |
{} |
| 635 |
|
| 636 |
void |
| 637 |
AttributeChecker::visit (AST::UseTreeList &) |
| 638 |
{} |
| 639 |
|
| 640 |
void |
| 641 |
AttributeChecker::visit (AST::UseTreeRebind &) |
| 642 |
{} |
| 643 |
|
| 644 |
void |
| 645 |
AttributeChecker::visit (AST::UseDeclaration &declaration) |
| 646 |
{ |
| 647 |
check_proc_macro_non_function (declaration.get_outer_attrs ()); |
| 648 |
} |
| 649 |
|
| 650 |
static void |
| 651 |
check_no_mangle_function (const AST::Attribute &attribute, |
| 652 |
const AST::Function &fun) |
| 653 |
{ |
| 654 |
if (attribute.has_attr_input ()) |
| 655 |
{ |
| 656 |
rust_error_at (attribute.get_locus (), ErrorCode::E0754, |
| 657 |
"malformed %<no_mangle%> attribute input"); |
| 658 |
rust_inform (attribute.get_locus (), |
| 659 |
"must be of the form: %<#[no_mangle]%>"); |
| 660 |
} |
| 661 |
if (!is_ascii_only (fun.get_function_name ().as_string ())) |
| 662 |
rust_error_at (fun.get_function_name ().get_locus (), |
| 663 |
"the %<#[no_mangle]%> attribute requires ASCII identifier"); |
| 664 |
} |
| 665 |
|
| 666 |
void |
| 667 |
AttributeChecker::visit (AST::Function &fun) |
| 668 |
{ |
| 669 |
auto check_crate_type = [] (const char *name, AST::Attribute &attribute) { |
| 670 |
if (!Session::get_instance ().options.is_proc_macro ()) |
| 671 |
rust_error_at (attribute.get_locus (), |
| 672 |
"the %<#[%s]%> attribute is only usable with crates of " |
| 673 |
"the %<proc-macro%> crate type", |
| 674 |
name); |
| 675 |
}; |
| 676 |
|
| 677 |
BuiltinAttrDefinition result; |
| 678 |
for (auto &attribute : fun.get_outer_attrs ()) |
| 679 |
{ |
| 680 |
if (!is_builtin (attribute, result)) |
| 681 |
return; |
| 682 |
|
| 683 |
auto name = result.name.c_str (); |
| 684 |
|
| 685 |
if (result.name == Attrs::PROC_MACRO_DERIVE) |
| 686 |
{ |
| 687 |
if (!attribute.has_attr_input ()) |
| 688 |
{ |
| 689 |
rust_error_at (attribute.get_locus (), |
| 690 |
"malformed %qs attribute input", name); |
| 691 |
rust_inform ( |
| 692 |
attribute.get_locus (), |
| 693 |
"must be of the form: %<#[proc_macro_derive(TraitName, " |
| 694 |
"/*opt*/ attributes(name1, name2, ...))]%>"); |
| 695 |
} |
| 696 |
check_crate_type (name, attribute); |
| 697 |
} |
| 698 |
else if (result.name == Attrs::PROC_MACRO |
| 699 |
|| result.name == Attrs::PROC_MACRO_ATTRIBUTE) |
| 700 |
{ |
| 701 |
check_crate_type (name, attribute); |
| 702 |
} |
| 703 |
else if (result.name == "no_mangle") |
| 704 |
check_no_mangle_function (attribute, fun); |
| 705 |
} |
| 706 |
if (fun.has_body ()) |
| 707 |
fun.get_definition ().value ()->accept_vis (*this); |
| 708 |
} |
| 709 |
|
| 710 |
void |
| 711 |
AttributeChecker::visit (AST::TypeAlias &alias) |
| 712 |
{ |
| 713 |
check_proc_macro_non_function (alias.get_outer_attrs ()); |
| 714 |
} |
| 715 |
|
| 716 |
void |
| 717 |
AttributeChecker::visit (AST::StructStruct &struct_item) |
| 718 |
{ |
| 719 |
check_attributes (struct_item.get_outer_attrs ()); |
| 720 |
check_proc_macro_non_function (struct_item.get_outer_attrs ()); |
| 721 |
} |
| 722 |
|
| 723 |
void |
| 724 |
AttributeChecker::visit (AST::TupleStruct &tuplestruct) |
| 725 |
{ |
| 726 |
check_proc_macro_non_function (tuplestruct.get_outer_attrs ()); |
| 727 |
} |
| 728 |
|
| 729 |
void |
| 730 |
AttributeChecker::visit (AST::EnumItem &) |
| 731 |
{} |
| 732 |
|
| 733 |
void |
| 734 |
AttributeChecker::visit (AST::EnumItemTuple &) |
| 735 |
{} |
| 736 |
|
| 737 |
void |
| 738 |
AttributeChecker::visit (AST::EnumItemStruct &) |
| 739 |
{} |
| 740 |
|
| 741 |
void |
| 742 |
AttributeChecker::visit (AST::EnumItemDiscriminant &) |
| 743 |
{} |
| 744 |
|
| 745 |
void |
| 746 |
AttributeChecker::visit (AST::Enum &enumeration) |
| 747 |
{ |
| 748 |
check_proc_macro_non_function (enumeration.get_outer_attrs ()); |
| 749 |
} |
| 750 |
|
| 751 |
void |
| 752 |
AttributeChecker::visit (AST::Union &u) |
| 753 |
{ |
| 754 |
check_proc_macro_non_function (u.get_outer_attrs ()); |
| 755 |
} |
| 756 |
|
| 757 |
void |
| 758 |
AttributeChecker::visit (AST::ConstantItem &item) |
| 759 |
{ |
| 760 |
check_proc_macro_non_function (item.get_outer_attrs ()); |
| 761 |
} |
| 762 |
|
| 763 |
void |
| 764 |
AttributeChecker::visit (AST::StaticItem &item) |
| 765 |
{ |
| 766 |
check_proc_macro_non_function (item.get_outer_attrs ()); |
| 767 |
} |
| 768 |
|
| 769 |
void |
| 770 |
AttributeChecker::visit (AST::TraitItemConst &) |
| 771 |
{} |
| 772 |
|
| 773 |
void |
| 774 |
AttributeChecker::visit (AST::TraitItemType &) |
| 775 |
{} |
| 776 |
|
| 777 |
void |
| 778 |
AttributeChecker::visit (AST::Trait &trait) |
| 779 |
{ |
| 780 |
check_proc_macro_non_function (trait.get_outer_attrs ()); |
| 781 |
} |
| 782 |
|
| 783 |
void |
| 784 |
AttributeChecker::visit (AST::InherentImpl &impl) |
| 785 |
{ |
| 786 |
check_proc_macro_non_function (impl.get_outer_attrs ()); |
| 787 |
AST::DefaultASTVisitor::visit (impl); |
| 788 |
} |
| 789 |
|
| 790 |
void |
| 791 |
AttributeChecker::visit (AST::TraitImpl &impl) |
| 792 |
{ |
| 793 |
check_proc_macro_non_function (impl.get_outer_attrs ()); |
| 794 |
AST::DefaultASTVisitor::visit (impl); |
| 795 |
} |
| 796 |
|
| 797 |
void |
| 798 |
AttributeChecker::visit (AST::ExternalTypeItem &) |
| 799 |
{} |
| 800 |
|
| 801 |
void |
| 802 |
AttributeChecker::visit (AST::ExternalStaticItem &) |
| 803 |
{} |
| 804 |
|
| 805 |
void |
| 806 |
AttributeChecker::visit (AST::ExternBlock &block) |
| 807 |
{ |
| 808 |
check_proc_macro_non_function (block.get_outer_attrs ()); |
| 809 |
} |
| 810 |
|
| 811 |
// rust-macro.h |
| 812 |
void |
| 813 |
AttributeChecker::visit (AST::MacroMatchFragment &) |
| 814 |
{} |
| 815 |
|
| 816 |
void |
| 817 |
AttributeChecker::visit (AST::MacroMatchRepetition &) |
| 818 |
{} |
| 819 |
|
| 820 |
void |
| 821 |
AttributeChecker::visit (AST::MacroMatcher &) |
| 822 |
{} |
| 823 |
|
| 824 |
void |
| 825 |
AttributeChecker::visit (AST::MacroRulesDefinition &) |
| 826 |
{} |
| 827 |
|
| 828 |
void |
| 829 |
AttributeChecker::visit (AST::MacroInvocation &) |
| 830 |
{} |
| 831 |
|
| 832 |
void |
| 833 |
AttributeChecker::visit (AST::MetaItemPath &) |
| 834 |
{} |
| 835 |
|
| 836 |
void |
| 837 |
AttributeChecker::visit (AST::MetaItemSeq &) |
| 838 |
{} |
| 839 |
|
| 840 |
void |
| 841 |
AttributeChecker::visit (AST::MetaWord &) |
| 842 |
{} |
| 843 |
|
| 844 |
void |
| 845 |
AttributeChecker::visit (AST::MetaNameValueStr &) |
| 846 |
{} |
| 847 |
|
| 848 |
void |
| 849 |
AttributeChecker::visit (AST::MetaListPaths &) |
| 850 |
{} |
| 851 |
|
| 852 |
void |
| 853 |
AttributeChecker::visit (AST::MetaListNameValueStr &) |
| 854 |
{} |
| 855 |
|
| 856 |
// rust-pattern.h |
| 857 |
void |
| 858 |
AttributeChecker::visit (AST::LiteralPattern &) |
| 859 |
{} |
| 860 |
|
| 861 |
void |
| 862 |
AttributeChecker::visit (AST::IdentifierPattern &) |
| 863 |
{} |
| 864 |
|
| 865 |
void |
| 866 |
AttributeChecker::visit (AST::WildcardPattern &) |
| 867 |
{} |
| 868 |
|
| 869 |
void |
| 870 |
AttributeChecker::visit (AST::RestPattern &) |
| 871 |
{} |
| 872 |
|
| 873 |
// void AttributeChecker::visit(RangePatternBound& ){} |
| 874 |
|
| 875 |
void |
| 876 |
AttributeChecker::visit (AST::RangePatternBoundLiteral &) |
| 877 |
{} |
| 878 |
|
| 879 |
void |
| 880 |
AttributeChecker::visit (AST::RangePatternBoundPath &) |
| 881 |
{} |
| 882 |
|
| 883 |
void |
| 884 |
AttributeChecker::visit (AST::RangePatternBoundQualPath &) |
| 885 |
{} |
| 886 |
|
| 887 |
void |
| 888 |
AttributeChecker::visit (AST::RangePattern &) |
| 889 |
{} |
| 890 |
|
| 891 |
void |
| 892 |
AttributeChecker::visit (AST::ReferencePattern &) |
| 893 |
{} |
| 894 |
|
| 895 |
// void AttributeChecker::visit(StructPatternField& ){} |
| 896 |
|
| 897 |
void |
| 898 |
AttributeChecker::visit (AST::StructPatternFieldTuplePat &) |
| 899 |
{} |
| 900 |
|
| 901 |
void |
| 902 |
AttributeChecker::visit (AST::StructPatternFieldIdentPat &) |
| 903 |
{} |
| 904 |
|
| 905 |
void |
| 906 |
AttributeChecker::visit (AST::StructPatternFieldIdent &) |
| 907 |
{} |
| 908 |
|
| 909 |
void |
| 910 |
AttributeChecker::visit (AST::StructPattern &) |
| 911 |
{} |
| 912 |
|
| 913 |
// void AttributeChecker::visit(TupleStructItems& ){} |
| 914 |
|
| 915 |
void |
| 916 |
AttributeChecker::visit (AST::TupleStructItemsNoRange &) |
| 917 |
{} |
| 918 |
|
| 919 |
void |
| 920 |
AttributeChecker::visit (AST::TupleStructItemsRange &) |
| 921 |
{} |
| 922 |
|
| 923 |
void |
| 924 |
AttributeChecker::visit (AST::TupleStructPattern &) |
| 925 |
{} |
| 926 |
|
| 927 |
// void AttributeChecker::visit(TuplePatternItems& ){} |
| 928 |
|
| 929 |
void |
| 930 |
AttributeChecker::visit (AST::TuplePatternItemsMultiple &) |
| 931 |
{} |
| 932 |
|
| 933 |
void |
| 934 |
AttributeChecker::visit (AST::TuplePatternItemsRanged &) |
| 935 |
{} |
| 936 |
|
| 937 |
void |
| 938 |
AttributeChecker::visit (AST::TuplePattern &) |
| 939 |
{} |
| 940 |
|
| 941 |
void |
| 942 |
AttributeChecker::visit (AST::GroupedPattern &) |
| 943 |
{} |
| 944 |
|
| 945 |
void |
| 946 |
AttributeChecker::visit (AST::SlicePattern &) |
| 947 |
{} |
| 948 |
|
| 949 |
void |
| 950 |
AttributeChecker::visit (AST::AltPattern &) |
| 951 |
{} |
| 952 |
|
| 953 |
// rust-stmt.h |
| 954 |
void |
| 955 |
AttributeChecker::visit (AST::EmptyStmt &) |
| 956 |
{} |
| 957 |
|
| 958 |
void |
| 959 |
AttributeChecker::visit (AST::LetStmt &) |
| 960 |
{} |
| 961 |
|
| 962 |
void |
| 963 |
AttributeChecker::visit (AST::ExprStmt &) |
| 964 |
{} |
| 965 |
|
| 966 |
// rust-type.h |
| 967 |
void |
| 968 |
AttributeChecker::visit (AST::TraitBound &) |
| 969 |
{} |
| 970 |
|
| 971 |
void |
| 972 |
AttributeChecker::visit (AST::ImplTraitType &) |
| 973 |
{} |
| 974 |
|
| 975 |
void |
| 976 |
AttributeChecker::visit (AST::TraitObjectType &) |
| 977 |
{} |
| 978 |
|
| 979 |
void |
| 980 |
AttributeChecker::visit (AST::ParenthesisedType &) |
| 981 |
{} |
| 982 |
|
| 983 |
void |
| 984 |
AttributeChecker::visit (AST::ImplTraitTypeOneBound &) |
| 985 |
{} |
| 986 |
|
| 987 |
void |
| 988 |
AttributeChecker::visit (AST::TraitObjectTypeOneBound &) |
| 989 |
{} |
| 990 |
|
| 991 |
void |
| 992 |
AttributeChecker::visit (AST::TupleType &) |
| 993 |
{} |
| 994 |
|
| 995 |
void |
| 996 |
AttributeChecker::visit (AST::NeverType &) |
| 997 |
{} |
| 998 |
|
| 999 |
void |
| 1000 |
AttributeChecker::visit (AST::RawPointerType &) |
| 1001 |
{} |
| 1002 |
|
| 1003 |
void |
| 1004 |
AttributeChecker::visit (AST::ReferenceType &) |
| 1005 |
{} |
| 1006 |
|
| 1007 |
void |
| 1008 |
AttributeChecker::visit (AST::ArrayType &) |
| 1009 |
{} |
| 1010 |
|
| 1011 |
void |
| 1012 |
AttributeChecker::visit (AST::SliceType &) |
| 1013 |
{} |
| 1014 |
|
| 1015 |
void |
| 1016 |
AttributeChecker::visit (AST::InferredType &) |
| 1017 |
{} |
| 1018 |
|
| 1019 |
void |
| 1020 |
AttributeChecker::visit (AST::BareFunctionType &) |
| 1021 |
{} |
| 1022 |
|
| 1023 |
void |
| 1024 |
AttributeChecker::visit (AST::SelfParam &) |
| 1025 |
{} |
| 1026 |
|
| 1027 |
void |
| 1028 |
AttributeChecker::visit (AST::VariadicParam &) |
| 1029 |
{} |
| 1030 |
|
| 1031 |
void |
| 1032 |
AttributeChecker::visit (AST::FunctionParam &) |
| 1033 |
{} |
| 1034 |
|
| 1035 |
} // namespace Analysis |
| 1036 |
} // namespace Rust |