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