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 (!SubExpr->getType()->isFloatingType() ||
380 !E->getType()->isFloatingType())
381 return false;
382 if (!this->visit(SubExpr))
383 return false;
384 const auto *TargetSemantics = &Ctx.getFloatSemantics(E->getType());
385 return this->emitCastFP(TargetSemantics, getRoundingMode(E), E);
386 }
387
388 case CK_IntegralToFloating: {
389 if (!E->getType()->isRealFloatingType())
390 return false;
391 if (!this->visit(SubExpr))
392 return false;
393 const auto *TargetSemantics = &Ctx.getFloatSemantics(E->getType());
394 return this->emitCastIntegralFloating(classifyPrim(SubExpr),
395 TargetSemantics, getFPOptions(E), E);
396 }
397
398 case CK_FloatingToBoolean: {
399 if (!SubExpr->getType()->isRealFloatingType() ||
400 !E->getType()->isBooleanType())
401 return false;
402 if (const auto *FL = dyn_cast<FloatingLiteral>(SubExpr))
403 return this->emitConstBool(FL->getValue().isNonZero(), E);
404 if (!this->visit(SubExpr))
405 return false;
406 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
407 }
408
409 case CK_FloatingToIntegral: {
411 return false;
412 if (!this->visit(SubExpr))
413 return false;
414 PrimType ToT = classifyPrim(E);
415 if (ToT == PT_IntAP)
416 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(E->getType()),
417 getFPOptions(E), E);
418 if (ToT == PT_IntAPS)
419 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(E->getType()),
420 getFPOptions(E), E);
421
422 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
423 }
424
425 case CK_NullToPointer:
426 case CK_NullToMemberPointer: {
427 if (!this->discard(SubExpr))
428 return false;
429 const Descriptor *Desc = nullptr;
430 const QualType PointeeType = E->getType()->getPointeeType();
431 if (!PointeeType.isNull()) {
432 if (OptPrimType T = classify(PointeeType))
433 Desc = P.createDescriptor(SubExpr, *T);
434 else
435 Desc = P.createDescriptor(SubExpr, PointeeType.getTypePtr(),
436 std::nullopt, /*IsConst=*/true);
437 }
438
439 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
440 return this->emitNull(classifyPrim(E->getType()), Val, Desc, E);
441 }
442
443 case CK_PointerToIntegral: {
444 if (!this->visit(SubExpr))
445 return false;
446
447 // If SubExpr doesn't result in a pointer, make it one.
448 if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) {
449 assert(isPtrType(FromT));
450 if (!this->emitDecayPtr(FromT, PT_Ptr, E))
451 return false;
452 }
453
454 PrimType T = classifyPrim(E->getType());
455 if (T == PT_IntAP)
456 return this->emitCastPointerIntegralAP(Ctx.getBitWidth(E->getType()), E);
457 if (T == PT_IntAPS)
458 return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(E->getType()), E);
459 return this->emitCastPointerIntegral(T, E);
460 }
461
462 case CK_ArrayToPointerDecay: {
463 if (!this->visit(SubExpr))
464 return false;
465 return this->emitArrayDecay(E);
466 }
467
468 case CK_IntegralToPointer: {
469 QualType IntType = SubExpr->getType();
470 assert(IntType->isIntegralOrEnumerationType());
471 if (!this->visit(SubExpr))
472 return false;
473 // FIXME: I think the discard is wrong since the int->ptr cast might cause a
474 // diagnostic.
475 PrimType T = classifyPrim(IntType);
476 QualType PtrType = E->getType();
477 const Descriptor *Desc;
478 if (OptPrimType T = classify(PtrType->getPointeeType()))
479 Desc = P.createDescriptor(SubExpr, *T);
480 else if (PtrType->getPointeeType()->isVoidType())
481 Desc = nullptr;
482 else
483 Desc = P.createDescriptor(E, PtrType->getPointeeType().getTypePtr(),
484 Descriptor::InlineDescMD, /*IsConst=*/true);
485
486 if (!this->emitGetIntPtr(T, Desc, E))
487 return false;
488
489 PrimType DestPtrT = classifyPrim(PtrType);
490 if (DestPtrT == PT_Ptr)
491 return true;
492
493 // In case we're converting the integer to a non-Pointer.
494 return this->emitDecayPtr(PT_Ptr, DestPtrT, E);
495 }
496
497 case CK_AtomicToNonAtomic:
498 case CK_ConstructorConversion:
499 case CK_FunctionToPointerDecay:
500 case CK_NonAtomicToAtomic:
501 case CK_NoOp:
502 case CK_UserDefinedConversion:
503 case CK_AddressSpaceConversion:
504 case CK_CPointerToObjCPointerCast:
505 return this->delegate(SubExpr);
506
507 case CK_BitCast: {
508 if (E->containsErrors())
509 return false;
510 QualType ETy = E->getType();
511 // Reject bitcasts to atomic types.
512 if (ETy->isAtomicType()) {
513 if (!this->discard(SubExpr))
514 return false;
515 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
516 }
517 QualType SubExprTy = SubExpr->getType();
518 OptPrimType FromT = classify(SubExprTy);
519 // Casts from integer/vector to vector.
520 if (E->getType()->isVectorType())
521 return this->emitBuiltinBitCast(E);
522
523 OptPrimType ToT = classify(E->getType());
524 if (!FromT || !ToT)
525 return false;
526
527 assert(isPtrType(*FromT));
528 assert(isPtrType(*ToT));
529 bool SrcIsVoidPtr = SubExprTy->isVoidPointerType();
530 if (FromT == ToT) {
531 if (E->getType()->isVoidPointerType() &&
532 !SubExprTy->isFunctionPointerType()) {
533 return this->delegate(SubExpr);
534 }
535
536 if (!this->visit(SubExpr))
537 return false;
538 if (!this->emitCheckBitCast(ETy->getPointeeType().getTypePtr(),
539 SrcIsVoidPtr, E))
540 return false;
541
542 if (E->getType()->isFunctionPointerType() ||
543 SubExprTy->isFunctionPointerType()) {
544 return this->emitFnPtrCast(E);
545 }
546 if (FromT == PT_Ptr)
547 return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), E);
548 return true;
549 }
550
551 if (!this->visit(SubExpr))
552 return false;
553 return this->emitDecayPtr(*FromT, *ToT, E);
554 }
555 case CK_IntegralToBoolean:
556 case CK_FixedPointToBoolean: {
557 // HLSL uses this to cast to one-element vectors.
558 OptPrimType FromT = classify(SubExpr->getType());
559 if (!FromT)
560 return false;
561
562 if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr))
563 return this->emitConst(IL->getValue(), E);
564 if (!this->visit(SubExpr))
565 return false;
566 return this->emitCast(*FromT, classifyPrim(E), E);
567 }
568
569 case CK_BooleanToSignedIntegral:
570 case CK_IntegralCast: {
571 OptPrimType FromT = classify(SubExpr->getType());
572 OptPrimType ToT = classify(E->getType());
573 if (!FromT || !ToT)
574 return false;
575
576 // Try to emit a casted known constant value directly.
577 if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr)) {
578 if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP &&
579 FromT != PT_IntAPS && !E->getType()->isEnumeralType())
580 return this->emitConst(APSInt(IL->getValue(), !isSignedType(*FromT)),
581 E);
582 if (!this->emitConst(IL->getValue(), SubExpr))
583 return false;
584 } else {
585 if (!this->visit(SubExpr))
586 return false;
587 }
588
589 // Possibly diagnose casts to enum types if the target type does not
590 // have a fixed size.
591 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) {
592 const auto *ED = E->getType()->castAsEnumDecl();
593 if (!ED->isFixed()) {
594 if (!this->emitCheckEnumValue(*FromT, ED, E))
595 return false;
596 }
597 }
598
599 if (ToT == PT_IntAP) {
600 if (!this->emitCastAP(*FromT, Ctx.getBitWidth(E->getType()), E))
601 return false;
602 } else if (ToT == PT_IntAPS) {
603 if (!this->emitCastAPS(*FromT, Ctx.getBitWidth(E->getType()), E))
604 return false;
605 } else {
606 if (FromT == ToT)
607 return true;
608 if (!this->emitCast(*FromT, *ToT, E))
609 return false;
610 }
611 if (E->getCastKind() == CK_BooleanToSignedIntegral)
612 return this->emitNeg(*ToT, E);
613 return true;
614 }
615
616 case CK_PointerToBoolean:
617 case CK_MemberPointerToBoolean: {
618 PrimType PtrT = classifyPrim(SubExpr->getType());
619
620 if (!this->visit(SubExpr))
621 return false;
622 return this->emitIsNonNull(PtrT, E);
623 }
624
625 case CK_IntegralComplexToBoolean:
626 case CK_FloatingComplexToBoolean: {
627 if (!this->visit(SubExpr))
628 return false;
629 return this->emitComplexBoolCast(SubExpr);
630 }
631
632 case CK_IntegralComplexToReal:
633 case CK_FloatingComplexToReal:
634 return this->emitComplexReal(SubExpr);
635
636 case CK_IntegralRealToComplex:
637 case CK_FloatingRealToComplex: {
638 // We're creating a complex value here, so we need to
639 // allocate storage for it.
640 if (!Initializing) {
641 UnsignedOrNone LocalIndex = allocateTemporary(E);
642 if (!LocalIndex)
643 return false;
644 if (!this->emitGetPtrLocal(*LocalIndex, E))
645 return false;
646 }
647
648 PrimType T = classifyPrim(SubExpr->getType());
649 // Init the complex value to {SubExpr, 0}.
650 if (!this->visitArrayElemInit(0, SubExpr, T))
651 return false;
652 // Zero-init the second element.
653 if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
654 return false;
655 return this->emitInitElem(T, 1, SubExpr);
656 }
657
658 case CK_IntegralComplexCast:
659 case CK_FloatingComplexCast:
660 case CK_IntegralComplexToFloatingComplex:
661 case CK_FloatingComplexToIntegralComplex: {
662 assert(E->getType()->isAnyComplexType());
663 assert(SubExpr->getType()->isAnyComplexType());
664 if (!Initializing) {
665 UnsignedOrNone LocalIndex = allocateLocal(E);
666 if (!LocalIndex)
667 return false;
668 if (!this->emitGetPtrLocal(*LocalIndex, E))
669 return false;
670 }
671
672 // Location for the SubExpr.
673 // Since SubExpr is of complex type, visiting it results in a pointer
674 // anyway, so we just create a temporary pointer variable.
675 unsigned SubExprOffset =
676 allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
677 if (!this->visit(SubExpr))
678 return false;
679 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
680 return false;
681
682 PrimType SourceElemT = classifyComplexElementType(SubExpr->getType());
683 QualType DestElemType =
684 E->getType()->getAs<ComplexType>()->getElementType();
685 PrimType DestElemT = classifyPrim(DestElemType);
686 // Cast both elements individually.
687 for (unsigned I = 0; I != 2; ++I) {
688 if (!this->emitGetLocal(PT_Ptr, SubExprOffset, E))
689 return false;
690 if (!this->emitArrayElemPop(SourceElemT, I, E))
691 return false;
692
693 // Do the cast.
694 if (!this->emitPrimCast(SourceElemT, DestElemT, DestElemType, E))
695 return false;
696
697 // Save the value.
698 if (!this->emitInitElem(DestElemT, I, E))
699 return false;
700 }
701 return true;
702 }
703
704 case CK_VectorSplat: {
705 assert(!canClassify(E->getType()));
706 assert(canClassify(SubExpr->getType()));
707 assert(E->getType()->isVectorType());
708
709 if (!Initializing) {
710 UnsignedOrNone LocalIndex = allocateLocal(E);
711 if (!LocalIndex)
712 return false;
713 if (!this->emitGetPtrLocal(*LocalIndex, E))
714 return false;
715 }
716
717 const auto *VT = E->getType()->getAs<VectorType>();
718 PrimType ElemT = classifyPrim(SubExpr->getType());
719 unsigned ElemOffset =
720 allocateLocalPrimitive(SubExpr, ElemT, /*IsConst=*/true);
721
722 // Prepare a local variable for the scalar value.
723 if (!this->visit(SubExpr))
724 return false;
725 if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, E))
726 return false;
727
728 if (!this->emitSetLocal(ElemT, ElemOffset, E))
729 return false;
730
731 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
732 if (!this->emitGetLocal(ElemT, ElemOffset, E))
733 return false;
734 if (!this->emitInitElem(ElemT, I, E))
735 return false;
736 }
737
738 return true;
739 }
740
741 case CK_HLSLVectorTruncation: {
742 assert(SubExpr->getType()->isVectorType());
743 if (OptPrimType ResultT = classify(E)) {
744 assert(!DiscardResult);
745 // Result must be either a float or integer. Take the first element.
746 if (!this->visit(SubExpr))
747 return false;
748 return this->emitArrayElemPop(*ResultT, 0, E);
749 }
750 // Otherwise, this truncates from one vector type to another.
751 assert(E->getType()->isVectorType());
752
753 if (!Initializing) {
754 UnsignedOrNone LocalIndex = allocateTemporary(E);
755 if (!LocalIndex)
756 return false;
757 if (!this->emitGetPtrLocal(*LocalIndex, E))
758 return false;
759 }
760 unsigned ToSize = E->getType()->getAs<VectorType>()->getNumElements();
761 assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize);
762 if (!this->visit(SubExpr))
763 return false;
764 return this->emitCopyArray(classifyVectorElementType(E->getType()), 0, 0,
765 ToSize, E);
766 };
767
768 case CK_IntegralToFixedPoint: {
769 if (!this->visit(SubExpr))
770 return false;
771
772 auto Sem =
773 Ctx.getASTContext().getFixedPointSemantics(E->getType()).toOpaqueInt();
774 return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()),
775 Sem, E);
776 }
777 case CK_FloatingToFixedPoint: {
778 if (!this->visit(SubExpr))
779 return false;
780
781 auto Sem =
782 Ctx.getASTContext().getFixedPointSemantics(E->getType()).toOpaqueInt();
783 return this->emitCastFloatingFixedPoint(Sem, E);
784 }
785 case CK_FixedPointToFloating: {
786 if (!this->visit(SubExpr))
787 return false;
788 const auto *TargetSemantics = &Ctx.getFloatSemantics(E->getType());
789 return this->emitCastFixedPointFloating(TargetSemantics, E);
790 }
791 case CK_FixedPointToIntegral: {
792 if (!this->visit(SubExpr))
793 return false;
794 return this->emitCastFixedPointIntegral(classifyPrim(E->getType()), E);
795 }
796 case CK_FixedPointCast: {
797 if (!this->visit(SubExpr))
798 return false;
799 auto Sem =
800 Ctx.getASTContext().getFixedPointSemantics(E->getType()).toOpaqueInt();
801 return this->emitCastFixedPoint(Sem, E);
802 }
803
804 case CK_ToVoid:
805 return discard(SubExpr);
806
807 case CK_Dynamic:
808 // This initially goes through VisitCXXDynamicCastExpr, where we emit
809 // a diagnostic if appropriate.
810 return this->delegate(SubExpr);
811
812 case CK_LValueBitCast:
813 return this->emitInvalidCast(CastKind::ReinterpretLike, /*Fatal=*/true, E);
814
815 case CK_HLSLArrayRValue: {
816 // Non-decaying array rvalue cast - creates an rvalue copy of an lvalue
817 // array, similar to LValueToRValue for composite types.
818 if (!Initializing) {
819 UnsignedOrNone LocalIndex = allocateLocal(E);
820 if (!LocalIndex)
821 return false;
822 if (!this->emitGetPtrLocal(*LocalIndex, E))
823 return false;
824 }
825 if (!this->visit(SubExpr))
826 return false;
827 return this->emitMemcpy(E);
828 }
829
830 case CK_HLSLMatrixTruncation: {
831 assert(SubExpr->getType()->isConstantMatrixType());
832 if (OptPrimType ResultT = classify(E)) {
833 assert(!DiscardResult);
834 // Result must be either a float or integer. Take the first element.
835 if (!this->visit(SubExpr))
836 return false;
837 return this->emitArrayElemPop(*ResultT, 0, E);
838 }
839 // Otherwise, this truncates to a a constant matrix type.
840 assert(E->getType()->isConstantMatrixType());
841
842 if (!Initializing) {
843 UnsignedOrNone LocalIndex = allocateTemporary(E);
844 if (!LocalIndex)
845 return false;
846 if (!this->emitGetPtrLocal(*LocalIndex, E))
847 return false;
848 }
849 unsigned ToSize =
850 E->getType()->getAs<ConstantMatrixType>()->getNumElementsFlattened();
851 if (!this->visit(SubExpr))
852 return false;
853 return this->emitCopyArray(classifyMatrixElementType(SubExpr->getType()), 0,
854 0, ToSize, E);
855 }
856
857 case CK_HLSLAggregateSplatCast: {
858 // Aggregate splat cast: convert a scalar value to one of an aggregate type,
859 // inserting casts when necessary to convert the scalar to the aggregate's
860 // element type(s).
861 // TODO: Aggregate splat to struct and array types
862 assert(canClassify(SubExpr->getType()));
863
864 unsigned NumElems;
865 PrimType DestElemT;
866 QualType DestElemType;
867 if (const auto *VT = E->getType()->getAs<VectorType>()) {
868 NumElems = VT->getNumElements();
869 DestElemType = VT->getElementType();
870 } else if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) {
871 NumElems = MT->getNumElementsFlattened();
872 DestElemType = MT->getElementType();
873 } else {
874 return false;
875 }
876 DestElemT = classifyPrim(DestElemType);
877
878 if (!Initializing) {
879 UnsignedOrNone LocalIndex = allocateLocal(E);
880 if (!LocalIndex)
881 return false;
882 if (!this->emitGetPtrLocal(*LocalIndex, E))
883 return false;
884 }
885
886 PrimType SrcElemT = classifyPrim(SubExpr->getType());
887 unsigned SrcOffset =
888 allocateLocalPrimitive(SubExpr, DestElemT, /*IsConst=*/true);
889
890 if (!this->visit(SubExpr))
891 return false;
892 if (SrcElemT != DestElemT) {
893 if (!this->emitPrimCast(SrcElemT, DestElemT, DestElemType, E))
894 return false;
895 }
896 if (!this->emitSetLocal(DestElemT, SrcOffset, E))
897 return false;
898
899 for (unsigned I = 0; I != NumElems; ++I) {
900 if (!this->emitGetLocal(DestElemT, SrcOffset, E))
901 return false;
902 if (!this->emitInitElem(DestElemT, I, E))
903 return false;
904 }
905 return true;
906 }
907
908 case CK_HLSLElementwiseCast: {
909 // Elementwise cast: flatten source elements of one aggregate type and store
910 // to a destination scalar or aggregate type of the same or fewer number of
911 // elements, while inserting casts as necessary.
912 // TODO: Elementwise cast to structs, nested arrays, and arrays of composite
913 // types
914 QualType SrcType = SubExpr->getType();
915 QualType DestType = E->getType();
916
917 // Allowed SrcTypes
918 const auto *SrcVT = SrcType->getAs<VectorType>();
919 const auto *SrcMT = SrcType->getAs<ConstantMatrixType>();
920 const auto *SrcAT = SrcType->getAsArrayTypeUnsafe();
921 const auto *SrcCAT = SrcAT ? dyn_cast<ConstantArrayType>(SrcAT) : nullptr;
922
923 // Allowed DestTypes
924 const auto *DestVT = DestType->getAs<VectorType>();
925 const auto *DestMT = DestType->getAs<ConstantMatrixType>();
926 const auto *DestAT = DestType->getAsArrayTypeUnsafe();
927 const auto *DestCAT =
928 DestAT ? dyn_cast<ConstantArrayType>(DestAT) : nullptr;
929 const OptPrimType DestPT = classify(DestType);
930
931 if (!SrcVT && !SrcMT && !SrcCAT)
932 return false;
933 if (!DestVT && !DestMT && !DestCAT && !DestPT)
934 return false;
935
936 unsigned SrcNumElems;
937 PrimType SrcElemT;
938 if (SrcVT) {
939 SrcNumElems = SrcVT->getNumElements();
940 SrcElemT = classifyPrim(SrcVT->getElementType());
941 } else if (SrcMT) {
942 SrcNumElems = SrcMT->getNumElementsFlattened();
943 SrcElemT = classifyPrim(SrcMT->getElementType());
944 } else if (SrcCAT) {
945 SrcNumElems = SrcCAT->getZExtSize();
946 SrcElemT = classifyPrim(SrcCAT->getElementType());
947 }
948
949 if (DestPT) {
950 // Scalar destination: extract element 0 and cast.
951 if (!this->visit(SubExpr))
952 return false;
953 if (!this->emitArrayElemPop(SrcElemT, 0, E))
954 return false;
955 if (SrcElemT != *DestPT) {
956 if (!this->emitPrimCast(SrcElemT, *DestPT, DestType, E))
957 return false;
958 }
959 return true;
960 }
961
962 unsigned DestNumElems;
963 PrimType DestElemT;
964 QualType DestElemType;
965 if (DestVT) {
966 DestNumElems = DestVT->getNumElements();
967 DestElemType = DestVT->getElementType();
968 } else if (DestMT) {
969 DestNumElems = DestMT->getNumElementsFlattened();
970 DestElemType = DestMT->getElementType();
971 } else if (DestCAT) {
972 DestNumElems = DestCAT->getZExtSize();
973 DestElemType = DestCAT->getElementType();
974 }
975 DestElemT = classifyPrim(DestElemType);
976
977 if (!Initializing) {
978 UnsignedOrNone LocalIndex = allocateTemporary(E);
979 if (!LocalIndex)
980 return false;
981 if (!this->emitGetPtrLocal(*LocalIndex, E))
982 return false;
983 }
984
985 unsigned SrcOffset =
986 allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
987 if (!this->visit(SubExpr))
988 return false;
989 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
990 return false;
991
992 unsigned NumElems = std::min(SrcNumElems, DestNumElems);
993 for (unsigned I = 0; I != NumElems; ++I) {
994 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
995 return false;
996 if (!this->emitArrayElemPop(SrcElemT, I, E))
997 return false;
998 if (SrcElemT != DestElemT) {
999 if (!this->emitPrimCast(SrcElemT, DestElemT, DestElemType, E))
1000 return false;
1001 }
1002 if (!this->emitInitElem(DestElemT, I, E))
1003 return false;
1004 }
1005 return true;
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 return this->emitNegFixedPoint(E);
1942 default:
1943 return false;
1944 }
1945
1946 llvm_unreachable("Unhandled unary opcode");
1947}
1948
1949template <class Emitter>
1951 const ImplicitValueInitExpr *E) {
1952 if (DiscardResult)
1953 return true;
1954
1955 QualType QT = E->getType();
1956
1957 if (OptPrimType T = classify(QT))
1958 return this->visitZeroInitializer(*T, QT, E);
1959
1960 if (QT->isRecordType()) {
1961 const RecordDecl *RD = QT->getAsRecordDecl();
1962 assert(RD);
1963 if (RD->isInvalidDecl())
1964 return false;
1965
1966 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1967 CXXRD && CXXRD->getNumVBases() > 0) {
1968 // TODO: Diagnose.
1969 return false;
1970 }
1971
1972 const Record *R = getRecord(QT);
1973 if (!R)
1974 return false;
1975
1976 assert(Initializing);
1977 return this->visitZeroRecordInitializer(R, E);
1978 }
1979
1980 if (QT->isIncompleteArrayType())
1981 return true;
1982
1983 if (QT->isArrayType())
1984 return this->visitZeroArrayInitializer(QT, E);
1985
1986 if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) {
1987 assert(Initializing);
1988 QualType ElemQT = ComplexTy->getElementType();
1989 PrimType ElemT = classifyPrim(ElemQT);
1990 for (unsigned I = 0; I < 2; ++I) {
1991 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1992 return false;
1993 if (!this->emitInitElem(ElemT, I, E))
1994 return false;
1995 }
1996 return true;
1997 }
1998
1999 if (const auto *VecT = E->getType()->getAs<VectorType>()) {
2000 unsigned NumVecElements = VecT->getNumElements();
2001 QualType ElemQT = VecT->getElementType();
2002 PrimType ElemT = classifyPrim(ElemQT);
2003
2004 for (unsigned I = 0; I < NumVecElements; ++I) {
2005 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2006 return false;
2007 if (!this->emitInitElem(ElemT, I, E))
2008 return false;
2009 }
2010 return true;
2011 }
2012
2013 if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) {
2014 unsigned NumElems = MT->getNumElementsFlattened();
2015 QualType ElemQT = MT->getElementType();
2016 PrimType ElemT = classifyPrim(ElemQT);
2017
2018 for (unsigned I = 0; I != NumElems; ++I) {
2019 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2020 return false;
2021 if (!this->emitInitElem(ElemT, I, E))
2022 return false;
2023 }
2024 return true;
2025 }
2026
2027 return false;
2028}
2029
2030template <class Emitter>
2032 if (E->getType()->isVoidType())
2033 return false;
2034
2035 const Expr *LHS = E->getLHS();
2036 const Expr *RHS = E->getRHS();
2037 const Expr *Index = E->getIdx();
2038 const Expr *Base = E->getBase();
2039
2040 // C++17's rules require us to evaluate the LHS first, regardless of which
2041 // side is the base.
2042 bool Success = true;
2043 for (const Expr *SubExpr : {LHS, RHS}) {
2044 if (!this->visit(SubExpr)) {
2045 Success = false;
2046 continue;
2047 }
2048
2049 // Expand the base if this is a subscript on a
2050 // pointer expression.
2051 if (SubExpr == Base && Base->getType()->isPointerType()) {
2052 if (!this->emitExpandPtr(E))
2053 Success = false;
2054 }
2055 }
2056
2057 if (!Success)
2058 return false;
2059
2060 OptPrimType IndexT = classify(Index->getType());
2061 // In error-recovery cases, the index expression has a dependent type.
2062 if (!IndexT)
2063 return this->emitError(E);
2064 // If the index is first, we need to change that.
2065 if (LHS == Index) {
2066 if (!this->emitFlip(PT_Ptr, *IndexT, E))
2067 return false;
2068 }
2069
2070 if (!this->emitArrayElemPtrPop(*IndexT, E))
2071 return false;
2072 if (DiscardResult)
2073 return this->emitPopPtr(E);
2074
2075 if (E->isGLValue())
2076 return true;
2077
2079 return this->emitLoadPop(*T, E);
2080}
2081
2082template <class Emitter>
2084 const Expr *ArrayFiller, const Expr *E) {
2086
2087 QualType QT = E->getType();
2088 if (const auto *AT = QT->getAs<AtomicType>())
2089 QT = AT->getValueType();
2090
2091 if (QT->isVoidType()) {
2092 if (Inits.size() == 0)
2093 return true;
2094 return this->emitInvalid(E);
2095 }
2096
2097 // Handle discarding first.
2098 if (DiscardResult) {
2099 for (const Expr *Init : Inits) {
2100 if (!this->discard(Init))
2101 return false;
2102 }
2103 return true;
2104 }
2105
2106 // Primitive values.
2107 if (OptPrimType T = classify(QT)) {
2108 assert(!DiscardResult);
2109 if (Inits.size() == 0)
2110 return this->visitZeroInitializer(*T, QT, E);
2111 assert(Inits.size() == 1);
2112 return this->delegate(Inits[0]);
2113 }
2114
2115 if (QT->isRecordType()) {
2116 const Record *R = getRecord(QT);
2117
2118 if (Inits.size() == 1 && E->getType() == Inits[0]->getType())
2119 return this->delegate(Inits[0]);
2120
2121 if (!R)
2122 return false;
2123
2124 auto initPrimitiveField = [=](const Record::Field *FieldToInit,
2125 const Expr *Init, PrimType T,
2126 bool Activate = false) -> bool {
2128 if (!this->visit(Init))
2129 return false;
2130
2131 bool BitField = FieldToInit->isBitField();
2132 if (BitField && Activate)
2133 return this->emitInitBitFieldActivate(T, FieldToInit->Offset,
2134 FieldToInit->bitWidth(), E);
2135 if (BitField)
2136 return this->emitInitBitField(T, FieldToInit->Offset,
2137 FieldToInit->bitWidth(), E);
2138 if (Activate)
2139 return this->emitInitFieldActivate(T, FieldToInit->Offset, E);
2140 return this->emitInitField(T, FieldToInit->Offset, E);
2141 };
2142
2143 auto initCompositeField = [=](const Record::Field *FieldToInit,
2144 const Expr *Init,
2145 bool Activate = false) -> bool {
2147 InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
2148
2149 // Non-primitive case. Get a pointer to the field-to-initialize
2150 // on the stack and recurse into visitInitializer().
2151 if (!this->emitGetPtrField(FieldToInit->Offset, Init))
2152 return false;
2153
2154 if (Activate && !this->emitActivate(E))
2155 return false;
2156
2157 if (!this->visitInitializer(Init))
2158 return false;
2159 return this->emitPopPtr(E);
2160 };
2161
2162 if (R->isUnion()) {
2163 if (Inits.size() == 0) {
2164 if (!this->visitZeroRecordInitializer(R, E))
2165 return false;
2166 } else {
2167 const Expr *Init = Inits[0];
2168 const FieldDecl *FToInit = nullptr;
2169 if (const auto *ILE = dyn_cast<InitListExpr>(E))
2170 FToInit = ILE->getInitializedFieldInUnion();
2171 else
2172 FToInit = cast<CXXParenListInitExpr>(E)->getInitializedFieldInUnion();
2173
2174 const Record::Field *FieldToInit = R->getField(FToInit);
2175 if (OptPrimType T = classify(Init)) {
2176 if (!initPrimitiveField(FieldToInit, Init, *T, /*Activate=*/true))
2177 return false;
2178 } else {
2179 if (!initCompositeField(FieldToInit, Init, /*Activate=*/true))
2180 return false;
2181 }
2182 }
2183 return this->emitFinishInit(E);
2184 }
2185
2186 assert(!R->isUnion());
2187 unsigned InitIndex = 0;
2188 for (const Expr *Init : Inits) {
2189 // Skip unnamed bitfields.
2190 while (InitIndex < R->getNumFields() &&
2191 R->getField(InitIndex)->isUnnamedBitField())
2192 ++InitIndex;
2193
2194 if (OptPrimType T = classify(Init)) {
2195 const Record::Field *FieldToInit = R->getField(InitIndex);
2196 if (!initPrimitiveField(FieldToInit, Init, *T))
2197 return false;
2198 ++InitIndex;
2199 } else {
2200 // Initializer for a direct base class.
2201 if (const Record::Base *B = R->getBase(Init->getType())) {
2202 if (!this->emitGetPtrBase(B->Offset, Init))
2203 return false;
2204
2205 if (!this->visitInitializer(Init))
2206 return false;
2207
2208 if (!this->emitFinishInitPop(E))
2209 return false;
2210 // Base initializers don't increase InitIndex, since they don't count
2211 // into the Record's fields.
2212 } else {
2213 const Record::Field *FieldToInit = R->getField(InitIndex);
2214 if (!initCompositeField(FieldToInit, Init))
2215 return false;
2216 ++InitIndex;
2217 }
2218 }
2219 }
2220 return this->emitFinishInit(E);
2221 }
2222
2223 if (QT->isArrayType()) {
2224 if (Inits.size() == 1 && QT == Inits[0]->getType())
2225 return this->delegate(Inits[0]);
2226
2227 const ConstantArrayType *CAT =
2228 Ctx.getASTContext().getAsConstantArrayType(QT);
2229 uint64_t NumElems = CAT->getZExtSize();
2230
2231 if (!this->emitCheckArraySize(NumElems, E))
2232 return false;
2233
2234 OptPrimType InitT = classify(CAT->getElementType());
2235 unsigned ElementIndex = 0;
2236 for (const Expr *Init : Inits) {
2237 if (const auto *EmbedS =
2238 dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
2239 PrimType TargetT = classifyPrim(Init->getType());
2240
2241 auto Eval = [&](const IntegerLiteral *IL, unsigned ElemIndex) {
2242 if (TargetT == PT_Float) {
2243 if (!this->emitConst(IL->getValue(), classifyPrim(IL), Init))
2244 return false;
2245 const auto *Sem = &Ctx.getFloatSemantics(CAT->getElementType());
2246 if (!this->emitCastIntegralFloating(classifyPrim(IL), Sem,
2247 getFPOptions(E), E))
2248 return false;
2249 } else {
2250 if (!this->emitConst(IL->getValue(), TargetT, Init))
2251 return false;
2252 }
2253 return this->emitInitElem(TargetT, ElemIndex, IL);
2254 };
2255 if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
2256 return false;
2257 } else {
2258 if (!this->visitArrayElemInit(ElementIndex, Init, InitT))
2259 return false;
2260 ++ElementIndex;
2261 }
2262 }
2263
2264 // Expand the filler expression.
2265 // FIXME: This should go away.
2266 if (ArrayFiller) {
2267 for (; ElementIndex != NumElems; ++ElementIndex) {
2268 if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
2269 return false;
2270 }
2271 }
2272
2273 return this->emitFinishInit(E);
2274 }
2275
2276 if (const auto *ComplexTy = QT->getAs<ComplexType>()) {
2277 unsigned NumInits = Inits.size();
2278
2279 if (NumInits == 1)
2280 return this->delegate(Inits[0]);
2281
2282 QualType ElemQT = ComplexTy->getElementType();
2283 PrimType ElemT = classifyPrim(ElemQT);
2284 if (NumInits == 0) {
2285 // Zero-initialize both elements.
2286 for (unsigned I = 0; I < 2; ++I) {
2287 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2288 return false;
2289 if (!this->emitInitElem(ElemT, I, E))
2290 return false;
2291 }
2292 } else if (NumInits == 2) {
2293 unsigned InitIndex = 0;
2294 for (const Expr *Init : Inits) {
2295 if (!this->visit(Init))
2296 return false;
2297
2298 if (!this->emitInitElem(ElemT, InitIndex, E))
2299 return false;
2300 ++InitIndex;
2301 }
2302 }
2303 return true;
2304 }
2305
2306 if (const auto *VecT = QT->getAs<VectorType>()) {
2307 unsigned NumVecElements = VecT->getNumElements();
2308 assert(NumVecElements >= Inits.size());
2309
2310 QualType ElemQT = VecT->getElementType();
2311 PrimType ElemT = classifyPrim(ElemQT);
2312
2313 // All initializer elements.
2314 unsigned InitIndex = 0;
2315 for (const Expr *Init : Inits) {
2316 if (!this->visit(Init))
2317 return false;
2318
2319 // If the initializer is of vector type itself, we have to deconstruct
2320 // that and initialize all the target fields from the initializer fields.
2321 if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) {
2322 if (!this->emitCopyArray(ElemT, 0, InitIndex,
2323 InitVecT->getNumElements(), E))
2324 return false;
2325 InitIndex += InitVecT->getNumElements();
2326 } else {
2327 if (!this->emitInitElem(ElemT, InitIndex, E))
2328 return false;
2329 ++InitIndex;
2330 }
2331 }
2332
2333 assert(InitIndex <= NumVecElements);
2334
2335 // Fill the rest with zeroes.
2336 for (; InitIndex != NumVecElements; ++InitIndex) {
2337 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2338 return false;
2339 if (!this->emitInitElem(ElemT, InitIndex, E))
2340 return false;
2341 }
2342 return true;
2343 }
2344
2345 if (const auto *MT = QT->getAs<ConstantMatrixType>()) {
2346 unsigned NumElems = MT->getNumElementsFlattened();
2347 assert(Inits.size() == NumElems);
2348
2349 QualType ElemQT = MT->getElementType();
2350 PrimType ElemT = classifyPrim(ElemQT);
2351
2352 // Matrix initializer list elements are in row-major order, which matches
2353 // the matrix APValue convention and therefore no index remapping is
2354 // required.
2355 for (unsigned I = 0; I != NumElems; ++I) {
2356 if (!this->visit(Inits[I]))
2357 return false;
2358 if (!this->emitInitElem(ElemT, I, E))
2359 return false;
2360 }
2361 return true;
2362 }
2363
2364 return false;
2365}
2366
2367/// Pointer to the array(not the element!) must be on the stack when calling
2368/// this.
2369template <class Emitter>
2370bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
2371 OptPrimType InitT) {
2372 if (InitT) {
2373 // Visit the primitive element like normal.
2374 if (!this->visit(Init))
2375 return false;
2376 return this->emitInitElem(*InitT, ElemIndex, Init);
2377 }
2378
2379 InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
2380 // Advance the pointer currently on the stack to the given
2381 // dimension.
2382 if (!this->emitConstUint32(ElemIndex, Init))
2383 return false;
2384 if (!this->emitArrayElemPtrUint32(Init))
2385 return false;
2386 if (!this->visitInitializer(Init))
2387 return false;
2388 return this->emitFinishInitPop(Init);
2389}
2390
2391template <class Emitter>
2393 const FunctionDecl *FuncDecl,
2394 bool Activate, bool IsOperatorCall) {
2395 assert(VarScope->getKind() == ScopeKind::Call);
2396 llvm::BitVector NonNullArgs;
2397 if (FuncDecl && FuncDecl->hasAttr<NonNullAttr>())
2398 NonNullArgs = collectNonNullArgs(FuncDecl, Args);
2399
2400 bool ExplicitMemberFn = false;
2401 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl))
2402 ExplicitMemberFn = MD->isExplicitObjectMemberFunction();
2403
2404 unsigned ArgIndex = 0;
2405 for (const Expr *Arg : Args) {
2406 if (canClassify(Arg)) {
2407 if (!this->visit(Arg))
2408 return false;
2409 } else {
2410
2411 DeclTy Source = Arg;
2412 if (FuncDecl) {
2413 // Try to use the parameter declaration instead of the argument
2414 // expression as a source.
2415 unsigned DeclIndex = ArgIndex - IsOperatorCall + ExplicitMemberFn;
2416 if (DeclIndex < FuncDecl->getNumParams())
2417 Source = FuncDecl->getParamDecl(ArgIndex - IsOperatorCall +
2418 ExplicitMemberFn);
2419 }
2420
2421 UnsignedOrNone LocalIndex =
2422 allocateLocal(std::move(Source), Arg->getType(), ScopeKind::Call);
2423 if (!LocalIndex)
2424 return false;
2425
2426 if (!this->emitGetPtrLocal(*LocalIndex, Arg))
2427 return false;
2428 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
2429 if (!this->visitInitializer(Arg))
2430 return false;
2431 }
2432
2433 if (ArgIndex == 1 && Activate) {
2434 if (!this->emitActivate(Arg))
2435 return false;
2436 }
2437
2438 if (!NonNullArgs.empty() && NonNullArgs[ArgIndex]) {
2439 PrimType ArgT = classify(Arg).value_or(PT_Ptr);
2440 if (ArgT == PT_Ptr) {
2441 if (!this->emitCheckNonNullArg(ArgT, Arg))
2442 return false;
2443 }
2444 }
2445
2446 ++ArgIndex;
2447 }
2448
2449 return true;
2450}
2451
2452template <class Emitter>
2454 return this->visitInitList(E->inits(), E->getArrayFiller(), E);
2455}
2456
2457template <class Emitter>
2462
2463template <class Emitter>
2468
2469template <class Emitter>
2471 OptPrimType T = classify(E->getType());
2472 if (T && E->hasAPValueResult()) {
2473 // Try to emit the APValue directly, without visiting the subexpr.
2474 // This will only fail if we can't emit the APValue, so won't emit any
2475 // diagnostics or any double values.
2476 if (DiscardResult)
2477 return true;
2478
2479 if (this->visitAPValue(E->getAPValueResult(), *T, E))
2480 return true;
2481 }
2482 return this->delegate(E->getSubExpr());
2483}
2484
2485template <class Emitter>
2487 auto It = E->begin();
2488 return this->visit(*It);
2489}
2490
2492 UnaryExprOrTypeTrait Kind) {
2493 bool AlignOfReturnsPreferred =
2494 ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;
2495
2496 // C++ [expr.alignof]p3:
2497 // When alignof is applied to a reference type, the result is the
2498 // alignment of the referenced type.
2499 if (const auto *Ref = T->getAs<ReferenceType>())
2500 T = Ref->getPointeeType();
2501
2502 if (T.getQualifiers().hasUnaligned())
2503 return CharUnits::One();
2504
2505 // __alignof is defined to return the preferred alignment.
2506 // Before 8, clang returned the preferred alignment for alignof and
2507 // _Alignof as well.
2508 if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
2509 return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T));
2510
2511 return ASTCtx.getTypeAlignInChars(T);
2512}
2513
2514template <class Emitter>
2516 const UnaryExprOrTypeTraitExpr *E) {
2517
2518 UnaryExprOrTypeTrait Kind = E->getKind();
2519 const ASTContext &ASTCtx = Ctx.getASTContext();
2520
2521 if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) {
2523
2524 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
2525 // the result is the size of the referenced type."
2526 if (const auto *Ref = ArgType->getAs<ReferenceType>())
2527 ArgType = Ref->getPointeeType();
2528
2529 CharUnits Size;
2530 if (ArgType->isVoidType() || ArgType->isFunctionType())
2531 Size = CharUnits::One();
2532 else {
2533 if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
2534 return this->emitInvalid(E);
2535
2536 if (Kind == UETT_SizeOf)
2537 Size = ASTCtx.getTypeSizeInChars(ArgType);
2538 else
2540 }
2541
2542 if (DiscardResult)
2543 return true;
2544
2545 return this->emitConst(Size.getQuantity(), E);
2546 }
2547
2548 if (Kind == UETT_CountOf) {
2549 QualType Ty = E->getTypeOfArgument();
2550 assert(Ty->isArrayType());
2551
2552 // We don't need to worry about array element qualifiers, so getting the
2553 // unsafe array type is fine.
2554 if (const auto *CAT =
2555 dyn_cast<ConstantArrayType>(Ty->getAsArrayTypeUnsafe())) {
2556 if (DiscardResult)
2557 return true;
2558 return this->emitConst(CAT->getSize(), E);
2559 }
2560
2561 assert(!Ty->isConstantSizeType());
2562
2563 // If it's a variable-length array type, we need to check whether it is a
2564 // multidimensional array. If so, we need to check the size expression of
2565 // the VLA to see if it's a constant size. If so, we can return that value.
2566 const auto *VAT = ASTCtx.getAsVariableArrayType(Ty);
2567 assert(VAT);
2568 if (VAT->getElementType()->isArrayType()) {
2569 std::optional<APSInt> Res =
2570 VAT->getSizeExpr()
2571 ? VAT->getSizeExpr()->getIntegerConstantExpr(ASTCtx)
2572 : std::nullopt;
2573 if (Res) {
2574 if (DiscardResult)
2575 return true;
2576 return this->emitConst(*Res, E);
2577 }
2578 }
2579 }
2580
2581 if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
2582 CharUnits Size;
2583
2584 if (E->isArgumentType()) {
2586
2587 Size = AlignOfType(ArgType, ASTCtx, Kind);
2588 } else {
2589 // Argument is an expression, not a type.
2590 const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
2591
2592 if (Arg->getType()->isDependentType())
2593 return false;
2594
2595 // The kinds of expressions that we have special-case logic here for
2596 // should be kept up to date with the special checks for those
2597 // expressions in Sema.
2598
2599 // alignof decl is always accepted, even if it doesn't make sense: we
2600 // default to 1 in those cases.
2601 if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg))
2602 Size = ASTCtx.getDeclAlign(DRE->getDecl(),
2603 /*RefAsPointee*/ true);
2604 else if (const auto *ME = dyn_cast<MemberExpr>(Arg))
2605 Size = ASTCtx.getDeclAlign(ME->getMemberDecl(),
2606 /*RefAsPointee*/ true);
2607 else
2608 Size = AlignOfType(Arg->getType(), ASTCtx, Kind);
2609 }
2610
2611 if (DiscardResult)
2612 return true;
2613
2614 return this->emitConst(Size.getQuantity(), E);
2615 }
2616
2617 if (Kind == UETT_VectorElements) {
2618 if (E->containsErrors())
2619 return false;
2620
2621 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
2622 return this->emitConst(VT->getNumElements(), E);
2624 return this->emitSizelessVectorElementSize(E);
2625 }
2626
2627 if (Kind == UETT_VecStep) {
2628 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) {
2629 unsigned N = VT->getNumElements();
2630
2631 // The vec_step built-in functions that take a 3-component
2632 // vector return 4. (OpenCL 1.1 spec 6.11.12)
2633 if (N == 3)
2634 N = 4;
2635
2636 return this->emitConst(N, E);
2637 }
2638 return this->emitConst(1, E);
2639 }
2640
2641 if (Kind == UETT_OpenMPRequiredSimdAlign) {
2642 assert(E->isArgumentType());
2643 unsigned Bits = ASTCtx.getOpenMPDefaultSimdAlign(E->getArgumentType());
2644
2645 return this->emitConst(ASTCtx.toCharUnitsFromBits(Bits).getQuantity(), E);
2646 }
2647
2648 if (Kind == UETT_PtrAuthTypeDiscriminator) {
2649 if (E->getArgumentType()->isDependentType())
2650 return this->emitInvalid(E);
2651
2652 return this->emitConst(
2653 const_cast<ASTContext &>(ASTCtx).getPointerAuthTypeDiscriminator(
2654 E->getArgumentType()),
2655 E);
2656 }
2657
2658 return false;
2659}
2660
2661template <class Emitter>
2663 // 'Base.Member'
2664 const Expr *Base = E->getBase();
2665 const ValueDecl *Member = E->getMemberDecl();
2666
2667 if (DiscardResult)
2668 return this->discard(Base);
2669
2670 // MemberExprs are almost always lvalues, in which case we don't need to
2671 // do the load. But sometimes they aren't.
2672 const auto maybeLoadValue = [&]() -> bool {
2673 if (E->isGLValue())
2674 return true;
2675 if (OptPrimType T = classify(E))
2676 return this->emitLoadPop(*T, E);
2677 return false;
2678 };
2679
2680 if (const auto *VD = dyn_cast<VarDecl>(Member)) {
2681 // I am almost confident in saying that a var decl must be static
2682 // and therefore registered as a global variable. But this will probably
2683 // turn out to be wrong some time in the future, as always.
2684 if (auto GlobalIndex = P.getGlobal(VD))
2685 return this->emitGetPtrGlobal(*GlobalIndex, E) && maybeLoadValue();
2686 return false;
2687 }
2688
2689 if (!isa<FieldDecl>(Member)) {
2690 if (!this->discard(Base) && !this->emitSideEffect(E))
2691 return false;
2692
2693 return this->visitDeclRef(Member, E);
2694 }
2695
2696 if (!this->visit(Base))
2697 return false;
2698
2699 // Base above gives us a pointer on the stack.
2700 const auto *FD = cast<FieldDecl>(Member);
2701 const RecordDecl *RD = FD->getParent();
2702 const Record *R = getRecord(RD);
2703 if (!R)
2704 return false;
2705 const Record::Field *F = R->getField(FD);
2706 // Leave a pointer to the field on the stack.
2707 if (F->Decl->getType()->isReferenceType())
2708 return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue();
2709 return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue();
2710}
2711
2712template <class Emitter>
2714 assert(!DiscardResult);
2715 // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
2716 // stand-alone, e.g. via EvaluateAsInt().
2717 if (!ArrayIndex)
2718 return false;
2719 return this->emitConst(*ArrayIndex, E);
2720}
2721
2722template <class Emitter>
2724 assert(Initializing);
2725 assert(!DiscardResult);
2726
2727 // We visit the common opaque expression here once so we have its value
2728 // cached.
2729 if (!this->discard(E->getCommonExpr()))
2730 return false;
2731
2732 // TODO: This compiles to quite a lot of bytecode if the array is larger.
2733 // Investigate compiling this to a loop.
2734 const Expr *SubExpr = E->getSubExpr();
2735 size_t Size = E->getArraySize().getZExtValue();
2736 OptPrimType SubExprT = classify(SubExpr);
2737
2738 // So, every iteration, we execute an assignment here
2739 // where the LHS is on the stack (the target array)
2740 // and the RHS is our SubExpr.
2741 for (size_t I = 0; I != Size; ++I) {
2742 ArrayIndexScope<Emitter> IndexScope(this, I);
2744
2745 if (!this->visitArrayElemInit(I, SubExpr, SubExprT))
2746 return false;
2747 if (!BS.destroyLocals())
2748 return false;
2749 }
2750 return true;
2751}
2752
2753template <class Emitter>
2755 const Expr *SourceExpr = E->getSourceExpr();
2756 if (!SourceExpr)
2757 return false;
2758
2759 if (Initializing) {
2760 assert(!DiscardResult);
2761 return this->visitInitializer(SourceExpr);
2762 }
2763
2764 PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr);
2765 if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end()) {
2766 if (DiscardResult)
2767 return true;
2768 return this->emitGetLocal(SubExprT, It->second, E);
2769 }
2770
2771 if (!this->visit(SourceExpr))
2772 return false;
2773
2774 // At this point we either have the evaluated source expression or a pointer
2775 // to an object on the stack. We want to create a local variable that stores
2776 // this value.
2777 unsigned LocalIndex = allocateLocalPrimitive(E, SubExprT, /*IsConst=*/true);
2778 if (!this->emitSetLocal(SubExprT, LocalIndex, E))
2779 return false;
2780
2781 // This is cleaned up when the local variable is destroyed.
2782 OpaqueExprs.insert({E, LocalIndex});
2783
2784 // Here the local variable is created but the value is removed from the stack,
2785 // so we put it back if the caller needs it.
2786 if (!DiscardResult)
2787 return this->emitGetLocal(SubExprT, LocalIndex, E);
2788 return true;
2789}
2790
2791template <class Emitter>
2793 const AbstractConditionalOperator *E) {
2794 const Expr *Condition = E->getCond();
2795 const Expr *TrueExpr = E->getTrueExpr();
2796 const Expr *FalseExpr = E->getFalseExpr();
2797
2798 if (std::optional<bool> BoolValue = getBoolValue(Condition)) {
2799 if (*BoolValue)
2800 return this->delegate(TrueExpr);
2801 return this->delegate(FalseExpr);
2802 }
2803
2804 // Force-init the scope, which creates a InitScope op. This is necessary so
2805 // the scope is not only initialized in one arm of the conditional operator.
2806 this->VarScope->forceInit();
2807 // The TrueExpr and FalseExpr of a conditional operator do _not_ create a
2808 // scope, which means the local variables created within them unconditionally
2809 // always exist. However, we need to later differentiate which branch was
2810 // taken and only destroy the varibles of the active branch. This is what the
2811 // "enabled" flags on local variables are used for.
2812 llvm::SaveAndRestore LAAA(this->VarScope->LocalsAlwaysEnabled,
2813 /*NewValue=*/false);
2814 bool IsBcpCall = false;
2815 if (const auto *CE = dyn_cast<CallExpr>(Condition->IgnoreParenCasts());
2816 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) {
2817 IsBcpCall = true;
2818 }
2819
2820 LabelTy LabelEnd = this->getLabel(); // Label after the operator.
2821 LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
2822
2823 if (IsBcpCall) {
2824 if (!this->emitPushIgnoreDiags(E))
2825 return false;
2826 }
2827
2828 if (!this->visitBool(Condition)) {
2829 // If the condition failed and we're checking for undefined behavior
2830 // (which only happens with EvalEmitter) check the TrueExpr and FalseExpr
2831 // as well.
2832 if (this->checkingForUndefinedBehavior()) {
2833 if (!this->discard(TrueExpr))
2834 return false;
2835 if (!this->discard(FalseExpr))
2836 return false;
2837 }
2838 return false;
2839 }
2840
2841 if (!this->jumpFalse(LabelFalse, E))
2842 return false;
2843 if (!this->delegate(TrueExpr))
2844 return false;
2845
2846 if (!this->jump(LabelEnd, E))
2847 return false;
2848 this->emitLabel(LabelFalse);
2849 if (!this->delegate(FalseExpr))
2850 return false;
2851
2852 this->fallthrough(LabelEnd);
2853 this->emitLabel(LabelEnd);
2854
2855 if (IsBcpCall)
2856 return this->emitPopIgnoreDiags(E);
2857 return true;
2858}
2859
2860template <class Emitter>
2862 if (DiscardResult)
2863 return true;
2864
2865 if (!Initializing) {
2866 unsigned StringIndex = P.createGlobalString(E);
2867 return this->emitGetPtrGlobal(StringIndex, E);
2868 }
2869
2870 // We are initializing an array on the stack.
2871 const ConstantArrayType *CAT =
2872 Ctx.getASTContext().getAsConstantArrayType(E->getType());
2873 assert(CAT && "a string literal that's not a constant array?");
2874
2875 // If the initializer string is too long, a diagnostic has already been
2876 // emitted. Read only the array length from the string literal.
2877 unsigned ArraySize = CAT->getZExtSize();
2878 unsigned N = std::min(ArraySize, E->getLength());
2879 unsigned CharWidth = E->getCharByteWidth();
2880
2881 for (unsigned I = 0; I != N; ++I) {
2882 uint32_t CodeUnit = E->getCodeUnit(I);
2883
2884 if (CharWidth == 1) {
2885 this->emitConstSint8(CodeUnit, E);
2886 this->emitInitElemSint8(I, E);
2887 } else if (CharWidth == 2) {
2888 this->emitConstUint16(CodeUnit, E);
2889 this->emitInitElemUint16(I, E);
2890 } else if (CharWidth == 4) {
2891 this->emitConstUint32(CodeUnit, E);
2892 this->emitInitElemUint32(I, E);
2893 } else {
2894 llvm_unreachable("unsupported character width");
2895 }
2896 }
2897
2898 // Fill up the rest of the char array with NUL bytes.
2899 for (unsigned I = N; I != ArraySize; ++I) {
2900 if (CharWidth == 1) {
2901 this->emitConstSint8(0, E);
2902 this->emitInitElemSint8(I, E);
2903 } else if (CharWidth == 2) {
2904 this->emitConstUint16(0, E);
2905 this->emitInitElemUint16(I, E);
2906 } else if (CharWidth == 4) {
2907 this->emitConstUint32(0, E);
2908 this->emitInitElemUint32(I, E);
2909 } else {
2910 llvm_unreachable("unsupported character width");
2911 }
2912 }
2913
2914 return true;
2915}
2916
2917template <class Emitter>
2919 if (DiscardResult)
2920 return true;
2921 return this->emitDummyPtr(E, E);
2922}
2923
2924template <class Emitter>
2926 auto &A = Ctx.getASTContext();
2927 std::string Str;
2928 A.getObjCEncodingForType(E->getEncodedType(), Str);
2929 StringLiteral *SL =
2931 /*Pascal=*/false, E->getType(), E->getAtLoc());
2932 return this->delegate(SL);
2933}
2934
2935template <class Emitter>
2937 const SYCLUniqueStableNameExpr *E) {
2938 if (DiscardResult)
2939 return true;
2940
2941 assert(!Initializing);
2942
2943 auto &A = Ctx.getASTContext();
2944 std::string ResultStr = E->ComputeName(A);
2945
2946 QualType CharTy = A.CharTy.withConst();
2947 APInt Size(A.getTypeSize(A.getSizeType()), ResultStr.size() + 1);
2948 QualType ArrayTy = A.getConstantArrayType(CharTy, Size, nullptr,
2950
2951 StringLiteral *SL =
2953 /*Pascal=*/false, ArrayTy, E->getLocation());
2954
2955 unsigned StringIndex = P.createGlobalString(SL);
2956 return this->emitGetPtrGlobal(StringIndex, E);
2957}
2958
2959template <class Emitter>
2961 if (DiscardResult)
2962 return true;
2963 return this->emitConst(E->getValue(), E);
2964}
2965
2966template <class Emitter>
2968 const CompoundAssignOperator *E) {
2969
2970 const Expr *LHS = E->getLHS();
2971 const Expr *RHS = E->getRHS();
2972 QualType LHSType = LHS->getType();
2973 QualType LHSComputationType = E->getComputationLHSType();
2974 QualType ResultType = E->getComputationResultType();
2975 OptPrimType LT = classify(LHSComputationType);
2976 OptPrimType RT = classify(ResultType);
2977
2978 assert(ResultType->isFloatingType());
2979
2980 if (!LT || !RT)
2981 return false;
2982
2983 PrimType LHST = classifyPrim(LHSType);
2984
2985 // C++17 onwards require that we evaluate the RHS first.
2986 // Compute RHS and save it in a temporary variable so we can
2987 // load it again later.
2988 if (!visit(RHS))
2989 return false;
2990
2991 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2992 if (!this->emitSetLocal(*RT, TempOffset, E))
2993 return false;
2994
2995 // First, visit LHS.
2996 if (!visit(LHS))
2997 return false;
2998 if (!this->emitLoad(LHST, E))
2999 return false;
3000
3001 // If necessary, convert LHS to its computation type.
3002 if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
3003 LHSComputationType, E))
3004 return false;
3005
3006 // Now load RHS.
3007 if (!this->emitGetLocal(*RT, TempOffset, E))
3008 return false;
3009
3010 switch (E->getOpcode()) {
3011 case BO_AddAssign:
3012 if (!this->emitAddf(getFPOptions(E), E))
3013 return false;
3014 break;
3015 case BO_SubAssign:
3016 if (!this->emitSubf(getFPOptions(E), E))
3017 return false;
3018 break;
3019 case BO_MulAssign:
3020 if (!this->emitMulf(getFPOptions(E), E))
3021 return false;
3022 break;
3023 case BO_DivAssign:
3024 if (!this->emitDivf(getFPOptions(E), E))
3025 return false;
3026 break;
3027 default:
3028 return false;
3029 }
3030
3031 if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
3032 return false;
3033
3034 if (DiscardResult)
3035 return this->emitStorePop(LHST, E);
3036 return this->emitStore(LHST, E);
3037}
3038
3039template <class Emitter>
3041 const CompoundAssignOperator *E) {
3042 BinaryOperatorKind Op = E->getOpcode();
3043 const Expr *LHS = E->getLHS();
3044 const Expr *RHS = E->getRHS();
3045 OptPrimType LT = classify(LHS->getType());
3046 OptPrimType RT = classify(RHS->getType());
3047
3048 if (Op != BO_AddAssign && Op != BO_SubAssign)
3049 return false;
3050
3051 if (!LT || !RT)
3052 return false;
3053
3054 if (!visit(LHS))
3055 return false;
3056
3057 if (!this->emitLoad(*LT, LHS))
3058 return false;
3059
3060 if (!visit(RHS))
3061 return false;
3062
3063 if (Op == BO_AddAssign) {
3064 if (!this->emitAddOffset(*RT, E))
3065 return false;
3066 } else {
3067 if (!this->emitSubOffset(*RT, E))
3068 return false;
3069 }
3070
3071 if (DiscardResult)
3072 return this->emitStorePopPtr(E);
3073 return this->emitStorePtr(E);
3074}
3075
3076template <class Emitter>
3078 const CompoundAssignOperator *E) {
3079 if (E->getType()->isVectorType())
3080 return VisitVectorBinOp(E);
3081
3082 const Expr *LHS = E->getLHS();
3083 const Expr *RHS = E->getRHS();
3084 OptPrimType LHSComputationT = classify(E->getComputationLHSType());
3085 OptPrimType LT = classify(LHS->getType());
3086 OptPrimType RT = classify(RHS->getType());
3087 OptPrimType ResultT = classify(E->getType());
3088
3089 if (!Ctx.getLangOpts().CPlusPlus14)
3090 return this->visit(RHS) && this->visit(LHS) && this->emitError(E);
3091
3092 if (!LT || !RT || !ResultT || !LHSComputationT)
3093 return false;
3094
3095 // Handle floating point operations separately here, since they
3096 // require special care.
3097
3098 if (ResultT == PT_Float || RT == PT_Float)
3100
3101 if (E->getType()->isPointerType())
3103
3104 assert(!E->getType()->isPointerType() && "Handled above");
3105 assert(!E->getType()->isFloatingType() && "Handled above");
3106
3107 // C++17 onwards require that we evaluate the RHS first.
3108 // Compute RHS and save it in a temporary variable so we can
3109 // load it again later.
3110 // FIXME: Compound assignments are unsequenced in C, so we might
3111 // have to figure out how to reject them.
3112 if (!visit(RHS))
3113 return false;
3114
3115 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
3116
3117 if (!this->emitSetLocal(*RT, TempOffset, E))
3118 return false;
3119
3120 // Get LHS pointer, load its value and cast it to the
3121 // computation type if necessary.
3122 if (!visit(LHS))
3123 return false;
3124 if (!this->emitLoad(*LT, E))
3125 return false;
3126 if (LT != LHSComputationT &&
3127 !this->emitIntegralCast(*LT, *LHSComputationT, E->getComputationLHSType(),
3128 E))
3129 return false;
3130
3131 // Get the RHS value on the stack.
3132 if (!this->emitGetLocal(*RT, TempOffset, E))
3133 return false;
3134
3135 // Perform operation.
3136 switch (E->getOpcode()) {
3137 case BO_AddAssign:
3138 if (!this->emitAdd(*LHSComputationT, E))
3139 return false;
3140 break;
3141 case BO_SubAssign:
3142 if (!this->emitSub(*LHSComputationT, E))
3143 return false;
3144 break;
3145 case BO_MulAssign:
3146 if (!this->emitMul(*LHSComputationT, E))
3147 return false;
3148 break;
3149 case BO_DivAssign:
3150 if (!this->emitDiv(*LHSComputationT, E))
3151 return false;
3152 break;
3153 case BO_RemAssign:
3154 if (!this->emitRem(*LHSComputationT, E))
3155 return false;
3156 break;
3157 case BO_ShlAssign:
3158 if (!this->emitShl(*LHSComputationT, *RT, E))
3159 return false;
3160 break;
3161 case BO_ShrAssign:
3162 if (!this->emitShr(*LHSComputationT, *RT, E))
3163 return false;
3164 break;
3165 case BO_AndAssign:
3166 if (!this->emitBitAnd(*LHSComputationT, E))
3167 return false;
3168 break;
3169 case BO_XorAssign:
3170 if (!this->emitBitXor(*LHSComputationT, E))
3171 return false;
3172 break;
3173 case BO_OrAssign:
3174 if (!this->emitBitOr(*LHSComputationT, E))
3175 return false;
3176 break;
3177 default:
3178 llvm_unreachable("Unimplemented compound assign operator");
3179 }
3180
3181 // And now cast from LHSComputationT to ResultT.
3182 if (ResultT != LHSComputationT &&
3183 !this->emitIntegralCast(*LHSComputationT, *ResultT, E->getType(), E))
3184 return false;
3185
3186 // And store the result in LHS.
3187 if (DiscardResult) {
3188 if (LHS->refersToBitField())
3189 return this->emitStoreBitFieldPop(*ResultT, E);
3190 return this->emitStorePop(*ResultT, E);
3191 }
3192 if (LHS->refersToBitField())
3193 return this->emitStoreBitField(*ResultT, E);
3194 return this->emitStore(*ResultT, E);
3195}
3196
3197template <class Emitter>
3200 const Expr *SubExpr = E->getSubExpr();
3201
3202 return this->delegate(SubExpr) && ES.destroyLocals(E);
3203}
3204
3205template <class Emitter>
3207 const MaterializeTemporaryExpr *E) {
3208 if (Initializing) {
3209 // We already have a value, just initialize that.
3210 return this->delegate(E->getSubExpr());
3211 }
3212 // If we don't end up using the materialized temporary anyway, don't
3213 // bother creating it.
3214 if (DiscardResult)
3215 return this->discard(E->getSubExpr());
3216
3219 const Expr *Inner;
3220 if (!Ctx.getLangOpts().CPlusPlus11)
3221 Inner =
3222 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
3223 else
3224 Inner = E->getSubExpr();
3225
3226 // If we passed any comma operators, evaluate their LHSs.
3227 for (const Expr *LHS : CommaLHSs) {
3228 if (!this->discard(LHS))
3229 return false;
3230 }
3231
3232 // FIXME: Find a test case where Adjustments matters.
3233
3234 // When we're initializing a global variable *or* the storage duration of
3235 // the temporary is explicitly static, create a global variable.
3236 OptPrimType InnerT = classify(Inner);
3237 if (E->getStorageDuration() == SD_Static) {
3238 UnsignedOrNone GlobalIndex = P.createGlobal(E, Inner->getType());
3239 if (!GlobalIndex)
3240 return false;
3241
3242 const LifetimeExtendedTemporaryDecl *TempDecl =
3244 assert(TempDecl);
3245
3246 if (InnerT) {
3247 if (!this->visit(Inner))
3248 return false;
3249 if (!this->emitInitGlobalTemp(*InnerT, *GlobalIndex, TempDecl, E))
3250 return false;
3251 return this->emitGetPtrGlobal(*GlobalIndex, E);
3252 }
3253
3254 if (!this->checkLiteralType(Inner))
3255 return false;
3256 // Non-primitive values.
3257 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3258 return false;
3259 if (!this->visitInitializer(Inner))
3260 return false;
3261 return this->emitInitGlobalTempComp(TempDecl, E);
3262 }
3263
3267
3268 // For everyhing else, use local variables.
3269 if (InnerT) {
3270 bool IsConst = Inner->getType().isConstQualified();
3271 bool IsVolatile = Inner->getType().isVolatileQualified();
3272 unsigned LocalIndex =
3273 allocateLocalPrimitive(E, *InnerT, IsConst, IsVolatile, VarScope);
3274 if (!this->VarScope->LocalsAlwaysEnabled &&
3275 !this->emitEnableLocal(LocalIndex, E))
3276 return false;
3277
3278 if (!this->visit(Inner))
3279 return false;
3280 if (!this->emitSetLocal(*InnerT, LocalIndex, E))
3281 return false;
3282
3283 return this->emitGetPtrLocal(LocalIndex, E);
3284 }
3285
3286 if (!this->checkLiteralType(Inner))
3287 return false;
3288
3289 if (UnsignedOrNone LocalIndex =
3290 allocateLocal(E, Inner->getType(), VarScope)) {
3291 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
3292
3293 if (!this->VarScope->LocalsAlwaysEnabled &&
3294 !this->emitEnableLocal(*LocalIndex, E))
3295 return false;
3296
3297 if (!this->emitGetPtrLocal(*LocalIndex, E))
3298 return false;
3299 return this->visitInitializer(Inner) && this->emitFinishInit(E);
3300 }
3301 return false;
3302}
3303
3304template <class Emitter>
3306 const CXXBindTemporaryExpr *E) {
3307 const Expr *SubExpr = E->getSubExpr();
3308
3309 if (Initializing)
3310 return this->delegate(SubExpr);
3311
3312 // Make sure we create a temporary even if we're discarding, since that will
3313 // make sure we will also call the destructor.
3314
3315 if (!this->visit(SubExpr))
3316 return false;
3317
3318 if (DiscardResult)
3319 return this->emitPopPtr(E);
3320 return true;
3321}
3322
3323template <class Emitter>
3325 const Expr *Init = E->getInitializer();
3326 if (DiscardResult)
3327 return this->discard(Init);
3328
3329 if (Initializing) {
3330 // We already have a value, just initialize that.
3331 return this->visitInitializer(Init) && this->emitFinishInit(E);
3332 }
3333
3334 OptPrimType T = classify(E->getType());
3335 if (E->isFileScope()) {
3336 // Avoid creating a variable if this is a primitive RValue anyway.
3337 if (T && !E->isLValue())
3338 return this->delegate(Init);
3339
3340 UnsignedOrNone GlobalIndex = P.createGlobal(E, E->getType());
3341 if (!GlobalIndex)
3342 return false;
3343
3344 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3345 return false;
3346
3347 // Since this is a global variable, we might've already seen,
3348 // don't do it again.
3349 if (P.isGlobalInitialized(*GlobalIndex))
3350 return true;
3351
3352 if (T) {
3353 if (!this->visit(Init))
3354 return false;
3355 return this->emitInitGlobal(*T, *GlobalIndex, E);
3356 }
3357
3358 return this->visitInitializer(Init) && this->emitFinishInit(E);
3359 }
3360
3361 // Otherwise, use a local variable.
3362 if (T && !E->isLValue()) {
3363 // For primitive types, we just visit the initializer.
3364 return this->delegate(Init);
3365 }
3366
3367 unsigned LocalIndex;
3368 if (T)
3369 LocalIndex = this->allocateLocalPrimitive(Init, *T, /*IsConst=*/false);
3370 else if (UnsignedOrNone MaybeIndex = this->allocateLocal(Init))
3371 LocalIndex = *MaybeIndex;
3372 else
3373 return false;
3374
3375 if (!this->emitGetPtrLocal(LocalIndex, E))
3376 return false;
3377
3378 if (T)
3379 return this->visit(Init) && this->emitInit(*T, E);
3380 return this->visitInitializer(Init) && this->emitFinishInit(E);
3381}
3382
3383template <class Emitter>
3385 if (DiscardResult)
3386 return true;
3387 if (E->isStoredAsBoolean()) {
3388 if (E->getType()->isBooleanType())
3389 return this->emitConstBool(E->getBoolValue(), E);
3390 return this->emitConst(E->getBoolValue(), E);
3391 }
3392 PrimType T = classifyPrim(E->getType());
3393 return this->visitAPValue(E->getAPValue(), T, E);
3394}
3395
3396template <class Emitter>
3398 if (DiscardResult)
3399 return true;
3400 return this->emitConst(E->getValue(), E);
3401}
3402
3403template <class Emitter>
3405 if (DiscardResult)
3406 return true;
3407
3408 assert(Initializing);
3409 const Record *R = P.getOrCreateRecord(E->getLambdaClass());
3410 if (!R)
3411 return false;
3412
3413 auto *CaptureInitIt = E->capture_init_begin();
3414 // Initialize all fields (which represent lambda captures) of the
3415 // record with their initializers.
3416 for (const Record::Field &F : R->fields()) {
3417 const Expr *Init = *CaptureInitIt;
3418 if (!Init || Init->containsErrors())
3419 continue;
3420 ++CaptureInitIt;
3421
3422 if (OptPrimType T = classify(Init)) {
3423 if (!this->visit(Init))
3424 return false;
3425
3426 if (!this->emitInitField(*T, F.Offset, E))
3427 return false;
3428 } else {
3429 if (!this->emitGetPtrField(F.Offset, E))
3430 return false;
3431
3432 if (!this->visitInitializer(Init))
3433 return false;
3434
3435 if (!this->emitPopPtr(E))
3436 return false;
3437 }
3438 }
3439
3440 return true;
3441}
3442
3443template <class Emitter>
3445 if (DiscardResult)
3446 return true;
3447
3448 if (!Initializing) {
3449 unsigned StringIndex = P.createGlobalString(E->getFunctionName(), E);
3450 return this->emitGetPtrGlobal(StringIndex, E);
3451 }
3452
3453 return this->delegate(E->getFunctionName());
3454}
3455
3456template <class Emitter>
3458 if (E->getSubExpr() && !this->discard(E->getSubExpr()))
3459 return false;
3460
3461 return this->emitInvalid(E);
3462}
3463
3464template <class Emitter>
3466 const CXXReinterpretCastExpr *E) {
3467 const Expr *SubExpr = E->getSubExpr();
3468
3469 OptPrimType FromT = classify(SubExpr);
3470 OptPrimType ToT = classify(E);
3471
3472 if (!FromT || !ToT)
3473 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
3474
3475 if (FromT == PT_Ptr || ToT == PT_Ptr) {
3476 // Both types could be PT_Ptr because their expressions are glvalues.
3477 OptPrimType PointeeFromT;
3478 if (SubExpr->getType()->isPointerOrReferenceType())
3479 PointeeFromT = classify(SubExpr->getType()->getPointeeType());
3480 else
3481 PointeeFromT = classify(SubExpr->getType());
3482
3483 OptPrimType PointeeToT;
3485 PointeeToT = classify(E->getType()->getPointeeType());
3486 else
3487 PointeeToT = classify(E->getType());
3488
3489 bool Fatal = true;
3490 if (PointeeToT && PointeeFromT) {
3491 if (isIntegerOrBoolType(*PointeeFromT) &&
3492 isIntegerOrBoolType(*PointeeToT))
3493 Fatal = false;
3494 else if (E->getCastKind() == CK_LValueBitCast)
3495 Fatal = false;
3496 } else {
3497 Fatal = SubExpr->getType().getTypePtr() != E->getType().getTypePtr();
3498 }
3499
3500 if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
3501 return false;
3502
3503 if (E->getCastKind() == CK_LValueBitCast)
3504 return this->delegate(SubExpr);
3505 return this->VisitCastExpr(E);
3506 }
3507
3508 // Try to actually do the cast.
3509 bool Fatal = (ToT != FromT);
3510 if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
3511 return false;
3512
3513 return this->VisitCastExpr(E);
3514}
3515
3516template <class Emitter>
3518
3519 if (!Ctx.getLangOpts().CPlusPlus20) {
3520 if (!this->emitInvalidCast(CastKind::Dynamic, /*Fatal=*/false, E))
3521 return false;
3522 }
3523
3524 return this->VisitCastExpr(E);
3525}
3526
3527template <class Emitter>
3529 assert(E->getType()->isBooleanType());
3530
3531 if (DiscardResult)
3532 return true;
3533 return this->emitConstBool(E->getValue(), E);
3534}
3535
3536template <class Emitter>
3538 QualType T = E->getType();
3539 assert(!canClassify(T));
3540
3541 if (T->isRecordType()) {
3542 const CXXConstructorDecl *Ctor = E->getConstructor();
3543
3544 // If we're discarding a construct expression, we still need
3545 // to allocate a variable and call the constructor and destructor.
3546 if (DiscardResult) {
3547 if (Ctor->isTrivial())
3548 return true;
3549 assert(!Initializing);
3550 UnsignedOrNone LocalIndex = allocateLocal(E);
3551
3552 if (!LocalIndex)
3553 return false;
3554
3555 if (!this->emitGetPtrLocal(*LocalIndex, E))
3556 return false;
3557 }
3558
3559 // Trivial copy/move constructor. Avoid copy.
3560 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
3561 Ctor->isTrivial() &&
3562 E->getArg(0)->isTemporaryObject(Ctx.getASTContext(),
3563 T->getAsCXXRecordDecl()))
3564 return this->visitInitializer(E->getArg(0));
3565
3566 // Zero initialization.
3567 bool ZeroInit = E->requiresZeroInitialization();
3568 if (ZeroInit) {
3569 const Record *R = getRecord(E->getType());
3570 if (!R)
3571 return false;
3572
3573 if (!this->visitZeroRecordInitializer(R, E))
3574 return false;
3575
3576 // If the constructor is trivial anyway, we're done.
3577 if (Ctor->isTrivial())
3578 return true;
3579 }
3580
3581 // Avoid materializing a temporary for an elidable copy/move constructor.
3582 if (!ZeroInit && E->isElidable()) {
3583 const Expr *SrcObj = E->getArg(0);
3584 assert(SrcObj->isTemporaryObject(Ctx.getASTContext(), Ctor->getParent()));
3585 assert(Ctx.getASTContext().hasSameUnqualifiedType(E->getType(),
3586 SrcObj->getType()));
3587 if (const auto *ME = dyn_cast<MaterializeTemporaryExpr>(SrcObj)) {
3588 if (!this->emitCheckFunctionDecl(Ctor, E))
3589 return false;
3590 return this->visitInitializer(ME->getSubExpr());
3591 }
3592 }
3593
3594 const Function *Func = getFunction(Ctor);
3595
3596 if (!Func)
3597 return false;
3598
3599 assert(Func->hasThisPointer());
3600 assert(!Func->hasRVO());
3601
3602 // The This pointer is already on the stack because this is an initializer,
3603 // but we need to dup() so the call() below has its own copy.
3604 if (!this->emitDupPtr(E))
3605 return false;
3606
3607 // Constructor arguments.
3608 for (const auto *Arg : E->arguments()) {
3609 if (!this->visit(Arg))
3610 return false;
3611 }
3612
3613 if (Func->isVariadic()) {
3614 uint32_t VarArgSize = 0;
3615 unsigned NumParams = Func->getNumWrittenParams();
3616 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) {
3617 VarArgSize +=
3618 align(primSize(classify(E->getArg(I)->getType()).value_or(PT_Ptr)));
3619 }
3620 if (!this->emitCallVar(Func, VarArgSize, E))
3621 return false;
3622 } else {
3623 if (!this->emitCall(Func, 0, E)) {
3624 // When discarding, we don't need the result anyway, so clean up
3625 // the instance dup we did earlier in case surrounding code wants
3626 // to keep evaluating.
3627 if (DiscardResult)
3628 (void)this->emitPopPtr(E);
3629 return false;
3630 }
3631 }
3632
3633 if (DiscardResult)
3634 return this->emitPopPtr(E);
3635 return this->emitFinishInit(E);
3636 }
3637
3638 if (T->isArrayType()) {
3639 const Function *Func = getFunction(E->getConstructor());
3640 if (!Func)
3641 return false;
3642
3643 if (!this->emitDupPtr(E))
3644 return false;
3645
3646 std::function<bool(QualType)> initArrayDimension;
3647 initArrayDimension = [&](QualType T) -> bool {
3648 if (!T->isArrayType()) {
3649 // Constructor arguments.
3650 for (const auto *Arg : E->arguments()) {
3651 if (!this->visit(Arg))
3652 return false;
3653 }
3654
3655 return this->emitCall(Func, 0, E);
3656 }
3657
3658 const ConstantArrayType *CAT =
3659 Ctx.getASTContext().getAsConstantArrayType(T);
3660 if (!CAT)
3661 return false;
3662 QualType ElemTy = CAT->getElementType();
3663 unsigned NumElems = CAT->getZExtSize();
3664 for (size_t I = 0; I != NumElems; ++I) {
3665 if (!this->emitConstUint64(I, E))
3666 return false;
3667 if (!this->emitArrayElemPtrUint64(E))
3668 return false;
3669 if (!initArrayDimension(ElemTy))
3670 return false;
3671 }
3672 return this->emitPopPtr(E);
3673 };
3674
3675 return initArrayDimension(E->getType());
3676 }
3677
3678 return false;
3679}
3680
3681template <class Emitter>
3683 if (DiscardResult)
3684 return true;
3685
3686 const APValue Val =
3687 E->EvaluateInContext(Ctx.getASTContext(), SourceLocDefaultExpr);
3688
3689 // Things like __builtin_LINE().
3690 if (E->getType()->isIntegerType()) {
3691 assert(Val.isInt());
3692 const APSInt &I = Val.getInt();
3693 return this->emitConst(I, E);
3694 }
3695 // Otherwise, the APValue is an LValue, with only one element.
3696 // Theoretically, we don't need the APValue at all of course.
3697 assert(E->getType()->isPointerType());
3698 assert(Val.isLValue());
3699 const APValue::LValueBase &Base = Val.getLValueBase();
3700 if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
3701 return this->visit(LValueExpr);
3702
3703 // Otherwise, we have a decl (which is the case for
3704 // __builtin_source_location).
3705 assert(Base.is<const ValueDecl *>());
3706 assert(Val.getLValuePath().size() == 0);
3707 const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
3708 assert(BaseDecl);
3709
3710 auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
3711
3712 UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(UGCD);
3713 if (!GlobalIndex)
3714 return false;
3715
3716 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3717 return false;
3718
3719 const Record *R = getRecord(E->getType());
3720 const APValue &V = UGCD->getValue();
3721 for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
3722 const Record::Field *F = R->getField(I);
3723 const APValue &FieldValue = V.getStructField(I);
3724
3725 PrimType FieldT = classifyPrim(F->Decl->getType());
3726
3727 if (!this->visitAPValue(FieldValue, FieldT, E))
3728 return false;
3729 if (!this->emitInitField(FieldT, F->Offset, E))
3730 return false;
3731 }
3732
3733 // Leave the pointer to the global on the stack.
3734 return true;
3735}
3736
3737template <class Emitter>
3739 unsigned N = E->getNumComponents();
3740 if (N == 0)
3741 return false;
3742
3743 for (unsigned I = 0; I != N; ++I) {
3744 const OffsetOfNode &Node = E->getComponent(I);
3745 if (Node.getKind() == OffsetOfNode::Array) {
3746 const Expr *ArrayIndexExpr = E->getIndexExpr(Node.getArrayExprIndex());
3747 PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
3748
3749 if (DiscardResult) {
3750 if (!this->discard(ArrayIndexExpr))
3751 return false;
3752 continue;
3753 }
3754
3755 if (!this->visit(ArrayIndexExpr))
3756 return false;
3757 // Cast to Sint64.
3758 if (IndexT != PT_Sint64) {
3759 if (!this->emitCast(IndexT, PT_Sint64, E))
3760 return false;
3761 }
3762 }
3763 }
3764
3765 if (DiscardResult)
3766 return true;
3767
3768 PrimType T = classifyPrim(E->getType());
3769 return this->emitOffsetOf(T, E, E);
3770}
3771
3772template <class Emitter>
3774 const CXXScalarValueInitExpr *E) {
3775 QualType Ty = E->getType();
3776
3777 if (DiscardResult || Ty->isVoidType())
3778 return true;
3779
3780 if (OptPrimType T = classify(Ty))
3781 return this->visitZeroInitializer(*T, Ty, E);
3782
3783 if (const auto *CT = Ty->getAs<ComplexType>()) {
3784 if (!Initializing) {
3785 UnsignedOrNone LocalIndex = allocateLocal(E);
3786 if (!LocalIndex)
3787 return false;
3788 if (!this->emitGetPtrLocal(*LocalIndex, E))
3789 return false;
3790 }
3791
3792 // Initialize both fields to 0.
3793 QualType ElemQT = CT->getElementType();
3794 PrimType ElemT = classifyPrim(ElemQT);
3795
3796 for (unsigned I = 0; I != 2; ++I) {
3797 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3798 return false;
3799 if (!this->emitInitElem(ElemT, I, E))
3800 return false;
3801 }
3802 return true;
3803 }
3804
3805 if (const auto *VT = Ty->getAs<VectorType>()) {
3806 // FIXME: Code duplication with the _Complex case above.
3807 if (!Initializing) {
3808 UnsignedOrNone LocalIndex = allocateLocal(E);
3809 if (!LocalIndex)
3810 return false;
3811 if (!this->emitGetPtrLocal(*LocalIndex, E))
3812 return false;
3813 }
3814
3815 // Initialize all fields to 0.
3816 QualType ElemQT = VT->getElementType();
3817 PrimType ElemT = classifyPrim(ElemQT);
3818
3819 for (unsigned I = 0, N = VT->getNumElements(); I != N; ++I) {
3820 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3821 return false;
3822 if (!this->emitInitElem(ElemT, I, E))
3823 return false;
3824 }
3825 return true;
3826 }
3827
3828 return false;
3829}
3830
3831template <class Emitter>
3833 return this->emitConst(E->getPackLength(), E);
3834}
3835
3836template <class Emitter>
3841
3842template <class Emitter>
3844 return this->delegate(E->getChosenSubExpr());
3845}
3846
3847template <class Emitter>
3849 if (DiscardResult)
3850 return true;
3851
3852 return this->emitConst(E->getValue(), E);
3853}
3854
3855template <class Emitter>
3857 const CXXInheritedCtorInitExpr *E) {
3858 const CXXConstructorDecl *Ctor = E->getConstructor();
3859 assert(!Ctor->isTrivial() &&
3860 "Trivial CXXInheritedCtorInitExpr, implement. (possible?)");
3861 const Function *F = this->getFunction(Ctor);
3862 assert(F);
3863 assert(!F->hasRVO());
3864 assert(F->hasThisPointer());
3865
3866 if (!this->emitDupPtr(SourceInfo{}))
3867 return false;
3868
3869 // Forward all arguments of the current function (which should be a
3870 // constructor itself) to the inherited ctor.
3871 // This is necessary because the calling code has pushed the pointer
3872 // of the correct base for us already, but the arguments need
3873 // to come after.
3874 unsigned ParamIndex = 0;
3875 for (const ParmVarDecl *PD : Ctor->parameters()) {
3876 PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr);
3877
3878 if (!this->emitGetParam(PT, ParamIndex, E))
3879 return false;
3880 ++ParamIndex;
3881 }
3882
3883 return this->emitCall(F, 0, E);
3884}
3885
3886// FIXME: This function has become rather unwieldy, especially
3887// the part where we initialize an array allocation of dynamic size.
3888template <class Emitter>
3890 assert(classifyPrim(E->getType()) == PT_Ptr);
3891 const Expr *Init = E->getInitializer();
3892 QualType ElementType = E->getAllocatedType();
3893 OptPrimType ElemT = classify(ElementType);
3894 unsigned PlacementArgs = E->getNumPlacementArgs();
3895 const FunctionDecl *OperatorNew = E->getOperatorNew();
3896 const Expr *PlacementDest = nullptr;
3897 bool IsNoThrow = false;
3898
3899 if (E->containsErrors())
3900 return false;
3901
3902 if (PlacementArgs != 0) {
3903 // FIXME: There is no restriction on this, but it's not clear that any
3904 // other form makes any sense. We get here for cases such as:
3905 //
3906 // new (std::align_val_t{N}) X(int)
3907 //
3908 // (which should presumably be valid only if N is a multiple of
3909 // alignof(int), and in any case can't be deallocated unless N is
3910 // alignof(X) and X has new-extended alignment).
3911 if (PlacementArgs == 1) {
3912 const Expr *Arg1 = E->getPlacementArg(0);
3913 if (Arg1->getType()->isNothrowT()) {
3914 if (!this->discard(Arg1))
3915 return false;
3916 IsNoThrow = true;
3917 } else {
3918 // Invalid unless we have C++26 or are in a std:: function.
3919 if (!this->emitInvalidNewDeleteExpr(E, E))
3920 return false;
3921
3922 // If we have a placement-new destination, we'll later use that instead
3923 // of allocating.
3924 if (OperatorNew->isReservedGlobalPlacementOperator())
3925 PlacementDest = Arg1;
3926 }
3927 } else {
3928 // Always invalid.
3929 return this->emitInvalid(E);
3930 }
3931 } else if (!OperatorNew
3932 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
3933 return this->emitInvalidNewDeleteExpr(E, E);
3934
3935 const Descriptor *Desc;
3936 if (!PlacementDest) {
3937 if (ElemT) {
3938 if (E->isArray())
3939 Desc = nullptr; // We're not going to use it in this case.
3940 else
3941 Desc = P.createDescriptor(E, *ElemT, /*SourceTy=*/nullptr,
3943 } else {
3944 Desc = P.createDescriptor(
3945 E, ElementType.getTypePtr(),
3946 E->isArray() ? std::nullopt : Descriptor::InlineDescMD,
3947 /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false,
3948 /*IsVolatile=*/false, Init);
3949 }
3950 }
3951
3952 if (E->isArray()) {
3953 std::optional<const Expr *> ArraySizeExpr = E->getArraySize();
3954 if (!ArraySizeExpr)
3955 return false;
3956
3957 const Expr *Stripped = *ArraySizeExpr;
3958 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
3959 Stripped = ICE->getSubExpr())
3960 if (ICE->getCastKind() != CK_NoOp &&
3961 ICE->getCastKind() != CK_IntegralCast)
3962 break;
3963
3964 PrimType SizeT = classifyPrim(Stripped->getType());
3965
3966 // Save evaluated array size to a variable.
3967 unsigned ArrayLen =
3968 allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
3969 if (!this->visit(Stripped))
3970 return false;
3971 if (!this->emitSetLocal(SizeT, ArrayLen, E))
3972 return false;
3973
3974 if (PlacementDest) {
3975 if (!this->visit(PlacementDest))
3976 return false;
3977 if (!this->emitGetLocal(SizeT, ArrayLen, E))
3978 return false;
3979 if (!this->emitCheckNewTypeMismatchArray(SizeT, E, E))
3980 return false;
3981 } else {
3982 if (!this->emitGetLocal(SizeT, ArrayLen, E))
3983 return false;
3984
3985 if (ElemT) {
3986 // N primitive elements.
3987 if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E))
3988 return false;
3989 } else {
3990 // N Composite elements.
3991 if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E))
3992 return false;
3993 }
3994 }
3995
3996 if (Init) {
3997 QualType InitType = Init->getType();
3998 size_t StaticInitElems = 0;
3999 const Expr *DynamicInit = nullptr;
4000 OptPrimType ElemT;
4001
4002 if (const ConstantArrayType *CAT =
4003 Ctx.getASTContext().getAsConstantArrayType(InitType)) {
4004 StaticInitElems = CAT->getZExtSize();
4005 // Initialize the first S element from the initializer.
4006 if (!this->visitInitializer(Init))
4007 return false;
4008
4009 if (const auto *ILE = dyn_cast<InitListExpr>(Init)) {
4010 if (ILE->hasArrayFiller())
4011 DynamicInit = ILE->getArrayFiller();
4012 else if (isa<StringLiteral>(ILE->getInit(0)))
4013 ElemT = classifyPrim(CAT->getElementType());
4014 }
4015 }
4016
4017 // The initializer initializes a certain number of elements, S.
4018 // However, the complete number of elements, N, might be larger than that.
4019 // In this case, we need to get an initializer for the remaining elements.
4020 // There are three cases:
4021 // 1) For the form 'new Struct[n];', the initializer is a
4022 // CXXConstructExpr and its type is an IncompleteArrayType.
4023 // 2) For the form 'new Struct[n]{1,2,3}', the initializer is an
4024 // InitListExpr and the initializer for the remaining elements
4025 // is the array filler.
4026 // 3) StringLiterals don't have an array filler, so we need to zero
4027 // the remaining elements.
4028
4029 if (DynamicInit || ElemT || InitType->isIncompleteArrayType()) {
4030 const Function *CtorFunc = nullptr;
4031 if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {
4032 CtorFunc = getFunction(CE->getConstructor());
4033 if (!CtorFunc)
4034 return false;
4035 } else if (!DynamicInit && !ElemT)
4036 DynamicInit = Init;
4037
4038 LabelTy EndLabel = this->getLabel();
4039 LabelTy StartLabel = this->getLabel();
4040
4041 // In the nothrow case, the alloc above might have returned nullptr.
4042 // Don't call any constructors that case.
4043 if (IsNoThrow) {
4044 if (!this->emitDupPtr(E))
4045 return false;
4046 if (!this->emitNullPtr(0, nullptr, E))
4047 return false;
4048 if (!this->emitEQPtr(E))
4049 return false;
4050 if (!this->jumpTrue(EndLabel, E))
4051 return false;
4052 }
4053
4054 // Create loop variables.
4055 unsigned Iter =
4056 allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
4057 if (!this->emitConst(StaticInitElems, SizeT, E))
4058 return false;
4059 if (!this->emitSetLocal(SizeT, Iter, E))
4060 return false;
4061
4062 this->fallthrough(StartLabel);
4063 this->emitLabel(StartLabel);
4064 // Condition. Iter < ArrayLen?
4065 if (!this->emitGetLocal(SizeT, Iter, E))
4066 return false;
4067 if (!this->emitGetLocal(SizeT, ArrayLen, E))
4068 return false;
4069 if (!this->emitLT(SizeT, E))
4070 return false;
4071 if (!this->jumpFalse(EndLabel, E))
4072 return false;
4073
4074 // Pointer to the allocated array is already on the stack.
4075 if (!this->emitGetLocal(SizeT, Iter, E))
4076 return false;
4077 if (!this->emitArrayElemPtr(SizeT, E))
4078 return false;
4079
4080 if (isa_and_nonnull<ImplicitValueInitExpr>(DynamicInit) &&
4081 DynamicInit->getType()->isArrayType()) {
4082 QualType ElemType =
4083 DynamicInit->getType()->getAsArrayTypeUnsafe()->getElementType();
4084 PrimType InitT = classifyPrim(ElemType);
4085 if (!this->visitZeroInitializer(InitT, ElemType, E))
4086 return false;
4087 if (!this->emitStorePop(InitT, E))
4088 return false;
4089 } else if (DynamicInit) {
4090 if (OptPrimType InitT = classify(DynamicInit)) {
4091 if (!this->visit(DynamicInit))
4092 return false;
4093 if (!this->emitStorePop(*InitT, E))
4094 return false;
4095 } else {
4096 if (!this->visitInitializer(DynamicInit))
4097 return false;
4098 if (!this->emitPopPtr(E))
4099 return false;
4100 }
4101 } else if (ElemT) {
4102 if (!this->visitZeroInitializer(
4103 *ElemT, InitType->getAsArrayTypeUnsafe()->getElementType(),
4104 Init))
4105 return false;
4106 if (!this->emitStorePop(*ElemT, E))
4107 return false;
4108 } else {
4109 assert(CtorFunc);
4110 if (!this->emitCall(CtorFunc, 0, E))
4111 return false;
4112 }
4113
4114 // ++Iter;
4115 if (!this->emitGetPtrLocal(Iter, E))
4116 return false;
4117 if (!this->emitIncPop(SizeT, false, E))
4118 return false;
4119
4120 if (!this->jump(StartLabel, E))
4121 return false;
4122
4123 this->fallthrough(EndLabel);
4124 this->emitLabel(EndLabel);
4125 }
4126 }
4127 } else { // Non-array.
4128 if (PlacementDest) {
4129 if (!this->visit(PlacementDest))
4130 return false;
4131 if (!this->emitCheckNewTypeMismatch(E, E))
4132 return false;
4133
4134 } else {
4135 // Allocate just one element.
4136 if (!this->emitAlloc(Desc, E))
4137 return false;
4138 }
4139
4140 if (Init) {
4141 if (ElemT) {
4142 if (!this->visit(Init))
4143 return false;
4144
4145 if (!this->emitInit(*ElemT, E))
4146 return false;
4147 } else {
4148 // Composite.
4149 if (!this->visitInitializer(Init))
4150 return false;
4151 }
4152 }
4153 }
4154
4155 if (DiscardResult)
4156 return this->emitPopPtr(E);
4157
4158 return true;
4159}
4160
4161template <class Emitter>
4163 if (E->containsErrors())
4164 return false;
4165 const FunctionDecl *OperatorDelete = E->getOperatorDelete();
4166
4167 if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
4168 return this->emitInvalidNewDeleteExpr(E, E);
4169
4170 // Arg must be an lvalue.
4171 if (!this->visit(E->getArgument()))
4172 return false;
4173
4174 return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E);
4175}
4176
4177template <class Emitter>
4179 if (DiscardResult)
4180 return true;
4181
4182 const Function *Func = nullptr;
4183 if (const Function *F = Ctx.getOrCreateObjCBlock(E))
4184 Func = F;
4185
4186 if (!Func)
4187 return false;
4188 return this->emitGetFnPtr(Func, E);
4189}
4190
4191template <class Emitter>
4193 const Type *TypeInfoType = E->getType().getTypePtr();
4194
4195 auto canonType = [](const Type *T) {
4196 return T->getCanonicalTypeUnqualified().getTypePtr();
4197 };
4198
4199 if (!E->isPotentiallyEvaluated()) {
4200 if (DiscardResult)
4201 return true;
4202
4203 if (E->isTypeOperand())
4204 return this->emitGetTypeid(
4205 canonType(E->getTypeOperand(Ctx.getASTContext()).getTypePtr()),
4206 TypeInfoType, E);
4207
4208 return this->emitGetTypeid(
4209 canonType(E->getExprOperand()->getType().getTypePtr()), TypeInfoType,
4210 E);
4211 }
4212
4213 // Otherwise, we need to evaluate the expression operand.
4214 assert(E->getExprOperand());
4215 assert(E->getExprOperand()->isLValue());
4216
4217 if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E))
4218 return false;
4219
4220 if (!this->visit(E->getExprOperand()))
4221 return false;
4222
4223 if (!this->emitGetTypeidPtr(TypeInfoType, E))
4224 return false;
4225 if (DiscardResult)
4226 return this->emitPopPtr(E);
4227 return true;
4228}
4229
4230template <class Emitter>
4232 const ObjCDictionaryLiteral *E) {
4234 return this->emitDummyPtr(E, E);
4235 return this->emitError(E);
4236}
4237
4238template <class Emitter>
4241 return this->emitDummyPtr(E, E);
4242 return this->emitError(E);
4243}
4244
4245template <class Emitter>
4247 assert(Ctx.getLangOpts().CPlusPlus);
4248 return this->emitConstBool(E->getValue(), E);
4249}
4250
4251template <class Emitter>
4253 if (DiscardResult)
4254 return true;
4255 assert(!Initializing);
4256
4257 const MSGuidDecl *GuidDecl = E->getGuidDecl();
4258 const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl();
4259 assert(RD);
4260 // If the definiton of the result type is incomplete, just return a dummy.
4261 // If (and when) that is read from, we will fail, but not now.
4262 if (!RD->isCompleteDefinition())
4263 return this->emitDummyPtr(GuidDecl, E);
4264
4265 UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(GuidDecl);
4266 if (!GlobalIndex)
4267 return false;
4268 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
4269 return false;
4270
4271 assert(this->getRecord(E->getType()));
4272
4273 const APValue &V = GuidDecl->getAsAPValue();
4274 if (V.getKind() == APValue::None)
4275 return true;
4276
4277 assert(V.isStruct());
4278 assert(V.getStructNumBases() == 0);
4279 if (!this->visitAPValueInitializer(V, E, E->getType()))
4280 return false;
4281
4282 return this->emitFinishInit(E);
4283}
4284
4285template <class Emitter>
4287 assert(classifyPrim(E->getType()) == PT_Bool);
4288 if (E->isValueDependent())
4289 return false;
4290 if (DiscardResult)
4291 return true;
4292 return this->emitConstBool(E->isSatisfied(), E);
4293}
4294
4295template <class Emitter>
4297 const ConceptSpecializationExpr *E) {
4298 assert(classifyPrim(E->getType()) == PT_Bool);
4299 if (DiscardResult)
4300 return true;
4301 return this->emitConstBool(E->isSatisfied(), E);
4302}
4303
4304template <class Emitter>
4309
4310template <class Emitter>
4312
4313 for (const Expr *SemE : E->semantics()) {
4314 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {
4315 if (SemE == E->getResultExpr())
4316 return false;
4317
4318 if (OVE->isUnique())
4319 continue;
4320
4321 if (!this->discard(OVE))
4322 return false;
4323 } else if (SemE == E->getResultExpr()) {
4324 if (!this->delegate(SemE))
4325 return false;
4326 } else {
4327 if (!this->discard(SemE))
4328 return false;
4329 }
4330 }
4331 return true;
4332}
4333
4334template <class Emitter>
4338
4339template <class Emitter>
4341 return this->emitError(E);
4342}
4343
4344template <class Emitter>
4346 assert(E->getType()->isVoidPointerType());
4347 if (DiscardResult)
4348 return true;
4349
4350 return this->emitDummyPtr(E, E);
4351}
4352
4353template <class Emitter>
4355 assert(Initializing);
4356 const auto *VT = E->getType()->castAs<VectorType>();
4357 QualType ElemType = VT->getElementType();
4358 PrimType ElemT = classifyPrim(ElemType);
4359 const Expr *Src = E->getSrcExpr();
4360 QualType SrcType = Src->getType();
4361 PrimType SrcElemT = classifyVectorElementType(SrcType);
4362
4363 unsigned SrcOffset =
4364 this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
4365 if (!this->visit(Src))
4366 return false;
4367 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
4368 return false;
4369
4370 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
4371 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
4372 return false;
4373 if (!this->emitArrayElemPop(SrcElemT, I, E))
4374 return false;
4375
4376 // Cast to the desired result element type.
4377 if (SrcElemT != ElemT) {
4378 if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
4379 return false;
4380 } else if (ElemType->isFloatingType() && SrcType != ElemType) {
4381 const auto *TargetSemantics = &Ctx.getFloatSemantics(ElemType);
4382 if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E))
4383 return false;
4384 }
4385 if (!this->emitInitElem(ElemT, I, E))
4386 return false;
4387 }
4388
4389 return true;
4390}
4391
4392template <class Emitter>
4394 // FIXME: Unary shuffle with mask not currently supported.
4395 if (E->getNumSubExprs() == 2)
4396 return this->emitInvalid(E);
4397
4398 assert(E->getNumSubExprs() > 2);
4399
4400 const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
4401 const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
4402 PrimType ElemT = classifyPrim(VT->getElementType());
4403 unsigned NumInputElems = VT->getNumElements();
4404 unsigned NumOutputElems = E->getNumSubExprs() - 2;
4405 assert(NumOutputElems > 0);
4406
4407 if (!Initializing) {
4408 UnsignedOrNone LocalIndex = allocateLocal(E);
4409 if (!LocalIndex)
4410 return false;
4411 if (!this->emitGetPtrLocal(*LocalIndex, E))
4412 return false;
4413 }
4414
4415 // Save both input vectors to a local variable.
4416 unsigned VectorOffsets[2];
4417 for (unsigned I = 0; I != 2; ++I) {
4418 VectorOffsets[I] =
4419 this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
4420 if (!this->visit(Vecs[I]))
4421 return false;
4422 if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
4423 return false;
4424 }
4425 for (unsigned I = 0; I != NumOutputElems; ++I) {
4426 APSInt ShuffleIndex = E->getShuffleMaskIdx(I);
4427 assert(ShuffleIndex >= -1);
4428 if (ShuffleIndex == -1)
4429 return this->emitInvalidShuffleVectorIndex(I, E);
4430
4431 assert(ShuffleIndex < (NumInputElems * 2));
4432 if (!this->emitGetLocal(PT_Ptr,
4433 VectorOffsets[ShuffleIndex >= NumInputElems], E))
4434 return false;
4435 unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
4436 if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
4437 return false;
4438
4439 if (!this->emitInitElem(ElemT, I, E))
4440 return false;
4441 }
4442
4443 if (DiscardResult)
4444 return this->emitPopPtr(E);
4445
4446 return true;
4447}
4448
4449template <class Emitter>
4451 const ExtVectorElementExpr *E) {
4452 const Expr *Base = E->getBase();
4453 assert(
4454 Base->getType()->isVectorType() ||
4455 Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
4456
4458 E->getEncodedElementAccess(Indices);
4459
4460 if (Indices.size() == 1) {
4461 if (!this->visit(Base))
4462 return false;
4463
4464 if (E->isGLValue()) {
4465 if (!this->emitConstUint32(Indices[0], E))
4466 return false;
4467 return this->emitArrayElemPtrPop(PT_Uint32, E);
4468 }
4469 // Else, also load the value.
4470 return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
4471 }
4472
4473 // Create a local variable for the base.
4474 unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true);
4475 if (!this->visit(Base))
4476 return false;
4477 if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
4478 return false;
4479
4480 // Now the vector variable for the return value.
4481 if (!Initializing) {
4482 UnsignedOrNone ResultIndex = allocateLocal(E);
4483 if (!ResultIndex)
4484 return false;
4485 if (!this->emitGetPtrLocal(*ResultIndex, E))
4486 return false;
4487 }
4488
4489 assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
4490
4491 PrimType ElemT =
4493 uint32_t DstIndex = 0;
4494 for (uint32_t I : Indices) {
4495 if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
4496 return false;
4497 if (!this->emitArrayElemPop(ElemT, I, E))
4498 return false;
4499 if (!this->emitInitElem(ElemT, DstIndex, E))
4500 return false;
4501 ++DstIndex;
4502 }
4503
4504 // Leave the result pointer on the stack.
4505 assert(!DiscardResult);
4506 return true;
4507}
4508
4509template <class Emitter>
4511 const Expr *SubExpr = E->getSubExpr();
4513 return this->discard(SubExpr) && this->emitInvalid(E);
4514
4515 if (DiscardResult)
4516 return true;
4517
4518 assert(classifyPrim(E) == PT_Ptr);
4519 return this->emitDummyPtr(E, E);
4520}
4521
4522template <class Emitter>
4524 const CXXStdInitializerListExpr *E) {
4525 const Expr *SubExpr = E->getSubExpr();
4527 Ctx.getASTContext().getAsConstantArrayType(SubExpr->getType());
4528 const Record *R = getRecord(E->getType());
4529 assert(Initializing);
4530 assert(SubExpr->isGLValue());
4531
4532 if (!this->visit(SubExpr))
4533 return false;
4534 if (!this->emitConstUint8(0, E))
4535 return false;
4536 if (!this->emitArrayElemPtrPopUint8(E))
4537 return false;
4538 if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E))
4539 return false;
4540
4541 PrimType SecondFieldT = classifyPrim(R->getField(1u)->Decl->getType());
4542 if (isIntegerOrBoolType(SecondFieldT)) {
4543 if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E))
4544 return false;
4545 return this->emitInitField(SecondFieldT, R->getField(1u)->Offset, E);
4546 }
4547 assert(SecondFieldT == PT_Ptr);
4548
4549 if (!this->emitGetFieldPtr(R->getField(0u)->Offset, E))
4550 return false;
4551 if (!this->emitExpandPtr(E))
4552 return false;
4553 if (!this->emitConst(ArrayType->getSize(), PT_Uint64, E))
4554 return false;
4555 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
4556 return false;
4557 return this->emitInitFieldPtr(R->getField(1u)->Offset, E);
4558}
4559
4560template <class Emitter>
4562 LocalScope<Emitter> BS(this);
4563 StmtExprScope<Emitter> SS(this);
4564
4565 const CompoundStmt *CS = E->getSubStmt();
4566 const Stmt *Result = CS->body_back();
4567 for (const Stmt *S : CS->body()) {
4568 if (S != Result) {
4569 if (!this->visitStmt(S))
4570 return false;
4571 continue;
4572 }
4573
4574 assert(S == Result);
4575 if (const Expr *ResultExpr = dyn_cast<Expr>(S))
4576 return this->delegate(ResultExpr);
4577 return this->emitUnsupported(E);
4578 }
4579
4580 return BS.destroyLocals();
4581}
4582
4583template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
4584 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
4585 /*NewInitializing=*/false, /*ToLValue=*/false);
4586 return this->Visit(E);
4587}
4588
4589template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
4590 // We're basically doing:
4591 // OptionScope<Emitter> Scope(this, DicardResult, Initializing, ToLValue);
4592 // but that's unnecessary of course.
4593 return this->Visit(E);
4594}
4595
4597 if (const auto *PE = dyn_cast<ParenExpr>(E))
4598 return stripCheckedDerivedToBaseCasts(PE->getSubExpr());
4599
4600 if (const auto *CE = dyn_cast<CastExpr>(E);
4601 CE &&
4602 (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp))
4603 return stripCheckedDerivedToBaseCasts(CE->getSubExpr());
4604
4605 return E;
4606}
4607
4608static const Expr *stripDerivedToBaseCasts(const Expr *E) {
4609 if (const auto *PE = dyn_cast<ParenExpr>(E))
4610 return stripDerivedToBaseCasts(PE->getSubExpr());
4611
4612 if (const auto *CE = dyn_cast<CastExpr>(E);
4613 CE && (CE->getCastKind() == CK_DerivedToBase ||
4614 CE->getCastKind() == CK_UncheckedDerivedToBase ||
4615 CE->getCastKind() == CK_NoOp))
4616 return stripDerivedToBaseCasts(CE->getSubExpr());
4617
4618 return E;
4619}
4620
4621template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
4622 if (E->getType().isNull())
4623 return false;
4624
4625 if (E->getType()->isVoidType())
4626 return this->discard(E);
4627
4628 // Create local variable to hold the return value.
4629 if (!E->isGLValue() && !canClassify(E->getType())) {
4630 UnsignedOrNone LocalIndex = allocateLocal(
4632 if (!LocalIndex)
4633 return false;
4634
4635 if (!this->emitGetPtrLocal(*LocalIndex, E))
4636 return false;
4637 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
4638 return this->visitInitializer(E);
4639 }
4640
4641 // Otherwise,we have a primitive return value, produce the value directly
4642 // and push it on the stack.
4643 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4644 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
4645 return this->Visit(E);
4646}
4647
4648template <class Emitter>
4650 assert(!canClassify(E->getType()));
4651
4652 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4653 /*NewInitializing=*/true, /*ToLValue=*/false);
4654 return this->Visit(E);
4655}
4656
4657template <class Emitter> bool Compiler<Emitter>::visitAsLValue(const Expr *E) {
4658 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4659 /*NewInitializing=*/false, /*ToLValue=*/true);
4660 return this->Visit(E);
4661}
4662
4663template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
4664 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4665 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
4666
4667 OptPrimType T = classify(E->getType());
4668 if (!T) {
4669 // Convert complex values to bool.
4670 if (E->getType()->isAnyComplexType()) {
4671 if (!this->visit(E))
4672 return false;
4673 return this->emitComplexBoolCast(E);
4674 }
4675 return false;
4676 }
4677
4678 if (!this->visit(E))
4679 return false;
4680
4681 if (T == PT_Bool)
4682 return true;
4683
4684 // Convert pointers to bool.
4685 if (T == PT_Ptr)
4686 return this->emitIsNonNullPtr(E);
4687
4688 // Or Floats.
4689 if (T == PT_Float)
4690 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
4691
4692 // Or anything else we can.
4693 return this->emitCast(*T, PT_Bool, E);
4694}
4695
4696template <class Emitter>
4697bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
4698 const Expr *E) {
4699 if (const auto *AT = QT->getAs<AtomicType>())
4700 QT = AT->getValueType();
4701
4702 switch (T) {
4703 case PT_Bool:
4704 return this->emitZeroBool(E);
4705 case PT_Sint8:
4706 return this->emitZeroSint8(E);
4707 case PT_Uint8:
4708 return this->emitZeroUint8(E);
4709 case PT_Sint16:
4710 return this->emitZeroSint16(E);
4711 case PT_Uint16:
4712 return this->emitZeroUint16(E);
4713 case PT_Sint32:
4714 return this->emitZeroSint32(E);
4715 case PT_Uint32:
4716 return this->emitZeroUint32(E);
4717 case PT_Sint64:
4718 return this->emitZeroSint64(E);
4719 case PT_Uint64:
4720 return this->emitZeroUint64(E);
4721 case PT_IntAP:
4722 return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
4723 case PT_IntAPS:
4724 return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
4725 case PT_Ptr:
4726 return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
4727 nullptr, E);
4728 case PT_MemberPtr:
4729 return this->emitNullMemberPtr(0, nullptr, E);
4730 case PT_Float: {
4731 APFloat F = APFloat::getZero(Ctx.getFloatSemantics(QT));
4732 return this->emitFloat(F, E);
4733 }
4734 case PT_FixedPoint: {
4735 auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
4736 return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
4737 }
4738 }
4739 llvm_unreachable("unknown primitive type");
4740}
4741
4742template <class Emitter>
4743bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
4744 const Expr *E) {
4745 assert(E);
4746 assert(R);
4747 // Fields
4748 for (const Record::Field &Field : R->fields()) {
4749 if (Field.isUnnamedBitField())
4750 continue;
4751
4752 const Descriptor *D = Field.Desc;
4753 if (D->isPrimitive()) {
4754 QualType QT = D->getType();
4755 PrimType T = classifyPrim(D->getType());
4756 if (!this->visitZeroInitializer(T, QT, E))
4757 return false;
4758 if (R->isUnion()) {
4759 if (!this->emitInitFieldActivate(T, Field.Offset, E))
4760 return false;
4761 break;
4762 }
4763 if (!this->emitInitField(T, Field.Offset, E))
4764 return false;
4765 continue;
4766 }
4767
4768 if (!this->emitGetPtrField(Field.Offset, E))
4769 return false;
4770
4771 if (D->isPrimitiveArray()) {
4772 QualType ET = D->getElemQualType();
4773 PrimType T = classifyPrim(ET);
4774 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
4775 if (!this->visitZeroInitializer(T, ET, E))
4776 return false;
4777 if (!this->emitInitElem(T, I, E))
4778 return false;
4779 }
4780 } else if (D->isCompositeArray()) {
4781 // Can't be a vector or complex field.
4782 if (!this->visitZeroArrayInitializer(D->getType(), E))
4783 return false;
4784 } else if (D->isRecord()) {
4785 if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
4786 return false;
4787 } else
4788 return false;
4789
4790 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
4791 // object's first non-static named data member is zero-initialized
4792 if (R->isUnion()) {
4793 if (!this->emitFinishInitActivatePop(E))
4794 return false;
4795 break;
4796 }
4797 if (!this->emitFinishInitPop(E))
4798 return false;
4799 }
4800
4801 for (const Record::Base &B : R->bases()) {
4802 if (!this->emitGetPtrBase(B.Offset, E))
4803 return false;
4804 if (!this->visitZeroRecordInitializer(B.R, E))
4805 return false;
4806 if (!this->emitFinishInitPop(E))
4807 return false;
4808 }
4809
4810 // FIXME: Virtual bases.
4811
4812 return true;
4813}
4814
4815template <class Emitter>
4816bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
4817 assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
4818 const ArrayType *AT = T->getAsArrayTypeUnsafe();
4819 QualType ElemType = AT->getElementType();
4820 size_t NumElems = cast<ConstantArrayType>(AT)->getZExtSize();
4821
4822 if (OptPrimType ElemT = classify(ElemType)) {
4823 for (size_t I = 0; I != NumElems; ++I) {
4824 if (!this->visitZeroInitializer(*ElemT, ElemType, E))
4825 return false;
4826 if (!this->emitInitElem(*ElemT, I, E))
4827 return false;
4828 }
4829 return true;
4830 }
4831 if (ElemType->isRecordType()) {
4832 const Record *R = getRecord(ElemType);
4833 if (!R)
4834 return false;
4835
4836 for (size_t I = 0; I != NumElems; ++I) {
4837 if (!this->emitConstUint32(I, E))
4838 return false;
4839 if (!this->emitArrayElemPtr(PT_Uint32, E))
4840 return false;
4841 if (!this->visitZeroRecordInitializer(R, E))
4842 return false;
4843 if (!this->emitPopPtr(E))
4844 return false;
4845 }
4846 return true;
4847 }
4848 if (ElemType->isArrayType()) {
4849 for (size_t I = 0; I != NumElems; ++I) {
4850 if (!this->emitConstUint32(I, E))
4851 return false;
4852 if (!this->emitArrayElemPtr(PT_Uint32, E))
4853 return false;
4854 if (!this->visitZeroArrayInitializer(ElemType, E))
4855 return false;
4856 if (!this->emitPopPtr(E))
4857 return false;
4858 }
4859 return true;
4860 }
4861
4862 return false;
4863}
4864
4865template <class Emitter>
4866bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS,
4867 const Expr *E) {
4868 if (!canClassify(E->getType()))
4869 return false;
4870
4871 if (!this->visit(RHS))
4872 return false;
4873 if (!this->visit(LHS))
4874 return false;
4875
4876 if (LHS->getType().isVolatileQualified())
4877 return this->emitInvalidStore(LHS->getType().getTypePtr(), E);
4878
4879 // We don't support assignments in C.
4880 if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(E))
4881 return false;
4882
4883 PrimType RHT = classifyPrim(RHS);
4884 bool Activates = refersToUnion(LHS);
4885 bool BitField = LHS->refersToBitField();
4886
4887 if (!this->emitFlip(PT_Ptr, RHT, E))
4888 return false;
4889
4890 if (DiscardResult) {
4891 if (BitField && Activates)
4892 return this->emitStoreBitFieldActivatePop(RHT, E);
4893 if (BitField)
4894 return this->emitStoreBitFieldPop(RHT, E);
4895 if (Activates)
4896 return this->emitStoreActivatePop(RHT, E);
4897 // Otherwise, regular non-activating store.
4898 return this->emitStorePop(RHT, E);
4899 }
4900
4901 auto maybeLoad = [&](bool Result) -> bool {
4902 if (!Result)
4903 return false;
4904 // Assignments aren't necessarily lvalues in C.
4905 // Load from them in that case.
4906 if (!E->isLValue())
4907 return this->emitLoadPop(RHT, E);
4908 return true;
4909 };
4910
4911 if (BitField && Activates)
4912 return maybeLoad(this->emitStoreBitFieldActivate(RHT, E));
4913 if (BitField)
4914 return maybeLoad(this->emitStoreBitField(RHT, E));
4915 if (Activates)
4916 return maybeLoad(this->emitStoreActivate(RHT, E));
4917 // Otherwise, regular non-activating store.
4918 return maybeLoad(this->emitStore(RHT, E));
4919}
4920
4921template <class Emitter>
4922template <typename T>
4923bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
4924 switch (Ty) {
4925 case PT_Sint8:
4926 return this->emitConstSint8(Value, E);
4927 case PT_Uint8:
4928 return this->emitConstUint8(Value, E);
4929 case PT_Sint16:
4930 return this->emitConstSint16(Value, E);
4931 case PT_Uint16:
4932 return this->emitConstUint16(Value, E);
4933 case PT_Sint32:
4934 return this->emitConstSint32(Value, E);
4935 case PT_Uint32:
4936 return this->emitConstUint32(Value, E);
4937 case PT_Sint64:
4938 return this->emitConstSint64(Value, E);
4939 case PT_Uint64:
4940 return this->emitConstUint64(Value, E);
4941 case PT_Bool:
4942 return this->emitConstBool(Value, E);
4943 case PT_Ptr:
4944 case PT_MemberPtr:
4945 case PT_Float:
4946 case PT_IntAP:
4947 case PT_IntAPS:
4948 case PT_FixedPoint:
4949 llvm_unreachable("Invalid integral type");
4950 break;
4951 }
4952 llvm_unreachable("unknown primitive type");
4953}
4954
4955template <class Emitter>
4956template <typename T>
4957bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
4958 return this->emitConst(Value, classifyPrim(E->getType()), E);
4959}
4960
4961template <class Emitter>
4962bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
4963 const Expr *E) {
4964 if (Ty == PT_IntAPS)
4965 return this->emitConstIntAPS(Value, E);
4966 if (Ty == PT_IntAP)
4967 return this->emitConstIntAP(Value, E);
4968
4969 if (Value.isSigned())
4970 return this->emitConst(Value.getSExtValue(), Ty, E);
4971 return this->emitConst(Value.getZExtValue(), Ty, E);
4972}
4973
4974template <class Emitter>
4975bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty,
4976 const Expr *E) {
4977 if (Ty == PT_IntAPS)
4978 return this->emitConstIntAPS(Value, E);
4979 if (Ty == PT_IntAP)
4980 return this->emitConstIntAP(Value, E);
4981
4982 if (isSignedType(Ty))
4983 return this->emitConst(Value.getSExtValue(), Ty, E);
4984 return this->emitConst(Value.getZExtValue(), Ty, E);
4985}
4986
4987template <class Emitter>
4988bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
4989 return this->emitConst(Value, classifyPrim(E->getType()), E);
4990}
4991
4992template <class Emitter>
4994 bool IsConst,
4995 bool IsVolatile,
4996 ScopeKind SC) {
4997 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
4998 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
4999 // or isa<MaterializeTemporaryExpr>().
5000 Descriptor *D = P.createDescriptor(Src, Ty, nullptr, Descriptor::InlineDescMD,
5001 IsConst, isa<const Expr *>(Src),
5002 /*IsMutable=*/false, IsVolatile);
5004 Scope::Local Local = this->createLocal(D);
5005 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
5006 Locals.insert({VD, Local});
5007 VarScope->addForScopeKind(Local, SC);
5008 return Local.Offset;
5009}
5010
5011template <class Emitter>
5013 ScopeKind SC) {
5014 const ValueDecl *Key = nullptr;
5015 const Expr *Init = nullptr;
5016 bool IsTemporary = false;
5017 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
5018 Key = VD;
5019
5020 if (const auto *VarD = dyn_cast<VarDecl>(VD))
5021 Init = VarD->getInit();
5022 }
5023 if (auto *E = Src.dyn_cast<const Expr *>()) {
5024 IsTemporary = true;
5025 if (Ty.isNull())
5026 Ty = E->getType();
5027 }
5028
5029 Descriptor *D = P.createDescriptor(
5031 IsTemporary, /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(),
5032 Init);
5033 if (!D)
5034 return std::nullopt;
5036
5037 Scope::Local Local = this->createLocal(D);
5038 if (Key)
5039 Locals.insert({Key, Local});
5040 VarScope->addForScopeKind(Local, SC);
5041 return Local.Offset;
5042}
5043
5044template <class Emitter>
5046 QualType Ty = E->getType();
5047 assert(!Ty->isRecordType());
5048
5049 Descriptor *D = P.createDescriptor(
5051 /*IsTemporary=*/true);
5052
5053 if (!D)
5054 return std::nullopt;
5055
5056 Scope::Local Local = this->createLocal(D);
5058 assert(S);
5059 // Attach to topmost scope.
5060 while (S->getParent())
5061 S = S->getParent();
5062 assert(S && !S->getParent());
5063 S->addLocal(Local);
5064 return Local.Offset;
5065}
5066
5067template <class Emitter>
5069 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
5070 return PT->getPointeeType()->getAsCanonical<RecordType>();
5071 return Ty->getAsCanonical<RecordType>();
5072}
5073
5074template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
5075 if (const auto *RecordTy = getRecordTy(Ty))
5076 return getRecord(RecordTy->getDecl()->getDefinitionOrSelf());
5077 return nullptr;
5078}
5079
5080template <class Emitter>
5082 return P.getOrCreateRecord(RD);
5083}
5084
5085template <class Emitter>
5087 return Ctx.getOrCreateFunction(FD);
5088}
5089
5090template <class Emitter>
5091bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
5093
5094 auto maybeDestroyLocals = [&]() -> bool {
5095 if (DestroyToplevelScope)
5096 return RootScope.destroyLocals() && this->emitCheckAllocations(E);
5097 return this->emitCheckAllocations(E);
5098 };
5099
5100 // Void expressions.
5101 if (E->getType()->isVoidType()) {
5102 if (!visit(E))
5103 return false;
5104 return this->emitRetVoid(E) && maybeDestroyLocals();
5105 }
5106
5107 // Expressions with a primitive return type.
5108 if (OptPrimType T = classify(E)) {
5109 if (!visit(E))
5110 return false;
5111
5112 return this->emitRet(*T, E) && maybeDestroyLocals();
5113 }
5114
5115 // Expressions with a composite return type.
5116 // For us, that means everything we don't
5117 // have a PrimType for.
5118 if (UnsignedOrNone LocalOffset = this->allocateLocal(E)) {
5119 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalOffset));
5120 if (!this->emitGetPtrLocal(*LocalOffset, E))
5121 return false;
5122
5123 if (!visitInitializer(E))
5124 return false;
5125
5126 if (!this->emitFinishInit(E))
5127 return false;
5128 // We are destroying the locals AFTER the Ret op.
5129 // The Ret op needs to copy the (alive) values, but the
5130 // destructors may still turn the entire expression invalid.
5131 return this->emitRetValue(E) && maybeDestroyLocals();
5132 }
5133
5134 return maybeDestroyLocals() && false;
5135}
5136
5137template <class Emitter>
5139
5140 auto R = this->visitVarDecl(VD, VD->getInit(), /*Toplevel=*/true);
5141
5142 if (R.notCreated())
5143 return R;
5144
5145 if (R)
5146 return true;
5147
5148 if (!R && Context::shouldBeGloballyIndexed(VD)) {
5149 if (auto GlobalIndex = P.getGlobal(VD)) {
5150 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5151 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5152
5154 GlobalBlock->invokeDtor();
5155 }
5156 }
5157
5158 return R;
5159}
5160
5161/// Toplevel visitDeclAndReturn().
5162/// We get here from evaluateAsInitializer().
5163/// We need to evaluate the initializer and return its value.
5164template <class Emitter>
5166 bool ConstantContext) {
5167 // We only create variables if we're evaluating in a constant context.
5168 // Otherwise, just evaluate the initializer and return it.
5169 if (!ConstantContext) {
5170 DeclScope<Emitter> LS(this, VD);
5171 if (!this->visit(Init))
5172 return false;
5173 return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
5174 LS.destroyLocals() && this->emitCheckAllocations(VD);
5175 }
5176
5177 LocalScope<Emitter> VDScope(this);
5178 if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true))
5179 return false;
5180
5181 OptPrimType VarT = classify(VD->getType());
5183 auto GlobalIndex = P.getGlobal(VD);
5184 assert(GlobalIndex); // visitVarDecl() didn't return false.
5185 if (VarT) {
5186 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
5187 return false;
5188 } else {
5189 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
5190 return false;
5191 }
5192 } else {
5193 auto Local = Locals.find(VD);
5194 assert(Local != Locals.end()); // Same here.
5195 if (VarT) {
5196 if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
5197 return false;
5198 } else {
5199 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
5200 return false;
5201 }
5202 }
5203
5204 // Return the value.
5205 if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
5206 // If the Ret above failed and this is a global variable, mark it as
5207 // uninitialized, even everything else succeeded.
5209 auto GlobalIndex = P.getGlobal(VD);
5210 assert(GlobalIndex);
5211 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5212 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5213
5215 GlobalBlock->invokeDtor();
5216 }
5217 return false;
5218 }
5219
5220 return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
5221}
5222
5223template <class Emitter>
5225 const Expr *Init,
5226 bool Toplevel) {
5227 // We don't know what to do with these, so just return false.
5228 if (VD->getType().isNull())
5229 return false;
5230
5231 // This case is EvalEmitter-only. If we won't create any instructions for the
5232 // initializer anyway, don't bother creating the variable in the first place.
5233 if (!this->isActive())
5235
5236 OptPrimType VarT = classify(VD->getType());
5237
5238 if (Init && Init->isValueDependent())
5239 return false;
5240
5242 auto checkDecl = [&]() -> bool {
5243 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
5244 return !NeedsOp || this->emitCheckDecl(VD, VD);
5245 };
5246
5248 UnsignedOrNone GlobalIndex = P.getGlobal(VD);
5249 if (GlobalIndex) {
5250 // The global was previously created but the initializer failed.
5251 if (!P.getGlobal(*GlobalIndex)->isInitialized())
5252 return false;
5253 // We've already seen and initialized this global.
5254 if (P.isGlobalInitialized(*GlobalIndex))
5255 return checkDecl();
5256 // The previous attempt at initialization might've been unsuccessful,
5257 // so let's try this one.
5258 } else if ((GlobalIndex = P.createGlobal(VD, Init))) {
5259 } else {
5260 return false;
5261 }
5262 if (!Init)
5263 return true;
5264
5265 if (!checkDecl())
5266 return false;
5267
5268 if (VarT) {
5269 if (!this->visit(Init))
5270 return false;
5271
5272 return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
5273 }
5274
5275 if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
5276 return false;
5277
5278 if (!visitInitializer(Init))
5279 return false;
5280
5281 return this->emitFinishInitGlobal(Init);
5282 }
5283 // Local variables.
5285
5286 if (VarT) {
5287 unsigned Offset = this->allocateLocalPrimitive(
5288 VD, *VarT, VD->getType().isConstQualified(),
5290
5291 if (!Init || Init->getType()->isVoidType())
5292 return true;
5293
5294 // If this is a toplevel declaration, create a scope for the
5295 // initializer.
5296 if (Toplevel) {
5298 if (!this->visit(Init))
5299 return false;
5300 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
5301 }
5302 if (!this->visit(Init))
5303 return false;
5304 return this->emitSetLocal(*VarT, Offset, VD);
5305 }
5306 // Local composite variables.
5307 if (UnsignedOrNone Offset =
5308 this->allocateLocal(VD, VD->getType(), ScopeKind::Block)) {
5309 if (!Init)
5310 return true;
5311
5312 if (!this->emitGetPtrLocal(*Offset, Init))
5313 return false;
5314
5315 if (!visitInitializer(Init))
5316 return false;
5317
5318 return this->emitFinishInitPop(Init);
5319 }
5320 return false;
5321}
5322
5323template <class Emitter>
5325 const Expr *E) {
5326 assert(!DiscardResult);
5327 if (Val.isInt())
5328 return this->emitConst(Val.getInt(), ValType, E);
5329 if (Val.isFloat()) {
5330 APFloat F = Val.getFloat();
5331 return this->emitFloat(F, E);
5332 }
5333
5334 if (Val.isLValue()) {
5335 if (Val.isNullPointer())
5336 return this->emitNull(ValType, 0, nullptr, E);
5338 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
5339 return this->visit(BaseExpr);
5340 if (const auto *VD = Base.dyn_cast<const ValueDecl *>())
5341 return this->visitDeclRef(VD, E);
5342 } else if (Val.isMemberPointer()) {
5343 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl())
5344 return this->emitGetMemberPtr(MemberDecl, E);
5345 return this->emitNullMemberPtr(0, nullptr, E);
5346 }
5347
5348 return false;
5349}
5350
5351template <class Emitter>
5353 const Expr *E, QualType T) {
5354 if (Val.isStruct()) {
5355 const Record *R = this->getRecord(T);
5356 assert(R);
5357 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
5358 const APValue &F = Val.getStructField(I);
5359 const Record::Field *RF = R->getField(I);
5360 QualType FieldType = RF->Decl->getType();
5361
5362 if (OptPrimType PT = classify(FieldType)) {
5363 if (!this->visitAPValue(F, *PT, E))
5364 return false;
5365 if (!this->emitInitField(*PT, RF->Offset, E))
5366 return false;
5367 } else {
5368 if (!this->emitGetPtrField(RF->Offset, E))
5369 return false;
5370 if (!this->visitAPValueInitializer(F, E, FieldType))
5371 return false;
5372 if (!this->emitPopPtr(E))
5373 return false;
5374 }
5375 }
5376 return true;
5377 }
5378 if (Val.isUnion()) {
5379 const FieldDecl *UnionField = Val.getUnionField();
5380 if (!UnionField)
5381 return true;
5382 const Record *R = this->getRecord(T);
5383 assert(R);
5384 const APValue &F = Val.getUnionValue();
5385 const Record::Field *RF = R->getField(UnionField);
5386 QualType FieldType = RF->Decl->getType();
5387
5388 if (OptPrimType PT = classify(FieldType)) {
5389 if (!this->visitAPValue(F, *PT, E))
5390 return false;
5391 if (RF->isBitField())
5392 return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(),
5393 E);
5394 return this->emitInitFieldActivate(*PT, RF->Offset, E);
5395 }
5396
5397 if (!this->emitGetPtrField(RF->Offset, E))
5398 return false;
5399 if (!this->emitActivate(E))
5400 return false;
5401 if (!this->visitAPValueInitializer(F, E, FieldType))
5402 return false;
5403 return this->emitPopPtr(E);
5404 }
5405 if (Val.isArray()) {
5406 const auto *ArrType = T->getAsArrayTypeUnsafe();
5407 QualType ElemType = ArrType->getElementType();
5408 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
5409 const APValue &Elem = Val.getArrayInitializedElt(A);
5410 if (OptPrimType ElemT = classify(ElemType)) {
5411 if (!this->visitAPValue(Elem, *ElemT, E))
5412 return false;
5413 if (!this->emitInitElem(*ElemT, A, E))
5414 return false;
5415 } else {
5416 if (!this->emitConstUint32(A, E))
5417 return false;
5418 if (!this->emitArrayElemPtrUint32(E))
5419 return false;
5420 if (!this->visitAPValueInitializer(Elem, E, ElemType))
5421 return false;
5422 if (!this->emitPopPtr(E))
5423 return false;
5424 }
5425 }
5426 return true;
5427 }
5428 // TODO: Other types.
5429
5430 return false;
5431}
5432
5433template <class Emitter>
5435 unsigned BuiltinID) {
5436 if (BuiltinID == Builtin::BI__builtin_constant_p) {
5437 // Void argument is always invalid and harder to handle later.
5438 if (E->getArg(0)->getType()->isVoidType()) {
5439 if (DiscardResult)
5440 return true;
5441 return this->emitConst(0, E);
5442 }
5443
5444 if (!this->emitStartSpeculation(E))
5445 return false;
5446 LabelTy EndLabel = this->getLabel();
5447 if (!this->speculate(E, EndLabel))
5448 return false;
5449 if (!this->emitEndSpeculation(E))
5450 return false;
5451 this->fallthrough(EndLabel);
5452 if (DiscardResult)
5453 return this->emitPop(classifyPrim(E), E);
5454 return true;
5455 }
5456
5457 // For these, we're expected to ultimately return an APValue pointing
5458 // to the CallExpr. This is needed to get the correct codegen.
5459 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
5460 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
5461 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
5462 BuiltinID == Builtin::BI__builtin_function_start) {
5463 if (DiscardResult)
5464 return true;
5465 return this->emitDummyPtr(E, E);
5466 }
5467
5469 OptPrimType ReturnT = classify(E);
5470
5471 // Non-primitive return type. Prepare storage.
5472 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
5473 UnsignedOrNone LocalIndex = allocateLocal(E);
5474 if (!LocalIndex)
5475 return false;
5476 if (!this->emitGetPtrLocal(*LocalIndex, E))
5477 return false;
5478 }
5479
5480 // Prepare function arguments including special cases.
5481 switch (BuiltinID) {
5482 case Builtin::BI__builtin_object_size:
5483 case Builtin::BI__builtin_dynamic_object_size: {
5484 assert(E->getNumArgs() == 2);
5485 const Expr *Arg0 = E->getArg(0);
5486 if (Arg0->isGLValue()) {
5487 if (!this->visit(Arg0))
5488 return false;
5489
5490 } else {
5491 if (!this->visitAsLValue(Arg0))
5492 return false;
5493 }
5494 if (!this->visit(E->getArg(1)))
5495 return false;
5496
5497 } break;
5498 case Builtin::BI__assume:
5499 case Builtin::BI__builtin_assume:
5500 // Argument is not evaluated.
5501 break;
5502 case Builtin::BI__atomic_is_lock_free:
5503 case Builtin::BI__atomic_always_lock_free: {
5504 assert(E->getNumArgs() == 2);
5505 if (!this->visit(E->getArg(0)))
5506 return false;
5507 if (!this->visitAsLValue(E->getArg(1)))
5508 return false;
5509 } break;
5510
5511 default:
5512 if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
5513 // Put arguments on the stack.
5514 for (const auto *Arg : E->arguments()) {
5515 if (!this->visit(Arg))
5516 return false;
5517 }
5518 }
5519 }
5520
5521 if (!this->emitCallBI(E, BuiltinID, E))
5522 return false;
5523
5524 if (DiscardResult && !ReturnType->isVoidType())
5525 return this->emitPop(ReturnT.value_or(PT_Ptr), E);
5526
5527 return true;
5528}
5529
5530template <class Emitter>
5532 if (E->containsErrors())
5533 return false;
5534 const FunctionDecl *FuncDecl = E->getDirectCallee();
5535
5536 if (FuncDecl) {
5537 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
5538 return VisitBuiltinCallExpr(E, BuiltinID);
5539
5540 // Calls to replaceable operator new/operator delete.
5542 if (FuncDecl->getDeclName().isAnyOperatorNew())
5543 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
5544 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete);
5545 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
5546 }
5547
5548 // Explicit calls to trivial destructors
5549 if (const auto *DD = dyn_cast<CXXDestructorDecl>(FuncDecl);
5550 DD && DD->isTrivial()) {
5551 const auto *MemberCall = cast<CXXMemberCallExpr>(E);
5552 if (!this->visit(MemberCall->getImplicitObjectArgument()))
5553 return false;
5554 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
5555 this->emitPopPtr(E);
5556 }
5557 }
5558
5559 LocalScope<Emitter> CallScope(this, ScopeKind::Call);
5560
5561 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
5563 bool HasRVO = !ReturnType->isVoidType() && !T;
5564
5565 if (HasRVO) {
5566 if (DiscardResult) {
5567 // If we need to discard the return value but the function returns its
5568 // value via an RVO pointer, we need to create one such pointer just
5569 // for this call.
5570 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5571 if (!this->emitGetPtrLocal(*LocalIndex, E))
5572 return false;
5573 }
5574 } else {
5575 // We need the result. Prepare a pointer to return or
5576 // dup the current one.
5577 if (!Initializing) {
5578 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5579 if (!this->emitGetPtrLocal(*LocalIndex, E))
5580 return false;
5581 }
5582 }
5583 if (!this->emitDupPtr(E))
5584 return false;
5585 }
5586 }
5587
5589
5590 bool IsAssignmentOperatorCall = false;
5591 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
5592 OCE && OCE->isAssignmentOp()) {
5593 // Just like with regular assignments, we need to special-case assignment
5594 // operators here and evaluate the RHS (the second arg) before the LHS (the
5595 // first arg). We fix this by using a Flip op later.
5596 assert(Args.size() == 2);
5597 IsAssignmentOperatorCall = true;
5598 std::reverse(Args.begin(), Args.end());
5599 }
5600 // Calling a static operator will still
5601 // pass the instance, but we don't need it.
5602 // Discard it here.
5603 if (isa<CXXOperatorCallExpr>(E)) {
5604 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
5605 MD && MD->isStatic()) {
5606 if (!this->discard(E->getArg(0)))
5607 return false;
5608 // Drop first arg.
5609 Args.erase(Args.begin());
5610 }
5611 }
5612
5613 bool Devirtualized = false;
5614 UnsignedOrNone CalleeOffset = std::nullopt;
5615 // Add the (optional, implicit) This pointer.
5616 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
5617 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
5618 // If we end up creating a CallPtr op for this, we need the base of the
5619 // member pointer as the instance pointer, and later extract the function
5620 // decl as the function pointer.
5621 const Expr *Callee = E->getCallee();
5622 CalleeOffset =
5623 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
5624 if (!this->visit(Callee))
5625 return false;
5626 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
5627 return false;
5628 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5629 return false;
5630 if (!this->emitGetMemberPtrBase(E))
5631 return false;
5632 } else {
5633 const auto *InstancePtr = MC->getImplicitObjectArgument();
5634 if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
5635 isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
5636 const auto *Stripped = stripCheckedDerivedToBaseCasts(InstancePtr);
5637 if (isa<CXXThisExpr>(Stripped)) {
5638 FuncDecl =
5639 cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
5640 Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5641 Devirtualized = true;
5642 if (!this->visit(Stripped))
5643 return false;
5644 } else {
5645 if (!this->visit(InstancePtr))
5646 return false;
5647 }
5648 } else {
5649 if (!this->visit(InstancePtr))
5650 return false;
5651 }
5652 }
5653 } else if (const auto *PD =
5654 dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
5655 if (!this->emitCheckPseudoDtor(E))
5656 return false;
5657 const Expr *Base = PD->getBase();
5658 // E.g. `using T = int; 0.~T();`.
5659 if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr)
5660 return this->discard(Base);
5661 if (!this->visit(Base))
5662 return false;
5663 return this->emitEndLifetimePop(E);
5664 } else if (!FuncDecl) {
5665 const Expr *Callee = E->getCallee();
5666 CalleeOffset =
5667 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
5668 if (!this->visit(Callee))
5669 return false;
5670 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
5671 return false;
5672 }
5673
5674 if (!this->visitCallArgs(Args, FuncDecl, IsAssignmentOperatorCall,
5676 return false;
5677
5678 // Undo the argument reversal we did earlier.
5679 if (IsAssignmentOperatorCall) {
5680 assert(Args.size() == 2);
5681 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
5682 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
5683 if (!this->emitFlip(Arg2T, Arg1T, E))
5684 return false;
5685 }
5686
5687 if (FuncDecl) {
5688 const Function *Func = getFunction(FuncDecl);
5689 if (!Func)
5690 return false;
5691
5692 // In error cases, the function may be called with fewer arguments than
5693 // parameters.
5694 if (E->getNumArgs() < Func->getNumWrittenParams())
5695 return false;
5696
5697 assert(HasRVO == Func->hasRVO());
5698
5699 bool HasQualifier = false;
5700 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
5701 HasQualifier = ME->hasQualifier();
5702
5703 bool IsVirtual = false;
5704 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
5705 IsVirtual = !Devirtualized && MD->isVirtual();
5706
5707 // In any case call the function. The return value will end up on the stack
5708 // and if the function has RVO, we already have the pointer on the stack to
5709 // write the result into.
5710 if (IsVirtual && !HasQualifier) {
5711 uint32_t VarArgSize = 0;
5712 unsigned NumParams =
5713 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
5714 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5715 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5716
5717 if (!this->emitCallVirt(Func, VarArgSize, E))
5718 return false;
5719 } else if (Func->isVariadic()) {
5720 uint32_t VarArgSize = 0;
5721 unsigned NumParams =
5722 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
5723 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5724 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5725 if (!this->emitCallVar(Func, VarArgSize, E))
5726 return false;
5727 } else {
5728 if (!this->emitCall(Func, 0, E))
5729 return false;
5730 }
5731 } else {
5732 // Indirect call. Visit the callee, which will leave a FunctionPointer on
5733 // the stack. Cleanup of the returned value if necessary will be done after
5734 // the function call completed.
5735
5736 // Sum the size of all args from the call expr.
5737 uint32_t ArgSize = 0;
5738 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
5739 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5740
5741 // Get the callee, either from a member pointer or function pointer saved in
5742 // CalleeOffset.
5743 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
5744 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5745 return false;
5746 if (!this->emitGetMemberPtrDecl(E))
5747 return false;
5748 } else {
5749 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
5750 return false;
5751 }
5752 if (!this->emitCallPtr(ArgSize, E, E))
5753 return false;
5754 }
5755
5756 // Cleanup for discarded return values.
5757 if (DiscardResult && !ReturnType->isVoidType() && T)
5758 return this->emitPop(*T, E) && CallScope.destroyLocals();
5759
5760 return CallScope.destroyLocals();
5761}
5762
5763template <class Emitter>
5765 SourceLocScope<Emitter> SLS(this, E);
5766
5767 return this->delegate(E->getExpr());
5768}
5769
5770template <class Emitter>
5772 SourceLocScope<Emitter> SLS(this, E);
5773
5774 return this->delegate(E->getExpr());
5775}
5776
5777template <class Emitter>
5779 if (DiscardResult)
5780 return true;
5781
5782 return this->emitConstBool(E->getValue(), E);
5783}
5784
5785template <class Emitter>
5787 const CXXNullPtrLiteralExpr *E) {
5788 if (DiscardResult)
5789 return true;
5790
5791 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
5792 return this->emitNullPtr(Val, nullptr, E);
5793}
5794
5795template <class Emitter>
5797 if (DiscardResult)
5798 return true;
5799
5800 assert(E->getType()->isIntegerType());
5801
5802 PrimType T = classifyPrim(E->getType());
5803 return this->emitZero(T, E);
5804}
5805
5806template <class Emitter>
5808 if (DiscardResult)
5809 return true;
5810
5811 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
5812 if (this->LambdaThisCapture.Offset > 0) {
5813 if (this->LambdaThisCapture.IsPtr)
5814 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
5815 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
5816 }
5817 }
5818
5819 // In some circumstances, the 'this' pointer does not actually refer to the
5820 // instance pointer of the current function frame, but e.g. to the declaration
5821 // currently being initialized. Here we emit the necessary instruction(s) for
5822 // this scenario.
5823 if (!InitStackActive || InitStack.empty())
5824 return this->emitThis(E);
5825
5826 // If our init stack is, for example:
5827 // 0 Stack: 3 (decl)
5828 // 1 Stack: 6 (init list)
5829 // 2 Stack: 1 (field)
5830 // 3 Stack: 6 (init list)
5831 // 4 Stack: 1 (field)
5832 //
5833 // We want to find the LAST element in it that's an init list,
5834 // which is marked with the K_InitList marker. The index right
5835 // before that points to an init list. We need to find the
5836 // elements before the K_InitList element that point to a base
5837 // (e.g. a decl or This), optionally followed by field, elem, etc.
5838 // In the example above, we want to emit elements [0..2].
5839 unsigned StartIndex = 0;
5840 unsigned EndIndex = 0;
5841 // Find the init list.
5842 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
5843 if (InitStack[StartIndex].Kind == InitLink::K_DIE) {
5844 EndIndex = StartIndex;
5845 --StartIndex;
5846 break;
5847 }
5848 }
5849
5850 // Walk backwards to find the base.
5851 for (; StartIndex > 0; --StartIndex) {
5852 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
5853 continue;
5854
5855 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
5856 InitStack[StartIndex].Kind != InitLink::K_Elem &&
5857 InitStack[StartIndex].Kind != InitLink::K_DIE)
5858 break;
5859 }
5860
5861 if (StartIndex == 0 && EndIndex == 0)
5862 EndIndex = InitStack.size() - 1;
5863
5864 assert(StartIndex < EndIndex);
5865
5866 // Emit the instructions.
5867 for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
5868 if (InitStack[I].Kind == InitLink::K_InitList ||
5869 InitStack[I].Kind == InitLink::K_DIE)
5870 continue;
5871 if (!InitStack[I].template emit<Emitter>(this, E))
5872 return false;
5873 }
5874 return true;
5875}
5876
5877template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
5878 switch (S->getStmtClass()) {
5879 case Stmt::CompoundStmtClass:
5881 case Stmt::DeclStmtClass:
5882 return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
5883 case Stmt::ReturnStmtClass:
5885 case Stmt::IfStmtClass:
5886 return visitIfStmt(cast<IfStmt>(S));
5887 case Stmt::WhileStmtClass:
5889 case Stmt::DoStmtClass:
5890 return visitDoStmt(cast<DoStmt>(S));
5891 case Stmt::ForStmtClass:
5892 return visitForStmt(cast<ForStmt>(S));
5893 case Stmt::CXXForRangeStmtClass:
5895 case Stmt::BreakStmtClass:
5897 case Stmt::ContinueStmtClass:
5899 case Stmt::SwitchStmtClass:
5901 case Stmt::CaseStmtClass:
5902 return visitCaseStmt(cast<CaseStmt>(S));
5903 case Stmt::DefaultStmtClass:
5905 case Stmt::AttributedStmtClass:
5907 case Stmt::CXXTryStmtClass:
5909 case Stmt::NullStmtClass:
5910 return true;
5911 // Always invalid statements.
5912 case Stmt::GCCAsmStmtClass:
5913 case Stmt::MSAsmStmtClass:
5914 case Stmt::GotoStmtClass:
5915 return this->emitInvalid(S);
5916 case Stmt::LabelStmtClass:
5917 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
5918 default: {
5919 if (const auto *E = dyn_cast<Expr>(S))
5920 return this->discard(E);
5921 return false;
5922 }
5923 }
5924}
5925
5926template <class Emitter>
5929 for (const auto *InnerStmt : S->body())
5930 if (!visitStmt(InnerStmt))
5931 return false;
5932 return Scope.destroyLocals();
5933}
5934
5935template <class Emitter>
5936bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
5937 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
5938 for (auto *BD : DD->flat_bindings())
5939 if (auto *KD = BD->getHoldingVar();
5940 KD && !this->visitVarDecl(KD, KD->getInit()))
5941 return false;
5942 }
5943 return true;
5944}
5945
5947 assert(FD);
5948 assert(FD->getParent()->isUnion());
5949 const CXXRecordDecl *CXXRD =
5951 return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
5952}
5953
5954template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
5955 for (;;) {
5956 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
5957 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
5958 FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
5959 return true;
5960 E = ME->getBase();
5961 continue;
5962 }
5963
5964 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
5965 E = ASE->getBase()->IgnoreImplicit();
5966 continue;
5967 }
5968
5969 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
5970 ICE && (ICE->getCastKind() == CK_NoOp ||
5971 ICE->getCastKind() == CK_DerivedToBase ||
5972 ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
5973 E = ICE->getSubExpr();
5974 continue;
5975 }
5976
5977 if (const auto *This = dyn_cast<CXXThisExpr>(E)) {
5978 const auto *ThisRecord =
5979 This->getType()->getPointeeType()->getAsRecordDecl();
5980 if (!ThisRecord->isUnion())
5981 return false;
5982 // Otherwise, always activate if we're in the ctor.
5983 if (const auto *Ctor =
5984 dyn_cast_if_present<CXXConstructorDecl>(CompilingFunction))
5985 return Ctor->getParent() == ThisRecord;
5986 return false;
5987 }
5988
5989 break;
5990 }
5991 return false;
5992}
5993
5994template <class Emitter>
5996 bool EvaluateConditionDecl) {
5997 for (const auto *D : DS->decls()) {
6000 continue;
6001
6002 const auto *VD = dyn_cast<VarDecl>(D);
6003 if (!VD)
6004 return false;
6005 if (!this->visitVarDecl(VD, VD->getInit()))
6006 return false;
6007
6008 // Register decomposition decl holding vars.
6009 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
6010 return false;
6011 }
6012
6013 return true;
6014}
6015
6016template <class Emitter>
6018 if (this->InStmtExpr)
6019 return this->emitUnsupported(RS);
6020
6021 if (const Expr *RE = RS->getRetValue()) {
6022 LocalScope<Emitter> RetScope(this);
6023 if (ReturnType) {
6024 // Primitive types are simply returned.
6025 if (!this->visit(RE))
6026 return false;
6027 this->emitCleanup();
6028 return this->emitRet(*ReturnType, RS);
6029 }
6030
6031 if (RE->getType()->isVoidType()) {
6032 if (!this->visit(RE))
6033 return false;
6034 } else {
6035 if (RE->containsErrors())
6036 return false;
6037
6039 // RVO - construct the value in the return location.
6040 if (!this->emitRVOPtr(RE))
6041 return false;
6042 if (!this->visitInitializer(RE))
6043 return false;
6044 if (!this->emitPopPtr(RE))
6045 return false;
6046
6047 this->emitCleanup();
6048 return this->emitRetVoid(RS);
6049 }
6050 }
6051
6052 // Void return.
6053 this->emitCleanup();
6054 return this->emitRetVoid(RS);
6055}
6056
6057template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
6058 LocalScope<Emitter> IfScope(this);
6059
6060 auto visitChildStmt = [&](const Stmt *S) -> bool {
6061 LocalScope<Emitter> SScope(this);
6062 if (!visitStmt(S))
6063 return false;
6064 return SScope.destroyLocals();
6065 };
6066
6067 if (auto *CondInit = IS->getInit()) {
6068 if (!visitStmt(CondInit))
6069 return false;
6070 }
6071
6072 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) {
6073 if (!visitDeclStmt(CondDecl))
6074 return false;
6075 }
6076
6077 // Save ourselves compiling some code and the jumps, etc. if the condition is
6078 // stataically known to be either true or false. We could look at more cases
6079 // here, but I think all the ones that actually happen are using a
6080 // ConstantExpr.
6081 if (std::optional<bool> BoolValue = getBoolValue(IS->getCond())) {
6082 if (*BoolValue)
6083 return visitChildStmt(IS->getThen());
6084 if (const Stmt *Else = IS->getElse())
6085 return visitChildStmt(Else);
6086 return true;
6087 }
6088
6089 // Otherwise, compile the condition.
6090 if (IS->isNonNegatedConsteval()) {
6091 if (!this->emitIsConstantContext(IS))
6092 return false;
6093 } else if (IS->isNegatedConsteval()) {
6094 if (!this->emitIsConstantContext(IS))
6095 return false;
6096 if (!this->emitInv(IS))
6097 return false;
6098 } else {
6100 if (!this->visitBool(IS->getCond()))
6101 return false;
6102 if (!CondScope.destroyLocals())
6103 return false;
6104 }
6105
6106 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
6107 return false;
6108
6109 if (const Stmt *Else = IS->getElse()) {
6110 LabelTy LabelElse = this->getLabel();
6111 LabelTy LabelEnd = this->getLabel();
6112 if (!this->jumpFalse(LabelElse, IS))
6113 return false;
6114 if (!visitChildStmt(IS->getThen()))
6115 return false;
6116 if (!this->jump(LabelEnd, IS))
6117 return false;
6118 this->emitLabel(LabelElse);
6119 if (!visitChildStmt(Else))
6120 return false;
6121 this->emitLabel(LabelEnd);
6122 } else {
6123 LabelTy LabelEnd = this->getLabel();
6124 if (!this->jumpFalse(LabelEnd, IS))
6125 return false;
6126 if (!visitChildStmt(IS->getThen()))
6127 return false;
6128 this->emitLabel(LabelEnd);
6129 }
6130
6131 if (!IfScope.destroyLocals())
6132 return false;
6133
6134 return true;
6135}
6136
6137template <class Emitter>
6139 const Expr *Cond = S->getCond();
6140 const Stmt *Body = S->getBody();
6141
6142 LabelTy CondLabel = this->getLabel(); // Label before the condition.
6143 LabelTy EndLabel = this->getLabel(); // Label after the loop.
6144 LocalScope<Emitter> WholeLoopScope(this);
6145 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6146
6147 this->fallthrough(CondLabel);
6148 this->emitLabel(CondLabel);
6149
6150 // Start of the loop body {
6151 LocalScope<Emitter> CondScope(this);
6152
6153 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6154 if (!visitDeclStmt(CondDecl))
6155 return false;
6156 }
6157
6158 if (!this->visitBool(Cond))
6159 return false;
6160
6161 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6162 return false;
6163
6164 if (!this->jumpFalse(EndLabel, S))
6165 return false;
6166
6167 if (!this->visitStmt(Body))
6168 return false;
6169
6170 if (!CondScope.destroyLocals())
6171 return false;
6172 // } End of loop body.
6173
6174 if (!this->jump(CondLabel, S))
6175 return false;
6176 this->fallthrough(EndLabel);
6177 this->emitLabel(EndLabel);
6178
6179 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6180}
6181
6182template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
6183 const Expr *Cond = S->getCond();
6184 const Stmt *Body = S->getBody();
6185
6186 LabelTy StartLabel = this->getLabel();
6187 LabelTy EndLabel = this->getLabel();
6188 LabelTy CondLabel = this->getLabel();
6189 LocalScope<Emitter> WholeLoopScope(this);
6190 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6191
6192 this->fallthrough(StartLabel);
6193 this->emitLabel(StartLabel);
6194
6195 {
6196 LocalScope<Emitter> CondScope(this);
6197 if (!this->visitStmt(Body))
6198 return false;
6199 this->fallthrough(CondLabel);
6200 this->emitLabel(CondLabel);
6201 if (!this->visitBool(Cond))
6202 return false;
6203
6204 if (!CondScope.destroyLocals())
6205 return false;
6206 }
6207 if (!this->jumpTrue(StartLabel, S))
6208 return false;
6209
6210 this->fallthrough(EndLabel);
6211 this->emitLabel(EndLabel);
6212 return WholeLoopScope.destroyLocals();
6213}
6214
6215template <class Emitter>
6217 // for (Init; Cond; Inc) { Body }
6218 const Stmt *Init = S->getInit();
6219 const Expr *Cond = S->getCond();
6220 const Expr *Inc = S->getInc();
6221 const Stmt *Body = S->getBody();
6222
6223 LabelTy EndLabel = this->getLabel();
6224 LabelTy CondLabel = this->getLabel();
6225 LabelTy IncLabel = this->getLabel();
6226
6227 LocalScope<Emitter> WholeLoopScope(this);
6228 if (Init && !this->visitStmt(Init))
6229 return false;
6230
6231 // Start of the loop body {
6232 this->fallthrough(CondLabel);
6233 this->emitLabel(CondLabel);
6234
6235 LocalScope<Emitter> CondScope(this);
6236 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6237 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6238 if (!visitDeclStmt(CondDecl))
6239 return false;
6240 }
6241
6242 if (Cond) {
6243 if (!this->visitBool(Cond))
6244 return false;
6245 if (!this->jumpFalse(EndLabel, S))
6246 return false;
6247 }
6248 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6249 return false;
6250
6251 if (Body && !this->visitStmt(Body))
6252 return false;
6253
6254 this->fallthrough(IncLabel);
6255 this->emitLabel(IncLabel);
6256 if (Inc && !this->discard(Inc))
6257 return false;
6258
6259 if (!CondScope.destroyLocals())
6260 return false;
6261 if (!this->jump(CondLabel, S))
6262 return false;
6263 // } End of loop body.
6264
6265 this->emitLabel(EndLabel);
6266 // If we jumped out of the loop above, we still need to clean up the condition
6267 // scope.
6268 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6269}
6270
6271template <class Emitter>
6273 const Stmt *Init = S->getInit();
6274 const Expr *Cond = S->getCond();
6275 const Expr *Inc = S->getInc();
6276 const Stmt *Body = S->getBody();
6277 const Stmt *BeginStmt = S->getBeginStmt();
6278 const Stmt *RangeStmt = S->getRangeStmt();
6279 const Stmt *EndStmt = S->getEndStmt();
6280
6281 LabelTy EndLabel = this->getLabel();
6282 LabelTy CondLabel = this->getLabel();
6283 LabelTy IncLabel = this->getLabel();
6284 LocalScope<Emitter> WholeLoopScope(this);
6285 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6286
6287 // Emit declarations needed in the loop.
6288 if (Init && !this->visitStmt(Init))
6289 return false;
6290 if (!this->visitStmt(RangeStmt))
6291 return false;
6292 if (!this->visitStmt(BeginStmt))
6293 return false;
6294 if (!this->visitStmt(EndStmt))
6295 return false;
6296
6297 // Now the condition as well as the loop variable assignment.
6298 this->fallthrough(CondLabel);
6299 this->emitLabel(CondLabel);
6300 if (!this->visitBool(Cond))
6301 return false;
6302 if (!this->jumpFalse(EndLabel, S))
6303 return false;
6304
6305 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
6306 return false;
6307
6308 // Body.
6309 {
6310 if (!this->visitStmt(Body))
6311 return false;
6312
6313 this->fallthrough(IncLabel);
6314 this->emitLabel(IncLabel);
6315 if (!this->discard(Inc))
6316 return false;
6317 }
6318
6319 if (!this->jump(CondLabel, S))
6320 return false;
6321
6322 this->fallthrough(EndLabel);
6323 this->emitLabel(EndLabel);
6324 return WholeLoopScope.destroyLocals();
6325}
6326
6327template <class Emitter>
6329 if (LabelInfoStack.empty())
6330 return false;
6331
6332 OptLabelTy TargetLabel = std::nullopt;
6333 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6334 const VariableScope<Emitter> *BreakScope = nullptr;
6335
6336 if (!TargetLoop) {
6337 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6338 if (LI.BreakLabel) {
6339 TargetLabel = *LI.BreakLabel;
6340 BreakScope = LI.BreakOrContinueScope;
6341 break;
6342 }
6343 }
6344 } else {
6345 for (auto LI : LabelInfoStack) {
6346 if (LI.Name == TargetLoop) {
6347 TargetLabel = *LI.BreakLabel;
6348 BreakScope = LI.BreakOrContinueScope;
6349 break;
6350 }
6351 }
6352 }
6353
6354 // Faulty break statement (e.g. label redefined or named loops disabled).
6355 if (!TargetLabel)
6356 return false;
6357
6358 for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope;
6359 C = C->getParent()) {
6360 if (!C->destroyLocals())
6361 return false;
6362 }
6363
6364 return this->jump(*TargetLabel, S);
6365}
6366
6367template <class Emitter>
6369 if (LabelInfoStack.empty())
6370 return false;
6371
6372 OptLabelTy TargetLabel = std::nullopt;
6373 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6374 const VariableScope<Emitter> *ContinueScope = nullptr;
6375
6376 if (!TargetLoop) {
6377 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6378 if (LI.ContinueLabel) {
6379 TargetLabel = *LI.ContinueLabel;
6380 ContinueScope = LI.BreakOrContinueScope;
6381 break;
6382 }
6383 }
6384 } else {
6385 for (auto LI : LabelInfoStack) {
6386 if (LI.Name == TargetLoop) {
6387 TargetLabel = *LI.ContinueLabel;
6388 ContinueScope = LI.BreakOrContinueScope;
6389 break;
6390 }
6391 }
6392 }
6393 assert(TargetLabel);
6394
6395 for (VariableScope<Emitter> *C = VarScope; C != ContinueScope;
6396 C = C->getParent()) {
6397 if (!C->destroyLocals())
6398 return false;
6399 }
6400
6401 return this->jump(*TargetLabel, S);
6402}
6403
6404template <class Emitter>
6406 const Expr *Cond = S->getCond();
6407 if (Cond->containsErrors())
6408 return false;
6409
6410 PrimType CondT = this->classifyPrim(Cond->getType());
6411 LocalScope<Emitter> LS(this);
6412
6413 LabelTy EndLabel = this->getLabel();
6414 UnsignedOrNone DefaultLabel = std::nullopt;
6415 unsigned CondVar =
6416 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
6417
6418 if (const auto *CondInit = S->getInit())
6419 if (!visitStmt(CondInit))
6420 return false;
6421
6422 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
6423 if (!visitDeclStmt(CondDecl))
6424 return false;
6425
6426 // Initialize condition variable.
6427 if (!this->visit(Cond))
6428 return false;
6429 if (!this->emitSetLocal(CondT, CondVar, S))
6430 return false;
6431
6432 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6433 return false;
6434
6436 // Create labels and comparison ops for all case statements.
6437 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
6438 SC = SC->getNextSwitchCase()) {
6439 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
6440 CaseLabels[SC] = this->getLabel();
6441
6442 if (CS->caseStmtIsGNURange()) {
6443 LabelTy EndOfRangeCheck = this->getLabel();
6444 const Expr *Low = CS->getLHS();
6445 const Expr *High = CS->getRHS();
6446 if (Low->isValueDependent() || High->isValueDependent())
6447 return false;
6448
6449 if (!this->emitGetLocal(CondT, CondVar, CS))
6450 return false;
6451 if (!this->visit(Low))
6452 return false;
6453 PrimType LT = this->classifyPrim(Low->getType());
6454 if (!this->emitGE(LT, S))
6455 return false;
6456 if (!this->jumpFalse(EndOfRangeCheck, S))
6457 return false;
6458
6459 if (!this->emitGetLocal(CondT, CondVar, CS))
6460 return false;
6461 if (!this->visit(High))
6462 return false;
6463 PrimType HT = this->classifyPrim(High->getType());
6464 if (!this->emitLE(HT, S))
6465 return false;
6466 if (!this->jumpTrue(CaseLabels[CS], S))
6467 return false;
6468 this->emitLabel(EndOfRangeCheck);
6469 continue;
6470 }
6471
6472 const Expr *Value = CS->getLHS();
6473 if (Value->isValueDependent())
6474 return false;
6475 PrimType ValueT = this->classifyPrim(Value->getType());
6476
6477 // Compare the case statement's value to the switch condition.
6478 if (!this->emitGetLocal(CondT, CondVar, CS))
6479 return false;
6480 if (!this->visit(Value))
6481 return false;
6482
6483 // Compare and jump to the case label.
6484 if (!this->emitEQ(ValueT, S))
6485 return false;
6486 if (!this->jumpTrue(CaseLabels[CS], S))
6487 return false;
6488 } else {
6489 assert(!DefaultLabel);
6490 DefaultLabel = this->getLabel();
6491 }
6492 }
6493
6494 // If none of the conditions above were true, fall through to the default
6495 // statement or jump after the switch statement.
6496 if (DefaultLabel) {
6497 if (!this->jump(*DefaultLabel, S))
6498 return false;
6499 } else {
6500 if (!this->jump(EndLabel, S))
6501 return false;
6502 }
6503
6504 SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel,
6505 DefaultLabel);
6506 if (!this->visitStmt(S->getBody()))
6507 return false;
6508 this->fallthrough(EndLabel);
6509 this->emitLabel(EndLabel);
6510
6511 return LS.destroyLocals();
6512}
6513
6514template <class Emitter>
6516 this->fallthrough(CaseLabels[S]);
6517 this->emitLabel(CaseLabels[S]);
6518 return this->visitStmt(S->getSubStmt());
6519}
6520
6521template <class Emitter>
6523 if (LabelInfoStack.empty())
6524 return false;
6525
6526 LabelTy DefaultLabel;
6527 for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) {
6528 if (LI.DefaultLabel) {
6529 DefaultLabel = *LI.DefaultLabel;
6530 break;
6531 }
6532 }
6533
6534 this->emitLabel(DefaultLabel);
6535 return this->visitStmt(S->getSubStmt());
6536}
6537
6538template <class Emitter>
6540 const Stmt *SubStmt = S->getSubStmt();
6541
6542 bool IsMSVCConstexprAttr = isa<ReturnStmt>(SubStmt) &&
6544
6545 if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S))
6546 return false;
6547
6548 if (this->Ctx.getLangOpts().CXXAssumptions &&
6549 !this->Ctx.getLangOpts().MSVCCompat) {
6550 for (const Attr *A : S->getAttrs()) {
6551 auto *AA = dyn_cast<CXXAssumeAttr>(A);
6552 if (!AA)
6553 continue;
6554
6555 assert(isa<NullStmt>(SubStmt));
6556
6557 const Expr *Assumption = AA->getAssumption();
6558 if (Assumption->isValueDependent())
6559 return false;
6560
6561 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
6562 continue;
6563
6564 // Evaluate assumption.
6565 if (!this->visitBool(Assumption))
6566 return false;
6567
6568 if (!this->emitAssume(Assumption))
6569 return false;
6570 }
6571 }
6572
6573 // Ignore other attributes.
6574 if (!this->visitStmt(SubStmt))
6575 return false;
6576
6577 if (IsMSVCConstexprAttr)
6578 return this->emitPopMSVCCE(S);
6579 return true;
6580}
6581
6582template <class Emitter>
6584 // Ignore all handlers.
6585 return this->visitStmt(S->getTryBlock());
6586}
6587
6588template <class Emitter>
6589bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
6590 assert(MD->isLambdaStaticInvoker());
6591 assert(MD->hasBody());
6592 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
6593
6594 const CXXRecordDecl *ClosureClass = MD->getParent();
6595 const FunctionDecl *LambdaCallOp;
6596 assert(ClosureClass->captures().empty());
6597 if (ClosureClass->isGenericLambda()) {
6598 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6599 assert(MD->isFunctionTemplateSpecialization() &&
6600 "A generic lambda's static-invoker function must be a "
6601 "template specialization");
6603 FunctionTemplateDecl *CallOpTemplate =
6604 LambdaCallOp->getDescribedFunctionTemplate();
6605 void *InsertPos = nullptr;
6606 const FunctionDecl *CorrespondingCallOpSpecialization =
6607 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
6608 assert(CorrespondingCallOpSpecialization);
6609 LambdaCallOp = CorrespondingCallOpSpecialization;
6610 } else {
6611 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6612 }
6613 assert(ClosureClass->captures().empty());
6614 const Function *Func = this->getFunction(LambdaCallOp);
6615 if (!Func)
6616 return false;
6617 assert(Func->hasThisPointer());
6618 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
6619
6620 if (Func->hasRVO()) {
6621 if (!this->emitRVOPtr(MD))
6622 return false;
6623 }
6624
6625 // The lambda call operator needs an instance pointer, but we don't have
6626 // one here, and we don't need one either because the lambda cannot have
6627 // any captures, as verified above. Emit a null pointer. This is then
6628 // special-cased when interpreting to not emit any misleading diagnostics.
6629 if (!this->emitNullPtr(0, nullptr, MD))
6630 return false;
6631
6632 // Forward all arguments from the static invoker to the lambda call operator.
6633 for (const ParmVarDecl *PVD : MD->parameters()) {
6634 auto It = this->Params.find(PVD);
6635 assert(It != this->Params.end());
6636
6637 // We do the lvalue-to-rvalue conversion manually here, so no need
6638 // to care about references.
6639 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
6640 if (!this->emitGetParam(ParamType, It->second.Index, MD))
6641 return false;
6642 }
6643
6644 if (!this->emitCall(Func, 0, LambdaCallOp))
6645 return false;
6646
6647 this->emitCleanup();
6648 if (ReturnType)
6649 return this->emitRet(*ReturnType, MD);
6650
6651 // Nothing to do, since we emitted the RVO pointer above.
6652 return this->emitRetVoid(MD);
6653}
6654
6655template <class Emitter>
6656bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
6657 if (Ctx.getLangOpts().CPlusPlus23)
6658 return true;
6659
6660 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
6661 return true;
6662
6663 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
6664}
6665
6667 const Expr *InitExpr = Init->getInit();
6668
6669 if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
6670 !isa<CXXConstructExpr>(InitExpr))
6671 return true;
6672
6673 if (const auto *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
6674 const CXXConstructorDecl *Ctor = CE->getConstructor();
6675 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
6676 Ctor->isTrivial())
6677 return true;
6678 }
6679
6680 return false;
6681}
6682
6683template <class Emitter>
6684bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
6685 assert(!ReturnType);
6686
6687 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
6688 const Expr *InitExpr,
6689 bool Activate = false) -> bool {
6690 // We don't know what to do with these, so just return false.
6691 if (InitExpr->getType().isNull())
6692 return false;
6693
6694 if (OptPrimType T = this->classify(InitExpr)) {
6695 if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
6696 return false;
6697
6698 if (!this->visit(InitExpr))
6699 return false;
6700
6701 if (F->isBitField())
6702 return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(),
6703 InitExpr);
6704 return this->emitInitThisField(*T, FieldOffset, InitExpr);
6705 }
6706 // Non-primitive case. Get a pointer to the field-to-initialize
6707 // on the stack and call visitInitialzer() for it.
6708 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
6709 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
6710 return false;
6711
6712 if (Activate && !this->emitActivate(InitExpr))
6713 return false;
6714
6715 if (!this->visitInitializer(InitExpr))
6716 return false;
6717
6718 return this->emitFinishInitPop(InitExpr);
6719 };
6720
6721 const RecordDecl *RD = Ctor->getParent();
6722 const Record *R = this->getRecord(RD);
6723 if (!R)
6724 return false;
6725 bool IsUnion = R->isUnion();
6726
6727 if (IsUnion && Ctor->isCopyOrMoveConstructor()) {
6729
6730 if (R->getNumFields() == 0)
6731 return this->emitRetVoid(Ctor);
6732 // union copy and move ctors are special.
6733 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
6734 if (!this->emitThis(Ctor))
6735 return false;
6736
6737 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor))
6738 return false;
6739
6740 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
6741 this->emitRetVoid(Ctor);
6742 }
6743
6745 for (const auto *Init : Ctor->inits()) {
6746 // Scope needed for the initializers.
6747 LocalScope<Emitter> Scope(this, ScopeKind::FullExpression);
6748
6749 const Expr *InitExpr = Init->getInit();
6750 if (const FieldDecl *Member = Init->getMember()) {
6751 const Record::Field *F = R->getField(Member);
6752
6755 if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
6756 return false;
6757 } else if (const Type *Base = Init->getBaseClass()) {
6758 const auto *BaseDecl = Base->getAsCXXRecordDecl();
6759 assert(BaseDecl);
6760
6761 if (Init->isBaseVirtual()) {
6762 assert(R->getVirtualBase(BaseDecl));
6763 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
6764 return false;
6765
6766 } else {
6767 // Base class initializer.
6768 // Get This Base and call initializer on it.
6769 const Record::Base *B = R->getBase(BaseDecl);
6770 assert(B);
6771 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
6772 return false;
6773 }
6774
6775 if (IsUnion && !this->emitActivate(InitExpr))
6776 return false;
6777
6778 if (!this->visitInitializer(InitExpr))
6779 return false;
6780 if (!this->emitFinishInitPop(InitExpr))
6781 return false;
6782 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
6785 assert(IFD->getChainingSize() >= 2);
6786
6787 unsigned NestedFieldOffset = 0;
6788 const Record::Field *NestedField = nullptr;
6789 for (const NamedDecl *ND : IFD->chain()) {
6790 const auto *FD = cast<FieldDecl>(ND);
6791 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6792 assert(FieldRecord);
6793
6794 NestedField = FieldRecord->getField(FD);
6795 assert(NestedField);
6796 IsUnion = IsUnion || FieldRecord->isUnion();
6797
6798 NestedFieldOffset += NestedField->Offset;
6799 }
6800 assert(NestedField);
6801
6802 unsigned FirstLinkOffset =
6803 R->getField(cast<FieldDecl>(IFD->chain()[0]))->Offset;
6804 InitLinkScope<Emitter> ILS(this, InitLink::Field(FirstLinkOffset));
6806 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
6807 IsUnion))
6808 return false;
6809
6810 // Mark all chain links as initialized.
6811 unsigned InitFieldOffset = 0;
6812 for (const NamedDecl *ND : IFD->chain().drop_back()) {
6813 const auto *FD = cast<FieldDecl>(ND);
6814 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6815 assert(FieldRecord);
6816 NestedField = FieldRecord->getField(FD);
6817 InitFieldOffset += NestedField->Offset;
6818 assert(NestedField);
6819 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
6820 return false;
6821 if (!this->emitFinishInitPop(InitExpr))
6822 return false;
6823 }
6824
6825 } else {
6826 assert(Init->isDelegatingInitializer());
6827 if (!this->emitThis(InitExpr))
6828 return false;
6829 if (!this->visitInitializer(Init->getInit()))
6830 return false;
6831 if (!this->emitPopPtr(InitExpr))
6832 return false;
6833 }
6834
6835 if (!Scope.destroyLocals())
6836 return false;
6837 }
6838
6839 if (const Stmt *Body = Ctor->getBody()) {
6840 // Only emit the CtorCheck op for non-empty CompoundStmt bodies.
6841 // For non-CompoundStmts, always assume they are non-empty and emit it.
6842 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
6843 if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{}))
6844 return false;
6845 } else {
6846 if (!this->emitCtorCheck(SourceInfo{}))
6847 return false;
6848 }
6849
6850 if (!visitStmt(Body))
6851 return false;
6852 }
6853
6854 return this->emitRetVoid(SourceInfo{});
6855}
6856
6857template <class Emitter>
6858bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
6859 const RecordDecl *RD = Dtor->getParent();
6860 const Record *R = this->getRecord(RD);
6861 if (!R)
6862 return false;
6863
6864 if (!Dtor->isTrivial() && Dtor->getBody()) {
6865 if (!this->visitStmt(Dtor->getBody()))
6866 return false;
6867 }
6868
6869 if (!this->emitThis(Dtor))
6870 return false;
6871
6872 if (!this->emitCheckDestruction(Dtor))
6873 return false;
6874
6875 assert(R);
6876 if (!R->isUnion()) {
6877
6879 // First, destroy all fields.
6880 for (const Record::Field &Field : llvm::reverse(R->fields())) {
6881 const Descriptor *D = Field.Desc;
6882 if (D->hasTrivialDtor())
6883 continue;
6884 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
6885 return false;
6886 if (!this->emitDestructionPop(D, SourceInfo{}))
6887 return false;
6888 }
6889 }
6890
6891 for (const Record::Base &Base : llvm::reverse(R->bases())) {
6892 if (Base.R->hasTrivialDtor())
6893 continue;
6894 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
6895 return false;
6896 if (!this->emitRecordDestructionPop(Base.R, {}))
6897 return false;
6898 }
6899
6900 // FIXME: Virtual bases.
6901 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
6902}
6903
6904template <class Emitter>
6905bool Compiler<Emitter>::compileUnionAssignmentOperator(
6906 const CXXMethodDecl *MD) {
6907 if (!this->emitThis(MD))
6908 return false;
6909
6910 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD))
6911 return false;
6912
6913 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
6914}
6915
6916template <class Emitter>
6918 // Classify the return type.
6919 ReturnType = this->classify(F->getReturnType());
6920
6921 this->CompilingFunction = F;
6922
6923 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
6924 return this->compileConstructor(Ctor);
6925 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
6926 return this->compileDestructor(Dtor);
6927
6928 // Emit custom code if this is a lambda static invoker.
6929 if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
6930 const RecordDecl *RD = MD->getParent();
6931
6932 if (RD->isUnion() &&
6934 return this->compileUnionAssignmentOperator(MD);
6935
6936 if (MD->isLambdaStaticInvoker())
6937 return this->emitLambdaStaticInvokerBody(MD);
6938 }
6939
6940 // Regular functions.
6941 if (const auto *Body = F->getBody())
6942 if (!visitStmt(Body))
6943 return false;
6944
6945 // Emit a guard return to protect against a code path missing one.
6946 if (F->getReturnType()->isVoidType())
6947 return this->emitRetVoid(SourceInfo{});
6948 return this->emitNoRet(SourceInfo{});
6949}
6950
6951static uint32_t getBitWidth(const Expr *E) {
6952 assert(E->refersToBitField());
6953 const auto *ME = cast<MemberExpr>(E);
6954 const auto *FD = cast<FieldDecl>(ME->getMemberDecl());
6955 return FD->getBitWidthValue();
6956}
6957
6958template <class Emitter>
6960 const Expr *SubExpr = E->getSubExpr();
6961 if (SubExpr->getType()->isAnyComplexType())
6962 return this->VisitComplexUnaryOperator(E);
6963 if (SubExpr->getType()->isVectorType())
6964 return this->VisitVectorUnaryOperator(E);
6965 if (SubExpr->getType()->isFixedPointType())
6966 return this->VisitFixedPointUnaryOperator(E);
6967 OptPrimType T = classify(SubExpr->getType());
6968
6969 switch (E->getOpcode()) {
6970 case UO_PostInc: { // x++
6971 if (!Ctx.getLangOpts().CPlusPlus14)
6972 return this->emitInvalid(E);
6973 if (!T)
6974 return this->emitError(E);
6975
6976 if (!this->visit(SubExpr))
6977 return false;
6978
6979 if (T == PT_Ptr) {
6980 if (!this->emitIncPtr(E))
6981 return false;
6982
6983 return DiscardResult ? this->emitPopPtr(E) : true;
6984 }
6985
6986 if (T == PT_Float)
6987 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
6988 : this->emitIncf(getFPOptions(E), E);
6989
6990 if (SubExpr->refersToBitField())
6991 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
6992 getBitWidth(SubExpr), E)
6993 : this->emitIncBitfield(*T, E->canOverflow(),
6994 getBitWidth(SubExpr), E);
6995
6996 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
6997 : this->emitInc(*T, E->canOverflow(), E);
6998 }
6999 case UO_PostDec: { // x--
7000 if (!Ctx.getLangOpts().CPlusPlus14)
7001 return this->emitInvalid(E);
7002 if (!T)
7003 return this->emitError(E);
7004
7005 if (!this->visit(SubExpr))
7006 return false;
7007
7008 if (T == PT_Ptr) {
7009 if (!this->emitDecPtr(E))
7010 return false;
7011
7012 return DiscardResult ? this->emitPopPtr(E) : true;
7013 }
7014
7015 if (T == PT_Float)
7016 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
7017 : this->emitDecf(getFPOptions(E), E);
7018
7019 if (SubExpr->refersToBitField()) {
7020 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7021 getBitWidth(SubExpr), E)
7022 : this->emitDecBitfield(*T, E->canOverflow(),
7023 getBitWidth(SubExpr), E);
7024 }
7025
7026 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
7027 : this->emitDec(*T, E->canOverflow(), E);
7028 }
7029 case UO_PreInc: { // ++x
7030 if (!Ctx.getLangOpts().CPlusPlus14)
7031 return this->emitInvalid(E);
7032 if (!T)
7033 return this->emitError(E);
7034
7035 if (!this->visit(SubExpr))
7036 return false;
7037
7038 if (T == PT_Ptr) {
7039 if (!this->emitLoadPtr(E))
7040 return false;
7041 if (!this->emitConstUint8(1, E))
7042 return false;
7043 if (!this->emitAddOffsetUint8(E))
7044 return false;
7045 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7046 }
7047
7048 // Post-inc and pre-inc are the same if the value is to be discarded.
7049 if (DiscardResult) {
7050 if (T == PT_Float)
7051 return this->emitIncfPop(getFPOptions(E), E);
7052 if (SubExpr->refersToBitField())
7053 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7054 getBitWidth(SubExpr), E)
7055 : this->emitIncBitfield(*T, E->canOverflow(),
7056 getBitWidth(SubExpr), E);
7057 return this->emitIncPop(*T, E->canOverflow(), E);
7058 }
7059
7060 if (T == PT_Float) {
7061 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7062 if (!this->emitLoadFloat(E))
7063 return false;
7064 APFloat F(TargetSemantics, 1);
7065 if (!this->emitFloat(F, E))
7066 return false;
7067
7068 if (!this->emitAddf(getFPOptions(E), E))
7069 return false;
7070 if (!this->emitStoreFloat(E))
7071 return false;
7072 } else if (SubExpr->refersToBitField()) {
7073 assert(isIntegerOrBoolType(*T));
7074 if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7075 E))
7076 return false;
7077 } else {
7078 assert(isIntegerOrBoolType(*T));
7079 if (!this->emitPreInc(*T, E->canOverflow(), E))
7080 return false;
7081 }
7082 return E->isGLValue() || this->emitLoadPop(*T, E);
7083 }
7084 case UO_PreDec: { // --x
7085 if (!Ctx.getLangOpts().CPlusPlus14)
7086 return this->emitInvalid(E);
7087 if (!T)
7088 return this->emitError(E);
7089
7090 if (!this->visit(SubExpr))
7091 return false;
7092
7093 if (T == PT_Ptr) {
7094 if (!this->emitLoadPtr(E))
7095 return false;
7096 if (!this->emitConstUint8(1, E))
7097 return false;
7098 if (!this->emitSubOffsetUint8(E))
7099 return false;
7100 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7101 }
7102
7103 // Post-dec and pre-dec are the same if the value is to be discarded.
7104 if (DiscardResult) {
7105 if (T == PT_Float)
7106 return this->emitDecfPop(getFPOptions(E), E);
7107 if (SubExpr->refersToBitField())
7108 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7109 getBitWidth(SubExpr), E)
7110 : this->emitDecBitfield(*T, E->canOverflow(),
7111 getBitWidth(SubExpr), E);
7112 return this->emitDecPop(*T, E->canOverflow(), E);
7113 }
7114
7115 if (T == PT_Float) {
7116 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7117 if (!this->emitLoadFloat(E))
7118 return false;
7119 APFloat F(TargetSemantics, 1);
7120 if (!this->emitFloat(F, E))
7121 return false;
7122
7123 if (!this->emitSubf(getFPOptions(E), E))
7124 return false;
7125 if (!this->emitStoreFloat(E))
7126 return false;
7127 } else if (SubExpr->refersToBitField()) {
7128 assert(isIntegerOrBoolType(*T));
7129 if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7130 E))
7131 return false;
7132 } else {
7133 assert(isIntegerOrBoolType(*T));
7134 if (!this->emitPreDec(*T, E->canOverflow(), E))
7135 return false;
7136 }
7137 return E->isGLValue() || this->emitLoadPop(*T, E);
7138 }
7139 case UO_LNot: // !x
7140 if (!T)
7141 return this->emitError(E);
7142
7143 if (DiscardResult)
7144 return this->discard(SubExpr);
7145
7146 if (!this->visitBool(SubExpr))
7147 return false;
7148
7149 if (!this->emitInv(E))
7150 return false;
7151
7152 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7153 return this->emitCast(PT_Bool, ET, E);
7154 return true;
7155 case UO_Minus: // -x
7156 if (!T)
7157 return this->emitError(E);
7158
7159 if (!this->visit(SubExpr))
7160 return false;
7161 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
7162 case UO_Plus: // +x
7163 if (!T)
7164 return this->emitError(E);
7165
7166 if (!this->visit(SubExpr)) // noop
7167 return false;
7168 return DiscardResult ? this->emitPop(*T, E) : true;
7169 case UO_AddrOf: // &x
7170 if (E->getType()->isMemberPointerType()) {
7171 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
7172 // member can be formed.
7173 if (DiscardResult)
7174 return true;
7175 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
7176 }
7177 // We should already have a pointer when we get here.
7178 return this->delegate(SubExpr);
7179 case UO_Deref: // *x
7180 if (DiscardResult)
7181 return this->discard(SubExpr);
7182
7183 if (!this->visit(SubExpr))
7184 return false;
7185
7186 if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
7187 return false;
7188
7189 if (classifyPrim(SubExpr) == PT_Ptr)
7190 return this->emitNarrowPtr(E);
7191 return true;
7192
7193 case UO_Not: // ~x
7194 if (!T)
7195 return this->emitError(E);
7196
7197 if (!this->visit(SubExpr))
7198 return false;
7199 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
7200 case UO_Real: // __real x
7201 if (!T)
7202 return false;
7203 return this->delegate(SubExpr);
7204 case UO_Imag: { // __imag x
7205 if (!T)
7206 return false;
7207 if (!this->discard(SubExpr))
7208 return false;
7209 return DiscardResult
7210 ? true
7211 : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
7212 }
7213 case UO_Extension:
7214 return this->delegate(SubExpr);
7215 case UO_Coawait:
7216 assert(false && "Unhandled opcode");
7217 }
7218
7219 return false;
7220}
7221
7222template <class Emitter>
7224 const Expr *SubExpr = E->getSubExpr();
7225 assert(SubExpr->getType()->isAnyComplexType());
7226
7227 if (DiscardResult)
7228 return this->discard(SubExpr);
7229
7230 OptPrimType ResT = classify(E);
7231 auto prepareResult = [=]() -> bool {
7232 if (!ResT && !Initializing) {
7233 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7234 if (!LocalIndex)
7235 return false;
7236 return this->emitGetPtrLocal(*LocalIndex, E);
7237 }
7238
7239 return true;
7240 };
7241
7242 // The offset of the temporary, if we created one.
7243 unsigned SubExprOffset = ~0u;
7244 auto createTemp = [=, &SubExprOffset]() -> bool {
7245 SubExprOffset =
7246 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7247 if (!this->visit(SubExpr))
7248 return false;
7249 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
7250 };
7251
7252 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
7253 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7254 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7255 return false;
7256 return this->emitArrayElemPop(ElemT, Index, E);
7257 };
7258
7259 switch (E->getOpcode()) {
7260 case UO_Minus: // -x
7261 if (!prepareResult())
7262 return false;
7263 if (!createTemp())
7264 return false;
7265 for (unsigned I = 0; I != 2; ++I) {
7266 if (!getElem(SubExprOffset, I))
7267 return false;
7268 if (!this->emitNeg(ElemT, E))
7269 return false;
7270 if (!this->emitInitElem(ElemT, I, E))
7271 return false;
7272 }
7273 break;
7274
7275 case UO_Plus: // +x
7276 case UO_AddrOf: // &x
7277 case UO_Deref: // *x
7278 return this->delegate(SubExpr);
7279
7280 case UO_LNot:
7281 if (!this->visit(SubExpr))
7282 return false;
7283 if (!this->emitComplexBoolCast(SubExpr))
7284 return false;
7285 if (!this->emitInv(E))
7286 return false;
7287 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7288 return this->emitCast(PT_Bool, ET, E);
7289 return true;
7290
7291 case UO_Real:
7292 return this->emitComplexReal(SubExpr);
7293
7294 case UO_Imag:
7295 if (!this->visit(SubExpr))
7296 return false;
7297
7298 if (SubExpr->isLValue()) {
7299 if (!this->emitConstUint8(1, E))
7300 return false;
7301 return this->emitArrayElemPtrPopUint8(E);
7302 }
7303
7304 // Since our _Complex implementation does not map to a primitive type,
7305 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
7306 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
7307
7308 case UO_Not: // ~x
7309 if (!this->delegate(SubExpr))
7310 return false;
7311 // Negate the imaginary component.
7312 if (!this->emitArrayElem(ElemT, 1, E))
7313 return false;
7314 if (!this->emitNeg(ElemT, E))
7315 return false;
7316 if (!this->emitInitElem(ElemT, 1, E))
7317 return false;
7318 return DiscardResult ? this->emitPopPtr(E) : true;
7319
7320 case UO_Extension:
7321 return this->delegate(SubExpr);
7322
7323 default:
7324 return this->emitInvalid(E);
7325 }
7326
7327 return true;
7328}
7329
7330template <class Emitter>
7332 const Expr *SubExpr = E->getSubExpr();
7333 assert(SubExpr->getType()->isVectorType());
7334
7335 if (DiscardResult)
7336 return this->discard(SubExpr);
7337
7338 auto UnaryOp = E->getOpcode();
7339 if (UnaryOp == UO_Extension)
7340 return this->delegate(SubExpr);
7341
7342 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
7343 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
7344 return this->emitInvalid(E);
7345
7346 // Nothing to do here.
7347 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
7348 return this->delegate(SubExpr);
7349
7350 if (!Initializing) {
7351 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7352 if (!LocalIndex)
7353 return false;
7354 if (!this->emitGetPtrLocal(*LocalIndex, E))
7355 return false;
7356 }
7357
7358 // The offset of the temporary, if we created one.
7359 unsigned SubExprOffset =
7360 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7361 if (!this->visit(SubExpr))
7362 return false;
7363 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
7364 return false;
7365
7366 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
7367 PrimType ElemT = classifyVectorElementType(SubExpr->getType());
7368 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7369 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7370 return false;
7371 return this->emitArrayElemPop(ElemT, Index, E);
7372 };
7373
7374 switch (UnaryOp) {
7375 case UO_Minus:
7376 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7377 if (!getElem(SubExprOffset, I))
7378 return false;
7379 if (!this->emitNeg(ElemT, E))
7380 return false;
7381 if (!this->emitInitElem(ElemT, I, E))
7382 return false;
7383 }
7384 break;
7385 case UO_LNot: { // !x
7386 // In C++, the logic operators !, &&, || are available for vectors. !v is
7387 // equivalent to v == 0.
7388 //
7389 // The result of the comparison is a vector of the same width and number of
7390 // elements as the comparison operands with a signed integral element type.
7391 //
7392 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
7393 QualType ResultVecTy = E->getType();
7394 PrimType ResultVecElemT =
7395 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
7396 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7397 if (!getElem(SubExprOffset, I))
7398 return false;
7399 // operator ! on vectors returns -1 for 'truth', so negate it.
7400 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
7401 return false;
7402 if (!this->emitInv(E))
7403 return false;
7404 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
7405 return false;
7406 if (!this->emitNeg(ElemT, E))
7407 return false;
7408 if (ElemT != ResultVecElemT &&
7409 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
7410 return false;
7411 if (!this->emitInitElem(ResultVecElemT, I, E))
7412 return false;
7413 }
7414 break;
7415 }
7416 case UO_Not: // ~x
7417 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7418 if (!getElem(SubExprOffset, I))
7419 return false;
7420 if (ElemT == PT_Bool) {
7421 if (!this->emitInv(E))
7422 return false;
7423 } else {
7424 if (!this->emitComp(ElemT, E))
7425 return false;
7426 }
7427 if (!this->emitInitElem(ElemT, I, E))
7428 return false;
7429 }
7430 break;
7431 default:
7432 llvm_unreachable("Unsupported unary operators should be handled up front");
7433 }
7434 return true;
7435}
7436
7437template <class Emitter>
7439 if (DiscardResult)
7440 return true;
7441
7442 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7443 return this->emitConst(ECD->getInitVal(), E);
7444 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
7445 const Function *F = getFunction(FuncDecl);
7446 return F && this->emitGetFnPtr(F, E);
7447 }
7448 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
7449 if (UnsignedOrNone Index = P.getOrCreateGlobal(D)) {
7450 if (!this->emitGetPtrGlobal(*Index, E))
7451 return false;
7452 if (OptPrimType T = classify(E->getType())) {
7453 if (!this->visitAPValue(TPOD->getValue(), *T, E))
7454 return false;
7455 return this->emitInitGlobal(*T, *Index, E);
7456 }
7457 if (!this->visitAPValueInitializer(TPOD->getValue(), E, TPOD->getType()))
7458 return false;
7459 return this->emitFinishInit(E);
7460 }
7461 return false;
7462 }
7463
7464 // References are implemented via pointers, so when we see a DeclRefExpr
7465 // pointing to a reference, we need to get its value directly (i.e. the
7466 // pointer to the actual value) instead of a pointer to the pointer to the
7467 // value.
7468 QualType DeclType = D->getType();
7469 bool IsReference = DeclType->isReferenceType();
7470
7471 // Function parameters.
7472 // Note that it's important to check them first since we might have a local
7473 // variable created for a ParmVarDecl as well.
7474 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
7475 if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
7476 !DeclType->isIntegralOrEnumerationType()) {
7477 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7478 /*InitializerFailed=*/false, E);
7479 }
7480 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
7481 if (IsReference || !It->second.IsPtr)
7482 return this->emitGetParam(classifyPrim(E), It->second.Index, E);
7483
7484 return this->emitGetPtrParam(It->second.Index, E);
7485 }
7486
7487 if (!Ctx.getLangOpts().CPlusPlus23 && IsReference)
7488 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7489 /*InitializerFailed=*/false, E);
7490 }
7491 // Local variables.
7492 if (auto It = Locals.find(D); It != Locals.end()) {
7493 const unsigned Offset = It->second.Offset;
7494 if (IsReference) {
7495 assert(classifyPrim(E) == PT_Ptr);
7496 return this->emitGetRefLocal(Offset, E);
7497 }
7498 return this->emitGetPtrLocal(Offset, E);
7499 }
7500 // Global variables.
7501 if (auto GlobalIndex = P.getGlobal(D)) {
7502 if (IsReference) {
7503 if (!Ctx.getLangOpts().CPlusPlus11)
7504 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
7505 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
7506 }
7507
7508 return this->emitGetPtrGlobal(*GlobalIndex, E);
7509 }
7510
7511 // In case we need to re-visit a declaration.
7512 auto revisit = [&](const VarDecl *VD,
7513 bool IsConstexprUnknown = true) -> bool {
7515 IsConstexprUnknown);
7516 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
7517 return false;
7518 auto VarState = this->visitDecl(VD);
7519
7520 if (!this->emitPopCC(E))
7521 return false;
7522
7523 if (VarState.notCreated())
7524 return true;
7525 if (!VarState)
7526 return false;
7527 // Retry.
7528 return this->visitDeclRef(D, E);
7529 };
7530
7531 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
7532 // Lambda captures.
7533 if (auto It = this->LambdaCaptures.find(D);
7534 It != this->LambdaCaptures.end()) {
7535 auto [Offset, IsPtr] = It->second;
7536
7537 if (IsPtr)
7538 return this->emitGetThisFieldPtr(Offset, E);
7539 return this->emitGetPtrThisField(Offset, E);
7540 }
7541 }
7542
7543 if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
7544 DRE && DRE->refersToEnclosingVariableOrCapture()) {
7545 if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
7546 return revisit(VD);
7547 }
7548
7549 if (const auto *BD = dyn_cast<BindingDecl>(D))
7550 return this->visit(BD->getBinding());
7551
7552 // Avoid infinite recursion.
7553 if (D == InitializingDecl)
7554 return this->emitDummyPtr(D, E);
7555
7556 // Try to lazily visit (or emit dummy pointers for) declarations
7557 // we haven't seen yet.
7558 const auto *VD = dyn_cast<VarDecl>(D);
7559 if (!VD)
7560 return this->emitError(E);
7561
7562 // For C.
7563 if (!Ctx.getLangOpts().CPlusPlus) {
7564 if (VD->getAnyInitializer() && DeclType.isConstant(Ctx.getASTContext()) &&
7565 !VD->isWeak())
7566 return revisit(VD, /*IsConstexprUnknown=*/false);
7567 return this->emitDummyPtr(D, E);
7568 }
7569
7570 // ... and C++.
7571 const auto typeShouldBeVisited = [&](QualType T) -> bool {
7572 if (T.isConstant(Ctx.getASTContext()))
7573 return true;
7574 return T->isReferenceType();
7575 };
7576
7577 if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
7578 typeShouldBeVisited(DeclType)) {
7579 if (const Expr *Init = VD->getAnyInitializer();
7580 Init && !Init->isValueDependent()) {
7581 // Whether or not the evaluation is successul doesn't really matter
7582 // here -- we will create a global variable in any case, and that
7583 // will have the state of initializer evaluation attached.
7584 APValue V;
7586 (void)Init->EvaluateAsInitializer(V, Ctx.getASTContext(), VD, Notes,
7587 true);
7588 return this->visitDeclRef(D, E);
7589 }
7590 return revisit(VD);
7591 }
7592
7593 // FIXME: The evaluateValue() check here is a little ridiculous, since
7594 // it will ultimately call into Context::evaluateAsInitializer(). In
7595 // other words, we're evaluating the initializer, just to know if we can
7596 // evaluate the initializer.
7597 if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() &&
7598 !VD->getInit()->isValueDependent()) {
7599 if (VD->evaluateValue()) {
7600 bool IsConstexprUnknown = !DeclType.isConstant(Ctx.getASTContext()) &&
7601 !DeclType->isReferenceType();
7602 // Revisit the variable declaration, but make sure it's associated with a
7603 // different evaluation, so e.g. mutable reads don't work on it.
7604 EvalIDScope _(Ctx);
7605 return revisit(VD, IsConstexprUnknown);
7606 } else if (Ctx.getLangOpts().CPlusPlus23 && IsReference)
7607 return revisit(VD, /*IsConstexprUnknown=*/true);
7608
7609 if (IsReference)
7610 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7611 /*InitializerFailed=*/true, E);
7612 }
7613
7614 return this->emitDummyPtr(D, E);
7615}
7616
7617template <class Emitter>
7619 const auto *D = E->getDecl();
7620 return this->visitDeclRef(D, E);
7621}
7622
7623template <class Emitter> bool Compiler<Emitter>::emitCleanup() {
7624 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) {
7625 if (!C->destroyLocals())
7626 return false;
7627 }
7628 return true;
7629}
7630
7631template <class Emitter>
7632unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
7633 const QualType DerivedType) {
7634 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
7635 if (const auto *R = Ty->getPointeeCXXRecordDecl())
7636 return R;
7637 return Ty->getAsCXXRecordDecl();
7638 };
7639 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
7640 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
7641
7642 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
7643}
7644
7645/// Emit casts from a PrimType to another PrimType.
7646template <class Emitter>
7647bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
7648 QualType ToQT, const Expr *E) {
7649
7650 if (FromT == PT_Float) {
7651 // Floating to floating.
7652 if (ToT == PT_Float) {
7653 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7654 return this->emitCastFP(ToSem, getRoundingMode(E), E);
7655 }
7656
7657 if (ToT == PT_IntAP)
7658 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
7659 getFPOptions(E), E);
7660 if (ToT == PT_IntAPS)
7661 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
7662 getFPOptions(E), E);
7663
7664 // Float to integral.
7665 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7666 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
7667 }
7668
7669 if (isIntegerOrBoolType(FromT) || FromT == PT_Bool) {
7670 if (ToT == PT_IntAP)
7671 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7672 if (ToT == PT_IntAPS)
7673 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7674
7675 // Integral to integral.
7676 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7677 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
7678
7679 if (ToT == PT_Float) {
7680 // Integral to floating.
7681 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7682 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
7683 }
7684 }
7685
7686 return false;
7687}
7688
7689template <class Emitter>
7690bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT,
7691 QualType ToQT, const Expr *E) {
7692 assert(FromT != ToT);
7693
7694 if (ToT == PT_IntAP)
7695 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7696 if (ToT == PT_IntAPS)
7697 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7698
7699 return this->emitCast(FromT, ToT, E);
7700}
7701
7702/// Emits __real(SubExpr)
7703template <class Emitter>
7704bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
7705 assert(SubExpr->getType()->isAnyComplexType());
7706
7707 if (DiscardResult)
7708 return this->discard(SubExpr);
7709
7710 if (!this->visit(SubExpr))
7711 return false;
7712 if (SubExpr->isLValue()) {
7713 if (!this->emitConstUint8(0, SubExpr))
7714 return false;
7715 return this->emitArrayElemPtrPopUint8(SubExpr);
7716 }
7717
7718 // Rvalue, load the actual element.
7719 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
7720 0, SubExpr);
7721}
7722
7723template <class Emitter>
7724bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
7725 assert(!DiscardResult);
7726 PrimType ElemT = classifyComplexElementType(E->getType());
7727 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
7728 // for us, that means (bool)E[0] || (bool)E[1]
7729 if (!this->emitArrayElem(ElemT, 0, E))
7730 return false;
7731 if (ElemT == PT_Float) {
7732 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7733 return false;
7734 } else {
7735 if (!this->emitCast(ElemT, PT_Bool, E))
7736 return false;
7737 }
7738
7739 // We now have the bool value of E[0] on the stack.
7740 LabelTy LabelTrue = this->getLabel();
7741 if (!this->jumpTrue(LabelTrue, E))
7742 return false;
7743
7744 if (!this->emitArrayElemPop(ElemT, 1, E))
7745 return false;
7746 if (ElemT == PT_Float) {
7747 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7748 return false;
7749 } else {
7750 if (!this->emitCast(ElemT, PT_Bool, E))
7751 return false;
7752 }
7753 // Leave the boolean value of E[1] on the stack.
7754 LabelTy EndLabel = this->getLabel();
7755 this->jump(EndLabel, E);
7756
7757 this->emitLabel(LabelTrue);
7758 if (!this->emitPopPtr(E))
7759 return false;
7760 if (!this->emitConstBool(true, E))
7761 return false;
7762
7763 this->fallthrough(EndLabel);
7764 this->emitLabel(EndLabel);
7765
7766 return true;
7767}
7768
7769template <class Emitter>
7770bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
7771 const BinaryOperator *E) {
7772 assert(E->isComparisonOp());
7773 assert(!Initializing);
7774 if (DiscardResult)
7775 return this->discard(LHS) && this->discard(RHS);
7776
7777 PrimType ElemT;
7778 bool LHSIsComplex;
7779 unsigned LHSOffset;
7780 if (LHS->getType()->isAnyComplexType()) {
7781 LHSIsComplex = true;
7782 ElemT = classifyComplexElementType(LHS->getType());
7783 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
7784 if (!this->visit(LHS))
7785 return false;
7786 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
7787 return false;
7788 } else {
7789 LHSIsComplex = false;
7790 PrimType LHST = classifyPrim(LHS->getType());
7791 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
7792 if (!this->visit(LHS))
7793 return false;
7794 if (!this->emitSetLocal(LHST, LHSOffset, E))
7795 return false;
7796 }
7797
7798 bool RHSIsComplex;
7799 unsigned RHSOffset;
7800 if (RHS->getType()->isAnyComplexType()) {
7801 RHSIsComplex = true;
7802 ElemT = classifyComplexElementType(RHS->getType());
7803 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
7804 if (!this->visit(RHS))
7805 return false;
7806 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
7807 return false;
7808 } else {
7809 RHSIsComplex = false;
7810 PrimType RHST = classifyPrim(RHS->getType());
7811 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
7812 if (!this->visit(RHS))
7813 return false;
7814 if (!this->emitSetLocal(RHST, RHSOffset, E))
7815 return false;
7816 }
7817
7818 auto getElem = [&](unsigned LocalOffset, unsigned Index,
7819 bool IsComplex) -> bool {
7820 if (IsComplex) {
7821 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
7822 return false;
7823 return this->emitArrayElemPop(ElemT, Index, E);
7824 }
7825 return this->emitGetLocal(ElemT, LocalOffset, E);
7826 };
7827
7828 for (unsigned I = 0; I != 2; ++I) {
7829 // Get both values.
7830 if (!getElem(LHSOffset, I, LHSIsComplex))
7831 return false;
7832 if (!getElem(RHSOffset, I, RHSIsComplex))
7833 return false;
7834 // And compare them.
7835 if (!this->emitEQ(ElemT, E))
7836 return false;
7837
7838 if (!this->emitCastBoolUint8(E))
7839 return false;
7840 }
7841
7842 // We now have two bool values on the stack. Compare those.
7843 if (!this->emitAddUint8(E))
7844 return false;
7845 if (!this->emitConstUint8(2, E))
7846 return false;
7847
7848 if (E->getOpcode() == BO_EQ) {
7849 if (!this->emitEQUint8(E))
7850 return false;
7851 } else if (E->getOpcode() == BO_NE) {
7852 if (!this->emitNEUint8(E))
7853 return false;
7854 } else
7855 return false;
7856
7857 // In C, this returns an int.
7858 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
7859 return this->emitCast(PT_Bool, ResT, E);
7860 return true;
7861}
7862
7863/// When calling this, we have a pointer of the local-to-destroy
7864/// on the stack.
7865/// Emit destruction of record types (or arrays of record types).
7866template <class Emitter>
7867bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R,
7868 SourceInfo Loc) {
7869 assert(R);
7870 assert(!R->hasTrivialDtor());
7871 const CXXDestructorDecl *Dtor = R->getDestructor();
7872 assert(Dtor);
7873 const Function *DtorFunc = getFunction(Dtor);
7874 if (!DtorFunc)
7875 return false;
7876 assert(DtorFunc->hasThisPointer());
7877 assert(DtorFunc->getNumParams() == 1);
7878 return this->emitCall(DtorFunc, 0, Loc);
7879}
7880/// When calling this, we have a pointer of the local-to-destroy
7881/// on the stack.
7882/// Emit destruction of record types (or arrays of record types).
7883template <class Emitter>
7884bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc,
7885 SourceInfo Loc) {
7886 assert(Desc);
7887 assert(!Desc->hasTrivialDtor());
7888
7889 // Arrays.
7890 if (Desc->isArray()) {
7891 const Descriptor *ElemDesc = Desc->ElemDesc;
7892 assert(ElemDesc);
7893
7894 unsigned N = Desc->getNumElems();
7895 if (N == 0)
7896 return this->emitPopPtr(Loc);
7897
7898 for (ssize_t I = N - 1; I >= 1; --I) {
7899 if (!this->emitConstUint64(I, Loc))
7900 return false;
7901 if (!this->emitArrayElemPtrUint64(Loc))
7902 return false;
7903 if (!this->emitDestructionPop(ElemDesc, Loc))
7904 return false;
7905 }
7906 // Last iteration, removes the instance pointer from the stack.
7907 if (!this->emitConstUint64(0, Loc))
7908 return false;
7909 if (!this->emitArrayElemPtrPopUint64(Loc))
7910 return false;
7911 return this->emitDestructionPop(ElemDesc, Loc);
7912 }
7913
7914 assert(Desc->ElemRecord);
7915 assert(!Desc->ElemRecord->hasTrivialDtor());
7916 return this->emitRecordDestructionPop(Desc->ElemRecord, Loc);
7917}
7918
7919/// Create a dummy pointer for the given decl (or expr) and
7920/// push a pointer to it on the stack.
7921template <class Emitter>
7922bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E) {
7923 assert(!DiscardResult && "Should've been checked before");
7924 unsigned DummyID = P.getOrCreateDummy(D);
7925
7926 if (!this->emitGetPtrGlobal(DummyID, E))
7927 return false;
7928 if (E->getType()->isVoidType())
7929 return true;
7930
7931 // Convert the dummy pointer to another pointer type if we have to.
7932 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
7933 if (isPtrType(PT))
7934 return this->emitDecayPtr(PT_Ptr, PT, E);
7935 return false;
7936 }
7937 return true;
7938}
7939
7940template <class Emitter>
7941bool Compiler<Emitter>::emitFloat(const APFloat &F, const Expr *E) {
7942 if (Floating::singleWord(F.getSemantics()))
7943 return this->emitConstFloat(Floating(F), E);
7944
7945 APInt I = F.bitcastToAPInt();
7946 return this->emitConstFloat(
7947 Floating(const_cast<uint64_t *>(I.getRawData()),
7948 llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
7949 E);
7950}
7951
7952// This function is constexpr if and only if To, From, and the types of
7953// all subobjects of To and From are types T such that...
7954// (3.1) - is_union_v<T> is false;
7955// (3.2) - is_pointer_v<T> is false;
7956// (3.3) - is_member_pointer_v<T> is false;
7957// (3.4) - is_volatile_v<T> is false; and
7958// (3.5) - T has no non-static data members of reference type
7959template <class Emitter>
7960bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
7961 const Expr *SubExpr = E->getSubExpr();
7962 QualType FromType = SubExpr->getType();
7963 QualType ToType = E->getType();
7964 OptPrimType ToT = classify(ToType);
7965
7966 assert(!ToType->isReferenceType());
7967
7968 // Prepare storage for the result in case we discard.
7969 if (DiscardResult && !Initializing && !ToT) {
7970 UnsignedOrNone LocalIndex = allocateLocal(E);
7971 if (!LocalIndex)
7972 return false;
7973 if (!this->emitGetPtrLocal(*LocalIndex, E))
7974 return false;
7975 }
7976
7977 // Get a pointer to the value-to-cast on the stack.
7978 // For CK_LValueToRValueBitCast, this is always an lvalue and
7979 // we later assume it to be one (i.e. a PT_Ptr). However,
7980 // we call this function for other utility methods where
7981 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
7982 if (SubExpr->isGLValue() || FromType->isVectorType()) {
7983 if (!this->visit(SubExpr))
7984 return false;
7985 } else if (OptPrimType FromT = classify(SubExpr)) {
7986 unsigned TempOffset =
7987 allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
7988 if (!this->visit(SubExpr))
7989 return false;
7990 if (!this->emitSetLocal(*FromT, TempOffset, E))
7991 return false;
7992 if (!this->emitGetPtrLocal(TempOffset, E))
7993 return false;
7994 } else {
7995 return false;
7996 }
7997
7998 if (!ToT) {
7999 if (!this->emitBitCast(E))
8000 return false;
8001 return DiscardResult ? this->emitPopPtr(E) : true;
8002 }
8003 assert(ToT);
8004
8005 const llvm::fltSemantics *TargetSemantics = nullptr;
8006 if (ToT == PT_Float)
8007 TargetSemantics = &Ctx.getFloatSemantics(ToType);
8008
8009 // Conversion to a primitive type. FromType can be another
8010 // primitive type, or a record/array.
8011 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
8012 ToType->isSpecificBuiltinType(BuiltinType::Char_U));
8013 uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
8014
8015 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
8016 ResultBitWidth, TargetSemantics,
8017 ToType.getTypePtr(), E))
8018 return false;
8019
8020 if (DiscardResult)
8021 return this->emitPop(*ToT, E);
8022
8023 return true;
8024}
8025
8026namespace clang {
8027namespace interp {
8028
8029template class Compiler<ByteCodeEmitter>;
8030template class Compiler<EvalEmitter>;
8031
8032} // namespace interp
8033} // 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
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:1015
APValue & getArrayInitializedElt(unsigned I)
Definition APValue.h:626
ArrayRef< LValuePathEntry > getLValuePath() const
Definition APValue.cpp:1035
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 isFloat() const
Definition APValue.h:486
const ValueDecl * getMemberPointerDecl() const
Definition APValue.cpp:1098
APValue & getUnionValue()
Definition APValue.h:683
bool isLValue() const
Definition APValue.h:490
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:1051
APFloat & getFloat()
Definition APValue.h:522
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
unsigned getPreferredTypeAlign(QualType T) const
Return the "preferred" alignment of the specified type T for the current target, in bits.
const LangOptions & getLangOpts() const
Definition ASTContext.h:952
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:6024
Represents a loop initializing the elements of an array.
Definition Expr.h:5971
llvm::APInt getArraySize() const
Definition Expr.h:5993
OpaqueValueExpr * getCommonExpr() const
Get the common subexpression shared by all initializations (the source array).
Definition Expr.h:5986
Expr * getSubExpr() const
Get the initializer to use for each array element.
Definition Expr.h:5991
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition Expr.h: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:2997
uint64_t getValue() const
Definition ExprCXX.h:3045
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3772
QualType getElementType() const
Definition TypeBase.h:3784
Attr - This represents one attribute.
Definition Attr.h:46
Represents an attribute applied to a statement.
Definition Stmt.h:2204
Stmt * getSubStmt()
Definition Stmt.h:2240
ArrayRef< const Attr * > getAttrs() const
Definition Stmt.h:2236
Represents a C++ declaration that introduces decls from somewhere else.
Definition DeclCXX.h:3512
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:6671
BreakStmt - This represents a break.
Definition Stmt.h:3136
Represents a C++2a __builtin_bit_cast(T, v) expression.
Definition ExprCXX.h:5477
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents binding an expression to a temporary.
Definition ExprCXX.h:1494
const Expr * getSubExpr() const
Definition ExprCXX.h:1516
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition ExprCXX.h:724
bool getValue() const
Definition ExprCXX.h:741
Represents a call to a C++ constructor.
Definition ExprCXX.h:1549
bool isElidable() const
Whether this construction is elidable.
Definition ExprCXX.h:1618
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition ExprCXX.h:1692
arg_range arguments()
Definition ExprCXX.h:1673
bool requiresZeroInitialization() const
Whether this construction first requires zero-initialization before the initializer is called.
Definition ExprCXX.h:1651
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1612
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
Definition ExprCXX.h:1689
Represents a C++ constructor within a class.
Definition DeclCXX.h:2624
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition DeclCXX.cpp:3071
Represents a C++ base or member initializer.
Definition DeclCXX.h:2389
A default argument (C++ [dcl.fct.default]).
Definition ExprCXX.h:1271
A use of a default initializer in a constructor or in aggregate initialization.
Definition ExprCXX.h:1378
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1105
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2627
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2666
bool isArrayForm() const
Definition ExprCXX.h:2653
bool isGlobalDelete() const
Definition ExprCXX.h:2652
Represents a C++ destructor within a class.
Definition DeclCXX.h:2889
A C++ dynamic_cast expression (C++ [expr.dynamic.cast]).
Definition ExprCXX.h:482
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:1752
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will call.
Definition ExprCXX.h:1789
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2275
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition DeclCXX.cpp:2753
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition DeclCXX.cpp:2732
bool isLambdaStaticInvoker() const
Determine whether this is a lambda closure type's static member function that is used for the result ...
Definition DeclCXX.cpp:2897
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2356
bool isArray() const
Definition ExprCXX.h:2465
QualType getAllocatedType() const
Definition ExprCXX.h:2435
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:2470
Expr * getPlacementArg(unsigned I)
Definition ExprCXX.h:2504
unsigned getNumPlacementArgs() const
Definition ExprCXX.h:2495
FunctionDecl * getOperatorNew() const
Definition ExprCXX.h:2460
Expr * getInitializer()
The initializer of this new-expression.
Definition ExprCXX.h:2534
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition ExprCXX.h:4310
bool getValue() const
Definition ExprCXX.h:4333
The null pointer literal (C++11 [lex.nullptr])
Definition ExprCXX.h:769
Represents a list-initialization with parenthesis.
Definition ExprCXX.h:5142
MutableArrayRef< Expr * > getInitExprs()
Definition ExprCXX.h:5182
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:1679
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:1742
A C++ reinterpret_cast expression (C++ [expr.reinterpret.cast]).
Definition ExprCXX.h:527
A rewritten comparison expression that was originally written using operator syntax.
Definition ExprCXX.h:287
Expr * getSemanticForm()
Get an equivalent semantic form for this expression.
Definition ExprCXX.h:305
An expression "T()" which creates an rvalue of a non-class type T.
Definition ExprCXX.h:2197
Implicit construction of a std::initializer_list<T> object from an array temporary within list-initia...
Definition ExprCXX.h:801
Represents the this expression in C++.
Definition ExprCXX.h:1155
A C++ throw-expression (C++ [except.throw]).
Definition ExprCXX.h:1209
const Expr * getSubExpr() const
Definition ExprCXX.h:1229
CXXTryStmt - A C++ try block, including all handlers.
Definition StmtCXX.h:69
CompoundStmt * getTryBlock()
Definition StmtCXX.h:100
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition ExprCXX.h:849
bool isTypeOperand() const
Definition ExprCXX.h:885
QualType getTypeOperand(const ASTContext &Context) const
Retrieves the type operand of this typeid() expression after various required adjustments (removing r...
Definition ExprCXX.cpp:161
Expr * getExprOperand() const
Definition ExprCXX.h:896
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr....
Definition ExprCXX.cpp:134
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition ExprCXX.h:1069
MSGuidDecl * getGuidDecl() const
Definition ExprCXX.h:1115
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h: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:1603
CaseStmt - Represent a case statement.
Definition Stmt.h:1921
Stmt * getSubStmt()
Definition Stmt.h:2034
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
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:3325
QualType getElementType() const
Definition TypeBase.h:3335
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:1741
body_range body()
Definition Stmt.h:1804
Stmt * body_back()
Definition Stmt.h:1809
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:3810
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition TypeBase.h:3886
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:413
bool hasAPValueResult() const
Definition Expr.h:1160
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4437
ContinueStmt - This represents a continue.
Definition Stmt.h:3120
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:1632
decl_range decls()
Definition Stmt.h:1680
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:2082
DoStmt - This represents a 'do/while' stmt.
Definition Stmt.h:2833
Stmt * getBody()
Definition Stmt.h:2858
Expr * getCond()
Definition Stmt.h:2851
const Expr * getBase() const
Definition Expr.h:6580
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:3662
This represents one expression.
Definition Expr.h:112
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition Expr.cpp:84
bool isGLValue() const
Definition Expr.h:287
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition Expr.h:177
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3078
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:3086
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:3688
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:3253
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:3070
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
Definition Expr.h:6609
void getEncodedElementAccess(SmallVectorImpl< uint32_t > &Elts) const
getEncodedElementAccess - Encode the elements accessed into an llvm aggregate Constant of ConstantInt...
Definition Expr.cpp:4549
Represents a member of a struct/union/class.
Definition Decl.h:3175
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3411
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:2889
Stmt * getInit()
Definition Stmt.h:2904
VarDecl * getConditionVariable() const
Retrieve the variable declared in this "for" statement, if any.
Definition Stmt.cpp:1120
Stmt * getBody()
Definition Stmt.h:2933
Expr * getInc()
Definition Stmt.h:2932
Expr * getCond()
Definition Stmt.h:2931
DeclStmt * getConditionVariableDeclStmt()
If this ForStmt has a condition variable, return the faux DeclStmt associated with the creation of th...
Definition Stmt.h:2919
const Expr * getSubExpr() const
Definition Expr.h:1065
Represents a function declaration or definition.
Definition Decl.h:2015
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2812
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3281
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4207
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4195
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3764
QualType getReturnType() const
Definition Decl.h:2860
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2789
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition Decl.h:2392
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition Decl.cpp:4331
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:3428
bool isDefaulted() const
Whether this function is defaulted.
Definition Decl.h:2400
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3828
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3201
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:6181
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition Expr.h:6467
IfStmt - This represents an if/then/else.
Definition Stmt.h:2260
Stmt * getThen()
Definition Stmt.h:2349
Stmt * getInit()
Definition Stmt.h:2410
bool isNonNegatedConsteval() const
Definition Stmt.h:2445
Expr * getCond()
Definition Stmt.h:2337
bool isNegatedConsteval() const
Definition Stmt.h:2449
Stmt * getElse()
Definition Stmt.h:2358
DeclStmt * getConditionVariableDeclStmt()
If this IfStmt has a condition variable, return the faux DeclStmt associated with the creation of tha...
Definition Stmt.h:2393
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:6060
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3482
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:5404
ArrayRef< Expr * > inits()
Definition Expr.h:5352
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition ExprCXX.h:1969
capture_init_iterator capture_init_begin()
Retrieve the first initialization argument for this lambda expression (which initializes the first ca...
Definition ExprCXX.h:2095
CXXRecordDecl * getLambdaClass() const
Retrieve the class that corresponds to the lambda.
Definition ExprCXX.cpp:1400
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition DeclCXX.h:3324
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:4414
APValue & getAsAPValue() const
Get the value of this MSGuidDecl as an APValue.
Definition DeclCXX.cpp:3866
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4921
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4946
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4938
LifetimeExtendedTemporaryDecl * getLifetimeExtendedTemporaryDecl()
Definition ExprCXX.h:4961
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:3703
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:3217
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:4640
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:1805
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3378
QualType getPointeeType() const
Definition TypeBase.h:3388
[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:6803
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
Definition Expr.h:6851
ArrayRef< Expr * > semantics()
Definition Expr.h:6875
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8515
QualType withConst() const
Definition TypeBase.h:1165
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:8431
bool isConstant(const ASTContext &Ctx) const
Definition TypeBase.h:1097
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8504
Represents a struct/union/class.
Definition Decl.h:4342
Frontend produces RecoveryExprs on semantic errors that prevent creating other well-formed expression...
Definition Expr.h:7502
Base for LValueReferenceType and RValueReferenceType.
Definition TypeBase.h:3623
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:3161
Expr * getRetValue()
Definition Stmt.h:3188
SourceLocation getLocation() const
Definition Expr.h:2158
std::string ComputeName(ASTContext &Context) const
Definition Expr.cpp:587
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:4442
unsigned getPackLength() const
Retrieve the length of the parameter pack.
Definition ExprCXX.h:4516
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:2282
Represents a C++11 static_assert declaration.
Definition DeclCXX.h:4152
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:1494
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:1188
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:4665
const SwitchCase * getNextSwitchCase() const
Definition Stmt.h:1894
SwitchStmt - This represents a 'switch' stmt.
Definition Stmt.h:2510
Expr * getCond()
Definition Stmt.h:2573
Stmt * getBody()
Definition Stmt.h:2585
VarDecl * getConditionVariable()
Retrieve the variable declared in this "switch" statement, if any.
Definition Stmt.cpp:1186
Stmt * getInit()
Definition Stmt.h:2590
SwitchCase * getSwitchCaseList()
Definition Stmt.h:2641
DeclStmt * getConditionVariableDeclStmt()
If this SwitchStmt has a condition variable, return the faux DeclStmt associated with the creation of...
Definition Stmt.h:2624
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3732
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3833
bool isUnion() const
Definition Decl.h:3943
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:2897
bool getBoolValue() const
Definition ExprCXX.h:2948
const APValue & getAPValue() const
Definition ExprCXX.h:2953
bool isStoredAsBoolean() const
Definition ExprCXX.h:2944
The base class of the type hierarchy.
Definition TypeBase.h:1866
bool isVoidType() const
Definition TypeBase.h:9034
bool isBooleanType() const
Definition TypeBase.h:9171
bool isLiteralType(const ASTContext &Ctx) const
Return true if this is a literal type (C++11 [basic.types]p10)
Definition Type.cpp:3061
bool isIncompleteArrayType() const
Definition TypeBase.h:8775
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:3245
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isVoidPointerType() const
Definition Type.cpp:714
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
Definition Type.cpp:2470
bool isArrayType() const
Definition TypeBase.h:8767
bool isFunctionPointerType() const
Definition TypeBase.h:8735
bool isConstantMatrixType() const
Definition TypeBase.h:8835
bool isPointerType() const
Definition TypeBase.h:8668
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9078
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9328
bool isReferenceType() const
Definition TypeBase.h:8692
bool isEnumeralType() const
Definition TypeBase.h:8799
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:754
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9156
bool isSpecificBuiltinType(unsigned K) const
Test for a particular builtin type.
Definition TypeBase.h:9003
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition TypeBase.h:2832
bool isAnyComplexType() const
Definition TypeBase.h:8803
bool isFixedPointType() const
Return true if this is a fixed point type according to ISO/IEC JTC1 SC22 WG14 N1169.
Definition TypeBase.h:9094
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition TypeBase.h:9214
bool isMemberPointerType() const
Definition TypeBase.h:8749
bool isAtomicType() const
Definition TypeBase.h:8860
EnumDecl * castAsEnumDecl() const
Definition Type.h:59
bool isStdByteType() const
Definition Type.cpp:3264
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9314
bool isPointerOrReferenceType() const
Definition TypeBase.h:8672
bool isFunctionType() const
Definition TypeBase.h:8664
bool isVectorType() const
Definition TypeBase.h:8807
bool isRealFloatingType() const
Floating point categories.
Definition Type.cpp:2358
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:2971
bool isFloatingType() const
Definition Type.cpp:2342
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9261
bool isRecordType() const
Definition TypeBase.h:8795
bool isSizelessVectorType() const
Returns true for all scalable vector types.
Definition Type.cpp:2616
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3577
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:3112
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:5583
QualType getType() const
Definition Value.cpp:237
Represents a variable declaration or definition.
Definition Decl.h:926
bool isInitCapture() const
Whether this variable is the implicit variable for a lambda init-capture.
Definition Decl.h:1593
APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
Definition Decl.cpp:2588
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition Decl.h:1298
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1241
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2661
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1208
const Expr * getInit() const
Definition Decl.h:1383
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1268
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1373
Represents a GCC generic vector type.
Definition TypeBase.h:4225
unsigned getNumElements() const
Definition TypeBase.h:4240
QualType getElementType() const
Definition TypeBase.h:4239
WhileStmt - This represents a 'while' stmt.
Definition Stmt.h:2698
Expr * getCond()
Definition Stmt.h:2750
DeclStmt * getConditionVariableDeclStmt()
If this WhileStmt has a condition variable, return the faux DeclStmt associated with the creation of ...
Definition Stmt.h:2786
VarDecl * getConditionVariable()
Retrieve the variable declared in this "while" statement, if any.
Definition Stmt.cpp:1247
Stmt * getBody()
Definition Stmt.h:2762
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:462
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 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:282
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:438
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:444
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:473
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:441
bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext) override
Toplevel visitDeclAndReturn().
bool VisitCXXNewExpr(const CXXNewExpr *E)
const ValueDecl * InitializingDecl
Definition Compiler.h:460
bool VisitCompoundAssignOperator(const CompoundAssignOperator *E)
bool visit(const Expr *E) override
Evaluates an expression and places the result on the stack.
bool delegate(const Expr *E)
Just pass evaluation on to E.
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
CaseMap CaseLabels
Switch case mapping.
Definition Compiler.h:469
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.
const Expr * SourceLocDefaultExpr
DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
Definition Compiler.h:447
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 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:459
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:435
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:466
bool VisitUnaryOperator(const UnaryOperator *E)
bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E)
OptPrimType classify(const Expr *E) const
Definition Compiler.h:276
llvm::SmallVector< LabelInfo > LabelInfoStack
Stack of label information for loops and switch statements.
Definition Compiler.h:471
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:450
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:278
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:722
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:151
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:219
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition Function.h:155
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:538
bool destroyLocals(const Expr *E=nullptr) override
Explicit destruction of local variables.
Definition Compiler.h:551
LocalScope(Compiler< Emitter > *Ctx, ScopeKind Kind=ScopeKind::Block)
Definition Compiler.h:540
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:87
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:145
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
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:480
Compiler< Emitter > * Ctx
Compiler instance.
Definition Compiler.h:531
virtual void addLocal(Scope::Local Local)
Definition Compiler.h:491
VariableScope * getParent() const
Definition Compiler.h:521
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:363
bool LT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1336
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:58
bool Div(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:650
static bool Activate(InterpState &S, CodePtr OpPC)
Definition Interp.h:2085
constexpr bool isPtrType(PrimType T)
Definition PrimType.h:54
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:191
bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:2567
constexpr bool isIntegerOrBoolType(PrimType T)
Definition PrimType.h:51
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:1343
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:33
static std::optional< bool > getBoolValue(const Expr *E)
Definition Compiler.cpp:29
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2202
bool Mul(InterpState &S, CodePtr OpPC)
Definition Interp.h:383
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:23
bool Inc(InterpState &S, CodePtr OpPC, bool CanOverflow)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value increased by ...
Definition Interp.h:837
bool Add(InterpState &S, CodePtr OpPC)
Definition Interp.h:343
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:343
@ SD_FullExpression
Full-expression storage duration (for temporaries).
Definition Specifiers.h:340
@ Result
The result type of a method or function.
Definition TypeBase.h:905
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:121
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:253
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:267
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:260
QualType getType() const
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:154
static constexpr MetadataSize InlineDescMD
Definition Descriptor.h:143
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:258
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:272
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:152
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:270
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