Skip to content

Commit 7fabcaa

Browse files
change scope to only parse primitive types for now
1 parent db1ad53 commit 7fabcaa

File tree

6 files changed

+37
-134
lines changed

6 files changed

+37
-134
lines changed

clang/include/clang/AST/ExprCXX.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "llvm/ADT/PointerUnion.h"
4545
#include "llvm/ADT/STLExtras.h"
4646
#include "llvm/ADT/StringRef.h"
47+
#include "llvm/ADT/TypeSwitch.h"
4748
#include "llvm/ADT/iterator_range.h"
4849
#include "llvm/Support/Casting.h"
4950
#include "llvm/Support/Compiler.h"
@@ -5509,35 +5510,34 @@ class BuiltinBitCastExpr final
55095510
/// - an id-expression.
55105511
class CXXReflectExpr : public Expr {
55115512

5512-
// Source locations.
5513-
SourceLocation OperatorLoc;
5514-
SourceRange OperandRange;
5513+
// TODO(Reflection): add support for TemplateReference, NamespaceReference and
5514+
// DeclRefExpr
5515+
using operand_type = llvm::PointerUnion<const TypeLoc *>;
5516+
5517+
SourceLocation CaretCaretLoc;
5518+
operand_type Operand;
55155519

5516-
CXXReflectExpr(const ASTContext &C, QualType T, QualType Operand);
5517-
CXXReflectExpr(const ASTContext &C, QualType T, Decl *Operand);
5520+
CXXReflectExpr(SourceLocation CaretCaretLoc, const TypeLoc *TL);
55185521
CXXReflectExpr(EmptyShell Empty);
55195522

55205523
public:
55215524
static CXXReflectExpr *Create(ASTContext &C, SourceLocation OperatorLoc,
5522-
SourceLocation OperandLoc, QualType Operand);
5523-
5524-
static CXXReflectExpr *Create(ASTContext &C, SourceLocation OperatorLoc,
5525-
SourceLocation OperandLoc, Decl *Operand);
5525+
TypeLoc *TL);
55265526

55275527
static CXXReflectExpr *CreateEmpty(ASTContext &C);
55285528

5529-
SourceLocation getBeginLoc() const LLVM_READONLY { return OperatorLoc; }
5529+
SourceLocation getBeginLoc() const LLVM_READONLY {
5530+
return llvm::TypeSwitch<operand_type, SourceLocation>(Operand)
5531+
.Case<const TypeLoc *>([](auto *Ptr) { return Ptr->getBeginLoc(); });
5532+
}
5533+
55305534
SourceLocation getEndLoc() const LLVM_READONLY {
5531-
return OperandRange.getEnd();
5535+
return llvm::TypeSwitch<operand_type, SourceLocation>(Operand)
5536+
.Case<const TypeLoc *>([](auto *Ptr) { return Ptr->getEndLoc(); });
55325537
}
55335538

55345539
/// Returns location of the '^^'-operator.
5535-
SourceLocation getOperatorLoc() const { return OperatorLoc; }
5536-
SourceRange getOperandRange() const { return OperandRange; }
5537-
5538-
/// Sets the location of the '^^'-operator.
5539-
void setOperatorLoc(SourceLocation L) { OperatorLoc = L; }
5540-
void setOperandRange(SourceRange R) { OperandRange = R; }
5540+
SourceLocation getOperatorLoc() const { return CaretCaretLoc; }
55415541

