2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2012-02-04 15:50:25 +02:00
|
|
|
|
|
|
|
#include "eval.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-12-28 20:18:17 +02:00
|
|
|
/* Note: Various places expect the allocated memory to be zeroed. */
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline void * allocBytes(size_t n)
|
|
|
|
{
|
|
|
|
void * p;
|
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
p = GC_MALLOC(n);
|
|
|
|
#else
|
|
|
|
p = calloc(n, 1);
|
|
|
|
#endif
|
|
|
|
if (!p) throw std::bad_alloc();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
Value * EvalState::allocValue()
|
|
|
|
{
|
2022-01-05 02:48:26 +02:00
|
|
|
#if HAVE_BOEHMGC
|
2021-12-28 20:18:17 +02:00
|
|
|
/* We use the boehm batch allocator to speed up allocations of Values (of which there are many).
|
|
|
|
GC_malloc_many returns a linked list of objects of the given size, where the first word
|
|
|
|
of each object is also the pointer to the next object in the list. This also means that we
|
|
|
|
have to explicitly clear the first word of every object we take. */
|
|
|
|
if (!*valueAllocCache) {
|
|
|
|
*valueAllocCache = GC_malloc_many(sizeof(Value));
|
|
|
|
if (!*valueAllocCache) throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GC_NEXT is a convenience macro for accessing the first word of an object.
|
|
|
|
Take the first list item, advance the list to the next item, and clear the next pointer. */
|
|
|
|
void * p = *valueAllocCache;
|
|
|
|
*valueAllocCache = GC_NEXT(p);
|
|
|
|
GC_NEXT(p) = nullptr;
|
2022-01-05 02:48:26 +02:00
|
|
|
#else
|
|
|
|
void * p = allocBytes(sizeof(Value));
|
|
|
|
#endif
|
2021-12-28 20:18:17 +02:00
|
|
|
|
|
|
|
nrValues++;
|
|
|
|
return (Value *) p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
Env & EvalState::allocEnv(size_t size)
|
|
|
|
{
|
|
|
|
nrEnvs++;
|
|
|
|
nrValuesInEnvs += size;
|
|
|
|
|
|
|
|
Env * env;
|
|
|
|
|
2022-01-05 02:48:26 +02:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
if (size == 1) {
|
2021-12-28 20:18:17 +02:00
|
|
|
/* see allocValue for explanations. */
|
|
|
|
if (!*env1AllocCache) {
|
|
|
|
*env1AllocCache = GC_malloc_many(sizeof(Env) + sizeof(Value *));
|
|
|
|
if (!*env1AllocCache) throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void * p = *env1AllocCache;
|
|
|
|
*env1AllocCache = GC_NEXT(p);
|
|
|
|
GC_NEXT(p) = nullptr;
|
|
|
|
env = (Env *) p;
|
2022-01-05 02:48:26 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
env = (Env *) allocBytes(sizeof(Env) + size * sizeof(Value *));
|
2021-12-28 20:18:17 +02:00
|
|
|
|
|
|
|
env->type = Env::Plain;
|
|
|
|
|
|
|
|
/* We assume that env->values has been cleared by the allocator; maybeThunk() and lookupVar fromWith expect this. */
|
|
|
|
|
|
|
|
return *env;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2022-03-04 20:31:59 +02:00
|
|
|
void EvalState::forceValue(Value & v, const PosIdx pos)
|
2022-02-04 01:31:33 +02:00
|
|
|
{
|
|
|
|
forceValue(v, [&]() { return pos; });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Callable>
|
|
|
|
void EvalState::forceValue(Value & v, Callable getPos)
|
2012-02-04 15:50:25 +02:00
|
|
|
{
|
2020-12-12 03:15:11 +02:00
|
|
|
if (v.isThunk()) {
|
2013-03-14 18:21:13 +02:00
|
|
|
Env * env = v.thunk.env;
|
|
|
|
Expr * expr = v.thunk.expr;
|
2012-02-04 15:50:25 +02:00
|
|
|
try {
|
2020-12-18 15:38:49 +02:00
|
|
|
v.mkBlackhole();
|
2012-02-04 15:50:25 +02:00
|
|
|
//checkInterrupt();
|
2013-03-14 18:21:13 +02:00
|
|
|
expr->eval(*this, *env, v);
|
2017-06-20 13:11:22 +03:00
|
|
|
} catch (...) {
|
2020-12-18 15:38:49 +02:00
|
|
|
v.mkThunk(env, expr);
|
2012-02-04 15:50:25 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 03:15:11 +02:00
|
|
|
else if (v.isApp())
|
2014-04-04 18:53:52 +03:00
|
|
|
callFunction(*v.app.left, *v.app.right, v, noPos);
|
2020-12-12 03:15:11 +02:00
|
|
|
else if (v.isBlackhole())
|
2023-01-19 14:23:04 +02:00
|
|
|
error("infinite recursion encountered").atPos(getPos()).template debugThrow<EvalError>();
|
2012-02-04 15:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 20:18:17 +02:00
|
|
|
[[gnu::always_inline]]
|
2023-01-19 14:23:04 +02:00
|
|
|
inline void EvalState::forceAttrs(Value & v, const PosIdx pos, std::string_view errorCtx)
|
2014-04-04 20:11:40 +03:00
|
|
|
{
|
2023-01-19 14:23:04 +02:00
|
|
|
forceAttrs(v, [&]() { return pos; }, errorCtx);
|
2022-02-04 01:31:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Callable>
|
2021-12-28 20:18:17 +02:00
|
|
|
[[gnu::always_inline]]
|
2023-01-19 14:23:04 +02:00
|
|
|
inline void EvalState::forceAttrs(Value & v, Callable getPos, std::string_view errorCtx)
|
2022-02-04 01:31:33 +02:00
|
|
|
{
|
2023-01-19 14:23:04 +02:00
|
|
|
forceValue(v, noPos);
|
|
|
|
if (v.type() != nAttrs) {
|
|
|
|
PosIdx pos = getPos();
|
|
|
|
error("value is %1% while a set was expected", showType(v)).withTrace(pos, errorCtx).debugThrow<TypeError>();
|
|
|
|
}
|
2014-04-04 20:11:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 20:18:17 +02:00
|
|
|
[[gnu::always_inline]]
|
2023-01-19 14:23:04 +02:00
|
|
|
inline void EvalState::forceList(Value & v, const PosIdx pos, std::string_view errorCtx)
|
2014-04-04 20:05:36 +03:00
|
|
|
{
|
2023-01-19 14:23:04 +02:00
|
|
|
forceValue(v, noPos);
|
|
|
|
if (!v.isList()) {
|
|
|
|
error("value is %1% while a list was expected", showType(v)).withTrace(pos, errorCtx).debugThrow<TypeError>();
|
|
|
|
}
|
2014-04-04 20:05:36 +03:00
|
|
|
}
|
|
|
|
|
2018-06-11 16:58:19 +03:00
|
|
|
|
2012-02-04 15:50:25 +02:00
|
|
|
}
|