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 if (OptPrimType InitT = classify(ElemType)) {
4106 if (!this->visitZeroInitializer(*InitT, ElemType, E))
4107 return false;
4108 if (!this->emitStorePop(*InitT, E))
4109 return false;
4110 } else {
4111 assert(ElemType->isArrayType());
4112 if (!this->visitZeroArrayInitializer(ElemType, E))
4113 return false;
4114 }
4115 } else if (DynamicInit) {
4116 if (OptPrimType InitT = classify(DynamicInit)) {
4117 if (!this->visit(DynamicInit))
4118 return false;
4119 if (!this->emitStorePop(*InitT, E))
4120 return false;
4121 } else {
4122 if (!this->visitInitializerPop(DynamicInit))
4123 return false;
4124 }
4125 } else if (ElemT) {
4126 if (!this->visitZeroInitializer(
4127 *ElemT, InitType->getAsArrayTypeUnsafe()->getElementType(),
4128 Init))
4129 return false;
4130 if (!this->emitStorePop(*ElemT, E))
4131 return false;
4132 } else {
4133 assert(CtorFunc);
4134 if (!this->emitCall(CtorFunc, 0, E))
4135 return false;
4136 }
4137
4138 // ++Iter;
4139 if (!this->emitGetPtrLocal(Iter, E))
4140 return false;
4141 if (!this->emitIncPop(SizeT, false, E))
4142 return false;
4143
4144 if (!this->jump(StartLabel, E))
4145 return false;
4146
4147 this->fallthrough(EndLabel);
4148 this->emitLabel(EndLabel);
4149 }
4150 }
4151 } else { // Non-array.
4152 if (PlacementDest) {
4153 if (!this->visit(PlacementDest))
4154 return false;
4155 if (!this->emitCheckNewTypeMismatch(E, E))
4156 return false;
4157
4158 } else {
4159 // Allocate just one element.
4160 if (!this->emitAlloc(Desc, E))
4161 return false;
4162 }
4163
4164 if (Init) {
4165 if (ElemT) {
4166 if (!this->visit(Init))
4167 return false;
4168
4169 if (!this->emitInit(*ElemT, E))
4170 return false;
4171 } else {
4172 // Composite.
4173 if (!this->visitInitializer(Init))
4174 return false;
4175 }
4176 }
4177 }
4178
4179 if (DiscardResult)
4180 return this->emitPopPtr(E);
4181
4182 return true;
4183}
4184
4185template <class Emitter>
4187 if (E->containsErrors())
4188 return false;
4189 const FunctionDecl *OperatorDelete = E->getOperatorDelete();
4190
4191 if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
4192 return this->emitInvalidNewDeleteExpr(E, E);
4193
4194 // Arg must be an lvalue.
4195 if (!this->visit(E->getArgument()))
4196 return false;
4197
4198 return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E);
4199}
4200
4201template <class Emitter>
4203 if (DiscardResult)
4204 return true;
4205
4206 const Function *Func = nullptr;
4207 if (const Function *F = Ctx.getOrCreateObjCBlock(E))
4208 Func = F;
4209
4210 if (!Func)
4211 return false;
4212 return this->emitGetFnPtr(Func, E);
4213}
4214
4215template <class Emitter>
4217 const Type *TypeInfoType = E->getType().getTypePtr();
4218
4219 auto canonType = [](const Type *T) {
4220 return T->getCanonicalTypeUnqualified().getTypePtr();
4221 };
4222
4223 if (!E->isPotentiallyEvaluated()) {
4224 if (DiscardResult)
4225 return true;
4226
4227 if (E->isTypeOperand())
4228 return this->emitGetTypeid(
4229 canonType(E->getTypeOperand(Ctx.getASTContext()).getTypePtr()),
4230 TypeInfoType, E);
4231
4232 return this->emitGetTypeid(
4233 canonType(E->getExprOperand()->getType().getTypePtr()), TypeInfoType,
4234 E);
4235 }
4236
4237 // Otherwise, we need to evaluate the expression operand.
4238 assert(E->getExprOperand());
4239 assert(E->getExprOperand()->isLValue());
4240
4241 if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E))
4242 return false;
4243
4244 if (!this->visit(E->getExprOperand()))
4245 return false;
4246
4247 if (!this->emitGetTypeidPtr(TypeInfoType, E))
4248 return false;
4249 if (DiscardResult)
4250 return this->emitPopPtr(E);
4251 return true;
4252}
4253
4254template <class Emitter>
4256 const ObjCDictionaryLiteral *E) {
4258 return this->emitDummyPtr(E, E);
4259 return this->emitError(E);
4260}
4261
4262template <class Emitter>
4265 return this->emitDummyPtr(E, E);
4266 return this->emitError(E);
4267}
4268
4269template <class Emitter>
4271 assert(Ctx.getLangOpts().CPlusPlus);
4272 return this->emitConstBool(E->getValue(), E);
4273}
4274
4275template <class Emitter>
4277 if (DiscardResult)
4278 return true;
4279 assert(!Initializing);
4280
4281 const MSGuidDecl *GuidDecl = E->getGuidDecl();
4282 const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl();
4283 assert(RD);
4284 // If the definiton of the result type is incomplete, just return a dummy.
4285 // If (and when) that is read from, we will fail, but not now.
4286 if (!RD->isCompleteDefinition())
4287 return this->emitDummyPtr(GuidDecl, E);
4288
4289 UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(GuidDecl);
4290 if (!GlobalIndex)
4291 return false;
4292 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
4293 return false;
4294
4295 assert(this->getRecord(E->getType()));
4296
4297 const APValue &V = GuidDecl->getAsAPValue();
4298 if (V.getKind() == APValue::None)
4299 return true;
4300
4301 assert(V.isStruct());
4302 assert(V.getStructNumBases() == 0);
4303 if (!this->visitAPValueInitializer(V, E, E->getType()))
4304 return false;
4305
4306 return this->emitFinishInit(E);
4307}
4308
4309template <class Emitter>
4311 assert(classifyPrim(E->getType()) == PT_Bool);
4312 if (E->isValueDependent())
4313 return false;
4314 if (DiscardResult)
4315 return true;
4316 return this->emitConstBool(E->isSatisfied(), E);
4317}
4318
4319template <class Emitter>
4321 const ConceptSpecializationExpr *E) {
4322 assert(classifyPrim(E->getType()) == PT_Bool);
4323 if (DiscardResult)
4324 return true;
4325 return this->emitConstBool(E->isSatisfied(), E);
4326}
4327
4328template <class Emitter>
4333
4334template <class Emitter>
4336
4337 for (const Expr *SemE : E->semantics()) {
4338 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {
4339 if (SemE == E->getResultExpr())
4340 return false;
4341
4342 if (OVE->isUnique())
4343 continue;
4344
4345 if (!this->discard(OVE))
4346 return false;
4347 } else if (SemE == E->getResultExpr()) {
4348 if (!this->delegate(SemE))
4349 return false;
4350 } else {
4351 if (!this->discard(SemE))
4352 return false;
4353 }
4354 }
4355 return true;
4356}
4357
4358template <class Emitter>
4362
4363template <class Emitter>
4365 return this->emitError(E);
4366}
4367
4368template <class Emitter>
4370 assert(E->getType()->isVoidPointerType());
4371 if (DiscardResult)
4372 return true;
4373
4374 return this->emitDummyPtr(E, E);
4375}
4376
4377template <class Emitter>
4378bool Compiler<Emitter>::emitVectorConversion(const Expr *Src, const Expr *E) {
4379 if (Src->containsErrors())
4380 return false;
4381
4382 const auto *VT = E->getType()->castAs<VectorType>();
4383 QualType ElemType = VT->getElementType();
4384 PrimType ElemT = classifyPrim(ElemType);
4385 QualType SrcType = Src->getType();
4386 PrimType SrcElemT = classifyVectorElementType(SrcType);
4387
4388 if (!Initializing) {
4389 UnsignedOrNone LocalIndex = allocateLocal(E);
4390 if (!LocalIndex)
4391 return false;
4392 if (!this->emitGetPtrLocal(*LocalIndex, E))
4393 return false;
4394 }
4395
4396 unsigned SrcOffset =
4397 this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
4398 if (!this->visit(Src))
4399 return false;
4400 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
4401 return false;
4402
4403 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
4404 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
4405 return false;
4406 if (!this->emitArrayElemPop(SrcElemT, I, E))
4407 return false;
4408
4409 // Cast to the desired result element type.
4410 if (SrcElemT != ElemT) {
4411 if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
4412 return false;
4413 } else if (ElemType->isFloatingType() && SrcType != ElemType) {
4414 const auto *TargetSemantics = &Ctx.getFloatSemantics(ElemType);
4415 if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E))
4416 return false;
4417 }
4418 if (!this->emitInitElem(ElemT, I, E))
4419 return false;
4420 }
4421 return true;
4422}
4423
4424template <class Emitter>
4426 return emitVectorConversion(E->getSrcExpr(), E);
4427}
4428
4429template <class Emitter>
4431 // FIXME: Unary shuffle with mask not currently supported.
4432 if (E->getNumSubExprs() == 2)
4433 return this->emitInvalid(E);
4434
4435 assert(E->getNumSubExprs() > 2);
4436
4437 const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
4438 const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
4439 PrimType ElemT = classifyPrim(VT->getElementType());
4440 unsigned NumInputElems = VT->getNumElements();
4441 unsigned NumOutputElems = E->getNumSubExprs() - 2;
4442 assert(NumOutputElems > 0);
4443
4444 if (!Initializing) {
4445 UnsignedOrNone LocalIndex = allocateLocal(E);
4446 if (!LocalIndex)
4447 return false;
4448 if (!this->emitGetPtrLocal(*LocalIndex, E))
4449 return false;
4450 }
4451
4452 // Save both input vectors to a local variable.
4453 unsigned VectorOffsets[2];
4454 for (unsigned I = 0; I != 2; ++I) {
4455 VectorOffsets[I] =
4456 this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
4457 if (!this->visit(Vecs[I]))
4458 return false;
4459 if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
4460 return false;
4461 }
4462 for (unsigned I = 0; I != NumOutputElems; ++I) {
4463 APSInt ShuffleIndex = E->getShuffleMaskIdx(I);
4464 assert(ShuffleIndex >= -1);
4465 if (ShuffleIndex == -1)
4466 return this->emitInvalidShuffleVectorIndex(I, E);
4467
4468 assert(ShuffleIndex < (NumInputElems * 2));
4469 if (!this->emitGetLocal(PT_Ptr,
4470 VectorOffsets[ShuffleIndex >= NumInputElems], E))
4471 return false;
4472 unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
4473 if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
4474 return false;
4475
4476 if (!this->emitInitElem(ElemT, I, E))
4477 return false;
4478 }
4479
4480 if (DiscardResult)
4481 return this->emitPopPtr(E);
4482
4483 return true;
4484}
4485
4486template <class Emitter>
4488 const ExtVectorElementExpr *E) {
4489 const Expr *Base = E->getBase();
4490 assert(
4491 Base->getType()->isVectorType() ||
4492 Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
4493
4495 E->getEncodedElementAccess(Indices);
4496
4497 if (Indices.size() == 1) {
4498 if (!this->visit(Base))
4499 return false;
4500
4501 if (E->isGLValue()) {
4502 if (!this->emitConstUint32(Indices[0], E))
4503 return false;
4504 return this->emitArrayElemPtrPop(PT_Uint32, E);
4505 }
4506 // Else, also load the value.
4507 return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
4508 }
4509
4510 // Create a local variable for the base.
4511 unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true);
4512 if (!this->visit(Base))
4513 return false;
4514 if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
4515 return false;
4516
4517 // Now the vector variable for the return value.
4518 if (!Initializing) {
4519 UnsignedOrNone ResultIndex = allocateLocal(E);
4520 if (!ResultIndex)
4521 return false;
4522 if (!this->emitGetPtrLocal(*ResultIndex, E))
4523 return false;
4524 }
4525
4526 assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
4527
4528 PrimType ElemT =
4530 uint32_t DstIndex = 0;
4531 for (uint32_t I : Indices) {
4532 if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
4533 return false;
4534 if (!this->emitArrayElemPop(ElemT, I, E))
4535 return false;
4536 if (!this->emitInitElem(ElemT, DstIndex, E))
4537 return false;
4538 ++DstIndex;
4539 }
4540
4541 // Leave the result pointer on the stack.
4542 assert(!DiscardResult);
4543 return true;
4544}
4545
4546template <class Emitter>
4548 const Expr *SubExpr = E->getSubExpr();
4550 return this->discard(SubExpr) && this->emitInvalid(E);
4551
4552 if (DiscardResult)
4553 return true;
4554
4555 assert(classifyPrim(E) == PT_Ptr);
4556 return this->emitDummyPtr(E, E);
4557}
4558
4559template <class Emitter>
4561 const CXXStdInitializerListExpr *E) {
4562 const Expr *SubExpr = E->getSubExpr();
4564 Ctx.getASTContext().getAsConstantArrayType(SubExpr->getType());
4565 const Record *R = getRecord(E->getType());
4566 assert(Initializing);
4567 assert(SubExpr->isGLValue());
4568
4569 if (!this->visit(SubExpr))
4570 return false;
4571 if (!this->emitConstUint8(0, E))
4572 return false;
4573 if (!this->emitArrayElemPtrPopUint8(E))
4574 return false;
4575 if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E))
4576 return false;
4577
4578 PrimType SecondFieldT = classifyPrim(R->getField(1u)->Decl->getType());
4579 if (isIntegerOrBoolType(SecondFieldT)) {
4580 if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E))
4581 return false;
4582 return this->emitInitField(SecondFieldT, R->getField(1u)->Offset, E);
4583 }
4584 assert(SecondFieldT == PT_Ptr);
4585
4586 if (!this->emitGetFieldPtr(R->getField(0u)->Offset, E))
4587 return false;
4588 if (!this->emitExpandPtr(E))
4589 return false;
4590 if (!this->emitConst(ArrayType->getSize(), PT_Uint64, E))
4591 return false;
4592 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
4593 return false;
4594 return this->emitInitFieldPtr(R->getField(1u)->Offset, E);
4595}
4596
4597template <class Emitter>
4599 LocalScope<Emitter> BS(this);
4600 llvm::SaveAndRestore StmtExprSAR(this->InStmtExpr, true);
4601
4602 const CompoundStmt *CS = E->getSubStmt();
4603 const Stmt *Result = CS->body_back();
4604 for (const Stmt *S : CS->body()) {
4605 if (S != Result) {
4606 if (!this->visitStmt(S))
4607 return false;
4608 continue;
4609 }
4610
4611 assert(S == Result);
4612 if (const Expr *ResultExpr = dyn_cast<Expr>(S))
4613 return this->delegate(ResultExpr);
4614 if (!this->visitStmt(S))
4615 return false;
4616 return this->emitUnsupported(E);
4617 }
4618
4619 return BS.destroyLocals();
4620}
4621
4622template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
4623 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
4624 /*NewInitializing=*/false, /*ToLValue=*/false);
4625 return this->Visit(E);
4626}
4627
4628template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
4629 // We're basically doing:
4630 // OptionScope<Emitter> Scope(this, DicardResult, Initializing, ToLValue);
4631 // but that's unnecessary of course.
4632 return this->Visit(E);
4633}
4634
4636 if (const auto *PE = dyn_cast<ParenExpr>(E))
4637 return stripCheckedDerivedToBaseCasts(PE->getSubExpr());
4638
4639 if (const auto *CE = dyn_cast<CastExpr>(E);
4640 CE &&
4641 (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp))
4642 return stripCheckedDerivedToBaseCasts(CE->getSubExpr());
4643
4644 return E;
4645}
4646
4647static const Expr *stripDerivedToBaseCasts(const Expr *E) {
4648 if (const auto *PE = dyn_cast<ParenExpr>(E))
4649 return stripDerivedToBaseCasts(PE->getSubExpr());
4650
4651 if (const auto *CE = dyn_cast<CastExpr>(E);
4652 CE && (CE->getCastKind() == CK_DerivedToBase ||
4653 CE->getCastKind() == CK_UncheckedDerivedToBase ||
4654 CE->getCastKind() == CK_NoOp))
4655 return stripDerivedToBaseCasts(CE->getSubExpr());
4656
4657 return E;
4658}
4659
4660template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
4661 if (E->getType().isNull())
4662 return false;
4663
4664 if (E->getType()->isVoidType())
4665 return this->discard(E);
4666
4667 // Create local variable to hold the return value.
4668 if (!E->isGLValue() && !canClassify(E->getType())) {
4669 UnsignedOrNone LocalIndex = allocateLocal(
4671 if (!LocalIndex)
4672 return false;
4673
4674 if (!this->emitGetPtrLocal(*LocalIndex, E))
4675 return false;
4676 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
4677 return this->visitInitializer(E);
4678 }
4679
4680 // Otherwise,we have a primitive return value, produce the value directly
4681 // and push it on the stack.
4682 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4683 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
4684 return this->Visit(E);
4685}
4686
4687template <class Emitter>
4689 assert(!canClassify(E->getType()));
4690
4691 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4692 /*NewInitializing=*/true, /*ToLValue=*/false);
4693 return this->Visit(E) && this->emitFinishInit(E);
4694}
4695
4696template <class Emitter>
4698 assert(!canClassify(E->getType()));
4699
4700 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4701 /*NewInitializing=*/true, /*ToLValue=*/false);
4702 return this->Visit(E) && this->emitFinishInitPop(E);
4703}
4704
4705template <class Emitter> bool Compiler<Emitter>::visitAsLValue(const Expr *E) {
4706 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4707 /*NewInitializing=*/false, /*ToLValue=*/true);
4708 return this->Visit(E);
4709}
4710
4711template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
4712 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4713 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
4714
4715 OptPrimType T = classify(E->getType());
4716 if (!T) {
4717 // Convert complex values to bool.
4718 if (E->getType()->isAnyComplexType()) {
4719 if (!this->visit(E))
4720 return false;
4721 return this->emitComplexBoolCast(E);
4722 }
4723 return false;
4724 }
4725
4726 if (!this->visit(E))
4727 return false;
4728
4729 if (T == PT_Bool)
4730 return true;
4731
4732 // Convert pointers to bool.
4733 if (T == PT_Ptr)
4734 return this->emitIsNonNullPtr(E);
4735
4736 // Or Floats.
4737 if (T == PT_Float)
4738 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
4739
4740 // Or anything else we can.
4741 return this->emitCast(*T, PT_Bool, E);
4742}
4743
4744template <class Emitter>
4745bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
4746 const Expr *E) {
4747 if (const auto *AT = QT->getAs<AtomicType>())
4748 QT = AT->getValueType();
4749
4750 switch (T) {
4751 case PT_Bool:
4752 return this->emitZeroBool(E);
4753 case PT_Sint8:
4754 return this->emitZeroSint8(E);
4755 case PT_Uint8:
4756 return this->emitZeroUint8(E);
4757 case PT_Sint16:
4758 return this->emitZeroSint16(E);
4759 case PT_Uint16:
4760 return this->emitZeroUint16(E);
4761 case PT_Sint32:
4762 return this->emitZeroSint32(E);
4763 case PT_Uint32:
4764 return this->emitZeroUint32(E);
4765 case PT_Sint64:
4766 return this->emitZeroSint64(E);
4767 case PT_Uint64:
4768 return this->emitZeroUint64(E);
4769 case PT_IntAP:
4770 return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
4771 case PT_IntAPS:
4772 return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
4773 case PT_Ptr:
4774 return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
4775 nullptr, E);
4776 case PT_MemberPtr:
4777 return this->emitNullMemberPtr(0, nullptr, E);
4778 case PT_Float: {
4779 APFloat F = APFloat::getZero(Ctx.getFloatSemantics(QT));
4780 return this->emitFloat(F, E);
4781 }
4782 case PT_FixedPoint: {
4783 auto Sem = Ctx.getASTContext().getFixedPointSemantics(QT);
4784 return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
4785 }
4786 }
4787 llvm_unreachable("unknown primitive type");
4788}
4789
4790template <class Emitter>
4791bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
4792 const Expr *E) {
4793 assert(E);
4794 assert(R);
4795 // Fields
4796 for (const Record::Field &Field : R->fields()) {
4797 if (Field.isUnnamedBitField())
4798 continue;
4799
4800 const Descriptor *D = Field.Desc;
4801 if (D->isPrimitive()) {
4802 QualType QT = D->getType();
4803 PrimType T = D->getPrimType();
4804 if (!this->visitZeroInitializer(T, QT, E))
4805 return false;
4806 if (R->isUnion()) {
4807 if (!this->emitInitFieldActivate(T, Field.Offset, E))
4808 return false;
4809 break;
4810 }
4811 if (!this->emitInitField(T, Field.Offset, E))
4812 return false;
4813 continue;
4814 }
4815
4816 if (!this->emitGetPtrField(Field.Offset, E))
4817 return false;
4818
4819 if (D->isPrimitiveArray()) {
4820 QualType ET = D->getElemQualType();
4821 PrimType T = D->getPrimType();
4822 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
4823 if (!this->visitZeroInitializer(T, ET, E))
4824 return false;
4825 if (!this->emitInitElem(T, I, E))
4826 return false;
4827 }
4828 } else if (D->isCompositeArray()) {
4829 // Can't be a vector or complex field.
4830 if (!this->visitZeroArrayInitializer(D->getType(), E))
4831 return false;
4832 } else if (D->isRecord()) {
4833 if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
4834 return false;
4835 } else
4836 return false;
4837
4838 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
4839 // object's first non-static named data member is zero-initialized
4840 if (R->isUnion()) {
4841 if (!this->emitFinishInitActivatePop(E))
4842 return false;
4843 break;
4844 }
4845 if (!this->emitFinishInitPop(E))
4846 return false;
4847 }
4848
4849 for (const Record::Base &B : R->bases()) {
4850 if (!this->emitGetPtrBase(B.Offset, E))
4851 return false;
4852 if (!this->visitZeroRecordInitializer(B.R, E))
4853 return false;
4854 if (!this->emitFinishInitPop(E))
4855 return false;
4856 }
4857
4858 // FIXME: Virtual bases.
4859
4860 return true;
4861}
4862
4863template <class Emitter>
4864bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
4865 assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
4866 const ArrayType *AT = T->getAsArrayTypeUnsafe();
4867 QualType ElemType = AT->getElementType();
4868 size_t NumElems = cast<ConstantArrayType>(AT)->getZExtSize();
4869
4870 if (OptPrimType ElemT = classify(ElemType)) {
4871 for (size_t I = 0; I != NumElems; ++I) {
4872 if (!this->visitZeroInitializer(*ElemT, ElemType, E))
4873 return false;
4874 if (!this->emitInitElem(*ElemT, I, E))
4875 return false;
4876 }
4877 return true;
4878 }
4879 if (ElemType->isRecordType()) {
4880 const Record *R = getRecord(ElemType);
4881 if (!R)
4882 return false;
4883
4884 for (size_t I = 0; I != NumElems; ++I) {
4885 if (!this->emitConstUint32(I, E))
4886 return false;
4887 if (!this->emitArrayElemPtr(PT_Uint32, E))
4888 return false;
4889 if (!this->visitZeroRecordInitializer(R, E))
4890 return false;
4891 if (!this->emitPopPtr(E))
4892 return false;
4893 }
4894 return true;
4895 }
4896 if (ElemType->isArrayType()) {
4897 for (size_t I = 0; I != NumElems; ++I) {
4898 if (!this->emitConstUint32(I, E))
4899 return false;
4900 if (!this->emitArrayElemPtr(PT_Uint32, E))
4901 return false;
4902 if (!this->visitZeroArrayInitializer(ElemType, E))
4903 return false;
4904 if (!this->emitPopPtr(E))
4905 return false;
4906 }
4907 return true;
4908 }
4909
4910 return false;
4911}
4912
4913template <class Emitter>
4914bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS,
4915 const Expr *E) {
4916 if (!canClassify(E->getType()))
4917 return false;
4918
4919 if (!this->visit(RHS))
4920 return false;
4921 if (!this->visit(LHS))
4922 return false;
4923
4924 if (LHS->getType().isVolatileQualified())
4925 return this->emitInvalidStore(LHS->getType().getTypePtr(), E);
4926
4927 // We don't support assignments in C.
4928 if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(E))
4929 return false;
4930
4931 PrimType RHT = classifyPrim(RHS);
4932 bool Activates = refersToUnion(LHS);
4933 bool BitField = LHS->refersToBitField();
4934
4935 if (!this->emitFlip(PT_Ptr, RHT, E))
4936 return false;
4937
4938 if (DiscardResult) {
4939 if (BitField && Activates)
4940 return this->emitStoreBitFieldActivatePop(RHT, E);
4941 if (BitField)
4942 return this->emitStoreBitFieldPop(RHT, E);
4943 if (Activates)
4944 return this->emitStoreActivatePop(RHT, E);
4945 // Otherwise, regular non-activating store.
4946 return this->emitStorePop(RHT, E);
4947 }
4948
4949 auto maybeLoad = [&](bool Result) -> bool {
4950 if (!Result)
4951 return false;
4952 // Assignments aren't necessarily lvalues in C.
4953 // Load from them in that case.
4954 if (!E->isLValue())
4955 return this->emitLoadPop(RHT, E);
4956 return true;
4957 };
4958
4959 if (BitField && Activates)
4960 return maybeLoad(this->emitStoreBitFieldActivate(RHT, E));
4961 if (BitField)
4962 return maybeLoad(this->emitStoreBitField(RHT, E));
4963 if (Activates)
4964 return maybeLoad(this->emitStoreActivate(RHT, E));
4965 // Otherwise, regular non-activating store.
4966 return maybeLoad(this->emitStore(RHT, E));
4967}
4968
4969template <class Emitter>
4970template <typename T>
4971bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, SourceInfo Info) {
4972 switch (Ty) {
4973 case PT_Sint8:
4974 return this->emitConstSint8(Value, Info);
4975 case PT_Uint8:
4976 return this->emitConstUint8(Value, Info);
4977 case PT_Sint16:
4978 return this->emitConstSint16(Value, Info);
4979 case PT_Uint16:
4980 return this->emitConstUint16(Value, Info);
4981 case PT_Sint32:
4982 return this->emitConstSint32(Value, Info);
4983 case PT_Uint32:
4984 return this->emitConstUint32(Value, Info);
4985 case PT_Sint64:
4986 return this->emitConstSint64(Value, Info);
4987 case PT_Uint64:
4988 return this->emitConstUint64(Value, Info);
4989 case PT_Bool:
4990 return this->emitConstBool(Value, Info);
4991 case PT_Ptr:
4992 case PT_MemberPtr:
4993 case PT_Float:
4994 case PT_IntAP:
4995 case PT_IntAPS:
4996 case PT_FixedPoint:
4997 llvm_unreachable("Invalid integral type");
4998 break;
4999 }
5000 llvm_unreachable("unknown primitive type");
5001}
5002
5003template <class Emitter>
5004template <typename T>
5005bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
5006 return this->emitConst(Value, classifyPrim(E->getType()), E);
5007}
5008
5009template <class Emitter>
5010bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
5011 SourceInfo Info) {
5012 if (Ty == PT_IntAPS)
5013 return this->emitConstIntAPS(Value, Info);
5014 if (Ty == PT_IntAP)
5015 return this->emitConstIntAP(Value, Info);
5016
5017 if (Value.isSigned())
5018 return this->emitConst(Value.getSExtValue(), Ty, Info);
5019 return this->emitConst(Value.getZExtValue(), Ty, Info);
5020}
5021
5022template <class Emitter>
5023bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty,
5024 SourceInfo Info) {
5025 if (Ty == PT_IntAPS)
5026 return this->emitConstIntAPS(Value, Info);
5027 if (Ty == PT_IntAP)
5028 return this->emitConstIntAP(Value, Info);
5029
5030 if (isSignedType(Ty))
5031 return this->emitConst(Value.getSExtValue(), Ty, Info);
5032 return this->emitConst(Value.getZExtValue(), Ty, Info);
5033}
5034
5035template <class Emitter>
5036bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
5037 return this->emitConst(Value, classifyPrim(E->getType()), E);
5038}
5039
5040template <class Emitter>
5042 bool IsConst,
5043 bool IsVolatile,
5044 ScopeKind SC) {
5045 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
5046 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
5047 // or isa<MaterializeTemporaryExpr>().
5048 Descriptor *D = P.createDescriptor(Src, Ty, nullptr, Descriptor::InlineDescMD,
5049 IsConst, isa<const Expr *>(Src),
5050 /*IsMutable=*/false, IsVolatile);
5052 Scope::Local Local = this->createLocal(D);
5053 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
5054 Locals.insert({VD, Local});
5055 VarScope->addForScopeKind(Local, SC);
5056 return Local.Offset;
5057}
5058
5059template <class Emitter>
5061 ScopeKind SC) {
5062 const ValueDecl *Key = nullptr;
5063 const Expr *Init = nullptr;
5064 bool IsTemporary = false;
5065 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
5066 Key = VD;
5067
5068 if (const auto *VarD = dyn_cast<VarDecl>(VD))
5069 Init = VarD->getInit();
5070 }
5071 if (auto *E = Src.dyn_cast<const Expr *>()) {
5072 IsTemporary = true;
5073 if (Ty.isNull())
5074 Ty = E->getType();
5075 }
5076
5077 Descriptor *D = P.createDescriptor(
5079 IsTemporary, /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(),
5080 Init);
5081 if (!D)
5082 return std::nullopt;
5084
5085 Scope::Local Local = this->createLocal(D);
5086 if (Key)
5087 Locals.insert({Key, Local});
5088 VarScope->addForScopeKind(Local, SC);
5089 return Local.Offset;
5090}
5091
5092template <class Emitter>
5094 QualType Ty = E->getType();
5095 assert(!Ty->isRecordType());
5096
5097 Descriptor *D = P.createDescriptor(
5099 /*IsTemporary=*/true);
5100
5101 if (!D)
5102 return std::nullopt;
5103
5104 Scope::Local Local = this->createLocal(D);
5106 assert(S);
5107 // Attach to topmost scope.
5108 while (S->getParent())
5109 S = S->getParent();
5110 assert(S && !S->getParent());
5111 S->addLocal(Local);
5112 return Local.Offset;
5113}
5114
5115template <class Emitter>
5117 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
5118 return PT->getPointeeType()->getAsCanonical<RecordType>();
5119 return Ty->getAsCanonical<RecordType>();
5120}
5121
5122template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
5123 if (const auto *RecordTy = getRecordTy(Ty))
5124 return getRecord(RecordTy->getDecl()->getDefinitionOrSelf());
5125 return nullptr;
5126}
5127
5128template <class Emitter>
5130 return P.getOrCreateRecord(RD);
5131}
5132
5133template <class Emitter>
5135 return Ctx.getOrCreateFunction(FD);
5136}
5137
5138template <class Emitter>
5139bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
5141
5142 auto maybeDestroyLocals = [&]() -> bool {
5143 if (DestroyToplevelScope)
5144 return RootScope.destroyLocals() && this->emitCheckAllocations(E);
5145 return this->emitCheckAllocations(E);
5146 };
5147
5148 // Void expressions.
5149 if (E->getType()->isVoidType()) {
5150 if (!visit(E))
5151 return false;
5152 return this->emitRetVoid(E) && maybeDestroyLocals();
5153 }
5154
5155 // Expressions with a primitive return type.
5156 if (OptPrimType T = classify(E)) {
5157 if (!visit(E))
5158 return false;
5159
5160 return this->emitRet(*T, E) && maybeDestroyLocals();
5161 }
5162
5163 // Expressions with a composite return type.
5164 // For us, that means everything we don't
5165 // have a PrimType for.
5166 if (UnsignedOrNone LocalOffset = this->allocateLocal(E)) {
5167 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalOffset));
5168 if (!this->emitGetPtrLocal(*LocalOffset, E))
5169 return false;
5170
5171 if (!visitInitializer(E))
5172 return false;
5173 // We are destroying the locals AFTER the Ret op.
5174 // The Ret op needs to copy the (alive) values, but the
5175 // destructors may still turn the entire expression invalid.
5176 return this->emitRetValue(E) && maybeDestroyLocals();
5177 }
5178
5179 return maybeDestroyLocals() && false;
5180}
5181
5182template <class Emitter>
5184 bool DestroyToplevelScope) {
5185 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5186 /*NewInitializing=*/false, /*ToLValue=*/true);
5187
5188 return this->visitExpr(E, DestroyToplevelScope);
5189}
5190
5191template <class Emitter>
5193
5194 auto R = this->visitVarDecl(VD, VD->getInit(), /*Toplevel=*/true);
5195
5196 if (R.notCreated())
5197 return R;
5198
5199 if (R)
5200 return true;
5201
5202 if (!R && Context::shouldBeGloballyIndexed(VD)) {
5203 if (auto GlobalIndex = P.getGlobal(VD)) {
5204 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5205 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5206
5208 GlobalBlock->invokeDtor();
5209 }
5210 }
5211
5212 return R;
5213}
5214
5215/// Toplevel visitDeclAndReturn().
5216/// We get here from evaluateAsInitializer().
5217/// We need to evaluate the initializer and return its value.
5218template <class Emitter>
5220 bool ConstantContext) {
5221 // We only create variables if we're evaluating in a constant context.
5222 // Otherwise, just evaluate the initializer and return it.
5223 if (!ConstantContext) {
5224 DeclScope<Emitter> LS(this, VD);
5225 if (!this->visit(Init))
5226 return false;
5227 return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
5228 LS.destroyLocals() && this->emitCheckAllocations(VD);
5229 }
5230
5231 LocalScope<Emitter> VDScope(this);
5232 if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true))
5233 return false;
5234
5235 OptPrimType VarT = classify(VD->getType());
5237 auto GlobalIndex = P.getGlobal(VD);
5238 assert(GlobalIndex); // visitVarDecl() didn't return false.
5239 if (VarT) {
5240 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
5241 return false;
5242 } else {
5243 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
5244 return false;
5245 }
5246 } else {
5247 auto Local = Locals.find(VD);
5248 assert(Local != Locals.end()); // Same here.
5249 if (VarT) {
5250 if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
5251 return false;
5252 } else {
5253 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
5254 return false;
5255 }
5256 }
5257
5258 // Return the value.
5259 if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
5260 // If the Ret above failed and this is a global variable, mark it as
5261 // uninitialized, even everything else succeeded.
5263 auto GlobalIndex = P.getGlobal(VD);
5264 assert(GlobalIndex);
5265 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5266 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5267
5269 GlobalBlock->invokeDtor();
5270 }
5271 return false;
5272 }
5273
5274 return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
5275}
5276
5277template <class Emitter>
5279 const Expr *Init,
5280 bool Toplevel) {
5281 QualType VarTy = VD->getType();
5282 // We don't know what to do with these, so just return false.
5283 if (VarTy.isNull())
5284 return false;
5285
5286 // This case is EvalEmitter-only. If we won't create any instructions for the
5287 // initializer anyway, don't bother creating the variable in the first place.
5288 if (!this->isActive())
5290
5291 OptPrimType VarT = classify(VD->getType());
5292
5293 if (Init && Init->isValueDependent())
5294 return false;
5295
5297 auto checkDecl = [&]() -> bool {
5298 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
5299 return !NeedsOp || this->emitCheckDecl(VD, VD);
5300 };
5301
5303 UnsignedOrNone GlobalIndex = P.getGlobal(VD);
5304 if (GlobalIndex) {
5305 // The global was previously created but the initializer failed.
5306 if (!P.getGlobal(*GlobalIndex)->isInitialized())
5307 return false;
5308 // We've already seen and initialized this global.
5309 if (P.isGlobalInitialized(*GlobalIndex))
5310 return checkDecl();
5311 // The previous attempt at initialization might've been unsuccessful,
5312 // so let's try this one.
5313 } else if ((GlobalIndex =
5314 P.createGlobal(VD, Init, VariablesAreConstexprUnknown))) {
5315 } else {
5316 return false;
5317 }
5318 if (!Init)
5319 return true;
5320
5321 if (!checkDecl())
5322 return false;
5323
5324 if (VarT) {
5325 if (!this->visit(Init))
5326 return false;
5327
5328 return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
5329 }
5330
5331 if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
5332 return false;
5333
5334 if (!this->emitStartInit(Init))
5335 return false;
5336
5337 if (!visitInitializer(Init))
5338 return false;
5339
5340 if (!this->emitEndInit(Init))
5341 return false;
5342
5343 return this->emitFinishInitGlobal(Init);
5344 }
5345 // Local variables.
5347
5348 if (VarT) {
5349 unsigned Offset = this->allocateLocalPrimitive(
5350 VD, *VarT, VarTy.isConstQualified(), VarTy.isVolatileQualified(),
5352
5353 if (!Init || Init->getType()->isVoidType())
5354 return true;
5355
5356 // If this is a toplevel declaration, create a scope for the
5357 // initializer.
5358 if (Toplevel) {
5360 if (!this->visit(Init))
5361 return false;
5362 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
5363 }
5364 if (!this->visit(Init))
5365 return false;
5366
5367 if (VarTy->isReferenceType()) {
5368 // [C++26][decl.ref]
5369 // The object designated by such a glvalue can be outside its lifetime
5370 // Because a null pointer value or a pointer past the end of an object
5371 // does not point to an object, a reference in a well-defined program
5372 // cannot refer to such things;
5373 assert(classifyPrim(VarTy) == PT_Ptr);
5374 if (!this->emitCheckRefInit(Init))
5375 return false;
5376 }
5377
5378 return this->emitSetLocal(*VarT, Offset, VD);
5379 }
5380 // Local composite variables.
5381 if (UnsignedOrNone Offset =
5382 this->allocateLocal(VD, VarTy, ScopeKind::Block)) {
5383 if (!Init)
5384 return true;
5385
5386 if (!this->emitGetPtrLocal(*Offset, Init))
5387 return false;
5388
5389 return visitInitializerPop(Init);
5390 }
5391 return false;
5392}
5393
5394template <class Emitter>
5396 assert(!canClassify(VD->getType()));
5397
5399 // Create a local variable to use as the instance.
5400 QualType Ty = VD->getType();
5401 Descriptor *D =
5402 P.createDescriptor(VD, Ty.getTypePtr(), Descriptor::InlineDescMD,
5403 /*IsConst=*/Ty.isConstQualified(),
5404 /*IsTemporary=*/false, /*IsMutable=*/false,
5405 /*IsVolatile=*/Ty.isVolatileQualified(), nullptr);
5406 if (!D)
5407 return false;
5408
5409 // FIXME: Would be nice if we didn't allocate the descriptor at all in this
5410 // case.
5411 if (D->hasTrivialDtor())
5412 return true;
5413
5414 Scope::Local Local = this->createLocal(D);
5415 Locals.insert({VD, Local});
5416 VarScope->addForScopeKind(Local, ScopeKind::Block);
5417
5418 if (!this->emitGetPtrLocal(Local.Offset, VD))
5419 return false;
5420
5421 if (!this->visitAPValueInitializer(Value, VD, Ty))
5422 return false;
5423
5424 return this->emitDestructionPop(D, VD);
5425}
5426
5427template <class Emitter>
5429 SourceInfo Info) {
5430 assert(!Val.isIndeterminate() && "Needs to be checked before");
5431 assert(!DiscardResult);
5432 if (Val.isInt())
5433 return this->emitConst(Val.getInt(), ValType, Info);
5434 if (Val.isFloat()) {
5435 APFloat F = Val.getFloat();
5436 return this->emitFloat(F, Info);
5437 }
5438
5439 if (Val.isMemberPointer()) {
5440 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl()) {
5441 if (!this->emitGetMemberPtr(MemberDecl, Info))
5442 return false;
5443
5444 bool IsDerived = Val.isMemberPointerToDerivedMember();
5445 // Apply the member pointer path.
5446 for (const CXXRecordDecl *PathEntry : Val.getMemberPointerPath()) {
5447 if (!this->emitCopyMemberPtrPath(PathEntry, IsDerived, Info))
5448 return false;
5449 }
5450
5451 return true;
5452 }
5453 return this->emitNullMemberPtr(0, nullptr, Info);
5454 }
5455
5456 if (Val.isLValue()) {
5457 if (Val.isNullPointer())
5458 return this->emitNull(ValType, 0, nullptr, Info);
5459
5462
5463 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
5464 return this->visit(BaseExpr);
5465 if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) {
5466 if (!this->visitDeclRef(VD, Info.asExpr()))
5467 return false;
5468
5469 QualType EntryType = VD->getType();
5470 for (auto &Entry : Path) {
5471 if (EntryType->isArrayType()) {
5472 uint64_t Index = Entry.getAsArrayIndex();
5473 QualType ElemType =
5474 EntryType->getAsArrayTypeUnsafe()->getElementType();
5475 if (!this->emitConst(Index, PT_Uint64, Info))
5476 return false;
5477 if (!this->emitArrayElemPtrPop(PT_Uint64, Info))
5478 return false;
5479 EntryType = ElemType;
5480 } else {
5481 assert(EntryType->isRecordType());
5482 const Record *EntryRecord = getRecord(EntryType);
5483 if (!EntryRecord) {
5484 assert(false);
5485
5486 return false;
5487 }
5488
5489 const Decl *BaseOrMember = Entry.getAsBaseOrMember().getPointer();
5490 if (const auto *FD = dyn_cast<FieldDecl>(BaseOrMember)) {
5491 unsigned EntryOffset = EntryRecord->getField(FD)->Offset;
5492 if (!this->emitGetPtrFieldPop(EntryOffset, Info))
5493 return false;
5494 EntryType = FD->getType();
5495 } else {
5496 const auto *Base = cast<CXXRecordDecl>(BaseOrMember);
5497 unsigned BaseOffset = EntryRecord->getBase(Base)->Offset;
5498 if (!this->emitGetPtrBasePop(BaseOffset, /*NullOK=*/false, Info))
5499 return false;
5500 EntryType = Ctx.getASTContext().getCanonicalTagType(Base);
5501 }
5502 }
5503 }
5504
5505 return true;
5506 }
5507 }
5508
5509 return false;
5510}
5511
5512template <class Emitter>
5514 SourceInfo Info, QualType T) {
5515 if (Val.isStruct()) {
5516 const Record *R = this->getRecord(T);
5517 assert(R);
5518 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
5519 const APValue &F = Val.getStructField(I);
5520 if (F.isIndeterminate())
5521 continue;
5522 const Record::Field *RF = R->getField(I);
5523 QualType FieldType = RF->Decl->getType();
5524
5525 // Fields.
5526 if (OptPrimType PT = classify(FieldType)) {
5527 if (!this->visitAPValue(F, *PT, Info))
5528 return false;
5529 if (!this->emitInitField(*PT, RF->Offset, Info))
5530 return false;
5531 } else {
5532 if (!this->emitGetPtrField(RF->Offset, Info))
5533 return false;
5534 if (!this->visitAPValueInitializer(F, Info, FieldType))
5535 return false;
5536 if (!this->emitFinishInitPop(Info))
5537 return false;
5538 }
5539 }
5540
5541 // Bases.
5542 for (unsigned I = 0, N = Val.getStructNumBases(); I != N; ++I) {
5543 // FIXME: APValue doesn't know about virtual bases.
5544 // We simply assume that if the APValue has more bases than the Record,
5545 // those additional bases must be virtual.
5546 if (I >= R->getNumBases())
5547 break;
5548 const APValue &B = Val.getStructBase(I);
5549 if (B.isIndeterminate())
5550 continue;
5551 const Record::Base *RB = R->getBase(I);
5552 QualType BaseType = Ctx.getASTContext().getCanonicalTagType(RB->Decl);
5553
5554 if (!this->emitGetPtrBase(RB->Offset, Info))
5555 return false;
5556 if (!this->visitAPValueInitializer(B, Info, BaseType))
5557 return false;
5558 if (!this->emitFinishInitPop(Info))
5559 return false;
5560 }
5561
5562 return true;
5563 }
5564 if (Val.isUnion()) {
5565 const FieldDecl *UnionField = Val.getUnionField();
5566 if (!UnionField)
5567 return true;
5568 const Record *R = this->getRecord(T);
5569 assert(R);
5570 const APValue &F = Val.getUnionValue();
5571 if (F.isIndeterminate())
5572 return true;
5573 const Record::Field *RF = R->getField(UnionField);
5574 QualType FieldType = RF->Decl->getType();
5575
5576 if (OptPrimType PT = classify(FieldType)) {
5577 if (!this->visitAPValue(F, *PT, Info))
5578 return false;
5579 if (RF->isBitField())
5580 return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(),
5581 Info);
5582 return this->emitInitFieldActivate(*PT, RF->Offset, Info);
5583 }
5584
5585 if (!this->emitGetPtrField(RF->Offset, Info))
5586 return false;
5587 if (!this->emitActivate(Info))
5588 return false;
5589 if (!this->visitAPValueInitializer(F, Info, FieldType))
5590 return false;
5591 return this->emitPopPtr(Info);
5592 }
5593 if (Val.isArray()) {
5594 unsigned InitializedElems = Val.getArrayInitializedElts();
5595 const auto *ArrType = T->getAsArrayTypeUnsafe();
5596 QualType ElemType = ArrType->getElementType();
5597 OptPrimType ElemT = classify(ElemType);
5598
5599 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
5600 const APValue &Elem = A >= InitializedElems
5601 ? Val.getArrayFiller()
5602 : Val.getArrayInitializedElt(A);
5603 if (Elem.isIndeterminate())
5604 continue;
5605
5606 if (ElemT) {
5607 if (!this->visitAPValue(Elem, *ElemT, Info))
5608 return false;
5609 if (!this->emitInitElem(*ElemT, A, Info))
5610 return false;
5611 } else {
5612 if (!this->emitConstUint32(A, Info))
5613 return false;
5614 if (!this->emitArrayElemPtrUint32(Info))
5615 return false;
5616 if (!this->visitAPValueInitializer(Elem, Info, ElemType))
5617 return false;
5618 if (!this->emitPopPtr(Info))
5619 return false;
5620 }
5621 }
5622 return true;
5623 }
5624 // TODO: Other types.
5625
5626 return false;
5627}
5628
5629template <class Emitter>
5631 unsigned BuiltinID) {
5632 if (BuiltinID == Builtin::BI__builtin_constant_p) {
5633 // Void argument is always invalid and harder to handle later.
5634 if (E->getArg(0)->getType()->isVoidType()) {
5635 if (DiscardResult)
5636 return true;
5637 return this->emitConst(0, E);
5638 }
5639
5640 if (!this->emitStartSpeculation(E))
5641 return false;
5642 LabelTy EndLabel = this->getLabel();
5643 if (!this->speculate(E, EndLabel))
5644 return false;
5645 if (!this->emitEndSpeculation(E))
5646 return false;
5647 this->fallthrough(EndLabel);
5648 if (DiscardResult)
5649 return this->emitPop(classifyPrim(E), E);
5650 return true;
5651 }
5652
5653 // For these, we're expected to ultimately return an APValue pointing
5654 // to the CallExpr. This is needed to get the correct codegen.
5655 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
5656 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
5657 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
5658 BuiltinID == Builtin::BI__builtin_function_start) {
5659 if (DiscardResult)
5660 return true;
5661 return this->emitDummyPtr(E, E);
5662 }
5663
5665 OptPrimType ReturnT = classify(E);
5666
5667 // Non-primitive return type. Prepare storage.
5668 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
5669 UnsignedOrNone LocalIndex = allocateLocal(E);
5670 if (!LocalIndex)
5671 return false;
5672 if (!this->emitGetPtrLocal(*LocalIndex, E))
5673 return false;
5674 }
5675
5676 // Prepare function arguments including special cases.
5677 switch (BuiltinID) {
5678 case Builtin::BI__builtin_object_size:
5679 case Builtin::BI__builtin_dynamic_object_size: {
5680 assert(E->getNumArgs() == 2);
5681 const Expr *Arg0 = E->getArg(0);
5682 if (Arg0->isGLValue()) {
5683 if (!this->visit(Arg0))
5684 return false;
5685
5686 } else {
5687 if (!this->visitAsLValue(Arg0))
5688 return false;
5689 }
5690 if (!this->visit(E->getArg(1)))
5691 return false;
5692
5693 } break;
5694 case Builtin::BI__assume:
5695 case Builtin::BI__builtin_assume:
5696 // Argument is not evaluated.
5697 break;
5698 case Builtin::BI__atomic_is_lock_free:
5699 case Builtin::BI__atomic_always_lock_free: {
5700 assert(E->getNumArgs() == 2);
5701 if (!this->visit(E->getArg(0)))
5702 return false;
5703 if (!this->visitAsLValue(E->getArg(1)))
5704 return false;
5705 } break;
5706
5707 default:
5708 if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
5709 // Put arguments on the stack.
5710 for (const auto *Arg : E->arguments()) {
5711 if (!this->visit(Arg))
5712 return false;
5713 }
5714 }
5715 }
5716
5717 if (!this->emitCallBI(E, BuiltinID, E))
5718 return false;
5719
5720 if (DiscardResult && !ReturnType->isVoidType())
5721 return this->emitPop(ReturnT.value_or(PT_Ptr), E);
5722
5723 return true;
5724}
5725
5726template <class Emitter>
5728 if (E->containsErrors())
5729 return false;
5730 const FunctionDecl *FuncDecl = E->getDirectCallee();
5731
5732 if (FuncDecl) {
5733 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
5734 return VisitBuiltinCallExpr(E, BuiltinID);
5735
5736 // Calls to replaceable operator new/operator delete.
5738 if (FuncDecl->getDeclName().isAnyOperatorNew())
5739 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
5740 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete ||
5741 FuncDecl->getDeclName().getCXXOverloadedOperator() ==
5742 OO_Array_Delete);
5743 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
5744 }
5745
5746 // Explicit calls to trivial destructors
5747 if (const auto *DD = dyn_cast<CXXDestructorDecl>(FuncDecl);
5748 DD && DD->isTrivial()) {
5749 const auto *MemberCall = cast<CXXMemberCallExpr>(E);
5750 if (!this->visit(MemberCall->getImplicitObjectArgument()))
5751 return false;
5752 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
5753 this->emitPopPtr(E);
5754 }
5755 }
5756
5757 LocalScope<Emitter> CallScope(this, ScopeKind::Call);
5758
5759 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
5761 bool HasRVO = !ReturnType->isVoidType() && !T;
5762
5763 if (HasRVO) {
5764 if (DiscardResult) {
5765 // If we need to discard the return value but the function returns its
5766 // value via an RVO pointer, we need to create one such pointer just
5767 // for this call.
5768 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5769 if (!this->emitGetPtrLocal(*LocalIndex, E))
5770 return false;
5771 }
5772 } else {
5773 // We need the result. Prepare a pointer to return or
5774 // dup the current one.
5775 if (!Initializing) {
5776 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5777 if (!this->emitGetPtrLocal(*LocalIndex, E))
5778 return false;
5779 }
5780 }
5781 if (!this->emitDupPtr(E))
5782 return false;
5783 }
5784 }
5785
5787
5788 bool IsAssignmentOperatorCall = false;
5789 bool ActivateLHS = false;
5790 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
5791 OCE && OCE->isAssignmentOp()) {
5792 // Just like with regular assignments, we need to special-case assignment
5793 // operators here and evaluate the RHS (the second arg) before the LHS (the
5794 // first arg). We fix this by using a Flip op later.
5795 assert(Args.size() == 2);
5796 const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl();
5797 ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor();
5798 IsAssignmentOperatorCall = true;
5799 std::reverse(Args.begin(), Args.end());
5800 }
5801 // Calling a static operator will still
5802 // pass the instance, but we don't need it.
5803 // Discard it here.
5804 if (isa<CXXOperatorCallExpr>(E)) {
5805 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
5806 MD && MD->isStatic()) {
5807 if (!this->discard(E->getArg(0)))
5808 return false;
5809 // Drop first arg.
5810 Args.erase(Args.begin());
5811 }
5812 }
5813
5814 bool Devirtualized = false;
5815 UnsignedOrNone CalleeOffset = std::nullopt;
5816 // Add the (optional, implicit) This pointer.
5817 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
5818 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
5819 // If we end up creating a CallPtr op for this, we need the base of the
5820 // member pointer as the instance pointer, and later extract the function
5821 // decl as the function pointer.
5822 const Expr *Callee = E->getCallee();
5823 CalleeOffset =
5824 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
5825 if (!this->visit(Callee))
5826 return false;
5827 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
5828 return false;
5829 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5830 return false;
5831 if (!this->emitGetMemberPtrBase(E))
5832 return false;
5833 } else {
5834 const auto *InstancePtr = MC->getImplicitObjectArgument();
5835 if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
5836 isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
5837 const auto *Stripped = stripCheckedDerivedToBaseCasts(InstancePtr);
5838 if (isa<CXXThisExpr>(Stripped)) {
5839 FuncDecl =
5840 cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
5841 Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5842 Devirtualized = true;
5843 if (!this->visit(Stripped))
5844 return false;
5845 } else {
5846 if (!this->visit(InstancePtr))
5847 return false;
5848 }
5849 } else {
5850 if (!this->visit(InstancePtr))
5851 return false;
5852 }
5853 }
5854 } else if (const auto *PD =
5855 dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
5856 if (!this->emitCheckPseudoDtor(E))
5857 return false;
5858 const Expr *Base = PD->getBase();
5859 // E.g. `using T = int; 0.~T();`.
5860 if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr)
5861 return this->discard(Base);
5862 if (!this->visit(Base))
5863 return false;
5864 return this->emitEndLifetimePop(E);
5865 } else if (!FuncDecl) {
5866 const Expr *Callee = E->getCallee();
5867 CalleeOffset =
5868 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
5869 if (!this->visit(Callee))
5870 return false;
5871 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
5872 return false;
5873 }
5874
5875 if (!this->visitCallArgs(Args, FuncDecl, ActivateLHS,
5877 return false;
5878
5879 // Undo the argument reversal we did earlier.
5880 if (IsAssignmentOperatorCall) {
5881 assert(Args.size() == 2);
5882 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
5883 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
5884 if (!this->emitFlip(Arg2T, Arg1T, E))
5885 return false;
5886 }
5887
5888 if (FuncDecl) {
5889 const Function *Func = getFunction(FuncDecl);
5890 if (!Func)
5891 return false;
5892
5893 // In error cases, the function may be called with fewer arguments than
5894 // parameters.
5895 if (E->getNumArgs() < Func->getNumWrittenParams())
5896 return false;
5897
5898 assert(HasRVO == Func->hasRVO());
5899
5900 bool HasQualifier = false;
5901 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
5902 HasQualifier = ME->hasQualifier();
5903
5904 bool IsVirtual = false;
5905 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
5906 IsVirtual = !Devirtualized && MD->isVirtual();
5907
5908 // In any case call the function. The return value will end up on the stack
5909 // and if the function has RVO, we already have the pointer on the stack to
5910 // write the result into.
5911 if (IsVirtual && !HasQualifier) {
5912 uint32_t VarArgSize = 0;
5913 unsigned NumParams =
5914 Func->getNumWrittenParams() +
5915 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisParam());
5916 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5917 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5918
5919 if (!this->emitCallVirt(Func, VarArgSize, E))
5920 return false;
5921 } else if (Func->isVariadic()) {
5922 uint32_t VarArgSize = 0;
5923 unsigned NumParams =
5924 Func->getNumWrittenParams() +
5925 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisParam());
5926 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5927 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5928 if (!this->emitCallVar(Func, VarArgSize, E))
5929 return false;
5930 } else {
5931 if (!this->emitCall(Func, 0, E))
5932 return false;
5933 }
5934 } else {
5935 // Indirect call. Visit the callee, which will leave a FunctionPointer on
5936 // the stack. Cleanup of the returned value if necessary will be done after
5937 // the function call completed.
5938
5939 // Sum the size of all args from the call expr.
5940 uint32_t ArgSize = 0;
5941 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
5942 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5943
5944 // Get the callee, either from a member pointer or function pointer saved in
5945 // CalleeOffset.
5946 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
5947 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5948 return false;
5949 if (!this->emitGetMemberPtrDecl(E))
5950 return false;
5951 } else {
5952 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
5953 return false;
5954 }
5955 if (!this->emitCallPtr(ArgSize, E, E))
5956 return false;
5957 }
5958
5959 // Cleanup for discarded return values.
5960 if (DiscardResult && !ReturnType->isVoidType() && T)
5961 return this->emitPop(*T, E) && CallScope.destroyLocals();
5962
5963 return CallScope.destroyLocals();
5964}
5965
5966template <class Emitter>
5968 SourceLocScope<Emitter> SLS(this, E);
5969
5970 return this->delegate(E->getExpr());
5971}
5972
5973template <class Emitter>
5975 SourceLocScope<Emitter> SLS(this, E);
5976
5977 return this->delegate(E->getExpr());
5978}
5979
5980template <class Emitter>
5982 if (DiscardResult)
5983 return true;
5984
5985 return this->emitConstBool(E->getValue(), E);
5986}
5987
5988template <class Emitter>
5990 const CXXNullPtrLiteralExpr *E) {
5991 if (DiscardResult)
5992 return true;
5993
5994 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
5995 return this->emitNullPtr(Val, nullptr, E);
5996}
5997
5998template <class Emitter>
6000 if (DiscardResult)
6001 return true;
6002
6003 assert(E->getType()->isIntegerType());
6004
6005 PrimType T = classifyPrim(E->getType());
6006 return this->emitZero(T, E);
6007}
6008
6009template <class Emitter>
6011 if (DiscardResult)
6012 return true;
6013
6014 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
6015 if (this->LambdaThisCapture.Offset > 0) {
6016 if (this->LambdaThisCapture.IsPtr)
6017 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
6018 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
6019 }
6020 }
6021
6022 // In some circumstances, the 'this' pointer does not actually refer to the
6023 // instance pointer of the current function frame, but e.g. to the declaration
6024 // currently being initialized. Here we emit the necessary instruction(s) for
6025 // this scenario.
6026 if (!InitStackActive || InitStack.empty())
6027 return this->emitThis(E);
6028
6029 // If our init stack is, for example:
6030 // 0 Stack: 3 (decl)
6031 // 1 Stack: 6 (init list)
6032 // 2 Stack: 1 (field)
6033 // 3 Stack: 6 (init list)
6034 // 4 Stack: 1 (field)
6035 //
6036 // We want to find the LAST element in it that's an init list,
6037 // which is marked with the K_InitList marker. The index right
6038 // before that points to an init list. We need to find the
6039 // elements before the K_InitList element that point to a base
6040 // (e.g. a decl or This), optionally followed by field, elem, etc.
6041 // In the example above, we want to emit elements [0..2].
6042 unsigned StartIndex = 0;
6043 unsigned EndIndex = 0;
6044 // Find the init list.
6045 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
6046 if (InitStack[StartIndex].Kind == InitLink::K_DIE) {
6047 EndIndex = StartIndex;
6048 --StartIndex;
6049 break;
6050 }
6051 }
6052
6053 // Walk backwards to find the base.
6054 for (; StartIndex > 0; --StartIndex) {
6055 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
6056 continue;
6057
6058 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
6059 InitStack[StartIndex].Kind != InitLink::K_Elem &&
6060 InitStack[StartIndex].Kind != InitLink::K_DIE)
6061 break;
6062 }
6063
6064 if (StartIndex == 0 && EndIndex == 0)
6065 EndIndex = InitStack.size() - 1;
6066
6067 assert(StartIndex < EndIndex);
6068
6069 // Emit the instructions.
6070 for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
6071 if (InitStack[I].Kind == InitLink::K_InitList ||
6072 InitStack[I].Kind == InitLink::K_DIE)
6073 continue;
6074 if (!InitStack[I].template emit<Emitter>(this, E))
6075 return false;
6076 }
6077 return true;
6078}
6079
6080template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
6081 switch (S->getStmtClass()) {
6082 case Stmt::CompoundStmtClass:
6084 case Stmt::DeclStmtClass:
6085 return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
6086 case Stmt::ReturnStmtClass:
6088 case Stmt::IfStmtClass:
6089 return visitIfStmt(cast<IfStmt>(S));
6090 case Stmt::WhileStmtClass:
6092 case Stmt::DoStmtClass:
6093 return visitDoStmt(cast<DoStmt>(S));
6094 case Stmt::ForStmtClass:
6095 return visitForStmt(cast<ForStmt>(S));
6096 case Stmt::CXXForRangeStmtClass:
6098 case Stmt::BreakStmtClass:
6100 case Stmt::ContinueStmtClass:
6102 case Stmt::SwitchStmtClass:
6104 case Stmt::CaseStmtClass:
6105 return visitCaseStmt(cast<CaseStmt>(S));
6106 case Stmt::DefaultStmtClass:
6108 case Stmt::AttributedStmtClass:
6110 case Stmt::CXXTryStmtClass:
6112 case Stmt::NullStmtClass:
6113 return true;
6114 // Always invalid statements.
6115 case Stmt::GCCAsmStmtClass:
6116 case Stmt::MSAsmStmtClass:
6117 case Stmt::GotoStmtClass:
6118 return this->emitInvalid(S);
6119 case Stmt::LabelStmtClass:
6120 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
6121 default: {
6122 if (const auto *E = dyn_cast<Expr>(S))
6123 return this->discard(E);
6124 return false;
6125 }
6126 }
6127}
6128
6129template <class Emitter>
6132 for (const auto *InnerStmt : S->body())
6133 if (!visitStmt(InnerStmt))
6134 return false;
6135 return Scope.destroyLocals();
6136}
6137
6138template <class Emitter>
6139bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
6140 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
6141 for (auto *BD : DD->flat_bindings())
6142 if (auto *KD = BD->getHoldingVar();
6143 KD && !this->visitVarDecl(KD, KD->getInit()))
6144 return false;
6145 }
6146 return true;
6147}
6148
6150 assert(FD);
6151 assert(FD->getParent()->isUnion());
6152 const CXXRecordDecl *CXXRD =
6154 return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
6155}
6156
6157template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
6158 for (;;) {
6159 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
6160 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
6161 FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
6162 return true;
6163 E = ME->getBase();
6164 continue;
6165 }
6166
6167 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
6168 E = ASE->getBase()->IgnoreImplicit();
6169 continue;
6170 }
6171
6172 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
6173 ICE && (ICE->getCastKind() == CK_NoOp ||
6174 ICE->getCastKind() == CK_DerivedToBase ||
6175 ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
6176 E = ICE->getSubExpr();
6177 continue;
6178 }
6179
6180 if (const auto *This = dyn_cast<CXXThisExpr>(E)) {
6181 const auto *ThisRecord =
6182 This->getType()->getPointeeType()->getAsRecordDecl();
6183 if (!ThisRecord->isUnion())
6184 return false;
6185 // Otherwise, always activate if we're in the ctor.
6186 if (const auto *Ctor =
6187 dyn_cast_if_present<CXXConstructorDecl>(CompilingFunction))
6188 return Ctor->getParent() == ThisRecord;
6189 return false;
6190 }
6191
6192 break;
6193 }
6194 return false;
6195}
6196
6197template <class Emitter>
6199 bool EvaluateConditionDecl) {
6200 for (const auto *D : DS->decls()) {
6203 continue;
6204
6205 const auto *VD = dyn_cast<VarDecl>(D);
6206 if (!VD)
6207 return false;
6208 if (!this->visitVarDecl(VD, VD->getInit()))
6209 return false;
6210
6211 // Register decomposition decl holding vars.
6212 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
6213 return false;
6214 }
6215
6216 return true;
6217}
6218
6219template <class Emitter>
6221 if (this->InStmtExpr)
6222 return this->emitUnsupported(RS);
6223
6224 if (const Expr *RE = RS->getRetValue()) {
6225 LocalScope<Emitter> RetScope(this);
6226 if (ReturnType) {
6227 // Primitive types are simply returned.
6228 if (!this->visit(RE))
6229 return false;
6230 this->emitCleanup();
6231 return this->emitRet(*ReturnType, RS);
6232 }
6233
6234 if (RE->getType()->isVoidType()) {
6235 if (!this->visit(RE))
6236 return false;
6237 } else {
6238 if (RE->containsErrors())
6239 return false;
6240
6242 // RVO - construct the value in the return location.
6243 if (!this->emitRVOPtr(RE))
6244 return false;
6245 if (!this->visitInitializerPop(RE))
6246 return false;
6247
6248 this->emitCleanup();
6249 return this->emitRetVoid(RS);
6250 }
6251 }
6252
6253 // Void return.
6254 this->emitCleanup();
6255 return this->emitRetVoid(RS);
6256}
6257
6258template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
6259 LocalScope<Emitter> IfScope(this);
6260
6261 auto visitChildStmt = [&](const Stmt *S) -> bool {
6262 LocalScope<Emitter> SScope(this);
6263 if (!visitStmt(S))
6264 return false;
6265 return SScope.destroyLocals();
6266 };
6267
6268 if (auto *CondInit = IS->getInit()) {
6269 if (!visitStmt(CondInit))
6270 return false;
6271 }
6272
6273 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) {
6274 if (!visitDeclStmt(CondDecl))
6275 return false;
6276 }
6277
6278 // Save ourselves compiling some code and the jumps, etc. if the condition is
6279 // stataically known to be either true or false. We could look at more cases
6280 // here, but I think all the ones that actually happen are using a
6281 // ConstantExpr.
6282 if (std::optional<bool> BoolValue = getBoolValue(IS->getCond())) {
6283 if (*BoolValue)
6284 return visitChildStmt(IS->getThen());
6285 if (const Stmt *Else = IS->getElse())
6286 return visitChildStmt(Else);
6287 return true;
6288 }
6289
6290 // Otherwise, compile the condition.
6291 if (IS->isNonNegatedConsteval()) {
6292 if (!this->emitIsConstantContext(IS))
6293 return false;
6294 } else if (IS->isNegatedConsteval()) {
6295 if (!this->emitIsConstantContext(IS))
6296 return false;
6297 if (!this->emitInv(IS))
6298 return false;
6299 } else {
6301 if (!this->visitBool(IS->getCond()))
6302 return false;
6303 if (!CondScope.destroyLocals())
6304 return false;
6305 }
6306
6307 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
6308 return false;
6309
6310 if (const Stmt *Else = IS->getElse()) {
6311 LabelTy LabelElse = this->getLabel();
6312 LabelTy LabelEnd = this->getLabel();
6313 if (!this->jumpFalse(LabelElse, IS))
6314 return false;
6315 if (!visitChildStmt(IS->getThen()))
6316 return false;
6317 if (!this->jump(LabelEnd, IS))
6318 return false;
6319 this->emitLabel(LabelElse);
6320 if (!visitChildStmt(Else))
6321 return false;
6322 this->emitLabel(LabelEnd);
6323 } else {
6324 LabelTy LabelEnd = this->getLabel();
6325 if (!this->jumpFalse(LabelEnd, IS))
6326 return false;
6327 if (!visitChildStmt(IS->getThen()))
6328 return false;
6329 this->emitLabel(LabelEnd);
6330 }
6331
6332 if (!IfScope.destroyLocals())
6333 return false;
6334
6335 return true;
6336}
6337
6338template <class Emitter>
6340 const Expr *Cond = S->getCond();
6341 const Stmt *Body = S->getBody();
6342
6343 LabelTy CondLabel = this->getLabel(); // Label before the condition.
6344 LabelTy EndLabel = this->getLabel(); // Label after the loop.
6345 LocalScope<Emitter> WholeLoopScope(this);
6346 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6347
6348 this->fallthrough(CondLabel);
6349 this->emitLabel(CondLabel);
6350
6351 // Start of the loop body {
6352 LocalScope<Emitter> CondScope(this);
6353
6354 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6355 if (!visitDeclStmt(CondDecl))
6356 return false;
6357 }
6358
6359 if (!this->visitBool(Cond))
6360 return false;
6361
6362 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6363 return false;
6364
6365 if (!this->jumpFalse(EndLabel, S))
6366 return false;
6367
6368 if (!this->visitStmt(Body))
6369 return false;
6370
6371 if (!CondScope.destroyLocals())
6372 return false;
6373 // } End of loop body.
6374
6375 if (!this->jump(CondLabel, S))
6376 return false;
6377 this->fallthrough(EndLabel);
6378 this->emitLabel(EndLabel);
6379
6380 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6381}
6382
6383template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
6384 const Expr *Cond = S->getCond();
6385 const Stmt *Body = S->getBody();
6386
6387 LabelTy StartLabel = this->getLabel();
6388 LabelTy EndLabel = this->getLabel();
6389 LabelTy CondLabel = this->getLabel();
6390 LocalScope<Emitter> WholeLoopScope(this);
6391 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6392
6393 this->fallthrough(StartLabel);
6394 this->emitLabel(StartLabel);
6395
6396 {
6397 LocalScope<Emitter> CondScope(this);
6398 if (!this->visitStmt(Body))
6399 return false;
6400 this->fallthrough(CondLabel);
6401 this->emitLabel(CondLabel);
6402 if (!this->visitBool(Cond))
6403 return false;
6404
6405 if (!CondScope.destroyLocals())
6406 return false;
6407 }
6408 if (!this->jumpTrue(StartLabel, S))
6409 return false;
6410
6411 this->fallthrough(EndLabel);
6412 this->emitLabel(EndLabel);
6413 return WholeLoopScope.destroyLocals();
6414}
6415
6416template <class Emitter>
6418 // for (Init; Cond; Inc) { Body }
6419 const Stmt *Init = S->getInit();
6420 const Expr *Cond = S->getCond();
6421 const Expr *Inc = S->getInc();
6422 const Stmt *Body = S->getBody();
6423
6424 LabelTy EndLabel = this->getLabel();
6425 LabelTy CondLabel = this->getLabel();
6426 LabelTy IncLabel = this->getLabel();
6427
6428 LocalScope<Emitter> WholeLoopScope(this);
6429 if (Init && !this->visitStmt(Init))
6430 return false;
6431
6432 // Start of the loop body {
6433 this->fallthrough(CondLabel);
6434 this->emitLabel(CondLabel);
6435
6436 LocalScope<Emitter> CondScope(this);
6437 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6438 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6439 if (!visitDeclStmt(CondDecl))
6440 return false;
6441 }
6442
6443 if (Cond) {
6444 if (!this->visitBool(Cond))
6445 return false;
6446 if (!this->jumpFalse(EndLabel, S))
6447 return false;
6448 }
6449 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6450 return false;
6451
6452 if (Body && !this->visitStmt(Body))
6453 return false;
6454
6455 this->fallthrough(IncLabel);
6456 this->emitLabel(IncLabel);
6457 if (Inc && !this->discard(Inc))
6458 return false;
6459
6460 if (!CondScope.destroyLocals())
6461 return false;
6462 if (!this->jump(CondLabel, S))
6463 return false;
6464 // } End of loop body.
6465
6466 this->emitLabel(EndLabel);
6467 // If we jumped out of the loop above, we still need to clean up the condition
6468 // scope.
6469 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6470}
6471
6472template <class Emitter>
6474 const Stmt *Init = S->getInit();
6475 const Expr *Cond = S->getCond();
6476 const Expr *Inc = S->getInc();
6477 const Stmt *Body = S->getBody();
6478 const Stmt *BeginStmt = S->getBeginStmt();
6479 const Stmt *RangeStmt = S->getRangeStmt();
6480 const Stmt *EndStmt = S->getEndStmt();
6481
6482 LabelTy EndLabel = this->getLabel();
6483 LabelTy CondLabel = this->getLabel();
6484 LabelTy IncLabel = this->getLabel();
6485 LocalScope<Emitter> WholeLoopScope(this);
6486 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6487
6488 // Emit declarations needed in the loop.
6489 if (Init && !this->visitStmt(Init))
6490 return false;
6491 if (!this->visitStmt(RangeStmt))
6492 return false;
6493 if (!this->visitStmt(BeginStmt))
6494 return false;
6495 if (!this->visitStmt(EndStmt))
6496 return false;
6497
6498 // Now the condition as well as the loop variable assignment.
6499 this->fallthrough(CondLabel);
6500 this->emitLabel(CondLabel);
6501 if (!this->visitBool(Cond))
6502 return false;
6503 if (!this->jumpFalse(EndLabel, S))
6504 return false;
6505
6506 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
6507 return false;
6508
6509 // Body.
6510 {
6511 if (!this->visitStmt(Body))
6512 return false;
6513
6514 this->fallthrough(IncLabel);
6515 this->emitLabel(IncLabel);
6516 if (!this->discard(Inc))
6517 return false;
6518 }
6519
6520 if (!this->jump(CondLabel, S))
6521 return false;
6522
6523 this->fallthrough(EndLabel);
6524 this->emitLabel(EndLabel);
6525 return WholeLoopScope.destroyLocals();
6526}
6527
6528template <class Emitter>
6530 if (LabelInfoStack.empty())
6531 return false;
6532
6533 OptLabelTy TargetLabel = std::nullopt;
6534 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6535 const VariableScope<Emitter> *BreakScope = nullptr;
6536
6537 if (!TargetLoop) {
6538 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6539 if (LI.BreakLabel) {
6540 TargetLabel = *LI.BreakLabel;
6541 BreakScope = LI.BreakOrContinueScope;
6542 break;
6543 }
6544 }
6545 } else {
6546 for (auto LI : LabelInfoStack) {
6547 if (LI.Name == TargetLoop) {
6548 TargetLabel = *LI.BreakLabel;
6549 BreakScope = LI.BreakOrContinueScope;
6550 break;
6551 }
6552 }
6553 }
6554
6555 // Faulty break statement (e.g. label redefined or named loops disabled).
6556 if (!TargetLabel)
6557 return false;
6558
6559 for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope;
6560 C = C->getParent()) {
6561 if (!C->destroyLocals())
6562 return false;
6563 }
6564
6565 return this->jump(*TargetLabel, S);
6566}
6567
6568template <class Emitter>
6570 if (LabelInfoStack.empty())
6571 return false;
6572
6573 OptLabelTy TargetLabel = std::nullopt;
6574 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6575 const VariableScope<Emitter> *ContinueScope = nullptr;
6576
6577 if (!TargetLoop) {
6578 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6579 if (LI.ContinueLabel) {
6580 TargetLabel = *LI.ContinueLabel;
6581 ContinueScope = LI.BreakOrContinueScope;
6582 break;
6583 }
6584 }
6585 } else {
6586 for (auto LI : LabelInfoStack) {
6587 if (LI.Name == TargetLoop) {
6588 TargetLabel = *LI.ContinueLabel;
6589 ContinueScope = LI.BreakOrContinueScope;
6590 break;
6591 }
6592 }
6593 }
6594 assert(TargetLabel);
6595
6596 for (VariableScope<Emitter> *C = VarScope; C != ContinueScope;
6597 C = C->getParent()) {
6598 if (!C->destroyLocals())
6599 return false;
6600 }
6601
6602 return this->jump(*TargetLabel, S);
6603}
6604
6605template <class Emitter>
6607 const Expr *Cond = S->getCond();
6608 if (Cond->containsErrors())
6609 return false;
6610
6611 PrimType CondT = this->classifyPrim(Cond->getType());
6612 LocalScope<Emitter> LS(this);
6613 llvm::SaveAndRestore StmtExprSAR(this->SwitchInStmtExpr, this->InStmtExpr);
6614
6615 LabelTy EndLabel = this->getLabel();
6616 UnsignedOrNone DefaultLabel = std::nullopt;
6617 unsigned CondVar =
6618 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
6619
6620 if (const auto *CondInit = S->getInit())
6621 if (!visitStmt(CondInit))
6622 return false;
6623
6624 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
6625 if (!visitDeclStmt(CondDecl))
6626 return false;
6627
6628 // Initialize condition variable.
6629 if (!this->visit(Cond))
6630 return false;
6631 if (!this->emitSetLocal(CondT, CondVar, S))
6632 return false;
6633
6634 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6635 return false;
6636
6638 // Create labels and comparison ops for all case statements.
6639 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
6640 SC = SC->getNextSwitchCase()) {
6641 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
6642 CaseLabels[SC] = this->getLabel();
6643
6644 if (CS->caseStmtIsGNURange()) {
6645 LabelTy EndOfRangeCheck = this->getLabel();
6646 const Expr *Low = CS->getLHS();
6647 const Expr *High = CS->getRHS();
6648 if (Low->isValueDependent() || High->isValueDependent())
6649 return false;
6650
6651 if (!this->emitGetLocal(CondT, CondVar, CS))
6652 return false;
6653 if (!this->visit(Low))
6654 return false;
6655 PrimType LT = this->classifyPrim(Low->getType());
6656 if (!this->emitGE(LT, S))
6657 return false;
6658 if (!this->jumpFalse(EndOfRangeCheck, S))
6659 return false;
6660
6661 if (!this->emitGetLocal(CondT, CondVar, CS))
6662 return false;
6663 if (!this->visit(High))
6664 return false;
6665 PrimType HT = this->classifyPrim(High->getType());
6666 if (!this->emitLE(HT, S))
6667 return false;
6668 if (!this->jumpTrue(CaseLabels[CS], S))
6669 return false;
6670 this->emitLabel(EndOfRangeCheck);
6671 continue;
6672 }
6673
6674 const Expr *Value = CS->getLHS();
6675 if (Value->isValueDependent())
6676 return false;
6677 PrimType ValueT = this->classifyPrim(Value->getType());
6678
6679 // Compare the case statement's value to the switch condition.
6680 if (!this->emitGetLocal(CondT, CondVar, CS))
6681 return false;
6682 if (!this->visit(Value))
6683 return false;
6684
6685 // Compare and jump to the case label.
6686 if (!this->emitEQ(ValueT, S))
6687 return false;
6688 if (!this->jumpTrue(CaseLabels[CS], S))
6689 return false;
6690 } else {
6691 assert(!DefaultLabel);
6692 DefaultLabel = this->getLabel();
6693 }
6694 }
6695
6696 // If none of the conditions above were true, fall through to the default
6697 // statement or jump after the switch statement.
6698 if (DefaultLabel) {
6699 if (!this->jump(*DefaultLabel, S))
6700 return false;
6701 } else {
6702 if (!this->jump(EndLabel, S))
6703 return false;
6704 }
6705
6706 SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel,
6707 DefaultLabel);
6708 if (!this->visitStmt(S->getBody()))
6709 return false;
6710 this->fallthrough(EndLabel);
6711 this->emitLabel(EndLabel);
6712
6713 return LS.destroyLocals();
6714}
6715
6716template <class Emitter>
6718 this->fallthrough(CaseLabels[S]);
6719 this->emitLabel(CaseLabels[S]);
6720
6721 // We can't jump from an outer switch statement to a case label
6722 // that's inside a StmtExpr.
6723 if (this->InStmtExpr && !this->SwitchInStmtExpr)
6724 return this->emitUnsupported(S);
6725
6726 return this->visitStmt(S->getSubStmt());
6727}
6728
6729template <class Emitter>
6731 if (LabelInfoStack.empty())
6732 return false;
6733
6734 LabelTy DefaultLabel;
6735 for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) {
6736 if (LI.DefaultLabel) {
6737 DefaultLabel = *LI.DefaultLabel;
6738 break;
6739 }
6740 }
6741
6742 this->emitLabel(DefaultLabel);
6743 return this->visitStmt(S->getSubStmt());
6744}
6745
6746template <class Emitter>
6748 const Stmt *SubStmt = S->getSubStmt();
6749
6750 bool IsMSVCConstexprAttr = isa<ReturnStmt>(SubStmt) &&
6752
6753 if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S))
6754 return false;
6755
6756 if (this->Ctx.getLangOpts().CXXAssumptions &&
6757 !this->Ctx.getLangOpts().MSVCCompat) {
6758 for (const Attr *A : S->getAttrs()) {
6759 auto *AA = dyn_cast<CXXAssumeAttr>(A);
6760 if (!AA)
6761 continue;
6762
6763 assert(isa<NullStmt>(SubStmt));
6764
6765 const Expr *Assumption = AA->getAssumption();
6766 if (Assumption->isValueDependent())
6767 return false;
6768
6769 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
6770 continue;
6771
6772 // Evaluate assumption.
6773 if (!this->visitBool(Assumption))
6774 return false;
6775
6776 if (!this->emitAssume(Assumption))
6777 return false;
6778 }
6779 }
6780
6781 // Ignore other attributes.
6782 if (!this->visitStmt(SubStmt))
6783 return false;
6784
6785 if (IsMSVCConstexprAttr)
6786 return this->emitPopMSVCCE(S);
6787 return true;
6788}
6789
6790template <class Emitter>
6792 // Ignore all handlers.
6793 return this->visitStmt(S->getTryBlock());
6794}
6795
6796template <class Emitter>
6797bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
6798 assert(MD->isLambdaStaticInvoker());
6799 assert(MD->hasBody());
6800 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
6801
6802 const CXXRecordDecl *ClosureClass = MD->getParent();
6803 const FunctionDecl *LambdaCallOp;
6804 assert(ClosureClass->captures().empty());
6805 if (ClosureClass->isGenericLambda()) {
6806 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6807 assert(MD->isFunctionTemplateSpecialization() &&
6808 "A generic lambda's static-invoker function must be a "
6809 "template specialization");
6811 FunctionTemplateDecl *CallOpTemplate =
6812 LambdaCallOp->getDescribedFunctionTemplate();
6813 void *InsertPos = nullptr;
6814 const FunctionDecl *CorrespondingCallOpSpecialization =
6815 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
6816 assert(CorrespondingCallOpSpecialization);
6817 LambdaCallOp = CorrespondingCallOpSpecialization;
6818 } else {
6819 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6820 }
6821 assert(ClosureClass->captures().empty());
6822 const Function *Func = this->getFunction(LambdaCallOp);
6823 if (!Func)
6824 return false;
6825 assert(Func->hasThisPointer());
6826 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
6827
6828 if (Func->hasRVO()) {
6829 if (!this->emitRVOPtr(MD))
6830 return false;
6831 }
6832
6833 // The lambda call operator needs an instance pointer, but we don't have
6834 // one here, and we don't need one either because the lambda cannot have
6835 // any captures, as verified above. Emit a null pointer. This is then
6836 // special-cased when interpreting to not emit any misleading diagnostics.
6837 if (!this->emitNullPtr(0, nullptr, MD))
6838 return false;
6839
6840 // Forward all arguments from the static invoker to the lambda call operator.
6841 for (const ParmVarDecl *PVD : MD->parameters()) {
6842 auto It = this->Params.find(PVD);
6843 assert(It != this->Params.end());
6844
6845 // We do the lvalue-to-rvalue conversion manually here, so no need
6846 // to care about references.
6847 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
6848 if (!this->emitGetParam(ParamType, It->second.Index, MD))
6849 return false;
6850 }
6851
6852 if (!this->emitCall(Func, 0, LambdaCallOp))
6853 return false;
6854
6855 this->emitCleanup();
6856 if (ReturnType)
6857 return this->emitRet(*ReturnType, MD);
6858
6859 // Nothing to do, since we emitted the RVO pointer above.
6860 return this->emitRetVoid(MD);
6861}
6862
6863template <class Emitter>
6864bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
6865 if (Ctx.getLangOpts().CPlusPlus23)
6866 return true;
6867
6868 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
6869 return true;
6870
6871 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
6872}
6873
6875 const Expr *InitExpr = Init->getInit();
6876
6877 if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
6878 !isa<CXXConstructExpr>(InitExpr))
6879 return true;
6880
6881 if (const auto *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
6882 const CXXConstructorDecl *Ctor = CE->getConstructor();
6883 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
6884 Ctor->isTrivial())
6885 return true;
6886 }
6887
6888 return false;
6889}
6890
6891template <class Emitter>
6892bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
6893 assert(!ReturnType);
6894
6895 // Only start the lifetime of the instance pointer.
6896 if (!this->emitStartThisLifetime1(Ctor))
6897 return false;
6898
6899 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
6900 const Expr *InitExpr,
6901 bool Activate = false) -> bool {
6902 // We don't know what to do with these, so just return false.
6903 if (InitExpr->getType().isNull())
6904 return false;
6905
6906 if (OptPrimType T = this->classify(InitExpr)) {
6907 if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
6908 return false;
6909
6910 if (!this->visit(InitExpr))
6911 return false;
6912
6913 if (F->isBitField())
6914 return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(),
6915 InitExpr);
6916 return this->emitInitThisField(*T, FieldOffset, InitExpr);
6917 }
6918 // Non-primitive case. Get a pointer to the field-to-initialize
6919 // on the stack and call visitInitialzer() for it.
6920 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
6921 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
6922 return false;
6923
6924 if (Activate && !this->emitActivate(InitExpr))
6925 return false;
6926
6927 return this->visitInitializerPop(InitExpr);
6928 };
6929
6930 const RecordDecl *RD = Ctor->getParent();
6931 const Record *R = this->getRecord(RD);
6932 if (!R)
6933 return false;
6934 bool IsUnion = R->isUnion();
6935
6936 // Default union copy and move ctors are special.
6937 if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) {
6939
6940 // No special case for NumFields == 0 here, so the Memcpy op
6941 // below also does its checks in those cases.
6942
6943 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
6944 if (!this->emitThis(Ctor))
6945 return false;
6946
6947 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor))
6948 return false;
6949
6950 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
6951 this->emitRetVoid(Ctor);
6952 }
6953
6954 unsigned FieldInits = 0;
6956 for (const auto *Init : Ctor->inits()) {
6957 // Scope needed for the initializers.
6958 LocalScope<Emitter> Scope(this, ScopeKind::FullExpression);
6959
6960 const Expr *InitExpr = Init->getInit();
6961 if (const FieldDecl *Member = Init->getMember()) {
6962 const Record::Field *F = R->getField(Member);
6963
6966 if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
6967 return false;
6968 ++FieldInits;
6969 } else if (const Type *Base = Init->getBaseClass()) {
6970 const auto *BaseDecl = Base->getAsCXXRecordDecl();
6971 assert(BaseDecl);
6972
6973 if (Init->isBaseVirtual()) {
6974 assert(R->getVirtualBase(BaseDecl));
6975 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
6976 return false;
6977
6978 } else {
6979 // Base class initializer.
6980 // Get This Base and call initializer on it.
6981 const Record::Base *B = R->getBase(BaseDecl);
6982 assert(B);
6983 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
6984 return false;
6985 }
6986
6987 if (!this->visitInitializerPop(InitExpr))
6988 return false;
6989 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
6992 unsigned ChainSize = IFD->getChainingSize();
6993 assert(ChainSize >= 2);
6994
6995 unsigned NestedFieldOffset = 0;
6996 const Record::Field *NestedField = nullptr;
6997 for (unsigned I = 0; I != ChainSize; ++I) {
6998 const auto *FD = cast<FieldDecl>(IFD->chain()[I]);
6999 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
7000 assert(FieldRecord);
7001
7002 NestedField = FieldRecord->getField(FD);
7003 assert(NestedField);
7004 IsUnion = IsUnion || FieldRecord->isUnion();
7005
7006 NestedFieldOffset += NestedField->Offset;
7007
7008 // Add a new InitChainLink for the record, but not for the final field.
7009 if (I != ChainSize - 1)
7010 InitStack.push_back(InitLink::Field(NestedField->Offset));
7011 }
7012 assert(NestedField);
7013
7015 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
7016 IsUnion))
7017 return false;
7018
7019 // Mark all chain links as initialized.
7020 unsigned InitFieldOffset = 0;
7021 for (const NamedDecl *ND : IFD->chain().drop_back()) {
7022 const auto *FD = cast<FieldDecl>(ND);
7023 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
7024 assert(FieldRecord);
7025 NestedField = FieldRecord->getField(FD);
7026 InitFieldOffset += NestedField->Offset;
7027 assert(NestedField);
7028 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
7029 return false;
7030 if (!this->emitFinishInitPop(InitExpr))
7031 return false;
7032 }
7033
7034 InitStack.pop_back_n(ChainSize - 1);
7035
7036 } else {
7037 assert(Init->isDelegatingInitializer());
7038 if (!this->emitThis(InitExpr))
7039 return false;
7040 if (!this->visitInitializerPop(Init->getInit()))
7041 return false;
7042 }
7043
7044 if (!Scope.destroyLocals())
7045 return false;
7046 }
7047
7048 if (FieldInits != R->getNumFields()) {
7049 assert(FieldInits < R->getNumFields());
7050 // Start the lifetime of all members.
7051 if (!this->emitStartThisLifetime(Ctor))
7052 return false;
7053 }
7054
7055 if (const Stmt *Body = Ctor->getBody()) {
7056 // Only emit the CtorCheck op for non-empty CompoundStmt bodies.
7057 // For non-CompoundStmts, always assume they are non-empty and emit it.
7058 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
7059 if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{}))
7060 return false;
7061 } else {
7062 if (!this->emitCtorCheck(SourceInfo{}))
7063 return false;
7064 }
7065
7066 if (!visitStmt(Body))
7067 return false;
7068 }
7069
7070 return this->emitRetVoid(SourceInfo{});
7071}
7072
7073template <class Emitter>
7074bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
7075 const RecordDecl *RD = Dtor->getParent();
7076 const Record *R = this->getRecord(RD);
7077 if (!R)
7078 return false;
7079
7080 if (!Dtor->isTrivial() && Dtor->getBody()) {
7081 if (!this->visitStmt(Dtor->getBody()))
7082 return false;
7083 }
7084
7085 if (!this->emitThis(Dtor))
7086 return false;
7087
7088 if (!this->emitCheckDestruction(Dtor))
7089 return false;
7090
7091 assert(R);
7092 if (!R->isUnion()) {
7093
7095 // First, destroy all fields.
7096 for (const Record::Field &Field : llvm::reverse(R->fields())) {
7097 const Descriptor *D = Field.Desc;
7098 if (D->hasTrivialDtor())
7099 continue;
7100 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
7101 return false;
7102 if (!this->emitDestructionPop(D, SourceInfo{}))
7103 return false;
7104 }
7105 }
7106
7107 for (const Record::Base &Base : llvm::reverse(R->bases())) {
7108 if (Base.R->hasTrivialDtor())
7109 continue;
7110 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
7111 return false;
7112 if (!this->emitRecordDestructionPop(Base.R, {}))
7113 return false;
7114 }
7115
7116 if (!this->emitMarkDestroyed(Dtor))
7117 return false;
7118
7119 // FIXME: Virtual bases.
7120 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
7121}
7122
7123template <class Emitter>
7124bool Compiler<Emitter>::compileUnionAssignmentOperator(
7125 const CXXMethodDecl *MD) {
7126 if (!this->emitThis(MD))
7127 return false;
7128
7129 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD))
7130 return false;
7131
7132 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
7133}
7134
7135template <class Emitter>
7137 if (F->getReturnType()->isDependentType())
7138 return false;
7139
7140 // Classify the return type.
7141 ReturnType = this->classify(F->getReturnType());
7142
7143 this->CompilingFunction = F;
7144
7145 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
7146 return this->compileConstructor(Ctor);
7147 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
7148 return this->compileDestructor(Dtor);
7149
7150 // Emit custom code if this is a lambda static invoker.
7151 if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
7152 const RecordDecl *RD = MD->getParent();
7153
7154 if (RD->isUnion() &&
7156 return this->compileUnionAssignmentOperator(MD);
7157
7158 if (MD->isLambdaStaticInvoker())
7159 return this->emitLambdaStaticInvokerBody(MD);
7160 }
7161
7162 // Regular functions.
7163 if (const auto *Body = F->getBody())
7164 if (!visitStmt(Body))
7165 return false;
7166
7167 // Emit a guard return to protect against a code path missing one.
7168 if (F->getReturnType()->isVoidType())
7169 return this->emitRetVoid(SourceInfo{});
7170 return this->emitNoRet(SourceInfo{});
7171}
7172
7173static uint32_t getBitWidth(const Expr *E) {
7174 assert(E->refersToBitField());
7175 const auto *ME = cast<MemberExpr>(E);
7176 const auto *FD = cast<FieldDecl>(ME->getMemberDecl());
7177 return FD->getBitWidthValue();
7178}
7179
7180template <class Emitter>
7182 if (E->containsErrors())
7183 return false;
7184
7185 const Expr *SubExpr = E->getSubExpr();
7186 if (SubExpr->getType()->isAnyComplexType())
7187 return this->VisitComplexUnaryOperator(E);
7188 if (SubExpr->getType()->isVectorType())
7189 return this->VisitVectorUnaryOperator(E);
7190 if (SubExpr->getType()->isFixedPointType())
7191 return this->VisitFixedPointUnaryOperator(E);
7192 OptPrimType T = classify(SubExpr->getType());
7193
7194 switch (E->getOpcode()) {
7195 case UO_PostInc: { // x++
7196 if (!Ctx.getLangOpts().CPlusPlus14)
7197 return this->emitInvalid(E);
7198 if (!T)
7199 return this->emitError(E);
7200
7201 if (!this->visit(SubExpr))
7202 return false;
7203
7204 if (T == PT_Ptr) {
7205 if (!this->emitIncPtr(E))
7206 return false;
7207
7208 return DiscardResult ? this->emitPopPtr(E) : true;
7209 }
7210
7211 if (T == PT_Float)
7212 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
7213 : this->emitIncf(getFPOptions(E), E);
7214
7215 if (SubExpr->refersToBitField())
7216 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7217 getBitWidth(SubExpr), E)
7218 : this->emitIncBitfield(*T, E->canOverflow(),
7219 getBitWidth(SubExpr), E);
7220
7221 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
7222 : this->emitInc(*T, E->canOverflow(), E);
7223 }
7224 case UO_PostDec: { // x--
7225 if (!Ctx.getLangOpts().CPlusPlus14)
7226 return this->emitInvalid(E);
7227 if (!T)
7228 return this->emitError(E);
7229
7230 if (!this->visit(SubExpr))
7231 return false;
7232
7233 if (T == PT_Ptr) {
7234 if (!this->emitDecPtr(E))
7235 return false;
7236
7237 return DiscardResult ? this->emitPopPtr(E) : true;
7238 }
7239
7240 if (T == PT_Float)
7241 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
7242 : this->emitDecf(getFPOptions(E), E);
7243
7244 if (SubExpr->refersToBitField()) {
7245 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7246 getBitWidth(SubExpr), E)
7247 : this->emitDecBitfield(*T, E->canOverflow(),
7248 getBitWidth(SubExpr), E);
7249 }
7250
7251 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
7252 : this->emitDec(*T, E->canOverflow(), E);
7253 }
7254 case UO_PreInc: { // ++x
7255 if (!Ctx.getLangOpts().CPlusPlus14)
7256 return this->emitInvalid(E);
7257 if (!T)
7258 return this->emitError(E);
7259
7260 if (!this->visit(SubExpr))
7261 return false;
7262
7263 if (T == PT_Ptr) {
7264 if (!this->emitLoadPtr(E))
7265 return false;
7266 if (!this->emitConstUint8(1, E))
7267 return false;
7268 if (!this->emitAddOffsetUint8(E))
7269 return false;
7270 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7271 }
7272
7273 // Post-inc and pre-inc are the same if the value is to be discarded.
7274 if (DiscardResult) {
7275 if (T == PT_Float)
7276 return this->emitIncfPop(getFPOptions(E), E);
7277 if (SubExpr->refersToBitField())
7278 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7279 getBitWidth(SubExpr), E)
7280 : this->emitIncBitfield(*T, E->canOverflow(),
7281 getBitWidth(SubExpr), E);
7282 return this->emitIncPop(*T, E->canOverflow(), E);
7283 }
7284
7285 if (T == PT_Float) {
7286 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7287 if (!this->emitLoadFloat(E))
7288 return false;
7289 APFloat F(TargetSemantics, 1);
7290 if (!this->emitFloat(F, E))
7291 return false;
7292
7293 if (!this->emitAddf(getFPOptions(E), E))
7294 return false;
7295 if (!this->emitStoreFloat(E))
7296 return false;
7297 } else if (SubExpr->refersToBitField()) {
7298 assert(isIntegerOrBoolType(*T));
7299 if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7300 E))
7301 return false;
7302 } else {
7303 assert(isIntegerOrBoolType(*T));
7304 if (!this->emitPreInc(*T, E->canOverflow(), E))
7305 return false;
7306 }
7307 return E->isGLValue() || this->emitLoadPop(*T, E);
7308 }
7309 case UO_PreDec: { // --x
7310 if (!Ctx.getLangOpts().CPlusPlus14)
7311 return this->emitInvalid(E);
7312 if (!T)
7313 return this->emitError(E);
7314
7315 if (!this->visit(SubExpr))
7316 return false;
7317
7318 if (T == PT_Ptr) {
7319 if (!this->emitLoadPtr(E))
7320 return false;
7321 if (!this->emitConstUint8(1, E))
7322 return false;
7323 if (!this->emitSubOffsetUint8(E))
7324 return false;
7325 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7326 }
7327
7328 // Post-dec and pre-dec are the same if the value is to be discarded.
7329 if (DiscardResult) {
7330 if (T == PT_Float)
7331 return this->emitDecfPop(getFPOptions(E), E);
7332 if (SubExpr->refersToBitField())
7333 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7334 getBitWidth(SubExpr), E)
7335 : this->emitDecBitfield(*T, E->canOverflow(),
7336 getBitWidth(SubExpr), E);
7337 return this->emitDecPop(*T, E->canOverflow(), E);
7338 }
7339
7340 if (T == PT_Float) {
7341 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7342 if (!this->emitLoadFloat(E))
7343 return false;
7344 APFloat F(TargetSemantics, 1);
7345 if (!this->emitFloat(F, E))
7346 return false;
7347
7348 if (!this->emitSubf(getFPOptions(E), E))
7349 return false;
7350 if (!this->emitStoreFloat(E))
7351 return false;
7352 } else if (SubExpr->refersToBitField()) {
7353 assert(isIntegerOrBoolType(*T));
7354 if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7355 E))
7356 return false;
7357 } else {
7358 assert(isIntegerOrBoolType(*T));
7359 if (!this->emitPreDec(*T, E->canOverflow(), E))
7360 return false;
7361 }
7362 return E->isGLValue() || this->emitLoadPop(*T, E);
7363 }
7364 case UO_LNot: // !x
7365 if (!T)
7366 return this->emitError(E);
7367
7368 if (DiscardResult)
7369 return this->discard(SubExpr);
7370
7371 if (!this->visitBool(SubExpr))
7372 return false;
7373
7374 if (!this->emitInv(E))
7375 return false;
7376
7377 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7378 return this->emitCast(PT_Bool, ET, E);
7379 return true;
7380 case UO_Minus: // -x
7381 if (!T)
7382 return this->emitError(E);
7383
7384 if (!this->visit(SubExpr))
7385 return false;
7386 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
7387 case UO_Plus: // +x
7388 if (!T)
7389 return this->emitError(E);
7390
7391 if (!this->visit(SubExpr)) // noop
7392 return false;
7393 return DiscardResult ? this->emitPop(*T, E) : true;
7394 case UO_AddrOf: // &x
7395 if (E->getType()->isMemberPointerType()) {
7396 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
7397 // member can be formed.
7398 if (DiscardResult)
7399 return true;
7400 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
7401 }
7402 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
7403 // operator, neither operator is evaluated and the result is as if both
7404 // were omitted. So '&*q' is just 'q' with no dereference; delegate to the
7405 // pointer operand directly instead of to the '*' (which would emit a null
7406 // check), so that e.g. '&*(int *)0' is not rejected.
7407 if (!Ctx.getLangOpts().CPlusPlus) {
7408 const Expr *Sub = SubExpr->IgnoreParens();
7409 if (const auto *Deref = dyn_cast<UnaryOperator>(Sub);
7410 Deref && Deref->getOpcode() == UO_Deref)
7411 return this->delegate(Deref->getSubExpr());
7412 }
7413 // We should already have a pointer when we get here.
7414 return this->delegate(SubExpr);
7415 case UO_Deref: // *x
7416 if (DiscardResult)
7417 return this->discard(SubExpr);
7418
7419 if (!this->visit(SubExpr))
7420 return false;
7421
7422 if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
7423 return false;
7424
7425 if (classifyPrim(SubExpr) == PT_Ptr)
7426 return this->emitNarrowPtr(E);
7427 return true;
7428
7429 case UO_Not: // ~x
7430 if (!T)
7431 return this->emitError(E);
7432
7433 if (!this->visit(SubExpr))
7434 return false;
7435 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
7436 case UO_Real: // __real x
7437 if (!T)
7438 return false;
7439 return this->delegate(SubExpr);
7440 case UO_Imag: { // __imag x
7441 if (!T)
7442 return false;
7443 if (!this->discard(SubExpr))
7444 return false;
7445 return DiscardResult
7446 ? true
7447 : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
7448 }
7449 case UO_Extension:
7450 return this->delegate(SubExpr);
7451 case UO_Coawait:
7452 assert(false && "Unhandled opcode");
7453 }
7454
7455 return false;
7456}
7457
7458template <class Emitter>
7460 const Expr *SubExpr = E->getSubExpr();
7461 assert(SubExpr->getType()->isAnyComplexType());
7462
7463 if (DiscardResult)
7464 return this->discard(SubExpr);
7465
7466 OptPrimType ResT = classify(E);
7467 auto prepareResult = [=]() -> bool {
7468 if (!ResT && !Initializing) {
7469 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7470 if (!LocalIndex)
7471 return false;
7472 return this->emitGetPtrLocal(*LocalIndex, E);
7473 }
7474
7475 return true;
7476 };
7477
7478 // The offset of the temporary, if we created one.
7479 unsigned SubExprOffset = ~0u;
7480 auto createTemp = [=, &SubExprOffset]() -> bool {
7481 SubExprOffset =
7482 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7483 if (!this->visit(SubExpr))
7484 return false;
7485 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
7486 };
7487
7488 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
7489 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7490 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7491 return false;
7492 return this->emitArrayElemPop(ElemT, Index, E);
7493 };
7494
7495 switch (E->getOpcode()) {
7496 case UO_Minus: // -x
7497 if (!prepareResult())
7498 return false;
7499 if (!createTemp())
7500 return false;
7501 for (unsigned I = 0; I != 2; ++I) {
7502 if (!getElem(SubExprOffset, I))
7503 return false;
7504 if (!this->emitNeg(ElemT, E))
7505 return false;
7506 if (!this->emitInitElem(ElemT, I, E))
7507 return false;
7508 }
7509 break;
7510
7511 case UO_Plus: // +x
7512 case UO_AddrOf: // &x
7513 case UO_Deref: // *x
7514 return this->delegate(SubExpr);
7515
7516 case UO_LNot:
7517 if (!this->visit(SubExpr))
7518 return false;
7519 if (!this->emitComplexBoolCast(SubExpr))
7520 return false;
7521 if (!this->emitInv(E))
7522 return false;
7523 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7524 return this->emitCast(PT_Bool, ET, E);
7525 return true;
7526
7527 case UO_Real:
7528 return this->emitComplexReal(SubExpr);
7529
7530 case UO_Imag:
7531 if (!this->visit(SubExpr))
7532 return false;
7533
7534 if (SubExpr->isLValue()) {
7535 if (!this->emitConstUint8(1, E))
7536 return false;
7537 return this->emitArrayElemPtrPopUint8(E);
7538 }
7539
7540 // Since our _Complex implementation does not map to a primitive type,
7541 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
7542 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
7543
7544 case UO_Not: // ~x
7545 if (!this->delegate(SubExpr))
7546 return false;
7547 // Negate the imaginary component.
7548 if (!this->emitArrayElem(ElemT, 1, E))
7549 return false;
7550 if (!this->emitNeg(ElemT, E))
7551 return false;
7552 if (!this->emitInitElem(ElemT, 1, E))
7553 return false;
7554 return DiscardResult ? this->emitPopPtr(E) : true;
7555
7556 case UO_Extension:
7557 return this->delegate(SubExpr);
7558
7559 default:
7560 return this->emitInvalid(E);
7561 }
7562
7563 return true;
7564}
7565
7566template <class Emitter>
7568 const Expr *SubExpr = E->getSubExpr();
7569 assert(SubExpr->getType()->isVectorType());
7570
7571 if (DiscardResult)
7572 return this->discard(SubExpr);
7573
7574 auto UnaryOp = E->getOpcode();
7575 if (UnaryOp == UO_Extension)
7576 return this->delegate(SubExpr);
7577
7578 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
7579 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
7580 return this->emitInvalid(E);
7581
7582 // Nothing to do here.
7583 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
7584 return this->delegate(SubExpr);
7585
7586 if (!Initializing) {
7587 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7588 if (!LocalIndex)
7589 return false;
7590 if (!this->emitGetPtrLocal(*LocalIndex, E))
7591 return false;
7592 }
7593
7594 // The offset of the temporary, if we created one.
7595 unsigned SubExprOffset =
7596 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7597 if (!this->visit(SubExpr))
7598 return false;
7599 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
7600 return false;
7601
7602 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
7603 PrimType ElemT = classifyVectorElementType(SubExpr->getType());
7604 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7605 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7606 return false;
7607 return this->emitArrayElemPop(ElemT, Index, E);
7608 };
7609
7610 switch (UnaryOp) {
7611 case UO_Minus:
7612 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7613 if (!getElem(SubExprOffset, I))
7614 return false;
7615 if (!this->emitNeg(ElemT, E))
7616 return false;
7617 if (!this->emitInitElem(ElemT, I, E))
7618 return false;
7619 }
7620 break;
7621 case UO_LNot: { // !x
7622 // In C++, the logic operators !, &&, || are available for vectors. !v is
7623 // equivalent to v == 0.
7624 //
7625 // The result of the comparison is a vector of the same width and number of
7626 // elements as the comparison operands with a signed integral element type.
7627 //
7628 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
7629 QualType ResultVecTy = E->getType();
7630 PrimType ResultVecElemT =
7631 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
7632 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7633 if (!getElem(SubExprOffset, I))
7634 return false;
7635 // operator ! on vectors returns -1 for 'truth', so negate it.
7636 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
7637 return false;
7638 if (!this->emitInv(E))
7639 return false;
7640 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
7641 return false;
7642 if (!this->emitNeg(ElemT, E))
7643 return false;
7644 if (ElemT != ResultVecElemT &&
7645 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
7646 return false;
7647 if (!this->emitInitElem(ResultVecElemT, I, E))
7648 return false;
7649 }
7650 break;
7651 }
7652 case UO_Not: // ~x
7653 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7654 if (!getElem(SubExprOffset, I))
7655 return false;
7656 if (ElemT == PT_Bool) {
7657 if (!this->emitInv(E))
7658 return false;
7659 } else {
7660 if (!this->emitComp(ElemT, E))
7661 return false;
7662 }
7663 if (!this->emitInitElem(ElemT, I, E))
7664 return false;
7665 }
7666 break;
7667 default:
7668 llvm_unreachable("Unsupported unary operators should be handled up front");
7669 }
7670 return true;
7671}
7672
7673template <class Emitter>
7675 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
7676 if (DiscardResult)
7677 return true;
7678 return this->emitConst(ECD->getInitVal(), E);
7679 }
7680 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
7681 if (DiscardResult)
7682 return true;
7683 const Function *F = getFunction(FuncDecl);
7684 return F && this->emitGetFnPtr(F, E);
7685 }
7686 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
7687 if (DiscardResult)
7688 return true;
7689
7690 if (UnsignedOrNone Index = P.getOrCreateGlobal(D)) {
7691 if (OptPrimType T = classify(D->getType())) {
7692 if (!this->visitAPValue(TPOD->getValue(), *T, E))
7693 return false;
7694 return this->emitInitGlobal(*T, *Index, E);
7695 }
7696
7697 if (!this->emitGetPtrGlobal(*Index, E))
7698 return false;
7699 if (!this->visitAPValueInitializer(TPOD->getValue(), E, TPOD->getType()))
7700 return false;
7701 return this->emitFinishInit(E);
7702 }
7703 return false;
7704 }
7705
7706 // References are implemented via pointers, so when we see a DeclRefExpr
7707 // pointing to a reference, we need to get its value directly (i.e. the
7708 // pointer to the actual value) instead of a pointer to the pointer to the
7709 // value.
7710 QualType DeclType = D->getType();
7711 bool IsReference = DeclType->isReferenceType();
7712
7713 auto maybePopPtr = [&]() -> bool {
7714 if (DiscardResult)
7715 return this->emitPopPtr(E);
7716 return true;
7717 };
7718
7719 // Function parameters.
7720 // Note that it's important to check them first since we might have a local
7721 // variable created for a ParmVarDecl as well.
7722 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
7723 if (DiscardResult)
7724 return true;
7725
7726 if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
7727 !DeclType->isIntegralOrEnumerationType()) {
7728 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7729 /*InitializerFailed=*/false, E);
7730 }
7731 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
7732 if (IsReference || !It->second.IsPtr)
7733 return this->emitGetParam(classifyPrim(E), It->second.Index, E);
7734
7735 return this->emitGetPtrParam(It->second.Index, E);
7736 }
7737
7738 if (!Ctx.getLangOpts().CPlusPlus23 && IsReference)
7739 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7740 /*InitializerFailed=*/false, E);
7741 }
7742 // Local variables.
7743 if (auto It = Locals.find(D); It != Locals.end()) {
7744 const unsigned Offset = It->second.Offset;
7745 if (IsReference) {
7746 assert(classifyPrim(E) == PT_Ptr);
7747 return this->emitGetRefLocal(Offset, E) && maybePopPtr();
7748 }
7749 return this->emitGetPtrLocal(Offset, E) && maybePopPtr();
7750 }
7751 // Global variables.
7752 if (auto GlobalIndex = P.getGlobal(D)) {
7753 if (IsReference) {
7754 if (!Ctx.getLangOpts().CPlusPlus11)
7755 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
7756 if (!Ctx.getLangOpts().CPlusPlus23)
7757 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
7758
7759 return this->emitGetRefGlobal(*GlobalIndex, E) && maybePopPtr();
7760 }
7761
7762 return this->emitGetPtrGlobal(*GlobalIndex, E) && maybePopPtr();
7763 }
7764
7765 // In case we need to re-visit a declaration.
7766 auto revisit = [&](const VarDecl *VD,
7767 bool IsConstexprUnknown = true) -> bool {
7769 IsConstexprUnknown);
7770 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
7771 return false;
7772 auto VarState = this->visitDecl(VD);
7773
7774 if (!this->emitPopCC(E))
7775 return false;
7776
7777 if (VarState.notCreated())
7778 return true;
7779 if (!VarState)
7780 return false;
7781 // Retry.
7782 return this->visitDeclRef(D, E);
7783 };
7784
7785 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
7786 // Lambda captures.
7787 if (auto It = this->LambdaCaptures.find(D);
7788 It != this->LambdaCaptures.end()) {
7789 auto [Offset, IsPtr] = It->second;
7790
7791 if (IsPtr)
7792 return this->emitGetThisFieldPtr(Offset, E) && maybePopPtr();
7793 return this->emitGetPtrThisField(Offset, E) && maybePopPtr();
7794 }
7795 }
7796
7797 if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
7798 DRE && DRE->refersToEnclosingVariableOrCapture()) {
7799 if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
7800 return revisit(VD);
7801 }
7802
7803 if (const auto *BD = dyn_cast<BindingDecl>(D))
7804 return this->delegate(BD->getBinding());
7805
7806 // Avoid infinite recursion.
7807 if (D == InitializingDecl) {
7808 if (DiscardResult)
7809 return true;
7810 return this->emitDummyPtr(D, E);
7811 }
7812
7813 // Try to lazily visit (or emit dummy pointers for) declarations
7814 // we haven't seen yet.
7815 const auto *VD = dyn_cast<VarDecl>(D);
7816 if (!VD)
7817 return this->emitError(E);
7818
7819 // For C.
7820 if (!Ctx.getLangOpts().CPlusPlus) {
7821 if (VD->getInit() && !VD->getInit()->isValueDependent() &&
7822 DeclType.isConstant(Ctx.getASTContext()) && !VD->isWeak() &&
7823 VD->evaluateValue())
7824 return revisit(VD, /*IsConstexprUnknown=*/false);
7825
7826 if (DiscardResult)
7827 return true;
7828 return this->emitDummyPtr(D, E);
7829 }
7830
7831 // ... and C++.
7832 const auto typeShouldBeVisited = [&](QualType T) -> bool {
7833 if (T.isConstant(Ctx.getASTContext()))
7834 return true;
7835 return T->isReferenceType();
7836 };
7837
7838 if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
7839 typeShouldBeVisited(DeclType)) {
7840 if (const Expr *Init = VD->getAnyInitializer();
7841 Init && !Init->isValueDependent()) {
7842 // Whether or not the evaluation is successul doesn't really matter
7843 // here -- we will create a global variable in any case, and that
7844 // will have the state of initializer evaluation attached.
7846 (void)Init->EvaluateAsInitializer(Ctx.getASTContext(), VD, Result, true);
7847 return this->visitDeclRef(D, E);
7848 }
7849 return revisit(VD, !VD->isConstexpr() && DeclType->isReferenceType());
7850 }
7851
7852 // FIXME: The evaluateValue() check here is a little ridiculous, since
7853 // it will ultimately call into Context::evaluateAsInitializer(). In
7854 // other words, we're evaluating the initializer, just to know if we can
7855 // evaluate the initializer.
7856 if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() &&
7857 !VD->getInit()->isValueDependent()) {
7858 if (VD->evaluateValue()) {
7859 bool IsConstexprUnknown = !DeclType.isConstant(Ctx.getASTContext()) &&
7860 !DeclType->isReferenceType();
7861 // Revisit the variable declaration, but make sure it's associated with a
7862 // different evaluation, so e.g. mutable reads don't work on it.
7863 EvalIDScope _(Ctx);
7864 return revisit(VD, IsConstexprUnknown);
7865 } else if (Ctx.getLangOpts().CPlusPlus23 && IsReference)
7866 return revisit(VD, /*IsConstexprUnknown=*/true);
7867
7868 if (IsReference)
7869 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7870 /*InitializerFailed=*/true, E);
7871 }
7872
7873 if (DiscardResult)
7874 return true;
7875 return this->emitDummyPtr(
7876 D, E, Ctx.getLangOpts().CPlusPlus23 && DeclType->isReferenceType());
7877}
7878
7879template <class Emitter>
7881 const auto *D = E->getDecl();
7882 return this->visitDeclRef(D, E);
7883}
7884
7885template <class Emitter>
7887 const DesignatedInitUpdateExpr *E) {
7888 if (!this->visitInitializer(E->getBase()))
7889 return false;
7890 return this->visitInitializer(E->getUpdater());
7891}
7892
7893template <class Emitter> bool Compiler<Emitter>::emitCleanup() {
7894 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) {
7895 if (!C->destroyLocals())
7896 return false;
7897 }
7898 return true;
7899}
7900
7901template <class Emitter>
7902unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
7903 const QualType DerivedType) {
7904 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
7905 if (const auto *R = Ty->getPointeeCXXRecordDecl())
7906 return R;
7907 return Ty->getAsCXXRecordDecl();
7908 };
7909 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
7910 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
7911
7912 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
7913}
7914
7915/// Emit casts from a PrimType to another PrimType.
7916template <class Emitter>
7917bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
7918 QualType ToQT, const Expr *E) {
7919
7920 if (FromT == PT_Float) {
7921 // Floating to floating.
7922 if (ToT == PT_Float) {
7923 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7924 return this->emitCastFP(ToSem, getRoundingMode(E), E);
7925 }
7926
7927 if (ToT == PT_IntAP)
7928 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
7929 getFPOptions(E), E);
7930 if (ToT == PT_IntAPS)
7931 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
7932 getFPOptions(E), E);
7933
7934 // Float to integral.
7935 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7936 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
7937 }
7938
7939 if (isIntegerOrBoolType(FromT) || FromT == PT_Bool) {
7940 if (ToT == PT_IntAP)
7941 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7942 if (ToT == PT_IntAPS)
7943 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7944
7945 // Integral to integral.
7946 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7947 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
7948
7949 if (ToT == PT_Float) {
7950 // Integral to floating.
7951 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7952 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
7953 }
7954 }
7955
7956 return false;
7957}
7958
7959template <class Emitter>
7960bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT,
7961 QualType ToQT, const Expr *E) {
7962 assert(FromT != ToT);
7963
7964 if (ToT == PT_IntAP)
7965 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7966 if (ToT == PT_IntAPS)
7967 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7968
7969 return this->emitCast(FromT, ToT, E);
7970}
7971
7972/// Emits __real(SubExpr)
7973template <class Emitter>
7974bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
7975 assert(SubExpr->getType()->isAnyComplexType());
7976
7977 if (DiscardResult)
7978 return this->discard(SubExpr);
7979
7980 if (!this->visit(SubExpr))
7981 return false;
7982 if (SubExpr->isLValue()) {
7983 if (!this->emitConstUint8(0, SubExpr))
7984 return false;
7985 return this->emitArrayElemPtrPopUint8(SubExpr);
7986 }
7987
7988 // Rvalue, load the actual element.
7989 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
7990 0, SubExpr);
7991}
7992
7993template <class Emitter>
7994bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
7995 assert(!DiscardResult);
7996 PrimType ElemT = classifyComplexElementType(E->getType());
7997 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
7998 // for us, that means (bool)E[0] || (bool)E[1]
7999 if (!this->emitArrayElem(ElemT, 0, E))
8000 return false;
8001 if (ElemT == PT_Float) {
8002 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
8003 return false;
8004 } else {
8005 if (!this->emitCast(ElemT, PT_Bool, E))
8006 return false;
8007 }
8008
8009 // We now have the bool value of E[0] on the stack.
8010 LabelTy LabelTrue = this->getLabel();
8011 if (!this->jumpTrue(LabelTrue, E))
8012 return false;
8013
8014 if (!this->emitArrayElemPop(ElemT, 1, E))
8015 return false;
8016 if (ElemT == PT_Float) {
8017 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
8018 return false;
8019 } else {
8020 if (!this->emitCast(ElemT, PT_Bool, E))
8021 return false;
8022 }
8023 // Leave the boolean value of E[1] on the stack.
8024 LabelTy EndLabel = this->getLabel();
8025 this->jump(EndLabel, E);
8026
8027 this->emitLabel(LabelTrue);
8028 if (!this->emitPopPtr(E))
8029 return false;
8030 if (!this->emitConstBool(true, E))
8031 return false;
8032
8033 this->fallthrough(EndLabel);
8034 this->emitLabel(EndLabel);
8035
8036 return true;
8037}
8038
8039template <class Emitter>
8040bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
8041 const BinaryOperator *E) {
8042 assert(E->isComparisonOp());
8043 assert(!Initializing);
8044 if (DiscardResult)
8045 return this->discard(LHS) && this->discard(RHS);
8046
8047 PrimType ElemT;
8048 bool LHSIsComplex;
8049 unsigned LHSOffset;
8050 if (LHS->getType()->isAnyComplexType()) {
8051 LHSIsComplex = true;
8052 ElemT = classifyComplexElementType(LHS->getType());
8053 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
8054 if (!this->visit(LHS))
8055 return false;
8056 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
8057 return false;
8058 } else {
8059 LHSIsComplex = false;
8060 PrimType LHST = classifyPrim(LHS->getType());
8061 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
8062 if (!this->visit(LHS))
8063 return false;
8064 if (!this->emitSetLocal(LHST, LHSOffset, E))
8065 return false;
8066 }
8067
8068 bool RHSIsComplex;
8069 unsigned RHSOffset;
8070 if (RHS->getType()->isAnyComplexType()) {
8071 RHSIsComplex = true;
8072 ElemT = classifyComplexElementType(RHS->getType());
8073 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
8074 if (!this->visit(RHS))
8075 return false;
8076 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
8077 return false;
8078 } else {
8079 RHSIsComplex = false;
8080 PrimType RHST = classifyPrim(RHS->getType());
8081 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
8082 if (!this->visit(RHS))
8083 return false;
8084 if (!this->emitSetLocal(RHST, RHSOffset, E))
8085 return false;
8086 }
8087
8088 auto getElem = [&](unsigned LocalOffset, unsigned Index,
8089 bool IsComplex) -> bool {
8090 if (IsComplex) {
8091 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
8092 return false;
8093 return this->emitArrayElemPop(ElemT, Index, E);
8094 }
8095 return this->emitGetLocal(ElemT, LocalOffset, E);
8096 };
8097
8098 for (unsigned I = 0; I != 2; ++I) {
8099 // Get both values.
8100 if (!getElem(LHSOffset, I, LHSIsComplex))
8101 return false;
8102 if (!getElem(RHSOffset, I, RHSIsComplex))
8103 return false;
8104 // And compare them.
8105 if (!this->emitEQ(ElemT, E))
8106 return false;
8107
8108 if (!this->emitCastBoolUint8(E))
8109 return false;
8110 }
8111
8112 // We now have two bool values on the stack. Compare those.
8113 if (!this->emitAddUint8(E))
8114 return false;
8115 if (!this->emitConstUint8(2, E))
8116 return false;
8117
8118 if (E->getOpcode() == BO_EQ) {
8119 if (!this->emitEQUint8(E))
8120 return false;
8121 } else if (E->getOpcode() == BO_NE) {
8122 if (!this->emitNEUint8(E))
8123 return false;
8124 } else
8125 return false;
8126
8127 // In C, this returns an int.
8128 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
8129 return this->emitCast(PT_Bool, ResT, E);
8130 return true;
8131}
8132
8133/// When calling this, we have a pointer of the local-to-destroy
8134/// on the stack.
8135/// Emit destruction of record types (or arrays of record types).
8136template <class Emitter>
8137bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R,
8138 SourceInfo Loc) {
8139 assert(R);
8140 assert(!R->hasTrivialDtor());
8141 const CXXDestructorDecl *Dtor = R->getDestructor();
8142 assert(Dtor);
8143 const Function *DtorFunc = getFunction(Dtor);
8144 if (!DtorFunc)
8145 return false;
8146 assert(DtorFunc->hasThisPointer());
8147 assert(DtorFunc->getNumParams() == 1);
8148 return this->emitCall(DtorFunc, 0, Loc);
8149}
8150/// When calling this, we have a pointer of the local-to-destroy
8151/// on the stack.
8152/// Emit destruction of record types (or arrays of record types).
8153template <class Emitter>
8154bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc,
8155 SourceInfo Loc) {
8156 assert(Desc);
8157 assert(!Desc->hasTrivialDtor());
8158
8159 // Arrays.
8160 if (Desc->isArray()) {
8161 const Descriptor *ElemDesc = Desc->ElemDesc;
8162 assert(ElemDesc);
8163
8164 unsigned N = Desc->getNumElems();
8165 if (N == 0)
8166 return this->emitPopPtr(Loc);
8167
8168 for (ssize_t I = N - 1; I >= 1; --I) {
8169 if (!this->emitConstUint64(I, Loc))
8170 return false;
8171 if (!this->emitArrayElemPtrUint64(Loc))
8172 return false;
8173 if (!this->emitDestructionPop(ElemDesc, Loc))
8174 return false;
8175 }
8176 // Last iteration, removes the instance pointer from the stack.
8177 if (!this->emitConstUint64(0, Loc))
8178 return false;
8179 if (!this->emitArrayElemPtrPopUint64(Loc))
8180 return false;
8181 return this->emitDestructionPop(ElemDesc, Loc);
8182 }
8183
8184 assert(Desc->ElemRecord);
8185 assert(!Desc->ElemRecord->hasTrivialDtor());
8186 return this->emitRecordDestructionPop(Desc->ElemRecord, Loc);
8187}
8188
8189/// Create a dummy pointer for the given decl (or expr) and
8190/// push a pointer to it on the stack.
8191template <class Emitter>
8192bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E, bool CU) {
8193 assert(!DiscardResult && "Should've been checked before");
8194 unsigned DummyID = P.getOrCreateDummy(D, CU);
8195
8196 if (!this->emitGetPtrGlobal(DummyID, E))
8197 return false;
8198 if (E->getType()->isVoidType())
8199 return true;
8200
8201 // Convert the dummy pointer to another pointer type if we have to.
8202 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
8203 if (isPtrType(PT))
8204 return this->emitDecayPtr(PT_Ptr, PT, E);
8205 return false;
8206 }
8207 return true;
8208}
8209
8210template <class Emitter>
8211bool Compiler<Emitter>::emitFloat(const APFloat &F, SourceInfo Info) {
8212 if (Floating::singleWord(F.getSemantics()))
8213 return this->emitConstFloat(Floating(F), Info);
8214
8215 APInt I = F.bitcastToAPInt();
8216 return this->emitConstFloat(
8217 Floating(const_cast<uint64_t *>(I.getRawData()),
8218 llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
8219 Info);
8220}
8221
8222// This function is constexpr if and only if To, From, and the types of
8223// all subobjects of To and From are types T such that...
8224// (3.1) - is_union_v<T> is false;
8225// (3.2) - is_pointer_v<T> is false;
8226// (3.3) - is_member_pointer_v<T> is false;
8227// (3.4) - is_volatile_v<T> is false; and
8228// (3.5) - T has no non-static data members of reference type
8229template <class Emitter>
8230bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
8231 const Expr *SubExpr = E->getSubExpr();
8232 QualType FromType = SubExpr->getType();
8233 QualType ToType = E->getType();
8234 OptPrimType ToT = classify(ToType);
8235
8236 assert(!ToType->isReferenceType());
8237
8238 // Prepare storage for the result in case we discard.
8239 if (DiscardResult && !Initializing && !ToT) {
8240 UnsignedOrNone LocalIndex = allocateLocal(E);
8241 if (!LocalIndex)
8242 return false;
8243 if (!this->emitGetPtrLocal(*LocalIndex, E))
8244 return false;
8245 }
8246
8247 // Get a pointer to the value-to-cast on the stack.
8248 // For CK_LValueToRValueBitCast, this is always an lvalue and
8249 // we later assume it to be one (i.e. a PT_Ptr). However,
8250 // we call this function for other utility methods where
8251 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
8252 if (SubExpr->isGLValue() || FromType->isVectorType()) {
8253 if (!this->visit(SubExpr))
8254 return false;
8255 } else if (OptPrimType FromT = classify(SubExpr)) {
8256 unsigned TempOffset =
8257 allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
8258 if (!this->visit(SubExpr))
8259 return false;
8260 if (!this->emitSetLocal(*FromT, TempOffset, E))
8261 return false;
8262 if (!this->emitGetPtrLocal(TempOffset, E))
8263 return false;
8264 } else {
8265 return false;
8266 }
8267
8268 if (!ToT) {
8269 if (!this->emitBitCast(E))
8270 return false;
8271 return DiscardResult ? this->emitPopPtr(E) : true;
8272 }
8273 assert(ToT);
8274
8275 const llvm::fltSemantics *TargetSemantics = nullptr;
8276 if (ToT == PT_Float)
8277 TargetSemantics = &Ctx.getFloatSemantics(ToType);
8278
8279 // Conversion to a primitive type. FromType can be another
8280 // primitive type, or a record/array.
8281 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
8282 ToType->isSpecificBuiltinType(BuiltinType::Char_U));
8283 uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
8284
8285 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
8286 ResultBitWidth, TargetSemantics,
8287 ToType.getTypePtr(), E))
8288 return false;
8289
8290 if (DiscardResult)
8291 return this->emitPop(*ToT, E);
8292
8293 return true;
8294}
8295
8296/// Replicate a scalar value into every scalar element of an aggregate.
8297/// The scalar is stored in a local at \p SrcOffset and a pointer to the
8298/// destination must be on top of the interpreter stack. Each element receives
8299/// the scalar, cast to its own type.
8300template <class Emitter>
8301bool Compiler<Emitter>::emitHLSLAggregateSplat(PrimType SrcT,
8302 unsigned SrcOffset,
8303 QualType DestType,
8304 const Expr *E) {
8305 // Vectors and matrices are treated as flat sequences of elements.
8306 unsigned NumElems = 0;
8307 QualType ElemType;
8308 if (const auto *VT = DestType->getAs<VectorType>()) {
8309 NumElems = VT->getNumElements();
8310 ElemType = VT->getElementType();
8311 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
8312 NumElems = MT->getNumElementsFlattened();
8313 ElemType = MT->getElementType();
8314 }
8315 if (NumElems > 0) {
8316 PrimType ElemT = classifyPrim(ElemType);
8317 for (unsigned I = 0; I != NumElems; ++I) {
8318 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8319 return false;
8320 if (!this->emitPrimCast(SrcT, ElemT, ElemType, E))
8321 return false;
8322 if (!this->emitInitElem(ElemT, I, E))
8323 return false;
8324 }
8325 return true;
8326 }
8327
8328 // Arrays: primitive elements are filled directly; composite elements
8329 // require recursion into each sub-aggregate.
8330 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
8331 const auto *CAT = cast<ConstantArrayType>(AT);
8332 QualType ArrElemType = CAT->getElementType();
8333 unsigned ArrSize = CAT->getZExtSize();
8334
8335 if (OptPrimType ElemT = classify(ArrElemType)) {
8336 for (unsigned I = 0; I != ArrSize; ++I) {
8337 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8338 return false;
8339 if (!this->emitPrimCast(SrcT, *ElemT, ArrElemType, E))
8340 return false;
8341 if (!this->emitInitElem(*ElemT, I, E))
8342 return false;
8343 }
8344 } else {
8345 for (unsigned I = 0; I != ArrSize; ++I) {
8346 if (!this->emitConstUint32(I, E))
8347 return false;
8348 if (!this->emitArrayElemPtrUint32(E))
8349 return false;
8350 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, ArrElemType, E))
8351 return false;
8352 if (!this->emitFinishInitPop(E))
8353 return false;
8354 }
8355 }
8356 return true;
8357 }
8358
8359 // Records: fill base classes first, then named fields in declaration
8360 // order.
8361 if (DestType->isRecordType()) {
8362 const Record *R = getRecord(DestType);
8363 if (!R)
8364 return false;
8365
8366 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8367 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8368 const Record::Base *B = R->getBase(BS.getType());
8369 assert(B);
8370 if (!this->emitGetPtrBase(B->Offset, E))
8371 return false;
8372 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, BS.getType(), E))
8373 return false;
8374 if (!this->emitFinishInitPop(E))
8375 return false;
8376 }
8377 }
8378
8379 for (const Record::Field &F : R->fields()) {
8380 if (F.isUnnamedBitField())
8381 continue;
8382
8383 QualType FieldType = F.Decl->getType();
8384 if (OptPrimType FieldT = classify(FieldType)) {
8385 if (!this->emitGetLocal(SrcT, SrcOffset, E))
8386 return false;
8387 if (!this->emitPrimCast(SrcT, *FieldT, FieldType, E))
8388 return false;
8389 if (F.isBitField()) {
8390 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
8391 return false;
8392 } else {
8393 if (!this->emitInitField(*FieldT, F.Offset, E))
8394 return false;
8395 }
8396 } else {
8397 if (!this->emitGetPtrField(F.Offset, E))
8398 return false;
8399 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, FieldType, E))
8400 return false;
8401 if (!this->emitPopPtr(E))
8402 return false;
8403 }
8404 }
8405 return true;
8406 }
8407
8408 return false;
8409}
8410
8411/// Return the total number of scalar elements in a type. This is used
8412/// to cap how many source elements are extracted during an elementwise cast,
8413/// so we never flatten more than the destination can hold.
8414template <class Emitter>
8415unsigned Compiler<Emitter>::countHLSLFlatElements(QualType Ty) {
8416 // Vector and matrix types are treated as flat sequences of elements.
8417 if (const auto *VT = Ty->getAs<VectorType>())
8418 return VT->getNumElements();
8419 if (const auto *MT = Ty->getAs<ConstantMatrixType>())
8420 return MT->getNumElementsFlattened();
8421 // Arrays: total count is array size * scalar elements per element.
8422 if (const auto *AT = Ty->getAsArrayTypeUnsafe()) {
8423 const auto *CAT = cast<ConstantArrayType>(AT);
8424 return CAT->getZExtSize() * countHLSLFlatElements(CAT->getElementType());
8425 }
8426 // Records: sum scalar element counts of base classes and named fields.
8427 if (Ty->isRecordType()) {
8428 const Record *R = getRecord(Ty);
8429 if (!R)
8430 return 0;
8431 unsigned Count = 0;
8432 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8433 for (const CXXBaseSpecifier &BS : CXXRD->bases())
8434 Count += countHLSLFlatElements(BS.getType());
8435 }
8436 for (const Record::Field &F : R->fields()) {
8437 if (F.isUnnamedBitField())
8438 continue;
8439 Count += countHLSLFlatElements(F.Decl->getType());
8440 }
8441 return Count;
8442 }
8443 // Scalar primitive types contribute one element.
8444 if (canClassify(Ty))
8445 return 1;
8446 return 0;
8447}
8448
8449/// Walk a source aggregate and extract every scalar element into its own local
8450/// variable. The results are appended to \p Elements in declaration order,
8451/// stopping once \p MaxElements have been collected. A pointer to the
8452/// source aggregate must be stored in the local at \p SrcOffset.
8453template <class Emitter>
8454bool Compiler<Emitter>::emitHLSLFlattenAggregate(
8455 QualType SrcType, unsigned SrcOffset,
8456 SmallVectorImpl<HLSLFlatElement> &Elements, unsigned MaxElements,
8457 const Expr *E) {
8458
8459 // Save a scalar value from the stack into a new local and record it.
8460 auto saveToLocal = [&](PrimType T) -> bool {
8461 unsigned Offset = allocateLocalPrimitive(E, T, /*IsConst=*/true);
8462 if (!this->emitSetLocal(T, Offset, E))
8463 return false;
8464 Elements.push_back({Offset, T});
8465 return true;
8466 };
8467
8468 // Save a pointer from the stack into a new local for later use.
8469 auto savePtrToLocal = [&]() -> UnsignedOrNone {
8470 unsigned Offset = allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
8471 if (!this->emitSetLocal(PT_Ptr, Offset, E))
8472 return std::nullopt;
8473 return Offset;
8474 };
8475
8476 // Vectors and matrices are flat sequences of elements.
8477 unsigned NumElems = 0;
8478 QualType ElemType;
8479 if (const auto *VT = SrcType->getAs<VectorType>()) {
8480 NumElems = VT->getNumElements();
8481 ElemType = VT->getElementType();
8482 } else if (const auto *MT = SrcType->getAs<ConstantMatrixType>()) {
8483 NumElems = MT->getNumElementsFlattened();
8484 ElemType = MT->getElementType();
8485 }
8486 if (NumElems > 0) {
8487 PrimType ElemT = classifyPrim(ElemType);
8488 for (unsigned I = 0; I != NumElems && Elements.size() < MaxElements; ++I) {
8489 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8490 return false;
8491 if (!this->emitArrayElemPop(ElemT, I, E))
8492 return false;
8493 if (!saveToLocal(ElemT))
8494 return false;
8495 }
8496 return true;
8497 }
8498
8499 // Arrays: primitive elements are extracted directly; composite elements
8500 // require recursion into each sub-aggregate.
8501 if (const auto *AT = SrcType->getAsArrayTypeUnsafe()) {
8502 const auto *CAT = cast<ConstantArrayType>(AT);
8503 QualType ArrElemType = CAT->getElementType();
8504 unsigned ArrSize = CAT->getZExtSize();
8505
8506 if (OptPrimType ElemT = classify(ArrElemType)) {
8507 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
8508 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8509 return false;
8510 if (!this->emitArrayElemPop(*ElemT, I, E))
8511 return false;
8512 if (!saveToLocal(*ElemT))
8513 return false;
8514 }
8515 } else {
8516 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
8517 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8518 return false;
8519 if (!this->emitConstUint32(I, E))
8520 return false;
8521 if (!this->emitArrayElemPtrPopUint32(E))
8522 return false;
8523 UnsignedOrNone ElemPtrOffset = savePtrToLocal();
8524 if (!ElemPtrOffset)
8525 return false;
8526 if (!emitHLSLFlattenAggregate(ArrElemType, *ElemPtrOffset, Elements,
8527 MaxElements, E))
8528 return false;
8529 }
8530 }
8531 return true;
8532 }
8533
8534 // Records: base classes come first, then named fields in declaration
8535 // order.
8536 if (SrcType->isRecordType()) {
8537 const Record *R = getRecord(SrcType);
8538 if (!R)
8539 return false;
8540
8541 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8542 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8543 if (Elements.size() >= MaxElements)
8544 break;
8545 const Record::Base *B = R->getBase(BS.getType());
8546 assert(B);
8547 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8548 return false;
8549 if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, E))
8550 return false;
8551 UnsignedOrNone BasePtrOffset = savePtrToLocal();
8552 if (!BasePtrOffset)
8553 return false;
8554 if (!emitHLSLFlattenAggregate(BS.getType(), *BasePtrOffset, Elements,
8555 MaxElements, E))
8556 return false;
8557 }
8558 }
8559
8560 for (const Record::Field &F : R->fields()) {
8561 if (Elements.size() >= MaxElements)
8562 break;
8563 if (F.isUnnamedBitField())
8564 continue;
8565
8566 QualType FieldType = F.Decl->getType();
8567 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
8568 return false;
8569 if (!this->emitGetPtrFieldPop(F.Offset, E))
8570 return false;
8571
8572 if (OptPrimType FieldT = classify(FieldType)) {
8573 if (!this->emitLoadPop(*FieldT, E))
8574 return false;
8575 if (!saveToLocal(*FieldT))
8576 return false;
8577 } else {
8578 UnsignedOrNone FieldPtrOffset = savePtrToLocal();
8579 if (!FieldPtrOffset)
8580 return false;
8581 if (!emitHLSLFlattenAggregate(FieldType, *FieldPtrOffset, Elements,
8582 MaxElements, E))
8583 return false;
8584 }
8585 }
8586 return true;
8587 }
8588
8589 return false;
8590}
8591
8592/// Populate an HLSL aggregate from a flat list of previously extracted source
8593/// elements, casting each to the corresponding destination element type.
8594/// \p ElemIdx tracks the current position in \p Elements and is advanced as
8595/// elements are consumed. A pointer to the destination must be on top of the
8596/// interpreter stack.
8597template <class Emitter>
8598bool Compiler<Emitter>::emitHLSLConstructAggregate(
8599 QualType DestType, ArrayRef<HLSLFlatElement> Elements, unsigned &ElemIdx,
8600 const Expr *E) {
8601
8602 // Consume the next source element, cast it, and leave it on the stack.
8603 auto loadAndCast = [&](PrimType DestT, QualType DestQT) -> bool {
8604 const auto &Src = Elements[ElemIdx++];
8605 if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E))
8606 return false;
8607 return this->emitPrimCast(Src.Type, DestT, DestQT, E);
8608 };
8609
8610 // Vectors and matrices are flat sequences of elements.
8611 unsigned NumElems = 0;
8612 QualType ElemType;
8613 if (const auto *VT = DestType->getAs<VectorType>()) {
8614 NumElems = VT->getNumElements();
8615 ElemType = VT->getElementType();
8616 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
8617 NumElems = MT->getNumElementsFlattened();
8618 ElemType = MT->getElementType();
8619 }
8620 if (NumElems > 0) {
8621 PrimType DestElemT = classifyPrim(ElemType);
8622 for (unsigned I = 0; I != NumElems; ++I) {
8623 if (!loadAndCast(DestElemT, ElemType))
8624 return false;
8625 if (!this->emitInitElem(DestElemT, I, E))
8626 return false;
8627 }
8628 return true;
8629 }
8630
8631 // Arrays: primitive elements are filled directly; composite elements
8632 // require recursion into each sub-aggregate.
8633 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
8634 const auto *CAT = cast<ConstantArrayType>(AT);
8635 QualType ArrElemType = CAT->getElementType();
8636 unsigned ArrSize = CAT->getZExtSize();
8637
8638 if (OptPrimType ElemT = classify(ArrElemType)) {
8639 for (unsigned I = 0; I != ArrSize; ++I) {
8640 if (!loadAndCast(*ElemT, ArrElemType))
8641 return false;
8642 if (!this->emitInitElem(*ElemT, I, E))
8643 return false;
8644 }
8645 } else {
8646 for (unsigned I = 0; I != ArrSize; ++I) {
8647 if (!this->emitConstUint32(I, E))
8648 return false;
8649 if (!this->emitArrayElemPtrUint32(E))
8650 return false;
8651 if (!emitHLSLConstructAggregate(ArrElemType, Elements, ElemIdx, E))
8652 return false;
8653 if (!this->emitFinishInitPop(E))
8654 return false;
8655 }
8656 }
8657 return true;
8658 }
8659
8660 // Records: base classes come first, then named fields in declaration
8661 // order.
8662 if (DestType->isRecordType()) {
8663 const Record *R = getRecord(DestType);
8664 if (!R)
8665 return false;
8666
8667 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
8668 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
8669 const Record::Base *B = R->getBase(BS.getType());
8670 assert(B);
8671 if (!this->emitGetPtrBase(B->Offset, E))
8672 return false;
8673 if (!emitHLSLConstructAggregate(BS.getType(), Elements, ElemIdx, E))
8674 return false;
8675 if (!this->emitFinishInitPop(E))
8676 return false;
8677 }
8678 }
8679
8680 for (const Record::Field &F : R->fields()) {
8681 if (F.isUnnamedBitField())
8682 continue;
8683
8684 QualType FieldType = F.Decl->getType();
8685 if (OptPrimType FieldT = classify(FieldType)) {
8686 if (!loadAndCast(*FieldT, FieldType))
8687 return false;
8688 if (F.isBitField()) {
8689 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
8690 return false;
8691 } else {
8692 if (!this->emitInitField(*FieldT, F.Offset, E))
8693 return false;
8694 }
8695 } else {
8696 if (!this->emitGetPtrField(F.Offset, E))
8697 return false;
8698 if (!emitHLSLConstructAggregate(FieldType, Elements, ElemIdx, E))
8699 return false;
8700 if (!this->emitPopPtr(E))
8701 return false;
8702 }
8703 }
8704 return true;
8705 }
8706
8707 return false;
8708}
8709
8710namespace clang {
8711namespace interp {
8712
8713template class Compiler<ByteCodeEmitter>;
8714template class Compiler<EvalEmitter>;
8715
8716} // namespace interp
8717} // 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
bool isIndeterminate() const
Definition APValue.h:482
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:2126
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1276
ValueDecl * getDecl()
Definition Expr.h:1344
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1641
decl_range decls()
Definition Stmt.h:1689
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isInvalidDecl() const
Definition DeclBase.h:596
bool hasAttr() const
Definition DeclBase.h:585
OverloadedOperatorKind getCXXOverloadedOperator() const
If this name is the name of an overloadable operator in C++ (e.g., operator+), retrieve the kind of o...
Stmt * getSubStmt()
Definition Stmt.h:2091
InitListExpr * getUpdater() const
Definition Expr.h:5939
DoStmt - This represents a 'do/while' stmt.
Definition Stmt.h:2842
Stmt * getBody()
Definition Stmt.h:2867
Expr * getCond()
Definition Stmt.h:2860
const Expr * getBase() const
Definition Expr.h:6584
Represents a reference to emded data.
Definition Expr.h:5132
ChildElementIter< false > begin()
Definition Expr.h:5238
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition ExprCXX.h:3661
This represents one expression.
Definition Expr.h:112
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition Expr.cpp:85
bool isGLValue() const
Definition Expr.h:287
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition Expr.h:177
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3087
bool containsErrors() const
Whether this expression contains subexpressions which had errors.
Definition Expr.h:246
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3095
bool isPRValue() const
Definition Expr.h:285
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:284
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition Expr.cpp:3697
bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const
Determine whether the result of this expression is a temporary object of the given class type.
Definition Expr.cpp:3262
bool refersToBitField() const
Returns true if this expression is a gl-value that potentially refers to a bit-field.
Definition Expr.h:479
QualType getType() const
Definition Expr.h:144
An expression trait intrinsic.
Definition ExprCXX.h:3073
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
Definition Expr.h:6613
void getEncodedElementAccess(SmallVectorImpl< uint32_t > &Elts) const
getEncodedElementAccess - Encode the elements accessed into an llvm aggregate Constant of ConstantInt...
Definition Expr.cpp:4558
Represents a member of a struct/union/class.
Definition Decl.h:3191
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3427
llvm::APInt getValue() const
Returns an internal integer representation of the literal.
Definition Expr.h:1581
llvm::APFloat getValue() const
Definition Expr.h:1672
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition Stmt.h:2898
Stmt * getInit()
Definition Stmt.h:2913
VarDecl * getConditionVariable() const
Retrieve the variable declared in this "for" statement, if any.
Definition Stmt.cpp:1120
Stmt * getBody()
Definition Stmt.h:2942
Expr * getInc()
Definition Stmt.h:2941
Expr * getCond()
Definition Stmt.h:2940
DeclStmt * getConditionVariableDeclStmt()
If this ForStmt has a condition variable, return the faux DeclStmt associated with the creation of th...
Definition Stmt.h:2928
const Expr * getSubExpr() const
Definition Expr.h:1068
Represents a function declaration or definition.
Definition Decl.h:2027
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2824
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3256
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4182
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4170
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3739
QualType getReturnType() const
Definition Decl.h:2872
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2801
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition Decl.h:2404
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition Decl.cpp:4306
bool isUsableAsGlobalAllocationFunctionInConstantEvaluation(UnsignedOrNone *AlignmentParam=nullptr, bool *IsNothrow=nullptr) const
Determines whether this function is one of the replaceable global allocation functions described in i...
Definition Decl.cpp:3403
bool isDefaulted() const
Whether this function is defaulted.
Definition Decl.h:2412
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3803
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3176
Declaration of a template function.
FunctionDecl * findSpecialization(ArrayRef< TemplateArgument > Args, void *&InsertPos)
Return the specialization with the provided arguments if it exists, otherwise return the insertion po...
GNUNullExpr - Implements the GNU __null extension, which is a name for a null pointer constant that h...
Definition Expr.h:4929
Represents a C11 generic selection.
Definition Expr.h:6185
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition Expr.h:6471
IfStmt - This represents an if/then/else.
Definition Stmt.h:2269
Stmt * getThen()
Definition Stmt.h:2358
Stmt * getInit()
Definition Stmt.h:2419
bool isNonNegatedConsteval() const
Definition Stmt.h:2454
Expr * getCond()
Definition Stmt.h:2346
bool isNegatedConsteval() const
Definition Stmt.h:2458
Stmt * getElse()
Definition Stmt.h:2367
DeclStmt * getConditionVariableDeclStmt()
If this IfStmt has a condition variable, return the faux DeclStmt associated with the creation of tha...
Definition Stmt.h:2402
VarDecl * getConditionVariable()
Retrieve the variable declared in this "if" statement, if any.
Definition Stmt.cpp:1068
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1....
Definition Expr.h:1737
const Expr * getSubExpr() const
Definition Expr.h:1749
Represents an implicitly-generated value initialization of an object of a given type.
Definition Expr.h:6060
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3498
Describes an C or C++ initializer list.
Definition Expr.h:5305
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition Expr.h:5408
ArrayRef< Expr * > inits() const
Definition Expr.h:5358
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition ExprCXX.h:1972
capture_init_iterator capture_init_begin()
Retrieve the first initialization argument for this lambda expression (which initializes the first ca...
Definition ExprCXX.h:2098
CXXRecordDecl * getLambdaClass() const
Retrieve the class that corresponds to the lambda.
Definition ExprCXX.cpp:1407
bool isCompatibleWith(ClangABI Version) const
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition DeclCXX.h:3326
const Stmt * getNamedLoopOrSwitch() const
If this is a named break/continue, get the loop or switch statement that this targets.
Definition Stmt.cpp:1535
A global _GUID constant.
Definition DeclCXX.h:4416
APValue & getAsAPValue() const
Get the value of this MSGuidDecl as an APValue.
Definition DeclCXX.cpp:3862
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4920
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4945
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4937
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Definition ExprCXX.h:4970
LifetimeExtendedTemporaryDecl * getLifetimeExtendedTemporaryDecl()
Definition ExprCXX.h:4960
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3370
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3453
Expr * getBase() const
Definition Expr.h:3447
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3717
This represents a decl that may have a name.
Definition Decl.h:274
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
Represents a C++ namespace alias.
Definition DeclCXX.h:3219
ObjCArrayLiteral - used for objective-c array containers; as in: @["Hello", NSApp,...
Definition ExprObjC.h:220
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition ExprObjC.h:119
ObjCBoxedExpr - used for generalized expression boxing.
Definition ExprObjC.h:159
ObjCDictionaryLiteral - AST node to represent objective-c dictionary literals; as in:"name" : NSUserN...
Definition ExprObjC.h:342
ObjCEncodeExpr, used for @encode in Objective-C.
Definition ExprObjC.h:441
QualType getEncodedType() const
Definition ExprObjC.h:460
SourceLocation getAtLoc() const
Definition ExprObjC.h:455
bool isExpressibleAsConstantInitializer() const
Definition ExprObjC.h:68
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition ExprObjC.h:84
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition Expr.h:2533
Expr * getIndexExpr(unsigned Idx)
Definition Expr.h:2592
const OffsetOfNode & getComponent(unsigned Idx) const
Definition Expr.h:2580
unsigned getNumComponents() const
Definition Expr.h:2588
Helper class for OffsetOfExpr.
Definition Expr.h:2427
unsigned getArrayExprIndex() const
For an array element node, returns the index into the array of expressions.
Definition Expr.h:2485
@ Array
An index into an array.
Definition Expr.h:2432
Kind getKind() const
Determine what kind of offsetof node this is.
Definition Expr.h:2481
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition Expr.h:1184
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition Expr.h:1234
Expr * getSelectedExpr() const
Definition ExprCXX.h:4639
ParenExpr - This represents a parenthesized expression, e.g.
Definition Expr.h:2188
const Expr * getSubExpr() const
Definition Expr.h:2205
Represents a parameter to a function.
Definition Decl.h:1817
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3392
QualType getPointeeType() const
Definition TypeBase.h:3402
[C99 6.4.2.2] - A predefined identifier such as func.
Definition Expr.h:2011
StringLiteral * getFunctionName()
Definition Expr.h:2055
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition Expr.h:6807
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
Definition Expr.h:6855
ArrayRef< Expr * > semantics()
Definition Expr.h:6879
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8531
QualType withConst() const
Definition TypeBase.h:1174
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8447
bool isConstant(const ASTContext &Ctx) const
Definition TypeBase.h:1097
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8520
Represents a struct/union/class.
Definition Decl.h:4356
Frontend produces RecoveryExprs on semantic errors that prevent creating other well-formed expression...
Definition Expr.h:7506
Base for LValueReferenceType and RValueReferenceType.
Definition TypeBase.h:3637
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
bool isSatisfied() const
Whether or not the requires clause is satisfied.
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3170
Expr * getRetValue()
Definition Stmt.h:3197
SourceLocation getLocation() const
Definition Expr.h:2161
std::string ComputeName(ASTContext &Context) const
Definition Expr.cpp:593
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
ShuffleVectorExpr - clang-specific builtin-in function __builtin_shufflevector.
Definition Expr.h:4649
llvm::APSInt getShuffleMaskIdx(unsigned N) const
Definition Expr.h:4701
unsigned getNumSubExprs() const
getNumSubExprs - Return the size of the SubExprs array.
Definition Expr.h:4682
Expr * getExpr(unsigned Index)
getExpr - Return the Expr at the specified index.
Definition Expr.h:4688
Represents an expression that computes the length of a parameter pack.
Definition ExprCXX.h:4441
unsigned getPackLength() const
Retrieve the length of the parameter pack.
Definition ExprCXX.h:4515
Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(), __builtin_FUNCTION(),...
Definition Expr.h:5023
APValue EvaluateInContext(const ASTContext &Ctx, const Expr *DefaultExpr) const
Return the result of evaluating this SourceLocExpr in the specified (and possibly null) default argum...
Definition Expr.cpp:2289
Represents a C++11 static_assert declaration.
Definition DeclCXX.h:4154
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition Expr.h:4601
CompoundStmt * getSubStmt()
Definition Expr.h:4618
Stmt - This represents one statement.
Definition Stmt.h:86
StmtClass getStmtClass() const
Definition Stmt.h:1503
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1805
unsigned getLength() const
Definition Expr.h:1915
static StringLiteral * Create(const ASTContext &Ctx, StringRef Str, StringLiteralKind Kind, bool Pascal, QualType Ty, ArrayRef< SourceLocation > Locs)
This is the "fully general" constructor that allows representation of strings formed from one or more...
Definition Expr.cpp:1194
uint32_t getCodeUnit(size_t i) const
Definition Expr.h:1888
unsigned getCharByteWidth() const
Definition Expr.h:1916
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition ExprCXX.h:4664
const SwitchCase * getNextSwitchCase() const
Definition Stmt.h:1903
SwitchStmt - This represents a 'switch' stmt.
Definition Stmt.h:2519
Expr * getCond()
Definition Stmt.h:2582
Stmt * getBody()
Definition Stmt.h:2594
VarDecl * getConditionVariable()
Retrieve the variable declared in this "switch" statement, if any.
Definition Stmt.cpp:1186
Stmt * getInit()
Definition Stmt.h:2599
SwitchCase * getSwitchCaseList()
Definition Stmt.h:2650
DeclStmt * getConditionVariableDeclStmt()
If this SwitchStmt has a condition variable, return the faux DeclStmt associated with the creation of...
Definition Stmt.h:2633
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3748
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3849
bool isUnion() const
Definition Decl.h:3959
A template argument list.
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition ExprCXX.h:2900
bool getBoolValue() const
Definition ExprCXX.h:2951
const APValue & getAPValue() const
Definition ExprCXX.h:2956
bool isStoredAsBoolean() const
Definition ExprCXX.h:2947
The base class of the type hierarchy.
Definition TypeBase.h:1875
bool isVoidType() const
Definition TypeBase.h:9050
bool isBooleanType() const
Definition TypeBase.h:9187
bool isLiteralType(const ASTContext &Ctx) const
Return true if this is a literal type (C++11 [basic.types]p10)
Definition Type.cpp:3113
bool isIncompleteArrayType() const
Definition TypeBase.h:8791
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isNothrowT() const
Definition Type.cpp:3297
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isVoidPointerType() const
Definition Type.cpp:749
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
Definition Type.cpp:2517
bool isArrayType() const
Definition TypeBase.h:8783
bool isFunctionPointerType() const
Definition TypeBase.h:8751
bool isConstantMatrixType() const
Definition TypeBase.h:8851
bool isPointerType() const
Definition TypeBase.h:8684
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9094
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9344
bool isReferenceType() const
Definition TypeBase.h:8708
bool isEnumeralType() const
Definition TypeBase.h:8815
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9172
bool isSpecificBuiltinType(unsigned K) const
Test for a particular builtin type.
Definition TypeBase.h:9019
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition TypeBase.h:2846
bool isAnyComplexType() const
Definition TypeBase.h:8819
bool isFixedPointType() const
Return true if this is a fixed point type according to ISO/IEC JTC1 SC22 WG14 N1169.
Definition TypeBase.h:9110
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition TypeBase.h:9230
bool isMemberPointerType() const
Definition TypeBase.h:8765
bool isAtomicType() const
Definition TypeBase.h:8876
EnumDecl * castAsEnumDecl() const
Definition Type.h:59
bool isStdByteType() const
Definition Type.cpp:3316
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9330
bool isPointerOrReferenceType() const
Definition TypeBase.h:8688
bool isFunctionType() const
Definition TypeBase.h:8680
bool isVectorType() const
Definition TypeBase.h:8823
bool isRealFloatingType() const
Floating point categories.
Definition Type.cpp:2405
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:2985
bool isFloatingType() const
Definition Type.cpp:2389
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9277
bool isRecordType() const
Definition TypeBase.h:8811
bool isSizelessVectorType() const
Returns true for all scalable vector types.
Definition Type.cpp:2663
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3593
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition Expr.h:2631
QualType getArgumentType() const
Definition Expr.h:2674
QualType getTypeOfArgument() const
Gets the argument type, or the type of the argument expression, whichever is appropriate.
Definition Expr.h:2700
UnaryExprOrTypeTrait getKind() const
Definition Expr.h:2663
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2250
Expr * getSubExpr() const
Definition Expr.h:2291
Opcode getOpcode() const
Definition Expr.h:2286
bool canOverflow() const
Returns true if the unary operator can cause an overflow.
Definition Expr.h:2304
Represents C++ using-directive.
Definition DeclCXX.h:3114
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
bool isWeak() const
Determine whether this symbol is weakly-imported, or declared with the weak or weak-ref attr.
Definition Decl.cpp:5578
QualType getType() const
Definition Value.cpp:238
Represents a variable declaration or definition.
Definition Decl.h:932
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1591
bool isInitCapture() const
Whether this variable is the implicit variable for a lambda init-capture.
Definition Decl.h:1600
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition Decl.h:1304
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1247
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2630
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1214
const Expr * getInit() const
Definition Decl.h:1389
const APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
Definition Decl.cpp:2554
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1274
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1379
Represents a GCC generic vector type.
Definition TypeBase.h:4239
unsigned getNumElements() const
Definition TypeBase.h:4254
QualType getElementType() const
Definition TypeBase.h:4253
WhileStmt - This represents a 'while' stmt.
Definition Stmt.h:2707
Expr * getCond()
Definition Stmt.h:2759
DeclStmt * getConditionVariableDeclStmt()
If this WhileStmt has a condition variable, return the faux DeclStmt associated with the creation of ...
Definition Stmt.h:2795
VarDecl * getConditionVariable()
Retrieve the variable declared in this "while" statement, if any.
Definition Stmt.cpp:1247
Stmt * getBody()
Definition Stmt.h:2771
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
void invokeDtor()
Invokes the Destructor.
Compilation context for expressions.
Definition Compiler.h:112
llvm::SmallVector< InitLink > InitStack
Definition Compiler.h:492
bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E)
bool VisitCXXDeleteExpr(const CXXDeleteExpr *E)
bool VisitOffsetOfExpr(const OffsetOfExpr *E)
bool visitContinueStmt(const ContinueStmt *S)
bool VisitCharacterLiteral(const CharacterLiteral *E)
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init, OptPrimType InitT)
Pointer to the array(not the element!) must be on the stack when calling this.
UnsignedOrNone allocateLocal(DeclTy &&Decl, QualType Ty=QualType(), ScopeKind=ScopeKind::Block)
Allocates a space storing a local given its type.
bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E)
bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E)
bool visitInitializerPop(const Expr *E)
Similar, but will also pop the pointer.
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E)
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
Definition Compiler.h:285
bool VisitTypeTraitExpr(const TypeTraitExpr *E)
bool VisitLambdaExpr(const LambdaExpr *E)
bool VisitMemberExpr(const MemberExpr *E)
llvm::DenseMap< const OpaqueValueExpr *, unsigned > OpaqueExprs
OpaqueValueExpr to location mapping.
Definition Compiler.h:467
bool VisitBinaryOperator(const BinaryOperator *E)
bool visitAttributedStmt(const AttributedStmt *S)
bool VisitPackIndexingExpr(const PackIndexingExpr *E)
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
bool VisitCallExpr(const CallExpr *E)
std::optional< uint64_t > ArrayIndex
Current argument index. Needed to emit ArrayInitIndexExpr.
Definition Compiler.h:473
bool VisitPseudoObjectExpr(const PseudoObjectExpr *E)
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E)
const Function * getFunction(const FunctionDecl *FD)
Returns a function for the given FunctionDecl.
bool VisitFixedPointBinOp(const BinaryOperator *E)
bool VisitCastExpr(const CastExpr *E)
Definition Compiler.cpp:200
bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E)
bool VisitFixedPointUnaryOperator(const UnaryOperator *E)
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst, bool IsVolatile=false, ScopeKind SC=ScopeKind::Block)
Creates a local primitive value.
bool VisitComplexUnaryOperator(const UnaryOperator *E)
llvm::DenseMap< const SwitchCase *, LabelTy > CaseMap
Definition Compiler.h:118
bool VisitBlockExpr(const BlockExpr *E)
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E)
bool VisitLogicalBinOp(const BinaryOperator *E)
bool visitCompoundStmt(const CompoundStmt *S)
Context & Ctx
Current compilation context.
Definition Compiler.h:135
bool visitDeclRef(const ValueDecl *D, const Expr *E)
Visit the given decl as if we have a reference to it.
bool visitBreakStmt(const BreakStmt *S)
bool visitExpr(const Expr *E, bool DestroyToplevelScope) override
bool visitForStmt(const ForStmt *S)
bool VisitDeclRefExpr(const DeclRefExpr *E)
bool VisitOpaqueValueExpr(const OpaqueValueExpr *E)
bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E)
bool visitAPValue(const APValue &Val, PrimType ValType, SourceInfo Info)
Visit an APValue.
bool VisitStmtExpr(const StmtExpr *E)
bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E)
Definition Compiler.cpp:982
bool VisitFixedPointLiteral(const FixedPointLiteral *E)
const FunctionDecl * CompilingFunction
Definition Compiler.h:503
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E)
VarCreationState visitVarDecl(const VarDecl *VD, const Expr *Init, bool Toplevel=false)
Creates and initializes a variable from the given decl.
VariableScope< Emitter > * VarScope
Current scope.
Definition Compiler.h:470
bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext) override
Toplevel visitDeclAndReturn().
bool VisitCXXNewExpr(const CXXNewExpr *E)
const ValueDecl * InitializingDecl
Definition Compiler.h:490
bool VisitCompoundAssignOperator(const CompoundAssignOperator *E)
bool visit(const Expr *E) override
Evaluates an expression and places the result on the stack.
bool delegate(const Expr *E)
Just pass evaluation on to E.
bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) override
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
CaseMap CaseLabels
Switch case mapping.
Definition Compiler.h:499
bool visitAPValueInitializer(const APValue &Val, SourceInfo Info, QualType T)
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E)
bool visitInitList(ArrayRef< const Expr * > Inits, const Expr *ArrayFiller, const Expr *E)
bool VisitSizeOfPackExpr(const SizeOfPackExpr *E)
bool VisitPredefinedExpr(const PredefinedExpr *E)
bool VisitSourceLocExpr(const SourceLocExpr *E)
bool visitDeclStmt(const DeclStmt *DS, bool EvaluateConditionDecl=false)
bool emitCleanup()
Emits scope cleanup instructions.
bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E)
bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E)
bool visitInitializer(const Expr *E)
Compiles an initializer.
bool visitDtorCall(const VarDecl *VD, const APValue &Value) override
const Expr * SourceLocDefaultExpr
DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
Definition Compiler.h:476
bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E)
UnsignedOrNone OptLabelTy
Definition Compiler.h:117
bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E)
bool VisitPointerArithBinOp(const BinaryOperator *E)
Perform addition/subtraction of a pointer and an integer or subtraction of two pointers.
bool visitCallArgs(ArrayRef< const Expr * > Args, const FunctionDecl *FuncDecl, bool Activate, bool IsOperatorCall)
bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E)
bool visitDefaultStmt(const DefaultStmt *S)
bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E)
typename Emitter::LabelTy LabelTy
Definition Compiler.h:115
VarCreationState visitDecl(const VarDecl *VD)
bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E)
bool visitStmt(const Stmt *S)
bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E)
bool VisitVectorUnaryOperator(const UnaryOperator *E)
bool VisitCXXConstructExpr(const CXXConstructExpr *E)
bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E)
bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E)
bool VisitRecoveryExpr(const RecoveryExpr *E)
bool VisitRequiresExpr(const RequiresExpr *E)
bool Initializing
Flag inidicating if we're initializing an already created variable.
Definition Compiler.h:489
bool visitReturnStmt(const ReturnStmt *RS)
bool VisitCXXThrowExpr(const CXXThrowExpr *E)
bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
bool VisitChooseExpr(const ChooseExpr *E)
bool visitFunc(const FunctionDecl *F) override
bool visitCXXForRangeStmt(const CXXForRangeStmt *S)
bool visitCaseStmt(const CaseStmt *S)
bool VisitComplexBinOp(const BinaryOperator *E)
llvm::DenseMap< const ValueDecl *, Scope::Local > Locals
Variable to storage mapping.
Definition Compiler.h:464
bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E)
bool VisitCXXTypeidExpr(const CXXTypeidExpr *E)
UnsignedOrNone allocateTemporary(const Expr *E)
bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinID)
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E)
OptPrimType ReturnType
Type of the expression returned by the function.
Definition Compiler.h:496
bool VisitUnaryOperator(const UnaryOperator *E)
bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E)
OptPrimType classify(const Expr *E) const
Definition Compiler.h:279
llvm::SmallVector< LabelInfo > LabelInfoStack
Stack of label information for loops and switch statements.
Definition Compiler.h:501
bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
bool visitDoStmt(const DoStmt *S)
bool VisitIntegerLiteral(const IntegerLiteral *E)
Definition Compiler.cpp:987
bool VisitInitListExpr(const InitListExpr *E)
bool VisitVectorBinOp(const BinaryOperator *E)
bool VisitStringLiteral(const StringLiteral *E)
bool VisitParenExpr(const ParenExpr *E)
bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E)
bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E)
bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E)
bool DiscardResult
Flag indicating if return value is to be discarded.
Definition Compiler.h:479
bool VisitEmbedExpr(const EmbedExpr *E)
bool VisitConvertVectorExpr(const ConvertVectorExpr *E)
bool VisitCXXThisExpr(const CXXThisExpr *E)
bool VisitConstantExpr(const ConstantExpr *E)
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
bool visitSwitchStmt(const SwitchStmt *S)
bool VisitCXXUuidofExpr(const CXXUuidofExpr *E)
bool VisitExprWithCleanups(const ExprWithCleanups *E)
bool visitAsLValue(const Expr *E)
bool visitWhileStmt(const WhileStmt *S)
bool visitIfStmt(const IfStmt *IS)
bool VisitAddrLabelExpr(const AddrLabelExpr *E)
bool canClassify(const Expr *E) const
Definition Compiler.h:281
bool VisitFloatingLiteral(const FloatingLiteral *E)
Definition Compiler.cpp:995
Program & P
Program to link to.
Definition Compiler.h:137
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
bool VisitGNUNullExpr(const GNUNullExpr *E)
bool VisitImaginaryLiteral(const ImaginaryLiteral *E)
bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E)
bool visitCXXTryStmt(const CXXTryStmt *S)
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition Context.cpp:741
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:159
Scope used to handle temporaries in toplevel variable declarations.
Definition Compiler.cpp:40
DeclScope(Compiler< Emitter > *Ctx, const ValueDecl *VD)
Definition Compiler.cpp:42
Wrapper around fixed point types.
Definition FixedPoint.h:23
static FixedPoint zero(llvm::FixedPointSemantics Sem)
Definition FixedPoint.h:36
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
bool singleWord() const
Definition Floating.h:107
Bytecode function.
Definition Function.h:99
bool hasThisPointer() const
Definition Function.h:226
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition Function.h:156
When generating code for e.g.
Definition Compiler.cpp:175
LocOverrideScope(Compiler< Emitter > *Ctx, SourceInfo NewValue, bool Enabled=true)
Definition Compiler.cpp:177
Generic scope for local variables.
Definition Compiler.h:568
bool destroyLocals(const Expr *E=nullptr) override
Explicit destruction of local variables.
Definition Compiler.h:581
LocalScope(Compiler< Emitter > *Ctx, ScopeKind Kind=ScopeKind::Block)
Definition Compiler.h:570
Sets the context for break/continue statements.
Definition Compiler.cpp:114
typename Compiler< Emitter >::LabelTy LabelTy
Definition Compiler.cpp:116
typename Compiler< Emitter >::OptLabelTy OptLabelTy
Definition Compiler.cpp:117
typename Compiler< Emitter >::LabelInfo LabelInfo
Definition Compiler.cpp:118
LoopScope(Compiler< Emitter > *Ctx, const Stmt *Name, LabelTy BreakLabel, LabelTy ContinueLabel)
Definition Compiler.cpp:120
PrimType value_or(PrimType PT) const
Definition PrimType.h:88
Scope used to handle initialization methods.
Definition Compiler.cpp:60
OptionScope(Compiler< Emitter > *Ctx, bool NewDiscardResult, bool NewInitializing, bool NewToLValue)
Root constructor, compiling or discarding primitives.
Definition Compiler.cpp:63
Context to manage declaration lifetimes.
Definition Program.h:148
Structure/Class descriptor.
Definition Record.h:25
bool isUnion() const
Checks if the record is a union.
Definition Record.h:69
const Field * getField(unsigned I) const
Definition Record.h:95
const Base * getBase(unsigned I) const
Definition Record.h:107
bool hasTrivialDtor() const
Returns true for anonymous unions and records with no destructor or for those with a trivial destruct...
Definition Record.cpp:45
Describes a scope block.
Definition Function.h:36
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
const Expr * asExpr() const
Definition Source.cpp:35
typename Compiler< Emitter >::LabelTy LabelTy
Definition Compiler.cpp:142
typename Compiler< Emitter >::OptLabelTy OptLabelTy
Definition Compiler.cpp:143
typename Compiler< Emitter >::LabelInfo LabelInfo
Definition Compiler.cpp:145
typename Compiler< Emitter >::CaseMap CaseMap
Definition Compiler.cpp:144
SwitchScope(Compiler< Emitter > *Ctx, const Stmt *Name, CaseMap &&CaseLabels, LabelTy BreakLabel, OptLabelTy DefaultLabel)
Definition Compiler.cpp:147
Scope chain managing the variable lifetimes.
Definition Compiler.h:510
Compiler< Emitter > * Ctx
Compiler instance.
Definition Compiler.h:561
virtual void addLocal(Scope::Local Local)
Definition Compiler.h:521
VariableScope * getParent() const
Definition Compiler.h:551
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:425
bool LT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1507
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:770
static bool Activate(InterpState &S, CodePtr OpPC)
Definition Interp.h:2265
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:2777
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:1514
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:2382
bool Mul(InterpState &S, CodePtr OpPC)
Definition Interp.h:479
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:964
bool Add(InterpState &S, CodePtr OpPC)
Definition Interp.h:396
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