From 95325d7864b950cb8cd1d85b364e07011489da0e Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Thu, 26 Oct 2023 13:26:13 +0200 Subject: [PATCH] Add a new visitor that gathers context information This visitor is intended to be used by other visitors that require context at some point for a given item. gcc/rust/ChangeLog: * ast/rust-ast-visitor.cc (ContextualASTVisitor::visit): Add multiple context saving calls. * ast/rust-ast-visitor.h (class DefaultASTVisitor): Make visit functions virtual. (class ContextualASTVisitor): Add a stack like container for the current context chain. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/ast/rust-ast-visitor.cc | 24 ++ gcc/rust/ast/rust-ast-visitor.h | 399 +++++++++++++++++-------------- 2 files changed, 239 insertions(+), 184 deletions(-) diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index b15c8edfed73..9fb10997e1fd 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -1447,5 +1447,29 @@ DefaultASTVisitor::visit (AST::BareFunctionType &type) visit (type.get_return_type ()); } +void +ContextualASTVisitor::visit (AST::Crate &crate) +{ + push_context (Context::CRATE); + DefaultASTVisitor::visit (crate); + pop_context (); +} + +void +ContextualASTVisitor::visit (AST::InherentImpl &impl) +{ + push_context (Context::INHERENT_IMPL); + DefaultASTVisitor::visit (impl); + pop_context (); +} + +void +ContextualASTVisitor::visit (AST::TraitImpl &impl) +{ + push_context (Context::TRAIT_IMPL); + DefaultASTVisitor::visit (impl); + pop_context (); +} + } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h index 92f889225b6a..94029479a385 100644 --- a/gcc/rust/ast/rust-ast-visitor.h +++ b/gcc/rust/ast/rust-ast-visitor.h @@ -232,194 +232,196 @@ class ASTVisitor class DefaultASTVisitor : public ASTVisitor { - void visit (AST::Crate &crate); - - void visit (AST::Token &tok) override; - void visit (AST::DelimTokenTree &delim_tok_tree) override; - void visit (AST::AttrInputMetaItemContainer &input) override; - void visit (AST::IdentifierExpr &ident_expr) override; - void visit (AST::Lifetime &lifetime) override; - void visit (AST::LifetimeParam &lifetime_param) override; - void visit (AST::ConstGenericParam &const_param) override; - void visit (AST::PathInExpression &path) override; - void visit (AST::TypePathSegment &segment) override; - void visit (AST::TypePathSegmentGeneric &segment) override; - void visit (AST::TypePathSegmentFunction &segment) override; - void visit (AST::TypePath &path) override; - void visit (AST::QualifiedPathInExpression &path) override; - void visit (AST::QualifiedPathInType &path) override; - void visit (AST::LiteralExpr &expr) override; - void visit (AST::AttrInputLiteral &attr_input) override; - void visit (AST::AttrInputMacro &attr_input) override; - void visit (AST::MetaItemLitExpr &meta_item) override; - void visit (AST::MetaItemPathLit &meta_item) override; - void visit (AST::BorrowExpr &expr) override; - void visit (AST::DereferenceExpr &expr) override; - void visit (AST::ErrorPropagationExpr &expr) override; - void visit (AST::NegationExpr &expr) override; - void visit (AST::ArithmeticOrLogicalExpr &expr) override; - void visit (AST::ComparisonExpr &expr) override; - void visit (AST::LazyBooleanExpr &expr) override; - void visit (AST::TypeCastExpr &expr) override; - void visit (AST::AssignmentExpr &expr) override; - void visit (AST::CompoundAssignmentExpr &expr) override; - void visit (AST::GroupedExpr &expr) override; - void visit (AST::ArrayElemsValues &elems) override; - void visit (AST::ArrayElemsCopied &elems) override; - void visit (AST::ArrayExpr &expr) override; - void visit (AST::ArrayIndexExpr &expr) override; - void visit (AST::TupleExpr &expr) override; - void visit (AST::TupleIndexExpr &expr) override; - void visit (AST::StructExprStruct &expr) override; - void visit (AST::StructExprFieldIdentifier &field) override; - void visit (AST::StructExprFieldIdentifierValue &field) override; - void visit (AST::StructExprFieldIndexValue &field) override; - void visit (AST::StructExprStructFields &expr) override; - void visit (AST::StructExprStructBase &expr) override; - void visit (AST::CallExpr &expr) override; - void visit (AST::MethodCallExpr &expr) override; - void visit (AST::FieldAccessExpr &expr) override; - void visit (AST::ClosureExprInner &expr) override; - void visit (AST::BlockExpr &expr) override; - void visit (AST::ClosureExprInnerTyped &expr) override; - void visit (AST::ContinueExpr &expr) override; - void visit (AST::BreakExpr &expr) override; - void visit (AST::RangeFromToExpr &expr) override; - void visit (AST::RangeFromExpr &expr) override; - void visit (AST::RangeToExpr &expr) override; - void visit (AST::RangeFullExpr &expr) override; - void visit (AST::RangeFromToInclExpr &expr) override; - void visit (AST::RangeToInclExpr &expr) override; - void visit (AST::ReturnExpr &expr) override; - void visit (AST::UnsafeBlockExpr &expr) override; - void visit (AST::LoopExpr &expr) override; - void visit (AST::WhileLoopExpr &expr) override; - void visit (AST::WhileLetLoopExpr &expr) override; - void visit (AST::ForLoopExpr &expr) override; - void visit (AST::IfExpr &expr) override; - void visit (AST::IfExprConseqElse &expr) override; - void visit (AST::IfLetExpr &expr) override; - void visit (AST::IfLetExprConseqElse &expr) override; - void visit (AST::MatchExpr &expr) override; - void visit (AST::AwaitExpr &expr) override; - void visit (AST::AsyncBlockExpr &expr) override; - void visit (AST::TypeParam ¶m) override; - void visit (AST::LifetimeWhereClauseItem &item) override; - void visit (AST::TypeBoundWhereClauseItem &item) override; - void visit (AST::Method &method) override; - void visit (AST::Module &module) override; - void visit (AST::ExternCrate &crate) override; - void visit (AST::UseTreeGlob &use_tree) override; - void visit (AST::UseTreeList &use_tree) override; - void visit (AST::UseTreeRebind &use_tree) override; - void visit (AST::UseDeclaration &use_decl) override; - void visit (AST::Function &function) override; - void visit (AST::TypeAlias &type_alias) override; - void visit (AST::StructStruct &struct_item) override; - void visit (AST::TupleStruct &tuple_struct) override; - void visit (AST::EnumItem &item) override; - void visit (AST::EnumItemTuple &item) override; - void visit (AST::EnumItemStruct &item) override; - void visit (AST::EnumItemDiscriminant &item) override; - void visit (AST::Enum &enum_item) override; - void visit (AST::Union &union_item) override; - void visit (AST::ConstantItem &const_item) override; - void visit (AST::StaticItem &static_item) override; - void visit (AST::TraitItemFunc &item) override; - void visit (AST::TraitItemMethod &item) override; - void visit (AST::TraitItemConst &item) override; - void visit (AST::TraitItemType &item) override; - void visit (AST::Trait &trait) override; - void visit (AST::InherentImpl &impl) override; - void visit (AST::TraitImpl &impl) override; - void visit (AST::ExternalTypeItem &item) override; - void visit (AST::ExternalStaticItem &item) override; - void visit (AST::ExternalFunctionItem &item) override; - void visit (AST::ExternBlock &block) override; - void visit (AST::MacroMatchFragment &match) override; - void visit (AST::MacroMatchRepetition &match) override; - void visit (AST::MacroMatcher &matcher) override; - void visit (AST::MacroRulesDefinition &rules_def) override; - void visit (AST::MacroInvocation ¯o_invoc) override; - void visit (AST::MetaItemPath &meta_item) override; - void visit (AST::MetaItemSeq &meta_item) override; - void visit (AST::MetaWord &meta_item) override; - void visit (AST::MetaNameValueStr &meta_item) override; - void visit (AST::MetaListPaths &meta_item) override; - void visit (AST::MetaListNameValueStr &meta_item) override; - void visit (AST::LiteralPattern &pattern) override; - void visit (AST::IdentifierPattern &pattern) override; - void visit (AST::WildcardPattern &pattern) override; - void visit (AST::RestPattern &pattern) override; - void visit (AST::RangePatternBoundLiteral &bound) override; - void visit (AST::RangePatternBoundPath &bound) override; - void visit (AST::RangePatternBoundQualPath &bound) override; - void visit (AST::RangePattern &pattern) override; - void visit (AST::ReferencePattern &pattern) override; - void visit (AST::StructPatternFieldTuplePat &field) override; - void visit (AST::StructPatternFieldIdentPat &field) override; - void visit (AST::StructPatternFieldIdent &field) override; - void visit (AST::StructPattern &pattern) override; - void visit (AST::TupleStructItemsNoRange &tuple_items) override; - void visit (AST::TupleStructItemsRange &tuple_items) override; - void visit (AST::TupleStructPattern &pattern) override; - void visit (AST::TuplePatternItemsMultiple &tuple_items) override; - void visit (AST::TuplePatternItemsRanged &tuple_items) override; - void visit (AST::TuplePattern &pattern) override; - void visit (AST::GroupedPattern &pattern) override; - void visit (AST::SlicePattern &pattern) override; - void visit (AST::AltPattern &pattern) override; - void visit (AST::EmptyStmt &stmt) override; - void visit (AST::LetStmt &stmt) override; - void visit (AST::ExprStmt &stmt) override; - void visit (AST::TraitBound &bound) override; - void visit (AST::ImplTraitType &type) override; - void visit (AST::TraitObjectType &type) override; - void visit (AST::ParenthesisedType &type) override; - void visit (AST::ImplTraitTypeOneBound &type) override; - void visit (AST::TraitObjectTypeOneBound &type) override; - void visit (AST::TupleType &type) override; - void visit (AST::NeverType &type) override; - void visit (AST::RawPointerType &type) override; - void visit (AST::ReferenceType &type) override; - void visit (AST::ArrayType &type) override; - void visit (AST::SliceType &type) override; - void visit (AST::InferredType &type) override; - void visit (AST::BareFunctionType &type) override; +public: + virtual void visit (AST::Crate &crate); + +protected: + virtual void visit (AST::Token &tok) override; + virtual void visit (AST::DelimTokenTree &delim_tok_tree) override; + virtual void visit (AST::AttrInputMetaItemContainer &input) override; + virtual void visit (AST::IdentifierExpr &ident_expr) override; + virtual void visit (AST::Lifetime &lifetime) override; + virtual void visit (AST::LifetimeParam &lifetime_param) override; + virtual void visit (AST::ConstGenericParam &const_param) override; + virtual void visit (AST::PathInExpression &path) override; + virtual void visit (AST::TypePathSegment &segment) override; + virtual void visit (AST::TypePathSegmentGeneric &segment) override; + virtual void visit (AST::TypePathSegmentFunction &segment) override; + virtual void visit (AST::TypePath &path) override; + virtual void visit (AST::QualifiedPathInExpression &path) override; + virtual void visit (AST::QualifiedPathInType &path) override; + virtual void visit (AST::LiteralExpr &expr) override; + virtual void visit (AST::AttrInputLiteral &attr_input) override; + virtual void visit (AST::AttrInputMacro &attr_input) override; + virtual void visit (AST::MetaItemLitExpr &meta_item) override; + virtual void visit (AST::MetaItemPathLit &meta_item) override; + virtual void visit (AST::BorrowExpr &expr) override; + virtual void visit (AST::DereferenceExpr &expr) override; + virtual void visit (AST::ErrorPropagationExpr &expr) override; + virtual void visit (AST::NegationExpr &expr) override; + virtual void visit (AST::ArithmeticOrLogicalExpr &expr) override; + virtual void visit (AST::ComparisonExpr &expr) override; + virtual void visit (AST::LazyBooleanExpr &expr) override; + virtual void visit (AST::TypeCastExpr &expr) override; + virtual void visit (AST::AssignmentExpr &expr) override; + virtual void visit (AST::CompoundAssignmentExpr &expr) override; + virtual void visit (AST::GroupedExpr &expr) override; + virtual void visit (AST::ArrayElemsValues &elems) override; + virtual void visit (AST::ArrayElemsCopied &elems) override; + virtual void visit (AST::ArrayExpr &expr) override; + virtual void visit (AST::ArrayIndexExpr &expr) override; + virtual void visit (AST::TupleExpr &expr) override; + virtual void visit (AST::TupleIndexExpr &expr) override; + virtual void visit (AST::StructExprStruct &expr) override; + virtual void visit (AST::StructExprFieldIdentifier &field) override; + virtual void visit (AST::StructExprFieldIdentifierValue &field) override; + virtual void visit (AST::StructExprFieldIndexValue &field) override; + virtual void visit (AST::StructExprStructFields &expr) override; + virtual void visit (AST::StructExprStructBase &expr) override; + virtual void visit (AST::CallExpr &expr) override; + virtual void visit (AST::MethodCallExpr &expr) override; + virtual void visit (AST::FieldAccessExpr &expr) override; + virtual void visit (AST::ClosureExprInner &expr) override; + virtual void visit (AST::BlockExpr &expr) override; + virtual void visit (AST::ClosureExprInnerTyped &expr) override; + virtual void visit (AST::ContinueExpr &expr) override; + virtual void visit (AST::BreakExpr &expr) override; + virtual void visit (AST::RangeFromToExpr &expr) override; + virtual void visit (AST::RangeFromExpr &expr) override; + virtual void visit (AST::RangeToExpr &expr) override; + virtual void visit (AST::RangeFullExpr &expr) override; + virtual void visit (AST::RangeFromToInclExpr &expr) override; + virtual void visit (AST::RangeToInclExpr &expr) override; + virtual void visit (AST::ReturnExpr &expr) override; + virtual void visit (AST::UnsafeBlockExpr &expr) override; + virtual void visit (AST::LoopExpr &expr) override; + virtual void visit (AST::WhileLoopExpr &expr) override; + virtual void visit (AST::WhileLetLoopExpr &expr) override; + virtual void visit (AST::ForLoopExpr &expr) override; + virtual void visit (AST::IfExpr &expr) override; + virtual void visit (AST::IfExprConseqElse &expr) override; + virtual void visit (AST::IfLetExpr &expr) override; + virtual void visit (AST::IfLetExprConseqElse &expr) override; + virtual void visit (AST::MatchExpr &expr) override; + virtual void visit (AST::AwaitExpr &expr) override; + virtual void visit (AST::AsyncBlockExpr &expr) override; + virtual void visit (AST::TypeParam ¶m) override; + virtual void visit (AST::LifetimeWhereClauseItem &item) override; + virtual void visit (AST::TypeBoundWhereClauseItem &item) override; + virtual void visit (AST::Method &method) override; + virtual void visit (AST::Module &module) override; + virtual void visit (AST::ExternCrate &crate) override; + virtual void visit (AST::UseTreeGlob &use_tree) override; + virtual void visit (AST::UseTreeList &use_tree) override; + virtual void visit (AST::UseTreeRebind &use_tree) override; + virtual void visit (AST::UseDeclaration &use_decl) override; + virtual void visit (AST::Function &function) override; + virtual void visit (AST::TypeAlias &type_alias) override; + virtual void visit (AST::StructStruct &struct_item) override; + virtual void visit (AST::TupleStruct &tuple_struct) override; + virtual void visit (AST::EnumItem &item) override; + virtual void visit (AST::EnumItemTuple &item) override; + virtual void visit (AST::EnumItemStruct &item) override; + virtual void visit (AST::EnumItemDiscriminant &item) override; + virtual void visit (AST::Enum &enum_item) override; + virtual void visit (AST::Union &union_item) override; + virtual void visit (AST::ConstantItem &const_item) override; + virtual void visit (AST::StaticItem &static_item) override; + virtual void visit (AST::TraitItemFunc &item) override; + virtual void visit (AST::TraitItemMethod &item) override; + virtual void visit (AST::TraitItemConst &item) override; + virtual void visit (AST::TraitItemType &item) override; + virtual void visit (AST::Trait &trait) override; + virtual void visit (AST::InherentImpl &impl) override; + virtual void visit (AST::TraitImpl &impl) override; + virtual void visit (AST::ExternalTypeItem &item) override; + virtual void visit (AST::ExternalStaticItem &item) override; + virtual void visit (AST::ExternalFunctionItem &item) override; + virtual void visit (AST::ExternBlock &block) override; + virtual void visit (AST::MacroMatchFragment &match) override; + virtual void visit (AST::MacroMatchRepetition &match) override; + virtual void visit (AST::MacroMatcher &matcher) override; + virtual void visit (AST::MacroRulesDefinition &rules_def) override; + virtual void visit (AST::MacroInvocation ¯o_invoc) override; + virtual void visit (AST::MetaItemPath &meta_item) override; + virtual void visit (AST::MetaItemSeq &meta_item) override; + virtual void visit (AST::MetaWord &meta_item) override; + virtual void visit (AST::MetaNameValueStr &meta_item) override; + virtual void visit (AST::MetaListPaths &meta_item) override; + virtual void visit (AST::MetaListNameValueStr &meta_item) override; + virtual void visit (AST::LiteralPattern &pattern) override; + virtual void visit (AST::IdentifierPattern &pattern) override; + virtual void visit (AST::WildcardPattern &pattern) override; + virtual void visit (AST::RestPattern &pattern) override; + virtual void visit (AST::RangePatternBoundLiteral &bound) override; + virtual void visit (AST::RangePatternBoundPath &bound) override; + virtual void visit (AST::RangePatternBoundQualPath &bound) override; + virtual void visit (AST::RangePattern &pattern) override; + virtual void visit (AST::ReferencePattern &pattern) override; + virtual void visit (AST::StructPatternFieldTuplePat &field) override; + virtual void visit (AST::StructPatternFieldIdentPat &field) override; + virtual void visit (AST::StructPatternFieldIdent &field) override; + virtual void visit (AST::StructPattern &pattern) override; + virtual void visit (AST::TupleStructItemsNoRange &tuple_items) override; + virtual void visit (AST::TupleStructItemsRange &tuple_items) override; + virtual void visit (AST::TupleStructPattern &pattern) override; + virtual void visit (AST::TuplePatternItemsMultiple &tuple_items) override; + virtual void visit (AST::TuplePatternItemsRanged &tuple_items) override; + virtual void visit (AST::TuplePattern &pattern) override; + virtual void visit (AST::GroupedPattern &pattern) override; + virtual void visit (AST::SlicePattern &pattern) override; + virtual void visit (AST::AltPattern &pattern) override; + virtual void visit (AST::EmptyStmt &stmt) override; + virtual void visit (AST::LetStmt &stmt) override; + virtual void visit (AST::ExprStmt &stmt) override; + virtual void visit (AST::TraitBound &bound) override; + virtual void visit (AST::ImplTraitType &type) override; + virtual void visit (AST::TraitObjectType &type) override; + virtual void visit (AST::ParenthesisedType &type) override; + virtual void visit (AST::ImplTraitTypeOneBound &type) override; + virtual void visit (AST::TraitObjectTypeOneBound &type) override; + virtual void visit (AST::TupleType &type) override; + virtual void visit (AST::NeverType &type) override; + virtual void visit (AST::RawPointerType &type) override; + virtual void visit (AST::ReferenceType &type) override; + virtual void visit (AST::ArrayType &type) override; + virtual void visit (AST::SliceType &type) override; + virtual void visit (AST::InferredType &type) override; + virtual void visit (AST::BareFunctionType &type) override; template void visit (T &node); template void visit (std::unique_ptr &node); -private: - void visit (AST::GenericArgsBinding &binding); - void visit (AST::PathExprSegment &segment); - void visit (AST::GenericArgs &args); - void visit (AST::QualifiedPathType &path); - void visit (AST::TypePathFunction &tpf); - void visit (AST::PathIdentSegment &segment); - void visit (AST::SimplePath &path); - void visit (AST::SimplePathSegment &segment); - void visit (AST::StructBase &base); - void visit (AST::ClosureParam ¶m); - void visit (AST::LoopLabel &label); - void visit (AST::MatchCase &arm); - void visit (AST::MatchArm &arm); - void visit (AST::Visibility &vis); - void visit (AST::FunctionQualifiers &qualifiers); - void visit (AST::SelfParam &self); - void visit (AST::WhereClause &where); - void visit (AST::FunctionParam ¶m); - void visit (AST::StructField &field); - void visit (AST::TupleField &field); - void visit (AST::TraitFunctionDecl &decl); - void visit (AST::TraitMethodDecl &decl); - void visit (AST::NamedFunctionParam ¶m); - void visit (AST::MacroRule &rule); - void visit (AST::MacroInvocData &data); - void visit (AST::MacroTranscriber &transcriber); - void visit (AST::StructPatternElements &spe); - void visit (AST::MaybeNamedParam ¶m); + virtual void visit (AST::GenericArgsBinding &binding); + virtual void visit (AST::PathExprSegment &segment); + virtual void visit (AST::GenericArgs &args); + virtual void visit (AST::QualifiedPathType &path); + virtual void visit (AST::TypePathFunction &tpf); + virtual void visit (AST::PathIdentSegment &segment); + virtual void visit (AST::SimplePath &path); + virtual void visit (AST::SimplePathSegment &segment); + virtual void visit (AST::StructBase &base); + virtual void visit (AST::ClosureParam ¶m); + virtual void visit (AST::LoopLabel &label); + virtual void visit (AST::MatchCase &arm); + virtual void visit (AST::MatchArm &arm); + virtual void visit (AST::Visibility &vis); + virtual void visit (AST::FunctionQualifiers &qualifiers); + virtual void visit (AST::SelfParam &self); + virtual void visit (AST::WhereClause &where); + virtual void visit (AST::FunctionParam ¶m); + virtual void visit (AST::StructField &field); + virtual void visit (AST::TupleField &field); + virtual void visit (AST::TraitFunctionDecl &decl); + virtual void visit (AST::TraitMethodDecl &decl); + virtual void visit (AST::NamedFunctionParam ¶m); + virtual void visit (AST::MacroRule &rule); + virtual void visit (AST::MacroInvocData &data); + virtual void visit (AST::MacroTranscriber &transcriber); + virtual void visit (AST::StructPatternElements &spe); + virtual void visit (AST::MaybeNamedParam ¶m); + void visit (AST::Attribute &attribute) {} template void visit_outer_attrs (T &node) { @@ -432,8 +434,37 @@ class DefaultASTVisitor : public ASTVisitor for (auto &attr : node.get_inner_attrs ()) visit (attr); } +}; - void visit (AST::Attribute &attribute) {} +class ContextualASTVisitor : public DefaultASTVisitor +{ +protected: + enum class Context + { + FUNCTION, + INHERENT_IMPL, + TRAIT_IMPL, + MODULE, + CRATE, + }; + using DefaultASTVisitor::visit; + + virtual void visit (AST::Crate &crate) override; + + virtual void visit (AST::InherentImpl &impl) override; + + virtual void visit (AST::TraitImpl &impl) override; + + template void visit (T &item) + { + DefaultASTVisitor::visit (item); + } + + std::vector context; + + void push_context (Context ctx) { context.push_back (ctx); } + + void pop_context () { context.pop_back (); } }; } // namespace AST