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