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