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