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, const Expr *E) {
4967 switch (Ty) {
4968 case PT_Sint8:
4969 return this->emitConstSint8(Value, E);
4970 case PT_Uint8:
4971 return this->emitConstUint8(Value, E);
4972 case PT_Sint16:
4973 return this->emitConstSint16(Value, E);
4974 case PT_Uint16:
4975 return this->emitConstUint16(Value, E);
4976 case PT_Sint32:
4977 return this->emitConstSint32(Value, E);
4978 case PT_Uint32:
4979 return this->emitConstUint32(Value, E);
4980 case PT_Sint64:
4981 return this->emitConstSint64(Value, E);
4982 case PT_Uint64:
4983 return this->emitConstUint64(Value, E);
4984 case PT_Bool:
4985 return this->emitConstBool(Value, E);
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 const Expr *E) {
5007 if (Ty == PT_IntAPS)
5008 return this->emitConstIntAPS(Value, E);
5009 if (Ty == PT_IntAP)
5010 return this->emitConstIntAP(Value, E);
5011
5012 if (Value.isSigned())
5013 return this->emitConst(Value.getSExtValue(), Ty, E);
5014 return this->emitConst(Value.getZExtValue(), Ty, E);
5015}
5016
5017template <class Emitter>
5018bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty,
5019 const Expr *E) {
5020 if (Ty == PT_IntAPS)
5021 return this->emitConstIntAPS(Value, E);
5022 if (Ty == PT_IntAP)
5023 return this->emitConstIntAP(Value, E);
5024
5025 if (isSignedType(Ty))
5026 return this->emitConst(Value.getSExtValue(), Ty, E);
5027 return this->emitConst(Value.getZExtValue(), Ty, E);
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 if (!this->visitAPValueInitializer(Value, nullptr, Ty))
5415 return false;
5416 return this->emitDestructionPop(D, VD);
5417}
5418
5419template <class Emitter>
5421 const Expr *E) {
5422 assert(!DiscardResult);
5423 if (Val.isInt())
5424 return this->emitConst(Val.getInt(), ValType, E);
5425 if (Val.isFloat()) {
5426 APFloat F = Val.getFloat();
5427 return this->emitFloat(F, E);
5428 }
5429
5430 if (Val.isMemberPointer()) {
5431 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl()) {
5432 if (!this->emitGetMemberPtr(MemberDecl, E))
5433 return false;
5434
5435 bool IsDerived = Val.isMemberPointerToDerivedMember();
5436 // Apply the member pointer path.
5437 for (const CXXRecordDecl *PathEntry : Val.getMemberPointerPath()) {
5438 if (!this->emitCopyMemberPtrPath(PathEntry, IsDerived, E))
5439 return false;
5440 }
5441
5442 return true;
5443 }
5444 return this->emitNullMemberPtr(0, nullptr, E);
5445 }
5446
5447 if (Val.isLValue()) {
5448 if (Val.isNullPointer())
5449 return this->emitNull(ValType, 0, nullptr, E);
5450
5453
5454 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
5455 return this->visit(BaseExpr);
5456 if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) {
5457 if (!this->visitDeclRef(VD, E))
5458 return false;
5459
5460 QualType EntryType = VD->getType();
5461 for (auto &Entry : Path) {
5462 if (EntryType->isArrayType()) {
5463 uint64_t Index = Entry.getAsArrayIndex();
5464 QualType ElemType =
5465 EntryType->getAsArrayTypeUnsafe()->getElementType();
5466 if (!this->emitConst(Index, PT_Uint64, E))
5467 return false;
5468 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
5469 return false;
5470 EntryType = ElemType;
5471 } else {
5472 assert(EntryType->isRecordType());
5473 const Record *EntryRecord = getRecord(EntryType);
5474 if (!EntryRecord) {
5475 assert(false);
5476
5477 return false;
5478 }
5479
5480 const Decl *BaseOrMember = Entry.getAsBaseOrMember().getPointer();
5481 if (const auto *FD = dyn_cast<FieldDecl>(BaseOrMember)) {
5482 unsigned EntryOffset = EntryRecord->getField(FD)->Offset;
5483 if (!this->emitGetPtrFieldPop(EntryOffset, E))
5484 return false;
5485 EntryType = FD->getType();
5486 } else {
5487 const auto *Base = cast<CXXRecordDecl>(BaseOrMember);
5488 unsigned BaseOffset = EntryRecord->getBase(Base)->Offset;
5489 if (!this->emitGetPtrBasePop(BaseOffset, /*NullOK=*/false, E))
5490 return false;
5491 EntryType = Ctx.getASTContext().getCanonicalTagType(Base);
5492 }
5493 }
5494 }
5495
5496 return true;
5497 }
5498 }
5499
5500 return false;
5501}
5502
5503template <class Emitter>
5505 const Expr *E, QualType T) {
5506 if (Val.isStruct()) {
5507 const Record *R = this->getRecord(T);
5508 assert(R);
5509 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
5510 const APValue &F = Val.getStructField(I);
5511 const Record::Field *RF = R->getField(I);
5512 QualType FieldType = RF->Decl->getType();
5513
5514 // Fields.
5515 if (OptPrimType PT = classify(FieldType)) {
5516 if (!this->visitAPValue(F, *PT, E))
5517 return false;
5518 if (!this->emitInitField(*PT, RF->Offset, E))
5519 return false;
5520 } else {
5521 if (!this->emitGetPtrField(RF->Offset, E))
5522 return false;
5523 if (!this->visitAPValueInitializer(F, E, FieldType))
5524 return false;
5525 if (!this->emitFinishInitPop(E))
5526 return false;
5527 }
5528 }
5529
5530 // Bases.
5531 for (unsigned I = 0, N = Val.getStructNumBases(); I != N; ++I) {
5532 const APValue &B = Val.getStructBase(I);
5533 const Record::Base *RB = R->getBase(I);
5534 QualType BaseType = Ctx.getASTContext().getCanonicalTagType(RB->Decl);
5535
5536 if (!this->emitGetPtrBase(RB->Offset, E))
5537 return false;
5538 if (!this->visitAPValueInitializer(B, E, BaseType))
5539 return false;
5540 if (!this->emitFinishInitPop(E))
5541 return false;
5542 }
5543
5544 return true;
5545 }
5546 if (Val.isUnion()) {
5547 const FieldDecl *UnionField = Val.getUnionField();
5548 if (!UnionField)
5549 return true;
5550 const Record *R = this->getRecord(T);
5551 assert(R);
5552 const APValue &F = Val.getUnionValue();
5553 const Record::Field *RF = R->getField(UnionField);
5554 QualType FieldType = RF->Decl->getType();
5555
5556 if (OptPrimType PT = classify(FieldType)) {
5557 if (!this->visitAPValue(F, *PT, E))
5558 return false;
5559 if (RF->isBitField())
5560 return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(),
5561 E);
5562 return this->emitInitFieldActivate(*PT, RF->Offset, E);
5563 }
5564
5565 if (!this->emitGetPtrField(RF->Offset, E))
5566 return false;
5567 if (!this->emitActivate(E))
5568 return false;
5569 if (!this->visitAPValueInitializer(F, E, FieldType))
5570 return false;
5571 return this->emitPopPtr(E);
5572 }
5573 if (Val.isArray()) {
5574 unsigned InitializedElems = Val.getArrayInitializedElts();
5575 const auto *ArrType = T->getAsArrayTypeUnsafe();
5576 QualType ElemType = ArrType->getElementType();
5577 OptPrimType ElemT = classify(ElemType);
5578
5579 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
5580 const APValue &Elem = A >= InitializedElems
5581 ? Val.getArrayFiller()
5582 : Val.getArrayInitializedElt(A);
5583
5584 if (ElemT) {
5585 if (!this->visitAPValue(Elem, *ElemT, E))
5586 return false;
5587 if (!this->emitInitElem(*ElemT, A, E))
5588 return false;
5589 } else {
5590 if (!this->emitConstUint32(A, E))
5591 return false;
5592 if (!this->emitArrayElemPtrUint32(E))
5593 return false;
5594 if (!this->visitAPValueInitializer(Elem, E, ElemType))
5595 return false;
5596 if (!this->emitPopPtr(E))
5597 return false;
5598 }
5599 }
5600 return true;
5601 }
5602 // TODO: Other types.
5603
5604 return false;
5605}
5606
5607template <class Emitter>
5609 unsigned BuiltinID) {
5610 if (BuiltinID == Builtin::BI__builtin_constant_p) {
5611 // Void argument is always invalid and harder to handle later.
5612 if (E->getArg(0)->getType()->isVoidType()) {
5613 if (DiscardResult)
5614 return true;
5615 return this->emitConst(0, E);
5616 }
5617
5618 if (!this->emitStartSpeculation(E))
5619 return false;
5620 LabelTy EndLabel = this->getLabel();
5621 if (!this->speculate(E, EndLabel))
5622 return false;
5623 if (!this->emitEndSpeculation(E))
5624 return false;
5625 this->fallthrough(EndLabel);
5626 if (DiscardResult)
5627 return this->emitPop(classifyPrim(E), E);
5628 return true;
5629 }
5630
5631 // For these, we're expected to ultimately return an APValue pointing
5632 // to the CallExpr. This is needed to get the correct codegen.
5633 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
5634 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
5635 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
5636 BuiltinID == Builtin::BI__builtin_function_start) {
5637 if (DiscardResult)
5638 return true;
5639 return this->emitDummyPtr(E, E);
5640 }
5641
5643 OptPrimType ReturnT = classify(E);
5644
5645 // Non-primitive return type. Prepare storage.
5646 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
5647 UnsignedOrNone LocalIndex = allocateLocal(E);
5648 if (!LocalIndex)
5649 return false;
5650 if (!this->emitGetPtrLocal(*LocalIndex, E))
5651 return false;
5652 }
5653
5654 // Prepare function arguments including special cases.
5655 switch (BuiltinID) {
5656 case Builtin::BI__builtin_object_size:
5657 case Builtin::BI__builtin_dynamic_object_size: {
5658 assert(E->getNumArgs() == 2);
5659 const Expr *Arg0 = E->getArg(0);
5660 if (Arg0->isGLValue()) {
5661 if (!this->visit(Arg0))
5662 return false;
5663
5664 } else {
5665 if (!this->visitAsLValue(Arg0))
5666 return false;
5667 }
5668 if (!this->visit(E->getArg(1)))
5669 return false;
5670
5671 } break;
5672 case Builtin::BI__assume:
5673 case Builtin::BI__builtin_assume:
5674 // Argument is not evaluated.
5675 break;
5676 case Builtin::BI__atomic_is_lock_free:
5677 case Builtin::BI__atomic_always_lock_free: {
5678 assert(E->getNumArgs() == 2);
5679 if (!this->visit(E->getArg(0)))
5680 return false;
5681 if (!this->visitAsLValue(E->getArg(1)))
5682 return false;
5683 } break;
5684
5685 default:
5686 if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
5687 // Put arguments on the stack.
5688 for (const auto *Arg : E->arguments()) {
5689 if (!this->visit(Arg))
5690 return false;
5691 }
5692 }
5693 }
5694
5695 if (!this->emitCallBI(E, BuiltinID, E))
5696 return false;
5697
5698 if (DiscardResult && !ReturnType->isVoidType())
5699 return this->emitPop(ReturnT.value_or(PT_Ptr), E);
5700
5701 return true;
5702}
5703
5704template <class Emitter>
5706 if (E->containsErrors())
5707 return false;
5708 const FunctionDecl *FuncDecl = E->getDirectCallee();
5709
5710 if (FuncDecl) {
5711 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
5712 return VisitBuiltinCallExpr(E, BuiltinID);
5713
5714 // Calls to replaceable operator new/operator delete.
5716 if (FuncDecl->getDeclName().isAnyOperatorNew())
5717 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
5718 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete ||
5719 FuncDecl->getDeclName().getCXXOverloadedOperator() ==
5720 OO_Array_Delete);
5721 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
5722 }
5723
5724 // Explicit calls to trivial destructors
5725 if (const auto *DD = dyn_cast<CXXDestructorDecl>(FuncDecl);
5726 DD && DD->isTrivial()) {
5727 const auto *MemberCall = cast<CXXMemberCallExpr>(E);
5728 if (!this->visit(MemberCall->getImplicitObjectArgument()))
5729 return false;
5730 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
5731 this->emitPopPtr(E);
5732 }
5733 }
5734
5735 LocalScope<Emitter> CallScope(this, ScopeKind::Call);
5736
5737 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
5739 bool HasRVO = !ReturnType->isVoidType() && !T;
5740
5741 if (HasRVO) {
5742 if (DiscardResult) {
5743 // If we need to discard the return value but the function returns its
5744 // value via an RVO pointer, we need to create one such pointer just
5745 // for this call.
5746 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5747 if (!this->emitGetPtrLocal(*LocalIndex, E))
5748 return false;
5749 }
5750 } else {
5751 // We need the result. Prepare a pointer to return or
5752 // dup the current one.
5753 if (!Initializing) {
5754 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5755 if (!this->emitGetPtrLocal(*LocalIndex, E))
5756 return false;
5757 }
5758 }
5759 if (!this->emitDupPtr(E))
5760 return false;
5761 }
5762 }
5763
5765
5766 bool IsAssignmentOperatorCall = false;
5767 bool ActivateLHS = false;
5768 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
5769 OCE && OCE->isAssignmentOp()) {
5770 // Just like with regular assignments, we need to special-case assignment
5771 // operators here and evaluate the RHS (the second arg) before the LHS (the
5772 // first arg). We fix this by using a Flip op later.
5773 assert(Args.size() == 2);
5774 const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl();
5775 ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor();
5776 IsAssignmentOperatorCall = true;
5777 std::reverse(Args.begin(), Args.end());
5778 }
5779 // Calling a static operator will still
5780 // pass the instance, but we don't need it.
5781 // Discard it here.
5782 if (isa<CXXOperatorCallExpr>(E)) {
5783 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
5784 MD && MD->isStatic()) {
5785 if (!this->discard(E->getArg(0)))
5786 return false;
5787 // Drop first arg.
5788 Args.erase(Args.begin());
5789 }
5790 }
5791
5792 bool Devirtualized = false;
5793 UnsignedOrNone CalleeOffset = std::nullopt;
5794 // Add the (optional, implicit) This pointer.
5795 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
5796 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
5797 // If we end up creating a CallPtr op for this, we need the base of the
5798 // member pointer as the instance pointer, and later extract the function
5799 // decl as the function pointer.
5800 const Expr *Callee = E->getCallee();
5801 CalleeOffset =
5802 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
5803 if (!this->visit(Callee))
5804 return false;
5805 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
5806 return false;
5807 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5808 return false;
5809 if (!this->emitGetMemberPtrBase(E))
5810 return false;
5811 } else {
5812 const auto *InstancePtr = MC->getImplicitObjectArgument();
5813 if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
5814 isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
5815 const auto *Stripped = stripCheckedDerivedToBaseCasts(InstancePtr);
5816 if (isa<CXXThisExpr>(Stripped)) {
5817 FuncDecl =
5818 cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
5819 Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5820 Devirtualized = true;
5821 if (!this->visit(Stripped))
5822 return false;
5823 } else {
5824 if (!this->visit(InstancePtr))
5825 return false;
5826 }
5827 } else {
5828 if (!this->visit(InstancePtr))
5829 return false;
5830 }
5831 }
5832 } else if (const auto *PD =
5833 dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
5834 if (!this->emitCheckPseudoDtor(E))
5835 return false;
5836 const Expr *Base = PD->getBase();
5837 // E.g. `using T = int; 0.~T();`.
5838 if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr)
5839 return this->discard(Base);
5840 if (!this->visit(Base))
5841 return false;
5842 return this->emitEndLifetimePop(E);
5843 } else if (!FuncDecl) {
5844 const Expr *Callee = E->getCallee();
5845 CalleeOffset =
5846 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
5847 if (!this->visit(Callee))
5848 return false;
5849 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
5850 return false;
5851 }
5852
5853 if (!this->visitCallArgs(Args, FuncDecl, ActivateLHS,
5855 return false;
5856
5857 // Undo the argument reversal we did earlier.
5858 if (IsAssignmentOperatorCall) {
5859 assert(Args.size() == 2);
5860 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
5861 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
5862 if (!this->emitFlip(Arg2T, Arg1T, E))
5863 return false;
5864 }
5865
5866 if (FuncDecl) {
5867 const Function *Func = getFunction(FuncDecl);
5868 if (!Func)
5869 return false;
5870
5871 // In error cases, the function may be called with fewer arguments than
5872 // parameters.
5873 if (E->getNumArgs() < Func->getNumWrittenParams())
5874 return false;
5875
5876 assert(HasRVO == Func->hasRVO());
5877
5878 bool HasQualifier = false;
5879 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
5880 HasQualifier = ME->hasQualifier();
5881
5882 bool IsVirtual = false;
5883 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
5884 IsVirtual = !Devirtualized && MD->isVirtual();
5885
5886 // In any case call the function. The return value will end up on the stack
5887 // and if the function has RVO, we already have the pointer on the stack to
5888 // write the result into.
5889 if (IsVirtual && !HasQualifier) {
5890 uint32_t VarArgSize = 0;
5891 unsigned NumParams =
5892 Func->getNumWrittenParams() +
5893 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisParam());
5894 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5895 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5896
5897 if (!this->emitCallVirt(Func, VarArgSize, E))
5898 return false;
5899 } else if (Func->isVariadic()) {
5900 uint32_t VarArgSize = 0;
5901 unsigned NumParams =
5902 Func->getNumWrittenParams() +
5903 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisParam());
5904 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5905 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5906 if (!this->emitCallVar(Func, VarArgSize, E))
5907 return false;
5908 } else {
5909 if (!this->emitCall(Func, 0, E))
5910 return false;
5911 }
5912 } else {
5913 // Indirect call. Visit the callee, which will leave a FunctionPointer on
5914 // the stack. Cleanup of the returned value if necessary will be done after
5915 // the function call completed.
5916
5917 // Sum the size of all args from the call expr.
5918 uint32_t ArgSize = 0;
5919 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
5920 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5921
5922 // Get the callee, either from a member pointer or function pointer saved in
5923 // CalleeOffset.
5924 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
5925 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5926 return false;
5927 if (!this->emitGetMemberPtrDecl(E))
5928 return false;
5929 } else {
5930 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
5931 return false;
5932 }
5933 if (!this->emitCallPtr(ArgSize, E, E))
5934 return false;
5935 }
5936
5937 // Cleanup for discarded return values.
5938 if (DiscardResult && !ReturnType->isVoidType() && T)
5939 return this->emitPop(*T, E) && CallScope.destroyLocals();
5940
5941 return CallScope.destroyLocals();
5942}
5943
5944template <class Emitter>
5946 SourceLocScope<Emitter> SLS(this, E);
5947
5948 return this->delegate(E->getExpr());
5949}
5950
5951template <class Emitter>
5953 SourceLocScope<Emitter> SLS(this, E);
5954
5955 return this->delegate(E->getExpr());
5956}
5957
5958template <class Emitter>
5960 if (DiscardResult)
5961 return true;
5962
5963 return this->emitConstBool(E->getValue(), E);
5964}
5965
5966template <class Emitter>
5968 const CXXNullPtrLiteralExpr *E) {
5969 if (DiscardResult)
5970 return true;
5971
5972 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
5973 return this->emitNullPtr(Val, nullptr, E);
5974}
5975
5976template <class Emitter>
5978 if (DiscardResult)
5979 return true;
5980
5981 assert(E->getType()->isIntegerType());
5982
5983 PrimType T = classifyPrim(E->getType());
5984 return this->emitZero(T, E);
5985}
5986
5987template <class Emitter>
5989 if (DiscardResult)
5990 return true;
5991
5992 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
5993 if (this->LambdaThisCapture.Offset > 0) {
5994 if (this->LambdaThisCapture.IsPtr)
5995 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
5996 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
5997 }
5998 }
5999
6000 // In some circumstances, the 'this' pointer does not actually refer to the
6001 // instance pointer of the current function frame, but e.g. to the declaration
6002 // currently being initialized. Here we emit the necessary instruction(s) for
6003 // this scenario.
6004 if (!InitStackActive || InitStack.empty())
6005 return this->emitThis(E);
6006
6007 // If our init stack is, for example:
6008 // 0 Stack: 3 (decl)
6009 // 1 Stack: 6 (init list)
6010 // 2 Stack: 1 (field)
6011 // 3 Stack: 6 (init list)
6012 // 4 Stack: 1 (field)
6013 //
6014 // We want to find the LAST element in it that's an init list,
6015 // which is marked with the K_InitList marker. The index right
6016 // before that points to an init list. We need to find the
6017 // elements before the K_InitList element that point to a base
6018 // (e.g. a decl or This), optionally followed by field, elem, etc.
6019 // In the example above, we want to emit elements [0..2].
6020 unsigned StartIndex = 0;
6021 unsigned EndIndex = 0;
6022 // Find the init list.
6023 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
6024 if (InitStack[StartIndex].Kind == InitLink::K_DIE) {
6025 EndIndex = StartIndex;
6026 --StartIndex;
6027 break;
6028 }
6029 }
6030
6031 // Walk backwards to find the base.
6032 for (; StartIndex > 0; --StartIndex) {
6033 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
6034 continue;
6035
6036 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
6037 InitStack[StartIndex].Kind != InitLink::K_Elem &&
6038 InitStack[StartIndex].Kind != InitLink::K_DIE)
6039 break;
6040 }
6041
6042 if (StartIndex == 0 && EndIndex == 0)
6043 EndIndex = InitStack.size() - 1;
6044
6045 assert(StartIndex < EndIndex);
6046
6047 // Emit the instructions.
6048 for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
6049 if (InitStack[I].Kind == InitLink::K_InitList ||
6050 InitStack[I].Kind == InitLink::K_DIE)
6051 continue;
6052 if (!InitStack[I].template emit<Emitter>(this, E))
6053 return false;
6054 }
6055 return true;
6056}
6057
6058template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
6059 switch (S->getStmtClass()) {
6060 case Stmt::CompoundStmtClass:
6062 case Stmt::DeclStmtClass:
6063 return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
6064 case Stmt::ReturnStmtClass:
6066 case Stmt::IfStmtClass:
6067 return visitIfStmt(cast<IfStmt>(S));
6068 case Stmt::WhileStmtClass:
6070 case Stmt::DoStmtClass:
6071 return visitDoStmt(cast<DoStmt>(S));
6072 case Stmt::ForStmtClass:
6073 return visitForStmt(cast<ForStmt>(S));
6074 case Stmt::CXXForRangeStmtClass:
6076 case Stmt::BreakStmtClass:
6078 case Stmt::ContinueStmtClass:
6080 case Stmt::SwitchStmtClass:
6082 case Stmt::CaseStmtClass:
6083 return visitCaseStmt(cast<CaseStmt>(S));
6084 case Stmt::DefaultStmtClass:
6086 case Stmt::AttributedStmtClass:
6088 case Stmt::CXXTryStmtClass:
6090 case Stmt::NullStmtClass:
6091 return true;
6092 // Always invalid statements.
6093 case Stmt::GCCAsmStmtClass:
6094 case Stmt::MSAsmStmtClass:
6095 case Stmt::GotoStmtClass:
6096 return this->emitInvalid(S);
6097 case Stmt::LabelStmtClass:
6098 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
6099 default: {
6100 if (const auto *E = dyn_cast<Expr>(S))
6101 return this->discard(E);
6102 return false;
6103 }
6104 }
6105}
6106
6107template <class Emitter>
6110 for (const auto *InnerStmt : S->body())
6111 if (!visitStmt(InnerStmt))
6112 return false;
6113 return Scope.destroyLocals();
6114}
6115
6116template <class Emitter>
6117bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
6118 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
6119 for (auto *BD : DD->flat_bindings())
6120 if (auto *KD = BD->getHoldingVar();
6121 KD && !this->visitVarDecl(KD, KD->getInit()))
6122 return false;
6123 }
6124 return true;
6125}
6126
6128 assert(FD);
6129 assert(FD->getParent()->isUnion());
6130 const CXXRecordDecl *CXXRD =
6132 return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
6133}
6134
6135template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
6136 for (;;) {
6137 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
6138 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
6139 FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
6140 return true;
6141 E = ME->getBase();
6142 continue;
6143 }
6144
6145 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
6146 E = ASE->getBase()->IgnoreImplicit();
6147 continue;
6148 }
6149
6150 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
6151 ICE && (ICE->getCastKind() == CK_NoOp ||
6152 ICE->getCastKind() == CK_DerivedToBase ||
6153 ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
6154 E = ICE->getSubExpr();
6155 continue;
6156 }
6157
6158 if (const auto *This = dyn_cast<CXXThisExpr>(E)) {
6159 const auto *ThisRecord =
6160 This->getType()->getPointeeType()->getAsRecordDecl();
6161 if (!ThisRecord->isUnion())
6162 return false;
6163 // Otherwise, always activate if we're in the ctor.
6164 if (const auto *Ctor =
6165 dyn_cast_if_present<CXXConstructorDecl>(CompilingFunction))
6166 return Ctor->getParent() == ThisRecord;
6167 return false;
6168 }
6169
6170 break;
6171 }
6172 return false;
6173}
6174
6175template <class Emitter>
6177 bool EvaluateConditionDecl) {
6178 for (const auto *D : DS->decls()) {
6181 continue;
6182
6183 const auto *VD = dyn_cast<VarDecl>(D);
6184 if (!VD)
6185 return false;
6186 if (!this->visitVarDecl(VD, VD->getInit()))
6187 return false;
6188
6189 // Register decomposition decl holding vars.
6190 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
6191 return false;
6192 }
6193
6194 return true;
6195}
6196
6197template <class Emitter>
6199 if (this->InStmtExpr)
6200 return this->emitUnsupported(RS);
6201
6202 if (const Expr *RE = RS->getRetValue()) {
6203 LocalScope<Emitter> RetScope(this);
6204 if (ReturnType) {
6205 // Primitive types are simply returned.
6206 if (!this->visit(RE))
6207 return false;
6208 this->emitCleanup();
6209 return this->emitRet(*ReturnType, RS);
6210 }
6211
6212 if (RE->getType()->isVoidType()) {
6213 if (!this->visit(RE))
6214 return false;
6215 } else {
6216 if (RE->containsErrors())
6217 return false;
6218
6220 // RVO - construct the value in the return location.
6221 if (!this->emitRVOPtr(RE))
6222 return false;
6223 if (!this->visitInitializerPop(RE))
6224 return false;
6225
6226 this->emitCleanup();
6227 return this->emitRetVoid(RS);
6228 }
6229 }
6230
6231 // Void return.
6232 this->emitCleanup();
6233 return this->emitRetVoid(RS);
6234}
6235
6236template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
6237 LocalScope<Emitter> IfScope(this);
6238
6239 auto visitChildStmt = [&](const Stmt *S) -> bool {
6240 LocalScope<Emitter> SScope(this);
6241 if (!visitStmt(S))
6242 return false;
6243 return SScope.destroyLocals();
6244 };
6245
6246 if (auto *CondInit = IS->getInit()) {
6247 if (!visitStmt(CondInit))
6248 return false;
6249 }
6250
6251 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) {
6252 if (!visitDeclStmt(CondDecl))
6253 return false;
6254 }
6255
6256 // Save ourselves compiling some code and the jumps, etc. if the condition is
6257 // stataically known to be either true or false. We could look at more cases
6258 // here, but I think all the ones that actually happen are using a
6259 // ConstantExpr.
6260 if (std::optional<bool> BoolValue = getBoolValue(IS->getCond())) {
6261 if (*BoolValue)
6262 return visitChildStmt(IS->getThen());
6263 if (const Stmt *Else = IS->getElse())
6264 return visitChildStmt(Else);
6265 return true;
6266 }
6267
6268 // Otherwise, compile the condition.
6269 if (IS->isNonNegatedConsteval()) {
6270 if (!this->emitIsConstantContext(IS))
6271 return false;
6272 } else if (IS->isNegatedConsteval()) {
6273 if (!this->emitIsConstantContext(IS))
6274 return false;
6275 if (!this->emitInv(IS))
6276 return false;
6277 } else {
6279 if (!this->visitBool(IS->getCond()))
6280 return false;
6281 if (!CondScope.destroyLocals())
6282 return false;
6283 }
6284
6285 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
6286 return false;
6287
6288 if (const Stmt *Else = IS->getElse()) {
6289 LabelTy LabelElse = this->getLabel();
6290 LabelTy LabelEnd = this->getLabel();
6291 if (!this->jumpFalse(LabelElse, IS))
6292 return false;
6293 if (!visitChildStmt(IS->getThen()))
6294 return false;
6295 if (!this->jump(LabelEnd, IS))
6296 return false;
6297 this->emitLabel(LabelElse);
6298 if (!visitChildStmt(Else))
6299 return false;
6300 this->emitLabel(LabelEnd);
6301 } else {
6302 LabelTy LabelEnd = this->getLabel();
6303 if (!this->jumpFalse(LabelEnd, IS))
6304 return false;
6305 if (!visitChildStmt(IS->getThen()))
6306 return false;
6307 this->emitLabel(LabelEnd);
6308 }
6309
6310 if (!IfScope.destroyLocals())
6311 return false;
6312
6313 return true;
6314}
6315
6316template <class Emitter>
6318 const Expr *Cond = S->getCond();
6319 const Stmt *Body = S->getBody();
6320
6321 LabelTy CondLabel = this->getLabel(); // Label before the condition.
6322 LabelTy EndLabel = this->getLabel(); // Label after the loop.
6323 LocalScope<Emitter> WholeLoopScope(this);
6324 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6325
6326 this->fallthrough(CondLabel);
6327 this->emitLabel(CondLabel);
6328
6329 // Start of the loop body {
6330 LocalScope<Emitter> CondScope(this);
6331
6332 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6333 if (!visitDeclStmt(CondDecl))
6334 return false;
6335 }
6336
6337 if (!this->visitBool(Cond))
6338 return false;
6339
6340 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6341 return false;
6342
6343 if (!this->jumpFalse(EndLabel, S))
6344 return false;
6345
6346 if (!this->visitStmt(Body))
6347 return false;
6348
6349 if (!CondScope.destroyLocals())
6350 return false;
6351 // } End of loop body.
6352
6353 if (!this->jump(CondLabel, S))
6354 return false;
6355 this->fallthrough(EndLabel);
6356 this->emitLabel(EndLabel);
6357
6358 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6359}
6360
6361template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
6362 const Expr *Cond = S->getCond();
6363 const Stmt *Body = S->getBody();
6364
6365 LabelTy StartLabel = this->getLabel();
6366 LabelTy EndLabel = this->getLabel();
6367 LabelTy CondLabel = this->getLabel();
6368 LocalScope<Emitter> WholeLoopScope(this);
6369 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6370
6371 this->fallthrough(StartLabel);
6372 this->emitLabel(StartLabel);
6373
6374 {
6375 LocalScope<Emitter> CondScope(this);
6376 if (!this->visitStmt(Body))
6377 return false;
6378 this->fallthrough(CondLabel);
6379 this->emitLabel(CondLabel);
6380 if (!this->visitBool(Cond))
6381 return false;
6382
6383 if (!CondScope.destroyLocals())
6384 return false;
6385 }
6386 if (!this->jumpTrue(StartLabel, S))
6387 return false;
6388
6389 this->fallthrough(EndLabel);
6390 this->emitLabel(EndLabel);
6391 return WholeLoopScope.destroyLocals();
6392}
6393
6394template <class Emitter>
6396 // for (Init; Cond; Inc) { Body }
6397 const Stmt *Init = S->getInit();
6398 const Expr *Cond = S->getCond();
6399 const Expr *Inc = S->getInc();
6400 const Stmt *Body = S->getBody();
6401
6402 LabelTy EndLabel = this->getLabel();
6403 LabelTy CondLabel = this->getLabel();
6404 LabelTy IncLabel = this->getLabel();
6405
6406 LocalScope<Emitter> WholeLoopScope(this);
6407 if (Init && !this->visitStmt(Init))
6408 return false;
6409
6410 // Start of the loop body {
6411 this->fallthrough(CondLabel);
6412 this->emitLabel(CondLabel);
6413
6414 LocalScope<Emitter> CondScope(this);
6415 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6416 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6417 if (!visitDeclStmt(CondDecl))
6418 return false;
6419 }
6420
6421 if (Cond) {
6422 if (!this->visitBool(Cond))
6423 return false;
6424 if (!this->jumpFalse(EndLabel, S))
6425 return false;
6426 }
6427 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6428 return false;
6429
6430 if (Body && !this->visitStmt(Body))
6431 return false;
6432
6433 this->fallthrough(IncLabel);
6434 this->emitLabel(IncLabel);
6435 if (Inc && !this->discard(Inc))
6436 return false;
6437
6438 if (!CondScope.destroyLocals())
6439 return false;
6440 if (!this->jump(CondLabel, S))
6441 return false;
6442 // } End of loop body.
6443
6444 this->emitLabel(EndLabel);
6445 // If we jumped out of the loop above, we still need to clean up the condition
6446 // scope.
6447 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6448}
6449
6450template <class Emitter>
6452 const Stmt *Init = S->getInit();
6453 const Expr *Cond = S->getCond();
6454 const Expr *Inc = S->getInc();
6455 const Stmt *Body = S->getBody();
6456 const Stmt *BeginStmt = S->getBeginStmt();
6457 const Stmt *RangeStmt = S->getRangeStmt();
6458 const Stmt *EndStmt = S->getEndStmt();
6459
6460 LabelTy EndLabel = this->getLabel();
6461 LabelTy CondLabel = this->getLabel();
6462 LabelTy IncLabel = this->getLabel();
6463 LocalScope<Emitter> WholeLoopScope(this);
6464 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6465
6466 // Emit declarations needed in the loop.
6467 if (Init && !this->visitStmt(Init))
6468 return false;
6469 if (!this->visitStmt(RangeStmt))
6470 return false;
6471 if (!this->visitStmt(BeginStmt))
6472 return false;
6473 if (!this->visitStmt(EndStmt))
6474 return false;
6475
6476 // Now the condition as well as the loop variable assignment.
6477 this->fallthrough(CondLabel);
6478 this->emitLabel(CondLabel);
6479 if (!this->visitBool(Cond))
6480 return false;
6481 if (!this->jumpFalse(EndLabel, S))
6482 return false;
6483
6484 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
6485 return false;
6486
6487 // Body.
6488 {
6489 if (!this->visitStmt(Body))
6490 return false;
6491
6492 this->fallthrough(IncLabel);
6493 this->emitLabel(IncLabel);
6494 if (!this->discard(Inc))
6495 return false;
6496 }
6497
6498 if (!this->jump(CondLabel, S))
6499 return false;
6500
6501 this->fallthrough(EndLabel);
6502 this->emitLabel(EndLabel);
6503 return WholeLoopScope.destroyLocals();
6504}
6505
6506template <class Emitter>
6508 if (LabelInfoStack.empty())
6509 return false;
6510
6511 OptLabelTy TargetLabel = std::nullopt;
6512 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6513 const VariableScope<Emitter> *BreakScope = nullptr;
6514
6515 if (!TargetLoop) {
6516 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6517 if (LI.BreakLabel) {
6518 TargetLabel = *LI.BreakLabel;
6519 BreakScope = LI.BreakOrContinueScope;
6520 break;
6521 }
6522 }
6523 } else {
6524 for (auto LI : LabelInfoStack) {
6525 if (LI.Name == TargetLoop) {
6526 TargetLabel = *LI.BreakLabel;
6527 BreakScope = LI.BreakOrContinueScope;
6528 break;
6529 }
6530 }
6531 }
6532
6533 // Faulty break statement (e.g. label redefined or named loops disabled).
6534 if (!TargetLabel)
6535 return false;
6536
6537 for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope;
6538 C = C->getParent()) {
6539 if (!C->destroyLocals())
6540 return false;
6541 }
6542
6543 return this->jump(*TargetLabel, S);
6544}
6545
6546template <class Emitter>
6548 if (LabelInfoStack.empty())
6549 return false;
6550
6551 OptLabelTy TargetLabel = std::nullopt;
6552 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6553 const VariableScope<Emitter> *ContinueScope = nullptr;
6554
6555 if (!TargetLoop) {
6556 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6557 if (LI.ContinueLabel) {
6558 TargetLabel = *LI.ContinueLabel;
6559 ContinueScope = LI.BreakOrContinueScope;
6560 break;
6561 }
6562 }
6563 } else {
6564 for (auto LI : LabelInfoStack) {
6565 if (LI.Name == TargetLoop) {
6566 TargetLabel = *LI.ContinueLabel;
6567 ContinueScope = LI.BreakOrContinueScope;
6568 break;
6569 }
6570 }
6571 }
6572 assert(TargetLabel);
6573
6574 for (VariableScope<Emitter> *C = VarScope; C != ContinueScope;
6575 C = C->getParent()) {
6576 if (!C->destroyLocals())
6577 return false;
6578 }
6579
6580 return this->jump(*TargetLabel, S);
6581}
6582
6583template <class Emitter>
6585 const Expr *Cond = S->getCond();
6586 if (Cond->containsErrors())
6587 return false;
6588
6589 PrimType CondT = this->classifyPrim(Cond->getType());
6590 LocalScope<Emitter> LS(this);
6591 llvm::SaveAndRestore StmtExprSAR(this->SwitchInStmtExpr, this->InStmtExpr);
6592
6593 LabelTy EndLabel = this->getLabel();
6594 UnsignedOrNone DefaultLabel = std::nullopt;
6595 unsigned CondVar =
6596 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
6597
6598 if (const auto *CondInit = S->getInit())
6599 if (!visitStmt(CondInit))
6600 return false;
6601
6602 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
6603 if (!visitDeclStmt(CondDecl))
6604 return false;
6605
6606 // Initialize condition variable.
6607 if (!this->visit(Cond))
6608 return false;
6609 if (!this->emitSetLocal(CondT, CondVar, S))
6610 return false;
6611
6612 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6613 return false;
6614
6616 // Create labels and comparison ops for all case statements.
6617 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
6618 SC = SC->getNextSwitchCase()) {
6619 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
6620 CaseLabels[SC] = this->getLabel();
6621
6622 if (CS->caseStmtIsGNURange()) {
6623 LabelTy EndOfRangeCheck = this->getLabel();
6624 const Expr *Low = CS->getLHS();
6625 const Expr *High = CS->getRHS();
6626 if (Low->isValueDependent() || High->isValueDependent())
6627 return false;
6628
6629 if (!this->emitGetLocal(CondT, CondVar, CS))
6630 return false;
6631 if (!this->visit(Low))
6632 return false;
6633 PrimType LT = this->classifyPrim(Low->getType());
6634 if (!this->emitGE(LT, S))
6635 return false;
6636 if (!this->jumpFalse(EndOfRangeCheck, S))
6637 return false;
6638
6639 if (!this->emitGetLocal(CondT, CondVar, CS))
6640 return false;
6641 if (!this->visit(High))
6642 return false;
6643 PrimType HT = this->classifyPrim(High->getType());
6644 if (!this->emitLE(HT, S))
6645 return false;
6646 if (!this->jumpTrue(CaseLabels[CS], S))
6647 return false;
6648 this->emitLabel(EndOfRangeCheck);
6649 continue;
6650 }
6651
6652 const Expr *Value = CS->getLHS();
6653 if (Value->isValueDependent())
6654 return false;
6655 PrimType ValueT = this->classifyPrim(Value->getType());
6656
6657 // Compare the case statement's value to the switch condition.
6658 if (!this->emitGetLocal(CondT, CondVar, CS))
6659 return false;
6660 if (!this->visit(Value))
6661 return false;
6662
6663 // Compare and jump to the case label.
6664 if (!this->emitEQ(ValueT, S))
6665 return false;
6666 if (!this->jumpTrue(CaseLabels[CS], S))
6667 return false;
6668 } else {
6669 assert(!DefaultLabel);
6670 DefaultLabel = this->getLabel();
6671 }
6672 }
6673
6674 // If none of the conditions above were true, fall through to the default
6675 // statement or jump after the switch statement.
6676 if (DefaultLabel) {
6677 if (!this->jump(*DefaultLabel, S))
6678 return false;
6679 } else {
6680 if (!this->jump(EndLabel, S))
6681 return false;
6682 }
6683
6684 SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel,
6685 DefaultLabel);
6686 if (!this->visitStmt(S->getBody()))
6687 return false;
6688 this->fallthrough(EndLabel);
6689 this->emitLabel(EndLabel);
6690
6691 return LS.destroyLocals();
6692}
6693
6694template <class Emitter>
6696 this->fallthrough(CaseLabels[S]);
6697 this->emitLabel(CaseLabels[S]);
6698
6699 // We can't jump from an outer switch statement to a case label
6700 // that's inside a StmtExpr.
6701 if (this->InStmtExpr && !this->SwitchInStmtExpr)
6702 return this->emitUnsupported(S);
6703
6704 return this->visitStmt(S->getSubStmt());
6705}
6706
6707template <class Emitter>
6709 if (LabelInfoStack.empty())
6710 return false;
6711
6712 LabelTy DefaultLabel;
6713 for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) {
6714 if (LI.DefaultLabel) {
6715 DefaultLabel = *LI.DefaultLabel;
6716 break;
6717 }
6718 }
6719
6720 this->emitLabel(DefaultLabel);
6721 return this->visitStmt(S->getSubStmt());
6722}
6723
6724template <class Emitter>
6726 const Stmt *SubStmt = S->getSubStmt();
6727
6728 bool IsMSVCConstexprAttr = isa<ReturnStmt>(SubStmt) &&
6730
6731 if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S))
6732 return false;
6733
6734 if (this->Ctx.getLangOpts().CXXAssumptions &&
6735 !this->Ctx.getLangOpts().MSVCCompat) {
6736 for (const Attr *A : S->getAttrs()) {
6737 auto *AA = dyn_cast<CXXAssumeAttr>(A);
6738 if (!AA)
6739 continue;
6740
6741 assert(isa<NullStmt>(SubStmt));
6742
6743 const Expr *Assumption = AA->getAssumption();
6744 if (Assumption->isValueDependent())
6745 return false;
6746
6747 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
6748 continue;
6749
6750 // Evaluate assumption.
6751 if (!this->visitBool(Assumption))
6752 return false;
6753
6754 if (!this->emitAssume(Assumption))
6755 return false;
6756 }
6757 }
6758
6759 // Ignore other attributes.
6760 if (!this->visitStmt(SubStmt))
6761 return false;
6762
6763 if (IsMSVCConstexprAttr)
6764 return this->emitPopMSVCCE(S);
6765 return true;
6766}
6767
6768template <class Emitter>
6770 // Ignore all handlers.
6771 return this->visitStmt(S->getTryBlock());
6772}
6773
6774template <class Emitter>
6775bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
6776 assert(MD->isLambdaStaticInvoker());
6777 assert(MD->hasBody());
6778 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
6779
6780 const CXXRecordDecl *ClosureClass = MD->getParent();
6781 const FunctionDecl *LambdaCallOp;
6782 assert(ClosureClass->captures().empty());
6783 if (ClosureClass->isGenericLambda()) {
6784 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6785 assert(MD->isFunctionTemplateSpecialization() &&
6786 "A generic lambda's static-invoker function must be a "
6787 "template specialization");
6789 FunctionTemplateDecl *CallOpTemplate =
6790 LambdaCallOp->getDescribedFunctionTemplate();
6791 void *InsertPos = nullptr;
6792 const FunctionDecl *CorrespondingCallOpSpecialization =
6793 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
6794 assert(CorrespondingCallOpSpecialization);
6795 LambdaCallOp = CorrespondingCallOpSpecialization;
6796 } else {
6797 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6798 }
6799 assert(ClosureClass->captures().empty());
6800 const Function *Func = this->getFunction(LambdaCallOp);
6801 if (!Func)
6802 return false;
6803 assert(Func->hasThisPointer());
6804 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
6805
6806 if (Func->hasRVO()) {
6807 if (!this->emitRVOPtr(MD))
6808 return false;
6809 }
6810
6811 // The lambda call operator needs an instance pointer, but we don't have
6812 // one here, and we don't need one either because the lambda cannot have
6813 // any captures, as verified above. Emit a null pointer. This is then
6814 // special-cased when interpreting to not emit any misleading diagnostics.
6815 if (!this->emitNullPtr(0, nullptr, MD))
6816 return false;
6817
6818 // Forward all arguments from the static invoker to the lambda call operator.
6819 for (const ParmVarDecl *PVD : MD->parameters()) {
6820 auto It = this->Params.find(PVD);
6821 assert(It != this->Params.end());
6822
6823 // We do the lvalue-to-rvalue conversion manually here, so no need
6824 // to care about references.
6825 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
6826 if (!this->emitGetParam(ParamType, It->second.Index, MD))
6827 return false;
6828 }
6829
6830 if (!this->emitCall(Func, 0, LambdaCallOp))
6831 return false;
6832
6833 this->emitCleanup();
6834 if (ReturnType)
6835 return this->emitRet(*ReturnType, MD);
6836
6837 // Nothing to do, since we emitted the RVO pointer above.
6838 return this->emitRetVoid(MD);
6839}
6840
6841template <class Emitter>
6842bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
6843 if (Ctx.getLangOpts().CPlusPlus23)
6844 return true;
6845
6846 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
6847 return true;
6848
6849 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
6850}
6851
6853 const Expr *InitExpr = Init->getInit();
6854
6855 if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
6856 !isa<CXXConstructExpr>(InitExpr))
6857 return true;
6858
6859 if (const auto *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
6860 const CXXConstructorDecl *Ctor = CE->getConstructor();
6861 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
6862 Ctor->isTrivial())
6863 return true;
6864 }
6865
6866 return false;
6867}
6868
6869template <class Emitter>
6870bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
6871 assert(!ReturnType);
6872
6873 // Only start the lifetime of the instance pointer.
6874 if (!this->emitStartThisLifetime1(Ctor))
6875 return false;
6876
6877 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
6878 const Expr *InitExpr,
6879 bool Activate = false) -> bool {
6880 // We don't know what to do with these, so just return false.
6881 if (InitExpr->getType().isNull())
6882 return false;
6883
6884 if (OptPrimType T = this->classify(InitExpr)) {
6885 if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
6886 return false;
6887
6888 if (!this->visit(InitExpr))
6889 return false;
6890
6891 if (F->isBitField())
6892 return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(),
6893 InitExpr);
6894 return this->emitInitThisField(*T, FieldOffset, InitExpr);
6895 }
6896 // Non-primitive case. Get a pointer to the field-to-initialize
6897 // on the stack and call visitInitialzer() for it.
6898 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
6899 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
6900 return false;
6901
6902 if (Activate && !this->emitActivate(InitExpr))
6903 return false;
6904
6905 return this->visitInitializerPop(InitExpr);
6906 };
6907
6908 const RecordDecl *RD = Ctor->getParent();
6909 const Record *R = this->getRecord(RD);
6910 if (!R)
6911 return false;
6912 bool IsUnion = R->isUnion();
6913
6914 // Default union copy and move ctors are special.
6915 if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) {
6917
6918 // No special case for NumFields == 0 here, so the Memcpy op
6919 // below also does its checks in those cases.
6920
6921 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
6922 if (!this->emitThis(Ctor))
6923 return false;
6924
6925 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor))
6926 return false;
6927
6928 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
6929 this->emitRetVoid(Ctor);
6930 }
6931
6932 unsigned FieldInits = 0;
6934 for (const auto *Init : Ctor->inits()) {
6935 // Scope needed for the initializers.
6936 LocalScope<Emitter> Scope(this, ScopeKind::FullExpression);
6937
6938 const Expr *InitExpr = Init->getInit();
6939 if (const FieldDecl *Member = Init->getMember()) {
6940 const Record::Field *F = R->getField(Member);
6941
6944 if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
6945 return false;
6946 ++FieldInits;
6947 } else if (const Type *Base = Init->getBaseClass()) {
6948 const auto *BaseDecl = Base->getAsCXXRecordDecl();
6949 assert(BaseDecl);
6950
6951 if (Init->isBaseVirtual()) {
6952 assert(R->getVirtualBase(BaseDecl));
6953 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
6954 return false;
6955
6956 } else {
6957 // Base class initializer.
6958 // Get This Base and call initializer on it.
6959 const Record::Base *B = R->getBase(BaseDecl);
6960 assert(B);
6961 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
6962 return false;
6963 }
6964
6965 if (IsUnion && !this->emitActivate(InitExpr))
6966 return false;
6967
6968 if (!this->visitInitializerPop(InitExpr))
6969 return false;
6970 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
6973 unsigned ChainSize = IFD->getChainingSize();
6974 assert(ChainSize >= 2);
6975
6976 unsigned NestedFieldOffset = 0;
6977 const Record::Field *NestedField = nullptr;
6978 for (unsigned I = 0; I != ChainSize; ++I) {
6979 const auto *FD = cast<FieldDecl>(IFD->chain()[I]);
6980 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6981 assert(FieldRecord);
6982
6983 NestedField = FieldRecord->getField(FD);
6984 assert(NestedField);
6985 IsUnion = IsUnion || FieldRecord->isUnion();
6986
6987 NestedFieldOffset += NestedField->Offset;
6988
6989 // Add a new InitChainLink for the record, but not for the final field.
6990 if (I != ChainSize - 1)
6991 InitStack.push_back(InitLink::Field(NestedField->Offset));
6992 }
6993 assert(NestedField);
6994
6996 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
6997 IsUnion))
6998 return false;
6999
7000 // Mark all chain links as initialized.
7001 unsigned InitFieldOffset = 0;
7002 for (const NamedDecl *ND : IFD->chain().drop_back()) {
7003 const auto *FD = cast<FieldDecl>(ND);
7004 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
7005 assert(FieldRecord);
7006 NestedField = FieldRecord->getField(FD);
7007 InitFieldOffset += NestedField->Offset;
7008 assert(NestedField);
7009 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
7010 return false;
7011 if (!this->emitFinishInitPop(InitExpr))
7012 return false;
7013 }
7014
7015 InitStack.pop_back_n(ChainSize - 1);
7016
7017 } else {
7018 assert(Init->isDelegatingInitializer());
7019 if (!this->emitThis(InitExpr))
7020 return false;
7021 if (!this->visitInitializerPop(Init->getInit()))
7022 return false;
7023 }
7024
7025 if (!Scope.destroyLocals())
7026 return false;
7027 }
7028
7029 if (FieldInits != R->getNumFields()) {
7030 assert(FieldInits < R->getNumFields());
7031 // Start the lifetime of all members.
7032 if (!this->emitStartThisLifetime(Ctor))
7033 return false;
7034 }
7035
7036 if (const Stmt *Body = Ctor->getBody()) {
7037 // Only emit the CtorCheck op for non-empty CompoundStmt bodies.
7038 // For non-CompoundStmts, always assume they are non-empty and emit it.
7039 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
7040 if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{}))
7041 return false;
7042 } else {
7043 if (!this->emitCtorCheck(SourceInfo{}))
7044 return false;
7045 }
7046
7047 if (!visitStmt(Body))
7048 return false;
7049 }
7050
7051 return this->emitRetVoid(SourceInfo{});
7052}
7053
7054template <class Emitter>
7055bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
7056 const RecordDecl *RD = Dtor->getParent();
7057 const Record *R = this->getRecord(RD);
7058 if (!R)
7059 return false;
7060
7061 if (!Dtor->isTrivial() && Dtor->getBody()) {
7062 if (!this->visitStmt(Dtor->getBody()))
7063 return false;
7064 }
7065
7066 if (!this->emitThis(Dtor))
7067 return false;
7068
7069 if (!this->emitCheckDestruction(Dtor))
7070 return false;
7071
7072 assert(R);
7073 if (!R->isUnion()) {
7074
7076 // First, destroy all fields.
7077 for (const Record::Field &Field : llvm::reverse(R->fields())) {
7078 const Descriptor *D = Field.Desc;
7079 if (D->hasTrivialDtor())
7080 continue;
7081 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
7082 return false;
7083 if (!this->emitDestructionPop(D, SourceInfo{}))
7084 return false;
7085 }
7086 }
7087
7088 for (const Record::Base &Base : llvm::reverse(R->bases())) {
7089 if (Base.R->hasTrivialDtor())
7090 continue;
7091 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
7092 return false;
7093 if (!this->emitRecordDestructionPop(Base.R, {}))
7094 return false;
7095 }
7096
7097 if (!this->emitMarkDestroyed(Dtor))
7098 return false;
7099
7100 // FIXME: Virtual bases.
7101 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
7102}
7103
7104template <class Emitter>
7105bool Compiler<Emitter>::compileUnionAssignmentOperator(
7106 const CXXMethodDecl *MD) {
7107 if (!this->emitThis(MD))
7108 return false;
7109
7110 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD))
7111 return false;
7112
7113 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
7114}
7115
7116template <class Emitter>
7118 if (F->getReturnType()->isDependentType())
7119 return false;
7120
7121 // Classify the return type.
7122 ReturnType = this->classify(F->getReturnType());
7123
7124 this->CompilingFunction = F;
7125
7126 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
7127 return this->compileConstructor(Ctor);
7128 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
7129 return this->compileDestructor(Dtor);
7130
7131 // Emit custom code if this is a lambda static invoker.
7132 if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
7133 const RecordDecl *RD = MD->getParent();
7134
7135 if (RD->isUnion() &&
7137 return this->compileUnionAssignmentOperator(MD);
7138
7139 if (MD->isLambdaStaticInvoker())
7140 return this->emitLambdaStaticInvokerBody(MD);
7141 }
7142
7143 // Regular functions.
7144 if (const auto *Body = F->getBody())
7145 if (!visitStmt(Body))
7146 return false;
7147
7148 // Emit a guard return to protect against a code path missing one.
7149 if (F->getReturnType()->isVoidType())
7150 return this->emitRetVoid(SourceInfo{});
7151 return this->emitNoRet(SourceInfo{});
7152}
7153
7154static uint32_t getBitWidth(const Expr *E) {
7155 assert(E->refersToBitField());
7156 const auto *ME = cast<MemberExpr>(E);
7157 const auto *FD = cast<FieldDecl>(ME->getMemberDecl());
7158 return FD->getBitWidthValue();
7159}
7160
7161template <class Emitter>
7163 if (E->containsErrors())
7164 return false;
7165
7166 const Expr *SubExpr = E->getSubExpr();
7167 if (SubExpr->getType()->isAnyComplexType())
7168 return this->VisitComplexUnaryOperator(E);
7169 if (SubExpr->getType()->isVectorType())
7170 return this->VisitVectorUnaryOperator(E);
7171 if (SubExpr->getType()->isFixedPointType())
7172 return this->VisitFixedPointUnaryOperator(E);
7173 OptPrimType T = classify(SubExpr->getType());
7174
7175 switch (E->getOpcode()) {
7176 case UO_PostInc: { // x++
7177 if (!Ctx.getLangOpts().CPlusPlus14)
7178 return this->emitInvalid(E);
7179 if (!T)
7180 return this->emitError(E);
7181
7182 if (!this->visit(SubExpr))
7183 return false;
7184
7185 if (T == PT_Ptr) {
7186 if (!this->emitIncPtr(E))
7187 return false;
7188
7189 return DiscardResult ? this->emitPopPtr(E) : true;
7190 }
7191
7192 if (T == PT_Float)
7193 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
7194 : this->emitIncf(getFPOptions(E), E);
7195
7196 if (SubExpr->refersToBitField())
7197 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7198 getBitWidth(SubExpr), E)
7199 : this->emitIncBitfield(*T, E->canOverflow(),
7200 getBitWidth(SubExpr), E);
7201
7202 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
7203 : this->emitInc(*T, E->canOverflow(), E);
7204 }
7205 case UO_PostDec: { // x--
7206 if (!Ctx.getLangOpts().CPlusPlus14)
7207 return this->emitInvalid(E);
7208 if (!T)
7209 return this->emitError(E);
7210
7211 if (!this->visit(SubExpr))
7212 return false;
7213
7214 if (T == PT_Ptr) {
7215 if (!this->emitDecPtr(E))
7216 return false;
7217
7218 return DiscardResult ? this->emitPopPtr(E) : true;
7219 }
7220
7221 if (T == PT_Float)
7222 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
7223 : this->emitDecf(getFPOptions(E), E);
7224
7225 if (SubExpr->refersToBitField()) {
7226 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7227 getBitWidth(SubExpr), E)
7228 : this->emitDecBitfield(*T, E->canOverflow(),
7229 getBitWidth(SubExpr), E);
7230 }
7231
7232 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
7233 : this->emitDec(*T, E->canOverflow(), E);
7234 }
7235 case UO_PreInc: { // ++x
7236 if (!Ctx.getLangOpts().CPlusPlus14)
7237 return this->emitInvalid(E);
7238 if (!T)
7239 return this->emitError(E);
7240
7241 if (!this->visit(SubExpr))
7242 return false;
7243
7244 if (T == PT_Ptr) {
7245 if (!this->emitLoadPtr(E))
7246 return false;
7247 if (!this->emitConstUint8(1, E))
7248 return false;
7249 if (!this->emitAddOffsetUint8(E))
7250 return false;
7251 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7252 }
7253
7254 // Post-inc and pre-inc are the same if the value is to be discarded.
7255 if (DiscardResult) {
7256 if (T == PT_Float)
7257 return this->emitIncfPop(getFPOptions(E), E);
7258 if (SubExpr->refersToBitField())
7259 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7260 getBitWidth(SubExpr), E)
7261 : this->emitIncBitfield(*T, E->canOverflow(),
7262 getBitWidth(SubExpr), E);
7263 return this->emitIncPop(*T, E->canOverflow(), E);
7264 }
7265
7266 if (T == PT_Float) {
7267 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7268 if (!this->emitLoadFloat(E))
7269 return false;
7270 APFloat F(TargetSemantics, 1);
7271 if (!this->emitFloat(F, E))
7272 return false;
7273
7274 if (!this->emitAddf(getFPOptions(E), E))
7275 return false;
7276 if (!this->emitStoreFloat(E))
7277 return false;
7278 } else if (SubExpr->refersToBitField()) {
7279 assert(isIntegerOrBoolType(*T));
7280 if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7281 E))
7282 return false;
7283 } else {
7284 assert(isIntegerOrBoolType(*T));
7285 if (!this->emitPreInc(*T, E->canOverflow(), E))
7286 return false;
7287 }
7288 return E->isGLValue() || this->emitLoadPop(*T, E);
7289 }
7290 case UO_PreDec: { // --x
7291 if (!Ctx.getLangOpts().CPlusPlus14)
7292 return this->emitInvalid(E);
7293 if (!T)
7294 return this->emitError(E);
7295
7296 if (!this->visit(SubExpr))
7297 return false;
7298
7299 if (T == PT_Ptr) {
7300 if (!this->emitLoadPtr(E))
7301 return false;
7302 if (!this->emitConstUint8(1, E))
7303 return false;
7304 if (!this->emitSubOffsetUint8(E))
7305 return false;
7306 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7307 }
7308
7309 // Post-dec and pre-dec are the same if the value is to be discarded.
7310 if (DiscardResult) {
7311 if (T == PT_Float)
7312 return this->emitDecfPop(getFPOptions(E), E);
7313 if (SubExpr->refersToBitField())
7314 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7315 getBitWidth(SubExpr), E)
7316 : this->emitDecBitfield(*T, E->canOverflow(),
7317 getBitWidth(SubExpr), E);
7318 return this->emitDecPop(*T, E->canOverflow(), E);
7319 }
7320
7321 if (T == PT_Float) {
7322 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7323 if (!this->emitLoadFloat(E))
7324 return false;
7325 APFloat F(TargetSemantics, 1);
7326 if (!this->emitFloat(F, E))
7327 return false;
7328
7329 if (!this->emitSubf(getFPOptions(E), E))
7330 return false;
7331 if (!this->emitStoreFloat(E))
7332 return false;
7333 } else if (SubExpr->refersToBitField()) {
7334 assert(isIntegerOrBoolType(*T));
7335 if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7336 E))
7337 return false;
7338 } else {
7339 assert(isIntegerOrBoolType(*T));
7340 if (!this->emitPreDec(*T, E->canOverflow(), E))
7341 return false;
7342 }
7343 return E->isGLValue() || this->emitLoadPop(*T, E);
7344 }
7345 case UO_LNot: // !x
7346 if (!T)
7347 return this->emitError(E);
7348
7349 if (DiscardResult)
7350 return this->discard(SubExpr);
7351
7352 if (!this->visitBool(SubExpr))
7353 return false;
7354
7355 if (!this->emitInv(E))
7356 return false;
7357
7358 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7359 return this->emitCast(PT_Bool, ET, E);
7360 return true;
7361 case UO_Minus: // -x
7362 if (!T)
7363 return this->emitError(E);
7364
7365 if (!this->visit(SubExpr))
7366 return false;
7367 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
7368 case UO_Plus: // +x
7369 if (!T)
7370 return this->emitError(E);
7371
7372 if (!this->visit(SubExpr)) // noop
7373 return false;
7374 return DiscardResult ? this->emitPop(*T, E) : true;
7375 case UO_AddrOf: // &x
7376 if (E->getType()->isMemberPointerType()) {
7377 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
7378 // member can be formed.
7379 if (DiscardResult)
7380 return true;
7381 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
7382 }
7383 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
7384 // operator, neither operator is evaluated and the result is as if both
7385 // were omitted. So '&*q' is just 'q' with no dereference; delegate to the
7386 // pointer operand directly instead of to the '*' (which would emit a null
7387 // check), so that e.g. '&*(int *)0' is not rejected.
7388 if (!Ctx.getLangOpts().CPlusPlus) {
7389 const Expr *Sub = SubExpr->IgnoreParens();
7390 if (const auto *Deref = dyn_cast<UnaryOperator>(Sub);
7391 Deref && Deref->getOpcode() == UO_Deref)
7392 return this->delegate(Deref->getSubExpr());
7393 }
7394 // We should already have a pointer when we get here.
7395 return this->delegate(SubExpr);
7396 case UO_Deref: // *x
7397 if (DiscardResult)
7398 return this->discard(SubExpr);
7399
7400 if (!this->visit(SubExpr))
7401 return false;
7402
7403 if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
7404 return false;
7405
7406 if (classifyPrim(SubExpr) == PT_Ptr)
7407 return this->emitNarrowPtr(E);
7408 return true;
7409
7410 case UO_Not: // ~x
7411 if (!T)
7412 return this->emitError(E);
7413
7414 if (!this->visit(SubExpr))
7415 return false;
7416 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
7417 case UO_Real: // __real x
7418 if (!T)
7419 return false;
7420 return this->delegate(SubExpr);
7421 case UO_Imag: { // __imag x
7422 if (!T)
7423 return false;
7424 if (!this->discard(SubExpr))
7425 return false;
7426 return DiscardResult
7427 ? true
7428 : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
7429 }
7430 case UO_Extension:
7431 return this->delegate(SubExpr);
7432 case UO_Coawait:
7433 assert(false && "Unhandled opcode");
7434 }
7435
7436 return false;
7437}
7438
7439template <class Emitter>
7441 const Expr *SubExpr = E->getSubExpr();
7442 assert(SubExpr->getType()->isAnyComplexType());
7443
7444 if (DiscardResult)
7445 return this->discard(SubExpr);
7446
7447 OptPrimType ResT = classify(E);
7448 auto prepareResult = [=]() -> bool {
7449 if (!ResT && !Initializing) {
7450 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7451 if (!LocalIndex)
7452 return false;
7453 return this->emitGetPtrLocal(*LocalIndex, E);
7454 }
7455
7456 return true;
7457 };
7458
7459 // The offset of the temporary, if we created one.
7460 unsigned SubExprOffset = ~0u;
7461 auto createTemp = [=, &SubExprOffset]() -> bool {
7462 SubExprOffset =
7463 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7464 if (!this->visit(SubExpr))
7465 return false;
7466 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
7467 };
7468
7469 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
7470 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7471 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7472 return false;
7473 return this->emitArrayElemPop(ElemT, Index, E);
7474 };
7475
7476 switch (E->getOpcode()) {
7477 case UO_Minus: // -x
7478 if (!prepareResult())
7479 return false;
7480 if (!createTemp())
7481 return false;
7482 for (unsigned I = 0; I != 2; ++I) {
7483 if (!getElem(SubExprOffset, I))
7484 return false;
7485 if (!this->emitNeg(ElemT, E))
7486 return false;
7487 if (!this->emitInitElem(ElemT, I, E))
7488 return false;
7489 }
7490 break;
7491
7492 case UO_Plus: // +x
7493 case UO_AddrOf: // &x
7494 case UO_Deref: // *x
7495 return this->delegate(SubExpr);
7496
7497 case UO_LNot:
7498 if (!this->visit(SubExpr))
7499 return false;
7500 if (!this->emitComplexBoolCast(SubExpr))
7501 return false;
7502 if (!this->emitInv(E))
7503 return false;
7504 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7505 return this->emitCast(PT_Bool, ET, E);
7506 return true;
7507
7508 case UO_Real:
7509 return this->emitComplexReal(SubExpr);
7510
7511 case UO_Imag:
7512 if (!this->visit(SubExpr))
7513 return false;
7514
7515 if (SubExpr->isLValue()) {
7516 if (!this->emitConstUint8(1, E))
7517 return false;
7518 return this->emitArrayElemPtrPopUint8(E);
7519 }
7520
7521 // Since our _Complex implementation does not map to a primitive type,
7522 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
7523 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
7524
7525 case UO_Not: // ~x
7526 if (!this->delegate(SubExpr))
7527 return false;
7528 // Negate the imaginary component.
7529 if (!this->emitArrayElem(ElemT, 1, E))
7530 return false;
7531 if (!this->emitNeg(ElemT, E))
7532 return false;
7533 if (!this->emitInitElem(ElemT, 1, E))
7534 return false;
7535 return DiscardResult ? this->emitPopPtr(E) : true;
7536
7537 case UO_Extension:
7538 return this->delegate(SubExpr);
7539
7540 default:
7541 return this->emitInvalid(E);
7542 }
7543
7544 return true;
7545}
7546
7547template <class Emitter>
7549 const Expr *SubExpr = E->getSubExpr();
7550 assert(SubExpr->getType()->isVectorType());
7551
7552 if (DiscardResult)
7553 return this->discard(SubExpr);
7554
7555 auto UnaryOp = E->getOpcode();
7556 if (UnaryOp == UO_Extension)
7557 return this->delegate(SubExpr);
7558
7559 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
7560 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
7561 return this->emitInvalid(E);
7562
7563 // Nothing to do here.
7564 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
7565 return this->delegate(SubExpr);
7566
7567 if (!Initializing) {
7568 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7569 if (!LocalIndex)
7570 return false;
7571 if (!this->emitGetPtrLocal(*LocalIndex, E))
7572 return false;
7573 }
7574
7575 // The offset of the temporary, if we created one.
7576 unsigned SubExprOffset =
7577 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7578 if (!this->visit(SubExpr))
7579 return false;
7580 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
7581 return false;
7582
7583 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
7584 PrimType ElemT = classifyVectorElementType(SubExpr->getType());
7585 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7586 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7587 return false;
7588 return this->emitArrayElemPop(ElemT, Index, E);
7589 };
7590
7591 switch (UnaryOp) {
7592 case UO_Minus:
7593 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7594 if (!getElem(SubExprOffset, I))
7595 return false;
7596 if (!this->emitNeg(ElemT, E))
7597 return false;
7598 if (!this->emitInitElem(ElemT, I, E))
7599 return false;
7600 }
7601 break;
7602 case UO_LNot: { // !x
7603 // In C++, the logic operators !, &&, || are available for vectors. !v is
7604 // equivalent to v == 0.
7605 //
7606 // The result of the comparison is a vector of the same width and number of
7607 // elements as the comparison operands with a signed integral element type.
7608 //
7609 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
7610 QualType ResultVecTy = E->getType();
7611 PrimType ResultVecElemT =
7612 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
7613 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7614 if (!getElem(SubExprOffset, I))
7615 return false;
7616 // operator ! on vectors returns -1 for 'truth', so negate it.
7617 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
7618 return false;
7619 if (!this->emitInv(E))
7620 return false;
7621 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
7622 return false;
7623 if (!this->emitNeg(ElemT, E))
7624 return false;
7625 if (ElemT != ResultVecElemT &&
7626 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
7627 return false;
7628 if (!this->emitInitElem(ResultVecElemT, I, E))
7629 return false;
7630 }
7631 break;
7632 }
7633 case UO_Not: // ~x
7634 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7635 if (!getElem(SubExprOffset, I))
7636 return false;
7637 if (ElemT == PT_Bool) {
7638 if (!this->emitInv(E))
7639 return false;
7640 } else {
7641 if (!this->emitComp(ElemT, E))
7642 return false;
7643 }
7644 if (!this->emitInitElem(ElemT, I, E))
7645 return false;
7646 }
7647 break;
7648 default:
7649 llvm_unreachable("Unsupported unary operators should be handled up front");
7650 }
7651 return true;
7652}
7653
7654template <class Emitter>
7656 if (DiscardResult)
7657 return true;
7658
7659 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7660 return this->emitConst(ECD->getInitVal(), E);
7661 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
7662 const Function *F = getFunction(FuncDecl);
7663 return F && this->emitGetFnPtr(F, E);
7664 }
7665 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
7666 if (UnsignedOrNone Index = P.getOrCreateGlobal(D)) {
7667 if (OptPrimType T = classify(D->getType())) {
7668 if (!this->visitAPValue(TPOD->getValue(), *T, E))
7669 return false;
7670 return this->emitInitGlobal(*T, *Index, E);
7671 }
7672
7673 if (!this->emitGetPtrGlobal(*Index, E))
7674 return false;
7675 if (!this->visitAPValueInitializer(TPOD->getValue(), E, TPOD->getType()))
7676 return false;
7677 return this->emitFinishInit(E);
7678 }
7679 return false;
7680 }
7681
7682 // References are implemented via pointers, so when we see a DeclRefExpr
7683 // pointing to a reference, we need to get its value directly (i.e. the
7684 // pointer to the actual value) instead of a pointer to the pointer to the
7685 // value.
7686 QualType DeclType = D->getType();
7687 bool IsReference = DeclType->isReferenceType();
7688
7689 // Function parameters.
7690 // Note that it's important to check them first since we might have a local
7691 // variable created for a ParmVarDecl as well.
7692 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
7693 if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
7694 !DeclType->isIntegralOrEnumerationType()) {
7695 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7696 /*InitializerFailed=*/false, E);
7697 }
7698 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
7699 if (IsReference || !It->second.IsPtr)
7700 return this->emitGetParam(classifyPrim(E), It->second.Index, E);
7701
7702 return this->emitGetPtrParam(It->second.Index, E);
7703 }
7704
7705 if (!Ctx.getLangOpts().CPlusPlus23 && IsReference)
7706 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7707 /*InitializerFailed=*/false, E);
7708 }
7709 // Local variables.
7710 if (auto It = Locals.find(D); It != Locals.end()) {
7711 const unsigned Offset = It->second.Offset;
7712 if (IsReference) {
7713 assert(classifyPrim(E) == PT_Ptr);
7714 return this->emitGetRefLocal(Offset, E);
7715 }
7716 return this->emitGetPtrLocal(Offset, E);
7717 }
7718 // Global variables.
7719 if (auto GlobalIndex = P.getGlobal(D)) {
7720 if (IsReference) {
7721 if (!Ctx.getLangOpts().CPlusPlus11)
7722 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
7723 if (!Ctx.getLangOpts().CPlusPlus23)
7724 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
7725 return this->emitGetRefGlobal(*GlobalIndex, E);
7726 }
7727
7728 return this->emitGetPtrGlobal(*GlobalIndex, E);
7729 }
7730
7731 // In case we need to re-visit a declaration.
7732 auto revisit = [&](const VarDecl *VD,
7733 bool IsConstexprUnknown = true) -> bool {
7735 IsConstexprUnknown);
7736 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
7737 return false;
7738 auto VarState = this->visitDecl(VD);
7739
7740 if (!this->emitPopCC(E))
7741 return false;
7742
7743 if (VarState.notCreated())
7744 return true;
7745 if (!VarState)
7746 return false;
7747 // Retry.
7748 return this->visitDeclRef(D, E);
7749 };
7750
7751 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
7752 // Lambda captures.
7753 if (auto It = this->LambdaCaptures.find(D);
7754 It != this->LambdaCaptures.end()) {
7755 auto [Offset, IsPtr] = It->second;
7756
7757 if (IsPtr)
7758 return this->emitGetThisFieldPtr(Offset, E);
7759 return this->emitGetPtrThisField(Offset, E);
7760 }
7761 }
7762
7763 if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
7764 DRE && DRE->refersToEnclosingVariableOrCapture()) {
7765 if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
7766 return revisit(VD);
7767 }
7768
7769 if (const auto *BD = dyn_cast<BindingDecl>(D))
7770 return this->visit(BD->getBinding());
7771
7772 // Avoid infinite recursion.
7773 if (D == InitializingDecl)
7774 return this->emitDummyPtr(D, E);
7775
7776 // Try to lazily visit (or emit dummy pointers for) declarations
7777 // we haven't seen yet.
7778 const auto *VD = dyn_cast<VarDecl>(D);
7779 if (!VD)
7780 return this->emitError(E);
7781
7782 // For C.
7783 if (!Ctx.getLangOpts().CPlusPlus) {
7784 if (VD->getInit() && !VD->getInit()->isValueDependent() &&
7785 DeclType.isConstant(Ctx.getASTContext()) && !VD->isWeak() &&
7786 VD->evaluateValue())
7787 return revisit(VD, /*IsConstexprUnknown=*/false);
7788 return this->emitDummyPtr(D, E);
7789 }
7790
7791 // ... and C++.
7792 const auto typeShouldBeVisited = [&](QualType T) -> bool {
7793 if (T.isConstant(Ctx.getASTContext()))
7794 return true;
7795 return T->isReferenceType();
7796 };
7797
7798 if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
7799 typeShouldBeVisited(DeclType)) {
7800 if (const Expr *Init = VD->getAnyInitializer();
7801 Init && !Init->isValueDependent()) {
7802 // Whether or not the evaluation is successul doesn't really matter
7803 // here -- we will create a global variable in any case, and that
7804 // will have the state of initializer evaluation attached.
7806 (void)Init->EvaluateAsInitializer(Ctx.getASTContext(), VD, Result, true);
7807 return this->visitDeclRef(D, E);
7808 }
7809 return revisit(VD, !VD->isConstexpr() && DeclType->isReferenceType());
7810 }
7811
7812 // FIXME: The evaluateValue() check here is a little ridiculous, since
7813 // it will ultimately call into Context::evaluateAsInitializer(). In
7814 // other words, we're evaluating the initializer, just to know if we can
7815 // evaluate the initializer.
7816 if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() &&
7817 !VD->getInit()->isValueDependent()) {
7818 if (VD->evaluateValue()) {
7819 bool IsConstexprUnknown = !DeclType.isConstant(Ctx.getASTContext()) &&
7820 !DeclType->isReferenceType();
7821 // Revisit the variable declaration, but make sure it's associated with a
7822 // different evaluation, so e.g. mutable reads don't work on it.
7823 EvalIDScope _(Ctx);
7824 return revisit(VD, IsConstexprUnknown);
7825 } else if (Ctx.getLangOpts().CPlusPlus23 && IsReference)
7826 return revisit(VD, /*IsConstexprUnknown=*/true);
7827
7828 if (IsReference)
7829 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7830 /*InitializerFailed=*/true, E);
7831 }
7832
7833 return this->emitDummyPtr(
7834 D, E, Ctx.getLangOpts().CPlusPlus23 && DeclType->isReferenceType());
7835}
7836
7837template <class Emitter>
7839 const auto *D = E->getDecl();
7840 return this->visitDeclRef(D, E);
7841}
7842
7843template <class Emitter>
7845 const DesignatedInitUpdateExpr *E) {
7846 if (!this->visitInitializer(E->getBase()))
7847 return false;
7848 return this->visitInitializer(E->getUpdater());
7849}
7850
7851template <class Emitter> bool Compiler<Emitter>::emitCleanup() {
7852 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) {
7853 if (!C->destroyLocals())
7854 return false;
7855 }
7856 return true;
7857}
7858
7859template <class Emitter>
7860unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
7861 const QualType DerivedType) {
7862 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
7863 if (const auto *R = Ty->getPointeeCXXRecordDecl())
7864 return R;
7865 return Ty->getAsCXXRecordDecl();
7866 };
7867 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
7868 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
7869
7870 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
7871}
7872
7873/// Emit casts from a PrimType to another PrimType.
7874template <class Emitter>
7875bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
7876 QualType ToQT, const Expr *E) {
7877
7878 if (FromT == PT_Float) {
7879 // Floating to floating.
7880 if (ToT == PT_Float) {
7881 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7882 return this->emitCastFP(ToSem, getRoundingMode(E), E);
7883 }
7884
7885 if (ToT == PT_IntAP)
7886 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
7887 getFPOptions(E), E);
7888 if (ToT == PT_IntAPS)
7889 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
7890 getFPOptions(E), E);
7891
7892 // Float to integral.
7893 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7894 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
7895 }
7896
7897 if (isIntegerOrBoolType(FromT) || FromT == PT_Bool) {
7898 if (ToT == PT_IntAP)
7899 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7900 if (ToT == PT_IntAPS)
7901 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7902
7903 // Integral to integral.
7904 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7905 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
7906
7907 if (ToT == PT_Float) {
7908 // Integral to floating.
7909 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7910 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
7911 }
7912 }
7913
7914 return false;
7915}
7916
7917template <class Emitter>
7918bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT,
7919 QualType ToQT, const Expr *E) {
7920 assert(FromT != ToT);
7921
7922 if (ToT == PT_IntAP)
7923 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7924 if (ToT == PT_IntAPS)
7925 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7926
7927 return this->emitCast(FromT, ToT, E);
7928}
7929
7930/// Emits __real(SubExpr)
7931template <class Emitter>
7932bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
7933 assert(SubExpr->getType()->isAnyComplexType());
7934
7935 if (DiscardResult)
7936 return this->discard(SubExpr);
7937
7938 if (!this->visit(SubExpr))
7939 return false;
7940 if (SubExpr->isLValue()) {
7941 if (!this->emitConstUint8(0, SubExpr))
7942 return false;
7943 return this->emitArrayElemPtrPopUint8(SubExpr);
7944 }
7945
7946 // Rvalue, load the actual element.
7947 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
7948 0, SubExpr);
7949}
7950
7951template <class Emitter>
7952bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
7953 assert(!DiscardResult);
7954 PrimType ElemT = classifyComplexElementType(E->getType());
7955 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
7956 // for us, that means (bool)E[0] || (bool)E[1]
7957 if (!this->emitArrayElem(ElemT, 0, E))
7958 return false;
7959 if (ElemT == PT_Float) {
7960 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7961 return false;
7962 } else {
7963 if (!this->emitCast(ElemT, PT_Bool, E))
7964 return false;
7965 }
7966
7967 // We now have the bool value of E[0] on the stack.
7968 LabelTy LabelTrue = this->getLabel();
7969 if (!this->jumpTrue(LabelTrue, E))
7970 return false;
7971
7972 if (!this->emitArrayElemPop(ElemT, 1, E))
7973 return false;
7974 if (ElemT == PT_Float) {
7975 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7976 return false;
7977 } else {
7978 if (!this->emitCast(ElemT, PT_Bool, E))
7979 return false;
7980 }
7981 // Leave the boolean value of E[1] on the stack.
7982 LabelTy EndLabel = this->getLabel();
7983 this->jump(EndLabel, E);
7984
7985 this->emitLabel(LabelTrue);
7986 if (!this->emitPopPtr(E))
7987 return false;
7988 if (!this->emitConstBool(true, E))
7989 return false;
7990
7991 this->fallthrough(EndLabel);
7992 this->emitLabel(EndLabel);
7993
7994 return true;
7995}
7996
7997template <class Emitter>
7998bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
7999 const BinaryOperator *E) {
8000 assert(E->isComparisonOp());
8001 assert(!Initializing);
8002 if (DiscardResult)
8003 return this->discard(LHS) && this->discard(RHS);
8004
8005 PrimType ElemT;
8006 bool LHSIsComplex;
8007 unsigned LHSOffset;
8008 if (LHS->getType()->isAnyComplexType()) {
8009 LHSIsComplex = true;
8010 ElemT = classifyComplexElementType(LHS->getType());
8011 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
8012 if (!this->visit(LHS))
8013 return false;
8014 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
8015 return false;
8016 } else {
8017 LHSIsComplex = false;
8018 PrimType LHST = classifyPrim(LHS->getType());
8019 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
8020 if (!this->visit(LHS))
8021 return false;
8022 if (!this->emitSetLocal(LHST, LHSOffset, E))
8023 return false;
8024 }
8025
8026 bool RHSIsComplex;
8027 unsigned RHSOffset;
8028 if (RHS->getType()->isAnyComplexType()) {
8029 RHSIsComplex = true;
8030 ElemT = classifyComplexElementType(RHS->getType());
8031 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
8032 if (!this->visit(RHS))
8033 return false;
8034 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
8035 return false;
8036 } else {
8037 RHSIsComplex = false;
8038 PrimType RHST = classifyPrim(RHS->getType());
8039 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
8040 if (!this->visit(RHS))
8041 return false;
8042 if (!this->emitSetLocal(RHST, RHSOffset, E))
8043 return false;
8044 }
8045
8046 auto getElem = [&](unsigned LocalOffset, unsigned Index,
8047 bool IsComplex) -> bool {
8048 if (IsComplex) {
8049 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
8050 return false;
8051 return this->emitArrayElemPop(ElemT, Index, E);
8052 }
8053 return this->emitGetLocal(ElemT, LocalOffset, E);
8054 };
8055
8056 for (unsigned I = 0; I != 2; ++I) {
8057 // Get both values.
8058 if (!getElem(LHSOffset, I, LHSIsComplex))
8059 return false;
8060 if (!getElem(RHSOffset, I, RHSIsComplex))
8061 return false;
8062 // And compare them.
8063 if (!this->emitEQ(ElemT, E))
8064 return false;
8065
8066 if (!this->emitCastBoolUint8(E))
8067 return false;
8068 }
8069
8070 // We now have two bool values on the stack. Compare those.
8071 if (!this->emitAddUint8(E))
8072 return false;
8073 if (!this->emitConstUint8(2, E))
8074 return false;
8075
8076 if (E->getOpcode() == BO_EQ) {
8077 if (!this->emitEQUint8(E))
8078 return false;
8079 } else if (E->getOpcode() == BO_NE) {
8080 if (!this->emitNEUint8(E))
8081 return false;
8082 } else
8083 return false;
8084
8085 // In C, this returns an int.
8086 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
8087 return this->emitCast(PT_Bool, ResT, E);
8088 return true;
8089}
8090
8091/// When calling this, we have a pointer of the local-to-destroy
8092/// on the stack.
8093/// Emit destruction of record types (or arrays of record types).
8094template <class Emitter>
8095bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R,
8096 SourceInfo Loc) {
8097 assert(R);
8098 assert(!R->hasTrivialDtor());
8099 const CXXDestructorDecl *Dtor = R->getDestructor();
8100 assert(Dtor);
8101 const Function *DtorFunc = getFunction(Dtor);
8102 if (!DtorFunc)
8103 return false;
8104 assert(DtorFunc->hasThisPointer());
8105 assert(DtorFunc->getNumParams() == 1);
8106 return this->emitCall(DtorFunc, 0, Loc);
8107}
8108/// When calling this, we have a pointer of the local-to-destroy
8109/// on the stack.
8110/// Emit destruction of record types (or arrays of record types).
8111template <class Emitter>
8112bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc,
8113 SourceInfo Loc) {
8114 assert(Desc);
8115 assert(!Desc->hasTrivialDtor());
8116
8117 // Arrays.
8118 if (Desc->isArray()) {
8119 const Descriptor *ElemDesc = Desc->ElemDesc;
8120 assert(ElemDesc);
8121
8122 unsigned N = Desc->getNumElems();
8123 if (N == 0)
8124 return this->emitPopPtr(Loc);
8125
8126 for (ssize_t I = N - 1; I >= 1; --I) {
8127 if (!this->emitConstUint64(I, Loc))
8128 return false;
8129 if (!this->emitArrayElemPtrUint64(Loc))
8130 return false;
8131 if (!this->emitDestructionPop(ElemDesc, Loc))
8132 return false;
8133 }
8134 // Last iteration, removes the instance pointer from the stack.
8135 if (!this->emitConstUint64(0, Loc))
8136 return false;
8137 if (!this->emitArrayElemPtrPopUint64(Loc))
8138 return false;
8139 return this->emitDestructionPop(ElemDesc, Loc);
8140 }
8141
8142 assert(Desc->ElemRecord);
8143 assert(!Desc->ElemRecord->hasTrivialDtor());
8144 return this->emitRecordDestructionPop(Desc->ElemRecord, Loc);
8145}
8146
8147/// Create a dummy pointer for the given decl (or expr) and
8148/// push a pointer to it on the stack.
8149template <class Emitter>
8150bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E, bool CU) {
8151 assert(!DiscardResult && "Should've been checked before");
8152 unsigned DummyID = P.getOrCreateDummy(D, CU);
8153
8154 if (!this->emitGetPtrGlobal(DummyID, E))
8155 return false;
8156 if (E->getType()->isVoidType())
8157 return true;
8158
8159 // Convert the dummy pointer to another pointer type if we have to.
8160 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
8161 if (isPtrType(PT))
8162 return this->emitDecayPtr(PT_Ptr, PT, E);
8163 return false;
8164 }
8165 return true;
8166}
8167
8168template <class Emitter>
8169bool Compiler<Emitter>::emitFloat(const APFloat &F, const Expr *E) {
8170 if (Floating::singleWord(F.getSemantics()))
8171 return this->emitConstFloat(Floating(F), E);
8172
8173 APInt I = F.bitcastToAPInt();
8174 return this->emitConstFloat(
8175 Floating(const_cast<uint64_t *>(I.getRawData()),
8176 llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
8177 E);
8178}
8179
8180// This function is constexpr if and only if To, From, and the types of
8181// all subobjects of To and From are types T such that...
8182// (3.1) - is_union_v<T> is false;
8183// (3.2) - is_pointer_v<T> is false;
8184// (3.3) - is_member_pointer_v<T> is false;
8185// (3.4) - is_volatile_v<T> is false; and
8186// (3.5) - T has no non-static data members of reference type
8187template <class Emitter>
8188bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
8189 const Expr *SubExpr = E->getSubExpr();
8190 QualType FromType = SubExpr->getType();
8191 QualType ToType = E->getType();
8192 OptPrimType ToT = classify(ToType);
8193
8194 assert(!ToType->isReferenceType());
8195
8196 // Prepare storage for the result in case we discard.
8197 if (DiscardResult && !Initializing && !ToT) {
8198 UnsignedOrNone LocalIndex = allocateLocal(E);
8199 if (!LocalIndex)
8200 return false;
8201 if (!this->emitGetPtrLocal(*LocalIndex, E))
8202 return false;
8203 }
8204
8205 // Get a pointer to the value-to-cast on the stack.
8206 // For CK_LValueToRValueBitCast, this is always an lvalue and
8207 // we later assume it to be one (i.e. a PT_Ptr). However,
8208 // we call this function for other utility methods where
8209 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
8210 if (SubExpr->isGLValue() || FromType->isVectorType()) {
8211 if (!this->visit(SubExpr))
8212 return false;
8213 } else if (OptPrimType FromT = classify(SubExpr)) {
8214 unsigned TempOffset =
8215 allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
8216 if (!this->visit(SubExpr))
8217 return false;
8218 if (!this->emitSetLocal(*FromT, TempOffset, E))
8219 return false;
8220 if (!this->emitGetPtrLocal(TempOffset, E))
8221 return false;
8222 } else {
8223 return false;
8224 }
8225
8226 if (!ToT) {
8227 if (!this->emitBitCast(E))
8228 return false;
8229 return DiscardResult ? this->emitPopPtr(E) : true;
8230 }
8231 assert(ToT);
8232
8233 const llvm::fltSemantics *TargetSemantics = nullptr;
8234 if (ToT == PT_Float)
8235 TargetSemantics = &Ctx.getFloatSemantics(ToType);
8236
8237 // Conversion to a primitive type. FromType can be another
8238 // primitive type, or a record/array.
8239 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
8240 ToType->isSpecificBuiltinType(BuiltinType::Char_U));
8241 uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
8242
8243 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
8244 ResultBitWidth, TargetSemantics,
8245 ToType.getTypePtr(), E))
8246 return false;
8247
8248 if (DiscardResult)
8249 return this->emitPop(*ToT, E);
8250
8251 return true;
8252}
8253
8254/// Replicate a scalar value into every scalar element of an aggregate.
8255/// The scalar is stored in a local at \p SrcOffset and a pointer to the
8256/// destination must be on top of the interpreter stack. Each element receives
8257/// the scalar, cast to its own type.
8258template <class Emitter>
8259bool Compiler<Emitter>::emitHLSLAggregateSplat(PrimType SrcT,
8260 unsigned SrcOffset,
8261 QualType DestType,
8262 const Expr *E) {
8263 // Vectors and matrices are treated as flat sequences of elements.
8264 unsigned NumElems = 0;
8265 QualType ElemType;
8266 if (const auto *VT = DestType->getAs<VectorType>()) {
8267 NumElems = VT->getNumElements();
8268 ElemType = VT->getElementType();
8269 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
8270 NumElems = MT->getNumElementsFlattened();
8271 ElemType = MT->getElementType();
8272 }
8273 if (NumElems > 0) {
8274 PrimType ElemT = classifyPrim(ElemType);
8275 for (unsigned I = 0; I != NumElems; ++I) {
8276 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8277 return false;
8278 if (!this->emitPrimCast(SrcT, ElemT, ElemType, E))
8279 return false;
8280 if (!this->emitInitElem(ElemT, I, E))
8281 return false;
8282 }
8283 return true;
8284 }
8285
8286 // Arrays: primitive elements are filled directly; composite elements
8287 // require recursion into each sub-aggregate.
8288 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
8289 const auto *CAT = cast<ConstantArrayType>(AT);
8290 QualType ArrElemType = CAT->getElementType();
8291 unsigned ArrSize = CAT->getZExtSize();
8292
8293 if (OptPrimType ElemT = classify(ArrElemType)) {
8294 for (unsigned I = 0; I != ArrSize; ++I) {
8295 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8296 return false;
8297 if (!this->emitPrimCast(SrcT, *ElemT, ArrElemType, E))
8298 return false;
8299 if (!this->emitInitElem(*ElemT, I, E))
8300 return false;
8301 }
8302 } else {
8303 for (unsigned I = 0; I != ArrSize; ++I) {
8304 if (!this->emitConstUint32(I, E))
8305 return false;
8306 if (!this->emitArrayElemPtrUint32(E))
8307 return false;
8308 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, ArrElemType, E))
8309 return false;
8310 if (!this->emitFinishInitPop(E))
8311 return false;
8312 }
8313 }
8314 return true;
8315 }
8316
8317 // Records: fill base classes first, then named fields in declaration
8318 // order.
8319 if (DestType->isRecordType()) {
8320 const Record *R = getRecord(DestType);
8321 if (!R)
8322 return false;
8323
8324 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8325 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8326 const Record::Base *B = R->getBase(BS.getType());
8327 assert(B);
8328 if (!this->emitGetPtrBase(B->Offset, E))
8329 return false;
8330 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, BS.getType(), E))
8331 return false;
8332 if (!this->emitFinishInitPop(E))
8333 return false;
8334 }
8335 }
8336
8337 for (const Record::Field &F : R->fields()) {
8338 if (F.isUnnamedBitField())
8339 continue;
8340
8341 QualType FieldType = F.Decl->getType();
8342 if (OptPrimType FieldT = classify(FieldType)) {
8343 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8344 return false;
8345 if (!this->emitPrimCast(SrcT, *FieldT, FieldType, E))
8346 return false;
8347 if (F.isBitField()) {
8348 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
8349 return false;
8350 } else {
8351 if (!this->emitInitField(*FieldT, F.Offset, E))
8352 return false;
8353 }
8354 } else {
8355 if (!this->emitGetPtrField(F.Offset, E))
8356 return false;
8357 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, FieldType, E))
8358 return false;
8359 if (!this->emitPopPtr(E))
8360 return false;
8361 }
8362 }
8363 return true;
8364 }
8365
8366 return false;
8367}
8368
8369/// Return the total number of scalar elements in a type. This is used
8370/// to cap how many source elements are extracted during an elementwise cast,
8371/// so we never flatten more than the destination can hold.
8372template <class Emitter>
8373unsigned Compiler<Emitter>::countHLSLFlatElements(QualType Ty) {
8374 // Vector and matrix types are treated as flat sequences of elements.
8375 if (const auto *VT = Ty->getAs<VectorType>())
8376 return VT->getNumElements();
8377 if (const auto *MT = Ty->getAs<ConstantMatrixType>())
8378 return MT->getNumElementsFlattened();
8379 // Arrays: total count is array size * scalar elements per element.
8380 if (const auto *AT = Ty->getAsArrayTypeUnsafe()) {
8381 const auto *CAT = cast<ConstantArrayType>(AT);
8382 return CAT->getZExtSize() * countHLSLFlatElements(CAT->getElementType());
8383 }
8384 // Records: sum scalar element counts of base classes and named fields.
8385 if (Ty->isRecordType()) {
8386 const Record *R = getRecord(Ty);
8387 if (!R)
8388 return 0;
8389 unsigned Count = 0;
8390 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8391 for (const CXXBaseSpecifier &BS : CXXRD->bases())
8392 Count += countHLSLFlatElements(BS.getType());
8393 }
8394 for (const Record::Field &F : R->fields()) {
8395 if (F.isUnnamedBitField())
8396 continue;
8397 Count += countHLSLFlatElements(F.Decl->getType());
8398 }
8399 return Count;
8400 }
8401 // Scalar primitive types contribute one element.
8402 if (canClassify(Ty))
8403 return 1;
8404 return 0;
8405}
8406
8407/// Walk a source aggregate and extract every scalar element into its own local
8408/// variable. The results are appended to \p Elements in declaration order,
8409/// stopping once \p MaxElements have been collected. A pointer to the
8410/// source aggregate must be stored in the local at \p SrcOffset.
8411template <class Emitter>
8412bool Compiler<Emitter>::emitHLSLFlattenAggregate(
8413 QualType SrcType, unsigned SrcOffset,
8414 SmallVectorImpl<HLSLFlatElement> &Elements, unsigned MaxElements,
8415 const Expr *E) {
8416
8417 // Save a scalar value from the stack into a new local and record it.
8418 auto saveToLocal = [&](PrimType T) -> bool {
8419 unsigned Offset = allocateLocalPrimitive(E, T, /*IsConst=*/true);
8420 if (!this->emitSetLocal(T, Offset, E))
8421 return false;
8422 Elements.push_back({Offset, T});
8423 return true;
8424 };
8425
8426 // Save a pointer from the stack into a new local for later use.
8427 auto savePtrToLocal = [&]() -> UnsignedOrNone {
8428 unsigned Offset = allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
8429 if (!this->emitSetLocal(PT_Ptr, Offset, E))
8430 return std::nullopt;
8431 return Offset;
8432 };
8433
8434 // Vectors and matrices are flat sequences of elements.
8435 unsigned NumElems = 0;
8436 QualType ElemType;
8437 if (const auto *VT = SrcType->getAs<VectorType>()) {
8438 NumElems = VT->getNumElements();
8439 ElemType = VT->getElementType();
8440 } else if (const auto *MT = SrcType->getAs<ConstantMatrixType>()) {
8441 NumElems = MT->getNumElementsFlattened();
8442 ElemType = MT->getElementType();
8443 }
8444 if (NumElems > 0) {
8445 PrimType ElemT = classifyPrim(ElemType);
8446 for (unsigned I = 0; I != NumElems && Elements.size() < MaxElements; ++I) {
8447 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8448 return false;
8449 if (!this->emitArrayElemPop(ElemT, I, E))
8450 return false;
8451 if (!saveToLocal(ElemT))
8452 return false;
8453 }
8454 return true;
8455 }
8456
8457 // Arrays: primitive elements are extracted directly; composite elements
8458 // require recursion into each sub-aggregate.
8459 if (const auto *AT = SrcType->getAsArrayTypeUnsafe()) {
8460 const auto *CAT = cast<ConstantArrayType>(AT);
8461 QualType ArrElemType = CAT->getElementType();
8462 unsigned ArrSize = CAT->getZExtSize();
8463
8464 if (OptPrimType ElemT = classify(ArrElemType)) {
8465 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
8466 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8467 return false;
8468 if (!this->emitArrayElemPop(*ElemT, I, E))
8469 return false;
8470 if (!saveToLocal(*ElemT))
8471 return false;
8472 }
8473 } else {
8474 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
8475 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8476 return false;
8477 if (!this->emitConstUint32(I, E))
8478 return false;
8479 if (!this->emitArrayElemPtrPopUint32(E))
8480 return false;
8481 UnsignedOrNone ElemPtrOffset = savePtrToLocal();
8482 if (!ElemPtrOffset)
8483 return false;
8484 if (!emitHLSLFlattenAggregate(ArrElemType, *ElemPtrOffset, Elements,
8485 MaxElements, E))
8486 return false;
8487 }
8488 }
8489 return true;
8490 }
8491
8492 // Records: base classes come first, then named fields in declaration
8493 // order.
8494 if (SrcType->isRecordType()) {
8495 const Record *R = getRecord(SrcType);
8496 if (!R)
8497 return false;
8498
8499 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8500 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8501 if (Elements.size() >= MaxElements)
8502 break;
8503 const Record::Base *B = R->getBase(BS.getType());
8504 assert(B);
8505 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8506 return false;
8507 if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, E))
8508 return false;
8509 UnsignedOrNone BasePtrOffset = savePtrToLocal();
8510 if (!BasePtrOffset)
8511 return false;
8512 if (!emitHLSLFlattenAggregate(BS.getType(), *BasePtrOffset, Elements,
8513 MaxElements, E))
8514 return false;
8515 }
8516 }
8517
8518 for (const Record::Field &F : R->fields()) {
8519 if (Elements.size() >= MaxElements)
8520 break;
8521 if (F.isUnnamedBitField())
8522 continue;
8523
8524 QualType FieldType = F.Decl->getType();
8525 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8526 return false;
8527 if (!this->emitGetPtrFieldPop(F.Offset, E))
8528 return false;
8529
8530 if (OptPrimType FieldT = classify(FieldType)) {
8531 if (!this->emitLoadPop(*FieldT, E))
8532 return false;
8533 if (!saveToLocal(*FieldT))
8534 return false;
8535 } else {
8536 UnsignedOrNone FieldPtrOffset = savePtrToLocal();
8537 if (!FieldPtrOffset)
8538 return false;
8539 if (!emitHLSLFlattenAggregate(FieldType, *FieldPtrOffset, Elements,
8540 MaxElements, E))
8541 return false;
8542 }
8543 }
8544 return true;
8545 }
8546
8547 return false;
8548}
8549
8550/// Populate an HLSL aggregate from a flat list of previously extracted source
8551/// elements, casting each to the corresponding destination element type.
8552/// \p ElemIdx tracks the current position in \p Elements and is advanced as
8553/// elements are consumed. A pointer to the destination must be on top of the
8554/// interpreter stack.
8555template <class Emitter>
8556bool Compiler<Emitter>::emitHLSLConstructAggregate(
8557 QualType DestType, ArrayRef<HLSLFlatElement> Elements, unsigned &ElemIdx,
8558 const Expr *E) {
8559
8560 // Consume the next source element, cast it, and leave it on the stack.
8561 auto loadAndCast = [&](PrimType DestT, QualType DestQT) -> bool {
8562 const auto &Src = Elements[ElemIdx++];
8563 if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E))
8564 return false;
8565 return this->emitPrimCast(Src.Type, DestT, DestQT, E);
8566 };
8567
8568 // Vectors and matrices are flat sequences of elements.
8569 unsigned NumElems = 0;
8570 QualType ElemType;
8571 if (const auto *VT = DestType->getAs<VectorType>()) {
8572 NumElems = VT->getNumElements();
8573 ElemType = VT->getElementType();
8574 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
8575 NumElems = MT->getNumElementsFlattened();
8576 ElemType = MT->getElementType();
8577 }
8578 if (NumElems > 0) {
8579 PrimType DestElemT = classifyPrim(ElemType);
8580 for (unsigned I = 0; I != NumElems; ++I) {
8581 if (!loadAndCast(DestElemT, ElemType))
8582 return false;
8583 if (!this->emitInitElem(DestElemT, I, E))
8584 return false;
8585 }
8586 return true;
8587 }
8588
8589 // Arrays: primitive elements are filled directly; composite elements
8590 // require recursion into each sub-aggregate.
8591 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
8592 const auto *CAT = cast<ConstantArrayType>(AT);
8593 QualType ArrElemType = CAT->getElementType();
8594 unsigned ArrSize = CAT->getZExtSize();
8595
8596 if (OptPrimType ElemT = classify(ArrElemType)) {
8597 for (unsigned I = 0; I != ArrSize; ++I) {
8598 if (!loadAndCast(*ElemT, ArrElemType))
8599 return false;
8600 if (!this->emitInitElem(*ElemT, I, E))
8601 return false;
8602 }
8603 } else {
8604 for (unsigned I = 0; I != ArrSize; ++I) {
8605 if (!this->emitConstUint32(I, E))
8606 return false;
8607 if (!this->emitArrayElemPtrUint32(E))
8608 return false;
8609 if (!emitHLSLConstructAggregate(ArrElemType, Elements, ElemIdx, E))
8610 return false;
8611 if (!this->emitFinishInitPop(E))
8612 return false;
8613 }
8614 }
8615 return true;
8616 }
8617
8618 // Records: base classes come first, then named fields in declaration
8619 // order.
8620 if (DestType->isRecordType()) {
8621 const Record *R = getRecord(DestType);
8622 if (!R)
8623 return false;
8624
8625 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8626 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8627 const Record::Base *B = R->getBase(BS.getType());
8628 assert(B);
8629 if (!this->emitGetPtrBase(B->Offset, E))
8630 return false;
8631 if (!emitHLSLConstructAggregate(BS.getType(), Elements, ElemIdx, E))
8632 return false;
8633 if (!this->emitFinishInitPop(E))
8634 return false;
8635 }
8636 }
8637
8638 for (const Record::Field &F : R->fields()) {
8639 if (F.isUnnamedBitField())
8640 continue;
8641
8642 QualType FieldType = F.Decl->getType();
8643 if (OptPrimType FieldT = classify(FieldType)) {
8644 if (!loadAndCast(*FieldT, FieldType))
8645 return false;
8646 if (F.isBitField()) {
8647 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
8648 return false;
8649 } else {
8650 if (!this->emitInitField(*FieldT, F.Offset, E))
8651 return false;
8652 }
8653 } else {
8654 if (!this->emitGetPtrField(F.Offset, E))
8655 return false;
8656 if (!emitHLSLConstructAggregate(FieldType, Elements, ElemIdx, E))
8657 return false;
8658 if (!this->emitPopPtr(E))
8659 return false;
8660 }
8661 }
8662 return true;
8663 }
8664
8665 return false;
8666}
8667
8668namespace clang {
8669namespace interp {
8670
8671template class Compiler<ByteCodeEmitter>;
8672template class Compiler<EvalEmitter>;
8673
8674} // namespace interp
8675} // 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:3182
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3418
llvm::APInt getValue() const
Returns an internal integer representation of the literal.
Definition Expr.h: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:2018
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2815
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3255
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4181
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4169
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3738
QualType getReturnType() const
Definition Decl.h:2863
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2792
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition Decl.h:2395
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition Decl.cpp:4305
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:3402
bool isDefaulted() const
Whether this function is defaulted.
Definition Decl.h:2403
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3802
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3175
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:3489
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:1808
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:4347
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:3739
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3840
bool isUnion() const
Definition Decl.h:3950
A template argument list.
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition ExprCXX.h:2900
bool getBoolValue() const
Definition ExprCXX.h:2951
const APValue & getAPValue() const
Definition ExprCXX.h:2956
bool isStoredAsBoolean() const
Definition ExprCXX.h:2947
The base class of the type hierarchy.
Definition TypeBase.h:1875
bool isVoidType() const
Definition TypeBase.h: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:3584
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:5577
QualType getType() const
Definition Value.cpp:238
Represents a variable declaration or definition.
Definition Decl.h:924
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1582
bool isInitCapture() const
Whether this variable is the implicit variable for a lambda init-capture.
Definition Decl.h:1591
APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
Definition Decl.cpp:2554
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition Decl.h:1296
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1239
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2629
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1206
const Expr * getInit() const
Definition Decl.h:1381
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1266
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1371
Represents a GCC generic vector type.
Definition TypeBase.h: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 visitAPValueInitializer(const APValue &Val, const Expr *E, QualType T)
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
bool VisitCallExpr(const CallExpr *E)
std::optional< uint64_t > ArrayIndex
Current argument index. Needed to emit ArrayInitIndexExpr.
Definition Compiler.h:473
bool VisitPseudoObjectExpr(const PseudoObjectExpr *E)
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E)
const Function * getFunction(const FunctionDecl *FD)
Returns a function for the given FunctionDecl.
bool VisitFixedPointBinOp(const BinaryOperator *E)
bool VisitCastExpr(const CastExpr *E)
Definition Compiler.cpp: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 visitAPValue(const APValue &Val, PrimType ValType, const Expr *E)
Visit an APValue.
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E)
bool VisitLogicalBinOp(const BinaryOperator *E)
bool visitCompoundStmt(const CompoundStmt *S)
Context & Ctx
Current compilation context.
Definition Compiler.h:135
bool visitDeclRef(const ValueDecl *D, const Expr *E)
Visit the given decl as if we have a reference to it.
bool visitBreakStmt(const BreakStmt *S)
bool visitExpr(const Expr *E, bool DestroyToplevelScope) override
bool visitForStmt(const ForStmt *S)
bool VisitDeclRefExpr(const DeclRefExpr *E)
bool VisitOpaqueValueExpr(const OpaqueValueExpr *E)
bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E)
bool VisitStmtExpr(const StmtExpr *E)
bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E)
Definition Compiler.cpp: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
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
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