1//===--- Compiler.cpp - Code generator for expressions ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Compiler.h"
10#include "ByteCodeEmitter.h"
11#include "Context.h"
12#include "FixedPoint.h"
13#include "Floating.h"
14#include "Function.h"
15#include "InterpShared.h"
16#include "PrimType.h"
17#include "Program.h"
18#include "clang/AST/Attr.h"
19#include "llvm/Support/SaveAndRestore.h"
20
21using namespace clang;
22using namespace clang::interp;
23
24using APSInt = llvm::APSInt;
25
26namespace clang {
27namespace interp {
28
29static std::optional<bool> getBoolValue(const Expr *E) {
30 if (const auto *CE = dyn_cast_if_present<ConstantExpr>(Val: E);
31 CE && CE->hasAPValueResult() &&
32 CE->getResultAPValueKind() == APValue::ValueKind::Int) {
33 return CE->getResultAsAPSInt().getBoolValue();
34 }
35
36 return std::nullopt;
37}
38
39/// Scope used to handle temporaries in toplevel variable declarations.
40template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
41public:
42 DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD)
43 : LocalScope<Emitter>(Ctx), Scope(Ctx->P),
44 OldInitializingDecl(Ctx->InitializingDecl) {
45 Ctx->InitializingDecl = VD;
46 Ctx->InitStack.push_back(InitLink::Decl(D: VD));
47 }
48
49 ~DeclScope() {
50 this->Ctx->InitializingDecl = OldInitializingDecl;
51 this->Ctx->InitStack.pop_back();
52 }
53
54private:
55 Program::DeclScope Scope;
56 const ValueDecl *OldInitializingDecl;
57};
58
59/// Scope used to handle initialization methods.
60template <class Emitter> class OptionScope final {
61public:
62 /// Root constructor, compiling or discarding primitives.
63 OptionScope(Compiler<Emitter> *Ctx, bool NewDiscardResult,
64 bool NewInitializing, bool NewToLValue)
65 : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
66 OldInitializing(Ctx->Initializing), OldToLValue(Ctx->ToLValue) {
67 Ctx->DiscardResult = NewDiscardResult;
68 Ctx->Initializing = NewInitializing;
69 Ctx->ToLValue = NewToLValue;
70 }
71
72 ~OptionScope() {
73 Ctx->DiscardResult = OldDiscardResult;
74 Ctx->Initializing = OldInitializing;
75 Ctx->ToLValue = OldToLValue;
76 }
77
78private:
79 /// Parent context.
80 Compiler<Emitter> *Ctx;
81 /// Old discard flag to restore.
82 bool OldDiscardResult;
83 bool OldInitializing;
84 bool OldToLValue;
85};
86
87template <class Emitter>
88bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const {
89 switch (Kind) {
90 case K_This:
91 return Ctx->emitThis(E);
92 case K_Field:
93 // We're assuming there's a base pointer on the stack already.
94 return Ctx->emitGetPtrFieldPop(Offset, E);
95 case K_Temp:
96 return Ctx->emitGetPtrLocal(Offset, E);
97 case K_Decl:
98 return Ctx->visitDeclRef(D, E);
99 case K_Elem:
100 if (!Ctx->emitConstUint32(Offset, E))
101 return false;
102 return Ctx->emitArrayElemPtrPopUint32(E);
103 case K_RVO:
104 return Ctx->emitRVOPtr(E);
105 case K_InitList:
106 return true;
107 default:
108 llvm_unreachable("Unhandled InitLink kind");
109 }
110 return true;
111}
112
113/// Sets the context for break/continue statements.
114template <class Emitter> class LoopScope final {
115public:
116 using LabelTy = typename Compiler<Emitter>::LabelTy;
117 using OptLabelTy = typename Compiler<Emitter>::OptLabelTy;
118 using LabelInfo = typename Compiler<Emitter>::LabelInfo;
119
120 LoopScope(Compiler<Emitter> *Ctx, const Stmt *Name, LabelTy BreakLabel,
121 LabelTy ContinueLabel)
122 : Ctx(Ctx) {
123#ifndef NDEBUG
124 for (const LabelInfo &LI : Ctx->LabelInfoStack)
125 assert(LI.Name != Name);
126#endif
127
128 this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel, ContinueLabel,
129 /*DefaultLabel=*/std::nullopt,
130 Ctx->VarScope);
131 }
132
133 ~LoopScope() { this->Ctx->LabelInfoStack.pop_back(); }
134
135private:
136 Compiler<Emitter> *Ctx;
137};
138
139// Sets the context for a switch scope, mapping labels.
140template <class Emitter> class SwitchScope final {
141public:
142 using LabelTy = typename Compiler<Emitter>::LabelTy;
143 using OptLabelTy = typename Compiler<Emitter>::OptLabelTy;
144 using CaseMap = typename Compiler<Emitter>::CaseMap;
145 using LabelInfo = typename Compiler<Emitter>::LabelInfo;
146
147 SwitchScope(Compiler<Emitter> *Ctx, const Stmt *Name, CaseMap &&CaseLabels,
148 LabelTy BreakLabel, OptLabelTy DefaultLabel)
149 : Ctx(Ctx), OldCaseLabels(std::move(this->Ctx->CaseLabels)) {
150#ifndef NDEBUG
151 for (const LabelInfo &LI : Ctx->LabelInfoStack)
152 assert(LI.Name != Name);
153#endif
154
155 this->Ctx->CaseLabels = std::move(CaseLabels);
156 this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel,
157 /*ContinueLabel=*/std::nullopt,
158 DefaultLabel, Ctx->VarScope);
159 }
160
161 ~SwitchScope() {
162 this->Ctx->CaseLabels = std::move(OldCaseLabels);
163 this->Ctx->LabelInfoStack.pop_back();
164 }
165
166private:
167 Compiler<Emitter> *Ctx;
168 CaseMap OldCaseLabels;
169};
170
171template <class Emitter> class StmtExprScope final {
172public:
173 StmtExprScope(Compiler<Emitter> *Ctx) : Ctx(Ctx), OldFlag(Ctx->InStmtExpr) {
174 Ctx->InStmtExpr = true;
175 }
176
177 ~StmtExprScope() { Ctx->InStmtExpr = OldFlag; }
178
179private:
180 Compiler<Emitter> *Ctx;
181 bool OldFlag;
182};
183
184/// When generating code for e.g. implicit field initializers in constructors,
185/// we don't have anything to point to in case the initializer causes an error.
186/// In that case, we need to disable location tracking for the initializer so
187/// we later point to the call range instead.
188template <class Emitter> class LocOverrideScope final {
189public:
190 LocOverrideScope(Compiler<Emitter> *Ctx, SourceInfo NewValue,
191 bool Enabled = true)
192 : Ctx(Ctx), OldFlag(Ctx->LocOverride), Enabled(Enabled) {
193
194 if (Enabled)
195 Ctx->LocOverride = NewValue;
196 }
197
198 ~LocOverrideScope() {
199 if (Enabled)
200 Ctx->LocOverride = OldFlag;
201 }
202
203private:
204 Compiler<Emitter> *Ctx;
205 std::optional<SourceInfo> OldFlag;
206 bool Enabled;
207};
208
209} // namespace interp
210} // namespace clang
211
212template <class Emitter>
213bool Compiler<Emitter>::VisitCastExpr(const CastExpr *E) {
214 const Expr *SubExpr = E->getSubExpr();
215
216 if (DiscardResult)
217 return this->delegate(SubExpr);
218
219 switch (E->getCastKind()) {
220 case CK_LValueToRValue: {
221 if (ToLValue && E->getType()->isPointerType())
222 return this->delegate(SubExpr);
223
224 if (SubExpr->getType().isVolatileQualified())
225 return this->emitInvalidCast(CastKind::Volatile, /*Fatal=*/true, E);
226
227 OptPrimType SubExprT = classify(SubExpr->getType());
228 // Try to load the value directly. This is purely a performance
229 // optimization.
230 if (SubExprT) {
231 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: SubExpr)) {
232 const ValueDecl *D = DRE->getDecl();
233 bool IsReference = D->getType()->isReferenceType();
234
235 if (!IsReference) {
236 if (Context::shouldBeGloballyIndexed(VD: D)) {
237 if (auto GlobalIndex = P.getGlobal(VD: D))
238 return this->emitGetGlobal(*SubExprT, *GlobalIndex, E);
239 } else if (auto It = Locals.find(Val: D); It != Locals.end()) {
240 return this->emitGetLocal(*SubExprT, It->second.Offset, E);
241 } else if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: D)) {
242 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
243 return this->emitGetParam(*SubExprT, It->second.Offset, E);
244 }
245 }
246 }
247 }
248 }
249
250 // Prepare storage for the result.
251 if (!Initializing && !SubExprT) {
252 UnsignedOrNone LocalIndex = allocateLocal(Decl: SubExpr);
253 if (!LocalIndex)
254 return false;
255 if (!this->emitGetPtrLocal(*LocalIndex, E))
256 return false;
257 }
258
259 if (!this->visit(SubExpr))
260 return false;
261
262 if (SubExprT)
263 return this->emitLoadPop(*SubExprT, E);
264
265 // If the subexpr type is not primitive, we need to perform a copy here.
266 // This happens for example in C when dereferencing a pointer of struct
267 // type.
268 return this->emitMemcpy(E);
269 }
270
271 case CK_DerivedToBaseMemberPointer: {
272 if (E->containsErrors())
273 return false;
274 assert(classifyPrim(E) == PT_MemberPtr);
275 assert(classifyPrim(SubExpr) == PT_MemberPtr);
276
277 if (!this->delegate(SubExpr))
278 return false;
279
280 const CXXRecordDecl *CurDecl = SubExpr->getType()
281 ->castAs<MemberPointerType>()
282 ->getMostRecentCXXRecordDecl();
283 for (const CXXBaseSpecifier *B : E->path()) {
284 const CXXRecordDecl *ToDecl = B->getType()->getAsCXXRecordDecl();
285 unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: ToDecl, DerivedDecl: CurDecl);
286
287 if (!this->emitCastMemberPtrBasePop(DerivedOffset, ToDecl, E))
288 return false;
289 CurDecl = ToDecl;
290 }
291
292 return true;
293 }
294
295 case CK_BaseToDerivedMemberPointer: {
296 if (E->containsErrors())
297 return false;
298 assert(classifyPrim(E) == PT_MemberPtr);
299 assert(classifyPrim(SubExpr) == PT_MemberPtr);
300
301 if (!this->delegate(SubExpr))
302 return false;
303
304 const CXXRecordDecl *CurDecl = SubExpr->getType()
305 ->castAs<MemberPointerType>()
306 ->getMostRecentCXXRecordDecl();
307 // Base-to-derived member pointer casts store the path in derived-to-base
308 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
309 // the wrong end of the derived->base arc, so stagger the path by one class.
310 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
311 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
312 PathI != PathE; ++PathI) {
313 const CXXRecordDecl *ToDecl = (*PathI)->getType()->getAsCXXRecordDecl();
314 unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: CurDecl, DerivedDecl: ToDecl);
315
316 if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E))
317 return false;
318 CurDecl = ToDecl;
319 }
320
321 const CXXRecordDecl *ToDecl =
322 E->getType()->castAs<MemberPointerType>()->getMostRecentCXXRecordDecl();
323 assert(ToDecl != CurDecl);
324 unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: CurDecl, DerivedDecl: ToDecl);
325
326 if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E))
327 return false;
328
329 return true;
330 }
331
332 case CK_UncheckedDerivedToBase:
333 case CK_DerivedToBase: {
334 if (!this->delegate(SubExpr))
335 return false;
336
337 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
338 if (const auto *PT = dyn_cast<PointerType>(Val&: Ty))
339 return PT->getPointeeType()->getAsCXXRecordDecl();
340 return Ty->getAsCXXRecordDecl();
341 };
342
343 // FIXME: We can express a series of non-virtual casts as a single
344 // GetPtrBasePop op.
345 QualType CurType = SubExpr->getType();
346 for (const CXXBaseSpecifier *B : E->path()) {
347 if (B->isVirtual()) {
348 if (!this->emitGetPtrVirtBasePop(extractRecordDecl(B->getType()), E))
349 return false;
350 CurType = B->getType();
351 } else {
352 unsigned DerivedOffset = collectBaseOffset(BaseType: B->getType(), DerivedType: CurType);
353 if (!this->emitGetPtrBasePop(
354 DerivedOffset, /*NullOK=*/E->getType()->isPointerType(), E))
355 return false;
356 CurType = B->getType();
357 }
358 }
359
360 return true;
361 }
362
363 case CK_BaseToDerived: {
364 if (!this->delegate(SubExpr))
365 return false;
366 unsigned DerivedOffset =
367 collectBaseOffset(BaseType: SubExpr->getType(), DerivedType: E->getType());
368
369 const Type *TargetType = E->getType().getTypePtr();
370 if (TargetType->isPointerOrReferenceType())
371 TargetType = TargetType->getPointeeType().getTypePtr();
372 return this->emitGetPtrDerivedPop(DerivedOffset,
373 /*NullOK=*/E->getType()->isPointerType(),
374 TargetType, E);
375 }
376
377 case CK_FloatingCast: {
378 // HLSL uses CK_FloatingCast to cast between vectors.
379 if (!SubExpr->getType()->isFloatingType() ||
380 !E->getType()->isFloatingType())
381 return false;
382 if (!this->visit(SubExpr))
383 return false;
384 const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType());
385 return this->emitCastFP(TargetSemantics, getRoundingMode(E), E);
386 }
387
388 case CK_IntegralToFloating: {
389 if (!E->getType()->isRealFloatingType())
390 return false;
391 if (!this->visit(SubExpr))
392 return false;
393 const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType());
394 return this->emitCastIntegralFloating(classifyPrim(SubExpr),
395 TargetSemantics, getFPOptions(E), E);
396 }
397
398 case CK_FloatingToBoolean: {
399 if (!SubExpr->getType()->isRealFloatingType() ||
400 !E->getType()->isBooleanType())
401 return false;
402 if (const auto *FL = dyn_cast<FloatingLiteral>(Val: SubExpr))
403 return this->emitConstBool(FL->getValue().isNonZero(), E);
404 if (!this->visit(SubExpr))
405 return false;
406 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
407 }
408
409 case CK_FloatingToIntegral: {
410 if (!E->getType()->isIntegralOrEnumerationType())
411 return false;
412 if (!this->visit(SubExpr))
413 return false;
414 PrimType ToT = classifyPrim(E);
415 if (ToT == PT_IntAP)
416 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(T: E->getType()),
417 getFPOptions(E), E);
418 if (ToT == PT_IntAPS)
419 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(T: E->getType()),
420 getFPOptions(E), E);
421
422 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
423 }
424
425 case CK_NullToPointer:
426 case CK_NullToMemberPointer: {
427 if (!this->discard(SubExpr))
428 return false;
429 const Descriptor *Desc = nullptr;
430 const QualType PointeeType = E->getType()->getPointeeType();
431 if (!PointeeType.isNull()) {
432 if (OptPrimType T = classify(PointeeType))
433 Desc = P.createDescriptor(D: SubExpr, T: *T);
434 else
435 Desc = P.createDescriptor(D: SubExpr, Ty: PointeeType.getTypePtr(),
436 MDSize: std::nullopt, /*IsConst=*/true);
437 }
438
439 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(QT: E->getType());
440 return this->emitNull(classifyPrim(E->getType()), Val, Desc, E);
441 }
442
443 case CK_PointerToIntegral: {
444 if (!this->visit(SubExpr))
445 return false;
446
447 // If SubExpr doesn't result in a pointer, make it one.
448 if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) {
449 assert(isPtrType(FromT));
450 if (!this->emitDecayPtr(FromT, PT_Ptr, E))
451 return false;
452 }
453
454 PrimType T = classifyPrim(E->getType());
455 if (T == PT_IntAP)
456 return this->emitCastPointerIntegralAP(Ctx.getBitWidth(T: E->getType()), E);
457 if (T == PT_IntAPS)
458 return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(T: E->getType()), E);
459 return this->emitCastPointerIntegral(T, E);
460 }
461
462 case CK_ArrayToPointerDecay: {
463 if (!this->visit(SubExpr))
464 return false;
465 return this->emitArrayDecay(E);
466 }
467
468 case CK_IntegralToPointer: {
469 QualType IntType = SubExpr->getType();
470 assert(IntType->isIntegralOrEnumerationType());
471 if (!this->visit(SubExpr))
472 return false;
473 // FIXME: I think the discard is wrong since the int->ptr cast might cause a
474 // diagnostic.
475 PrimType T = classifyPrim(IntType);
476 QualType PtrType = E->getType();
477 const Descriptor *Desc;
478 if (OptPrimType T = classify(PtrType->getPointeeType()))
479 Desc = P.createDescriptor(D: SubExpr, T: *T);
480 else if (PtrType->getPointeeType()->isVoidType())
481 Desc = nullptr;
482 else
483 Desc = P.createDescriptor(D: E, Ty: PtrType->getPointeeType().getTypePtr(),
484 MDSize: Descriptor::InlineDescMD, /*IsConst=*/true);
485
486 if (!this->emitGetIntPtr(T, Desc, E))
487 return false;
488
489 PrimType DestPtrT = classifyPrim(PtrType);
490 if (DestPtrT == PT_Ptr)
491 return true;
492
493 // In case we're converting the integer to a non-Pointer.
494 return this->emitDecayPtr(PT_Ptr, DestPtrT, E);
495 }
496
497 case CK_AtomicToNonAtomic:
498 case CK_ConstructorConversion:
499 case CK_FunctionToPointerDecay:
500 case CK_NonAtomicToAtomic:
501 case CK_NoOp:
502 case CK_UserDefinedConversion:
503 case CK_AddressSpaceConversion:
504 case CK_CPointerToObjCPointerCast:
505 return this->delegate(SubExpr);
506
507 case CK_BitCast: {
508 if (E->containsErrors())
509 return false;
510 QualType ETy = E->getType();
511 // Reject bitcasts to atomic types.
512 if (ETy->isAtomicType()) {
513 if (!this->discard(SubExpr))
514 return false;
515 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
516 }
517 QualType SubExprTy = SubExpr->getType();
518 OptPrimType FromT = classify(SubExprTy);
519 // Casts from integer/vector to vector.
520 if (E->getType()->isVectorType())
521 return this->emitBuiltinBitCast(E);
522
523 OptPrimType ToT = classify(E->getType());
524 if (!FromT || !ToT)
525 return false;
526
527 assert(isPtrType(*FromT));
528 assert(isPtrType(*ToT));
529 bool SrcIsVoidPtr = SubExprTy->isVoidPointerType();
530 if (FromT == ToT) {
531 if (E->getType()->isVoidPointerType() &&
532 !SubExprTy->isFunctionPointerType()) {
533 return this->delegate(SubExpr);
534 }
535
536 if (!this->visit(SubExpr))
537 return false;
538 if (!this->emitCheckBitCast(ETy->getPointeeType().getTypePtr(),
539 SrcIsVoidPtr, E))
540 return false;
541
542 if (E->getType()->isFunctionPointerType() ||
543 SubExprTy->isFunctionPointerType()) {
544 return this->emitFnPtrCast(E);
545 }
546 if (FromT == PT_Ptr)
547 return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), E);
548 return true;
549 }
550
551 if (!this->visit(SubExpr))
552 return false;
553 return this->emitDecayPtr(*FromT, *ToT, E);
554 }
555 case CK_IntegralToBoolean:
556 case CK_FixedPointToBoolean: {
557 // HLSL uses this to cast to one-element vectors.
558 OptPrimType FromT = classify(SubExpr->getType());
559 if (!FromT)
560 return false;
561
562 if (const auto *IL = dyn_cast<IntegerLiteral>(Val: SubExpr))
563 return this->emitConst(IL->getValue(), E);
564 if (!this->visit(SubExpr))
565 return false;
566 return this->emitCast(*FromT, classifyPrim(E), E);
567 }
568
569 case CK_BooleanToSignedIntegral:
570 case CK_IntegralCast: {
571 OptPrimType FromT = classify(SubExpr->getType());
572 OptPrimType ToT = classify(E->getType());
573 if (!FromT || !ToT)
574 return false;
575
576 // Try to emit a casted known constant value directly.
577 if (const auto *IL = dyn_cast<IntegerLiteral>(Val: SubExpr)) {
578 if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP &&
579 FromT != PT_IntAPS && !E->getType()->isEnumeralType())
580 return this->emitConst(APSInt(IL->getValue(), !isSignedType(T: *FromT)),
581 E);
582 if (!this->emitConst(IL->getValue(), SubExpr))
583 return false;
584 } else {
585 if (!this->visit(SubExpr))
586 return false;
587 }
588
589 // Possibly diagnose casts to enum types if the target type does not
590 // have a fixed size.
591 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) {
592 const auto *ED = E->getType()->castAsEnumDecl();
593 if (!ED->isFixed()) {
594 if (!this->emitCheckEnumValue(*FromT, ED, E))
595 return false;
596 }
597 }
598
599 if (ToT == PT_IntAP) {
600 if (!this->emitCastAP(*FromT, Ctx.getBitWidth(T: E->getType()), E))
601 return false;
602 } else if (ToT == PT_IntAPS) {
603 if (!this->emitCastAPS(*FromT, Ctx.getBitWidth(T: E->getType()), E))
604 return false;
605 } else {
606 if (FromT == ToT)
607 return true;
608 if (!this->emitCast(*FromT, *ToT, E))
609 return false;
610 }
611 if (E->getCastKind() == CK_BooleanToSignedIntegral)
612 return this->emitNeg(*ToT, E);
613 return true;
614 }
615
616 case CK_PointerToBoolean:
617 case CK_MemberPointerToBoolean: {
618 PrimType PtrT = classifyPrim(SubExpr->getType());
619
620 if (!this->visit(SubExpr))
621 return false;
622 return this->emitIsNonNull(PtrT, E);
623 }
624
625 case CK_IntegralComplexToBoolean:
626 case CK_FloatingComplexToBoolean: {
627 if (!this->visit(SubExpr))
628 return false;
629 return this->emitComplexBoolCast(SubExpr);
630 }
631
632 case CK_IntegralComplexToReal:
633 case CK_FloatingComplexToReal:
634 return this->emitComplexReal(SubExpr);
635
636 case CK_IntegralRealToComplex:
637 case CK_FloatingRealToComplex: {
638 // We're creating a complex value here, so we need to
639 // allocate storage for it.
640 if (!Initializing) {
641 UnsignedOrNone LocalIndex = allocateTemporary(E);
642 if (!LocalIndex)
643 return false;
644 if (!this->emitGetPtrLocal(*LocalIndex, E))
645 return false;
646 }
647
648 PrimType T = classifyPrim(SubExpr->getType());
649 // Init the complex value to {SubExpr, 0}.
650 if (!this->visitArrayElemInit(0, SubExpr, T))
651 return false;
652 // Zero-init the second element.
653 if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
654 return false;
655 return this->emitInitElem(T, 1, SubExpr);
656 }
657
658 case CK_IntegralComplexCast:
659 case CK_FloatingComplexCast:
660 case CK_IntegralComplexToFloatingComplex:
661 case CK_FloatingComplexToIntegralComplex: {
662 assert(E->getType()->isAnyComplexType());
663 assert(SubExpr->getType()->isAnyComplexType());
664 if (!Initializing) {
665 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
666 if (!LocalIndex)
667 return false;
668 if (!this->emitGetPtrLocal(*LocalIndex, E))
669 return false;
670 }
671
672 // Location for the SubExpr.
673 // Since SubExpr is of complex type, visiting it results in a pointer
674 // anyway, so we just create a temporary pointer variable.
675 unsigned SubExprOffset =
676 allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true);
677 if (!this->visit(SubExpr))
678 return false;
679 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
680 return false;
681
682 PrimType SourceElemT = classifyComplexElementType(T: SubExpr->getType());
683 QualType DestElemType =
684 E->getType()->getAs<ComplexType>()->getElementType();
685 PrimType DestElemT = classifyPrim(DestElemType);
686 // Cast both elements individually.
687 for (unsigned I = 0; I != 2; ++I) {
688 if (!this->emitGetLocal(PT_Ptr, SubExprOffset, E))
689 return false;
690 if (!this->emitArrayElemPop(SourceElemT, I, E))
691 return false;
692
693 // Do the cast.
694 if (!this->emitPrimCast(SourceElemT, DestElemT, DestElemType, E))
695 return false;
696
697 // Save the value.
698 if (!this->emitInitElem(DestElemT, I, E))
699 return false;
700 }
701 return true;
702 }
703
704 case CK_VectorSplat: {
705 assert(!canClassify(E->getType()));
706 assert(canClassify(SubExpr->getType()));
707 assert(E->getType()->isVectorType());
708
709 if (!Initializing) {
710 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
711 if (!LocalIndex)
712 return false;
713 if (!this->emitGetPtrLocal(*LocalIndex, E))
714 return false;
715 }
716
717 const auto *VT = E->getType()->getAs<VectorType>();
718 PrimType ElemT = classifyPrim(SubExpr->getType());
719 unsigned ElemOffset =
720 allocateLocalPrimitive(Decl: SubExpr, Ty: ElemT, /*IsConst=*/true);
721
722 // Prepare a local variable for the scalar value.
723 if (!this->visit(SubExpr))
724 return false;
725 if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, E))
726 return false;
727
728 if (!this->emitSetLocal(ElemT, ElemOffset, E))
729 return false;
730
731 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
732 if (!this->emitGetLocal(ElemT, ElemOffset, E))
733 return false;
734 if (!this->emitInitElem(ElemT, I, E))
735 return false;
736 }
737
738 return true;
739 }
740
741 case CK_HLSLVectorTruncation: {
742 assert(SubExpr->getType()->isVectorType());
743 if (OptPrimType ResultT = classify(E)) {
744 assert(!DiscardResult);
745 // Result must be either a float or integer. Take the first element.
746 if (!this->visit(SubExpr))
747 return false;
748 return this->emitArrayElemPop(*ResultT, 0, E);
749 }
750 // Otherwise, this truncates from one vector type to another.
751 assert(E->getType()->isVectorType());
752
753 if (!Initializing) {
754 UnsignedOrNone LocalIndex = allocateTemporary(E);
755 if (!LocalIndex)
756 return false;
757 if (!this->emitGetPtrLocal(*LocalIndex, E))
758 return false;
759 }
760 unsigned ToSize = E->getType()->getAs<VectorType>()->getNumElements();
761 assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize);
762 if (!this->visit(SubExpr))
763 return false;
764 return this->emitCopyArray(classifyVectorElementType(T: E->getType()), 0, 0,
765 ToSize, E);
766 };
767
768 case CK_IntegralToFixedPoint: {
769 if (!this->visit(SubExpr))
770 return false;
771
772 auto Sem =
773 Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt();
774 return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()),
775 Sem, E);
776 }
777 case CK_FloatingToFixedPoint: {
778 if (!this->visit(SubExpr))
779 return false;
780
781 auto Sem =
782 Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt();
783 return this->emitCastFloatingFixedPoint(Sem, E);
784 }
785 case CK_FixedPointToFloating: {
786 if (!this->visit(SubExpr))
787 return false;
788 const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType());
789 return this->emitCastFixedPointFloating(TargetSemantics, E);
790 }
791 case CK_FixedPointToIntegral: {
792 if (!this->visit(SubExpr))
793 return false;
794 return this->emitCastFixedPointIntegral(classifyPrim(E->getType()), E);
795 }
796 case CK_FixedPointCast: {
797 if (!this->visit(SubExpr))
798 return false;
799 auto Sem =
800 Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt();
801 return this->emitCastFixedPoint(Sem, E);
802 }
803
804 case CK_ToVoid:
805 return discard(E: SubExpr);
806
807 case CK_Dynamic:
808 // This initially goes through VisitCXXDynamicCastExpr, where we emit
809 // a diagnostic if appropriate.
810 return this->delegate(SubExpr);
811
812 case CK_LValueBitCast:
813 return this->emitInvalidCast(CastKind::ReinterpretLike, /*Fatal=*/true, E);
814
815 case CK_HLSLArrayRValue: {
816 // Non-decaying array rvalue cast - creates an rvalue copy of an lvalue
817 // array, similar to LValueToRValue for composite types.
818 if (!Initializing) {
819 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
820 if (!LocalIndex)
821 return false;
822 if (!this->emitGetPtrLocal(*LocalIndex, E))
823 return false;
824 }
825 if (!this->visit(SubExpr))
826 return false;
827 return this->emitMemcpy(E);
828 }
829
830 case CK_HLSLMatrixTruncation: {
831 assert(SubExpr->getType()->isConstantMatrixType());
832 if (OptPrimType ResultT = classify(E)) {
833 assert(!DiscardResult);
834 // Result must be either a float or integer. Take the first element.
835 if (!this->visit(SubExpr))
836 return false;
837 return this->emitArrayElemPop(*ResultT, 0, E);
838 }
839 // Otherwise, this truncates to a a constant matrix type.
840 assert(E->getType()->isConstantMatrixType());
841
842 if (!Initializing) {
843 UnsignedOrNone LocalIndex = allocateTemporary(E);
844 if (!LocalIndex)
845 return false;
846 if (!this->emitGetPtrLocal(*LocalIndex, E))
847 return false;
848 }
849 unsigned ToSize =
850 E->getType()->getAs<ConstantMatrixType>()->getNumElementsFlattened();
851 if (!this->visit(SubExpr))
852 return false;
853 return this->emitCopyArray(classifyMatrixElementType(T: SubExpr->getType()), 0,
854 0, ToSize, E);
855 }
856
857 case CK_HLSLAggregateSplatCast: {
858 // Aggregate splat cast: convert a scalar value to one of an aggregate type,
859 // inserting casts when necessary to convert the scalar to the aggregate's
860 // element type(s).
861 // TODO: Aggregate splat to struct and array types
862 assert(canClassify(SubExpr->getType()));
863
864 unsigned NumElems;
865 PrimType DestElemT;
866 QualType DestElemType;
867 if (const auto *VT = E->getType()->getAs<VectorType>()) {
868 NumElems = VT->getNumElements();
869 DestElemType = VT->getElementType();
870 } else if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) {
871 NumElems = MT->getNumElementsFlattened();
872 DestElemType = MT->getElementType();
873 } else {
874 return false;
875 }
876 DestElemT = classifyPrim(DestElemType);
877
878 if (!Initializing) {
879 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
880 if (!LocalIndex)
881 return false;
882 if (!this->emitGetPtrLocal(*LocalIndex, E))
883 return false;
884 }
885
886 PrimType SrcElemT = classifyPrim(SubExpr->getType());
887 unsigned SrcOffset =
888 allocateLocalPrimitive(Decl: SubExpr, Ty: DestElemT, /*IsConst=*/true);
889
890 if (!this->visit(SubExpr))
891 return false;
892 if (SrcElemT != DestElemT) {
893 if (!this->emitPrimCast(SrcElemT, DestElemT, DestElemType, E))
894 return false;
895 }
896 if (!this->emitSetLocal(DestElemT, SrcOffset, E))
897 return false;
898
899 for (unsigned I = 0; I != NumElems; ++I) {
900 if (!this->emitGetLocal(DestElemT, SrcOffset, E))
901 return false;
902 if (!this->emitInitElem(DestElemT, I, E))
903 return false;
904 }
905 return true;
906 }
907
908 case CK_HLSLElementwiseCast: {
909 // Elementwise cast: flatten source elements of one aggregate type and store
910 // to a destination scalar or aggregate type of the same or fewer number of
911 // elements, while inserting casts as necessary.
912 // TODO: Elementwise cast to structs, nested arrays, and arrays of composite
913 // types
914 QualType SrcType = SubExpr->getType();
915 QualType DestType = E->getType();
916
917 // Allowed SrcTypes
918 const auto *SrcVT = SrcType->getAs<VectorType>();
919 const auto *SrcMT = SrcType->getAs<ConstantMatrixType>();
920 const auto *SrcAT = SrcType->getAsArrayTypeUnsafe();
921 const auto *SrcCAT = SrcAT ? dyn_cast<ConstantArrayType>(Val: SrcAT) : nullptr;
922
923 // Allowed DestTypes
924 const auto *DestVT = DestType->getAs<VectorType>();
925 const auto *DestMT = DestType->getAs<ConstantMatrixType>();
926 const auto *DestAT = DestType->getAsArrayTypeUnsafe();
927 const auto *DestCAT =
928 DestAT ? dyn_cast<ConstantArrayType>(Val: DestAT) : nullptr;
929 const OptPrimType DestPT = classify(DestType);
930
931 if (!SrcVT && !SrcMT && !SrcCAT)
932 return false;
933 if (!DestVT && !DestMT && !DestCAT && !DestPT)
934 return false;
935
936 unsigned SrcNumElems;
937 PrimType SrcElemT;
938 if (SrcVT) {
939 SrcNumElems = SrcVT->getNumElements();
940 SrcElemT = classifyPrim(SrcVT->getElementType());
941 } else if (SrcMT) {
942 SrcNumElems = SrcMT->getNumElementsFlattened();
943 SrcElemT = classifyPrim(SrcMT->getElementType());
944 } else if (SrcCAT) {
945 SrcNumElems = SrcCAT->getZExtSize();
946 SrcElemT = classifyPrim(SrcCAT->getElementType());
947 }
948
949 if (DestPT) {
950 // Scalar destination: extract element 0 and cast.
951 if (!this->visit(SubExpr))
952 return false;
953 if (!this->emitArrayElemPop(SrcElemT, 0, E))
954 return false;
955 if (SrcElemT != *DestPT) {
956 if (!this->emitPrimCast(SrcElemT, *DestPT, DestType, E))
957 return false;
958 }
959 return true;
960 }
961
962 unsigned DestNumElems;
963 PrimType DestElemT;
964 QualType DestElemType;
965 if (DestVT) {
966 DestNumElems = DestVT->getNumElements();
967 DestElemType = DestVT->getElementType();
968 } else if (DestMT) {
969 DestNumElems = DestMT->getNumElementsFlattened();
970 DestElemType = DestMT->getElementType();
971 } else if (DestCAT) {
972 DestNumElems = DestCAT->getZExtSize();
973 DestElemType = DestCAT->getElementType();
974 }
975 DestElemT = classifyPrim(DestElemType);
976
977 if (!Initializing) {
978 UnsignedOrNone LocalIndex = allocateTemporary(E);
979 if (!LocalIndex)
980 return false;
981 if (!this->emitGetPtrLocal(*LocalIndex, E))
982 return false;
983 }
984
985 unsigned SrcOffset =
986 allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true);
987 if (!this->visit(SubExpr))
988 return false;
989 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
990 return false;
991
992 unsigned NumElems = std::min(a: SrcNumElems, b: DestNumElems);
993 for (unsigned I = 0; I != NumElems; ++I) {
994 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
995 return false;
996 if (!this->emitArrayElemPop(SrcElemT, I, E))
997 return false;
998 if (SrcElemT != DestElemT) {
999 if (!this->emitPrimCast(SrcElemT, DestElemT, DestElemType, E))
1000 return false;
1001 }
1002 if (!this->emitInitElem(DestElemT, I, E))
1003 return false;
1004 }
1005 return true;
1006 }
1007
1008 default:
1009 return this->emitInvalid(E);
1010 }
1011 llvm_unreachable("Unhandled clang::CastKind enum");
1012}
1013
1014template <class Emitter>
1015bool Compiler<Emitter>::VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
1016 return this->emitBuiltinBitCast(E);
1017}
1018
1019template <class Emitter>
1020bool Compiler<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
1021 if (DiscardResult)
1022 return true;
1023
1024 return this->emitConst(LE->getValue(), LE);
1025}
1026
1027template <class Emitter>
1028bool Compiler<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
1029 if (DiscardResult)
1030 return true;
1031
1032 APFloat F = E->getValue();
1033 return this->emitFloat(F, E);
1034}
1035
1036template <class Emitter>
1037bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
1038 assert(E->getType()->isAnyComplexType());
1039 if (DiscardResult)
1040 return true;
1041
1042 if (!Initializing) {
1043 UnsignedOrNone LocalIndex = allocateTemporary(E);
1044 if (!LocalIndex)
1045 return false;
1046 if (!this->emitGetPtrLocal(*LocalIndex, E))
1047 return false;
1048 }
1049
1050 const Expr *SubExpr = E->getSubExpr();
1051 PrimType SubExprT = classifyPrim(SubExpr->getType());
1052
1053 if (!this->visitZeroInitializer(SubExprT, SubExpr->getType(), SubExpr))
1054 return false;
1055 if (!this->emitInitElem(SubExprT, 0, SubExpr))
1056 return false;
1057 return this->visitArrayElemInit(1, SubExpr, SubExprT);
1058}
1059
1060template <class Emitter>
1061bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) {
1062 assert(E->getType()->isFixedPointType());
1063 assert(classifyPrim(E) == PT_FixedPoint);
1064
1065 if (DiscardResult)
1066 return true;
1067
1068 auto Sem = Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType());
1069 APInt Value = E->getValue();
1070 return this->emitConstFixedPoint(FixedPoint(Value, Sem), E);
1071}
1072
1073template <class Emitter>
1074bool Compiler<Emitter>::VisitParenExpr(const ParenExpr *E) {
1075 return this->delegate(E->getSubExpr());
1076}
1077
1078template <class Emitter>
1079bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *E) {
1080 // Need short-circuiting for these.
1081 if (E->isLogicalOp() && !E->getType()->isVectorType())
1082 return this->VisitLogicalBinOp(E);
1083
1084 const Expr *LHS = E->getLHS();
1085 const Expr *RHS = E->getRHS();
1086
1087 // Handle comma operators. Just discard the LHS
1088 // and delegate to RHS.
1089 if (E->isCommaOp()) {
1090 if (!this->discard(LHS))
1091 return false;
1092 if (RHS->getType()->isVoidType())
1093 return this->discard(RHS);
1094
1095 return this->delegate(RHS);
1096 }
1097
1098 if (E->getType()->isAnyComplexType())
1099 return this->VisitComplexBinOp(E);
1100 if (E->getType()->isVectorType())
1101 return this->VisitVectorBinOp(E);
1102 if ((LHS->getType()->isAnyComplexType() ||
1103 RHS->getType()->isAnyComplexType()) &&
1104 E->isComparisonOp())
1105 return this->emitComplexComparison(LHS, RHS, E);
1106 if (LHS->getType()->isFixedPointType() || RHS->getType()->isFixedPointType())
1107 return this->VisitFixedPointBinOp(E);
1108
1109 if (E->isPtrMemOp()) {
1110 if (E->containsErrors())
1111 return false;
1112
1113 if (!this->visit(LHS))
1114 return false;
1115
1116 if (!this->visit(RHS))
1117 return false;
1118
1119 if (!this->emitToMemberPtr(E))
1120 return false;
1121
1122 if (classifyPrim(E) == PT_MemberPtr)
1123 return true;
1124
1125 if (!this->emitCastMemberPtrPtr(E))
1126 return false;
1127 return DiscardResult ? this->emitPopPtr(E) : true;
1128 }
1129
1130 // Typecheck the args.
1131 OptPrimType LT = classify(LHS);
1132 OptPrimType RT = classify(RHS);
1133 OptPrimType T = classify(E->getType());
1134
1135 // Special case for C++'s three-way/spaceship operator <=>, which
1136 // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
1137 // have a PrimType).
1138 if (!T && E->getOpcode() == BO_Cmp) {
1139 if (DiscardResult)
1140 return true;
1141 const ComparisonCategoryInfo *CmpInfo =
1142 Ctx.getASTContext().CompCategories.lookupInfoForType(Ty: E->getType());
1143 assert(CmpInfo);
1144
1145 // We need a temporary variable holding our return value.
1146 if (!Initializing) {
1147 UnsignedOrNone ResultIndex = this->allocateLocal(E);
1148 if (!this->emitGetPtrLocal(*ResultIndex, E))
1149 return false;
1150 }
1151
1152 if (!visit(E: LHS) || !visit(E: RHS))
1153 return false;
1154
1155 return this->emitCMP3(*LT, CmpInfo, E);
1156 }
1157
1158 if (!LT || !RT || !T)
1159 return false;
1160
1161 // Pointer arithmetic special case.
1162 if (E->getOpcode() == BO_Add || E->getOpcode() == BO_Sub) {
1163 if (isPtrType(T: *T) || (isPtrType(T: *LT) && isPtrType(T: *RT)))
1164 return this->VisitPointerArithBinOp(E);
1165 }
1166
1167 if (E->getOpcode() == BO_Assign)
1168 return this->visitAssignment(LHS, RHS, E);
1169
1170 if (!visit(E: LHS) || !visit(E: RHS))
1171 return false;
1172
1173 // For languages such as C, cast the result of one
1174 // of our comparision opcodes to T (which is usually int).
1175 auto MaybeCastToBool = [this, T, E](bool Result) {
1176 if (!Result)
1177 return false;
1178 if (DiscardResult)
1179 return this->emitPopBool(E);
1180 if (T != PT_Bool)
1181 return this->emitCast(PT_Bool, *T, E);
1182 return true;
1183 };
1184
1185 auto Discard = [this, T, E](bool Result) {
1186 if (!Result)
1187 return false;
1188 return DiscardResult ? this->emitPop(*T, E) : true;
1189 };
1190
1191 switch (E->getOpcode()) {
1192 case BO_EQ:
1193 return MaybeCastToBool(this->emitEQ(*LT, E));
1194 case BO_NE:
1195 return MaybeCastToBool(this->emitNE(*LT, E));
1196 case BO_LT:
1197 return MaybeCastToBool(this->emitLT(*LT, E));
1198 case BO_LE:
1199 return MaybeCastToBool(this->emitLE(*LT, E));
1200 case BO_GT:
1201 return MaybeCastToBool(this->emitGT(*LT, E));
1202 case BO_GE:
1203 return MaybeCastToBool(this->emitGE(*LT, E));
1204 case BO_Sub:
1205 if (E->getType()->isFloatingType())
1206 return Discard(this->emitSubf(getFPOptions(E), E));
1207 return Discard(this->emitSub(*T, E));
1208 case BO_Add:
1209 if (E->getType()->isFloatingType())
1210 return Discard(this->emitAddf(getFPOptions(E), E));
1211 return Discard(this->emitAdd(*T, E));
1212 case BO_Mul:
1213 if (E->getType()->isFloatingType())
1214 return Discard(this->emitMulf(getFPOptions(E), E));
1215 return Discard(this->emitMul(*T, E));
1216 case BO_Rem:
1217 return Discard(this->emitRem(*T, E));
1218 case BO_Div:
1219 if (E->getType()->isFloatingType())
1220 return Discard(this->emitDivf(getFPOptions(E), E));
1221 return Discard(this->emitDiv(*T, E));
1222 case BO_And:
1223 return Discard(this->emitBitAnd(*T, E));
1224 case BO_Or:
1225 return Discard(this->emitBitOr(*T, E));
1226 case BO_Shl:
1227 return Discard(this->emitShl(*LT, *RT, E));
1228 case BO_Shr:
1229 return Discard(this->emitShr(*LT, *RT, E));
1230 case BO_Xor:
1231 return Discard(this->emitBitXor(*T, E));
1232 case BO_LOr:
1233 case BO_LAnd:
1234 llvm_unreachable("Already handled earlier");
1235 default:
1236 return false;
1237 }
1238
1239 llvm_unreachable("Unhandled binary op");
1240}
1241
1242/// Perform addition/subtraction of a pointer and an integer or
1243/// subtraction of two pointers.
1244template <class Emitter>
1245bool Compiler<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
1246 BinaryOperatorKind Op = E->getOpcode();
1247 const Expr *LHS = E->getLHS();
1248 const Expr *RHS = E->getRHS();
1249
1250 if ((Op != BO_Add && Op != BO_Sub) ||
1251 (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
1252 return false;
1253
1254 OptPrimType LT = classify(LHS);
1255 OptPrimType RT = classify(RHS);
1256
1257 if (!LT || !RT)
1258 return false;
1259
1260 // Visit the given pointer expression and optionally convert to a PT_Ptr.
1261 auto visitAsPointer = [&](const Expr *E, PrimType T) -> bool {
1262 if (!this->visit(E))
1263 return false;
1264 if (T != PT_Ptr)
1265 return this->emitDecayPtr(T, PT_Ptr, E);
1266 return true;
1267 };
1268
1269 if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
1270 if (Op != BO_Sub)
1271 return false;
1272
1273 assert(E->getType()->isIntegerType());
1274 if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT))
1275 return false;
1276
1277 QualType ElemType = LHS->getType()->getPointeeType();
1278 CharUnits ElemTypeSize;
1279 if (ElemType->isVoidType() || ElemType->isFunctionType())
1280 ElemTypeSize = CharUnits::One();
1281 else
1282 ElemTypeSize = Ctx.getASTContext().getTypeSizeInChars(T: ElemType);
1283
1284 PrimType IntT = classifyPrim(E->getType());
1285 if (!this->emitSubPtr(IntT, ElemTypeSize.isZero(), E))
1286 return false;
1287 return DiscardResult ? this->emitPop(IntT, E) : true;
1288 }
1289
1290 PrimType OffsetType;
1291 if (LHS->getType()->isIntegerType()) {
1292 if (!visitAsPointer(RHS, *RT))
1293 return false;
1294 if (!this->visit(LHS))
1295 return false;
1296 OffsetType = *LT;
1297 } else if (RHS->getType()->isIntegerType()) {
1298 if (!visitAsPointer(LHS, *LT))
1299 return false;
1300 if (!this->visit(RHS))
1301 return false;
1302 OffsetType = *RT;
1303 } else {
1304 return false;
1305 }
1306
1307 // Do the operation and optionally transform to
1308 // result pointer type.
1309 switch (Op) {
1310 case BO_Add:
1311 if (!this->emitAddOffset(OffsetType, E))
1312 return false;
1313 break;
1314 case BO_Sub:
1315 if (!this->emitSubOffset(OffsetType, E))
1316 return false;
1317 break;
1318 default:
1319 return false;
1320 }
1321
1322 if (classifyPrim(E) != PT_Ptr) {
1323 if (!this->emitDecayPtr(PT_Ptr, classifyPrim(E), E))
1324 return false;
1325 }
1326
1327 if (DiscardResult)
1328 return this->emitPop(classifyPrim(E), E);
1329 return true;
1330}
1331
1332template <class Emitter>
1333bool Compiler<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
1334 assert(E->isLogicalOp());
1335 BinaryOperatorKind Op = E->getOpcode();
1336 const Expr *LHS = E->getLHS();
1337 const Expr *RHS = E->getRHS();
1338 OptPrimType T = classify(E->getType());
1339
1340 if (Op == BO_LOr) {
1341 // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
1342 LabelTy LabelTrue = this->getLabel();
1343 LabelTy LabelEnd = this->getLabel();
1344
1345 if (!this->visitBool(LHS))
1346 return false;
1347 if (!this->jumpTrue(LabelTrue))
1348 return false;
1349
1350 if (!this->visitBool(RHS))
1351 return false;
1352 if (!this->jump(LabelEnd))
1353 return false;
1354
1355 this->emitLabel(LabelTrue);
1356 this->emitConstBool(true, E);
1357 this->fallthrough(LabelEnd);
1358 this->emitLabel(LabelEnd);
1359
1360 } else {
1361 assert(Op == BO_LAnd);
1362 // Logical AND.
1363 // Visit LHS. Only visit RHS if LHS was TRUE.
1364 LabelTy LabelFalse = this->getLabel();
1365 LabelTy LabelEnd = this->getLabel();
1366
1367 if (!this->visitBool(LHS))
1368 return false;
1369 if (!this->jumpFalse(LabelFalse))
1370 return false;
1371
1372 if (!this->visitBool(RHS))
1373 return false;
1374 if (!this->jump(LabelEnd))
1375 return false;
1376
1377 this->emitLabel(LabelFalse);
1378 this->emitConstBool(false, E);
1379 this->fallthrough(LabelEnd);
1380 this->emitLabel(LabelEnd);
1381 }
1382
1383 if (DiscardResult)
1384 return this->emitPopBool(E);
1385
1386 // For C, cast back to integer type.
1387 assert(T);
1388 if (T != PT_Bool)
1389 return this->emitCast(PT_Bool, *T, E);
1390 return true;
1391}
1392
1393template <class Emitter>
1394bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
1395 // Prepare storage for result.
1396 if (!Initializing) {
1397 UnsignedOrNone LocalIndex = allocateTemporary(E);
1398 if (!LocalIndex)
1399 return false;
1400 if (!this->emitGetPtrLocal(*LocalIndex, E))
1401 return false;
1402 }
1403
1404 // Both LHS and RHS might _not_ be of complex type, but one of them
1405 // needs to be.
1406 const Expr *LHS = E->getLHS();
1407 const Expr *RHS = E->getRHS();
1408
1409 PrimType ResultElemT = this->classifyComplexElementType(E->getType());
1410 unsigned ResultOffset = ~0u;
1411 if (!DiscardResult)
1412 ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
1413
1414 // Save result pointer in ResultOffset
1415 if (!this->DiscardResult) {
1416 if (!this->emitDupPtr(E))
1417 return false;
1418 if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
1419 return false;
1420 }
1421 QualType LHSType = LHS->getType();
1422 if (const auto *AT = LHSType->getAs<AtomicType>())
1423 LHSType = AT->getValueType();
1424 QualType RHSType = RHS->getType();
1425 if (const auto *AT = RHSType->getAs<AtomicType>())
1426 RHSType = AT->getValueType();
1427
1428 bool LHSIsComplex = LHSType->isAnyComplexType();
1429 unsigned LHSOffset;
1430 bool RHSIsComplex = RHSType->isAnyComplexType();
1431
1432 // For ComplexComplex Mul, we have special ops to make their implementation
1433 // easier.
1434 BinaryOperatorKind Op = E->getOpcode();
1435 if (Op == BO_Mul && LHSIsComplex && RHSIsComplex) {
1436 assert(classifyPrim(LHSType->getAs<ComplexType>()->getElementType()) ==
1437 classifyPrim(RHSType->getAs<ComplexType>()->getElementType()));
1438 PrimType ElemT =
1439 classifyPrim(LHSType->getAs<ComplexType>()->getElementType());
1440 if (!this->visit(LHS))
1441 return false;
1442 if (!this->visit(RHS))
1443 return false;
1444 if (!this->emitMulc(ElemT, E))
1445 return false;
1446 if (DiscardResult)
1447 return this->emitPopPtr(E);
1448 return true;
1449 }
1450
1451 if (Op == BO_Div && RHSIsComplex) {
1452 QualType ElemQT = RHSType->getAs<ComplexType>()->getElementType();
1453 PrimType ElemT = classifyPrim(ElemQT);
1454 // If the LHS is not complex, we still need to do the full complex
1455 // division, so just stub create a complex value and stub it out with
1456 // the LHS and a zero.
1457
1458 if (!LHSIsComplex) {
1459 // This is using the RHS type for the fake-complex LHS.
1460 UnsignedOrNone LocalIndex = allocateTemporary(E: RHS);
1461 if (!LocalIndex)
1462 return false;
1463 LHSOffset = *LocalIndex;
1464
1465 if (!this->emitGetPtrLocal(LHSOffset, E))
1466 return false;
1467
1468 if (!this->visit(LHS))
1469 return false;
1470 // real is LHS
1471 if (!this->emitInitElem(ElemT, 0, E))
1472 return false;
1473 // imag is zero
1474 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1475 return false;
1476 if (!this->emitInitElem(ElemT, 1, E))
1477 return false;
1478 } else {
1479 if (!this->visit(LHS))
1480 return false;
1481 }
1482
1483 if (!this->visit(RHS))
1484 return false;
1485 if (!this->emitDivc(ElemT, E))
1486 return false;
1487 if (DiscardResult)
1488 return this->emitPopPtr(E);
1489 return true;
1490 }
1491
1492 // Evaluate LHS and save value to LHSOffset.
1493 if (LHSType->isAnyComplexType()) {
1494 LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
1495 if (!this->visit(LHS))
1496 return false;
1497 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1498 return false;
1499 } else {
1500 PrimType LHST = classifyPrim(LHSType);
1501 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
1502 if (!this->visit(LHS))
1503 return false;
1504 if (!this->emitSetLocal(LHST, LHSOffset, E))
1505 return false;
1506 }
1507
1508 // Same with RHS.
1509 unsigned RHSOffset;
1510 if (RHSType->isAnyComplexType()) {
1511 RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
1512 if (!this->visit(RHS))
1513 return false;
1514 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1515 return false;
1516 } else {
1517 PrimType RHST = classifyPrim(RHSType);
1518 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
1519 if (!this->visit(RHS))
1520 return false;
1521 if (!this->emitSetLocal(RHST, RHSOffset, E))
1522 return false;
1523 }
1524
1525 // For both LHS and RHS, either load the value from the complex pointer, or
1526 // directly from the local variable. For index 1 (i.e. the imaginary part),
1527 // just load 0 and do the operation anyway.
1528 auto loadComplexValue = [this](bool IsComplex, bool LoadZero,
1529 unsigned ElemIndex, unsigned Offset,
1530 const Expr *E) -> bool {
1531 if (IsComplex) {
1532 if (!this->emitGetLocal(PT_Ptr, Offset, E))
1533 return false;
1534 return this->emitArrayElemPop(classifyComplexElementType(T: E->getType()),
1535 ElemIndex, E);
1536 }
1537 if (ElemIndex == 0 || !LoadZero)
1538 return this->emitGetLocal(classifyPrim(E->getType()), Offset, E);
1539 return this->visitZeroInitializer(classifyPrim(E->getType()), E->getType(),
1540 E);
1541 };
1542
1543 // Now we can get pointers to the LHS and RHS from the offsets above.
1544 for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
1545 // Result pointer for the store later.
1546 if (!this->DiscardResult) {
1547 if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
1548 return false;
1549 }
1550
1551 // The actual operation.
1552 switch (Op) {
1553 case BO_Add:
1554 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1555 return false;
1556
1557 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1558 return false;
1559 if (ResultElemT == PT_Float) {
1560 if (!this->emitAddf(getFPOptions(E), E))
1561 return false;
1562 } else {
1563 if (!this->emitAdd(ResultElemT, E))
1564 return false;
1565 }
1566 break;
1567 case BO_Sub:
1568 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1569 return false;
1570
1571 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1572 return false;
1573 if (ResultElemT == PT_Float) {
1574 if (!this->emitSubf(getFPOptions(E), E))
1575 return false;
1576 } else {
1577 if (!this->emitSub(ResultElemT, E))
1578 return false;
1579 }
1580 break;
1581 case BO_Mul:
1582 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1583 return false;
1584
1585 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1586 return false;
1587
1588 if (ResultElemT == PT_Float) {
1589 if (!this->emitMulf(getFPOptions(E), E))
1590 return false;
1591 } else {
1592 if (!this->emitMul(ResultElemT, E))
1593 return false;
1594 }
1595 break;
1596 case BO_Div:
1597 assert(!RHSIsComplex);
1598 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1599 return false;
1600
1601 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1602 return false;
1603
1604 if (ResultElemT == PT_Float) {
1605 if (!this->emitDivf(getFPOptions(E), E))
1606 return false;
1607 } else {
1608 if (!this->emitDiv(ResultElemT, E))
1609 return false;
1610 }
1611 break;
1612
1613 default:
1614 return false;
1615 }
1616
1617 if (!this->DiscardResult) {
1618 // Initialize array element with the value we just computed.
1619 if (!this->emitInitElemPop(ResultElemT, ElemIndex, E))
1620 return false;
1621 } else {
1622 if (!this->emitPop(ResultElemT, E))
1623 return false;
1624 // Remove the Complex temporary pointer we created ourselves at the
1625 // beginning of this function.
1626 if (!Initializing)
1627 return this->emitPopPtr(E);
1628 }
1629 }
1630 return true;
1631}
1632
1633template <class Emitter>
1634bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
1635 const Expr *LHS = E->getLHS();
1636 const Expr *RHS = E->getRHS();
1637 assert(!E->isCommaOp() &&
1638 "Comma op should be handled in VisitBinaryOperator");
1639 assert(E->getType()->isVectorType());
1640 assert(LHS->getType()->isVectorType());
1641 assert(RHS->getType()->isVectorType());
1642
1643 // We can only handle vectors with primitive element types.
1644 if (!canClassify(LHS->getType()->castAs<VectorType>()->getElementType()))
1645 return false;
1646
1647 // Prepare storage for result.
1648 if (!Initializing && !E->isCompoundAssignmentOp() && !E->isAssignmentOp()) {
1649 UnsignedOrNone LocalIndex = allocateTemporary(E);
1650 if (!LocalIndex)
1651 return false;
1652 if (!this->emitGetPtrLocal(*LocalIndex, E))
1653 return false;
1654 }
1655
1656 const auto *VecTy = E->getType()->getAs<VectorType>();
1657 auto Op = E->isCompoundAssignmentOp()
1658 ? BinaryOperator::getOpForCompoundAssignment(Opc: E->getOpcode())
1659 : E->getOpcode();
1660
1661 PrimType ElemT = this->classifyVectorElementType(LHS->getType());
1662 PrimType RHSElemT = this->classifyVectorElementType(RHS->getType());
1663 PrimType ResultElemT = this->classifyVectorElementType(E->getType());
1664
1665 if (E->getOpcode() == BO_Assign) {
1666 assert(Ctx.getASTContext().hasSameUnqualifiedType(
1667 LHS->getType()->castAs<VectorType>()->getElementType(),
1668 RHS->getType()->castAs<VectorType>()->getElementType()));
1669 if (!this->visit(LHS))
1670 return false;
1671 if (!this->visit(RHS))
1672 return false;
1673 if (!this->emitCopyArray(ElemT, 0, 0, VecTy->getNumElements(), E))
1674 return false;
1675 if (DiscardResult)
1676 return this->emitPopPtr(E);
1677 return true;
1678 }
1679
1680 // Evaluate LHS and save value to LHSOffset.
1681 unsigned LHSOffset =
1682 this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
1683 if (!this->visit(LHS))
1684 return false;
1685 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1686 return false;
1687
1688 // Evaluate RHS and save value to RHSOffset.
1689 unsigned RHSOffset =
1690 this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
1691 if (!this->visit(RHS))
1692 return false;
1693 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1694 return false;
1695
1696 if (E->isCompoundAssignmentOp() && !this->emitGetLocal(PT_Ptr, LHSOffset, E))
1697 return false;
1698
1699 // BitAdd/BitOr/BitXor/Shl/Shr doesn't support bool type, we need perform the
1700 // integer promotion.
1701 bool NeedIntPromot = ElemT == PT_Bool && (E->isBitwiseOp() || E->isShiftOp());
1702 QualType PromotTy;
1703 PrimType PromotT = PT_Bool;
1704 PrimType OpT = ElemT;
1705 if (NeedIntPromot) {
1706 PromotTy =
1707 Ctx.getASTContext().getPromotedIntegerType(PromotableType: Ctx.getASTContext().BoolTy);
1708 PromotT = classifyPrim(PromotTy);
1709 OpT = PromotT;
1710 }
1711
1712 auto getElem = [=](unsigned Offset, PrimType ElemT, unsigned Index) {
1713 if (!this->emitGetLocal(PT_Ptr, Offset, E))
1714 return false;
1715 if (!this->emitArrayElemPop(ElemT, Index, E))
1716 return false;
1717 if (E->isLogicalOp()) {
1718 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
1719 return false;
1720 if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
1721 return false;
1722 } else if (NeedIntPromot) {
1723 if (!this->emitPrimCast(ElemT, PromotT, PromotTy, E))
1724 return false;
1725 }
1726 return true;
1727 };
1728
1729#define EMIT_ARITH_OP(OP) \
1730 { \
1731 if (ElemT == PT_Float) { \
1732 if (!this->emit##OP##f(getFPOptions(E), E)) \
1733 return false; \
1734 } else { \
1735 if (!this->emit##OP(ElemT, E)) \
1736 return false; \
1737 } \
1738 break; \
1739 }
1740
1741 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
1742 if (!getElem(LHSOffset, ElemT, I))
1743 return false;
1744 if (!getElem(RHSOffset, RHSElemT, I))
1745 return false;
1746 switch (Op) {
1747 case BO_Add:
1748 EMIT_ARITH_OP(Add)
1749 case BO_Sub:
1750 EMIT_ARITH_OP(Sub)
1751 case BO_Mul:
1752 EMIT_ARITH_OP(Mul)
1753 case BO_Div:
1754 EMIT_ARITH_OP(Div)
1755 case BO_Rem:
1756 if (!this->emitRem(ElemT, E))
1757 return false;
1758 break;
1759 case BO_And:
1760 if (!this->emitBitAnd(OpT, E))
1761 return false;
1762 break;
1763 case BO_Or:
1764 if (!this->emitBitOr(OpT, E))
1765 return false;
1766 break;
1767 case BO_Xor:
1768 if (!this->emitBitXor(OpT, E))
1769 return false;
1770 break;
1771 case BO_Shl:
1772 if (!this->emitShl(OpT, RHSElemT, E))
1773 return false;
1774 break;
1775 case BO_Shr:
1776 if (!this->emitShr(OpT, RHSElemT, E))
1777 return false;
1778 break;
1779 case BO_EQ:
1780 if (!this->emitEQ(ElemT, E))
1781 return false;
1782 break;
1783 case BO_NE:
1784 if (!this->emitNE(ElemT, E))
1785 return false;
1786 break;
1787 case BO_LE:
1788 if (!this->emitLE(ElemT, E))
1789 return false;
1790 break;
1791 case BO_LT:
1792 if (!this->emitLT(ElemT, E))
1793 return false;
1794 break;
1795 case BO_GE:
1796 if (!this->emitGE(ElemT, E))
1797 return false;
1798 break;
1799 case BO_GT:
1800 if (!this->emitGT(ElemT, E))
1801 return false;
1802 break;
1803 case BO_LAnd:
1804 // a && b is equivalent to a!=0 & b!=0
1805 if (!this->emitBitAnd(ResultElemT, E))
1806 return false;
1807 break;
1808 case BO_LOr:
1809 // a || b is equivalent to a!=0 | b!=0
1810 if (!this->emitBitOr(ResultElemT, E))
1811 return false;
1812 break;
1813 default:
1814 return this->emitInvalid(E);
1815 }
1816
1817 // The result of the comparison is a vector of the same width and number
1818 // of elements as the comparison operands with a signed integral element
1819 // type.
1820 //
1821 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
1822 if (E->isComparisonOp()) {
1823 if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
1824 return false;
1825 if (!this->emitNeg(ResultElemT, E))
1826 return false;
1827 }
1828
1829 // If we performed an integer promotion, we need to cast the compute result
1830 // into result vector element type.
1831 if (NeedIntPromot &&
1832 !this->emitPrimCast(PromotT, ResultElemT, VecTy->getElementType(), E))
1833 return false;
1834
1835 // Initialize array element with the value we just computed.
1836 if (!this->emitInitElem(ResultElemT, I, E))
1837 return false;
1838 }
1839
1840 if (DiscardResult && E->isCompoundAssignmentOp() && !this->emitPopPtr(E))
1841 return false;
1842 return true;
1843}
1844
1845template <class Emitter>
1846bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
1847 const Expr *LHS = E->getLHS();
1848 const Expr *RHS = E->getRHS();
1849 const ASTContext &ASTCtx = Ctx.getASTContext();
1850
1851 assert(LHS->getType()->isFixedPointType() ||
1852 RHS->getType()->isFixedPointType());
1853
1854 auto LHSSema = ASTCtx.getFixedPointSemantics(Ty: LHS->getType());
1855 auto LHSSemaInt = LHSSema.toOpaqueInt();
1856 auto RHSSema = ASTCtx.getFixedPointSemantics(Ty: RHS->getType());
1857 auto RHSSemaInt = RHSSema.toOpaqueInt();
1858
1859 if (!this->visit(LHS))
1860 return false;
1861 if (!LHS->getType()->isFixedPointType()) {
1862 if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()),
1863 LHSSemaInt, E))
1864 return false;
1865 }
1866
1867 if (!this->visit(RHS))
1868 return false;
1869 if (!RHS->getType()->isFixedPointType()) {
1870 if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()),
1871 RHSSemaInt, E))
1872 return false;
1873 }
1874
1875 // Convert the result to the target semantics.
1876 auto ConvertResult = [&](bool R) -> bool {
1877 if (!R)
1878 return false;
1879 auto ResultSema = ASTCtx.getFixedPointSemantics(Ty: E->getType()).toOpaqueInt();
1880 auto CommonSema = LHSSema.getCommonSemantics(Other: RHSSema).toOpaqueInt();
1881 if (ResultSema != CommonSema)
1882 return this->emitCastFixedPoint(ResultSema, E);
1883 return true;
1884 };
1885
1886 auto MaybeCastToBool = [&](bool Result) {
1887 if (!Result)
1888 return false;
1889 PrimType T = classifyPrim(E);
1890 if (DiscardResult)
1891 return this->emitPop(T, E);
1892 if (T != PT_Bool)
1893 return this->emitCast(PT_Bool, T, E);
1894 return true;
1895 };
1896
1897 switch (E->getOpcode()) {
1898 case BO_EQ:
1899 return MaybeCastToBool(this->emitEQFixedPoint(E));
1900 case BO_NE:
1901 return MaybeCastToBool(this->emitNEFixedPoint(E));
1902 case BO_LT:
1903 return MaybeCastToBool(this->emitLTFixedPoint(E));
1904 case BO_LE:
1905 return MaybeCastToBool(this->emitLEFixedPoint(E));
1906 case BO_GT:
1907 return MaybeCastToBool(this->emitGTFixedPoint(E));
1908 case BO_GE:
1909 return MaybeCastToBool(this->emitGEFixedPoint(E));
1910 case BO_Add:
1911 return ConvertResult(this->emitAddFixedPoint(E));
1912 case BO_Sub:
1913 return ConvertResult(this->emitSubFixedPoint(E));
1914 case BO_Mul:
1915 return ConvertResult(this->emitMulFixedPoint(E));
1916 case BO_Div:
1917 return ConvertResult(this->emitDivFixedPoint(E));
1918 case BO_Shl:
1919 return ConvertResult(this->emitShiftFixedPoint(/*Left=*/true, E));
1920 case BO_Shr:
1921 return ConvertResult(this->emitShiftFixedPoint(/*Left=*/false, E));
1922
1923 default:
1924 return this->emitInvalid(E);
1925 }
1926
1927 llvm_unreachable("unhandled binop opcode");
1928}
1929
1930template <class Emitter>
1931bool Compiler<Emitter>::VisitFixedPointUnaryOperator(const UnaryOperator *E) {
1932 const Expr *SubExpr = E->getSubExpr();
1933 assert(SubExpr->getType()->isFixedPointType());
1934
1935 switch (E->getOpcode()) {
1936 case UO_Plus:
1937 return this->delegate(SubExpr);
1938 case UO_Minus:
1939 if (!this->visit(SubExpr))
1940 return false;
1941 return this->emitNegFixedPoint(E);
1942 default:
1943 return false;
1944 }
1945
1946 llvm_unreachable("Unhandled unary opcode");
1947}
1948
1949template <class Emitter>
1950bool Compiler<Emitter>::VisitImplicitValueInitExpr(
1951 const ImplicitValueInitExpr *E) {
1952 if (DiscardResult)
1953 return true;
1954
1955 QualType QT = E->getType();
1956
1957 if (OptPrimType T = classify(QT))
1958 return this->visitZeroInitializer(*T, QT, E);
1959
1960 if (QT->isRecordType()) {
1961 const RecordDecl *RD = QT->getAsRecordDecl();
1962 assert(RD);
1963 if (RD->isInvalidDecl())
1964 return false;
1965
1966 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
1967 CXXRD && CXXRD->getNumVBases() > 0) {
1968 // TODO: Diagnose.
1969 return false;
1970 }
1971
1972 const Record *R = getRecord(QT);
1973 if (!R)
1974 return false;
1975
1976 assert(Initializing);
1977 return this->visitZeroRecordInitializer(R, E);
1978 }
1979
1980 if (QT->isIncompleteArrayType())
1981 return true;
1982
1983 if (QT->isArrayType())
1984 return this->visitZeroArrayInitializer(QT, E);
1985
1986 if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) {
1987 assert(Initializing);
1988 QualType ElemQT = ComplexTy->getElementType();
1989 PrimType ElemT = classifyPrim(ElemQT);
1990 for (unsigned I = 0; I < 2; ++I) {
1991 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1992 return false;
1993 if (!this->emitInitElem(ElemT, I, E))
1994 return false;
1995 }
1996 return true;
1997 }
1998
1999 if (const auto *VecT = E->getType()->getAs<VectorType>()) {
2000 unsigned NumVecElements = VecT->getNumElements();
2001 QualType ElemQT = VecT->getElementType();
2002 PrimType ElemT = classifyPrim(ElemQT);
2003
2004 for (unsigned I = 0; I < NumVecElements; ++I) {
2005 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2006 return false;
2007 if (!this->emitInitElem(ElemT, I, E))
2008 return false;
2009 }
2010 return true;
2011 }
2012
2013 if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) {
2014 unsigned NumElems = MT->getNumElementsFlattened();
2015 QualType ElemQT = MT->getElementType();
2016 PrimType ElemT = classifyPrim(ElemQT);
2017
2018 for (unsigned I = 0; I != NumElems; ++I) {
2019 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2020 return false;
2021 if (!this->emitInitElem(ElemT, I, E))
2022 return false;
2023 }
2024 return true;
2025 }
2026
2027 return false;
2028}
2029
2030template <class Emitter>
2031bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
2032 if (E->getType()->isVoidType())
2033 return false;
2034
2035 const Expr *LHS = E->getLHS();
2036 const Expr *RHS = E->getRHS();
2037 const Expr *Index = E->getIdx();
2038 const Expr *Base = E->getBase();
2039
2040 // C++17's rules require us to evaluate the LHS first, regardless of which
2041 // side is the base.
2042 bool Success = true;
2043 for (const Expr *SubExpr : {LHS, RHS}) {
2044 if (!this->visit(SubExpr)) {
2045 Success = false;
2046 continue;
2047 }
2048
2049 // Expand the base if this is a subscript on a
2050 // pointer expression.
2051 if (SubExpr == Base && Base->getType()->isPointerType()) {
2052 if (!this->emitExpandPtr(E))
2053 Success = false;
2054 }
2055 }
2056
2057 if (!Success)
2058 return false;
2059
2060 OptPrimType IndexT = classify(Index->getType());
2061 // In error-recovery cases, the index expression has a dependent type.
2062 if (!IndexT)
2063 return this->emitError(E);
2064 // If the index is first, we need to change that.
2065 if (LHS == Index) {
2066 if (!this->emitFlip(PT_Ptr, *IndexT, E))
2067 return false;
2068 }
2069
2070 if (!this->emitArrayElemPtrPop(*IndexT, E))
2071 return false;
2072 if (DiscardResult)
2073 return this->emitPopPtr(E);
2074
2075 if (E->isGLValue())
2076 return true;
2077
2078 OptPrimType T = classifyPrim(E);
2079 return this->emitLoadPop(*T, E);
2080}
2081
2082template <class Emitter>
2083bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
2084 const Expr *ArrayFiller, const Expr *E) {
2085 InitLinkScope<Emitter> ILS(this, InitLink::InitList());
2086
2087 QualType QT = E->getType();
2088 if (const auto *AT = QT->getAs<AtomicType>())
2089 QT = AT->getValueType();
2090
2091 if (QT->isVoidType()) {
2092 if (Inits.size() == 0)
2093 return true;
2094 return this->emitInvalid(E);
2095 }
2096
2097 // Handle discarding first.
2098 if (DiscardResult) {
2099 for (const Expr *Init : Inits) {
2100 if (!this->discard(Init))
2101 return false;
2102 }
2103 return true;
2104 }
2105
2106 // Primitive values.
2107 if (OptPrimType T = classify(QT)) {
2108 assert(!DiscardResult);
2109 if (Inits.size() == 0)
2110 return this->visitZeroInitializer(*T, QT, E);
2111 assert(Inits.size() == 1);
2112 return this->delegate(Inits[0]);
2113 }
2114
2115 if (QT->isRecordType()) {
2116 const Record *R = getRecord(QT);
2117
2118 if (Inits.size() == 1 && E->getType() == Inits[0]->getType())
2119 return this->delegate(Inits[0]);
2120
2121 if (!R)
2122 return false;
2123
2124 auto initPrimitiveField = [=](const Record::Field *FieldToInit,
2125 const Expr *Init, PrimType T,
2126 bool Activate = false) -> bool {
2127 InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: Init));
2128 if (!this->visit(Init))
2129 return false;
2130
2131 bool BitField = FieldToInit->isBitField();
2132 if (BitField && Activate)
2133 return this->emitInitBitFieldActivate(T, FieldToInit, E);
2134 if (BitField)
2135 return this->emitInitBitField(T, FieldToInit, E);
2136 if (Activate)
2137 return this->emitInitFieldActivate(T, FieldToInit->Offset, E);
2138 return this->emitInitField(T, FieldToInit->Offset, E);
2139 };
2140
2141 auto initCompositeField = [=](const Record::Field *FieldToInit,
2142 const Expr *Init,
2143 bool Activate = false) -> bool {
2144 InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: Init));
2145 InitLinkScope<Emitter> ILS(this, InitLink::Field(Offset: FieldToInit->Offset));
2146
2147 // Non-primitive case. Get a pointer to the field-to-initialize
2148 // on the stack and recurse into visitInitializer().
2149 if (!this->emitGetPtrField(FieldToInit->Offset, Init))
2150 return false;
2151
2152 if (Activate && !this->emitActivate(E))
2153 return false;
2154
2155 if (!this->visitInitializer(Init))
2156 return false;
2157 return this->emitPopPtr(E);
2158 };
2159
2160 if (R->isUnion()) {
2161 if (Inits.size() == 0) {
2162 if (!this->visitZeroRecordInitializer(R, E))
2163 return false;
2164 } else {
2165 const Expr *Init = Inits[0];
2166 const FieldDecl *FToInit = nullptr;
2167 if (const auto *ILE = dyn_cast<InitListExpr>(Val: E))
2168 FToInit = ILE->getInitializedFieldInUnion();
2169 else
2170 FToInit = cast<CXXParenListInitExpr>(Val: E)->getInitializedFieldInUnion();
2171
2172 const Record::Field *FieldToInit = R->getField(FD: FToInit);
2173 if (OptPrimType T = classify(Init)) {
2174 if (!initPrimitiveField(FieldToInit, Init, *T, /*Activate=*/true))
2175 return false;
2176 } else {
2177 if (!initCompositeField(FieldToInit, Init, /*Activate=*/true))
2178 return false;
2179 }
2180 }
2181 return this->emitFinishInit(E);
2182 }
2183
2184 assert(!R->isUnion());
2185 unsigned InitIndex = 0;
2186 for (const Expr *Init : Inits) {
2187 // Skip unnamed bitfields.
2188 while (InitIndex < R->getNumFields() &&
2189 R->getField(I: InitIndex)->isUnnamedBitField())
2190 ++InitIndex;
2191
2192 if (OptPrimType T = classify(Init)) {
2193 const Record::Field *FieldToInit = R->getField(I: InitIndex);
2194 if (!initPrimitiveField(FieldToInit, Init, *T))
2195 return false;
2196 ++InitIndex;
2197 } else {
2198 // Initializer for a direct base class.
2199 if (const Record::Base *B = R->getBase(T: Init->getType())) {
2200 if (!this->emitGetPtrBase(B->Offset, Init))
2201 return false;
2202
2203 if (!this->visitInitializer(Init))
2204 return false;
2205
2206 if (!this->emitFinishInitPop(E))
2207 return false;
2208 // Base initializers don't increase InitIndex, since they don't count
2209 // into the Record's fields.
2210 } else {
2211 const Record::Field *FieldToInit = R->getField(I: InitIndex);
2212 if (!initCompositeField(FieldToInit, Init))
2213 return false;
2214 ++InitIndex;
2215 }
2216 }
2217 }
2218 return this->emitFinishInit(E);
2219 }
2220
2221 if (QT->isArrayType()) {
2222 if (Inits.size() == 1 && QT == Inits[0]->getType())
2223 return this->delegate(Inits[0]);
2224
2225 const ConstantArrayType *CAT =
2226 Ctx.getASTContext().getAsConstantArrayType(T: QT);
2227 uint64_t NumElems = CAT->getZExtSize();
2228
2229 if (!this->emitCheckArraySize(NumElems, E))
2230 return false;
2231
2232 OptPrimType InitT = classify(CAT->getElementType());
2233 unsigned ElementIndex = 0;
2234 for (const Expr *Init : Inits) {
2235 if (const auto *EmbedS =
2236 dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) {
2237 PrimType TargetT = classifyPrim(Init->getType());
2238
2239 auto Eval = [&](const IntegerLiteral *IL, unsigned ElemIndex) {
2240 if (TargetT == PT_Float) {
2241 if (!this->emitConst(IL->getValue(), classifyPrim(IL), Init))
2242 return false;
2243 const auto *Sem = &Ctx.getFloatSemantics(T: CAT->getElementType());
2244 if (!this->emitCastIntegralFloating(classifyPrim(IL), Sem,
2245 getFPOptions(E), E))
2246 return false;
2247 } else {
2248 if (!this->emitConst(IL->getValue(), TargetT, Init))
2249 return false;
2250 }
2251 return this->emitInitElem(TargetT, ElemIndex, IL);
2252 };
2253 if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
2254 return false;
2255 } else {
2256 if (!this->visitArrayElemInit(ElementIndex, Init, InitT))
2257 return false;
2258 ++ElementIndex;
2259 }
2260 }
2261
2262 // Expand the filler expression.
2263 // FIXME: This should go away.
2264 if (ArrayFiller) {
2265 for (; ElementIndex != NumElems; ++ElementIndex) {
2266 if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
2267 return false;
2268 }
2269 }
2270
2271 return this->emitFinishInit(E);
2272 }
2273
2274 if (const auto *ComplexTy = QT->getAs<ComplexType>()) {
2275 unsigned NumInits = Inits.size();
2276
2277 if (NumInits == 1)
2278 return this->delegate(Inits[0]);
2279
2280 QualType ElemQT = ComplexTy->getElementType();
2281 PrimType ElemT = classifyPrim(ElemQT);
2282 if (NumInits == 0) {
2283 // Zero-initialize both elements.
2284 for (unsigned I = 0; I < 2; ++I) {
2285 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2286 return false;
2287 if (!this->emitInitElem(ElemT, I, E))
2288 return false;
2289 }
2290 } else if (NumInits == 2) {
2291 unsigned InitIndex = 0;
2292 for (const Expr *Init : Inits) {
2293 if (!this->visit(Init))
2294 return false;
2295
2296 if (!this->emitInitElem(ElemT, InitIndex, E))
2297 return false;
2298 ++InitIndex;
2299 }
2300 }
2301 return true;
2302 }
2303
2304 if (const auto *VecT = QT->getAs<VectorType>()) {
2305 unsigned NumVecElements = VecT->getNumElements();
2306 assert(NumVecElements >= Inits.size());
2307
2308 QualType ElemQT = VecT->getElementType();
2309 PrimType ElemT = classifyPrim(ElemQT);
2310
2311 // All initializer elements.
2312 unsigned InitIndex = 0;
2313 for (const Expr *Init : Inits) {
2314 if (!this->visit(Init))
2315 return false;
2316
2317 // If the initializer is of vector type itself, we have to deconstruct
2318 // that and initialize all the target fields from the initializer fields.
2319 if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) {
2320 if (!this->emitCopyArray(ElemT, 0, InitIndex,
2321 InitVecT->getNumElements(), E))
2322 return false;
2323 InitIndex += InitVecT->getNumElements();
2324 } else {
2325 if (!this->emitInitElem(ElemT, InitIndex, E))
2326 return false;
2327 ++InitIndex;
2328 }
2329 }
2330
2331 assert(InitIndex <= NumVecElements);
2332
2333 // Fill the rest with zeroes.
2334 for (; InitIndex != NumVecElements; ++InitIndex) {
2335 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2336 return false;
2337 if (!this->emitInitElem(ElemT, InitIndex, E))
2338 return false;
2339 }
2340 return true;
2341 }
2342
2343 if (const auto *MT = QT->getAs<ConstantMatrixType>()) {
2344 unsigned NumElems = MT->getNumElementsFlattened();
2345 assert(Inits.size() == NumElems);
2346
2347 QualType ElemQT = MT->getElementType();
2348 PrimType ElemT = classifyPrim(ElemQT);
2349
2350 // Matrix initializer list elements are in row-major order, which matches
2351 // the matrix APValue convention and therefore no index remapping is
2352 // required.
2353 for (unsigned I = 0; I != NumElems; ++I) {
2354 if (!this->visit(Inits[I]))
2355 return false;
2356 if (!this->emitInitElem(ElemT, I, E))
2357 return false;
2358 }
2359 return true;
2360 }
2361
2362 return false;
2363}
2364
2365/// Pointer to the array(not the element!) must be on the stack when calling
2366/// this.
2367template <class Emitter>
2368bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
2369 OptPrimType InitT) {
2370 if (InitT) {
2371 // Visit the primitive element like normal.
2372 if (!this->visit(Init))
2373 return false;
2374 return this->emitInitElem(*InitT, ElemIndex, Init);
2375 }
2376
2377 InitLinkScope<Emitter> ILS(this, InitLink::Elem(Index: ElemIndex));
2378 // Advance the pointer currently on the stack to the given
2379 // dimension.
2380 if (!this->emitConstUint32(ElemIndex, Init))
2381 return false;
2382 if (!this->emitArrayElemPtrUint32(Init))
2383 return false;
2384 if (!this->visitInitializer(Init))
2385 return false;
2386 return this->emitFinishInitPop(Init);
2387}
2388
2389template <class Emitter>
2390bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
2391 const FunctionDecl *FuncDecl,
2392 bool Activate, bool IsOperatorCall) {
2393 assert(VarScope->getKind() == ScopeKind::Call);
2394 llvm::BitVector NonNullArgs;
2395 if (FuncDecl && FuncDecl->hasAttr<NonNullAttr>())
2396 NonNullArgs = collectNonNullArgs(F: FuncDecl, Args);
2397
2398 bool ExplicitMemberFn = false;
2399 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: FuncDecl))
2400 ExplicitMemberFn = MD->isExplicitObjectMemberFunction();
2401
2402 unsigned ArgIndex = 0;
2403 for (const Expr *Arg : Args) {
2404 if (canClassify(Arg)) {
2405 if (!this->visit(Arg))
2406 return false;
2407 } else {
2408
2409 DeclTy Source = Arg;
2410 if (FuncDecl) {
2411 // Try to use the parameter declaration instead of the argument
2412 // expression as a source.
2413 unsigned DeclIndex = ArgIndex - IsOperatorCall + ExplicitMemberFn;
2414 if (DeclIndex < FuncDecl->getNumParams())
2415 Source = FuncDecl->getParamDecl(i: ArgIndex - IsOperatorCall +
2416 ExplicitMemberFn);
2417 }
2418
2419 UnsignedOrNone LocalIndex =
2420 allocateLocal(Decl: std::move(Source), Ty: Arg->getType(), ScopeKind::Call);
2421 if (!LocalIndex)
2422 return false;
2423
2424 if (!this->emitGetPtrLocal(*LocalIndex, Arg))
2425 return false;
2426 InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex));
2427 if (!this->visitInitializer(Arg))
2428 return false;
2429 }
2430
2431 if (ArgIndex == 1 && Activate) {
2432 if (!this->emitActivate(Arg))
2433 return false;
2434 }
2435
2436 if (!NonNullArgs.empty() && NonNullArgs[ArgIndex]) {
2437 PrimType ArgT = classify(Arg).value_or(PT_Ptr);
2438 if (ArgT == PT_Ptr) {
2439 if (!this->emitCheckNonNullArg(ArgT, Arg))
2440 return false;
2441 }
2442 }
2443
2444 ++ArgIndex;
2445 }
2446
2447 return true;
2448}
2449
2450template <class Emitter>
2451bool Compiler<Emitter>::VisitInitListExpr(const InitListExpr *E) {
2452 return this->visitInitList(E->inits(), E->getArrayFiller(), E);
2453}
2454
2455template <class Emitter>
2456bool Compiler<Emitter>::VisitCXXParenListInitExpr(
2457 const CXXParenListInitExpr *E) {
2458 return this->visitInitList(E->getInitExprs(), E->getArrayFiller(), E);
2459}
2460
2461template <class Emitter>
2462bool Compiler<Emitter>::VisitSubstNonTypeTemplateParmExpr(
2463 const SubstNonTypeTemplateParmExpr *E) {
2464 return this->delegate(E->getReplacement());
2465}
2466
2467template <class Emitter>
2468bool Compiler<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
2469 OptPrimType T = classify(E->getType());
2470 if (T && E->hasAPValueResult()) {
2471 // Try to emit the APValue directly, without visiting the subexpr.
2472 // This will only fail if we can't emit the APValue, so won't emit any
2473 // diagnostics or any double values.
2474 if (DiscardResult)
2475 return true;
2476
2477 if (this->visitAPValue(E->getAPValueResult(), *T, E))
2478 return true;
2479 }
2480 return this->delegate(E->getSubExpr());
2481}
2482
2483template <class Emitter>
2484bool Compiler<Emitter>::VisitEmbedExpr(const EmbedExpr *E) {
2485 auto It = E->begin();
2486 return this->visit(*It);
2487}
2488
2489static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx,
2490 UnaryExprOrTypeTrait Kind) {
2491 bool AlignOfReturnsPreferred =
2492 ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;
2493
2494 // C++ [expr.alignof]p3:
2495 // When alignof is applied to a reference type, the result is the
2496 // alignment of the referenced type.
2497 if (const auto *Ref = T->getAs<ReferenceType>())
2498 T = Ref->getPointeeType();
2499
2500 if (T.getQualifiers().hasUnaligned())
2501 return CharUnits::One();
2502
2503 // __alignof is defined to return the preferred alignment.
2504 // Before 8, clang returned the preferred alignment for alignof and
2505 // _Alignof as well.
2506 if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
2507 return ASTCtx.toCharUnitsFromBits(BitSize: ASTCtx.getPreferredTypeAlign(T));
2508
2509 return ASTCtx.getTypeAlignInChars(T);
2510}
2511
2512template <class Emitter>
2513bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
2514 const UnaryExprOrTypeTraitExpr *E) {
2515
2516 UnaryExprOrTypeTrait Kind = E->getKind();
2517 const ASTContext &ASTCtx = Ctx.getASTContext();
2518
2519 if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) {
2520 QualType ArgType = E->getTypeOfArgument();
2521
2522 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
2523 // the result is the size of the referenced type."
2524 if (const auto *Ref = ArgType->getAs<ReferenceType>())
2525 ArgType = Ref->getPointeeType();
2526
2527 CharUnits Size;
2528 if (ArgType->isVoidType() || ArgType->isFunctionType())
2529 Size = CharUnits::One();
2530 else {
2531 if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
2532 return this->emitInvalid(E);
2533
2534 if (Kind == UETT_SizeOf)
2535 Size = ASTCtx.getTypeSizeInChars(T: ArgType);
2536 else
2537 Size = ASTCtx.getTypeInfoDataSizeInChars(T: ArgType).Width;
2538 }
2539
2540 if (DiscardResult)
2541 return true;
2542
2543 return this->emitConst(Size.getQuantity(), E);
2544 }
2545
2546 if (Kind == UETT_CountOf) {
2547 QualType Ty = E->getTypeOfArgument();
2548 assert(Ty->isArrayType());
2549
2550 // We don't need to worry about array element qualifiers, so getting the
2551 // unsafe array type is fine.
2552 if (const auto *CAT =
2553 dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) {
2554 if (DiscardResult)
2555 return true;
2556 return this->emitConst(CAT->getSize(), E);
2557 }
2558
2559 assert(!Ty->isConstantSizeType());
2560
2561 // If it's a variable-length array type, we need to check whether it is a
2562 // multidimensional array. If so, we need to check the size expression of
2563 // the VLA to see if it's a constant size. If so, we can return that value.
2564 const auto *VAT = ASTCtx.getAsVariableArrayType(T: Ty);
2565 assert(VAT);
2566 if (VAT->getElementType()->isArrayType()) {
2567 std::optional<APSInt> Res =
2568 VAT->getSizeExpr()
2569 ? VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: ASTCtx)
2570 : std::nullopt;
2571 if (Res) {
2572 if (DiscardResult)
2573 return true;
2574 return this->emitConst(*Res, E);
2575 }
2576 }
2577 }
2578
2579 if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
2580 CharUnits Size;
2581
2582 if (E->isArgumentType()) {
2583 QualType ArgType = E->getTypeOfArgument();
2584
2585 Size = AlignOfType(T: ArgType, ASTCtx, Kind);
2586 } else {
2587 // Argument is an expression, not a type.
2588 const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
2589
2590 if (Arg->getType()->isDependentType())
2591 return false;
2592
2593 // The kinds of expressions that we have special-case logic here for
2594 // should be kept up to date with the special checks for those
2595 // expressions in Sema.
2596
2597 // alignof decl is always accepted, even if it doesn't make sense: we
2598 // default to 1 in those cases.
2599 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Arg))
2600 Size = ASTCtx.getDeclAlign(D: DRE->getDecl(),
2601 /*RefAsPointee*/ ForAlignof: true);
2602 else if (const auto *ME = dyn_cast<MemberExpr>(Val: Arg))
2603 Size = ASTCtx.getDeclAlign(D: ME->getMemberDecl(),
2604 /*RefAsPointee*/ ForAlignof: true);
2605 else
2606 Size = AlignOfType(T: Arg->getType(), ASTCtx, Kind);
2607 }
2608
2609 if (DiscardResult)
2610 return true;
2611
2612 return this->emitConst(Size.getQuantity(), E);
2613 }
2614
2615 if (Kind == UETT_VectorElements) {
2616 if (E->containsErrors())
2617 return false;
2618
2619 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
2620 return this->emitConst(VT->getNumElements(), E);
2621 assert(E->getTypeOfArgument()->isSizelessVectorType());
2622 return this->emitSizelessVectorElementSize(E);
2623 }
2624
2625 if (Kind == UETT_VecStep) {
2626 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) {
2627 unsigned N = VT->getNumElements();
2628
2629 // The vec_step built-in functions that take a 3-component
2630 // vector return 4. (OpenCL 1.1 spec 6.11.12)
2631 if (N == 3)
2632 N = 4;
2633
2634 return this->emitConst(N, E);
2635 }
2636 return this->emitConst(1, E);
2637 }
2638
2639 if (Kind == UETT_OpenMPRequiredSimdAlign) {
2640 assert(E->isArgumentType());
2641 unsigned Bits = ASTCtx.getOpenMPDefaultSimdAlign(T: E->getArgumentType());
2642
2643 return this->emitConst(ASTCtx.toCharUnitsFromBits(BitSize: Bits).getQuantity(), E);
2644 }
2645
2646 if (Kind == UETT_PtrAuthTypeDiscriminator) {
2647 if (E->getArgumentType()->isDependentType())
2648 return this->emitInvalid(E);
2649
2650 return this->emitConst(
2651 const_cast<ASTContext &>(ASTCtx).getPointerAuthTypeDiscriminator(
2652 T: E->getArgumentType()),
2653 E);
2654 }
2655
2656 return false;
2657}
2658
2659template <class Emitter>
2660bool Compiler<Emitter>::VisitMemberExpr(const MemberExpr *E) {
2661 // 'Base.Member'
2662 const Expr *Base = E->getBase();
2663 const ValueDecl *Member = E->getMemberDecl();
2664
2665 if (DiscardResult)
2666 return this->discard(Base);
2667
2668 // MemberExprs are almost always lvalues, in which case we don't need to
2669 // do the load. But sometimes they aren't.
2670 const auto maybeLoadValue = [&]() -> bool {
2671 if (E->isGLValue())
2672 return true;
2673 if (OptPrimType T = classify(E))
2674 return this->emitLoadPop(*T, E);
2675 return false;
2676 };
2677
2678 if (const auto *VD = dyn_cast<VarDecl>(Val: Member)) {
2679 // I am almost confident in saying that a var decl must be static
2680 // and therefore registered as a global variable. But this will probably
2681 // turn out to be wrong some time in the future, as always.
2682 if (auto GlobalIndex = P.getGlobal(VD))
2683 return this->emitGetPtrGlobal(*GlobalIndex, E) && maybeLoadValue();
2684 return false;
2685 }
2686
2687 if (!isa<FieldDecl>(Val: Member)) {
2688 if (!this->discard(Base) && !this->emitSideEffect(E))
2689 return false;
2690
2691 return this->visitDeclRef(Member, E);
2692 }
2693
2694 if (!this->visit(Base))
2695 return false;
2696
2697 // Base above gives us a pointer on the stack.
2698 const auto *FD = cast<FieldDecl>(Val: Member);
2699 const RecordDecl *RD = FD->getParent();
2700 const Record *R = getRecord(RD);
2701 if (!R)
2702 return false;
2703 const Record::Field *F = R->getField(FD);
2704 // Leave a pointer to the field on the stack.
2705 if (F->Decl->getType()->isReferenceType())
2706 return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue();
2707 return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue();
2708}
2709
2710template <class Emitter>
2711bool Compiler<Emitter>::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
2712 // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
2713 // stand-alone, e.g. via EvaluateAsInt().
2714 if (!ArrayIndex)
2715 return false;
2716 return this->emitConst(*ArrayIndex, E);
2717}
2718
2719template <class Emitter>
2720bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
2721 assert(Initializing);
2722 assert(!DiscardResult);
2723
2724 // We visit the common opaque expression here once so we have its value
2725 // cached.
2726 if (!this->discard(E->getCommonExpr()))
2727 return false;
2728
2729 // TODO: This compiles to quite a lot of bytecode if the array is larger.
2730 // Investigate compiling this to a loop.
2731 const Expr *SubExpr = E->getSubExpr();
2732 size_t Size = E->getArraySize().getZExtValue();
2733 OptPrimType SubExprT = classify(SubExpr);
2734
2735 // So, every iteration, we execute an assignment here
2736 // where the LHS is on the stack (the target array)
2737 // and the RHS is our SubExpr.
2738 for (size_t I = 0; I != Size; ++I) {
2739 ArrayIndexScope<Emitter> IndexScope(this, I);
2740 LocalScope<Emitter> BS(this, ScopeKind::FullExpression);
2741
2742 if (!this->visitArrayElemInit(I, SubExpr, SubExprT))
2743 return false;
2744 if (!BS.destroyLocals())
2745 return false;
2746 }
2747 return true;
2748}
2749
2750template <class Emitter>
2751bool Compiler<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2752 const Expr *SourceExpr = E->getSourceExpr();
2753 if (!SourceExpr)
2754 return false;
2755
2756 if (Initializing)
2757 return this->visitInitializer(SourceExpr);
2758
2759 PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr);
2760 if (auto It = OpaqueExprs.find(Val: E); It != OpaqueExprs.end())
2761 return this->emitGetLocal(SubExprT, It->second, E);
2762
2763 if (!this->visit(SourceExpr))
2764 return false;
2765
2766 // At this point we either have the evaluated source expression or a pointer
2767 // to an object on the stack. We want to create a local variable that stores
2768 // this value.
2769 unsigned LocalIndex = allocateLocalPrimitive(Decl: E, Ty: SubExprT, /*IsConst=*/true);
2770 if (!this->emitSetLocal(SubExprT, LocalIndex, E))
2771 return false;
2772
2773 // Here the local variable is created but the value is removed from the stack,
2774 // so we put it back if the caller needs it.
2775 if (!DiscardResult) {
2776 if (!this->emitGetLocal(SubExprT, LocalIndex, E))
2777 return false;
2778 }
2779
2780 // This is cleaned up when the local variable is destroyed.
2781 OpaqueExprs.insert(KV: {E, LocalIndex});
2782
2783 return true;
2784}
2785
2786template <class Emitter>
2787bool Compiler<Emitter>::VisitAbstractConditionalOperator(
2788 const AbstractConditionalOperator *E) {
2789 const Expr *Condition = E->getCond();
2790 const Expr *TrueExpr = E->getTrueExpr();
2791 const Expr *FalseExpr = E->getFalseExpr();
2792
2793 if (std::optional<bool> BoolValue = getBoolValue(E: Condition)) {
2794 if (*BoolValue)
2795 return this->delegate(TrueExpr);
2796 return this->delegate(FalseExpr);
2797 }
2798
2799 // Force-init the scope, which creates a InitScope op. This is necessary so
2800 // the scope is not only initialized in one arm of the conditional operator.
2801 this->VarScope->forceInit();
2802 // The TrueExpr and FalseExpr of a conditional operator do _not_ create a
2803 // scope, which means the local variables created within them unconditionally
2804 // always exist. However, we need to later differentiate which branch was
2805 // taken and only destroy the varibles of the active branch. This is what the
2806 // "enabled" flags on local variables are used for.
2807 llvm::SaveAndRestore LAAA(this->VarScope->LocalsAlwaysEnabled,
2808 /*NewValue=*/false);
2809 bool IsBcpCall = false;
2810 if (const auto *CE = dyn_cast<CallExpr>(Val: Condition->IgnoreParenCasts());
2811 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) {
2812 IsBcpCall = true;
2813 }
2814
2815 LabelTy LabelEnd = this->getLabel(); // Label after the operator.
2816 LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
2817
2818 if (IsBcpCall) {
2819 if (!this->emitStartSpeculation(E))
2820 return false;
2821 }
2822
2823 if (!this->visitBool(Condition)) {
2824 // If the condition failed and we're checking for undefined behavior
2825 // (which only happens with EvalEmitter) check the TrueExpr and FalseExpr
2826 // as well.
2827 if (this->checkingForUndefinedBehavior()) {
2828 if (!this->discard(TrueExpr))
2829 return false;
2830 if (!this->discard(FalseExpr))
2831 return false;
2832 }
2833 return false;
2834 }
2835
2836 if (!this->jumpFalse(LabelFalse))
2837 return false;
2838 if (!this->delegate(TrueExpr))
2839 return false;
2840
2841 if (!this->jump(LabelEnd))
2842 return false;
2843 this->emitLabel(LabelFalse);
2844 if (!this->delegate(FalseExpr))
2845 return false;
2846
2847 this->fallthrough(LabelEnd);
2848 this->emitLabel(LabelEnd);
2849
2850 if (IsBcpCall)
2851 return this->emitEndSpeculation(E);
2852 return true;
2853}
2854
2855template <class Emitter>
2856bool Compiler<Emitter>::VisitStringLiteral(const StringLiteral *E) {
2857 if (DiscardResult)
2858 return true;
2859
2860 if (!Initializing) {
2861 unsigned StringIndex = P.createGlobalString(S: E);
2862 return this->emitGetPtrGlobal(StringIndex, E);
2863 }
2864
2865 // We are initializing an array on the stack.
2866 const ConstantArrayType *CAT =
2867 Ctx.getASTContext().getAsConstantArrayType(T: E->getType());
2868 assert(CAT && "a string literal that's not a constant array?");
2869
2870 // If the initializer string is too long, a diagnostic has already been
2871 // emitted. Read only the array length from the string literal.
2872 unsigned ArraySize = CAT->getZExtSize();
2873 unsigned N = std::min(a: ArraySize, b: E->getLength());
2874 unsigned CharWidth = E->getCharByteWidth();
2875
2876 for (unsigned I = 0; I != N; ++I) {
2877 uint32_t CodeUnit = E->getCodeUnit(i: I);
2878
2879 if (CharWidth == 1) {
2880 this->emitConstSint8(CodeUnit, E);
2881 this->emitInitElemSint8(I, E);
2882 } else if (CharWidth == 2) {
2883 this->emitConstUint16(CodeUnit, E);
2884 this->emitInitElemUint16(I, E);
2885 } else if (CharWidth == 4) {
2886 this->emitConstUint32(CodeUnit, E);
2887 this->emitInitElemUint32(I, E);
2888 } else {
2889 llvm_unreachable("unsupported character width");
2890 }
2891 }
2892
2893 // Fill up the rest of the char array with NUL bytes.
2894 for (unsigned I = N; I != ArraySize; ++I) {
2895 if (CharWidth == 1) {
2896 this->emitConstSint8(0, E);
2897 this->emitInitElemSint8(I, E);
2898 } else if (CharWidth == 2) {
2899 this->emitConstUint16(0, E);
2900 this->emitInitElemUint16(I, E);
2901 } else if (CharWidth == 4) {
2902 this->emitConstUint32(0, E);
2903 this->emitInitElemUint32(I, E);
2904 } else {
2905 llvm_unreachable("unsupported character width");
2906 }
2907 }
2908
2909 return true;
2910}
2911
2912template <class Emitter>
2913bool Compiler<Emitter>::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
2914 if (DiscardResult)
2915 return true;
2916 return this->emitDummyPtr(E, E);
2917}
2918
2919template <class Emitter>
2920bool Compiler<Emitter>::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2921 auto &A = Ctx.getASTContext();
2922 std::string Str;
2923 A.getObjCEncodingForType(T: E->getEncodedType(), S&: Str);
2924 StringLiteral *SL =
2925 StringLiteral::Create(Ctx: A, Str, Kind: StringLiteralKind::Ordinary,
2926 /*Pascal=*/false, Ty: E->getType(), Locs: E->getAtLoc());
2927 return this->delegate(SL);
2928}
2929
2930template <class Emitter>
2931bool Compiler<Emitter>::VisitSYCLUniqueStableNameExpr(
2932 const SYCLUniqueStableNameExpr *E) {
2933 if (DiscardResult)
2934 return true;
2935
2936 assert(!Initializing);
2937
2938 auto &A = Ctx.getASTContext();
2939 std::string ResultStr = E->ComputeName(Context&: A);
2940
2941 QualType CharTy = A.CharTy.withConst();
2942 APInt Size(A.getTypeSize(T: A.getSizeType()), ResultStr.size() + 1);
2943 QualType ArrayTy = A.getConstantArrayType(EltTy: CharTy, ArySize: Size, SizeExpr: nullptr,
2944 ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
2945
2946 StringLiteral *SL =
2947 StringLiteral::Create(Ctx: A, Str: ResultStr, Kind: StringLiteralKind::Ordinary,
2948 /*Pascal=*/false, Ty: ArrayTy, Locs: E->getLocation());
2949
2950 unsigned StringIndex = P.createGlobalString(S: SL);
2951 return this->emitGetPtrGlobal(StringIndex, E);
2952}
2953
2954template <class Emitter>
2955bool Compiler<Emitter>::VisitCharacterLiteral(const CharacterLiteral *E) {
2956 if (DiscardResult)
2957 return true;
2958 return this->emitConst(E->getValue(), E);
2959}
2960
2961template <class Emitter>
2962bool Compiler<Emitter>::VisitFloatCompoundAssignOperator(
2963 const CompoundAssignOperator *E) {
2964
2965 const Expr *LHS = E->getLHS();
2966 const Expr *RHS = E->getRHS();
2967 QualType LHSType = LHS->getType();
2968 QualType LHSComputationType = E->getComputationLHSType();
2969 QualType ResultType = E->getComputationResultType();
2970 OptPrimType LT = classify(LHSComputationType);
2971 OptPrimType RT = classify(ResultType);
2972
2973 assert(ResultType->isFloatingType());
2974
2975 if (!LT || !RT)
2976 return false;
2977
2978 PrimType LHST = classifyPrim(LHSType);
2979
2980 // C++17 onwards require that we evaluate the RHS first.
2981 // Compute RHS and save it in a temporary variable so we can
2982 // load it again later.
2983 if (!visit(E: RHS))
2984 return false;
2985
2986 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2987 if (!this->emitSetLocal(*RT, TempOffset, E))
2988 return false;
2989
2990 // First, visit LHS.
2991 if (!visit(E: LHS))
2992 return false;
2993 if (!this->emitLoad(LHST, E))
2994 return false;
2995
2996 // If necessary, convert LHS to its computation type.
2997 if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
2998 LHSComputationType, E))
2999 return false;
3000
3001 // Now load RHS.
3002 if (!this->emitGetLocal(*RT, TempOffset, E))
3003 return false;
3004
3005 switch (E->getOpcode()) {
3006 case BO_AddAssign:
3007 if (!this->emitAddf(getFPOptions(E), E))
3008 return false;
3009 break;
3010 case BO_SubAssign:
3011 if (!this->emitSubf(getFPOptions(E), E))
3012 return false;
3013 break;
3014 case BO_MulAssign:
3015 if (!this->emitMulf(getFPOptions(E), E))
3016 return false;
3017 break;
3018 case BO_DivAssign:
3019 if (!this->emitDivf(getFPOptions(E), E))
3020 return false;
3021 break;
3022 default:
3023 return false;
3024 }
3025
3026 if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
3027 return false;
3028
3029 if (DiscardResult)
3030 return this->emitStorePop(LHST, E);
3031 return this->emitStore(LHST, E);
3032}
3033
3034template <class Emitter>
3035bool Compiler<Emitter>::VisitPointerCompoundAssignOperator(
3036 const CompoundAssignOperator *E) {
3037 BinaryOperatorKind Op = E->getOpcode();
3038 const Expr *LHS = E->getLHS();
3039 const Expr *RHS = E->getRHS();
3040 OptPrimType LT = classify(LHS->getType());
3041 OptPrimType RT = classify(RHS->getType());
3042
3043 if (Op != BO_AddAssign && Op != BO_SubAssign)
3044 return false;
3045
3046 if (!LT || !RT)
3047 return false;
3048
3049 if (!visit(E: LHS))
3050 return false;
3051
3052 if (!this->emitLoad(*LT, LHS))
3053 return false;
3054
3055 if (!visit(E: RHS))
3056 return false;
3057
3058 if (Op == BO_AddAssign) {
3059 if (!this->emitAddOffset(*RT, E))
3060 return false;
3061 } else {
3062 if (!this->emitSubOffset(*RT, E))
3063 return false;
3064 }
3065
3066 if (DiscardResult)
3067 return this->emitStorePopPtr(E);
3068 return this->emitStorePtr(E);
3069}
3070
3071template <class Emitter>
3072bool Compiler<Emitter>::VisitCompoundAssignOperator(
3073 const CompoundAssignOperator *E) {
3074 if (E->getType()->isVectorType())
3075 return VisitVectorBinOp(E);
3076
3077 const Expr *LHS = E->getLHS();
3078 const Expr *RHS = E->getRHS();
3079 OptPrimType LHSComputationT = classify(E->getComputationLHSType());
3080 OptPrimType LT = classify(LHS->getType());
3081 OptPrimType RT = classify(RHS->getType());
3082 OptPrimType ResultT = classify(E->getType());
3083
3084 if (!Ctx.getLangOpts().CPlusPlus14)
3085 return this->visit(RHS) && this->visit(LHS) && this->emitError(E);
3086
3087 if (!LT || !RT || !ResultT || !LHSComputationT)
3088 return false;
3089
3090 // Handle floating point operations separately here, since they
3091 // require special care.
3092
3093 if (ResultT == PT_Float || RT == PT_Float)
3094 return VisitFloatCompoundAssignOperator(E);
3095
3096 if (E->getType()->isPointerType())
3097 return VisitPointerCompoundAssignOperator(E);
3098
3099 assert(!E->getType()->isPointerType() && "Handled above");
3100 assert(!E->getType()->isFloatingType() && "Handled above");
3101
3102 // C++17 onwards require that we evaluate the RHS first.
3103 // Compute RHS and save it in a temporary variable so we can
3104 // load it again later.
3105 // FIXME: Compound assignments are unsequenced in C, so we might
3106 // have to figure out how to reject them.
3107 if (!visit(E: RHS))
3108 return false;
3109
3110 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
3111
3112 if (!this->emitSetLocal(*RT, TempOffset, E))
3113 return false;
3114
3115 // Get LHS pointer, load its value and cast it to the
3116 // computation type if necessary.
3117 if (!visit(E: LHS))
3118 return false;
3119 if (!this->emitLoad(*LT, E))
3120 return false;
3121 if (LT != LHSComputationT &&
3122 !this->emitIntegralCast(*LT, *LHSComputationT, E->getComputationLHSType(),
3123 E))
3124 return false;
3125
3126 // Get the RHS value on the stack.
3127 if (!this->emitGetLocal(*RT, TempOffset, E))
3128 return false;
3129
3130 // Perform operation.
3131 switch (E->getOpcode()) {
3132 case BO_AddAssign:
3133 if (!this->emitAdd(*LHSComputationT, E))
3134 return false;
3135 break;
3136 case BO_SubAssign:
3137 if (!this->emitSub(*LHSComputationT, E))
3138 return false;
3139 break;
3140 case BO_MulAssign:
3141 if (!this->emitMul(*LHSComputationT, E))
3142 return false;
3143 break;
3144 case BO_DivAssign:
3145 if (!this->emitDiv(*LHSComputationT, E))
3146 return false;
3147 break;
3148 case BO_RemAssign:
3149 if (!this->emitRem(*LHSComputationT, E))
3150 return false;
3151 break;
3152 case BO_ShlAssign:
3153 if (!this->emitShl(*LHSComputationT, *RT, E))
3154 return false;
3155 break;
3156 case BO_ShrAssign:
3157 if (!this->emitShr(*LHSComputationT, *RT, E))
3158 return false;
3159 break;
3160 case BO_AndAssign:
3161 if (!this->emitBitAnd(*LHSComputationT, E))
3162 return false;
3163 break;
3164 case BO_XorAssign:
3165 if (!this->emitBitXor(*LHSComputationT, E))
3166 return false;
3167 break;
3168 case BO_OrAssign:
3169 if (!this->emitBitOr(*LHSComputationT, E))
3170 return false;
3171 break;
3172 default:
3173 llvm_unreachable("Unimplemented compound assign operator");
3174 }
3175
3176 // And now cast from LHSComputationT to ResultT.
3177 if (ResultT != LHSComputationT &&
3178 !this->emitIntegralCast(*LHSComputationT, *ResultT, E->getType(), E))
3179 return false;
3180
3181 // And store the result in LHS.
3182 if (DiscardResult) {
3183 if (LHS->refersToBitField())
3184 return this->emitStoreBitFieldPop(*ResultT, E);
3185 return this->emitStorePop(*ResultT, E);
3186 }
3187 if (LHS->refersToBitField())
3188 return this->emitStoreBitField(*ResultT, E);
3189 return this->emitStore(*ResultT, E);
3190}
3191
3192template <class Emitter>
3193bool Compiler<Emitter>::VisitExprWithCleanups(const ExprWithCleanups *E) {
3194 LocalScope<Emitter> ES(this, ScopeKind::FullExpression);
3195 const Expr *SubExpr = E->getSubExpr();
3196
3197 return this->delegate(SubExpr) && ES.destroyLocals(E);
3198}
3199
3200template <class Emitter>
3201bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
3202 const MaterializeTemporaryExpr *E) {
3203 const Expr *SubExpr = E->getSubExpr();
3204
3205 if (Initializing) {
3206 // We already have a value, just initialize that.
3207 return this->delegate(SubExpr);
3208 }
3209 // If we don't end up using the materialized temporary anyway, don't
3210 // bother creating it.
3211 if (DiscardResult)
3212 return this->discard(SubExpr);
3213
3214 // When we're initializing a global variable *or* the storage duration of
3215 // the temporary is explicitly static, create a global variable.
3216 OptPrimType SubExprT = classify(SubExpr);
3217 if (E->getStorageDuration() == SD_Static) {
3218 UnsignedOrNone GlobalIndex = P.createGlobal(E);
3219 if (!GlobalIndex)
3220 return false;
3221
3222 const LifetimeExtendedTemporaryDecl *TempDecl =
3223 E->getLifetimeExtendedTemporaryDecl();
3224 assert(TempDecl);
3225
3226 if (SubExprT) {
3227 if (!this->visit(SubExpr))
3228 return false;
3229 if (!this->emitInitGlobalTemp(*SubExprT, *GlobalIndex, TempDecl, E))
3230 return false;
3231 return this->emitGetPtrGlobal(*GlobalIndex, E);
3232 }
3233
3234 if (!this->checkLiteralType(SubExpr))
3235 return false;
3236 // Non-primitive values.
3237 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3238 return false;
3239 if (!this->visitInitializer(SubExpr))
3240 return false;
3241 return this->emitInitGlobalTempComp(TempDecl, E);
3242 }
3243
3244 ScopeKind VarScope = E->getStorageDuration() == SD_FullExpression
3245 ? ScopeKind::FullExpression
3246 : ScopeKind::Block;
3247
3248 // For everyhing else, use local variables.
3249 if (SubExprT) {
3250 bool IsConst = SubExpr->getType().isConstQualified();
3251 bool IsVolatile = SubExpr->getType().isVolatileQualified();
3252 unsigned LocalIndex =
3253 allocateLocalPrimitive(Decl: E, Ty: *SubExprT, IsConst, IsVolatile, SC: VarScope);
3254 if (!this->VarScope->LocalsAlwaysEnabled &&
3255 !this->emitEnableLocal(LocalIndex, E))
3256 return false;
3257
3258 if (!this->visit(SubExpr))
3259 return false;
3260 if (!this->emitSetLocal(*SubExprT, LocalIndex, E))
3261 return false;
3262
3263 return this->emitGetPtrLocal(LocalIndex, E);
3264 }
3265
3266 if (!this->checkLiteralType(SubExpr))
3267 return false;
3268
3269 const Expr *Inner = E->getSubExpr()->skipRValueSubobjectAdjustments();
3270 if (UnsignedOrNone LocalIndex =
3271 allocateLocal(Decl: E, Ty: Inner->getType(), VarScope)) {
3272 InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex));
3273
3274 if (!this->VarScope->LocalsAlwaysEnabled &&
3275 !this->emitEnableLocal(*LocalIndex, E))
3276 return false;
3277
3278 if (!this->emitGetPtrLocal(*LocalIndex, E))
3279 return false;
3280 return this->visitInitializer(SubExpr) && this->emitFinishInit(E);
3281 }
3282 return false;
3283}
3284
3285template <class Emitter>
3286bool Compiler<Emitter>::VisitCXXBindTemporaryExpr(
3287 const CXXBindTemporaryExpr *E) {
3288 const Expr *SubExpr = E->getSubExpr();
3289
3290 if (Initializing)
3291 return this->delegate(SubExpr);
3292
3293 // Make sure we create a temporary even if we're discarding, since that will
3294 // make sure we will also call the destructor.
3295
3296 if (!this->visit(SubExpr))
3297 return false;
3298
3299 if (DiscardResult)
3300 return this->emitPopPtr(E);
3301 return true;
3302}
3303
3304template <class Emitter>
3305bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
3306 const Expr *Init = E->getInitializer();
3307 if (DiscardResult)
3308 return this->discard(Init);
3309
3310 if (Initializing) {
3311 // We already have a value, just initialize that.
3312 return this->visitInitializer(Init) && this->emitFinishInit(E);
3313 }
3314
3315 OptPrimType T = classify(E->getType());
3316 if (E->isFileScope()) {
3317 // Avoid creating a variable if this is a primitive RValue anyway.
3318 if (T && !E->isLValue())
3319 return this->delegate(Init);
3320
3321 UnsignedOrNone GlobalIndex = P.createGlobal(E);
3322 if (!GlobalIndex)
3323 return false;
3324
3325 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3326 return false;
3327
3328 // Since this is a global variable, we might've already seen,
3329 // don't do it again.
3330 if (P.isGlobalInitialized(Index: *GlobalIndex))
3331 return true;
3332
3333 if (T) {
3334 if (!this->visit(Init))
3335 return false;
3336 return this->emitInitGlobal(*T, *GlobalIndex, E);
3337 }
3338
3339 return this->visitInitializer(Init) && this->emitFinishInit(E);
3340 }
3341
3342 // Otherwise, use a local variable.
3343 if (T && !E->isLValue()) {
3344 // For primitive types, we just visit the initializer.
3345 return this->delegate(Init);
3346 }
3347
3348 unsigned LocalIndex;
3349 if (T)
3350 LocalIndex = this->allocateLocalPrimitive(Init, *T, /*IsConst=*/false);
3351 else if (UnsignedOrNone MaybeIndex = this->allocateLocal(Init))
3352 LocalIndex = *MaybeIndex;
3353 else
3354 return false;
3355
3356 if (!this->emitGetPtrLocal(LocalIndex, E))
3357 return false;
3358
3359 if (T)
3360 return this->visit(Init) && this->emitInit(*T, E);
3361 return this->visitInitializer(Init) && this->emitFinishInit(E);
3362}
3363
3364template <class Emitter>
3365bool Compiler<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) {
3366 if (DiscardResult)
3367 return true;
3368 if (E->isStoredAsBoolean()) {
3369 if (E->getType()->isBooleanType())
3370 return this->emitConstBool(E->getBoolValue(), E);
3371 return this->emitConst(E->getBoolValue(), E);
3372 }
3373 PrimType T = classifyPrim(E->getType());
3374 return this->visitAPValue(E->getAPValue(), T, E);
3375}
3376
3377template <class Emitter>
3378bool Compiler<Emitter>::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
3379 if (DiscardResult)
3380 return true;
3381 return this->emitConst(E->getValue(), E);
3382}
3383
3384template <class Emitter>
3385bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
3386 if (DiscardResult)
3387 return true;
3388
3389 assert(Initializing);
3390 const Record *R = P.getOrCreateRecord(RD: E->getLambdaClass());
3391 if (!R)
3392 return false;
3393
3394 auto *CaptureInitIt = E->capture_init_begin();
3395 // Initialize all fields (which represent lambda captures) of the
3396 // record with their initializers.
3397 for (const Record::Field &F : R->fields()) {
3398 const Expr *Init = *CaptureInitIt;
3399 if (!Init || Init->containsErrors())
3400 continue;
3401 ++CaptureInitIt;
3402
3403 if (OptPrimType T = classify(Init)) {
3404 if (!this->visit(Init))
3405 return false;
3406
3407 if (!this->emitInitField(*T, F.Offset, E))
3408 return false;
3409 } else {
3410 if (!this->emitGetPtrField(F.Offset, E))
3411 return false;
3412
3413 if (!this->visitInitializer(Init))
3414 return false;
3415
3416 if (!this->emitPopPtr(E))
3417 return false;
3418 }
3419 }
3420
3421 return true;
3422}
3423
3424template <class Emitter>
3425bool Compiler<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) {
3426 if (DiscardResult)
3427 return true;
3428
3429 if (!Initializing) {
3430 unsigned StringIndex = P.createGlobalString(S: E->getFunctionName(), Base: E);
3431 return this->emitGetPtrGlobal(StringIndex, E);
3432 }
3433
3434 return this->delegate(E->getFunctionName());
3435}
3436
3437template <class Emitter>
3438bool Compiler<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) {
3439 if (E->getSubExpr() && !this->discard(E->getSubExpr()))
3440 return false;
3441
3442 return this->emitInvalid(E);
3443}
3444
3445template <class Emitter>
3446bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
3447 const CXXReinterpretCastExpr *E) {
3448 const Expr *SubExpr = E->getSubExpr();
3449
3450 OptPrimType FromT = classify(SubExpr);
3451 OptPrimType ToT = classify(E);
3452
3453 if (!FromT || !ToT)
3454 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
3455
3456 if (FromT == PT_Ptr || ToT == PT_Ptr) {
3457 // Both types could be PT_Ptr because their expressions are glvalues.
3458 OptPrimType PointeeFromT;
3459 if (SubExpr->getType()->isPointerOrReferenceType())
3460 PointeeFromT = classify(SubExpr->getType()->getPointeeType());
3461 else
3462 PointeeFromT = classify(SubExpr->getType());
3463
3464 OptPrimType PointeeToT;
3465 if (E->getType()->isPointerOrReferenceType())
3466 PointeeToT = classify(E->getType()->getPointeeType());
3467 else
3468 PointeeToT = classify(E->getType());
3469
3470 bool Fatal = true;
3471 if (PointeeToT && PointeeFromT) {
3472 if (isIntegerOrBoolType(T: *PointeeFromT) &&
3473 isIntegerOrBoolType(T: *PointeeToT))
3474 Fatal = false;
3475 else if (E->getCastKind() == CK_LValueBitCast)
3476 Fatal = false;
3477 } else {
3478 Fatal = SubExpr->getType().getTypePtr() != E->getType().getTypePtr();
3479 }
3480
3481 if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
3482 return false;
3483
3484 if (E->getCastKind() == CK_LValueBitCast)
3485 return this->delegate(SubExpr);
3486 return this->VisitCastExpr(E);
3487 }
3488
3489 // Try to actually do the cast.
3490 bool Fatal = (ToT != FromT);
3491 if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
3492 return false;
3493
3494 return this->VisitCastExpr(E);
3495}
3496
3497template <class Emitter>
3498bool Compiler<Emitter>::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
3499
3500 if (!Ctx.getLangOpts().CPlusPlus20) {
3501 if (!this->emitInvalidCast(CastKind::Dynamic, /*Fatal=*/false, E))
3502 return false;
3503 }
3504
3505 return this->VisitCastExpr(E);
3506}
3507
3508template <class Emitter>
3509bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
3510 assert(E->getType()->isBooleanType());
3511
3512 if (DiscardResult)
3513 return true;
3514 return this->emitConstBool(E->getValue(), E);
3515}
3516
3517template <class Emitter>
3518bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3519 QualType T = E->getType();
3520 assert(!canClassify(T));
3521
3522 if (T->isRecordType()) {
3523 const CXXConstructorDecl *Ctor = E->getConstructor();
3524
3525 // If we're discarding a construct expression, we still need
3526 // to allocate a variable and call the constructor and destructor.
3527 if (DiscardResult) {
3528 if (Ctor->isTrivial())
3529 return true;
3530 assert(!Initializing);
3531 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
3532
3533 if (!LocalIndex)
3534 return false;
3535
3536 if (!this->emitGetPtrLocal(*LocalIndex, E))
3537 return false;
3538 }
3539
3540 // Trivial copy/move constructor. Avoid copy.
3541 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
3542 Ctor->isTrivial() &&
3543 E->getArg(Arg: 0)->isTemporaryObject(Ctx&: Ctx.getASTContext(),
3544 TempTy: T->getAsCXXRecordDecl()))
3545 return this->visitInitializer(E->getArg(Arg: 0));
3546
3547 // Zero initialization.
3548 bool ZeroInit = E->requiresZeroInitialization();
3549 if (ZeroInit) {
3550 const Record *R = getRecord(E->getType());
3551 if (!R)
3552 return false;
3553
3554 if (!this->visitZeroRecordInitializer(R, E))
3555 return false;
3556
3557 // If the constructor is trivial anyway, we're done.
3558 if (Ctor->isTrivial())
3559 return true;
3560 }
3561
3562 // Avoid materializing a temporary for an elidable copy/move constructor.
3563 if (!ZeroInit && E->isElidable()) {
3564 const Expr *SrcObj = E->getArg(Arg: 0);
3565 assert(SrcObj->isTemporaryObject(Ctx.getASTContext(), Ctor->getParent()));
3566 assert(Ctx.getASTContext().hasSameUnqualifiedType(E->getType(),
3567 SrcObj->getType()));
3568 if (const auto *ME = dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj)) {
3569 if (!this->emitCheckFunctionDecl(Ctor, E))
3570 return false;
3571 return this->visitInitializer(ME->getSubExpr());
3572 }
3573 }
3574
3575 const Function *Func = getFunction(FD: Ctor);
3576
3577 if (!Func)
3578 return false;
3579
3580 assert(Func->hasThisPointer());
3581 assert(!Func->hasRVO());
3582
3583 // The This pointer is already on the stack because this is an initializer,
3584 // but we need to dup() so the call() below has its own copy.
3585 if (!this->emitDupPtr(E))
3586 return false;
3587
3588 // Constructor arguments.
3589 for (const auto *Arg : E->arguments()) {
3590 if (!this->visit(Arg))
3591 return false;
3592 }
3593
3594 if (Func->isVariadic()) {
3595 uint32_t VarArgSize = 0;
3596 unsigned NumParams = Func->getNumWrittenParams();
3597 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) {
3598 VarArgSize +=
3599 align(primSize(classify(E->getArg(Arg: I)->getType()).value_or(PT_Ptr)));
3600 }
3601 if (!this->emitCallVar(Func, VarArgSize, E))
3602 return false;
3603 } else {
3604 if (!this->emitCall(Func, 0, E)) {
3605 // When discarding, we don't need the result anyway, so clean up
3606 // the instance dup we did earlier in case surrounding code wants
3607 // to keep evaluating.
3608 if (DiscardResult)
3609 (void)this->emitPopPtr(E);
3610 return false;
3611 }
3612 }
3613
3614 if (DiscardResult)
3615 return this->emitPopPtr(E);
3616 return this->emitFinishInit(E);
3617 }
3618
3619 if (T->isArrayType()) {
3620 const Function *Func = getFunction(FD: E->getConstructor());
3621 if (!Func)
3622 return false;
3623
3624 if (!this->emitDupPtr(E))
3625 return false;
3626
3627 std::function<bool(QualType)> initArrayDimension;
3628 initArrayDimension = [&](QualType T) -> bool {
3629 if (!T->isArrayType()) {
3630 // Constructor arguments.
3631 for (const auto *Arg : E->arguments()) {
3632 if (!this->visit(Arg))
3633 return false;
3634 }
3635
3636 return this->emitCall(Func, 0, E);
3637 }
3638
3639 const ConstantArrayType *CAT =
3640 Ctx.getASTContext().getAsConstantArrayType(T);
3641 if (!CAT)
3642 return false;
3643 QualType ElemTy = CAT->getElementType();
3644 unsigned NumElems = CAT->getZExtSize();
3645 for (size_t I = 0; I != NumElems; ++I) {
3646 if (!this->emitConstUint64(I, E))
3647 return false;
3648 if (!this->emitArrayElemPtrUint64(E))
3649 return false;
3650 if (!initArrayDimension(ElemTy))
3651 return false;
3652 }
3653 return this->emitPopPtr(E);
3654 };
3655
3656 return initArrayDimension(E->getType());
3657 }
3658
3659 return false;
3660}
3661
3662template <class Emitter>
3663bool Compiler<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) {
3664 if (DiscardResult)
3665 return true;
3666
3667 const APValue Val =
3668 E->EvaluateInContext(Ctx: Ctx.getASTContext(), DefaultExpr: SourceLocDefaultExpr);
3669
3670 // Things like __builtin_LINE().
3671 if (E->getType()->isIntegerType()) {
3672 assert(Val.isInt());
3673 const APSInt &I = Val.getInt();
3674 return this->emitConst(I, E);
3675 }
3676 // Otherwise, the APValue is an LValue, with only one element.
3677 // Theoretically, we don't need the APValue at all of course.
3678 assert(E->getType()->isPointerType());
3679 assert(Val.isLValue());
3680 const APValue::LValueBase &Base = Val.getLValueBase();
3681 if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
3682 return this->visit(LValueExpr);
3683
3684 // Otherwise, we have a decl (which is the case for
3685 // __builtin_source_location).
3686 assert(Base.is<const ValueDecl *>());
3687 assert(Val.getLValuePath().size() == 0);
3688 const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
3689 assert(BaseDecl);
3690
3691 auto *UGCD = cast<UnnamedGlobalConstantDecl>(Val: BaseDecl);
3692
3693 UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(VD: UGCD);
3694 if (!GlobalIndex)
3695 return false;
3696
3697 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3698 return false;
3699
3700 const Record *R = getRecord(E->getType());
3701 const APValue &V = UGCD->getValue();
3702 for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
3703 const Record::Field *F = R->getField(I);
3704 const APValue &FieldValue = V.getStructField(i: I);
3705
3706 PrimType FieldT = classifyPrim(F->Decl->getType());
3707
3708 if (!this->visitAPValue(FieldValue, FieldT, E))
3709 return false;
3710 if (!this->emitInitField(FieldT, F->Offset, E))
3711 return false;
3712 }
3713
3714 // Leave the pointer to the global on the stack.
3715 return true;
3716}
3717
3718template <class Emitter>
3719bool Compiler<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
3720 unsigned N = E->getNumComponents();
3721 if (N == 0)
3722 return false;
3723
3724 for (unsigned I = 0; I != N; ++I) {
3725 const OffsetOfNode &Node = E->getComponent(Idx: I);
3726 if (Node.getKind() == OffsetOfNode::Array) {
3727 const Expr *ArrayIndexExpr = E->getIndexExpr(Idx: Node.getArrayExprIndex());
3728 PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
3729
3730 if (DiscardResult) {
3731 if (!this->discard(ArrayIndexExpr))
3732 return false;
3733 continue;
3734 }
3735
3736 if (!this->visit(ArrayIndexExpr))
3737 return false;
3738 // Cast to Sint64.
3739 if (IndexT != PT_Sint64) {
3740 if (!this->emitCast(IndexT, PT_Sint64, E))
3741 return false;
3742 }
3743 }
3744 }
3745
3746 if (DiscardResult)
3747 return true;
3748
3749 PrimType T = classifyPrim(E->getType());
3750 return this->emitOffsetOf(T, E, E);
3751}
3752
3753template <class Emitter>
3754bool Compiler<Emitter>::VisitCXXScalarValueInitExpr(
3755 const CXXScalarValueInitExpr *E) {
3756 QualType Ty = E->getType();
3757
3758 if (DiscardResult || Ty->isVoidType())
3759 return true;
3760
3761 if (OptPrimType T = classify(Ty))
3762 return this->visitZeroInitializer(*T, Ty, E);
3763
3764 if (const auto *CT = Ty->getAs<ComplexType>()) {
3765 if (!Initializing) {
3766 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
3767 if (!LocalIndex)
3768 return false;
3769 if (!this->emitGetPtrLocal(*LocalIndex, E))
3770 return false;
3771 }
3772
3773 // Initialize both fields to 0.
3774 QualType ElemQT = CT->getElementType();
3775 PrimType ElemT = classifyPrim(ElemQT);
3776
3777 for (unsigned I = 0; I != 2; ++I) {
3778 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3779 return false;
3780 if (!this->emitInitElem(ElemT, I, E))
3781 return false;
3782 }
3783 return true;
3784 }
3785
3786 if (const auto *VT = Ty->getAs<VectorType>()) {
3787 // FIXME: Code duplication with the _Complex case above.
3788 if (!Initializing) {
3789 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
3790 if (!LocalIndex)
3791 return false;
3792 if (!this->emitGetPtrLocal(*LocalIndex, E))
3793 return false;
3794 }
3795
3796 // Initialize all fields to 0.
3797 QualType ElemQT = VT->getElementType();
3798 PrimType ElemT = classifyPrim(ElemQT);
3799
3800 for (unsigned I = 0, N = VT->getNumElements(); I != N; ++I) {
3801 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3802 return false;
3803 if (!this->emitInitElem(ElemT, I, E))
3804 return false;
3805 }
3806 return true;
3807 }
3808
3809 return false;
3810}
3811
3812template <class Emitter>
3813bool Compiler<Emitter>::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
3814 return this->emitConst(E->getPackLength(), E);
3815}
3816
3817template <class Emitter>
3818bool Compiler<Emitter>::VisitGenericSelectionExpr(
3819 const GenericSelectionExpr *E) {
3820 return this->delegate(E->getResultExpr());
3821}
3822
3823template <class Emitter>
3824bool Compiler<Emitter>::VisitChooseExpr(const ChooseExpr *E) {
3825 return this->delegate(E->getChosenSubExpr());
3826}
3827
3828template <class Emitter>
3829bool Compiler<Emitter>::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
3830 if (DiscardResult)
3831 return true;
3832
3833 return this->emitConst(E->getValue(), E);
3834}
3835
3836template <class Emitter>
3837bool Compiler<Emitter>::VisitCXXInheritedCtorInitExpr(
3838 const CXXInheritedCtorInitExpr *E) {
3839 const CXXConstructorDecl *Ctor = E->getConstructor();
3840 assert(!Ctor->isTrivial() &&
3841 "Trivial CXXInheritedCtorInitExpr, implement. (possible?)");
3842 const Function *F = this->getFunction(Ctor);
3843 assert(F);
3844 assert(!F->hasRVO());
3845 assert(F->hasThisPointer());
3846
3847 if (!this->emitDupPtr(SourceInfo{}))
3848 return false;
3849
3850 // Forward all arguments of the current function (which should be a
3851 // constructor itself) to the inherited ctor.
3852 // This is necessary because the calling code has pushed the pointer
3853 // of the correct base for us already, but the arguments need
3854 // to come after.
3855 unsigned Offset = align(Size: primSize(Type: PT_Ptr)); // instance pointer.
3856 for (const ParmVarDecl *PD : Ctor->parameters()) {
3857 PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr);
3858
3859 if (!this->emitGetParam(PT, Offset, E))
3860 return false;
3861 Offset += align(Size: primSize(Type: PT));
3862 }
3863
3864 return this->emitCall(F, 0, E);
3865}
3866
3867// FIXME: This function has become rather unwieldy, especially
3868// the part where we initialize an array allocation of dynamic size.
3869template <class Emitter>
3870bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
3871 assert(classifyPrim(E->getType()) == PT_Ptr);
3872 const Expr *Init = E->getInitializer();
3873 QualType ElementType = E->getAllocatedType();
3874 OptPrimType ElemT = classify(ElementType);
3875 unsigned PlacementArgs = E->getNumPlacementArgs();
3876 const FunctionDecl *OperatorNew = E->getOperatorNew();
3877 const Expr *PlacementDest = nullptr;
3878 bool IsNoThrow = false;
3879
3880 if (E->containsErrors())
3881 return false;
3882
3883 if (PlacementArgs != 0) {
3884 // FIXME: There is no restriction on this, but it's not clear that any
3885 // other form makes any sense. We get here for cases such as:
3886 //
3887 // new (std::align_val_t{N}) X(int)
3888 //
3889 // (which should presumably be valid only if N is a multiple of
3890 // alignof(int), and in any case can't be deallocated unless N is
3891 // alignof(X) and X has new-extended alignment).
3892 if (PlacementArgs == 1) {
3893 const Expr *Arg1 = E->getPlacementArg(I: 0);
3894 if (Arg1->getType()->isNothrowT()) {
3895 if (!this->discard(Arg1))
3896 return false;
3897 IsNoThrow = true;
3898 } else {
3899 // Invalid unless we have C++26 or are in a std:: function.
3900 if (!this->emitInvalidNewDeleteExpr(E, E))
3901 return false;
3902
3903 // If we have a placement-new destination, we'll later use that instead
3904 // of allocating.
3905 if (OperatorNew->isReservedGlobalPlacementOperator())
3906 PlacementDest = Arg1;
3907 }
3908 } else {
3909 // Always invalid.
3910 return this->emitInvalid(E);
3911 }
3912 } else if (!OperatorNew
3913 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
3914 return this->emitInvalidNewDeleteExpr(E, E);
3915
3916 const Descriptor *Desc;
3917 if (!PlacementDest) {
3918 if (ElemT) {
3919 if (E->isArray())
3920 Desc = nullptr; // We're not going to use it in this case.
3921 else
3922 Desc = P.createDescriptor(D: E, T: *ElemT, /*SourceTy=*/nullptr,
3923 MDSize: Descriptor::InlineDescMD);
3924 } else {
3925 Desc = P.createDescriptor(
3926 D: E, Ty: ElementType.getTypePtr(),
3927 MDSize: E->isArray() ? std::nullopt : Descriptor::InlineDescMD,
3928 /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false,
3929 /*IsVolatile=*/false, Init);
3930 }
3931 }
3932
3933 if (E->isArray()) {
3934 std::optional<const Expr *> ArraySizeExpr = E->getArraySize();
3935 if (!ArraySizeExpr)
3936 return false;
3937
3938 const Expr *Stripped = *ArraySizeExpr;
3939 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped);
3940 Stripped = ICE->getSubExpr())
3941 if (ICE->getCastKind() != CK_NoOp &&
3942 ICE->getCastKind() != CK_IntegralCast)
3943 break;
3944
3945 PrimType SizeT = classifyPrim(Stripped->getType());
3946
3947 // Save evaluated array size to a variable.
3948 unsigned ArrayLen =
3949 allocateLocalPrimitive(Decl: Stripped, Ty: SizeT, /*IsConst=*/false);
3950 if (!this->visit(Stripped))
3951 return false;
3952 if (!this->emitSetLocal(SizeT, ArrayLen, E))
3953 return false;
3954
3955 if (PlacementDest) {
3956 if (!this->visit(PlacementDest))
3957 return false;
3958 if (!this->emitGetLocal(SizeT, ArrayLen, E))
3959 return false;
3960 if (!this->emitCheckNewTypeMismatchArray(SizeT, E, E))
3961 return false;
3962 } else {
3963 if (!this->emitGetLocal(SizeT, ArrayLen, E))
3964 return false;
3965
3966 if (ElemT) {
3967 // N primitive elements.
3968 if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E))
3969 return false;
3970 } else {
3971 // N Composite elements.
3972 if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E))
3973 return false;
3974 }
3975 }
3976
3977 if (Init) {
3978 QualType InitType = Init->getType();
3979 size_t StaticInitElems = 0;
3980 const Expr *DynamicInit = nullptr;
3981 OptPrimType ElemT;
3982
3983 if (const ConstantArrayType *CAT =
3984 Ctx.getASTContext().getAsConstantArrayType(T: InitType)) {
3985 StaticInitElems = CAT->getZExtSize();
3986 // Initialize the first S element from the initializer.
3987 if (!this->visitInitializer(Init))
3988 return false;
3989
3990 if (const auto *ILE = dyn_cast<InitListExpr>(Val: Init)) {
3991 if (ILE->hasArrayFiller())
3992 DynamicInit = ILE->getArrayFiller();
3993 else if (isa<StringLiteral>(Val: ILE->getInit(Init: 0)))
3994 ElemT = classifyPrim(CAT->getElementType());
3995 }
3996 }
3997
3998 // The initializer initializes a certain number of elements, S.
3999 // However, the complete number of elements, N, might be larger than that.
4000 // In this case, we need to get an initializer for the remaining elements.
4001 // There are three cases:
4002 // 1) For the form 'new Struct[n];', the initializer is a
4003 // CXXConstructExpr and its type is an IncompleteArrayType.
4004 // 2) For the form 'new Struct[n]{1,2,3}', the initializer is an
4005 // InitListExpr and the initializer for the remaining elements
4006 // is the array filler.
4007 // 3) StringLiterals don't have an array filler, so we need to zero
4008 // the remaining elements.
4009
4010 if (DynamicInit || ElemT || InitType->isIncompleteArrayType()) {
4011 const Function *CtorFunc = nullptr;
4012 if (const auto *CE = dyn_cast<CXXConstructExpr>(Val: Init)) {
4013 CtorFunc = getFunction(FD: CE->getConstructor());
4014 if (!CtorFunc)
4015 return false;
4016 } else if (!DynamicInit && !ElemT)
4017 DynamicInit = Init;
4018
4019 LabelTy EndLabel = this->getLabel();
4020 LabelTy StartLabel = this->getLabel();
4021
4022 // In the nothrow case, the alloc above might have returned nullptr.
4023 // Don't call any constructors that case.
4024 if (IsNoThrow) {
4025 if (!this->emitDupPtr(E))
4026 return false;
4027 if (!this->emitNullPtr(0, nullptr, E))
4028 return false;
4029 if (!this->emitEQPtr(E))
4030 return false;
4031 if (!this->jumpTrue(EndLabel))
4032 return false;
4033 }
4034
4035 // Create loop variables.
4036 unsigned Iter =
4037 allocateLocalPrimitive(Decl: Stripped, Ty: SizeT, /*IsConst=*/false);
4038 if (!this->emitConst(StaticInitElems, SizeT, E))
4039 return false;
4040 if (!this->emitSetLocal(SizeT, Iter, E))
4041 return false;
4042
4043 this->fallthrough(StartLabel);
4044 this->emitLabel(StartLabel);
4045 // Condition. Iter < ArrayLen?
4046 if (!this->emitGetLocal(SizeT, Iter, E))
4047 return false;
4048 if (!this->emitGetLocal(SizeT, ArrayLen, E))
4049 return false;
4050 if (!this->emitLT(SizeT, E))
4051 return false;
4052 if (!this->jumpFalse(EndLabel))
4053 return false;
4054
4055 // Pointer to the allocated array is already on the stack.
4056 if (!this->emitGetLocal(SizeT, Iter, E))
4057 return false;
4058 if (!this->emitArrayElemPtr(SizeT, E))
4059 return false;
4060
4061 if (isa_and_nonnull<ImplicitValueInitExpr>(Val: DynamicInit) &&
4062 DynamicInit->getType()->isArrayType()) {
4063 QualType ElemType =
4064 DynamicInit->getType()->getAsArrayTypeUnsafe()->getElementType();
4065 PrimType InitT = classifyPrim(ElemType);
4066 if (!this->visitZeroInitializer(InitT, ElemType, E))
4067 return false;
4068 if (!this->emitStorePop(InitT, E))
4069 return false;
4070 } else if (DynamicInit) {
4071 if (OptPrimType InitT = classify(DynamicInit)) {
4072 if (!this->visit(DynamicInit))
4073 return false;
4074 if (!this->emitStorePop(*InitT, E))
4075 return false;
4076 } else {
4077 if (!this->visitInitializer(DynamicInit))
4078 return false;
4079 if (!this->emitPopPtr(E))
4080 return false;
4081 }
4082 } else if (ElemT) {
4083 if (!this->visitZeroInitializer(
4084 *ElemT, InitType->getAsArrayTypeUnsafe()->getElementType(),
4085 Init))
4086 return false;
4087 if (!this->emitStorePop(*ElemT, E))
4088 return false;
4089 } else {
4090 assert(CtorFunc);
4091 if (!this->emitCall(CtorFunc, 0, E))
4092 return false;
4093 }
4094
4095 // ++Iter;
4096 if (!this->emitGetPtrLocal(Iter, E))
4097 return false;
4098 if (!this->emitIncPop(SizeT, false, E))
4099 return false;
4100
4101 if (!this->jump(StartLabel))
4102 return false;
4103
4104 this->fallthrough(EndLabel);
4105 this->emitLabel(EndLabel);
4106 }
4107 }
4108 } else { // Non-array.
4109 if (PlacementDest) {
4110 if (!this->visit(PlacementDest))
4111 return false;
4112 if (!this->emitCheckNewTypeMismatch(E, E))
4113 return false;
4114
4115 } else {
4116 // Allocate just one element.
4117 if (!this->emitAlloc(Desc, E))
4118 return false;
4119 }
4120
4121 if (Init) {
4122 if (ElemT) {
4123 if (!this->visit(Init))
4124 return false;
4125
4126 if (!this->emitInit(*ElemT, E))
4127 return false;
4128 } else {
4129 // Composite.
4130 if (!this->visitInitializer(Init))
4131 return false;
4132 }
4133 }
4134 }
4135
4136 if (DiscardResult)
4137 return this->emitPopPtr(E);
4138
4139 return true;
4140}
4141
4142template <class Emitter>
4143bool Compiler<Emitter>::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
4144 if (E->containsErrors())
4145 return false;
4146 const FunctionDecl *OperatorDelete = E->getOperatorDelete();
4147
4148 if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
4149 return this->emitInvalidNewDeleteExpr(E, E);
4150
4151 // Arg must be an lvalue.
4152 if (!this->visit(E->getArgument()))
4153 return false;
4154
4155 return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E);
4156}
4157
4158template <class Emitter>
4159bool Compiler<Emitter>::VisitBlockExpr(const BlockExpr *E) {
4160 if (DiscardResult)
4161 return true;
4162
4163 const Function *Func = nullptr;
4164 if (const Function *F = Ctx.getOrCreateObjCBlock(E))
4165 Func = F;
4166
4167 if (!Func)
4168 return false;
4169 return this->emitGetFnPtr(Func, E);
4170}
4171
4172template <class Emitter>
4173bool Compiler<Emitter>::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
4174 const Type *TypeInfoType = E->getType().getTypePtr();
4175
4176 auto canonType = [](const Type *T) {
4177 return T->getCanonicalTypeUnqualified().getTypePtr();
4178 };
4179
4180 if (!E->isPotentiallyEvaluated()) {
4181 if (DiscardResult)
4182 return true;
4183
4184 if (E->isTypeOperand())
4185 return this->emitGetTypeid(
4186 canonType(E->getTypeOperand(Context: Ctx.getASTContext()).getTypePtr()),
4187 TypeInfoType, E);
4188
4189 return this->emitGetTypeid(
4190 canonType(E->getExprOperand()->getType().getTypePtr()), TypeInfoType,
4191 E);
4192 }
4193
4194 // Otherwise, we need to evaluate the expression operand.
4195 assert(E->getExprOperand());
4196 assert(E->getExprOperand()->isLValue());
4197
4198 if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E))
4199 return false;
4200
4201 if (!this->visit(E->getExprOperand()))
4202 return false;
4203
4204 if (!this->emitGetTypeidPtr(TypeInfoType, E))
4205 return false;
4206 if (DiscardResult)
4207 return this->emitPopPtr(E);
4208 return true;
4209}
4210
4211template <class Emitter>
4212bool Compiler<Emitter>::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
4213 assert(Ctx.getLangOpts().CPlusPlus);
4214 return this->emitConstBool(E->getValue(), E);
4215}
4216
4217template <class Emitter>
4218bool Compiler<Emitter>::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
4219 if (DiscardResult)
4220 return true;
4221 assert(!Initializing);
4222
4223 const MSGuidDecl *GuidDecl = E->getGuidDecl();
4224 const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl();
4225 assert(RD);
4226 // If the definiton of the result type is incomplete, just return a dummy.
4227 // If (and when) that is read from, we will fail, but not now.
4228 if (!RD->isCompleteDefinition())
4229 return this->emitDummyPtr(GuidDecl, E);
4230
4231 UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(VD: GuidDecl);
4232 if (!GlobalIndex)
4233 return false;
4234 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
4235 return false;
4236
4237 assert(this->getRecord(E->getType()));
4238
4239 const APValue &V = GuidDecl->getAsAPValue();
4240 if (V.getKind() == APValue::None)
4241 return true;
4242
4243 assert(V.isStruct());
4244 assert(V.getStructNumBases() == 0);
4245 if (!this->visitAPValueInitializer(V, E, E->getType()))
4246 return false;
4247
4248 return this->emitFinishInit(E);
4249}
4250
4251template <class Emitter>
4252bool Compiler<Emitter>::VisitRequiresExpr(const RequiresExpr *E) {
4253 assert(classifyPrim(E->getType()) == PT_Bool);
4254 if (E->isValueDependent())
4255 return false;
4256 if (DiscardResult)
4257 return true;
4258 return this->emitConstBool(E->isSatisfied(), E);
4259}
4260
4261template <class Emitter>
4262bool Compiler<Emitter>::VisitConceptSpecializationExpr(
4263 const ConceptSpecializationExpr *E) {
4264 assert(classifyPrim(E->getType()) == PT_Bool);
4265 if (DiscardResult)
4266 return true;
4267 return this->emitConstBool(E->isSatisfied(), E);
4268}
4269
4270template <class Emitter>
4271bool Compiler<Emitter>::VisitCXXRewrittenBinaryOperator(
4272 const CXXRewrittenBinaryOperator *E) {
4273 return this->delegate(E->getSemanticForm());
4274}
4275
4276template <class Emitter>
4277bool Compiler<Emitter>::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
4278
4279 for (const Expr *SemE : E->semantics()) {
4280 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) {
4281 if (SemE == E->getResultExpr())
4282 return false;
4283
4284 if (OVE->isUnique())
4285 continue;
4286
4287 if (!this->discard(OVE))
4288 return false;
4289 } else if (SemE == E->getResultExpr()) {
4290 if (!this->delegate(SemE))
4291 return false;
4292 } else {
4293 if (!this->discard(SemE))
4294 return false;
4295 }
4296 }
4297 return true;
4298}
4299
4300template <class Emitter>
4301bool Compiler<Emitter>::VisitPackIndexingExpr(const PackIndexingExpr *E) {
4302 return this->delegate(E->getSelectedExpr());
4303}
4304
4305template <class Emitter>
4306bool Compiler<Emitter>::VisitRecoveryExpr(const RecoveryExpr *E) {
4307 return this->emitError(E);
4308}
4309
4310template <class Emitter>
4311bool Compiler<Emitter>::VisitAddrLabelExpr(const AddrLabelExpr *E) {
4312 assert(E->getType()->isVoidPointerType());
4313 if (DiscardResult)
4314 return true;
4315
4316 return this->emitDummyPtr(E, E);
4317}
4318
4319template <class Emitter>
4320bool Compiler<Emitter>::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
4321 assert(Initializing);
4322 const auto *VT = E->getType()->castAs<VectorType>();
4323 QualType ElemType = VT->getElementType();
4324 PrimType ElemT = classifyPrim(ElemType);
4325 const Expr *Src = E->getSrcExpr();
4326 QualType SrcType = Src->getType();
4327 PrimType SrcElemT = classifyVectorElementType(T: SrcType);
4328
4329 unsigned SrcOffset =
4330 this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
4331 if (!this->visit(Src))
4332 return false;
4333 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
4334 return false;
4335
4336 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
4337 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
4338 return false;
4339 if (!this->emitArrayElemPop(SrcElemT, I, E))
4340 return false;
4341
4342 // Cast to the desired result element type.
4343 if (SrcElemT != ElemT) {
4344 if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
4345 return false;
4346 } else if (ElemType->isFloatingType() && SrcType != ElemType) {
4347 const auto *TargetSemantics = &Ctx.getFloatSemantics(T: ElemType);
4348 if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E))
4349 return false;
4350 }
4351 if (!this->emitInitElem(ElemT, I, E))
4352 return false;
4353 }
4354
4355 return true;
4356}
4357
4358template <class Emitter>
4359bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
4360 // FIXME: Unary shuffle with mask not currently supported.
4361 if (E->getNumSubExprs() == 2)
4362 return this->emitInvalid(E);
4363
4364 assert(E->getNumSubExprs() > 2);
4365
4366 const Expr *Vecs[] = {E->getExpr(Index: 0), E->getExpr(Index: 1)};
4367 const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
4368 PrimType ElemT = classifyPrim(VT->getElementType());
4369 unsigned NumInputElems = VT->getNumElements();
4370 unsigned NumOutputElems = E->getNumSubExprs() - 2;
4371 assert(NumOutputElems > 0);
4372
4373 if (!Initializing) {
4374 UnsignedOrNone LocalIndex = allocateLocal(Decl: E);
4375 if (!LocalIndex)
4376 return false;
4377 if (!this->emitGetPtrLocal(*LocalIndex, E))
4378 return false;
4379 }
4380
4381 // Save both input vectors to a local variable.
4382 unsigned VectorOffsets[2];
4383 for (unsigned I = 0; I != 2; ++I) {
4384 VectorOffsets[I] =
4385 this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
4386 if (!this->visit(Vecs[I]))
4387 return false;
4388 if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
4389 return false;
4390 }
4391 for (unsigned I = 0; I != NumOutputElems; ++I) {
4392 APSInt ShuffleIndex = E->getShuffleMaskIdx(N: I);
4393 assert(ShuffleIndex >= -1);
4394 if (ShuffleIndex == -1)
4395 return this->emitInvalidShuffleVectorIndex(I, E);
4396
4397 assert(ShuffleIndex < (NumInputElems * 2));
4398 if (!this->emitGetLocal(PT_Ptr,
4399 VectorOffsets[ShuffleIndex >= NumInputElems], E))
4400 return false;
4401 unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
4402 if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
4403 return false;
4404
4405 if (!this->emitInitElem(ElemT, I, E))
4406 return false;
4407 }
4408
4409 if (DiscardResult)
4410 return this->emitPopPtr(E);
4411
4412 return true;
4413}
4414
4415template <class Emitter>
4416bool Compiler<Emitter>::VisitExtVectorElementExpr(
4417 const ExtVectorElementExpr *E) {
4418 const Expr *Base = E->getBase();
4419 assert(
4420 Base->getType()->isVectorType() ||
4421 Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
4422
4423 SmallVector<uint32_t, 4> Indices;
4424 E->getEncodedElementAccess(Elts&: Indices);
4425
4426 if (Indices.size() == 1) {
4427 if (!this->visit(Base))
4428 return false;
4429
4430 if (E->isGLValue()) {
4431 if (!this->emitConstUint32(Indices[0], E))
4432 return false;
4433 return this->emitArrayElemPtrPop(PT_Uint32, E);
4434 }
4435 // Else, also load the value.
4436 return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
4437 }
4438
4439 // Create a local variable for the base.
4440 unsigned BaseOffset = allocateLocalPrimitive(Decl: Base, Ty: PT_Ptr, /*IsConst=*/true);
4441 if (!this->visit(Base))
4442 return false;
4443 if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
4444 return false;
4445
4446 // Now the vector variable for the return value.
4447 if (!Initializing) {
4448 UnsignedOrNone ResultIndex = allocateLocal(Decl: E);
4449 if (!ResultIndex)
4450 return false;
4451 if (!this->emitGetPtrLocal(*ResultIndex, E))
4452 return false;
4453 }
4454
4455 assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
4456
4457 PrimType ElemT =
4458 classifyPrim(E->getType()->getAs<VectorType>()->getElementType());
4459 uint32_t DstIndex = 0;
4460 for (uint32_t I : Indices) {
4461 if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
4462 return false;
4463 if (!this->emitArrayElemPop(ElemT, I, E))
4464 return false;
4465 if (!this->emitInitElem(ElemT, DstIndex, E))
4466 return false;
4467 ++DstIndex;
4468 }
4469
4470 // Leave the result pointer on the stack.
4471 assert(!DiscardResult);
4472 return true;
4473}
4474
4475template <class Emitter>
4476bool Compiler<Emitter>::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
4477 const Expr *SubExpr = E->getSubExpr();
4478 if (!E->isExpressibleAsConstantInitializer())
4479 return this->discard(SubExpr) && this->emitInvalid(E);
4480
4481 if (DiscardResult)
4482 return true;
4483
4484 assert(classifyPrim(E) == PT_Ptr);
4485 return this->emitDummyPtr(E, E);
4486}
4487
4488template <class Emitter>
4489bool Compiler<Emitter>::VisitCXXStdInitializerListExpr(
4490 const CXXStdInitializerListExpr *E) {
4491 const Expr *SubExpr = E->getSubExpr();
4492 const ConstantArrayType *ArrayType =
4493 Ctx.getASTContext().getAsConstantArrayType(T: SubExpr->getType());
4494 const Record *R = getRecord(E->getType());
4495 assert(Initializing);
4496 assert(SubExpr->isGLValue());
4497
4498 if (!this->visit(SubExpr))
4499 return false;
4500 if (!this->emitConstUint8(0, E))
4501 return false;
4502 if (!this->emitArrayElemPtrPopUint8(E))
4503 return false;
4504 if (!this->emitInitFieldPtr(R->getField(I: 0u)->Offset, E))
4505 return false;
4506
4507 PrimType SecondFieldT = classifyPrim(R->getField(I: 1u)->Decl->getType());
4508 if (isIntegerOrBoolType(T: SecondFieldT)) {
4509 if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E))
4510 return false;
4511 return this->emitInitField(SecondFieldT, R->getField(I: 1u)->Offset, E);
4512 }
4513 assert(SecondFieldT == PT_Ptr);
4514
4515 if (!this->emitGetFieldPtr(R->getField(I: 0u)->Offset, E))
4516 return false;
4517 if (!this->emitExpandPtr(E))
4518 return false;
4519 if (!this->emitConst(ArrayType->getSize(), PT_Uint64, E))
4520 return false;
4521 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
4522 return false;
4523 return this->emitInitFieldPtr(R->getField(I: 1u)->Offset, E);
4524}
4525
4526template <class Emitter>
4527bool Compiler<Emitter>::VisitStmtExpr(const StmtExpr *E) {
4528 LocalScope<Emitter> BS(this);
4529 StmtExprScope<Emitter> SS(this);
4530
4531 const CompoundStmt *CS = E->getSubStmt();
4532 const Stmt *Result = CS->body_back();
4533 for (const Stmt *S : CS->body()) {
4534 if (S != Result) {
4535 if (!this->visitStmt(S))
4536 return false;
4537 continue;
4538 }
4539
4540 assert(S == Result);
4541 if (const Expr *ResultExpr = dyn_cast<Expr>(Val: S))
4542 return this->delegate(ResultExpr);
4543 return this->emitUnsupported(E);
4544 }
4545
4546 return BS.destroyLocals();
4547}
4548
4549template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
4550 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
4551 /*NewInitializing=*/false, /*ToLValue=*/false);
4552 return this->Visit(E);
4553}
4554
4555template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
4556 // We're basically doing:
4557 // OptionScope<Emitter> Scope(this, DicardResult, Initializing, ToLValue);
4558 // but that's unnecessary of course.
4559 return this->Visit(E);
4560}
4561
4562static const Expr *stripCheckedDerivedToBaseCasts(const Expr *E) {
4563 if (const auto *PE = dyn_cast<ParenExpr>(Val: E))
4564 return stripCheckedDerivedToBaseCasts(E: PE->getSubExpr());
4565
4566 if (const auto *CE = dyn_cast<CastExpr>(Val: E);
4567 CE &&
4568 (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp))
4569 return stripCheckedDerivedToBaseCasts(E: CE->getSubExpr());
4570
4571 return E;
4572}
4573
4574static const Expr *stripDerivedToBaseCasts(const Expr *E) {
4575 if (const auto *PE = dyn_cast<ParenExpr>(Val: E))
4576 return stripDerivedToBaseCasts(E: PE->getSubExpr());
4577
4578 if (const auto *CE = dyn_cast<CastExpr>(Val: E);
4579 CE && (CE->getCastKind() == CK_DerivedToBase ||
4580 CE->getCastKind() == CK_UncheckedDerivedToBase ||
4581 CE->getCastKind() == CK_NoOp))
4582 return stripDerivedToBaseCasts(E: CE->getSubExpr());
4583
4584 return E;
4585}
4586
4587template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
4588 if (E->getType().isNull())
4589 return false;
4590
4591 if (E->getType()->isVoidType())
4592 return this->discard(E);
4593
4594 // Create local variable to hold the return value.
4595 if (!E->isGLValue() && !canClassify(E->getType())) {
4596 UnsignedOrNone LocalIndex = allocateLocal(
4597 Decl: stripDerivedToBaseCasts(E), Ty: QualType(), ScopeKind::FullExpression);
4598 if (!LocalIndex)
4599 return false;
4600
4601 if (!this->emitGetPtrLocal(*LocalIndex, E))
4602 return false;
4603 InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex));
4604 return this->visitInitializer(E);
4605 }
4606
4607 // Otherwise,we have a primitive return value, produce the value directly
4608 // and push it on the stack.
4609 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4610 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
4611 return this->Visit(E);
4612}
4613
4614template <class Emitter>
4615bool Compiler<Emitter>::visitInitializer(const Expr *E) {
4616 assert(!canClassify(E->getType()));
4617
4618 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4619 /*NewInitializing=*/true, /*ToLValue=*/false);
4620 return this->Visit(E);
4621}
4622
4623template <class Emitter> bool Compiler<Emitter>::visitAsLValue(const Expr *E) {
4624 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4625 /*NewInitializing=*/false, /*ToLValue=*/true);
4626 return this->Visit(E);
4627}
4628
4629template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
4630 OptPrimType T = classify(E->getType());
4631 if (!T) {
4632 // Convert complex values to bool.
4633 if (E->getType()->isAnyComplexType()) {
4634 if (!this->visit(E))
4635 return false;
4636 return this->emitComplexBoolCast(E);
4637 }
4638 return false;
4639 }
4640
4641 if (!this->visit(E))
4642 return false;
4643
4644 if (T == PT_Bool)
4645 return true;
4646
4647 // Convert pointers to bool.
4648 if (T == PT_Ptr)
4649 return this->emitIsNonNullPtr(E);
4650
4651 // Or Floats.
4652 if (T == PT_Float)
4653 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
4654
4655 // Or anything else we can.
4656 return this->emitCast(*T, PT_Bool, E);
4657}
4658
4659template <class Emitter>
4660bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
4661 const Expr *E) {
4662 if (const auto *AT = QT->getAs<AtomicType>())
4663 QT = AT->getValueType();
4664
4665 switch (T) {
4666 case PT_Bool:
4667 return this->emitZeroBool(E);
4668 case PT_Sint8:
4669 return this->emitZeroSint8(E);
4670 case PT_Uint8:
4671 return this->emitZeroUint8(E);
4672 case PT_Sint16:
4673 return this->emitZeroSint16(E);
4674 case PT_Uint16:
4675 return this->emitZeroUint16(E);
4676 case PT_Sint32:
4677 return this->emitZeroSint32(E);
4678 case PT_Uint32:
4679 return this->emitZeroUint32(E);
4680 case PT_Sint64:
4681 return this->emitZeroSint64(E);
4682 case PT_Uint64:
4683 return this->emitZeroUint64(E);
4684 case PT_IntAP:
4685 return this->emitZeroIntAP(Ctx.getBitWidth(T: QT), E);
4686 case PT_IntAPS:
4687 return this->emitZeroIntAPS(Ctx.getBitWidth(T: QT), E);
4688 case PT_Ptr:
4689 return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
4690 nullptr, E);
4691 case PT_MemberPtr:
4692 return this->emitNullMemberPtr(0, nullptr, E);
4693 case PT_Float: {
4694 APFloat F = APFloat::getZero(Sem: Ctx.getFloatSemantics(T: QT));
4695 return this->emitFloat(F, E);
4696 }
4697 case PT_FixedPoint: {
4698 auto Sem = Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType());
4699 return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
4700 }
4701 }
4702 llvm_unreachable("unknown primitive type");
4703}
4704
4705template <class Emitter>
4706bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
4707 const Expr *E) {
4708 assert(E);
4709 assert(R);
4710 // Fields
4711 for (const Record::Field &Field : R->fields()) {
4712 if (Field.isUnnamedBitField())
4713 continue;
4714
4715 const Descriptor *D = Field.Desc;
4716 if (D->isPrimitive()) {
4717 QualType QT = D->getType();
4718 PrimType T = classifyPrim(D->getType());
4719 if (!this->visitZeroInitializer(T, QT, E))
4720 return false;
4721 if (R->isUnion()) {
4722 if (!this->emitInitFieldActivate(T, Field.Offset, E))
4723 return false;
4724 break;
4725 }
4726 if (!this->emitInitField(T, Field.Offset, E))
4727 return false;
4728 continue;
4729 }
4730
4731 if (!this->emitGetPtrField(Field.Offset, E))
4732 return false;
4733
4734 if (D->isPrimitiveArray()) {
4735 QualType ET = D->getElemQualType();
4736 PrimType T = classifyPrim(ET);
4737 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
4738 if (!this->visitZeroInitializer(T, ET, E))
4739 return false;
4740 if (!this->emitInitElem(T, I, E))
4741 return false;
4742 }
4743 } else if (D->isCompositeArray()) {
4744 // Can't be a vector or complex field.
4745 if (!this->visitZeroArrayInitializer(D->getType(), E))
4746 return false;
4747 } else if (D->isRecord()) {
4748 if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
4749 return false;
4750 } else
4751 return false;
4752
4753 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
4754 // object's first non-static named data member is zero-initialized
4755 if (R->isUnion()) {
4756 if (!this->emitFinishInitActivatePop(E))
4757 return false;
4758 break;
4759 }
4760 if (!this->emitFinishInitPop(E))
4761 return false;
4762 }
4763
4764 for (const Record::Base &B : R->bases()) {
4765 if (!this->emitGetPtrBase(B.Offset, E))
4766 return false;
4767 if (!this->visitZeroRecordInitializer(B.R, E))
4768 return false;
4769 if (!this->emitFinishInitPop(E))
4770 return false;
4771 }
4772
4773 // FIXME: Virtual bases.
4774
4775 return true;
4776}
4777
4778template <class Emitter>
4779bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
4780 assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
4781 const ArrayType *AT = T->getAsArrayTypeUnsafe();
4782 QualType ElemType = AT->getElementType();
4783 size_t NumElems = cast<ConstantArrayType>(Val: AT)->getZExtSize();
4784
4785 if (OptPrimType ElemT = classify(ElemType)) {
4786 for (size_t I = 0; I != NumElems; ++I) {
4787 if (!this->visitZeroInitializer(*ElemT, ElemType, E))
4788 return false;
4789 if (!this->emitInitElem(*ElemT, I, E))
4790 return false;
4791 }
4792 return true;
4793 }
4794 if (ElemType->isRecordType()) {
4795 const Record *R = getRecord(ElemType);
4796 if (!R)
4797 return false;
4798
4799 for (size_t I = 0; I != NumElems; ++I) {
4800 if (!this->emitConstUint32(I, E))
4801 return false;
4802 if (!this->emitArrayElemPtr(PT_Uint32, E))
4803 return false;
4804 if (!this->visitZeroRecordInitializer(R, E))
4805 return false;
4806 if (!this->emitPopPtr(E))
4807 return false;
4808 }
4809 return true;
4810 }
4811 if (ElemType->isArrayType()) {
4812 for (size_t I = 0; I != NumElems; ++I) {
4813 if (!this->emitConstUint32(I, E))
4814 return false;
4815 if (!this->emitArrayElemPtr(PT_Uint32, E))
4816 return false;
4817 if (!this->visitZeroArrayInitializer(ElemType, E))
4818 return false;
4819 if (!this->emitPopPtr(E))
4820 return false;
4821 }
4822 return true;
4823 }
4824
4825 return false;
4826}
4827
4828template <class Emitter>
4829bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS,
4830 const Expr *E) {
4831 if (!canClassify(E->getType()))
4832 return false;
4833
4834 if (!this->visit(RHS))
4835 return false;
4836 if (!this->visit(LHS))
4837 return false;
4838
4839 if (LHS->getType().isVolatileQualified())
4840 return this->emitInvalidStore(LHS->getType().getTypePtr(), E);
4841
4842 // We don't support assignments in C.
4843 if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(E))
4844 return false;
4845
4846 PrimType RHT = classifyPrim(RHS);
4847 bool Activates = refersToUnion(E: LHS);
4848 bool BitField = LHS->refersToBitField();
4849
4850 if (!this->emitFlip(PT_Ptr, RHT, E))
4851 return false;
4852
4853 if (DiscardResult) {
4854 if (BitField && Activates)
4855 return this->emitStoreBitFieldActivatePop(RHT, E);
4856 if (BitField)
4857 return this->emitStoreBitFieldPop(RHT, E);
4858 if (Activates)
4859 return this->emitStoreActivatePop(RHT, E);
4860 // Otherwise, regular non-activating store.
4861 return this->emitStorePop(RHT, E);
4862 }
4863
4864 auto maybeLoad = [&](bool Result) -> bool {
4865 if (!Result)
4866 return false;
4867 // Assignments aren't necessarily lvalues in C.
4868 // Load from them in that case.
4869 if (!E->isLValue())
4870 return this->emitLoadPop(RHT, E);
4871 return true;
4872 };
4873
4874 if (BitField && Activates)
4875 return maybeLoad(this->emitStoreBitFieldActivate(RHT, E));
4876 if (BitField)
4877 return maybeLoad(this->emitStoreBitField(RHT, E));
4878 if (Activates)
4879 return maybeLoad(this->emitStoreActivate(RHT, E));
4880 // Otherwise, regular non-activating store.
4881 return maybeLoad(this->emitStore(RHT, E));
4882}
4883
4884template <class Emitter>
4885template <typename T>
4886bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
4887 switch (Ty) {
4888 case PT_Sint8:
4889 return this->emitConstSint8(Value, E);
4890 case PT_Uint8:
4891 return this->emitConstUint8(Value, E);
4892 case PT_Sint16:
4893 return this->emitConstSint16(Value, E);
4894 case PT_Uint16:
4895 return this->emitConstUint16(Value, E);
4896 case PT_Sint32:
4897 return this->emitConstSint32(Value, E);
4898 case PT_Uint32:
4899 return this->emitConstUint32(Value, E);
4900 case PT_Sint64:
4901 return this->emitConstSint64(Value, E);
4902 case PT_Uint64:
4903 return this->emitConstUint64(Value, E);
4904 case PT_Bool:
4905 return this->emitConstBool(Value, E);
4906 case PT_Ptr:
4907 case PT_MemberPtr:
4908 case PT_Float:
4909 case PT_IntAP:
4910 case PT_IntAPS:
4911 case PT_FixedPoint:
4912 llvm_unreachable("Invalid integral type");
4913 break;
4914 }
4915 llvm_unreachable("unknown primitive type");
4916}
4917
4918template <class Emitter>
4919template <typename T>
4920bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
4921 return this->emitConst(Value, classifyPrim(E->getType()), E);
4922}
4923
4924template <class Emitter>
4925bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
4926 const Expr *E) {
4927 if (Ty == PT_IntAPS)
4928 return this->emitConstIntAPS(Value, E);
4929 if (Ty == PT_IntAP)
4930 return this->emitConstIntAP(Value, E);
4931
4932 if (Value.isSigned())
4933 return this->emitConst(Value.getSExtValue(), Ty, E);
4934 return this->emitConst(Value.getZExtValue(), Ty, E);
4935}
4936
4937template <class Emitter>
4938bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty,
4939 const Expr *E) {
4940 if (Ty == PT_IntAPS)
4941 return this->emitConstIntAPS(Value, E);
4942 if (Ty == PT_IntAP)
4943 return this->emitConstIntAP(Value, E);
4944
4945 if (isSignedType(T: Ty))
4946 return this->emitConst(Value.getSExtValue(), Ty, E);
4947 return this->emitConst(Value.getZExtValue(), Ty, E);
4948}
4949
4950template <class Emitter>
4951bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
4952 return this->emitConst(Value, classifyPrim(E->getType()), E);
4953}
4954
4955template <class Emitter>
4956unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty,
4957 bool IsConst,
4958 bool IsVolatile,
4959 ScopeKind SC,
4960 bool IsConstexprUnknown) {
4961 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
4962 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
4963 // or isa<MaterializeTemporaryExpr>().
4964 Descriptor *D = P.createDescriptor(D: Src, T: Ty, SourceTy: nullptr, MDSize: Descriptor::InlineDescMD,
4965 IsConst, IsTemporary: isa<const Expr *>(Val: Src),
4966 /*IsMutable=*/false, IsVolatile);
4967 D->IsConstexprUnknown = IsConstexprUnknown;
4968 Scope::Local Local = this->createLocal(D);
4969 if (auto *VD = dyn_cast_if_present<ValueDecl>(Val: Src.dyn_cast<const Decl *>()))
4970 Locals.insert(KV: {VD, Local});
4971 VarScope->addForScopeKind(Local, SC);
4972 return Local.Offset;
4973}
4974
4975template <class Emitter>
4976UnsignedOrNone Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
4977 ScopeKind SC,
4978 bool IsConstexprUnknown) {
4979 const ValueDecl *Key = nullptr;
4980 const Expr *Init = nullptr;
4981 bool IsTemporary = false;
4982 if (auto *VD = dyn_cast_if_present<ValueDecl>(Val: Src.dyn_cast<const Decl *>())) {
4983 Key = VD;
4984
4985 if (const auto *VarD = dyn_cast<VarDecl>(Val: VD))
4986 Init = VarD->getInit();
4987 }
4988 if (auto *E = Src.dyn_cast<const Expr *>()) {
4989 IsTemporary = true;
4990 if (Ty.isNull())
4991 Ty = E->getType();
4992 }
4993
4994 Descriptor *D = P.createDescriptor(
4995 D: Src, Ty: Ty.getTypePtr(), MDSize: Descriptor::InlineDescMD, IsConst: Ty.isConstQualified(),
4996 IsTemporary, /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(),
4997 Init);
4998 if (!D)
4999 return std::nullopt;
5000 D->IsConstexprUnknown = IsConstexprUnknown;
5001
5002 Scope::Local Local = this->createLocal(D);
5003 if (Key)
5004 Locals.insert(KV: {Key, Local});
5005 VarScope->addForScopeKind(Local, SC);
5006 return Local.Offset;
5007}
5008
5009template <class Emitter>
5010UnsignedOrNone Compiler<Emitter>::allocateTemporary(const Expr *E) {
5011 QualType Ty = E->getType();
5012 assert(!Ty->isRecordType());
5013
5014 Descriptor *D = P.createDescriptor(
5015 D: E, Ty: Ty.getTypePtr(), MDSize: Descriptor::InlineDescMD, IsConst: Ty.isConstQualified(),
5016 /*IsTemporary=*/true);
5017
5018 if (!D)
5019 return std::nullopt;
5020
5021 Scope::Local Local = this->createLocal(D);
5022 VariableScope<Emitter> *S = VarScope;
5023 assert(S);
5024 // Attach to topmost scope.
5025 while (S->getParent())
5026 S = S->getParent();
5027 assert(S && !S->getParent());
5028 S->addLocal(Local);
5029 return Local.Offset;
5030}
5031
5032template <class Emitter>
5033const RecordType *Compiler<Emitter>::getRecordTy(QualType Ty) {
5034 if (const PointerType *PT = dyn_cast<PointerType>(Val&: Ty))
5035 return PT->getPointeeType()->getAsCanonical<RecordType>();
5036 return Ty->getAsCanonical<RecordType>();
5037}
5038
5039template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
5040 if (const auto *RecordTy = getRecordTy(Ty))
5041 return getRecord(RecordTy->getDecl()->getDefinitionOrSelf());
5042 return nullptr;
5043}
5044
5045template <class Emitter>
5046Record *Compiler<Emitter>::getRecord(const RecordDecl *RD) {
5047 return P.getOrCreateRecord(RD);
5048}
5049
5050template <class Emitter>
5051const Function *Compiler<Emitter>::getFunction(const FunctionDecl *FD) {
5052 return Ctx.getOrCreateFunction(FuncDecl: FD);
5053}
5054
5055template <class Emitter>
5056bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
5057 LocalScope<Emitter> RootScope(this, ScopeKind::FullExpression);
5058
5059 auto maybeDestroyLocals = [&]() -> bool {
5060 if (DestroyToplevelScope)
5061 return RootScope.destroyLocals() && this->emitCheckAllocations(E);
5062 return this->emitCheckAllocations(E);
5063 };
5064
5065 // Void expressions.
5066 if (E->getType()->isVoidType()) {
5067 if (!visit(E))
5068 return false;
5069 return this->emitRetVoid(E) && maybeDestroyLocals();
5070 }
5071
5072 // Expressions with a primitive return type.
5073 if (OptPrimType T = classify(E)) {
5074 if (!visit(E))
5075 return false;
5076
5077 return this->emitRet(*T, E) && maybeDestroyLocals();
5078 }
5079
5080 // Expressions with a composite return type.
5081 // For us, that means everything we don't
5082 // have a PrimType for.
5083 if (UnsignedOrNone LocalOffset = this->allocateLocal(E)) {
5084 InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalOffset));
5085 if (!this->emitGetPtrLocal(*LocalOffset, E))
5086 return false;
5087
5088 if (!visitInitializer(E))
5089 return false;
5090
5091 if (!this->emitFinishInit(E))
5092 return false;
5093 // We are destroying the locals AFTER the Ret op.
5094 // The Ret op needs to copy the (alive) values, but the
5095 // destructors may still turn the entire expression invalid.
5096 return this->emitRetValue(E) && maybeDestroyLocals();
5097 }
5098
5099 return maybeDestroyLocals() && false;
5100}
5101
5102template <class Emitter>
5103VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD,
5104 bool IsConstexprUnknown) {
5105
5106 auto R = this->visitVarDecl(VD, VD->getInit(), /*Toplevel=*/true,
5107 IsConstexprUnknown);
5108
5109 if (R.notCreated())
5110 return R;
5111
5112 if (R)
5113 return true;
5114
5115 if (!R && Context::shouldBeGloballyIndexed(VD)) {
5116 if (auto GlobalIndex = P.getGlobal(VD)) {
5117 Block *GlobalBlock = P.getGlobal(Idx: *GlobalIndex);
5118 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5119
5120 GD.InitState = GlobalInitState::InitializerFailed;
5121 GlobalBlock->invokeDtor();
5122 }
5123 }
5124
5125 return R;
5126}
5127
5128/// Toplevel visitDeclAndReturn().
5129/// We get here from evaluateAsInitializer().
5130/// We need to evaluate the initializer and return its value.
5131template <class Emitter>
5132bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
5133 bool ConstantContext) {
5134 // We only create variables if we're evaluating in a constant context.
5135 // Otherwise, just evaluate the initializer and return it.
5136 if (!ConstantContext) {
5137 DeclScope<Emitter> LS(this, VD);
5138 if (!this->visit(Init))
5139 return false;
5140 return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
5141 LS.destroyLocals() && this->emitCheckAllocations(VD);
5142 }
5143
5144 LocalScope<Emitter> VDScope(this);
5145 if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true))
5146 return false;
5147
5148 OptPrimType VarT = classify(VD->getType());
5149 if (Context::shouldBeGloballyIndexed(VD)) {
5150 auto GlobalIndex = P.getGlobal(VD);
5151 assert(GlobalIndex); // visitVarDecl() didn't return false.
5152 if (VarT) {
5153 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
5154 return false;
5155 } else {
5156 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
5157 return false;
5158 }
5159 } else {
5160 auto Local = Locals.find(Val: VD);
5161 assert(Local != Locals.end()); // Same here.
5162 if (VarT) {
5163 if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
5164 return false;
5165 } else {
5166 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
5167 return false;
5168 }
5169 }
5170
5171 // Return the value.
5172 if (!this->emitRet(VarT.value_or(PT: PT_Ptr), VD)) {
5173 // If the Ret above failed and this is a global variable, mark it as
5174 // uninitialized, even everything else succeeded.
5175 if (Context::shouldBeGloballyIndexed(VD)) {
5176 auto GlobalIndex = P.getGlobal(VD);
5177 assert(GlobalIndex);
5178 Block *GlobalBlock = P.getGlobal(Idx: *GlobalIndex);
5179 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5180
5181 GD.InitState = GlobalInitState::InitializerFailed;
5182 GlobalBlock->invokeDtor();
5183 }
5184 return false;
5185 }
5186
5187 return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
5188}
5189
5190template <class Emitter>
5191VarCreationState
5192Compiler<Emitter>::visitVarDecl(const VarDecl *VD, const Expr *Init,
5193 bool Toplevel, bool IsConstexprUnknown) {
5194 // We don't know what to do with these, so just return false.
5195 if (VD->getType().isNull())
5196 return false;
5197
5198 // This case is EvalEmitter-only. If we won't create any instructions for the
5199 // initializer anyway, don't bother creating the variable in the first place.
5200 if (!this->isActive())
5201 return VarCreationState::NotCreated();
5202
5203 OptPrimType VarT = classify(VD->getType());
5204
5205 if (Init && Init->isValueDependent())
5206 return false;
5207
5208 if (Context::shouldBeGloballyIndexed(VD)) {
5209 auto checkDecl = [&]() -> bool {
5210 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
5211 return !NeedsOp || this->emitCheckDecl(VD, VD);
5212 };
5213
5214 DeclScope<Emitter> LocalScope(this, VD);
5215
5216 UnsignedOrNone GlobalIndex = P.getGlobal(VD);
5217 if (GlobalIndex) {
5218 // The global was previously created but the initializer failed.
5219 if (!P.getGlobal(Idx: *GlobalIndex)->isInitialized())
5220 return false;
5221 // We've already seen and initialized this global.
5222 if (P.isGlobalInitialized(Index: *GlobalIndex))
5223 return checkDecl();
5224 // The previous attempt at initialization might've been unsuccessful,
5225 // so let's try this one.
5226 } else if ((GlobalIndex = P.createGlobal(VD, Init))) {
5227 } else {
5228 return false;
5229 }
5230 if (!Init)
5231 return true;
5232
5233 if (!checkDecl())
5234 return false;
5235
5236 if (VarT) {
5237 if (!this->visit(Init))
5238 return false;
5239
5240 return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
5241 }
5242
5243 if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
5244 return false;
5245
5246 if (!visitInitializer(E: Init))
5247 return false;
5248
5249 return this->emitFinishInitGlobal(Init);
5250 }
5251 // Local variables.
5252 InitLinkScope<Emitter> ILS(this, InitLink::Decl(D: VD));
5253
5254 if (VarT) {
5255 unsigned Offset = this->allocateLocalPrimitive(
5256 VD, *VarT, VD->getType().isConstQualified(),
5257 VD->getType().isVolatileQualified(), ScopeKind::Block,
5258 IsConstexprUnknown);
5259
5260 if (!Init)
5261 return true;
5262
5263 // If this is a toplevel declaration, create a scope for the
5264 // initializer.
5265 if (Toplevel) {
5266 LocalScope<Emitter> Scope(this);
5267 if (!this->visit(Init))
5268 return false;
5269 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
5270 }
5271 if (!this->visit(Init))
5272 return false;
5273 return this->emitSetLocal(*VarT, Offset, VD);
5274 }
5275 // Local composite variables.
5276 if (UnsignedOrNone Offset = this->allocateLocal(
5277 VD, VD->getType(), ScopeKind::Block, IsConstexprUnknown)) {
5278 if (!Init)
5279 return true;
5280
5281 if (!this->emitGetPtrLocal(*Offset, Init))
5282 return false;
5283
5284 if (!visitInitializer(E: Init))
5285 return false;
5286
5287 return this->emitFinishInitPop(Init);
5288 }
5289 return false;
5290}
5291
5292template <class Emitter>
5293bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType,
5294 const Expr *E) {
5295 assert(!DiscardResult);
5296 if (Val.isInt())
5297 return this->emitConst(Val.getInt(), ValType, E);
5298 if (Val.isFloat()) {
5299 APFloat F = Val.getFloat();
5300 return this->emitFloat(F, E);
5301 }
5302
5303 if (Val.isLValue()) {
5304 if (Val.isNullPointer())
5305 return this->emitNull(ValType, 0, nullptr, E);
5306 APValue::LValueBase Base = Val.getLValueBase();
5307 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
5308 return this->visit(BaseExpr);
5309 if (const auto *VD = Base.dyn_cast<const ValueDecl *>())
5310 return this->visitDeclRef(VD, E);
5311 } else if (Val.isMemberPointer()) {
5312 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl())
5313 return this->emitGetMemberPtr(MemberDecl, E);
5314 return this->emitNullMemberPtr(0, nullptr, E);
5315 }
5316
5317 return false;
5318}
5319
5320template <class Emitter>
5321bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val,
5322 const Expr *E, QualType T) {
5323 if (Val.isStruct()) {
5324 const Record *R = this->getRecord(T);
5325 assert(R);
5326 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
5327 const APValue &F = Val.getStructField(i: I);
5328 const Record::Field *RF = R->getField(I);
5329 QualType FieldType = RF->Decl->getType();
5330
5331 if (OptPrimType PT = classify(FieldType)) {
5332 if (!this->visitAPValue(F, *PT, E))
5333 return false;
5334 if (!this->emitInitField(*PT, RF->Offset, E))
5335 return false;
5336 } else {
5337 if (!this->emitGetPtrField(RF->Offset, E))
5338 return false;
5339 if (!this->visitAPValueInitializer(F, E, FieldType))
5340 return false;
5341 if (!this->emitPopPtr(E))
5342 return false;
5343 }
5344 }
5345 return true;
5346 }
5347 if (Val.isUnion()) {
5348 const FieldDecl *UnionField = Val.getUnionField();
5349 if (!UnionField)
5350 return true;
5351 const Record *R = this->getRecord(T);
5352 assert(R);
5353 const APValue &F = Val.getUnionValue();
5354 const Record::Field *RF = R->getField(FD: UnionField);
5355 QualType FieldType = RF->Decl->getType();
5356
5357 if (OptPrimType PT = classify(FieldType)) {
5358 if (!this->visitAPValue(F, *PT, E))
5359 return false;
5360 if (RF->isBitField())
5361 return this->emitInitBitFieldActivate(*PT, RF, E);
5362 return this->emitInitFieldActivate(*PT, RF->Offset, E);
5363 }
5364
5365 if (!this->emitGetPtrField(RF->Offset, E))
5366 return false;
5367 if (!this->emitActivate(E))
5368 return false;
5369 if (!this->visitAPValueInitializer(F, E, FieldType))
5370 return false;
5371 return this->emitPopPtr(E);
5372 }
5373 if (Val.isArray()) {
5374 const auto *ArrType = T->getAsArrayTypeUnsafe();
5375 QualType ElemType = ArrType->getElementType();
5376 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
5377 const APValue &Elem = Val.getArrayInitializedElt(I: A);
5378 if (OptPrimType ElemT = classify(ElemType)) {
5379 if (!this->visitAPValue(Elem, *ElemT, E))
5380 return false;
5381 if (!this->emitInitElem(*ElemT, A, E))
5382 return false;
5383 } else {
5384 if (!this->emitConstUint32(A, E))
5385 return false;
5386 if (!this->emitArrayElemPtrUint32(E))
5387 return false;
5388 if (!this->visitAPValueInitializer(Elem, E, ElemType))
5389 return false;
5390 if (!this->emitPopPtr(E))
5391 return false;
5392 }
5393 }
5394 return true;
5395 }
5396 // TODO: Other types.
5397
5398 return false;
5399}
5400
5401template <class Emitter>
5402bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E,
5403 unsigned BuiltinID) {
5404 if (BuiltinID == Builtin::BI__builtin_constant_p) {
5405 // Void argument is always invalid and harder to handle later.
5406 if (E->getArg(Arg: 0)->getType()->isVoidType()) {
5407 if (DiscardResult)
5408 return true;
5409 return this->emitConst(0, E);
5410 }
5411
5412 if (!this->emitStartSpeculation(E))
5413 return false;
5414 LabelTy EndLabel = this->getLabel();
5415 if (!this->speculate(E, EndLabel))
5416 return false;
5417 this->fallthrough(EndLabel);
5418 if (!this->emitEndSpeculation(E))
5419 return false;
5420 if (DiscardResult)
5421 return this->emitPop(classifyPrim(E), E);
5422 return true;
5423 }
5424
5425 // For these, we're expected to ultimately return an APValue pointing
5426 // to the CallExpr. This is needed to get the correct codegen.
5427 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
5428 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
5429 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
5430 BuiltinID == Builtin::BI__builtin_function_start) {
5431 if (DiscardResult)
5432 return true;
5433 return this->emitDummyPtr(E, E);
5434 }
5435
5436 QualType ReturnType = E->getType();
5437 OptPrimType ReturnT = classify(E);
5438
5439 // Non-primitive return type. Prepare storage.
5440 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
5441 UnsignedOrNone LocalIndex = allocateLocal(Src: E);
5442 if (!LocalIndex)
5443 return false;
5444 if (!this->emitGetPtrLocal(*LocalIndex, E))
5445 return false;
5446 }
5447
5448 // Prepare function arguments including special cases.
5449 switch (BuiltinID) {
5450 case Builtin::BI__builtin_object_size:
5451 case Builtin::BI__builtin_dynamic_object_size: {
5452 assert(E->getNumArgs() == 2);
5453 const Expr *Arg0 = E->getArg(Arg: 0);
5454 if (Arg0->isGLValue()) {
5455 if (!this->visit(Arg0))
5456 return false;
5457
5458 } else {
5459 if (!this->visitAsLValue(Arg0))
5460 return false;
5461 }
5462 if (!this->visit(E->getArg(Arg: 1)))
5463 return false;
5464
5465 } break;
5466 case Builtin::BI__assume:
5467 case Builtin::BI__builtin_assume:
5468 // Argument is not evaluated.
5469 break;
5470 case Builtin::BI__atomic_is_lock_free:
5471 case Builtin::BI__atomic_always_lock_free: {
5472 assert(E->getNumArgs() == 2);
5473 if (!this->visit(E->getArg(Arg: 0)))
5474 return false;
5475 if (!this->visitAsLValue(E->getArg(Arg: 1)))
5476 return false;
5477 } break;
5478
5479 default:
5480 if (!Context::isUnevaluatedBuiltin(ID: BuiltinID)) {
5481 // Put arguments on the stack.
5482 for (const auto *Arg : E->arguments()) {
5483 if (!this->visit(Arg))
5484 return false;
5485 }
5486 }
5487 }
5488
5489 if (!this->emitCallBI(E, BuiltinID, E))
5490 return false;
5491
5492 if (DiscardResult && !ReturnType->isVoidType())
5493 return this->emitPop(ReturnT.value_or(PT: PT_Ptr), E);
5494
5495 return true;
5496}
5497
5498template <class Emitter>
5499bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
5500 if (E->containsErrors())
5501 return false;
5502 const FunctionDecl *FuncDecl = E->getDirectCallee();
5503
5504 if (FuncDecl) {
5505 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
5506 return VisitBuiltinCallExpr(E, BuiltinID);
5507
5508 // Calls to replaceable operator new/operator delete.
5509 if (FuncDecl->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
5510 if (FuncDecl->getDeclName().isAnyOperatorNew())
5511 return VisitBuiltinCallExpr(E, BuiltinID: Builtin::BI__builtin_operator_new);
5512 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete);
5513 return VisitBuiltinCallExpr(E, BuiltinID: Builtin::BI__builtin_operator_delete);
5514 }
5515
5516 // Explicit calls to trivial destructors
5517 if (const auto *DD = dyn_cast<CXXDestructorDecl>(Val: FuncDecl);
5518 DD && DD->isTrivial()) {
5519 const auto *MemberCall = cast<CXXMemberCallExpr>(Val: E);
5520 if (!this->visit(MemberCall->getImplicitObjectArgument()))
5521 return false;
5522 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
5523 this->emitPopPtr(E);
5524 }
5525 }
5526
5527 LocalScope<Emitter> CallScope(this, ScopeKind::Call);
5528
5529 QualType ReturnType = E->getCallReturnType(Ctx: Ctx.getASTContext());
5530 OptPrimType T = classify(ReturnType);
5531 bool HasRVO = !ReturnType->isVoidType() && !T;
5532
5533 if (HasRVO) {
5534 if (DiscardResult) {
5535 // If we need to discard the return value but the function returns its
5536 // value via an RVO pointer, we need to create one such pointer just
5537 // for this call.
5538 if (UnsignedOrNone LocalIndex = allocateLocal(Src: E)) {
5539 if (!this->emitGetPtrLocal(*LocalIndex, E))
5540 return false;
5541 }
5542 } else {
5543 // We need the result. Prepare a pointer to return or
5544 // dup the current one.
5545 if (!Initializing) {
5546 if (UnsignedOrNone LocalIndex = allocateLocal(Src: E)) {
5547 if (!this->emitGetPtrLocal(*LocalIndex, E))
5548 return false;
5549 }
5550 }
5551 if (!this->emitDupPtr(E))
5552 return false;
5553 }
5554 }
5555
5556 SmallVector<const Expr *, 8> Args(ArrayRef(E->getArgs(), E->getNumArgs()));
5557
5558 bool IsAssignmentOperatorCall = false;
5559 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E);
5560 OCE && OCE->isAssignmentOp()) {
5561 // Just like with regular assignments, we need to special-case assignment
5562 // operators here and evaluate the RHS (the second arg) before the LHS (the
5563 // first arg). We fix this by using a Flip op later.
5564 assert(Args.size() == 2);
5565 IsAssignmentOperatorCall = true;
5566 std::reverse(first: Args.begin(), last: Args.end());
5567 }
5568 // Calling a static operator will still
5569 // pass the instance, but we don't need it.
5570 // Discard it here.
5571 if (isa<CXXOperatorCallExpr>(Val: E)) {
5572 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: FuncDecl);
5573 MD && MD->isStatic()) {
5574 if (!this->discard(E->getArg(Arg: 0)))
5575 return false;
5576 // Drop first arg.
5577 Args.erase(CI: Args.begin());
5578 }
5579 }
5580
5581 bool Devirtualized = false;
5582 UnsignedOrNone CalleeOffset = std::nullopt;
5583 // Add the (optional, implicit) This pointer.
5584 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(Val: E)) {
5585 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
5586 // If we end up creating a CallPtr op for this, we need the base of the
5587 // member pointer as the instance pointer, and later extract the function
5588 // decl as the function pointer.
5589 const Expr *Callee = E->getCallee();
5590 CalleeOffset =
5591 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
5592 if (!this->visit(Callee))
5593 return false;
5594 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
5595 return false;
5596 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5597 return false;
5598 if (!this->emitGetMemberPtrBase(E))
5599 return false;
5600 } else {
5601 const auto *InstancePtr = MC->getImplicitObjectArgument();
5602 if (isa_and_nonnull<CXXDestructorDecl>(Val: CompilingFunction) ||
5603 isa_and_nonnull<CXXConstructorDecl>(Val: CompilingFunction)) {
5604 const auto *Stripped = stripCheckedDerivedToBaseCasts(E: InstancePtr);
5605 if (isa<CXXThisExpr>(Val: Stripped)) {
5606 FuncDecl =
5607 cast<CXXMethodDecl>(Val: FuncDecl)->getCorrespondingMethodInClass(
5608 RD: Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5609 Devirtualized = true;
5610 if (!this->visit(Stripped))
5611 return false;
5612 } else {
5613 if (!this->visit(InstancePtr))
5614 return false;
5615 }
5616 } else {
5617 if (!this->visit(InstancePtr))
5618 return false;
5619 }
5620 }
5621 } else if (const auto *PD =
5622 dyn_cast<CXXPseudoDestructorExpr>(Val: E->getCallee())) {
5623 if (!this->emitCheckPseudoDtor(E))
5624 return false;
5625 const Expr *Base = PD->getBase();
5626 // E.g. `using T = int; 0.~T();`.
5627 if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr)
5628 return this->discard(Base);
5629 if (!this->visit(Base))
5630 return false;
5631 return this->emitEndLifetimePop(E);
5632 } else if (!FuncDecl) {
5633 const Expr *Callee = E->getCallee();
5634 CalleeOffset =
5635 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
5636 if (!this->visit(Callee))
5637 return false;
5638 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
5639 return false;
5640 }
5641
5642 if (!this->visitCallArgs(Args, FuncDecl, IsAssignmentOperatorCall,
5643 isa<CXXOperatorCallExpr>(Val: E)))
5644 return false;
5645
5646 // Undo the argument reversal we did earlier.
5647 if (IsAssignmentOperatorCall) {
5648 assert(Args.size() == 2);
5649 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
5650 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
5651 if (!this->emitFlip(Arg2T, Arg1T, E))
5652 return false;
5653 }
5654
5655 if (FuncDecl) {
5656 const Function *Func = getFunction(FD: FuncDecl);
5657 if (!Func)
5658 return false;
5659
5660 // In error cases, the function may be called with fewer arguments than
5661 // parameters.
5662 if (E->getNumArgs() < Func->getNumWrittenParams())
5663 return false;
5664
5665 assert(HasRVO == Func->hasRVO());
5666
5667 bool HasQualifier = false;
5668 if (const auto *ME = dyn_cast<MemberExpr>(Val: E->getCallee()))
5669 HasQualifier = ME->hasQualifier();
5670
5671 bool IsVirtual = false;
5672 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FuncDecl))
5673 IsVirtual = !Devirtualized && MD->isVirtual();
5674
5675 // In any case call the function. The return value will end up on the stack
5676 // and if the function has RVO, we already have the pointer on the stack to
5677 // write the result into.
5678 if (IsVirtual && !HasQualifier) {
5679 uint32_t VarArgSize = 0;
5680 unsigned NumParams =
5681 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(Val: E);
5682 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5683 VarArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr)));
5684
5685 if (!this->emitCallVirt(Func, VarArgSize, E))
5686 return false;
5687 } else if (Func->isVariadic()) {
5688 uint32_t VarArgSize = 0;
5689 unsigned NumParams =
5690 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(Val: E);
5691 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5692 VarArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr)));
5693 if (!this->emitCallVar(Func, VarArgSize, E))
5694 return false;
5695 } else {
5696 if (!this->emitCall(Func, 0, E))
5697 return false;
5698 }
5699 } else {
5700 // Indirect call. Visit the callee, which will leave a FunctionPointer on
5701 // the stack. Cleanup of the returned value if necessary will be done after
5702 // the function call completed.
5703
5704 // Sum the size of all args from the call expr.
5705 uint32_t ArgSize = 0;
5706 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
5707 ArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr)));
5708
5709 // Get the callee, either from a member pointer or function pointer saved in
5710 // CalleeOffset.
5711 if (isa<CXXMemberCallExpr>(Val: E) && CalleeOffset) {
5712 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5713 return false;
5714 if (!this->emitGetMemberPtrDecl(E))
5715 return false;
5716 } else {
5717 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
5718 return false;
5719 }
5720 if (!this->emitCallPtr(ArgSize, E, E))
5721 return false;
5722 }
5723
5724 // Cleanup for discarded return values.
5725 if (DiscardResult && !ReturnType->isVoidType() && T)
5726 return this->emitPop(*T, E) && CallScope.destroyLocals();
5727
5728 return CallScope.destroyLocals();
5729}
5730
5731template <class Emitter>
5732bool Compiler<Emitter>::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
5733 SourceLocScope<Emitter> SLS(this, E);
5734
5735 return this->delegate(E->getExpr());
5736}
5737
5738template <class Emitter>
5739bool Compiler<Emitter>::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
5740 SourceLocScope<Emitter> SLS(this, E);
5741
5742 return this->delegate(E->getExpr());
5743}
5744
5745template <class Emitter>
5746bool Compiler<Emitter>::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
5747 if (DiscardResult)
5748 return true;
5749
5750 return this->emitConstBool(E->getValue(), E);
5751}
5752
5753template <class Emitter>
5754bool Compiler<Emitter>::VisitCXXNullPtrLiteralExpr(
5755 const CXXNullPtrLiteralExpr *E) {
5756 if (DiscardResult)
5757 return true;
5758
5759 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(QT: E->getType());
5760 return this->emitNullPtr(Val, nullptr, E);
5761}
5762
5763template <class Emitter>
5764bool Compiler<Emitter>::VisitGNUNullExpr(const GNUNullExpr *E) {
5765 if (DiscardResult)
5766 return true;
5767
5768 assert(E->getType()->isIntegerType());
5769
5770 PrimType T = classifyPrim(E->getType());
5771 return this->emitZero(T, E);
5772}
5773
5774template <class Emitter>
5775bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
5776 if (DiscardResult)
5777 return true;
5778
5779 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
5780 if (this->LambdaThisCapture.Offset > 0) {
5781 if (this->LambdaThisCapture.IsPtr)
5782 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
5783 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
5784 }
5785 }
5786
5787 // In some circumstances, the 'this' pointer does not actually refer to the
5788 // instance pointer of the current function frame, but e.g. to the declaration
5789 // currently being initialized. Here we emit the necessary instruction(s) for
5790 // this scenario.
5791 if (!InitStackActive || InitStack.empty())
5792 return this->emitThis(E);
5793
5794 // If our init stack is, for example:
5795 // 0 Stack: 3 (decl)
5796 // 1 Stack: 6 (init list)
5797 // 2 Stack: 1 (field)
5798 // 3 Stack: 6 (init list)
5799 // 4 Stack: 1 (field)
5800 //
5801 // We want to find the LAST element in it that's an init list,
5802 // which is marked with the K_InitList marker. The index right
5803 // before that points to an init list. We need to find the
5804 // elements before the K_InitList element that point to a base
5805 // (e.g. a decl or This), optionally followed by field, elem, etc.
5806 // In the example above, we want to emit elements [0..2].
5807 unsigned StartIndex = 0;
5808 unsigned EndIndex = 0;
5809 // Find the init list.
5810 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
5811 if (InitStack[StartIndex].Kind == InitLink::K_DIE) {
5812 EndIndex = StartIndex;
5813 --StartIndex;
5814 break;
5815 }
5816 }
5817
5818 // Walk backwards to find the base.
5819 for (; StartIndex > 0; --StartIndex) {
5820 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
5821 continue;
5822
5823 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
5824 InitStack[StartIndex].Kind != InitLink::K_Elem &&
5825 InitStack[StartIndex].Kind != InitLink::K_DIE)
5826 break;
5827 }
5828
5829 if (StartIndex == 0 && EndIndex == 0)
5830 EndIndex = InitStack.size() - 1;
5831
5832 assert(StartIndex < EndIndex);
5833
5834 // Emit the instructions.
5835 for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
5836 if (InitStack[I].Kind == InitLink::K_InitList ||
5837 InitStack[I].Kind == InitLink::K_DIE)
5838 continue;
5839 if (!InitStack[I].template emit<Emitter>(this, E))
5840 return false;
5841 }
5842 return true;
5843}
5844
5845template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
5846 switch (S->getStmtClass()) {
5847 case Stmt::CompoundStmtClass:
5848 return visitCompoundStmt(S: cast<CompoundStmt>(Val: S));
5849 case Stmt::DeclStmtClass:
5850 return visitDeclStmt(DS: cast<DeclStmt>(Val: S), /*EvaluateConditionDecl=*/true);
5851 case Stmt::ReturnStmtClass:
5852 return visitReturnStmt(RS: cast<ReturnStmt>(Val: S));
5853 case Stmt::IfStmtClass:
5854 return visitIfStmt(IS: cast<IfStmt>(Val: S));
5855 case Stmt::WhileStmtClass:
5856 return visitWhileStmt(S: cast<WhileStmt>(Val: S));
5857 case Stmt::DoStmtClass:
5858 return visitDoStmt(S: cast<DoStmt>(Val: S));
5859 case Stmt::ForStmtClass:
5860 return visitForStmt(S: cast<ForStmt>(Val: S));
5861 case Stmt::CXXForRangeStmtClass:
5862 return visitCXXForRangeStmt(S: cast<CXXForRangeStmt>(Val: S));
5863 case Stmt::BreakStmtClass:
5864 return visitBreakStmt(S: cast<BreakStmt>(Val: S));
5865 case Stmt::ContinueStmtClass:
5866 return visitContinueStmt(S: cast<ContinueStmt>(Val: S));
5867 case Stmt::SwitchStmtClass:
5868 return visitSwitchStmt(S: cast<SwitchStmt>(Val: S));
5869 case Stmt::CaseStmtClass:
5870 return visitCaseStmt(S: cast<CaseStmt>(Val: S));
5871 case Stmt::DefaultStmtClass:
5872 return visitDefaultStmt(S: cast<DefaultStmt>(Val: S));
5873 case Stmt::AttributedStmtClass:
5874 return visitAttributedStmt(S: cast<AttributedStmt>(Val: S));
5875 case Stmt::CXXTryStmtClass:
5876 return visitCXXTryStmt(S: cast<CXXTryStmt>(Val: S));
5877 case Stmt::NullStmtClass:
5878 return true;
5879 // Always invalid statements.
5880 case Stmt::GCCAsmStmtClass:
5881 case Stmt::MSAsmStmtClass:
5882 case Stmt::GotoStmtClass:
5883 return this->emitInvalid(S);
5884 case Stmt::LabelStmtClass:
5885 return this->visitStmt(cast<LabelStmt>(Val: S)->getSubStmt());
5886 default: {
5887 if (const auto *E = dyn_cast<Expr>(Val: S))
5888 return this->discard(E);
5889 return false;
5890 }
5891 }
5892}
5893
5894template <class Emitter>
5895bool Compiler<Emitter>::visitCompoundStmt(const CompoundStmt *S) {
5896 LocalScope<Emitter> Scope(this);
5897 for (const auto *InnerStmt : S->body())
5898 if (!visitStmt(S: InnerStmt))
5899 return false;
5900 return Scope.destroyLocals();
5901}
5902
5903template <class Emitter>
5904bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
5905 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) {
5906 for (auto *BD : DD->flat_bindings())
5907 if (auto *KD = BD->getHoldingVar();
5908 KD && !this->visitVarDecl(KD, KD->getInit()))
5909 return false;
5910 }
5911 return true;
5912}
5913
5914static bool hasTrivialDefaultCtorParent(const FieldDecl *FD) {
5915 assert(FD);
5916 assert(FD->getParent()->isUnion());
5917 const CXXRecordDecl *CXXRD =
5918 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
5919 return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
5920}
5921
5922template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
5923 for (;;) {
5924 if (const auto *ME = dyn_cast<MemberExpr>(Val: E)) {
5925 if (const auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
5926 FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
5927 return true;
5928 E = ME->getBase();
5929 continue;
5930 }
5931
5932 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) {
5933 E = ASE->getBase()->IgnoreImplicit();
5934 continue;
5935 }
5936
5937 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E);
5938 ICE && (ICE->getCastKind() == CK_NoOp ||
5939 ICE->getCastKind() == CK_DerivedToBase ||
5940 ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
5941 E = ICE->getSubExpr();
5942 continue;
5943 }
5944
5945 if (const auto *This = dyn_cast<CXXThisExpr>(Val: E)) {
5946 const auto *ThisRecord =
5947 This->getType()->getPointeeType()->getAsRecordDecl();
5948 if (!ThisRecord->isUnion())
5949 return false;
5950 // Otherwise, always activate if we're in the ctor.
5951 if (const auto *Ctor =
5952 dyn_cast_if_present<CXXConstructorDecl>(Val: CompilingFunction))
5953 return Ctor->getParent() == ThisRecord;
5954 return false;
5955 }
5956
5957 break;
5958 }
5959 return false;
5960}
5961
5962template <class Emitter>
5963bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS,
5964 bool EvaluateConditionDecl) {
5965 for (const auto *D : DS->decls()) {
5966 if (isa<StaticAssertDecl, TagDecl, TypedefNameDecl, BaseUsingDecl,
5967 FunctionDecl, NamespaceAliasDecl, UsingDirectiveDecl>(Val: D))
5968 continue;
5969
5970 const auto *VD = dyn_cast<VarDecl>(Val: D);
5971 if (!VD)
5972 return false;
5973 if (!this->visitVarDecl(VD, VD->getInit()))
5974 return false;
5975
5976 // Register decomposition decl holding vars.
5977 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
5978 return false;
5979 }
5980
5981 return true;
5982}
5983
5984template <class Emitter>
5985bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
5986 if (this->InStmtExpr)
5987 return this->emitUnsupported(RS);
5988
5989 if (const Expr *RE = RS->getRetValue()) {
5990 LocalScope<Emitter> RetScope(this);
5991 if (ReturnType) {
5992 // Primitive types are simply returned.
5993 if (!this->visit(RE))
5994 return false;
5995 this->emitCleanup();
5996 return this->emitRet(*ReturnType, RS);
5997 }
5998
5999 if (RE->getType()->isVoidType()) {
6000 if (!this->visit(RE))
6001 return false;
6002 } else {
6003 if (RE->containsErrors())
6004 return false;
6005
6006 InitLinkScope<Emitter> ILS(this, InitLink::RVO());
6007 // RVO - construct the value in the return location.
6008 if (!this->emitRVOPtr(RE))
6009 return false;
6010 if (!this->visitInitializer(RE))
6011 return false;
6012 if (!this->emitPopPtr(RE))
6013 return false;
6014
6015 this->emitCleanup();
6016 return this->emitRetVoid(RS);
6017 }
6018 }
6019
6020 // Void return.
6021 this->emitCleanup();
6022 return this->emitRetVoid(RS);
6023}
6024
6025template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
6026 LocalScope<Emitter> IfScope(this);
6027
6028 auto visitChildStmt = [&](const Stmt *S) -> bool {
6029 LocalScope<Emitter> SScope(this);
6030 if (!visitStmt(S))
6031 return false;
6032 return SScope.destroyLocals();
6033 };
6034
6035 if (auto *CondInit = IS->getInit()) {
6036 if (!visitStmt(S: CondInit))
6037 return false;
6038 }
6039
6040 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) {
6041 if (!visitDeclStmt(DS: CondDecl))
6042 return false;
6043 }
6044
6045 // Save ourselves compiling some code and the jumps, etc. if the condition is
6046 // stataically known to be either true or false. We could look at more cases
6047 // here, but I think all the ones that actually happen are using a
6048 // ConstantExpr.
6049 if (std::optional<bool> BoolValue = getBoolValue(E: IS->getCond())) {
6050 if (*BoolValue)
6051 return visitChildStmt(IS->getThen());
6052 if (const Stmt *Else = IS->getElse())
6053 return visitChildStmt(Else);
6054 return true;
6055 }
6056
6057 // Otherwise, compile the condition.
6058 if (IS->isNonNegatedConsteval()) {
6059 if (!this->emitIsConstantContext(IS))
6060 return false;
6061 } else if (IS->isNegatedConsteval()) {
6062 if (!this->emitIsConstantContext(IS))
6063 return false;
6064 if (!this->emitInv(IS))
6065 return false;
6066 } else {
6067 LocalScope<Emitter> CondScope(this, ScopeKind::FullExpression);
6068 if (!this->visitBool(IS->getCond()))
6069 return false;
6070 if (!CondScope.destroyLocals())
6071 return false;
6072 }
6073
6074 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
6075 return false;
6076
6077 if (const Stmt *Else = IS->getElse()) {
6078 LabelTy LabelElse = this->getLabel();
6079 LabelTy LabelEnd = this->getLabel();
6080 if (!this->jumpFalse(LabelElse))
6081 return false;
6082 if (!visitChildStmt(IS->getThen()))
6083 return false;
6084 if (!this->jump(LabelEnd))
6085 return false;
6086 this->emitLabel(LabelElse);
6087 if (!visitChildStmt(Else))
6088 return false;
6089 this->emitLabel(LabelEnd);
6090 } else {
6091 LabelTy LabelEnd = this->getLabel();
6092 if (!this->jumpFalse(LabelEnd))
6093 return false;
6094 if (!visitChildStmt(IS->getThen()))
6095 return false;
6096 this->emitLabel(LabelEnd);
6097 }
6098
6099 if (!IfScope.destroyLocals())
6100 return false;
6101
6102 return true;
6103}
6104
6105template <class Emitter>
6106bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
6107 const Expr *Cond = S->getCond();
6108 const Stmt *Body = S->getBody();
6109
6110 LabelTy CondLabel = this->getLabel(); // Label before the condition.
6111 LabelTy EndLabel = this->getLabel(); // Label after the loop.
6112 LocalScope<Emitter> WholeLoopScope(this);
6113 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6114
6115 this->fallthrough(CondLabel);
6116 this->emitLabel(CondLabel);
6117
6118 // Start of the loop body {
6119 LocalScope<Emitter> CondScope(this);
6120
6121 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6122 if (!visitDeclStmt(DS: CondDecl))
6123 return false;
6124 }
6125
6126 if (!this->visitBool(Cond))
6127 return false;
6128
6129 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6130 return false;
6131
6132 if (!this->jumpFalse(EndLabel))
6133 return false;
6134
6135 if (!this->visitStmt(Body))
6136 return false;
6137
6138 if (!CondScope.destroyLocals())
6139 return false;
6140 // } End of loop body.
6141
6142 if (!this->jump(CondLabel))
6143 return false;
6144 this->fallthrough(EndLabel);
6145 this->emitLabel(EndLabel);
6146
6147 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6148}
6149
6150template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
6151 const Expr *Cond = S->getCond();
6152 const Stmt *Body = S->getBody();
6153
6154 LabelTy StartLabel = this->getLabel();
6155 LabelTy EndLabel = this->getLabel();
6156 LabelTy CondLabel = this->getLabel();
6157 LocalScope<Emitter> WholeLoopScope(this);
6158 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6159
6160 this->fallthrough(StartLabel);
6161 this->emitLabel(StartLabel);
6162
6163 {
6164 LocalScope<Emitter> CondScope(this);
6165 if (!this->visitStmt(Body))
6166 return false;
6167 this->fallthrough(CondLabel);
6168 this->emitLabel(CondLabel);
6169 if (!this->visitBool(Cond))
6170 return false;
6171
6172 if (!CondScope.destroyLocals())
6173 return false;
6174 }
6175 if (!this->jumpTrue(StartLabel))
6176 return false;
6177
6178 this->fallthrough(EndLabel);
6179 this->emitLabel(EndLabel);
6180 return WholeLoopScope.destroyLocals();
6181}
6182
6183template <class Emitter>
6184bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
6185 // for (Init; Cond; Inc) { Body }
6186 const Stmt *Init = S->getInit();
6187 const Expr *Cond = S->getCond();
6188 const Expr *Inc = S->getInc();
6189 const Stmt *Body = S->getBody();
6190
6191 LabelTy EndLabel = this->getLabel();
6192 LabelTy CondLabel = this->getLabel();
6193 LabelTy IncLabel = this->getLabel();
6194
6195 LocalScope<Emitter> WholeLoopScope(this);
6196 if (Init && !this->visitStmt(Init))
6197 return false;
6198
6199 // Start of the loop body {
6200 this->fallthrough(CondLabel);
6201 this->emitLabel(CondLabel);
6202
6203 LocalScope<Emitter> CondScope(this);
6204 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6205 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6206 if (!visitDeclStmt(DS: CondDecl))
6207 return false;
6208 }
6209
6210 if (Cond) {
6211 if (!this->visitBool(Cond))
6212 return false;
6213 if (!this->jumpFalse(EndLabel))
6214 return false;
6215 }
6216 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6217 return false;
6218
6219 if (Body && !this->visitStmt(Body))
6220 return false;
6221
6222 this->fallthrough(IncLabel);
6223 this->emitLabel(IncLabel);
6224 if (Inc && !this->discard(Inc))
6225 return false;
6226
6227 if (!CondScope.destroyLocals())
6228 return false;
6229 if (!this->jump(CondLabel))
6230 return false;
6231 // } End of loop body.
6232
6233 this->emitLabel(EndLabel);
6234 // If we jumped out of the loop above, we still need to clean up the condition
6235 // scope.
6236 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6237}
6238
6239template <class Emitter>
6240bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
6241 const Stmt *Init = S->getInit();
6242 const Expr *Cond = S->getCond();
6243 const Expr *Inc = S->getInc();
6244 const Stmt *Body = S->getBody();
6245 const Stmt *BeginStmt = S->getBeginStmt();
6246 const Stmt *RangeStmt = S->getRangeStmt();
6247 const Stmt *EndStmt = S->getEndStmt();
6248
6249 LabelTy EndLabel = this->getLabel();
6250 LabelTy CondLabel = this->getLabel();
6251 LabelTy IncLabel = this->getLabel();
6252 LocalScope<Emitter> WholeLoopScope(this);
6253 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6254
6255 // Emit declarations needed in the loop.
6256 if (Init && !this->visitStmt(Init))
6257 return false;
6258 if (!this->visitStmt(RangeStmt))
6259 return false;
6260 if (!this->visitStmt(BeginStmt))
6261 return false;
6262 if (!this->visitStmt(EndStmt))
6263 return false;
6264
6265 // Now the condition as well as the loop variable assignment.
6266 this->fallthrough(CondLabel);
6267 this->emitLabel(CondLabel);
6268 if (!this->visitBool(Cond))
6269 return false;
6270 if (!this->jumpFalse(EndLabel))
6271 return false;
6272
6273 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
6274 return false;
6275
6276 // Body.
6277 {
6278 if (!this->visitStmt(Body))
6279 return false;
6280
6281 this->fallthrough(IncLabel);
6282 this->emitLabel(IncLabel);
6283 if (!this->discard(Inc))
6284 return false;
6285 }
6286
6287 if (!this->jump(CondLabel))
6288 return false;
6289
6290 this->fallthrough(EndLabel);
6291 this->emitLabel(EndLabel);
6292 return WholeLoopScope.destroyLocals();
6293}
6294
6295template <class Emitter>
6296bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) {
6297 if (LabelInfoStack.empty())
6298 return false;
6299
6300 OptLabelTy TargetLabel = std::nullopt;
6301 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6302 const VariableScope<Emitter> *BreakScope = nullptr;
6303
6304 if (!TargetLoop) {
6305 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6306 if (LI.BreakLabel) {
6307 TargetLabel = *LI.BreakLabel;
6308 BreakScope = LI.BreakOrContinueScope;
6309 break;
6310 }
6311 }
6312 } else {
6313 for (auto LI : LabelInfoStack) {
6314 if (LI.Name == TargetLoop) {
6315 TargetLabel = *LI.BreakLabel;
6316 BreakScope = LI.BreakOrContinueScope;
6317 break;
6318 }
6319 }
6320 }
6321
6322 // Faulty break statement (e.g. label redefined or named loops disabled).
6323 if (!TargetLabel)
6324 return false;
6325
6326 for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope;
6327 C = C->getParent()) {
6328 if (!C->destroyLocals())
6329 return false;
6330 }
6331
6332 return this->jump(*TargetLabel);
6333}
6334
6335template <class Emitter>
6336bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) {
6337 if (LabelInfoStack.empty())
6338 return false;
6339
6340 OptLabelTy TargetLabel = std::nullopt;
6341 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6342 const VariableScope<Emitter> *ContinueScope = nullptr;
6343
6344 if (!TargetLoop) {
6345 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6346 if (LI.ContinueLabel) {
6347 TargetLabel = *LI.ContinueLabel;
6348 ContinueScope = LI.BreakOrContinueScope;
6349 break;
6350 }
6351 }
6352 } else {
6353 for (auto LI : LabelInfoStack) {
6354 if (LI.Name == TargetLoop) {
6355 TargetLabel = *LI.ContinueLabel;
6356 ContinueScope = LI.BreakOrContinueScope;
6357 break;
6358 }
6359 }
6360 }
6361 assert(TargetLabel);
6362
6363 for (VariableScope<Emitter> *C = VarScope; C != ContinueScope;
6364 C = C->getParent()) {
6365 if (!C->destroyLocals())
6366 return false;
6367 }
6368
6369 return this->jump(*TargetLabel);
6370}
6371
6372template <class Emitter>
6373bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
6374 const Expr *Cond = S->getCond();
6375 if (Cond->containsErrors())
6376 return false;
6377
6378 PrimType CondT = this->classifyPrim(Cond->getType());
6379 LocalScope<Emitter> LS(this);
6380
6381 LabelTy EndLabel = this->getLabel();
6382 UnsignedOrNone DefaultLabel = std::nullopt;
6383 unsigned CondVar =
6384 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
6385
6386 if (const auto *CondInit = S->getInit())
6387 if (!visitStmt(S: CondInit))
6388 return false;
6389
6390 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
6391 if (!visitDeclStmt(DS: CondDecl))
6392 return false;
6393
6394 // Initialize condition variable.
6395 if (!this->visit(Cond))
6396 return false;
6397 if (!this->emitSetLocal(CondT, CondVar, S))
6398 return false;
6399
6400 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6401 return false;
6402
6403 CaseMap CaseLabels;
6404 // Create labels and comparison ops for all case statements.
6405 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
6406 SC = SC->getNextSwitchCase()) {
6407 if (const auto *CS = dyn_cast<CaseStmt>(Val: SC)) {
6408 CaseLabels[SC] = this->getLabel();
6409
6410 if (CS->caseStmtIsGNURange()) {
6411 LabelTy EndOfRangeCheck = this->getLabel();
6412 const Expr *Low = CS->getLHS();
6413 const Expr *High = CS->getRHS();
6414 if (Low->isValueDependent() || High->isValueDependent())
6415 return false;
6416
6417 if (!this->emitGetLocal(CondT, CondVar, CS))
6418 return false;
6419 if (!this->visit(Low))
6420 return false;
6421 PrimType LT = this->classifyPrim(Low->getType());
6422 if (!this->emitGE(LT, S))
6423 return false;
6424 if (!this->jumpFalse(EndOfRangeCheck))
6425 return false;
6426
6427 if (!this->emitGetLocal(CondT, CondVar, CS))
6428 return false;
6429 if (!this->visit(High))
6430 return false;
6431 PrimType HT = this->classifyPrim(High->getType());
6432 if (!this->emitLE(HT, S))
6433 return false;
6434 if (!this->jumpTrue(CaseLabels[CS]))
6435 return false;
6436 this->emitLabel(EndOfRangeCheck);
6437 continue;
6438 }
6439
6440 const Expr *Value = CS->getLHS();
6441 if (Value->isValueDependent())
6442 return false;
6443 PrimType ValueT = this->classifyPrim(Value->getType());
6444
6445 // Compare the case statement's value to the switch condition.
6446 if (!this->emitGetLocal(CondT, CondVar, CS))
6447 return false;
6448 if (!this->visit(Value))
6449 return false;
6450
6451 // Compare and jump to the case label.
6452 if (!this->emitEQ(ValueT, S))
6453 return false;
6454 if (!this->jumpTrue(CaseLabels[CS]))
6455 return false;
6456 } else {
6457 assert(!DefaultLabel);
6458 DefaultLabel = this->getLabel();
6459 }
6460 }
6461
6462 // If none of the conditions above were true, fall through to the default
6463 // statement or jump after the switch statement.
6464 if (DefaultLabel) {
6465 if (!this->jump(*DefaultLabel))
6466 return false;
6467 } else {
6468 if (!this->jump(EndLabel))
6469 return false;
6470 }
6471
6472 SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel,
6473 DefaultLabel);
6474 if (!this->visitStmt(S->getBody()))
6475 return false;
6476 this->fallthrough(EndLabel);
6477 this->emitLabel(EndLabel);
6478
6479 return LS.destroyLocals();
6480}
6481
6482template <class Emitter>
6483bool Compiler<Emitter>::visitCaseStmt(const CaseStmt *S) {
6484 this->fallthrough(CaseLabels[S]);
6485 this->emitLabel(CaseLabels[S]);
6486 return this->visitStmt(S->getSubStmt());
6487}
6488
6489template <class Emitter>
6490bool Compiler<Emitter>::visitDefaultStmt(const DefaultStmt *S) {
6491 if (LabelInfoStack.empty())
6492 return false;
6493
6494 LabelTy DefaultLabel;
6495 for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) {
6496 if (LI.DefaultLabel) {
6497 DefaultLabel = *LI.DefaultLabel;
6498 break;
6499 }
6500 }
6501
6502 this->emitLabel(DefaultLabel);
6503 return this->visitStmt(S->getSubStmt());
6504}
6505
6506template <class Emitter>
6507bool Compiler<Emitter>::visitAttributedStmt(const AttributedStmt *S) {
6508 const Stmt *SubStmt = S->getSubStmt();
6509
6510 bool IsMSVCConstexprAttr = isa<ReturnStmt>(Val: SubStmt) &&
6511 hasSpecificAttr<MSConstexprAttr>(container: S->getAttrs());
6512
6513 if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S))
6514 return false;
6515
6516 if (this->Ctx.getLangOpts().CXXAssumptions &&
6517 !this->Ctx.getLangOpts().MSVCCompat) {
6518 for (const Attr *A : S->getAttrs()) {
6519 auto *AA = dyn_cast<CXXAssumeAttr>(Val: A);
6520 if (!AA)
6521 continue;
6522
6523 assert(isa<NullStmt>(SubStmt));
6524
6525 const Expr *Assumption = AA->getAssumption();
6526 if (Assumption->isValueDependent())
6527 return false;
6528
6529 if (Assumption->HasSideEffects(Ctx: this->Ctx.getASTContext()))
6530 continue;
6531
6532 // Evaluate assumption.
6533 if (!this->visitBool(Assumption))
6534 return false;
6535
6536 if (!this->emitAssume(Assumption))
6537 return false;
6538 }
6539 }
6540
6541 // Ignore other attributes.
6542 if (!this->visitStmt(SubStmt))
6543 return false;
6544
6545 if (IsMSVCConstexprAttr)
6546 return this->emitPopMSVCCE(S);
6547 return true;
6548}
6549
6550template <class Emitter>
6551bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) {
6552 // Ignore all handlers.
6553 return this->visitStmt(S->getTryBlock());
6554}
6555
6556template <class Emitter>
6557bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
6558 assert(MD->isLambdaStaticInvoker());
6559 assert(MD->hasBody());
6560 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
6561
6562 const CXXRecordDecl *ClosureClass = MD->getParent();
6563 const FunctionDecl *LambdaCallOp;
6564 assert(ClosureClass->captures().empty());
6565 if (ClosureClass->isGenericLambda()) {
6566 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6567 assert(MD->isFunctionTemplateSpecialization() &&
6568 "A generic lambda's static-invoker function must be a "
6569 "template specialization");
6570 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
6571 FunctionTemplateDecl *CallOpTemplate =
6572 LambdaCallOp->getDescribedFunctionTemplate();
6573 void *InsertPos = nullptr;
6574 const FunctionDecl *CorrespondingCallOpSpecialization =
6575 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos);
6576 assert(CorrespondingCallOpSpecialization);
6577 LambdaCallOp = CorrespondingCallOpSpecialization;
6578 } else {
6579 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6580 }
6581 assert(ClosureClass->captures().empty());
6582 const Function *Func = this->getFunction(LambdaCallOp);
6583 if (!Func)
6584 return false;
6585 assert(Func->hasThisPointer());
6586 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
6587
6588 if (Func->hasRVO()) {
6589 if (!this->emitRVOPtr(MD))
6590 return false;
6591 }
6592
6593 // The lambda call operator needs an instance pointer, but we don't have
6594 // one here, and we don't need one either because the lambda cannot have
6595 // any captures, as verified above. Emit a null pointer. This is then
6596 // special-cased when interpreting to not emit any misleading diagnostics.
6597 if (!this->emitNullPtr(0, nullptr, MD))
6598 return false;
6599
6600 // Forward all arguments from the static invoker to the lambda call operator.
6601 for (const ParmVarDecl *PVD : MD->parameters()) {
6602 auto It = this->Params.find(PVD);
6603 assert(It != this->Params.end());
6604
6605 // We do the lvalue-to-rvalue conversion manually here, so no need
6606 // to care about references.
6607 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
6608 if (!this->emitGetParam(ParamType, It->second.Offset, MD))
6609 return false;
6610 }
6611
6612 if (!this->emitCall(Func, 0, LambdaCallOp))
6613 return false;
6614
6615 this->emitCleanup();
6616 if (ReturnType)
6617 return this->emitRet(*ReturnType, MD);
6618
6619 // Nothing to do, since we emitted the RVO pointer above.
6620 return this->emitRetVoid(MD);
6621}
6622
6623template <class Emitter>
6624bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
6625 if (Ctx.getLangOpts().CPlusPlus23)
6626 return true;
6627
6628 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Ctx.getASTContext()))
6629 return true;
6630
6631 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
6632}
6633
6634static bool initNeedsOverridenLoc(const CXXCtorInitializer *Init) {
6635 const Expr *InitExpr = Init->getInit();
6636
6637 if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
6638 !isa<CXXConstructExpr>(Val: InitExpr))
6639 return true;
6640
6641 if (const auto *CE = dyn_cast<CXXConstructExpr>(Val: InitExpr)) {
6642 const CXXConstructorDecl *Ctor = CE->getConstructor();
6643 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
6644 Ctor->isTrivial())
6645 return true;
6646 }
6647
6648 return false;
6649}
6650
6651template <class Emitter>
6652bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
6653 assert(!ReturnType);
6654
6655 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
6656 const Expr *InitExpr,
6657 bool Activate = false) -> bool {
6658 // We don't know what to do with these, so just return false.
6659 if (InitExpr->getType().isNull())
6660 return false;
6661
6662 if (OptPrimType T = this->classify(InitExpr)) {
6663 if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
6664 return false;
6665
6666 if (!this->visit(InitExpr))
6667 return false;
6668
6669 bool BitField = F->isBitField();
6670 if (BitField)
6671 return this->emitInitThisBitField(*T, F, FieldOffset, InitExpr);
6672 return this->emitInitThisField(*T, FieldOffset, InitExpr);
6673 }
6674 // Non-primitive case. Get a pointer to the field-to-initialize
6675 // on the stack and call visitInitialzer() for it.
6676 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(Offset: F->Offset));
6677 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
6678 return false;
6679
6680 if (Activate && !this->emitActivate(InitExpr))
6681 return false;
6682
6683 if (!this->visitInitializer(InitExpr))
6684 return false;
6685
6686 return this->emitFinishInitPop(InitExpr);
6687 };
6688
6689 const RecordDecl *RD = Ctor->getParent();
6690 const Record *R = this->getRecord(RD);
6691 if (!R)
6692 return false;
6693 bool IsUnion = R->isUnion();
6694
6695 if (IsUnion && Ctor->isCopyOrMoveConstructor()) {
6696 LocOverrideScope<Emitter> LOS(this, SourceInfo{});
6697
6698 if (R->getNumFields() == 0)
6699 return this->emitRetVoid(Ctor);
6700 // union copy and move ctors are special.
6701 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
6702 if (!this->emitThis(Ctor))
6703 return false;
6704
6705 const ParmVarDecl *PVD = Ctor->getParamDecl(i: 0);
6706 ParamOffset PO = this->Params[PVD]; // Must exist.
6707
6708 if (!this->emitGetParam(PT_Ptr, PO.Offset, Ctor))
6709 return false;
6710
6711 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
6712 this->emitRetVoid(Ctor);
6713 }
6714
6715 InitLinkScope<Emitter> InitScope(this, InitLink::This());
6716 for (const auto *Init : Ctor->inits()) {
6717 // Scope needed for the initializers.
6718 LocalScope<Emitter> Scope(this, ScopeKind::FullExpression);
6719
6720 const Expr *InitExpr = Init->getInit();
6721 if (const FieldDecl *Member = Init->getMember()) {
6722 const Record::Field *F = R->getField(FD: Member);
6723
6724 LocOverrideScope<Emitter> LOS(this, SourceInfo{},
6725 initNeedsOverridenLoc(Init));
6726 if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
6727 return false;
6728 } else if (const Type *Base = Init->getBaseClass()) {
6729 const auto *BaseDecl = Base->getAsCXXRecordDecl();
6730 assert(BaseDecl);
6731
6732 if (Init->isBaseVirtual()) {
6733 assert(R->getVirtualBase(BaseDecl));
6734 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
6735 return false;
6736
6737 } else {
6738 // Base class initializer.
6739 // Get This Base and call initializer on it.
6740 const Record::Base *B = R->getBase(FD: BaseDecl);
6741 assert(B);
6742 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
6743 return false;
6744 }
6745
6746 if (IsUnion && !this->emitActivate(InitExpr))
6747 return false;
6748
6749 if (!this->visitInitializer(InitExpr))
6750 return false;
6751 if (!this->emitFinishInitPop(InitExpr))
6752 return false;
6753 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
6754 LocOverrideScope<Emitter> LOS(this, SourceInfo{},
6755 initNeedsOverridenLoc(Init));
6756 assert(IFD->getChainingSize() >= 2);
6757
6758 unsigned NestedFieldOffset = 0;
6759 const Record::Field *NestedField = nullptr;
6760 for (const NamedDecl *ND : IFD->chain()) {
6761 const auto *FD = cast<FieldDecl>(Val: ND);
6762 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6763 assert(FieldRecord);
6764
6765 NestedField = FieldRecord->getField(FD);
6766 assert(NestedField);
6767 IsUnion = IsUnion || FieldRecord->isUnion();
6768
6769 NestedFieldOffset += NestedField->Offset;
6770 }
6771 assert(NestedField);
6772
6773 unsigned FirstLinkOffset =
6774 R->getField(FD: cast<FieldDecl>(Val: IFD->chain()[0]))->Offset;
6775 InitLinkScope<Emitter> ILS(this, InitLink::Field(Offset: FirstLinkOffset));
6776 InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: InitExpr));
6777 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
6778 IsUnion))
6779 return false;
6780
6781 // Mark all chain links as initialized.
6782 unsigned InitFieldOffset = 0;
6783 for (const NamedDecl *ND : IFD->chain().drop_back()) {
6784 const auto *FD = cast<FieldDecl>(Val: ND);
6785 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6786 assert(FieldRecord);
6787 NestedField = FieldRecord->getField(FD);
6788 InitFieldOffset += NestedField->Offset;
6789 assert(NestedField);
6790 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
6791 return false;
6792 if (!this->emitFinishInitPop(InitExpr))
6793 return false;
6794 }
6795
6796 } else {
6797 assert(Init->isDelegatingInitializer());
6798 if (!this->emitThis(InitExpr))
6799 return false;
6800 if (!this->visitInitializer(Init->getInit()))
6801 return false;
6802 if (!this->emitPopPtr(InitExpr))
6803 return false;
6804 }
6805
6806 if (!Scope.destroyLocals())
6807 return false;
6808 }
6809
6810 if (const Stmt *Body = Ctor->getBody()) {
6811 // Only emit the CtorCheck op for non-empty CompoundStmt bodies.
6812 // For non-CompoundStmts, always assume they are non-empty and emit it.
6813 if (const auto *CS = dyn_cast<CompoundStmt>(Val: Body)) {
6814 if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{}))
6815 return false;
6816 } else {
6817 if (!this->emitCtorCheck(SourceInfo{}))
6818 return false;
6819 }
6820
6821 if (!visitStmt(S: Body))
6822 return false;
6823 }
6824
6825 return this->emitRetVoid(SourceInfo{});
6826}
6827
6828template <class Emitter>
6829bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
6830 const RecordDecl *RD = Dtor->getParent();
6831 const Record *R = this->getRecord(RD);
6832 if (!R)
6833 return false;
6834
6835 if (!Dtor->isTrivial() && Dtor->getBody()) {
6836 if (!this->visitStmt(Dtor->getBody()))
6837 return false;
6838 }
6839
6840 if (!this->emitThis(Dtor))
6841 return false;
6842
6843 if (!this->emitCheckDestruction(Dtor))
6844 return false;
6845
6846 assert(R);
6847 if (!R->isUnion()) {
6848
6849 LocOverrideScope<Emitter> LOS(this, SourceInfo{});
6850 // First, destroy all fields.
6851 for (const Record::Field &Field : llvm::reverse(C: R->fields())) {
6852 const Descriptor *D = Field.Desc;
6853 if (D->hasTrivialDtor())
6854 continue;
6855 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
6856 return false;
6857 if (!this->emitDestructionPop(D, SourceInfo{}))
6858 return false;
6859 }
6860 }
6861
6862 for (const Record::Base &Base : llvm::reverse(C: R->bases())) {
6863 if (Base.R->hasTrivialDtor())
6864 continue;
6865 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
6866 return false;
6867 if (!this->emitRecordDestructionPop(Base.R, {}))
6868 return false;
6869 }
6870
6871 // FIXME: Virtual bases.
6872 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
6873}
6874
6875template <class Emitter>
6876bool Compiler<Emitter>::compileUnionAssignmentOperator(
6877 const CXXMethodDecl *MD) {
6878 if (!this->emitThis(MD))
6879 return false;
6880
6881 const ParmVarDecl *PVD = MD->getParamDecl(i: 0);
6882 ParamOffset PO = this->Params[PVD]; // Must exist.
6883
6884 if (!this->emitGetParam(PT_Ptr, PO.Offset, MD))
6885 return false;
6886
6887 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
6888}
6889
6890template <class Emitter>
6891bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) {
6892 // Classify the return type.
6893 ReturnType = this->classify(F->getReturnType());
6894
6895 this->CompilingFunction = F;
6896
6897 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Val: F))
6898 return this->compileConstructor(Ctor);
6899 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(Val: F))
6900 return this->compileDestructor(Dtor);
6901
6902 // Emit custom code if this is a lambda static invoker.
6903 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: F)) {
6904 const RecordDecl *RD = MD->getParent();
6905
6906 if (RD->isUnion() &&
6907 (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()))
6908 return this->compileUnionAssignmentOperator(MD);
6909
6910 if (MD->isLambdaStaticInvoker())
6911 return this->emitLambdaStaticInvokerBody(MD);
6912 }
6913
6914 // Regular functions.
6915 if (const auto *Body = F->getBody())
6916 if (!visitStmt(S: Body))
6917 return false;
6918
6919 // Emit a guard return to protect against a code path missing one.
6920 if (F->getReturnType()->isVoidType())
6921 return this->emitRetVoid(SourceInfo{});
6922 return this->emitNoRet(SourceInfo{});
6923}
6924
6925static uint32_t getBitWidth(const Expr *E) {
6926 assert(E->refersToBitField());
6927 const auto *ME = cast<MemberExpr>(Val: E);
6928 const auto *FD = cast<FieldDecl>(Val: ME->getMemberDecl());
6929 return FD->getBitWidthValue();
6930}
6931
6932template <class Emitter>
6933bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
6934 const Expr *SubExpr = E->getSubExpr();
6935 if (SubExpr->getType()->isAnyComplexType())
6936 return this->VisitComplexUnaryOperator(E);
6937 if (SubExpr->getType()->isVectorType())
6938 return this->VisitVectorUnaryOperator(E);
6939 if (SubExpr->getType()->isFixedPointType())
6940 return this->VisitFixedPointUnaryOperator(E);
6941 OptPrimType T = classify(SubExpr->getType());
6942
6943 switch (E->getOpcode()) {
6944 case UO_PostInc: { // x++
6945 if (!Ctx.getLangOpts().CPlusPlus14)
6946 return this->emitInvalid(E);
6947 if (!T)
6948 return this->emitError(E);
6949
6950 if (!this->visit(SubExpr))
6951 return false;
6952
6953 if (T == PT_Ptr) {
6954 if (!this->emitIncPtr(E))
6955 return false;
6956
6957 return DiscardResult ? this->emitPopPtr(E) : true;
6958 }
6959
6960 if (T == PT_Float)
6961 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
6962 : this->emitIncf(getFPOptions(E), E);
6963
6964 if (SubExpr->refersToBitField())
6965 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
6966 getBitWidth(E: SubExpr), E)
6967 : this->emitIncBitfield(*T, E->canOverflow(),
6968 getBitWidth(E: SubExpr), E);
6969
6970 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
6971 : this->emitInc(*T, E->canOverflow(), E);
6972 }
6973 case UO_PostDec: { // x--
6974 if (!Ctx.getLangOpts().CPlusPlus14)
6975 return this->emitInvalid(E);
6976 if (!T)
6977 return this->emitError(E);
6978
6979 if (!this->visit(SubExpr))
6980 return false;
6981
6982 if (T == PT_Ptr) {
6983 if (!this->emitDecPtr(E))
6984 return false;
6985
6986 return DiscardResult ? this->emitPopPtr(E) : true;
6987 }
6988
6989 if (T == PT_Float)
6990 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
6991 : this->emitDecf(getFPOptions(E), E);
6992
6993 if (SubExpr->refersToBitField()) {
6994 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
6995 getBitWidth(E: SubExpr), E)
6996 : this->emitDecBitfield(*T, E->canOverflow(),
6997 getBitWidth(E: SubExpr), E);
6998 }
6999
7000 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
7001 : this->emitDec(*T, E->canOverflow(), E);
7002 }
7003 case UO_PreInc: { // ++x
7004 if (!Ctx.getLangOpts().CPlusPlus14)
7005 return this->emitInvalid(E);
7006 if (!T)
7007 return this->emitError(E);
7008
7009 if (!this->visit(SubExpr))
7010 return false;
7011
7012 if (T == PT_Ptr) {
7013 if (!this->emitLoadPtr(E))
7014 return false;
7015 if (!this->emitConstUint8(1, E))
7016 return false;
7017 if (!this->emitAddOffsetUint8(E))
7018 return false;
7019 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7020 }
7021
7022 // Post-inc and pre-inc are the same if the value is to be discarded.
7023 if (DiscardResult) {
7024 if (T == PT_Float)
7025 return this->emitIncfPop(getFPOptions(E), E);
7026 if (SubExpr->refersToBitField())
7027 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7028 getBitWidth(E: SubExpr), E)
7029 : this->emitIncBitfield(*T, E->canOverflow(),
7030 getBitWidth(E: SubExpr), E);
7031 return this->emitIncPop(*T, E->canOverflow(), E);
7032 }
7033
7034 if (T == PT_Float) {
7035 const auto &TargetSemantics = Ctx.getFloatSemantics(T: E->getType());
7036 if (!this->emitLoadFloat(E))
7037 return false;
7038 APFloat F(TargetSemantics, 1);
7039 if (!this->emitFloat(F, E))
7040 return false;
7041
7042 if (!this->emitAddf(getFPOptions(E), E))
7043 return false;
7044 if (!this->emitStoreFloat(E))
7045 return false;
7046 } else if (SubExpr->refersToBitField()) {
7047 assert(isIntegerOrBoolType(*T));
7048 if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(E: SubExpr),
7049 E))
7050 return false;
7051 } else {
7052 assert(isIntegerOrBoolType(*T));
7053 if (!this->emitPreInc(*T, E->canOverflow(), E))
7054 return false;
7055 }
7056 return E->isGLValue() || this->emitLoadPop(*T, E);
7057 }
7058 case UO_PreDec: { // --x
7059 if (!Ctx.getLangOpts().CPlusPlus14)
7060 return this->emitInvalid(E);
7061 if (!T)
7062 return this->emitError(E);
7063
7064 if (!this->visit(SubExpr))
7065 return false;
7066
7067 if (T == PT_Ptr) {
7068 if (!this->emitLoadPtr(E))
7069 return false;
7070 if (!this->emitConstUint8(1, E))
7071 return false;
7072 if (!this->emitSubOffsetUint8(E))
7073 return false;
7074 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7075 }
7076
7077 // Post-dec and pre-dec are the same if the value is to be discarded.
7078 if (DiscardResult) {
7079 if (T == PT_Float)
7080 return this->emitDecfPop(getFPOptions(E), E);
7081 if (SubExpr->refersToBitField())
7082 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7083 getBitWidth(E: SubExpr), E)
7084 : this->emitDecBitfield(*T, E->canOverflow(),
7085 getBitWidth(E: SubExpr), E);
7086 return this->emitDecPop(*T, E->canOverflow(), E);
7087 }
7088
7089 if (T == PT_Float) {
7090 const auto &TargetSemantics = Ctx.getFloatSemantics(T: E->getType());
7091 if (!this->emitLoadFloat(E))
7092 return false;
7093 APFloat F(TargetSemantics, 1);
7094 if (!this->emitFloat(F, E))
7095 return false;
7096
7097 if (!this->emitSubf(getFPOptions(E), E))
7098 return false;
7099 if (!this->emitStoreFloat(E))
7100 return false;
7101 } else if (SubExpr->refersToBitField()) {
7102 assert(isIntegerOrBoolType(*T));
7103 if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(E: SubExpr),
7104 E))
7105 return false;
7106 } else {
7107 assert(isIntegerOrBoolType(*T));
7108 if (!this->emitPreDec(*T, E->canOverflow(), E))
7109 return false;
7110 }
7111 return E->isGLValue() || this->emitLoadPop(*T, E);
7112 }
7113 case UO_LNot: // !x
7114 if (!T)
7115 return this->emitError(E);
7116
7117 if (DiscardResult)
7118 return this->discard(SubExpr);
7119
7120 if (!this->visitBool(SubExpr))
7121 return false;
7122
7123 if (!this->emitInv(E))
7124 return false;
7125
7126 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7127 return this->emitCast(PT_Bool, ET, E);
7128 return true;
7129 case UO_Minus: // -x
7130 if (!T)
7131 return this->emitError(E);
7132
7133 if (!this->visit(SubExpr))
7134 return false;
7135 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
7136 case UO_Plus: // +x
7137 if (!T)
7138 return this->emitError(E);
7139
7140 if (!this->visit(SubExpr)) // noop
7141 return false;
7142 return DiscardResult ? this->emitPop(*T, E) : true;
7143 case UO_AddrOf: // &x
7144 if (E->getType()->isMemberPointerType()) {
7145 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
7146 // member can be formed.
7147 if (DiscardResult)
7148 return true;
7149 return this->emitGetMemberPtr(cast<DeclRefExpr>(Val: SubExpr)->getDecl(), E);
7150 }
7151 // We should already have a pointer when we get here.
7152 return this->delegate(SubExpr);
7153 case UO_Deref: // *x
7154 if (DiscardResult)
7155 return this->discard(SubExpr);
7156
7157 if (!this->visit(SubExpr))
7158 return false;
7159
7160 if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
7161 return false;
7162
7163 if (classifyPrim(SubExpr) == PT_Ptr)
7164 return this->emitNarrowPtr(E);
7165 return true;
7166
7167 case UO_Not: // ~x
7168 if (!T)
7169 return this->emitError(E);
7170
7171 if (!this->visit(SubExpr))
7172 return false;
7173 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
7174 case UO_Real: // __real x
7175 if (!T)
7176 return false;
7177 return this->delegate(SubExpr);
7178 case UO_Imag: { // __imag x
7179 if (!T)
7180 return false;
7181 if (!this->discard(SubExpr))
7182 return false;
7183 return DiscardResult
7184 ? true
7185 : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
7186 }
7187 case UO_Extension:
7188 return this->delegate(SubExpr);
7189 case UO_Coawait:
7190 assert(false && "Unhandled opcode");
7191 }
7192
7193 return false;
7194}
7195
7196template <class Emitter>
7197bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) {
7198 const Expr *SubExpr = E->getSubExpr();
7199 assert(SubExpr->getType()->isAnyComplexType());
7200
7201 if (DiscardResult)
7202 return this->discard(SubExpr);
7203
7204 OptPrimType ResT = classify(E);
7205 auto prepareResult = [=]() -> bool {
7206 if (!ResT && !Initializing) {
7207 UnsignedOrNone LocalIndex = allocateLocal(Src: SubExpr);
7208 if (!LocalIndex)
7209 return false;
7210 return this->emitGetPtrLocal(*LocalIndex, E);
7211 }
7212
7213 return true;
7214 };
7215
7216 // The offset of the temporary, if we created one.
7217 unsigned SubExprOffset = ~0u;
7218 auto createTemp = [=, &SubExprOffset]() -> bool {
7219 SubExprOffset =
7220 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7221 if (!this->visit(SubExpr))
7222 return false;
7223 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
7224 };
7225
7226 PrimType ElemT = classifyComplexElementType(T: SubExpr->getType());
7227 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7228 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7229 return false;
7230 return this->emitArrayElemPop(ElemT, Index, E);
7231 };
7232
7233 switch (E->getOpcode()) {
7234 case UO_Minus: // -x
7235 if (!prepareResult())
7236 return false;
7237 if (!createTemp())
7238 return false;
7239 for (unsigned I = 0; I != 2; ++I) {
7240 if (!getElem(SubExprOffset, I))
7241 return false;
7242 if (!this->emitNeg(ElemT, E))
7243 return false;
7244 if (!this->emitInitElem(ElemT, I, E))
7245 return false;
7246 }
7247 break;
7248
7249 case UO_Plus: // +x
7250 case UO_AddrOf: // &x
7251 case UO_Deref: // *x
7252 return this->delegate(SubExpr);
7253
7254 case UO_LNot:
7255 if (!this->visit(SubExpr))
7256 return false;
7257 if (!this->emitComplexBoolCast(SubExpr))
7258 return false;
7259 if (!this->emitInv(E))
7260 return false;
7261 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7262 return this->emitCast(PT_Bool, ET, E);
7263 return true;
7264
7265 case UO_Real:
7266 return this->emitComplexReal(SubExpr);
7267
7268 case UO_Imag:
7269 if (!this->visit(SubExpr))
7270 return false;
7271
7272 if (SubExpr->isLValue()) {
7273 if (!this->emitConstUint8(1, E))
7274 return false;
7275 return this->emitArrayElemPtrPopUint8(E);
7276 }
7277
7278 // Since our _Complex implementation does not map to a primitive type,
7279 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
7280 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
7281
7282 case UO_Not: // ~x
7283 if (!this->delegate(SubExpr))
7284 return false;
7285 // Negate the imaginary component.
7286 if (!this->emitArrayElem(ElemT, 1, E))
7287 return false;
7288 if (!this->emitNeg(ElemT, E))
7289 return false;
7290 if (!this->emitInitElem(ElemT, 1, E))
7291 return false;
7292 return DiscardResult ? this->emitPopPtr(E) : true;
7293
7294 case UO_Extension:
7295 return this->delegate(SubExpr);
7296
7297 default:
7298 return this->emitInvalid(E);
7299 }
7300
7301 return true;
7302}
7303
7304template <class Emitter>
7305bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) {
7306 const Expr *SubExpr = E->getSubExpr();
7307 assert(SubExpr->getType()->isVectorType());
7308
7309 if (DiscardResult)
7310 return this->discard(SubExpr);
7311
7312 auto UnaryOp = E->getOpcode();
7313 if (UnaryOp == UO_Extension)
7314 return this->delegate(SubExpr);
7315
7316 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
7317 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
7318 return this->emitInvalid(E);
7319
7320 // Nothing to do here.
7321 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
7322 return this->delegate(SubExpr);
7323
7324 if (!Initializing) {
7325 UnsignedOrNone LocalIndex = allocateLocal(Src: SubExpr);
7326 if (!LocalIndex)
7327 return false;
7328 if (!this->emitGetPtrLocal(*LocalIndex, E))
7329 return false;
7330 }
7331
7332 // The offset of the temporary, if we created one.
7333 unsigned SubExprOffset =
7334 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7335 if (!this->visit(SubExpr))
7336 return false;
7337 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
7338 return false;
7339
7340 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
7341 PrimType ElemT = classifyVectorElementType(T: SubExpr->getType());
7342 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7343 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7344 return false;
7345 return this->emitArrayElemPop(ElemT, Index, E);
7346 };
7347
7348 switch (UnaryOp) {
7349 case UO_Minus:
7350 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7351 if (!getElem(SubExprOffset, I))
7352 return false;
7353 if (!this->emitNeg(ElemT, E))
7354 return false;
7355 if (!this->emitInitElem(ElemT, I, E))
7356 return false;
7357 }
7358 break;
7359 case UO_LNot: { // !x
7360 // In C++, the logic operators !, &&, || are available for vectors. !v is
7361 // equivalent to v == 0.
7362 //
7363 // The result of the comparison is a vector of the same width and number of
7364 // elements as the comparison operands with a signed integral element type.
7365 //
7366 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
7367 QualType ResultVecTy = E->getType();
7368 PrimType ResultVecElemT =
7369 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
7370 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7371 if (!getElem(SubExprOffset, I))
7372 return false;
7373 // operator ! on vectors returns -1 for 'truth', so negate it.
7374 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
7375 return false;
7376 if (!this->emitInv(E))
7377 return false;
7378 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
7379 return false;
7380 if (!this->emitNeg(ElemT, E))
7381 return false;
7382 if (ElemT != ResultVecElemT &&
7383 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
7384 return false;
7385 if (!this->emitInitElem(ResultVecElemT, I, E))
7386 return false;
7387 }
7388 break;
7389 }
7390 case UO_Not: // ~x
7391 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7392 if (!getElem(SubExprOffset, I))
7393 return false;
7394 if (ElemT == PT_Bool) {
7395 if (!this->emitInv(E))
7396 return false;
7397 } else {
7398 if (!this->emitComp(ElemT, E))
7399 return false;
7400 }
7401 if (!this->emitInitElem(ElemT, I, E))
7402 return false;
7403 }
7404 break;
7405 default:
7406 llvm_unreachable("Unsupported unary operators should be handled up front");
7407 }
7408 return true;
7409}
7410
7411template <class Emitter>
7412bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
7413 if (DiscardResult)
7414 return true;
7415
7416 if (const auto *ECD = dyn_cast<EnumConstantDecl>(Val: D))
7417 return this->emitConst(ECD->getInitVal(), E);
7418 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(Val: D)) {
7419 const Function *F = getFunction(FD: FuncDecl);
7420 return F && this->emitGetFnPtr(F, E);
7421 }
7422 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Val: D)) {
7423 if (UnsignedOrNone Index = P.getOrCreateGlobal(VD: D)) {
7424 if (!this->emitGetPtrGlobal(*Index, E))
7425 return false;
7426 if (OptPrimType T = classify(E->getType())) {
7427 if (!this->visitAPValue(TPOD->getValue(), *T, E))
7428 return false;
7429 return this->emitInitGlobal(*T, *Index, E);
7430 }
7431 if (!this->visitAPValueInitializer(TPOD->getValue(), E, TPOD->getType()))
7432 return false;
7433 return this->emitFinishInit(E);
7434 }
7435 return false;
7436 }
7437
7438 // References are implemented via pointers, so when we see a DeclRefExpr
7439 // pointing to a reference, we need to get its value directly (i.e. the
7440 // pointer to the actual value) instead of a pointer to the pointer to the
7441 // value.
7442 QualType DeclType = D->getType();
7443 bool IsReference = DeclType->isReferenceType();
7444
7445 // Function parameters.
7446 // Note that it's important to check them first since we might have a local
7447 // variable created for a ParmVarDecl as well.
7448 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: D)) {
7449 if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
7450 !DeclType->isIntegralOrEnumerationType()) {
7451 return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E),
7452 /*InitializerFailed=*/false, E);
7453 }
7454 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
7455 if (IsReference || !It->second.IsPtr)
7456 return this->emitGetParam(classifyPrim(E), It->second.Offset, E);
7457
7458 return this->emitGetPtrParam(It->second.Offset, E);
7459 }
7460
7461 if (!Ctx.getLangOpts().CPlusPlus23 && IsReference)
7462 return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E),
7463 /*InitializerFailed=*/false, E);
7464 }
7465 // Local variables.
7466 if (auto It = Locals.find(Val: D); It != Locals.end()) {
7467 const unsigned Offset = It->second.Offset;
7468 if (IsReference)
7469 return this->emitGetLocal(classifyPrim(E), Offset, E);
7470 return this->emitGetPtrLocal(Offset, E);
7471 }
7472 // Global variables.
7473 if (auto GlobalIndex = P.getGlobal(VD: D)) {
7474 if (IsReference) {
7475 if (!Ctx.getLangOpts().CPlusPlus11)
7476 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
7477 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
7478 }
7479
7480 return this->emitGetPtrGlobal(*GlobalIndex, E);
7481 }
7482
7483 // In case we need to re-visit a declaration.
7484 auto revisit = [&](const VarDecl *VD) -> bool {
7485 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
7486 return false;
7487 auto VarState = this->visitDecl(VD, /*IsConstexprUnknown=*/true);
7488
7489 if (!this->emitPopCC(E))
7490 return false;
7491
7492 if (VarState.notCreated())
7493 return true;
7494 if (!VarState)
7495 return false;
7496 // Retry.
7497 return this->visitDeclRef(D, E);
7498 };
7499
7500 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
7501 // Lambda captures.
7502 if (auto It = this->LambdaCaptures.find(D);
7503 It != this->LambdaCaptures.end()) {
7504 auto [Offset, IsPtr] = It->second;
7505
7506 if (IsPtr)
7507 return this->emitGetThisFieldPtr(Offset, E);
7508 return this->emitGetPtrThisField(Offset, E);
7509 }
7510 }
7511
7512 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E);
7513 DRE && DRE->refersToEnclosingVariableOrCapture()) {
7514 if (const auto *VD = dyn_cast<VarDecl>(Val: D); VD && VD->isInitCapture())
7515 return revisit(VD);
7516 }
7517
7518 if (const auto *BD = dyn_cast<BindingDecl>(Val: D))
7519 return this->visit(BD->getBinding());
7520
7521 // Avoid infinite recursion.
7522 if (D == InitializingDecl)
7523 return this->emitDummyPtr(D, E);
7524
7525 // Try to lazily visit (or emit dummy pointers for) declarations
7526 // we haven't seen yet.
7527 const auto *VD = dyn_cast<VarDecl>(Val: D);
7528 if (!VD)
7529 return this->emitError(E);
7530
7531 // For C.
7532 if (!Ctx.getLangOpts().CPlusPlus) {
7533 if (VD->getAnyInitializer() && DeclType.isConstant(Ctx: Ctx.getASTContext()) &&
7534 !VD->isWeak())
7535 return revisit(VD);
7536 return this->emitDummyPtr(D, E);
7537 }
7538
7539 // ... and C++.
7540 const auto typeShouldBeVisited = [&](QualType T) -> bool {
7541 if (T.isConstant(Ctx: Ctx.getASTContext()))
7542 return true;
7543 return T->isReferenceType();
7544 };
7545
7546 if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
7547 typeShouldBeVisited(DeclType)) {
7548 if (const Expr *Init = VD->getAnyInitializer();
7549 Init && !Init->isValueDependent()) {
7550 // Whether or not the evaluation is successul doesn't really matter
7551 // here -- we will create a global variable in any case, and that
7552 // will have the state of initializer evaluation attached.
7553 APValue V;
7554 SmallVector<PartialDiagnosticAt> Notes;
7555 (void)Init->EvaluateAsInitializer(Result&: V, Ctx: Ctx.getASTContext(), VD, Notes,
7556 IsConstantInitializer: true);
7557 return this->visitDeclRef(D, E);
7558 }
7559 return revisit(VD);
7560 }
7561
7562 // FIXME: The evaluateValue() check here is a little ridiculous, since
7563 // it will ultimately call into Context::evaluateAsInitializer(). In
7564 // other words, we're evaluating the initializer, just to know if we can
7565 // evaluate the initializer.
7566 if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() &&
7567 !VD->getInit()->isValueDependent()) {
7568 if (VD->evaluateValue()) {
7569 // Revisit the variable declaration, but make sure it's associated with a
7570 // different evaluation, so e.g. mutable reads don't work on it.
7571 EvalIDScope _(Ctx);
7572 return revisit(VD);
7573 }
7574
7575 if (IsReference)
7576 return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E),
7577 /*InitializerFailed=*/true, E);
7578 }
7579
7580 return this->emitDummyPtr(D, E);
7581}
7582
7583template <class Emitter>
7584bool Compiler<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
7585 const auto *D = E->getDecl();
7586 return this->visitDeclRef(D, E);
7587}
7588
7589template <class Emitter> bool Compiler<Emitter>::emitCleanup() {
7590 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) {
7591 if (!C->destroyLocals())
7592 return false;
7593 }
7594 return true;
7595}
7596
7597template <class Emitter>
7598unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
7599 const QualType DerivedType) {
7600 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
7601 if (const auto *R = Ty->getPointeeCXXRecordDecl())
7602 return R;
7603 return Ty->getAsCXXRecordDecl();
7604 };
7605 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
7606 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
7607
7608 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
7609}
7610
7611/// Emit casts from a PrimType to another PrimType.
7612template <class Emitter>
7613bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
7614 QualType ToQT, const Expr *E) {
7615
7616 if (FromT == PT_Float) {
7617 // Floating to floating.
7618 if (ToT == PT_Float) {
7619 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(T: ToQT);
7620 return this->emitCastFP(ToSem, getRoundingMode(E), E);
7621 }
7622
7623 if (ToT == PT_IntAP)
7624 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(T: ToQT),
7625 getFPOptions(E), E);
7626 if (ToT == PT_IntAPS)
7627 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(T: ToQT),
7628 getFPOptions(E), E);
7629
7630 // Float to integral.
7631 if (isIntegerOrBoolType(T: ToT) || ToT == PT_Bool)
7632 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
7633 }
7634
7635 if (isIntegerOrBoolType(T: FromT) || FromT == PT_Bool) {
7636 if (ToT == PT_IntAP)
7637 return this->emitCastAP(FromT, Ctx.getBitWidth(T: ToQT), E);
7638 if (ToT == PT_IntAPS)
7639 return this->emitCastAPS(FromT, Ctx.getBitWidth(T: ToQT), E);
7640
7641 // Integral to integral.
7642 if (isIntegerOrBoolType(T: ToT) || ToT == PT_Bool)
7643 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
7644
7645 if (ToT == PT_Float) {
7646 // Integral to floating.
7647 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(T: ToQT);
7648 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
7649 }
7650 }
7651
7652 return false;
7653}
7654
7655template <class Emitter>
7656bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT,
7657 QualType ToQT, const Expr *E) {
7658 assert(FromT != ToT);
7659
7660 if (ToT == PT_IntAP)
7661 return this->emitCastAP(FromT, Ctx.getBitWidth(T: ToQT), E);
7662 if (ToT == PT_IntAPS)
7663 return this->emitCastAPS(FromT, Ctx.getBitWidth(T: ToQT), E);
7664
7665 return this->emitCast(FromT, ToT, E);
7666}
7667
7668/// Emits __real(SubExpr)
7669template <class Emitter>
7670bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
7671 assert(SubExpr->getType()->isAnyComplexType());
7672
7673 if (DiscardResult)
7674 return this->discard(SubExpr);
7675
7676 if (!this->visit(SubExpr))
7677 return false;
7678 if (SubExpr->isLValue()) {
7679 if (!this->emitConstUint8(0, SubExpr))
7680 return false;
7681 return this->emitArrayElemPtrPopUint8(SubExpr);
7682 }
7683
7684 // Rvalue, load the actual element.
7685 return this->emitArrayElemPop(classifyComplexElementType(T: SubExpr->getType()),
7686 0, SubExpr);
7687}
7688
7689template <class Emitter>
7690bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
7691 assert(!DiscardResult);
7692 PrimType ElemT = classifyComplexElementType(T: E->getType());
7693 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
7694 // for us, that means (bool)E[0] || (bool)E[1]
7695 if (!this->emitArrayElem(ElemT, 0, E))
7696 return false;
7697 if (ElemT == PT_Float) {
7698 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7699 return false;
7700 } else {
7701 if (!this->emitCast(ElemT, PT_Bool, E))
7702 return false;
7703 }
7704
7705 // We now have the bool value of E[0] on the stack.
7706 LabelTy LabelTrue = this->getLabel();
7707 if (!this->jumpTrue(LabelTrue))
7708 return false;
7709
7710 if (!this->emitArrayElemPop(ElemT, 1, E))
7711 return false;
7712 if (ElemT == PT_Float) {
7713 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7714 return false;
7715 } else {
7716 if (!this->emitCast(ElemT, PT_Bool, E))
7717 return false;
7718 }
7719 // Leave the boolean value of E[1] on the stack.
7720 LabelTy EndLabel = this->getLabel();
7721 this->jump(EndLabel);
7722
7723 this->emitLabel(LabelTrue);
7724 if (!this->emitPopPtr(E))
7725 return false;
7726 if (!this->emitConstBool(true, E))
7727 return false;
7728
7729 this->fallthrough(EndLabel);
7730 this->emitLabel(EndLabel);
7731
7732 return true;
7733}
7734
7735template <class Emitter>
7736bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
7737 const BinaryOperator *E) {
7738 assert(E->isComparisonOp());
7739 assert(!Initializing);
7740 if (DiscardResult)
7741 return this->discard(LHS) && this->discard(RHS);
7742
7743 PrimType ElemT;
7744 bool LHSIsComplex;
7745 unsigned LHSOffset;
7746 if (LHS->getType()->isAnyComplexType()) {
7747 LHSIsComplex = true;
7748 ElemT = classifyComplexElementType(T: LHS->getType());
7749 LHSOffset = allocateLocalPrimitive(Src: LHS, Ty: PT_Ptr, /*IsConst=*/true);
7750 if (!this->visit(LHS))
7751 return false;
7752 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
7753 return false;
7754 } else {
7755 LHSIsComplex = false;
7756 PrimType LHST = classifyPrim(LHS->getType());
7757 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
7758 if (!this->visit(LHS))
7759 return false;
7760 if (!this->emitSetLocal(LHST, LHSOffset, E))
7761 return false;
7762 }
7763
7764 bool RHSIsComplex;
7765 unsigned RHSOffset;
7766 if (RHS->getType()->isAnyComplexType()) {
7767 RHSIsComplex = true;
7768 ElemT = classifyComplexElementType(T: RHS->getType());
7769 RHSOffset = allocateLocalPrimitive(Src: RHS, Ty: PT_Ptr, /*IsConst=*/true);
7770 if (!this->visit(RHS))
7771 return false;
7772 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
7773 return false;
7774 } else {
7775 RHSIsComplex = false;
7776 PrimType RHST = classifyPrim(RHS->getType());
7777 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
7778 if (!this->visit(RHS))
7779 return false;
7780 if (!this->emitSetLocal(RHST, RHSOffset, E))
7781 return false;
7782 }
7783
7784 auto getElem = [&](unsigned LocalOffset, unsigned Index,
7785 bool IsComplex) -> bool {
7786 if (IsComplex) {
7787 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
7788 return false;
7789 return this->emitArrayElemPop(ElemT, Index, E);
7790 }
7791 return this->emitGetLocal(ElemT, LocalOffset, E);
7792 };
7793
7794 for (unsigned I = 0; I != 2; ++I) {
7795 // Get both values.
7796 if (!getElem(LHSOffset, I, LHSIsComplex))
7797 return false;
7798 if (!getElem(RHSOffset, I, RHSIsComplex))
7799 return false;
7800 // And compare them.
7801 if (!this->emitEQ(ElemT, E))
7802 return false;
7803
7804 if (!this->emitCastBoolUint8(E))
7805 return false;
7806 }
7807
7808 // We now have two bool values on the stack. Compare those.
7809 if (!this->emitAddUint8(E))
7810 return false;
7811 if (!this->emitConstUint8(2, E))
7812 return false;
7813
7814 if (E->getOpcode() == BO_EQ) {
7815 if (!this->emitEQUint8(E))
7816 return false;
7817 } else if (E->getOpcode() == BO_NE) {
7818 if (!this->emitNEUint8(E))
7819 return false;
7820 } else
7821 return false;
7822
7823 // In C, this returns an int.
7824 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
7825 return this->emitCast(PT_Bool, ResT, E);
7826 return true;
7827}
7828
7829/// When calling this, we have a pointer of the local-to-destroy
7830/// on the stack.
7831/// Emit destruction of record types (or arrays of record types).
7832template <class Emitter>
7833bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R,
7834 SourceInfo Loc) {
7835 assert(R);
7836 assert(!R->hasTrivialDtor());
7837 const CXXDestructorDecl *Dtor = R->getDestructor();
7838 assert(Dtor);
7839 const Function *DtorFunc = getFunction(FD: Dtor);
7840 if (!DtorFunc)
7841 return false;
7842 assert(DtorFunc->hasThisPointer());
7843 assert(DtorFunc->getNumParams() == 1);
7844 return this->emitCall(DtorFunc, 0, Loc);
7845}
7846/// When calling this, we have a pointer of the local-to-destroy
7847/// on the stack.
7848/// Emit destruction of record types (or arrays of record types).
7849template <class Emitter>
7850bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc,
7851 SourceInfo Loc) {
7852 assert(Desc);
7853 assert(!Desc->hasTrivialDtor());
7854
7855 // Arrays.
7856 if (Desc->isArray()) {
7857 const Descriptor *ElemDesc = Desc->ElemDesc;
7858 assert(ElemDesc);
7859
7860 unsigned N = Desc->getNumElems();
7861 if (N == 0)
7862 return this->emitPopPtr(Loc);
7863
7864 for (ssize_t I = N - 1; I >= 1; --I) {
7865 if (!this->emitConstUint64(I, Loc))
7866 return false;
7867 if (!this->emitArrayElemPtrUint64(Loc))
7868 return false;
7869 if (!this->emitDestructionPop(ElemDesc, Loc))
7870 return false;
7871 }
7872 // Last iteration, removes the instance pointer from the stack.
7873 if (!this->emitConstUint64(0, Loc))
7874 return false;
7875 if (!this->emitArrayElemPtrPopUint64(Loc))
7876 return false;
7877 return this->emitDestructionPop(ElemDesc, Loc);
7878 }
7879
7880 assert(Desc->ElemRecord);
7881 assert(!Desc->ElemRecord->hasTrivialDtor());
7882 return this->emitRecordDestructionPop(Desc->ElemRecord, Loc);
7883}
7884
7885/// Create a dummy pointer for the given decl (or expr) and
7886/// push a pointer to it on the stack.
7887template <class Emitter>
7888bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E) {
7889 assert(!DiscardResult && "Should've been checked before");
7890 unsigned DummyID = P.getOrCreateDummy(D);
7891
7892 if (!this->emitGetPtrGlobal(DummyID, E))
7893 return false;
7894 if (E->getType()->isVoidType())
7895 return true;
7896
7897 // Convert the dummy pointer to another pointer type if we have to.
7898 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
7899 if (isPtrType(T: PT))
7900 return this->emitDecayPtr(PT_Ptr, PT, E);
7901 return false;
7902 }
7903 return true;
7904}
7905
7906template <class Emitter>
7907bool Compiler<Emitter>::emitFloat(const APFloat &F, const Expr *E) {
7908 if (Floating::singleWord(F.getSemantics()))
7909 return this->emitConstFloat(Floating(F), E);
7910
7911 APInt I = F.bitcastToAPInt();
7912 return this->emitConstFloat(
7913 Floating(const_cast<uint64_t *>(I.getRawData()),
7914 llvm::APFloatBase::SemanticsToEnum(Sem: F.getSemantics())),
7915 E);
7916}
7917
7918// This function is constexpr if and only if To, From, and the types of
7919// all subobjects of To and From are types T such that...
7920// (3.1) - is_union_v<T> is false;
7921// (3.2) - is_pointer_v<T> is false;
7922// (3.3) - is_member_pointer_v<T> is false;
7923// (3.4) - is_volatile_v<T> is false; and
7924// (3.5) - T has no non-static data members of reference type
7925template <class Emitter>
7926bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
7927 const Expr *SubExpr = E->getSubExpr();
7928 QualType FromType = SubExpr->getType();
7929 QualType ToType = E->getType();
7930 OptPrimType ToT = classify(ToType);
7931
7932 assert(!ToType->isReferenceType());
7933
7934 // Prepare storage for the result in case we discard.
7935 if (DiscardResult && !Initializing && !ToT) {
7936 UnsignedOrNone LocalIndex = allocateLocal(Src: E);
7937 if (!LocalIndex)
7938 return false;
7939 if (!this->emitGetPtrLocal(*LocalIndex, E))
7940 return false;
7941 }
7942
7943 // Get a pointer to the value-to-cast on the stack.
7944 // For CK_LValueToRValueBitCast, this is always an lvalue and
7945 // we later assume it to be one (i.e. a PT_Ptr). However,
7946 // we call this function for other utility methods where
7947 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
7948 if (SubExpr->isGLValue() || FromType->isVectorType()) {
7949 if (!this->visit(SubExpr))
7950 return false;
7951 } else if (OptPrimType FromT = classify(SubExpr)) {
7952 unsigned TempOffset =
7953 allocateLocalPrimitive(Src: SubExpr, Ty: *FromT, /*IsConst=*/true);
7954 if (!this->visit(SubExpr))
7955 return false;
7956 if (!this->emitSetLocal(*FromT, TempOffset, E))
7957 return false;
7958 if (!this->emitGetPtrLocal(TempOffset, E))
7959 return false;
7960 } else {
7961 return false;
7962 }
7963
7964 if (!ToT) {
7965 if (!this->emitBitCast(E))
7966 return false;
7967 return DiscardResult ? this->emitPopPtr(E) : true;
7968 }
7969 assert(ToT);
7970
7971 const llvm::fltSemantics *TargetSemantics = nullptr;
7972 if (ToT == PT_Float)
7973 TargetSemantics = &Ctx.getFloatSemantics(T: ToType);
7974
7975 // Conversion to a primitive type. FromType can be another
7976 // primitive type, or a record/array.
7977 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(K: BuiltinType::UChar) ||
7978 ToType->isSpecificBuiltinType(K: BuiltinType::Char_U));
7979 uint32_t ResultBitWidth = std::max(a: Ctx.getBitWidth(T: ToType), b: 8u);
7980
7981 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
7982 ResultBitWidth, TargetSemantics,
7983 ToType.getTypePtr(), E))
7984 return false;
7985
7986 if (DiscardResult)
7987 return this->emitPop(*ToT, E);
7988
7989 return true;
7990}
7991
7992namespace clang {
7993namespace interp {
7994
7995template class Compiler<ByteCodeEmitter>;
7996template class Compiler<EvalEmitter>;
7997
7998} // namespace interp
7999} // namespace clang
8000