Skip to content

Commit

Permalink
Make use of the Contextual visitor in validation
Browse files Browse the repository at this point in the history
Use the new contextual ast visitor to reduce the amount of code in the
ast validation visitor.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Adapt
	the call to the new visit functions.
	(ASTValidation::check): Launch the parent class visitor root function.
	* checks/errors/rust-ast-validation.h (class ASTValidation): Inherit
	from the contextual visitor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
  • Loading branch information
P-E-P committed Oct 26, 2023
1 parent 585b020 commit a698a8f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 219 deletions.
48 changes: 1 addition & 47 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,6 @@

namespace Rust {

template <typename T>
void
ASTValidation::visit (T &node)
{
node.accept_vis (*this);
}

template <typename T>
void
ASTValidation::visit (std::unique_ptr<T> &node)
{
node->accept_vis (*this);
}

void
ASTValidation::check (AST::Crate &crate)
{
push_context (Context::CRATE);
for (auto &item : crate.items)
{
visit (item);
}
pop_context ();
}

void
ASTValidation::visit (AST::InherentImpl &impl)
{
push_context (Context::INHERENT_IMPL);
for (auto &item : impl.get_impl_items ())
{
visit (item);
}
pop_context ();
}

void
ASTValidation::visit (AST::TraitImpl &impl)
{
push_context (Context::TRAIT_IMPL);
for (auto &item : impl.get_impl_items ())
{
visit (item);
}
pop_context ();
}

void
ASTValidation::visit (AST::ConstantItem &const_item)
{
Expand All @@ -76,6 +29,7 @@ ASTValidation::visit (AST::ConstantItem &const_item)
rust_error_at (const_item.get_locus (),
"associated constant in %<impl%> without body");
}
AST::ContextualASTVisitor::visit (const_item);
}

} // namespace Rust
175 changes: 3 additions & 172 deletions gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,183 +24,14 @@

namespace Rust {

class ASTValidation : public AST::ASTVisitor
class ASTValidation : public AST::ContextualASTVisitor
{
public:
ASTValidation () {}

void check (AST::Crate &crate);
void check (AST::Crate &crate) { AST::ContextualASTVisitor::visit (crate); }

template <typename T> void visit (T &node);

template <typename T> void visit (std::unique_ptr<T> &node);

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 &param) 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 &macro_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 {}

private:
enum class Context
{
INHERENT_IMPL,
TRAIT_IMPL,
CRATE,
};

std::vector<Context> context;

void push_context (Context ctx) { context.push_back (ctx); }

void pop_context () { context.pop_back (); }
virtual void visit (AST::ConstantItem &const_item);
};

} // namespace Rust
Expand Down

0 comments on commit a698a8f

Please sign in to comment.