Skip to content

Commit 5434de6

Browse files
committed
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.
1 parent 3abf724 commit 5434de6

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

gcc/rust/ast/rust-ast-visitor.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -1453,33 +1453,33 @@ DefaultASTVisitor::visit (AST::VariadicParam &param)
14531453
void
14541454
ContextualASTVisitor::visit (AST::Crate &crate)
14551455
{
1456-
push_context (Context::CRATE);
1456+
ctx.enter (Kind::CRATE);
14571457
DefaultASTVisitor::visit (crate);
1458-
pop_context ();
1458+
ctx.exit ();
14591459
}
14601460

14611461
void
14621462
ContextualASTVisitor::visit (AST::InherentImpl &impl)
14631463
{
1464-
push_context (Context::INHERENT_IMPL);
1464+
ctx.enter (Kind::INHERENT_IMPL);
14651465
DefaultASTVisitor::visit (impl);
1466-
pop_context ();
1466+
ctx.exit ();
14671467
}
14681468

14691469
void
14701470
ContextualASTVisitor::visit (AST::TraitImpl &impl)
14711471
{
1472-
push_context (Context::TRAIT_IMPL);
1472+
ctx.enter (Kind::TRAIT_IMPL);
14731473
DefaultASTVisitor::visit (impl);
1474-
pop_context ();
1474+
ctx.exit ();
14751475
}
14761476

14771477
void
14781478
ContextualASTVisitor::visit (AST::Trait &trait)
14791479
{
1480-
push_context (Context::TRAIT);
1480+
ctx.enter (Kind::TRAIT);
14811481
DefaultASTVisitor::visit (trait);
1482-
pop_context ();
1482+
ctx.exit ();
14831483
}
14841484

14851485
} // namespace AST

gcc/rust/ast/rust-ast-visitor.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "rust-item.h"
2727
#include "rust-path.h"
2828
#include "rust-system.h"
29+
#include "rust-stacked-contexts.h"
2930

3031
namespace Rust {
3132
namespace AST {
@@ -452,7 +453,7 @@ class DefaultASTVisitor : public ASTVisitor
452453
class ContextualASTVisitor : public DefaultASTVisitor
453454
{
454455
protected:
455-
enum class Context
456+
enum class Kind
456457
{
457458
FUNCTION,
458459
INHERENT_IMPL,
@@ -461,6 +462,7 @@ class ContextualASTVisitor : public DefaultASTVisitor
461462
MODULE,
462463
CRATE,
463464
};
465+
464466
using DefaultASTVisitor::visit;
465467

466468
virtual void visit (AST::Crate &crate) override;
@@ -476,11 +478,7 @@ class ContextualASTVisitor : public DefaultASTVisitor
476478
DefaultASTVisitor::visit (item);
477479
}
478480

479-
std::vector<Context> context;
480-
481-
void push_context (Context ctx) { context.push_back (ctx); }
482-
483-
void pop_context () { context.pop_back (); }
481+
StackedContexts<Kind> ctx;
484482
};
485483

486484
} // namespace AST

gcc/rust/checks/errors/rust-ast-validation.cc

+8-12
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ASTValidation::visit (AST::LoopLabel &label)
5656
void
5757
ASTValidation::visit (AST::ConstantItem &const_item)
5858
{
59-
if (!const_item.has_expr () && context.back () != Context::TRAIT_IMPL)
59+
if (!const_item.has_expr () && ctx.peek () != Kind::TRAIT_IMPL)
6060
{
6161
rust_error_at (const_item.get_locus (),
6262
"associated constant in %<impl%> without body");
@@ -82,23 +82,19 @@ ASTValidation::visit (AST::Function &function)
8282
"functions cannot be both %<const%> and %<async%>");
8383

8484
if (qualifiers.is_const ()
85-
&& (context.back () == Context::TRAIT_IMPL
86-
|| context.back () == Context::TRAIT))
85+
&& (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT))
8786
rust_error_at (function.get_locus (), ErrorCode::E0379,
8887
"functions in traits cannot be declared %<const%>");
8988

9089
// may change soon
9190
if (qualifiers.is_async ()
92-
&& (context.back () == Context::TRAIT_IMPL
93-
|| context.back () == Context::TRAIT))
91+
&& (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT))
9492
rust_error_at (function.get_locus (), ErrorCode::E0706,
9593
"functions in traits cannot be declared %<async%>");
9694

9795
// if not an associated function but has a self parameter
98-
if (context.back () != Context::TRAIT
99-
&& context.back () != Context::TRAIT_IMPL
100-
&& context.back () != Context::INHERENT_IMPL
101-
&& function.has_self_param ())
96+
if (ctx.peek () != Kind::TRAIT && ctx.peek () != Kind::TRAIT_IMPL
97+
&& ctx.peek () != Kind::INHERENT_IMPL && function.has_self_param ())
10298
rust_error_at (
10399
function.get_self_param ().get_locus (),
104100
"%<self%> parameter is only allowed in associated functions");
@@ -140,11 +136,11 @@ ASTValidation::visit (AST::Function &function)
140136
{
141137
if (!function.has_body ())
142138
{
143-
if (context.back () == Context::INHERENT_IMPL
144-
|| context.back () == Context::TRAIT_IMPL)
139+
if (ctx.peek () == Kind::INHERENT_IMPL
140+
|| ctx.peek () == Kind::TRAIT_IMPL)
145141
rust_error_at (function.get_locus (),
146142
"associated function in %<impl%> without body");
147-
else if (context.back () != Context::TRAIT)
143+
else if (ctx.peek () != Kind::TRAIT)
148144
rust_error_at (function.get_locus (),
149145
"free function without a body");
150146
}

0 commit comments

Comments
 (0)