nixd
Loading...
Searching...
No Matches
Serialize.h
Go to the documentation of this file.
1/// \file
2/// \brief Serialize nix::Expr to bytes & deserialize from bytes.
3
4#pragma once
5
6#include "nixt/ArrayRef.h"
7#include "nixt/PtrPool.h"
8
9#include <nixbc/Type.h>
10
11#include <nix/nixexpr.hh>
12
13namespace nixt {
14
15//===----------------------------------------------------------------------===//
16// Shared type definitions & constants
17//===----------------------------------------------------------------------===//
18
19enum class EncodeKind : uint32_t {
20#define NIX_EXPR(EXPR) EXPR,
21#include "Nodes.inc"
22#undef NIX_EXPR
23
24 // Special discriminator for nix::AttrName.
25 // struct AttrName
26 // {
27 // Symbol symbol;
28 // Expr * expr;
29 // AttrName(Symbol s) : symbol(s) {};
30 // AttrName(Expr * e) : expr(e) {};
31 // };
33};
34
35/// \brief Header of serialized AST.
36struct ASTHeader {
37 char Magic[8];
38 uint32_t Version;
39};
40
41//===----------------------------------------------------------------------===//
42// Encoder
43//===----------------------------------------------------------------------===//
44
45/// \brief Basic primitives. Trivial data types are just written to a stream.
46/// \returns The beginning offset of the data in the stream.
47template <class T>
48 requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
49std::size_t encode(std::ostream &OS, const T &Data) {
50 std::size_t Ret = OS.tellp();
51 OS.write(reinterpret_cast<const char *>(&Data), sizeof(Data));
52 return Ret;
53}
54
55/// \brief Encode string to bytes.
56std::size_t encode(std::ostream &OS, const std::string &Data);
57
58/// \brief Encode string to bytes.
59std::size_t encode(std::ostream &OS, const nix::Pos::Origin &Origin);
60
61/// \brief Encode an AST. \p E is the root of the AST.
62void encodeAST(std::ostream &OS, const nix::SymbolTable &STable,
63 const nix::PosTable &PTable, const nix::Pos::Origin &Origin,
64 const nix::Expr *E);
65
66//===----------------------------------------------------------------------===//
67// Decoder
68//===----------------------------------------------------------------------===//
69
70/// \brief Basic primitives. Deocde from bytes by `memcpy`.
71/// \returns Size of bytes consumed.
72template <class T>
73 requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
74std::size_t decode(BytesRef Data, T &Obj) {
75 assert(lengthof(Data) >= sizeof(T));
76 std::memcpy(&Obj, begin(Data), sizeof(T));
77 return sizeof(T);
78}
79
80/// \brief Decode string from bytes.
81std::size_t decode(BytesRef Data, std::string &Str);
82
83/// \brief Consume bytes from \p Data and construct an object of type \p T.
84template <class T> T consume(BytesRef &Data) {
85 T Obj;
86 Data = advance(Data, decode(Data, Obj));
87 return Obj;
88}
89
90nix::Expr *consumeAST(BytesRef &Data, PtrPool<nix::Expr> &Pool,
91 nix::PosTable &PTable, nix::SymbolTable &STable);
92
93} // namespace nixt
ArrayRef, BytesRef, and related functions.
Pointer pool, for RAII memory management.
Access EvalCache in nix::EvalState.
Definition ArrayRef.h:7
std::size_t encode(std::ostream &OS, const T &Data)
Basic primitives. Trivial data types are just written to a stream.
Definition Serialize.h:49
const T * begin(ArrayRef< T > B)
Iterator begin. Used for range-based-for
Definition ArrayRef.h:19
std::size_t decode(BytesRef Data, T &Obj)
Basic primitives. Deocde from bytes by memcpy.
Definition Serialize.h:74
EncodeKind
Definition Serialize.h:19
ArrayRef< T > advance(ArrayRef< T > B, long Offset)
Advance the beginning pointer of bytes array.
Definition ArrayRef.h:27
void encodeAST(std::ostream &OS, const nix::SymbolTable &STable, const nix::PosTable &PTable, const nix::Pos::Origin &Origin, const nix::Expr *E)
Encode an AST. E is the root of the AST.
std::size_t lengthof(ArrayRef< T > B)
Get length of this array.
Definition ArrayRef.h:31
T consume(BytesRef &Data)
Consume bytes from Data and construct an object of type T.
Definition Serialize.h:84
nix::Expr * consumeAST(BytesRef &Data, PtrPool< nix::Expr > &Pool, nix::PosTable &PTable, nix::SymbolTable &STable)
Header of serialized AST.
Definition Serialize.h:36
uint32_t Version
Definition Serialize.h:38
char Magic[8]
Definition Serialize.h:37
Weak reference to an array, with begin and end pointers.
Definition ArrayRef.h:11
A simple pointer pool, a vector of unique_ptrs.
Definition PtrPool.h:22