[clang] [clang-tools-extra] [llvm] [clang]: reflection operator parsing for global namespace and primitive types (PR #164692)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 28 19:16:02 PDT 2025


================
@@ -0,0 +1,78 @@
+//===--- ParseReflect.cpp - C++26 Reflection Parsing ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements parsing for reflection facilities.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/LocInfoType.h"
+#include "clang/Basic/DiagnosticParse.h"
+#include "clang/Parse/Parser.h"
+#include "clang/Sema/EnterExpressionEvaluationContext.h"
+using namespace clang;
+
+ExprResult Parser::ParseCXXReflectExpression(SourceLocation DoubleCaretLoc) {
+  // TODO(reflection) : support parsing for more reflect-expressions.
+  EnterExpressionEvaluationContext Unevaluated(
+      Actions, Sema::ExpressionEvaluationContext::Unevaluated);
+
+  CXXScopeSpec SS;
+  if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
+                                     /*ObjectHasErrors=*/false,
+                                     /*EnteringContext=*/false)) {
+    SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
+    return ExprError();
+  }
+
+  SourceLocation OperandLoc = Tok.getLocation();
+  TentativeParsingAction TPA(*this);
+
+  if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::kw_template,
+                  tok::tilde, tok::annot_template_id)) {
+    // TODO(reflection) : support parsing for
+    // - type-name::
+    // - nested-name-specifier identifier ::
+    // - namespace-name ::
+    // - nested-name-specifier template_opt simple-template-id
+    TPA.Revert();
+    Diag(OperandLoc, diag::err_cannot_reflect_operand);
+    return ExprError();
+  } else if (SS.isValid() &&
+             SS.getScopeRep().getKind() == NestedNameSpecifier::Kind::Global) {
+    // global namespace ::.
+    TPA.Commit();
+    Decl *TUDecl = Actions.getASTContext().getTranslationUnitDecl();
+    return Actions.ActOnCXXReflectExpr(DoubleCaretLoc, SourceLocation(),
+                                       TUDecl);
+  }
+  TPA.Revert();
----------------
Sirraide wrote:

The `TentativeParsingAction` isn’t doing anything at the moment since you’re not doing any parsing while it’s engaged; we might need it once we support more things, but we should add only at that point imo.

https://github.com/llvm/llvm-project/pull/164692


More information about the cfe-commits mailing list