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 "Compiler.h"
13#include "EvalEmitter.h"
14#include "Integral.h"
15#include "InterpFrame.h"
16#include "InterpHelpers.h"
17#include "InterpStack.h"
18#include "Pointer.h"
19#include "PrimType.h"
20#include "Program.h"
21#include "clang/AST/ASTLambda.h"
22#include "clang/AST/Expr.h"
23#include "clang/Basic/TargetInfo.h"
24
25using namespace clang;
26using namespace clang::interp;
27
28Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {
29 this->ShortWidth = Ctx.getTargetInfo().getShortWidth();
30 this->IntWidth = Ctx.getTargetInfo().getIntWidth();
31 this->LongWidth = Ctx.getTargetInfo().getLongWidth();
32 this->LongLongWidth = Ctx.getTargetInfo().getLongLongWidth();
33 assert(Ctx.getTargetInfo().getCharWidth() == 8 &&
34 "We're assuming 8 bit chars");
35}
36
37Context::~Context() {}
38
39bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
40 assert(Stk.empty());
41
42 // Get a function handle.
43 const Function *Func = getOrCreateFunction(FuncDecl: FD);
44 if (!Func)
45 return false;
46
47 // Compile the function.
48 Compiler<ByteCodeEmitter>(*this, *P).compileFunc(
49 FuncDecl: FD, Func: const_cast<Function *>(Func));
50
51 if (!Func->isValid())
52 return false;
53
54 ++EvalID;
55 // And run it.
56 return Run(Parent, Func);
57}
58
59void Context::isPotentialConstantExprUnevaluated(State &Parent, const Expr *E,
60 const FunctionDecl *FD) {
61 assert(Stk.empty());
62 ++EvalID;
63 size_t StackSizeBefore = Stk.size();
64 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
65
66 if (!C.interpretCall(FD, E)) {
67 C.cleanup();
68 Stk.clearTo(NewSize: StackSizeBefore);
69 }
70}
71
72bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
73 ++EvalID;
74 bool Recursing = !Stk.empty();
75 size_t StackSizeBefore = Stk.size();
76 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
77
78 auto Res = C.interpretExpr(E, /*ConvertResultToRValue=*/E->isGLValue());
79
80 if (Res.isInvalid()) {
81 C.cleanup();
82 Stk.clearTo(NewSize: StackSizeBefore);
83 return false;
84 }
85
86 if (!Recursing) {
87 // We *can* actually get here with a non-empty stack, since
88 // things like InterpState::noteSideEffect() exist.
89 C.cleanup();
90#ifndef NDEBUG
91 // Make sure we don't rely on some value being still alive in
92 // InterpStack memory.
93 Stk.clearTo(StackSizeBefore);
94#endif
95 }
96
97 Result = Res.stealAPValue();
98
99 return true;
100}
101
102bool Context::evaluate(State &Parent, const Expr *E, APValue &Result,
103 ConstantExprKind Kind) {
104 ++EvalID;
105 bool Recursing = !Stk.empty();
106 size_t StackSizeBefore = Stk.size();
107 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
108
109 auto Res = C.interpretExpr(E, /*ConvertResultToRValue=*/false,
110 /*DestroyToplevelScope=*/true);
111 if (Res.isInvalid()) {
112 C.cleanup();
113 Stk.clearTo(NewSize: StackSizeBefore);
114 return false;
115 }
116
117 if (!Recursing) {
118 assert(Stk.empty());
119 C.cleanup();
120#ifndef NDEBUG
121 // Make sure we don't rely on some value being still alive in
122 // InterpStack memory.
123 Stk.clearTo(StackSizeBefore);
124#endif
125 }
126
127 Result = Res.stealAPValue();
128 return true;
129}
130
131bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
132 const Expr *Init, APValue &Result) {
133 ++EvalID;
134 bool Recursing = !Stk.empty();
135 size_t StackSizeBefore = Stk.size();
136 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
137
138 bool CheckGlobalInitialized =
139 shouldBeGloballyIndexed(VD) &&
140 (VD->getType()->isRecordType() || VD->getType()->isArrayType());
141 auto Res = C.interpretDecl(VD, Init, CheckFullyInitialized: CheckGlobalInitialized);
142 if (Res.isInvalid()) {
143 C.cleanup();
144 Stk.clearTo(NewSize: StackSizeBefore);
145
146 return false;
147 }
148
149 if (!Recursing) {
150 assert(Stk.empty());
151 C.cleanup();
152#ifndef NDEBUG
153 // Make sure we don't rely on some value being still alive in
154 // InterpStack memory.
155 Stk.clearTo(StackSizeBefore);
156#endif
157 }
158
159 Result = Res.stealAPValue();
160 return true;
161}
162
163template <typename ResultT>
164bool Context::evaluateStringRepr(State &Parent, const Expr *SizeExpr,
165 const Expr *PtrExpr, ResultT &Result) {
166 assert(Stk.empty());
167 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
168
169 // Evaluate size value.
170 APValue SizeValue;
171 if (!evaluateAsRValue(Parent, E: SizeExpr, Result&: SizeValue))
172 return false;
173
174 if (!SizeValue.isInt())
175 return false;
176 uint64_t Size = SizeValue.getInt().getZExtValue();
177
178 auto PtrRes = C.interpretAsPointer(E: PtrExpr, PtrCB: [&](const Pointer &Ptr) {
179 if (Size == 0) {
180 if constexpr (std::is_same_v<ResultT, APValue>)
181 Result = APValue(APValue::UninitArray{}, 0, 0);
182 return true;
183 }
184
185 if (!Ptr.isLive() || !Ptr.getFieldDesc()->isPrimitiveArray())
186 return false;
187
188 // Must be char.
189 if (Ptr.getFieldDesc()->getElemSize() != 1 /*bytes*/)
190 return false;
191
192 if (Size > Ptr.getNumElems()) {
193 Parent.FFDiag(E: SizeExpr, DiagId: diag::note_constexpr_access_past_end) << AK_Read;
194 Size = Ptr.getNumElems();
195 }
196
197 if constexpr (std::is_same_v<ResultT, APValue>) {
198 QualType CharTy = PtrExpr->getType()->getPointeeType();
199 Result = APValue(APValue::UninitArray{}, Size, Size);
200 for (uint64_t I = 0; I != Size; ++I) {
201 if (std::optional<APValue> ElemVal =
202 Ptr.atIndex(Idx: I).toRValue(Ctx: *this, ResultType: CharTy))
203 Result.getArrayInitializedElt(I) = *ElemVal;
204 else
205 return false;
206 }
207 } else {
208 assert((std::is_same_v<ResultT, std::string>));
209 if (Size < Result.max_size())
210 Result.resize(Size);
211 Result.assign(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
212 }
213
214 return true;
215 });
216
217 if (PtrRes.isInvalid()) {
218 C.cleanup();
219 Stk.clear();
220 return false;
221 }
222
223 return true;
224}
225
226bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
227 const Expr *PtrExpr, APValue &Result) {
228 assert(SizeExpr);
229 assert(PtrExpr);
230
231 return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
232}
233
234bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
235 const Expr *PtrExpr, std::string &Result) {
236 assert(SizeExpr);
237 assert(PtrExpr);
238
239 return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
240}
241
242bool Context::evaluateString(State &Parent, const Expr *E,
243 std::string &Result) {
244 assert(Stk.empty());
245 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
246
247 auto PtrRes = C.interpretAsPointer(E, PtrCB: [&](const Pointer &Ptr) {
248 if (!Ptr.isBlockPointer())
249 return false;
250
251 const Descriptor *FieldDesc = Ptr.getFieldDesc();
252 if (!FieldDesc->isPrimitiveArray())
253 return false;
254
255 if (!Ptr.isConst())
256 return false;
257
258 unsigned N = Ptr.getNumElems();
259
260 if (Ptr.elemSize() == 1 /* bytes */) {
261 const char *Chars = reinterpret_cast<const char *>(Ptr.getRawAddress());
262 unsigned Length = strnlen(string: Chars, maxlen: N);
263 // Wasn't null terminated.
264 if (N == Length)
265 return false;
266 Result.assign(s: Chars, n: Length);
267 return true;
268 }
269
270 PrimType ElemT = FieldDesc->getPrimType();
271 for (unsigned I = Ptr.getIndex(); I != N; ++I) {
272 INT_TYPE_SWITCH(ElemT, {
273 auto Elem = Ptr.elem<T>(I);
274 if (Elem.isZero())
275 return true;
276 Result.push_back(static_cast<char>(Elem));
277 });
278 }
279 // We didn't find a 0 byte.
280 return false;
281 });
282
283 if (PtrRes.isInvalid()) {
284 C.cleanup();
285 Stk.clear();
286 return false;
287 }
288 return true;
289}
290
291std::optional<uint64_t> Context::evaluateStrlen(State &Parent, const Expr *E) {
292 assert(Stk.empty());
293 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
294
295 std::optional<uint64_t> Result;
296 auto PtrRes = C.interpretAsPointer(E, PtrCB: [&](const Pointer &Ptr) {
297 if (!Ptr.isBlockPointer())
298 return false;
299
300 const Descriptor *FieldDesc = Ptr.getFieldDesc();
301 if (!FieldDesc->isPrimitiveArray())
302 return false;
303
304 if (Ptr.isDummy() || Ptr.isUnknownSizeArray() || Ptr.isPastEnd())
305 return false;
306
307 unsigned N = Ptr.getNumElems();
308 if (Ptr.elemSize() == 1) {
309 unsigned Size = N - Ptr.getIndex();
310 Result =
311 strnlen(string: reinterpret_cast<const char *>(Ptr.getRawAddress()), maxlen: Size);
312 return Result != Size;
313 }
314
315 PrimType ElemT = FieldDesc->getPrimType();
316 Result = 0;
317 for (unsigned I = Ptr.getIndex(); I != N; ++I) {
318 INT_TYPE_SWITCH(ElemT, {
319 auto Elem = Ptr.elem<T>(I);
320 if (Elem.isZero())
321 return true;
322 ++(*Result);
323 });
324 }
325 // We didn't find a 0 byte.
326 return false;
327 });
328
329 if (PtrRes.isInvalid()) {
330 C.cleanup();
331 Stk.clear();
332 return std::nullopt;
333 }
334 return Result;
335}
336
337std::optional<uint64_t>
338Context::tryEvaluateObjectSize(State &Parent, const Expr *E, unsigned Kind) {
339 assert(Stk.empty());
340 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
341
342 std::optional<uint64_t> Result;
343
344 auto PtrRes = C.interpretAsPointer(E, PtrCB: [&](const Pointer &Ptr) {
345 const Descriptor *DeclDesc = Ptr.getDeclDesc();
346 if (!DeclDesc)
347 return false;
348
349 QualType T = DeclDesc->getType().getNonReferenceType();
350 if (T->isIncompleteType() || T->isFunctionType() ||
351 !T->isConstantSizeType())
352 return false;
353
354 Pointer P = Ptr;
355 if (auto ObjectSize = evaluateBuiltinObjectSize(ASTCtx: getASTContext(), Kind, Ptr&: P)) {
356 Result = *ObjectSize;
357 return true;
358 }
359 return false;
360 });
361
362 if (PtrRes.isInvalid()) {
363 C.cleanup();
364 Stk.clear();
365 return std::nullopt;
366 }
367 return Result;
368}
369
370const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
371
372static PrimType integralTypeToPrimTypeS(unsigned BitWidth) {
373 switch (BitWidth) {
374 case 64:
375 return PT_Sint64;
376 case 32:
377 return PT_Sint32;
378 case 16:
379 return PT_Sint16;
380 case 8:
381 return PT_Sint8;
382 default:
383 return PT_IntAPS;
384 }
385 llvm_unreachable("Unhandled BitWidth");
386}
387
388static PrimType integralTypeToPrimTypeU(unsigned BitWidth) {
389 switch (BitWidth) {
390 case 64:
391 return PT_Uint64;
392 case 32:
393 return PT_Uint32;
394 case 16:
395 return PT_Uint16;
396 case 8:
397 return PT_Uint8;
398 default:
399 return PT_IntAP;
400 }
401 llvm_unreachable("Unhandled BitWidth");
402}
403
404OptPrimType Context::classify(QualType T) const {
405
406 if (const auto *BT = dyn_cast<BuiltinType>(Val: T.getCanonicalType())) {
407 auto Kind = BT->getKind();
408 if (Kind == BuiltinType::Bool)
409 return PT_Bool;
410 if (Kind == BuiltinType::NullPtr)
411 return PT_Ptr;
412 if (Kind == BuiltinType::BoundMember)
413 return PT_MemberPtr;
414
415 // Just trying to avoid the ASTContext::getIntWidth call below.
416 if (Kind == BuiltinType::Short)
417 return integralTypeToPrimTypeS(BitWidth: this->ShortWidth);
418 if (Kind == BuiltinType::UShort)
419 return integralTypeToPrimTypeU(BitWidth: this->ShortWidth);
420
421 if (Kind == BuiltinType::Int)
422 return integralTypeToPrimTypeS(BitWidth: this->IntWidth);
423 if (Kind == BuiltinType::UInt)
424 return integralTypeToPrimTypeU(BitWidth: this->IntWidth);
425 if (Kind == BuiltinType::Long)
426 return integralTypeToPrimTypeS(BitWidth: this->LongWidth);
427 if (Kind == BuiltinType::ULong)
428 return integralTypeToPrimTypeU(BitWidth: this->LongWidth);
429 if (Kind == BuiltinType::LongLong)
430 return integralTypeToPrimTypeS(BitWidth: this->LongLongWidth);
431 if (Kind == BuiltinType::ULongLong)
432 return integralTypeToPrimTypeU(BitWidth: this->LongLongWidth);
433
434 if (Kind == BuiltinType::SChar || Kind == BuiltinType::Char_S)
435 return integralTypeToPrimTypeS(BitWidth: 8);
436 if (Kind == BuiltinType::UChar || Kind == BuiltinType::Char_U ||
437 Kind == BuiltinType::Char8)
438 return integralTypeToPrimTypeU(BitWidth: 8);
439
440 if (BT->isSignedInteger())
441 return integralTypeToPrimTypeS(BitWidth: Ctx.getIntWidth(T));
442 if (BT->isUnsignedInteger())
443 return integralTypeToPrimTypeU(BitWidth: Ctx.getIntWidth(T));
444
445 if (BT->isFloatingPoint())
446 return PT_Float;
447 }
448
449 if (T->isPointerOrReferenceType())
450 return PT_Ptr;
451
452 if (T->isMemberPointerType())
453 return PT_MemberPtr;
454
455 if (const auto *BT = T->getAs<BitIntType>()) {
456 if (BT->isSigned())
457 return integralTypeToPrimTypeS(BitWidth: BT->getNumBits());
458 return integralTypeToPrimTypeU(BitWidth: BT->getNumBits());
459 }
460
461 if (const auto *D = T->getAsEnumDecl()) {
462 if (!D->isComplete())
463 return std::nullopt;
464 return classify(T: D->getIntegerType());
465 }
466
467 if (const auto *AT = T->getAs<AtomicType>())
468 return classify(T: AT->getValueType());
469
470 if (const auto *DT = dyn_cast<DecltypeType>(Val&: T))
471 return classify(T: DT->getUnderlyingType());
472
473 if (T->isObjCObjectPointerType() || T->isBlockPointerType())
474 return PT_Ptr;
475
476 if (T->isFixedPointType())
477 return PT_FixedPoint;
478
479 // Vector and complex types get here.
480 return std::nullopt;
481}
482
483unsigned Context::getCharBit() const {
484 return Ctx.getTargetInfo().getCharWidth();
485}
486
487/// Simple wrapper around getFloatTypeSemantics() to make code a
488/// little shorter.
489const llvm::fltSemantics &Context::getFloatSemantics(QualType T) const {
490 return Ctx.getFloatTypeSemantics(T);
491}
492
493bool Context::Run(State &Parent, const Function *Func) {
494 InterpState State(Parent, *P, Stk, *this, Func);
495 if (Interpret(S&: State)) {
496 assert(Stk.empty());
497 return true;
498 }
499 Stk.clear();
500 return false;
501}
502
503// TODO: Virtual bases?
504const CXXMethodDecl *
505Context::getOverridingFunction(const CXXRecordDecl *DynamicDecl,
506 const CXXRecordDecl *StaticDecl,
507 const CXXMethodDecl *InitialFunction) const {
508 assert(DynamicDecl);
509 assert(StaticDecl);
510 assert(InitialFunction);
511
512 const CXXRecordDecl *CurRecord = DynamicDecl;
513 const CXXMethodDecl *FoundFunction = InitialFunction;
514 for (;;) {
515 const CXXMethodDecl *Overrider =
516 FoundFunction->getCorrespondingMethodDeclaredInClass(RD: CurRecord, MayBeBase: false);
517 if (Overrider)
518 return Overrider;
519
520 // Common case of only one base class.
521 if (CurRecord->getNumBases() == 1) {
522 CurRecord = CurRecord->bases_begin()->getType()->getAsCXXRecordDecl();
523 continue;
524 }
525
526 // Otherwise, go to the base class that will lead to the StaticDecl.
527 for (const CXXBaseSpecifier &Spec : CurRecord->bases()) {
528 const CXXRecordDecl *Base = Spec.getType()->getAsCXXRecordDecl();
529 if (Base == StaticDecl || Base->isDerivedFrom(Base: StaticDecl)) {
530 CurRecord = Base;
531 break;
532 }
533 }
534 }
535
536 llvm_unreachable(
537 "Couldn't find an overriding function in the class hierarchy?");
538 return nullptr;
539}
540
541const Function *Context::getOrCreateFunction(const FunctionDecl *FuncDecl) {
542 assert(FuncDecl);
543 if (const Function *Func = P->getFunction(F: FuncDecl))
544 return Func;
545
546 // Manually created functions that haven't been assigned proper
547 // parameters yet.
548 if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
549 return nullptr;
550
551 bool IsLambdaStaticInvoker = false;
552 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FuncDecl);
553 MD && MD->isLambdaStaticInvoker()) {
554 // For a lambda static invoker, we might have to pick a specialized
555 // version if the lambda is generic. In that case, the picked function
556 // will *NOT* be a static invoker anymore. However, it will still
557 // be a non-static member function, this (usually) requiring an
558 // instance pointer. We suppress that later in this function.
559 IsLambdaStaticInvoker = true;
560 }
561 // Set up argument indices.
562 unsigned ParamOffset = 0;
563 llvm::SmallVector<Function::ParamDescriptor> ParamDescriptors;
564
565 // If the return is not a primitive, a pointer to the storage where the
566 // value is initialized in is passed as the first argument. See 'RVO'
567 // elsewhere in the code.
568 QualType Ty = FuncDecl->getReturnType();
569 bool HasRVO = false;
570 if (!Ty->isVoidType() && !canClassify(T: Ty)) {
571 HasRVO = true;
572 ParamDescriptors.emplace_back(Args: nullptr, Args&: ParamOffset, Args: PT_Ptr);
573 ParamOffset += align(Size: primSize(Type: PT_Ptr));
574 }
575
576 // If the function decl is a member decl, the next parameter is
577 // the 'this' pointer. This parameter is pop()ed from the
578 // InterpStack when calling the function.
579 bool HasThisPointer = false;
580 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FuncDecl)) {
581 if (!IsLambdaStaticInvoker) {
582 HasThisPointer = MD->isInstance();
583 if (MD->isImplicitObjectMemberFunction()) {
584 ParamDescriptors.emplace_back(Args: nullptr, Args&: ParamOffset, Args: PT_Ptr);
585 ParamOffset += align(Size: primSize(Type: PT_Ptr));
586 }
587 }
588
589 if (isLambdaCallOperator(MD)) {
590 // The parent record needs to be complete, we need to know about all
591 // the lambda captures.
592 if (!MD->getParent()->isCompleteDefinition())
593 return nullptr;
594 if (MD->isStatic()) {
595 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
596 FieldDecl *LTC;
597
598 MD->getParent()->getCaptureFields(Captures&: LC, ThisCapture&: LTC);
599 // Static lambdas cannot have any captures. If this one does,
600 // it has already been diagnosed and we can only ignore it.
601 if (!LC.empty())
602 return nullptr;
603 }
604 }
605 }
606
607 // Assign descriptors to all parameters.
608 // Composite objects are lowered to pointers.
609 const auto *FuncProto = FuncDecl->getType()->getAs<FunctionProtoType>();
610 for (auto [ParamIndex, PD] : llvm::enumerate(First: FuncDecl->parameters())) {
611 bool IsConst = PD->getType().isConstQualified();
612 bool IsVolatile = PD->getType().isVolatileQualified();
613
614 if (!getASTContext().hasSameType(T1: PD->getType(),
615 T2: FuncProto->getParamType(i: ParamIndex)))
616 return nullptr;
617
618 OptPrimType T = classify(T: PD->getType());
619 PrimType PT = T.value_or(PT: PT_Ptr);
620 Descriptor *Desc = P->createDescriptor(D: PD, T: PT, SourceTy: nullptr, MDSize: std::nullopt,
621 IsConst, /*IsTemporary=*/false,
622 /*IsMutable=*/false, IsVolatile);
623 ParamDescriptors.emplace_back(Args&: Desc, Args&: ParamOffset, Args&: PT);
624 ParamOffset += align(Size: primSize(Type: PT));
625 }
626
627 // Create a handle over the emitted code.
628 assert(!P->getFunction(FuncDecl));
629 const Function *Func =
630 P->createFunction(Def: FuncDecl, Args&: ParamOffset, Args: std::move(ParamDescriptors),
631 Args&: HasThisPointer, Args&: HasRVO, Args&: IsLambdaStaticInvoker);
632 return Func;
633}
634
635const Function *Context::getOrCreateObjCBlock(const BlockExpr *E) {
636 const BlockDecl *BD = E->getBlockDecl();
637 // Set up argument indices.
638 unsigned ParamOffset = 0;
639 llvm::SmallVector<Function::ParamDescriptor> ParamDescriptors;
640
641 // Assign descriptors to all parameters.
642 // Composite objects are lowered to pointers.
643 for (const ParmVarDecl *PD : BD->parameters()) {
644 bool IsConst = PD->getType().isConstQualified();
645 bool IsVolatile = PD->getType().isVolatileQualified();
646
647 OptPrimType T = classify(T: PD->getType());
648 PrimType PT = T.value_or(PT: PT_Ptr);
649 Descriptor *Desc = P->createDescriptor(D: PD, T: PT, SourceTy: nullptr, MDSize: std::nullopt,
650 IsConst, /*IsTemporary=*/false,
651 /*IsMutable=*/false, IsVolatile);
652 ParamDescriptors.emplace_back(Args&: Desc, Args&: ParamOffset, Args&: PT);
653 ParamOffset += align(Size: primSize(Type: PT));
654 }
655
656 if (BD->hasCaptures())
657 return nullptr;
658
659 // Create a handle over the emitted code.
660 Function *Func =
661 P->createFunction(Args&: E, Args&: ParamOffset, Args: std::move(ParamDescriptors),
662 /*HasThisPointer=*/Args: false, /*HasRVO=*/Args: false,
663 /*IsLambdaStaticInvoker=*/Args: false);
664
665 assert(Func);
666 Func->setDefined(true);
667 // We don't compile the BlockDecl code at all right now.
668 Func->setIsFullyCompiled(true);
669
670 return Func;
671}
672
673unsigned Context::collectBaseOffset(const RecordDecl *BaseDecl,
674 const RecordDecl *DerivedDecl) const {
675 assert(BaseDecl);
676 assert(DerivedDecl);
677 const auto *FinalDecl = cast<CXXRecordDecl>(Val: BaseDecl);
678 const RecordDecl *CurDecl = DerivedDecl;
679 const Record *CurRecord = P->getOrCreateRecord(RD: CurDecl);
680 assert(CurDecl && FinalDecl);
681
682 unsigned OffsetSum = 0;
683 for (;;) {
684 assert(CurRecord->getNumBases() > 0);
685 // One level up
686 for (const Record::Base &B : CurRecord->bases()) {
687 const auto *BaseDecl = cast<CXXRecordDecl>(Val: B.Decl);
688
689 if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(Base: FinalDecl)) {
690 OffsetSum += B.Offset;
691 CurRecord = B.R;
692 CurDecl = BaseDecl;
693 break;
694 }
695 }
696 if (CurDecl == FinalDecl)
697 break;
698 }
699
700 assert(OffsetSum > 0);
701 return OffsetSum;
702}
703
704const Record *Context::getRecord(const RecordDecl *D) const {
705 return P->getOrCreateRecord(RD: D);
706}
707
708bool Context::isUnevaluatedBuiltin(unsigned ID) {
709 return ID == Builtin::BI__builtin_classify_type ||
710 ID == Builtin::BI__builtin_os_log_format_buffer_size ||
711 ID == Builtin::BI__builtin_constant_p || ID == Builtin::BI__noop;
712}
713