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