clang 20.0.0git
Compiler.cpp
Go to the documentation of this file.
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 "Floating.h"
13#include "Function.h"
14#include "InterpShared.h"
15#include "PrimType.h"
16#include "Program.h"
17#include "clang/AST/Attr.h"
18
19using namespace clang;
20using namespace clang::interp;
21
22using APSInt = llvm::APSInt;
23
24namespace clang {
25namespace interp {
26
27/// Scope used to handle temporaries in toplevel variable declarations.
28template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
29public:
31 : LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P, VD),
32 OldInitializingDecl(Ctx->InitializingDecl) {
33 Ctx->InitializingDecl = VD;
34 Ctx->InitStack.push_back(InitLink::Decl(VD));
35 }
36
37 void addExtended(const Scope::Local &Local) override {
38 return this->addLocal(Local);
39 }
40
42 this->Ctx->InitializingDecl = OldInitializingDecl;
43 this->Ctx->InitStack.pop_back();
44 }
45
46private:
48 const ValueDecl *OldInitializingDecl;
49};
50
51/// Scope used to handle initialization methods.
52template <class Emitter> class OptionScope final {
53public:
54 /// Root constructor, compiling or discarding primitives.
55 OptionScope(Compiler<Emitter> *Ctx, bool NewDiscardResult,
56 bool NewInitializing)
57 : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
58 OldInitializing(Ctx->Initializing) {
59 Ctx->DiscardResult = NewDiscardResult;
60 Ctx->Initializing = NewInitializing;
61 }
62
64 Ctx->DiscardResult = OldDiscardResult;
65 Ctx->Initializing = OldInitializing;
66 }
67
68private:
69 /// Parent context.
71 /// Old discard flag to restore.
72 bool OldDiscardResult;
73 bool OldInitializing;
74};
75
76template <class Emitter>
77bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const {
78 switch (Kind) {
79 case K_This:
80 return Ctx->emitThis(E);
81 case K_Field:
82 // We're assuming there's a base pointer on the stack already.
83 return Ctx->emitGetPtrFieldPop(Offset, E);
84 case K_Temp:
85 return Ctx->emitGetPtrLocal(Offset, E);
86 case K_Decl:
87 return Ctx->visitDeclRef(D, E);
88 case K_Elem:
89 if (!Ctx->emitConstUint32(Offset, E))
90 return false;
91 return Ctx->emitArrayElemPtrPopUint32(E);
92 default:
93 llvm_unreachable("Unhandled InitLink kind");
94 }
95 return true;
96}
97
98/// Scope managing label targets.
99template <class Emitter> class LabelScope {
100public:
101 virtual ~LabelScope() {}
102
103protected:
105 /// Compiler instance.
107};
108
109/// Sets the context for break/continue statements.
110template <class Emitter> class LoopScope final : public LabelScope<Emitter> {
111public:
114
115 LoopScope(Compiler<Emitter> *Ctx, LabelTy BreakLabel, LabelTy ContinueLabel)
116 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
117 OldContinueLabel(Ctx->ContinueLabel) {
118 this->Ctx->BreakLabel = BreakLabel;
119 this->Ctx->ContinueLabel = ContinueLabel;
120 }
121
123 this->Ctx->BreakLabel = OldBreakLabel;
124 this->Ctx->ContinueLabel = OldContinueLabel;
125 }
126
127private:
128 OptLabelTy OldBreakLabel;
129 OptLabelTy OldContinueLabel;
130};
131
132// Sets the context for a switch scope, mapping labels.
133template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
134public:
138
139 SwitchScope(Compiler<Emitter> *Ctx, CaseMap &&CaseLabels, LabelTy BreakLabel,
140 OptLabelTy DefaultLabel)
141 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
142 OldDefaultLabel(this->Ctx->DefaultLabel),
143 OldCaseLabels(std::move(this->Ctx->CaseLabels)) {
144 this->Ctx->BreakLabel = BreakLabel;
145 this->Ctx->DefaultLabel = DefaultLabel;
146 this->Ctx->CaseLabels = std::move(CaseLabels);
147 }
148
150 this->Ctx->BreakLabel = OldBreakLabel;
151 this->Ctx->DefaultLabel = OldDefaultLabel;
152 this->Ctx->CaseLabels = std::move(OldCaseLabels);
153 }
154
155private:
156 OptLabelTy OldBreakLabel;
157 OptLabelTy OldDefaultLabel;
158 CaseMap OldCaseLabels;
159};
160
161template <class Emitter> class StmtExprScope final {
162public:
163 StmtExprScope(Compiler<Emitter> *Ctx) : Ctx(Ctx), OldFlag(Ctx->InStmtExpr) {
164 Ctx->InStmtExpr = true;
165 }
166
167 ~StmtExprScope() { Ctx->InStmtExpr = OldFlag; }
168
169private:
171 bool OldFlag;
172};
173
174} // namespace interp
175} // namespace clang
176
177template <class Emitter>
179 const Expr *SubExpr = CE->getSubExpr();
180 switch (CE->getCastKind()) {
181
182 case CK_LValueToRValue: {
183 if (DiscardResult)
184 return this->discard(SubExpr);
185
186 std::optional<PrimType> SubExprT = classify(SubExpr->getType());
187 // Prepare storage for the result.
188 if (!Initializing && !SubExprT) {
189 std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
190 if (!LocalIndex)
191 return false;
192 if (!this->emitGetPtrLocal(*LocalIndex, CE))
193 return false;
194 }
195
196 if (!this->visit(SubExpr))
197 return false;
198
199 if (SubExprT)
200 return this->emitLoadPop(*SubExprT, CE);
201
202 // If the subexpr type is not primitive, we need to perform a copy here.
203 // This happens for example in C when dereferencing a pointer of struct
204 // type.
205 return this->emitMemcpy(CE);
206 }
207
208 case CK_DerivedToBaseMemberPointer: {
209 assert(classifyPrim(CE->getType()) == PT_MemberPtr);
210 assert(classifyPrim(SubExpr->getType()) == PT_MemberPtr);
211 const auto *FromMP = SubExpr->getType()->getAs<MemberPointerType>();
212 const auto *ToMP = CE->getType()->getAs<MemberPointerType>();
213
214 unsigned DerivedOffset = collectBaseOffset(QualType(ToMP->getClass(), 0),
215 QualType(FromMP->getClass(), 0));
216
217 if (!this->delegate(SubExpr))
218 return false;
219
220 return this->emitGetMemberPtrBasePop(DerivedOffset, CE);
221 }
222
223 case CK_BaseToDerivedMemberPointer: {
224 assert(classifyPrim(CE) == PT_MemberPtr);
225 assert(classifyPrim(SubExpr) == PT_MemberPtr);
226 const auto *FromMP = SubExpr->getType()->getAs<MemberPointerType>();
227 const auto *ToMP = CE->getType()->getAs<MemberPointerType>();
228
229 unsigned DerivedOffset = collectBaseOffset(QualType(FromMP->getClass(), 0),
230 QualType(ToMP->getClass(), 0));
231
232 if (!this->delegate(SubExpr))
233 return false;
234 return this->emitGetMemberPtrBasePop(-DerivedOffset, CE);
235 }
236
237 case CK_UncheckedDerivedToBase:
238 case CK_DerivedToBase: {
239 if (!this->delegate(SubExpr))
240 return false;
241
242 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
243 if (const auto *PT = dyn_cast<PointerType>(Ty))
244 return PT->getPointeeType()->getAsCXXRecordDecl();
245 return Ty->getAsCXXRecordDecl();
246 };
247
248 // FIXME: We can express a series of non-virtual casts as a single
249 // GetPtrBasePop op.
250 QualType CurType = SubExpr->getType();
251 for (const CXXBaseSpecifier *B : CE->path()) {
252 if (B->isVirtual()) {
253 if (!this->emitGetPtrVirtBasePop(extractRecordDecl(B->getType()), CE))
254 return false;
255 CurType = B->getType();
256 } else {
257 unsigned DerivedOffset = collectBaseOffset(B->getType(), CurType);
258 if (!this->emitGetPtrBasePop(DerivedOffset, CE))
259 return false;
260 CurType = B->getType();
261 }
262 }
263
264 return true;
265 }
266
267 case CK_BaseToDerived: {
268 if (!this->delegate(SubExpr))
269 return false;
270
271 unsigned DerivedOffset =
272 collectBaseOffset(SubExpr->getType(), CE->getType());
273
274 return this->emitGetPtrDerivedPop(DerivedOffset, CE);
275 }
276
277 case CK_FloatingCast: {
278 // HLSL uses CK_FloatingCast to cast between vectors.
279 if (!SubExpr->getType()->isFloatingType() ||
280 !CE->getType()->isFloatingType())
281 return false;
282 if (DiscardResult)
283 return this->discard(SubExpr);
284 if (!this->visit(SubExpr))
285 return false;
286 const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
287 return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
288 }
289
290 case CK_IntegralToFloating: {
291 if (DiscardResult)
292 return this->discard(SubExpr);
293 std::optional<PrimType> FromT = classify(SubExpr->getType());
294 if (!FromT)
295 return false;
296
297 if (!this->visit(SubExpr))
298 return false;
299
300 const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
301 llvm::RoundingMode RM = getRoundingMode(CE);
302 return this->emitCastIntegralFloating(*FromT, TargetSemantics, RM, CE);
303 }
304
305 case CK_FloatingToBoolean:
306 case CK_FloatingToIntegral: {
307 if (DiscardResult)
308 return this->discard(SubExpr);
309
310 std::optional<PrimType> ToT = classify(CE->getType());
311
312 if (!ToT)
313 return false;
314
315 if (!this->visit(SubExpr))
316 return false;
317
318 if (ToT == PT_IntAP)
319 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(CE->getType()),
320 CE);
321 if (ToT == PT_IntAPS)
322 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(CE->getType()),
323 CE);
324
325 return this->emitCastFloatingIntegral(*ToT, CE);
326 }
327
328 case CK_NullToPointer:
329 case CK_NullToMemberPointer: {
330 if (!this->discard(SubExpr))
331 return false;
332 if (DiscardResult)
333 return true;
334
335 const Descriptor *Desc = nullptr;
336 const QualType PointeeType = CE->getType()->getPointeeType();
337 if (!PointeeType.isNull()) {
338 if (std::optional<PrimType> T = classify(PointeeType))
339 Desc = P.createDescriptor(SubExpr, *T);
340 else
341 Desc = P.createDescriptor(SubExpr, PointeeType.getTypePtr(),
342 std::nullopt, true, false,
343 /*IsMutable=*/false, nullptr);
344 }
345 return this->emitNull(classifyPrim(CE->getType()), Desc, CE);
346 }
347
348 case CK_PointerToIntegral: {
349 if (DiscardResult)
350 return this->discard(SubExpr);
351
352 if (!this->visit(SubExpr))
353 return false;
354
355 // If SubExpr doesn't result in a pointer, make it one.
356 if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) {
357 assert(isPtrType(FromT));
358 if (!this->emitDecayPtr(FromT, PT_Ptr, CE))
359 return false;
360 }
361
362 PrimType T = classifyPrim(CE->getType());
363 if (T == PT_IntAP)
364 return this->emitCastPointerIntegralAP(Ctx.getBitWidth(CE->getType()),
365 CE);
366 if (T == PT_IntAPS)
367 return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(CE->getType()),
368 CE);
369 return this->emitCastPointerIntegral(T, CE);
370 }
371
372 case CK_ArrayToPointerDecay: {
373 if (!this->visit(SubExpr))
374 return false;
375 if (!this->emitArrayDecay(CE))
376 return false;
377 if (DiscardResult)
378 return this->emitPopPtr(CE);
379 return true;
380 }
381
382 case CK_IntegralToPointer: {
383 QualType IntType = SubExpr->getType();
384 assert(IntType->isIntegralOrEnumerationType());
385 if (!this->visit(SubExpr))
386 return false;
387 // FIXME: I think the discard is wrong since the int->ptr cast might cause a
388 // diagnostic.
389 PrimType T = classifyPrim(IntType);
390 if (DiscardResult)
391 return this->emitPop(T, CE);
392
393 QualType PtrType = CE->getType();
394 const Descriptor *Desc;
395 if (std::optional<PrimType> T = classify(PtrType->getPointeeType()))
396 Desc = P.createDescriptor(SubExpr, *T);
397 else if (PtrType->getPointeeType()->isVoidType())
398 Desc = nullptr;
399 else
400 Desc = P.createDescriptor(CE, PtrType->getPointeeType().getTypePtr(),
401 Descriptor::InlineDescMD, true, false,
402 /*IsMutable=*/false, nullptr);
403
404 if (!this->emitGetIntPtr(T, Desc, CE))
405 return false;
406
407 PrimType DestPtrT = classifyPrim(PtrType);
408 if (DestPtrT == PT_Ptr)
409 return true;
410
411 // In case we're converting the integer to a non-Pointer.
412 return this->emitDecayPtr(PT_Ptr, DestPtrT, CE);
413 }
414
415 case CK_AtomicToNonAtomic:
416 case CK_ConstructorConversion:
417 case CK_FunctionToPointerDecay:
418 case CK_NonAtomicToAtomic:
419 case CK_NoOp:
420 case CK_UserDefinedConversion:
421 case CK_AddressSpaceConversion:
422 return this->delegate(SubExpr);
423
424 case CK_BitCast: {
425 // Reject bitcasts to atomic types.
426 if (CE->getType()->isAtomicType()) {
427 if (!this->discard(SubExpr))
428 return false;
429 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, CE);
430 }
431
432 if (DiscardResult)
433 return this->discard(SubExpr);
434
435 QualType SubExprTy = SubExpr->getType();
436 std::optional<PrimType> FromT = classify(SubExprTy);
437 std::optional<PrimType> ToT = classify(CE->getType());
438 if (!FromT || !ToT)
439 return false;
440
441 assert(isPtrType(*FromT));
442 assert(isPtrType(*ToT));
443 if (FromT == ToT) {
444 if (CE->getType()->isVoidPointerType())
445 return this->delegate(SubExpr);
446
447 if (!this->visit(SubExpr))
448 return false;
449 if (FromT == PT_Ptr)
450 return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), CE);
451 return true;
452 }
453
454 if (!this->visit(SubExpr))
455 return false;
456 return this->emitDecayPtr(*FromT, *ToT, CE);
457 }
458
459 case CK_IntegralToBoolean:
460 case CK_BooleanToSignedIntegral:
461 case CK_IntegralCast: {
462 if (DiscardResult)
463 return this->discard(SubExpr);
464 std::optional<PrimType> FromT = classify(SubExpr->getType());
465 std::optional<PrimType> ToT = classify(CE->getType());
466
467 if (!FromT || !ToT)
468 return false;
469
470 if (!this->visit(SubExpr))
471 return false;
472
473 // Possibly diagnose casts to enum types if the target type does not
474 // have a fixed size.
475 if (Ctx.getLangOpts().CPlusPlus && CE->getType()->isEnumeralType()) {
476 if (const auto *ET = CE->getType().getCanonicalType()->getAs<EnumType>();
477 ET && !ET->getDecl()->isFixed()) {
478 if (!this->emitCheckEnumValue(*FromT, ET->getDecl(), CE))
479 return false;
480 }
481 }
482
483 auto maybeNegate = [&]() -> bool {
484 if (CE->getCastKind() == CK_BooleanToSignedIntegral)
485 return this->emitNeg(*ToT, CE);
486 return true;
487 };
488
489 if (ToT == PT_IntAP)
490 return this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE) &&
491 maybeNegate();
492 if (ToT == PT_IntAPS)
493 return this->emitCastAPS(*FromT, Ctx.getBitWidth(CE->getType()), CE) &&
494 maybeNegate();
495
496 if (FromT == ToT)
497 return true;
498 if (!this->emitCast(*FromT, *ToT, CE))
499 return false;
500
501 return maybeNegate();
502 }
503
504 case CK_PointerToBoolean:
505 case CK_MemberPointerToBoolean: {
506 PrimType PtrT = classifyPrim(SubExpr->getType());
507
508 // Just emit p != nullptr for this.
509 if (!this->visit(SubExpr))
510 return false;
511
512 if (!this->emitNull(PtrT, nullptr, CE))
513 return false;
514
515 return this->emitNE(PtrT, CE);
516 }
517
518 case CK_IntegralComplexToBoolean:
519 case CK_FloatingComplexToBoolean: {
520 if (DiscardResult)
521 return this->discard(SubExpr);
522 if (!this->visit(SubExpr))
523 return false;
524 return this->emitComplexBoolCast(SubExpr);
525 }
526
527 case CK_IntegralComplexToReal:
528 case CK_FloatingComplexToReal:
529 return this->emitComplexReal(SubExpr);
530
531 case CK_IntegralRealToComplex:
532 case CK_FloatingRealToComplex: {
533 // We're creating a complex value here, so we need to
534 // allocate storage for it.
535 if (!Initializing) {
536 unsigned LocalIndex = allocateTemporary(CE);
537 if (!this->emitGetPtrLocal(LocalIndex, CE))
538 return false;
539 }
540
541 // Init the complex value to {SubExpr, 0}.
542 if (!this->visitArrayElemInit(0, SubExpr))
543 return false;
544 // Zero-init the second element.
545 PrimType T = classifyPrim(SubExpr->getType());
546 if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
547 return false;
548 return this->emitInitElem(T, 1, SubExpr);
549 }
550
551 case CK_IntegralComplexCast:
552 case CK_FloatingComplexCast:
553 case CK_IntegralComplexToFloatingComplex:
554 case CK_FloatingComplexToIntegralComplex: {
555 assert(CE->getType()->isAnyComplexType());
556 assert(SubExpr->getType()->isAnyComplexType());
557 if (DiscardResult)
558 return this->discard(SubExpr);
559
560 if (!Initializing) {
561 std::optional<unsigned> LocalIndex = allocateLocal(CE);
562 if (!LocalIndex)
563 return false;
564 if (!this->emitGetPtrLocal(*LocalIndex, CE))
565 return false;
566 }
567
568 // Location for the SubExpr.
569 // Since SubExpr is of complex type, visiting it results in a pointer
570 // anyway, so we just create a temporary pointer variable.
571 unsigned SubExprOffset = allocateLocalPrimitive(
572 SubExpr, PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
573 if (!this->visit(SubExpr))
574 return false;
575 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, CE))
576 return false;
577
578 PrimType SourceElemT = classifyComplexElementType(SubExpr->getType());
579 QualType DestElemType =
580 CE->getType()->getAs<ComplexType>()->getElementType();
581 PrimType DestElemT = classifyPrim(DestElemType);
582 // Cast both elements individually.
583 for (unsigned I = 0; I != 2; ++I) {
584 if (!this->emitGetLocal(PT_Ptr, SubExprOffset, CE))
585 return false;
586 if (!this->emitArrayElemPop(SourceElemT, I, CE))
587 return false;
588
589 // Do the cast.
590 if (!this->emitPrimCast(SourceElemT, DestElemT, DestElemType, CE))
591 return false;
592
593 // Save the value.
594 if (!this->emitInitElem(DestElemT, I, CE))
595 return false;
596 }
597 return true;
598 }
599
600 case CK_VectorSplat: {
601 assert(!classify(CE->getType()));
602 assert(classify(SubExpr->getType()));
603 assert(CE->getType()->isVectorType());
604
605 if (DiscardResult)
606 return this->discard(SubExpr);
607
608 if (!Initializing) {
609 std::optional<unsigned> LocalIndex = allocateLocal(CE);
610 if (!LocalIndex)
611 return false;
612 if (!this->emitGetPtrLocal(*LocalIndex, CE))
613 return false;
614 }
615
616 const auto *VT = CE->getType()->getAs<VectorType>();
617 PrimType ElemT = classifyPrim(SubExpr->getType());
618 unsigned ElemOffset = allocateLocalPrimitive(
619 SubExpr, ElemT, /*IsConst=*/true, /*IsExtended=*/false);
620
621 // Prepare a local variable for the scalar value.
622 if (!this->visit(SubExpr))
623 return false;
624 if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, CE))
625 return false;
626
627 if (!this->emitSetLocal(ElemT, ElemOffset, CE))
628 return false;
629
630 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
631 if (!this->emitGetLocal(ElemT, ElemOffset, CE))
632 return false;
633 if (!this->emitInitElem(ElemT, I, CE))
634 return false;
635 }
636
637 return true;
638 }
639
640 case CK_ToVoid:
641 return discard(SubExpr);
642
643 default:
644 return this->emitInvalid(CE);
645 }
646 llvm_unreachable("Unhandled clang::CastKind enum");
647}
648
649template <class Emitter>
651 if (DiscardResult)
652 return true;
653
654 return this->emitConst(LE->getValue(), LE);
655}
656
657template <class Emitter>
659 if (DiscardResult)
660 return true;
661
662 return this->emitConstFloat(E->getValue(), E);
663}
664
665template <class Emitter>
667 assert(E->getType()->isAnyComplexType());
668 if (DiscardResult)
669 return true;
670
671 if (!Initializing) {
672 unsigned LocalIndex = allocateTemporary(E);
673 if (!this->emitGetPtrLocal(LocalIndex, E))
674 return false;
675 }
676
677 const Expr *SubExpr = E->getSubExpr();
678 PrimType SubExprT = classifyPrim(SubExpr->getType());
679
680 if (!this->visitZeroInitializer(SubExprT, SubExpr->getType(), SubExpr))
681 return false;
682 if (!this->emitInitElem(SubExprT, 0, SubExpr))
683 return false;
684 return this->visitArrayElemInit(1, SubExpr);
685}
686
687template <class Emitter>
689 return this->delegate(E->getSubExpr());
690}
691
692template <class Emitter>
694 // Need short-circuiting for these.
695 if (BO->isLogicalOp())
696 return this->VisitLogicalBinOp(BO);
697
698 const Expr *LHS = BO->getLHS();
699 const Expr *RHS = BO->getRHS();
700
701 // Handle comma operators. Just discard the LHS
702 // and delegate to RHS.
703 if (BO->isCommaOp()) {
704 if (!this->discard(LHS))
705 return false;
706 if (RHS->getType()->isVoidType())
707 return this->discard(RHS);
708
709 return this->delegate(RHS);
710 }
711
712 if (BO->getType()->isAnyComplexType())
713 return this->VisitComplexBinOp(BO);
714 if ((LHS->getType()->isAnyComplexType() ||
715 RHS->getType()->isAnyComplexType()) &&
716 BO->isComparisonOp())
717 return this->emitComplexComparison(LHS, RHS, BO);
718
719 if (BO->isPtrMemOp()) {
720 if (!this->visit(LHS))
721 return false;
722
723 if (!this->visit(RHS))
724 return false;
725
726 if (!this->emitToMemberPtr(BO))
727 return false;
728
729 if (classifyPrim(BO) == PT_MemberPtr)
730 return true;
731
732 if (!this->emitCastMemberPtrPtr(BO))
733 return false;
734 return DiscardResult ? this->emitPopPtr(BO) : true;
735 }
736
737 // Typecheck the args.
738 std::optional<PrimType> LT = classify(LHS);
739 std::optional<PrimType> RT = classify(RHS);
740 std::optional<PrimType> T = classify(BO->getType());
741
742 // Special case for C++'s three-way/spaceship operator <=>, which
743 // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
744 // have a PrimType).
745 if (!T && BO->getOpcode() == BO_Cmp) {
746 if (DiscardResult)
747 return true;
748 const ComparisonCategoryInfo *CmpInfo =
749 Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
750 assert(CmpInfo);
751
752 // We need a temporary variable holding our return value.
753 if (!Initializing) {
754 std::optional<unsigned> ResultIndex = this->allocateLocal(BO);
755 if (!this->emitGetPtrLocal(*ResultIndex, BO))
756 return false;
757 }
758
759 if (!visit(LHS) || !visit(RHS))
760 return false;
761
762 return this->emitCMP3(*LT, CmpInfo, BO);
763 }
764
765 if (!LT || !RT || !T)
766 return false;
767
768 // Pointer arithmetic special case.
769 if (BO->getOpcode() == BO_Add || BO->getOpcode() == BO_Sub) {
770 if (isPtrType(*T) || (isPtrType(*LT) && isPtrType(*RT)))
771 return this->VisitPointerArithBinOp(BO);
772 }
773
774 // Assignmentes require us to evalute the RHS first.
775 if (BO->getOpcode() == BO_Assign) {
776 if (!visit(RHS) || !visit(LHS))
777 return false;
778 if (!this->emitFlip(*LT, *RT, BO))
779 return false;
780 } else {
781 if (!visit(LHS) || !visit(RHS))
782 return false;
783 }
784
785 // For languages such as C, cast the result of one
786 // of our comparision opcodes to T (which is usually int).
787 auto MaybeCastToBool = [this, T, BO](bool Result) {
788 if (!Result)
789 return false;
790 if (DiscardResult)
791 return this->emitPop(*T, BO);
792 if (T != PT_Bool)
793 return this->emitCast(PT_Bool, *T, BO);
794 return true;
795 };
796
797 auto Discard = [this, T, BO](bool Result) {
798 if (!Result)
799 return false;
800 return DiscardResult ? this->emitPop(*T, BO) : true;
801 };
802
803 switch (BO->getOpcode()) {
804 case BO_EQ:
805 return MaybeCastToBool(this->emitEQ(*LT, BO));
806 case BO_NE:
807 return MaybeCastToBool(this->emitNE(*LT, BO));
808 case BO_LT:
809 return MaybeCastToBool(this->emitLT(*LT, BO));
810 case BO_LE:
811 return MaybeCastToBool(this->emitLE(*LT, BO));
812 case BO_GT:
813 return MaybeCastToBool(this->emitGT(*LT, BO));
814 case BO_GE:
815 return MaybeCastToBool(this->emitGE(*LT, BO));
816 case BO_Sub:
817 if (BO->getType()->isFloatingType())
818 return Discard(this->emitSubf(getRoundingMode(BO), BO));
819 return Discard(this->emitSub(*T, BO));
820 case BO_Add:
821 if (BO->getType()->isFloatingType())
822 return Discard(this->emitAddf(getRoundingMode(BO), BO));
823 return Discard(this->emitAdd(*T, BO));
824 case BO_Mul:
825 if (BO->getType()->isFloatingType())
826 return Discard(this->emitMulf(getRoundingMode(BO), BO));
827 return Discard(this->emitMul(*T, BO));
828 case BO_Rem:
829 return Discard(this->emitRem(*T, BO));
830 case BO_Div:
831 if (BO->getType()->isFloatingType())
832 return Discard(this->emitDivf(getRoundingMode(BO), BO));
833 return Discard(this->emitDiv(*T, BO));
834 case BO_Assign:
835 if (DiscardResult)
836 return LHS->refersToBitField() ? this->emitStoreBitFieldPop(*T, BO)
837 : this->emitStorePop(*T, BO);
838 if (LHS->refersToBitField()) {
839 if (!this->emitStoreBitField(*T, BO))
840 return false;
841 } else {
842 if (!this->emitStore(*T, BO))
843 return false;
844 }
845 // Assignments aren't necessarily lvalues in C.
846 // Load from them in that case.
847 if (!BO->isLValue())
848 return this->emitLoadPop(*T, BO);
849 return true;
850 case BO_And:
851 return Discard(this->emitBitAnd(*T, BO));
852 case BO_Or:
853 return Discard(this->emitBitOr(*T, BO));
854 case BO_Shl:
855 return Discard(this->emitShl(*LT, *RT, BO));
856 case BO_Shr:
857 return Discard(this->emitShr(*LT, *RT, BO));
858 case BO_Xor:
859 return Discard(this->emitBitXor(*T, BO));
860 case BO_LOr:
861 case BO_LAnd:
862 llvm_unreachable("Already handled earlier");
863 default:
864 return false;
865 }
866
867 llvm_unreachable("Unhandled binary op");
868}
869
870/// Perform addition/subtraction of a pointer and an integer or
871/// subtraction of two pointers.
872template <class Emitter>
874 BinaryOperatorKind Op = E->getOpcode();
875 const Expr *LHS = E->getLHS();
876 const Expr *RHS = E->getRHS();
877
878 if ((Op != BO_Add && Op != BO_Sub) ||
879 (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
880 return false;
881
882 std::optional<PrimType> LT = classify(LHS);
883 std::optional<PrimType> RT = classify(RHS);
884
885 if (!LT || !RT)
886 return false;
887
888 if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
889 if (Op != BO_Sub)
890 return false;
891
892 assert(E->getType()->isIntegerType());
893 if (!visit(RHS) || !visit(LHS))
894 return false;
895
896 return this->emitSubPtr(classifyPrim(E->getType()), E);
897 }
898
899 PrimType OffsetType;
900 if (LHS->getType()->isIntegerType()) {
901 if (!visit(RHS) || !visit(LHS))
902 return false;
903 OffsetType = *LT;
904 } else if (RHS->getType()->isIntegerType()) {
905 if (!visit(LHS) || !visit(RHS))
906 return false;
907 OffsetType = *RT;
908 } else {
909 return false;
910 }
911
912 if (Op == BO_Add)
913 return this->emitAddOffset(OffsetType, E);
914 else if (Op == BO_Sub)
915 return this->emitSubOffset(OffsetType, E);
916
917 return false;
918}
919
920template <class Emitter>
922 assert(E->isLogicalOp());
923 BinaryOperatorKind Op = E->getOpcode();
924 const Expr *LHS = E->getLHS();
925 const Expr *RHS = E->getRHS();
926 std::optional<PrimType> T = classify(E->getType());
927
928 if (Op == BO_LOr) {
929 // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
930 LabelTy LabelTrue = this->getLabel();
931 LabelTy LabelEnd = this->getLabel();
932
933 if (!this->visitBool(LHS))
934 return false;
935 if (!this->jumpTrue(LabelTrue))
936 return false;
937
938 if (!this->visitBool(RHS))
939 return false;
940 if (!this->jump(LabelEnd))
941 return false;
942
943 this->emitLabel(LabelTrue);
944 this->emitConstBool(true, E);
945 this->fallthrough(LabelEnd);
946 this->emitLabel(LabelEnd);
947
948 } else {
949 assert(Op == BO_LAnd);
950 // Logical AND.
951 // Visit LHS. Only visit RHS if LHS was TRUE.
952 LabelTy LabelFalse = this->getLabel();
953 LabelTy LabelEnd = this->getLabel();
954
955 if (!this->visitBool(LHS))
956 return false;
957 if (!this->jumpFalse(LabelFalse))
958 return false;
959
960 if (!this->visitBool(RHS))
961 return false;
962 if (!this->jump(LabelEnd))
963 return false;
964
965 this->emitLabel(LabelFalse);
966 this->emitConstBool(false, E);
967 this->fallthrough(LabelEnd);
968 this->emitLabel(LabelEnd);
969 }
970
971 if (DiscardResult)
972 return this->emitPopBool(E);
973
974 // For C, cast back to integer type.
975 assert(T);
976 if (T != PT_Bool)
977 return this->emitCast(PT_Bool, *T, E);
978 return true;
979}
980
981template <class Emitter>
983 // Prepare storage for result.
984 if (!Initializing) {
985 unsigned LocalIndex = allocateTemporary(E);
986 if (!this->emitGetPtrLocal(LocalIndex, E))
987 return false;
988 }
989
990 // Both LHS and RHS might _not_ be of complex type, but one of them
991 // needs to be.
992 const Expr *LHS = E->getLHS();
993 const Expr *RHS = E->getRHS();
994
995 PrimType ResultElemT = this->classifyComplexElementType(E->getType());
996 unsigned ResultOffset = ~0u;
997 if (!DiscardResult)
998 ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, true, false);
999
1000 // Save result pointer in ResultOffset
1001 if (!this->DiscardResult) {
1002 if (!this->emitDupPtr(E))
1003 return false;
1004 if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
1005 return false;
1006 }
1007 QualType LHSType = LHS->getType();
1008 if (const auto *AT = LHSType->getAs<AtomicType>())
1009 LHSType = AT->getValueType();
1010 QualType RHSType = RHS->getType();
1011 if (const auto *AT = RHSType->getAs<AtomicType>())
1012 RHSType = AT->getValueType();
1013
1014 bool LHSIsComplex = LHSType->isAnyComplexType();
1015 unsigned LHSOffset;
1016 bool RHSIsComplex = RHSType->isAnyComplexType();
1017
1018 // For ComplexComplex Mul, we have special ops to make their implementation
1019 // easier.
1020 BinaryOperatorKind Op = E->getOpcode();
1021 if (Op == BO_Mul && LHSIsComplex && RHSIsComplex) {
1022 assert(classifyPrim(LHSType->getAs<ComplexType>()->getElementType()) ==
1023 classifyPrim(RHSType->getAs<ComplexType>()->getElementType()));
1024 PrimType ElemT =
1025 classifyPrim(LHSType->getAs<ComplexType>()->getElementType());
1026 if (!this->visit(LHS))
1027 return false;
1028 if (!this->visit(RHS))
1029 return false;
1030 return this->emitMulc(ElemT, E);
1031 }
1032
1033 if (Op == BO_Div && RHSIsComplex) {
1034 QualType ElemQT = RHSType->getAs<ComplexType>()->getElementType();
1035 PrimType ElemT = classifyPrim(ElemQT);
1036 // If the LHS is not complex, we still need to do the full complex
1037 // division, so just stub create a complex value and stub it out with
1038 // the LHS and a zero.
1039
1040 if (!LHSIsComplex) {
1041 // This is using the RHS type for the fake-complex LHS.
1042 LHSOffset = allocateTemporary(RHS);
1043
1044 if (!this->emitGetPtrLocal(LHSOffset, E))
1045 return false;
1046
1047 if (!this->visit(LHS))
1048 return false;
1049 // real is LHS
1050 if (!this->emitInitElem(ElemT, 0, E))
1051 return false;
1052 // imag is zero
1053 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1054 return false;
1055 if (!this->emitInitElem(ElemT, 1, E))
1056 return false;
1057 } else {
1058 if (!this->visit(LHS))
1059 return false;
1060 }
1061
1062 if (!this->visit(RHS))
1063 return false;
1064 return this->emitDivc(ElemT, E);
1065 }
1066
1067 // Evaluate LHS and save value to LHSOffset.
1068 if (LHSType->isAnyComplexType()) {
1069 LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
1070 if (!this->visit(LHS))
1071 return false;
1072 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1073 return false;
1074 } else {
1075 PrimType LHST = classifyPrim(LHSType);
1076 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
1077 if (!this->visit(LHS))
1078 return false;
1079 if (!this->emitSetLocal(LHST, LHSOffset, E))
1080 return false;
1081 }
1082
1083 // Same with RHS.
1084 unsigned RHSOffset;
1085 if (RHSType->isAnyComplexType()) {
1086 RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
1087 if (!this->visit(RHS))
1088 return false;
1089 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1090 return false;
1091 } else {
1092 PrimType RHST = classifyPrim(RHSType);
1093 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
1094 if (!this->visit(RHS))
1095 return false;
1096 if (!this->emitSetLocal(RHST, RHSOffset, E))
1097 return false;
1098 }
1099
1100 // For both LHS and RHS, either load the value from the complex pointer, or
1101 // directly from the local variable. For index 1 (i.e. the imaginary part),
1102 // just load 0 and do the operation anyway.
1103 auto loadComplexValue = [this](bool IsComplex, bool LoadZero,
1104 unsigned ElemIndex, unsigned Offset,
1105 const Expr *E) -> bool {
1106 if (IsComplex) {
1107 if (!this->emitGetLocal(PT_Ptr, Offset, E))
1108 return false;
1109 return this->emitArrayElemPop(classifyComplexElementType(E->getType()),
1110 ElemIndex, E);
1111 }
1112 if (ElemIndex == 0 || !LoadZero)
1113 return this->emitGetLocal(classifyPrim(E->getType()), Offset, E);
1114 return this->visitZeroInitializer(classifyPrim(E->getType()), E->getType(),
1115 E);
1116 };
1117
1118 // Now we can get pointers to the LHS and RHS from the offsets above.
1119 for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
1120 // Result pointer for the store later.
1121 if (!this->DiscardResult) {
1122 if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
1123 return false;
1124 }
1125
1126 // The actual operation.
1127 switch (Op) {
1128 case BO_Add:
1129 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1130 return false;
1131
1132 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1133 return false;
1134 if (ResultElemT == PT_Float) {
1135 if (!this->emitAddf(getRoundingMode(E), E))
1136 return false;
1137 } else {
1138 if (!this->emitAdd(ResultElemT, E))
1139 return false;
1140 }
1141 break;
1142 case BO_Sub:
1143 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1144 return false;
1145
1146 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1147 return false;
1148 if (ResultElemT == PT_Float) {
1149 if (!this->emitSubf(getRoundingMode(E), E))
1150 return false;
1151 } else {
1152 if (!this->emitSub(ResultElemT, E))
1153 return false;
1154 }
1155 break;
1156 case BO_Mul:
1157 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1158 return false;
1159
1160 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1161 return false;
1162
1163 if (ResultElemT == PT_Float) {
1164 if (!this->emitMulf(getRoundingMode(E), E))
1165 return false;
1166 } else {
1167 if (!this->emitMul(ResultElemT, E))
1168 return false;
1169 }
1170 break;
1171 case BO_Div:
1172 assert(!RHSIsComplex);
1173 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1174 return false;
1175
1176 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1177 return false;
1178
1179 if (ResultElemT == PT_Float) {
1180 if (!this->emitDivf(getRoundingMode(E), E))
1181 return false;
1182 } else {
1183 if (!this->emitDiv(ResultElemT, E))
1184 return false;
1185 }
1186 break;
1187
1188 default:
1189 return false;
1190 }
1191
1192 if (!this->DiscardResult) {
1193 // Initialize array element with the value we just computed.
1194 if (!this->emitInitElemPop(ResultElemT, ElemIndex, E))
1195 return false;
1196 } else {
1197 if (!this->emitPop(ResultElemT, E))
1198 return false;
1199 }
1200 }
1201 return true;
1202}
1203
1204template <class Emitter>
1206 const ImplicitValueInitExpr *E) {
1207 QualType QT = E->getType();
1208
1209 if (std::optional<PrimType> T = classify(QT))
1210 return this->visitZeroInitializer(*T, QT, E);
1211
1212 if (QT->isRecordType()) {
1213 const RecordDecl *RD = QT->getAsRecordDecl();
1214 assert(RD);
1215 if (RD->isInvalidDecl())
1216 return false;
1217 if (RD->isUnion()) {
1218 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
1219 // object's first non-static named data member is zero-initialized
1220 // FIXME
1221 return false;
1222 }
1223
1224 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1225 CXXRD && CXXRD->getNumVBases() > 0) {
1226 // TODO: Diagnose.
1227 return false;
1228 }
1229
1230 const Record *R = getRecord(QT);
1231 if (!R)
1232 return false;
1233
1234 assert(Initializing);
1235 return this->visitZeroRecordInitializer(R, E);
1236 }
1237
1238 if (QT->isIncompleteArrayType())
1239 return true;
1240
1241 if (QT->isArrayType()) {
1242 const ArrayType *AT = QT->getAsArrayTypeUnsafe();
1243 assert(AT);
1244 const auto *CAT = cast<ConstantArrayType>(AT);
1245 size_t NumElems = CAT->getZExtSize();
1246 PrimType ElemT = classifyPrim(CAT->getElementType());
1247
1248 for (size_t I = 0; I != NumElems; ++I) {
1249 if (!this->visitZeroInitializer(ElemT, CAT->getElementType(), E))
1250 return false;
1251 if (!this->emitInitElem(ElemT, I, E))
1252 return false;
1253 }
1254
1255 return true;
1256 }
1257
1258 if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) {
1259 assert(Initializing);
1260 QualType ElemQT = ComplexTy->getElementType();
1261 PrimType ElemT = classifyPrim(ElemQT);
1262 for (unsigned I = 0; I < 2; ++I) {
1263 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1264 return false;
1265 if (!this->emitInitElem(ElemT, I, E))
1266 return false;
1267 }
1268 return true;
1269 }
1270
1271 if (const auto *VecT = E->getType()->getAs<VectorType>()) {
1272 unsigned NumVecElements = VecT->getNumElements();
1273 QualType ElemQT = VecT->getElementType();
1274 PrimType ElemT = classifyPrim(ElemQT);
1275
1276 for (unsigned I = 0; I < NumVecElements; ++I) {
1277 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1278 return false;
1279 if (!this->emitInitElem(ElemT, I, E))
1280 return false;
1281 }
1282 return true;
1283 }
1284
1285 return false;
1286}
1287
1288template <class Emitter>
1290 const Expr *LHS = E->getLHS();
1291 const Expr *RHS = E->getRHS();
1292 const Expr *Index = E->getIdx();
1293
1294 if (DiscardResult)
1295 return this->discard(LHS) && this->discard(RHS);
1296
1297 // C++17's rules require us to evaluate the LHS first, regardless of which
1298 // side is the base.
1299 bool Success = true;
1300 for (const Expr *SubExpr : {LHS, RHS}) {
1301 if (!this->visit(SubExpr))
1302 Success = false;
1303 }
1304
1305 if (!Success)
1306 return false;
1307
1308 PrimType IndexT = classifyPrim(Index->getType());
1309 // If the index is first, we need to change that.
1310 if (LHS == Index) {
1311 if (!this->emitFlip(PT_Ptr, IndexT, E))
1312 return false;
1313 }
1314
1315 return this->emitArrayElemPtrPop(IndexT, E);
1316}
1317
1318template <class Emitter>
1320 const Expr *ArrayFiller, const Expr *E) {
1321
1322 QualType QT = E->getType();
1323
1324 if (const auto *AT = QT->getAs<AtomicType>())
1325 QT = AT->getValueType();
1326
1327 if (QT->isVoidType())
1328 return this->emitInvalid(E);
1329
1330 // Handle discarding first.
1331 if (DiscardResult) {
1332 for (const Expr *Init : Inits) {
1333 if (!this->discard(Init))
1334 return false;
1335 }
1336 return true;
1337 }
1338
1339 // Primitive values.
1340 if (std::optional<PrimType> T = classify(QT)) {
1341 assert(!DiscardResult);
1342 if (Inits.size() == 0)
1343 return this->visitZeroInitializer(*T, QT, E);
1344 assert(Inits.size() == 1);
1345 return this->delegate(Inits[0]);
1346 }
1347
1348 if (QT->isRecordType()) {
1349 const Record *R = getRecord(QT);
1350
1351 if (Inits.size() == 1 && E->getType() == Inits[0]->getType())
1352 return this->delegate(Inits[0]);
1353
1354 auto initPrimitiveField = [=](const Record::Field *FieldToInit,
1355 const Expr *Init, PrimType T) -> bool {
1356 InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
1357 if (!this->visit(Init))
1358 return false;
1359
1360 if (FieldToInit->isBitField())
1361 return this->emitInitBitField(T, FieldToInit, E);
1362 return this->emitInitField(T, FieldToInit->Offset, E);
1363 };
1364
1365 auto initCompositeField = [=](const Record::Field *FieldToInit,
1366 const Expr *Init) -> bool {
1367 InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
1368 InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
1369 // Non-primitive case. Get a pointer to the field-to-initialize
1370 // on the stack and recurse into visitInitializer().
1371 if (!this->emitGetPtrField(FieldToInit->Offset, Init))
1372 return false;
1373 if (!this->visitInitializer(Init))
1374 return false;
1375 return this->emitPopPtr(E);
1376 };
1377
1378 if (R->isUnion()) {
1379 if (Inits.size() == 0) {
1380 if (!this->visitZeroRecordInitializer(R, E))
1381 return false;
1382 } else {
1383 const Expr *Init = Inits[0];
1384 const FieldDecl *FToInit = nullptr;
1385 if (const auto *ILE = dyn_cast<InitListExpr>(E))
1386 FToInit = ILE->getInitializedFieldInUnion();
1387 else
1388 FToInit = cast<CXXParenListInitExpr>(E)->getInitializedFieldInUnion();
1389
1390 const Record::Field *FieldToInit = R->getField(FToInit);
1391 if (std::optional<PrimType> T = classify(Init)) {
1392 if (!initPrimitiveField(FieldToInit, Init, *T))
1393 return false;
1394 } else {
1395 if (!initCompositeField(FieldToInit, Init))
1396 return false;
1397 }
1398 }
1399 return this->emitFinishInit(E);
1400 }
1401
1402 assert(!R->isUnion());
1403 unsigned InitIndex = 0;
1404 for (const Expr *Init : Inits) {
1405 // Skip unnamed bitfields.
1406 while (InitIndex < R->getNumFields() &&
1407 R->getField(InitIndex)->Decl->isUnnamedBitField())
1408 ++InitIndex;
1409
1410 if (std::optional<PrimType> T = classify(Init)) {
1411 const Record::Field *FieldToInit = R->getField(InitIndex);
1412 if (!initPrimitiveField(FieldToInit, Init, *T))
1413 return false;
1414 ++InitIndex;
1415 } else {
1416 // Initializer for a direct base class.
1417 if (const Record::Base *B = R->getBase(Init->getType())) {
1418 if (!this->emitGetPtrBase(B->Offset, Init))
1419 return false;
1420
1421 if (!this->visitInitializer(Init))
1422 return false;
1423
1424 if (!this->emitFinishInitPop(E))
1425 return false;
1426 // Base initializers don't increase InitIndex, since they don't count
1427 // into the Record's fields.
1428 } else {
1429 const Record::Field *FieldToInit = R->getField(InitIndex);
1430 if (!initCompositeField(FieldToInit, Init))
1431 return false;
1432 ++InitIndex;
1433 }
1434 }
1435 }
1436 return this->emitFinishInit(E);
1437 }
1438
1439 if (QT->isArrayType()) {
1440 if (Inits.size() == 1 && QT == Inits[0]->getType())
1441 return this->delegate(Inits[0]);
1442
1443 unsigned ElementIndex = 0;
1444 for (const Expr *Init : Inits) {
1445 if (const auto *EmbedS =
1446 dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
1447 PrimType TargetT = classifyPrim(Init->getType());
1448
1449 auto Eval = [&](const Expr *Init, unsigned ElemIndex) {
1450 PrimType InitT = classifyPrim(Init->getType());
1451 if (!this->visit(Init))
1452 return false;
1453 if (InitT != TargetT) {
1454 if (!this->emitCast(InitT, TargetT, E))
1455 return false;
1456 }
1457 return this->emitInitElem(TargetT, ElemIndex, Init);
1458 };
1459 if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
1460 return false;
1461 } else {
1462 if (!this->visitArrayElemInit(ElementIndex, Init))
1463 return false;
1464 ++ElementIndex;
1465 }
1466 }
1467
1468 // Expand the filler expression.
1469 // FIXME: This should go away.
1470 if (ArrayFiller) {
1471 const ConstantArrayType *CAT =
1472 Ctx.getASTContext().getAsConstantArrayType(QT);
1473 uint64_t NumElems = CAT->getZExtSize();
1474
1475 for (; ElementIndex != NumElems; ++ElementIndex) {
1476 if (!this->visitArrayElemInit(ElementIndex, ArrayFiller))
1477 return false;
1478 }
1479 }
1480
1481 return this->emitFinishInit(E);
1482 }
1483
1484 if (const auto *ComplexTy = QT->getAs<ComplexType>()) {
1485 unsigned NumInits = Inits.size();
1486
1487 if (NumInits == 1)
1488 return this->delegate(Inits[0]);
1489
1490 QualType ElemQT = ComplexTy->getElementType();
1491 PrimType ElemT = classifyPrim(ElemQT);
1492 if (NumInits == 0) {
1493 // Zero-initialize both elements.
1494 for (unsigned I = 0; I < 2; ++I) {
1495 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1496 return false;
1497 if (!this->emitInitElem(ElemT, I, E))
1498 return false;
1499 }
1500 } else if (NumInits == 2) {
1501 unsigned InitIndex = 0;
1502 for (const Expr *Init : Inits) {
1503 if (!this->visit(Init))
1504 return false;
1505
1506 if (!this->emitInitElem(ElemT, InitIndex, E))
1507 return false;
1508 ++InitIndex;
1509 }
1510 }
1511 return true;
1512 }
1513
1514 if (const auto *VecT = QT->getAs<VectorType>()) {
1515 unsigned NumVecElements = VecT->getNumElements();
1516 assert(NumVecElements >= Inits.size());
1517
1518 QualType ElemQT = VecT->getElementType();
1519 PrimType ElemT = classifyPrim(ElemQT);
1520
1521 // All initializer elements.
1522 unsigned InitIndex = 0;
1523 for (const Expr *Init : Inits) {
1524 if (!this->visit(Init))
1525 return false;
1526
1527 // If the initializer is of vector type itself, we have to deconstruct
1528 // that and initialize all the target fields from the initializer fields.
1529 if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) {
1530 if (!this->emitCopyArray(ElemT, 0, InitIndex,
1531 InitVecT->getNumElements(), E))
1532 return false;
1533 InitIndex += InitVecT->getNumElements();
1534 } else {
1535 if (!this->emitInitElem(ElemT, InitIndex, E))
1536 return false;
1537 ++InitIndex;
1538 }
1539 }
1540
1541 assert(InitIndex <= NumVecElements);
1542
1543 // Fill the rest with zeroes.
1544 for (; InitIndex != NumVecElements; ++InitIndex) {
1545 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1546 return false;
1547 if (!this->emitInitElem(ElemT, InitIndex, E))
1548 return false;
1549 }
1550 return true;
1551 }
1552
1553 return false;
1554}
1555
1556/// Pointer to the array(not the element!) must be on the stack when calling
1557/// this.
1558template <class Emitter>
1560 const Expr *Init) {
1561 if (std::optional<PrimType> T = classify(Init->getType())) {
1562 // Visit the primitive element like normal.
1563 if (!this->visit(Init))
1564 return false;
1565 return this->emitInitElem(*T, ElemIndex, Init);
1566 }
1567
1568 InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
1569 // Advance the pointer currently on the stack to the given
1570 // dimension.
1571 if (!this->emitConstUint32(ElemIndex, Init))
1572 return false;
1573 if (!this->emitArrayElemPtrUint32(Init))
1574 return false;
1575 if (!this->visitInitializer(Init))
1576 return false;
1577 return this->emitFinishInitPop(Init);
1578}
1579
1580template <class Emitter>
1582 return this->visitInitList(E->inits(), E->getArrayFiller(), E);
1583}
1584
1585template <class Emitter>
1587 const CXXParenListInitExpr *E) {
1588 return this->visitInitList(E->getInitExprs(), E->getArrayFiller(), E);
1589}
1590
1591template <class Emitter>
1594 return this->delegate(E->getReplacement());
1595}
1596
1597template <class Emitter>
1599 std::optional<PrimType> T = classify(E->getType());
1600 if (T && E->hasAPValueResult()) {
1601 // Try to emit the APValue directly, without visiting the subexpr.
1602 // This will only fail if we can't emit the APValue, so won't emit any
1603 // diagnostics or any double values.
1604 if (DiscardResult)
1605 return true;
1606
1607 if (this->visitAPValue(E->getAPValueResult(), *T, E))
1608 return true;
1609 }
1610 return this->delegate(E->getSubExpr());
1611}
1612
1613template <class Emitter>
1615 auto It = E->begin();
1616 return this->visit(*It);
1617}
1618
1620 UnaryExprOrTypeTrait Kind) {
1621 bool AlignOfReturnsPreferred =
1622 ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;
1623
1624 // C++ [expr.alignof]p3:
1625 // When alignof is applied to a reference type, the result is the
1626 // alignment of the referenced type.
1627 if (const auto *Ref = T->getAs<ReferenceType>())
1628 T = Ref->getPointeeType();
1629
1630 if (T.getQualifiers().hasUnaligned())
1631 return CharUnits::One();
1632
1633 // __alignof is defined to return the preferred alignment.
1634 // Before 8, clang returned the preferred alignment for alignof and
1635 // _Alignof as well.
1636 if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
1637 return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T));
1638
1639 return ASTCtx.getTypeAlignInChars(T);
1640}
1641
1642template <class Emitter>
1644 const UnaryExprOrTypeTraitExpr *E) {
1645 UnaryExprOrTypeTrait Kind = E->getKind();
1646 const ASTContext &ASTCtx = Ctx.getASTContext();
1647
1648 if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) {
1649 QualType ArgType = E->getTypeOfArgument();
1650
1651 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1652 // the result is the size of the referenced type."
1653 if (const auto *Ref = ArgType->getAs<ReferenceType>())
1654 ArgType = Ref->getPointeeType();
1655
1656 CharUnits Size;
1657 if (ArgType->isVoidType() || ArgType->isFunctionType())
1658 Size = CharUnits::One();
1659 else {
1660 if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
1661 return false;
1662
1663 if (Kind == UETT_SizeOf)
1664 Size = ASTCtx.getTypeSizeInChars(ArgType);
1665 else
1666 Size = ASTCtx.getTypeInfoDataSizeInChars(ArgType).Width;
1667 }
1668
1669 if (DiscardResult)
1670 return true;
1671
1672 return this->emitConst(Size.getQuantity(), E);
1673 }
1674
1675 if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
1676 CharUnits Size;
1677
1678 if (E->isArgumentType()) {
1679 QualType ArgType = E->getTypeOfArgument();
1680
1681 Size = AlignOfType(ArgType, ASTCtx, Kind);
1682 } else {
1683 // Argument is an expression, not a type.
1684 const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
1685
1686 // The kinds of expressions that we have special-case logic here for
1687 // should be kept up to date with the special checks for those
1688 // expressions in Sema.
1689
1690 // alignof decl is always accepted, even if it doesn't make sense: we
1691 // default to 1 in those cases.
1692 if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg))
1693 Size = ASTCtx.getDeclAlign(DRE->getDecl(),
1694 /*RefAsPointee*/ true);
1695 else if (const auto *ME = dyn_cast<MemberExpr>(Arg))
1696 Size = ASTCtx.getDeclAlign(ME->getMemberDecl(),
1697 /*RefAsPointee*/ true);
1698 else
1699 Size = AlignOfType(Arg->getType(), ASTCtx, Kind);
1700 }
1701
1702 if (DiscardResult)
1703 return true;
1704
1705 return this->emitConst(Size.getQuantity(), E);
1706 }
1707
1708 if (Kind == UETT_VectorElements) {
1709 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
1710 return this->emitConst(VT->getNumElements(), E);
1711 assert(E->getTypeOfArgument()->isSizelessVectorType());
1712 return this->emitSizelessVectorElementSize(E);
1713 }
1714
1715 if (Kind == UETT_VecStep) {
1716 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) {
1717 unsigned N = VT->getNumElements();
1718
1719 // The vec_step built-in functions that take a 3-component
1720 // vector return 4. (OpenCL 1.1 spec 6.11.12)
1721 if (N == 3)
1722 N = 4;
1723
1724 return this->emitConst(N, E);
1725 }
1726 return this->emitConst(1, E);
1727 }
1728
1729 return false;
1730}
1731
1732template <class Emitter>
1734 // 'Base.Member'
1735 const Expr *Base = E->getBase();
1736 const ValueDecl *Member = E->getMemberDecl();
1737
1738 if (DiscardResult)
1739 return this->discard(Base);
1740
1741 // MemberExprs are almost always lvalues, in which case we don't need to
1742 // do the load. But sometimes they aren't.
1743 const auto maybeLoadValue = [&]() -> bool {
1744 if (E->isGLValue())
1745 return true;
1746 if (std::optional<PrimType> T = classify(E))
1747 return this->emitLoadPop(*T, E);
1748 return false;
1749 };
1750
1751 if (const auto *VD = dyn_cast<VarDecl>(Member)) {
1752 // I am almost confident in saying that a var decl must be static
1753 // and therefore registered as a global variable. But this will probably
1754 // turn out to be wrong some time in the future, as always.
1755 if (auto GlobalIndex = P.getGlobal(VD))
1756 return this->emitGetPtrGlobal(*GlobalIndex, E) && maybeLoadValue();
1757 return false;
1758 }
1759
1760 if (!isa<FieldDecl>(Member))
1761 return this->discard(Base) && this->visitDeclRef(Member, E);
1762
1763 if (Initializing) {
1764 if (!this->delegate(Base))
1765 return false;
1766 } else {
1767 if (!this->visit(Base))
1768 return false;
1769 }
1770
1771 // Base above gives us a pointer on the stack.
1772 const auto *FD = cast<FieldDecl>(Member);
1773 const RecordDecl *RD = FD->getParent();
1774 const Record *R = getRecord(RD);
1775 if (!R)
1776 return false;
1777 const Record::Field *F = R->getField(FD);
1778 // Leave a pointer to the field on the stack.
1779 if (F->Decl->getType()->isReferenceType())
1780 return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue();
1781 return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue();
1782}
1783
1784template <class Emitter>
1786 // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
1787 // stand-alone, e.g. via EvaluateAsInt().
1788 if (!ArrayIndex)
1789 return false;
1790 return this->emitConst(*ArrayIndex, E);
1791}
1792
1793template <class Emitter>
1795 assert(Initializing);
1796 assert(!DiscardResult);
1797
1798 // We visit the common opaque expression here once so we have its value
1799 // cached.
1800 if (!this->discard(E->getCommonExpr()))
1801 return false;
1802
1803 // TODO: This compiles to quite a lot of bytecode if the array is larger.
1804 // Investigate compiling this to a loop.
1805 const Expr *SubExpr = E->getSubExpr();
1806 size_t Size = E->getArraySize().getZExtValue();
1807
1808 // So, every iteration, we execute an assignment here
1809 // where the LHS is on the stack (the target array)
1810 // and the RHS is our SubExpr.
1811 for (size_t I = 0; I != Size; ++I) {
1812 ArrayIndexScope<Emitter> IndexScope(this, I);
1813 BlockScope<Emitter> BS(this);
1814
1815 if (!this->visitArrayElemInit(I, SubExpr))
1816 return false;
1817 if (!BS.destroyLocals())
1818 return false;
1819 }
1820 return true;
1821}
1822
1823template <class Emitter>
1825 const Expr *SourceExpr = E->getSourceExpr();
1826 if (!SourceExpr)
1827 return false;
1828
1829 if (Initializing)
1830 return this->visitInitializer(SourceExpr);
1831
1832 PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr);
1833 if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end())
1834 return this->emitGetLocal(SubExprT, It->second, E);
1835
1836 if (!this->visit(SourceExpr))
1837 return false;
1838
1839 // At this point we either have the evaluated source expression or a pointer
1840 // to an object on the stack. We want to create a local variable that stores
1841 // this value.
1842 unsigned LocalIndex = allocateLocalPrimitive(E, SubExprT, /*IsConst=*/true);
1843 if (!this->emitSetLocal(SubExprT, LocalIndex, E))
1844 return false;
1845
1846 // Here the local variable is created but the value is removed from the stack,
1847 // so we put it back if the caller needs it.
1848 if (!DiscardResult) {
1849 if (!this->emitGetLocal(SubExprT, LocalIndex, E))
1850 return false;
1851 }
1852
1853 // This is cleaned up when the local variable is destroyed.
1854 OpaqueExprs.insert({E, LocalIndex});
1855
1856 return true;
1857}
1858
1859template <class Emitter>
1862 const Expr *Condition = E->getCond();
1863 const Expr *TrueExpr = E->getTrueExpr();
1864 const Expr *FalseExpr = E->getFalseExpr();
1865
1866 LabelTy LabelEnd = this->getLabel(); // Label after the operator.
1867 LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
1868
1869 if (!this->visitBool(Condition))
1870 return false;
1871
1872 if (!this->jumpFalse(LabelFalse))
1873 return false;
1874
1875 {
1876 LocalScope<Emitter> S(this);
1877 if (!this->delegate(TrueExpr))
1878 return false;
1879 if (!S.destroyLocals())
1880 return false;
1881 }
1882
1883 if (!this->jump(LabelEnd))
1884 return false;
1885
1886 this->emitLabel(LabelFalse);
1887
1888 {
1889 LocalScope<Emitter> S(this);
1890 if (!this->delegate(FalseExpr))
1891 return false;
1892 if (!S.destroyLocals())
1893 return false;
1894 }
1895
1896 this->fallthrough(LabelEnd);
1897 this->emitLabel(LabelEnd);
1898
1899 return true;
1900}
1901
1902template <class Emitter>
1904 if (DiscardResult)
1905 return true;
1906
1907 if (!Initializing) {
1908 unsigned StringIndex = P.createGlobalString(E);
1909 return this->emitGetPtrGlobal(StringIndex, E);
1910 }
1911
1912 // We are initializing an array on the stack.
1913 const ConstantArrayType *CAT =
1914 Ctx.getASTContext().getAsConstantArrayType(E->getType());
1915 assert(CAT && "a string literal that's not a constant array?");
1916
1917 // If the initializer string is too long, a diagnostic has already been
1918 // emitted. Read only the array length from the string literal.
1919 unsigned ArraySize = CAT->getZExtSize();
1920 unsigned N = std::min(ArraySize, E->getLength());
1921 size_t CharWidth = E->getCharByteWidth();
1922
1923 for (unsigned I = 0; I != N; ++I) {
1924 uint32_t CodeUnit = E->getCodeUnit(I);
1925
1926 if (CharWidth == 1) {
1927 this->emitConstSint8(CodeUnit, E);
1928 this->emitInitElemSint8(I, E);
1929 } else if (CharWidth == 2) {
1930 this->emitConstUint16(CodeUnit, E);
1931 this->emitInitElemUint16(I, E);
1932 } else if (CharWidth == 4) {
1933 this->emitConstUint32(CodeUnit, E);
1934 this->emitInitElemUint32(I, E);
1935 } else {
1936 llvm_unreachable("unsupported character width");
1937 }
1938 }
1939
1940 // Fill up the rest of the char array with NUL bytes.
1941 for (unsigned I = N; I != ArraySize; ++I) {
1942 if (CharWidth == 1) {
1943 this->emitConstSint8(0, E);
1944 this->emitInitElemSint8(I, E);
1945 } else if (CharWidth == 2) {
1946 this->emitConstUint16(0, E);
1947 this->emitInitElemUint16(I, E);
1948 } else if (CharWidth == 4) {
1949 this->emitConstUint32(0, E);
1950 this->emitInitElemUint32(I, E);
1951 } else {
1952 llvm_unreachable("unsupported character width");
1953 }
1954 }
1955
1956 return true;
1957}
1958
1959template <class Emitter>
1961 return this->delegate(E->getString());
1962}
1963
1964template <class Emitter>
1966 auto &A = Ctx.getASTContext();
1967 std::string Str;
1968 A.getObjCEncodingForType(E->getEncodedType(), Str);
1969 StringLiteral *SL =
1970 StringLiteral::Create(A, Str, StringLiteralKind::Ordinary,
1971 /*Pascal=*/false, E->getType(), E->getAtLoc());
1972 return this->delegate(SL);
1973}
1974
1975template <class Emitter>
1977 const SYCLUniqueStableNameExpr *E) {
1978 if (DiscardResult)
1979 return true;
1980
1981 assert(!Initializing);
1982
1983 auto &A = Ctx.getASTContext();
1984 std::string ResultStr = E->ComputeName(A);
1985
1986 QualType CharTy = A.CharTy.withConst();
1987 APInt Size(A.getTypeSize(A.getSizeType()), ResultStr.size() + 1);
1988 QualType ArrayTy = A.getConstantArrayType(CharTy, Size, nullptr,
1989 ArraySizeModifier::Normal, 0);
1990
1991 StringLiteral *SL =
1992 StringLiteral::Create(A, ResultStr, StringLiteralKind::Ordinary,
1993 /*Pascal=*/false, ArrayTy, E->getLocation());
1994
1995 unsigned StringIndex = P.createGlobalString(SL);
1996 return this->emitGetPtrGlobal(StringIndex, E);
1997}
1998
1999template <class Emitter>
2001 if (DiscardResult)
2002 return true;
2003 return this->emitConst(E->getValue(), E);
2004}
2005
2006template <class Emitter>
2008 const CompoundAssignOperator *E) {
2009
2010 const Expr *LHS = E->getLHS();
2011 const Expr *RHS = E->getRHS();
2012 QualType LHSType = LHS->getType();
2013 QualType LHSComputationType = E->getComputationLHSType();
2014 QualType ResultType = E->getComputationResultType();
2015 std::optional<PrimType> LT = classify(LHSComputationType);
2016 std::optional<PrimType> RT = classify(ResultType);
2017
2018 assert(ResultType->isFloatingType());
2019
2020 if (!LT || !RT)
2021 return false;
2022
2023 PrimType LHST = classifyPrim(LHSType);
2024
2025 // C++17 onwards require that we evaluate the RHS first.
2026 // Compute RHS and save it in a temporary variable so we can
2027 // load it again later.
2028 if (!visit(RHS))
2029 return false;
2030
2031 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2032 if (!this->emitSetLocal(*RT, TempOffset, E))
2033 return false;
2034
2035 // First, visit LHS.
2036 if (!visit(LHS))
2037 return false;
2038 if (!this->emitLoad(LHST, E))
2039 return false;
2040
2041 // If necessary, convert LHS to its computation type.
2042 if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
2043 LHSComputationType, E))
2044 return false;
2045
2046 // Now load RHS.
2047 if (!this->emitGetLocal(*RT, TempOffset, E))
2048 return false;
2049
2050 llvm::RoundingMode RM = getRoundingMode(E);
2051 switch (E->getOpcode()) {
2052 case BO_AddAssign:
2053 if (!this->emitAddf(RM, E))
2054 return false;
2055 break;
2056 case BO_SubAssign:
2057 if (!this->emitSubf(RM, E))
2058 return false;
2059 break;
2060 case BO_MulAssign:
2061 if (!this->emitMulf(RM, E))
2062 return false;
2063 break;
2064 case BO_DivAssign:
2065 if (!this->emitDivf(RM, E))
2066 return false;
2067 break;
2068 default:
2069 return false;
2070 }
2071
2072 if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
2073 return false;
2074
2075 if (DiscardResult)
2076 return this->emitStorePop(LHST, E);
2077 return this->emitStore(LHST, E);
2078}
2079
2080template <class Emitter>
2082 const CompoundAssignOperator *E) {
2083 BinaryOperatorKind Op = E->getOpcode();
2084 const Expr *LHS = E->getLHS();
2085 const Expr *RHS = E->getRHS();
2086 std::optional<PrimType> LT = classify(LHS->getType());
2087 std::optional<PrimType> RT = classify(RHS->getType());
2088
2089 if (Op != BO_AddAssign && Op != BO_SubAssign)
2090 return false;
2091
2092 if (!LT || !RT)
2093 return false;
2094
2095 if (!visit(LHS))
2096 return false;
2097
2098 if (!this->emitLoad(*LT, LHS))
2099 return false;
2100
2101 if (!visit(RHS))
2102 return false;
2103
2104 if (Op == BO_AddAssign) {
2105 if (!this->emitAddOffset(*RT, E))
2106 return false;
2107 } else {
2108 if (!this->emitSubOffset(*RT, E))
2109 return false;
2110 }
2111
2112 if (DiscardResult)
2113 return this->emitStorePopPtr(E);
2114 return this->emitStorePtr(E);
2115}
2116
2117template <class Emitter>
2119 const CompoundAssignOperator *E) {
2120
2121 const Expr *LHS = E->getLHS();
2122 const Expr *RHS = E->getRHS();
2123 std::optional<PrimType> LHSComputationT =
2124 classify(E->getComputationLHSType());
2125 std::optional<PrimType> LT = classify(LHS->getType());
2126 std::optional<PrimType> RT = classify(RHS->getType());
2127 std::optional<PrimType> ResultT = classify(E->getType());
2128
2129 if (!Ctx.getLangOpts().CPlusPlus14)
2130 return this->visit(RHS) && this->visit(LHS) && this->emitError(E);
2131
2132 if (!LT || !RT || !ResultT || !LHSComputationT)
2133 return false;
2134
2135 // Handle floating point operations separately here, since they
2136 // require special care.
2137
2138 if (ResultT == PT_Float || RT == PT_Float)
2139 return VisitFloatCompoundAssignOperator(E);
2140
2141 if (E->getType()->isPointerType())
2142 return VisitPointerCompoundAssignOperator(E);
2143
2144 assert(!E->getType()->isPointerType() && "Handled above");
2145 assert(!E->getType()->isFloatingType() && "Handled above");
2146
2147 // C++17 onwards require that we evaluate the RHS first.
2148 // Compute RHS and save it in a temporary variable so we can
2149 // load it again later.
2150 // FIXME: Compound assignments are unsequenced in C, so we might
2151 // have to figure out how to reject them.
2152 if (!visit(RHS))
2153 return false;
2154
2155 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2156
2157 if (!this->emitSetLocal(*RT, TempOffset, E))
2158 return false;
2159
2160 // Get LHS pointer, load its value and cast it to the
2161 // computation type if necessary.
2162 if (!visit(LHS))
2163 return false;
2164 if (!this->emitLoad(*LT, E))
2165 return false;
2166 if (LT != LHSComputationT) {
2167 if (!this->emitCast(*LT, *LHSComputationT, E))
2168 return false;
2169 }
2170
2171 // Get the RHS value on the stack.
2172 if (!this->emitGetLocal(*RT, TempOffset, E))
2173 return false;
2174
2175 // Perform operation.
2176 switch (E->getOpcode()) {
2177 case BO_AddAssign:
2178 if (!this->emitAdd(*LHSComputationT, E))
2179 return false;
2180 break;
2181 case BO_SubAssign:
2182 if (!this->emitSub(*LHSComputationT, E))
2183 return false;
2184 break;
2185 case BO_MulAssign:
2186 if (!this->emitMul(*LHSComputationT, E))
2187 return false;
2188 break;
2189 case BO_DivAssign:
2190 if (!this->emitDiv(*LHSComputationT, E))
2191 return false;
2192 break;
2193 case BO_RemAssign:
2194 if (!this->emitRem(*LHSComputationT, E))
2195 return false;
2196 break;
2197 case BO_ShlAssign:
2198 if (!this->emitShl(*LHSComputationT, *RT, E))
2199 return false;
2200 break;
2201 case BO_ShrAssign:
2202 if (!this->emitShr(*LHSComputationT, *RT, E))
2203 return false;
2204 break;
2205 case BO_AndAssign:
2206 if (!this->emitBitAnd(*LHSComputationT, E))
2207 return false;
2208 break;
2209 case BO_XorAssign:
2210 if (!this->emitBitXor(*LHSComputationT, E))
2211 return false;
2212 break;
2213 case BO_OrAssign:
2214 if (!this->emitBitOr(*LHSComputationT, E))
2215 return false;
2216 break;
2217 default:
2218 llvm_unreachable("Unimplemented compound assign operator");
2219 }
2220
2221 // And now cast from LHSComputationT to ResultT.
2222 if (ResultT != LHSComputationT) {
2223 if (!this->emitCast(*LHSComputationT, *ResultT, E))
2224 return false;
2225 }
2226
2227 // And store the result in LHS.
2228 if (DiscardResult) {
2229 if (LHS->refersToBitField())
2230 return this->emitStoreBitFieldPop(*ResultT, E);
2231 return this->emitStorePop(*ResultT, E);
2232 }
2233 if (LHS->refersToBitField())
2234 return this->emitStoreBitField(*ResultT, E);
2235 return this->emitStore(*ResultT, E);
2236}
2237
2238template <class Emitter>
2240 LocalScope<Emitter> ES(this);
2241 const Expr *SubExpr = E->getSubExpr();
2242
2243 return this->delegate(SubExpr) && ES.destroyLocals(E);
2244}
2245
2246template <class Emitter>
2248 const MaterializeTemporaryExpr *E) {
2249 const Expr *SubExpr = E->getSubExpr();
2250
2251 if (Initializing) {
2252 // We already have a value, just initialize that.
2253 return this->delegate(SubExpr);
2254 }
2255 // If we don't end up using the materialized temporary anyway, don't
2256 // bother creating it.
2257 if (DiscardResult)
2258 return this->discard(SubExpr);
2259
2260 // When we're initializing a global variable *or* the storage duration of
2261 // the temporary is explicitly static, create a global variable.
2262 std::optional<PrimType> SubExprT = classify(SubExpr);
2263 bool IsStatic = E->getStorageDuration() == SD_Static;
2264 if (IsStatic) {
2265 std::optional<unsigned> GlobalIndex = P.createGlobal(E);
2266 if (!GlobalIndex)
2267 return false;
2268
2269 const LifetimeExtendedTemporaryDecl *TempDecl =
2270 E->getLifetimeExtendedTemporaryDecl();
2271 if (IsStatic)
2272 assert(TempDecl);
2273
2274 if (SubExprT) {
2275 if (!this->visit(SubExpr))
2276 return false;
2277 if (IsStatic) {
2278 if (!this->emitInitGlobalTemp(*SubExprT, *GlobalIndex, TempDecl, E))
2279 return false;
2280 } else {
2281 if (!this->emitInitGlobal(*SubExprT, *GlobalIndex, E))
2282 return false;
2283 }
2284 return this->emitGetPtrGlobal(*GlobalIndex, E);
2285 }
2286
2287 // Non-primitive values.
2288 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2289 return false;
2290 if (!this->visitInitializer(SubExpr))
2291 return false;
2292 if (IsStatic)
2293 return this->emitInitGlobalTempComp(TempDecl, E);
2294 return true;
2295 }
2296
2297 // For everyhing else, use local variables.
2298 if (SubExprT) {
2299 unsigned LocalIndex = allocateLocalPrimitive(
2300 SubExpr, *SubExprT, /*IsConst=*/true, /*IsExtended=*/true);
2301 if (!this->visit(SubExpr))
2302 return false;
2303 if (!this->emitSetLocal(*SubExprT, LocalIndex, E))
2304 return false;
2305 return this->emitGetPtrLocal(LocalIndex, E);
2306 } else {
2307 const Expr *Inner = E->getSubExpr()->skipRValueSubobjectAdjustments();
2308 if (std::optional<unsigned> LocalIndex =
2309 allocateLocal(Inner, E->getExtendingDecl())) {
2310 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
2311 if (!this->emitGetPtrLocal(*LocalIndex, E))
2312 return false;
2313 return this->visitInitializer(SubExpr);
2314 }
2315 }
2316 return false;
2317}
2318
2319template <class Emitter>
2321 const CXXBindTemporaryExpr *E) {
2322 return this->delegate(E->getSubExpr());
2323}
2324
2325template <class Emitter>
2327 const Expr *Init = E->getInitializer();
2328 if (DiscardResult)
2329 return this->discard(Init);
2330
2331 if (Initializing) {
2332 // We already have a value, just initialize that.
2333 return this->visitInitializer(Init) && this->emitFinishInit(E);
2334 }
2335
2336 std::optional<PrimType> T = classify(E->getType());
2337 if (E->isFileScope()) {
2338 // Avoid creating a variable if this is a primitive RValue anyway.
2339 if (T && !E->isLValue())
2340 return this->delegate(Init);
2341
2342 if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
2343 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2344 return false;
2345
2346 if (T) {
2347 if (!this->visit(Init))
2348 return false;
2349 return this->emitInitGlobal(*T, *GlobalIndex, E);
2350 }
2351
2352 return this->visitInitializer(Init) && this->emitFinishInit(E);
2353 }
2354
2355 return false;
2356 }
2357
2358 // Otherwise, use a local variable.
2359 if (T && !E->isLValue()) {
2360 // For primitive types, we just visit the initializer.
2361 return this->delegate(Init);
2362 } else {
2363 unsigned LocalIndex;
2364
2365 if (T)
2366 LocalIndex = this->allocateLocalPrimitive(Init, *T, false, false);
2367 else if (std::optional<unsigned> MaybeIndex = this->allocateLocal(Init))
2368 LocalIndex = *MaybeIndex;
2369 else
2370 return false;
2371
2372 if (!this->emitGetPtrLocal(LocalIndex, E))
2373 return false;
2374
2375 if (T) {
2376 if (!this->visit(Init)) {
2377 return false;
2378 }
2379 return this->emitInit(*T, E);
2380 } else {
2381 if (!this->visitInitializer(Init) || !this->emitFinishInit(E))
2382 return false;
2383 }
2384 return true;
2385 }
2386
2387 return false;
2388}
2389
2390template <class Emitter>
2392 if (DiscardResult)
2393 return true;
2394 if (E->getType()->isBooleanType())
2395 return this->emitConstBool(E->getValue(), E);
2396 return this->emitConst(E->getValue(), E);
2397}
2398
2399template <class Emitter>
2401 if (DiscardResult)
2402 return true;
2403 return this->emitConst(E->getValue(), E);
2404}
2405
2406template <class Emitter>
2408 if (DiscardResult)
2409 return true;
2410
2411 assert(Initializing);
2412 const Record *R = P.getOrCreateRecord(E->getLambdaClass());
2413
2414 auto *CaptureInitIt = E->capture_init_begin();
2415 // Initialize all fields (which represent lambda captures) of the
2416 // record with their initializers.
2417 for (const Record::Field &F : R->fields()) {
2418 const Expr *Init = *CaptureInitIt;
2419 ++CaptureInitIt;
2420
2421 if (!Init)
2422 continue;
2423
2424 if (std::optional<PrimType> T = classify(Init)) {
2425 if (!this->visit(Init))
2426 return false;
2427
2428 if (!this->emitInitField(*T, F.Offset, E))
2429 return false;
2430 } else {
2431 if (!this->emitGetPtrField(F.Offset, E))
2432 return false;
2433
2434 if (!this->visitInitializer(Init))
2435 return false;
2436
2437 if (!this->emitPopPtr(E))
2438 return false;
2439 }
2440 }
2441
2442 return true;
2443}
2444
2445template <class Emitter>
2447 if (DiscardResult)
2448 return true;
2449
2450 return this->delegate(E->getFunctionName());
2451}
2452
2453template <class Emitter>
2455 if (E->getSubExpr() && !this->discard(E->getSubExpr()))
2456 return false;
2457
2458 return this->emitInvalid(E);
2459}
2460
2461template <class Emitter>
2463 const CXXReinterpretCastExpr *E) {
2464 const Expr *SubExpr = E->getSubExpr();
2465
2466 bool TypesMatch = classify(E) == classify(SubExpr);
2467 if (!this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/!TypesMatch, E))
2468 return false;
2469
2470 return this->delegate(SubExpr);
2471}
2472
2473template <class Emitter>
2475 assert(E->getType()->isBooleanType());
2476
2477 if (DiscardResult)
2478 return true;
2479 return this->emitConstBool(E->getValue(), E);
2480}
2481
2482template <class Emitter>
2484 QualType T = E->getType();
2485 assert(!classify(T));
2486
2487 if (T->isRecordType()) {
2488 const CXXConstructorDecl *Ctor = E->getConstructor();
2489
2490 // Trivial copy/move constructor. Avoid copy.
2491 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
2492 Ctor->isTrivial() &&
2493 E->getArg(0)->isTemporaryObject(Ctx.getASTContext(),
2495 return this->visitInitializer(E->getArg(0));
2496
2497 // If we're discarding a construct expression, we still need
2498 // to allocate a variable and call the constructor and destructor.
2499 if (DiscardResult) {
2500 if (Ctor->isTrivial())
2501 return true;
2502 assert(!Initializing);
2503 std::optional<unsigned> LocalIndex = allocateLocal(E);
2504
2505 if (!LocalIndex)
2506 return false;
2507
2508 if (!this->emitGetPtrLocal(*LocalIndex, E))
2509 return false;
2510 }
2511
2512 // Zero initialization.
2513 if (E->requiresZeroInitialization()) {
2514 const Record *R = getRecord(E->getType());
2515
2516 if (!this->visitZeroRecordInitializer(R, E))
2517 return false;
2518
2519 // If the constructor is trivial anyway, we're done.
2520 if (Ctor->isTrivial())
2521 return true;
2522 }
2523
2524 const Function *Func = getFunction(Ctor);
2525
2526 if (!Func)
2527 return false;
2528
2529 assert(Func->hasThisPointer());
2530 assert(!Func->hasRVO());
2531
2532 // The This pointer is already on the stack because this is an initializer,
2533 // but we need to dup() so the call() below has its own copy.
2534 if (!this->emitDupPtr(E))
2535 return false;
2536
2537 // Constructor arguments.
2538 for (const auto *Arg : E->arguments()) {
2539 if (!this->visit(Arg))
2540 return false;
2541 }
2542
2543 if (Func->isVariadic()) {
2544 uint32_t VarArgSize = 0;
2545 unsigned NumParams = Func->getNumWrittenParams();
2546 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) {
2547 VarArgSize +=
2548 align(primSize(classify(E->getArg(I)->getType()).value_or(PT_Ptr)));
2549 }
2550 if (!this->emitCallVar(Func, VarArgSize, E))
2551 return false;
2552 } else {
2553 if (!this->emitCall(Func, 0, E))
2554 return false;
2555 }
2556
2557 if (DiscardResult)
2558 return this->emitPopPtr(E);
2559 return this->emitFinishInit(E);
2560 }
2561
2562 if (T->isArrayType()) {
2563 const ConstantArrayType *CAT =
2564 Ctx.getASTContext().getAsConstantArrayType(E->getType());
2565 if (!CAT)
2566 return false;
2567
2568 size_t NumElems = CAT->getZExtSize();
2569 const Function *Func = getFunction(E->getConstructor());
2570 if (!Func || !Func->isConstexpr())
2571 return false;
2572
2573 // FIXME(perf): We're calling the constructor once per array element here,
2574 // in the old intepreter we had a special-case for trivial constructors.
2575 for (size_t I = 0; I != NumElems; ++I) {
2576 if (!this->emitConstUint64(I, E))
2577 return false;
2578 if (!this->emitArrayElemPtrUint64(E))
2579 return false;
2580
2581 // Constructor arguments.
2582 for (const auto *Arg : E->arguments()) {
2583 if (!this->visit(Arg))
2584 return false;
2585 }
2586
2587 if (!this->emitCall(Func, 0, E))
2588 return false;
2589 }
2590 return true;
2591 }
2592
2593 return false;
2594}
2595
2596template <class Emitter>
2598 if (DiscardResult)
2599 return true;
2600
2601 const APValue Val =
2602 E->EvaluateInContext(Ctx.getASTContext(), SourceLocDefaultExpr);
2603
2604 // Things like __builtin_LINE().
2605 if (E->getType()->isIntegerType()) {
2606 assert(Val.isInt());
2607 const APSInt &I = Val.getInt();
2608 return this->emitConst(I, E);
2609 }
2610 // Otherwise, the APValue is an LValue, with only one element.
2611 // Theoretically, we don't need the APValue at all of course.
2612 assert(E->getType()->isPointerType());
2613 assert(Val.isLValue());
2614 const APValue::LValueBase &Base = Val.getLValueBase();
2615 if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
2616 return this->visit(LValueExpr);
2617
2618 // Otherwise, we have a decl (which is the case for
2619 // __builtin_source_location).
2620 assert(Base.is<const ValueDecl *>());
2621 assert(Val.getLValuePath().size() == 0);
2622 const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
2623 assert(BaseDecl);
2624
2625 auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
2626
2627 std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(UGCD);
2628 if (!GlobalIndex)
2629 return false;
2630
2631 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2632 return false;
2633
2634 const Record *R = getRecord(E->getType());
2635 const APValue &V = UGCD->getValue();
2636 for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
2637 const Record::Field *F = R->getField(I);
2638 const APValue &FieldValue = V.getStructField(I);
2639
2640 PrimType FieldT = classifyPrim(F->Decl->getType());
2641
2642 if (!this->visitAPValue(FieldValue, FieldT, E))
2643 return false;
2644 if (!this->emitInitField(FieldT, F->Offset, E))
2645 return false;
2646 }
2647
2648 // Leave the pointer to the global on the stack.
2649 return true;
2650}
2651
2652template <class Emitter>
2654 unsigned N = E->getNumComponents();
2655 if (N == 0)
2656 return false;
2657
2658 for (unsigned I = 0; I != N; ++I) {
2659 const OffsetOfNode &Node = E->getComponent(I);
2660 if (Node.getKind() == OffsetOfNode::Array) {
2661 const Expr *ArrayIndexExpr = E->getIndexExpr(Node.getArrayExprIndex());
2662 PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
2663
2664 if (DiscardResult) {
2665 if (!this->discard(ArrayIndexExpr))
2666 return false;
2667 continue;
2668 }
2669
2670 if (!this->visit(ArrayIndexExpr))
2671 return false;
2672 // Cast to Sint64.
2673 if (IndexT != PT_Sint64) {
2674 if (!this->emitCast(IndexT, PT_Sint64, E))
2675 return false;
2676 }
2677 }
2678 }
2679
2680 if (DiscardResult)
2681 return true;
2682
2683 PrimType T = classifyPrim(E->getType());
2684 return this->emitOffsetOf(T, E, E);
2685}
2686
2687template <class Emitter>
2689 const CXXScalarValueInitExpr *E) {
2690 QualType Ty = E->getType();
2691
2692 if (DiscardResult || Ty->isVoidType())
2693 return true;
2694
2695 if (std::optional<PrimType> T = classify(Ty))
2696 return this->visitZeroInitializer(*T, Ty, E);
2697
2698 if (const auto *CT = Ty->getAs<ComplexType>()) {
2699 if (!Initializing) {
2700 std::optional<unsigned> LocalIndex = allocateLocal(E);
2701 if (!LocalIndex)
2702 return false;
2703 if (!this->emitGetPtrLocal(*LocalIndex, E))
2704 return false;
2705 }
2706
2707 // Initialize both fields to 0.
2708 QualType ElemQT = CT->getElementType();
2709 PrimType ElemT = classifyPrim(ElemQT);
2710
2711 for (unsigned I = 0; I != 2; ++I) {
2712 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2713 return false;
2714 if (!this->emitInitElem(ElemT, I, E))
2715 return false;
2716 }
2717 return true;
2718 }
2719
2720 if (const auto *VT = Ty->getAs<VectorType>()) {
2721 // FIXME: Code duplication with the _Complex case above.
2722 if (!Initializing) {
2723 std::optional<unsigned> LocalIndex = allocateLocal(E);
2724 if (!LocalIndex)
2725 return false;
2726 if (!this->emitGetPtrLocal(*LocalIndex, E))
2727 return false;
2728 }
2729
2730 // Initialize all fields to 0.
2731 QualType ElemQT = VT->getElementType();
2732 PrimType ElemT = classifyPrim(ElemQT);
2733
2734 for (unsigned I = 0, N = VT->getNumElements(); I != N; ++I) {
2735 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2736 return false;
2737 if (!this->emitInitElem(ElemT, I, E))
2738 return false;
2739 }
2740 return true;
2741 }
2742
2743 return false;
2744}
2745
2746template <class Emitter>
2748 return this->emitConst(E->getPackLength(), E);
2749}
2750
2751template <class Emitter>
2753 const GenericSelectionExpr *E) {
2754 return this->delegate(E->getResultExpr());
2755}
2756
2757template <class Emitter>
2759 return this->delegate(E->getChosenSubExpr());
2760}
2761
2762template <class Emitter>
2764 if (DiscardResult)
2765 return true;
2766
2767 return this->emitConst(E->getValue(), E);
2768}
2769
2770template <class Emitter>
2772 const CXXInheritedCtorInitExpr *E) {
2773 const CXXConstructorDecl *Ctor = E->getConstructor();
2774 assert(!Ctor->isTrivial() &&
2775 "Trivial CXXInheritedCtorInitExpr, implement. (possible?)");
2776 const Function *F = this->getFunction(Ctor);
2777 assert(F);
2778 assert(!F->hasRVO());
2779 assert(F->hasThisPointer());
2780
2781 if (!this->emitDupPtr(SourceInfo{}))
2782 return false;
2783
2784 // Forward all arguments of the current function (which should be a
2785 // constructor itself) to the inherited ctor.
2786 // This is necessary because the calling code has pushed the pointer
2787 // of the correct base for us already, but the arguments need
2788 // to come after.
2789 unsigned Offset = align(primSize(PT_Ptr)); // instance pointer.
2790 for (const ParmVarDecl *PD : Ctor->parameters()) {
2791 PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr);
2792
2793 if (!this->emitGetParam(PT, Offset, E))
2794 return false;
2795 Offset += align(primSize(PT));
2796 }
2797
2798 return this->emitCall(F, 0, E);
2799}
2800
2801template <class Emitter>
2803 assert(classifyPrim(E->getType()) == PT_Ptr);
2804 const Expr *Init = E->getInitializer();
2805 QualType ElementType = E->getAllocatedType();
2806 std::optional<PrimType> ElemT = classify(ElementType);
2807 unsigned PlacementArgs = E->getNumPlacementArgs();
2808 bool IsNoThrow = false;
2809
2810 // FIXME: Better diagnostic. diag::note_constexpr_new_placement
2811 if (PlacementArgs != 0) {
2812 // The only new-placement list we support is of the form (std::nothrow).
2813 //
2814 // FIXME: There is no restriction on this, but it's not clear that any
2815 // other form makes any sense. We get here for cases such as:
2816 //
2817 // new (std::align_val_t{N}) X(int)
2818 //
2819 // (which should presumably be valid only if N is a multiple of
2820 // alignof(int), and in any case can't be deallocated unless N is
2821 // alignof(X) and X has new-extended alignment).
2822 if (PlacementArgs != 1 || !E->getPlacementArg(0)->getType()->isNothrowT())
2823 return this->emitInvalid(E);
2824
2825 if (!this->discard(E->getPlacementArg(0)))
2826 return false;
2827 IsNoThrow = true;
2828 }
2829
2830 const Descriptor *Desc;
2831 if (ElemT) {
2832 if (E->isArray())
2833 Desc = nullptr; // We're not going to use it in this case.
2834 else
2835 Desc = P.createDescriptor(E, *ElemT, Descriptor::InlineDescMD,
2836 /*IsConst=*/false, /*IsTemporary=*/false,
2837 /*IsMutable=*/false);
2838 } else {
2839 Desc = P.createDescriptor(
2840 E, ElementType.getTypePtr(),
2841 E->isArray() ? std::nullopt : Descriptor::InlineDescMD,
2842 /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false, Init);
2843 }
2844
2845 if (E->isArray()) {
2846 std::optional<const Expr *> ArraySizeExpr = E->getArraySize();
2847 if (!ArraySizeExpr)
2848 return false;
2849
2850 const Expr *Stripped = *ArraySizeExpr;
2851 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
2852 Stripped = ICE->getSubExpr())
2853 if (ICE->getCastKind() != CK_NoOp &&
2854 ICE->getCastKind() != CK_IntegralCast)
2855 break;
2856
2857 PrimType SizeT = classifyPrim(Stripped->getType());
2858
2859 if (!this->visit(Stripped))
2860 return false;
2861
2862 if (ElemT) {
2863 // N primitive elements.
2864 if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E))
2865 return false;
2866 } else {
2867 // N Composite elements.
2868 if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E))
2869 return false;
2870 }
2871
2872 if (Init && !this->visitInitializer(Init))
2873 return false;
2874
2875 } else {
2876 // Allocate just one element.
2877 if (!this->emitAlloc(Desc, E))
2878 return false;
2879
2880 if (Init) {
2881 if (ElemT) {
2882 if (!this->visit(Init))
2883 return false;
2884
2885 if (!this->emitInit(*ElemT, E))
2886 return false;
2887 } else {
2888 // Composite.
2889 if (!this->visitInitializer(Init))
2890 return false;
2891 }
2892 }
2893 }
2894
2895 if (DiscardResult)
2896 return this->emitPopPtr(E);
2897
2898 return true;
2899}
2900
2901template <class Emitter>
2903 const Expr *Arg = E->getArgument();
2904
2905 // Arg must be an lvalue.
2906 if (!this->visit(Arg))
2907 return false;
2908
2909 return this->emitFree(E->isArrayForm(), E);
2910}
2911
2912template <class Emitter>
2914 const Function *Func = nullptr;
2915 if (auto F = Compiler<ByteCodeEmitter>(Ctx, P).compileObjCBlock(E))
2916 Func = F;
2917
2918 if (!Func)
2919 return false;
2920 return this->emitGetFnPtr(Func, E);
2921}
2922
2923template <class Emitter>
2925 assert(Ctx.getLangOpts().CPlusPlus);
2926 return this->emitConstBool(E->getValue(), E);
2927}
2928
2929template <class Emitter>
2931 if (DiscardResult)
2932 return true;
2933 assert(!Initializing);
2934
2935 const MSGuidDecl *GuidDecl = E->getGuidDecl();
2936 const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl();
2937 assert(RD);
2938 // If the definiton of the result type is incomplete, just return a dummy.
2939 // If (and when) that is read from, we will fail, but not now.
2940 if (!RD->isCompleteDefinition()) {
2941 if (std::optional<unsigned> I = P.getOrCreateDummy(GuidDecl))
2942 return this->emitGetPtrGlobal(*I, E);
2943 return false;
2944 }
2945
2946 std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(GuidDecl);
2947 if (!GlobalIndex)
2948 return false;
2949 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2950 return false;
2951
2952 assert(this->getRecord(E->getType()));
2953
2954 const APValue &V = GuidDecl->getAsAPValue();
2955 if (V.getKind() == APValue::None)
2956 return true;
2957
2958 assert(V.isStruct());
2959 assert(V.getStructNumBases() == 0);
2960 if (!this->visitAPValueInitializer(V, E))
2961 return false;
2962
2963 return this->emitFinishInit(E);
2964}
2965
2966template <class Emitter>
2968 assert(classifyPrim(E->getType()) == PT_Bool);
2969 if (DiscardResult)
2970 return true;
2971 return this->emitConstBool(E->isSatisfied(), E);
2972}
2973
2974template <class Emitter>
2977 assert(classifyPrim(E->getType()) == PT_Bool);
2978 if (DiscardResult)
2979 return true;
2980 return this->emitConstBool(E->isSatisfied(), E);
2981}
2982
2983template <class Emitter>
2986 return this->delegate(E->getSemanticForm());
2987}
2988
2989template <class Emitter>
2991
2992 for (const Expr *SemE : E->semantics()) {
2993 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {
2994 if (SemE == E->getResultExpr())
2995 return false;
2996
2997 if (OVE->isUnique())
2998 continue;
2999
3000 if (!this->discard(OVE))
3001 return false;
3002 } else if (SemE == E->getResultExpr()) {
3003 if (!this->delegate(SemE))
3004 return false;
3005 } else {
3006 if (!this->discard(SemE))
3007 return false;
3008 }
3009 }
3010 return true;
3011}
3012
3013template <class Emitter>
3015 return this->delegate(E->getSelectedExpr());
3016}
3017
3018template <class Emitter>
3020 return this->emitError(E);
3021}
3022
3023template <class Emitter>
3025 assert(E->getType()->isVoidPointerType());
3026
3027 unsigned Offset = allocateLocalPrimitive(
3028 E->getLabel(), PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3029
3030 return this->emitGetLocal(PT_Ptr, Offset, E);
3031}
3032
3033template <class Emitter>
3035 assert(Initializing);
3036 const auto *VT = E->getType()->castAs<VectorType>();
3037 QualType ElemType = VT->getElementType();
3038 PrimType ElemT = classifyPrim(ElemType);
3039 const Expr *Src = E->getSrcExpr();
3040 PrimType SrcElemT =
3041 classifyPrim(Src->getType()->castAs<VectorType>()->getElementType());
3042
3043 unsigned SrcOffset = this->allocateLocalPrimitive(Src, PT_Ptr, true, false);
3044 if (!this->visit(Src))
3045 return false;
3046 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
3047 return false;
3048
3049 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
3050 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
3051 return false;
3052 if (!this->emitArrayElemPop(SrcElemT, I, E))
3053 return false;
3054 if (SrcElemT != ElemT) {
3055 if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
3056 return false;
3057 }
3058 if (!this->emitInitElem(ElemT, I, E))
3059 return false;
3060 }
3061
3062 return true;
3063}
3064
3065template <class Emitter>
3067 assert(Initializing);
3068 assert(E->getNumSubExprs() > 2);
3069
3070 const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
3071 const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
3072 PrimType ElemT = classifyPrim(VT->getElementType());
3073 unsigned NumInputElems = VT->getNumElements();
3074 unsigned NumOutputElems = E->getNumSubExprs() - 2;
3075 assert(NumOutputElems > 0);
3076
3077 // Save both input vectors to a local variable.
3078 unsigned VectorOffsets[2];
3079 for (unsigned I = 0; I != 2; ++I) {
3080 VectorOffsets[I] = this->allocateLocalPrimitive(
3081 Vecs[I], PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3082 if (!this->visit(Vecs[I]))
3083 return false;
3084 if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
3085 return false;
3086 }
3087 for (unsigned I = 0; I != NumOutputElems; ++I) {
3088 APSInt ShuffleIndex = E->getShuffleMaskIdx(Ctx.getASTContext(), I);
3089 if (ShuffleIndex == -1)
3090 return this->emitInvalid(E); // FIXME: Better diagnostic.
3091
3092 assert(ShuffleIndex < (NumInputElems * 2));
3093 if (!this->emitGetLocal(PT_Ptr,
3094 VectorOffsets[ShuffleIndex >= NumInputElems], E))
3095 return false;
3096 unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
3097 if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
3098 return false;
3099
3100 if (!this->emitInitElem(ElemT, I, E))
3101 return false;
3102 }
3103
3104 return true;
3105}
3106
3107template <class Emitter>
3109 const ExtVectorElementExpr *E) {
3110 const Expr *Base = E->getBase();
3111 assert(
3112 Base->getType()->isVectorType() ||
3113 Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
3114
3116 E->getEncodedElementAccess(Indices);
3117
3118 if (Indices.size() == 1) {
3119 if (!this->visit(Base))
3120 return false;
3121
3122 if (E->isGLValue()) {
3123 if (!this->emitConstUint32(Indices[0], E))
3124 return false;
3125 return this->emitArrayElemPtrPop(PT_Uint32, E);
3126 }
3127 // Else, also load the value.
3128 return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
3129 }
3130
3131 // Create a local variable for the base.
3132 unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true,
3133 /*IsExtended=*/false);
3134 if (!this->visit(Base))
3135 return false;
3136 if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
3137 return false;
3138
3139 // Now the vector variable for the return value.
3140 if (!Initializing) {
3141 std::optional<unsigned> ResultIndex;
3142 ResultIndex = allocateLocal(E);
3143 if (!ResultIndex)
3144 return false;
3145 if (!this->emitGetPtrLocal(*ResultIndex, E))
3146 return false;
3147 }
3148
3149 assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
3150
3151 PrimType ElemT =
3152 classifyPrim(E->getType()->getAs<VectorType>()->getElementType());
3153 uint32_t DstIndex = 0;
3154 for (uint32_t I : Indices) {
3155 if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
3156 return false;
3157 if (!this->emitArrayElemPop(ElemT, I, E))
3158 return false;
3159 if (!this->emitInitElem(ElemT, DstIndex, E))
3160 return false;
3161 ++DstIndex;
3162 }
3163
3164 // Leave the result pointer on the stack.
3165 assert(!DiscardResult);
3166 return true;
3167}
3168
3169template <class Emitter>
3171 const Expr *SubExpr = E->getSubExpr();
3172 if (!E->isExpressibleAsConstantInitializer())
3173 return this->discard(SubExpr) && this->emitInvalid(E);
3174
3175 return this->delegate(SubExpr);
3176}
3177
3178template <class Emitter>
3181 const Expr *SubExpr = E->getSubExpr();
3183 Ctx.getASTContext().getAsConstantArrayType(SubExpr->getType());
3184 const Record *R = getRecord(E->getType());
3185 assert(Initializing);
3186 assert(SubExpr->isGLValue());
3187
3188 if (!this->visit(SubExpr))
3189 return false;
3190 if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E))
3191 return false;
3192
3193 PrimType SecondFieldT = classifyPrim(R->getField(1u)->Decl->getType());
3194 if (isIntegralType(SecondFieldT)) {
3195 if (!this->emitConst(static_cast<APSInt>(ArrayType->getSize()),
3196 SecondFieldT, E))
3197 return false;
3198 return this->emitInitField(SecondFieldT, R->getField(1u)->Offset, E);
3199 }
3200 assert(SecondFieldT == PT_Ptr);
3201
3202 if (!this->emitGetFieldPtr(R->getField(0u)->Offset, E))
3203 return false;
3204 if (!this->emitConst(static_cast<APSInt>(ArrayType->getSize()), PT_Uint64, E))
3205 return false;
3206 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
3207 return false;
3208 return this->emitInitFieldPtr(R->getField(1u)->Offset, E);
3209}
3210
3211template <class Emitter>
3213 BlockScope<Emitter> BS(this);
3214 StmtExprScope<Emitter> SS(this);
3215
3216 const CompoundStmt *CS = E->getSubStmt();
3217 const Stmt *Result = CS->getStmtExprResult();
3218 for (const Stmt *S : CS->body()) {
3219 if (S != Result) {
3220 if (!this->visitStmt(S))
3221 return false;
3222 continue;
3223 }
3224
3225 assert(S == Result);
3226 if (const Expr *ResultExpr = dyn_cast<Expr>(S))
3227 return this->delegate(ResultExpr);
3228 return this->emitUnsupported(E);
3229 }
3230
3231 return BS.destroyLocals();
3232}
3233
3234template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
3235 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
3236 /*NewInitializing=*/false);
3237 return this->Visit(E);
3238}
3239
3240template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
3241 if (E->containsErrors())
3242 return this->emitError(E);
3243
3244 // We're basically doing:
3245 // OptionScope<Emitter> Scope(this, DicardResult, Initializing);
3246 // but that's unnecessary of course.
3247 return this->Visit(E);
3248}
3249
3250template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
3251 if (E->getType().isNull())
3252 return false;
3253
3254 if (E->getType()->isVoidType())
3255 return this->discard(E);
3256
3257 // Create local variable to hold the return value.
3258 if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
3259 !classify(E->getType())) {
3260 std::optional<unsigned> LocalIndex = allocateLocal(E);
3261 if (!LocalIndex)
3262 return false;
3263
3264 if (!this->emitGetPtrLocal(*LocalIndex, E))
3265 return false;
3266 return this->visitInitializer(E);
3267 }
3268
3269 // Otherwise,we have a primitive return value, produce the value directly
3270 // and push it on the stack.
3271 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
3272 /*NewInitializing=*/false);
3273 return this->Visit(E);
3274}
3275
3276template <class Emitter>
3278 assert(!classify(E->getType()));
3279
3280 if (E->containsErrors())
3281 return this->emitError(E);
3282
3283 if (!this->checkLiteralType(E))
3284 return false;
3285
3286 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
3287 /*NewInitializing=*/true);
3288 return this->Visit(E);
3289}
3290
3291template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
3292 std::optional<PrimType> T = classify(E->getType());
3293 if (!T) {
3294 // Convert complex values to bool.
3295 if (E->getType()->isAnyComplexType()) {
3296 if (!this->visit(E))
3297 return false;
3298 return this->emitComplexBoolCast(E);
3299 }
3300 return false;
3301 }
3302
3303 if (!this->visit(E))
3304 return false;
3305
3306 if (T == PT_Bool)
3307 return true;
3308
3309 // Convert pointers to bool.
3310 if (T == PT_Ptr || T == PT_FnPtr) {
3311 if (!this->emitNull(*T, nullptr, E))
3312 return false;
3313 return this->emitNE(*T, E);
3314 }
3315
3316 // Or Floats.
3317 if (T == PT_Float)
3318 return this->emitCastFloatingIntegralBool(E);
3319
3320 // Or anything else we can.
3321 return this->emitCast(*T, PT_Bool, E);
3322}
3323
3324template <class Emitter>
3326 const Expr *E) {
3327 switch (T) {
3328 case PT_Bool:
3329 return this->emitZeroBool(E);
3330 case PT_Sint8:
3331 return this->emitZeroSint8(E);
3332 case PT_Uint8:
3333 return this->emitZeroUint8(E);
3334 case PT_Sint16:
3335 return this->emitZeroSint16(E);
3336 case PT_Uint16:
3337 return this->emitZeroUint16(E);
3338 case PT_Sint32:
3339 return this->emitZeroSint32(E);
3340 case PT_Uint32:
3341 return this->emitZeroUint32(E);
3342 case PT_Sint64:
3343 return this->emitZeroSint64(E);
3344 case PT_Uint64:
3345 return this->emitZeroUint64(E);
3346 case PT_IntAP:
3347 return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
3348 case PT_IntAPS:
3349 return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
3350 case PT_Ptr:
3351 return this->emitNullPtr(nullptr, E);
3352 case PT_FnPtr:
3353 return this->emitNullFnPtr(nullptr, E);
3354 case PT_MemberPtr:
3355 return this->emitNullMemberPtr(nullptr, E);
3356 case PT_Float: {
3357 return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E);
3358 }
3359 }
3360 llvm_unreachable("unknown primitive type");
3361}
3362
3363template <class Emitter>
3365 const Expr *E) {
3366 assert(E);
3367 assert(R);
3368 // Fields
3369 for (const Record::Field &Field : R->fields()) {
3370 if (Field.Decl->isUnnamedBitField())
3371 continue;
3372
3373 const Descriptor *D = Field.Desc;
3374 if (D->isPrimitive()) {
3375 QualType QT = D->getType();
3376 PrimType T = classifyPrim(D->getType());
3377 if (!this->visitZeroInitializer(T, QT, E))
3378 return false;
3379 if (!this->emitInitField(T, Field.Offset, E))
3380 return false;
3381 if (R->isUnion())
3382 break;
3383 continue;
3384 }
3385
3386 if (!this->emitGetPtrField(Field.Offset, E))
3387 return false;
3388
3389 if (D->isPrimitiveArray()) {
3390 QualType ET = D->getElemQualType();
3391 PrimType T = classifyPrim(ET);
3392 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
3393 if (!this->visitZeroInitializer(T, ET, E))
3394 return false;
3395 if (!this->emitInitElem(T, I, E))
3396 return false;
3397 }
3398 } else if (D->isCompositeArray()) {
3399 const Record *ElemRecord = D->ElemDesc->ElemRecord;
3400 assert(D->ElemDesc->ElemRecord);
3401 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
3402 if (!this->emitConstUint32(I, E))
3403 return false;
3404 if (!this->emitArrayElemPtr(PT_Uint32, E))
3405 return false;
3406 if (!this->visitZeroRecordInitializer(ElemRecord, E))
3407 return false;
3408 if (!this->emitPopPtr(E))
3409 return false;
3410 }
3411 } else if (D->isRecord()) {
3412 if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
3413 return false;
3414 } else {
3415 assert(false);
3416 }
3417
3418 if (!this->emitFinishInitPop(E))
3419 return false;
3420
3421 if (R->isUnion())
3422 break;
3423 }
3424
3425 for (const Record::Base &B : R->bases()) {
3426 if (!this->emitGetPtrBase(B.Offset, E))
3427 return false;
3428 if (!this->visitZeroRecordInitializer(B.R, E))
3429 return false;
3430 if (!this->emitFinishInitPop(E))
3431 return false;
3432 }
3433
3434 // FIXME: Virtual bases.
3435
3436 return true;
3437}
3438
3439template <class Emitter>
3440template <typename T>
3442 switch (Ty) {
3443 case PT_Sint8:
3444 return this->emitConstSint8(Value, E);
3445 case PT_Uint8:
3446 return this->emitConstUint8(Value, E);
3447 case PT_Sint16:
3448 return this->emitConstSint16(Value, E);
3449 case PT_Uint16:
3450 return this->emitConstUint16(Value, E);
3451 case PT_Sint32:
3452 return this->emitConstSint32(Value, E);
3453 case PT_Uint32:
3454 return this->emitConstUint32(Value, E);
3455 case PT_Sint64:
3456 return this->emitConstSint64(Value, E);
3457 case PT_Uint64:
3458 return this->emitConstUint64(Value, E);
3459 case PT_Bool:
3460 return this->emitConstBool(Value, E);
3461 case PT_Ptr:
3462 case PT_FnPtr:
3463 case PT_MemberPtr:
3464 case PT_Float:
3465 case PT_IntAP:
3466 case PT_IntAPS:
3467 llvm_unreachable("Invalid integral type");
3468 break;
3469 }
3470 llvm_unreachable("unknown primitive type");
3471}
3472
3473template <class Emitter>
3474template <typename T>
3476 return this->emitConst(Value, classifyPrim(E->getType()), E);
3477}
3478
3479template <class Emitter>
3481 const Expr *E) {
3482 if (Ty == PT_IntAPS)
3483 return this->emitConstIntAPS(Value, E);
3484 if (Ty == PT_IntAP)
3485 return this->emitConstIntAP(Value, E);
3486
3487 if (Value.isSigned())
3488 return this->emitConst(Value.getSExtValue(), Ty, E);
3489 return this->emitConst(Value.getZExtValue(), Ty, E);
3490}
3491
3492template <class Emitter>
3493bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
3494 return this->emitConst(Value, classifyPrim(E->getType()), E);
3495}
3496
3497template <class Emitter>
3499 bool IsConst,
3500 bool IsExtended) {
3501 // Make sure we don't accidentally register the same decl twice.
3502 if (const auto *VD =
3503 dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
3504 assert(!P.getGlobal(VD));
3505 assert(!Locals.contains(VD));
3506 (void)VD;
3507 }
3508
3509 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
3510 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
3511 // or isa<MaterializeTemporaryExpr>().
3512 Descriptor *D = P.createDescriptor(Src, Ty, Descriptor::InlineDescMD, IsConst,
3513 Src.is<const Expr *>());
3514 Scope::Local Local = this->createLocal(D);
3515 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
3516 Locals.insert({VD, Local});
3517 VarScope->add(Local, IsExtended);
3518 return Local.Offset;
3519}
3520
3521template <class Emitter>
3522std::optional<unsigned>
3524 // Make sure we don't accidentally register the same decl twice.
3525 if ([[maybe_unused]] const auto *VD =
3526 dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
3527 assert(!P.getGlobal(VD));
3528 assert(!Locals.contains(VD));
3529 }
3530
3531 QualType Ty;
3532 const ValueDecl *Key = nullptr;
3533 const Expr *Init = nullptr;
3534 bool IsTemporary = false;
3535 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
3536 Key = VD;
3537 Ty = VD->getType();
3538
3539 if (const auto *VarD = dyn_cast<VarDecl>(VD))
3540 Init = VarD->getInit();
3541 }
3542 if (auto *E = Src.dyn_cast<const Expr *>()) {
3543 IsTemporary = true;
3544 Ty = E->getType();
3545 }
3546
3547 Descriptor *D = P.createDescriptor(
3549 IsTemporary, /*IsMutable=*/false, Init);
3550 if (!D)
3551 return std::nullopt;
3552
3553 Scope::Local Local = this->createLocal(D);
3554 if (Key)
3555 Locals.insert({Key, Local});
3556 if (ExtendingDecl)
3557 VarScope->addExtended(Local, ExtendingDecl);
3558 else
3559 VarScope->add(Local, false);
3560 return Local.Offset;
3561}
3562
3563template <class Emitter>
3565 QualType Ty = E->getType();
3566 assert(!Ty->isRecordType());
3567
3568 Descriptor *D = P.createDescriptor(
3570 /*IsTemporary=*/true, /*IsMutable=*/false, /*Init=*/nullptr);
3571 assert(D);
3572
3573 Scope::Local Local = this->createLocal(D);
3574 VariableScope<Emitter> *S = VarScope;
3575 assert(S);
3576 // Attach to topmost scope.
3577 while (S->getParent())
3578 S = S->getParent();
3579 assert(S && !S->getParent());
3580 S->addLocal(Local);
3581 return Local.Offset;
3582}
3583
3584template <class Emitter>
3586 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
3587 return PT->getPointeeType()->getAs<RecordType>();
3588 return Ty->getAs<RecordType>();
3589}
3590
3591template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
3592 if (const auto *RecordTy = getRecordTy(Ty))
3593 return getRecord(RecordTy->getDecl());
3594 return nullptr;
3595}
3596
3597template <class Emitter>
3599 return P.getOrCreateRecord(RD);
3600}
3601
3602template <class Emitter>
3604 return Ctx.getOrCreateFunction(FD);
3605}
3606
3607template <class Emitter> bool Compiler<Emitter>::visitExpr(const Expr *E) {
3608 LocalScope<Emitter> RootScope(this);
3609 // Void expressions.
3610 if (E->getType()->isVoidType()) {
3611 if (!visit(E))
3612 return false;
3613 return this->emitRetVoid(E) && RootScope.destroyLocals();
3614 }
3615
3616 // Expressions with a primitive return type.
3617 if (std::optional<PrimType> T = classify(E)) {
3618 if (!visit(E))
3619 return false;
3620 return this->emitRet(*T, E) && RootScope.destroyLocals();
3621 }
3622
3623 // Expressions with a composite return type.
3624 // For us, that means everything we don't
3625 // have a PrimType for.
3626 if (std::optional<unsigned> LocalOffset = this->allocateLocal(E)) {
3627 if (!this->emitGetPtrLocal(*LocalOffset, E))
3628 return false;
3629
3630 if (!visitInitializer(E))
3631 return false;
3632
3633 if (!this->emitFinishInit(E))
3634 return false;
3635 // We are destroying the locals AFTER the Ret op.
3636 // The Ret op needs to copy the (alive) values, but the
3637 // destructors may still turn the entire expression invalid.
3638 return this->emitRetValue(E) && RootScope.destroyLocals();
3639 }
3640
3641 RootScope.destroyLocals();
3642 return false;
3643}
3644
3645template <class Emitter>
3647
3648 auto R = this->visitVarDecl(VD, /*Toplevel=*/true);
3649
3650 if (R.notCreated())
3651 return R;
3652
3653 if (R)
3654 return true;
3655
3656 if (!R && Context::shouldBeGloballyIndexed(VD)) {
3657 if (auto GlobalIndex = P.getGlobal(VD)) {
3658 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
3660 *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
3661
3662 GD.InitState = GlobalInitState::InitializerFailed;
3663 GlobalBlock->invokeDtor();
3664 }
3665 }
3666
3667 return R;
3668}
3669
3670/// Toplevel visitDeclAndReturn().
3671/// We get here from evaluateAsInitializer().
3672/// We need to evaluate the initializer and return its value.
3673template <class Emitter>
3675 bool ConstantContext) {
3676 std::optional<PrimType> VarT = classify(VD->getType());
3677
3678 // We only create variables if we're evaluating in a constant context.
3679 // Otherwise, just evaluate the initializer and return it.
3680 if (!ConstantContext) {
3681 DeclScope<Emitter> LS(this, VD);
3682 if (!this->visit(VD->getAnyInitializer()))
3683 return false;
3684 return this->emitRet(VarT.value_or(PT_Ptr), VD) && LS.destroyLocals();
3685 }
3686
3687 LocalScope<Emitter> VDScope(this, VD);
3688 if (!this->visitVarDecl(VD, /*Toplevel=*/true))
3689 return false;
3690
3692 auto GlobalIndex = P.getGlobal(VD);
3693 assert(GlobalIndex); // visitVarDecl() didn't return false.
3694 if (VarT) {
3695 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
3696 return false;
3697 } else {
3698 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
3699 return false;
3700 }
3701 } else {
3702 auto Local = Locals.find(VD);
3703 assert(Local != Locals.end()); // Same here.
3704 if (VarT) {
3705 if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
3706 return false;
3707 } else {
3708 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
3709 return false;
3710 }
3711 }
3712
3713 // Return the value.
3714 if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
3715 // If the Ret above failed and this is a global variable, mark it as
3716 // uninitialized, even everything else succeeded.
3718 auto GlobalIndex = P.getGlobal(VD);
3719 assert(GlobalIndex);
3720 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
3722 *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
3723
3724 GD.InitState = GlobalInitState::InitializerFailed;
3725 GlobalBlock->invokeDtor();
3726 }
3727 return false;
3728 }
3729
3730 return VDScope.destroyLocals();
3731}
3732
3733template <class Emitter>
3735 bool Toplevel) {
3736 // We don't know what to do with these, so just return false.
3737 if (VD->getType().isNull())
3738 return false;
3739
3740 // This case is EvalEmitter-only. If we won't create any instructions for the
3741 // initializer anyway, don't bother creating the variable in the first place.
3742 if (!this->isActive())
3744
3745 const Expr *Init = VD->getInit();
3746 std::optional<PrimType> VarT = classify(VD->getType());
3747
3748 if (Init && Init->isValueDependent())
3749 return false;
3750
3752 auto checkDecl = [&]() -> bool {
3753 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
3754 return !NeedsOp || this->emitCheckDecl(VD, VD);
3755 };
3756
3757 auto initGlobal = [&](unsigned GlobalIndex) -> bool {
3758 assert(Init);
3760
3761 if (VarT) {
3762 if (!this->visit(Init))
3763 return checkDecl() && false;
3764
3765 return checkDecl() && this->emitInitGlobal(*VarT, GlobalIndex, VD);
3766 }
3767
3768 if (!checkDecl())
3769 return false;
3770
3771 if (!this->emitGetPtrGlobal(GlobalIndex, Init))
3772 return false;
3773
3774 if (!visitInitializer(Init))
3775 return false;
3776
3777 if (!this->emitFinishInit(Init))
3778 return false;
3779
3780 return this->emitPopPtr(Init);
3781 };
3782
3783 // We've already seen and initialized this global.
3784 if (std::optional<unsigned> GlobalIndex = P.getGlobal(VD)) {
3785 if (P.getPtrGlobal(*GlobalIndex).isInitialized())
3786 return checkDecl();
3787
3788 // The previous attempt at initialization might've been unsuccessful,
3789 // so let's try this one.
3790 return Init && checkDecl() && initGlobal(*GlobalIndex);
3791 }
3792
3793 std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
3794
3795 if (!GlobalIndex)
3796 return false;
3797
3798 return !Init || (checkDecl() && initGlobal(*GlobalIndex));
3799 } else {
3801
3802 if (VarT) {
3803 unsigned Offset = this->allocateLocalPrimitive(
3804 VD, *VarT, VD->getType().isConstQualified());
3805 if (Init) {
3806 // If this is a toplevel declaration, create a scope for the
3807 // initializer.
3808 if (Toplevel) {
3810 if (!this->visit(Init))
3811 return false;
3812 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
3813 } else {
3814 if (!this->visit(Init))
3815 return false;
3816 return this->emitSetLocal(*VarT, Offset, VD);
3817 }
3818 }
3819 } else {
3820 if (std::optional<unsigned> Offset = this->allocateLocal(VD)) {
3821 if (!Init)
3822 return true;
3823
3824 if (!this->emitGetPtrLocal(*Offset, Init))
3825 return false;
3826
3827 if (!visitInitializer(Init))
3828 return false;
3829
3830 if (!this->emitFinishInit(Init))
3831 return false;
3832
3833 return this->emitPopPtr(Init);
3834 }
3835 return false;
3836 }
3837 return true;
3838 }
3839
3840 return false;
3841}
3842
3843template <class Emitter>
3845 const Expr *E) {
3846 assert(!DiscardResult);
3847 if (Val.isInt())
3848 return this->emitConst(Val.getInt(), ValType, E);
3849 else if (Val.isFloat())
3850 return this->emitConstFloat(Val.getFloat(), E);
3851
3852 if (Val.isLValue()) {
3853 if (Val.isNullPointer())
3854 return this->emitNull(ValType, nullptr, E);
3856 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
3857 return this->visit(BaseExpr);
3858 else if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) {
3859 return this->visitDeclRef(VD, E);
3860 }
3861 } else if (Val.isMemberPointer()) {
3862 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl())
3863 return this->emitGetMemberPtr(MemberDecl, E);
3864 return this->emitNullMemberPtr(nullptr, E);
3865 }
3866
3867 return false;
3868}
3869
3870template <class Emitter>
3872 const Expr *E) {
3873
3874 if (Val.isStruct()) {
3875 const Record *R = this->getRecord(E->getType());
3876 assert(R);
3877 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
3878 const APValue &F = Val.getStructField(I);
3879 const Record::Field *RF = R->getField(I);
3880
3881 if (F.isInt() || F.isFloat() || F.isLValue() || F.isMemberPointer()) {
3882 PrimType T = classifyPrim(RF->Decl->getType());
3883 if (!this->visitAPValue(F, T, E))
3884 return false;
3885 if (!this->emitInitField(T, RF->Offset, E))
3886 return false;
3887 } else if (F.isArray()) {
3888 assert(RF->Desc->isPrimitiveArray());
3889 const auto *ArrType = RF->Decl->getType()->getAsArrayTypeUnsafe();
3890 PrimType ElemT = classifyPrim(ArrType->getElementType());
3891 assert(ArrType);
3892
3893 if (!this->emitGetPtrField(RF->Offset, E))
3894 return false;
3895
3896 for (unsigned A = 0, AN = F.getArraySize(); A != AN; ++A) {
3897 if (!this->visitAPValue(F.getArrayInitializedElt(A), ElemT, E))
3898 return false;
3899 if (!this->emitInitElem(ElemT, A, E))
3900 return false;
3901 }
3902
3903 if (!this->emitPopPtr(E))
3904 return false;
3905 } else if (F.isStruct() || F.isUnion()) {
3906 if (!this->emitGetPtrField(RF->Offset, E))
3907 return false;
3908 if (!this->visitAPValueInitializer(F, E))
3909 return false;
3910 if (!this->emitPopPtr(E))
3911 return false;
3912 } else {
3913 assert(false && "I don't think this should be possible");
3914 }
3915 }
3916 return true;
3917 } else if (Val.isUnion()) {
3918 const FieldDecl *UnionField = Val.getUnionField();
3919 const Record *R = this->getRecord(UnionField->getParent());
3920 assert(R);
3921 const APValue &F = Val.getUnionValue();
3922 const Record::Field *RF = R->getField(UnionField);
3923 PrimType T = classifyPrim(RF->Decl->getType());
3924 if (!this->visitAPValue(F, T, E))
3925 return false;
3926 return this->emitInitField(T, RF->Offset, E);
3927 }
3928 // TODO: Other types.
3929
3930 return false;
3931}
3932
3933template <class Emitter>
3935 const Function *Func = getFunction(E->getDirectCallee());
3936 if (!Func)
3937 return false;
3938
3939 // For these, we're expected to ultimately return an APValue pointing
3940 // to the CallExpr. This is needed to get the correct codegen.
3941 unsigned Builtin = E->getBuiltinCallee();
3942 if (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
3943 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
3944 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
3945 Builtin == Builtin::BI__builtin_function_start) {
3946 if (std::optional<unsigned> GlobalOffset = P.createGlobal(E)) {
3947 if (!this->emitGetPtrGlobal(*GlobalOffset, E))
3948 return false;
3949
3950 if (PrimType PT = classifyPrim(E); PT != PT_Ptr && isPtrType(PT))
3951 return this->emitDecayPtr(PT_Ptr, PT, E);
3952 return true;
3953 }
3954 return false;
3955 }
3956
3957 QualType ReturnType = E->getType();
3958 std::optional<PrimType> ReturnT = classify(E);
3959
3960 // Non-primitive return type. Prepare storage.
3961 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
3962 std::optional<unsigned> LocalIndex = allocateLocal(E);
3963 if (!LocalIndex)
3964 return false;
3965 if (!this->emitGetPtrLocal(*LocalIndex, E))
3966 return false;
3967 }
3968
3969 if (!Func->isUnevaluatedBuiltin()) {
3970 // Put arguments on the stack.
3971 for (const auto *Arg : E->arguments()) {
3972 if (!this->visit(Arg))
3973 return false;
3974 }
3975 }
3976
3977 if (!this->emitCallBI(Func, E, E))
3978 return false;
3979
3980 if (DiscardResult && !ReturnType->isVoidType()) {
3981 assert(ReturnT);
3982 return this->emitPop(*ReturnT, E);
3983 }
3984
3985 return true;
3986}
3987
3988template <class Emitter>
3990 if (E->getBuiltinCallee())
3991 return VisitBuiltinCallExpr(E);
3992
3993 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
3994 std::optional<PrimType> T = classify(ReturnType);
3995 bool HasRVO = !ReturnType->isVoidType() && !T;
3996 const FunctionDecl *FuncDecl = E->getDirectCallee();
3997
3998 if (HasRVO) {
3999 if (DiscardResult) {
4000 // If we need to discard the return value but the function returns its
4001 // value via an RVO pointer, we need to create one such pointer just
4002 // for this call.
4003 if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
4004 if (!this->emitGetPtrLocal(*LocalIndex, E))
4005 return false;
4006 }
4007 } else {
4008 // We need the result. Prepare a pointer to return or
4009 // dup the current one.
4010 if (!Initializing) {
4011 if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
4012 if (!this->emitGetPtrLocal(*LocalIndex, E))
4013 return false;
4014 }
4015 }
4016 if (!this->emitDupPtr(E))
4017 return false;
4018 }
4019 }
4020
4022 llvm::ArrayRef(E->getArgs(), E->getNumArgs()));
4023
4024 bool IsAssignmentOperatorCall = false;
4025 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
4026 OCE && OCE->isAssignmentOp()) {
4027 // Just like with regular assignments, we need to special-case assignment
4028 // operators here and evaluate the RHS (the second arg) before the LHS (the
4029 // first arg. We fix this by using a Flip op later.
4030 assert(Args.size() == 2);
4031 IsAssignmentOperatorCall = true;
4032 std::reverse(Args.begin(), Args.end());
4033 }
4034 // Calling a static operator will still
4035 // pass the instance, but we don't need it.
4036 // Discard it here.
4037 if (isa<CXXOperatorCallExpr>(E)) {
4038 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
4039 MD && MD->isStatic()) {
4040 if (!this->discard(E->getArg(0)))
4041 return false;
4042 // Drop first arg.
4043 Args.erase(Args.begin());
4044 }
4045 }
4046
4047 std::optional<unsigned> CalleeOffset;
4048 // Add the (optional, implicit) This pointer.
4049 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
4050 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
4051 // If we end up creating a CallPtr op for this, we need the base of the
4052 // member pointer as the instance pointer, and later extract the function
4053 // decl as the function pointer.
4054 const Expr *Callee = E->getCallee();
4055 CalleeOffset =
4056 this->allocateLocalPrimitive(Callee, PT_MemberPtr, true, false);
4057 if (!this->visit(Callee))
4058 return false;
4059 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
4060 return false;
4061 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
4062 return false;
4063 if (!this->emitGetMemberPtrBase(E))
4064 return false;
4065 } else if (!this->visit(MC->getImplicitObjectArgument())) {
4066 return false;
4067 }
4068 } else if (!FuncDecl) {
4069 const Expr *Callee = E->getCallee();
4070 CalleeOffset = this->allocateLocalPrimitive(Callee, PT_FnPtr, true, false);
4071 if (!this->visit(Callee))
4072 return false;
4073 if (!this->emitSetLocal(PT_FnPtr, *CalleeOffset, E))
4074 return false;
4075 }
4076
4077 llvm::BitVector NonNullArgs = collectNonNullArgs(FuncDecl, Args);
4078 // Put arguments on the stack.
4079 unsigned ArgIndex = 0;
4080 for (const auto *Arg : Args) {
4081 if (!this->visit(Arg))
4082 return false;
4083
4084 // If we know the callee already, check the known parametrs for nullability.
4085 if (FuncDecl && NonNullArgs[ArgIndex]) {
4086 PrimType ArgT = classify(Arg).value_or(PT_Ptr);
4087 if (ArgT == PT_Ptr || ArgT == PT_FnPtr) {
4088 if (!this->emitCheckNonNullArg(ArgT, Arg))
4089 return false;
4090 }
4091 }
4092 ++ArgIndex;
4093 }
4094
4095 // Undo the argument reversal we did earlier.
4096 if (IsAssignmentOperatorCall) {
4097 assert(Args.size() == 2);
4098 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
4099 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
4100 if (!this->emitFlip(Arg2T, Arg1T, E))
4101 return false;
4102 }
4103
4104 if (FuncDecl) {
4105 const Function *Func = getFunction(FuncDecl);
4106 if (!Func)
4107 return false;
4108 assert(HasRVO == Func->hasRVO());
4109
4110 bool HasQualifier = false;
4111 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
4112 HasQualifier = ME->hasQualifier();
4113
4114 bool IsVirtual = false;
4115 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
4116 IsVirtual = MD->isVirtual();
4117
4118 // In any case call the function. The return value will end up on the stack
4119 // and if the function has RVO, we already have the pointer on the stack to
4120 // write the result into.
4121 if (IsVirtual && !HasQualifier) {
4122 uint32_t VarArgSize = 0;
4123 unsigned NumParams =
4124 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
4125 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
4126 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
4127
4128 if (!this->emitCallVirt(Func, VarArgSize, E))
4129 return false;
4130 } else if (Func->isVariadic()) {
4131 uint32_t VarArgSize = 0;
4132 unsigned NumParams =
4133 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
4134 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
4135 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
4136 if (!this->emitCallVar(Func, VarArgSize, E))
4137 return false;
4138 } else {
4139 if (!this->emitCall(Func, 0, E))
4140 return false;
4141 }
4142 } else {
4143 // Indirect call. Visit the callee, which will leave a FunctionPointer on
4144 // the stack. Cleanup of the returned value if necessary will be done after
4145 // the function call completed.
4146
4147 // Sum the size of all args from the call expr.
4148 uint32_t ArgSize = 0;
4149 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
4150 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
4151
4152 // Get the callee, either from a member pointer or function pointer saved in
4153 // CalleeOffset.
4154 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
4155 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
4156 return false;
4157 if (!this->emitGetMemberPtrDecl(E))
4158 return false;
4159 } else {
4160 if (!this->emitGetLocal(PT_FnPtr, *CalleeOffset, E))
4161 return false;
4162 }
4163 if (!this->emitCallPtr(ArgSize, E, E))
4164 return false;
4165 }
4166
4167 // Cleanup for discarded return values.
4168 if (DiscardResult && !ReturnType->isVoidType() && T)
4169 return this->emitPop(*T, E);
4170
4171 return true;
4172}
4173
4174template <class Emitter>
4176 SourceLocScope<Emitter> SLS(this, E);
4177
4178 return this->delegate(E->getExpr());
4179}
4180
4181template <class Emitter>
4183 SourceLocScope<Emitter> SLS(this, E);
4184
4185 const Expr *SubExpr = E->getExpr();
4186 if (std::optional<PrimType> T = classify(E->getExpr()))
4187 return this->visit(SubExpr);
4188
4189 assert(Initializing);
4190 return this->visitInitializer(SubExpr);
4191}
4192
4193template <class Emitter>
4195 if (DiscardResult)
4196 return true;
4197
4198 return this->emitConstBool(E->getValue(), E);
4199}
4200
4201template <class Emitter>
4203 const CXXNullPtrLiteralExpr *E) {
4204 if (DiscardResult)
4205 return true;
4206
4207 return this->emitNullPtr(nullptr, E);
4208}
4209
4210template <class Emitter>
4212 if (DiscardResult)
4213 return true;
4214
4215 assert(E->getType()->isIntegerType());
4216
4217 PrimType T = classifyPrim(E->getType());
4218 return this->emitZero(T, E);
4219}
4220
4221template <class Emitter>
4223 if (DiscardResult)
4224 return true;
4225
4226 if (this->LambdaThisCapture.Offset > 0) {
4227 if (this->LambdaThisCapture.IsPtr)
4228 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
4229 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
4230 }
4231
4232 // In some circumstances, the 'this' pointer does not actually refer to the
4233 // instance pointer of the current function frame, but e.g. to the declaration
4234 // currently being initialized. Here we emit the necessary instruction(s) for
4235 // this scenario.
4236 if (!InitStackActive || !E->isImplicit())
4237 return this->emitThis(E);
4238
4239 if (InitStackActive && !InitStack.empty()) {
4240 unsigned StartIndex = 0;
4241 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
4242 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
4243 InitStack[StartIndex].Kind != InitLink::K_Elem)
4244 break;
4245 }
4246
4247 for (unsigned I = StartIndex, N = InitStack.size(); I != N; ++I) {
4248 if (!InitStack[I].template emit<Emitter>(this, E))
4249 return false;
4250 }
4251 return true;
4252 }
4253 return this->emitThis(E);
4254}
4255
4256template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
4257 switch (S->getStmtClass()) {
4258 case Stmt::CompoundStmtClass:
4259 return visitCompoundStmt(cast<CompoundStmt>(S));
4260 case Stmt::DeclStmtClass:
4261 return visitDeclStmt(cast<DeclStmt>(S));
4262 case Stmt::ReturnStmtClass:
4263 return visitReturnStmt(cast<ReturnStmt>(S));
4264 case Stmt::IfStmtClass:
4265 return visitIfStmt(cast<IfStmt>(S));
4266 case Stmt::WhileStmtClass:
4267 return visitWhileStmt(cast<WhileStmt>(S));
4268 case Stmt::DoStmtClass:
4269 return visitDoStmt(cast<DoStmt>(S));
4270 case Stmt::ForStmtClass:
4271 return visitForStmt(cast<ForStmt>(S));
4272 case Stmt::CXXForRangeStmtClass:
4273 return visitCXXForRangeStmt(cast<CXXForRangeStmt>(S));
4274 case Stmt::BreakStmtClass:
4275 return visitBreakStmt(cast<BreakStmt>(S));
4276 case Stmt::ContinueStmtClass:
4277 return visitContinueStmt(cast<ContinueStmt>(S));
4278 case Stmt::SwitchStmtClass:
4279 return visitSwitchStmt(cast<SwitchStmt>(S));
4280 case Stmt::CaseStmtClass:
4281 return visitCaseStmt(cast<CaseStmt>(S));
4282 case Stmt::DefaultStmtClass:
4283 return visitDefaultStmt(cast<DefaultStmt>(S));
4284 case Stmt::AttributedStmtClass:
4285 return visitAttributedStmt(cast<AttributedStmt>(S));
4286 case Stmt::CXXTryStmtClass:
4287 return visitCXXTryStmt(cast<CXXTryStmt>(S));
4288 case Stmt::NullStmtClass:
4289 return true;
4290 // Always invalid statements.
4291 case Stmt::GCCAsmStmtClass:
4292 case Stmt::MSAsmStmtClass:
4293 case Stmt::GotoStmtClass:
4294 return this->emitInvalid(S);
4295 case Stmt::LabelStmtClass:
4296 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
4297 default: {
4298 if (const auto *E = dyn_cast<Expr>(S))
4299 return this->discard(E);
4300 return false;
4301 }
4302 }
4303}
4304
4305template <class Emitter>
4308 for (const auto *InnerStmt : S->body())
4309 if (!visitStmt(InnerStmt))
4310 return false;
4311 return Scope.destroyLocals();
4312}
4313
4314template <class Emitter>
4316 for (const auto *D : DS->decls()) {
4318 FunctionDecl>(D))
4319 continue;
4320
4321 const auto *VD = dyn_cast<VarDecl>(D);
4322 if (!VD)
4323 return false;
4324 if (!this->visitVarDecl(VD))
4325 return false;
4326 }
4327
4328 return true;
4329}
4330
4331template <class Emitter>
4333 if (this->InStmtExpr)
4334 return this->emitUnsupported(RS);
4335
4336 if (const Expr *RE = RS->getRetValue()) {
4337 LocalScope<Emitter> RetScope(this);
4338 if (ReturnType) {
4339 // Primitive types are simply returned.
4340 if (!this->visit(RE))
4341 return false;
4342 this->emitCleanup();
4343 return this->emitRet(*ReturnType, RS);
4344 } else if (RE->getType()->isVoidType()) {
4345 if (!this->visit(RE))
4346 return false;
4347 } else {
4348 // RVO - construct the value in the return location.
4349 if (!this->emitRVOPtr(RE))
4350 return false;
4351 if (!this->visitInitializer(RE))
4352 return false;
4353 if (!this->emitPopPtr(RE))
4354 return false;
4355
4356 this->emitCleanup();
4357 return this->emitRetVoid(RS);
4358 }
4359 }
4360
4361 // Void return.
4362 this->emitCleanup();
4363 return this->emitRetVoid(RS);
4364}
4365
4366template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
4367 if (IS->isNonNegatedConsteval())
4368 return visitStmt(IS->getThen());
4369 if (IS->isNegatedConsteval())
4370 return IS->getElse() ? visitStmt(IS->getElse()) : true;
4371
4372 if (auto *CondInit = IS->getInit())
4373 if (!visitStmt(CondInit))
4374 return false;
4375
4376 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt())
4377 if (!visitDeclStmt(CondDecl))
4378 return false;
4379
4380 if (!this->visitBool(IS->getCond()))
4381 return false;
4382
4383 if (const Stmt *Else = IS->getElse()) {
4384 LabelTy LabelElse = this->getLabel();
4385 LabelTy LabelEnd = this->getLabel();
4386 if (!this->jumpFalse(LabelElse))
4387 return false;
4388 if (!visitStmt(IS->getThen()))
4389 return false;
4390 if (!this->jump(LabelEnd))
4391 return false;
4392 this->emitLabel(LabelElse);
4393 if (!visitStmt(Else))
4394 return false;
4395 this->emitLabel(LabelEnd);
4396 } else {
4397 LabelTy LabelEnd = this->getLabel();
4398 if (!this->jumpFalse(LabelEnd))
4399 return false;
4400 if (!visitStmt(IS->getThen()))
4401 return false;
4402 this->emitLabel(LabelEnd);
4403 }
4404
4405 return true;
4406}
4407
4408template <class Emitter>
4410 const Expr *Cond = S->getCond();
4411 const Stmt *Body = S->getBody();
4412
4413 LabelTy CondLabel = this->getLabel(); // Label before the condition.
4414 LabelTy EndLabel = this->getLabel(); // Label after the loop.
4415 LoopScope<Emitter> LS(this, EndLabel, CondLabel);
4416
4417 this->fallthrough(CondLabel);
4418 this->emitLabel(CondLabel);
4419
4420 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
4421 if (!visitDeclStmt(CondDecl))
4422 return false;
4423
4424 if (!this->visitBool(Cond))
4425 return false;
4426 if (!this->jumpFalse(EndLabel))
4427 return false;
4428
4429 if (!this->visitStmt(Body))
4430 return false;
4431
4432 if (!this->jump(CondLabel))
4433 return false;
4434 this->fallthrough(EndLabel);
4435 this->emitLabel(EndLabel);
4436
4437 return true;
4438}
4439
4440template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
4441 const Expr *Cond = S->getCond();
4442 const Stmt *Body = S->getBody();
4443
4444 LabelTy StartLabel = this->getLabel();
4445 LabelTy EndLabel = this->getLabel();
4446 LabelTy CondLabel = this->getLabel();
4447 LoopScope<Emitter> LS(this, EndLabel, CondLabel);
4448
4449 this->fallthrough(StartLabel);
4450 this->emitLabel(StartLabel);
4451 {
4452 if (!this->visitStmt(Body))
4453 return false;
4454 this->fallthrough(CondLabel);
4455 this->emitLabel(CondLabel);
4456 if (!this->visitBool(Cond))
4457 return false;
4458 }
4459 if (!this->jumpTrue(StartLabel))
4460 return false;
4461
4462 this->fallthrough(EndLabel);
4463 this->emitLabel(EndLabel);
4464 return true;
4465}
4466
4467template <class Emitter>
4469 // for (Init; Cond; Inc) { Body }
4470 const Stmt *Init = S->getInit();
4471 const Expr *Cond = S->getCond();
4472 const Expr *Inc = S->getInc();
4473 const Stmt *Body = S->getBody();
4474
4475 LabelTy EndLabel = this->getLabel();
4476 LabelTy CondLabel = this->getLabel();
4477 LabelTy IncLabel = this->getLabel();
4478 LoopScope<Emitter> LS(this, EndLabel, IncLabel);
4479
4480 if (Init && !this->visitStmt(Init))
4481 return false;
4482
4483 this->fallthrough(CondLabel);
4484 this->emitLabel(CondLabel);
4485
4486 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
4487 if (!visitDeclStmt(CondDecl))
4488 return false;
4489
4490 if (Cond) {
4491 if (!this->visitBool(Cond))
4492 return false;
4493 if (!this->jumpFalse(EndLabel))
4494 return false;
4495 }
4496
4497 {
4498 if (Body && !this->visitStmt(Body))
4499 return false;
4500
4501 this->fallthrough(IncLabel);
4502 this->emitLabel(IncLabel);
4503 if (Inc && !this->discard(Inc))
4504 return false;
4505 }
4506
4507 if (!this->jump(CondLabel))
4508 return false;
4509 this->fallthrough(EndLabel);
4510 this->emitLabel(EndLabel);
4511 return true;
4512}
4513
4514template <class Emitter>
4516 const Stmt *Init = S->getInit();
4517 const Expr *Cond = S->getCond();
4518 const Expr *Inc = S->getInc();
4519 const Stmt *Body = S->getBody();
4520 const Stmt *BeginStmt = S->getBeginStmt();
4521 const Stmt *RangeStmt = S->getRangeStmt();
4522 const Stmt *EndStmt = S->getEndStmt();
4523 const VarDecl *LoopVar = S->getLoopVariable();
4524
4525 LabelTy EndLabel = this->getLabel();
4526 LabelTy CondLabel = this->getLabel();
4527 LabelTy IncLabel = this->getLabel();
4528 LoopScope<Emitter> LS(this, EndLabel, IncLabel);
4529
4530 // Emit declarations needed in the loop.
4531 if (Init && !this->visitStmt(Init))
4532 return false;
4533 if (!this->visitStmt(RangeStmt))
4534 return false;
4535 if (!this->visitStmt(BeginStmt))
4536 return false;
4537 if (!this->visitStmt(EndStmt))
4538 return false;
4539
4540 // Now the condition as well as the loop variable assignment.
4541 this->fallthrough(CondLabel);
4542 this->emitLabel(CondLabel);
4543 if (!this->visitBool(Cond))
4544 return false;
4545 if (!this->jumpFalse(EndLabel))
4546 return false;
4547
4548 if (!this->visitVarDecl(LoopVar))
4549 return false;
4550
4551 // Body.
4552 {
4553 if (!this->visitStmt(Body))
4554 return false;
4555
4556 this->fallthrough(IncLabel);
4557 this->emitLabel(IncLabel);
4558 if (!this->discard(Inc))
4559 return false;
4560 }
4561
4562 if (!this->jump(CondLabel))
4563 return false;
4564
4565 this->fallthrough(EndLabel);
4566 this->emitLabel(EndLabel);
4567 return true;
4568}
4569
4570template <class Emitter>
4572 if (!BreakLabel)
4573 return false;
4574
4575 this->emitCleanup();
4576 return this->jump(*BreakLabel);
4577}
4578
4579template <class Emitter>
4581 if (!ContinueLabel)
4582 return false;
4583
4584 this->emitCleanup();
4585 return this->jump(*ContinueLabel);
4586}
4587
4588template <class Emitter>
4590 const Expr *Cond = S->getCond();
4591 PrimType CondT = this->classifyPrim(Cond->getType());
4592
4593 LabelTy EndLabel = this->getLabel();
4594 OptLabelTy DefaultLabel = std::nullopt;
4595 unsigned CondVar = this->allocateLocalPrimitive(Cond, CondT, true, false);
4596
4597 if (const auto *CondInit = S->getInit())
4598 if (!visitStmt(CondInit))
4599 return false;
4600
4601 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
4602 if (!visitDeclStmt(CondDecl))
4603 return false;
4604
4605 // Initialize condition variable.
4606 if (!this->visit(Cond))
4607 return false;
4608 if (!this->emitSetLocal(CondT, CondVar, S))
4609 return false;
4610
4611 CaseMap CaseLabels;
4612 // Create labels and comparison ops for all case statements.
4613 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
4614 SC = SC->getNextSwitchCase()) {
4615 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
4616 // FIXME: Implement ranges.
4617 if (CS->caseStmtIsGNURange())
4618 return false;
4619 CaseLabels[SC] = this->getLabel();
4620
4621 const Expr *Value = CS->getLHS();
4622 PrimType ValueT = this->classifyPrim(Value->getType());
4623
4624 // Compare the case statement's value to the switch condition.
4625 if (!this->emitGetLocal(CondT, CondVar, CS))
4626 return false;
4627 if (!this->visit(Value))
4628 return false;
4629
4630 // Compare and jump to the case label.
4631 if (!this->emitEQ(ValueT, S))
4632 return false;
4633 if (!this->jumpTrue(CaseLabels[CS]))
4634 return false;
4635 } else {
4636 assert(!DefaultLabel);
4637 DefaultLabel = this->getLabel();
4638 }
4639 }
4640
4641 // If none of the conditions above were true, fall through to the default
4642 // statement or jump after the switch statement.
4643 if (DefaultLabel) {
4644 if (!this->jump(*DefaultLabel))
4645 return false;
4646 } else {
4647 if (!this->jump(EndLabel))
4648 return false;
4649 }
4650
4651 SwitchScope<Emitter> SS(this, std::move(CaseLabels), EndLabel, DefaultLabel);
4652 if (!this->visitStmt(S->getBody()))
4653 return false;
4654 this->emitLabel(EndLabel);
4655 return true;
4656}
4657
4658template <class Emitter>
4660 this->emitLabel(CaseLabels[S]);
4661 return this->visitStmt(S->getSubStmt());
4662}
4663
4664template <class Emitter>
4666 this->emitLabel(*DefaultLabel);
4667 return this->visitStmt(S->getSubStmt());
4668}
4669
4670template <class Emitter>
4672 if (this->Ctx.getLangOpts().CXXAssumptions &&
4673 !this->Ctx.getLangOpts().MSVCCompat) {
4674 for (const Attr *A : S->getAttrs()) {
4675 auto *AA = dyn_cast<CXXAssumeAttr>(A);
4676 if (!AA)
4677 continue;
4678
4679 assert(isa<NullStmt>(S->getSubStmt()));
4680
4681 const Expr *Assumption = AA->getAssumption();
4682 if (Assumption->isValueDependent())
4683 return false;
4684
4685 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
4686 continue;
4687
4688 // Evaluate assumption.
4689 if (!this->visitBool(Assumption))
4690 return false;
4691
4692 if (!this->emitAssume(Assumption))
4693 return false;
4694 }
4695 }
4696
4697 // Ignore other attributes.
4698 return this->visitStmt(S->getSubStmt());
4699}
4700
4701template <class Emitter>
4703 // Ignore all handlers.
4704 return this->visitStmt(S->getTryBlock());
4705}
4706
4707template <class Emitter>
4709 assert(MD->isLambdaStaticInvoker());
4710 assert(MD->hasBody());
4711 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
4712
4713 const CXXRecordDecl *ClosureClass = MD->getParent();
4714 const CXXMethodDecl *LambdaCallOp = ClosureClass->getLambdaCallOperator();
4715 assert(ClosureClass->captures_begin() == ClosureClass->captures_end());
4716 const Function *Func = this->getFunction(LambdaCallOp);
4717 if (!Func)
4718 return false;
4719 assert(Func->hasThisPointer());
4720 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
4721
4722 if (Func->hasRVO()) {
4723 if (!this->emitRVOPtr(MD))
4724 return false;
4725 }
4726
4727 // The lambda call operator needs an instance pointer, but we don't have
4728 // one here, and we don't need one either because the lambda cannot have
4729 // any captures, as verified above. Emit a null pointer. This is then
4730 // special-cased when interpreting to not emit any misleading diagnostics.
4731 if (!this->emitNullPtr(nullptr, MD))
4732 return false;
4733
4734 // Forward all arguments from the static invoker to the lambda call operator.
4735 for (const ParmVarDecl *PVD : MD->parameters()) {
4736 auto It = this->Params.find(PVD);
4737 assert(It != this->Params.end());
4738
4739 // We do the lvalue-to-rvalue conversion manually here, so no need
4740 // to care about references.
4741 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
4742 if (!this->emitGetParam(ParamType, It->second.Offset, MD))
4743 return false;
4744 }
4745
4746 if (!this->emitCall(Func, 0, LambdaCallOp))
4747 return false;
4748
4749 this->emitCleanup();
4750 if (ReturnType)
4751 return this->emitRet(*ReturnType, MD);
4752
4753 // Nothing to do, since we emitted the RVO pointer above.
4754 return this->emitRetVoid(MD);
4755}
4756
4757template <class Emitter>
4759 if (Ctx.getLangOpts().CPlusPlus23)
4760 return true;
4761
4762 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
4763 return true;
4764
4765 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
4766}
4767
4768template <class Emitter>
4770 assert(!ReturnType);
4771
4772 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
4773 const Expr *InitExpr) -> bool {
4774 // We don't know what to do with these, so just return false.
4775 if (InitExpr->getType().isNull())
4776 return false;
4777
4778 if (std::optional<PrimType> T = this->classify(InitExpr)) {
4779 if (!this->visit(InitExpr))
4780 return false;
4781
4782 if (F->isBitField())
4783 return this->emitInitThisBitField(*T, F, FieldOffset, InitExpr);
4784 return this->emitInitThisField(*T, FieldOffset, InitExpr);
4785 }
4786 // Non-primitive case. Get a pointer to the field-to-initialize
4787 // on the stack and call visitInitialzer() for it.
4788 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
4789 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
4790 return false;
4791
4792 if (!this->visitInitializer(InitExpr))
4793 return false;
4794
4795 return this->emitFinishInitPop(InitExpr);
4796 };
4797
4798 const RecordDecl *RD = Ctor->getParent();
4799 const Record *R = this->getRecord(RD);
4800 if (!R)
4801 return false;
4802
4803 if (R->isUnion() && Ctor->isCopyOrMoveConstructor()) {
4804 // union copy and move ctors are special.
4805 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
4806 if (!this->emitThis(Ctor))
4807 return false;
4808
4809 auto PVD = Ctor->getParamDecl(0);
4810 ParamOffset PO = this->Params[PVD]; // Must exist.
4811
4812 if (!this->emitGetParam(PT_Ptr, PO.Offset, Ctor))
4813 return false;
4814
4815 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
4816 this->emitRetVoid(Ctor);
4817 }
4818
4820 for (const auto *Init : Ctor->inits()) {
4821 // Scope needed for the initializers.
4823
4824 const Expr *InitExpr = Init->getInit();
4825 if (const FieldDecl *Member = Init->getMember()) {
4826 const Record::Field *F = R->getField(Member);
4827
4828 if (!emitFieldInitializer(F, F->Offset, InitExpr))
4829 return false;
4830 } else if (const Type *Base = Init->getBaseClass()) {
4831 const auto *BaseDecl = Base->getAsCXXRecordDecl();
4832 assert(BaseDecl);
4833
4834 if (Init->isBaseVirtual()) {
4835 assert(R->getVirtualBase(BaseDecl));
4836 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
4837 return false;
4838
4839 } else {
4840 // Base class initializer.
4841 // Get This Base and call initializer on it.
4842 const Record::Base *B = R->getBase(BaseDecl);
4843 assert(B);
4844 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
4845 return false;
4846 }
4847
4848 if (!this->visitInitializer(InitExpr))
4849 return false;
4850 if (!this->emitFinishInitPop(InitExpr))
4851 return false;
4852 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
4853 assert(IFD->getChainingSize() >= 2);
4854
4855 unsigned NestedFieldOffset = 0;
4856 const Record::Field *NestedField = nullptr;
4857 for (const NamedDecl *ND : IFD->chain()) {
4858 const auto *FD = cast<FieldDecl>(ND);
4859 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
4860 assert(FieldRecord);
4861
4862 NestedField = FieldRecord->getField(FD);
4863 assert(NestedField);
4864
4865 NestedFieldOffset += NestedField->Offset;
4866 }
4867 assert(NestedField);
4868
4869 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr))
4870 return false;
4871 } else {
4872 assert(Init->isDelegatingInitializer());
4873 if (!this->emitThis(InitExpr))
4874 return false;
4875 if (!this->visitInitializer(Init->getInit()))
4876 return false;
4877 if (!this->emitPopPtr(InitExpr))
4878 return false;
4879 }
4880
4881 if (!Scope.destroyLocals())
4882 return false;
4883 }
4884
4885 if (const auto *Body = Ctor->getBody())
4886 if (!visitStmt(Body))
4887 return false;
4888
4889 return this->emitRetVoid(SourceInfo{});
4890}
4891
4892template <class Emitter>
4894 const RecordDecl *RD = Dtor->getParent();
4895 const Record *R = this->getRecord(RD);
4896 if (!R)
4897 return false;
4898
4899 if (!Dtor->isTrivial() && Dtor->getBody()) {
4900 if (!this->visitStmt(Dtor->getBody()))
4901 return false;
4902 }
4903
4904 if (!this->emitThis(Dtor))
4905 return false;
4906
4907 assert(R);
4908 if (!R->isUnion()) {
4909 // First, destroy all fields.
4910 for (const Record::Field &Field : llvm::reverse(R->fields())) {
4911 const Descriptor *D = Field.Desc;
4912 if (!D->isPrimitive() && !D->isPrimitiveArray()) {
4913 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
4914 return false;
4915 if (!this->emitDestruction(D))
4916 return false;
4917 if (!this->emitPopPtr(SourceInfo{}))
4918 return false;
4919 }
4920 }
4921 }
4922
4923 for (const Record::Base &Base : llvm::reverse(R->bases())) {
4924 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
4925 return false;
4926 if (!this->emitRecordDestruction(Base.R))
4927 return false;
4928 if (!this->emitPopPtr(SourceInfo{}))
4929 return false;
4930 }
4931
4932 // FIXME: Virtual bases.
4933 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
4934}
4935
4936template <class Emitter>
4938 // Classify the return type.
4939 ReturnType = this->classify(F->getReturnType());
4940
4941 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
4942 return this->compileConstructor(Ctor);
4943 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
4944 return this->compileDestructor(Dtor);
4945
4946 // Emit custom code if this is a lambda static invoker.
4947 if (const auto *MD = dyn_cast<CXXMethodDecl>(F);
4948 MD && MD->isLambdaStaticInvoker())
4949 return this->emitLambdaStaticInvokerBody(MD);
4950
4951 // Regular functions.
4952 if (const auto *Body = F->getBody())
4953 if (!visitStmt(Body))
4954 return false;
4955
4956 // Emit a guard return to protect against a code path missing one.
4957 if (F->getReturnType()->isVoidType())
4958 return this->emitRetVoid(SourceInfo{});
4959 return this->emitNoRet(SourceInfo{});
4960}
4961
4962template <class Emitter>
4964 const Expr *SubExpr = E->getSubExpr();
4965 if (SubExpr->getType()->isAnyComplexType())
4966 return this->VisitComplexUnaryOperator(E);
4967 std::optional<PrimType> T = classify(SubExpr->getType());
4968
4969 switch (E->getOpcode()) {
4970 case UO_PostInc: { // x++
4971 if (!Ctx.getLangOpts().CPlusPlus14)
4972 return this->emitInvalid(E);
4973 if (!T)
4974 return this->emitError(E);
4975
4976 if (!this->visit(SubExpr))
4977 return false;
4978
4979 if (T == PT_Ptr || T == PT_FnPtr) {
4980 if (!this->emitIncPtr(E))
4981 return false;
4982
4983 return DiscardResult ? this->emitPopPtr(E) : true;
4984 }
4985
4986 if (T == PT_Float) {
4987 return DiscardResult ? this->emitIncfPop(getRoundingMode(E), E)
4988 : this->emitIncf(getRoundingMode(E), E);
4989 }
4990
4991 return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E);
4992 }
4993 case UO_PostDec: { // x--
4994 if (!Ctx.getLangOpts().CPlusPlus14)
4995 return this->emitInvalid(E);
4996 if (!T)
4997 return this->emitError(E);
4998
4999 if (!this->visit(SubExpr))
5000 return false;
5001
5002 if (T == PT_Ptr || T == PT_FnPtr) {
5003 if (!this->emitDecPtr(E))
5004 return false;
5005
5006 return DiscardResult ? this->emitPopPtr(E) : true;
5007 }
5008
5009 if (T == PT_Float) {
5010 return DiscardResult ? this->emitDecfPop(getRoundingMode(E), E)
5011 : this->emitDecf(getRoundingMode(E), E);
5012 }
5013
5014 return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E);
5015 }
5016 case UO_PreInc: { // ++x
5017 if (!Ctx.getLangOpts().CPlusPlus14)
5018 return this->emitInvalid(E);
5019 if (!T)
5020 return this->emitError(E);
5021
5022 if (!this->visit(SubExpr))
5023 return false;
5024
5025 if (T == PT_Ptr || T == PT_FnPtr) {
5026 if (!this->emitLoadPtr(E))
5027 return false;
5028 if (!this->emitConstUint8(1, E))
5029 return false;
5030 if (!this->emitAddOffsetUint8(E))
5031 return false;
5032 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
5033 }
5034
5035 // Post-inc and pre-inc are the same if the value is to be discarded.
5036 if (DiscardResult) {
5037 if (T == PT_Float)
5038 return this->emitIncfPop(getRoundingMode(E), E);
5039 return this->emitIncPop(*T, E);
5040 }
5041
5042 if (T == PT_Float) {
5043 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
5044 if (!this->emitLoadFloat(E))
5045 return false;
5046 if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
5047 return false;
5048 if (!this->emitAddf(getRoundingMode(E), E))
5049 return false;
5050 if (!this->emitStoreFloat(E))
5051 return false;
5052 } else {
5053 assert(isIntegralType(*T));
5054 if (!this->emitLoad(*T, E))
5055 return false;
5056 if (!this->emitConst(1, E))
5057 return false;
5058 if (!this->emitAdd(*T, E))
5059 return false;
5060 if (!this->emitStore(*T, E))
5061 return false;
5062 }
5063 return E->isGLValue() || this->emitLoadPop(*T, E);
5064 }
5065 case UO_PreDec: { // --x
5066 if (!Ctx.getLangOpts().CPlusPlus14)
5067 return this->emitInvalid(E);
5068 if (!T)
5069 return this->emitError(E);
5070
5071 if (!this->visit(SubExpr))
5072 return false;
5073
5074 if (T == PT_Ptr || T == PT_FnPtr) {
5075 if (!this->emitLoadPtr(E))
5076 return false;
5077 if (!this->emitConstUint8(1, E))
5078 return false;
5079 if (!this->emitSubOffsetUint8(E))
5080 return false;
5081 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
5082 }
5083
5084 // Post-dec and pre-dec are the same if the value is to be discarded.
5085 if (DiscardResult) {
5086 if (T == PT_Float)
5087 return this->emitDecfPop(getRoundingMode(E), E);
5088 return this->emitDecPop(*T, E);
5089 }
5090
5091 if (T == PT_Float) {
5092 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
5093 if (!this->emitLoadFloat(E))
5094 return false;
5095 if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
5096 return false;
5097 if (!this->emitSubf(getRoundingMode(E), E))
5098 return false;
5099 if (!this->emitStoreFloat(E))
5100 return false;
5101 } else {
5102 assert(isIntegralType(*T));
5103 if (!this->emitLoad(*T, E))
5104 return false;
5105 if (!this->emitConst(1, E))
5106 return false;
5107 if (!this->emitSub(*T, E))
5108 return false;
5109 if (!this->emitStore(*T, E))
5110 return false;
5111 }
5112 return E->isGLValue() || this->emitLoadPop(*T, E);
5113 }
5114 case UO_LNot: // !x
5115 if (!T)
5116 return this->emitError(E);
5117
5118 if (DiscardResult)
5119 return this->discard(SubExpr);
5120
5121 if (!this->visitBool(SubExpr))
5122 return false;
5123
5124 if (!this->emitInv(E))
5125 return false;
5126
5127 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
5128 return this->emitCast(PT_Bool, ET, E);
5129 return true;
5130 case UO_Minus: // -x
5131 if (!T)
5132 return this->emitError(E);
5133
5134 if (!this->visit(SubExpr))
5135 return false;
5136 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
5137 case UO_Plus: // +x
5138 if (!T)
5139 return this->emitError(E);
5140
5141 if (!this->visit(SubExpr)) // noop
5142 return false;
5143 return DiscardResult ? this->emitPop(*T, E) : true;
5144 case UO_AddrOf: // &x
5145 if (E->getType()->isMemberPointerType()) {
5146 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
5147 // member can be formed.
5148 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
5149 }
5150 // We should already have a pointer when we get here.
5151 return this->delegate(SubExpr);
5152 case UO_Deref: // *x
5153 if (DiscardResult)
5154 return this->discard(SubExpr);
5155 return this->visit(SubExpr);
5156 case UO_Not: // ~x
5157 if (!T)
5158 return this->emitError(E);
5159
5160 if (!this->visit(SubExpr))
5161 return false;
5162 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
5163 case UO_Real: // __real x
5164 assert(T);
5165 return this->delegate(SubExpr);
5166 case UO_Imag: { // __imag x
5167 assert(T);
5168 if (!this->discard(SubExpr))
5169 return false;
5170 return this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
5171 }
5172 case UO_Extension:
5173 return this->delegate(SubExpr);
5174 case UO_Coawait:
5175 assert(false && "Unhandled opcode");
5176 }
5177
5178 return false;
5179}
5180
5181template <class Emitter>
5183 const Expr *SubExpr = E->getSubExpr();
5184 assert(SubExpr->getType()->isAnyComplexType());
5185
5186 if (DiscardResult)
5187 return this->discard(SubExpr);
5188
5189 std::optional<PrimType> ResT = classify(E);
5190 auto prepareResult = [=]() -> bool {
5191 if (!ResT && !Initializing) {
5192 std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
5193 if (!LocalIndex)
5194 return false;
5195 return this->emitGetPtrLocal(*LocalIndex, E);
5196 }
5197
5198 return true;
5199 };
5200
5201 // The offset of the temporary, if we created one.
5202 unsigned SubExprOffset = ~0u;
5203 auto createTemp = [=, &SubExprOffset]() -> bool {
5204 SubExprOffset = this->allocateLocalPrimitive(SubExpr, PT_Ptr, true, false);
5205 if (!this->visit(SubExpr))
5206 return false;
5207 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
5208 };
5209
5210 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
5211 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
5212 if (!this->emitGetLocal(PT_Ptr, Offset, E))
5213 return false;
5214 return this->emitArrayElemPop(ElemT, Index, E);
5215 };
5216
5217 switch (E->getOpcode()) {
5218 case UO_Minus:
5219 if (!prepareResult())
5220 return false;
5221 if (!createTemp())
5222 return false;
5223 for (unsigned I = 0; I != 2; ++I) {
5224 if (!getElem(SubExprOffset, I))
5225 return false;
5226 if (!this->emitNeg(ElemT, E))
5227 return false;
5228 if (!this->emitInitElem(ElemT, I, E))
5229 return false;
5230 }
5231 break;
5232
5233 case UO_Plus: // +x
5234 case UO_AddrOf: // &x
5235 case UO_Deref: // *x
5236 return this->delegate(SubExpr);
5237
5238 case UO_LNot:
5239 if (!this->visit(SubExpr))
5240 return false;
5241 if (!this->emitComplexBoolCast(SubExpr))
5242 return false;
5243 if (!this->emitInv(E))
5244 return false;
5245 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
5246 return this->emitCast(PT_Bool, ET, E);
5247 return true;
5248
5249 case UO_Real:
5250 return this->emitComplexReal(SubExpr);
5251
5252 case UO_Imag:
5253 if (!this->visit(SubExpr))
5254 return false;
5255
5256 if (SubExpr->isLValue()) {
5257 if (!this->emitConstUint8(1, E))
5258 return false;
5259 return this->emitArrayElemPtrPopUint8(E);
5260 }
5261
5262 // Since our _Complex implementation does not map to a primitive type,
5263 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
5264 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
5265
5266 case UO_Not: // ~x
5267 if (!this->visit(SubExpr))
5268 return false;
5269 // Negate the imaginary component.
5270 if (!this->emitArrayElem(ElemT, 1, E))
5271 return false;
5272 if (!this->emitNeg(ElemT, E))
5273 return false;
5274 if (!this->emitInitElem(ElemT, 1, E))
5275 return false;
5276 return DiscardResult ? this->emitPopPtr(E) : true;
5277
5278 case UO_Extension:
5279 return this->delegate(SubExpr);
5280
5281 default:
5282 return this->emitInvalid(E);
5283 }
5284
5285 return true;
5286}
5287
5288template <class Emitter>
5290 if (DiscardResult)
5291 return true;
5292
5293 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
5294 return this->emitConst(ECD->getInitVal(), E);
5295 } else if (const auto *BD = dyn_cast<BindingDecl>(D)) {
5296 return this->visit(BD->getBinding());
5297 } else if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
5298 const Function *F = getFunction(FuncDecl);
5299 return F && this->emitGetFnPtr(F, E);
5300 } else if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
5301 if (std::optional<unsigned> Index = P.getOrCreateGlobal(D)) {
5302 if (!this->emitGetPtrGlobal(*Index, E))
5303 return false;
5304 if (std::optional<PrimType> T = classify(E->getType())) {
5305 if (!this->visitAPValue(TPOD->getValue(), *T, E))
5306 return false;
5307 return this->emitInitGlobal(*T, *Index, E);
5308 }
5309 return this->visitAPValueInitializer(TPOD->getValue(), E);
5310 }
5311 return false;
5312 }
5313
5314 // References are implemented via pointers, so when we see a DeclRefExpr
5315 // pointing to a reference, we need to get its value directly (i.e. the
5316 // pointer to the actual value) instead of a pointer to the pointer to the
5317 // value.
5318 bool IsReference = D->getType()->isReferenceType();
5319
5320 // Check for local/global variables and parameters.
5321 if (auto It = Locals.find(D); It != Locals.end()) {
5322 const unsigned Offset = It->second.Offset;
5323 if (IsReference)
5324 return this->emitGetLocal(PT_Ptr, Offset, E);
5325 return this->emitGetPtrLocal(Offset, E);
5326 } else if (auto GlobalIndex = P.getGlobal(D)) {
5327 if (IsReference) {
5328 if (!Ctx.getLangOpts().CPlusPlus11)
5329 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
5330 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
5331 }
5332
5333 return this->emitGetPtrGlobal(*GlobalIndex, E);
5334 } else if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
5335 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
5336 if (IsReference || !It->second.IsPtr)
5337 return this->emitGetParam(classifyPrim(E), It->second.Offset, E);
5338
5339 return this->emitGetPtrParam(It->second.Offset, E);
5340 }
5341 }
5342
5343 // In case we need to re-visit a declaration.
5344 auto revisit = [&](const VarDecl *VD) -> bool {
5345 auto VarState = this->visitDecl(VD);
5346
5347 if (VarState.notCreated())
5348 return true;
5349 if (!VarState)
5350 return false;
5351 // Retry.
5352 return this->visitDeclRef(D, E);
5353 };
5354
5355 // Handle lambda captures.
5356 if (auto It = this->LambdaCaptures.find(D);
5357 It != this->LambdaCaptures.end()) {
5358 auto [Offset, IsPtr] = It->second;
5359
5360 if (IsPtr)
5361 return this->emitGetThisFieldPtr(Offset, E);
5362 return this->emitGetPtrThisField(Offset, E);
5363 } else if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
5364 DRE && DRE->refersToEnclosingVariableOrCapture()) {
5365 if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
5366 return revisit(VD);
5367 }
5368
5369 if (D != InitializingDecl) {
5370 // Try to lazily visit (or emit dummy pointers for) declarations
5371 // we haven't seen yet.
5372 if (Ctx.getLangOpts().CPlusPlus) {
5373 if (const auto *VD = dyn_cast<VarDecl>(D)) {
5374 const auto typeShouldBeVisited = [&](QualType T) -> bool {
5375 if (T.isConstant(Ctx.getASTContext()))
5376 return true;
5377 if (const auto *RT = T->getAs<ReferenceType>())
5378 return RT->getPointeeType().isConstQualified();
5379 return false;
5380 };
5381
5382 // DecompositionDecls are just proxies for us.
5383 if (isa<DecompositionDecl>(VD))
5384 return revisit(VD);
5385
5386 // Visit local const variables like normal.
5387 if ((VD->hasGlobalStorage() || VD->isLocalVarDecl() ||
5388 VD->isStaticDataMember()) &&
5389 typeShouldBeVisited(VD->getType()))
5390 return revisit(VD);
5391 }
5392 } else {
5393 if (const auto *VD = dyn_cast<VarDecl>(D);
5394 VD && VD->getAnyInitializer() &&
5395 VD->getType().isConstant(Ctx.getASTContext()) && !VD->isWeak())
5396 return revisit(VD);
5397 }
5398 }
5399
5400 if (std::optional<unsigned> I = P.getOrCreateDummy(D)) {
5401 if (!this->emitGetPtrGlobal(*I, E))
5402 return false;
5403 if (E->getType()->isVoidType())
5404 return true;
5405 // Convert the dummy pointer to another pointer type if we have to.
5406 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
5407 if (isPtrType(PT))
5408 return this->emitDecayPtr(PT_Ptr, PT, E);
5409 return false;
5410 }
5411 return true;
5412 }
5413
5414 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5415 return this->emitInvalidDeclRef(DRE, E);
5416 return false;
5417}
5418
5419template <class Emitter>
5421 const auto *D = E->getDecl();
5422 return this->visitDeclRef(D, E);
5423}
5424
5425template <class Emitter> void Compiler<Emitter>::emitCleanup() {
5426 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent())
5427 C->emitDestruction();
5428}
5429
5430template <class Emitter>
5431unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
5432 const QualType DerivedType) {
5433 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
5434 if (const auto *R = Ty->getPointeeCXXRecordDecl())
5435 return R;
5436 return Ty->getAsCXXRecordDecl();
5437 };
5438 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
5439 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
5440
5441 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
5442}
5443
5444/// Emit casts from a PrimType to another PrimType.
5445template <class Emitter>
5447 QualType ToQT, const Expr *E) {
5448
5449 if (FromT == PT_Float) {
5450 // Floating to floating.
5451 if (ToT == PT_Float) {
5452 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
5453 return this->emitCastFP(ToSem, getRoundingMode(E), E);
5454 }
5455
5456 if (ToT == PT_IntAP)
5457 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT), E);
5458 if (ToT == PT_IntAPS)
5459 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT), E);
5460
5461 // Float to integral.
5462 if (isIntegralType(ToT) || ToT == PT_Bool)
5463 return this->emitCastFloatingIntegral(ToT, E);
5464 }
5465
5466 if (isIntegralType(FromT) || FromT == PT_Bool) {
5467 if (ToT == PT_IntAP)
5468 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
5469 if (ToT == PT_IntAPS)
5470 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
5471
5472 // Integral to integral.
5473 if (isIntegralType(ToT) || ToT == PT_Bool)
5474 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
5475
5476 if (ToT == PT_Float) {
5477 // Integral to floating.
5478 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
5479 return this->emitCastIntegralFloating(FromT, ToSem, getRoundingMode(E),
5480 E);
5481 }
5482 }
5483
5484 return false;
5485}
5486
5487/// Emits __real(SubExpr)
5488template <class Emitter>
5489bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
5490 assert(SubExpr->getType()->isAnyComplexType());
5491
5492 if (DiscardResult)
5493 return this->discard(SubExpr);
5494
5495 if (!this->visit(SubExpr))
5496 return false;
5497 if (SubExpr->isLValue()) {
5498 if (!this->emitConstUint8(0, SubExpr))
5499 return false;
5500 return this->emitArrayElemPtrPopUint8(SubExpr);
5501 }
5502
5503 // Rvalue, load the actual element.
5504 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
5505 0, SubExpr);
5506}
5507
5508template <class Emitter>
5510 assert(!DiscardResult);
5511 PrimType ElemT = classifyComplexElementType(E->getType());
5512 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
5513 // for us, that means (bool)E[0] || (bool)E[1]
5514 if (!this->emitArrayElem(ElemT, 0, E))
5515 return false;
5516 if (ElemT == PT_Float) {
5517 if (!this->emitCastFloatingIntegral(PT_Bool, E))
5518 return false;
5519 } else {
5520 if (!this->emitCast(ElemT, PT_Bool, E))
5521 return false;
5522 }
5523
5524 // We now have the bool value of E[0] on the stack.
5525 LabelTy LabelTrue = this->getLabel();
5526 if (!this->jumpTrue(LabelTrue))
5527 return false;
5528
5529 if (!this->emitArrayElemPop(ElemT, 1, E))
5530 return false;
5531 if (ElemT == PT_Float) {
5532 if (!this->emitCastFloatingIntegral(PT_Bool, E))
5533 return false;
5534 } else {
5535 if (!this->emitCast(ElemT, PT_Bool, E))
5536 return false;
5537 }
5538 // Leave the boolean value of E[1] on the stack.
5539 LabelTy EndLabel = this->getLabel();
5540 this->jump(EndLabel);
5541
5542 this->emitLabel(LabelTrue);
5543 if (!this->emitPopPtr(E))
5544 return false;
5545 if (!this->emitConstBool(true, E))
5546 return false;
5547
5548 this->fallthrough(EndLabel);
5549 this->emitLabel(EndLabel);
5550
5551 return true;
5552}
5553
5554template <class Emitter>
5555bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
5556 const BinaryOperator *E) {
5557 assert(E->isComparisonOp());
5558 assert(!Initializing);
5559 assert(!DiscardResult);
5560
5561 PrimType ElemT;
5562 bool LHSIsComplex;
5563 unsigned LHSOffset;
5564 if (LHS->getType()->isAnyComplexType()) {
5565 LHSIsComplex = true;
5566 ElemT = classifyComplexElementType(LHS->getType());
5567 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true,
5568 /*IsExtended=*/false);
5569 if (!this->visit(LHS))
5570 return false;
5571 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
5572 return false;
5573 } else {
5574 LHSIsComplex = false;
5575 PrimType LHST = classifyPrim(LHS->getType());
5576 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
5577 if (!this->visit(LHS))
5578 return false;
5579 if (!this->emitSetLocal(LHST, LHSOffset, E))
5580 return false;
5581 }
5582
5583 bool RHSIsComplex;
5584 unsigned RHSOffset;
5585 if (RHS->getType()->isAnyComplexType()) {
5586 RHSIsComplex = true;
5587 ElemT = classifyComplexElementType(RHS->getType());
5588 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true,
5589 /*IsExtended=*/false);
5590 if (!this->visit(RHS))
5591 return false;
5592 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
5593 return false;
5594 } else {
5595 RHSIsComplex = false;
5596 PrimType RHST = classifyPrim(RHS->getType());
5597 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
5598 if (!this->visit(RHS))
5599 return false;
5600 if (!this->emitSetLocal(RHST, RHSOffset, E))
5601 return false;
5602 }
5603
5604 auto getElem = [&](unsigned LocalOffset, unsigned Index,
5605 bool IsComplex) -> bool {
5606 if (IsComplex) {
5607 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
5608 return false;
5609 return this->emitArrayElemPop(ElemT, Index, E);
5610 }
5611 return this->emitGetLocal(ElemT, LocalOffset, E);
5612 };
5613
5614 for (unsigned I = 0; I != 2; ++I) {
5615 // Get both values.
5616 if (!getElem(LHSOffset, I, LHSIsComplex))
5617 return false;
5618 if (!getElem(RHSOffset, I, RHSIsComplex))
5619 return false;
5620 // And compare them.
5621 if (!this->emitEQ(ElemT, E))
5622 return false;
5623
5624 if (!this->emitCastBoolUint8(E))
5625 return false;
5626 }
5627
5628 // We now have two bool values on the stack. Compare those.
5629 if (!this->emitAddUint8(E))
5630 return false;
5631 if (!this->emitConstUint8(2, E))
5632 return false;
5633
5634 if (E->getOpcode() == BO_EQ) {
5635 if (!this->emitEQUint8(E))
5636 return false;
5637 } else if (E->getOpcode() == BO_NE) {
5638 if (!this->emitNEUint8(E))
5639 return false;
5640 } else
5641 return false;
5642
5643 // In C, this returns an int.
5644 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
5645 return this->emitCast(PT_Bool, ResT, E);
5646 return true;
5647}
5648
5649/// When calling this, we have a pointer of the local-to-destroy
5650/// on the stack.
5651/// Emit destruction of record types (or arrays of record types).
5652template <class Emitter>
5654 assert(R);
5655 const CXXDestructorDecl *Dtor = R->getDestructor();
5656 if (!Dtor || Dtor->isTrivial())
5657 return true;
5658
5659 assert(Dtor);
5660 const Function *DtorFunc = getFunction(Dtor);
5661 if (!DtorFunc)
5662 return false;
5663 assert(DtorFunc->hasThisPointer());
5664 assert(DtorFunc->getNumParams() == 1);
5665 if (!this->emitDupPtr(SourceInfo{}))
5666 return false;
5667 return this->emitCall(DtorFunc, 0, SourceInfo{});
5668}
5669/// When calling this, we have a pointer of the local-to-destroy
5670/// on the stack.
5671/// Emit destruction of record types (or arrays of record types).
5672template <class Emitter>
5674 assert(Desc);
5675 assert(!Desc->isPrimitive());
5676 assert(!Desc->isPrimitiveArray());
5677
5678 // Arrays.
5679 if (Desc->isArray()) {
5680 const Descriptor *ElemDesc = Desc->ElemDesc;
5681 assert(ElemDesc);
5682
5683 // Don't need to do anything for these.
5684 if (ElemDesc->isPrimitiveArray())
5685 return true;
5686
5687 // If this is an array of record types, check if we need
5688 // to call the element destructors at all. If not, try
5689 // to save the work.
5690 if (const Record *ElemRecord = ElemDesc->ElemRecord) {
5691 if (const CXXDestructorDecl *Dtor = ElemRecord->getDestructor();
5692 !Dtor || Dtor->isTrivial())
5693 return true;
5694 }
5695
5696 for (ssize_t I = Desc->getNumElems() - 1; I >= 0; --I) {
5697 if (!this->emitConstUint64(I, SourceInfo{}))
5698 return false;
5699 if (!this->emitArrayElemPtrUint64(SourceInfo{}))
5700 return false;
5701 if (!this->emitDestruction(ElemDesc))
5702 return false;
5703 if (!this->emitPopPtr(SourceInfo{}))
5704 return false;
5705 }
5706 return true;
5707 }
5708
5709 assert(Desc->ElemRecord);
5710 return this->emitRecordDestruction(Desc->ElemRecord);
5711}
5712
5713namespace clang {
5714namespace interp {
5715
5716template class Compiler<ByteCodeEmitter>;
5717template class Compiler<EvalEmitter>;
5718
5719} // namespace interp
5720} // namespace clang
#define V(N, I)
Definition: ASTContext.h:3341
ASTImporterLookupTable & LT
DynTypedNode Node
StringRef P
const Decl * D
Expr * E
static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx, UnaryExprOrTypeTrait Kind)
Definition: Compiler.cpp:1619
llvm::APSInt APSInt
Definition: Compiler.cpp:22
bool IsStatic
Definition: Format.cpp:3013
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
const LValueBase getLValueBase() const
Definition: APValue.cpp:974
APValue & getArrayInitializedElt(unsigned I)
Definition: APValue.h:510
ArrayRef< LValuePathEntry > getLValuePath() const
Definition: APValue.cpp:994
APSInt & getInt()
Definition: APValue.h:423
APValue & getStructField(unsigned i)
Definition: APValue.h:551
const FieldDecl * getUnionField() const
Definition: APValue.h:563
unsigned getStructNumFields() const
Definition: APValue.h:542
bool isArray() const
Definition: APValue.h:408
bool isFloat() const
Definition: APValue.h:402
const ValueDecl * getMemberPointerDecl() const
Definition: APValue.cpp:1057
APValue & getUnionValue()
Definition: APValue.h:567
bool isLValue() const
Definition: APValue.h:406
bool isMemberPointer() const
Definition: APValue.h:411
bool isInt() const
Definition: APValue.h:401
unsigned getArraySize() const
Definition: APValue.h:533
bool isUnion() const
Definition: APValue.h:410
@ None
There is no such object (it's outside its lifetime).
Definition: APValue.h:129
bool isStruct() const
Definition: APValue.h:409
bool isNullPointer() const
Definition: APValue.cpp:1010
APFloat & getFloat()
Definition: APValue.h:437
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
unsigned getPreferredTypeAlign(QualType T) const
Return the "preferred" alignment of the specified type T for the current target, in bits.
Definition: ASTContext.h:2485
const LangOptions & getLangOpts() const
Definition: ASTContext.h:797
TypeInfoChars getTypeInfoDataSizeInChars(QualType T) const
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition: Expr.h:4175
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:4372
Represents the index of the current element of an array being initialized by an ArrayInitLoopExpr.
Definition: Expr.h:5756
Represents a loop initializing the elements of an array.
Definition: Expr.h:5703
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition: Expr.h:2674
An Embarcadero array type trait, as used in the implementation of __array_rank and __array_extent.
Definition: ExprCXX.h:2853
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3566
Attr - This represents one attribute.
Definition: Attr.h:42
Represents an attribute applied to a statement.
Definition: Stmt.h:2090
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3860
static bool isLogicalOp(Opcode Opc)
Definition: Expr.h:3993
Expr * getLHS() const
Definition: Expr.h:3910
static bool isComparisonOp(Opcode Opc)
Definition: Expr.h:3960
static bool isCommaOp(Opcode Opc)
Definition: Expr.h:3963
Expr * getRHS() const
Definition: Expr.h:3912
static bool isPtrMemOp(Opcode Opc)
predicates to categorize the respective opcodes.
Definition: Expr.h:3937
Opcode getOpcode() const
Definition: Expr.h:3905
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6365
BreakStmt - This represents a break.
Definition: Stmt.h:2990
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1491
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition: ExprCXX.h:720
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1546
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2539
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition: DeclCXX.cpp:2811
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1268
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1375
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition: ExprCXX.h:2498
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2803
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition: StmtCXX.h:135
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition: ExprCXX.h:1737
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2064
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2190
bool isLambdaStaticInvoker() const
Determine whether this is a lambda closure type's static member function that is used for the result ...
Definition: DeclCXX.cpp:2639
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition: ExprCXX.h:2241
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition: ExprCXX.h:4126
The null pointer literal (C++11 [lex.nullptr])
Definition: ExprCXX.h:765
Represents a list-initialization with parenthesis.
Definition: ExprCXX.h:4954
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
capture_const_iterator captures_end() const
Definition: DeclCXX.h:1112
capture_const_iterator captures_begin() const
Definition: DeclCXX.h:1106
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition: DeclCXX.cpp:1633
A C++ reinterpret_cast expression (C++ [expr.reinterpret.cast]).
Definition: ExprCXX.h:523
A rewritten comparison expression that was originally written using operator syntax.
Definition: ExprCXX.h:283
An expression "T()" which creates an rvalue of a non-class type T.
Definition: ExprCXX.h:2182
Implicit construction of a std::initializer_list<T> object from an array temporary within list-initia...
Definition: ExprCXX.h:797
Represents the this expression in C++.
Definition: ExprCXX.h:1152
A C++ throw-expression (C++ [except.throw]).
Definition: ExprCXX.h:1206
CXXTryStmt - A C++ try block, including all handlers.
Definition: StmtCXX.h:69
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:1066
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2830
CaseStmt - Represent a case statement.
Definition: Stmt.h:1811
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:3498
CastKind getCastKind() const
Definition: Expr.h:3542
llvm::iterator_range< path_iterator > path()
Path through the class hierarchy taken by casts between base and derived classes (see implementation ...
Definition: Expr.h:3585
Expr * getSubExpr()
Definition: Expr.h:3548
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4592
Complex values, per C99 6.2.5p11.
Definition: Type.h:3134
QualType getElementType() const
Definition: Type.h:3144
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:4122
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:3428
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1611
body_range body()
Definition: Stmt.h:1674
Stmt * getStmtExprResult()
Definition: Stmt.h:1733
Represents the specialization of a concept - evaluates to a prvalue of type bool.
Definition: ExprConcepts.h:42
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:3604
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition: Type.h:3680
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
Definition: Expr.h:1077
ContinueStmt - This represents a continue.
Definition: Stmt.h:2960
ConvertVectorExpr - Clang builtin function __builtin_convertvector This AST node provides support for...
Definition: Expr.h:4533
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2090
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition: Stmt.h:1502
decl_range decls()
Definition: Stmt.h:1550
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
bool isInvalidDecl() const
Definition: DeclBase.h:595
DoStmt - This represents a 'do/while' stmt.
Definition: Stmt.h:2735
Represents a reference to #emded data.
Definition: Expr.h:4867
bool isFixed() const
Returns true if this is an Objective-C, C++11, or Microsoft-style enumeration with a fixed underlying...
Definition: Decl.h:4058
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:5991
EnumDecl * getDecl() const
Definition: Type.h:5998
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:3474
This represents one expression.
Definition: Expr.h:110
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition: Expr.cpp:82
bool isGLValue() const
Definition: Expr.h:280
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition: Expr.h:175
bool containsErrors() const
Whether this expression contains subexpressions which had errors, e.g.
Definition: Expr.h:245
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3066
bool isPRValue() const
Definition: Expr.h:278
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition: Expr.h:277
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition: Expr.cpp:3567
bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const
Determine whether the result of this expression is a temporary object of the given class type.
Definition: Expr.cpp:3204
bool refersToBitField() const
Returns true if this expression is a gl-value that potentially refers to a bit-field.
Definition: Expr.h:469
QualType getType() const
Definition: Expr.h:142
An expression trait intrinsic.
Definition: ExprCXX.h:2924
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
Definition: Expr.h:6305
Represents a member of a struct/union/class.
Definition: Decl.h:3030
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3247
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
Definition: Decl.h:3124
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition: Stmt.h:2791
Represents a function declaration or definition.
Definition: Decl.h:1932
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2669
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:3224
QualType getReturnType() const
Definition: Decl.h:2717
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2646
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2302
bool isDefaulted() const
Whether this function is defaulted.
Definition: Decl.h:2310
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3678
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3144
GNUNullExpr - Implements the GNU __null extension, which is a name for a null pointer constant that h...
Definition: Expr.h:4667
Represents a C11 generic selection.
Definition: Expr.h:5917
IfStmt - This represents an if/then/else.
Definition: Stmt.h:2148
Stmt * getThen()
Definition: Stmt.h:2237
Stmt * getInit()
Definition: Stmt.h:2298
bool isNonNegatedConsteval() const
Definition: Stmt.h:2333
Expr * getCond()
Definition: Stmt.h:2225
bool isNegatedConsteval() const
Definition: Stmt.h:2337
Stmt * getElse()
Definition: Stmt.h:2246
DeclStmt * getConditionVariableDeclStmt()
If this IfStmt has a condition variable, return the faux DeclStmt associated with the creation of tha...
Definition: Stmt.h:2281
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1....
Definition: Expr.h:1717
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:5792
Represents a field injected from an anonymous union/struct into the parent scope.
Definition: Decl.h:3318
Describes an C or C++ initializer list.
Definition: Expr.h:5039
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1954
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition: DeclCXX.h:3233
A global _GUID constant.
Definition: DeclCXX.h:4293
APValue & getAsAPValue() const
Get the value of this MSGuidDecl as an APValue.
Definition: DeclCXX.cpp:3508
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition: ExprCXX.h:4728
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3187
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:3508
const Type * getClass() const
Definition: Type.h:3538
This represents a decl that may have a name.
Definition: Decl.h:249
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition: ExprObjC.h:87
ObjCBoxedExpr - used for generalized expression boxing.
Definition: ExprObjC.h:127
ObjCEncodeExpr, used for @encode in Objective-C.
Definition: ExprObjC.h:410
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition: ExprObjC.h:51
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition: Expr.h:2475
Helper class for OffsetOfExpr.
Definition: Expr.h:2369
@ Array
An index into an array.
Definition: Expr.h:2374
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1173
ParenExpr - This represents a parenthesized expression, e.g.
Definition: Expr.h:2135
Represents a parameter to a function.
Definition: Decl.h:1722
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:3187
QualType getPointeeType() const
Definition: Type.h:3197
[C99 6.4.2.2] - A predefined identifier such as func.
Definition: Expr.h:1991
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6497
A (possibly-)qualified type.
Definition: Type.h:941
QualType withConst() const
Definition: Type.h:1166
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:1008
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7750
QualType getCanonicalType() const
Definition: Type.h:7802
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:7823
Represents a struct/union/class.
Definition: Decl.h:4145
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5965
Frontend produces RecoveryExprs on semantic errors that prevent creating other well-formed expression...
Definition: Expr.h:7101
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:3428
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
Definition: ExprConcepts.h:510
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition: Stmt.h:3029
Expr * getRetValue()
Definition: Stmt.h:3060
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
ShuffleVectorExpr - clang-specific builtin-in function __builtin_shufflevector.
Definition: Expr.h:4465
Represents an expression that computes the length of a parameter pack.
Definition: ExprCXX.h:4258
Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(), __builtin_FUNCTION(),...
Definition: Expr.h:4761
Represents a C++11 static_assert declaration.
Definition: DeclCXX.h:4062
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:4417
Stmt - This represents one statement.
Definition: Stmt.h:84
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1778
static StringLiteral * Create(const ASTContext &Ctx, StringRef Str, StringLiteralKind Kind, bool Pascal, QualType Ty, const SourceLocation *Loc, unsigned NumConcatenated)
This is the "fully general" constructor that allows representation of strings formed from multiple co...
Definition: Expr.cpp:1191
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:4484
SwitchStmt - This represents a 'switch' stmt.
Definition: Stmt.h:2398
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3561
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3664
bool isUnion() const
Definition: Decl.h:3767
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition: ExprCXX.h:2768
The base class of the type hierarchy.
Definition: Type.h:1829
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1882
bool isVoidType() const
Definition: Type.h:8319
bool isBooleanType() const
Definition: Type.h:8447
bool isLiteralType(const ASTContext &Ctx) const
Return true if this is a literal type (C++11 [basic.types]p10)
Definition: Type.cpp:2892
bool isIncompleteArrayType() const
Definition: Type.h:8083
bool isNothrowT() const
Definition: Type.cpp:3061
bool isVoidPointerType() const
Definition: Type.cpp:665
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
Definition: Type.cpp:2352
bool isArrayType() const
Definition: Type.h:8075
bool isPointerType() const
Definition: Type.h:8003
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition: Type.h:8359
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8607
bool isEnumeralType() const
Definition: Type.h:8107
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:705
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:8434
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition: Type.h:2695
bool isAnyComplexType() const
Definition: Type.h:8111
bool isMemberPointerType() const
Definition: Type.h:8057
bool isAtomicType() const
Definition: Type.h:8158
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition: Type.h:8593
bool isFunctionType() const
Definition: Type.h:7999
bool isVectorType() const
Definition: Type.h:8115
bool isFloatingType() const
Definition: Type.cpp:2249
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
bool isRecordType() const
Definition: Type.h:8103
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.cpp:1886
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3409
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition: Expr.h:2578
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2188
Represents a C++ using-enum-declaration.
Definition: DeclCXX.h:3717
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:667
QualType getType() const
Definition: Decl.h:678
QualType getType() const
Definition: Value.cpp:234
Represents a variable declaration or definition.
Definition: Decl.h:879
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition: Decl.h:1156
const Expr * getInit() const
Definition: Decl.h:1316
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1201
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition: Decl.h:1306
Represents a GCC generic vector type.
Definition: Type.h:4021
unsigned getNumElements() const
Definition: Type.h:4036
QualType getElementType() const
Definition: Type.h:4035
WhileStmt - This represents a 'while' stmt.
Definition: Stmt.h:2594
Scope for storage declared in a compound statement.
Definition: Compiler.h:563
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:121
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:102
Compilation context for expressions.
Definition: Compiler.h:104
OptLabelTy BreakLabel
Point to break to.
Definition: Compiler.h:403
bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E)
Definition: Compiler.cpp:1785
bool VisitCXXDeleteExpr(const CXXDeleteExpr *E)
Definition: Compiler.cpp:2902
bool VisitOffsetOfExpr(const OffsetOfExpr *E)
Definition: Compiler.cpp:2653
bool visitContinueStmt(const ContinueStmt *S)
Definition: Compiler.cpp:4580
bool VisitCharacterLiteral(const CharacterLiteral *E)
Definition: Compiler.cpp:2000
bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E)
Definition: Compiler.cpp:1586
bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E)
Definition: Compiler.cpp:2975
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
Definition: Compiler.cpp:2326
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
Definition: Compiler.cpp:3291
bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E)
Definition: Compiler.cpp:4175
bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) override
Toplevel visitDeclAndReturn().
Definition: Compiler.cpp:3674
bool visitExpr(const Expr *E) override
Definition: Compiler.cpp:3607
bool VisitTypeTraitExpr(const TypeTraitExpr *E)
Definition: Compiler.cpp:2391
bool VisitLambdaExpr(const LambdaExpr *E)
Definition: Compiler.cpp:2407
bool VisitMemberExpr(const MemberExpr *E)
Definition: Compiler.cpp:1733
bool VisitBinaryOperator(const BinaryOperator *E)
Definition: Compiler.cpp:693
bool visitAttributedStmt(const AttributedStmt *S)
Definition: Compiler.cpp:4671
bool VisitPackIndexingExpr(const PackIndexingExpr *E)
Definition: Compiler.cpp:3014
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
Definition: Compiler.cpp:1289
bool VisitCallExpr(const CallExpr *E)
Definition: Compiler.cpp:3989
bool VisitPseudoObjectExpr(const PseudoObjectExpr *E)
Definition: Compiler.cpp:2990
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E)
Definition: Compiler.cpp:2462
const Function * getFunction(const FunctionDecl *FD)
Returns a function for the given FunctionDecl.
Definition: Compiler.cpp:3603
void emitCleanup()
Emits scope cleanup instructions.
Definition: Compiler.cpp:5425
bool VisitCastExpr(const CastExpr *E)
Definition: Compiler.cpp:178
bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E)
Definition: Compiler.cpp:1965
bool VisitComplexUnaryOperator(const UnaryOperator *E)
Definition: Compiler.cpp:5182
llvm::DenseMap< const SwitchCase *, LabelTy > CaseMap
Definition: Compiler.h:110
bool visitDeclStmt(const DeclStmt *DS)
Definition: Compiler.cpp:4315
bool VisitBlockExpr(const BlockExpr *E)
Definition: Compiler.cpp:2913
bool visitAPValue(const APValue &Val, PrimType ValType, const Expr *E)
Visit an APValue.
Definition: Compiler.cpp:3844
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E)
Definition: Compiler.cpp:2688
bool VisitLogicalBinOp(const BinaryOperator *E)
Definition: Compiler.cpp:921
bool visitCompoundStmt(const CompoundStmt *S)
Definition: Compiler.cpp:4306
bool visitDeclRef(const ValueDecl *D, const Expr *E)
Visit the given decl as if we have a reference to it.
Definition: Compiler.cpp:5289
bool visitBreakStmt(const BreakStmt *S)
Definition: Compiler.cpp:4571
bool visitForStmt(const ForStmt *S)
Definition: Compiler.cpp:4468
bool VisitDeclRefExpr(const DeclRefExpr *E)
Definition: Compiler.cpp:5420
bool VisitOpaqueValueExpr(const OpaqueValueExpr *E)
Definition: Compiler.cpp:1824
bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E)
Definition: Compiler.cpp:1794
OptLabelTy DefaultLabel
Default case label.
Definition: Compiler.h:407
bool VisitStmtExpr(const StmtExpr *E)
Definition: Compiler.cpp:3212
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E)
Definition: Compiler.cpp:4194
bool VisitCXXNewExpr(const CXXNewExpr *E)
Definition: Compiler.cpp:2802
bool VisitCompoundAssignOperator(const CompoundAssignOperator *E)
Definition: Compiler.cpp:2118
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init)
Pointer to the array(not the element!) must be on the stack when calling this.
Definition: Compiler.cpp:1559
bool delegate(const Expr *E)
Just pass evaluation on to E.
Definition: Compiler.cpp:3240
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
Definition: Compiler.cpp:3234
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
Definition: Compiler.cpp:4182
CaseMap CaseLabels
Switch case mapping.
Definition: Compiler.h:400
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
Definition: Compiler.cpp:3591
bool visit(const Expr *E)
Evaluates an expression and places the result on the stack.
Definition: Compiler.cpp:3250
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
Definition: Compiler.cpp:3585
bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E)
Definition: Compiler.cpp:3179
std::optional< unsigned > allocateLocal(DeclTy &&Decl, const ValueDecl *ExtendingDecl=nullptr)
Allocates a space storing a local given its type.
Definition: Compiler.cpp:3523
bool visitInitList(ArrayRef< const Expr * > Inits, const Expr *ArrayFiller, const Expr *E)
Definition: Compiler.cpp:1319
bool VisitSizeOfPackExpr(const SizeOfPackExpr *E)
Definition: Compiler.cpp:2747
bool VisitPredefinedExpr(const PredefinedExpr *E)
Definition: Compiler.cpp:2446
bool VisitSourceLocExpr(const SourceLocExpr *E)
Definition: Compiler.cpp:2597
bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E)
Definition: Compiler.cpp:3108
bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
Definition: Compiler.cpp:1960
bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E)
Definition: Compiler.cpp:2400
bool visitInitializer(const Expr *E)
Compiles an initializer.
Definition: Compiler.cpp:3277
bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E)
Definition: Compiler.cpp:2320
bool VisitPointerArithBinOp(const BinaryOperator *E)
Perform addition/subtraction of a pointer and an integer or subtraction of two pointers.
Definition: Compiler.cpp:873
bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E)
Definition: Compiler.cpp:2763
bool visitDefaultStmt(const DefaultStmt *S)
Definition: Compiler.cpp:4665
typename Emitter::LabelTy LabelTy
Definition: Compiler.h:107
VarCreationState visitDecl(const VarDecl *VD)
Definition: Compiler.cpp:3646
bool visitStmt(const Stmt *S)
Definition: Compiler.cpp:4256
bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E)
Definition: Compiler.cpp:2924
bool visitAPValueInitializer(const APValue &Val, const Expr *E)
Definition: Compiler.cpp:3871
bool VisitCXXConstructExpr(const CXXConstructExpr *E)
Definition: Compiler.cpp:2483
bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
Definition: Compiler.cpp:4202
bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
Definition: Compiler.cpp:3170
bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E)
Definition: Compiler.cpp:2771
bool VisitRecoveryExpr(const RecoveryExpr *E)
Definition: Compiler.cpp:3019
bool VisitRequiresExpr(const RequiresExpr *E)
Definition: Compiler.cpp:2967
bool Initializing
Flag inidicating if we're initializing an already created variable.
Definition: Compiler.h:390
bool visitReturnStmt(const ReturnStmt *RS)
Definition: Compiler.cpp:4332
bool VisitCXXThrowExpr(const CXXThrowExpr *E)
Definition: Compiler.cpp:2454
bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
Definition: Compiler.cpp:1592
bool VisitChooseExpr(const ChooseExpr *E)
Definition: Compiler.cpp:2758
bool visitFunc(const FunctionDecl *F) override
Definition: Compiler.cpp:4937
bool visitCXXForRangeStmt(const CXXForRangeStmt *S)
Definition: Compiler.cpp:4515
bool visitCaseStmt(const CaseStmt *S)
Definition: Compiler.cpp:4659
bool VisitComplexBinOp(const BinaryOperator *E)
Definition: Compiler.cpp:982
bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E)
Definition: Compiler.cpp:1860
OptLabelTy ContinueLabel
Point to continue to.
Definition: Compiler.h:405
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
Definition: Compiler.cpp:1205
bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E)
Definition: Compiler.cpp:2984
bool VisitUnaryOperator(const UnaryOperator *E)
Definition: Compiler.cpp:4963
bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E)
Definition: Compiler.cpp:2007
bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
Definition: Compiler.cpp:2752
bool visitDoStmt(const DoStmt *S)
Definition: Compiler.cpp:4440
bool VisitIntegerLiteral(const IntegerLiteral *E)
Definition: Compiler.cpp:650
bool VisitInitListExpr(const InitListExpr *E)
Definition: Compiler.cpp:1581
bool VisitStringLiteral(const StringLiteral *E)
Definition: Compiler.cpp:1903
bool VisitParenExpr(const ParenExpr *E)
Definition: Compiler.cpp:688
bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E)
Definition: Compiler.cpp:2474
bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E)
Definition: Compiler.cpp:3066
bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E)
Definition: Compiler.cpp:2081
std::optional< LabelTy > OptLabelTy
Definition: Compiler.h:109
bool DiscardResult
Flag indicating if return value is to be discarded.
Definition: Compiler.h:384
bool VisitEmbedExpr(const EmbedExpr *E)
Definition: Compiler.cpp:1614
bool VisitConvertVectorExpr(const ConvertVectorExpr *E)
Definition: Compiler.cpp:3034
bool VisitCXXThisExpr(const CXXThisExpr *E)
Definition: Compiler.cpp:4222
bool VisitConstantExpr(const ConstantExpr *E)
Definition: Compiler.cpp:1598
unsigned allocateTemporary(const Expr *E)
Definition: Compiler.cpp:3564
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
Definition: Compiler.cpp:1643
bool visitSwitchStmt(const SwitchStmt *S)
Definition: Compiler.cpp:4589
bool VisitCXXUuidofExpr(const CXXUuidofExpr *E)
Definition: Compiler.cpp:2930
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst, bool IsExtended=false)
Creates a local primitive value.
Definition: Compiler.cpp:3498
bool VisitExprWithCleanups(const ExprWithCleanups *E)
Definition: Compiler.cpp:2239
bool visitWhileStmt(const WhileStmt *S)
Definition: Compiler.cpp:4409
bool visitIfStmt(const IfStmt *IS)
Definition: Compiler.cpp:4366
bool VisitAddrLabelExpr(const AddrLabelExpr *E)
Definition: Compiler.cpp:3024
bool VisitFloatingLiteral(const FloatingLiteral *E)
Definition: Compiler.cpp:658
bool VisitBuiltinCallExpr(const CallExpr *E)
Definition: Compiler.cpp:3934
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
Definition: Compiler.cpp:2247
bool VisitGNUNullExpr(const GNUNullExpr *E)
Definition: Compiler.cpp:4211
bool VisitImaginaryLiteral(const ImaginaryLiteral *E)
Definition: Compiler.cpp:666
bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E)
Definition: Compiler.cpp:1976
VarCreationState visitVarDecl(const VarDecl *VD, bool Toplevel=false)
Creates and initializes a variable from the given decl.
Definition: Compiler.cpp:3734
bool visitCXXTryStmt(const CXXTryStmt *S)
Definition: Compiler.cpp:4702
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition: Context.h:97
Scope used to handle temporaries in toplevel variable declarations.
Definition: Compiler.cpp:28
void addExtended(const Scope::Local &Local) override
Definition: Compiler.cpp:37
DeclScope(Compiler< Emitter > *Ctx, const ValueDecl *VD)
Definition: Compiler.cpp:30
Bytecode function.
Definition: Function.h:81
unsigned getNumParams() const
Definition: Function.h:206
bool hasThisPointer() const
Definition: Function.h:186
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:116
Scope managing label targets.
Definition: Compiler.cpp:99
LabelScope(Compiler< Emitter > *Ctx)
Definition: Compiler.cpp:104
Compiler< Emitter > * Ctx
Compiler instance.
Definition: Compiler.cpp:106
Generic scope for local variables.
Definition: Compiler.h:475
bool destroyLocals(const Expr *E=nullptr) override
Explicit destruction of local variables.
Definition: Compiler.h:499
void addLocal(const Scope::Local &Local) override
Definition: Compiler.h:509
Sets the context for break/continue statements.
Definition: Compiler.cpp:110
typename Compiler< Emitter >::LabelTy LabelTy
Definition: Compiler.cpp:112
LoopScope(Compiler< Emitter > *Ctx, LabelTy BreakLabel, LabelTy ContinueLabel)
Definition: Compiler.cpp:115
typename Compiler< Emitter >::OptLabelTy OptLabelTy
Definition: Compiler.cpp:113
Scope used to handle initialization methods.
Definition: Compiler.cpp:52
OptionScope(Compiler< Emitter > *Ctx, bool NewDiscardResult, bool NewInitializing)
Root constructor, compiling or discarding primitives.
Definition: Compiler.cpp:55
Context to manage declaration lifetimes.
Definition: Program.h:132
Structure/Class descriptor.
Definition: Record.h:25
bool isUnion() const
Checks if the record is a union.
Definition: Record.h:56
const CXXDestructorDecl * getDestructor() const
Returns the destructor of the record, if any.
Definition: Record.h:70
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition: Record.cpp:39
llvm::iterator_range< const_base_iter > bases() const
Definition: Record.h:85
const Base * getVirtualBase(const RecordDecl *RD) const
Returns a virtual base descriptor.
Definition: Record.cpp:59
unsigned getNumFields() const
Definition: Record.h:81
llvm::iterator_range< const_field_iter > fields() const
Definition: Record.h:77
const Base * getBase(const RecordDecl *FD) const
Returns a base descriptor.
Definition: Record.cpp:45
Describes a scope block.
Definition: Function.h:36
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
StmtExprScope(Compiler< Emitter > *Ctx)
Definition: Compiler.cpp:163
typename Compiler< Emitter >::LabelTy LabelTy
Definition: Compiler.cpp:135
typename Compiler< Emitter >::OptLabelTy OptLabelTy
Definition: Compiler.cpp:136
SwitchScope(Compiler< Emitter > *Ctx, CaseMap &&CaseLabels, LabelTy BreakLabel, OptLabelTy DefaultLabel)
Definition: Compiler.cpp:139
typename Compiler< Emitter >::CaseMap CaseMap
Definition: Compiler.cpp:137
Scope chain managing the variable lifetimes.
Definition: Compiler.h:414
Compiler< Emitter > * Ctx
Compiler instance.
Definition: Compiler.h:468
llvm::APInt APInt
Definition: Integral.h:29
constexpr bool isPtrType(PrimType T)
Definition: PrimType.h:51
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:126
bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:2043
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1104
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:33
llvm::BitVector collectNonNullArgs(const FunctionDecl *F, const llvm::ArrayRef< const Expr * > &Args)
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:23
llvm::APSInt APSInt
Definition: Floating.h:24
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:28
constexpr bool isIntegralType(PrimType T)
Definition: PrimType.h:72
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition: Address.h:328
BinaryOperatorKind
UnaryExprOrTypeTrait
Names for the "expression or type" traits.
Definition: TypeTraits.h:51
@ SD_Static
Static storage duration.
Definition: Specifiers.h:331
const FunctionProtoType * T
@ Success
Template argument deduction was successful.
#define true
Definition: stdbool.h:25
Describes a memory block created by an allocation site.
Definition: Descriptor.h:111
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:237
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:251
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:143
static constexpr MetadataSize InlineDescMD
Definition: Descriptor.h:132
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:242
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:141
bool isArray() const
Checks if the descriptor is of an array.
Definition: Descriptor.h:254
Descriptor used for global variables.
Definition: Descriptor.h:58
const FieldDecl * Decl
Definition: Record.h:29
Information about a local's storage.
Definition: Function.h:39
State encapsulating if a the variable creation has been successful, unsuccessful, or no variable has ...
Definition: Compiler.h:91
static VarCreationState NotCreated()
Definition: Compiler.h:95