================
@@ -4188,6 +4188,109 @@ void Parser::ParseTrailingRequiresClause(Declarator &D) 
{
   }
 }
 
+/// Parse a sequence of C++26 contract specifiers (P2900R14).
+///
+/// Called from ParseFunctionDeclarator after the trailing return type,
+/// while still inside the function prototype scope (parameters are visible).
+///
+///   function-contract-specifier-seq:
+///     function-contract-specifier
+///     function-contract-specifier-seq function-contract-specifier
+///
+///   function-contract-specifier:
+///     'pre' '(' conditional-expression ')'
+///     'post' '(' result-name-introducer[opt] conditional-expression ')'
+///
+///   result-name-introducer:
+///     identifier ':'
+///
+/// 'pre' and 'post' are context-sensitive keywords (identifiers that are
+/// only special in this position). Parsed specifiers are stored in the
+/// Declarator and later converted to ContractAnnotation nodes on
+/// FunctionDecl by Sema::ActOnFunctionContractSpecifiers.
+///
+/// \param TrailingReturnType The trailing return type (if any), needed to
+///        determine the type of the result variable in post(name: expr).
+///        For non-trailing return types (e.g. `int f()`), pass ParsedType().
+void Parser::ParseContractSpecifiers(Declarator &D,
+                                     ParsedType TrailingReturnType) {
+  if (!getLangOpts().Contracts)
+    return;
+
+  // Lazily initialize context-sensitive keyword identifiers.
+  if (!Ident_pre) {
+    Ident_pre = &PP.getIdentifierTable().get("pre");
+    Ident_post = &PP.getIdentifierTable().get("post");
+  }
+
+  // Quick check: bail out early if the next token isn't 'pre' or 'post'
+  // followed by '(' to avoid unnecessary work for the common case.
+  if (!Tok.is(tok::identifier))
+    return;
+  IdentifierInfo *II = Tok.getIdentifierInfo();
+  if (II != Ident_pre && II != Ident_post)
----------------
yronglin wrote:

Can we add a help function `isContractSpecifiers` in `IdentifierInfo`? Then we 
can avoid `II != Ident_pre && II != Ident_post`.

https://github.com/llvm/llvm-project/pull/205739
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to