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 bool IsConstexprUnknown) {
4998 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
4999 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
5000 // or isa<MaterializeTemporaryExpr>().
5001 Descriptor *D = P.createDescriptor(Src, Ty, nullptr, Descriptor::InlineDescMD,
5002 IsConst, isa<const Expr *>(Src),
5003 /*IsMutable=*/false, IsVolatile);
5004 D->IsConstexprUnknown = IsConstexprUnknown;
5005 Scope::Local Local = this->createLocal(D);
5006 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
5007 Locals.insert({VD, Local});
5008 VarScope->addForScopeKind(Local, SC);
5009 return Local.Offset;
5010}
5011
5012template <class Emitter>
5014 ScopeKind SC,
5015 bool IsConstexprUnknown) {
5016 const ValueDecl *Key = nullptr;
5017 const Expr *Init = nullptr;
5018 bool IsTemporary = false;
5019 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
5020 Key = VD;
5021
5022 if (const auto *VarD = dyn_cast<VarDecl>(VD))
5023 Init = VarD->getInit();
5024 }
5025 if (auto *E = Src.dyn_cast<const Expr *>()) {
5026 IsTemporary = true;
5027 if (Ty.isNull())
5028 Ty = E->getType();
5029 }
5030
5031 Descriptor *D = P.createDescriptor(
5033 IsTemporary, /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(),
5034 Init);
5035 if (!D)
5036 return std::nullopt;
5037 D->IsConstexprUnknown = IsConstexprUnknown;
5038
5039 Scope::Local Local = this->createLocal(D);
5040 if (Key)
5041 Locals.insert({Key, Local});
5042 VarScope->addForScopeKind(Local, SC);
5043 return Local.Offset;
5044}
5045
5046template <class Emitter>
5048 QualType Ty = E->getType();
5049 assert(!Ty->isRecordType());
5050
5051 Descriptor *D = P.createDescriptor(
5053 /*IsTemporary=*/true);
5054
5055 if (!D)
5056 return std::nullopt;
5057
5058 Scope::Local Local = this->createLocal(D);
5060 assert(S);
5061 // Attach to topmost scope.
5062 while (S->getParent())
5063 S = S->getParent();
5064 assert(S && !S->getParent());
5065 S->addLocal(Local);
5066 return Local.Offset;
5067}
5068
5069template <class Emitter>
5071 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
5072 return PT->getPointeeType()->getAsCanonical<RecordType>();
5073 return Ty->getAsCanonical<RecordType>();
5074}
5075
5076template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
5077 if (const auto *RecordTy = getRecordTy(Ty))
5078 return getRecord(RecordTy->getDecl()->getDefinitionOrSelf());
5079 return nullptr;
5080}
5081
5082template <class Emitter>
5084 return P.getOrCreateRecord(RD);
5085}
5086
5087template <class Emitter>
5089 return Ctx.getOrCreateFunction(FD);
5090}
5091
5092template <class Emitter>
5093bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
5095
5096 auto maybeDestroyLocals = [&]() -> bool {
5097 if (DestroyToplevelScope)
5098 return RootScope.destroyLocals() && this->emitCheckAllocations(E);
5099 return this->emitCheckAllocations(E);
5100 };
5101
5102 // Void expressions.
5103 if (E->getType()->isVoidType()) {
5104 if (!visit(E))
5105 return false;
5106 return this->emitRetVoid(E) && maybeDestroyLocals();
5107 }
5108
5109 // Expressions with a primitive return type.
5110 if (OptPrimType T = classify(E)) {
5111 if (!visit(E))
5112 return false;
5113
5114 return this->emitRet(*T, E) && maybeDestroyLocals();
5115 }
5116
5117 // Expressions with a composite return type.
5118 // For us, that means everything we don't
5119 // have a PrimType for.
5120 if (UnsignedOrNone LocalOffset = this->allocateLocal(E)) {
5121 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalOffset));
5122 if (!this->emitGetPtrLocal(*LocalOffset, E))
5123 return false;
5124
5125 if (!visitInitializer(E))
5126 return false;
5127
5128 if (!this->emitFinishInit(E))
5129 return false;
5130 // We are destroying the locals AFTER the Ret op.
5131 // The Ret op needs to copy the (alive) values, but the
5132 // destructors may still turn the entire expression invalid.
5133 return this->emitRetValue(E) && maybeDestroyLocals();
5134 }
5135
5136 return maybeDestroyLocals() && false;
5137}
5138
5139template <class Emitter>
5141 bool IsConstexprUnknown) {
5142
5143 auto R = this->visitVarDecl(VD, VD->getInit(), /*Toplevel=*/true,
5144 IsConstexprUnknown);
5145
5146 if (R.notCreated())
5147 return R;
5148
5149 if (R)
5150 return true;
5151
5152 if (!R && Context::shouldBeGloballyIndexed(VD)) {
5153 if (auto GlobalIndex = P.getGlobal(VD)) {
5154 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5155 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5156
5158 GlobalBlock->invokeDtor();
5159 }
5160 }
5161
5162 return R;
5163}
5164
5165/// Toplevel visitDeclAndReturn().
5166/// We get here from evaluateAsInitializer().
5167/// We need to evaluate the initializer and return its value.
5168template <class Emitter>
5170 bool ConstantContext) {
5171 // We only create variables if we're evaluating in a constant context.
5172 // Otherwise, just evaluate the initializer and return it.
5173 if (!ConstantContext) {
5174 DeclScope<Emitter> LS(this, VD);
5175 if (!this->visit(Init))
5176 return false;
5177 return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
5178 LS.destroyLocals() && this->emitCheckAllocations(VD);
5179 }
5180
5181 LocalScope<Emitter> VDScope(this);
5182 if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true))
5183 return false;
5184
5185 OptPrimType VarT = classify(VD->getType());
5187 auto GlobalIndex = P.getGlobal(VD);
5188 assert(GlobalIndex); // visitVarDecl() didn't return false.
5189 if (VarT) {
5190 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
5191 return false;
5192 } else {
5193 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
5194 return false;
5195 }
5196 } else {
5197 auto Local = Locals.find(VD);
5198 assert(Local != Locals.end()); // Same here.
5199 if (VarT) {
5200 if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
5201 return false;
5202 } else {
5203 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
5204 return false;
5205 }
5206 }
5207
5208 // Return the value.
5209 if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
5210 // If the Ret above failed and this is a global variable, mark it as
5211 // uninitialized, even everything else succeeded.
5213 auto GlobalIndex = P.getGlobal(VD);
5214 assert(GlobalIndex);
5215 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5216 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5217
5219 GlobalBlock->invokeDtor();
5220 }
5221 return false;
5222 }
5223
5224 return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
5225}
5226
5227template <class Emitter>
5230 bool Toplevel, bool IsConstexprUnknown) {
5231 // We don't know what to do with these, so just return false.
5232 if (VD->getType().isNull())
5233 return false;
5234
5235 // This case is EvalEmitter-only. If we won't create any instructions for the
5236 // initializer anyway, don't bother creating the variable in the first place.
5237 if (!this->isActive())
5239
5240 OptPrimType VarT = classify(VD->getType());
5241
5242 if (Init && Init->isValueDependent())
5243 return false;
5244
5246 auto checkDecl = [&]() -> bool {
5247 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
5248 return !NeedsOp || this->emitCheckDecl(VD, VD);
5249 };
5250
5252 UnsignedOrNone GlobalIndex = P.getGlobal(VD);
5253 if (GlobalIndex) {
5254 // The global was previously created but the initializer failed.
5255 if (!P.getGlobal(*GlobalIndex)->isInitialized())
5256 return false;
5257 // We've already seen and initialized this global.
5258 if (P.isGlobalInitialized(*GlobalIndex))
5259 return checkDecl();
5260 // The previous attempt at initialization might've been unsuccessful,
5261 // so let's try this one.
5262 } else if ((GlobalIndex = P.createGlobal(VD, Init))) {
5263 } else {
5264 return false;
5265 }
5266 if (!Init)
5267 return true;
5268
5269 if (!checkDecl())
5270 return false;
5271
5272 if (VarT) {
5273 if (!this->visit(Init))
5274 return false;
5275
5276 return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
5277 }
5278
5279 if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
5280 return false;
5281
5282 if (!visitInitializer(Init))
5283 return false;
5284
5285 return this->emitFinishInitGlobal(Init);
5286 }
5287 // Local variables.
5289
5290 if (VarT) {
5291 unsigned Offset = this->allocateLocalPrimitive(
5292 VD, *VarT, VD->getType().isConstQualified(),
5294 IsConstexprUnknown);
5295
5296 if (!Init)
5297 return true;
5298
5299 // If this is a toplevel declaration, create a scope for the
5300 // initializer.
5301 if (Toplevel) {
5303 if (!this->visit(Init))
5304 return false;
5305 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
5306 }
5307 if (!this->visit(Init))
5308 return false;
5309 return this->emitSetLocal(*VarT, Offset, VD);
5310 }
5311 // Local composite variables.
5312 if (UnsignedOrNone Offset = this->allocateLocal(
5313 VD, VD->getType(), ScopeKind::Block, IsConstexprUnknown)) {
5314 if (!Init)
5315 return true;
5316
5317 if (!this->emitGetPtrLocal(*Offset, Init))
5318 return false;
5319
5320 if (!visitInitializer(Init))
5321 return false;
5322
5323 return this->emitFinishInitPop(Init);
5324 }
5325 return false;
5326}
5327
5328template <class Emitter>
5330 const Expr *E) {
5331 assert(!DiscardResult);
5332 if (Val.isInt())
5333 return this->emitConst(Val.getInt(), ValType, E);
5334 if (Val.isFloat()) {
5335 APFloat F = Val.getFloat();
5336 return this->emitFloat(F, E);
5337 }
5338
5339 if (Val.isLValue()) {
5340 if (Val.isNullPointer())
5341 return this->emitNull(ValType, 0, nullptr, E);
5343 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
5344 return this->visit(BaseExpr);
5345 if (const auto *VD = Base.dyn_cast<const ValueDecl *>())
5346 return this->visitDeclRef(VD, E);
5347 } else if (Val.isMemberPointer()) {
5348 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl())
5349 return this->emitGetMemberPtr(MemberDecl, E);
5350 return this->emitNullMemberPtr(0, nullptr, E);
5351 }
5352
5353 return false;
5354}
5355
5356template <class Emitter>
5358 const Expr *E, QualType T) {
5359 if (Val.isStruct()) {
5360 const Record *R = this->getRecord(T);
5361 assert(R);
5362 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
5363 const APValue &F = Val.getStructField(I);
5364 const Record::Field *RF = R->getField(I);
5365 QualType FieldType = RF->Decl->getType();
5366
5367 if (OptPrimType PT = classify(FieldType)) {
5368 if (!this->visitAPValue(F, *PT, E))
5369 return false;
5370 if (!this->emitInitField(*PT, RF->Offset, E))
5371 return false;
5372 } else {
5373 if (!this->emitGetPtrField(RF->Offset, E))
5374 return false;
5375 if (!this->visitAPValueInitializer(F, E, FieldType))
5376 return false;
5377 if (!this->emitPopPtr(E))
5378 return false;
5379 }
5380 }
5381 return true;
5382 }
5383 if (Val.isUnion()) {
5384 const FieldDecl *UnionField = Val.getUnionField();
5385 if (!UnionField)
5386 return true;
5387 const Record *R = this->getRecord(T);
5388 assert(R);
5389 const APValue &F = Val.getUnionValue();
5390 const Record::Field *RF = R->getField(UnionField);
5391 QualType FieldType = RF->Decl->getType();
5392
5393 if (OptPrimType PT = classify(FieldType)) {
5394 if (!this->visitAPValue(F, *PT, E))
5395 return false;
5396 if (RF->isBitField())
5397 return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(),
5398 E);
5399 return this->emitInitFieldActivate(*PT, RF->Offset, E);
5400 }
5401
5402 if (!this->emitGetPtrField(RF->Offset, E))
5403 return false;
5404 if (!this->emitActivate(E))
5405 return false;
5406 if (!this->visitAPValueInitializer(F, E, FieldType))
5407 return false;
5408 return this->emitPopPtr(E);
5409 }
5410 if (Val.isArray()) {
5411 const auto *ArrType = T->getAsArrayTypeUnsafe();
5412 QualType ElemType = ArrType->getElementType();
5413 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
5414 const APValue &Elem = Val.getArrayInitializedElt(A);
5415 if (OptPrimType ElemT = classify(ElemType)) {
5416 if (!this->visitAPValue(Elem, *ElemT, E))
5417 return false;
5418 if (!this->emitInitElem(*ElemT, A, E))
5419 return false;
5420 } else {
5421 if (!this->emitConstUint32(A, E))
5422 return false;
5423 if (!this->emitArrayElemPtrUint32(E))
5424 return false;
5425 if (!this->visitAPValueInitializer(Elem, E, ElemType))
5426 return false;
5427 if (!this->emitPopPtr(E))
5428 return false;
5429 }
5430 }
5431 return true;
5432 }
5433 // TODO: Other types.
5434
5435 return false;
5436}
5437
5438template <class Emitter>
5440 unsigned BuiltinID) {
5441 if (BuiltinID == Builtin::BI__builtin_constant_p) {
5442 // Void argument is always invalid and harder to handle later.
5443 if (E->getArg(0)->getType()->isVoidType()) {
5444 if (DiscardResult)
5445 return true;
5446 return this->emitConst(0, E);
5447 }
5448
5449 if (!this->emitStartSpeculation(E))
5450 return false;
5451 LabelTy EndLabel = this->getLabel();
5452 if (!this->speculate(E, EndLabel))
5453 return false;
5454 if (!this->emitEndSpeculation(E))
5455 return false;
5456 this->fallthrough(EndLabel);
5457 if (DiscardResult)
5458 return this->emitPop(classifyPrim(E), E);
5459 return true;
5460 }
5461
5462 // For these, we're expected to ultimately return an APValue pointing
5463 // to the CallExpr. This is needed to get the correct codegen.
5464 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
5465 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
5466 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
5467 BuiltinID == Builtin::BI__builtin_function_start) {
5468 if (DiscardResult)
5469 return true;
5470 return this->emitDummyPtr(E, E);
5471 }
5472
5474 OptPrimType ReturnT = classify(E);
5475
5476 // Non-primitive return type. Prepare storage.
5477 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
5478 UnsignedOrNone LocalIndex = allocateLocal(E);
5479 if (!LocalIndex)
5480 return false;
5481 if (!this->emitGetPtrLocal(*LocalIndex, E))
5482 return false;
5483 }
5484
5485 // Prepare function arguments including special cases.
5486 switch (BuiltinID) {
5487 case Builtin::BI__builtin_object_size:
5488 case Builtin::BI__builtin_dynamic_object_size: {
5489 assert(E->getNumArgs() == 2);
5490 const Expr *Arg0 = E->getArg(0);
5491 if (Arg0->isGLValue()) {
5492 if (!this->visit(Arg0))
5493 return false;
5494
5495 } else {
5496 if (!this->visitAsLValue(Arg0))
5497 return false;
5498 }
5499 if (!this->visit(E->getArg(1)))
5500 return false;
5501
5502 } break;
5503 case Builtin::BI__assume:
5504 case Builtin::BI__builtin_assume:
5505 // Argument is not evaluated.
5506 break;
5507 case Builtin::BI__atomic_is_lock_free:
5508 case Builtin::BI__atomic_always_lock_free: {
5509 assert(E->getNumArgs() == 2);
5510 if (!this->visit(E->getArg(0)))
5511 return false;
5512 if (!this->visitAsLValue(E->getArg(1)))
5513 return false;
5514 } break;
5515
5516 default:
5517 if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
5518 // Put arguments on the stack.
5519 for (const auto *Arg : E->arguments()) {
5520 if (!this->visit(Arg))
5521 return false;
5522 }
5523 }
5524 }
5525
5526 if (!this->emitCallBI(E, BuiltinID, E))
5527 return false;
5528
5529 if (DiscardResult && !ReturnType->isVoidType())
5530 return this->emitPop(ReturnT.value_or(PT_Ptr), E);
5531
5532 return true;
5533}
5534
5535template <class Emitter>
5537 if (E->containsErrors())
5538 return false;
5539 const FunctionDecl *FuncDecl = E->getDirectCallee();
5540
5541 if (FuncDecl) {
5542 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
5543 return VisitBuiltinCallExpr(E, BuiltinID);
5544
5545 // Calls to replaceable operator new/operator delete.
5547 if (FuncDecl->getDeclName().isAnyOperatorNew())
5548 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
5549 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete);
5550 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
5551 }
5552
5553 // Explicit calls to trivial destructors
5554 if (const auto *DD = dyn_cast<CXXDestructorDecl>(FuncDecl);
5555 DD && DD->isTrivial()) {
5556 const auto *MemberCall = cast<CXXMemberCallExpr>(E);
5557 if (!this->visit(MemberCall->getImplicitObjectArgument()))
5558 return false;
5559 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
5560 this->emitPopPtr(E);
5561 }
5562 }
5563
5564 LocalScope<Emitter> CallScope(this, ScopeKind::Call);
5565
5566 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
5568 bool HasRVO = !ReturnType->isVoidType() && !T;
5569
5570 if (HasRVO) {
5571 if (DiscardResult) {
5572 // If we need to discard the return value but the function returns its
5573 // value via an RVO pointer, we need to create one such pointer just
5574 // for this call.
5575 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5576 if (!this->emitGetPtrLocal(*LocalIndex, E))
5577 return false;
5578 }
5579 } else {
5580 // We need the result. Prepare a pointer to return or
5581 // dup the current one.
5582 if (!Initializing) {
5583 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
5584 if (!this->emitGetPtrLocal(*LocalIndex, E))
5585 return false;
5586 }
5587 }
5588 if (!this->emitDupPtr(E))
5589 return false;
5590 }
5591 }
5592
5594
5595 bool IsAssignmentOperatorCall = false;
5596 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
5597 OCE && OCE->isAssignmentOp()) {
5598 // Just like with regular assignments, we need to special-case assignment
5599 // operators here and evaluate the RHS (the second arg) before the LHS (the
5600 // first arg). We fix this by using a Flip op later.
5601 assert(Args.size() == 2);
5602 IsAssignmentOperatorCall = true;
5603 std::reverse(Args.begin(), Args.end());
5604 }
5605 // Calling a static operator will still
5606 // pass the instance, but we don't need it.
5607 // Discard it here.
5608 if (isa<CXXOperatorCallExpr>(E)) {
5609 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
5610 MD && MD->isStatic()) {
5611 if (!this->discard(E->getArg(0)))
5612 return false;
5613 // Drop first arg.
5614 Args.erase(Args.begin());
5615 }
5616 }
5617
5618 bool Devirtualized = false;
5619 UnsignedOrNone CalleeOffset = std::nullopt;
5620 // Add the (optional, implicit) This pointer.
5621 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
5622 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
5623 // If we end up creating a CallPtr op for this, we need the base of the
5624 // member pointer as the instance pointer, and later extract the function
5625 // decl as the function pointer.
5626 const Expr *Callee = E->getCallee();
5627 CalleeOffset =
5628 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
5629 if (!this->visit(Callee))
5630 return false;
5631 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
5632 return false;
5633 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5634 return false;
5635 if (!this->emitGetMemberPtrBase(E))
5636 return false;
5637 } else {
5638 const auto *InstancePtr = MC->getImplicitObjectArgument();
5639 if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
5640 isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
5641 const auto *Stripped = stripCheckedDerivedToBaseCasts(InstancePtr);
5642 if (isa<CXXThisExpr>(Stripped)) {
5643 FuncDecl =
5644 cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
5645 Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5646 Devirtualized = true;
5647 if (!this->visit(Stripped))
5648 return false;
5649 } else {
5650 if (!this->visit(InstancePtr))
5651 return false;
5652 }
5653 } else {
5654 if (!this->visit(InstancePtr))
5655 return false;
5656 }
5657 }
5658 } else if (const auto *PD =
5659 dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
5660 if (!this->emitCheckPseudoDtor(E))
5661 return false;
5662 const Expr *Base = PD->getBase();
5663 // E.g. `using T = int; 0.~T();`.
5664 if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr)
5665 return this->discard(Base);
5666 if (!this->visit(Base))
5667 return false;
5668 return this->emitEndLifetimePop(E);
5669 } else if (!FuncDecl) {
5670 const Expr *Callee = E->getCallee();
5671 CalleeOffset =
5672 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
5673 if (!this->visit(Callee))
5674 return false;
5675 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
5676 return false;
5677 }
5678
5679 if (!this->visitCallArgs(Args, FuncDecl, IsAssignmentOperatorCall,
5681 return false;
5682
5683 // Undo the argument reversal we did earlier.
5684 if (IsAssignmentOperatorCall) {
5685 assert(Args.size() == 2);
5686 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
5687 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
5688 if (!this->emitFlip(Arg2T, Arg1T, E))
5689 return false;
5690 }
5691
5692 if (FuncDecl) {
5693 const Function *Func = getFunction(FuncDecl);
5694 if (!Func)
5695 return false;
5696
5697 // In error cases, the function may be called with fewer arguments than
5698 // parameters.
5699 if (E->getNumArgs() < Func->getNumWrittenParams())
5700 return false;
5701
5702 assert(HasRVO == Func->hasRVO());
5703
5704 bool HasQualifier = false;
5705 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
5706 HasQualifier = ME->hasQualifier();
5707
5708 bool IsVirtual = false;
5709 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
5710 IsVirtual = !Devirtualized && MD->isVirtual();
5711
5712 // In any case call the function. The return value will end up on the stack
5713 // and if the function has RVO, we already have the pointer on the stack to
5714 // write the result into.
5715 if (IsVirtual && !HasQualifier) {
5716 uint32_t VarArgSize = 0;
5717 unsigned NumParams =
5718 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
5719 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5720 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5721
5722 if (!this->emitCallVirt(Func, VarArgSize, E))
5723 return false;
5724 } else if (Func->isVariadic()) {
5725 uint32_t VarArgSize = 0;
5726 unsigned NumParams =
5727 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
5728 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5729 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5730 if (!this->emitCallVar(Func, VarArgSize, E))
5731 return false;
5732 } else {
5733 if (!this->emitCall(Func, 0, E))
5734 return false;
5735 }
5736 } else {
5737 // Indirect call. Visit the callee, which will leave a FunctionPointer on
5738 // the stack. Cleanup of the returned value if necessary will be done after
5739 // the function call completed.
5740
5741 // Sum the size of all args from the call expr.
5742 uint32_t ArgSize = 0;
5743 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
5744 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5745
5746 // Get the callee, either from a member pointer or function pointer saved in
5747 // CalleeOffset.
5748 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
5749 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5750 return false;
5751 if (!this->emitGetMemberPtrDecl(E))
5752 return false;
5753 } else {
5754 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
5755 return false;
5756 }
5757 if (!this->emitCallPtr(ArgSize, E, E))
5758 return false;
5759 }
5760
5761 // Cleanup for discarded return values.
5762 if (DiscardResult && !ReturnType->isVoidType() && T)
5763 return this->emitPop(*T, E) && CallScope.destroyLocals();
5764
5765 return CallScope.destroyLocals();
5766}
5767
5768template <class Emitter>
5770 SourceLocScope<Emitter> SLS(this, E);
5771
5772 return this->delegate(E->getExpr());
5773}
5774
5775template <class Emitter>
5777 SourceLocScope<Emitter> SLS(this, E);
5778
5779 return this->delegate(E->getExpr());
5780}
5781
5782template <class Emitter>
5784 if (DiscardResult)
5785 return true;
5786
5787 return this->emitConstBool(E->getValue(), E);
5788}
5789
5790template <class Emitter>
5792 const CXXNullPtrLiteralExpr *E) {
5793 if (DiscardResult)
5794 return true;
5795
5796 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
5797 return this->emitNullPtr(Val, nullptr, E);
5798}
5799
5800template <class Emitter>
5802 if (DiscardResult)
5803 return true;
5804
5805 assert(E->getType()->isIntegerType());
5806
5807 PrimType T = classifyPrim(E->getType());
5808 return this->emitZero(T, E);
5809}
5810
5811template <class Emitter>
5813 if (DiscardResult)
5814 return true;
5815
5816 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
5817 if (this->LambdaThisCapture.Offset > 0) {
5818 if (this->LambdaThisCapture.IsPtr)
5819 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
5820 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
5821 }
5822 }
5823
5824 // In some circumstances, the 'this' pointer does not actually refer to the
5825 // instance pointer of the current function frame, but e.g. to the declaration
5826 // currently being initialized. Here we emit the necessary instruction(s) for
5827 // this scenario.
5828 if (!InitStackActive || InitStack.empty())
5829 return this->emitThis(E);
5830
5831 // If our init stack is, for example:
5832 // 0 Stack: 3 (decl)
5833 // 1 Stack: 6 (init list)
5834 // 2 Stack: 1 (field)
5835 // 3 Stack: 6 (init list)
5836 // 4 Stack: 1 (field)
5837 //
5838 // We want to find the LAST element in it that's an init list,
5839 // which is marked with the K_InitList marker. The index right
5840 // before that points to an init list. We need to find the
5841 // elements before the K_InitList element that point to a base
5842 // (e.g. a decl or This), optionally followed by field, elem, etc.
5843 // In the example above, we want to emit elements [0..2].
5844 unsigned StartIndex = 0;
5845 unsigned EndIndex = 0;
5846 // Find the init list.
5847 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
5848 if (InitStack[StartIndex].Kind == InitLink::K_DIE) {
5849 EndIndex = StartIndex;
5850 --StartIndex;
5851 break;
5852 }
5853 }
5854
5855 // Walk backwards to find the base.
5856 for (; StartIndex > 0; --StartIndex) {
5857 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
5858 continue;
5859
5860 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
5861 InitStack[StartIndex].Kind != InitLink::K_Elem &&
5862 InitStack[StartIndex].Kind != InitLink::K_DIE)
5863 break;
5864 }
5865
5866 if (StartIndex == 0 && EndIndex == 0)
5867 EndIndex = InitStack.size() - 1;
5868
5869 assert(StartIndex < EndIndex);
5870
5871 // Emit the instructions.
5872 for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
5873 if (InitStack[I].Kind == InitLink::K_InitList ||
5874 InitStack[I].Kind == InitLink::K_DIE)
5875 continue;
5876 if (!InitStack[I].template emit<Emitter>(this, E))
5877 return false;
5878 }
5879 return true;
5880}
5881
5882template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
5883 switch (S->getStmtClass()) {
5884 case Stmt::CompoundStmtClass:
5886 case Stmt::DeclStmtClass:
5887 return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
5888 case Stmt::ReturnStmtClass:
5890 case Stmt::IfStmtClass:
5891 return visitIfStmt(cast<IfStmt>(S));
5892 case Stmt::WhileStmtClass:
5894 case Stmt::DoStmtClass:
5895 return visitDoStmt(cast<DoStmt>(S));
5896 case Stmt::ForStmtClass:
5897 return visitForStmt(cast<ForStmt>(S));
5898 case Stmt::CXXForRangeStmtClass:
5900 case Stmt::BreakStmtClass:
5902 case Stmt::ContinueStmtClass:
5904 case Stmt::SwitchStmtClass:
5906 case Stmt::CaseStmtClass:
5907 return visitCaseStmt(cast<CaseStmt>(S));
5908 case Stmt::DefaultStmtClass:
5910 case Stmt::AttributedStmtClass:
5912 case Stmt::CXXTryStmtClass:
5914 case Stmt::NullStmtClass:
5915 return true;
5916 // Always invalid statements.
5917 case Stmt::GCCAsmStmtClass:
5918 case Stmt::MSAsmStmtClass:
5919 case Stmt::GotoStmtClass:
5920 return this->emitInvalid(S);
5921 case Stmt::LabelStmtClass:
5922 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
5923 default: {
5924 if (const auto *E = dyn_cast<Expr>(S))
5925 return this->discard(E);
5926 return false;
5927 }
5928 }
5929}
5930
5931template <class Emitter>
5934 for (const auto *InnerStmt : S->body())
5935 if (!visitStmt(InnerStmt))
5936 return false;
5937 return Scope.destroyLocals();
5938}
5939
5940template <class Emitter>
5941bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
5942 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
5943 for (auto *BD : DD->flat_bindings())
5944 if (auto *KD = BD->getHoldingVar();
5945 KD && !this->visitVarDecl(KD, KD->getInit()))
5946 return false;
5947 }
5948 return true;
5949}
5950
5952 assert(FD);
5953 assert(FD->getParent()->isUnion());
5954 const CXXRecordDecl *CXXRD =
5956 return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
5957}
5958
5959template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
5960 for (;;) {
5961 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
5962 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
5963 FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
5964 return true;
5965 E = ME->getBase();
5966 continue;
5967 }
5968
5969 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
5970 E = ASE->getBase()->IgnoreImplicit();
5971 continue;
5972 }
5973
5974 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
5975 ICE && (ICE->getCastKind() == CK_NoOp ||
5976 ICE->getCastKind() == CK_DerivedToBase ||
5977 ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
5978 E = ICE->getSubExpr();
5979 continue;
5980 }
5981
5982 if (const auto *This = dyn_cast<CXXThisExpr>(E)) {
5983 const auto *ThisRecord =
5984 This->getType()->getPointeeType()->getAsRecordDecl();
5985 if (!ThisRecord->isUnion())
5986 return false;
5987 // Otherwise, always activate if we're in the ctor.
5988 if (const auto *Ctor =
5989 dyn_cast_if_present<CXXConstructorDecl>(CompilingFunction))
5990 return Ctor->getParent() == ThisRecord;
5991 return false;
5992 }
5993
5994 break;
5995 }
5996 return false;
5997}
5998
5999template <class Emitter>
6001 bool EvaluateConditionDecl) {
6002 for (const auto *D : DS->decls()) {
6005 continue;
6006
6007 const auto *VD = dyn_cast<VarDecl>(D);
6008 if (!VD)
6009 return false;
6010 if (!this->visitVarDecl(VD, VD->getInit()))
6011 return false;
6012
6013 // Register decomposition decl holding vars.
6014 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
6015 return false;
6016 }
6017
6018 return true;
6019}
6020
6021template <class Emitter>
6023 if (this->InStmtExpr)
6024 return this->emitUnsupported(RS);
6025
6026 if (const Expr *RE = RS->getRetValue()) {
6027 LocalScope<Emitter> RetScope(this);
6028 if (ReturnType) {
6029 // Primitive types are simply returned.
6030 if (!this->visit(RE))
6031 return false;
6032 this->emitCleanup();
6033 return this->emitRet(*ReturnType, RS);
6034 }
6035
6036 if (RE->getType()->isVoidType()) {
6037 if (!this->visit(RE))
6038 return false;
6039 } else {
6040 if (RE->containsErrors())
6041 return false;
6042
6044 // RVO - construct the value in the return location.
6045 if (!this->emitRVOPtr(RE))
6046 return false;
6047 if (!this->visitInitializer(RE))
6048 return false;
6049 if (!this->emitPopPtr(RE))
6050 return false;
6051
6052 this->emitCleanup();
6053 return this->emitRetVoid(RS);
6054 }
6055 }
6056
6057 // Void return.
6058 this->emitCleanup();
6059 return this->emitRetVoid(RS);
6060}
6061
6062template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
6063 LocalScope<Emitter> IfScope(this);
6064
6065 auto visitChildStmt = [&](const Stmt *S) -> bool {
6066 LocalScope<Emitter> SScope(this);
6067 if (!visitStmt(S))
6068 return false;
6069 return SScope.destroyLocals();
6070 };
6071
6072 if (auto *CondInit = IS->getInit()) {
6073 if (!visitStmt(CondInit))
6074 return false;
6075 }
6076
6077 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) {
6078 if (!visitDeclStmt(CondDecl))
6079 return false;
6080 }
6081
6082 // Save ourselves compiling some code and the jumps, etc. if the condition is
6083 // stataically known to be either true or false. We could look at more cases
6084 // here, but I think all the ones that actually happen are using a
6085 // ConstantExpr.
6086 if (std::optional<bool> BoolValue = getBoolValue(IS->getCond())) {
6087 if (*BoolValue)
6088 return visitChildStmt(IS->getThen());
6089 if (const Stmt *Else = IS->getElse())
6090 return visitChildStmt(Else);
6091 return true;
6092 }
6093
6094 // Otherwise, compile the condition.
6095 if (IS->isNonNegatedConsteval()) {
6096 if (!this->emitIsConstantContext(IS))
6097 return false;
6098 } else if (IS->isNegatedConsteval()) {
6099 if (!this->emitIsConstantContext(IS))
6100 return false;
6101 if (!this->emitInv(IS))
6102 return false;
6103 } else {
6105 if (!this->visitBool(IS->getCond()))
6106 return false;
6107 if (!CondScope.destroyLocals())
6108 return false;
6109 }
6110
6111 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
6112 return false;
6113
6114 if (const Stmt *Else = IS->getElse()) {
6115 LabelTy LabelElse = this->getLabel();
6116 LabelTy LabelEnd = this->getLabel();
6117 if (!this->jumpFalse(LabelElse, IS))
6118 return false;
6119 if (!visitChildStmt(IS->getThen()))
6120 return false;
6121 if (!this->jump(LabelEnd, IS))
6122 return false;
6123 this->emitLabel(LabelElse);
6124 if (!visitChildStmt(Else))
6125 return false;
6126 this->emitLabel(LabelEnd);
6127 } else {
6128 LabelTy LabelEnd = this->getLabel();
6129 if (!this->jumpFalse(LabelEnd, IS))
6130 return false;
6131 if (!visitChildStmt(IS->getThen()))
6132 return false;
6133 this->emitLabel(LabelEnd);
6134 }
6135
6136 if (!IfScope.destroyLocals())
6137 return false;
6138
6139 return true;
6140}
6141
6142template <class Emitter>
6144 const Expr *Cond = S->getCond();
6145 const Stmt *Body = S->getBody();
6146
6147 LabelTy CondLabel = this->getLabel(); // Label before the condition.
6148 LabelTy EndLabel = this->getLabel(); // Label after the loop.
6149 LocalScope<Emitter> WholeLoopScope(this);
6150 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6151
6152 this->fallthrough(CondLabel);
6153 this->emitLabel(CondLabel);
6154
6155 // Start of the loop body {
6156 LocalScope<Emitter> CondScope(this);
6157
6158 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6159 if (!visitDeclStmt(CondDecl))
6160 return false;
6161 }
6162
6163 if (!this->visitBool(Cond))
6164 return false;
6165
6166 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6167 return false;
6168
6169 if (!this->jumpFalse(EndLabel, S))
6170 return false;
6171
6172 if (!this->visitStmt(Body))
6173 return false;
6174
6175 if (!CondScope.destroyLocals())
6176 return false;
6177 // } End of loop body.
6178
6179 if (!this->jump(CondLabel, S))
6180 return false;
6181 this->fallthrough(EndLabel);
6182 this->emitLabel(EndLabel);
6183
6184 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6185}
6186
6187template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
6188 const Expr *Cond = S->getCond();
6189 const Stmt *Body = S->getBody();
6190
6191 LabelTy StartLabel = this->getLabel();
6192 LabelTy EndLabel = this->getLabel();
6193 LabelTy CondLabel = this->getLabel();
6194 LocalScope<Emitter> WholeLoopScope(this);
6195 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
6196
6197 this->fallthrough(StartLabel);
6198 this->emitLabel(StartLabel);
6199
6200 {
6201 LocalScope<Emitter> CondScope(this);
6202 if (!this->visitStmt(Body))
6203 return false;
6204 this->fallthrough(CondLabel);
6205 this->emitLabel(CondLabel);
6206 if (!this->visitBool(Cond))
6207 return false;
6208
6209 if (!CondScope.destroyLocals())
6210 return false;
6211 }
6212 if (!this->jumpTrue(StartLabel, S))
6213 return false;
6214
6215 this->fallthrough(EndLabel);
6216 this->emitLabel(EndLabel);
6217 return WholeLoopScope.destroyLocals();
6218}
6219
6220template <class Emitter>
6222 // for (Init; Cond; Inc) { Body }
6223 const Stmt *Init = S->getInit();
6224 const Expr *Cond = S->getCond();
6225 const Expr *Inc = S->getInc();
6226 const Stmt *Body = S->getBody();
6227
6228 LabelTy EndLabel = this->getLabel();
6229 LabelTy CondLabel = this->getLabel();
6230 LabelTy IncLabel = this->getLabel();
6231
6232 LocalScope<Emitter> WholeLoopScope(this);
6233 if (Init && !this->visitStmt(Init))
6234 return false;
6235
6236 // Start of the loop body {
6237 this->fallthrough(CondLabel);
6238 this->emitLabel(CondLabel);
6239
6240 LocalScope<Emitter> CondScope(this);
6241 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6242 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
6243 if (!visitDeclStmt(CondDecl))
6244 return false;
6245 }
6246
6247 if (Cond) {
6248 if (!this->visitBool(Cond))
6249 return false;
6250 if (!this->jumpFalse(EndLabel, S))
6251 return false;
6252 }
6253 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6254 return false;
6255
6256 if (Body && !this->visitStmt(Body))
6257 return false;
6258
6259 this->fallthrough(IncLabel);
6260 this->emitLabel(IncLabel);
6261 if (Inc && !this->discard(Inc))
6262 return false;
6263
6264 if (!CondScope.destroyLocals())
6265 return false;
6266 if (!this->jump(CondLabel, S))
6267 return false;
6268 // } End of loop body.
6269
6270 this->emitLabel(EndLabel);
6271 // If we jumped out of the loop above, we still need to clean up the condition
6272 // scope.
6273 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
6274}
6275
6276template <class Emitter>
6278 const Stmt *Init = S->getInit();
6279 const Expr *Cond = S->getCond();
6280 const Expr *Inc = S->getInc();
6281 const Stmt *Body = S->getBody();
6282 const Stmt *BeginStmt = S->getBeginStmt();
6283 const Stmt *RangeStmt = S->getRangeStmt();
6284 const Stmt *EndStmt = S->getEndStmt();
6285
6286 LabelTy EndLabel = this->getLabel();
6287 LabelTy CondLabel = this->getLabel();
6288 LabelTy IncLabel = this->getLabel();
6289 LocalScope<Emitter> WholeLoopScope(this);
6290 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
6291
6292 // Emit declarations needed in the loop.
6293 if (Init && !this->visitStmt(Init))
6294 return false;
6295 if (!this->visitStmt(RangeStmt))
6296 return false;
6297 if (!this->visitStmt(BeginStmt))
6298 return false;
6299 if (!this->visitStmt(EndStmt))
6300 return false;
6301
6302 // Now the condition as well as the loop variable assignment.
6303 this->fallthrough(CondLabel);
6304 this->emitLabel(CondLabel);
6305 if (!this->visitBool(Cond))
6306 return false;
6307 if (!this->jumpFalse(EndLabel, S))
6308 return false;
6309
6310 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
6311 return false;
6312
6313 // Body.
6314 {
6315 if (!this->visitStmt(Body))
6316 return false;
6317
6318 this->fallthrough(IncLabel);
6319 this->emitLabel(IncLabel);
6320 if (!this->discard(Inc))
6321 return false;
6322 }
6323
6324 if (!this->jump(CondLabel, S))
6325 return false;
6326
6327 this->fallthrough(EndLabel);
6328 this->emitLabel(EndLabel);
6329 return WholeLoopScope.destroyLocals();
6330}
6331
6332template <class Emitter>
6334 if (LabelInfoStack.empty())
6335 return false;
6336
6337 OptLabelTy TargetLabel = std::nullopt;
6338 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6339 const VariableScope<Emitter> *BreakScope = nullptr;
6340
6341 if (!TargetLoop) {
6342 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6343 if (LI.BreakLabel) {
6344 TargetLabel = *LI.BreakLabel;
6345 BreakScope = LI.BreakOrContinueScope;
6346 break;
6347 }
6348 }
6349 } else {
6350 for (auto LI : LabelInfoStack) {
6351 if (LI.Name == TargetLoop) {
6352 TargetLabel = *LI.BreakLabel;
6353 BreakScope = LI.BreakOrContinueScope;
6354 break;
6355 }
6356 }
6357 }
6358
6359 // Faulty break statement (e.g. label redefined or named loops disabled).
6360 if (!TargetLabel)
6361 return false;
6362
6363 for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope;
6364 C = C->getParent()) {
6365 if (!C->destroyLocals())
6366 return false;
6367 }
6368
6369 return this->jump(*TargetLabel, S);
6370}
6371
6372template <class Emitter>
6374 if (LabelInfoStack.empty())
6375 return false;
6376
6377 OptLabelTy TargetLabel = std::nullopt;
6378 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
6379 const VariableScope<Emitter> *ContinueScope = nullptr;
6380
6381 if (!TargetLoop) {
6382 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
6383 if (LI.ContinueLabel) {
6384 TargetLabel = *LI.ContinueLabel;
6385 ContinueScope = LI.BreakOrContinueScope;
6386 break;
6387 }
6388 }
6389 } else {
6390 for (auto LI : LabelInfoStack) {
6391 if (LI.Name == TargetLoop) {
6392 TargetLabel = *LI.ContinueLabel;
6393 ContinueScope = LI.BreakOrContinueScope;
6394 break;
6395 }
6396 }
6397 }
6398 assert(TargetLabel);
6399
6400 for (VariableScope<Emitter> *C = VarScope; C != ContinueScope;
6401 C = C->getParent()) {
6402 if (!C->destroyLocals())
6403 return false;
6404 }
6405
6406 return this->jump(*TargetLabel, S);
6407}
6408
6409template <class Emitter>
6411 const Expr *Cond = S->getCond();
6412 if (Cond->containsErrors())
6413 return false;
6414
6415 PrimType CondT = this->classifyPrim(Cond->getType());
6416 LocalScope<Emitter> LS(this);
6417
6418 LabelTy EndLabel = this->getLabel();
6419 UnsignedOrNone DefaultLabel = std::nullopt;
6420 unsigned CondVar =
6421 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
6422
6423 if (const auto *CondInit = S->getInit())
6424 if (!visitStmt(CondInit))
6425 return false;
6426
6427 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
6428 if (!visitDeclStmt(CondDecl))
6429 return false;
6430
6431 // Initialize condition variable.
6432 if (!this->visit(Cond))
6433 return false;
6434 if (!this->emitSetLocal(CondT, CondVar, S))
6435 return false;
6436
6437 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
6438 return false;
6439
6441 // Create labels and comparison ops for all case statements.
6442 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
6443 SC = SC->getNextSwitchCase()) {
6444 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
6445 CaseLabels[SC] = this->getLabel();
6446
6447 if (CS->caseStmtIsGNURange()) {
6448 LabelTy EndOfRangeCheck = this->getLabel();
6449 const Expr *Low = CS->getLHS();
6450 const Expr *High = CS->getRHS();
6451 if (Low->isValueDependent() || High->isValueDependent())
6452 return false;
6453
6454 if (!this->emitGetLocal(CondT, CondVar, CS))
6455 return false;
6456 if (!this->visit(Low))
6457 return false;
6458 PrimType LT = this->classifyPrim(Low->getType());
6459 if (!this->emitGE(LT, S))
6460 return false;
6461 if (!this->jumpFalse(EndOfRangeCheck, S))
6462 return false;
6463
6464 if (!this->emitGetLocal(CondT, CondVar, CS))
6465 return false;
6466 if (!this->visit(High))
6467 return false;
6468 PrimType HT = this->classifyPrim(High->getType());
6469 if (!this->emitLE(HT, S))
6470 return false;
6471 if (!this->jumpTrue(CaseLabels[CS], S))
6472 return false;
6473 this->emitLabel(EndOfRangeCheck);
6474 continue;
6475 }
6476
6477 const Expr *Value = CS->getLHS();
6478 if (Value->isValueDependent())
6479 return false;
6480 PrimType ValueT = this->classifyPrim(Value->getType());
6481
6482 // Compare the case statement's value to the switch condition.
6483 if (!this->emitGetLocal(CondT, CondVar, CS))
6484 return false;
6485 if (!this->visit(Value))
6486 return false;
6487
6488 // Compare and jump to the case label.
6489 if (!this->emitEQ(ValueT, S))
6490 return false;
6491 if (!this->jumpTrue(CaseLabels[CS], S))
6492 return false;
6493 } else {
6494 assert(!DefaultLabel);
6495 DefaultLabel = this->getLabel();
6496 }
6497 }
6498
6499 // If none of the conditions above were true, fall through to the default
6500 // statement or jump after the switch statement.
6501 if (DefaultLabel) {
6502 if (!this->jump(*DefaultLabel, S))
6503 return false;
6504 } else {
6505 if (!this->jump(EndLabel, S))
6506 return false;
6507 }
6508
6509 SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel,
6510 DefaultLabel);
6511 if (!this->visitStmt(S->getBody()))
6512 return false;
6513 this->fallthrough(EndLabel);
6514 this->emitLabel(EndLabel);
6515
6516 return LS.destroyLocals();
6517}
6518
6519template <class Emitter>
6521 this->fallthrough(CaseLabels[S]);
6522 this->emitLabel(CaseLabels[S]);
6523 return this->visitStmt(S->getSubStmt());
6524}
6525
6526template <class Emitter>
6528 if (LabelInfoStack.empty())
6529 return false;
6530
6531 LabelTy DefaultLabel;
6532 for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) {
6533 if (LI.DefaultLabel) {
6534 DefaultLabel = *LI.DefaultLabel;
6535 break;
6536 }
6537 }
6538
6539 this->emitLabel(DefaultLabel);
6540 return this->visitStmt(S->getSubStmt());
6541}
6542
6543template <class Emitter>
6545 const Stmt *SubStmt = S->getSubStmt();
6546
6547 bool IsMSVCConstexprAttr = isa<ReturnStmt>(SubStmt) &&
6549
6550 if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S))
6551 return false;
6552
6553 if (this->Ctx.getLangOpts().CXXAssumptions &&
6554 !this->Ctx.getLangOpts().MSVCCompat) {
6555 for (const Attr *A : S->getAttrs()) {
6556 auto *AA = dyn_cast<CXXAssumeAttr>(A);
6557 if (!AA)
6558 continue;
6559
6560 assert(isa<NullStmt>(SubStmt));
6561
6562 const Expr *Assumption = AA->getAssumption();
6563 if (Assumption->isValueDependent())
6564 return false;
6565
6566 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
6567 continue;
6568
6569 // Evaluate assumption.
6570 if (!this->visitBool(Assumption))
6571 return false;
6572
6573 if (!this->emitAssume(Assumption))
6574 return false;
6575 }
6576 }
6577
6578 // Ignore other attributes.
6579 if (!this->visitStmt(SubStmt))
6580 return false;
6581
6582 if (IsMSVCConstexprAttr)
6583 return this->emitPopMSVCCE(S);
6584 return true;
6585}
6586
6587template <class Emitter>
6589 // Ignore all handlers.
6590 return this->visitStmt(S->getTryBlock());
6591}
6592
6593template <class Emitter>
6594bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
6595 assert(MD->isLambdaStaticInvoker());
6596 assert(MD->hasBody());
6597 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
6598
6599 const CXXRecordDecl *ClosureClass = MD->getParent();
6600 const FunctionDecl *LambdaCallOp;
6601 assert(ClosureClass->captures().empty());
6602 if (ClosureClass->isGenericLambda()) {
6603 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6604 assert(MD->isFunctionTemplateSpecialization() &&
6605 "A generic lambda's static-invoker function must be a "
6606 "template specialization");
6608 FunctionTemplateDecl *CallOpTemplate =
6609 LambdaCallOp->getDescribedFunctionTemplate();
6610 void *InsertPos = nullptr;
6611 const FunctionDecl *CorrespondingCallOpSpecialization =
6612 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
6613 assert(CorrespondingCallOpSpecialization);
6614 LambdaCallOp = CorrespondingCallOpSpecialization;
6615 } else {
6616 LambdaCallOp = ClosureClass->getLambdaCallOperator();
6617 }
6618 assert(ClosureClass->captures().empty());
6619 const Function *Func = this->getFunction(LambdaCallOp);
6620 if (!Func)
6621 return false;
6622 assert(Func->hasThisPointer());
6623 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
6624
6625 if (Func->hasRVO()) {
6626 if (!this->emitRVOPtr(MD))
6627 return false;
6628 }
6629
6630 // The lambda call operator needs an instance pointer, but we don't have
6631 // one here, and we don't need one either because the lambda cannot have
6632 // any captures, as verified above. Emit a null pointer. This is then
6633 // special-cased when interpreting to not emit any misleading diagnostics.
6634 if (!this->emitNullPtr(0, nullptr, MD))
6635 return false;
6636
6637 // Forward all arguments from the static invoker to the lambda call operator.
6638 for (const ParmVarDecl *PVD : MD->parameters()) {
6639 auto It = this->Params.find(PVD);
6640 assert(It != this->Params.end());
6641
6642 // We do the lvalue-to-rvalue conversion manually here, so no need
6643 // to care about references.
6644 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
6645 if (!this->emitGetParam(ParamType, It->second.Index, MD))
6646 return false;
6647 }
6648
6649 if (!this->emitCall(Func, 0, LambdaCallOp))
6650 return false;
6651
6652 this->emitCleanup();
6653 if (ReturnType)
6654 return this->emitRet(*ReturnType, MD);
6655
6656 // Nothing to do, since we emitted the RVO pointer above.
6657 return this->emitRetVoid(MD);
6658}
6659
6660template <class Emitter>
6661bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
6662 if (Ctx.getLangOpts().CPlusPlus23)
6663 return true;
6664
6665 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
6666 return true;
6667
6668 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
6669}
6670
6672 const Expr *InitExpr = Init->getInit();
6673
6674 if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
6675 !isa<CXXConstructExpr>(InitExpr))
6676 return true;
6677
6678 if (const auto *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
6679 const CXXConstructorDecl *Ctor = CE->getConstructor();
6680 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
6681 Ctor->isTrivial())
6682 return true;
6683 }
6684
6685 return false;
6686}
6687
6688template <class Emitter>
6689bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
6690 assert(!ReturnType);
6691
6692 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
6693 const Expr *InitExpr,
6694 bool Activate = false) -> bool {
6695 // We don't know what to do with these, so just return false.
6696 if (InitExpr->getType().isNull())
6697 return false;
6698
6699 if (OptPrimType T = this->classify(InitExpr)) {
6700 if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
6701 return false;
6702
6703 if (!this->visit(InitExpr))
6704 return false;
6705
6706 if (F->isBitField())
6707 return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(),
6708 InitExpr);
6709 return this->emitInitThisField(*T, FieldOffset, InitExpr);
6710 }
6711 // Non-primitive case. Get a pointer to the field-to-initialize
6712 // on the stack and call visitInitialzer() for it.
6713 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
6714 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
6715 return false;
6716
6717 if (Activate && !this->emitActivate(InitExpr))
6718 return false;
6719
6720 if (!this->visitInitializer(InitExpr))
6721 return false;
6722
6723 return this->emitFinishInitPop(InitExpr);
6724 };
6725
6726 const RecordDecl *RD = Ctor->getParent();
6727 const Record *R = this->getRecord(RD);
6728 if (!R)
6729 return false;
6730 bool IsUnion = R->isUnion();
6731
6732 if (IsUnion && Ctor->isCopyOrMoveConstructor()) {
6734
6735 if (R->getNumFields() == 0)
6736 return this->emitRetVoid(Ctor);
6737 // union copy and move ctors are special.
6738 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
6739 if (!this->emitThis(Ctor))
6740 return false;
6741
6742 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor))
6743 return false;
6744
6745 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
6746 this->emitRetVoid(Ctor);
6747 }
6748
6750 for (const auto *Init : Ctor->inits()) {
6751 // Scope needed for the initializers.
6752 LocalScope<Emitter> Scope(this, ScopeKind::FullExpression);
6753
6754 const Expr *InitExpr = Init->getInit();
6755 if (const FieldDecl *Member = Init->getMember()) {
6756 const Record::Field *F = R->getField(Member);
6757
6760 if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
6761 return false;
6762 } else if (const Type *Base = Init->getBaseClass()) {
6763 const auto *BaseDecl = Base->getAsCXXRecordDecl();
6764 assert(BaseDecl);
6765
6766 if (Init->isBaseVirtual()) {
6767 assert(R->getVirtualBase(BaseDecl));
6768 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
6769 return false;
6770
6771 } else {
6772 // Base class initializer.
6773 // Get This Base and call initializer on it.
6774 const Record::Base *B = R->getBase(BaseDecl);
6775 assert(B);
6776 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
6777 return false;
6778 }
6779
6780 if (IsUnion && !this->emitActivate(InitExpr))
6781 return false;
6782
6783 if (!this->visitInitializer(InitExpr))
6784 return false;
6785 if (!this->emitFinishInitPop(InitExpr))
6786 return false;
6787 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
6790 assert(IFD->getChainingSize() >= 2);
6791
6792 unsigned NestedFieldOffset = 0;
6793 const Record::Field *NestedField = nullptr;
6794 for (const NamedDecl *ND : IFD->chain()) {
6795 const auto *FD = cast<FieldDecl>(ND);
6796 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6797 assert(FieldRecord);
6798
6799 NestedField = FieldRecord->getField(FD);
6800 assert(NestedField);
6801 IsUnion = IsUnion || FieldRecord->isUnion();
6802
6803 NestedFieldOffset += NestedField->Offset;
6804 }
6805 assert(NestedField);
6806
6807 unsigned FirstLinkOffset =
6808 R->getField(cast<FieldDecl>(IFD->chain()[0]))->Offset;
6809 InitLinkScope<Emitter> ILS(this, InitLink::Field(FirstLinkOffset));
6811 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
6812 IsUnion))
6813 return false;
6814
6815 // Mark all chain links as initialized.
6816 unsigned InitFieldOffset = 0;
6817 for (const NamedDecl *ND : IFD->chain().drop_back()) {
6818 const auto *FD = cast<FieldDecl>(ND);
6819 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
6820 assert(FieldRecord);
6821 NestedField = FieldRecord->getField(FD);
6822 InitFieldOffset += NestedField->Offset;
6823 assert(NestedField);
6824 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
6825 return false;
6826 if (!this->emitFinishInitPop(InitExpr))
6827 return false;
6828 }
6829
6830 } else {
6831 assert(Init->isDelegatingInitializer());
6832 if (!this->emitThis(InitExpr))
6833 return false;
6834 if (!this->visitInitializer(Init->getInit()))
6835 return false;
6836 if (!this->emitPopPtr(InitExpr))
6837 return false;
6838 }
6839
6840 if (!Scope.destroyLocals())
6841 return false;
6842 }
6843
6844 if (const Stmt *Body = Ctor->getBody()) {
6845 // Only emit the CtorCheck op for non-empty CompoundStmt bodies.
6846 // For non-CompoundStmts, always assume they are non-empty and emit it.
6847 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
6848 if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{}))
6849 return false;
6850 } else {
6851 if (!this->emitCtorCheck(SourceInfo{}))
6852 return false;
6853 }
6854
6855 if (!visitStmt(Body))
6856 return false;
6857 }
6858
6859 return this->emitRetVoid(SourceInfo{});
6860}
6861
6862template <class Emitter>
6863bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
6864 const RecordDecl *RD = Dtor->getParent();
6865 const Record *R = this->getRecord(RD);
6866 if (!R)
6867 return false;
6868
6869 if (!Dtor->isTrivial() && Dtor->getBody()) {
6870 if (!this->visitStmt(Dtor->getBody()))
6871 return false;
6872 }
6873
6874 if (!this->emitThis(Dtor))
6875 return false;
6876
6877 if (!this->emitCheckDestruction(Dtor))
6878 return false;
6879
6880 assert(R);
6881 if (!R->isUnion()) {
6882
6884 // First, destroy all fields.
6885 for (const Record::Field &Field : llvm::reverse(R->fields())) {
6886 const Descriptor *D = Field.Desc;
6887 if (D->hasTrivialDtor())
6888 continue;
6889 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
6890 return false;
6891 if (!this->emitDestructionPop(D, SourceInfo{}))
6892 return false;
6893 }
6894 }
6895
6896 for (const Record::Base &Base : llvm::reverse(R->bases())) {
6897 if (Base.R->hasTrivialDtor())
6898 continue;
6899 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
6900 return false;
6901 if (!this->emitRecordDestructionPop(Base.R, {}))
6902 return false;
6903 }
6904
6905 // FIXME: Virtual bases.
6906 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
6907}
6908
6909template <class Emitter>
6910bool Compiler<Emitter>::compileUnionAssignmentOperator(
6911 const CXXMethodDecl *MD) {
6912 if (!this->emitThis(MD))
6913 return false;
6914
6915 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD))
6916 return false;
6917
6918 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
6919}
6920
6921template <class Emitter>
6923 // Classify the return type.
6924 ReturnType = this->classify(F->getReturnType());
6925
6926 this->CompilingFunction = F;
6927
6928 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
6929 return this->compileConstructor(Ctor);
6930 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
6931 return this->compileDestructor(Dtor);
6932
6933 // Emit custom code if this is a lambda static invoker.
6934 if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
6935 const RecordDecl *RD = MD->getParent();
6936
6937 if (RD->isUnion() &&
6939 return this->compileUnionAssignmentOperator(MD);
6940
6941 if (MD->isLambdaStaticInvoker())
6942 return this->emitLambdaStaticInvokerBody(MD);
6943 }
6944
6945 // Regular functions.
6946 if (const auto *Body = F->getBody())
6947 if (!visitStmt(Body))
6948 return false;
6949
6950 // Emit a guard return to protect against a code path missing one.
6951 if (F->getReturnType()->isVoidType())
6952 return this->emitRetVoid(SourceInfo{});
6953 return this->emitNoRet(SourceInfo{});
6954}
6955
6956static uint32_t getBitWidth(const Expr *E) {
6957 assert(E->refersToBitField());
6958 const auto *ME = cast<MemberExpr>(E);
6959 const auto *FD = cast<FieldDecl>(ME->getMemberDecl());
6960 return FD->getBitWidthValue();
6961}
6962
6963template <class Emitter>
6965 const Expr *SubExpr = E->getSubExpr();
6966 if (SubExpr->getType()->isAnyComplexType())
6967 return this->VisitComplexUnaryOperator(E);
6968 if (SubExpr->getType()->isVectorType())
6969 return this->VisitVectorUnaryOperator(E);
6970 if (SubExpr->getType()->isFixedPointType())
6971 return this->VisitFixedPointUnaryOperator(E);
6972 OptPrimType T = classify(SubExpr->getType());
6973
6974 switch (E->getOpcode()) {
6975 case UO_PostInc: { // x++
6976 if (!Ctx.getLangOpts().CPlusPlus14)
6977 return this->emitInvalid(E);
6978 if (!T)
6979 return this->emitError(E);
6980
6981 if (!this->visit(SubExpr))
6982 return false;
6983
6984 if (T == PT_Ptr) {
6985 if (!this->emitIncPtr(E))
6986 return false;
6987
6988 return DiscardResult ? this->emitPopPtr(E) : true;
6989 }
6990
6991 if (T == PT_Float)
6992 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
6993 : this->emitIncf(getFPOptions(E), E);
6994
6995 if (SubExpr->refersToBitField())
6996 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
6997 getBitWidth(SubExpr), E)
6998 : this->emitIncBitfield(*T, E->canOverflow(),
6999 getBitWidth(SubExpr), E);
7000
7001 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
7002 : this->emitInc(*T, E->canOverflow(), E);
7003 }
7004 case UO_PostDec: { // x--
7005 if (!Ctx.getLangOpts().CPlusPlus14)
7006 return this->emitInvalid(E);
7007 if (!T)
7008 return this->emitError(E);
7009
7010 if (!this->visit(SubExpr))
7011 return false;
7012
7013 if (T == PT_Ptr) {
7014 if (!this->emitDecPtr(E))
7015 return false;
7016
7017 return DiscardResult ? this->emitPopPtr(E) : true;
7018 }
7019
7020 if (T == PT_Float)
7021 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
7022 : this->emitDecf(getFPOptions(E), E);
7023
7024 if (SubExpr->refersToBitField()) {
7025 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7026 getBitWidth(SubExpr), E)
7027 : this->emitDecBitfield(*T, E->canOverflow(),
7028 getBitWidth(SubExpr), E);
7029 }
7030
7031 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
7032 : this->emitDec(*T, E->canOverflow(), E);
7033 }
7034 case UO_PreInc: { // ++x
7035 if (!Ctx.getLangOpts().CPlusPlus14)
7036 return this->emitInvalid(E);
7037 if (!T)
7038 return this->emitError(E);
7039
7040 if (!this->visit(SubExpr))
7041 return false;
7042
7043 if (T == PT_Ptr) {
7044 if (!this->emitLoadPtr(E))
7045 return false;
7046 if (!this->emitConstUint8(1, E))
7047 return false;
7048 if (!this->emitAddOffsetUint8(E))
7049 return false;
7050 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7051 }
7052
7053 // Post-inc and pre-inc are the same if the value is to be discarded.
7054 if (DiscardResult) {
7055 if (T == PT_Float)
7056 return this->emitIncfPop(getFPOptions(E), E);
7057 if (SubExpr->refersToBitField())
7058 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
7059 getBitWidth(SubExpr), E)
7060 : this->emitIncBitfield(*T, E->canOverflow(),
7061 getBitWidth(SubExpr), E);
7062 return this->emitIncPop(*T, E->canOverflow(), E);
7063 }
7064
7065 if (T == PT_Float) {
7066 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7067 if (!this->emitLoadFloat(E))
7068 return false;
7069 APFloat F(TargetSemantics, 1);
7070 if (!this->emitFloat(F, E))
7071 return false;
7072
7073 if (!this->emitAddf(getFPOptions(E), E))
7074 return false;
7075 if (!this->emitStoreFloat(E))
7076 return false;
7077 } else if (SubExpr->refersToBitField()) {
7078 assert(isIntegerOrBoolType(*T));
7079 if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7080 E))
7081 return false;
7082 } else {
7083 assert(isIntegerOrBoolType(*T));
7084 if (!this->emitPreInc(*T, E->canOverflow(), E))
7085 return false;
7086 }
7087 return E->isGLValue() || this->emitLoadPop(*T, E);
7088 }
7089 case UO_PreDec: { // --x
7090 if (!Ctx.getLangOpts().CPlusPlus14)
7091 return this->emitInvalid(E);
7092 if (!T)
7093 return this->emitError(E);
7094
7095 if (!this->visit(SubExpr))
7096 return false;
7097
7098 if (T == PT_Ptr) {
7099 if (!this->emitLoadPtr(E))
7100 return false;
7101 if (!this->emitConstUint8(1, E))
7102 return false;
7103 if (!this->emitSubOffsetUint8(E))
7104 return false;
7105 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
7106 }
7107
7108 // Post-dec and pre-dec are the same if the value is to be discarded.
7109 if (DiscardResult) {
7110 if (T == PT_Float)
7111 return this->emitDecfPop(getFPOptions(E), E);
7112 if (SubExpr->refersToBitField())
7113 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
7114 getBitWidth(SubExpr), E)
7115 : this->emitDecBitfield(*T, E->canOverflow(),
7116 getBitWidth(SubExpr), E);
7117 return this->emitDecPop(*T, E->canOverflow(), E);
7118 }
7119
7120 if (T == PT_Float) {
7121 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
7122 if (!this->emitLoadFloat(E))
7123 return false;
7124 APFloat F(TargetSemantics, 1);
7125 if (!this->emitFloat(F, E))
7126 return false;
7127
7128 if (!this->emitSubf(getFPOptions(E), E))
7129 return false;
7130 if (!this->emitStoreFloat(E))
7131 return false;
7132 } else if (SubExpr->refersToBitField()) {
7133 assert(isIntegerOrBoolType(*T));
7134 if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
7135 E))
7136 return false;
7137 } else {
7138 assert(isIntegerOrBoolType(*T));
7139 if (!this->emitPreDec(*T, E->canOverflow(), E))
7140 return false;
7141 }
7142 return E->isGLValue() || this->emitLoadPop(*T, E);
7143 }
7144 case UO_LNot: // !x
7145 if (!T)
7146 return this->emitError(E);
7147
7148 if (DiscardResult)
7149 return this->discard(SubExpr);
7150
7151 if (!this->visitBool(SubExpr))
7152 return false;
7153
7154 if (!this->emitInv(E))
7155 return false;
7156
7157 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7158 return this->emitCast(PT_Bool, ET, E);
7159 return true;
7160 case UO_Minus: // -x
7161 if (!T)
7162 return this->emitError(E);
7163
7164 if (!this->visit(SubExpr))
7165 return false;
7166 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
7167 case UO_Plus: // +x
7168 if (!T)
7169 return this->emitError(E);
7170
7171 if (!this->visit(SubExpr)) // noop
7172 return false;
7173 return DiscardResult ? this->emitPop(*T, E) : true;
7174 case UO_AddrOf: // &x
7175 if (E->getType()->isMemberPointerType()) {
7176 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
7177 // member can be formed.
7178 if (DiscardResult)
7179 return true;
7180 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
7181 }
7182 // We should already have a pointer when we get here.
7183 return this->delegate(SubExpr);
7184 case UO_Deref: // *x
7185 if (DiscardResult)
7186 return this->discard(SubExpr);
7187
7188 if (!this->visit(SubExpr))
7189 return false;
7190
7191 if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
7192 return false;
7193
7194 if (classifyPrim(SubExpr) == PT_Ptr)
7195 return this->emitNarrowPtr(E);
7196 return true;
7197
7198 case UO_Not: // ~x
7199 if (!T)
7200 return this->emitError(E);
7201
7202 if (!this->visit(SubExpr))
7203 return false;
7204 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
7205 case UO_Real: // __real x
7206 if (!T)
7207 return false;
7208 return this->delegate(SubExpr);
7209 case UO_Imag: { // __imag x
7210 if (!T)
7211 return false;
7212 if (!this->discard(SubExpr))
7213 return false;
7214 return DiscardResult
7215 ? true
7216 : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
7217 }
7218 case UO_Extension:
7219 return this->delegate(SubExpr);
7220 case UO_Coawait:
7221 assert(false && "Unhandled opcode");
7222 }
7223
7224 return false;
7225}
7226
7227template <class Emitter>
7229 const Expr *SubExpr = E->getSubExpr();
7230 assert(SubExpr->getType()->isAnyComplexType());
7231
7232 if (DiscardResult)
7233 return this->discard(SubExpr);
7234
7235 OptPrimType ResT = classify(E);
7236 auto prepareResult = [=]() -> bool {
7237 if (!ResT && !Initializing) {
7238 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7239 if (!LocalIndex)
7240 return false;
7241 return this->emitGetPtrLocal(*LocalIndex, E);
7242 }
7243
7244 return true;
7245 };
7246
7247 // The offset of the temporary, if we created one.
7248 unsigned SubExprOffset = ~0u;
7249 auto createTemp = [=, &SubExprOffset]() -> bool {
7250 SubExprOffset =
7251 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7252 if (!this->visit(SubExpr))
7253 return false;
7254 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
7255 };
7256
7257 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
7258 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7259 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7260 return false;
7261 return this->emitArrayElemPop(ElemT, Index, E);
7262 };
7263
7264 switch (E->getOpcode()) {
7265 case UO_Minus: // -x
7266 if (!prepareResult())
7267 return false;
7268 if (!createTemp())
7269 return false;
7270 for (unsigned I = 0; I != 2; ++I) {
7271 if (!getElem(SubExprOffset, I))
7272 return false;
7273 if (!this->emitNeg(ElemT, E))
7274 return false;
7275 if (!this->emitInitElem(ElemT, I, E))
7276 return false;
7277 }
7278 break;
7279
7280 case UO_Plus: // +x
7281 case UO_AddrOf: // &x
7282 case UO_Deref: // *x
7283 return this->delegate(SubExpr);
7284
7285 case UO_LNot:
7286 if (!this->visit(SubExpr))
7287 return false;
7288 if (!this->emitComplexBoolCast(SubExpr))
7289 return false;
7290 if (!this->emitInv(E))
7291 return false;
7292 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
7293 return this->emitCast(PT_Bool, ET, E);
7294 return true;
7295
7296 case UO_Real:
7297 return this->emitComplexReal(SubExpr);
7298
7299 case UO_Imag:
7300 if (!this->visit(SubExpr))
7301 return false;
7302
7303 if (SubExpr->isLValue()) {
7304 if (!this->emitConstUint8(1, E))
7305 return false;
7306 return this->emitArrayElemPtrPopUint8(E);
7307 }
7308
7309 // Since our _Complex implementation does not map to a primitive type,
7310 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
7311 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
7312
7313 case UO_Not: // ~x
7314 if (!this->delegate(SubExpr))
7315 return false;
7316 // Negate the imaginary component.
7317 if (!this->emitArrayElem(ElemT, 1, E))
7318 return false;
7319 if (!this->emitNeg(ElemT, E))
7320 return false;
7321 if (!this->emitInitElem(ElemT, 1, E))
7322 return false;
7323 return DiscardResult ? this->emitPopPtr(E) : true;
7324
7325 case UO_Extension:
7326 return this->delegate(SubExpr);
7327
7328 default:
7329 return this->emitInvalid(E);
7330 }
7331
7332 return true;
7333}
7334
7335template <class Emitter>
7337 const Expr *SubExpr = E->getSubExpr();
7338 assert(SubExpr->getType()->isVectorType());
7339
7340 if (DiscardResult)
7341 return this->discard(SubExpr);
7342
7343 auto UnaryOp = E->getOpcode();
7344 if (UnaryOp == UO_Extension)
7345 return this->delegate(SubExpr);
7346
7347 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
7348 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
7349 return this->emitInvalid(E);
7350
7351 // Nothing to do here.
7352 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
7353 return this->delegate(SubExpr);
7354
7355 if (!Initializing) {
7356 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
7357 if (!LocalIndex)
7358 return false;
7359 if (!this->emitGetPtrLocal(*LocalIndex, E))
7360 return false;
7361 }
7362
7363 // The offset of the temporary, if we created one.
7364 unsigned SubExprOffset =
7365 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
7366 if (!this->visit(SubExpr))
7367 return false;
7368 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
7369 return false;
7370
7371 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
7372 PrimType ElemT = classifyVectorElementType(SubExpr->getType());
7373 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
7374 if (!this->emitGetLocal(PT_Ptr, Offset, E))
7375 return false;
7376 return this->emitArrayElemPop(ElemT, Index, E);
7377 };
7378
7379 switch (UnaryOp) {
7380 case UO_Minus:
7381 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7382 if (!getElem(SubExprOffset, I))
7383 return false;
7384 if (!this->emitNeg(ElemT, E))
7385 return false;
7386 if (!this->emitInitElem(ElemT, I, E))
7387 return false;
7388 }
7389 break;
7390 case UO_LNot: { // !x
7391 // In C++, the logic operators !, &&, || are available for vectors. !v is
7392 // equivalent to v == 0.
7393 //
7394 // The result of the comparison is a vector of the same width and number of
7395 // elements as the comparison operands with a signed integral element type.
7396 //
7397 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
7398 QualType ResultVecTy = E->getType();
7399 PrimType ResultVecElemT =
7400 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
7401 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7402 if (!getElem(SubExprOffset, I))
7403 return false;
7404 // operator ! on vectors returns -1 for 'truth', so negate it.
7405 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
7406 return false;
7407 if (!this->emitInv(E))
7408 return false;
7409 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
7410 return false;
7411 if (!this->emitNeg(ElemT, E))
7412 return false;
7413 if (ElemT != ResultVecElemT &&
7414 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
7415 return false;
7416 if (!this->emitInitElem(ResultVecElemT, I, E))
7417 return false;
7418 }
7419 break;
7420 }
7421 case UO_Not: // ~x
7422 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
7423 if (!getElem(SubExprOffset, I))
7424 return false;
7425 if (ElemT == PT_Bool) {
7426 if (!this->emitInv(E))
7427 return false;
7428 } else {
7429 if (!this->emitComp(ElemT, E))
7430 return false;
7431 }
7432 if (!this->emitInitElem(ElemT, I, E))
7433 return false;
7434 }
7435 break;
7436 default:
7437 llvm_unreachable("Unsupported unary operators should be handled up front");
7438 }
7439 return true;
7440}
7441
7442template <class Emitter>
7444 if (DiscardResult)
7445 return true;
7446
7447 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7448 return this->emitConst(ECD->getInitVal(), E);
7449 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
7450 const Function *F = getFunction(FuncDecl);
7451 return F && this->emitGetFnPtr(F, E);
7452 }
7453 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
7454 if (UnsignedOrNone Index = P.getOrCreateGlobal(D)) {
7455 if (!this->emitGetPtrGlobal(*Index, E))
7456 return false;
7457 if (OptPrimType T = classify(E->getType())) {
7458 if (!this->visitAPValue(TPOD->getValue(), *T, E))
7459 return false;
7460 return this->emitInitGlobal(*T, *Index, E);
7461 }
7462 if (!this->visitAPValueInitializer(TPOD->getValue(), E, TPOD->getType()))
7463 return false;
7464 return this->emitFinishInit(E);
7465 }
7466 return false;
7467 }
7468
7469 // References are implemented via pointers, so when we see a DeclRefExpr
7470 // pointing to a reference, we need to get its value directly (i.e. the
7471 // pointer to the actual value) instead of a pointer to the pointer to the
7472 // value.
7473 QualType DeclType = D->getType();
7474 bool IsReference = DeclType->isReferenceType();
7475
7476 // Function parameters.
7477 // Note that it's important to check them first since we might have a local
7478 // variable created for a ParmVarDecl as well.
7479 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
7480 if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
7481 !DeclType->isIntegralOrEnumerationType()) {
7482 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7483 /*InitializerFailed=*/false, E);
7484 }
7485 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
7486 if (IsReference || !It->second.IsPtr)
7487 return this->emitGetParam(classifyPrim(E), It->second.Index, E);
7488
7489 return this->emitGetPtrParam(It->second.Index, E);
7490 }
7491
7492 if (!Ctx.getLangOpts().CPlusPlus23 && IsReference)
7493 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7494 /*InitializerFailed=*/false, E);
7495 }
7496 // Local variables.
7497 if (auto It = Locals.find(D); It != Locals.end()) {
7498 const unsigned Offset = It->second.Offset;
7499 if (IsReference)
7500 return this->emitGetLocal(classifyPrim(E), Offset, E);
7501 return this->emitGetPtrLocal(Offset, E);
7502 }
7503 // Global variables.
7504 if (auto GlobalIndex = P.getGlobal(D)) {
7505 if (IsReference) {
7506 if (!Ctx.getLangOpts().CPlusPlus11)
7507 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
7508 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
7509 }
7510
7511 return this->emitGetPtrGlobal(*GlobalIndex, E);
7512 }
7513
7514 // In case we need to re-visit a declaration.
7515 auto revisit = [&](const VarDecl *VD) -> bool {
7516 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
7517 return false;
7518 auto VarState = this->visitDecl(VD, /*IsConstexprUnknown=*/true);
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);
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 // Revisit the variable declaration, but make sure it's associated with a
7601 // different evaluation, so e.g. mutable reads don't work on it.
7602 EvalIDScope _(Ctx);
7603 return revisit(VD);
7604 }
7605
7606 if (IsReference)
7607 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
7608 /*InitializerFailed=*/true, E);
7609 }
7610
7611 return this->emitDummyPtr(D, E);
7612}
7613
7614template <class Emitter>
7616 const auto *D = E->getDecl();
7617 return this->visitDeclRef(D, E);
7618}
7619
7620template <class Emitter> bool Compiler<Emitter>::emitCleanup() {
7621 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) {
7622 if (!C->destroyLocals())
7623 return false;
7624 }
7625 return true;
7626}
7627
7628template <class Emitter>
7629unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
7630 const QualType DerivedType) {
7631 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
7632 if (const auto *R = Ty->getPointeeCXXRecordDecl())
7633 return R;
7634 return Ty->getAsCXXRecordDecl();
7635 };
7636 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
7637 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
7638
7639 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
7640}
7641
7642/// Emit casts from a PrimType to another PrimType.
7643template <class Emitter>
7644bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
7645 QualType ToQT, const Expr *E) {
7646
7647 if (FromT == PT_Float) {
7648 // Floating to floating.
7649 if (ToT == PT_Float) {
7650 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7651 return this->emitCastFP(ToSem, getRoundingMode(E), E);
7652 }
7653
7654 if (ToT == PT_IntAP)
7655 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
7656 getFPOptions(E), E);
7657 if (ToT == PT_IntAPS)
7658 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
7659 getFPOptions(E), E);
7660
7661 // Float to integral.
7662 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7663 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
7664 }
7665
7666 if (isIntegerOrBoolType(FromT) || FromT == PT_Bool) {
7667 if (ToT == PT_IntAP)
7668 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7669 if (ToT == PT_IntAPS)
7670 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7671
7672 // Integral to integral.
7673 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
7674 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
7675
7676 if (ToT == PT_Float) {
7677 // Integral to floating.
7678 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
7679 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
7680 }
7681 }
7682
7683 return false;
7684}
7685
7686template <class Emitter>
7687bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT,
7688 QualType ToQT, const Expr *E) {
7689 assert(FromT != ToT);
7690
7691 if (ToT == PT_IntAP)
7692 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
7693 if (ToT == PT_IntAPS)
7694 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
7695
7696 return this->emitCast(FromT, ToT, E);
7697}
7698
7699/// Emits __real(SubExpr)
7700template <class Emitter>
7701bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
7702 assert(SubExpr->getType()->isAnyComplexType());
7703
7704 if (DiscardResult)
7705 return this->discard(SubExpr);
7706
7707 if (!this->visit(SubExpr))
7708 return false;
7709 if (SubExpr->isLValue()) {
7710 if (!this->emitConstUint8(0, SubExpr))
7711 return false;
7712 return this->emitArrayElemPtrPopUint8(SubExpr);
7713 }
7714
7715 // Rvalue, load the actual element.
7716 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
7717 0, SubExpr);
7718}
7719
7720template <class Emitter>
7721bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
7722 assert(!DiscardResult);
7723 PrimType ElemT = classifyComplexElementType(E->getType());
7724 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
7725 // for us, that means (bool)E[0] || (bool)E[1]
7726 if (!this->emitArrayElem(ElemT, 0, E))
7727 return false;
7728 if (ElemT == PT_Float) {
7729 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7730 return false;
7731 } else {
7732 if (!this->emitCast(ElemT, PT_Bool, E))
7733 return false;
7734 }
7735
7736 // We now have the bool value of E[0] on the stack.
7737 LabelTy LabelTrue = this->getLabel();
7738 if (!this->jumpTrue(LabelTrue, E))
7739 return false;
7740
7741 if (!this->emitArrayElemPop(ElemT, 1, E))
7742 return false;
7743 if (ElemT == PT_Float) {
7744 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
7745 return false;
7746 } else {
7747 if (!this->emitCast(ElemT, PT_Bool, E))
7748 return false;
7749 }
7750 // Leave the boolean value of E[1] on the stack.
7751 LabelTy EndLabel = this->getLabel();
7752 this->jump(EndLabel, E);
7753
7754 this->emitLabel(LabelTrue);
7755 if (!this->emitPopPtr(E))
7756 return false;
7757 if (!this->emitConstBool(true, E))
7758 return false;
7759
7760 this->fallthrough(EndLabel);
7761 this->emitLabel(EndLabel);
7762
7763 return true;
7764}
7765
7766template <class Emitter>
7767bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
7768 const BinaryOperator *E) {
7769 assert(E->isComparisonOp());
7770 assert(!Initializing);
7771 if (DiscardResult)
7772 return this->discard(LHS) && this->discard(RHS);
7773
7774 PrimType ElemT;
7775 bool LHSIsComplex;
7776 unsigned LHSOffset;
7777 if (LHS->getType()->isAnyComplexType()) {
7778 LHSIsComplex = true;
7779 ElemT = classifyComplexElementType(LHS->getType());
7780 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
7781 if (!this->visit(LHS))
7782 return false;
7783 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
7784 return false;
7785 } else {
7786 LHSIsComplex = false;
7787 PrimType LHST = classifyPrim(LHS->getType());
7788 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
7789 if (!this->visit(LHS))
7790 return false;
7791 if (!this->emitSetLocal(LHST, LHSOffset, E))
7792 return false;
7793 }
7794
7795 bool RHSIsComplex;
7796 unsigned RHSOffset;
7797 if (RHS->getType()->isAnyComplexType()) {
7798 RHSIsComplex = true;
7799 ElemT = classifyComplexElementType(RHS->getType());
7800 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
7801 if (!this->visit(RHS))
7802 return false;
7803 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
7804 return false;
7805 } else {
7806 RHSIsComplex = false;
7807 PrimType RHST = classifyPrim(RHS->getType());
7808 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
7809 if (!this->visit(RHS))
7810 return false;
7811 if (!this->emitSetLocal(RHST, RHSOffset, E))
7812 return false;
7813 }
7814
7815 auto getElem = [&](unsigned LocalOffset, unsigned Index,
7816 bool IsComplex) -> bool {
7817 if (IsComplex) {
7818 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
7819 return false;
7820 return this->emitArrayElemPop(ElemT, Index, E);
7821 }
7822 return this->emitGetLocal(ElemT, LocalOffset, E);
7823 };
7824
7825 for (unsigned I = 0; I != 2; ++I) {
7826 // Get both values.
7827 if (!getElem(LHSOffset, I, LHSIsComplex))
7828 return false;
7829 if (!getElem(RHSOffset, I, RHSIsComplex))
7830 return false;
7831 // And compare them.
7832 if (!this->emitEQ(ElemT, E))
7833 return false;
7834
7835 if (!this->emitCastBoolUint8(E))
7836 return false;
7837 }
7838
7839 // We now have two bool values on the stack. Compare those.
7840 if (!this->emitAddUint8(E))
7841 return false;
7842 if (!this->emitConstUint8(2, E))
7843 return false;
7844
7845 if (E->getOpcode() == BO_EQ) {
7846 if (!this->emitEQUint8(E))
7847 return false;
7848 } else if (E->getOpcode() == BO_NE) {
7849 if (!this->emitNEUint8(E))
7850 return false;
7851 } else
7852 return false;
7853
7854 // In C, this returns an int.
7855 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
7856 return this->emitCast(PT_Bool, ResT, E);
7857 return true;
7858}
7859
7860/// When calling this, we have a pointer of the local-to-destroy
7861/// on the stack.
7862/// Emit destruction of record types (or arrays of record types).
7863template <class Emitter>
7864bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R,
7865 SourceInfo Loc) {
7866 assert(R);
7867 assert(!R->hasTrivialDtor());
7868 const CXXDestructorDecl *Dtor = R->getDestructor();
7869 assert(Dtor);
7870 const Function *DtorFunc = getFunction(Dtor);
7871 if (!DtorFunc)
7872 return false;
7873 assert(DtorFunc->hasThisPointer());
7874 assert(DtorFunc->getNumParams() == 1);
7875 return this->emitCall(DtorFunc, 0, Loc);
7876}
7877/// When calling this, we have a pointer of the local-to-destroy
7878/// on the stack.
7879/// Emit destruction of record types (or arrays of record types).
7880template <class Emitter>
7881bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc,
7882 SourceInfo Loc) {
7883 assert(Desc);
7884 assert(!Desc->hasTrivialDtor());
7885
7886 // Arrays.
7887 if (Desc->isArray()) {
7888 const Descriptor *ElemDesc = Desc->ElemDesc;
7889 assert(ElemDesc);
7890
7891 unsigned N = Desc->getNumElems();
7892 if (N == 0)
7893 return this->emitPopPtr(Loc);
7894
7895 for (ssize_t I = N - 1; I >= 1; --I) {
7896 if (!this->emitConstUint64(I, Loc))
7897 return false;
7898 if (!this->emitArrayElemPtrUint64(Loc))
7899 return false;
7900 if (!this->emitDestructionPop(ElemDesc, Loc))
7901 return false;
7902 }
7903 // Last iteration, removes the instance pointer from the stack.
7904 if (!this->emitConstUint64(0, Loc))
7905 return false;
7906 if (!this->emitArrayElemPtrPopUint64(Loc))
7907 return false;
7908 return this->emitDestructionPop(ElemDesc, Loc);
7909 }
7910
7911 assert(Desc->ElemRecord);
7912 assert(!Desc->ElemRecord->hasTrivialDtor());
7913 return this->emitRecordDestructionPop(Desc->ElemRecord, Loc);
7914}
7915
7916/// Create a dummy pointer for the given decl (or expr) and
7917/// push a pointer to it on the stack.
7918template <class Emitter>
7919bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E) {
7920 assert(!DiscardResult && "Should've been checked before");
7921 unsigned DummyID = P.getOrCreateDummy(D);
7922
7923 if (!this->emitGetPtrGlobal(DummyID, E))
7924 return false;
7925 if (E->getType()->isVoidType())
7926 return true;
7927
7928 // Convert the dummy pointer to another pointer type if we have to.
7929 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
7930 if (isPtrType(PT))
7931 return this->emitDecayPtr(PT_Ptr, PT, E);
7932 return false;
7933 }
7934 return true;
7935}
7936
7937template <class Emitter>
7938bool Compiler<Emitter>::emitFloat(const APFloat &F, const Expr *E) {
7939 if (Floating::singleWord(F.getSemantics()))
7940 return this->emitConstFloat(Floating(F), E);
7941
7942 APInt I = F.bitcastToAPInt();
7943 return this->emitConstFloat(
7944 Floating(const_cast<uint64_t *>(I.getRawData()),
7945 llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
7946 E);
7947}
7948
7949// This function is constexpr if and only if To, From, and the types of
7950// all subobjects of To and From are types T such that...
7951// (3.1) - is_union_v<T> is false;
7952// (3.2) - is_pointer_v<T> is false;
7953// (3.3) - is_member_pointer_v<T> is false;
7954// (3.4) - is_volatile_v<T> is false; and
7955// (3.5) - T has no non-static data members of reference type
7956template <class Emitter>
7957bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
7958 const Expr *SubExpr = E->getSubExpr();
7959 QualType FromType = SubExpr->getType();
7960 QualType ToType = E->getType();
7961 OptPrimType ToT = classify(ToType);
7962
7963 assert(!ToType->isReferenceType());
7964
7965 // Prepare storage for the result in case we discard.
7966 if (DiscardResult && !Initializing && !ToT) {
7967 UnsignedOrNone LocalIndex = allocateLocal(E);
7968 if (!LocalIndex)
7969 return false;
7970 if (!this->emitGetPtrLocal(*LocalIndex, E))
7971 return false;
7972 }
7973
7974 // Get a pointer to the value-to-cast on the stack.
7975 // For CK_LValueToRValueBitCast, this is always an lvalue and
7976 // we later assume it to be one (i.e. a PT_Ptr). However,
7977 // we call this function for other utility methods where
7978 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
7979 if (SubExpr->isGLValue() || FromType->isVectorType()) {
7980 if (!this->visit(SubExpr))
7981 return false;
7982 } else if (OptPrimType FromT = classify(SubExpr)) {
7983 unsigned TempOffset =
7984 allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
7985 if (!this->visit(SubExpr))
7986 return false;
7987 if (!this->emitSetLocal(*FromT, TempOffset, E))
7988 return false;
7989 if (!this->emitGetPtrLocal(TempOffset, E))
7990 return false;
7991 } else {
7992 return false;
7993 }
7994
7995 if (!ToT) {
7996 if (!this->emitBitCast(E))
7997 return false;
7998 return DiscardResult ? this->emitPopPtr(E) : true;
7999 }
8000 assert(ToT);
8001
8002 const llvm::fltSemantics *TargetSemantics = nullptr;
8003 if (ToT == PT_Float)
8004 TargetSemantics = &Ctx.getFloatSemantics(ToType);
8005
8006 // Conversion to a primitive type. FromType can be another
8007 // primitive type, or a record/array.
8008 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
8009 ToType->isSpecificBuiltinType(BuiltinType::Char_U));
8010 uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
8011
8012 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
8013 ResultBitWidth, TargetSemantics,
8014 ToType.getTypePtr(), E))
8015 return false;
8016
8017 if (DiscardResult)
8018 return this->emitPop(*ToT, E);
8019
8020 return true;
8021}
8022
8023namespace clang {
8024namespace interp {
8025
8026template class Compiler<ByteCodeEmitter>;
8027template class Compiler<EvalEmitter>;
8028
8029} // namespace interp
8030} // 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)
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:3499
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:2611
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition DeclCXX.cpp:3037
Represents a C++ base or member initializer.
Definition DeclCXX.h:2376
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:2876
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:2262
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:2863
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:2109
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:588
bool hasAttr() const
Definition DeclBase.h:577
OverloadedOperatorKind getCXXOverloadedOperator() const
If this name is the name of an overloadable operator in C++ (e.g., operator+), retrieve the kind of o...
Stmt * getSubStmt()
Definition Stmt.h: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:3280
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4206
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4194
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3763
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:4330
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:3427
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:3827
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3200
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:3311
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:4401
APValue & getAsAPValue() const
Get the value of this MSGuidDecl as an APValue.
Definition DeclCXX.cpp:3832
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:3204
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:4139
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:3059
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:3243
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:3262
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:2614
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:3099
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:5582
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:2587
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:2660
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:464
bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E)
bool VisitCXXDeleteExpr(const CXXDeleteExpr *E)
bool VisitOffsetOfExpr(const OffsetOfExpr *E)
bool visitContinueStmt(const ContinueStmt *S)
bool VisitCharacterLiteral(const CharacterLiteral *E)
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init, OptPrimType InitT)
Pointer to the array(not the element!) must be on the stack when calling this.
bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E)
bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E)
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E)
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
Definition Compiler.h: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:442
bool VisitBinaryOperator(const BinaryOperator *E)
bool visitAttributedStmt(const AttributedStmt *S)
bool VisitPackIndexingExpr(const PackIndexingExpr *E)
VarCreationState visitDecl(const VarDecl *VD, bool IsConstexprUnknown=false)
bool visitAPValueInitializer(const APValue &Val, const Expr *E, QualType T)
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
bool VisitCallExpr(const CallExpr *E)
std::optional< uint64_t > ArrayIndex
Current argument index. Needed to emit ArrayInitIndexExpr.
Definition Compiler.h:448
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)
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:475
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E)
VariableScope< Emitter > * VarScope
Current scope.
Definition Compiler.h:445
bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext) override
Toplevel visitDeclAndReturn().
bool VisitCXXNewExpr(const CXXNewExpr *E)
const ValueDecl * InitializingDecl
Definition Compiler.h:462
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:471
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst, bool IsVolatile=false, ScopeKind SC=ScopeKind::Block, bool IsConstexprUnknown=false)
Creates a local primitive value.
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:451
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
bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E)
bool visitStmt(const Stmt *S)
bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E)
bool VisitVectorUnaryOperator(const UnaryOperator *E)
bool VisitCXXConstructExpr(const CXXConstructExpr *E)
bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
VarCreationState visitVarDecl(const VarDecl *VD, const Expr *Init, bool Toplevel=false, bool IsConstexprUnknown=false)
Creates and initializes a variable from the given decl.
bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E)
bool VisitRecoveryExpr(const RecoveryExpr *E)
bool VisitRequiresExpr(const RequiresExpr *E)
bool Initializing
Flag inidicating if we're initializing an already created variable.
Definition Compiler.h:461
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:439
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:468
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:473
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:454
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)
UnsignedOrNone allocateLocal(DeclTy &&Decl, QualType Ty=QualType(), ScopeKind=ScopeKind::Block, bool IsConstexprUnknown=false)
Allocates a space storing a local given its type.
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:540
bool destroyLocals(const Expr *E=nullptr) override
Explicit destruction of local variables.
Definition Compiler.h:553
LocalScope(Compiler< Emitter > *Ctx, ScopeKind Kind=ScopeKind::Block)
Definition Compiler.h:542
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:482
Compiler< Emitter > * Ctx
Compiler instance.
Definition Compiler.h:533
virtual void addLocal(Scope::Local Local)
Definition Compiler.h:493
VariableScope * getParent() const
Definition Compiler.h:523
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:358
bool LT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1331
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:645
static bool Activate(InterpState &S, CodePtr OpPC)
Definition Interp.h:2045
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:2521
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:1338
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:2162
bool Mul(InterpState &S, CodePtr OpPC)
Definition Interp.h:378
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:832
bool Add(InterpState &S, CodePtr OpPC)
Definition Interp.h:338
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