about summary refs log tree commit diff
path: root/third_party/nix/src/libexpr/attr-set.hh
blob: 55641a020325ea2654a2b994258010834dd8bb10 (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
// This file implements the underlying structure of Nix attribute sets.
#pragma once

#include <cstddef>

#include <absl/container/btree_map.h>
#include <gc/gc_allocator.h>

#include "libexpr/nixexpr.hh"
#include "libexpr/symbol-table.hh"
#include "libutil/types.hh"

namespace nix {  // TODO(tazjin): ::expr

class EvalState;
struct Value;

/* Map one attribute name to its value. */
struct Attr {
  Symbol name;
  Value* value;  // TODO(tazjin): Who owns this?
  Pos* pos;      // TODO(tazjin): Who owns this?
  Attr(Symbol name, Value* value, Pos* pos = &noPos)
      : name(name), value(value), pos(pos){};
};

// Convenience alias for the backing map, with the garbage-collecting
// allocator explicitly specified.
using AttributeMap = absl::btree_map<Symbol, Attr, std::less<Symbol>,
                                     gc_allocator<std::pair<Symbol, Attr>>>;

using AttributeVector =
    std::vector<std::pair<Symbol, Attr>, gc_allocator<std::pair<Symbol, Attr>>>;

class BindingsIterator : public std::iterator<std::forward_iterator_tag,
                                              std::pair<const Symbol, Attr>> {
  friend class Bindings;
  friend class BTreeBindings;
  friend class VectorBindings;

 public:
  BindingsIterator() : _iterator(){};
  BindingsIterator& operator++();
  BindingsIterator operator++(int);
  bool operator==(const BindingsIterator& other) const;
  bool operator!=(const BindingsIterator& other) const;
  reference operator*() const;
  pointer operator->() const { return &operator*(); }

  BindingsIterator& operator=(const BindingsIterator& other) {
    _iterator = other._iterator;
    return *this;
  }

 protected:
  explicit BindingsIterator(AttributeMap::iterator&& iterator)
      : _iterator(iterator){};

  explicit BindingsIterator(AttributeVector::iterator&& iterator)
      : _iterator(iterator){};

 private:
  std::variant<AttributeMap::iterator, AttributeVector::iterator> _iterator;
};

class Bindings {
 public:
  typedef BindingsIterator iterator;

  // Allocate a new attribute set with a static capacity that is visible to the
  // garbage collector.
  static Bindings* NewGC(size_t capacity = 0);

  // Return the number of contained elements.
  virtual size_t size() = 0;

  // Is this attribute set empty?
  virtual bool empty() = 0;

  // Insert, but do not replace, values in the attribute set.
  virtual void push_back(const Attr& attr) = 0;

  // Insert a value, or replace an existing one.
  virtual void insert_or_assign(Attr& attr) = 0;

  // Look up a specific element of the attribute set.
  virtual iterator find(const Symbol& name) = 0;

  // TODO
  virtual iterator begin() = 0;
  virtual iterator end() = 0;

  // Merge values from other into this attribute set.
  virtual void merge(Bindings& other) = 0;

  // TODO: can callers just iterate?
  [[deprecated]] virtual std::vector<const Attr*> lexicographicOrder() = 0;

  // oh no
  friend class EvalState;
};

}  // namespace nix