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