about summary refs log tree commit diff
path: root/third_party/nix/src/libexpr/nixexpr.hh
blob: 22b5f871f0ad915c4b5a454a5f701c182305f0ac (plain) (blame)
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
#pragma once

#include <map>
#include <optional>
#include <variant>

#include <absl/container/flat_hash_map.h>

#include "libexpr/symbol-table.hh"
#include "libexpr/value.hh"
#include "libutil/types.hh"  // TODO(tazjin): audit this include

namespace nix {

MakeError(EvalError, Error);
MakeError(ParseError, Error);
MakeError(AssertionError, EvalError);
MakeError(ThrownError, AssertionError);
MakeError(Abort, EvalError);
MakeError(TypeError, EvalError);
MakeError(UndefinedVarError, Error);
MakeError(RestrictedPathError, Error);

/* Position objects. */

struct Pos {
  std::optional<Symbol> file;
  unsigned int line, column;
  Pos(const std::optional<Symbol>& file, unsigned int line, unsigned int column)
      : file(file), line(line), column(column){};

  // TODO(tazjin): remove this - empty pos is never useful
  Pos() : file(std::nullopt), line(0), column(0){};

  operator bool() const { return line != 0; }

  bool operator<(const Pos& p2) const {
    if (!file.has_value()) {
      return true;
    }

    if (!line) {
      return p2.line;
    }
    if (!p2.line) {
      return false;
    }
    int d = ((std::string)file.value()).compare((std::string)p2.file.value());
    if (d < 0) {
      return true;
    }
    if (d > 0) {
      return false;
    }
    if (line < p2.line) {
      return true;
    }
    if (line > p2.line) {
      return false;
    }
    return column < p2.column;
  }
};

extern Pos noPos;

std::ostream& operator<<(std::ostream& str, const Pos& pos);

struct Env;
struct Value;
class EvalState;
struct StaticEnv;

/* An attribute path is a sequence of attribute names. */
using AttrName = std::variant<Symbol, Expr*>;
using AttrPath = std::vector<AttrName>;
using AttrNameVector = std::vector<AttrName>;

using VectorExprs = std::vector<nix::Expr*>;

std::string showAttrPath(const AttrPath& attrPath);

/* Abstract syntax of Nix expressions. */

struct Expr {
  virtual ~Expr(){};
  virtual void show(std::ostream& str) const;
  virtual void bindVars(const StaticEnv& env);
  virtual void eval(EvalState& state, Env& env, Value& v);
  virtual Value* maybeThunk(EvalState& state, Env& env);
};

std::ostream& operator<<(std::ostream& str, const Expr& e);

#define COMMON_METHODS                             \
  void show(std::ostream& str) const;              \
  void eval(EvalState& state, Env& env, Value& v); \
  void bindVars(const StaticEnv& env);

struct ExprInt : Expr {
  NixInt n;
  Value v;
  ExprInt(NixInt n) : n(n) { mkInt(v, n); };
  COMMON_METHODS
  Value* maybeThunk(EvalState& state, Env& env);
};

struct ExprFloat : Expr {
  NixFloat nf;
  Value v;
  ExprFloat(NixFloat nf) : nf(nf) { mkFloat(v, nf); };
  COMMON_METHODS
  Value* maybeThunk(EvalState& state, Env& env);
};

struct ExprString : Expr {
  Symbol s;
  Value v;
  ExprString(const Symbol& s) : s(s) { mkString(v, s); };
  COMMON_METHODS
  Value* maybeThunk(EvalState& state, Env& env);
};

/* Temporary class used during parsing of indented strings. */
struct ExprIndStr : Expr {
  std::string s;
  ExprIndStr(const std::string& s) : s(s){};
};

struct ExprPath : Expr {
  std::string s;
  Value v;
  ExprPath(const std::string& s) : s(s) { mkPathNoCopy(v, this->s.c_str()); };
  COMMON_METHODS
  Value* maybeThunk(EvalState& state, Env& env);
};

struct ExprVar : Expr {
  Pos pos;
  Symbol name;

  /* Whether the variable comes from an environment (e.g. a rec, let
     or function argument) or from a "with". */
  bool fromWith;

  /* In the former case, the value is obtained by going `level'
     levels up from the current environment and getting the
     `displ'th value in that environment.  In the latter case, the
     value is obtained by getting the attribute named `name' from
     the set stored in the environment that is `level' levels up
     from the current one.*/
  unsigned int level;
  unsigned int displ;

  ExprVar(const Symbol& name) : name(name){};
  ExprVar(const Pos& pos, const Symbol& name) : pos(pos), name(name){};
  COMMON_METHODS
  Value* maybeThunk(EvalState& state, Env& env);
};

// [tazjin] I *think* that this struct describes the syntactic
// construct for "selecting" something out of an attribute set, e.g.
// `a.b.c` => ExprSelect{"b", "c"}.
//
// Each path element has got a pointer to an expression, which seems
// to be the thing preceding its period, but afaict that is only set
// for the first one in a path.
struct ExprSelect : Expr {
  Pos pos;
  Expr *e, *def;
  AttrPath attrPath;
  ExprSelect(const Pos& pos, Expr* e, const AttrPath& attrPath, Expr* def)
      : pos(pos), e(e), def(def), attrPath(attrPath){};
  ExprSelect(const Pos& pos, Expr* e, const Symbol& name)
      : pos(pos), e(e), def(0) {
    attrPath.push_back(AttrName(name));
  };
  COMMON_METHODS
};

struct ExprOpHasAttr : Expr {
  Expr* e;
  AttrPath attrPath;
  ExprOpHasAttr(Expr* e, const AttrPath& attrPath) : e(e), attrPath(attrPath){};
  COMMON_METHODS
};

struct ExprAttrs : Expr {
  bool recursive;

