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