From 0787dcf5f6550ab4ee87c54627b565252ebed1c7 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Tue, 16 Jul 2024 12:18:39 -0600 Subject: [PATCH] libexpr: move Value implementations out of eval.cc Change-Id: I2ce8a9713533888b3d109a56947156eb3a5ab492 --- src/libexpr/eval.cc | 97 ------------------------------------ src/libexpr/meson.build | 1 + src/libexpr/value.cc | 106 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 97 deletions(-) create mode 100644 src/libexpr/value.cc diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 4885db68d..702b9b6ac 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -70,11 +70,6 @@ std::string printValue(EvalState & state, Value & v) return out.str(); } -void Value::print(EvalState & state, std::ostream & str, PrintOptions options) -{ - printValue(state, str, *this, options); -} - const Value * getPrimOp(const Value &v) { const Value * primOp = &v; while (primOp->isPrimOpApp()) { @@ -124,32 +119,6 @@ std::string showType(const Value & v) #pragma GCC diagnostic pop } -PosIdx Value::determinePos(const PosIdx pos) const -{ - // Allow selecting a subset of enum values - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wswitch-enum" - switch (internalType) { - case tAttrs: return attrs->pos; - case tLambda: return lambda.fun->pos; - case tApp: return app.left->determinePos(pos); - default: return pos; - } - #pragma GCC diagnostic pop -} - -bool Value::isTrivial() const -{ - return - internalType != tApp - && internalType != tPrimOpApp - && (internalType != tThunk - || (dynamic_cast(thunk.expr) - && (static_cast(thunk.expr))->dynamicAttrs.empty()) - || dynamic_cast(thunk.expr) - || dynamic_cast(thunk.expr)); -} - #if HAVE_BOEHMGC /* Called when the Boehm GC runs out of memory. */ @@ -498,36 +467,6 @@ std::ostream & operator<<(std::ostream & output, PrimOp & primOp) return output; } - -Value::Value(primop_t, PrimOp & primop) - : internalType(tPrimOp) - , primOp(&primop) - , _primop_pad(0) -{ - primop.check(); -} - -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) -{ - p->check(); - clearValue(); - internalType = tPrimOp; - primOp = p; -} - - Value * EvalState::addPrimOp(PrimOp && primOp) { /* Hack to make constants lazy: turn them into a application of @@ -779,42 +718,6 @@ DebugTraceStacker::DebugTraceStacker(EvalState & evalState, DebugTrace t) evalState.runDebugRepl(nullptr, trace.env, trace.expr); } -void Value::mkString(std::string_view s) -{ - mkString(gcCopyStringIfNeeded(s)); -} - - -static void copyContextToValue(Value & v, const NixStringContext & context) -{ - if (!context.empty()) { - size_t n = 0; - v.string.context = gcAllocType(context.size() + 1); - for (auto & i : context) - v.string.context[n++] = gcCopyStringIfNeeded(i.to_string()); - v.string.context[n] = 0; - } -} - -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) -{ - mkPath(gcCopyStringIfNeeded(path.path.abs())); -} - - inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval) { for (auto l = var.level; l; --l, env = env->up) ; diff --git a/src/libexpr/meson.build b/src/libexpr/meson.build index b11e4c2e0..af72bd549 100644 --- a/src/libexpr/meson.build +++ b/src/libexpr/meson.build @@ -31,6 +31,7 @@ libexpr_sources = files( 'print-ambiguous.cc', 'print.cc', 'search-path.cc', + 'value.cc', 'value-to-json.cc', 'value-to-xml.cc', 'flake/config.cc', diff --git a/src/libexpr/value.cc b/src/libexpr/value.cc new file mode 100644 index 000000000..7e172f989 --- /dev/null +++ b/src/libexpr/value.cc @@ -0,0 +1,106 @@ +#include "value.hh" + +#include + +#include "eval.hh" +#include "print.hh" + + +namespace nix +{ + +static void copyContextToValue(Value & v, const NixStringContext & context) +{ + if (!context.empty()) { + size_t n = 0; + v.string.context = gcAllocType(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) +{ + primop.check(); +} + + +void Value::print(EvalState & state, std::ostream & str, PrintOptions options) +{ + printValue(state, str, *this, options); +} + +PosIdx Value::determinePos(const PosIdx pos) const +{ + // Allow selecting a subset of enum values + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wswitch-enum" + switch (internalType) { + case tAttrs: return attrs->pos; + case tLambda: return lambda.fun->pos; + case tApp: return app.left->determinePos(pos); + default: return pos; + } + #pragma GCC diagnostic pop +} + +bool Value::isTrivial() const +{ + return + internalType != tApp + && internalType != tPrimOpApp + && (internalType != tThunk + || (dynamic_cast(thunk.expr) + && static_cast(thunk.expr)->dynamicAttrs.empty()) + || dynamic_cast(thunk.expr) + || dynamic_cast(thunk.expr)); +} + +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) +{ + p->check(); + 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) +{ + mkPath(gcCopyStringIfNeeded(path.path.abs())); +} + +}