55425542
child_range children() {
55435543
// TODO(Reflection)

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14737,14 +14737,9 @@ class Sema final : public SemaBase {
1473714737
///@{
1473814738

1473914739
public:
14740-
ExprResult ActOnCXXReflectExpr(SourceLocation OpLoc, TypeSourceInfo *T);
14741-
ExprResult ActOnCXXReflectExpr(SourceLocation OpLoc,
14742-
SourceLocation OperandLoc, Decl *D);
14743-
14744-
ExprResult BuildCXXReflectExpr(SourceLocation OperatorLoc,
14745-
SourceLocation OperandLoc, QualType T);
14746-
ExprResult BuildCXXReflectExpr(SourceLocation OperatorLoc,
14747-
SourceLocation OperandLoc, Decl *D);
14740+
ExprResult ActOnCXXReflectExpr(SourceLocation OpLoc, TypeLoc *TL);
14741+
14742+
ExprResult BuildCXXReflectExpr(SourceLocation OperatorLoc, TypeLoc *TL);
1474814743

1474914744
public:
1475014745
void PushSatisfactionStackEntry(const NamedDecl *D,

clang/lib/AST/ExprCXX.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,34 +1939,17 @@ TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
19391939
return new (Mem) TypeTraitExpr(EmptyShell(), IsStoredAsBool);
19401940
}
19411941

1942-
CXXReflectExpr::CXXReflectExpr(const ASTContext &C, QualType T,
1943-
QualType Operand)
1944-
: Expr(CXXReflectExprClass, T, VK_PRValue, OK_Ordinary) {}
1945-
1946-
CXXReflectExpr::CXXReflectExpr(const ASTContext &C, QualType T, Decl *Operand)
1947-
: Expr(CXXReflectExprClass, T, VK_PRValue, OK_Ordinary) {}
1948-
19491942
CXXReflectExpr::CXXReflectExpr(EmptyShell Empty)
19501943
: Expr(CXXReflectExprClass, Empty) {}
19511944

1952-
CXXReflectExpr *CXXReflectExpr::Create(ASTContext &C,
1953-
SourceLocation OperatorLoc,
1954-
SourceLocation OperandLoc,
1955-
QualType Operand) {
1956-
CXXReflectExpr *E = new (C) CXXReflectExpr(C, C.VoidTy, Operand);
1957-
E->setOperatorLoc(OperatorLoc);
1958-
E->setOperandRange(OperandLoc);
1959-
return E;
1960-
}
1945+
CXXReflectExpr::CXXReflectExpr(SourceLocation CaretCaretLoc, const TypeLoc *TL)
1946+
: Expr(CXXReflectExprClass, TL->getType(), VK_PRValue, OK_Ordinary),
1947+
CaretCaretLoc(CaretCaretLoc), Operand(TL) {}
19611948

19621949
CXXReflectExpr *CXXReflectExpr::Create(ASTContext &C,
1963-
SourceLocation OperatorLoc,
1964-
SourceLocation OperandLoc,
1965-
Decl *Operand) {
1966-
CXXReflectExpr *E = new (C) CXXReflectExpr(C, C.VoidTy, Operand);
1967-
E->setOperatorLoc(OperatorLoc);
1968-
E->setOperandRange(OperandLoc);
1969-
return E;
1950+
SourceLocation CaretCaretLoc,
1951+
TypeLoc *TL) {
1952+
return new (C) CXXReflectExpr(CaretCaretLoc, TL);
19701953
}
19711954

19721955
CXXReflectExpr *CXXReflectExpr::CreateEmpty(ASTContext &C) {

clang/lib/Parse/ParseReflect.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,9 @@ ExprResult Parser::ParseCXXReflectExpression() {
2121
EnterExpressionEvaluationContext Unevaluated(
2222
Actions, Sema::ExpressionEvaluationContext::Unevaluated);
2323
assert(Tok.is(tok::caretcaret) && "expecting reflection operator ^^");
24-
SourceLocation DoubleCaretLoc = ConsumeToken();
25-
26-
CXXScopeSpec SS;
27-
if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
28-
/*ObjectHasErrors=*/false,
29-
/*EnteringContext=*/false)) {
30-
return ExprError();
31-
}
32-
24+
SourceLocation CaretCaretLoc = ConsumeToken();
3325
SourceLocation OperandLoc = Tok.getLocation();
3426

35-
if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::kw_template,
36-
tok::tilde, tok::annot_template_id)) {
37-
// TODO(reflection) : support parsing for
38-
// - type-name::
39-
// - nested-name-specifier identifier ::
40-
// - namespace-name ::
41-
// - nested-name-specifier template_opt simple-template-id
42-
Diag(OperandLoc, diag::err_cannot_reflect_operand);
43-
return ExprError();
44-
} else if (SS.isValid() &&
45-
SS.getScopeRep().getKind() == NestedNameSpecifier::Kind::Global) {
46-
// global namespace ::.
47-
Decl *TUDecl = Actions.getASTContext().getTranslationUnitDecl();
48-
return Actions.ActOnCXXReflectExpr(DoubleCaretLoc, SourceLocation(),
49-
TUDecl);
50-
}
51-
5227
if (isCXXTypeId(TentativeCXXTypeIdContext::AsReflectionOperand)) {
5328
TypeResult TR = ParseTypeName(/*TypeOf=*/nullptr);
5429
if (TR.isInvalid())
@@ -63,10 +38,11 @@ ExprResult Parser::ParseCXXReflectExpression() {
6338
if (!TSI)
6439
TSI = Actions.getASTContext().getTrivialTypeSourceInfo(QT, OperandLoc);
6540

41+
TypeLoc TL = TSI->getTypeLoc();
6642
QualType Canon = QT.getCanonicalType();
67-
if (Canon->isBuiltinType()) {
43+
if (!TL.isNull() && Canon->isBuiltinType()) {
6844
// Only supports builtin types for now
69-
return Actions.ActOnCXXReflectExpr(DoubleCaretLoc, TSI);
45+
return Actions.ActOnCXXReflectExpr(CaretCaretLoc, &TL);
7046
}
7147
}
7248

clang/lib/Sema/SemaExpr.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17781,25 +17781,14 @@ void Sema::PushExpressionEvaluationContextForFunction(
1778117781
}
1778217782
}
1778317783

17784-
ExprResult Sema::ActOnCXXReflectExpr(SourceLocation OpLoc,
17785-
TypeSourceInfo *TSI) {
17786-
return BuildCXXReflectExpr(OpLoc, TSI->getTypeLoc().getBeginLoc(),
17787-
TSI->getType());
17784+
ExprResult Sema::ActOnCXXReflectExpr(SourceLocation CaretCaretLoc,
17785+
TypeLoc *TL) {
17786+
return BuildCXXReflectExpr(CaretCaretLoc, TL);
1778817787
}
1778917788

17790-
ExprResult Sema::ActOnCXXReflectExpr(SourceLocation OpLoc,
17791-
SourceLocation OperandLoc, Decl *D) {
17792-
return BuildCXXReflectExpr(OpLoc, OperandLoc, D);
17793-
}
17794-
17795-
ExprResult Sema::BuildCXXReflectExpr(SourceLocation OperatorLoc,
17796-
SourceLocation OperandLoc, QualType T) {
17797-
return CXXReflectExpr::Create(Context, OperatorLoc, OperandLoc, T);
17798-
}
17799-
17800-
ExprResult Sema::BuildCXXReflectExpr(SourceLocation OperatorLoc,
17801-
SourceLocation OperandLoc, Decl *D) {
17802-
return CXXReflectExpr::Create(Context, OperatorLoc, OperandLoc, D);
17789+
ExprResult Sema::BuildCXXReflectExpr(SourceLocation CaretCaretLoc,
17790+
TypeLoc *TL) {
17791+
return CXXReflectExpr::Create(Context, CaretCaretLoc, TL);
1780317792
}
1780417793

1780517794
namespace {
Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
// RUN: %clang_cc1 %s -std=c++26 -freflection -fsyntax-only -verify
22

3-
namespace a {
4-
struct X {
5-
int y;
6-
bool operator==(const X&);
7-
};
8-
9-
namespace b {
10-
struct Y {};
11-
int x;
12-
}
13-
14-
template<typename T>
15-
struct Z{
16-
template<typename U>
17-
using type = U;
18-
};
19-
20-
}
3+
struct X{};
214

225
consteval void test()
236
{
24-
(void)^^::;
257
(void)(^^void);
268
(void)(^^bool);
279
(void)(^^char);
@@ -38,26 +20,4 @@ consteval void test()
3820
(void)(^^double);
3921
(void)(^^const void);
4022
(void)(^^decltype(nullptr));
41-
42-
// Not supported yet.
43-
(void)^^a; // expected-error {{expected reflectable entity}}
44-
(void)^^a::X; // expected-error {{expected reflectable entity}}
45-
(void)(^^a::b); // expected-error {{expected reflectable entity}}
46-
(void)^^a::b::Y; // expected-error {{expected reflectable entity}}
47-
(void)^^a::b::x; // expected-error {{expected reflectable entity}}
48-
(void)(^^::a::X::operator==); // expected-error {{expected reflectable entity}}
49-
(void)(^^::a::X::~X); // expected-error {{expected reflectable entity}}
50-
(void)(^^::a::Z<int>); // expected-error {{expected reflectable entity}}
51-
(void)(^^::a::Z<int>::template type<int>); // expected-error {{expected reflectable entity}}
52-
namespace c = a::b;
53-
(void)(^^c); // expected-error {{expected reflectable entity}}
54-
55-
56-
// ill-formed
57-
(void)^^a::; // expected-error {{expected reflectable entity}}
58-
(void)^^a::X::; // expected-error {{expected reflectable entity}}
59-
(void)^^a::b::; // expected-error {{expected reflectable entity}}
60-
(void)(^^::a::); // expected-error {{expected reflectable entity}}
61-
(void)^^a::b::Y::; // expected-error {{expected reflectable entity}}
62-
6323
}

0 commit comments

Comments
 (0)