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