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