diff options
author | Raiki Tamura <tamaron1203@gmail.com> | 2022-12-19 01:48:54 +0900 |
---|---|---|
committer | Raiki Tamura <tamaron1203@gmail.com> | 2023-01-10 00:38:52 +0900 |
commit | 9e5769cf45cfc703e807e51b3ad301e123b05b55 (patch) | |
tree | d97961c7c3a95c95bf02dc9b8bc4271eb2a72967 /gcc/rust/ast | |
parent | 0152926ab36ba52153f3f457f6f3bb02bb274073 (diff) | |
download | gcc-9e5769cf45cfc703e807e51b3ad301e123b05b55.zip gcc-9e5769cf45cfc703e807e51b3ad301e123b05b55.tar.gz gcc-9e5769cf45cfc703e807e51b3ad301e123b05b55.tar.bz2 |
Implement declarative macro 2.0 parser
Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
Diffstat (limited to 'gcc/rust/ast')
-rw-r--r-- | gcc/rust/ast/rust-ast-full-decls.h | 1 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-full-test.cc | 1 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast.h | 5 | ||||
-rw-r--r-- | gcc/rust/ast/rust-item.h | 2 | ||||
-rw-r--r-- | gcc/rust/ast/rust-macro.h | 65 |
5 files changed, 50 insertions, 24 deletions
diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h index 47f3321..0927adb 100644 --- a/gcc/rust/ast/rust-ast-full-decls.h +++ b/gcc/rust/ast/rust-ast-full-decls.h @@ -51,7 +51,6 @@ class Lifetime; class GenericParam; class LifetimeParam; class ConstGenericParam; -class MacroItem; class TraitItem; class InherentImplItem; class TraitImplItem; diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index c4df730..c98df51 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -1284,6 +1284,7 @@ MacroRulesDefinition::as_string () const // get outer attrs str += append_attributes (outer_attrs, OUTER); + // TODO: deal with macro_2_0 str += "macro_rules!"; str += rule_name; diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index fc7af58e..1539e78 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1350,11 +1350,6 @@ protected: } }; -// A macro item AST node - abstract base class -class MacroItem : public Item -{ -}; - // Item used in trait declarations - abstract base class class TraitItem { diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index b91aec0..206887c 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -4391,8 +4391,6 @@ protected: } }; -// Replaced with forward decls - defined in "rust-macro.h" -class MacroItem; class MacroRulesDefinition; } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index fe95ce1..52c7efb 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -23,13 +23,11 @@ #include "rust-ast.h" #include "rust-ast-fragment.h" #include "rust-location.h" +#include "rust-item.h" +#include "rust-make-unique.h" namespace Rust { namespace AST { - -// Decls as definitions moved to rust-ast.h -class MacroItem; - class MacroFragSpec { public: @@ -446,8 +444,18 @@ public: }; // A macro rules definition item AST node -class MacroRulesDefinition : public MacroItem +class MacroRulesDefinition : public VisItem { +public: + enum MacroKind + { + // Macro by Example (legacy macro rules) + MBE, + // Declarative macros 2.0 + DeclMacro, + }; + +private: std::vector<Attribute> outer_attrs; Identifier rule_name; // MacroRulesDef rules_def; @@ -460,6 +468,7 @@ class MacroRulesDefinition : public MacroItem std::function<Fragment (Location, MacroInvocData &)> associated_transcriber; // Since we can't compare std::functions, we need to use an extra boolean bool is_builtin_rule; + MacroKind kind; /** * Default function to use as an associated transcriber. This function should @@ -479,27 +488,51 @@ class MacroRulesDefinition : public MacroItem * I am not aware of the implications of this decision. The rustc spec does * mention that using the same parser for macro definitions and invocations * is "extremely self-referential and non-intuitive". */ - -public: - std::string as_string () const override; - MacroRulesDefinition (Identifier rule_name, DelimType delim_type, std::vector<MacroRule> rules, - std::vector<Attribute> outer_attrs, Location locus) - : outer_attrs (std::move (outer_attrs)), rule_name (std::move (rule_name)), + std::vector<Attribute> outer_attrs, Location locus, + MacroKind kind, Visibility vis) + : VisItem (std::move (vis), outer_attrs), + outer_attrs (std::move (outer_attrs)), rule_name (std::move (rule_name)), delim_type (delim_type), rules (std::move (rules)), locus (locus), - associated_transcriber (dummy_builtin), is_builtin_rule (false) + associated_transcriber (dummy_builtin), is_builtin_rule (false), + kind (kind) {} MacroRulesDefinition ( Identifier builtin_name, DelimType delim_type, - std::function<Fragment (Location, MacroInvocData &)> associated_transcriber) - : outer_attrs (std::vector<Attribute> ()), rule_name (builtin_name), + std::function<Fragment (Location, MacroInvocData &)> associated_transcriber, + MacroKind kind, Visibility vis) + : VisItem (std::move (vis), std::vector<Attribute> ()), + outer_attrs (std::vector<Attribute> ()), rule_name (builtin_name), delim_type (delim_type), rules (std::vector<MacroRule> ()), locus (Location ()), associated_transcriber (associated_transcriber), - is_builtin_rule (true) + is_builtin_rule (true), kind (kind) {} +public: + std::string as_string () const override; + + static std::unique_ptr<MacroRulesDefinition> + mbe (Identifier rule_name, DelimType delim_type, std::vector<MacroRule> rules, + std::vector<Attribute> outer_attrs, Location locus) + { + return Rust::make_unique<MacroRulesDefinition> ( + MacroRulesDefinition (rule_name, delim_type, rules, outer_attrs, locus, + AST::MacroRulesDefinition::MacroKind::MBE, + AST::Visibility::create_error ())); + } + + static std::unique_ptr<MacroRulesDefinition> + decl_macro (Identifier rule_name, std::vector<MacroRule> rules, + std::vector<Attribute> outer_attrs, Location locus, + Visibility vis) + { + return Rust::make_unique<MacroRulesDefinition> (MacroRulesDefinition ( + rule_name, AST::DelimType::CURLY, rules, outer_attrs, locus, + AST::MacroRulesDefinition::MacroKind::DeclMacro, vis)); + } + void accept_vis (ASTVisitor &vis) override; // Invalid if rule name is empty, so base stripping on that. @@ -549,7 +582,7 @@ protected: * compile time */ class MacroInvocation : public TypeNoBounds, public Pattern, - public MacroItem, + public Item, public TraitItem, public TraitImplItem, public InherentImplItem, |