From 9cbcb74d71d402bfa20d1a7def6a0eea5e57d525 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Sun, 30 Apr 2023 02:54:22 -0400 Subject: Add AST::AttrInputMacro gcc/rust/ChangeLog: * ast/rust-ast-dump.cc (Dump::visit): Add AttrInputMacro visitor. * ast/rust-ast-dump.h: (Dump::visit): Likewise. * ast/rust-ast-full-decls.h (class AttrInputMacro): New. * ast/rust-ast-collector.cc (TokenCollector::visit): Add and call out to AttrInputMacro visitor. * ast/rust-ast-collector.h: (TokenCollector::visit): Add AttrInputMacro visitor. * ast/rust-ast-visitor.h: (ASTVisitor::visit): Likewise. * ast/rust-ast.cc (AttrInputMacro::accept_vis): New. (AttrInputMacro::as_string): New. (AttrInputMacro::AttrInputMacro): New. (AttrInputMacro::operator=): New. * ast/rust-ast.h: (AttrInput::AttrInputType::MACRO): New. * ast/rust-expr.h (class AttrInputMacro): New. * checks/errors/rust-feature-gate.h: (FeatureGate::visit): Add AttrInputMacro visitor. * expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise. * expand/rust-cfg-strip.h: (CfgStrip::visit): Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise. * expand/rust-expand-visitor.h: (ExpandVisitor::visit): Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-base.h: (ASTLoweringBase::visit): Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-base.h: (ResolverBase::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise. * resolve/rust-early-name-resolver.h: (EarlyNameResolver::visit): Likewise. * util/rust-attributes.cc (AttributeChecker::visit): Likewise. (check_doc_attribute): Handle AttrInputType::MACRO. * util/rust-attributes.h: (AttributeChecker::visit): Add AttrInputMacro visitor. Signed-off-by: Owen Avery --- gcc/rust/ast/rust-ast-collector.cc | 11 ++++++++ gcc/rust/ast/rust-ast-collector.h | 1 + gcc/rust/ast/rust-ast-dump.cc | 4 +++ gcc/rust/ast/rust-ast-dump.h | 1 + gcc/rust/ast/rust-ast-full-decls.h | 1 + gcc/rust/ast/rust-ast-visitor.h | 1 + gcc/rust/ast/rust-ast.cc | 24 ++++++++++++++++ gcc/rust/ast/rust-ast.h | 1 + gcc/rust/ast/rust-expr.h | 42 ++++++++++++++++++++++++++++ gcc/rust/checks/errors/rust-feature-gate.h | 1 + gcc/rust/expand/rust-cfg-strip.cc | 5 ++++ gcc/rust/expand/rust-cfg-strip.h | 1 + gcc/rust/expand/rust-expand-visitor.cc | 6 ++++ gcc/rust/expand/rust-expand-visitor.h | 1 + gcc/rust/hir/rust-ast-lower-base.cc | 3 ++ gcc/rust/hir/rust-ast-lower-base.h | 1 + gcc/rust/resolve/rust-ast-resolve-base.cc | 4 +++ gcc/rust/resolve/rust-ast-resolve-base.h | 1 + gcc/rust/resolve/rust-early-name-resolver.cc | 4 +++ gcc/rust/resolve/rust-early-name-resolver.h | 1 + gcc/rust/util/rust-attributes.cc | 5 ++++ gcc/rust/util/rust-attributes.h | 1 + 22 files changed, 120 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index afc1695..45a7609 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -170,6 +170,10 @@ TokenCollector::visit (Attribute &attrib) visit (static_cast (attrib.get_attr_input ())); break; } + case AST::AttrInput::AttrInputType::MACRO: { + visit (static_cast (attrib.get_attr_input ())); + break; + } case AST::AttrInput::AttrInputType::META_ITEM: { visit (static_cast ( attrib.get_attr_input ())); @@ -840,6 +844,13 @@ TokenCollector::visit (AttrInputLiteral &literal) } void +TokenCollector::visit (AttrInputMacro ¯o) +{ + tokens.push_back (Rust::Token::make (EQUAL, Location ())); + visit (macro.get_macro ()); +} + +void TokenCollector::visit (MetaItemLitExpr &item) { auto lit = item.get_literal (); diff --git a/gcc/rust/ast/rust-ast-collector.h b/gcc/rust/ast/rust-ast-collector.h index d4712c2..e185797 100644 --- a/gcc/rust/ast/rust-ast-collector.h +++ b/gcc/rust/ast/rust-ast-collector.h @@ -149,6 +149,7 @@ private: // rust-expr.h void visit (LiteralExpr &expr); void visit (AttrInputLiteral &attr_input); + void visit (AttrInputMacro &attr_input); void visit (MetaItemLitExpr &meta_item); void visit (MetaItemPathLit &meta_item); void visit (BorrowExpr &expr); diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 0695b76..c00ed65 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -515,6 +515,10 @@ Dump::visit (AttrInputLiteral &) {} void +Dump::visit (AttrInputMacro &) +{} + +void Dump::visit (MetaItemLitExpr &) {} diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index f014ad1..a3deced 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -137,6 +137,7 @@ private: // rust-expr.h void visit (LiteralExpr &expr); void visit (AttrInputLiteral &attr_input); + void visit (AttrInputMacro &attr_input); void visit (MetaItemLitExpr &meta_item); void visit (MetaItemPathLit &meta_item); void visit (BorrowExpr &expr); diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h index 28bee14..64b9b3d 100644 --- a/gcc/rust/ast/rust-ast-full-decls.h +++ b/gcc/rust/ast/rust-ast-full-decls.h @@ -77,6 +77,7 @@ class QualifiedPathInType; class ExprWithBlock; class LiteralExpr; class AttrInputLiteral; +class AttrInputMacro; class MetaItemLitExpr; class MetaItemPathLit; class OperatorExpr; diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h index ccc8196..7b41863 100644 --- a/gcc/rust/ast/rust-ast-visitor.h +++ b/gcc/rust/ast/rust-ast-visitor.h @@ -66,6 +66,7 @@ public: // rust-expr.h virtual void visit (LiteralExpr &expr) = 0; virtual void visit (AttrInputLiteral &attr_input) = 0; + virtual void visit (AttrInputMacro &attr_input) = 0; virtual void visit (MetaItemLitExpr &meta_item) = 0; virtual void visit (MetaItemPathLit &meta_item) = 0; virtual void visit (BorrowExpr &expr) = 0; diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index ea267f4..9a53359 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -3266,6 +3266,12 @@ AttrInputMetaItemContainer::as_string () const return str + ")"; } +std::string +AttrInputMacro::as_string () const +{ + return " = " + macro->as_string (); +} + /* Override that calls the function recursively on all items contained within * the module. */ void @@ -4220,6 +4226,18 @@ BlockExpr::strip_tail_expr () } } +// needed here because "rust-expr.h" doesn't include "rust-macro.h" +AttrInputMacro::AttrInputMacro (const AttrInputMacro &oth) + : macro (oth.macro->clone_macro_invocation_impl ()) +{} + +void +AttrInputMacro::operator= (const AttrInputMacro &oth) +{ + macro = std::unique_ptr ( + oth.macro->clone_macro_invocation_impl ()); +} + /* Visitor implementations - these are short but inlining can't happen anyway * due to virtual functions and I didn't want to make the ast header includes * any longer than they already are. */ @@ -4267,6 +4285,12 @@ AttrInputLiteral::accept_vis (ASTVisitor &vis) } void +AttrInputMacro::accept_vis (ASTVisitor &vis) +{ + vis.visit (*this); +} + +void MetaItemLitExpr::accept_vis (ASTVisitor &vis) { vis.visit (*this); diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index ccc0925..56207bd 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -597,6 +597,7 @@ public: enum AttrInputType { LITERAL, + MACRO, META_ITEM, TOKEN_TREE, }; diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 8f30965..2b23c24 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -139,6 +139,48 @@ protected: } }; +// Like an AttrInputLiteral, but stores a MacroInvocation +class AttrInputMacro : public AttrInput +{ + std::unique_ptr macro; + +public: + AttrInputMacro (std::unique_ptr macro) + : macro (std::move (macro)) + {} + + AttrInputMacro (const AttrInputMacro &oth); + + AttrInputMacro (AttrInputMacro &&oth) : macro (std::move (oth.macro)) {} + + void operator= (const AttrInputMacro &oth); + + void operator= (AttrInputMacro &&oth) { macro = std::move (oth.macro); } + + std::string as_string () const override; + + void accept_vis (ASTVisitor &vis) override; + + // assuming this can't be a cfg predicate + bool check_cfg_predicate (const Session &) const override { return false; } + + // assuming this is like AttrInputLiteral + bool is_meta_item () const override { return false; } + + std::unique_ptr &get_macro () { return macro; } + + AttrInputType get_attr_input_type () const final override + { + return AttrInput::AttrInputType::MACRO; + } + +protected: + AttrInputMacro *clone_attr_input_impl () const override + { + return new AttrInputMacro (*this); + } +}; + /* literal expr only meta item inner - TODO possibly replace with inheritance of * LiteralExpr itself? */ class MetaItemLitExpr : public MetaItemInner diff --git a/gcc/rust/checks/errors/rust-feature-gate.h b/gcc/rust/checks/errors/rust-feature-gate.h index 4bae8c0..5ce0571 100644 --- a/gcc/rust/checks/errors/rust-feature-gate.h +++ b/gcc/rust/checks/errors/rust-feature-gate.h @@ -48,6 +48,7 @@ public: 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 {} diff --git a/gcc/rust/expand/rust-cfg-strip.cc b/gcc/rust/expand/rust-cfg-strip.cc index ae06785..f9d98c0 100644 --- a/gcc/rust/expand/rust-cfg-strip.cc +++ b/gcc/rust/expand/rust-cfg-strip.cc @@ -604,6 +604,11 @@ CfgStrip::visit (AST::AttrInputLiteral &) // shouldn't require? } void +CfgStrip::visit (AST::AttrInputMacro &) +{ + // shouldn't require? +} +void CfgStrip::visit (AST::MetaItemLitExpr &) { // shouldn't require? diff --git a/gcc/rust/expand/rust-cfg-strip.h b/gcc/rust/expand/rust-cfg-strip.h index 03b47b2..7235bfa 100644 --- a/gcc/rust/expand/rust-cfg-strip.h +++ b/gcc/rust/expand/rust-cfg-strip.h @@ -81,6 +81,7 @@ public: void visit (AST::LiteralExpr &expr) override; void visit (AST::AttrInputLiteral &) override; + void visit (AST::AttrInputMacro &) override; void visit (AST::MetaItemLitExpr &) override; void visit (AST::MetaItemPathLit &) override; void visit (AST::BorrowExpr &expr) override; diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index a0cfd00..be1a7a4 100644 --- a/gcc/rust/expand/rust-expand-visitor.cc +++ b/gcc/rust/expand/rust-expand-visitor.cc @@ -324,6 +324,12 @@ ExpandVisitor::visit (AST::AttrInputLiteral &) {} void +ExpandVisitor::visit (AST::AttrInputMacro ¯o) +{ + rust_sorry_at (Location (), "macros in attributes not supported"); +} + +void ExpandVisitor::visit (AST::MetaItemLitExpr &) {} diff --git a/gcc/rust/expand/rust-expand-visitor.h b/gcc/rust/expand/rust-expand-visitor.h index 7b24518..21677ec 100644 --- a/gcc/rust/expand/rust-expand-visitor.h +++ b/gcc/rust/expand/rust-expand-visitor.h @@ -166,6 +166,7 @@ public: void visit (AST::LiteralExpr &expr) override; void visit (AST::AttrInputLiteral &) override; + void visit (AST::AttrInputMacro &) override; void visit (AST::MetaItemLitExpr &) override; void visit (AST::MetaItemPathLit &) override; void visit (AST::BorrowExpr &expr) override; diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index 38f0c3e..d7c6a3c 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -87,6 +87,9 @@ void ASTLoweringBase::visit (AST::AttrInputLiteral &) {} void +ASTLoweringBase::visit (AST::AttrInputMacro &) +{} +void ASTLoweringBase::visit (AST::MetaItemLitExpr &) {} void diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index 6bfbcfa..c8d2c27 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -94,6 +94,7 @@ public: // rust-expr.h virtual void visit (AST::LiteralExpr &expr); virtual void visit (AST::AttrInputLiteral &attr_input); + virtual void visit (AST::AttrInputMacro &attr_input); virtual void visit (AST::MetaItemLitExpr &meta_item); virtual void visit (AST::MetaItemPathLit &meta_item); virtual void visit (AST::BorrowExpr &expr); diff --git a/gcc/rust/resolve/rust-ast-resolve-base.cc b/gcc/rust/resolve/rust-ast-resolve-base.cc index 6caa9e3..3bd7e4a 100644 --- a/gcc/rust/resolve/rust-ast-resolve-base.cc +++ b/gcc/rust/resolve/rust-ast-resolve-base.cc @@ -107,6 +107,10 @@ ResolverBase::visit (AST::AttrInputLiteral &) {} void +ResolverBase::visit (AST::AttrInputMacro &) +{} + +void ResolverBase::visit (AST::MetaItemLitExpr &) {} diff --git a/gcc/rust/resolve/rust-ast-resolve-base.h b/gcc/rust/resolve/rust-ast-resolve-base.h index 327ee70..bef7f3d 100644 --- a/gcc/rust/resolve/rust-ast-resolve-base.h +++ b/gcc/rust/resolve/rust-ast-resolve-base.h @@ -48,6 +48,7 @@ public: void visit (AST::QualifiedPathInType &); void visit (AST::LiteralExpr &); void visit (AST::AttrInputLiteral &); + void visit (AST::AttrInputMacro &); void visit (AST::MetaItemLitExpr &); void visit (AST::MetaItemPathLit &); void visit (AST::BorrowExpr &); diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc b/gcc/rust/resolve/rust-early-name-resolver.cc index 3c628d0..00f661f 100644 --- a/gcc/rust/resolve/rust-early-name-resolver.cc +++ b/gcc/rust/resolve/rust-early-name-resolver.cc @@ -222,6 +222,10 @@ EarlyNameResolver::visit (AST::AttrInputLiteral &) {} void +EarlyNameResolver::visit (AST::AttrInputMacro &) +{} + +void EarlyNameResolver::visit (AST::MetaItemLitExpr &) {} diff --git a/gcc/rust/resolve/rust-early-name-resolver.h b/gcc/rust/resolve/rust-early-name-resolver.h index 9f1fc0d..44d1a18 100644 --- a/gcc/rust/resolve/rust-early-name-resolver.h +++ b/gcc/rust/resolve/rust-early-name-resolver.h @@ -142,6 +142,7 @@ private: virtual void visit (AST::QualifiedPathInType &path); virtual void visit (AST::LiteralExpr &expr); virtual void visit (AST::AttrInputLiteral &attr_input); + virtual void visit (AST::AttrInputMacro &attr_input); virtual void visit (AST::MetaItemLitExpr &meta_item); virtual void visit (AST::MetaItemPathLit &meta_item); virtual void visit (AST::BorrowExpr &expr); diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index 64f82c1..19fab50 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -163,6 +163,7 @@ check_doc_attribute (const AST::Attribute &attribute) switch (attribute.get_attr_input ().get_attr_input_type ()) { case AST::AttrInput::LITERAL: + case AST::AttrInput::MACRO: case AST::AttrInput::META_ITEM: break; // FIXME: Handle them as well @@ -281,6 +282,10 @@ AttributeChecker::visit (AST::AttrInputLiteral &) {} void +AttributeChecker::visit (AST::AttrInputMacro &) +{} + +void AttributeChecker::visit (AST::MetaItemLitExpr &) {} diff --git a/gcc/rust/util/rust-attributes.h b/gcc/rust/util/rust-attributes.h index bd635f2..806ea78 100644 --- a/gcc/rust/util/rust-attributes.h +++ b/gcc/rust/util/rust-attributes.h @@ -113,6 +113,7 @@ private: // rust-expr.h void visit (AST::LiteralExpr &expr); void visit (AST::AttrInputLiteral &attr_input); + void visit (AST::AttrInputMacro &attr_input); void visit (AST::MetaItemLitExpr &meta_item); void visit (AST::MetaItemPathLit &meta_item); void visit (AST::BorrowExpr &expr); -- cgit v1.1