6#include "nix/expr/eval-error.hh"
25 throw std::bad_alloc();
35template<
typename T, std::
size_t N>
41 if (__builtin_mul_overflow(count,
sizeof(T), &res)) [[unlikely]]
42 throw std::bad_alloc();
44 if (__builtin_add_overflow(res, N, &res)) [[unlikely]]
45 throw std::bad_alloc();
52template<
typename T, std::
size_t N>
62 void * p = GC_MALLOC_ATOMIC(n);
64 throw std::bad_alloc();
68template<
typename T, std::
size_t N>
76Value * EvalMemory::allocValue()
81 static thread_local std::shared_ptr<void *> valueAllocCache{
82 std::allocate_shared<void *>(traceable_allocator<void *>(),
nullptr)};
88 if (!*valueAllocCache) {
89 *valueAllocCache = GC_malloc_many(
sizeof(Value));
90 if (!*valueAllocCache)
91 throw std::bad_alloc();
96 void * p = *valueAllocCache;
97 *valueAllocCache = GC_NEXT(p);
107[[gnu::always_inline]]
108Env & EvalMemory::allocEnv(
size_t size)
111 stats.nrValuesInEnvs += size;
119 static thread_local std::shared_ptr<void *> env1AllocCache{
120 std::allocate_shared<void *>(traceable_allocator<void *>(),
nullptr)};
122 if (!*env1AllocCache) {
123 *env1AllocCache = GC_malloc_many(
sizeof(Env) +
sizeof(Value *));
124 if (!*env1AllocCache)
125 throw std::bad_alloc();
128 void * p = *env1AllocCache;
129 *env1AllocCache = GC_NEXT(p);
130 GC_NEXT(p) =
nullptr;
134 env = (Env *)
allocBytes<Value *,
sizeof(Env)>(size);
141[[gnu::always_inline]]
145 Env * env = v.thunk().env;
146 assert(env || v.isBlackhole());
147 Expr * expr = v.thunk().expr;
151 expr->
eval(*
this, *env, v);
153 ExprBlackHole::throwInfiniteRecursionError(*
this, v);
155 handleEvalExceptionForThunk(env, expr, v, pos);
158 }
else if (v.isApp()) {
161 callFunction(*v.app().left, *v.app().right, v, pos);
163 handleEvalExceptionForApp(v, savedApp);
166 }
else if (v.isFailed()) {
167 handleEvalFailed(v, pos);
171[[gnu::always_inline]]
172inline void EvalState::forceAttrs(
Value & v,
const PosIdx pos, std::string_view errorCtx)
174 forceAttrs(v, [&]() {
return pos; }, errorCtx);
177template<
typename Callable>
178[[gnu::always_inline]]
179inline void EvalState::forceAttrs(
Value & v, Callable getPos, std::string_view errorCtx)
181 PosIdx pos = getPos();
183 if (v.type() != nAttrs) {
184 error<TypeError>(
"expected a set but found %1%: %2%", showType(v), ValuePrinter(*
this, v, errorPrintOptions))
185 .withTrace(pos, errorCtx)
190[[gnu::always_inline]]
191inline void EvalState::forceList(
Value & v,
const PosIdx pos, std::string_view errorCtx)
195 error<TypeError>(
"expected a list but found %1%: %2%", showType(v), ValuePrinter(*
this, v, errorPrintOptions))
196 .withTrace(pos, errorCtx)
201[[gnu::always_inline]]
204 if (callDepth > settings.maxCallDepth)
205 error<StackOverflowError>().atPos(pos).debugThrow();
211std::invoke_result_t<Cb, Value *, bool>
214 using R = std::invoke_result_t<Cb, Value *, bool>;
215 bool cameThroughToString =
false;
217 auto peel = [&, &state = *
this](
this const auto & peel,
const PosIdx pos, Value & v) -> R {
218 state.forceValue(v, pos);
219 auto vType = v.
type();
220 if (vType != nAttrs) {
229 if (checkToStringReturn && cameThroughToString && vType != nString && vType != nPath && vType != nExternal)
232 "`__toString` should return a string, but got %1%: %2%",
237 return std::forward<Cb>(cb)(&v, cameThroughToString);
239 if (
auto i = v.attrs()->
get(state.s.toString)) {
240 Value & v1 = *state.allocValue();
242 state.callFunction(*i->value, v, v1, i->pos);
243 }
catch (
Error & e) {
244 e.
addTrace(state.positions[i->pos],
"while calling the `__toString` attribute");
247 cameThroughToString =
true;
249 auto _level = state.addCallDepth(pos);
250 return peel(i->pos, v1);
251 }
catch (
Error & e) {
253 state.positions[i->pos],
"while %s the result of the `__toString` attribute",
WhileTryingToUse{v1});
257 if (
auto i = v.attrs()->
get(state.s.outPath)) {
259 state.forceValue(*i->value, i->pos);
260 auto _level = state.addCallDepth(pos);
261 return peel(i->pos, *i->value);
262 }
catch (
Error & e) {
270 if (checkToStringReturn && cameThroughToString)
273 "`__toString` must return a string, but got %1%: %2%",
278 return std::forward<Cb>(cb)(&v, cameThroughToString);
void addTrace(std::shared_ptr< const Pos > &&pos, std::string_view fs, Args &&... args)
Definition error.hh:238
const Attr * get(Symbol name) const noexcept
Definition attr-set.hh:357
void * allocBytes(size_t n)
Definition eval-inline.hh:16
void * allocBytesAtomic(size_t n)
Definition eval-inline.hh:60
void forceValue(Value &v, const PosIdx pos)
Definition eval-inline.hh:142
auto peelToStringOutPath(const PosIdx pos, Value &v, bool checkToStringReturn, Cb &&cb) -> std::invoke_result_t< Cb, Value *, bool >
Definition eval-inline.hh:212
CallDepth addCallDepth(const PosIdx pos)
Definition eval-inline.hh:202
size_t checkedAllocSize(size_t count)
Definition eval-inline.hh:37
Common printing functions for the Nix language.
Definition nixexpr.hh:100
virtual void eval(EvalState &state, Env &env, Value &v)
Definition eval.cc:1258
ValueType type() const
Definition value.hh:1285