mirror of
https://github.com/NixOS/nix
synced 2024-09-20 23:28:26 -04:00
a385e51a08
after #6218 `Symbol` no longer confers a uniqueness invariant on the string it wraps, it is now possible to create multiple symbols that compare equal but whose string contents have different addresses. this guarantee is now only provided by `SymbolIdx`, leaving `Symbol` only as a string wrapper that knows about the intricacies of how symbols need to be formatted for output. this change renames `SymbolIdx` to `Symbol` to restore the previous semantics of `Symbol` to that name. we also keep the wrapper type and rename it to `SymbolStr` instead of returning plain strings from lookups into the symbol table because symbols are formatted for output in many places. theoretically we do not need `SymbolStr`, only a function that formats a string for output as a symbol, but having to wrap every symbol that appears in a message into eg `formatSymbol()` is error-prone and inconvient.
124 lines
2.6 KiB
C++
124 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include "sync.hh"
|
|
#include "hash.hh"
|
|
#include "eval.hh"
|
|
|
|
#include <functional>
|
|
#include <variant>
|
|
|
|
namespace nix::eval_cache {
|
|
|
|
MakeError(CachedEvalError, EvalError);
|
|
|
|
struct AttrDb;
|
|
class AttrCursor;
|
|
|
|
class EvalCache : public std::enable_shared_from_this<EvalCache>
|
|
{
|
|
friend class AttrCursor;
|
|
|
|
std::shared_ptr<AttrDb> db;
|
|
EvalState & state;
|
|
typedef std::function<Value *()> RootLoader;
|
|
RootLoader rootLoader;
|
|
RootValue value;
|
|
|
|
Value * getRootValue();
|
|
|
|
public:
|
|
|
|
EvalCache(
|
|
std::optional<std::reference_wrapper<const Hash>> useCache,
|
|
EvalState & state,
|
|
RootLoader rootLoader);
|
|
|
|
ref<AttrCursor> getRoot();
|
|
};
|
|
|
|
enum AttrType {
|
|
Placeholder = 0,
|
|
FullAttrs = 1,
|
|
String = 2,
|
|
Missing = 3,
|
|
Misc = 4,
|
|
Failed = 5,
|
|
Bool = 6,
|
|
};
|
|
|
|
struct placeholder_t {};
|
|
struct missing_t {};
|
|
struct misc_t {};
|
|
struct failed_t {};
|
|
typedef uint64_t AttrId;
|
|
typedef std::pair<AttrId, std::string> AttrKey;
|
|
typedef std::pair<std::string, NixStringContext> string_t;
|
|
|
|
typedef std::variant<
|
|
std::vector<Symbol>,
|
|
string_t,
|
|
placeholder_t,
|
|
missing_t,
|
|
misc_t,
|
|
failed_t,
|
|
bool
|
|
> AttrValue;
|
|
|
|
class AttrCursor : public std::enable_shared_from_this<AttrCursor>
|
|
{
|
|
friend class EvalCache;
|
|
|
|
ref<EvalCache> root;
|
|
typedef std::optional<std::pair<std::shared_ptr<AttrCursor>, Symbol>> Parent;
|
|
Parent parent;
|
|
RootValue _value;
|
|
std::optional<std::pair<AttrId, AttrValue>> cachedValue;
|
|
|
|
AttrKey getKey();
|
|
|
|
Value & getValue();
|
|
|
|
public:
|
|
|
|
AttrCursor(
|
|
ref<EvalCache> root,
|
|
Parent parent,
|
|
Value * value = nullptr,
|
|
std::optional<std::pair<AttrId, AttrValue>> && cachedValue = {});
|
|
|
|
std::vector<Symbol> getAttrPath() const;
|
|
|
|
std::vector<Symbol> getAttrPath(Symbol name) const;
|
|
|
|
std::string getAttrPathStr() const;
|
|
|
|
std::string getAttrPathStr(Symbol name) const;
|
|
|
|
Suggestions getSuggestionsForAttr(Symbol name);
|
|
|
|
std::shared_ptr<AttrCursor> maybeGetAttr(std::string_view name, bool forceErrors = false);
|
|
|
|
ref<AttrCursor> getAttr(std::string_view name, bool forceErrors = false);
|
|
|
|
/* Get an attribute along a chain of attrsets. Note that this does
|
|
not auto-call functors or functions. */
|
|
OrSuggestions<ref<AttrCursor>> findAlongAttrPath(const std::vector<Symbol> & attrPath, bool force = false);
|
|
|
|
std::string getString();
|
|
|
|
string_t getStringWithContext();
|
|
|
|
bool getBool();
|
|
|
|
std::vector<Symbol> getAttrs();
|
|
|
|
bool isDerivation();
|
|
|
|
Value & forceValue();
|
|
|
|
/* Force creation of the .drv file in the Nix store. */
|
|
StorePath forceDerivation();
|
|
};
|
|
|
|
}
|