Files
lix/lix/libexpr/value.cc
T
eldritch horrors c7cc7d6c31 libexpr: "hide" Value union members
on its own this is not very useful, but having accessors for every value
kind is a prerequisite for doing smart things with Value than the union.
the net effect for now is only to add a few parentheses across the tree.

Change-Id: I88688ac09eb08495dad1eb221034ca540f094950
2025-09-29 15:22:41 +02:00

93 lines
1.9 KiB
C++

#include "lix/libexpr/value.hh"
#include <ostream>
#include "lix/libexpr/eval.hh"
#include "lix/libexpr/print.hh"
namespace nix
{
Value Value::EMPTY_LIST{Value::list_t{}, {}};
static void copyContextToValue(Value & v, const NixStringContext & context)
{
if (!context.empty()) {
size_t n = 0;
v._string.context = gcAllocType<char const *>(context.size() + 1);
for (auto & i : context)
v._string.context[n++] = gcCopyStringIfNeeded(i.to_string());
v._string.context[n] = 0;
}
}
Value::Value(primop_t, PrimOp & primop)
: internalType(tPrimOp)
, _primOp(&primop)
, _primop_pad(0)
{
}
void Value::print(EvalState & state, std::ostream & str, PrintOptions options)
{
printValue(state, str, *this, options);
}
bool Value::isTrivial() const
{
return
internalType != tApp
&& internalType != tPrimOpApp
&& (internalType != tThunk
|| (thunk().expr->try_cast<ExprSet>()
&& static_cast<ExprSet *>(thunk().expr)->dynamicAttrs.empty())
|| thunk().expr->try_cast<ExprLambda>()
|| thunk().expr->try_cast<ExprList>());
}
PrimOp * Value::primOpAppPrimOp() const
{
Value * left = primOpApp().left;
while (left && !left->isPrimOp()) {
left = left->primOpApp().left;
}
if (!left)
return nullptr;
return left->primOp();
}
void Value::mkPrimOp(PrimOp * p)
{
clearValue();
internalType = tPrimOp;
_primOp = p;
}
void Value::mkString(std::string_view s)
{
mkString(gcCopyStringIfNeeded(s));
}
void Value::mkString(std::string_view s, const NixStringContext & context)
{
mkString(s);
copyContextToValue(*this, context);
}
void Value::mkStringMove(const char * s, const NixStringContext & context)
{
mkString(s);
copyContextToValue(*this, context);
}
void Value::mkPath(const SourcePath & path)
{
*this = Value(NewValueAs::path, path);
}
}