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