2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2012-01-07 19:26:33 +02:00
|
|
|
|
|
|
|
#include "symbol-table.hh"
|
|
|
|
|
2016-08-30 14:12:12 +03:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
#include <gc/gc_allocator.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
tBlackhole,
|
|
|
|
tPrimOp,
|
|
|
|
tPrimOpApp,
|
2014-11-30 20:16:19 +02:00
|
|
|
tExternal,
|
2016-01-05 01:40:40 +02:00
|
|
|
tFloat
|
2012-01-07 19:26:33 +02:00
|
|
|
} ValueType;
|
|
|
|
|
|
|
|
|
2014-01-21 19:29:55 +02:00
|
|
|
class Bindings;
|
2012-01-07 19:26:33 +02:00
|
|
|
struct Env;
|
|
|
|
struct Expr;
|
|
|
|
struct ExprLambda;
|
|
|
|
struct PrimOp;
|
|
|
|
struct PrimOp;
|
2014-01-21 19:29:55 +02:00
|
|
|
class Symbol;
|
2014-11-30 20:16:19 +02:00
|
|
|
struct Pos;
|
|
|
|
class EvalState;
|
|
|
|
class XMLWriter;
|
2016-08-26 19:55:55 +03:00
|
|
|
class JSONPlaceholder;
|
2012-01-07 19:26:33 +02:00
|
|
|
|
|
|
|
|
2013-08-19 13:35:03 +03:00
|
|
|
typedef long NixInt;
|
2018-07-03 19:04:51 +03:00
|
|
|
typedef double NixFloat;
|
2013-08-19 13:35:03 +03:00
|
|
|
|
2014-11-30 20:16:19 +02:00
|
|
|
/* External values must descend from ExternalValueBase, so that
|
|
|
|
* 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);
|
2014-11-30 20:16:19 +02:00
|
|
|
protected:
|
|
|
|
/* 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:
|
|
|
|
/* Return a simple string describing the type */
|
2014-12-02 17:02:03 +02:00
|
|
|
virtual string showType() const = 0;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
/* Return a string to be used in builtins.typeOf */
|
2014-12-02 17:02:03 +02:00
|
|
|
virtual string typeOf() const = 0;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
/* How much space does this value take up */
|
2014-12-02 17:02:03 +02:00
|
|
|
virtual size_t valueSize(std::set<const void *> & seen) const = 0;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
/* Coerce the value to a string. Defaults to uncoercable, i.e. throws an
|
|
|
|
* error
|
|
|
|
*/
|
2014-12-02 17:02:03 +02:00
|
|
|
virtual string coerceToString(const Pos & pos, PathSet & context, bool copyMore, bool copyToStore) const;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
/* Compare to another value of the same type. Defaults to uncomparable,
|
|
|
|
* i.e. always false.
|
|
|
|
*/
|
2014-12-02 17:02:03 +02:00
|
|
|
virtual bool operator==(const ExternalValueBase & b) const;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
/* Print the value as JSON. Defaults to unconvertable, i.e. throws an error */
|
|
|
|
virtual void printValueAsJSON(EvalState & state, bool strict,
|
2016-08-26 19:55:55 +03:00
|
|
|
JSONPlaceholder & out, PathSet & context) const;
|
2014-11-30 20:16:19 +02:00
|
|
|
|
|
|
|
/* Print the value as XML. Defaults to unevaluated */
|
|
|
|
virtual void printValueAsXML(EvalState & state, bool strict, bool location,
|
2014-12-02 17:02:03 +02:00
|
|
|
XMLWriter & doc, PathSet & context, PathSet & drvsSeen) 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
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
struct Value
|
|
|
|
{
|
|
|
|
ValueType type;
|
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
|
|
|
|
2013-08-06 15:15:11 +03: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
|
2012-01-07 19:26:33 +02:00
|
|
|
|
|
|
|
"--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 {
|
|
|
|
const char * s;
|
|
|
|
const char * * context; // must be in sorted order
|
|
|
|
} string;
|
2013-08-19 13:35:03 +03:00
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
const char * path;
|
|
|
|
Bindings * attrs;
|
|
|
|
struct {
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t size;
|
2012-01-07 19:26:33 +02:00
|
|
|
Value * * elems;
|
2015-07-23 23:05:09 +03:00
|
|
|
} bigList;
|
|
|
|
Value * smallList[2];
|
2012-01-07 19:26:33 +02:00
|
|
|
struct {
|
|
|
|
Env * env;
|
|
|
|
Expr * expr;
|
|
|
|
} thunk;
|
|
|
|
struct {
|
|
|
|
Value * left, * right;
|
|
|
|
} app;
|
|
|
|
struct {
|
|
|
|
Env * env;
|
|
|
|
ExprLambda * fun;
|
|
|
|
} lambda;
|
|
|
|
PrimOp * primOp;
|
|
|
|
struct {
|
|
|
|
Value * left, * right;
|
|
|
|
} 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
|
|
|
|
|
|
|
bool isList() const
|
|
|
|
{
|
|
|
|
return type == tList1 || type == tList2 || type == tListN;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value * * listElems()
|
|
|
|
{
|
|
|
|
return type == tList1 || type == tList2 ? smallList : bigList.elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Value * const * listElems() const
|
|
|
|
{
|
|
|
|
return type == tList1 || type == tList2 ? smallList : bigList.elems;
|
|
|
|
}
|
|
|
|
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t listSize() const
|
2015-07-23 23:05:09 +03:00
|
|
|
{
|
|
|
|
return type == tList1 ? 1 : type == tList2 ? 2 : bigList.size;
|
|
|
|
}
|
2012-01-07 19:26:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* After overwriting an app node, be sure to clear pointers in the
|
|
|
|
Value to ensure that the target isn't kept alive unnecessarily. */
|
|
|
|
static inline void clearValue(Value & v)
|
|
|
|
{
|
2014-10-09 14:08:53 +03:00
|
|
|
v.app.left = v.app.right = 0;
|
2012-01-07 19:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-19 13:35:03 +03:00
|
|
|
static inline void mkInt(Value & v, NixInt n)
|
2012-01-07 19:26:33 +02:00
|
|
|
{
|
|
|
|
clearValue(v);
|
|
|
|
v.type = tInt;
|
|
|
|
v.integer = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-05 01:40:40 +02:00
|
|
|
static inline void mkFloat(Value & v, NixFloat n)
|
|
|
|
{
|
|
|
|
clearValue(v);
|
|
|
|
v.type = tFloat;
|
|
|
|
v.fpoint = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
static inline void mkBool(Value & v, bool b)
|
|
|
|
{
|
|
|
|
clearValue(v);
|
|
|
|
v.type = tBool;
|
|
|
|
v.boolean = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-18 23:22:35 +02:00
|
|
|
static inline void mkNull(Value & v)
|
|
|
|
{
|
2014-10-09 14:08:53 +03:00
|
|
|
clearValue(v);
|
2013-11-18 23:22:35 +02:00
|
|
|
v.type = tNull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
static inline void mkApp(Value & v, Value & left, Value & right)
|
|
|
|
{
|
|
|
|
v.type = tApp;
|
|
|
|
v.app.left = &left;
|
|
|
|
v.app.right = &right;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 12:20:37 +03:00
|
|
|
static inline void mkPrimOpApp(Value & v, Value & left, Value & right)
|
|
|
|
{
|
|
|
|
v.type = tPrimOpApp;
|
|
|
|
v.app.left = &left;
|
|
|
|
v.app.right = &right;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
static inline void mkStringNoCopy(Value & v, const char * s)
|
|
|
|
{
|
|
|
|
v.type = tString;
|
|
|
|
v.string.s = s;
|
|
|
|
v.string.context = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void mkString(Value & v, const Symbol & s)
|
|
|
|
{
|
2012-12-13 07:13:26 +02:00
|
|
|
mkStringNoCopy(v, ((const string &) s).c_str());
|
2012-01-07 19:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mkString(Value & v, const char * s);
|
|
|
|
|
|
|
|
|
|
|
|
static inline void mkPathNoCopy(Value & v, const char * s)
|
|
|
|
{
|
|
|
|
clearValue(v);
|
|
|
|
v.type = tPath;
|
|
|
|
v.path = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mkPath(Value & v, const char * s);
|
|
|
|
|
|
|
|
|
2014-09-22 15:46:42 +03:00
|
|
|
/* Compute the size in bytes of the given value, including all values
|
|
|
|
and environments reachable from it. Static expressions (Exprs) are
|
|
|
|
not included. */
|
|
|
|
size_t valueSize(Value & v);
|
|
|
|
|
|
|
|
|
2016-08-29 18:28:20 +03:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
typedef std::vector<Value *, gc_allocator<Value *> > ValueVector;
|
2017-03-25 00:05:49 +02:00
|
|
|
typedef std::map<Symbol, Value *, std::less<Symbol>, gc_allocator<std::pair<const Symbol, Value *> > > ValueMap;
|
2016-08-29 18:28:20 +03:00
|
|
|
#else
|
|
|
|
typedef std::vector<Value *> ValueVector;
|
|
|
|
typedef std::map<Symbol, Value *> ValueMap;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
}
|