From b036d91bea9fc20841533c53c68dd8db64fc24a8 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Tue, 26 Nov 2024 16:39:04 +0000 Subject: [PATCH] ast: Use StackedContexts class in ContextualASTVisitor gcc/rust/ChangeLog: * ast/rust-ast-visitor.h: Replace context with StackedContexts. * ast/rust-ast-visitor.cc (ContextualASTVisitor::visit): Use new APIs. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Likewise. --- gcc/rust/ast/rust-ast-visitor.cc | 16 +++++++-------- gcc/rust/ast/rust-ast-visitor.h | 10 ++++------ gcc/rust/checks/errors/rust-ast-validation.cc | 20 ++++++++----------- gcc/rust/util/rust-stacked-contexts.h | 7 ++++--- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index a57d1a0bdd8a..e60f5ca9b85d 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -1453,33 +1453,33 @@ DefaultASTVisitor::visit (AST::VariadicParam ¶m) void ContextualASTVisitor::visit (AST::Crate &crate) { - push_context (Context::CRATE); + ctx.enter (Kind::CRATE); DefaultASTVisitor::visit (crate); - pop_context (); + ctx.exit (); } void ContextualASTVisitor::visit (AST::InherentImpl &impl) { - push_context (Context::INHERENT_IMPL); + ctx.enter (Kind::INHERENT_IMPL); DefaultASTVisitor::visit (impl); - pop_context (); + ctx.exit (); } void ContextualASTVisitor::visit (AST::TraitImpl &impl) { - push_context (Context::TRAIT_IMPL); + ctx.enter (Kind::TRAIT_IMPL); DefaultASTVisitor::visit (impl); - pop_context (); + ctx.exit (); } void ContextualASTVisitor::visit (AST::Trait &trait) { - push_context (Context::TRAIT); + ctx.enter (Kind::TRAIT); DefaultASTVisitor::visit (trait); - pop_context (); + ctx.exit (); } } // namespace AST diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h index 7d418989c049..42513fe22d3c 100644 --- a/gcc/rust/ast/rust-ast-visitor.h +++ b/gcc/rust/ast/rust-ast-visitor.h @@ -26,6 +26,7 @@ #include "rust-item.h" #include "rust-path.h" #include "rust-system.h" +#include "rust-stacked-contexts.h" namespace Rust { namespace AST { @@ -452,7 +453,7 @@ class DefaultASTVisitor : public ASTVisitor class ContextualASTVisitor : public DefaultASTVisitor { protected: - enum class Context + enum class Kind { FUNCTION, INHERENT_IMPL, @@ -461,6 +462,7 @@ class ContextualASTVisitor : public DefaultASTVisitor MODULE, CRATE, }; + using DefaultASTVisitor::visit; virtual void visit (AST::Crate &crate) override; @@ -476,11 +478,7 @@ class ContextualASTVisitor : public DefaultASTVisitor DefaultASTVisitor::visit (item); } - std::vector context; - - void push_context (Context ctx) { context.push_back (ctx); } - - void pop_context () { context.pop_back (); } + StackedContexts ctx; }; } // namespace AST diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index e219fba5ce8f..48577b98c0cb 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -56,7 +56,7 @@ ASTValidation::visit (AST::LoopLabel &label) void ASTValidation::visit (AST::ConstantItem &const_item) { - if (!const_item.has_expr () && context.back () != Context::TRAIT_IMPL) + if (!const_item.has_expr () && ctx.peek () != Kind::TRAIT_IMPL) { rust_error_at (const_item.get_locus (), "associated constant in % without body"); @@ -82,23 +82,19 @@ ASTValidation::visit (AST::Function &function) "functions cannot be both % and %"); if (qualifiers.is_const () - && (context.back () == Context::TRAIT_IMPL - || context.back () == Context::TRAIT)) + && (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT)) rust_error_at (function.get_locus (), ErrorCode::E0379, "functions in traits cannot be declared %"); // may change soon if (qualifiers.is_async () - && (context.back () == Context::TRAIT_IMPL - || context.back () == Context::TRAIT)) + && (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT)) rust_error_at (function.get_locus (), ErrorCode::E0706, "functions in traits cannot be declared %"); // if not an associated function but has a self parameter - if (context.back () != Context::TRAIT - && context.back () != Context::TRAIT_IMPL - && context.back () != Context::INHERENT_IMPL - && function.has_self_param ()) + if (ctx.peek () != Kind::TRAIT && ctx.peek () != Kind::TRAIT_IMPL + && ctx.peek () != Kind::INHERENT_IMPL && function.has_self_param ()) rust_error_at ( function.get_self_param ().get_locus (), "% parameter is only allowed in associated functions"); @@ -140,11 +136,11 @@ ASTValidation::visit (AST::Function &function) { if (!function.has_body ()) { - if (context.back () == Context::INHERENT_IMPL - || context.back () == Context::TRAIT_IMPL) + if (ctx.peek () == Kind::INHERENT_IMPL + || ctx.peek () == Kind::TRAIT_IMPL) rust_error_at (function.get_locus (), "associated function in % without body"); - else if (context.back () != Context::TRAIT) + else if (ctx.peek () != Kind::TRAIT) rust_error_at (function.get_locus (), "free function without a body"); } diff --git a/gcc/rust/util/rust-stacked-contexts.h b/gcc/rust/util/rust-stacked-contexts.h index 603db5627e94..d537d6ec451b 100644 --- a/gcc/rust/util/rust-stacked-contexts.h +++ b/gcc/rust/util/rust-stacked-contexts.h @@ -71,10 +71,11 @@ template class StackedContexts return last; } - const T& peek() { - rust_assert (!stack.empty()); + const T &peek () + { + rust_assert (!stack.empty ()); - return stack.back(); + return stack.back (); } /**