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