1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 10:11:47 +02:00

libexpr: Format value.hh

The following commits will touch this file significantly, so
it's better to get the formatting out of the way first.
This commit is contained in:
Sergei Zimmerman 2025-06-28 15:30:38 +03:00
parent 04a731b4b9
commit ea32580c9b
No known key found for this signature in database
GPG key ID: A9B0B557CA632325
2 changed files with 158 additions and 87 deletions

View file

@ -256,7 +256,6 @@
''^src/libexpr/include/nix/expr/value-to-json\.hh$'' ''^src/libexpr/include/nix/expr/value-to-json\.hh$''
''^src/libexpr/value-to-xml\.cc$'' ''^src/libexpr/value-to-xml\.cc$''
''^src/libexpr/include/nix/expr/value-to-xml\.hh$'' ''^src/libexpr/include/nix/expr/value-to-xml\.hh$''
''^src/libexpr/include/nix/expr/value\.hh$''
''^src/libexpr/value/context\.cc$'' ''^src/libexpr/value/context\.cc$''
''^src/libexpr/include/nix/expr/value/context\.hh$'' ''^src/libexpr/include/nix/expr/value/context\.hh$''
''^src/libfetchers/attrs\.cc$'' ''^src/libfetchers/attrs\.cc$''

View file

@ -17,7 +17,6 @@ namespace nix {
struct Value; struct Value;
class BindingsBuilder; class BindingsBuilder;
typedef enum { typedef enum {
tUninitialized = 0, tUninitialized = 0,
tInt = 1, tInt = 1,
@ -54,7 +53,7 @@ typedef enum {
nAttrs, nAttrs,
nList, nList,
nFunction, nFunction,
nExternal nExternal,
} ValueType; } ValueType;
class Bindings; class Bindings;
@ -104,7 +103,8 @@ class ExternalValueBase
* Coerce the value to a string. Defaults to uncoercable, i.e. throws an * Coerce the value to a string. Defaults to uncoercable, i.e. throws an
* error. * error.
*/ */
virtual std::string coerceToString(EvalState & state, const PosIdx & pos, NixStringContext & context, bool copyMore, bool copyToStore) const; virtual std::string coerceToString(
EvalState & state, const PosIdx & pos, NixStringContext & context, bool copyMore, bool copyToStore) const;
/** /**
* Compare to another value of the same type. Defaults to uncomparable, * Compare to another value of the same type. Defaults to uncomparable,
@ -115,24 +115,26 @@ class ExternalValueBase
/** /**
* Print the value as JSON. Defaults to unconvertable, i.e. throws an error * Print the value as JSON. Defaults to unconvertable, i.e. throws an error
*/ */
virtual nlohmann::json printValueAsJSON(EvalState & state, bool strict, virtual nlohmann::json
NixStringContext & context, bool copyToStore = true) const; printValueAsJSON(EvalState & state, bool strict, NixStringContext & context, bool copyToStore = true) const;
/** /**
* Print the value as XML. Defaults to unevaluated * Print the value as XML. Defaults to unevaluated
*/ */
virtual void printValueAsXML(EvalState & state, bool strict, bool location, virtual void printValueAsXML(
XMLWriter & doc, NixStringContext & context, PathSet & drvsSeen, EvalState & state,
bool strict,
bool location,
XMLWriter & doc,
NixStringContext & context,
PathSet & drvsSeen,
const PosIdx pos) const; const PosIdx pos) const;
virtual ~ExternalValueBase() virtual ~ExternalValueBase() {};
{
};
}; };
std::ostream & operator<<(std::ostream & str, const ExternalValueBase & v); std::ostream & operator<<(std::ostream & str, const ExternalValueBase & v);
class ListBuilder class ListBuilder
{ {
const size_t size; const size_t size;
@ -147,7 +149,8 @@ public:
: size(x.size) : size(x.size)
, inlineElems{x.inlineElems[0], x.inlineElems[1]} , inlineElems{x.inlineElems[0], x.inlineElems[1]}
, elems(size <= 2 ? inlineElems : x.elems) , elems(size <= 2 ? inlineElems : x.elems)
{ } {
}
Value *& operator[](size_t n) Value *& operator[](size_t n)
{ {
@ -156,13 +159,18 @@ public:
typedef Value ** iterator; typedef Value ** iterator;
iterator begin() { return &elems[0]; } iterator begin()
iterator end() { return &elems[size]; } {
return &elems[0];
}
iterator end()
{
return &elems[size];
}
friend struct Value; friend struct Value;
}; };
struct Value struct Value
{ {
private: private:
@ -184,14 +192,29 @@ public:
// needed by callers into methods of this type // needed by callers into methods of this type
// type() == nThunk // type() == nThunk
inline bool isThunk() const { return internalType == tThunk; }; inline bool isThunk() const
inline bool isApp() const { return internalType == tApp; }; {
return internalType == tThunk;
};
inline bool isApp() const
{
return internalType == tApp;
};
inline bool isBlackhole() const; inline bool isBlackhole() const;
// type() == nFunction // type() == nFunction
inline bool isLambda() const { return internalType == tLambda; }; inline bool isLambda() const
inline bool isPrimOp() const { return internalType == tPrimOp; }; {
inline bool isPrimOpApp() const { return internalType == tPrimOpApp; }; return internalType == tLambda;
};
inline bool isPrimOp() const
{
return internalType == tPrimOp;
};
inline bool isPrimOpApp() const
{
return internalType == tPrimOpApp;
};
/** /**
* Strings in the evaluator carry a so-called `context` which * Strings in the evaluator carry a so-called `context` which
@ -215,26 +238,31 @@ public:
* For canonicity, the store paths should be in sorted order. * For canonicity, the store paths should be in sorted order.
*/ */
struct StringWithContext { struct StringWithContext
{
const char * c_str; const char * c_str;
const char ** context; // must be in sorted order const char ** context; // must be in sorted order
}; };
struct Path { struct Path
{
SourceAccessor * accessor; SourceAccessor * accessor;
const char * path; const char * path;
}; };
struct ClosureThunk { struct ClosureThunk
{
Env * env; Env * env;
Expr * expr; Expr * expr;
}; };
struct FunctionApplicationThunk { struct FunctionApplicationThunk
{
Value *left, *right; Value *left, *right;
}; };
struct Lambda { struct Lambda
{
Env * env; Env * env;
ExprLambda * fun; ExprLambda * fun;
}; };
@ -249,7 +277,8 @@ public:
Path path; Path path;
Bindings * attrs; Bindings * attrs;
struct { struct
{
size_t size; size_t size;
Value * const * elems; Value * const * elems;
} bigList; } bigList;
@ -275,18 +304,35 @@ public:
inline ValueType type(bool invalidIsThunk = false) const inline ValueType type(bool invalidIsThunk = false) const
{ {
switch (internalType) { switch (internalType) {
case tUninitialized: break; case tUninitialized:
case tInt: return nInt; break;
case tBool: return nBool; case tInt:
case tString: return nString; return nInt;
case tPath: return nPath; case tBool:
case tNull: return nNull; return nBool;
case tAttrs: return nAttrs; case tString:
case tList1: case tList2: case tListN: return nList; return nString;
case tLambda: case tPrimOp: case tPrimOpApp: return nFunction; case tPath:
case tExternal: return nExternal; return nPath;
case tFloat: return nFloat; case tNull:
case tThunk: case tApp: return nThunk; 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;
case tThunk:
case tApp:
return nThunk;
} }
if (invalidIsThunk) if (invalidIsThunk)
return nThunk; return nThunk;
@ -438,9 +484,7 @@ public:
SourcePath path() const SourcePath path() const
{ {
assert(internalType == tPath); assert(internalType == tPath);
return SourcePath( return SourcePath(ref(pathAccessor()->shared_from_this()), CanonPath(CanonPath::unchecked_t(), pathStr()));
ref(pathAccessor()->shared_from_this()),
CanonPath(CanonPath::unchecked_t(), pathStr()));
} }
std::string_view string_view() const std::string_view string_view() const
@ -461,43 +505,66 @@ public:
} }
ExternalValueBase * external() const ExternalValueBase * external() const
{ return payload.external; } {
return payload.external;
}
const Bindings * attrs() const const Bindings * attrs() const
{ return payload.attrs; } {
return payload.attrs;
}
const PrimOp * primOp() const const PrimOp * primOp() const
{ return payload.primOp; } {
return payload.primOp;
}
bool boolean() const bool boolean() const
{ return payload.boolean; } {
return payload.boolean;
}
NixInt integer() const NixInt integer() const
{ return payload.integer; } {
return payload.integer;
}
NixFloat fpoint() const NixFloat fpoint() const
{ return payload.fpoint; } {
return payload.fpoint;
}
Lambda lambda() const Lambda lambda() const
{ return payload.lambda; } {
return payload.lambda;
}
ClosureThunk thunk() const ClosureThunk thunk() const
{ return payload.thunk; } {
return payload.thunk;
}
FunctionApplicationThunk primOpApp() const FunctionApplicationThunk primOpApp() const
{ return payload.primOpApp; } {
return payload.primOpApp;
}
FunctionApplicationThunk app() const FunctionApplicationThunk app() const
{ return payload.app; } {
return payload.app;
}
const char * pathStr() const const char * pathStr() const
{ return payload.path.path; } {
return payload.path.path;
}
SourceAccessor * pathAccessor() const SourceAccessor * pathAccessor() const
{ return payload.path.accessor; } {
return payload.path.accessor;
}
}; };
extern ExprBlackHole eBlackHole; extern ExprBlackHole eBlackHole;
bool Value::isBlackhole() const bool Value::isBlackhole() const
@ -510,11 +577,16 @@ void Value::mkBlackhole()
mkThunk(nullptr, (Expr *) &eBlackHole); mkThunk(nullptr, (Expr *) &eBlackHole);
} }
typedef std::vector<Value *, traceable_allocator<Value *>> ValueVector; typedef std::vector<Value *, traceable_allocator<Value *>> ValueVector;
typedef std::unordered_map<Symbol, Value *, std::hash<Symbol>, std::equal_to<Symbol>, traceable_allocator<std::pair<const Symbol, Value *>>> ValueMap; typedef std::unordered_map<
typedef std::map<Symbol, ValueVector, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, ValueVector>>> ValueVectorMap; Symbol,
Value *,
std::hash<Symbol>,
std::equal_to<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;
/** /**
* A value allocated in traceable memory. * A value allocated in traceable memory.