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