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