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 return true;
178}
179
180template <typename ResultT>
181bool Context::evaluateStringRepr(State &Parent, const Expr *SizeExpr,
182 const Expr *PtrExpr, ResultT &Result) {
183 assert(Stk.empty());
184 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
185
186 // Evaluate size value.
187 APValue SizeValue;
188 if (!evaluateAsRValue(Parent, SizeExpr, SizeValue))
189 return false;
190
191 if (!SizeValue.isInt())
192 return false;
193 uint64_t Size = SizeValue.getInt().getZExtValue();
194
195 auto PtrRes = C.interpretAsPointer(PtrExpr, [&](const Pointer &Ptr) {
196 if (Size == 0) {
197 if constexpr (std::is_same_v<ResultT, APValue>)
199 return true;
200 }
201
202 if (!Ptr.isLive() || !Ptr.getFieldDesc()->isPrimitiveArray())
203 return false;
204
205 // Must be char.
206 if (Ptr.getFieldDesc()->getElemDataSize() != 1 /*bytes*/)
207 return false;
208
209 if (Size > Ptr.getNumElems()) {
210 Parent.FFDiag(SizeExpr, diag::note_constexpr_access_past_end) << AK_Read;
211 Size = Ptr.getNumElems();
212 }
213
214 if constexpr (std::is_same_v<ResultT, APValue>) {
215 QualType CharTy = PtrExpr->getType()->getPointeeType();
216 Result = APValue(APValue::UninitArray{}, Size, Size);
217 for (uint64_t I = 0; I != Size; ++I) {
218 if (std::optional<APValue> ElemVal =
219 Ptr.atIndex(I).toRValue(*this, CharTy))
220 Result.getArrayInitializedElt(I) = *ElemVal;
221 else
222 return false;
223 }
224 } else {
225 assert((std::is_same_v<ResultT, std::string>));
226 if (Size < Result.max_size())
227 Result.resize(Size);
228 Result.assign(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
229 }
230
231 return true;
232 });
233
234 if (PtrRes.isInvalid()) {
235 C.cleanup();
236 Stk.clear();
237 return false;
238 }
239
240 return true;
241}
242
243bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
244 const Expr *PtrExpr, APValue &Result) {
245 assert(SizeExpr);
246 assert(PtrExpr);
247
248 return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
249}
250
251bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
252 const Expr *PtrExpr, std::string &Result) {
253 assert(SizeExpr);
254 assert(PtrExpr);
255
256 return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
257}
258
259bool Context::evaluateString(State &Parent, const Expr *E,
260 std::string &Result) {
261 assert(Stk.empty());
262 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
263
264 auto PtrRes = C.interpretAsPointer(E, [&](const Pointer &Ptr) {
265 if (!Ptr.isBlockPointer())
266 return false;
267
268 const Descriptor *FieldDesc = Ptr.getFieldDesc();
269 if (!FieldDesc->isPrimitiveArray())
270 return false;
271
272 if (!Ptr.isConst())
273 return false;
274
275 unsigned N = Ptr.getNumElems();
276
277 if (Ptr.elemSize() == 1 /* bytes */) {
278 const char *Chars = reinterpret_cast<const char *>(Ptr.getRawAddress());
279 unsigned Length = strnlen(Chars, N);
280 // Wasn't null terminated.
281 if (N == Length)
282 return false;
283 Result.assign(Chars, Length);
284 return true;
285 }
286
287 PrimType ElemT = FieldDesc->getPrimType();
288 for (unsigned I = Ptr.getIndex(); I != N; ++I) {
289 INT_TYPE_SWITCH(ElemT, {
290 auto Elem = Ptr.elem<T>(I);
291 if (Elem.isZero())
292 return true;
293 Result.push_back(static_cast<char>(Elem));
294 });
295 }
296 // We didn't find a 0 byte.
297 return false;
298 });
299
300 if (PtrRes.isInvalid()) {
301 C.cleanup();
302 Stk.clear();
303 return false;
304 }
305 return true;
306}
307
308std::optional<uint64_t> Context::evaluateStrlen(State &Parent, const Expr *E) {
309 assert(Stk.empty());
310 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
311
312 std::optional<uint64_t> Result;
313 auto PtrRes = C.interpretAsPointer(E, [&](const Pointer &Ptr) {
314 if (!Ptr.isBlockPointer())
315 return false;
316
317 const Descriptor *FieldDesc = Ptr.getFieldDesc();
318 if (!FieldDesc->isPrimitiveArray())
319 return false;
320
321 if (Ptr.isDummy() || Ptr.isUnknownSizeArray() || Ptr.isPastEnd())
322 return false;
323
324 PrimType ElemT = FieldDesc->getPrimType();
325 if (!isIntegerType(ElemT))
326 return false;
327
328 unsigned N = Ptr.getNumElems();
329 if (Ptr.elemSize() == 1) {
330 unsigned Size = N - Ptr.getIndex();
331 Result =
332 strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
333 return Result != Size;
334 }
335
336 Result = 0;
337 for (unsigned I = Ptr.getIndex(); I != N; ++I) {
338 INT_TYPE_SWITCH(ElemT, {
339 auto Elem = Ptr.elem<T>(I);
340 if (Elem.isZero())
341 return true;
342 ++(*Result);
343 });
344 }
345 // We didn't find a 0 byte.
346 return false;
347 });
348
349 if (PtrRes.isInvalid()) {
350 C.cleanup();
351 Stk.clear();
352 return std::nullopt;
353 }
354 return Result;
355}
356
357std::optional<uint64_t>
358Context::tryEvaluateObjectSize(State &Parent, const Expr *E, unsigned Kind) {
359 assert(Stk.empty());
360 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
361
362 std::optional<uint64_t> Result;
363
364 auto PtrRes = C.interpretAsLValuePointer(E, [&](const Pointer &Ptr) {
365 const Descriptor *DeclDesc = Ptr.getDeclDesc();
366 if (!DeclDesc)
367 return false;
368
369 QualType T = DeclDesc->getType().getNonReferenceType();
370 if (T->isIncompleteType() || T->isFunctionType() ||
371 !T->isConstantSizeType())
372 return false;
373
374 Pointer P = Ptr;
375 if (auto ObjectSize = evaluateBuiltinObjectSize(getASTContext(), Kind, P)) {
376 Result = *ObjectSize;
377 return true;
378 }
379 return false;
380 });
381
382 if (PtrRes.isInvalid()) {
383 C.cleanup();
384 Stk.clear();
385 return std::nullopt;
386 }
387 return Result;
388}
389
390const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
391
392static PrimType integralTypeToPrimTypeS(unsigned BitWidth) {
393 switch (BitWidth) {
394 case 64:
395 return PT_Sint64;
396 case 32:
397 return PT_Sint32;
398 case 16:
399 return PT_Sint16;
400 case 8:
401 return PT_Sint8;
402 default:
403 return PT_IntAPS;
404 }
405 llvm_unreachable("Unhandled BitWidth");
406}
407
408static PrimType integralTypeToPrimTypeU(unsigned BitWidth) {
409 switch (BitWidth) {
410 case 64:
411 return PT_Uint64;
412 case 32:
413 return PT_Uint32;
414 case 16:
415 return PT_Uint16;
416 case 8:
417 return PT_Uint8;
418 default:
419 return PT_IntAP;
420 }
421 llvm_unreachable("Unhandled BitWidth");
422}
423
425
426 if (const auto *BT = dyn_cast<BuiltinType>(T.getCanonicalType())) {
427 auto Kind = BT->getKind();
428 if (Kind == BuiltinType::Bool)
429 return PT_Bool;
430 if (Kind == BuiltinType::NullPtr)
431 return PT_Ptr;
432 if (Kind == BuiltinType::BoundMember)
433 return PT_MemberPtr;
434
435 // Just trying to avoid the ASTContext::getIntWidth call below.
436 if (Kind == BuiltinType::Short)
437 return integralTypeToPrimTypeS(this->ShortWidth);
438 if (Kind == BuiltinType::UShort)
439 return integralTypeToPrimTypeU(this->ShortWidth);
440
441 if (Kind == BuiltinType::Int)
442 return integralTypeToPrimTypeS(this->IntWidth);
443 if (Kind == BuiltinType::UInt)
444 return integralTypeToPrimTypeU(this->IntWidth);
445 if (Kind == BuiltinType::Long)
446 return integralTypeToPrimTypeS(this->LongWidth);
447 if (Kind == BuiltinType::ULong)
448 return integralTypeToPrimTypeU(this->LongWidth);
449 if (Kind == BuiltinType::LongLong)
450 return integralTypeToPrimTypeS(this->LongLongWidth);
451 if (Kind == BuiltinType::ULongLong)
452 return integralTypeToPrimTypeU(this->LongLongWidth);
453
454 if (Kind == BuiltinType::SChar || Kind == BuiltinType::Char_S)
455 return integralTypeToPrimTypeS(8);
456 if (Kind == BuiltinType::UChar || Kind == BuiltinType::Char_U ||
457 Kind == BuiltinType::Char8)
458 return integralTypeToPrimTypeU(8);
459
460 if (BT->isSignedInteger())
461 return integralTypeToPrimTypeS(Ctx.getIntWidth(T));
462 if (BT->isUnsignedInteger())
463 return integralTypeToPrimTypeU(Ctx.getIntWidth(T));
464
465 if (BT->isFloatingPoint())
466 return PT_Float;
467 }
468
469 if (T->isPointerOrReferenceType())
470 return PT_Ptr;
471
472 if (T->isMemberPointerType())
473 return PT_MemberPtr;
474
475 if (const auto *BT = T->getAs<BitIntType>()) {
476 if (BT->isSigned())
477 return integralTypeToPrimTypeS(BT->getNumBits());
478 return integralTypeToPrimTypeU(BT->getNumBits());
479 }
480
481 if (const auto *D = T->getAsEnumDecl()) {
482 if (!D->isComplete())
483 return std::nullopt;
484 return classify(D->getIntegerType());
485 }
486
487 if (const auto *AT = T->getAs<AtomicType>())
488 return classify(AT->getValueType());
489
490 if (const auto *DT = dyn_cast<DecltypeType>(T))
491 return classify(DT->getUnderlyingType());
492
493 if (const auto *OBT = T.getCanonicalType()->getAs<OverflowBehaviorType>())
494 return classify(OBT->getUnderlyingType());
495
496 if (T->isObjCObjectPointerType() || T->isBlockPointerType())
497 return PT_Ptr;
498
499 if (T->isFixedPointType())
500 return PT_FixedPoint;
501
502 // Vector and complex types get here.
503 return std::nullopt;
504}
505
506unsigned Context::getCharBit() const {
507 return Ctx.getTargetInfo().getCharWidth();
508}
509
510/// Simple wrapper around getFloatTypeSemantics() to make code a
511/// little shorter.
512const llvm::fltSemantics &Context::getFloatSemantics(QualType T) const {
513 return Ctx.getFloatTypeSemantics(T);
514}
515
516bool Context::Run(State &Parent, const Function *Func) {
517 InterpState State(Parent, *P, Stk, *this, Func);
518 auto Memory = std::make_unique<char[]>(InterpFrame::allocSize(Func));
519 InterpFrame *Frame = new (Memory.get()) InterpFrame(
520 State, Func, /*Caller=*/nullptr, CodePtr(), Func->getArgSize());
521 State.Current = Frame;
522
523 if (Interpret(State)) {
524 assert(Stk.empty());
525 return true;
526 }
527
528 Stk.clear();
529 Frame->~InterpFrame();
530 State.Current = &State.BottomFrame;
531 return false;
532}
533
534// TODO: Virtual bases?
535const CXXMethodDecl *
537 const CXXRecordDecl *StaticDecl,
538 const CXXMethodDecl *InitialFunction) const {
539 assert(DynamicDecl);
540 assert(StaticDecl);
541 assert(InitialFunction);
542
543 const CXXRecordDecl *CurRecord = DynamicDecl;
544 const CXXMethodDecl *FoundFunction = InitialFunction;
545 for (;;) {
546 const CXXMethodDecl *Overrider =
547 FoundFunction->getCorrespondingMethodDeclaredInClass(CurRecord, false);
548 if (Overrider)
549 return Overrider;
550
551 // Common case of only one base class.
552 if (CurRecord->getNumBases() == 1) {
553 CurRecord = CurRecord->bases_begin()->getType()->getAsCXXRecordDecl();
554 continue;
555 }
556
557 // Otherwise, go to the base class that will lead to the StaticDecl.
558 for (const CXXBaseSpecifier &Spec : CurRecord->bases()) {
559 const CXXRecordDecl *Base = Spec.getType()->getAsCXXRecordDecl();
560 if (Base == StaticDecl || Base->isDerivedFrom(StaticDecl)) {
561 CurRecord = Base;
562 break;
563 }
564 }
565 }
566
567 llvm_unreachable(
568 "Couldn't find an overriding function in the class hierarchy?");
569 return nullptr;
570}
571
573 assert(FuncDecl);
574 if (const Function *Func = P->getFunction(FuncDecl))
575 return Func;
576
577 // Manually created functions that haven't been assigned proper
578 // parameters yet.
579 if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
580 return nullptr;
581
582 bool IsLambdaStaticInvoker = false;
583 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
584 MD && MD->isLambdaStaticInvoker()) {
585 // For a lambda static invoker, we might have to pick a specialized
586 // version if the lambda is generic. In that case, the picked function
587 // will *NOT* be a static invoker anymore. However, it will still
588 // be a non-static member function, this (usually) requiring an
589 // instance pointer. We suppress that later in this function.
590 IsLambdaStaticInvoker = true;
591 }
592 // Set up argument indices.
593 unsigned ParamOffset = 0;
595
596 // If the return is not a primitive, a pointer to the storage where the
597 // value is initialized in is passed as the first argument. See 'RVO'
598 // elsewhere in the code.
599 QualType Ty = FuncDecl->getReturnType();
600 bool HasRVO = false;
601 if (!Ty->isVoidType() && !canClassify(Ty)) {
602 HasRVO = true;
604 }
605
606 // If the function decl is a member decl, the next parameter is
607 // the 'this' pointer. This parameter is pop()ed from the
608 // InterpStack when calling the function.
609 bool HasThisPointer = false;
610 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) {
611 if (!IsLambdaStaticInvoker) {
612 HasThisPointer = MD->isInstance();
613 if (MD->isImplicitObjectMemberFunction())
615 }
616
617 if (isLambdaCallOperator(MD)) {
618 // The parent record needs to be complete, we need to know about all
619 // the lambda captures.
620 if (!MD->getParent()->isCompleteDefinition())
621 return nullptr;
622 if (MD->isStatic()) {
623 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
624 FieldDecl *LTC;
625
626 MD->getParent()->getCaptureFields(LC, LTC);
627 // Static lambdas cannot have any captures. If this one does,
628 // it has already been diagnosed and we can only ignore it.
629 if (!LC.empty())
630 return nullptr;
631 }
632 }
633 }
634
635 // Assign descriptors to all parameters.
636 // Composite objects are lowered to pointers.
637 const auto *FuncProto = FuncDecl->getType()->getAs<FunctionProtoType>();
638 unsigned BlockOffset = 0;
639 for (auto [ParamIndex, PD] : llvm::enumerate(FuncDecl->parameters())) {
640 bool IsConst = PD->getType().isConstQualified();
641 bool IsVolatile = PD->getType().isVolatileQualified();
642
643 if (!getASTContext().hasSameType(PD->getType(),
644 FuncProto->getParamType(ParamIndex)))
645 return nullptr;
646
647 OptPrimType T = classify(PD->getType());
648 PrimType PT = T.value_or(PT_Ptr);
649 Descriptor *Desc = P->createDescriptor(PD, PT, nullptr, std::nullopt,
650 IsConst, /*IsTemporary=*/false,
651 /*IsMutable=*/false, IsVolatile);
652 unsigned PrimTSize = align(primSize(PT));
653 ParamDescriptors.emplace_back(Desc, ParamOffset, BlockOffset, PT);
654 ParamOffset += PrimTSize;
655 BlockOffset += sizeof(Block) + PrimTSize;
656 }
657
658 // Create a handle over the emitted code.
659 assert(!P->getFunction(FuncDecl));
660 const Function *Func =
661 P->createFunction(FuncDecl, ParamOffset, std::move(ParamDescriptors),
662 HasThisPointer, HasRVO, IsLambdaStaticInvoker);
663 return Func;
664}
665
667 const BlockDecl *BD = E->getBlockDecl();
668 // Set up argument indices.
669 unsigned ParamOffset = 0;
671
672 // Assign descriptors to all parameters.
673 // Composite objects are lowered to pointers.
674 for (const ParmVarDecl *PD : BD->parameters()) {
675 bool IsConst = PD->getType().isConstQualified();
676 bool IsVolatile = PD->getType().isVolatileQualified();
677
678 OptPrimType T = classify(PD->getType());
679 PrimType PT = T.value_or(PT_Ptr);
680 Descriptor *Desc = P->createDescriptor(PD, PT, nullptr, std::nullopt,
681 IsConst, /*IsTemporary=*/false,
682 /*IsMutable=*/false, IsVolatile);
683 ParamDescriptors.emplace_back(Desc, ParamOffset, ~0u, PT);
684 ParamOffset += align(primSize(PT));
685 }
686
687 if (BD->hasCaptures())
688 return nullptr;
689
690 // Create a handle over the emitted code.
691 Function *Func =
692 P->createFunction(E, ParamOffset, std::move(ParamDescriptors),
693 /*HasThisPointer=*/false, /*HasRVO=*/false,
694 /*IsLambdaStaticInvoker=*/false);
695
696 assert(Func);
697 Func->setDefined(true);
698 // We don't compile the BlockDecl code at all right now.
699 Func->setIsFullyCompiled(true);
700
701 return Func;
702}
703
704unsigned Context::collectBaseOffset(const RecordDecl *BaseDecl,
705 const RecordDecl *DerivedDecl) const {
706 assert(BaseDecl);
707 assert(DerivedDecl);
708 const auto *FinalDecl = cast<CXXRecordDecl>(BaseDecl);
709 const RecordDecl *CurDecl = DerivedDecl;
710 const Record *CurRecord = P->getOrCreateRecord(CurDecl);
711 assert(CurDecl && FinalDecl);
712
713 unsigned OffsetSum = 0;
714 for (;;) {
715 assert(CurRecord->getNumBases() > 0);
716 // One level up
717 for (const Record::Base &B : CurRecord->bases()) {
718 const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
719
720 if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
721 OffsetSum += B.Offset;
722 CurRecord = B.R;
723 CurDecl = BaseDecl;
724 break;
725 }
726 }
727 if (CurDecl == FinalDecl)
728 break;
729 }
730
731 assert(OffsetSum > 0);
732 return OffsetSum;
733}
734
735const Record *Context::getRecord(const RecordDecl *D) const {
736 return P->getOrCreateRecord(D);
737}
738
740 return ID == Builtin::BI__builtin_classify_type ||
741 ID == Builtin::BI__builtin_os_log_format_buffer_size ||
742 ID == Builtin::BI__builtin_constant_p || ID == Builtin::BI__noop;
743}
This file provides some common utility functions for processing Lambda related AST Constructs.
static PrimType integralTypeToPrimTypeS(unsigned BitWidth)
Definition Context.cpp:392
static PrimType integralTypeToPrimTypeU(unsigned BitWidth)
Definition Context.cpp:408
#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:226
A fixed int type of a specified bitwidth.
Definition TypeBase.h:8297
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4694
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
Definition Decl.h:4813
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:4780
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition Expr.h:6672
const BlockDecl * getBlockDecl() const
Definition Expr.h:6684
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:2132
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:3182
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3418
Represents a function declaration or definition.
Definition Decl.h:2018
QualType getReturnType() const
Definition Decl.h:2863
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2792
param_iterator param_begin()
Definition Decl.h:2804
bool param_empty() const
Definition Decl.h:2803
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5369
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:1808
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:8630
Represents a struct/union/class.
Definition Decl.h:4347
bool isVoidType() const
Definition TypeBase.h:9048
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:8781
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:9275
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
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:390
const Function * getOrCreateObjCBlock(const BlockExpr *E)
Definition Context.cpp:666
~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:243
std::optional< uint64_t > evaluateStrlen(State &Parent, const Expr *E)
Evalute.
Definition Context.cpp:308
bool evaluateString(State &Parent, const Expr *E, std::string &Result)
Evaluate.
Definition Context.cpp:259
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition Context.cpp:739
unsigned getCharBit() const
Returns CHAR_BIT.
Definition Context.cpp:506
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition Context.cpp:512
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:154
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:704
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:735
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:572
ASTContext & getASTContext() const
Returns the AST context.
Definition Context.h:101
OptPrimType classify(QualType T) const
Classifies a type.
Definition Context.cpp:424
bool canClassify(QualType T) const
Definition Context.h:123
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:536
std::optional< uint64_t > tryEvaluateObjectSize(State &Parent, const Expr *E, unsigned Kind)
If.
Definition Context.cpp:358
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
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:36
A pointer to a memory block, live or dead.
Definition Pointer.h:97
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:162
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:571
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:636
bool isConst() const
Checks if an object or a subfield is mutable.
Definition Pointer.h:581
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:620
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:435
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:278
T & elem(unsigned I) const
Dereferences the element at index I.
Definition Pointer.h:712
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:292
bool isPastEnd() const
Checks if the pointer points past the end of the object.
Definition Pointer.h:667
bool isBlockPointer() const
Definition Pointer.h:488
std::optional< APValue > toRValue(const Context &Ctx, QualType ResultType) const
Converts the pointer to an APValue that is an rvalue.
Definition Pointer.cpp:802
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:336
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:378
const std::byte * getRawAddress() const
If backed by actual data (i.e.
Definition Pointer.h:630
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
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2329
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:2877
The JSON file list parser is used to communicate input to InstallAPI.
Expr::ConstantExprKind ConstantExprKind
Definition Expr.h:1045
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