1//===--- InterpHelpers.h - Interpreter Helper Functions --------*- 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#ifndef LLVM_CLANG_AST_INTERP_INTERPHELPERS_H
10#define LLVM_CLANG_AST_INTERP_INTERPHELPERS_H
11
12#include "DynamicAllocator.h"
13#include "InterpState.h"
14#include "Pointer.h"
15
16namespace clang {
17class CallExpr;
18class OffsetOfExpr;
19
20namespace interp {
21class Block;
22struct Descriptor;
23
24/// Interpreter entry point.
25bool Interpret(InterpState &S);
26
27/// Interpret a builtin function.
28bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
29 uint32_t BuiltinID);
30
31/// Interpret an offsetof operation.
32bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
33 ArrayRef<int64_t> ArrayIndices, int64_t &Result);
34
35/// Checks if the array is offsetable.
36bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
37
38/// Checks if a pointer is live and accessible.
39bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
40 AccessKinds AK);
41
42/// Checks if a pointer is a dummy pointer.
43bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
44 AccessKinds AK);
45bool diagnoseDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
46 AccessKinds AK);
47
48bool arrayElemPtrOpaque(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
49 APSInt &&Index, bool AllowReplace = true);
50
51/// Checks if a pointer is in range.
52inline bool CheckRange(InterpState &S, CodePtr OpPC, PtrView Ptr,
53 AccessKinds AK) {
54 if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray())
55 return true;
56 if (S.getLangOpts().CPlusPlus)
57 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_access_past_end)
58 << AK << S.Current->getRange(PC: OpPC);
59
60 return false;
61}
62inline bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
63 AccessKinds AK) {
64 if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray())
65 return true;
66 if (S.getLangOpts().CPlusPlus)
67 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_access_past_end)
68 << AK << S.Current->getRange(PC: OpPC);
69
70 return false;
71}
72
73/// Checks if a field from which a pointer is going to be derived is valid.
74bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
75 CheckSubobjectKind CSK);
76
77/// Checks if a pointer points to a mutable field.
78bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr,
79 AccessKinds AK = AK_Read);
80inline bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
81 AccessKinds AK = AK_Read) {
82 if (!Ptr.isBlockPointer())
83 return true;
84 return CheckMutable(S, OpPC, Ptr: Ptr.view(), AK);
85}
86
87/// Checks if a value can be loaded from a block.
88bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
89 AccessKinds AK = AK_Read);
90bool CheckLoad(InterpState &S, CodePtr OpPC, PtrView Ptr,
91 AccessKinds AK = AK_Read);
92
93/// Diagnose mismatched new[]/delete or new/delete[] pairs.
94bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC,
95 DynamicAllocator::Form AllocForm,
96 DynamicAllocator::Form DeleteForm, const Descriptor *D,
97 const Expr *NewExpr);
98
99/// Copy the contents of Src into Dest.
100bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest,
101 bool Activate = true, bool Diagnose = false);
102
103UnsignedOrNone evaluateBuiltinObjectSize(const ASTContext &ASTCtx,
104 unsigned Kind, Pointer &Ptr,
105 const Expr *E, bool IsDynamic = false);
106
107bool diagnoseUninitialized(InterpState &S, CodePtr OpPC, bool Extern,
108 const Block *B, Lifetime LT = Lifetime::Started,
109 AccessKinds AK = AK_Read);
110
111template <typename T>
112bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {
113 const Expr *E = S.Current->getExpr(PC: OpPC);
114 S.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << E->getType();
115 return S.noteUndefinedBehavior();
116}
117
118inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
119 // Descriptors store the number of elements as unsigned.
120 if (NumElems > std::numeric_limits<unsigned>::max()) {
121 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_new_too_large)
122 << NumElems;
123 return false;
124 }
125
126 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
127 if (Limit != 0 && NumElems > Limit) {
128 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
129 DiagId: diag::note_constexpr_new_exceeds_limits, ExtraNotes: 1)
130 << NumElems << Limit;
131 S.Note(Loc: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_steps);
132 return false;
133 }
134 return true;
135}
136
137static inline llvm::RoundingMode getRoundingMode(FPOptions FPO) {
138 auto RM = FPO.getRoundingMode();
139 if (RM == llvm::RoundingMode::Dynamic)
140 return llvm::RoundingMode::NearestTiesToEven;
141 return RM;
142}
143
144inline bool Invalid(InterpState &S, CodePtr OpPC) {
145 if (S.diagnosing())
146 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
147 DiagId: diag::note_invalid_subexpr_in_const_expr)
148 << S.Current->getRange(PC: OpPC);
149 return false;
150}
151
152template <typename SizeT>
153bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT NumElements,
154 unsigned ElemSize, bool IsNoThrow) {
155
156 if (ElemSize == 0)
157 return true;
158
159 // FIXME: Both the SizeT::from() as well as the
160 // NumElements.toAPSInt() in this function are rather expensive.
161
162 // Can't be too many elements if the bitwidth of NumElements is lower than
163 // that of Descriptor::MaxArrayElemBytes.
164 if ((NumElements.bitWidth() - NumElements.isSigned()) <
165 (sizeof(Descriptor::MaxArrayElemBytes) * 8))
166 return true;
167
168 // FIXME: GH63562
169 // APValue stores array extents as unsigned,
170 // so anything that is greater that unsigned would overflow when
171 // constructing the array, we catch this here.
172 SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize);
173 assert(MaxElements.isPositive());
174 if (NumElements.toAPSInt().getActiveBits() >
175 ConstantArrayType::getMaxSizeBits(Context: S.getASTContext()) ||
176 NumElements > MaxElements) {
177 if (!IsNoThrow) {
178 if (NumElements.isSigned() && NumElements.isNegative()) {
179 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_new_negative)
180 << NumElements.toDiagnosticString(S.getASTContext());
181 } else {
182 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_new_too_large)
183 << NumElements.toDiagnosticString(S.getASTContext());
184 }
185 }
186 return false;
187 }
188 return true;
189}
190
191} // namespace interp
192} // namespace clang
193
194#endif // LLVM_CLANG_AST_INTERP_INTERPHELPERS_H
195