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