]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
parser: Add base for parsing const blocks
authorArthur Cohen <arthur.cohen@embecosm.com>
Tue, 15 Apr 2025 15:33:11 +0000 (17:33 +0200)
committerCohenArthur <arthur.cohen@embecosm.com>
Tue, 27 May 2025 11:18:07 +0000 (11:18 +0000)
gcc/rust/ChangeLog:

* parse/rust-parse-impl.h (Parser::parse_const_block_expr): New function.
* parse/rust-parse.h: Declare it.

gcc/rust/parse/rust-parse-impl.h
gcc/rust/parse/rust-parse.h

index 20b17b6292e91f33189105e7cff3fa692cc80641..35f6c899ea2255451a9734f8b46d34c3d986a6de 100644 (file)
@@ -7238,6 +7238,30 @@ Parser<ManagedTokenSource>::parse_block_expr (
                        std::move (label), locus, end_locus));
 }
 
+/* Parse a "const block", a block preceded by the `const` keyword whose
+ * statements can be const evaluated and used in constant contexts */
+template <typename ManagedTokenSource>
+std::unique_ptr<AST::ConstBlock>
+Parser<ManagedTokenSource>::parse_const_block_expr (AST::AttrVec outer_attrs,
+                                                   location_t locus)
+{
+  auto block = parse_block_expr ();
+
+  if (!block)
+    {
+      add_error (Error (locus, "failed to parse inner block in const block"));
+      skip_after_end_block ();
+
+      return nullptr;
+    }
+
+  auto block_locus = block->get_locus ();
+
+  return std::make_unique<AST::ConstBlock> (AST::AnonConst (std::move (block),
+                                                           block_locus),
+                                           locus, std::move (outer_attrs));
+}
+
 /* Parses a "grouped" expression (expression in parentheses), used to control
  * precedence. */
 template <typename ManagedTokenSource>
@@ -12460,6 +12484,9 @@ Parser<ManagedTokenSource>::null_denotation_not_path (
               "use of %qs is not allowed on the right-side of an assignment",
               tok->get_token_description ()));
       return nullptr;
+    case CONST:
+      return parse_const_block_expr (std::move (outer_attrs),
+                                    tok->get_locus ());
     default:
       if (!restrictions.expr_can_be_null)
        add_error (Error (tok->get_locus (),
index 827d91d6cbb1f49290602f105fc9e03229e30e08..1dc12af0a3aa405482ffe6d5eb9efd9b3a92852e 100644 (file)
@@ -17,6 +17,7 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef RUST_PARSE_H
 #define RUST_PARSE_H
 
+#include "rust-ast.h"
 #include "rust-item.h"
 #include "rust-lex.h"
 #include "rust-ast-full.h"
@@ -165,6 +166,10 @@ public:
                    tl::optional<AST::LoopLabel> = tl::nullopt,
                    location_t pratt_parsed_loc = UNKNOWN_LOCATION);
 
+  std::unique_ptr<AST::ConstBlock>
+  parse_const_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
+                         location_t loc = UNKNOWN_LOCATION);
+
   bool is_macro_rules_def (const_TokenPtr t);
   std::unique_ptr<AST::Item> parse_item (bool called_from_statement);
   std::unique_ptr<AST::Pattern> parse_pattern ();