2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
2012-02-04 13:50:25 +00:00
|
|
|
|
|
|
|
#include "eval.hh"
|
|
|
|
|
|
|
|
#define LocalNoInline(f) static f __attribute__((noinline)); f
|
|
|
|
#define LocalNoInlineNoReturn(f) static f __attribute__((noinline, noreturn)); f
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-05-12 17:27:37 +00:00
|
|
|
LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s))
|
2012-02-04 13:50:25 +00:00
|
|
|
{
|
2020-06-15 12:06:58 +00:00
|
|
|
throw EvalError({
|
2021-01-20 23:27:36 +00:00
|
|
|
.msg = hintfmt(s),
|
2020-06-23 21:30:13 +00:00
|
|
|
.errPos = pos
|
2020-06-15 12:06:58 +00:00
|
|
|
});
|
2012-02-04 13:50:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:27:37 +00:00
|
|
|
LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v))
|
2014-04-04 17:05:36 +00:00
|
|
|
{
|
2020-06-15 12:06:58 +00:00
|
|
|
throw TypeError({
|
2021-01-20 23:27:36 +00:00
|
|
|
.msg = hintfmt(s, showType(v)),
|
2020-06-23 21:30:13 +00:00
|
|
|
.errPos = pos
|
2020-06-15 12:06:58 +00:00
|
|
|
});
|
2014-04-04 17:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 18:18:17 +00: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 00:48:26 +00:00
|
|
|
#if HAVE_BOEHMGC
|
2021-12-28 18:18:17 +00: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 00:48:26 +00:00
|
|
|
#else
|
|
|
|
void * p = allocBytes(sizeof(Value));
|
|
|
|
#endif
|
2021-12-28 18:18:17 +00:00
|
|
|
|
|
|
|
nrValues++;
|
|
|
|
return (Value *) p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
Env & EvalState::allocEnv(size_t size)
|
|
|
|
{
|
|
|
|
nrEnvs++;
|
|
|
|
nrValuesInEnvs += size;
|
|
|
|
|
|
|
|
Env * env;
|
|
|
|
|
2022-01-05 00:48:26 +00:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
if (size == 1) {
|
2021-12-28 18:18:17 +00: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 00:48:26 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
env = (Env *) allocBytes(sizeof(Env) + size * sizeof(Value *));
|
2021-12-28 18:18:17 +00: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]]
|
2015-07-31 15:32:25 +00:00
|
|
|
void EvalState::forceValue(Value & v, const Pos & pos)
|
2022-02-03 23:31:33 +00:00
|
|
|
{
|
|
|
|
forceValue(v, [&]() { return pos; });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Callable>
|
|
|
|
void EvalState::forceValue(Value & v, Callable getPos)
|
2012-02-04 13:50:25 +00:00
|
|
|
{
|
2020-12-12 01:15:11 +00:00
|
|
|
if (v.isThunk()) {
|
2013-03-14 16:21:13 +00:00
|
|
|
Env * env = v.thunk.env;
|
|
|
|
Expr * expr = v.thunk.expr;
|
2012-02-04 13:50:25 +00:00
|
|
|
try {
|
2020-12-18 13:38:49 +00:00
|
|
|
v.mkBlackhole();
|
2012-02-04 13:50:25 +00:00
|
|
|
//checkInterrupt();
|
2013-03-14 16:21:13 +00:00
|
|
|
expr->eval(*this, *env, v);
|
2017-06-20 10:11:22 +00:00
|
|
|
} catch (...) {
|
2020-12-18 13:38:49 +00:00
|
|
|
v.mkThunk(env, expr);
|
2012-02-04 13:50:25 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 01:15:11 +00:00
|
|
|
else if (v.isApp())
|
2014-04-04 15:53:52 +00:00
|
|
|
callFunction(*v.app.left, *v.app.right, v, noPos);
|
2020-12-12 01:15:11 +00:00
|
|
|
else if (v.isBlackhole())
|
2022-02-03 23:31:33 +00:00
|
|
|
throwEvalError(getPos(), "infinite recursion encountered");
|
2012-02-04 13:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 18:18:17 +00:00
|
|
|
[[gnu::always_inline]]
|
2014-04-04 17:11:40 +00:00
|
|
|
inline void EvalState::forceAttrs(Value & v, const Pos & pos)
|
|
|
|
{
|
2022-02-03 23:31:33 +00:00
|
|
|
forceAttrs(v, [&]() { return pos; });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Callable>
|
2021-12-28 18:18:17 +00:00
|
|
|
[[gnu::always_inline]]
|
2022-02-03 23:31:33 +00:00
|
|
|
inline void EvalState::forceAttrs(Value & v, Callable getPos)
|
|
|
|
{
|
|
|
|
forceValue(v, getPos);
|
2020-12-17 13:45:45 +00:00
|
|
|
if (v.type() != nAttrs)
|
2022-02-03 23:31:33 +00:00
|
|
|
throwTypeError(getPos(), "value is %1% while a set was expected", v);
|
2014-04-04 17:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 18:18:17 +00:00
|
|
|
[[gnu::always_inline]]
|
2014-04-04 17:05:36 +00:00
|
|
|
inline void EvalState::forceList(Value & v, const Pos & pos)
|
|
|
|
{
|
2020-04-16 10:32:07 +00:00
|
|
|
forceValue(v, pos);
|
2015-07-23 20:05:09 +00:00
|
|
|
if (!v.isList())
|
2020-05-12 17:27:37 +00:00
|
|
|
throwTypeError(pos, "value is %1% while a list was expected", v);
|
2014-04-04 17:05:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:58:19 +00:00
|
|
|
|
2012-02-04 13:50:25 +00:00
|
|
|
}
|