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