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