2010-04-07 13:59:45 +00:00
|
|
|
#include "value-to-xml.hh"
|
2006-08-24 14:16:55 +00:00
|
|
|
#include "xml-writer.hh"
|
2012-03-05 21:04:40 +00:00
|
|
|
#include "eval-inline.hh"
|
2007-01-13 14:48:41 +00:00
|
|
|
#include "util.hh"
|
2006-08-24 14:16:55 +00:00
|
|
|
|
2008-05-21 11:17:31 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2006-08-24 14:16:55 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
static XMLAttrs singletonAttrs(const std::string & name, const std::string & value)
|
2006-08-24 14:16:55 +00:00
|
|
|
{
|
|
|
|
XMLAttrs attrs;
|
|
|
|
attrs[name] = value;
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 14:46:47 +00:00
|
|
|
static void printValueAsXML(EvalState & state, bool strict, bool location,
|
2021-11-14 01:29:31 +00:00
|
|
|
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen,
|
2022-03-04 18:31:59 +00:00
|
|
|
const PosIdx pos);
|
2007-01-13 15:11:10 +00:00
|
|
|
|
|
|
|
|
2022-03-05 13:40:24 +00:00
|
|
|
static void posToXML(EvalState & state, XMLAttrs & xmlAttrs, const Pos & pos)
|
2010-05-07 14:46:47 +00:00
|
|
|
{
|
2022-12-12 23:48:04 +00:00
|
|
|
if (auto path = std::get_if<Path>(&pos.origin))
|
|
|
|
xmlAttrs["path"] = *path;
|
2023-03-02 14:44:19 +00:00
|
|
|
xmlAttrs["line"] = fmt("%1%", pos.line);
|
|
|
|
xmlAttrs["column"] = fmt("%1%", pos.column);
|
2010-05-07 14:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void showAttrs(EvalState & state, bool strict, bool location,
|
|
|
|
Bindings & attrs, XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
|
2007-01-13 15:11:10 +00:00
|
|
|
{
|
|
|
|
StringSet names;
|
2015-07-17 17:24:28 +00:00
|
|
|
|
|
|
|
for (auto & i : attrs)
|
2022-03-05 13:40:24 +00:00
|
|
|
names.emplace(state.symbols[i.name]);
|
2015-07-17 17:24:28 +00:00
|
|
|
|
|
|
|
for (auto & i : names) {
|
|
|
|
Attr & a(*attrs.find(state.symbols.create(i)));
|
|
|
|
|
2010-05-07 14:46:47 +00:00
|
|
|
XMLAttrs xmlAttrs;
|
2015-07-17 17:24:28 +00:00
|
|
|
xmlAttrs["name"] = i;
|
2022-03-05 13:40:24 +00:00
|
|
|
if (location && a.pos) posToXML(state, xmlAttrs, state.positions[a.pos]);
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2010-05-07 14:46:47 +00:00
|
|
|
XMLOpenElement _(doc, "attr", xmlAttrs);
|
|
|
|
printValueAsXML(state, strict, location,
|
2022-03-04 18:31:59 +00:00
|
|
|
*a.value, doc, context, drvsSeen, a.pos);
|
2008-08-14 12:53:29 +00:00
|
|
|
}
|
2008-08-14 10:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 14:46:47 +00:00
|
|
|
static void printValueAsXML(EvalState & state, bool strict, bool location,
|
2021-11-14 01:29:31 +00:00
|
|
|
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen,
|
2022-03-04 18:31:59 +00:00
|
|
|
const PosIdx pos)
|
2006-08-24 14:16:55 +00:00
|
|
|
{
|
2007-01-13 14:48:41 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2021-11-14 01:29:31 +00:00
|
|
|
if (strict) state.forceValue(v, pos);
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
switch (v.type()) {
|
2006-08-24 14:16:55 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nInt:
|
2023-03-02 14:44:19 +00:00
|
|
|
doc.writeEmptyElement("int", singletonAttrs("value", fmt("%1%", v.integer)));
|
2010-04-07 13:55:46 +00:00
|
|
|
break;
|
2006-08-24 14:16:55 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nBool:
|
2010-04-07 13:55:46 +00:00
|
|
|
doc.writeEmptyElement("bool", singletonAttrs("value", v.boolean ? "true" : "false"));
|
|
|
|
break;
|
2006-08-24 14:16:55 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nString:
|
2010-04-07 13:55:46 +00:00
|
|
|
/* !!! show the context? */
|
2010-06-10 10:29:50 +00:00
|
|
|
copyContext(v, context);
|
2010-04-07 13:55:46 +00:00
|
|
|
doc.writeEmptyElement("string", singletonAttrs("value", v.string.s));
|
|
|
|
break;
|
2006-08-24 14:16:55 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nPath:
|
2023-04-06 11:15:50 +00:00
|
|
|
doc.writeEmptyElement("path", singletonAttrs("value", v.path().to_string()));
|
2010-04-07 13:55:46 +00:00
|
|
|
break;
|
2007-01-13 15:11:10 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nNull:
|
2010-04-07 13:55:46 +00:00
|
|
|
doc.writeEmptyElement("null");
|
|
|
|
break;
|
2007-01-13 15:11:10 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nAttrs:
|
2010-04-07 13:55:46 +00:00
|
|
|
if (state.isDerivation(v)) {
|
|
|
|
XMLAttrs xmlAttrs;
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2010-04-13 12:25:42 +00:00
|
|
|
Bindings::iterator a = v.attrs->find(state.symbols.create("derivation"));
|
2010-04-07 13:55:46 +00:00
|
|
|
|
|
|
|
Path drvPath;
|
2010-04-13 12:25:42 +00:00
|
|
|
a = v.attrs->find(state.sDrvPath);
|
2010-05-07 14:46:47 +00:00
|
|
|
if (a != v.attrs->end()) {
|
2022-03-04 18:31:59 +00:00
|
|
|
if (strict) state.forceValue(*a->value, a->pos);
|
2020-12-17 13:45:45 +00:00
|
|
|
if (a->value->type() == nString)
|
2010-10-24 00:41:29 +00:00
|
|
|
xmlAttrs["drvPath"] = drvPath = a->value->string.s;
|
2010-05-07 14:46:47 +00:00
|
|
|
}
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2010-04-13 12:25:42 +00:00
|
|
|
a = v.attrs->find(state.sOutPath);
|
2010-05-07 14:46:47 +00:00
|
|
|
if (a != v.attrs->end()) {
|
2022-03-04 18:31:59 +00:00
|
|
|
if (strict) state.forceValue(*a->value, a->pos);
|
2020-12-17 13:45:45 +00:00
|
|
|
if (a->value->type() == nString)
|
2010-10-24 00:41:29 +00:00
|
|
|
xmlAttrs["outPath"] = a->value->string.s;
|
2010-05-07 14:46:47 +00:00
|
|
|
}
|
2010-04-07 13:55:46 +00:00
|
|
|
|
|
|
|
XMLOpenElement _(doc, "derivation", xmlAttrs);
|
|
|
|
|
2019-10-09 13:51:52 +00:00
|
|
|
if (drvPath != "" && drvsSeen.insert(drvPath).second)
|
2010-05-07 14:46:47 +00:00
|
|
|
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
|
2019-10-09 13:51:52 +00:00
|
|
|
else
|
2010-04-07 13:55:46 +00:00
|
|
|
doc.writeEmptyElement("repeated");
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
XMLOpenElement _(doc, "attrs");
|
2010-05-07 14:46:47 +00:00
|
|
|
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
|
2010-04-07 13:55:46 +00:00
|
|
|
}
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2010-04-07 13:55:46 +00:00
|
|
|
break;
|
2007-01-13 15:11:10 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nList: {
|
2010-04-07 13:55:46 +00:00
|
|
|
XMLOpenElement _(doc, "list");
|
2021-11-24 19:21:34 +00:00
|
|
|
for (auto v2 : v.listItems())
|
|
|
|
printValueAsXML(state, strict, location, *v2, doc, context, drvsSeen, pos);
|
2010-04-07 13:55:46 +00:00
|
|
|
break;
|
2007-01-13 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nFunction: {
|
|
|
|
if (!v.isLambda()) {
|
|
|
|
// FIXME: Serialize primops and primopapps
|
|
|
|
doc.writeEmptyElement("unevaluated");
|
|
|
|
break;
|
|
|
|
}
|
2010-05-07 14:46:47 +00:00
|
|
|
XMLAttrs xmlAttrs;
|
2022-03-05 13:40:24 +00:00
|
|
|
if (location) posToXML(state, xmlAttrs, state.positions[v.lambda.fun->pos]);
|
2010-05-07 14:46:47 +00:00
|
|
|
XMLOpenElement _(doc, "function", xmlAttrs);
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2021-10-06 15:08:08 +00:00
|
|
|
if (v.lambda.fun->hasFormals()) {
|
2010-04-12 18:30:11 +00:00
|
|
|
XMLAttrs attrs;
|
2022-03-05 13:40:24 +00:00
|
|
|
if (v.lambda.fun->arg) attrs["name"] = state.symbols[v.lambda.fun->arg];
|
2010-04-12 18:30:11 +00:00
|
|
|
if (v.lambda.fun->formals->ellipsis) attrs["ellipsis"] = "1";
|
|
|
|
XMLOpenElement _(doc, "attrspat", attrs);
|
2022-03-05 13:40:24 +00:00
|
|
|
for (auto & i : v.lambda.fun->formals->lexicographicOrder(state.symbols))
|
|
|
|
doc.writeEmptyElement("attr", singletonAttrs("name", state.symbols[i.name]));
|
2010-04-12 18:30:11 +00:00
|
|
|
} else
|
2022-03-05 13:40:24 +00:00
|
|
|
doc.writeEmptyElement("varpat", singletonAttrs("name", state.symbols[v.lambda.fun->arg]));
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2010-04-07 13:55:46 +00:00
|
|
|
break;
|
2006-08-24 14:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nExternal:
|
2021-11-14 01:29:31 +00:00
|
|
|
v.external->printValueAsXML(state, strict, location, doc, context, drvsSeen, pos);
|
2014-11-30 18:16:19 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nFloat:
|
2023-03-02 14:44:19 +00:00
|
|
|
doc.writeEmptyElement("float", singletonAttrs("value", fmt("%1%", v.fpoint)));
|
2016-01-04 23:40:40 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nThunk:
|
2010-04-07 13:55:46 +00:00
|
|
|
doc.writeEmptyElement("unevaluated");
|
2006-08-24 14:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-30 18:16:19 +00:00
|
|
|
void ExternalValueBase::printValueAsXML(EvalState & state, bool strict,
|
2021-11-14 01:29:31 +00:00
|
|
|
bool location, XMLWriter & doc, PathSet & context, PathSet & drvsSeen,
|
2022-03-04 18:31:59 +00:00
|
|
|
const PosIdx pos) const
|
2014-11-30 18:16:19 +00:00
|
|
|
{
|
|
|
|
doc.writeEmptyElement("unevaluated");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 14:46:47 +00:00
|
|
|
void printValueAsXML(EvalState & state, bool strict, bool location,
|
2022-03-04 18:31:59 +00:00
|
|
|
Value & v, std::ostream & out, PathSet & context, const PosIdx pos)
|
2006-08-24 14:16:55 +00:00
|
|
|
{
|
|
|
|
XMLWriter doc(true, out);
|
|
|
|
XMLOpenElement root(doc, "expr");
|
2015-07-17 17:24:28 +00:00
|
|
|
PathSet drvsSeen;
|
2021-11-14 01:29:31 +00:00
|
|
|
printValueAsXML(state, strict, location, v, doc, context, drvsSeen, pos);
|
2006-08-24 14:16:55 +00:00
|
|
|
}
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|