@@ -1272,12 +1272,23 @@ bool ByteCodeExprGen<Emitter>::VisitMemberExpr(const MemberExpr *E) {
1272
1272
if (DiscardResult)
1273
1273
return this ->discard (Base);
1274
1274
1275
+ // MemberExprs are almost always lvalues, in which case we don't need to
1276
+ // do the load. But sometimes they aren't.
1277
+ const auto maybeLoadValue = [&]() -> bool {
1278
+ if (E->isGLValue ())
1279
+ return true ;
1280
+ if (std::optional<PrimType> T = classify (E))
1281
+ return this ->emitLoadPop (*T, E);
1282
+ return false ;
1283
+ };
1284
+
1275
1285
if (const auto *VD = dyn_cast<VarDecl>(Member)) {
1276
1286
// I am almost confident in saying that a var decl must be static
1277
1287
// and therefore registered as a global variable. But this will probably
1278
1288
// turn out to be wrong some time in the future, as always.
1279
1289
if (auto GlobalIndex = P.getGlobal (VD))
1280
- return this ->emitGetPtrGlobal (*GlobalIndex, E);
1290
+ return this ->emitGetPtrGlobal (*GlobalIndex, E) && maybeLoadValue ();
1291
+ return false ;
1281
1292
}
1282
1293
1283
1294
if (Initializing) {
@@ -1295,8 +1306,8 @@ bool ByteCodeExprGen<Emitter>::VisitMemberExpr(const MemberExpr *E) {
1295
1306
const Record::Field *F = R->getField (FD);
1296
1307
// Leave a pointer to the field on the stack.
1297
1308
if (F->Decl ->getType ()->isReferenceType ())
1298
- return this ->emitGetFieldPop (PT_Ptr, F->Offset , E);
1299
- return this ->emitGetPtrField (F->Offset , E);
1309
+ return this ->emitGetFieldPop (PT_Ptr, F->Offset , E) && maybeLoadValue () ;
1310
+ return this ->emitGetPtrField (F->Offset , E) && maybeLoadValue () ;
1300
1311
}
1301
1312
1302
1313
return false ;
0 commit comments