  struct AttrDef {
    bool inherited;
    Expr* e;
    Pos pos;
    unsigned int displ;  // displacement
    AttrDef(Expr* e, const Pos& pos, bool inherited = false)
        : inherited(inherited), e(e), pos(pos), displ(0){};
    AttrDef(){};
  };

  using AttrDefs = absl::flat_hash_map<Symbol, AttrDef>;
  AttrDefs attrs;

  struct DynamicAttrDef {
    Expr *nameExpr, *valueExpr;
    Pos pos;
    DynamicAttrDef(Expr* nameExpr, Expr* valueExpr, const Pos& pos)
        : nameExpr(nameExpr), valueExpr(valueExpr), pos(pos){};
  };

  using DynamicAttrDefs = std::vector<DynamicAttrDef>;
  DynamicAttrDefs dynamicAttrs;

  ExprAttrs() : recursive(false){};
  COMMON_METHODS
};

struct ExprList : Expr {
  VectorExprs elems;
  ExprList(){};
  COMMON_METHODS
};

struct Formal {
  Symbol name;
  Expr* def;  // def = default, not definition
  Formal(const Symbol& name, Expr* def) : name(name), def(def){};
};

// Describes structured function arguments (e.g. `{ a }: ...`)
struct Formals {
  using Formals_ = std::list<Formal>;
  Formals_ formals;
  std::set<Symbol> argNames;  // used during parsing
  bool ellipsis;
};

struct ExprLambda : Expr {
 public:
  Pos pos;
  std::optional<Symbol> name;
  Symbol arg;
  bool matchAttrs;
  Formals* formals;
  Expr* body;
  ExprLambda(const Pos& pos, const Symbol& arg, bool matchAttrs,
             Formals* formals, Expr* body)
      : pos(pos),
        arg(arg),
        matchAttrs(matchAttrs),
        formals(formals),
        body(body) {
    if (!arg.empty() && formals &&
        formals->argNames.find(arg) != formals->argNames.end()) {
      throw ParseError(
          format("duplicate formal function argument '%1%' at %2%") % arg %
          pos);
    }
  };
  void setName(Symbol& name);
  std::string showNamePos() const;
  COMMON_METHODS
};

struct ExprLet : Expr {
  ExprAttrs* attrs;
  Expr* body;
  ExprLet(ExprAttrs* attrs, Expr* body) : attrs(attrs), body(body){};
  COMMON_METHODS
};

struct ExprWith : Expr {
  Pos pos;
  Expr *attrs, *body;
  size_t prevWith;
  ExprWith(const Pos& pos, Expr* attrs, Expr* body)
      : pos(pos), attrs(attrs), body(body){};
  COMMON_METHODS
};

struct ExprIf : Expr {
  Expr *cond, *then, *else_;
  ExprIf(Expr* cond, Expr* then, Expr* else_)
      : cond(cond), then(then), else_(else_){};
  COMMON_METHODS
};

struct ExprAssert : Expr {
  Pos pos;
  Expr *cond, *body;
  ExprAssert(const Pos& pos, Expr* cond, Expr* body)
      : pos(pos), cond(cond), body(body){};
  COMMON_METHODS
};

struct ExprOpNot : Expr {
  Expr* e;
  ExprOpNot(Expr* e) : e(e){};
  COMMON_METHODS
};

#define MakeBinOp(name, s)                                                 \
  struct name : Expr {                                                     \
    Pos pos;                                                               \
    Expr *e1, *e2;                                                         \
    name(Expr* e1, Expr* e2) : e1(e1), e2(e2){};                           \
    name(const Pos& pos, Expr* e1, Expr* e2) : pos(pos), e1(e1), e2(e2){}; \
    void show(std::ostream& str) const {                                   \
      str << "(" << *e1 << " " s " " << *e2 << ")";                        \
    }                                                                      \
    void bindVars(const StaticEnv& env) {                                  \
      e1->bindVars(env);                                                   \
      e2->bindVars(env);                                                   \
    }                                                                      \
    void eval(EvalState& state, Env& env, Value& v);                       \
  };

MakeBinOp(ExprApp, "");
MakeBinOp(ExprOpEq, "==");
MakeBinOp(ExprOpNEq, "!=");
MakeBinOp(ExprOpAnd, "&&");
MakeBinOp(ExprOpOr, "||");
MakeBinOp(ExprOpImpl, "->");
MakeBinOp(ExprOpUpdate, "//");
MakeBinOp(ExprOpConcatLists, "++");

struct ExprConcatStrings : Expr {
  Pos pos;
  bool forceString;
  nix::VectorExprs* es;
  ExprConcatStrings(const Pos& pos, bool forceString, nix::VectorExprs* es)
      : pos(pos), forceString(forceString), es(es){};
  COMMON_METHODS
};

struct ExprPos : Expr {
  Pos pos;
  ExprPos(const Pos& pos) : pos(pos){};
  COMMON_METHODS
};

/* Static environments are used to map variable names onto (level,
   displacement) pairs used to obtain the value of the variable at
   runtime. */
struct StaticEnv {
  bool isWith;
  const StaticEnv* up;
  typedef absl::flat_hash_map<Symbol, unsigned int> Vars;
  Vars vars;
  StaticEnv(bool isWith, const StaticEnv* up) : isWith(isWith), up(up){};
};

}  // namespace nix