Skip to content

Commit bd8d432

Browse files
authored
[clang][bytecode] Add support for creating dummies for expressions (#108394)
And use that to fix VisitObjCBoxedExprs.
1 parent 43fd2c4 commit bd8d432

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,7 +3335,11 @@ bool Compiler<Emitter>::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
33353335
if (!E->isExpressibleAsConstantInitializer())
33363336
return this->discard(SubExpr) && this->emitInvalid(E);
33373337

3338-
return this->delegate(SubExpr);
3338+
assert(classifyPrim(E) == PT_Ptr);
3339+
if (std::optional<unsigned> I = P.getOrCreateDummy(E))
3340+
return this->emitGetPtrGlobal(*I, E);
3341+
3342+
return false;
33393343
}
33403344

33413345
template <class Emitter>
@@ -4118,7 +4122,7 @@ bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E,
41184122
BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
41194123
BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
41204124
BuiltinID == Builtin::BI__builtin_function_start) {
4121-
if (std::optional<unsigned> GlobalOffset = P.createGlobal(E)) {
4125+
if (std::optional<unsigned> GlobalOffset = P.getOrCreateDummy(E)) {
41224126
if (!this->emitGetPtrGlobal(*GlobalOffset, E))
41234127
return false;
41244128

clang/lib/AST/ByteCode/Program.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,33 @@ std::optional<unsigned> Program::getOrCreateGlobal(const ValueDecl *VD,
144144
return std::nullopt;
145145
}
146146

147-
std::optional<unsigned> Program::getOrCreateDummy(const ValueDecl *VD) {
147+
std::optional<unsigned> Program::getOrCreateDummy(const DeclTy &D) {
148+
assert(D);
148149
// Dedup blocks since they are immutable and pointers cannot be compared.
149-
if (auto It = DummyVariables.find(VD); It != DummyVariables.end())
150+
if (auto It = DummyVariables.find(D.getOpaqueValue());
151+
It != DummyVariables.end())
150152
return It->second;
151153

152-
QualType QT = VD->getType();
153-
if (const auto *RT = QT->getAs<ReferenceType>())
154-
QT = RT->getPointeeType();
154+
QualType QT;
155+
if (const auto *E = D.dyn_cast<const Expr *>()) {
156+
QT = E->getType();
157+
} else {
158+
const ValueDecl *VD = cast<ValueDecl>(D.get<const Decl *>());
159+
QT = VD->getType();
160+
if (const auto *RT = QT->getAs<ReferenceType>())
161+
QT = RT->getPointeeType();
162+
}
163+
assert(!QT.isNull());
155164

156165
Descriptor *Desc;
157166
if (std::optional<PrimType> T = Ctx.classify(QT))
158-
Desc = createDescriptor(VD, *T, std::nullopt, true, false);
167+
Desc = createDescriptor(D, *T, std::nullopt, /*IsTemporary=*/true,
168+
/*IsMutable=*/false);
159169
else
160-
Desc = createDescriptor(VD, QT.getTypePtr(), std::nullopt, true, false);
170+
Desc = createDescriptor(D, QT.getTypePtr(), std::nullopt,
171+
/*IsTemporary=*/true, /*IsMutable=*/false);
161172
if (!Desc)
162-
Desc = allocateDescriptor(VD);
173+
Desc = allocateDescriptor(D);
163174

164175
assert(Desc);
165176
Desc->makeDummy();
@@ -175,7 +186,7 @@ std::optional<unsigned> Program::getOrCreateDummy(const ValueDecl *VD) {
175186
G->block()->invokeCtor();
176187

177188
Globals.push_back(G);
178-
DummyVariables[VD] = I;
189+
DummyVariables[D.getOpaqueValue()] = I;
179190
return I;
180191
}
181192

clang/lib/AST/ByteCode/Program.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Program final {
8484
const Expr *Init = nullptr);
8585

8686
/// Returns or creates a dummy value for unknown declarations.
87-
std::optional<unsigned> getOrCreateDummy(const ValueDecl *VD);
87+
std::optional<unsigned> getOrCreateDummy(const DeclTy &D);
8888

8989
/// Creates a global and returns its index.
9090
std::optional<unsigned> createGlobal(const ValueDecl *VD, const Expr *Init);
@@ -209,7 +209,7 @@ class Program final {
209209
llvm::DenseMap<const RecordDecl *, Record *> Records;
210210

211211
/// Dummy parameter to generate pointers from.
212-
llvm::DenseMap<const ValueDecl *, unsigned> DummyVariables;
212+
llvm::DenseMap<const void *, unsigned> DummyVariables;
213213

214214
/// Creates a new descriptor.
215215
template <typename... Ts> Descriptor *allocateDescriptor(Ts &&...Args) {

clang/test/CodeGenObjC/boxing.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fexperimental-new-constant-interpreter | FileCheck %s
23

34
typedef long NSInteger;
45
typedef unsigned long NSUInteger;

0 commit comments

Comments
 (0)