public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7994] gccrs: Use optional for proc macro invocation lookup
@ 2024-01-16 18:11 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:11 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:8a343bd46fb454a1f5dfb47fdb6cf5599ce070ba

commit r14-7994-g8a343bd46fb454a1f5dfb47fdb6cf5599ce070ba
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Tue Aug 29 14:42:02 2023 +0200

    gccrs: Use optional for proc macro invocation lookup
    
    The old interface for those mappings was clunky at best. Now we can use
    the optional structure to return a reference to the instance.
    
    gcc/rust/ChangeLog:
    
            * expand/rust-macro-expand.h (struct MacroExpander): Update
            lookup function prototypes.
            * util/rust-hir-map.cc (Mappings::lookup_derive_proc_macro_invocation):
            Update lookup function according to new signature.
            (Mappings::lookup_bang_proc_macro_invocation): Likewise.
            (Mappings::lookup_attribute_proc_macro_invocation): Likewise.
            * util/rust-hir-map.h: Update function prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/expand/rust-macro-expand.h | 25 +++++++++++++++----------
 gcc/rust/util/rust-hir-map.cc       | 30 ++++++++++++------------------
 gcc/rust/util/rust-hir-map.h        | 13 ++++++-------
 3 files changed, 33 insertions(+), 35 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index 92103a705c8..f18e8e24a1d 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -410,8 +410,9 @@ struct MacroExpander
   template <typename T>
   AST::Fragment expand_derive_proc_macro (T &item, AST::SimplePath &path)
   {
-    CustomDeriveProcMacro macro;
-    if (!mappings->lookup_derive_proc_macro_invocation (path, macro))
+    tl::optional<CustomDeriveProcMacro &> macro
+      = mappings->lookup_derive_proc_macro_invocation (path);
+    if (!macro.has_value ())
       {
 	rust_error_at (path.get_locus (), "Macro not found");
 	return AST::Fragment::create_error ();
@@ -424,15 +425,17 @@ struct MacroExpander
     auto c = collector.collect_tokens ();
     std::vector<const_TokenPtr> vec (c.cbegin (), c.cend ());
 
-    return parse_proc_macro_output (macro.get_handle () (convert (vec)));
+    return parse_proc_macro_output (
+      macro.value ().get_handle () (convert (vec)));
   }
 
   template <typename T>
   AST::Fragment expand_bang_proc_macro (T &item,
 					AST::MacroInvocation &invocation)
   {
-    BangProcMacro macro;
-    if (!mappings->lookup_bang_proc_macro_invocation (invocation, macro))
+    tl::optional<BangProcMacro &> macro
+      = mappings->lookup_bang_proc_macro_invocation (invocation);
+    if (!macro.has_value ())
       {
 	rust_error_at (invocation.get_locus (), "Macro not found");
 	return AST::Fragment::create_error ();
@@ -445,14 +448,16 @@ struct MacroExpander
     auto c = collector.collect_tokens ();
     std::vector<const_TokenPtr> vec (c.cbegin (), c.cend ());
 
-    return parse_proc_macro_output (macro.get_handle () (convert (vec)));
+    return parse_proc_macro_output (
+      macro.value ().get_handle () (convert (vec)));
   }
 
   template <typename T>
   AST::Fragment expand_attribute_proc_macro (T &item, AST::SimplePath &path)
   {
-    AttributeProcMacro macro;
-    if (!mappings->lookup_attribute_proc_macro_invocation (path, macro))
+    tl::optional<AttributeProcMacro &> macro
+      = mappings->lookup_attribute_proc_macro_invocation (path);
+    if (!macro.has_value ())
       {
 	rust_error_at (path.get_locus (), "Macro not found");
 	return AST::Fragment::create_error ();
@@ -467,8 +472,8 @@ struct MacroExpander
 
     // FIXME: Handle attributes
     return parse_proc_macro_output (
-      macro.get_handle () (ProcMacro::TokenStream::make_tokenstream (),
-			   convert (vec)));
+      macro.value ().get_handle () (ProcMacro::TokenStream::make_tokenstream (),
+				    convert (vec)));
   }
 
   /**
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 7041aaa9945..6acec3dbe13 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1072,16 +1072,14 @@ Mappings::insert_derive_proc_macro_invocation (AST::SimplePath &invoc,
   procmacroDeriveInvocations[invoc.get_node_id ()] = def;
 }
 
-bool
-Mappings::lookup_derive_proc_macro_invocation (AST::SimplePath &invoc,
-					       CustomDeriveProcMacro &def)
+tl::optional<CustomDeriveProcMacro &>
+Mappings::lookup_derive_proc_macro_invocation (AST::SimplePath &invoc)
 {
   auto it = procmacroDeriveInvocations.find (invoc.get_node_id ());
   if (it == procmacroDeriveInvocations.end ())
-    return false;
+    return tl::nullopt;
 
-  def = it->second;
-  return true;
+  return it->second;
 }
 
 void
@@ -1094,16 +1092,14 @@ Mappings::insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc,
   procmacroBangInvocations[invoc.get_macro_node_id ()] = def;
 }
 
-bool
-Mappings::lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc,
-					     BangProcMacro &def)
+tl::optional<BangProcMacro &>
+Mappings::lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc)
 {
   auto it = procmacroBangInvocations.find (invoc.get_macro_node_id ());
   if (it == procmacroBangInvocations.end ())
-    return false;
+    return tl::nullopt;
 
-  def = it->second;
-  return true;
+  return it->second;
 }
 
 void
@@ -1116,16 +1112,14 @@ Mappings::insert_attribute_proc_macro_invocation (AST::SimplePath &invoc,
   procmacroAttributeInvocations[invoc.get_node_id ()] = def;
 }
 
-bool
-Mappings::lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc,
-						  AttributeProcMacro &def)
+tl::optional<AttributeProcMacro &>
+Mappings::lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc)
 {
   auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
   if (it == procmacroAttributeInvocations.end ())
-    return false;
+    return tl::nullopt;
 
-  def = it->second;
-  return true;
+  return it->second;
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 06d117d3ebe..177bcbacfc5 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -310,19 +310,18 @@ public:
   bool lookup_bang_proc_macro_def (NodeId id, BangProcMacro &macro);
   bool lookup_attribute_proc_macro_def (NodeId id, AttributeProcMacro &macro);
 
+  tl::optional<CustomDeriveProcMacro &>
+  lookup_derive_proc_macro_invocation (AST::SimplePath &invoc);
+  tl::optional<BangProcMacro &>
+  lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc_id);
+  tl::optional<AttributeProcMacro &>
+  lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc);
   void insert_derive_proc_macro_invocation (AST::SimplePath &invoc,
 					    CustomDeriveProcMacro def);
-
-  bool lookup_derive_proc_macro_invocation (AST::SimplePath &invoc,
-					    CustomDeriveProcMacro &def);
   void insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc,
 					  BangProcMacro def);
-  bool lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc_id,
-					  BangProcMacro &def);
   void insert_attribute_proc_macro_invocation (AST::SimplePath &invoc,
 					       AttributeProcMacro def);
-  bool lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc,
-					       AttributeProcMacro &def);
 
   void insert_visibility (NodeId id, Privacy::ModuleVisibility visibility);
   bool lookup_visibility (NodeId id, Privacy::ModuleVisibility &def);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 18:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:11 [gcc r14-7994] gccrs: Use optional for proc macro invocation lookup Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).