diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-02-23 13:37:07 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-23 13:37:07 +0000 |
commit | bf92a1012264f2544e73a7a8dd0ac1e473c7f658 (patch) | |
tree | f7a3fd3afb0c2e2b2678d80dcd742da23c1b8f9a /gcc/rust/ast/rust-macro.h | |
parent | 10de9cf4f3765526a1a82a4a7d14908b58c6538c (diff) | |
parent | 48b3fe622e9a582b076c6f4b19b5e8b69891ac31 (diff) | |
download | gcc-bf92a1012264f2544e73a7a8dd0ac1e473c7f658.zip gcc-bf92a1012264f2544e73a7a8dd0ac1e473c7f658.tar.gz gcc-bf92a1012264f2544e73a7a8dd0ac1e473c7f658.tar.bz2 |
Merge #969
969: Add builtin macros framework r=CohenArthur a=CohenArthur
This PR adds bases to define new builtin macro functions.
Since we operate at the `insert_macro_def` level, this requires builtin macros to be defined, as is the case in the rust standard library:
```rust
macro_rules! assert {
($cond:expr $(,)?) => {{ /* compiler built-in */ }};
($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};
}
```
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust/ast/rust-macro.h')
-rw-r--r-- | gcc/rust/ast/rust-macro.h | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 995b255..9f8f19c 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -361,9 +361,26 @@ class MacroRulesDefinition : public MacroItem DelimType delim_type; // MacroRules rules; std::vector<MacroRule> rules; // inlined form - Location locus; + std::function<ASTFragment (Location, MacroInvocData &)> + associated_transcriber; + // Since we can't compare std::functions, we need to use an extra boolean + bool is_builtin_rule; + + /** + * Default function to use as an associated transcriber. This function should + * never be called, hence the gcc_unreachable(). + * If this function is used, then the macro is not builtin and the compiler + * should make use of the actual rules. If the macro is builtin, then another + * associated transcriber should be used + */ + static ASTFragment dummy_builtin (Location, MacroInvocData &) + { + gcc_unreachable (); + return ASTFragment::create_empty (); + } + /* NOTE: in rustc, macro definitions are considered (and parsed as) a type * of macro, whereas here they are considered part of the language itself. * I am not aware of the implications of this decision. The rustc spec does @@ -377,7 +394,17 @@ public: std::vector<MacroRule> rules, std::vector<Attribute> outer_attrs, Location locus) : outer_attrs (std::move (outer_attrs)), rule_name (std::move (rule_name)), - delim_type (delim_type), rules (std::move (rules)), locus (locus) + delim_type (delim_type), rules (std::move (rules)), locus (locus), + associated_transcriber (dummy_builtin), is_builtin_rule (false) + {} + + MacroRulesDefinition (Identifier builtin_name, DelimType delim_type, + std::function<ASTFragment (Location, MacroInvocData &)> + associated_transcriber) + : 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) {} void accept_vis (ASTVisitor &vis) override; @@ -400,6 +427,20 @@ public: std::vector<MacroRule> &get_rules () { return rules; } const std::vector<MacroRule> &get_rules () const { return rules; } + bool is_builtin () const { return is_builtin_rule; } + const std::function<ASTFragment (Location, MacroInvocData &)> & + get_builtin_transcriber () const + { + rust_assert (is_builtin ()); + return associated_transcriber; + } + void set_builtin_transcriber ( + std::function<ASTFragment (Location, MacroInvocData &)> transcriber) + { + associated_transcriber = transcriber; + is_builtin_rule = true; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ |