(root)/
gcc-13.2.0/
gcc/
rust/
util/
rust-attributes.h
       1  // Copyright (C) 2020-2023 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.h"
      20  #include "rust-system.h"
      21  #include "rust-ast-visitor.h"
      22  
      23  namespace Rust {
      24  namespace Analysis {
      25  
      26  enum CompilerPass
      27  {
      28    UNKNOWN,
      29  
      30    EXPANSION,
      31    NAME_RESOLUTION,
      32    HIR_LOWERING,
      33    TYPE_CHECK,
      34    STATIC_ANALYSIS,
      35    CODE_GENERATION
      36  };
      37  
      38  struct BuiltinAttrDefinition
      39  {
      40    std::string name;
      41    CompilerPass handler;
      42  
      43    static BuiltinAttrDefinition get_error ()
      44    {
      45      return BuiltinAttrDefinition{"", UNKNOWN};
      46    }
      47  
      48    static BuiltinAttrDefinition &error_node ()
      49    {
      50      static BuiltinAttrDefinition error_node = get_error ();
      51      return error_node;
      52    }
      53  
      54    bool is_error () const { return name.empty (); }
      55  };
      56  
      57  class BuiltinAttributeMappings
      58  {
      59  public:
      60    static BuiltinAttributeMappings *get ();
      61  
      62    const BuiltinAttrDefinition &
      63    lookup_builtin (const std::string &attr_name) const;
      64  
      65  private:
      66    BuiltinAttributeMappings ();
      67  
      68    std::map<std::string, const BuiltinAttrDefinition> mappings;
      69  };
      70  
      71  /**
      72   * Checks the validity of various attributes. The goal of this visitor is to
      73   * make sure that attributes are applied in allowed contexts, for example to
      74   * make sure that #[inline] is only applied to functions and closures, as well
      75   * as checking the "arguments" or input given to these attributes, making sure
      76   * it is appropriate and valid.
      77   */
      78  class AttributeChecker : public AST::ASTVisitor
      79  {
      80  public:
      81    AttributeChecker ();
      82  
      83    /**
      84     * Check all the attributes of all the items of a crate
      85     */
      86    void go (AST::Crate &crate);
      87  
      88  private:
      89    /* Check the validity of a given attribute */
      90    void check_attribute (const AST::Attribute &attribute);
      91  
      92    /* Check the validity of all given attributes */
      93    void check_attributes (const AST::AttrVec &attributes);
      94  
      95    // rust-ast.h
      96    void visit (AST::Token &tok);
      97    void visit (AST::DelimTokenTree &delim_tok_tree);
      98    void visit (AST::AttrInputMetaItemContainer &input);
      99    void visit (AST::IdentifierExpr &ident_expr);
     100    void visit (AST::Lifetime &lifetime);
     101    void visit (AST::LifetimeParam &lifetime_param);
     102    void visit (AST::ConstGenericParam &const_param);
     103  
     104    // rust-path.h
     105    void visit (AST::PathInExpression &path);
     106    void visit (AST::TypePathSegment &segment);
     107    void visit (AST::TypePathSegmentGeneric &segment);
     108    void visit (AST::TypePathSegmentFunction &segment);
     109    void visit (AST::TypePath &path);
     110    void visit (AST::QualifiedPathInExpression &path);
     111    void visit (AST::QualifiedPathInType &path);
     112  
     113    // rust-expr.h
     114    void visit (AST::LiteralExpr &expr);
     115    void visit (AST::AttrInputLiteral &attr_input);
     116    void visit (AST::MetaItemLitExpr &meta_item);
     117    void visit (AST::MetaItemPathLit &meta_item);
     118    void visit (AST::BorrowExpr &expr);
     119    void visit (AST::DereferenceExpr &expr);
     120    void visit (AST::ErrorPropagationExpr &expr);
     121    void visit (AST::NegationExpr &expr);
     122    void visit (AST::ArithmeticOrLogicalExpr &expr);
     123    void visit (AST::ComparisonExpr &expr);
     124    void visit (AST::LazyBooleanExpr &expr);
     125    void visit (AST::TypeCastExpr &expr);
     126    void visit (AST::AssignmentExpr &expr);
     127    void visit (AST::CompoundAssignmentExpr &expr);
     128    void visit (AST::GroupedExpr &expr);
     129    void visit (AST::ArrayElemsValues &elems);
     130    void visit (AST::ArrayElemsCopied &elems);
     131    void visit (AST::ArrayExpr &expr);
     132    void visit (AST::ArrayIndexExpr &expr);
     133    void visit (AST::TupleExpr &expr);
     134    void visit (AST::TupleIndexExpr &expr);
     135    void visit (AST::StructExprStruct &expr);
     136    void visit (AST::StructExprFieldIdentifier &field);
     137    void visit (AST::StructExprFieldIdentifierValue &field);
     138    void visit (AST::StructExprFieldIndexValue &field);
     139    void visit (AST::StructExprStructFields &expr);
     140    void visit (AST::StructExprStructBase &expr);
     141    void visit (AST::CallExpr &expr);
     142    void visit (AST::MethodCallExpr &expr);
     143    void visit (AST::FieldAccessExpr &expr);
     144    void visit (AST::ClosureExprInner &expr);
     145    void visit (AST::BlockExpr &expr);
     146    void visit (AST::ClosureExprInnerTyped &expr);
     147    void visit (AST::ContinueExpr &expr);
     148    void visit (AST::BreakExpr &expr);
     149    void visit (AST::RangeFromToExpr &expr);
     150    void visit (AST::RangeFromExpr &expr);
     151    void visit (AST::RangeToExpr &expr);
     152    void visit (AST::RangeFullExpr &expr);
     153    void visit (AST::RangeFromToInclExpr &expr);
     154    void visit (AST::RangeToInclExpr &expr);
     155    void visit (AST::ReturnExpr &expr);
     156    void visit (AST::UnsafeBlockExpr &expr);
     157    void visit (AST::LoopExpr &expr);
     158    void visit (AST::WhileLoopExpr &expr);
     159    void visit (AST::WhileLetLoopExpr &expr);
     160    void visit (AST::ForLoopExpr &expr);
     161    void visit (AST::IfExpr &expr);
     162    void visit (AST::IfExprConseqElse &expr);
     163    void visit (AST::IfExprConseqIf &expr);
     164    void visit (AST::IfExprConseqIfLet &expr);
     165    void visit (AST::IfLetExpr &expr);
     166    void visit (AST::IfLetExprConseqElse &expr);
     167    void visit (AST::IfLetExprConseqIf &expr);
     168    void visit (AST::IfLetExprConseqIfLet &expr);
     169    void visit (AST::MatchExpr &expr);
     170    void visit (AST::AwaitExpr &expr);
     171    void visit (AST::AsyncBlockExpr &expr);
     172  
     173    // rust-item.h
     174    void visit (AST::TypeParam &param);
     175    void visit (AST::LifetimeWhereClauseItem &item);
     176    void visit (AST::TypeBoundWhereClauseItem &item);
     177    void visit (AST::Method &method);
     178    void visit (AST::Module &module);
     179    void visit (AST::ExternCrate &crate);
     180    void visit (AST::UseTreeGlob &use_tree);
     181    void visit (AST::UseTreeList &use_tree);
     182    void visit (AST::UseTreeRebind &use_tree);
     183    void visit (AST::UseDeclaration &use_decl);
     184    void visit (AST::Function &function);
     185    void visit (AST::TypeAlias &type_alias);
     186    void visit (AST::StructStruct &struct_item);
     187    void visit (AST::TupleStruct &tuple_struct);
     188    void visit (AST::EnumItem &item);
     189    void visit (AST::EnumItemTuple &item);
     190    void visit (AST::EnumItemStruct &item);
     191    void visit (AST::EnumItemDiscriminant &item);
     192    void visit (AST::Enum &enum_item);
     193    void visit (AST::Union &union_item);
     194    void visit (AST::ConstantItem &const_item);
     195    void visit (AST::StaticItem &static_item);
     196    void visit (AST::TraitItemFunc &item);
     197    void visit (AST::TraitItemMethod &item);
     198    void visit (AST::TraitItemConst &item);
     199    void visit (AST::TraitItemType &item);
     200    void visit (AST::Trait &trait);
     201    void visit (AST::InherentImpl &impl);
     202    void visit (AST::TraitImpl &impl);
     203    void visit (AST::ExternalStaticItem &item);
     204    void visit (AST::ExternalFunctionItem &item);
     205    void visit (AST::ExternBlock &block);
     206  
     207    // rust-macro.h
     208    void visit (AST::MacroMatchFragment &match);
     209    void visit (AST::MacroMatchRepetition &match);
     210    void visit (AST::MacroMatcher &matcher);
     211    void visit (AST::MacroRulesDefinition &rules_def);
     212    void visit (AST::MacroInvocation &macro_invoc);
     213    void visit (AST::MetaItemPath &meta_item);
     214    void visit (AST::MetaItemSeq &meta_item);
     215    void visit (AST::MetaWord &meta_item);
     216    void visit (AST::MetaNameValueStr &meta_item);
     217    void visit (AST::MetaListPaths &meta_item);
     218    void visit (AST::MetaListNameValueStr &meta_item);
     219  
     220    // rust-pattern.h
     221    void visit (AST::LiteralPattern &pattern);
     222    void visit (AST::IdentifierPattern &pattern);
     223    void visit (AST::WildcardPattern &pattern);
     224    // void visit(RangePatternBound& bound);
     225    void visit (AST::RangePatternBoundLiteral &bound);
     226    void visit (AST::RangePatternBoundPath &bound);
     227    void visit (AST::RangePatternBoundQualPath &bound);
     228    void visit (AST::RangePattern &pattern);
     229    void visit (AST::ReferencePattern &pattern);
     230    // void visit(StructPatternField& field);
     231    void visit (AST::StructPatternFieldTuplePat &field);
     232    void visit (AST::StructPatternFieldIdentPat &field);
     233    void visit (AST::StructPatternFieldIdent &field);
     234    void visit (AST::StructPattern &pattern);
     235    // void visit(TupleStructItems& tuple_items);
     236    void visit (AST::TupleStructItemsNoRange &tuple_items);
     237    void visit (AST::TupleStructItemsRange &tuple_items);
     238    void visit (AST::TupleStructPattern &pattern);
     239    // void visit(TuplePatternItems& tuple_items);
     240    void visit (AST::TuplePatternItemsMultiple &tuple_items);
     241    void visit (AST::TuplePatternItemsRanged &tuple_items);
     242    void visit (AST::TuplePattern &pattern);
     243    void visit (AST::GroupedPattern &pattern);
     244    void visit (AST::SlicePattern &pattern);
     245    void visit (AST::AltPattern &pattern);
     246  
     247    // rust-stmt.h
     248    void visit (AST::EmptyStmt &stmt);
     249    void visit (AST::LetStmt &stmt);
     250    void visit (AST::ExprStmtWithoutBlock &stmt);
     251    void visit (AST::ExprStmtWithBlock &stmt);
     252  
     253    // rust-type.h
     254    void visit (AST::TraitBound &bound);
     255    void visit (AST::ImplTraitType &type);
     256    void visit (AST::TraitObjectType &type);
     257    void visit (AST::ParenthesisedType &type);
     258    void visit (AST::ImplTraitTypeOneBound &type);
     259    void visit (AST::TraitObjectTypeOneBound &type);
     260    void visit (AST::TupleType &type);
     261    void visit (AST::NeverType &type);
     262    void visit (AST::RawPointerType &type);
     263    void visit (AST::ReferenceType &type);
     264    void visit (AST::ArrayType &type);
     265    void visit (AST::SliceType &type);
     266    void visit (AST::InferredType &type);
     267    void visit (AST::BareFunctionType &type);
     268  };
     269  
     270  } // namespace Analysis
     271  } // namespace Rust