1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
#include "eval.hh"
#include "libexpr/nixexpr.hh"
#include "primops.hh"
#include "gc-small-vector.hh"
/// This file contains all implementations of `Expr::eval`, and some other helper functions defined by
/// `Expr` subtypes. Note that some of the evaluation helper functions on `EvalState` that do the heavy
/// lifting are not in this file but kept in `eval.cc`. In the future, more logic from here will be factored
/// out into helpers over at `eval.cc` until this file contains a readable and high-level implementation of
/// the evaluator.
namespace nix {
Value Expr::makeThunk(Evaluator & ctx, Env & env)
{
ctx.stats.nrThunks++;
return {NewValueAs::thunk, ctx.mem, env, *this};
}
/* Create a thunk for the delayed computation of the given expression
in the given environment. But if the expression is a variable,
then look it up right away. This significantly reduces the number
of thunks allocated. */
Value Expr::maybeThunk(EvalState & state, Env & env)
{
return makeThunk(state.ctx, env);
}
Value ExprVar::maybeThunk(EvalState & state, Env & env)
{
Value * v = state.lookupVar(&env, *this, true);
/* The value might not be initialised in the environment yet.
In that case, ignore it. */
if (v && !v->isInvalid()) {
state.ctx.stats.nrAvoided++;
return *v;
}
return Expr::maybeThunk(state, env);
}
Value ExprLiteral::maybeThunk(EvalState & state, Env & env)
{
state.ctx.stats.nrAvoided++;
return v;
}
Value ExprList::maybeThunk(EvalState & state, Env & env)
{
if (elems.empty()) {
return Value::EMPTY_LIST;
}
return Expr::maybeThunk(state, env);
}
Value Expr::eval(EvalState & state, Env & env)
{
abort();
}
Value ExprLiteral::eval(EvalState & state, Env & env)
{
return this->v;
}
Value ExprInheritFrom::eval(EvalState & state, Env & env)
{
Value & v2 = env.values[displ];
state.forceValue(v2, pos);
return v2;
}
Env * ExprAttrs::buildInheritFromEnv(EvalState & state, Env & up)
{
Env & inheritEnv = state.ctx.mem.allocEnv(inheritFromExprs->size());
inheritEnv.up = &up;
Displacement displ = 0;
for (auto & from : *inheritFromExprs) {
inheritEnv.values[displ++] = from->maybeThunk(state, up);
}
return &inheritEnv;
}
Value ExprSet::eval(EvalState & state, Env & env)
{
Bindings::Size capacity = attrs.size() + dynamicAttrs.size();
Value v = {NewValueAs::attrs, state.ctx.buildBindings(capacity).finish()};
auto dynamicEnv = &env;
if (recursive) {
/* Create a new environment that contains the attributes in
this `rec'. */
Env & env2(state.ctx.mem.allocEnv(attrs.size()));
env2.up = &env;
dynamicEnv = &env2;
Env * inheritEnv = inheritFromExprs ? buildInheritFromEnv(state, env2) : nullptr;
ExprAttrs::AttrDefs::iterator overrides = attrs.find(state.ctx.symbols.sym___overrides);
bool hasOverrides = overrides != attrs.end();
/* The recursive attributes are evaluated in the new
environment, while the inherited attributes are evaluated
in the original environment. */
Displacement displ = 0;
for (auto & i : attrs) {
Env & thunkEnv = *i.second.chooseByKind(&env2, &env, inheritEnv);
Value vAttr = hasOverrides && i.second.kind != ExprAttrs::AttrDef::Kind::Inherited
? i.second.e->makeThunk(state.ctx, thunkEnv)
: i.second.e->maybeThunk(state, thunkEnv);
env2.values[displ++] = vAttr;
v.attrs()->push_back(Attr(i.first, vAttr, i.second.pos));
}
/* If the rec contains an attribute called `__overrides', then
evaluate it, and add the attributes in that set to the rec.
This allows overriding of recursive attributes, which is
otherwise not possible. (You can use the // operator to
replace an attribute, but other attributes in the rec will
still reference the original value, because that value has
been substituted into the bodies of the other attributes.
Hence we need __overrides.) */
if (hasOverrides) {
Value & vOverrides = (*v.attrs())[overrides->second.displ].value;
state.forceAttrs(vOverrides, noPos, "while evaluating the `__overrides` attribute");
Bindings * newBnds = state.ctx.mem.allocBindings(capacity + vOverrides.attrs()->size());
for (auto & i : *v.attrs()) {
newBnds->push_back(i);
}
for (auto & i : *vOverrides.attrs()) {
ExprAttrs::AttrDefs::iterator j = attrs.find(i.name);
if (j != attrs.end()) {
(*newBnds)[j->second.displ] = i;
env2.values[j->second.displ] = i.value;
} else {
newBnds->push_back(i);
}
}
newBnds->sort();
v = {NewValueAs::attrs, newBnds};
}
}
else {
Env * inheritEnv = inheritFromExprs ? buildInheritFromEnv(state, env) : nullptr;
for (auto & i : attrs) {
v.attrs()->push_back(Attr(
i.first,
i.second.e->maybeThunk(state, *i.second.chooseByKind(&env, &env, inheritEnv)),
i.second.pos
));
}
}
/* Dynamic attrs apply *after* rec and __overrides. */
for (auto & i : dynamicAttrs) {
/* Before evaluating dynamic attrs, we blackhole the output attrset and only restore it after the operation.
* This is to avoid exposing the partially constructed set as a value, see
* http://github.com/NixOS/nix/issues/7012. Any accesses to the output attrset will thus infrec.
*/
Value vBackup = v;
Symbol nameSym;
{
KJ_DEFER(v = vBackup);
v = Value{NewValueAs::blackhole};
Value nameVal = i.nameExpr->eval(state, *dynamicEnv);
state.forceValue(nameVal, i.pos);
if (nameVal.type() == nNull) {
continue;
}
state.forceStringNoCtx(nameVal, i.pos, "while evaluating the name of a dynamic attribute");
nameSym = state.ctx.symbols.create(nameVal.str());
}
auto j = v.attrs()->get(nameSym);
if (j) {
state.ctx.errors
.make<EvalError>(
"dynamic attribute '%1%' already defined at %2%",
state.ctx.symbols[nameSym],
state.ctx.positions[j->pos]
)
.atPos(i.pos)
.withFrame(env, *this)
.debugThrow();
}
// clang-format off
/* This line is so wrong that it is best kept in here with the documentation why it is wrong,
* lest some naive soul may add it once again some year in the future.
* See the following witness as to why it is wrong:
*
* nix-repl> fun = (name: { ${name} = x: x; }) # This function creates a dynamic attribute with a variable name
* Added fun.
* nix-repl> revSeq = x: y: builtins.seq x (builtins.seq y x) # evaluate x, then y in sequence, then return x
* Added revSeq.
* nix-repl> fun "foo" # The code seemingly works
* { foo = «lambda foo @ «string»:1:26»; }
* nix-repl> fun "bar" #
* { bar = «lambda bar @ «string»:1:26»; }
* nix-repl> revSeq (fun "foo") (fun "bar") # Until it doesn't
* { foo = «lambda bar @ «string»:1:26»; }
*
* What happened? Expressions are AST bound, therefore all lambdas share the same Expr and thus *the same name*.
* Using `setName` here updates the name of *all* lambdas from that expression, not just of the value at hand.
* And this is why all expressions must be treated as immutable after parsing.
*/
/* i.valueExpr->setName(nameSym); */
// clang-format on
/* Keep sorted order so find can catch duplicates */
v.attrs()->push_back(Attr(nameSym, i.valueExpr->maybeThunk(state, *dynamicEnv), i.pos));
v.attrs()->sort(); // FIXME: inefficient
}
v.attrs()->pos = pos;
return v;
}
Value ExprLet::eval(EvalState & state, Env & env)
{
/* Create a new environment that contains the attributes in this
`let'. */
Env & env2(state.ctx.mem.allocEnv(attrs.size()));
env2.up = &env;
Env * inheritEnv = inheritFromExprs ? buildInheritFromEnv(state, env2) : nullptr;
/* The recursive attributes are evaluated in the new environment,
while the inherited attributes are evaluated in the original
environment. */
Displacement displ = 0;
for (auto & i : attrs) {
env2.values[displ++] = i.second.e->maybeThunk(state, *i.second.chooseByKind(&env2, &env, inheritEnv));
}
return body->eval(state, env2);
}
Value ExprList::eval(EvalState & state, Env & env)
{
auto result = state.ctx.mem.newList(elems.size());
Value v = {NewValueAs::list, result};
for (auto && [n, v2] : enumerate(result->span())) {
v2 = elems[n]->maybeThunk(state, env);
}
return v;
}
Value ExprVar::eval(EvalState & state, Env & env)
{
Value * v2 = state.lookupVar(&env, *this, false);
try {
state.forceValue(*v2, pos);
} catch (Error & e) {
/* `name` can be invalid if we are an ExprInheritFrom */
if (name) {
e.addTrace(state.ctx.positions[getPos()], "while evaluating %s", state.ctx.symbols[name]);
}
throw;
}
return *v2;
}
Value ExprWith::eval(EvalState & state, Env & env)
{
Env & env2(state.ctx.mem.allocEnv(1));
env2.up = &env;
env2.values[0] = attrs->maybeThunk(state, env);
return body->eval(state, env2);
}
Value ExprIf::eval(EvalState & state, Env & env)
{
Value vCond = cond->eval(state, env);
return (state.checkBool(vCond, env, *cond) ? *then : *else_).eval(state, env);
}
Value ExprAssert::eval(EvalState & state, Env & env)
{
Value vCond = cond->eval(state, env);
if (!state.checkBool(vCond, env, *cond)) {
state.ctx.errors.make<AssertionError>("assertion failed")
.atPos(pos)
.withFrame(env, *this)
.debugThrow();
}
return body->eval(state, env);
}
Value ExprOpNot::eval(EvalState & state, Env & env)
{
Value vInner = e->eval(state, env);
return {NewValueAs::boolean, !state.checkBool(vInner, env, *e)};
}
Value ExprOpEq::eval(EvalState & state, Env & env)
{
Value v1 = e1->eval(state, env);
Value v2 = e2->eval(state, env);
return {NewValueAs::boolean, state.eqValues(v1, v2, pos, "while testing two values for equality")};
}
Value ExprOpNEq::eval(EvalState & state, Env & env)
{
Value v1 = e1->eval(state, env);
Value v2 = e2->eval(state, env);
return {NewValueAs::boolean, !state.eqValues(v1, v2, pos, "while testing two values for inequality")};
}
Value ExprOpAnd::eval(EvalState & state, Env & env)
{
Value v1 = e1->eval(state, env);
/* Explicitly short-circuit */
if (!state.checkBool(v1, env, *e1)) {
return {NewValueAs::boolean, false};
}
Value v2 = e2->eval(state, env);
return {NewValueAs::boolean, state.checkBool(v2, env, *e2)};
}
Value ExprOpOr::eval(EvalState & state, Env & env)
{
Value v1 = e1->eval(state, env);
/* Explicitly short-circuit */
if (state.checkBool(v1, env, *e1)) {
return {NewValueAs::boolean, true};
}
Value v2 = e2->eval(state, env);
return {NewValueAs::boolean, state.checkBool(v2, env, *e2)};
}
Value ExprOpImpl::eval(EvalState & state, Env & env)
{
Value v1 = e1->eval(state, env);
/* Explicitly short-circuit (ex falso quodlibet) */
if (!state.checkBool(v1, env, *e1)) {
return {NewValueAs::boolean, true};
}
Value v2 = e2->eval(state, env);
return {NewValueAs::boolean, state.checkBool(v2, env, *e2)};
}
Value ExprOpUpdate::eval(EvalState & state, Env & env)
{
Value v1 = e1->eval(state, env);
state.checkAttrs(v1, env, *e1);
Value v2 = e2->eval(state, env);
state.checkAttrs(v2, env, *e2);
return state.updateAttrs(v1, v2);
}
Value ExprOpConcatLists::eval(EvalState & state, Env & env)
{
state.ctx.stats.nrListConcats++;
/* We could simply call into `state.concatLists`, but that would add a redundant trace to our errors,
* and to fix that we would need to make the error on it and `forceList` optional, and *sigh*
*/
Value v1 = e1->eval(state, env);
state.checkList(v1, env, *this); // Pass in `this` instead of `e1` to make the error point to the `++`
Value v2 = e2->eval(state, env);
state.checkList(v2, env, *this); // Pass in `this` instead of `e2` to make the error point to the `++`
size_t l1 = v1.listSize(), l2 = v2.listSize(), len = l1 + l2;
if (l1 == 0) {
return v2;
} else if (l2 == 0) {
return v1;
} else {
auto list = state.ctx.mem.newList(len);
auto out = list->elems;
std::copy(v1.listElems(), v1.listElems() + l1, out);
std::copy(v2.listElems(), v2.listElems() + l2, out + l1);
return {NewValueAs::list, list};
}
}
Value ExprConcatStrings::eval(EvalState & state, Env & env)
{
NixStringContext context;
std::vector<BackedStringView> s;
size_t sSize = 0;
NixInt n{0};
NixFloat nf = 0;
bool first = !isInterpolation;
ValueType firstType = nString;
const auto str = [&] {
std::string result;
result.reserve(sSize);
for (const auto & part : s) {
result += *part;
}
return result;
};
/* build a gc'd value string directly instead of going through str()
and mkString to save an allocation and copy */
const auto gcStr = [&] {
auto result = Value::Str::gcAlloc(sSize);
char * tmp = result->contents;
for (const auto & part : s) {
memcpy(tmp, part->data(), part->size());
tmp += part->size();
}
return result;
};
// List of returned strings. References to these Values must NOT be persisted.
SmallTemporaryValueVector<conservativeStackReservation> values;
values.reserve(es.size());
for (auto & [i_pos, i] : es) {
values.push_back(i->eval(state, env));
Value & vTmp = values.back();
/* If the first element is a path, then the result will also
be a path, we don't copy anything (yet - that's done later,
since paths are copied when they are used in a derivation),
and none of the strings are allowed to have contexts. */
if (first) {
firstType = vTmp.type();
}
if (firstType == nInt) {
if (vTmp.type() == nInt) {
auto newN = n + vTmp.integer();
if (auto checked = newN.valueChecked(); checked.has_value()) {
n = NixInt(*checked);
} else {
state.ctx.errors
.make<EvalError>("integer overflow in adding %1% + %2%", n, vTmp.integer())
.atPos(isInterpolation ? i_pos : pos)
.debugThrow();
}
} else if (vTmp.type() == nFloat) {
// Upgrade the type from int to float;
firstType = nFloat;
nf = n.value;
nf += vTmp.fpoint();
} else {
state.ctx.errors.make<EvalError>("cannot add %1% to an integer", showType(vTmp))
.atPos(isInterpolation ? i_pos : pos)
.withFrame(env, *this)
.debugThrow();
}
} else if (firstType == nFloat) {
if (vTmp.type() == nInt) {
nf += vTmp.integer().value;
} else if (vTmp.type() == nFloat) {
nf += vTmp.fpoint();
} else {
state.ctx.errors.make<EvalError>("cannot add %1% to a float", showType(vTmp))
.atPos(isInterpolation ? i_pos : pos)
.withFrame(env, *this)
.debugThrow();
}
} else {
if (s.empty()) {
s.reserve(es.size());
}
/* If we are coercing inside of an interpolation, we may allow slightly more comfort by coercing
* things like integers. */
auto coercionMode = isInterpolation && featureSettings.isEnabled(Xp::CoerceIntegers)
? StringCoercionMode::Interpolation
: StringCoercionMode::Strict;
/* skip canonization of first path, which would only be not
canonized in the first place if it's coming from a ./${foo} type
path */
auto part = state.coerceToString(
isInterpolation ? i_pos : pos,
vTmp,
context,
(isInterpolation && firstType == nPath)
? "while evaluating a path interpolation"
: (isInterpolation ? "while evaluating a string interpolation"
// TODO: to the person who eventually cleans up all of this mess,
// please turn this into "cannot concatenate $type to a $type" instead.
: "while concatenating"),
coercionMode,
firstType == nString,
!first
);
sSize += part->size();
s.emplace_back(std::move(part));
}
first = false;
}
if (firstType == nInt) {
return {NewValueAs::integer, n};
} else if (firstType == nFloat) {
return {NewValueAs::floating, nf};
} else if (firstType == nPath) {
if (!context.empty()) {
state.ctx.errors
.make<EvalError>("a string that refers to a store path cannot be appended to a path")
.atPos(pos)
.withFrame(env, *this)
.debugThrow();
}
return {NewValueAs::path, CanonPath(canonPath(str()))};
} else {
return {NewValueAs::string, gcStr(), context};
}
}
Value ExprPos::eval(EvalState & state, Env & env)
{
return state.mkPos(pos);
}
Value ExprBlackHole::eval(EvalState & state, Env & env)
{
state.ctx.errors.make<InfiniteRecursionError>("infinite recursion encountered").debugThrow();
}
Value ExprDebugFrame::eval(EvalState & state, Env & env)
{
auto dts = makeDebugTraceStacker(state, *inner, env, state.ctx.positions[pos], message);
return inner->eval(state, env);
}
/** Returns `nullptr` if we should be using a default instead. */
Attr const *
ExprSelect::selectSingleAttr(EvalState & state, Env & env, AttrName const & attrName, Value & vCurrent)
{
Symbol const attrSym = getName(attrName, state, env);
try {
state.forceValue(vCurrent, pos);
} catch (Error & e) {
// clang-format off
e.addTrace(state.ctx.positions[attrName.pos], HintFmt(
"while evaluating an expression to select '%s' on it", state.ctx.symbols[attrSym]
));
// clang-format on
throw;
}
if (vCurrent.type() != nAttrs) {
// If we have an `or` provided default, then it doesn't have to be an attrset.
// Let the caller know there's no attr value here.
if (def != nullptr) {
return nullptr;
}
// Otherwise, we must type error.
// clang-format off
state.ctx.errors.make<TypeError>(
"expected a set but found %s: %s",
showType(vCurrent),
ValuePrinter(state, vCurrent, errorPrintOptions)
).addTrace(
attrName.pos,
HintFmt("while selecting '%s'", state.ctx.symbols[attrSym])
).debugThrow();
// clang-format on
}
// Now that we know it's an attrset, we can actually look for the name.
auto const attrIt = vCurrent.attrs()->get(attrSym);
if (!attrIt) {
// Again if we have an `or` provided default, then missing attr is not an error.
if (def != nullptr) {
return nullptr;
}
// Otherwise, we collect all attr names and throw an attr missing error.
std::set<std::string> const allAttrNames = *vCurrent.attrs()
| std::views::transform([&state](auto const & attr) {
return std::string{state.ctx.symbols[attr.name]};
})
| std::ranges::to<std::set>();
auto suggestions = Suggestions::bestMatches(allAttrNames, state.ctx.symbols[attrSym]);
state.ctx.errors.make<EvalError>("attribute '%s' missing", state.ctx.symbols[attrSym])
.atPos(attrName.pos)
.withSuggestions(suggestions)
.withFrame(env, *this)
.debugThrow();
}
// If we made it here, then we successfully found the attribute.
// Return it to our caller!
return attrIt;
}
Value ExprSelect::eval(EvalState & state, Env & env)
{
// Position for the current attrset Value in this select chain.
PosIdx posCurrent;
// Position for the current selector in this select chain.
PosIdx posCurrentSyntax;
Value baseSelectee = [&]() {
try {
// Evaluate the original thing we're selecting on.
return e->eval(state, env);
} catch (Error & e) {
// clang-format off
e.addTrace(state.ctx.positions[getPos()], HintFmt(
"while evaluating an expression to select '%s' on it",
showAttrPath(state.ctx.symbols, attrPath)
));
// clang-format on
throw;
}
}();
try {
// With the original selectee evaluated, we'll walk the selection path starting
// with the evaluated original selectee.
std::reference_wrapper<Value> curSelectee = std::ref(baseSelectee);
for (AttrName const & attrName : attrPath) {
state.ctx.stats.nrLookups++;
// Select `attrName` on `curSelectee`.
auto const attr = selectSingleAttr(state, env, attrName, curSelectee.get());
if (!attr) {
// Use default.
try {
return this->def->eval(state, env);
} catch (Error & err) {
err.addTrace(
state.ctx.positions[this->def->pos],
"while evaluating fallback for missing attribute '%s'",
state.ctx.symbols[getName(attrName, state, env)]
);
throw;
}
}
// The selection worked. If we have another iteration, then we use `attr->value`
// as the thing to select on. If this is the last iteration, then `attr->value`
// is the final value this ExprSelect evaluated to.
curSelectee = std::ref(attr->value);
posCurrent = attr->pos;
posCurrentSyntax = attrName.pos;
if (state.ctx.stats.countCalls) {
state.ctx.stats.attrSelects[posCurrent]++;
}
}
state.forceValue(curSelectee.get(), posCurrent ? posCurrent : posCurrentSyntax);
return curSelectee.get();
} catch (Error & err) {
auto const & lastPos = state.ctx.positions[posCurrent];
if (lastPos && !std::get_if<Pos::Hidden>(&lastPos.origin)) {
err.addTrace(lastPos, "while evaluating the attribute '%s'", showAttrPath(state, env, attrPath));
}
throw;
}
}
Value ExprOpHasAttr::eval(EvalState & state, Env & env)
{
Value vTmp = e->eval(state, env);
Value * vAttrs = &vTmp;
for (auto & i : attrPath) {
state.forceValue(*vAttrs, getPos());
const Attr * j;
auto name = getName(i, state, env);
if (vAttrs->type() != nAttrs || (j = vAttrs->attrs()->get(name)) == nullptr) {
return {NewValueAs::boolean, false};
} else {
vAttrs = &j->value;
}
}
return {NewValueAs::boolean, true};
}
Value ExprLambda::eval(EvalState & state, Env & env)
{
return {NewValueAs::lambda, state.ctx.mem, env, *this};
}
Value ExprCall::eval(EvalState & state, Env & env)
{
Value vFun = fun->eval(state, env);
// Empirical arity of Nixpkgs lambdas by regex e.g. ([a-zA-Z]+:(\s|(/\*.*\/)|(#.*\n))*){5}
// 2: over 4000
// 3: about 300
// 4: about 60
// 5: under 10
// This excluded attrset lambdas (`{...}:`). Contributions of mixed lambdas appears insignificant at ~150
// total.
SmallValueVector<4> vArgs;
vArgs.reserve(args.size());
for (size_t i = 0; i < args.size(); ++i) {
vArgs.push_back(args[i]->maybeThunk(state, env));
}
return state.callFunction(vFun, vArgs, pos);
}
}
|