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().getClangABICompat() <= 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 const auto *VT = E->getType()->castAs<VectorType>();
4394 QualType ElemType = VT->getElementType();
4395 PrimType ElemT = classifyPrim(ElemType);
4396 QualType SrcType = Src->getType();
4397 PrimType SrcElemT = classifyVectorElementType(SrcType);
4398
4399 if (!Initializing) {
4400 UnsignedOrNone LocalIndex = allocateLocal(E);
4401 if (!LocalIndex)
4402 return false;
4403 if (!this->emitGetPtrLocal(*LocalIndex, E))
4404 return false;
4405 }
4406
4407 unsigned SrcOffset =
4408 this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
4409 if (!this->visit(Src))
4410 return false;
4411 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
4412 return false;
4413
4414 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
4415 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
4416 return false;
4417 if (!this->emitArrayElemPop(SrcElemT, I, E))
4418 return false;
4419
4420 // Cast to the desired result element type.
4421 if (SrcElemT != ElemT) {
4422 if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
4423 return false;
4424 } else if (ElemType->isFloatingType() && SrcType != ElemType) {
4425 const auto *TargetSemantics = &Ctx.getFloatSemantics(ElemType);
4426 if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E))
4427 return false;
4428 }
4429 if (!this->emitInitElem(ElemT, I, E))
4430 return false;
4431 }
4432 return true;
4433}
4434
4435template <class Emitter>
4437 return emitVectorConversion(E->getSrcExpr(), E);
4438}
4439
4440template <class Emitter>
4442 // FIXME: Unary shuffle with mask not currently supported.
4443 if (E->getNumSubExprs() == 2)
4444 return this->emitInvalid(E);
4445
4446 assert(E->getNumSubExprs() > 2);
4447
4448 const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
4449 const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
4450 PrimType ElemT = classifyPrim(VT->getElementType());
4451 unsigned NumInputElems = VT->getNumElements();
4452 unsigned NumOutputElems = E->getNumSubExprs() - 2;
4453 assert(NumOutputElems > 0);
4454
4455 if (!Initializing) {
4456 UnsignedOrNone LocalIndex = allocateLocal(E);
4457 if (!LocalIndex)
4458 return false;
4459 if (!this->emitGetPtrLocal(*LocalIndex, E))
4460 return false;
4461 }
4462
4463 // Save both input vectors to a local variable.
4464 unsigned VectorOffsets[2];
4465 for (unsigned I = 0; I != 2; ++I) {
4466 VectorOffsets[I] =
4467 this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
4468 if (!this->visit(Vecs[I]))
4469 return false;
4470 if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
4471 return false;
4472 }
4473 for (unsigned I = 0; I != NumOutputElems; ++I) {
4474 APSInt ShuffleIndex = E->getShuffleMaskIdx(I);
4475 assert(ShuffleIndex >= -1);
4476 if (ShuffleIndex == -1)
4477 return this->emitInvalidShuffleVectorIndex(I, E);
4478
4479 assert(ShuffleIndex < (NumInputElems * 2));
4480 if (!this->emitGetLocal(PT_Ptr,
4481 VectorOffsets[ShuffleIndex >= NumInputElems], E))
4482 return false;
4483 unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
4484 if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
4485 return false;
4486
4487 if (!this->emitInitElem(ElemT, I, E))
4488 return false;
4489 }
4490
4491 if (DiscardResult)
4492 return this->emitPopPtr(E);
4493
4494 return true;
4495}
4496
4497template <class Emitter>
4499 const ExtVectorElementExpr *E) {
4500 const Expr *Base = E->getBase();
4501 assert(
4502 Base->getType()->isVectorType() ||
4503 Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
4504
4506 E->getEncodedElementAccess(Indices);
4507
4508 if (Indices.size() == 1) {
4509 if (!this->visit(Base))
4510 return false;
4511
4512 if (E->isGLValue()) {
4513 if (!this->emitConstUint32(Indices[0], E))
4514 return false;
4515 return this->emitArrayElemPtrPop(PT_Uint32, E);
4516 }
4517 // Else, also load the value.
4518 return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
4519 }
4520
4521 // Create a local variable for the base.
4522 unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true);
4523 if (!this->visit(Base))
4524 return false;
4525 if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
4526 return false;
4527
4528 // Now the vector variable for the return value.
4529 if (!Initializing) {
4530 UnsignedOrNone ResultIndex = allocateLocal(E);
4531 if (!ResultIndex)
4532 return false;
4533 if (!this->emitGetPtrLocal(*ResultIndex, E))
4534 return false;
4535 }
4536
4537 assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
4538
4539 PrimType ElemT =
4541 uint32_t DstIndex = 0;
4542 for (uint32_t I : Indices) {
4543 if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
4544 return false;
4545 if (!this->emitArrayElemPop(ElemT, I, E))
4546 return false;
4547 if (!this->emitInitElem(ElemT, DstIndex, E))
4548 return false;
4549 ++DstIndex;
4550 }
4551
4552 // Leave the result pointer on the stack.
4553 assert(!DiscardResult);
4554 return true;
4555}
4556
4557template <class Emitter>
4559 const Expr *SubExpr = E->getSubExpr();
4561 return this->discard(SubExpr) && this->emitInvalid(E);
4562
4563 if (DiscardResult)
4564 return true;
4565
4566 assert(classifyPrim(E) == PT_Ptr);
4567 return this->emitDummyPtr(E, E);
4568}
4569
4570template <class Emitter>
4572 const CXXStdInitializerListExpr *E) {
4573 const Expr *SubExpr = E->getSubExpr();
4575 Ctx.getASTContext().getAsConstantArrayType(SubExpr->getType());
4576 const Record *R = getRecord(E->getType());
4577 assert(Initializing);
4578 assert(SubExpr->isGLValue());
4579
4580 if (!this->visit(SubExpr))
4581 return false;
4582 if (!this->emitConstUint8(0, E))
4583 return false;
4584 if (!this->emitArrayElemPtrPopUint8(E))
4585 return false;
4586 if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E))
4587 return false;
4588
4589 PrimType SecondFieldT = classifyPrim(R->getField(1u)->Decl->getType());
4590 if (isIntegerOrBoolType(SecondFieldT)) {
4591 if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E))
4592 return false;
4593 return this->emitInitField(SecondFieldT, R->getField(1u)->Offset, E);
4594 }
4595 assert(SecondFieldT == PT_Ptr);
4596
4597 if (!this->emitGetFieldPtr(R->getField(0u)->Offset, E))
4598 return false;
4599 if (!this->emitExpandPtr(E))
4600 return false;
4601 if (!this->emitConst(ArrayType->getSize(), PT_Uint64, E))
4602 return false;
4603 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
4604 return false;
4605 return this->emitInitFieldPtr(R->getField(1u)->Offset, E);
4606}
4607
4608template <class Emitter>
4610 LocalScope<Emitter> BS(this);
4611 StmtExprScope<Emitter> SS(this);
4612
4613 const CompoundStmt *CS = E->getSubStmt();
4614 const Stmt *Result = CS->body_back();
4615 for (const Stmt *S : CS->body()) {
4616 if (S != Result) {
4617 if (!this->visitStmt(S))
4618 return false;
4619 continue;
4620 }
4621
4622 assert(S == Result);
4623 if (const Expr *ResultExpr = dyn_cast<Expr>(S))
4624 return this->delegate(ResultExpr);
4625 return this->emitUnsupported(E);
4626 }
4627
4628 return BS.destroyLocals();
4629}
4630
4631template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
4632 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
4633 /*NewInitializing=*/false, /*ToLValue=*/false);
4634 return this->Visit(E);
4635}
4636
4637template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
4638 // We're basically doing:
4639 // OptionScope<Emitter> Scope(this, DicardResult, Initializing, ToLValue);
4640 // but that's unnecessary of course.
4641 return this->Visit(E);
4642}
4643
4645 if (const auto *PE = dyn_cast<ParenExpr>(E))
4646 return stripCheckedDerivedToBaseCasts(PE->getSubExpr());
4647
4648 if (const auto *CE = dyn_cast<CastExpr>(E);
4649 CE &&
4650 (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp))
4651 return stripCheckedDerivedToBaseCasts(CE->getSubExpr());
4652
4653 return E;
4654}
4655
4656static const Expr *stripDerivedToBaseCasts(const Expr *E) {
4657 if (const auto *PE = dyn_cast<ParenExpr>(E))
4658 return stripDerivedToBaseCasts(PE->getSubExpr());
4659
4660 if (const auto *CE = dyn_cast<CastExpr>(E);
4661 CE && (CE->getCastKind() == CK_DerivedToBase ||
4662 CE->getCastKind() == CK_UncheckedDerivedToBase ||
4663 CE->getCastKind() == CK_NoOp))
4664 return stripDerivedToBaseCasts(CE->getSubExpr());
4665
4666 return E;
4667}
4668
4669template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
4670 if (E->getType().isNull())
4671 return false;
4672
4673 if (E->getType()->isVoidType())
4674 return this->discard(E);
4675
4676 // Create local variable to hold the return value.
4677 if (!E->isGLValue() && !canClassify(E->getType())) {
4678 UnsignedOrNone LocalIndex = allocateLocal(
4680 if (!LocalIndex)
4681 return false;
4682
4683 if (!this->emitGetPtrLocal(*LocalIndex, E))
4684 return false;
4685 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
4686 return this->visitInitializer(E);
4687 }
4688
4689 // Otherwise,we have a primitive return value, produce the value directly
4690 // and push it on the stack.
4691 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4692 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
4693 return this->Visit(E);
4694}
4695
4696template <class Emitter>
4698 assert(!canClassify(E->getType()));
4699
4700 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4701 /*NewInitializing=*/true, /*ToLValue=*/false);
4702 return this->Visit(E) && this->emitFinishInit(E);
4703}
4704
4705template <class Emitter>
4707 assert(!canClassify(E->getType()));
4708
4709 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4710 /*NewInitializing=*/true, /*ToLValue=*/false);
4711 return this->Visit(E) && this->emitFinishInitPop(E);
4712}
4713
4714template <class Emitter> bool Compiler<Emitter>::visitAsLValue(const Expr *E) {
4715 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4716 /*NewInitializing=*/false, /*ToLValue=*/true);
4717 return this->Visit(E);
4718}
4719
4720template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
4721 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4722 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
4723
4724 OptPrimType T = classify(E->getType());
4725 if (!T) {
4726 // Convert complex values to bool.
4727 if (E->getType()->isAnyComplexType()) {
4728 if (!this->visit(E))
4729 return false;
4730 return this->emitComplexBoolCast(E);
4731 }
4732 return false;
4733 }
4734
4735 if (!this->visit(E))
4736 return false;
4737
4738 if (T == PT_Bool)
4739 return true;
4740
4741 // Convert pointers to bool.
4742 if (T == PT_Ptr)
4743 return this->emitIsNonNullPtr(E);
4744
4745 // Or Floats.
4746 if (T == PT_Float)
4747 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
4748
4749 // Or anything else we can.
4750 return this->emitCast(*T, PT_Bool, E);
4751}
4752
4753template <class Emitter>
4754bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
4755 const Expr *E) {
4756 if (const auto *AT = QT->getAs<AtomicType>())
4757 QT = AT->getValueType();
4758
4759 switch (T) {
4760 case PT_Bool:
4761 return this->emitZeroBool(E);
4762 case PT_Sint8:
4763 return this->emitZeroSint8(E);
4764 case PT_Uint8:
4765 return this->emitZeroUint8(E);
4766 case PT_Sint16:
4767 return this->emitZeroSint16(E);
4768 case PT_Uint16:
4769 return this->emitZeroUint16(E);
4770 case PT_Sint32:
4771 return this->emitZeroSint32(E);
4772 case PT_Uint32:
4773 return this->emitZeroUint32(E);
4774 case PT_Sint64:
4775 return this->emitZeroSint64(E);
4776 case PT_Uint64:
4777 return this->emitZeroUint64(E);
4778 case PT_IntAP:
4779 return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
4780 case PT_IntAPS:
4781 return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
4782 case PT_Ptr:
4783 return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
4784 nullptr, E);
4785 case PT_MemberPtr:
4786 return this->emitNullMemberPtr(0, nullptr, E);
4787 case PT_Float: {
4788 APFloat F = APFloat::getZero(Ctx.getFloatSemantics(QT));
4789 return this->emitFloat(F, E);
4790 }
4791 case PT_FixedPoint: {
4792 auto Sem = Ctx.getASTContext().getFixedPointSemantics(QT);
4793 return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
4794 }
4795 }
4796 llvm_unreachable("unknown primitive type");
4797}
4798
4799template <class Emitter>
4800bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
4801 const Expr *E) {
4802 assert(E);
4803 assert(R);
4804 // Fields
4805 for (const Record::Field &Field : R->fields()) {
4806 if (Field.isUnnamedBitField())
4807 continue;
4808
4809 const Descriptor *D = Field.Desc;
4810 if (D->isPrimitive()) {
4811 QualType QT = D->getType();
4812 PrimType T = D->getPrimType();
4813 if (!this->visitZeroInitializer(T, QT, E))
4814 return false;
4815 if (R->isUnion()) {
4816 if (!this->emitInitFieldActivate(T, Field.Offset, E))
4817 return false;
4818 break;
4819 }
4820 if (!this->emitInitField(T, Field.Offset, E))
4821 return false;
4822 continue;
4823 }
4824
4825 if (!this->emitGetPtrField(Field.Offset, E))
4826 return false;
4827
4828 if (D->isPrimitiveArray()) {
4829 QualType ET = D->getElemQualType();
4830 PrimType T = D->getPrimType();
4831 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
4832 if (!this->visitZeroInitializer(T, ET, E))
4833 return false;
4834 if (!this->emitInitElem(T, I, E))
4835 return false;
4836 }
4837 } else if (D->isCompositeArray()) {
4838 // Can't be a vector or complex field.
4839 if (!this->visitZeroArrayInitializer(D->getType(), E))
4840 return false;
4841 } else if (D->isRecord()) {
4842 if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
4843 return false;
4844 } else
4845 return false;
4846
4847 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
4848 // object's first non-static named data member is zero-initialized
4849 if (R->isUnion()) {
4850 if (!this->emitFinishInitActivatePop(E))
4851 return false;
4852 break;
4853 }
4854 if (!this->emitFinishInitPop(E))
4855 return false;
4856 }
4857
4858 for (const Record::Base &B : R->bases()) {
4859 if (!this->emitGetPtrBase(B.Offset, E))
4860 return false;
4861 if (!this->visitZeroRecordInitializer(B.R, E))
4862 return false;
4863 if (!this->emitFinishInitPop(E))
4864 return false;
4865 }
4866
4867 // FIXME: Virtual bases.
4868
4869 return true;
4870}
4871
4872template <class Emitter>
4873bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
4874 assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
4875 const ArrayType *AT = T->getAsArrayTypeUnsafe();
4876 QualType ElemType = AT->getElementType();
4877 size_t NumElems = cast<ConstantArrayType>(AT)->getZExtSize();
4878
4879 if (OptPrimType ElemT = classify(ElemType)) {
4880 for (size_t I = 0; I != NumElems; ++I) {
4881 if (!this->visitZeroInitializer(*ElemT, ElemType, E))
4882 return false;
4883 if (!this->emitInitElem(*ElemT, I, E))
4884 return false;
4885 }
4886 return true;
4887 }
4888 if (ElemType->isRecordType()) {
4889 const Record *R = getRecord(ElemType);
4890 if (!R)
4891 return false;
4892
4893 for (size_t I = 0; I != NumElems; ++I) {
4894 if (!this->emitConstUint32(I, E))
4895 return false;
4896 if (!this->emitArrayElemPtr(PT_Uint32, E))
4897 return false;
4898 if (!this->visitZeroRecordInitializer(R, E))
4899 return false;
4900 if (!this->emitPopPtr(E))
4901 return false;
4902 }
4903 return true;
4904 }
4905 if (ElemType->isArrayType()) {
4906 for (size_t I = 0; I != NumElems; ++I) {
4907 if (!this->emitConstUint32(I, E))
4908 return false;
4909 if (!this->emitArrayElemPtr(PT_Uint32, E))
4910 return false;
4911 if (!this->visitZeroArrayInitializer(ElemType, E))
4912 return false;
4913 if (!this->emitPopPtr(E))
4914 return false;
4915 }
4916 return true;
4917 }
4918
4919 return false;
4920}
4921
4922template <class Emitter>
4923bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS,
4924 const Expr *E) {
4925 if (!canClassify(E->getType()))
4926 return false;
4927
4928 if (!this->visit(RHS))
4929 return false;
4930 if (!this->visit(LHS))
4931 return false;
4932
4933 if (LHS->getType().isVolatileQualified())
4934 return this->emitInvalidStore(LHS->getType().getTypePtr(), E);
4935
4936 // We don't support assignments in C.
4937 if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(E))
4938 return false;
4939
4940 PrimType RHT = classifyPrim(RHS);
4941 bool Activates = refersToUnion(LHS);
4942 bool BitField = LHS->refersToBitField();
4943
4944 if (!this->emitFlip(PT_Ptr, RHT, E))
4945 return false;
4946
4947 if (DiscardResult) {
4948 if (BitField && Activates)
4949 return this->emitStoreBitFieldActivatePop(RHT, E);
4950 if (BitField)
4951 return this->emitStoreBitFieldPop(RHT, E);
4952 if (Activates)
4953 return this->emitStoreActivatePop(RHT, E);
4954 // Otherwise, regular non-activating store.
4955 return this->emitStorePop(RHT, E);
4956 }
4957
4958 auto maybeLoad = [&](bool Result) -> bool {
4959 if (!Result)
4960 return false;
4961 // Assignments aren't necessarily lvalues in C.
4962 // Load from them in that case.
4963 if (!E->isLValue())
4964 return this->emitLoadPop(RHT, E);
4965 return true;
4966 };
4967
4968 if (BitField && Activates)
4969 return maybeLoad(this->emitStoreBitFieldActivate(RHT, E));
4970 if (BitField)
4971 return maybeLoad(this->emitStoreBitField(RHT, E));
4972 if (Activates)
4973 return maybeLoad(this->emitStoreActivate(RHT, E));
4974 // Otherwise, regular non-activating store.
4975 return maybeLoad(this->emitStore(RHT, E));
4976}
4977
4978template <class Emitter>
4979template <typename T>
4980bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
4981 switch (Ty) {
4982 case PT_Sint8:
4983 return this->emitConstSint8(Value, E);
4984 case PT_Uint8:
4985 return this->emitConstUint8(Value, E);
4986 case PT_Sint16:
4987 return this->emitConstSint16(Value, E);
4988 case PT_Uint16:
4989 return this->emitConstUint16(Value, E);
4990 case PT_Sint32:
4991 return this->emitConstSint32(Value, E);
4992 case PT_Uint32:
4993 return this->emitConstUint32(Value, E);
4994 case PT_Sint64:
4995 return this->emitConstSint64(Value, E);
4996 case PT_Uint64:
4997 return this->emitConstUint64(Value, E);
4998 case PT_Bool:
4999 return this->emitConstBool(Value, E);
5000 case PT_Ptr:
5001 case PT_MemberPtr:
5002 case PT_Float:
5003 case PT_IntAP:
5004 case PT_IntAPS:
5005 case PT_FixedPoint:
5006 llvm_unreachable("Invalid integral type");
5007 break;
5008 }
5009 llvm_unreachable("unknown primitive type");
5010}
5011
5012template <class Emitter>
5013template <typename T>
5014bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
5015 return this->emitConst(Value, classifyPrim(E->getType()), E);
5016}
5017
5018template <class Emitter>
5019bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
5020 const Expr *E) {
5021 if (Ty == PT_IntAPS)
5022 return this->emitConstIntAPS(Value, E);
5023 if (Ty == PT_IntAP)
5024 return this->emitConstIntAP(Value, E);
5025
5026 if (Value.isSigned())
5027 return this->emitConst(Value.getSExtValue(), Ty, E);
5028 return this->emitConst(Value.getZExtValue(), Ty, E);
5029}
5030
5031template <class Emitter>
5032bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty,
5033 const Expr *E) {
5034 if (Ty == PT_IntAPS)
5035 return this->emitConstIntAPS(Value, E);
5036 if (Ty == PT_IntAP)
5037 return this->emitConstIntAP(Value, E);
5038
5039 if (isSignedType(Ty))
5040 return this->emitConst(Value.getSExtValue(), Ty, E);
5041 return this->emitConst(Value.getZExtValue(), Ty, E);
5042}
5043
5044template <class Emitter>
5045bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
5046 return this->emitConst(Value, classifyPrim(E->getType()), E);
5047}
5048
5049template <class Emitter>
5051 bool IsConst,
5052 bool IsVolatile,
5053 ScopeKind SC) {
5054 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
5055 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
5056 // or isa<MaterializeTemporaryExpr>().
5057 Descriptor *D = P.createDescriptor(Src, Ty, nullptr, Descriptor::InlineDescMD,
5058 IsConst, isa<const Expr *>(Src),
5059 /*IsMutable=*/false, IsVolatile);
5061 Scope::Local Local = this->createLocal(D);
5062 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
5063 Locals.insert({VD, Local});
5064 VarScope->addForScopeKind(Local, SC);
5065 return Local.Offset;
5066}
5067
5068template <class Emitter>
5070 ScopeKind SC) {
5071 const ValueDecl *Key = nullptr;
5072 const Expr *Init = nullptr;
5073 bool IsTemporary = false;
5074 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
5075 Key = VD;
5076
5077 if (const auto *VarD = dyn_cast<VarDecl>(VD))
5078 Init = VarD->getInit();
5079 }
5080 if (auto *E = Src.dyn_cast<const Expr *>()) {
5081 IsTemporary = true;
5082 if (Ty.isNull())
5083 Ty = E->getType();
5084 }
5085
5086 Descriptor *D = P.createDescriptor(
5088 IsTemporary, /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(),
5089 Init);
5090 if (!D)
5091 return std::nullopt;
5093
5094 Scope::Local Local = this->createLocal(D);
5095 if (Key)
5096 Locals.insert({Key, Local});
5097 VarScope->addForScopeKind(Local, SC);
5098 return Local.Offset;
5099}
5100
5101template <class Emitter>
5103 QualType Ty = E->getType();
5104 assert(!Ty->isRecordType());
5105
5106 Descriptor *D = P.createDescriptor(
5108 /*IsTemporary=*/true);
5109
5110 if (!D)
5111 return std::nullopt;
5112
5113 Scope::Local Local = this->createLocal(D);
5115 assert(S);
5116 // Attach to topmost scope.
5117 while (S->getParent())
5118 S = S->getParent();
5119 assert(S && !S->getParent());
5120 S->addLocal(Local);
5121 return Local.Offset;
5122}
5123
5124template <class Emitter>
5126 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
5127 return PT->getPointeeType()->getAsCanonical<RecordType>();
5128 return Ty->getAsCanonical<RecordType>();
5129}
5130
5131template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
5132 if (const auto *RecordTy = getRecordTy(Ty))
5133 return getRecord(RecordTy->getDecl()->getDefinitionOrSelf());
5134 return nullptr;
5135}
5136
5137template <class Emitter>
5139 return P.getOrCreateRecord(RD);
5140}
5141
5142template <class Emitter>
5144 return Ctx.getOrCreateFunction(FD);
5145}
5146
5147template <class Emitter>
5148bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
5150
5151 auto maybeDestroyLocals = [&]() -> bool {
5152 if (DestroyToplevelScope)
5153 return RootScope.destroyLocals() && this->emitCheckAllocations(E);
5154 return this->emitCheckAllocations(E);
5155 };
5156
5157 // Void expressions.
5158 if (E->getType()->isVoidType()) {
5159 if (!visit(E))
5160 return false;
5161 return this->emitRetVoid(E) && maybeDestroyLocals();
5162 }
5163
5164 // Expressions with a primitive return type.
5165 if (OptPrimType T = classify(E)) {
5166 if (!visit(E))
5167 return false;
5168
5169 return this->emitRet(*T, E) && maybeDestroyLocals();
5170 }
5171
5172 // Expressions with a composite return type.
5173 // For us, that means everything we don't
5174 // have a PrimType for.
5175 if (UnsignedOrNone LocalOffset = this->allocateLocal(E)) {
5176 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalOffset));
5177 if (!this->emitGetPtrLocal(*LocalOffset, E))
5178 return false;
5179
5180 if (!visitInitializer(E))
5181 return false;
5182 // We are destroying the locals AFTER the Ret op.
5183 // The Ret op needs to copy the (alive) values, but the
5184 // destructors may still turn the entire expression invalid.
5185 return this->emitRetValue(E) && maybeDestroyLocals();
5186 }
5187
5188 return maybeDestroyLocals() && false;
5189}
5190
5191template <class Emitter>
5193 bool DestroyToplevelScope) {
5194 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5195 /*NewInitializing=*/false, /*ToLValue=*/true);
5196
5197 return this->visitExpr(E, DestroyToplevelScope);
5198}
5199
5200template <class Emitter>
5202
5203 auto R = this->visitVarDecl(VD, VD->getInit(), /*Toplevel=*/true);
5204
5205 if (R.notCreated())
5206 return R;
5207
5208 if (R)
5209 return true;
5210
5211 if (!R && Context::shouldBeGloballyIndexed(VD)) {
5212 if (auto GlobalIndex = P.getGlobal(VD)) {
5213 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5214 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5215
5217 GlobalBlock->invokeDtor();
5218 }
5219 }
5220
5221 return R;
5222}
5223
5224/// Toplevel visitDeclAndReturn().
5225/// We get here from evaluateAsInitializer().
5226/// We need to evaluate the initializer and return its value.
5227template <class Emitter>
5229 bool ConstantContext) {
5230 // We only create variables if we're evaluating in a constant context.
5231 // Otherwise, just evaluate the initializer and return it.
5232 if (!ConstantContext) {
5233 DeclScope<Emitter> LS(this, VD);
5234 if (!this->visit(Init))
5235 return false;
5236 return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
5237 LS.destroyLocals() && this->emitCheckAllocations(VD);
5238 }
5239
5240 LocalScope<Emitter> VDScope(this);
5241 if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true))
5242 return false;
5243
5244 OptPrimType VarT = classify(VD->getType());
5246 auto GlobalIndex = P.getGlobal(VD);
5247 assert(GlobalIndex); // visitVarDecl() didn't return false.
5248 if (VarT) {
5249 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
5250 return false;
5251 } else {
5252 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
5253 return false;
5254 }
5255 } else {
5256 auto Local = Locals.find(VD);
5257 assert(Local != Locals.end()); // Same here.
5258 if (VarT) {
5259 if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
5260 return false;
5261 } else {
5262 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
5263 return false;
5264 }
5265 }
5266
5267 // Return the value.
5268 if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
5269 // If the Ret above failed and this is a global variable, mark it as
5270 // uninitialized, even everything else succeeded.
5272 auto GlobalIndex = P.getGlobal(VD);
5273 assert(GlobalIndex);
5274 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5275 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5276
5278 GlobalBlock->invokeDtor();
5279 }
5280 return false;
5281 }
5282
5283 return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
5284}
5285
5286template <class Emitter>
5288 const Expr *Init,
5289 bool Toplevel) {
5290 QualType VarTy = VD->getType();
5291 // We don't know what to do with these, so just return false.
5292 if (VarTy.isNull())
5293 return false;
5294
5295 // This case is EvalEmitter-only. If we won't create any instructions for the
5296 // initializer anyway, don't bother creating the variable in the first place.
5297 if (!this->isActive())
5299
5300 OptPrimType VarT = classify(VD->getType());
5301
5302 if (Init && Init->isValueDependent())
5303 return false;
5304
5306 auto checkDecl = [&]() -> bool {
5307 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
5308 return !NeedsOp || this->emitCheckDecl(VD, VD);
5309 };
5310
5312 UnsignedOrNone GlobalIndex = P.getGlobal(VD);
5313 if (GlobalIndex) {
5314 // The global was previously created but the initializer failed.
5315 if (!P.getGlobal(*GlobalIndex)->isInitialized())
5316 return false;
5317 // We've already seen and initialized this global.
5318 if (P.isGlobalInitialized(*GlobalIndex))
5319 return checkDecl();
5320 // The previous attempt at initialization might've been unsuccessful,
5321 // so let's try this one.
5322 } else if ((GlobalIndex =
5323 P.createGlobal(VD, Init, VariablesAreConstexprUnknown))) {
5324 } else {
5325 return false;
5326 }
5327 if (!Init)
5328 return true;
5329
5330 if (!checkDecl())
5331 return false;
5332
5333 if (VarT) {
5334 if (!this->visit(Init))
5335 return false;
5336
5337 return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
5338 }
5339
5340 if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
5341 return false;
5342
5343 if (!this->emitStartInit(Init))
5344 return false;
5345
5346 if (!visitInitializer(Init))
5347 return false;
5348
5349 if (!this->emitEndInit(Init))
5350 return false;
5351
5352 return this->emitFinishInitGlobal(Init);
5353 }
5354 // Local variables.
5356
5357 if (VarT) {
5358 unsigned Offset = this->allocateLocalPrimitive(
5359 VD, *VarT, VarTy.isConstQualified(), VarTy.isVolatileQualified(),
5361
5362 if (!Init || Init->getType()->isVoidType())
5363 return true;
5364
5365 // If this is a toplevel declaration, create a scope for the
5366 // initializer.
5367 if (Toplevel) {
5369 if (!this->visit(Init))
5370 return false;
5371 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
5372 }
5373 if (!this->visit(Init))
5374 return false;
5375
5376 if (VarTy->isReferenceType()) {
5377 // [C++26][decl.ref]
5378 // The object designated by such a glvalue can be outside its lifetime
5379 // Because a null pointer value or a pointer past the end of an object
5380 // does not point to an object, a reference in a well-defined program
5381 // cannot refer to such things;
5382 assert(classifyPrim(VarTy) == PT_Ptr);
5383 if (!this->emitCheckRefInit(Init))
5384 return false;
5385 }
5386
5387 return this->emitSetLocal(*VarT, Offset, VD);
5388 }
5389 // Local composite variables.
5390 if (UnsignedOrNone Offset =
5391 this->allocateLocal(VD, VarTy, ScopeKind::Block)) {
5392 if (!Init)
5393 return true;
5394
5395 if (!this->emitGetPtrLocal(*Offset, Init))
5396 return false;
5397
5398 return visitInitializerPop(Init);
5399 }
5400 return false;
5401}
5402
5403template <class Emitter>
5405 assert(!canClassify(VD->getType()));
5406
5408 // Create a local variable to use as the instance.
5409 QualType Ty = VD->getType();
5410 Descriptor *D = P.createDescriptor(
5411 VD, Ty.getTypePtr(), Descriptor::InlineDescMD, /*IsConst=*/false,
5412 /*IsTemporary=*/false, /*IsMutable=*/false,
5413 /*IsVolatile=*/Ty.isVolatileQualified(), nullptr);
5414 if (!D)
5415 return false;
5416
5417 Scope::Local Local = this->createLocal(D);
5418 Locals.insert({VD, Local});
5419 VarScope->addForScopeKind(Local, ScopeKind::Block);
5420
5421 if (!this->emitGetPtrLocal(Local.Offset, VD))
5422 return false;
5423 if (!this->visitAPValueInitializer(Value, nullptr, Ty))
5424 return false;
5425 return this->emitDestructionPop(D, VD);
5426}
5427
5428template <class Emitter>
5430 const Expr *E) {
5431 assert(!DiscardResult);
5432 if (Val.isInt())
5433 return this->emitConst(Val.getInt(), ValType, E);
5434 if (Val.isFloat()) {
5435 APFloat F = Val.getFloat();
5436 return this->emitFloat(F, E);
5437 }
5438
5439 if (Val.isMemberPointer()) {
5440 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl()) {
5441 if (!this->emitGetMemberPtr(MemberDecl, E))
5442 return false;
5443
5444 bool IsDerived = Val.isMemberPointerToDerivedMember();
5445 // Apply the member pointer path.
5446 for (const CXXRecordDecl *PathEntry : Val.getMemberPointerPath()) {
5447 if (!this->emitCopyMemberPtrPath(PathEntry, IsDerived, E))
5448 return false;
5449 }
5450
5451 return true;
5452 }
5453 return this->emitNullMemberPtr(0, nullptr, E);
5454 }
5455
5456 if (Val.isLValue()) {
5457 if (Val.isNullPointer())
5458 return this->emitNull(ValType, 0, nullptr, E);
5459
5462
5463 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
5464 return this->visit(BaseExpr);
5465 if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) {
5466 if (!this->visitDeclRef(VD, E))
5467 return false;
5468
5469 QualType EntryType = VD->getType();
5470 for (auto &Entry : Path) {
5471 if (EntryType->isArrayType()) {
5472 uint64_t Index = Entry.getAsArrayIndex();
5473 QualType ElemType =
5474 EntryType->getAsArrayTypeUnsafe()->getElementType();
5475 if (!this->emitConst(Index, PT_Uint64, E))
5476 return false;
5477 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
5478 return false;
5479 EntryType = ElemType;
5480 } else {
5481 assert(EntryType->isRecordType());
5482 const Record *EntryRecord = getRecord(EntryType);
5483 if (!EntryRecord) {
5484 assert(false);
5485
5486 return false;
5487 }
5488
5489 const Decl *BaseOrMember = Entry.getAsBaseOrMember().getPointer();
5490 if (const auto *FD = dyn_cast<FieldDecl>(BaseOrMember)) {
5491 unsigned EntryOffset = EntryRecord->getField(FD)->Offset;
5492 if (!this->emitGetPtrFieldPop(EntryOffset, E))
5493 return false;
5494 EntryType = FD->getType();
5495 } else {
5496 const auto *Base = cast<CXXRecordDecl>(BaseOrMember);
5497 unsigned BaseOffset = EntryRecord->getBase(Base)->Offset;
5498 if (!this->emitGetPtrBasePop(BaseOffset, /*NullOK=*/false, E))
5499 return false;
5500 EntryType = Ctx.getASTContext().getCanonicalTagType(Base);
5501 }
5502 }
5503 }
5504
5505 return true;
5506 }
5507 }
5508
5509 return false;
5510}
5511
5512template <class Emitter>
5514 const Expr *E, QualType T) {
5515 if (Val.isStruct()) {
5516 const Record *R = this->getRecord(T);
5517 assert(R);
5518 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
5519 const APValue &F = Val.getStructField(I);
5520 const Record::Field *RF = R->getField(I);
5521 QualType FieldType = RF->Decl->getType();
5522
5523 // Fields.
5524 if (OptPrimType PT = classify(FieldType)) {
5525 if (!this->visitAPValue(F, *PT, E))
5526 return false;
5527 if (!this->emitInitField(*PT, RF->Offset, E))
5528 return false;
5529 } else {
5530 if (!this->emitGetPtrField(RF->Offset, E))
5531 return false;
5532 if (!this->visitAPValueInitializer(F, E, FieldType))
5533 return false;
5534 if (!this->emitFinishInitPop(E))
5535 return false;
5536 }
5537 }
5538
5539 // Bases.
5540 for (unsigned I = 0, N = Val.getStructNumBases(); I != N; ++I) {
5541 const APValue &B = Val.getStructBase(I);
5542 const Record::Base *RB = R->getBase(I);
5543 QualType BaseType = Ctx.getASTContext().getCanonicalTagType(RB->Decl);
5544
5545 if (!this->emitGetPtrBase(RB->Offset, E))
5546 return false;
5547 if (!this->visitAPValueInitializer(B, E, BaseType))
5548 return false;
5549 if (!this->emitFinishInitPop(E))
5550 return false;
5551 }
5552
5553 return true;
5554 }
5555 if (Val.isUnion()) {
5556 const FieldDecl *UnionField = Val.getUnionField();
5557 if (!UnionField)
5558 return true;
5559 const Record *R = this->getRecord(T);
5560 assert(R);
5561 const APValue &F = Val.getUnionValue();
5562 const Record::Field *RF = R->getField(UnionField);
5563 QualType FieldType = RF->Decl->getType();
5564
5565 if (OptPrimType PT = classify(FieldType)) {
5566 if (!this->visitAPValue(F, *PT, E))
5567 return false;
5568 if (RF->isBitField())
5569 return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(),
5570 E);
5571 return this->emitInitFieldActivate(*PT, RF->Offset, E);
5572 }
5573
5574 if (!this->emitGetPtrField(RF->Offset, E))
5575 return false;
5576 if (!this->emitActivate(E))
5577 return false;
5578 if (!this->visitAPValueInitializer(F, E, FieldType))
5579 return false;
5580 return this->emitPopPtr(E);
5581 }
5582 if (Val.isArray()) {
5583 unsigned InitializedElems = Val.getArrayInitializedElts();
5584 const auto *ArrType = T->getAsArrayTypeUnsafe();
5585 QualType ElemType = ArrType->getElementType();
5586 OptPrimType ElemT = classify(ElemType);
5587
5588 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
5589 const APValue &Elem = A >= InitializedElems
5590 ? Val.getArrayFiller()
5591 : Val.getArrayInitializedElt(A);
5592
5593 if (ElemT) {
5594 if (!this->visitAPValue(Elem, *ElemT, E))
5595 return false;
5596 if (!this->emitInitElem(*ElemT, A, E))
5597 return false;
5598 } else {
5599 if (!this->emitConstUint32(A, E))
5600 return false;
5601 if (!this->emitArrayElemPtrUint32(E))
5602 return false;
5603 if (!this->visitAPValueInitializer(Elem, E, ElemType))
5604 return false;
5605 if (!this->emitPopPtr(E))
5606 return false;
5607 }
5608 }
5609 return true;
5610 }
5611 // TODO: Other types.
5612
5613 return false;
5614}
5615
5616template <class Emitter>
5618 unsigned BuiltinID) {
5619 if (BuiltinID == Builtin::BI__builtin_constant_p) {
5620 // Void argument is always invalid and harder to handle later.
5621 if (E->getArg(0)->getType()->isVoidType()) {
5622 if (DiscardResult)
5623 return true;
5624 return this->emitConst(0, E);
5625 }
5626
5627 if (!this->emitStartSpeculation(E))
5628 return false;
5629 LabelTy EndLabel = this->getLabel();
5630 if (!this->speculate(E, EndLabel))
5631 return false;
5632 if (!this->emitEndSpeculation(E))
5633 return false;
5634 this->fallthrough(EndLabel);
5635 if (DiscardResult)
5636 return this->emitPop(classifyPrim(E), E);
5637 return true;
5638 }
5639
5640 // For these, we're expected to ultimately return an APValue pointing
5641 // to the CallExpr. This is needed to get the correct codegen.
5642 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
5643 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
5644 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
5645 BuiltinID == Builtin::BI__builtin_function_start) {
5646 if (DiscardResult)
5647 return true;
5648 return this->emitDummyPtr(E, E);
5649 }
5650
5652 OptPrimType ReturnT = classify(E);
5653
5654 // Non-primitive return type. Prepare storage.
5655 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
5656 UnsignedOrNone LocalIndex = allocateLocal(E);
5657 if (!LocalIndex)
5658 return false;
5659 if (!this->emitGetPtrLocal(*LocalIndex, E))
5660 return false;
5661 }
5662
5663 // Prepare function arguments including special cases.
5664 switch (BuiltinID) {
5665 case Builtin::BI__builtin_object_size:
5666 case Builtin::BI__builtin_dynamic_object_size: {
5667 assert(E->getNumArgs() == 2);
5668 const Expr *Arg0 = E->getArg(0);
5669 if (Arg0->isGLValue()) {
5670 if (!this->visit(Arg0))
5671 return false;
5672
5673 } else {
5674 if (!this->visitAsLValue(Arg0))
5675 return false;
5676 }
5677 if (!this->visit(E->getArg(1)))
5678 return false;
5679
5680 } break;
5681 case Builtin::BI__assume:
5682 case Builtin::BI__builtin_assume:
5683 // Argument is not evaluated.
5684 break;
5685 case Builtin::BI__atomic_is_lock_free:
5686 case Builtin::BI__atomic_always_lock_free: {
5687 assert(E->getNumArgs() == 2);
5688 if (!this->visit(E->getArg(0)))
5689 return false;
5690 if (!this->visitAsLValue(E->getArg(1)))
5691 return false;
5692 } break;
5693
5694 default:
5695 if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
5696 // Put arguments on the stack.
5697 for (const auto *Arg : E->arguments()) {
5698 if (!this->visit(Arg))
5699 return false;
5700 }
5701 }
5702 }
5703
5704 if (!this->emitCallBI(E, BuiltinID, E))
5705 return false;
5706
5707 if (DiscardResult && !ReturnType->isVoidType())
5708 return this->emitPop(ReturnT.value_or(PT_Ptr), E);
5709
5710 return true;
5711}
5712
5713template <class Emitter>
5715 if (E->containsErrors())
5716 return false;
5717 const FunctionDecl *FuncDecl = E->getDirectCallee();
5718
5719 if (FuncDecl) {
5720 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
5721 return VisitBuiltinCallExpr(E, BuiltinID);
5722
5723 // Calls to replaceable operator new/operator delete.
5725 if (FuncDecl->getDeclName().isAnyOperatorNew())
5726 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
5727 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete ||
5728 FuncDecl->getDeclName().getCXXOverloadedOperator() ==
5729 OO_Array_Delete);
5730 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
5731 }
5732
5733 // Explicit calls to trivial destructors
5734 if (const auto *DD = dyn_cast<CXXDestructorDecl>(FuncDecl);
5735 DD && DD->isTrivial()) {
5736 const auto *MemberCall = cast<CXXMemberCallExpr>(E);
5737 if (!this->visit(MemberCall->getImplicitObjectArgument()))
5738 return false;
5739 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
5740 this->emitPopPtr(E);
5741 }
5742 }
5743
5744 LocalScope<Emitter> CallScope(this, ScopeKind::Call);
5745
5746 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
5748 bool HasRVO = !ReturnType->isVoidType() && !T;
5749
5750 if (HasRVO) {
5751 if (DiscardResult) {
5752 // If we need to discard the return value but the function returns its
5753 // value via an RVO pointer, we need to create one such pointer just
5754 // for this call.
5755 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5756 if (!this->emitGetPtrLocal(*LocalIndex, E))
5757 return false;
5758 }
5759 } else {
5760 // We need the result. Prepare a pointer to return or
5761 // dup the current one.
5762 if (!Initializing) {
5763 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5764 if (!this->emitGetPtrLocal(*LocalIndex, E))
5765 return false;
5766 }
5767 }
5768 if (!this->emitDupPtr(E))
5769 return false;
5770 }
5771 }
5772
5774
5775 bool IsAssignmentOperatorCall = false;
5776 bool ActivateLHS = false;
5777 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
5778 OCE && OCE->isAssignmentOp()) {
5779 // Just like with regular assignments, we need to special-case assignment
5780 // operators here and evaluate the RHS (the second arg) before the LHS (the
5781 // first arg). We fix this by using a Flip op later.
5782 assert(Args.size() == 2);
5783 const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl();
5784 ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor();
5785 IsAssignmentOperatorCall = true;
5786 std::reverse(Args.begin(), Args.end());
5787 }
5788 // Calling a static operator will still
5789 // pass the instance, but we don't need it.
5790 // Discard it here.
5791 if (isa<CXXOperatorCallExpr>(E)) {
5792 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
5793 MD && MD->isStatic()) {
5794 if (!this->discard(E->getArg(0)))
5795 return false;
5796 // Drop first arg.
5797 Args.erase(Args.begin());
5798 }
5799 }
5800
5801 bool Devirtualized = false;
5802 UnsignedOrNone CalleeOffset = std::nullopt;
5803 // Add the (optional, implicit) This pointer.
5804 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
5805 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
5806 // If we end up creating a CallPtr op for this, we need the base of the
5807 // member pointer as the instance pointer, and later extract the function
5808 // decl as the function pointer.
5809 const Expr *Callee = E->getCallee();
5810 CalleeOffset =
5811 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
5812 if (!this->visit(Callee))
5813 return false;
5814 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
5815 return false;
5816 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5817 return false;
5818 if (!this->emitGetMemberPtrBase(E))
5819 return false;
5820 } else {
5821 const auto *InstancePtr = MC->getImplicitObjectArgument();
5822 if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
5823 isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
5824 const auto *Stripped = stripCheckedDerivedToBaseCasts(InstancePtr);
5825 if (isa<CXXThisExpr>(Stripped)) {
5826 FuncDecl =
5827 cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
5828 Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5829 Devirtualized = true;
5830 if (!this->visit(Stripped))
5831 return false;
5832 } else {
5833 if (!this->visit(InstancePtr))
5834 return false;
5835 }
5836 } else {
5837 if (!this->visit(InstancePtr))
5838 return false;
5839 }
5840 }
5841 } else if (const auto *PD =
5842 dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
5843 if (!this->emitCheckPseudoDtor(E))
5844 return false;
5845 const Expr *Base = PD->getBase();
5846 // E.g. `using T = int; 0.~T();`.
5847 if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr)
5848 return this->discard(Base);
5849 if (!this->visit(Base))
5850 return false;
5851 return this->emitEndLifetimePop(E);
5852 } else if (!FuncDecl) {
5853 const Expr *Callee = E->getCallee();
5854 CalleeOffset =
5855 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
5856 if (!this->visit(Callee))
5857 return false;
5858 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
5859 return false;
5860 }
5861
5862 if (!this->visitCallArgs(Args, FuncDecl, ActivateLHS,
5864 return false;
5865
5866 // Undo the argument reversal we did earlier.
5867 if (IsAssignmentOperatorCall) {
5868 assert(Args.size() == 2);
5869 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
5870 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
5871 if (!this->emitFlip(Arg2T, Arg1T, E))
5872 return false;
5873 }
5874
5875 if (FuncDecl) {
5876 const Function *Func = getFunction(FuncDecl);
5877 if (!Func)
5878 return false;
5879
5880 // In error cases, the function may be called with fewer arguments than
5881 // parameters.
5882 if (E->getNumArgs() < Func->getNumWrittenParams())
5883 return false;
5884
5885 assert(HasRVO == Func->hasRVO());
5886
5887 bool HasQualifier = false;
5888 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
5889 HasQualifier = ME->hasQualifier();
5890
5891 bool IsVirtual = false;
5892 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
5893 IsVirtual = !Devirtualized && MD->isVirtual();
5894
5895 // In any case call the function. The return value will end up on the stack
5896 // and if the function has RVO, we already have the pointer on the stack to
5897 // write the result into.
5898 if (IsVirtual && !HasQualifier) {
5899 uint32_t VarArgSize = 0;
5900 unsigned NumParams =
5901 Func->getNumWrittenParams() +
5902 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisParam());
5903 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5904 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5905
5906 if (!this->emitCallVirt(Func, VarArgSize, E))
5907 return false;
5908 } else if (Func->isVariadic()) {
5909 uint32_t VarArgSize = 0;
5910 unsigned NumParams =
5911 Func->getNumWrittenParams() +
5912 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisParam());
5913 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5914 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5915 if (!this->emitCallVar(Func, VarArgSize, E))
5916 return false;
5917 } else {
5918 if (!this->emitCall(Func, 0, E))
5919 return false;
5920 }
5921 } else {
5922 // Indirect call. Visit the callee, which will leave a FunctionPointer on
5923 // the stack. Cleanup of the returned value if necessary will be done after
5924 // the function call completed.
5925
5926 // Sum the size of all args from the call expr.
5927 uint32_t ArgSize = 0;
5928 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
5929 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5930
5931 // Get the callee, either from a member pointer or function pointer saved in
5932 // CalleeOffset.
5933 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
5934 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5935 return false;
5936 if (!this->emitGetMemberPtrDecl(E))
5937 return false;
5938 } else {
5939 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
5940 return false;
5941 }
5942 if (!this->emitCallPtr(ArgSize, E, E))
5943 return false;
5944 }
5945
5946 // Cleanup for discarded return values.
5947 if (DiscardResult && !ReturnType->isVoidType() && T)
5948 return this->emitPop(*T, E) && CallScope.destroyLocals();
5949
5950 return CallScope.destroyLocals();
5951}
5952
5953template <class Emitter>
5955 SourceLocScope<Emitter> SLS(this, E);
5956
5957 return this->delegate(E->getExpr());
5958}
5959
5960template <class Emitter>
5962 SourceLocScope<Emitter> SLS(this, E);
5963
5964 return this->delegate(E->getExpr());
5965}
5966
5967template <class Emitter>
5969 if (DiscardResult)
5970 return true;
5971
5972 return this->emitConstBool(E->getValue(), E);
5973}
5974
5975template <class Emitter>
5977 const CXXNullPtrLiteralExpr *E) {
5978 if (DiscardResult)
5979 return true;
5980
5981 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
5982 return this->emitNullPtr(Val, nullptr, E);
5983}
5984
5985template <class Emitter>
5987 if (DiscardResult)
5988 return true;
5989
5990 assert(E->getType()->isIntegerType());
5991
5992 PrimType T = classifyPrim(E->getType());
5993 return this->emitZero(T, E);
5994}
5995
5996template <class Emitter>
5998 if (DiscardResult)
5999 return true;
6000
6001 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
6002 if (this->LambdaThisCapture.Offset > 0) {
6003 if (this->LambdaThisCapture.IsPtr)
6004 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
6005 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
6006 }
6007 }
6008
6009 // In some circumstances, the 'this' pointer does not actually refer to the
6010 // instance pointer of the current function frame, but e.g. to the declaration
6011 // currently being initialized. Here we emit the necessary instruction(s) for
6012 // this scenario.
6013 if (!InitStackActive || InitStack.empty())
6014 return this->emitThis(E);
6015
6016 // If our init stack is, for example:
6017 // 0 Stack: 3 (decl)
6018 // 1 Stack: 6 (init list)
6019 // 2 Stack: 1 (field)
6020 // 3 Stack: 6 (init list)
6021 // 4 Stack: 1 (field)
6022 //
6023 // We want to find the LAST element in it that's an init list,
6024 // which is marked with the K_InitList marker. The index right
6025 // before that points to an init list. We need to find the
6026 // elements before the K_InitList element that point to a base
6027 // (e.g. a decl or This), optionally followed by field, elem, etc.
6028 // In the example above, we want to emit elements [0..2].
6029 unsigned StartIndex = 0;
6030 unsigned EndIndex = 0;
6031 // Find the init list.
6032 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
6033 if (InitStack[StartIndex].Kind == InitLink::K_DIE) {
6034 EndIndex = StartIndex;
6035 --StartIndex;
6036 break;
6037 }
6038 }
6039
6040 // Walk backwards to find the base.
6041 for (; StartIndex > 0; --StartIndex) {
6042 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
6043 continue;
6044
6045 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
6046 InitStack[StartIndex].Kind != InitLink::K_Elem &&
6047 InitStack[StartIndex].Kind != InitLink::K_DIE)
6048 break;
6049 }
6050
6051 if (StartIndex == 0 && EndIndex == 0)
6052 EndIndex = InitStack.size() - 1;
6053
6054 assert(StartIndex < EndIndex);
6055
6056 // Emit the instructions.
6057 for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
6058 if (InitStack[I].Kind == InitLink::K_InitList ||
6059 InitStack[I].Kind == InitLink::K_DIE)
6060 continue;
6061 if (!InitStack[I].template emit<Emitter>(this, E))
6062 return false;
6063 }
6064 return true;
6065}
6066
6067template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
6068 switch (S->getStmtClass()) {
6069 case Stmt::CompoundStmtClass:
6071 case Stmt::DeclStmtClass:
6072 return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
6073 case Stmt::ReturnStmtClass:
6075 case Stmt::IfStmtClass:
6076 return visitIfStmt(cast<IfStmt>(S));
6077 case Stmt::WhileStmtClass:
6079 case Stmt::DoStmtClass:
6080 return visitDoStmt(cast<DoStmt>(S));
6081 case Stmt::ForStmtClass:
6082 return visitForStmt(cast<ForStmt>(S));
6083 case Stmt::CXXForRangeStmtClass:
6085 case Stmt::BreakStmtClass:
6087 case Stmt::ContinueStmtClass:
6089 case Stmt::SwitchStmtClass:
6091 case Stmt::CaseStmtClass:
6092 return visitCaseStmt(cast<CaseStmt>(S));
6093 case Stmt::DefaultStmtClass:
6095 case Stmt::AttributedStmtClass:
6097 case Stmt::CXXTryStmtClass:
6099 case Stmt::NullStmtClass:
6100 return true;
6101 // Always invalid statements.
6102 case Stmt::GCCAsmStmtClass:
6103 case Stmt::MSAsmStmtClass:
6104 case Stmt::GotoStmtClass:
6105 return this->emitInvalid(S);
6106 case Stmt::LabelStmtClass:
6107 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
6108 default: {
6109 if (const auto *E = dyn_cast<Expr>(S))
6110 return this->discard(E);
6111 return false;
6112 }
6113 }
6114}
6115
6116template <class Emitter>
6119 for (const auto *InnerStmt : S->body())
6120 if (!visitStmt(InnerStmt))
6121 return false;
6122 return Scope.destroyLocals();
6123}
6124
6125template <class Emitter>
6126bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
6127 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
6128 for (auto *BD : DD->flat_bindings())
6129 if (auto *KD = BD->getHoldingVar();
6130 KD && !this->visitVarDecl(KD, KD->getInit()))
6131 return false;
6132 }
6133 return true;
6134}
6135
6137 assert(FD);
6138 assert(FD->getParent()->isUnion());
6139 const CXXRecordDecl *CXXRD =
6141 return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
6142}
6143
6144template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
6145 for (;;) {
6146 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
6147 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
6148 FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
6149 return true;
6150 E = ME->getBase();
6151 continue;
6152 }
6153
6154 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
6155 E = ASE->getBase()->IgnoreImplicit();
6156 continue;
6157 }
6158
6159 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
6160 ICE && (ICE->getCastKind() == CK_NoOp ||
6161 ICE->getCastKind() == CK_DerivedToBase ||
6162 ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
6163 E = ICE->getSubExpr();
6164 continue;
6165 }
6166
6167 if (const auto *This = dyn_cast<CXXThisExpr>(E)) {
6168 const auto *ThisRecord =
6169 This->getType()->getPointeeType()->getAsRecordDecl();
6170 if (!ThisRecord->isUnion())
6171 return false;
6172 // Otherwise, always activate if we're in the ctor.
6173 if (const auto *Ctor =
6174 dyn_cast_if_present<CXXConstructorDecl>(CompilingFunction))
6175 return Ctor->getParent() == ThisRecord;
6176 return false;
6177 }
6178
6179 break;
6180 }
6181 return false;
6182}
6183
6184template <class Emitter>
6186 bool EvaluateConditionDecl) {
6187 for (const auto *D : DS->decls()) {
6190 continue;
6191
6192 const auto *VD = dyn_cast<VarDecl>(D);
6193 if (!VD)
6194 return false;
6195 if (!this->visitVarDecl(VD, VD->getInit()))
6196 return false;
6197
6198 // Register decomposition decl holding vars.
6199 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
6200 return false;
6201 }
6202
6203 return true;
6204}
6205
6206template <class Emitter>
6208 if (this->InStmtExpr)
6209 return this->emitUnsupported(RS);
6210
6211 if (const Expr *RE = RS->getRetValue()) {
6212 LocalScope<Emitter> RetScope(this);
6213 if (ReturnType) {
6214 // Primitive types are simply returned.
6215 if (!this->visit(RE))
6216 return false;
6217 this->emitCleanup();
6218 return this->emitRet(*ReturnType, RS);
6219 }
6220
6221 if (RE->getType()->isVoidType()) {
6222 if (!this->visit(RE))
6223 return false;
6224 } else {
6225 if (RE->containsErrors())
6226 return false;
6227
6229 // RVO - construct the value in the return location.
6230 if (!this->emitRVOPtr(RE))
6231 return false;
6232 if (!this->visitInitializerPop(RE))
6233 return false;
6234
6235 this->emitCleanup();
6236 return this->emitRetVoid(RS);
6237 }
6238 }
6239
6240 // Void return.
6241 this->emitCleanup();
6242 return this->emitRetVoid(RS);
6243}
6244
6245template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
6246 LocalScope<Emitter> IfScope(this);
6247
6248 auto visitChildStmt = [&](const Stmt *S) -> bool {
6249 LocalScope<Emitter> SScope(this);
6250 if (!visitStmt(S))
6251 return false;
6252 return SScope.destroyLocals();
6253 };
6254
6255 if (auto *CondInit = IS->getInit()) {
6256 if (!visitStmt(CondInit))
6257 return false;
6258 }
6259
6260 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) {
6261 if (!visitDeclStmt(CondDecl))
6262 return false;
6263 }
6264
6265 // Save ourselves compiling some code and the jumps, etc. if the condition is
6266 // stataically known to be either true or false. We could look at more cases
6267 // here, but I think all the ones that actually happen are using a
6268 // ConstantExpr.
6269 if (std::optional<bool> BoolValue = getBoolValue(IS->getCond())) {
6270 if (*BoolValue)
6271 return visitChildStmt(IS->getThen());
6272 if (const Stmt *Else = IS->getElse())
6273 return visitChildStmt(Else);
6274 return true;
6275 }
6276
6277 // Otherwise, compile the condition.
6278 if (IS->isNonNegatedConsteval()) {
6279 if (!this->emitIsConstantContext(IS))
6280 return false;
6281 } else if (IS->isNegatedConsteval()) {
6282 if (!this->emitIsConstantContext(IS))
6283 return false;
6284 if (!this->emitInv(IS))
6285 return false;
6286 } else {
6288 if (!this->visitBool(IS->getCond()))
6289 return false;
6290 if (!CondScope.destroyLocals())
6291 return false;
6292 }
6293
6294 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
6295 return false;
6296
6297 if (const Stmt *Else = IS->getElse()) {
6298 LabelTy LabelElse = this->getLabel();
6299 LabelTy LabelEnd = this->getLabel();
6300 if (!this->jumpFalse(LabelElse, IS))
6301 return false;
6302 if (!visitChildStmt(IS->getThen()))
6303 return false;
6304 if (!this->jump(LabelEnd, IS))
6305 return false;
6306 this->emitLabel(LabelElse);
6307 if (!visitChildStmt(Else))
6308 return false;
6309 this->emitLabel(LabelEnd);
6310 } else {
6311 LabelTy LabelEnd = this->getLabel();
6312 if (!this->jumpFalse(LabelEnd, IS))
6313 return false;
6314 if (!visitChildStmt(IS->getThen()))
6315 return false;
6316 this->emitLabel(LabelEnd);
6317 }
6318
6319 if (!IfScope.destroyLocals())
6320 return false;
6321
6322 return true;
6323}
6324
6325template <class Emitter>
6327 const Expr *Cond = S->getCond();
6328 const Stmt *Body = S->getBody();
6329
6330 LabelTy CondLabel = this->getLabel(); // Label before the condition.
6331 LabelTy EndLabel = this->getLabel(); // Label after the loop.
6332 LocalScope<Emitter> WholeLoopScope(this);
6333 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6334
6335 this->fallthrough(CondLabel);
6336 this->emitLabel(CondLabel);
6337
6338 // Start of the loop body {
6339 LocalScope<Emitter> CondScope(this);
6340
6341 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6342 if (!visitDeclStmt(CondDecl))
6343 return false;
6344 }
6345
6346 if (!this->visitBool(Cond))
6347 return false;
6348
6349 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6350 return false;
6351
6352 if (!this->jumpFalse(EndLabel, S))
6353 return false;
6354
6355 if (!this->visitStmt(Body))
6356 return false;
6357
6358 if (!CondScope.destroyLocals())
6359 return false;
6360 // } End of loop body.
6361
6362 if (!this->jump(CondLabel, S))
6363 return false;
6364 this->fallthrough(EndLabel);
6365 this->emitLabel(EndLabel);
6366
6367 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6368}
6369
6370template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
6371 const Expr *Cond = S->getCond();
6372 const Stmt *Body = S->getBody();
6373
6374 LabelTy StartLabel = this->getLabel();
6375 LabelTy EndLabel = this->getLabel();
6376 LabelTy CondLabel = this->getLabel();
6377 LocalScope<Emitter> WholeLoopScope(this);
6378 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6379
6380 this->fallthrough(StartLabel);
6381 this->emitLabel(StartLabel);
6382
6383 {
6384 LocalScope<Emitter> CondScope(this);
6385 if (!this->visitStmt(Body))
6386 return false;
6387 this->fallthrough(CondLabel);
6388 this->emitLabel(CondLabel);
6389 if (!this->visitBool(Cond))
6390 return false;
6391
6392 if (!CondScope.destroyLocals())
6393 return false;
6394 }
6395 if (!this->jumpTrue(StartLabel, S))
6396 return false;
6397
6398 this->fallthrough(EndLabel);
6399 this->emitLabel(EndLabel);
6400 return WholeLoopScope.destroyLocals();
6401}
6402
6403template <class Emitter>
6405 // for (Init; Cond; Inc) { Body }
6406 const Stmt *Init = S->getInit();
6407 const Expr *Cond = S->getCond();
6408 const Expr *Inc = S->getInc();
6409 const Stmt *Body = S->getBody();
6410
6411 LabelTy EndLabel = this->getLabel();
6412 LabelTy CondLabel = this->getLabel();
6413 LabelTy IncLabel = this->getLabel();
6414
6415 LocalScope<Emitter> WholeLoopScope(this);
6416 if (Init && !this->visitStmt(Init))
6417 return false;
6418
6419 // Start of the loop body {
6420 this->fallthrough(CondLabel);
6421 this->emitLabel(CondLabel);
6422
6423 LocalScope<Emitter> CondScope(this);
6424 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6425 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6426 if (!visitDeclStmt(CondDecl))
6427 return false;
6428 }
6429
6430 if (Cond) {
6431 if (!this->visitBool(Cond))
6432 return false;
6433 if (!this->jumpFalse(EndLabel, S))
6434 return false;
6435 }
6436 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6437 return false;
6438
6439 if (Body && !this->visitStmt(Body))
6440 return false;
6441
6442 this->fallthrough(IncLabel);
6443 this->emitLabel(IncLabel);
6444 if (Inc && !this->discard(Inc))
6445 return false;
6446
6447 if (!CondScope.destroyLocals())
6448 return false;
6449 if (!this->jump(CondLabel, S))
6450 return false;
6451 // } End of loop body.
6452
6453 this->emitLabel(EndLabel);
6454 // If we jumped out of the loop above, we still need to clean up the condition
6455 // scope.
6456 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6457}
6458
6459template <class Emitter>
6461 const Stmt *Init = S->getInit();
6462 const Expr *Cond = S->getCond();
6463 const Expr *Inc = S->getInc();
6464 const Stmt *Body = S->getBody();
6465 const Stmt *BeginStmt = S->getBeginStmt();
6466 const Stmt *RangeStmt = S->getRangeStmt();
6467 const Stmt *EndStmt = S->getEndStmt();
6468
6469 LabelTy EndLabel = this->getLabel();
6470 LabelTy CondLabel = this->getLabel();
6471 LabelTy IncLabel = this->getLabel();
6472 LocalScope<Emitter> WholeLoopScope(this);
6473 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6474
6475 // Emit declarations needed in the loop.
6476 if (Init && !this->visitStmt(Init))
6477 return false;
6478 if (!this->visitStmt(RangeStmt))
6479 return false;
6480 if (!this->visitStmt(BeginStmt))
6481 return false;
6482 if (!this->visitStmt(EndStmt))
6483 return false;
6484
6485 // Now the condition as well as the loop variable assignment.
6486 this->fallthrough(CondLabel);
6487 this->emitLabel(CondLabel);
6488 if (!this->visitBool(Cond))
6489 return false;
6490 if (!this->jumpFalse(EndLabel, S))
6491 return false;
6492
6493 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
6494 return false;
6495
6496 // Body.
6497 {
6498 if (!this->visitStmt(Body))
6499 return false;
6500
6501 this->fallthrough(IncLabel);
6502 this->emitLabel(IncLabel);
6503 if (!this->discard(Inc))
6504 return false;
6505 }
6506
6507 if (!this->jump(CondLabel, S))
6508 return false;
6509
6510 this->fallthrough(EndLabel);
6511 this->emitLabel(EndLabel);
6512 return WholeLoopScope.destroyLocals();
6513}
6514
6515template <class Emitter>
6517 if (LabelInfoStack.empty())
6518 return false;
6519
6520 OptLabelTy TargetLabel = std::nullopt;
6521 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6522 const VariableScope<Emitter> *BreakScope = nullptr;
6523
6524 if (!TargetLoop) {
6525 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6526 if (LI.BreakLabel) {
6527 TargetLabel = *LI.BreakLabel;
6528 BreakScope = LI.BreakOrContinueScope;
6529 break;
6530 }
6531 }
6532 } else {
6533 for (auto LI : LabelInfoStack) {
6534 if (LI.Name == TargetLoop) {
6535 TargetLabel = *LI.BreakLabel;
6536 BreakScope = LI.BreakOrContinueScope;
6537 break;
6538 }
6539 }
6540 }
6541
6542 // Faulty break statement (e.g. label redefined or named loops disabled).
6543 if (!TargetLabel)
6544 return false;
6545
6546 for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope;
6547 C = C->getParent()) {
6548 if (!C->destroyLocals())
6549 return false;
6550 }
6551
6552 return this->jump(*TargetLabel, S);
6553}
6554
6555template <class Emitter>
6557 if (LabelInfoStack.empty())
6558 return false;
6559
6560 OptLabelTy TargetLabel = std::nullopt;
6561 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6562 const VariableScope<Emitter> *ContinueScope = nullptr;
6563
6564 if (!TargetLoop) {
6565 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6566 if (LI.ContinueLabel) {
6567 TargetLabel = *LI.ContinueLabel;
6568 ContinueScope = LI.BreakOrContinueScope;
6569 break;
6570 }
6571 }
6572 } else {
6573 for (auto LI : LabelInfoStack) {
6574 if (LI.Name == TargetLoop) {
6575 TargetLabel = *LI.ContinueLabel;
6576 ContinueScope = LI.BreakOrContinueScope;
6577 break;
6578 }
6579 }
6580 }
6581 assert(TargetLabel);
6582
6583 for (VariableScope<Emitter> *C = VarScope; C != ContinueScope;
6584 C = C->getParent()) {
6585 if (!C->destroyLocals())
6586 return false;
6587 }
6588
6589 return this->jump(*TargetLabel, S);
6590}
6591
6592template <class Emitter>
6594 const Expr *Cond = S->getCond();
6595 if (Cond->containsErrors())
6596 return false;
6597
6598 PrimType CondT = this->classifyPrim(Cond->getType());
6599 LocalScope<Emitter> LS(this);
6600
6601 LabelTy EndLabel = this->getLabel();
6602 UnsignedOrNone DefaultLabel = std::nullopt;
6603 unsigned CondVar =
6604 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
6605
6606 if (const auto *CondInit = S->getInit())
6607 if (!visitStmt(CondInit))
6608 return false;
6609
6610 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
6611 if (!visitDeclStmt(CondDecl))
6612 return false;
6613
6614 // Initialize condition variable.
6615 if (!this->visit(Cond))
6616 return false;
6617 if (!this->emitSetLocal(CondT, CondVar, S))
6618 return false;
6619
6620 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6621 return false;
6622
6624 // Create labels and comparison ops for all case statements.
6625 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
6626 SC = SC->getNextSwitchCase()) {
6627 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
6628 CaseLabels[SC] = this->getLabel();
6629
6630 if (CS->caseStmtIsGNURange()) {
6631 LabelTy EndOfRangeCheck = this->getLabel();
6632 const Expr *Low = CS->getLHS();
6633 const Expr *High = CS->getRHS();
6634 if (Low->isValueDependent() || High->isValueDependent())
6635 return false;
6636
6637 if (!this->emitGetLocal(CondT, CondVar, CS))
6638 return false;
6639 if (!this->visit(Low))
6640 return false;
6641 PrimType LT = this->classifyPrim(Low->getType());
6642 if (!this->emitGE(LT, S))
6643 return false;
6644 if (!this->jumpFalse(EndOfRangeCheck, S))
6645 return false;
6646
6647 if (!this->emitGetLocal(CondT, CondVar, CS))
6648 return false;
6649 if (!this->visit(High))
6650 return false;
6651 PrimType HT = this->classifyPrim(High->getType());
6652 if (!this->emitLE(HT, S))
6653 return false;
6654 if (!this->jumpTrue(CaseLabels[CS], S))
6655 return false;
6656 this->emitLabel(EndOfRangeCheck);
6657 continue;
6658 }
6659
6660 const Expr *Value = CS->getLHS();
6661 if (Value->isValueDependent())
6662 return false;
6663 PrimType ValueT = this->classifyPrim(Value->getType());
6664
6665 // Compare the case statement's value to the switch condition.
6666 if (!this->emitGetLocal(CondT, CondVar, CS))
6667 return false;
6668 if (!this->visit(Value))
6669 return false;
6670
6671 // Compare and jump to the case label.
6672 if (!this->emitEQ(ValueT, S))
6673 return false;
6674 if (!this->jumpTrue(CaseLabels[CS], S))
6675 return false;
6676 } else {
6677 assert(!DefaultLabel);
6678 DefaultLabel = this->getLabel();
6679 }
6680 }
6681
6682 // If none of the conditions above were true, fall through to the default
6683 // statement or jump after the switch statement.
6684 if (DefaultLabel) {
6685 if (!this->jump(*DefaultLabel, S))
6686 return false;
6687 } else {
6688 if (!this->jump(EndLabel, S))
6689 return false;
6690 }
6691
6692 SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel,
6693 DefaultLabel);
6694 if (!this->visitStmt(S->getBody()))
6695 return false;
6696 this->fallthrough(EndLabel);
6697 this->emitLabel(EndLabel);
6698
6699 return LS.destroyLocals();
6700}
6701
6702template <class Emitter>
6704 this->fallthrough(CaseLabels[S]);
6705 this->emitLabel(CaseLabels[S]);
6706 return this->visitStmt(S->getSubStmt());
6707}
6708
6709template <class Emitter>
6711 if (LabelInfoStack.empty())
6712 return false;
6713
6714 LabelTy DefaultLabel;
6715 for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) {
6716 if (LI.DefaultLabel) {
6717 DefaultLabel = *LI.DefaultLabel;
6718 break;
6719 }
6720 }
6721
6722 this->emitLabel(DefaultLabel);
6723 return this->visitStmt(S->getSubStmt());
6724}
6725
6726template <class Emitter>
6728 const Stmt *SubStmt = S->getSubStmt();
6729
6730 bool IsMSVCConstexprAttr = isa<ReturnStmt>(SubStmt) &&
6732
6733 if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S))
6734 return false;
6735
6736 if (this->Ctx.getLangOpts().CXXAssumptions &&
6737 !this->Ctx.getLangOpts().MSVCCompat) {
6738 for (const Attr *A : S->getAttrs()) {
6739 auto *AA = dyn_cast<CXXAssumeAttr>(A);
6740 if (!AA)
6741 continue;
6742
6743 assert(isa<NullStmt>(SubStmt));
6744
6745 const Expr *Assumption = AA->getAssumption();
6746 if (Assumption->isValueDependent())
6747 return false;
6748
6749 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
6750 continue;
6751
6752 // Evaluate assumption.
6753 if (!this->visitBool(Assumption))
6754 return false;
6755
6756 if (!this->emitAssume(Assumption))
6757 return false;
6758 }
6759 }
6760
6761 // Ignore other attributes.
6762 if (!this->visitStmt(SubStmt))
6763 return false;
6764
6765 if (IsMSVCConstexprAttr)
6766 return this->emitPopMSVCCE(S);
6767 return true;
6768}
6769
6770template <class Emitter>
6772 // Ignore all handlers.
6773 return this->visitStmt(S->getTryBlock());
6774}
6775
6776template <class Emitter>
6777bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
6778 assert(MD->isLambdaStaticInvoker());
6779 assert(MD->hasBody());
6780 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
6781
6782 const CXXRecordDecl *ClosureClass = MD->getParent();
6783 const FunctionDecl *LambdaCallOp;
6784 assert(ClosureClass->captures().empty());
6785 if (ClosureClass->isGenericLambda()) {
6786 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6787 assert(MD->isFunctionTemplateSpecialization() &&
6788 "A generic lambda's static-invoker function must be a "
6789 "template specialization");
6791 FunctionTemplateDecl *CallOpTemplate =
6792 LambdaCallOp->getDescribedFunctionTemplate();
6793 void *InsertPos = nullptr;
6794 const FunctionDecl *CorrespondingCallOpSpecialization =
6795 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
6796 assert(CorrespondingCallOpSpecialization);
6797 LambdaCallOp = CorrespondingCallOpSpecialization;
6798 } else {
6799 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6800 }
6801 assert(ClosureClass->captures().empty());
6802 const Function *Func = this->getFunction(LambdaCallOp);
6803 if (!Func)
6804 return false;
6805 assert(Func->hasThisPointer());
6806 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
6807
6808 if (Func->hasRVO()) {
6809 if (!this->emitRVOPtr(MD))
6810 return false;
6811 }
6812
6813 // The lambda call operator needs an instance pointer, but we don't have
6814 // one here, and we don't need one either because the lambda cannot have
6815 // any captures, as verified above. Emit a null pointer. This is then
6816 // special-cased when interpreting to not emit any misleading diagnostics.
6817 if (!this->emitNullPtr(0, nullptr, MD))
6818 return false;
6819
6820 // Forward all arguments from the static invoker to the lambda call operator.
6821 for (const ParmVarDecl *PVD : MD->parameters()) {
6822 auto It = this->Params.find(PVD);
6823 assert(It != this->Params.end());
6824
6825 // We do the lvalue-to-rvalue conversion manually here, so no need
6826 // to care about references.
6827 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
6828 if (!this->emitGetParam(ParamType, It->second.Index, MD))
6829 return false;
6830 }
6831
6832 if (!this->emitCall(Func, 0, LambdaCallOp))
6833 return false;
6834
6835 this->emitCleanup();
6836 if (ReturnType)
6837 return this->emitRet(*ReturnType, MD);
6838
6839 // Nothing to do, since we emitted the RVO pointer above.
6840 return this->emitRetVoid(MD);
6841}
6842
6843template <class Emitter>
6844bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
6845 if (Ctx.getLangOpts().CPlusPlus23)
6846 return true;
6847
6848 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
6849 return true;
6850
6851 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
6852}
6853
6855 const Expr *InitExpr = Init->getInit();
6856
6857 if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
6858 !isa<CXXConstructExpr>(InitExpr))
6859 return true;
6860
6861 if (const auto *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
6862 const CXXConstructorDecl *Ctor = CE->getConstructor();
6863 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
6864 Ctor->isTrivial())
6865 return true;
6866 }
6867
6868 return false;
6869}
6870
6871template <class Emitter>
6872bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
6873 assert(!ReturnType);
6874
6875 // Only start the lifetime of the instance pointer.
6876 if (!this->emitStartThisLifetime1(Ctor))
6877 return false;
6878
6879 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
6880 const Expr *InitExpr,
6881 bool Activate = false) -> bool {
6882 // We don't know what to do with these, so just return false.
6883 if (InitExpr->getType().isNull())
6884 return false;
6885
6886 if (OptPrimType T = this->classify(InitExpr)) {
6887 if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
6888 return false;
6889
6890 if (!this->visit(InitExpr))
6891 return false;
6892
6893 if (F->isBitField())
6894 return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(),
6895 InitExpr);
6896 return this->emitInitThisField(*T, FieldOffset, InitExpr);
6897 }
6898 // Non-primitive case. Get a pointer to the field-to-initialize
6899 // on the stack and call visitInitialzer() for it.
6900 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
6901 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
6902 return false;
6903
6904 if (Activate && !this->emitActivate(InitExpr))
6905 return false;
6906
6907 return this->visitInitializerPop(InitExpr);
6908 };
6909
6910 const RecordDecl *RD = Ctor->getParent();
6911 const Record *R = this->getRecord(RD);
6912 if (!R)
6913 return false;
6914 bool IsUnion = R->isUnion();
6915
6916 // Default union copy and move ctors are special.
6917 if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) {
6919
6920 // No special case for NumFields == 0 here, so the Memcpy op
6921 // below also does its checks in those cases.
6922
6923 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
6924 if (!this->emitThis(Ctor))
6925 return false;
6926
6927 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor))
6928 return false;
6929
6930 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
6931 this->emitRetVoid(Ctor);
6932 }
6933
6934 unsigned FieldInits = 0;
6936 for (const auto *Init : Ctor->inits()) {
6937 // Scope needed for the initializers.
6938 LocalScope<Emitter> Scope(this, ScopeKind::FullExpression);
6939
6940 const Expr *InitExpr = Init->getInit();
6941 if (const FieldDecl *Member = Init->getMember()) {
6942 const Record::Field *F = R->getField(Member);
6943
6946 if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
6947 return false;
6948 ++FieldInits;
6949 } else if (const Type *Base = Init->getBaseClass()) {
6950 const auto *BaseDecl = Base->getAsCXXRecordDecl();
6951 assert(BaseDecl);
6952
6953 if (Init->isBaseVirtual()) {
6954 assert(R->getVirtualBase(BaseDecl));
6955 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
6956 return false;
6957
6958 } else {
6959 // Base class initializer.
6960 // Get This Base and call initializer on it.
6961 const Record::Base *B = R->getBase(BaseDecl);
6962 assert(B);
6963 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
6964 return false;
6965 }
6966
6967 if (IsUnion && !this->emitActivate(InitExpr))
6968 return false;
6969
6970 if (!this->visitInitializerPop(InitExpr))
6971 return false;
6972 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
6975 unsigned ChainSize = IFD->getChainingSize();
6976 assert(ChainSize >= 2);
6977
6978 unsigned NestedFieldOffset = 0;
6979 const Record::Field *NestedField = nullptr;
6980 for (unsigned I = 0; I != ChainSize; ++I) {
6981 const auto *FD = cast<FieldDecl>(IFD->chain()[I]);
6982 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6983 assert(FieldRecord);
6984
6985 NestedField = FieldRecord->getField(FD);
6986 assert(NestedField);
6987 IsUnion = IsUnion || FieldRecord->isUnion();
6988
6989 NestedFieldOffset += NestedField->Offset;
6990
6991 // Add a new InitChainLink for the record, but not for the final field.
6992 if (I != ChainSize - 1)
6993 InitStack.push_back(InitLink::Field(NestedField->Offset));
6994 }
6995 assert(NestedField);
6996
6998 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
6999 IsUnion))
7000 return false;
7001
7002 // Mark all chain links as initialized.
7003 unsigned InitFieldOffset = 0;
7004 for (const NamedDecl *ND : IFD->chain().drop_back()) {
7005 const auto *FD = cast<FieldDecl>(ND);
7006 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
7007 assert(FieldRecord);
7008 NestedField = FieldRecord->getField(FD);
7009 InitFieldOffset += NestedField->Offset;
7010 assert(NestedField);
7011 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
7012 return false;
7013 if (!this->emitFinishInitPop(InitExpr))
7014 return false;
7015 }
7016
7017 InitStack.pop_back_n(ChainSize - 1);
7018
7019 } else {
7020 assert(Init->isDelegatingInitializer());
7021 if (!this->emitThis(InitExpr))
7022 return false;
7023 if (!this->visitInitializerPop(Init->getInit()))
7024 return false;
7025 }
7026
7027 if (!Scope.destroyLocals())
7028 return false;
7029 }
7030
7031 if (FieldInits != R->getNumFields()) {
7032 assert(FieldInits < R->getNumFields());
7033 // Start the lifetime of all members.
7034 if (!this->emitStartThisLifetime(Ctor))
7035 return false;
7036 }
7037
7038 if (const Stmt *Body = Ctor->getBody()) {
7039 // Only emit the CtorCheck op for non-empty CompoundStmt bodies.
7040 // For non-CompoundStmts, always assume they are non-empty and emit it.
7041 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
7042 if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{}))
7043 return false;
7044 } else {
7045 if (!this->emitCtorCheck(SourceInfo{}))
7046 return false;
7047 }
7048
7049 if (!visitStmt(Body))
7050 return false;
7051 }
7052
7053 return this->emitRetVoid(SourceInfo{});
7054}
7055
7056template <class Emitter>
7057bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
7058 const RecordDecl *RD = Dtor->getParent();
7059 const Record *R = this->getRecord(RD);
7060 if (!R)
7061 return false;
7062
7063 if (!Dtor->isTrivial() && Dtor->getBody()) {
7064 if (!this->visitStmt(Dtor->getBody()))
7065 return false;
7066 }
7067
7068 if (!this->emitThis(Dtor))
7069 return false;
7070
7071 if (!this->emitCheckDestruction(Dtor))
7072 return false;
7073
7074 assert(R);
7075 if (!R->isUnion()) {
7076
7078 // First, destroy all fields.
7079 for (const Record::Field &Field : llvm::reverse(R->fields())) {
7080 const Descriptor *D = Field.Desc;
7081 if (D->hasTrivialDtor())
7082 continue;
7083 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
7084 return false;
7085 if (!this->emitDestructionPop(D, SourceInfo{}))
7086 return false;
7087 }
7088 }
7089
7090 for (const Record::Base &Base : llvm::reverse(R->bases())) {
7091 if (Base.R->hasTrivialDtor())
7092 continue;
7093 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
7094 return false;
7095 if (!this->emitRecordDestructionPop(Base.R, {}))
7096 return false;
7097 }
7098
7099 if (!this->emitMarkDestroyed(Dtor))
7100 return false;
7101
7102 // FIXME: Virtual bases.
7103 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
7104}
7105
7106template <class Emitter>
7107bool Compiler<Emitter>::compileUnionAssignmentOperator(
7108 const CXXMethodDecl *MD) {
7109 if (!this->emitThis(MD))
7110 return false;
7111
7112 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD))
7113 return false;
7114
7115 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
7116}
7117
7118template <class Emitter>
7120 if (F->getReturnType()->isDependentType())
7121 return false;
7122
7123 // Classify the return type.
7124 ReturnType = this->classify(F->getReturnType());
7125
7126 this->CompilingFunction = F;
7127
7128 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
7129 return this->compileConstructor(Ctor);
7130 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
7131 return this->compileDestructor(Dtor);
7132
7133 // Emit custom code if this is a lambda static invoker.
7134 if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
7135 const RecordDecl *RD = MD->getParent();
7136
7137 if (RD->isUnion() &&
7139 return this->compileUnionAssignmentOperator(MD);
7140
7141 if (MD->isLambdaStaticInvoker())
7142 return this->emitLambdaStaticInvokerBody(MD);
7143 }
7144
7145 // Regular functions.
7146 if (const auto *Body = F->getBody())
7147 if (!visitStmt(Body))
7148 return false;
7149
7150 // Emit a guard return to protect against a code path missing one.
7151 if (F->getReturnType()->isVoidType())
7152 return this->emitRetVoid(SourceInfo{});
7153 return this->emitNoRet(SourceInfo{});
7154}
7155
7156static uint32_t getBitWidth(const Expr *E) {
7157 assert(E->refersToBitField());
7158 const auto *ME = cast<MemberExpr>(E);
7159 const auto *FD = cast<FieldDecl>(ME->getMemberDecl());
7160 return FD->getBitWidthValue();
7161}
7162
7163template <class Emitter>
7165 if (E->containsErrors())
7166 return false;
7167
7168 const Expr *SubExpr = E->getSubExpr();
7169 if (SubExpr->getType()->isAnyComplexType())
7170 return this->VisitComplexUnaryOperator(E);
7171 if (SubExpr->getType()->isVectorType())
7172 return this->VisitVectorUnaryOperator(E);
7173 if (SubExpr->getType()->isFixedPointType())
7174 return this->VisitFixedPointUnaryOperator(E);
7175 OptPrimType T = classify(SubExpr->getType());
7176
7177 switch (E->getOpcode()) {
7178 case UO_PostInc: { // x++
7179 if (!Ctx.getLangOpts().CPlusPlus14)
7180 return this->emitInvalid(E);
7181 if (!T)
7182 return this->emitError(E);
7183
7184 if (!this->visit(SubExpr))
7185 return false;
7186
7187 if (T == PT_Ptr) {
7188 if (!this->emitIncPtr(E))
7189 return false;
7190
7191 return DiscardResult ? this->emitPopPtr(E) : true;
7192 }
7193
7194 if (T == PT_Float)
7195 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
7196 : this->emitIncf(getFPOptions(E), E);
7197
7198 if (SubExpr->refersToBitField())
7199 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7200 getBitWidth(SubExpr), E)
7201 : this->emitIncBitfield(*T, E->canOverflow(),
7202 getBitWidth(SubExpr), E);
7203
7204 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
7205 : this->emitInc(*T, E->canOverflow(), E);
7206 }
7207 case UO_PostDec: { // x--
7208 if (!Ctx.getLangOpts().CPlusPlus14)
7209 return this->emitInvalid(E);
7210 if (!T)
7211 return this->emitError(E);
7212
7213 if (!this->visit(SubExpr))
7214 return false;
7215
7216 if (T == PT_Ptr) {
7217 if (!this->emitDecPtr(E))
7218 return false;
7219
7220 return DiscardResult ? this->emitPopPtr(E) : true;
7221 }
7222
7223 if (T == PT_Float)
7224 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
7225 : this->emitDecf(getFPOptions(E), E);
7226
7227 if (SubExpr->refersToBitField()) {
7228 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7229 getBitWidth(SubExpr), E)
7230 : this->emitDecBitfield(*T, E->canOverflow(),
7231 getBitWidth(SubExpr), E);
7232 }
7233
7234 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
7235 : this->emitDec(*T, E->canOverflow(), E);
7236 }
7237 case UO_PreInc: { // ++x
7238 if (!Ctx.getLangOpts().CPlusPlus14)
7239 return this->emitInvalid(E);
7240 if (!T)
7241 return this->emitError(E);
7242
7243 if (!this->visit(SubExpr))
7244 return false;
7245
7246 if (T == PT_Ptr) {
7247 if (!this->emitLoadPtr(E))
7248 return false;
7249 if (!this->emitConstUint8(1, E))
7250 return false;
7251 if (!this->emitAddOffsetUint8(E))
7252 return false;
7253 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7254 }
7255
7256 // Post-inc and pre-inc are the same if the value is to be discarded.
7257 if (DiscardResult) {
7258 if (T == PT_Float)
7259 return this->emitIncfPop(getFPOptions(E), E);
7260 if (SubExpr->refersToBitField())
7261 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7262 getBitWidth(SubExpr), E)
7263 : this->emitIncBitfield(*T, E->canOverflow(),
7264 getBitWidth(SubExpr), E);
7265 return this->emitIncPop(*T, E->canOverflow(), E);
7266 }
7267
7268 if (T == PT_Float) {
7269 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7270 if (!this->emitLoadFloat(E))
7271 return false;
7272 APFloat F(TargetSemantics, 1);
7273 if (!this->emitFloat(F, E))
7274 return false;
7275
7276 if (!this->emitAddf(getFPOptions(E), E))
7277 return false;
7278 if (!this->emitStoreFloat(E))
7279 return false;
7280 } else if (SubExpr->refersToBitField()) {
7281 assert(isIntegerOrBoolType(*T));
7282 if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7283 E))
7284 return false;
7285 } else {
7286 assert(isIntegerOrBoolType(*T));
7287 if (!this->emitPreInc(*T, E->canOverflow(), E))
7288 return false;
7289 }
7290 return E->isGLValue() || this->emitLoadPop(*T, E);
7291 }
7292 case UO_PreDec: { // --x
7293 if (!Ctx.getLangOpts().CPlusPlus14)
7294 return this->emitInvalid(E);
7295 if (!T)
7296 return this->emitError(E);
7297
7298 if (!this->visit(SubExpr))
7299 return false;
7300
7301 if (T == PT_Ptr) {
7302 if (!this->emitLoadPtr(E))
7303 return false;
7304 if (!this->emitConstUint8(1, E))
7305 return false;
7306 if (!this->emitSubOffsetUint8(E))
7307 return false;
7308 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7309 }
7310
7311 // Post-dec and pre-dec are the same if the value is to be discarded.
7312 if (DiscardResult) {
7313 if (T == PT_Float)
7314 return this->emitDecfPop(getFPOptions(E), E);
7315 if (SubExpr->refersToBitField())
7316 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7317 getBitWidth(SubExpr), E)
7318 : this->emitDecBitfield(*T, E->canOverflow(),
7319 getBitWidth(SubExpr), E);
7320 return this->emitDecPop(*T, E->canOverflow(), E);
7321 }
7322
7323 if (T == PT_Float) {
7324 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7325 if (!this->emitLoadFloat(E))
7326 return false;
7327 APFloat F(TargetSemantics, 1);
7328 if (!this->emitFloat(F, E))
7329 return false;
7330
7331 if (!this->emitSubf(getFPOptions(E), E))
7332 return false;
7333 if (!this->emitStoreFloat(E))
7334 return false;
7335 } else if (SubExpr->refersToBitField()) {
7336 assert(isIntegerOrBoolType(*T));
7337 if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7338 E))
7339 return false;
7340 } else {
7341 assert(isIntegerOrBoolType(*T));
7342 if (!this->emitPreDec(*T, E->canOverflow(), E))
7343 return false;
7344 }
7345 return E->isGLValue() || this->emitLoadPop(*T, E);
7346 }
7347 case UO_LNot: // !x
7348 if (!T)
7349 return this->emitError(E);
7350
7351 if (DiscardResult)
7352 return this->discard(SubExpr);
7353
7354 if (!this->visitBool(SubExpr))
7355 return false;
7356
7357 if (!this->emitInv(E))
7358 return false;
7359
7360 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7361 return this->emitCast(PT_Bool, ET, E);
7362 return true;
7363 case UO_Minus: // -x
7364 if (!T)
7365 return this->emitError(E);
7366
7367 if (!this->visit(SubExpr))
7368 return false;
7369 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
7370 case UO_Plus: // +x
7371 if (!T)
7372 return this->emitError(E);
7373
7374 if (!this->visit(SubExpr)) // noop
7375 return false;
7376 return DiscardResult ? this->emitPop(*T, E) : true;
7377 case UO_AddrOf: // &x
7378 if (E->getType()->isMemberPointerType()) {
7379 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
7380 // member can be formed.
7381 if (DiscardResult)
7382 return true;
7383 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
7384 }
7385 // We should already have a pointer when we get here.
7386 return this->delegate(SubExpr);
7387 case UO_Deref: // *x
7388 if (DiscardResult)
7389 return this->discard(SubExpr);
7390
7391 if (!this->visit(SubExpr))
7392 return false;
7393
7394 if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
7395 return false;
7396
7397 if (classifyPrim(SubExpr) == PT_Ptr)
7398 return this->emitNarrowPtr(E);
7399 return true;
7400
7401 case UO_Not: // ~x
7402 if (!T)
7403 return this->emitError(E);
7404
7405 if (!this->visit(SubExpr))
7406 return false;
7407 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
7408 case UO_Real: // __real x
7409 if (!T)
7410 return false;
7411 return this->delegate(SubExpr);
7412 case UO_Imag: { // __imag x
7413 if (!T)
7414 return false;
7415 if (!this->discard(SubExpr))
7416 return false;
7417 return DiscardResult
7418 ? true
7419 : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
7420 }
7421 case UO_Extension:
7422 return this->delegate(SubExpr);
7423 case UO_Coawait:
7424 assert(false && "Unhandled opcode");
7425 }
7426
7427 return false;
7428}
7429
7430template <class Emitter>
7432 const Expr *SubExpr = E->getSubExpr();
7433 assert(SubExpr->getType()->isAnyComplexType());
7434
7435 if (DiscardResult)
7436 return this->discard(SubExpr);
7437
7438 OptPrimType ResT = classify(E);
7439 auto prepareResult = [=]() -> bool {
7440 if (!ResT && !Initializing) {
7441 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7442 if (!LocalIndex)
7443 return false;
7444 return this->emitGetPtrLocal(*LocalIndex, E);
7445 }
7446
7447 return true;
7448 };
7449
7450 // The offset of the temporary, if we created one.
7451 unsigned SubExprOffset = ~0u;
7452 auto createTemp = [=, &SubExprOffset]() -> bool {
7453 SubExprOffset =
7454 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7455 if (!this->visit(SubExpr))
7456 return false;
7457 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
7458 };
7459
7460 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
7461 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7462 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7463 return false;
7464 return this->emitArrayElemPop(ElemT, Index, E);
7465 };
7466
7467 switch (E->getOpcode()) {
7468 case UO_Minus: // -x
7469 if (!prepareResult())
7470 return false;
7471 if (!createTemp())
7472 return false;
7473 for (unsigned I = 0; I != 2; ++I) {
7474 if (!getElem(SubExprOffset, I))
7475 return false;
7476 if (!this->emitNeg(ElemT, E))
7477 return false;
7478 if (!this->emitInitElem(ElemT, I, E))
7479 return false;
7480 }
7481 break;
7482
7483 case UO_Plus: // +x
7484 case UO_AddrOf: // &x
7485 case UO_Deref: // *x
7486 return this->delegate(SubExpr);
7487
7488 case UO_LNot:
7489 if (!this->visit(SubExpr))
7490 return false;
7491 if (!this->emitComplexBoolCast(SubExpr))
7492 return false;
7493 if (!this->emitInv(E))
7494 return false;
7495 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7496 return this->emitCast(PT_Bool, ET, E);
7497 return true;
7498
7499 case UO_Real:
7500 return this->emitComplexReal(SubExpr);
7501
7502 case UO_Imag:
7503 if (!this->visit(SubExpr))
7504 return false;
7505
7506 if (SubExpr->isLValue()) {
7507 if (!this->emitConstUint8(1, E))
7508 return false;
7509 return this->emitArrayElemPtrPopUint8(E);
7510 }
7511
7512 // Since our _Complex implementation does not map to a primitive type,
7513 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
7514 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
7515
7516 case UO_Not: // ~x
7517 if (!this->delegate(SubExpr))
7518 return false;
7519 // Negate the imaginary component.
7520 if (!this->emitArrayElem(ElemT, 1, E))
7521 return false;
7522 if (!this->emitNeg(ElemT, E))
7523 return false;
7524 if (!this->emitInitElem(ElemT, 1, E))
7525 return false;
7526 return DiscardResult ? this->emitPopPtr(E) : true;
7527
7528 case UO_Extension:
7529 return this->delegate(SubExpr);
7530
7531 default:
7532 return this->emitInvalid(E);
7533 }
7534
7535 return true;
7536}
7537
7538template <class Emitter>
7540 const Expr *SubExpr = E->getSubExpr();
7541 assert(SubExpr->getType()->isVectorType());
7542
7543 if (DiscardResult)
7544 return this->discard(SubExpr);
7545
7546 auto UnaryOp = E->getOpcode();
7547 if (UnaryOp == UO_Extension)
7548 return this->delegate(SubExpr);
7549
7550 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
7551 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
7552 return this->emitInvalid(E);
7553
7554 // Nothing to do here.
7555 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
7556 return this->delegate(SubExpr);
7557
7558 if (!Initializing) {
7559 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7560 if (!LocalIndex)
7561 return false;
7562 if (!this->emitGetPtrLocal(*LocalIndex, E))
7563 return false;
7564 }
7565
7566 // The offset of the temporary, if we created one.
7567 unsigned SubExprOffset =
7568 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7569 if (!this->visit(SubExpr))
7570 return false;
7571 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
7572 return false;
7573
7574 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
7575 PrimType ElemT = classifyVectorElementType(SubExpr->getType());
7576 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7577 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7578 return false;
7579 return this->emitArrayElemPop(ElemT, Index, E);
7580 };
7581
7582 switch (UnaryOp) {
7583 case UO_Minus:
7584 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7585 if (!getElem(SubExprOffset, I))
7586 return false;
7587 if (!this->emitNeg(ElemT, E))
7588 return false;
7589 if (!this->emitInitElem(ElemT, I, E))
7590 return false;
7591 }
7592 break;
7593 case UO_LNot: { // !x
7594 // In C++, the logic operators !, &&, || are available for vectors. !v is
7595 // equivalent to v == 0.
7596 //
7597 // The result of the comparison is a vector of the same width and number of
7598 // elements as the comparison operands with a signed integral element type.
7599 //
7600 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
7601 QualType ResultVecTy = E->getType();
7602 PrimType ResultVecElemT =
7603 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
7604 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7605 if (!getElem(SubExprOffset, I))
7606 return false;
7607 // operator ! on vectors returns -1 for 'truth', so negate it.
7608 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
7609 return false;
7610 if (!this->emitInv(E))
7611 return false;
7612 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
7613 return false;
7614 if (!this->emitNeg(ElemT, E))
7615 return false;
7616 if (ElemT != ResultVecElemT &&
7617 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
7618 return false;
7619 if (!this->emitInitElem(ResultVecElemT, I, E))
7620 return false;
7621 }
7622 break;
7623 }
7624 case UO_Not: // ~x
7625 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7626 if (!getElem(SubExprOffset, I))
7627 return false;
7628 if (ElemT == PT_Bool) {
7629 if (!this->emitInv(E))
7630 return false;
7631 } else {
7632 if (!this->emitComp(ElemT, E))
7633 return false;
7634 }
7635 if (!this->emitInitElem(ElemT, I, E))
7636 return false;
7637 }
7638 break;
7639 default:
7640 llvm_unreachable("Unsupported unary operators should be handled up front");
7641 }
7642 return true;
7643}
7644
7645template <class Emitter>
7647 if (DiscardResult)
7648 return true;
7649
7650 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7651 return this->emitConst(ECD->getInitVal(), E);
7652 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
7653 const Function *F = getFunction(FuncDecl);
7654 return F && this->emitGetFnPtr(F, E);
7655 }
7656 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
7657 if (UnsignedOrNone Index = P.getOrCreateGlobal(D)) {
7658 if (OptPrimType T = classify(D->getType())) {
7659 if (!this->visitAPValue(TPOD->getValue(), *T, E))
7660 return false;
7661 return this->emitInitGlobal(*T, *Index, E);
7662 }
7663
7664 if (!this->emitGetPtrGlobal(*Index, E))
7665 return false;
7666 if (!this->visitAPValueInitializer(TPOD->getValue(), E, TPOD->getType()))
7667 return false;
7668 return this->emitFinishInit(E);
7669 }
7670 return false;
7671 }
7672
7673 // References are implemented via pointers, so when we see a DeclRefExpr
7674 // pointing to a reference, we need to get its value directly (i.e. the
7675 // pointer to the actual value) instead of a pointer to the pointer to the
7676 // value.
7677 QualType DeclType = D->getType();
7678 bool IsReference = DeclType->isReferenceType();
7679
7680 // Function parameters.
7681 // Note that it's important to check them first since we might have a local
7682 // variable created for a ParmVarDecl as well.
7683 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
7684 if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
7685 !DeclType->isIntegralOrEnumerationType()) {
7686 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7687 /*InitializerFailed=*/false, E);
7688 }
7689 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
7690 if (IsReference || !It->second.IsPtr)
7691 return this->emitGetParam(classifyPrim(E), It->second.Index, E);
7692
7693 return this->emitGetPtrParam(It->second.Index, E);
7694 }
7695
7696 if (!Ctx.getLangOpts().CPlusPlus23 && IsReference)
7697 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7698 /*InitializerFailed=*/false, E);
7699 }
7700 // Local variables.
7701 if (auto It = Locals.find(D); It != Locals.end()) {
7702 const unsigned Offset = It->second.Offset;
7703 if (IsReference) {
7704 assert(classifyPrim(E) == PT_Ptr);
7705 return this->emitGetRefLocal(Offset, E);
7706 }
7707 return this->emitGetPtrLocal(Offset, E);
7708 }
7709 // Global variables.
7710 if (auto GlobalIndex = P.getGlobal(D)) {
7711 if (IsReference) {
7712 if (!Ctx.getLangOpts().CPlusPlus11)
7713 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
7714 if (!Ctx.getLangOpts().CPlusPlus23)
7715 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
7716 return this->emitGetRefGlobal(*GlobalIndex, E);
7717 }
7718
7719 return this->emitGetPtrGlobal(*GlobalIndex, E);
7720 }
7721
7722 // In case we need to re-visit a declaration.
7723 auto revisit = [&](const VarDecl *VD,
7724 bool IsConstexprUnknown = true) -> bool {
7726 IsConstexprUnknown);
7727 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
7728 return false;
7729 auto VarState = this->visitDecl(VD);
7730
7731 if (!this->emitPopCC(E))
7732 return false;
7733
7734 if (VarState.notCreated())
7735 return true;
7736 if (!VarState)
7737 return false;
7738 // Retry.
7739 return this->visitDeclRef(D, E);
7740 };
7741
7742 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
7743 // Lambda captures.
7744 if (auto It = this->LambdaCaptures.find(D);
7745 It != this->LambdaCaptures.end()) {
7746 auto [Offset, IsPtr] = It->second;
7747
7748 if (IsPtr)
7749 return this->emitGetThisFieldPtr(Offset, E);
7750 return this->emitGetPtrThisField(Offset, E);
7751 }
7752 }
7753
7754 if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
7755 DRE && DRE->refersToEnclosingVariableOrCapture()) {
7756 if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
7757 return revisit(VD);
7758 }
7759
7760 if (const auto *BD = dyn_cast<BindingDecl>(D))
7761 return this->visit(BD->getBinding());
7762
7763 // Avoid infinite recursion.
7764 if (D == InitializingDecl)
7765 return this->emitDummyPtr(D, E);
7766
7767 // Try to lazily visit (or emit dummy pointers for) declarations
7768 // we haven't seen yet.
7769 const auto *VD = dyn_cast<VarDecl>(D);
7770 if (!VD)
7771 return this->emitError(E);
7772
7773 // For C.
7774 if (!Ctx.getLangOpts().CPlusPlus) {
7775 if (VD->getInit() && !VD->getInit()->isValueDependent() &&
7776 DeclType.isConstant(Ctx.getASTContext()) && !VD->isWeak() &&
7777 VD->evaluateValue())
7778 return revisit(VD, /*IsConstexprUnknown=*/false);
7779 return this->emitDummyPtr(D, E);
7780 }
7781
7782 // ... and C++.
7783 const auto typeShouldBeVisited = [&](QualType T) -> bool {
7784 if (T.isConstant(Ctx.getASTContext()))
7785 return true;
7786 return T->isReferenceType();
7787 };
7788
7789 if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
7790 typeShouldBeVisited(DeclType)) {
7791 if (const Expr *Init = VD->getAnyInitializer();
7792 Init && !Init->isValueDependent()) {
7793 // Whether or not the evaluation is successul doesn't really matter
7794 // here -- we will create a global variable in any case, and that
7795 // will have the state of initializer evaluation attached.
7796 APValue V;
7798 (void)Init->EvaluateAsInitializer(V, Ctx.getASTContext(), VD, Notes,
7799 true);
7800 return this->visitDeclRef(D, E);
7801 }
7802 return revisit(VD, !VD->isConstexpr() && DeclType->isReferenceType());
7803 }
7804
7805 // FIXME: The evaluateValue() check here is a little ridiculous, since
7806 // it will ultimately call into Context::evaluateAsInitializer(). In
7807 // other words, we're evaluating the initializer, just to know if we can
7808 // evaluate the initializer.
7809 if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() &&
7810 !VD->getInit()->isValueDependent()) {
7811 if (VD->evaluateValue()) {
7812 bool IsConstexprUnknown = !DeclType.isConstant(Ctx.getASTContext()) &&
7813 !DeclType->isReferenceType();
7814 // Revisit the variable declaration, but make sure it's associated with a
7815 // different evaluation, so e.g. mutable reads don't work on it.
7816 EvalIDScope _(Ctx);
7817 return revisit(VD, IsConstexprUnknown);
7818 } else if (Ctx.getLangOpts().CPlusPlus23 && IsReference)
7819 return revisit(VD, /*IsConstexprUnknown=*/true);
7820
7821 if (IsReference)
7822 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7823 /*InitializerFailed=*/true, E);
7824 }
7825
7826 return this->emitDummyPtr(
7827 D, E, Ctx.getLangOpts().CPlusPlus23 && DeclType->isReferenceType());
7828}
7829
7830template <class Emitter>
7832 const auto *D = E->getDecl();
7833 return this->visitDeclRef(D, E);
7834}
7835
7836template <class Emitter>
7838 const DesignatedInitUpdateExpr *E) {
7839 if (!this->visitInitializer(E->getBase()))
7840 return false;
7841 return this->visitInitializer(E->getUpdater());
7842}
7843
7844template <class Emitter> bool Compiler<Emitter>::emitCleanup() {
7845 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) {
7846 if (!C->destroyLocals())
7847 return false;
7848 }
7849 return true;
7850}
7851
7852template <class Emitter>
7853unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
7854 const QualType DerivedType) {
7855 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
7856 if (const auto *R = Ty->getPointeeCXXRecordDecl())
7857 return R;
7858 return Ty->getAsCXXRecordDecl();
7859 };
7860 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
7861 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
7862
7863 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
7864}
7865
7866/// Emit casts from a PrimType to another PrimType.
7867template <class Emitter>
7868bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
7869 QualType ToQT, const Expr *E) {
7870
7871 if (FromT == PT_Float) {
7872 // Floating to floating.
7873 if (ToT == PT_Float) {
7874 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7875 return this->emitCastFP(ToSem, getRoundingMode(E), E);
7876 }
7877
7878 if (ToT == PT_IntAP)
7879 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
7880 getFPOptions(E), E);
7881 if (ToT == PT_IntAPS)
7882 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
7883 getFPOptions(E), E);
7884
7885 // Float to integral.
7886 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7887 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
7888 }
7889
7890 if (isIntegerOrBoolType(FromT) || FromT == PT_Bool) {
7891 if (ToT == PT_IntAP)
7892 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7893 if (ToT == PT_IntAPS)
7894 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7895
7896 // Integral to integral.
7897 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7898 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
7899
7900 if (ToT == PT_Float) {
7901 // Integral to floating.
7902 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7903 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
7904 }
7905 }
7906
7907 return false;
7908}
7909
7910template <class Emitter>
7911bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT,
7912 QualType ToQT, const Expr *E) {
7913 assert(FromT != ToT);
7914
7915 if (ToT == PT_IntAP)
7916 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7917 if (ToT == PT_IntAPS)
7918 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7919
7920 return this->emitCast(FromT, ToT, E);
7921}
7922
7923/// Emits __real(SubExpr)
7924template <class Emitter>
7925bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
7926 assert(SubExpr->getType()->isAnyComplexType());
7927
7928 if (DiscardResult)
7929 return this->discard(SubExpr);
7930
7931 if (!this->visit(SubExpr))
7932 return false;
7933 if (SubExpr->isLValue()) {
7934 if (!this->emitConstUint8(0, SubExpr))
7935 return false;
7936 return this->emitArrayElemPtrPopUint8(SubExpr);
7937 }
7938
7939 // Rvalue, load the actual element.
7940 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
7941 0, SubExpr);
7942}
7943
7944template <class Emitter>
7945bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
7946 assert(!DiscardResult);
7947 PrimType ElemT = classifyComplexElementType(E->getType());
7948 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
7949 // for us, that means (bool)E[0] || (bool)E[1]
7950 if (!this->emitArrayElem(ElemT, 0, E))
7951 return false;
7952 if (ElemT == PT_Float) {
7953 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7954 return false;
7955 } else {
7956 if (!this->emitCast(ElemT, PT_Bool, E))
7957 return false;
7958 }
7959
7960 // We now have the bool value of E[0] on the stack.
7961 LabelTy LabelTrue = this->getLabel();
7962 if (!this->jumpTrue(LabelTrue, E))
7963 return false;
7964
7965 if (!this->emitArrayElemPop(ElemT, 1, E))
7966 return false;
7967 if (ElemT == PT_Float) {
7968 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7969 return false;
7970 } else {
7971 if (!this->emitCast(ElemT, PT_Bool, E))
7972 return false;
7973 }
7974 // Leave the boolean value of E[1] on the stack.
7975 LabelTy EndLabel = this->getLabel();
7976 this->jump(EndLabel, E);
7977
7978 this->emitLabel(LabelTrue);
7979 if (!this->emitPopPtr(E))
7980 return false;
7981 if (!this->emitConstBool(true, E))
7982 return false;
7983
7984 this->fallthrough(EndLabel);
7985 this->emitLabel(EndLabel);
7986
7987 return true;
7988}
7989
7990template <class Emitter>
7991bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
7992 const BinaryOperator *E) {
7993 assert(E->isComparisonOp());
7994 assert(!Initializing);
7995 if (DiscardResult)
7996 return this->discard(LHS) && this->discard(RHS);
7997
7998 PrimType ElemT;
7999 bool LHSIsComplex;
8000 unsigned LHSOffset;
8001 if (LHS->getType()->isAnyComplexType()) {
8002 LHSIsComplex = true;
8003 ElemT = classifyComplexElementType(LHS->getType());
8004 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
8005 if (!this->visit(LHS))
8006 return false;
8007 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
8008 return false;
8009 } else {
8010 LHSIsComplex = false;
8011 PrimType LHST = classifyPrim(LHS->getType());
8012 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
8013 if (!this->visit(LHS))
8014 return false;
8015 if (!this->emitSetLocal(LHST, LHSOffset, E))
8016 return false;
8017 }
8018
8019 bool RHSIsComplex;
8020 unsigned RHSOffset;
8021 if (RHS->getType()->isAnyComplexType()) {
8022 RHSIsComplex = true;
8023 ElemT = classifyComplexElementType(RHS->getType());
8024 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
8025 if (!this->visit(RHS))
8026 return false;
8027 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
8028 return false;
8029 } else {
8030 RHSIsComplex = false;
8031 PrimType RHST = classifyPrim(RHS->getType());
8032 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
8033 if (!this->visit(RHS))
8034 return false;
8035 if (!this->emitSetLocal(RHST, RHSOffset, E))
8036 return false;
8037 }
8038
8039 auto getElem = [&](unsigned LocalOffset, unsigned Index,
8040 bool IsComplex) -> bool {
8041 if (IsComplex) {
8042 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
8043 return false;
8044 return this->emitArrayElemPop(ElemT, Index, E);
8045 }
8046 return this->emitGetLocal(ElemT, LocalOffset, E);
8047 };
8048
8049 for (unsigned I = 0; I != 2; ++I) {
8050 // Get both values.
8051 if (!getElem(LHSOffset, I, LHSIsComplex))
8052 return false;
8053 if (!getElem(RHSOffset, I, RHSIsComplex))
8054 return false;
8055 // And compare them.
8056 if (!this->emitEQ(ElemT, E))
8057 return false;
8058
8059 if (!this->emitCastBoolUint8(E))
8060 return false;
8061 }
8062
8063 // We now have two bool values on the stack. Compare those.
8064 if (!this->emitAddUint8(E))
8065 return false;
8066 if (!this->emitConstUint8(2, E))
8067 return false;
8068
8069 if (E->getOpcode() == BO_EQ) {
8070 if (!this->emitEQUint8(E))
8071 return false;
8072 } else if (E->getOpcode() == BO_NE) {
8073 if (!this->emitNEUint8(E))
8074 return false;
8075 } else
8076 return false;
8077
8078 // In C, this returns an int.
8079 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
8080 return this->emitCast(PT_Bool, ResT, E);
8081 return true;
8082}
8083
8084/// When calling this, we have a pointer of the local-to-destroy
8085/// on the stack.
8086/// Emit destruction of record types (or arrays of record types).
8087template <class Emitter>
8088bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R,
8089 SourceInfo Loc) {
8090 assert(R);
8091 assert(!R->hasTrivialDtor());
8092 const CXXDestructorDecl *Dtor = R->getDestructor();
8093 assert(Dtor);
8094 const Function *DtorFunc = getFunction(Dtor);
8095 if (!DtorFunc)
8096 return false;
8097 assert(DtorFunc->hasThisPointer());
8098 assert(DtorFunc->getNumParams() == 1);
8099 return this->emitCall(DtorFunc, 0, Loc);
8100}
8101/// When calling this, we have a pointer of the local-to-destroy
8102/// on the stack.
8103/// Emit destruction of record types (or arrays of record types).
8104template <class Emitter>
8105bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc,
8106 SourceInfo Loc) {
8107 assert(Desc);
8108 assert(!Desc->hasTrivialDtor());
8109
8110 // Arrays.
8111 if (Desc->isArray()) {
8112 const Descriptor *ElemDesc = Desc->ElemDesc;
8113 assert(ElemDesc);
8114
8115 unsigned N = Desc->getNumElems();
8116 if (N == 0)
8117 return this->emitPopPtr(Loc);
8118
8119 for (ssize_t I = N - 1; I >= 1; --I) {
8120 if (!this->emitConstUint64(I, Loc))
8121 return false;
8122 if (!this->emitArrayElemPtrUint64(Loc))
8123 return false;
8124 if (!this->emitDestructionPop(ElemDesc, Loc))
8125 return false;
8126 }
8127 // Last iteration, removes the instance pointer from the stack.
8128 if (!this->emitConstUint64(0, Loc))
8129 return false;
8130 if (!this->emitArrayElemPtrPopUint64(Loc))
8131 return false;
8132 return this->emitDestructionPop(ElemDesc, Loc);
8133 }
8134
8135 assert(Desc->ElemRecord);
8136 assert(!Desc->ElemRecord->hasTrivialDtor());
8137 return this->emitRecordDestructionPop(Desc->ElemRecord, Loc);
8138}
8139
8140/// Create a dummy pointer for the given decl (or expr) and
8141/// push a pointer to it on the stack.
8142template <class Emitter>
8143bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E, bool CU) {
8144 assert(!DiscardResult && "Should've been checked before");
8145 unsigned DummyID = P.getOrCreateDummy(D, CU);
8146
8147 if (!this->emitGetPtrGlobal(DummyID, E))
8148 return false;
8149 if (E->getType()->isVoidType())
8150 return true;
8151
8152 // Convert the dummy pointer to another pointer type if we have to.
8153 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
8154 if (isPtrType(PT))
8155 return this->emitDecayPtr(PT_Ptr, PT, E);
8156 return false;
8157 }
8158 return true;
8159}
8160
8161template <class Emitter>
8162bool Compiler<Emitter>::emitFloat(const APFloat &F, const Expr *E) {
8163 if (Floating::singleWord(F.getSemantics()))
8164 return this->emitConstFloat(Floating(F), E);
8165
8166 APInt I = F.bitcastToAPInt();
8167 return this->emitConstFloat(
8168 Floating(const_cast<uint64_t *>(I.getRawData()),
8169 llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
8170 E);
8171}
8172
8173// This function is constexpr if and only if To, From, and the types of
8174// all subobjects of To and From are types T such that...
8175// (3.1) - is_union_v<T> is false;
8176// (3.2) - is_pointer_v<T> is false;
8177// (3.3) - is_member_pointer_v<T> is false;
8178// (3.4) - is_volatile_v<T> is false; and
8179// (3.5) - T has no non-static data members of reference type
8180template <class Emitter>
8181bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
8182 const Expr *SubExpr = E->getSubExpr();
8183 QualType FromType = SubExpr->getType();
8184 QualType ToType = E->getType();
8185 OptPrimType ToT = classify(ToType);
8186
8187 assert(!ToType->isReferenceType());
8188
8189 // Prepare storage for the result in case we discard.
8190 if (DiscardResult && !Initializing && !ToT) {
8191 UnsignedOrNone LocalIndex = allocateLocal(E);
8192 if (!LocalIndex)
8193 return false;
8194 if (!this->emitGetPtrLocal(*LocalIndex, E))
8195 return false;
8196 }
8197
8198 // Get a pointer to the value-to-cast on the stack.
8199 // For CK_LValueToRValueBitCast, this is always an lvalue and
8200 // we later assume it to be one (i.e. a PT_Ptr). However,
8201 // we call this function for other utility methods where
8202 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
8203 if (SubExpr->isGLValue() || FromType->isVectorType()) {
8204 if (!this->visit(SubExpr))
8205 return false;
8206 } else if (OptPrimType FromT = classify(SubExpr)) {
8207 unsigned TempOffset =
8208 allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
8209 if (!this->visit(SubExpr))
8210 return false;
8211 if (!this->emitSetLocal(*FromT, TempOffset, E))
8212 return false;
8213 if (!this->emitGetPtrLocal(TempOffset, E))
8214 return false;
8215 } else {
8216 return false;
8217 }
8218
8219 if (!ToT) {
8220 if (!this->emitBitCast(E))
8221 return false;
8222 return DiscardResult ? this->emitPopPtr(E) : true;
8223 }
8224 assert(ToT);
8225
8226 const llvm::fltSemantics *TargetSemantics = nullptr;
8227 if (ToT == PT_Float)
8228 TargetSemantics = &Ctx.getFloatSemantics(ToType);
8229
8230 // Conversion to a primitive type. FromType can be another
8231 // primitive type, or a record/array.
8232 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
8233 ToType->isSpecificBuiltinType(BuiltinType::Char_U));
8234 uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
8235
8236 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
8237 ResultBitWidth, TargetSemantics,
8238 ToType.getTypePtr(), E))
8239 return false;
8240
8241 if (DiscardResult)
8242 return this->emitPop(*ToT, E);
8243
8244 return true;
8245}
8246
8247/// Replicate a scalar value into every scalar element of an aggregate.
8248/// The scalar is stored in a local at \p SrcOffset and a pointer to the
8249/// destination must be on top of the interpreter stack. Each element receives
8250/// the scalar, cast to its own type.
8251template <class Emitter>
8252bool Compiler<Emitter>::emitHLSLAggregateSplat(PrimType SrcT,
8253 unsigned SrcOffset,
8254 QualType DestType,
8255 const Expr *E) {
8256 // Vectors and matrices are treated as flat sequences of elements.
8257 unsigned NumElems = 0;
8258 QualType ElemType;
8259 if (const auto *VT = DestType->getAs<VectorType>()) {
8260 NumElems = VT->getNumElements();
8261 ElemType = VT->getElementType();
8262 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
8263 NumElems = MT->getNumElementsFlattened();
8264 ElemType = MT->getElementType();
8265 }
8266 if (NumElems > 0) {
8267 PrimType ElemT = classifyPrim(ElemType);
8268 for (unsigned I = 0; I != NumElems; ++I) {
8269 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8270 return false;
8271 if (!this->emitPrimCast(SrcT, ElemT, ElemType, E))
8272 return false;
8273 if (!this->emitInitElem(ElemT, I, E))
8274 return false;
8275 }
8276 return true;
8277 }
8278
8279 // Arrays: primitive elements are filled directly; composite elements
8280 // require recursion into each sub-aggregate.
8281 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
8282 const auto *CAT = cast<ConstantArrayType>(AT);
8283 QualType ArrElemType = CAT->getElementType();
8284 unsigned ArrSize = CAT->getZExtSize();
8285
8286 if (OptPrimType ElemT = classify(ArrElemType)) {
8287 for (unsigned I = 0; I != ArrSize; ++I) {
8288 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8289 return false;
8290 if (!this->emitPrimCast(SrcT, *ElemT, ArrElemType, E))
8291 return false;
8292 if (!this->emitInitElem(*ElemT, I, E))
8293 return false;
8294 }
8295 } else {
8296 for (unsigned I = 0; I != ArrSize; ++I) {
8297 if (!this->emitConstUint32(I, E))
8298 return false;
8299 if (!this->emitArrayElemPtrUint32(E))
8300 return false;
8301 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, ArrElemType, E))
8302 return false;
8303 if (!this->emitFinishInitPop(E))
8304 return false;
8305 }
8306 }
8307 return true;
8308 }
8309
8310 // Records: fill base classes first, then named fields in declaration
8311 // order.
8312 if (DestType->isRecordType()) {
8313 const Record *R = getRecord(DestType);
8314 if (!R)
8315 return false;
8316
8317 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8318 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8319 const Record::Base *B = R->getBase(BS.getType());
8320 assert(B);
8321 if (!this->emitGetPtrBase(B->Offset, E))
8322 return false;
8323 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, BS.getType(), E))
8324 return false;
8325 if (!this->emitFinishInitPop(E))
8326 return false;
8327 }
8328 }
8329
8330 for (const Record::Field &F : R->fields()) {
8331 if (F.isUnnamedBitField())
8332 continue;
8333
8334 QualType FieldType = F.Decl->getType();
8335 if (OptPrimType FieldT = classify(FieldType)) {
8336 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8337 return false;
8338 if (!this->emitPrimCast(SrcT, *FieldT, FieldType, E))
8339 return false;
8340 if (F.isBitField()) {
8341 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
8342 return false;
8343 } else {
8344 if (!this->emitInitField(*FieldT, F.Offset, E))
8345 return false;
8346 }
8347 } else {
8348 if (!this->emitGetPtrField(F.Offset, E))
8349 return false;
8350 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, FieldType, E))
8351 return false;
8352 if (!this->emitPopPtr(E))
8353 return false;
8354 }
8355 }
8356 return true;
8357 }
8358
8359 return false;
8360}
8361
8362/// Return the total number of scalar elements in a type. This is used
8363/// to cap how many source elements are extracted during an elementwise cast,
8364/// so we never flatten more than the destination can hold.
8365template <class Emitter>
8366unsigned Compiler<Emitter>::countHLSLFlatElements(QualType Ty) {
8367 // Vector and matrix types are treated as flat sequences of elements.
8368 if (const auto *VT = Ty->getAs<VectorType>())
8369 return VT->getNumElements();
8370 if (const auto *MT = Ty->getAs<ConstantMatrixType>())
8371 return MT->getNumElementsFlattened();
8372 // Arrays: total count is array size * scalar elements per element.
8373 if (const auto *AT = Ty->getAsArrayTypeUnsafe()) {
8374 const auto *CAT = cast<ConstantArrayType>(AT);
8375 return CAT->getZExtSize() * countHLSLFlatElements(CAT->getElementType());
8376 }
8377 // Records: sum scalar element counts of base classes and named fields.
8378 if (Ty->isRecordType()) {
8379 const Record *R = getRecord(Ty);
8380 if (!R)
8381 return 0;
8382 unsigned Count = 0;
8383 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8384 for (const CXXBaseSpecifier &BS : CXXRD->bases())
8385 Count += countHLSLFlatElements(BS.getType());
8386 }
8387 for (const Record::Field &F : R->fields()) {
8388 if (F.isUnnamedBitField())
8389 continue;
8390 Count += countHLSLFlatElements(F.Decl->getType());
8391 }
8392 return Count;
8393 }
8394 // Scalar primitive types contribute one element.
8395 if (canClassify(Ty))
8396 return 1;
8397 return 0;
8398}
8399
8400/// Walk a source aggregate and extract every scalar element into its own local
8401/// variable. The results are appended to \p Elements in declaration order,
8402/// stopping once \p MaxElements have been collected. A pointer to the
8403/// source aggregate must be stored in the local at \p SrcOffset.
8404template <class Emitter>
8405bool Compiler<Emitter>::emitHLSLFlattenAggregate(
8406 QualType SrcType, unsigned SrcOffset,
8407 SmallVectorImpl<HLSLFlatElement> &Elements, unsigned MaxElements,
8408 const Expr *E) {
8409
8410 // Save a scalar value from the stack into a new local and record it.
8411 auto saveToLocal = [&](PrimType T) -> bool {
8412 unsigned Offset = allocateLocalPrimitive(E, T, /*IsConst=*/true);
8413 if (!this->emitSetLocal(T, Offset, E))
8414 return false;
8415 Elements.push_back({Offset, T});
8416 return true;
8417 };
8418
8419 // Save a pointer from the stack into a new local for later use.
8420 auto savePtrToLocal = [&]() -> UnsignedOrNone {
8421 unsigned Offset = allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
8422 if (!this->emitSetLocal(PT_Ptr, Offset, E))
8423 return std::nullopt;
8424 return Offset;
8425 };
8426
8427 // Vectors and matrices are flat sequences of elements.
8428 unsigned NumElems = 0;
8429 QualType ElemType;
8430 if (const auto *VT = SrcType->getAs<VectorType>()) {
8431 NumElems = VT->getNumElements();
8432 ElemType = VT->getElementType();
8433 } else if (const auto *MT = SrcType->getAs<ConstantMatrixType>()) {
8434 NumElems = MT->getNumElementsFlattened();
8435 ElemType = MT->getElementType();
8436 }
8437 if (NumElems > 0) {
8438 PrimType ElemT = classifyPrim(ElemType);
8439 for (unsigned I = 0; I != NumElems && Elements.size() < MaxElements; ++I) {
8440 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8441 return false;
8442 if (!this->emitArrayElemPop(ElemT, I, E))
8443 return false;
8444 if (!saveToLocal(ElemT))
8445 return false;
8446 }
8447 return true;
8448 }
8449
8450 // Arrays: primitive elements are extracted directly; composite elements
8451 // require recursion into each sub-aggregate.
8452 if (const auto *AT = SrcType->getAsArrayTypeUnsafe()) {
8453 const auto *CAT = cast<ConstantArrayType>(AT);
8454 QualType ArrElemType = CAT->getElementType();
8455 unsigned ArrSize = CAT->getZExtSize();
8456
8457 if (OptPrimType ElemT = classify(ArrElemType)) {
8458 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
8459 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8460 return false;
8461 if (!this->emitArrayElemPop(*ElemT, I, E))
8462 return false;
8463 if (!saveToLocal(*ElemT))
8464 return false;
8465 }
8466 } else {
8467 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
8468 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8469 return false;
8470 if (!this->emitConstUint32(I, E))
8471 return false;
8472 if (!this->emitArrayElemPtrPopUint32(E))
8473 return false;
8474 UnsignedOrNone ElemPtrOffset = savePtrToLocal();
8475 if (!ElemPtrOffset)
8476 return false;
8477 if (!emitHLSLFlattenAggregate(ArrElemType, *ElemPtrOffset, Elements,
8478 MaxElements, E))
8479 return false;
8480 }
8481 }
8482 return true;
8483 }
8484
8485 // Records: base classes come first, then named fields in declaration
8486 // order.
8487 if (SrcType->isRecordType()) {
8488 const Record *R = getRecord(SrcType);
8489 if (!R)
8490 return false;
8491
8492 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8493 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8494 if (Elements.size() >= MaxElements)
8495 break;
8496 const Record::Base *B = R->getBase(BS.getType());
8497 assert(B);
8498 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8499 return false;
8500 if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, E))
8501 return false;
8502 UnsignedOrNone BasePtrOffset = savePtrToLocal();
8503 if (!BasePtrOffset)
8504 return false;
8505 if (!emitHLSLFlattenAggregate(BS.getType(), *BasePtrOffset, Elements,
8506 MaxElements, E))
8507 return false;
8508 }
8509 }
8510
8511 for (const Record::Field &F : R->fields()) {
8512 if (Elements.size() >= MaxElements)
8513 break;
8514 if (F.isUnnamedBitField())
8515 continue;
8516
8517 QualType FieldType = F.Decl->getType();
8518 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8519 return false;
8520 if (!this->emitGetPtrFieldPop(F.Offset, E))
8521 return false;
8522
8523 if (OptPrimType FieldT = classify(FieldType)) {
8524 if (!this->emitLoadPop(*FieldT, E))
8525 return false;
8526 if (!saveToLocal(*FieldT))
8527 return false;
8528 } else {
8529 UnsignedOrNone FieldPtrOffset = savePtrToLocal();
8530 if (!FieldPtrOffset)
8531 return false;
8532 if (!emitHLSLFlattenAggregate(FieldType, *FieldPtrOffset, Elements,
8533 MaxElements, E))
8534 return false;
8535 }
8536 }
8537 return true;
8538 }
8539
8540 return false;
8541}
8542
8543/// Populate an HLSL aggregate from a flat list of previously extracted source
8544/// elements, casting each to the corresponding destination element type.
8545/// \p ElemIdx tracks the current position in \p Elements and is advanced as
8546/// elements are consumed. A pointer to the destination must be on top of the
8547/// interpreter stack.
8548template <class Emitter>
8549bool Compiler<Emitter>::emitHLSLConstructAggregate(
8550 QualType DestType, ArrayRef<HLSLFlatElement> Elements, unsigned &ElemIdx,
8551 const Expr *E) {
8552
8553 // Consume the next source element, cast it, and leave it on the stack.
8554 auto loadAndCast = [&](PrimType DestT, QualType DestQT) -> bool {
8555 const auto &Src = Elements[ElemIdx++];
8556 if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E))
8557 return false;
8558 return this->emitPrimCast(Src.Type, DestT, DestQT, E);
8559 };
8560
8561 // Vectors and matrices are flat sequences of elements.
8562 unsigned NumElems = 0;
8563 QualType ElemType;
8564 if (const auto *VT = DestType->getAs<VectorType>()) {
8565 NumElems = VT->getNumElements();
8566 ElemType = VT->getElementType();
8567 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
8568 NumElems = MT->getNumElementsFlattened();
8569 ElemType = MT->getElementType();
8570 }
8571 if (NumElems > 0) {
8572 PrimType DestElemT = classifyPrim(ElemType);
8573 for (unsigned I = 0; I != NumElems; ++I) {
8574 if (!loadAndCast(DestElemT, ElemType))
8575 return false;
8576 if (!this->emitInitElem(DestElemT, I, E))
8577 return false;
8578 }
8579 return true;
8580 }
8581
8582 // Arrays: primitive elements are filled directly; composite elements
8583 // require recursion into each sub-aggregate.
8584 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
8585 const auto *CAT = cast<ConstantArrayType>(AT);
8586 QualType ArrElemType = CAT->getElementType();
8587 unsigned ArrSize = CAT->getZExtSize();
8588
8589 if (OptPrimType ElemT = classify(ArrElemType)) {
8590 for (unsigned I = 0; I != ArrSize; ++I) {
8591 if (!loadAndCast(*ElemT, ArrElemType))
8592 return false;
8593 if (!this->emitInitElem(*ElemT, I, E))
8594 return false;
8595 }
8596 } else {
8597 for (unsigned I = 0; I != ArrSize; ++I) {
8598 if (!this->emitConstUint32(I, E))
8599 return false;
8600 if (!this->emitArrayElemPtrUint32(E))
8601 return false;
8602 if (!emitHLSLConstructAggregate(ArrElemType, Elements, ElemIdx, E))
8603 return false;
8604 if (!this->emitFinishInitPop(E))
8605 return false;
8606 }
8607 }
8608 return true;
8609 }
8610
8611 // Records: base classes come first, then named fields in declaration
8612 // order.
8613 if (DestType->isRecordType()) {
8614 const Record *R = getRecord(DestType);
8615 if (!R)
8616 return false;
8617
8618 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8619 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8620 const Record::Base *B = R->getBase(BS.getType());
8621 assert(B);
8622 if (!this->emitGetPtrBase(B->Offset, E))
8623 return false;
8624 if (!emitHLSLConstructAggregate(BS.getType(), Elements, ElemIdx, E))
8625 return false;
8626 if (!this->emitFinishInitPop(E))
8627 return false;
8628 }
8629 }
8630
8631 for (const Record::Field &F : R->fields()) {
8632 if (F.isUnnamedBitField())
8633 continue;
8634
8635 QualType FieldType = F.Decl->getType();
8636 if (OptPrimType FieldT = classify(FieldType)) {
8637 if (!loadAndCast(*FieldT, FieldType))
8638 return false;
8639 if (F.isBitField()) {
8640 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
8641 return false;
8642 } else {
8643 if (!this->emitInitField(*FieldT, F.Offset, E))
8644 return false;
8645 }
8646 } else {
8647 if (!this->emitGetPtrField(F.Offset, E))
8648 return false;
8649 if (!emitHLSLConstructAggregate(FieldType, Elements, ElemIdx, E))
8650 return false;
8651 if (!this->emitPopPtr(E))
8652 return false;
8653 }
8654 }
8655 return true;
8656 }
8657
8658 return false;
8659}
8660
8661namespace clang {
8662namespace interp {
8663
8664template class Compiler<ByteCodeEmitter>;
8665template class Compiler<EvalEmitter>;
8666
8667} // namespace interp
8668} // 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:3067
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:2749
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition DeclCXX.cpp:2728
bool isLambdaStaticInvoker() const
Determine whether this is a lambda closure type's static member function that is used for the result ...
Definition DeclCXX.cpp:2893
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:1679
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:1742
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
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:3862
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