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