2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2012-01-07 19:26:33 +02:00
|
|
|
|
2021-11-24 21:21:34 +02:00
|
|
|
#include <cassert>
|
2023-06-22 07:27:19 +03:00
|
|
|
#include <climits>
|
Value: use std::span, change use of const
**`Value` and `const`**
These two deserve some explanation. We'll get to lists later.
Values can normally be thought of as immutable, except they are
are also the vehicle for call by need, which must be implemented
using mutation.
This circumstance makes a `const Value` a rather useless thing:
- If it's a thunk, you can't evaluate it, except by copying, but
that would not be call by need.
- If it's not a thunk, you know the type, so the method that
acquired it for you should have returned something more specific,
such as a `const Bindings &` (which actually does make sense
because that's an immutable span of pointers to mutable `Value`s.
- If you don't care about the type yet, you might establish the
convention that `const Value` means `deepSeq`-ed data, but
this is hardly useful and not actually as safe as you would
supposedly want to trust it to be - just convention.
**Lists**
`std::span` is a tuple of pointer and size - just what we need.
We don't return them as `const Value`, because considering the
first bullet point we discussed before, we'd have to force all
the list values, which isn't what we want.
So what we end up with is a nice representation of a list in
weak head normal form: the spine is immutable, but the
items may need some evaluation later.
2023-11-12 04:31:45 +02:00
|
|
|
#include <span>
|
2021-11-24 21:21:34 +02:00
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
#include "symbol-table.hh"
|
2023-01-03 18:44:59 +02:00
|
|
|
#include "value/context.hh"
|
2023-04-06 14:15:50 +03:00
|
|
|
#include "input-accessor.hh"
|
2023-12-18 23:14:42 +02:00
|
|
|
#include "source-path.hh"
|
2023-12-12 23:57:36 +02:00
|
|
|
#include "print-options.hh"
|
2012-01-07 19:26:33 +02:00
|
|
|
|
2016-08-30 14:12:12 +03:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
#include <gc/gc_allocator.h>
|
|
|
|
#endif
|
2022-11-16 17:49:49 +02:00
|
|
|
#include <nlohmann/json_fwd.hpp>
|
2016-08-30 14:12:12 +03:00
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
namespace nix {
|
|
|
|
|
2024-03-14 20:10:31 +02:00
|
|
|
struct Value;
|
2022-01-04 18:39:16 +02:00
|
|
|
class BindingsBuilder;
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
tInt = 1,
|
|
|
|
tBool,
|
|
|
|
tString,
|
|
|
|
tPath,
|
|
|
|
tNull,
|
|
|
|
tAttrs,
|
2015-07-23 23:05:09 +03:00
|
|
|
tList1,
|
|
|
|
tList2,
|
|
|
|
tListN,
|
2012-01-07 19:26:33 +02:00
|
|
|
tThunk,
|
|
|
|
tApp,
|
|
|
|
tLambda,
|
|
|
|
tPrimOp,
|
|
|
|
tPrimOpApp,
|
2014-11-30 20:16:19 +02:00
|
|
|
tExternal,
|
2016-01-05 01:40:40 +02:00
|
|
|
tFloat
|
2020-12-17 15:42:52 +02:00
|
|
|
} InternalType;
|
2012-01-07 19:26:33 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* This type abstracts over all actual value types in the language,
|
|
|
|
* grouping together implementation details like tList*, different function
|
|
|
|
* types, and types in non-normal form (so thunks and co.)
|
|
|
|
*/
|
2020-12-12 00:32:45 +02:00
|
|
|
typedef enum {
|
|
|
|
nThunk,
|
|
|
|
nInt,
|
|
|
|
nFloat,
|
|
|
|
nBool,
|
|
|
|
nString,
|
|
|
|
nPath,
|
|
|
|
nNull,
|
|
|
|
nAttrs,
|
|
|
|
nList,
|
|
|
|
nFunction,
|
|
|
|
nExternal
|
2020-12-17 15:42:52 +02:00
|
|
|
} ValueType;
|
2012-01-07 19:26:33 +02:00
|
|
|
|
2014-01-21 19:29:55 +02:00
|
|
|
class Bindings;
|
2012-01-07 19:26:33 +02:00
|
|
|
struct Env;
|
|
|
|
struct Expr;
|
|
|
|
struct ExprLambda;
|
2023-12-11 17:23:08 +02:00
|
|
|
struct ExprBlackHole;
|
2012-01-07 19:26:33 +02:00
|
|
|
struct PrimOp;
|
2014-01-21 19:29:55 +02:00
|
|
|
class Symbol;
|
2022-03-04 20:31:59 +02:00
|
|
|
class PosIdx;
|
2014-11-30 20:16:19 +02:00
|
|
|
struct Pos;
|
2022-03-12 02:28:00 +02:00
|
|
|
class StorePath;
|
2014-11-30 20:16:19 +02:00
|
|
|
class EvalState;
|
|
|
|
class XMLWriter;
|
2023-12-12 23:57:36 +02:00
|
|
|
class Printer;
|
2012-01-07 19:26:33 +02:00
|
|
|
|
libexpr: Use int64_t for NixInt
Using a 64bit integer on 32bit systems will come with a bit of a
performance overhead, but given that Nix doesn't use a lot of integers
compared to other types, I think the overhead is negligible also
considering that 32bit systems are in decline.
The biggest advantage however is that when we use a consistent integer
size across all platforms it's less likely that we miss things that we
break due to that. One example would be:
https://github.com/NixOS/nixpkgs/pull/44233
On Hydra it will evaluate, because the evaluator runs on a 64bit
machine, but when evaluating the same on a 32bit machine it will fail,
so using 64bit integers should make that consistent.
While the change of the type in value.hh is rather easy to do, we have a
few more options available for doing the conversion in the lexer:
* Via an #ifdef on the architecture and using strtol() or strtoll()
accordingly depending on which architecture we are. For the #ifdef
we would need another AX_COMPILE_CHECK_SIZEOF in configure.ac.
* Using istringstream, which would involve copying the value.
* As we're already using boost, lexical_cast might be a good idea.
Spoiler: I went for the latter, first of all because lexical_cast does
have an overload for const char* and second of all, because it doesn't
involve copying around the input string. Also, because istringstream
seems to come with a bigger overhead than boost::lexical_cast:
https://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast/performance.html
The first method (still using strtol/strtoll) also wasn't something I
pursued further, because it is also locale-aware which I doubt is what
we want, given that the regex for int is [0-9]+.
Signed-off-by: aszlig <aszlig@nix.build>
Fixes: #2339
2018-08-29 01:23:51 +03:00
|
|
|
typedef int64_t NixInt;
|
2018-07-03 19:04:51 +03:00
|
|
|
typedef double NixFloat;
|
2013-08-19 13:35:03 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* External values must descend from ExternalValueBase, so that
|
2014-11-30 20:16:19 +02:00
|
|
|
* type-agnostic nix functions (e.g. showType) can be implemented
|
|
|
|
*/
|
|
|
|
class ExternalValueBase
|
|
|
|
{
|
2014-12-02 17:02:03 +02:00
|
|
|
friend std::ostream & operator << (std::ostream & str, const ExternalValueBase & v);
|
2023-12-12 23:57:36 +02:00
|
|
|
friend class Printer;
|
2014-11-30 20:16:19 +02:00
|
|
|
protected:
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Print out the value
|
|
|
|
*/
|
2014-12-02 17:02:03 +02:00
|
|
|
virtual std::ostream & print(std::ostream & str) const = 0;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
public:
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Return a simple string describing the type
|
|
|
|
*/
|
2022-02-21 17:37:25 +02:00
|
|
|
virtual std::string showType() const = 0;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Return a string to be used in builtins.typeOf
|
|
|
|
*/
|
2022-02-21 17:37:25 +02:00
|
|
|
virtual std::string typeOf() const = 0;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Coerce the value to a string. Defaults to uncoercable, i.e. throws an
|
2022-02-21 17:37:25 +02:00
|
|
|
* error.
|
2014-11-30 20:16:19 +02:00
|
|
|
*/
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
virtual std::string coerceToString(EvalState & state, const PosIdx & pos, NixStringContext & context, bool copyMore, bool copyToStore) const;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Compare to another value of the same type. Defaults to uncomparable,
|
2014-11-30 20:16:19 +02:00
|
|
|
* i.e. always false.
|
|
|
|
*/
|
2022-02-21 17:37:25 +02:00
|
|
|
virtual bool operator ==(const ExternalValueBase & b) const;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Print the value as JSON. Defaults to unconvertable, i.e. throws an error
|
|
|
|
*/
|
2022-11-16 17:49:49 +02:00
|
|
|
virtual nlohmann::json printValueAsJSON(EvalState & state, bool strict,
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
NixStringContext & context, bool copyToStore = true) const;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Print the value as XML. Defaults to unevaluated
|
|
|
|
*/
|
2014-11-30 20:16:19 +02:00
|
|
|
virtual void printValueAsXML(EvalState & state, bool strict, bool location,
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
XMLWriter & doc, NixStringContext & context, PathSet & drvsSeen,
|
2022-03-04 20:31:59 +02:00
|
|
|
const PosIdx pos) const;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
virtual ~ExternalValueBase()
|
|
|
|
{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-12-02 17:02:03 +02:00
|
|
|
std::ostream & operator << (std::ostream & str, const ExternalValueBase & v);
|
2014-11-30 20:16:19 +02:00
|
|
|
|
2013-08-19 13:35:03 +03:00
|
|
|
|
2024-03-14 20:10:31 +02:00
|
|
|
class ListBuilder
|
|
|
|
{
|
|
|
|
const size_t size;
|
|
|
|
Value * inlineElems[2] = {nullptr, nullptr};
|
|
|
|
public:
|
|
|
|
Value * * elems;
|
|
|
|
ListBuilder(EvalState & state, size_t size);
|
|
|
|
|
|
|
|
ListBuilder(ListBuilder && x)
|
|
|
|
: size(x.size)
|
|
|
|
, inlineElems{x.inlineElems[0], x.inlineElems[1]}
|
|
|
|
, elems(size <= 2 ? inlineElems : x.elems)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Value * & operator [](size_t n)
|
|
|
|
{
|
|
|
|
return elems[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef Value * * iterator;
|
|
|
|
|
|
|
|
iterator begin() { return &elems[0]; }
|
|
|
|
iterator end() { return &elems[size]; }
|
|
|
|
|
2024-03-21 00:07:00 +02:00
|
|
|
friend struct Value;
|
2024-03-14 20:10:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
struct Value
|
|
|
|
{
|
2020-12-12 03:22:58 +02:00
|
|
|
private:
|
2020-12-17 15:42:52 +02:00
|
|
|
InternalType internalType;
|
2020-12-12 01:19:05 +02:00
|
|
|
|
2022-03-03 14:07:50 +02:00
|
|
|
friend std::string showType(const Value & v);
|
2022-03-22 14:18:11 +02:00
|
|
|
|
2020-12-12 03:22:58 +02:00
|
|
|
public:
|
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
void print(EvalState &state, std::ostream &str, PrintOptions options = PrintOptions {});
|
2022-03-22 14:18:11 +02:00
|
|
|
|
2020-12-12 03:15:11 +02:00
|
|
|
// Functions needed to distinguish the type
|
|
|
|
// These should be removed eventually, by putting the functionality that's
|
|
|
|
// needed by callers into methods of this type
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
// type() == nThunk
|
2020-12-17 15:42:52 +02:00
|
|
|
inline bool isThunk() const { return internalType == tThunk; };
|
|
|
|
inline bool isApp() const { return internalType == tApp; };
|
2023-12-10 09:24:45 +02:00
|
|
|
inline bool isBlackhole() const;
|
2020-12-12 03:15:11 +02:00
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
// type() == nFunction
|
2020-12-17 15:42:52 +02:00
|
|
|
inline bool isLambda() const { return internalType == tLambda; };
|
|
|
|
inline bool isPrimOp() const { return internalType == tPrimOp; };
|
|
|
|
inline bool isPrimOpApp() const { return internalType == tPrimOpApp; };
|
2020-12-12 03:15:11 +02:00
|
|
|
|
2023-11-12 04:02:02 +02:00
|
|
|
/**
|
|
|
|
* Strings in the evaluator carry a so-called `context` which
|
|
|
|
* is a list of strings representing store paths. This is to
|
|
|
|
* allow users to write things like
|
|
|
|
*
|
|
|
|
* "--with-freetype2-library=" + freetype + "/lib"
|
|
|
|
*
|
|
|
|
* where `freetype` is a derivation (or a source to be copied
|
|
|
|
* to the store). If we just concatenated the strings without
|
|
|
|
* keeping track of the referenced store paths, then if the
|
|
|
|
* string is used as a derivation attribute, the derivation
|
|
|
|
* will not have the correct dependencies in its inputDrvs and
|
|
|
|
* inputSrcs.
|
|
|
|
|
|
|
|
* The semantics of the context is as follows: when a string
|
|
|
|
* with context C is used as a derivation attribute, then the
|
|
|
|
* derivations in C will be added to the inputDrvs of the
|
|
|
|
* derivation, and the other store paths in C will be added to
|
|
|
|
* the inputSrcs of the derivations.
|
|
|
|
|
|
|
|
* For canonicity, the store paths should be in sorted order.
|
|
|
|
*/
|
|
|
|
struct StringWithContext {
|
|
|
|
const char * c_str;
|
|
|
|
const char * * context; // must be in sorted order
|
|
|
|
};
|
|
|
|
|
2023-11-12 04:03:37 +02:00
|
|
|
struct Path {
|
|
|
|
InputAccessor * accessor;
|
|
|
|
const char * path;
|
|
|
|
};
|
|
|
|
|
2023-11-12 04:06:04 +02:00
|
|
|
struct ClosureThunk {
|
|
|
|
Env * env;
|
|
|
|
Expr * expr;
|
|
|
|
};
|
|
|
|
|
2023-11-12 04:06:55 +02:00
|
|
|
struct FunctionApplicationThunk {
|
|
|
|
Value * left, * right;
|
|
|
|
};
|
|
|
|
|
2023-11-12 04:07:32 +02:00
|
|
|
struct Lambda {
|
|
|
|
Env * env;
|
|
|
|
ExprLambda * fun;
|
|
|
|
};
|
|
|
|
|
2013-08-19 13:35:03 +03:00
|
|
|
union
|
2012-01-07 19:26:33 +02:00
|
|
|
{
|
2013-08-19 13:35:03 +03:00
|
|
|
NixInt integer;
|
2012-01-07 19:26:33 +02:00
|
|
|
bool boolean;
|
2013-08-19 13:35:03 +03:00
|
|
|
|
2023-11-12 04:02:02 +02:00
|
|
|
StringWithContext string;
|
2023-10-18 16:32:31 +03:00
|
|
|
|
2023-11-12 04:03:37 +02:00
|
|
|
Path _path;
|
2023-10-18 16:32:31 +03:00
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
Bindings * attrs;
|
|
|
|
struct {
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t size;
|
2024-03-15 19:22:39 +02:00
|
|
|
Value * const * elems;
|
2015-07-23 23:05:09 +03:00
|
|
|
} bigList;
|
|
|
|
Value * smallList[2];
|
2023-11-12 04:06:04 +02:00
|
|
|
ClosureThunk thunk;
|
2023-11-12 04:06:55 +02:00
|
|
|
FunctionApplicationThunk app;
|
2023-11-12 04:07:32 +02:00
|
|
|
Lambda lambda;
|
2012-01-07 19:26:33 +02:00
|
|
|
PrimOp * primOp;
|
2023-11-12 04:06:55 +02:00
|
|
|
FunctionApplicationThunk primOpApp;
|
2014-11-30 20:16:19 +02:00
|
|
|
ExternalValueBase * external;
|
2016-01-05 01:40:40 +02:00
|
|
|
NixFloat fpoint;
|
2012-01-07 19:26:33 +02:00
|
|
|
};
|
2015-07-23 23:05:09 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Returns the normal type of a Value. This only returns nThunk if
|
|
|
|
* the Value hasn't been forceValue'd
|
2023-05-13 20:52:45 +03:00
|
|
|
*
|
|
|
|
* @param invalidIsThunk Instead of aborting an an invalid (probably
|
|
|
|
* 0, so uninitialized) internal type, return `nThunk`.
|
2023-04-07 16:55:28 +03:00
|
|
|
*/
|
2023-05-13 20:52:45 +03:00
|
|
|
inline ValueType type(bool invalidIsThunk = false) const
|
2020-12-12 00:32:45 +02:00
|
|
|
{
|
2020-12-17 15:42:52 +02:00
|
|
|
switch (internalType) {
|
2020-12-12 00:32:45 +02:00
|
|
|
case tInt: return nInt;
|
|
|
|
case tBool: return nBool;
|
|
|
|
case tString: return nString;
|
|
|
|
case tPath: return nPath;
|
|
|
|
case tNull: return nNull;
|
|
|
|
case tAttrs: return nAttrs;
|
|
|
|
case tList1: case tList2: case tListN: return nList;
|
|
|
|
case tLambda: case tPrimOp: case tPrimOpApp: return nFunction;
|
|
|
|
case tExternal: return nExternal;
|
|
|
|
case tFloat: return nFloat;
|
2023-12-10 09:24:45 +02:00
|
|
|
case tThunk: case tApp: return nThunk;
|
2020-12-12 00:32:45 +02:00
|
|
|
}
|
2023-05-13 20:52:45 +03:00
|
|
|
if (invalidIsThunk)
|
|
|
|
return nThunk;
|
|
|
|
else
|
|
|
|
abort();
|
2020-12-12 00:32:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* After overwriting an app node, be sure to clear pointers in the
|
|
|
|
* Value to ensure that the target isn't kept alive unnecessarily.
|
|
|
|
*/
|
2020-12-18 15:38:49 +02:00
|
|
|
inline void clearValue()
|
|
|
|
{
|
|
|
|
app.left = app.right = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkInt(NixInt n)
|
|
|
|
{
|
|
|
|
clearValue();
|
|
|
|
internalType = tInt;
|
|
|
|
integer = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkBool(bool b)
|
|
|
|
{
|
|
|
|
clearValue();
|
|
|
|
internalType = tBool;
|
|
|
|
boolean = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkString(const char * s, const char * * context = 0)
|
|
|
|
{
|
|
|
|
internalType = tString;
|
2023-09-26 04:30:41 +03:00
|
|
|
string.c_str = s;
|
2020-12-18 15:38:49 +02:00
|
|
|
string.context = context;
|
|
|
|
}
|
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
void mkString(std::string_view s);
|
|
|
|
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
void mkString(std::string_view s, const NixStringContext & context);
|
2022-01-04 18:39:16 +02:00
|
|
|
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
void mkStringMove(const char * s, const NixStringContext & context);
|
2021-12-27 03:04:49 +02:00
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
inline void mkString(const Symbol & s)
|
|
|
|
{
|
|
|
|
mkString(((const std::string &) s).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mkPath(const SourcePath & path);
|
|
|
|
|
2023-10-18 16:32:31 +03:00
|
|
|
inline void mkPath(InputAccessor * accessor, const char * path)
|
2020-12-18 15:38:49 +02:00
|
|
|
{
|
|
|
|
clearValue();
|
|
|
|
internalType = tPath;
|
2023-10-18 16:32:31 +03:00
|
|
|
_path.accessor = accessor;
|
|
|
|
_path.path = path;
|
2020-12-18 15:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkNull()
|
|
|
|
{
|
|
|
|
clearValue();
|
|
|
|
internalType = tNull;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkAttrs(Bindings * a)
|
|
|
|
{
|
|
|
|
clearValue();
|
|
|
|
internalType = tAttrs;
|
|
|
|
attrs = a;
|
|
|
|
}
|
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
Value & mkAttrs(BindingsBuilder & bindings);
|
|
|
|
|
2024-03-14 20:10:31 +02:00
|
|
|
void mkList(const ListBuilder & builder)
|
2020-12-18 15:38:49 +02:00
|
|
|
{
|
|
|
|
clearValue();
|
2024-03-14 20:10:31 +02:00
|
|
|
if (builder.size == 1) {
|
|
|
|
smallList[0] = builder.inlineElems[0];
|
2020-12-18 15:38:49 +02:00
|
|
|
internalType = tList1;
|
2024-03-14 20:10:31 +02:00
|
|
|
} else if (builder.size == 2) {
|
|
|
|
smallList[0] = builder.inlineElems[0];
|
|
|
|
smallList[1] = builder.inlineElems[1];
|
2020-12-18 15:38:49 +02:00
|
|
|
internalType = tList2;
|
2024-03-14 20:10:31 +02:00
|
|
|
} else {
|
|
|
|
bigList.size = builder.size;
|
|
|
|
bigList.elems = builder.elems;
|
2020-12-18 15:38:49 +02:00
|
|
|
internalType = tListN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkThunk(Env * e, Expr * ex)
|
|
|
|
{
|
|
|
|
internalType = tThunk;
|
|
|
|
thunk.env = e;
|
|
|
|
thunk.expr = ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkApp(Value * l, Value * r)
|
|
|
|
{
|
|
|
|
internalType = tApp;
|
|
|
|
app.left = l;
|
|
|
|
app.right = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkLambda(Env * e, ExprLambda * f)
|
|
|
|
{
|
|
|
|
internalType = tLambda;
|
|
|
|
lambda.env = e;
|
|
|
|
lambda.fun = f;
|
|
|
|
}
|
|
|
|
|
2023-12-10 09:24:45 +02:00
|
|
|
inline void mkBlackhole();
|
2020-12-18 15:38:49 +02:00
|
|
|
|
2023-11-16 12:10:25 +02:00
|
|
|
void mkPrimOp(PrimOp * p);
|
2020-12-18 15:38:49 +02:00
|
|
|
|
|
|
|
inline void mkPrimOpApp(Value * l, Value * r)
|
|
|
|
{
|
|
|
|
internalType = tPrimOpApp;
|
2023-12-12 23:57:36 +02:00
|
|
|
primOpApp.left = l;
|
|
|
|
primOpApp.right = r;
|
2020-12-18 15:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* For a `tPrimOpApp` value, get the original `PrimOp` value.
|
|
|
|
*/
|
|
|
|
PrimOp * primOpAppPrimOp() const;
|
|
|
|
|
2020-12-18 15:38:49 +02:00
|
|
|
inline void mkExternal(ExternalValueBase * e)
|
|
|
|
{
|
|
|
|
clearValue();
|
|
|
|
internalType = tExternal;
|
|
|
|
external = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mkFloat(NixFloat n)
|
|
|
|
{
|
|
|
|
clearValue();
|
|
|
|
internalType = tFloat;
|
|
|
|
fpoint = n;
|
|
|
|
}
|
|
|
|
|
2015-07-23 23:05:09 +03:00
|
|
|
bool isList() const
|
|
|
|
{
|
2020-12-17 15:42:52 +02:00
|
|
|
return internalType == tList1 || internalType == tList2 || internalType == tListN;
|
2015-07-23 23:05:09 +03:00
|
|
|
}
|
|
|
|
|
2024-03-15 19:22:39 +02:00
|
|
|
Value * const * listElems()
|
2015-07-23 23:05:09 +03:00
|
|
|
{
|
2020-12-17 15:42:52 +02:00
|
|
|
return internalType == tList1 || internalType == tList2 ? smallList : bigList.elems;
|
2015-07-23 23:05:09 +03:00
|
|
|
}
|
|
|
|
|
Value: use std::span, change use of const
**`Value` and `const`**
These two deserve some explanation. We'll get to lists later.
Values can normally be thought of as immutable, except they are
are also the vehicle for call by need, which must be implemented
using mutation.
This circumstance makes a `const Value` a rather useless thing:
- If it's a thunk, you can't evaluate it, except by copying, but
that would not be call by need.
- If it's not a thunk, you know the type, so the method that
acquired it for you should have returned something more specific,
such as a `const Bindings &` (which actually does make sense
because that's an immutable span of pointers to mutable `Value`s.
- If you don't care about the type yet, you might establish the
convention that `const Value` means `deepSeq`-ed data, but
this is hardly useful and not actually as safe as you would
supposedly want to trust it to be - just convention.
**Lists**
`std::span` is a tuple of pointer and size - just what we need.
We don't return them as `const Value`, because considering the
first bullet point we discussed before, we'd have to force all
the list values, which isn't what we want.
So what we end up with is a nice representation of a list in
weak head normal form: the spine is immutable, but the
items may need some evaluation later.
2023-11-12 04:31:45 +02:00
|
|
|
std::span<Value * const> listItems() const
|
|
|
|
{
|
|
|
|
assert(isList());
|
|
|
|
return std::span<Value * const>(listElems(), listSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
Value * const * listElems() const
|
2015-07-23 23:05:09 +03:00
|
|
|
{
|
2020-12-17 15:42:52 +02:00
|
|
|
return internalType == tList1 || internalType == tList2 ? smallList : bigList.elems;
|
2015-07-23 23:05:09 +03:00
|
|
|
}
|
|
|
|
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t listSize() const
|
2015-07-23 23:05:09 +03:00
|
|
|
{
|
2020-12-17 15:42:52 +02:00
|
|
|
return internalType == tList1 ? 1 : internalType == tList2 ? 2 : bigList.size;
|
2015-07-23 23:05:09 +03:00
|
|
|
}
|
2019-09-09 18:34:38 +03:00
|
|
|
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx determinePos(const PosIdx pos) const;
|
2021-01-08 23:27:00 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Check whether forcing this value requires a trivial amount of
|
|
|
|
* computation. In particular, function applications are
|
|
|
|
* non-trivial.
|
|
|
|
*/
|
2019-09-09 18:34:38 +03:00
|
|
|
bool isTrivial() const;
|
2020-06-29 20:08:37 +03:00
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
SourcePath path() const
|
|
|
|
{
|
|
|
|
assert(internalType == tPath);
|
2023-11-30 17:44:54 +02:00
|
|
|
return SourcePath(
|
|
|
|
ref(_path.accessor->shared_from_this()),
|
|
|
|
CanonPath(CanonPath::unchecked_t(), _path.path));
|
2023-04-06 14:15:50 +03:00
|
|
|
}
|
|
|
|
|
2023-09-26 04:30:41 +03:00
|
|
|
std::string_view string_view() const
|
|
|
|
{
|
|
|
|
assert(internalType == tString);
|
|
|
|
return std::string_view(string.c_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const c_str() const
|
2023-04-06 14:15:50 +03:00
|
|
|
{
|
|
|
|
assert(internalType == tString);
|
2023-09-26 04:30:41 +03:00
|
|
|
return string.c_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * * context() const
|
|
|
|
{
|
|
|
|
return string.context;
|
2023-04-06 14:15:50 +03:00
|
|
|
}
|
2012-01-07 19:26:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-12-11 17:23:08 +02:00
|
|
|
extern ExprBlackHole eBlackHole;
|
2023-12-10 09:24:45 +02:00
|
|
|
|
2023-12-11 17:23:08 +02:00
|
|
|
bool Value::isBlackhole() const
|
2023-12-10 09:24:45 +02:00
|
|
|
{
|
2023-12-11 17:23:08 +02:00
|
|
|
return internalType == tThunk && thunk.expr == (Expr*) &eBlackHole;
|
2023-12-10 09:24:45 +02:00
|
|
|
}
|
|
|
|
|
2023-12-11 17:23:08 +02:00
|
|
|
void Value::mkBlackhole()
|
2023-12-10 09:24:45 +02:00
|
|
|
{
|
2023-12-11 17:23:08 +02:00
|
|
|
internalType = tThunk;
|
|
|
|
thunk.expr = (Expr*) &eBlackHole;
|
2023-12-10 09:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 18:28:20 +03:00
|
|
|
#if HAVE_BOEHMGC
|
2022-05-25 16:49:41 +03:00
|
|
|
typedef std::vector<Value *, traceable_allocator<Value *>> ValueVector;
|
|
|
|
typedef std::map<Symbol, Value *, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, Value *>>> ValueMap;
|
|
|
|
typedef std::map<Symbol, ValueVector, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, ValueVector>>> ValueVectorMap;
|
2016-08-29 18:28:20 +03:00
|
|
|
#else
|
|
|
|
typedef std::vector<Value *> ValueVector;
|
|
|
|
typedef std::map<Symbol, Value *> ValueMap;
|
2021-12-02 18:46:44 +02:00
|
|
|
typedef std::map<Symbol, ValueVector> ValueVectorMap;
|
2016-08-29 18:28:20 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* A value allocated in traceable memory.
|
|
|
|
*/
|
2020-04-16 17:28:07 +03:00
|
|
|
typedef std::shared_ptr<Value *> RootValue;
|
|
|
|
|
|
|
|
RootValue allocRootValue(Value * v);
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
}
|