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