1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-20 23:28:26 -04:00
nix/src/libexpr/eval-cache.hh

128 lines
2.7 KiB
C++
Raw Normal View History

#pragma once
#include "sync.hh"
2020-04-20 07:13:52 -04:00
#include "hash.hh"
#include "eval.hh"
#include <functional>
2020-04-20 07:13:52 -04:00
#include <variant>
2020-04-20 07:13:52 -04:00
namespace nix::eval_cache {
MakeError(CachedEvalError, EvalError);
2020-10-06 08:00:42 -04:00
struct AttrDb;
2020-04-20 07:13:52 -04:00
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,
2020-04-20 07:13:52 -04:00
EvalState & state,
RootLoader rootLoader);
ref<AttrCursor> getRoot();
2020-04-20 07:13:52 -04:00
};
enum AttrType {
Placeholder = 0,
FullAttrs = 1,
String = 2,
Missing = 3,
Misc = 4,
Failed = 5,
Bool = 6,
2020-04-20 07:13:52 -04:00
};
struct placeholder_t {};
struct missing_t {};
struct misc_t {};
struct failed_t {};
typedef uint64_t AttrId;
typedef std::pair<AttrId, Symbol> AttrKey;
typedef std::pair<std::string, NixStringContext> string_t;
2020-06-29 13:08:37 -04:00
typedef std::variant<
std::vector<Symbol>,
string_t,
placeholder_t,
missing_t,
misc_t,
failed_t,
bool
> AttrValue;
2020-04-20 07:13:52 -04:00
class AttrCursor : public std::enable_shared_from_this<AttrCursor>
{
2020-04-20 07:13:52 -04:00
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;
2020-04-20 07:13:52 -04:00
AttrKey getKey();
2020-04-20 07:13:52 -04:00
Value & getValue();
public:
2020-04-20 07:13:52 -04:00
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(Symbol name, bool forceErrors = false);
2020-04-20 07:13:52 -04:00
std::shared_ptr<AttrCursor> maybeGetAttr(std::string_view name);
ref<AttrCursor> getAttr(Symbol name, bool forceErrors = false);
ref<AttrCursor> getAttr(std::string_view name);
2020-04-20 07:13:52 -04:00
/* 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);
2020-04-20 07:13:52 -04:00
std::string getString();
2020-06-29 13:08:37 -04:00
string_t getStringWithContext();
bool getBool();
2020-04-20 07:13:52 -04:00
std::vector<Symbol> getAttrs();
2020-04-20 07:13:52 -04:00
bool isDerivation();
2020-04-20 07:13:52 -04:00
Value & forceValue();
/* Force creation of the .drv file in the Nix store. */
StorePath forceDerivation();
};
}