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