clang 23.0.0git
Context.cpp
Go to the documentation of this file.
1//===--- Context.cpp - Context for the constexpr VM -------------*- 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 "Context.h"
10#include "Boolean.h"
11#include "ByteCodeEmitter.h"
12#include "Char.h"
13#include "Compiler.h"
14#include "EvalEmitter.h"
15#include "Integral.h"
16#include "InterpFrame.h"
17#include "InterpHelpers.h"
18#include "InterpStack.h"
19#include "Pointer.h"
20#include "PrimType.h"
21#include "Program.h"
22#include "clang/AST/ASTLambda.h"
23#include "clang/AST/Expr.h"
25
26using namespace clang;
27using namespace clang::interp;
28
29Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {
30 this->ShortWidth = Ctx.getTargetInfo().getShortWidth();
31 this->IntWidth = Ctx.getTargetInfo().getIntWidth();
32 this->LongWidth = Ctx.getTargetInfo().getLongWidth();
33 this->LongLongWidth = Ctx.getTargetInfo().getLongLongWidth();
34 assert(Ctx.getTargetInfo().getCharWidth() == 8 &&
35 "We're assuming 8 bit chars");
36}
37
38Context::~Context() = default;
39
41 assert(Stk.empty());
42
43 // Get a function handle.
45 if (!Func)
46 return false;
47
48 // Compile the function.
49 Compiler<ByteCodeEmitter>(*this, *P).compileFunc(
50 FD, const_cast<Function *>(Func));
51
52 if (!Func->isValid())
53 return false;
54
55 ++EvalID;
56 // And run it.
57 return Run(Parent, Func);
58}
59
61 const FunctionDecl *FD) {
62 assert(Stk.empty());
63 ++EvalID;
64 size_t StackSizeBefore = Stk.size();
65 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
66
67 if (!C.interpretCall(FD, E)) {
68 C.cleanup();
69 Stk.clearTo(StackSizeBefore);
70 }
71}
72
74 ++EvalID;
75 bool Recursing = !Stk.empty();
76 size_t StackSizeBefore = Stk.size();
77 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
78
79 auto Res = C.interpretExpr(E, /*ConvertResultToRValue=*/E->isGLValue());
80
81 if (Res.isInvalid()) {
82 C.cleanup();
83 Stk.clearTo(StackSizeBefore);
84 return false;
85 }
86
87 if (!Recursing) {
88 // We *can* actually get here with a non-empty stack, since
89 // things like InterpState::noteSideEffect() exist.
90 C.cleanup();
91#ifndef NDEBUG
92 // Make sure we don't rely on some value being still alive in
93 // InterpStack memory.
94 Stk.clearTo(StackSizeBefore);
95#endif
96 }
97
98 Result = Res.stealAPValue();
99
100 return true;
101}
102
103bool Context::evaluate(State &Parent, const Expr *E, APValue &Result,
104 ConstantExprKind Kind) {
105 ++EvalID;
106 bool Recursing = !Stk.empty();
107 size_t StackSizeBefore = Stk.size();
108 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
109
110 auto Res = C.interpretExpr(E, /*ConvertResultToRValue=*/false,
111 /*DestroyToplevelScope=*/true);
112 if (Res.isInvalid()) {
113 C.cleanup();
114 Stk.clearTo(StackSizeBefore);
115 return false;
116 }
117
118 if (!Recursing) {
119 assert(Stk.empty());
120 C.cleanup();
121#ifndef NDEBUG
122 // Make sure we don't rely on some value being still alive in
123 // InterpStack memory.
124 Stk.clearTo(StackSizeBefore);
125#endif
126 }
127
128 Result = Res.stealAPValue();
129 return true;
130}
131
133 const Expr *Init, APValue &Result) {
134 ++EvalID;
135 bool Recursing = !Stk.empty();
136 size_t StackSizeBefore = Stk.size();
137 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
138
139 bool CheckGlobalInitialized =
141 (VD->getType()->isRecordType() || VD->getType()->isArrayType());
142 auto Res = C.interpretDecl(VD, Init, CheckGlobalInitialized);
143 if (Res.isInvalid()) {
144 C.cleanup();
145 Stk.clearTo(StackSizeBefore);
146
147 return false;
148 }
149
150 if (!Recursing) {
151 assert(Stk.empty());
152 C.cleanup();
153#ifndef NDEBUG
154 // Make sure we don't rely on some value being still alive in
155 // InterpStack memory.
156 Stk.clearTo(StackSizeBefore);
157#endif
158 }
159
160 Result = Res.stealAPValue();
161 return true;
162}
163
165 APValue Value) {
166 assert(Stk.empty());
167 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
168
169 auto Res = C.interpretDestructor(VD, Value);
170
171 if (Res.isInvalid()) {
172 C.cleanup();
173 Stk.clear();
174 return false;
175 }
176
177 assert(Stk.empty());
178
179 return true;
180}
181
182template <typename ResultT>
183bool Context::evaluateStringRepr(State &Parent, const Expr *SizeExpr,
184 const Expr *PtrExpr, ResultT &Result) {
185 assert(Stk.empty());
186 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
187
188 // Evaluate size value.
189 APValue SizeValue;
190 if (!evaluateAsRValue(Parent, SizeExpr, SizeValue))
191 return false;
192
193 if (!SizeValue.isInt())
194 return false;
195 uint64_t Size = SizeValue.getInt().getZExtValue();
196
197 auto PtrRes = C.interpretAsPointer(PtrExpr, [&](InterpState &S, CodePtr OpPC,
198 const Pointer &Ptr) {
199 if (Size == 0) {
200 if constexpr (std::is_same_v<ResultT, APValue>)
202 return true;
203 }
204
205 if (Ptr.isZero()) {
206 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_null)
207 << AK_Read;
208 return false;
209 }
210
211 if (!Ptr.isLive() || !Ptr.getFieldDesc()->isPrimitiveArray())
212 return false;
213
214 // Must be char.
215 if (Ptr.getFieldDesc()->getElemDataSize() != 1 /*bytes*/)
216 return false;
217
218 if (Size > Ptr.getNumElems()) {
219 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_past_end)
220 << AK_Read;
221 Size = Ptr.getNumElems();
222 }
223
224 if constexpr (std::is_same_v<ResultT, APValue>) {
225 QualType CharTy = PtrExpr->getType()->getPointeeType();
226 Result = APValue(APValue::UninitArray{}, Size, Size);
227 for (uint64_t I = 0; I != Size; ++I) {
228 if (std::optional<APValue> ElemVal =
229 Ptr.atIndex(I).toRValue(*this, CharTy))
230 Result.getArrayInitializedElt(I) = *ElemVal;
231 else
232 return false;
233 }
234 } else {
235 assert((std::is_same_v<ResultT, std::string>));
236 if (Size < Result.max_size())
237 Result.resize(Size);
238 Result.assign(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
239 }
240
241 return true;
242 });
243
244 if (PtrRes.isInvalid()) {
245 C.cleanup();
246 Stk.clear();
247 return false;
248 }
249
250 return true;
251}
252
253bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
254 const Expr *PtrExpr, APValue &Result) {
255 assert(SizeExpr);
256 assert(PtrExpr);
257
258 return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
259}
260
261bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
262 const Expr *PtrExpr, std::string &Result) {
263 assert(SizeExpr);
264 assert(PtrExpr);
265
266 return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
267}
268
269bool Context::evaluateString(State &Parent, const Expr *E,
270 std::string &Result) {
271 assert(Stk.empty());
272 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
273
274 auto PtrRes = C.interpretAsPointer(E, [&](InterpState &S, CodePtr OpPC,
275 const Pointer &Ptr) {
276 if (!Ptr.isBlockPointer())
277 return false;
278
279 const Descriptor *FieldDesc = Ptr.getFieldDesc();
280 if (!FieldDesc->isPrimitiveArray())
281 return false;
282
283 if (!Ptr.isConst())
284 return false;
285
286 unsigned N = Ptr.getNumElems();
287
288 if (Ptr.elemSize() == 1 /* bytes */) {
289 const char *Chars = reinterpret_cast<const char *>(Ptr.getRawAddress());
290 unsigned Length = strnlen(Chars, N);
291 // Wasn't null terminated.
292 if (N == Length)
293 return false;
294 Result.assign(Chars, Length);
295 return true;
296 }
297
298 PrimType ElemT = FieldDesc->getPrimType();
299 for (unsigned I = Ptr.getIndex(); I != N; ++I) {
300 INT_TYPE_SWITCH(ElemT, {
301 auto Elem = Ptr.elem<T>(I);
302 if (Elem.isZero())
303 return true;
304 Result.push_back(static_cast<char>(Elem));
305 });
306 }
307 // We didn't find a 0 byte.
308 return false;
309 });
310
311 if (PtrRes.isInvalid()) {
312 C.cleanup();
313 Stk.clear();
314 return false;
315 }
316 return true;
317}
318
319std::optional<uint64_t> Context::evaluateStrlen(State &Parent, const Expr *E) {
320 assert(Stk.empty());
321 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
322
323 std::optional<uint64_t> Result;
324 auto PtrRes = C.interpretAsPointer(E, [&](InterpState &S, CodePtr OpPC,
325 const Pointer &Ptr) {
326 if (!Ptr.isBlockPointer())
327 return false;
328
329 const Descriptor *FieldDesc = Ptr.getFieldDesc();
330 if (!FieldDesc->isPrimitiveArray())
331 return false;
332
333 if (Ptr.isDummy() || Ptr.isUnknownSizeArray() || Ptr.isPastEnd())
334 return false;
335
336 PrimType ElemT = FieldDesc->getPrimType();
337 if (!isIntegerType(ElemT))
338 return false;
339
340 unsigned N = Ptr.getNumElems();
341 if (Ptr.elemSize() == 1) {
342 unsigned Size = N - Ptr.getIndex();
343 Result =
344 strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
345 return Result != Size;
346 }
347
348 Result = 0;
349 for (unsigned I = Ptr.getIndex(); I != N; ++I) {
350 INT_TYPE_SWITCH(ElemT, {
351 auto Elem = Ptr.elem<T>(I);
352 if (Elem.isZero())
353 return true;
354 ++(*Result);
355 });
356 }
357 // We didn't find a 0 byte.
358 return false;
359 });
360
361 if (PtrRes.isInvalid()) {
362 C.cleanup();
363 Stk.clear();
364 return std::nullopt;
365 }
366 return Result;
367}
368
369std::optional<uint64_t>
370Context::tryEvaluateObjectSize(State &Parent, const Expr *E, unsigned Kind) {
371 assert(Stk.empty());
372 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
373
374 std::optional<uint64_t> Result;
375
376 auto PtrRes = C.interpretAsLValuePointer(E, [&](InterpState &S, CodePtr OpPC,
377 const Pointer &Ptr) {
378 const Descriptor *DeclDesc = Ptr.getDeclDesc();
379 if (!DeclDesc)
380 return false;
381
382 QualType T = DeclDesc->getType().getNonReferenceType();
383 if (T->isIncompleteType() || T->isFunctionType() ||
384 !T->isConstantSizeType())
385 return false;
386
387 Pointer P = Ptr;
388 if (auto ObjectSize = evaluateBuiltinObjectSize(getASTContext(), Kind, P)) {
389 Result = *ObjectSize;
390 return true;
391 }
392 return false;
393 });
394
395 if (PtrRes.isInvalid()) {
396 C.cleanup();
397 Stk.clear();
398 return std::nullopt;
399 }
400 return Result;
401}
402
403std::optional<bool>
405 ArrayRef<const Expr *> Args, const Expr *This,
406 const Expr *Condition) {
407 if (OptPrimType ConditionT = classify(Condition);
408 !ConditionT || ConditionT != PT_Bool) {
409 return std::nullopt;
410 }
411
412 assert(Stk.empty());
413 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
414 std::optional<bool> Result =
415 C.interpretWithSubstitutions(Callee, Args, This, Condition);
416
417 // This is somewhat of a special case here. We don't allow
418 // evaluateWithSubstitution to recurse (see the Stk.empty() assertion above),
419 // BUT we allow the args to fail evaluation, which means they can leave some
420 // garbage on the stack. So we always clear() here, not only if the evaluation
421 // failed.
422 Stk.clear();
423 if (!Result) {
424 C.cleanup();
425 return std::nullopt;
426 }
427 return Result;
428}
429
430const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
431
432static PrimType integralTypeToPrimTypeS(unsigned BitWidth) {
433 switch (BitWidth) {
434 case 64:
435 return PT_Sint64;
436 case 32:
437 return PT_Sint32;
438 case 16:
439 return PT_Sint16;
440 case 8:
441 return PT_Sint8;
442 default:
443 return PT_IntAPS;
444 }
445 llvm_unreachable("Unhandled BitWidth");
446}
447
448static PrimType integralTypeToPrimTypeU(unsigned BitWidth) {
449 switch (BitWidth) {
450 case 64:
451 return PT_Uint64;
452 case 32:
453 return PT_Uint32;
454 case 16:
455 return PT_Uint16;
456 case 8:
457 return PT_Uint8;
458 default:
459 return PT_IntAP;
460 }
461 llvm_unreachable("Unhandled BitWidth");
462}
463
465
466 if (const auto *BT = dyn_cast<BuiltinType>(T.getCanonicalType())) {
467 auto Kind = BT->getKind();
468 if (Kind == BuiltinType::Bool)
469 return PT_Bool;
470 if (Kind == BuiltinType::NullPtr)
471 return PT_Ptr;
472 if (Kind == BuiltinType::BoundMember)
473 return PT_MemberPtr;
474
475 // Just trying to avoid the ASTContext::getIntWidth call below.
476 if (Kind == BuiltinType::Short)
477 return integralTypeToPrimTypeS(this->ShortWidth);
478 if (Kind == BuiltinType::UShort)
479 return integralTypeToPrimTypeU(this->ShortWidth);
480
481 if (Kind == BuiltinType::Int)
482 return integralTypeToPrimTypeS(this->IntWidth);
483 if (Kind == BuiltinType::UInt)
484 return integralTypeToPrimTypeU(this->IntWidth);
485 if (Kind == BuiltinType::Long)
486 return integralTypeToPrimTypeS(this->LongWidth);
487 if (Kind == BuiltinType::ULong)
488 return integralTypeToPrimTypeU(this->LongWidth);
489 if (Kind == BuiltinType::LongLong)
490 return integralTypeToPrimTypeS(this->LongLongWidth);
491 if (Kind == BuiltinType::ULongLong)
492 return integralTypeToPrimTypeU(this->LongLongWidth);
493
494 if (Kind == BuiltinType::SChar || Kind == BuiltinType::Char_S)
495 return integralTypeToPrimTypeS(8);
496 if (Kind == BuiltinType::UChar || Kind == BuiltinType::Char_U ||
497 Kind == BuiltinType::Char8)
498 return integralTypeToPrimTypeU(8);
499
500 if (BT->isSignedInteger())
501 return integralTypeToPrimTypeS(Ctx.getIntWidth(T));
502 if (BT->isUnsignedInteger())
503 return integralTypeToPrimTypeU(Ctx.getIntWidth(T));
504
505 if (BT->isFloatingPoint())
506 return PT_Float;
507 }
508
509 if (T->isPointerOrReferenceType())
510 return PT_Ptr;
511
512 if (T->isMemberPointerType())
513 return PT_MemberPtr;
514
515 if (const auto *BT = T->getAs<BitIntType>()) {
516 if (BT->isSigned())
517 return integralTypeToPrimTypeS(BT->getNumBits());
518 return integralTypeToPrimTypeU(BT->getNumBits());
519 }
520
521 if (const auto *D = T->getAsEnumDecl()) {
522 if (!D->isComplete())
523 return std::nullopt;
524 return classify(D->getIntegerType());
525 }
526
527 if (const auto *AT = T->getAs<AtomicType>())
528 return classify(AT->getValueType());
529
530 if (const auto *DT = dyn_cast<DecltypeType>(T))
531 return classify(DT->getUnderlyingType());
532
533 if (const auto *OBT = T.getCanonicalType()->getAs<OverflowBehaviorType>())
534 return classify(OBT->getUnderlyingType());
535
536 if (T->isObjCObjectPointerType() || T->isBlockPointerType())
537 return PT_Ptr;
538
539 if (T->isFixedPointType())
540 return PT_FixedPoint;
541
542 // Vector and complex types get here.
543 return std::nullopt;
544}
545
546unsigned Context::getCharBit() const {
547 return Ctx.getTargetInfo().getCharWidth();
548}
549
550/// Simple wrapper around getFloatTypeSemantics() to make code a
551/// little shorter.
552const llvm::fltSemantics &Context::getFloatSemantics(QualType T) const {
553 return Ctx.getFloatTypeSemantics(T);
554}
555
556bool Context::Run(State &Parent, const Function *Func) {
557 InterpState State(Parent, *P, Stk, *this, Func);
558 auto Memory = std::make_unique<char[]>(InterpFrame::allocSize(Func));
559 InterpFrame *Frame = new (Memory.get()) InterpFrame(
560 State, Func, /*Caller=*/nullptr, CodePtr(), Func->getArgSize());
561 State.Current = Frame;
562
563 if (Interpret(State)) {
564 assert(Stk.empty());
565 return true;
566 }
567
568 Stk.clear();
569 Frame->~InterpFrame();
570 State.Current = &State.BottomFrame;
571 return false;
572}
573
574// TODO: Virtual bases?
575const CXXMethodDecl *
577 const CXXRecordDecl *StaticDecl,
578 const CXXMethodDecl *InitialFunction) const {
579 assert(DynamicDecl);
580 assert(StaticDecl);
581 assert(InitialFunction);
582
583 const CXXRecordDecl *CurRecord = DynamicDecl;
584 const CXXMethodDecl *FoundFunction = InitialFunction;
585 for (;;) {
586 const CXXMethodDecl *Overrider =
587 FoundFunction->getCorrespondingMethodDeclaredInClass(CurRecord, false);
588 if (Overrider)
589 return Overrider;
590
591 // Common case of only one base class.
592 if (CurRecord->getNumBases() == 1) {
593 CurRecord = CurRecord->bases_begin()->getType()->getAsCXXRecordDecl();
594 continue;
595 }
596
597 // Otherwise, go to the base class that will lead to the StaticDecl.
598 for (const CXXBaseSpecifier &Spec : CurRecord->bases()) {
599 const CXXRecordDecl *Base = Spec.getType()->getAsCXXRecordDecl();
600 if (Base == StaticDecl || Base->isDerivedFrom(StaticDecl)) {
601 CurRecord = Base;
602 break;
603 }
604 }
605 }
606
607 llvm_unreachable(
608 "Couldn't find an overriding function in the class hierarchy?");
609 return nullptr;
610}
611
613 assert(FuncDecl);
614 if (const Function *Func = P->getFunction(FuncDecl))
615 return Func;
616
617 // Manually created functions that haven't been assigned proper
618 // parameters yet.
619 if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
620 return nullptr;
621
622 bool IsLambdaStaticInvoker = false;
623 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
624 MD && MD->isLambdaStaticInvoker()) {
625 // For a lambda static invoker, we might have to pick a specialized
626 // version if the lambda is generic. In that case, the picked function
627 // will *NOT* be a static invoker anymore. However, it will still
628 // be a non-static member function, this (usually) requiring an
629 // instance pointer. We suppress that later in this function.
630 IsLambdaStaticInvoker = true;
631 }
632 // Set up argument indices.
633 unsigned ParamOffset = 0;
635
636 // If the return is not a primitive, a pointer to the storage where the
637 // value is initialized in is passed as the first argument. See 'RVO'
638 // elsewhere in the code.
639 QualType Ty = FuncDecl->getReturnType();
640 bool HasRVO = false;
641 if (!Ty->isVoidType() && !canClassify(Ty)) {
642 HasRVO = true;
644 }
645
646 // If the function decl is a member decl, the next parameter is
647 // the 'this' pointer. This parameter is pop()ed from the
648 // InterpStack when calling the function.
649 bool HasThisPointer = false;
650 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) {
651 if (!IsLambdaStaticInvoker) {
652 HasThisPointer = MD->isInstance();
653 if (MD->isImplicitObjectMemberFunction())
655 }
656
657 if (isLambdaCallOperator(MD)) {
658 // The parent record needs to be complete, we need to know about all
659 // the lambda captures.
660 if (!MD->getParent()->isCompleteDefinition())
661 return nullptr;
662 if (MD->isStatic()) {
663 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
664 FieldDecl *LTC;
665
666 MD->getParent()->getCaptureFields(LC, LTC);
667 // Static lambdas cannot have any captures. If this one does,
668 // it has already been diagnosed and we can only ignore it.
669 if (!LC.empty())
670 return nullptr;
671 }
672 }
673 }
674
675 // Assign descriptors to all parameters.
676 // Composite objects are lowered to pointers.
677 const auto *FuncProto = FuncDecl->getType()->getAs<FunctionProtoType>();
678 unsigned BlockOffset = 0;
679 for (auto [ParamIndex, PD] : llvm::enumerate(FuncDecl->parameters())) {
680 bool IsConst = PD->getType().isConstQualified();
681 bool IsVolatile = PD->getType().isVolatileQualified();
682
683 if (!getASTContext().hasSameType(PD->getType(),
684 FuncProto->getParamType(ParamIndex)))
685 return nullptr;
686
687 OptPrimType T = classify(PD->getType());
688 PrimType PT = T.value_or(PT_Ptr);
689 Descriptor *Desc = P->createDescriptor(PD, PT, nullptr, std::nullopt,
690 IsConst, /*IsTemporary=*/false,
691 /*IsMutable=*/false, IsVolatile);
692 unsigned PrimTSize = align(primSize(PT));
693 ParamDescriptors.emplace_back(Desc, ParamOffset, BlockOffset, PT);
694 ParamOffset += PrimTSize;
695 BlockOffset += sizeof(Block) + PrimTSize;
696 }
697
698 // Create a handle over the emitted code.
699 assert(!P->getFunction(FuncDecl));
700 const Function *Func =
701 P->createFunction(FuncDecl, ParamOffset, std::move(ParamDescriptors),
702 HasThisPointer, HasRVO, IsLambdaStaticInvoker);
703 return Func;
704}
705
707 const BlockDecl *BD = E->getBlockDecl();
708 // Set up argument indices.
709 unsigned ParamOffset = 0;
711
712 // Assign descriptors to all parameters.
713 // Composite objects are lowered to pointers.
714 for (const ParmVarDecl *PD : BD->parameters()) {
715 bool IsConst = PD->getType().isConstQualified();
716 bool IsVolatile = PD->getType().isVolatileQualified();
717
718 OptPrimType T = classify(PD->getType());
719 PrimType PT = T.value_or(PT_Ptr);
720 Descriptor *Desc = P->createDescriptor(PD, PT, nullptr, std::nullopt,
721 IsConst, /*IsTemporary=*/false,
722 /*IsMutable=*/false, IsVolatile);
723 ParamDescriptors.emplace_back(Desc, ParamOffset, ~0u, PT);
724 ParamOffset += align(primSize(PT));
725 }
726
727 if (BD->hasCaptures())
728 return nullptr;
729
730 // Create a handle over the emitted code.
731 Function *Func =
732 P->createFunction(E, ParamOffset, std::move(ParamDescriptors),
733 /*HasThisPointer=*/false, /*HasRVO=*/false,
734 /*IsLambdaStaticInvoker=*/false);
735
736 assert(Func);
737 Func->setDefined(true);
738 // We don't compile the BlockDecl code at all right now.
739 Func->setIsFullyCompiled(true);
740
741 return Func;
742}
743
744unsigned Context::collectBaseOffset(const RecordDecl *BaseDecl,
745 const RecordDecl *DerivedDecl) const {
746 assert(BaseDecl);
747 assert(DerivedDecl);
748 const auto *FinalDecl = cast<CXXRecordDecl>(BaseDecl);
749 const RecordDecl *CurDecl = DerivedDecl;
750 const Record *CurRecord = P->getOrCreateRecord(CurDecl);
751 assert(CurDecl && FinalDecl);
752
753 unsigned OffsetSum = 0;
754 for (;;) {
755 assert(CurRecord->getNumBases() > 0);
756 // One level up
757 for (const Record::Base &B : CurRecord->bases()) {
758 const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
759
760 if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
761 OffsetSum += B.Offset;
762 CurRecord = B.R;
763 CurDecl = BaseDecl;
764 break;
765 }
766 }
767 if (CurDecl == FinalDecl)
768 break;
769 }
770
771 assert(OffsetSum > 0);
772 return OffsetSum;
773}
774
775const Record *Context::getRecord(const RecordDecl *D) const {
776 return P->getOrCreateRecord(D);
777}
778
780 return ID == Builtin::BI__builtin_classify_type ||
781 ID == Builtin::BI__builtin_os_log_format_buffer_size ||
782 ID == Builtin::BI__builtin_constant_p || ID == Builtin::BI__noop;
783}
This file provides some common utility functions for processing Lambda related AST Constructs.
static PrimType integralTypeToPrimTypeS(unsigned BitWidth)
Definition Context.cpp:432
static PrimType integralTypeToPrimTypeU(unsigned BitWidth)
Definition Context.cpp:448
#define INT_TYPE_SWITCH(Expr, B)
Definition PrimType.h:244
static bool isRecordType(QualType T)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
APSInt & getInt()
Definition APValue.h:508
bool isInt() const
Definition APValue.h:485
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
A fixed int type of a specified bitwidth.
Definition TypeBase.h:8299
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4703
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
Definition Decl.h:4822
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:4789
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition Expr.h:6675
const BlockDecl * getBlockDecl() const
Definition Expr.h:6687
Represents a base class of a C++ class.
Definition DeclCXX.h:146
QualType getType() const
Retrieves the type of the base class.
Definition DeclCXX.h:249
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
CXXMethodDecl * getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD, bool MayBeBase=false)
Find if RD declares a function that overrides this function, and if so, return it.
Definition DeclCXX.cpp:2437
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
base_class_range bases()
Definition DeclCXX.h:608
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition DeclCXX.h:602
base_class_iterator bases_begin()
Definition DeclCXX.h:615
This represents one expression.
Definition Expr.h:112
bool isGLValue() const
Definition Expr.h:287
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3191
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3427
Represents a function declaration or definition.
Definition Decl.h:2027
QualType getReturnType() const
Definition Decl.h:2872
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2801
param_iterator param_begin()
Definition Decl.h:2813
bool param_empty() const
Definition Decl.h:2812
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5371
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Represents a parameter to a function.
Definition Decl.h:1817
A (possibly-)qualified type.
Definition TypeBase.h:937
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition TypeBase.h:8632
Represents a struct/union/class.
Definition Decl.h:4356
bool isVoidType() const
Definition TypeBase.h:9050
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isArrayType() const
Definition TypeBase.h:8783
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9277
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:932
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
Pointer into the code segment.
Definition Source.h:30
Compilation context for expressions.
Definition Compiler.h:112
const LangOptions & getLangOpts() const
Returns the language options.
Definition Context.cpp:430
const Function * getOrCreateObjCBlock(const BlockExpr *E)
Definition Context.cpp:706
~Context()
Cleans up the constexpr VM.
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition Context.cpp:29
bool evaluateCharRange(State &Parent, const Expr *SizeExpr, const Expr *PtrExpr, APValue &Result)
Definition Context.cpp:253
std::optional< uint64_t > evaluateStrlen(State &Parent, const Expr *E)
Evalute.
Definition Context.cpp:319
std::optional< bool > evaluateWithSubstitution(State &Parent, const FunctionDecl *Callee, ArrayRef< const Expr * > Args, const Expr *This, const Expr *Condition)
Definition Context.cpp:404
bool evaluateString(State &Parent, const Expr *E, std::string &Result)
Evaluate.
Definition Context.cpp:269
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition Context.cpp:779
unsigned getCharBit() const
Returns CHAR_BIT.
Definition Context.cpp:546
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition Context.cpp:552
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:165
void isPotentialConstantExprUnevaluated(State &Parent, const Expr *E, const FunctionDecl *FD)
Definition Context.cpp:60
unsigned collectBaseOffset(const RecordDecl *BaseDecl, const RecordDecl *DerivedDecl) const
Definition Context.cpp:744
bool evaluateDestruction(State &Parent, const VarDecl *VD, APValue Value)
Evaluates the destruction of a variable.
Definition Context.cpp:164
const Record * getRecord(const RecordDecl *D) const
Definition Context.cpp:775
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FD)
Checks if a function is a potential constant expression.
Definition Context.cpp:40
const Function * getOrCreateFunction(const FunctionDecl *FuncDecl)
Definition Context.cpp:612
ASTContext & getASTContext() const
Returns the AST context.
Definition Context.h:107
OptPrimType classify(QualType T) const
Classifies a type.
Definition Context.cpp:464
bool canClassify(QualType T) const
Definition Context.h:129
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition Context.cpp:73
const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, const CXXMethodDecl *InitialFunction) const
Definition Context.cpp:576
std::optional< uint64_t > tryEvaluateObjectSize(State &Parent, const Expr *E, unsigned Kind)
If.
Definition Context.cpp:370
bool evaluate(State &Parent, const Expr *E, APValue &Result, ConstantExprKind Kind)
Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
Definition Context.cpp:103
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, const Expr *Init, APValue &Result)
Evaluates a toplevel initializer.
Definition Context.cpp:132
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:99
Frame storing local variables.
Definition InterpFrame.h:27
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
static size_t allocSize(const Function *F)
Returns the number of bytes needed to allocate an InterpFrame for the given function.
Definition InterpFrame.h:51
void clear()
Clears the stack.
bool empty() const
Returns whether the stack is empty.
Definition InterpStack.h:84
Interpreter context.
Definition InterpState.h:43
InterpFrame * Current
The current frame.
A pointer to a memory block, live or dead.
Definition Pointer.h:394
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:460
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:751
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:813
bool isConst() const
Checks if an object or a subfield is mutable.
Definition Pointer.h:758
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:797
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:625
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:511
T & elem(unsigned I) const
Dereferences the element at index I.
Definition Pointer.h:876
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:497
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:525
bool isPastEnd() const
Checks if the pointer points past the end of the object.
Definition Pointer.h:835
bool isBlockPointer() const
Definition Pointer.h:668
std::optional< APValue > toRValue(const Context &Ctx, QualType ResultType) const
Converts the pointer to an APValue that is an rvalue.
Definition Pointer.cpp:901
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:553
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:582
const std::byte * getRawAddress() const
If backed by actual data (i.e.
Definition Pointer.h:807
The program contains and links the bytecode for all functions.
Definition Program.h:36
Structure/Class descriptor.
Definition Record.h:25
unsigned getNumBases() const
Definition Record.h:106
llvm::iterator_range< const_base_iter > bases() const
Definition Record.h:102
Interface for the VM to interact with the AST walker's context.
Definition State.h:81
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:21
Defines the clang::TargetInfo interface.
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:201
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:3172
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2382
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:24
constexpr bool isIntegerType(PrimType T)
Definition PrimType.h:53
UnsignedOrNone evaluateBuiltinObjectSize(const ASTContext &ASTCtx, unsigned Kind, Pointer &Ptr)
bool Interpret(InterpState &S)
Interpreter entry point.
Definition Interp.cpp:3153
The JSON file list parser is used to communicate input to InstallAPI.
Expr::ConstantExprKind ConstantExprKind
Definition Expr.h:1048
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition ASTLambda.h:28
@ Result
The result type of a method or function.
Definition TypeBase.h:905
@ AK_Read
Definition State.h:29
U cast(CodeGen::Address addr)
Definition Address.h:327
Describes a memory block created by an allocation site.
Definition Descriptor.h:123
QualType getType() const
unsigned getElemDataSize() const
Returns the element data size, i.e.
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:265
PrimType getPrimType() const
Definition Descriptor.h:242