LLVM 23.0.0git
NamedValuesSchema.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the declarations for the NamedValuesSchema, a schema to
11/// represent an array of named nodes inside CAS.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CAS_NAMEDVALUESSCHEMA_H
16#define LLVM_CAS_NAMEDVALUESSCHEMA_H
17
22
23namespace llvm::cas {
24
26
27/// Represents an entry in NamedValuesSchema.
30
33
34 friend bool operator==(const NamedValuesEntry &LHS,
35 const NamedValuesEntry &RHS) {
36 return LHS.Ref == RHS.Ref && LHS.Name == RHS.Name;
37 }
38
39 /// Ordering the entries by name. Items should have unique names.
40 friend bool operator<(const NamedValuesEntry &LHS,
41 const NamedValuesEntry &RHS) {
42 return LHS.Name < RHS.Name;
43 }
44};
45
46/// A schema for representing an array of named nodes in a CAS. The name of the
47/// nodes are stored in the root node so child node can be loaded on demand
48/// based on name and the name for all nodes need to be unique.
49class LLVM_ABI NamedValuesSchema
50 : public RTTIExtends<NamedValuesSchema, NodeSchema> {
51 void anchor() override;
52
53public:
54 static char ID;
55
56 bool isRootNode(const ObjectProxy &Node) const final {
57 // NamedValuesSchema only has one node, thus root node.
58 return isNode(Node);
59 }
60
61 /// Check if a proxy represents a valid node.
62 bool isNode(const ObjectProxy &Node) const final;
63
64 /// Create a NamedValuesSchema.
65 static Expected<NamedValuesSchema> create(ObjectStore &CAS);
66
67 /// Load NamedValuesProxy from an ObjectRef.
69
70 /// Load NamedValuesProxy from an ObjectProxy.
72
73 /// Construct a \c NamedValuesSchema CAS object with the given entries.
75
76 /// A builder class for creating nodes in NamedValuesSchema.
77 class Builder {
78 public:
79 Builder(ObjectStore &CAS) : CAS(CAS) {}
80
81 /// Add an entry to the builder.
82 LLVM_ABI void add(StringRef Name, ObjectRef Ref);
83
84 /// Build the node from added entries.
86
87 private:
88 ObjectStore &CAS;
91 };
92
93private:
94 friend class NamedValuesProxy;
95
96 NamedValuesSchema(ObjectStore &CAS, Error &E);
97
98 /// Get the number of entries.
99 size_t getNumEntries(NamedValuesProxy Values) const;
100
101 /// Iterate over entries with a callback.
102 Error
103 forEachEntry(NamedValuesProxy Values,
104 function_ref<Error(const NamedValuesEntry &)> Callback) const;
105
106 /// Lookup an entry by name.
107 std::optional<size_t> lookupEntry(NamedValuesProxy Values,
108 StringRef Name) const;
109
110 /// Load an entry by index.
111 NamedValuesEntry loadEntry(NamedValuesProxy Values, size_t I) const;
112
113 /// Name for the schema.
114 static constexpr StringLiteral SchemaName =
115 "llvm::cas::schema::namedvalues::v1";
116 std::optional<ObjectRef> NamedValuesKindRef;
117};
118
119/// A proxy for a loaded CAS Object in NamedValuesSchema.
120class NamedValuesProxy : public ObjectProxy {
121public:
122 /// Get the schema associated with this proxy.
123 const NamedValuesSchema &getSchema() const { return *Schema; }
124
125 /// Iterate over entries with a callback.
126 Error
128 return Schema->forEachEntry(*this, Callback);
129 }
130
131 /// Check if the object is empty.
132 bool empty() const { return size() == 0; }
133
134 /// Get the number of entries in the CAS object.
135 size_t size() const { return Schema->getNumEntries(*this); }
136
137 /// Lookup an entry by name.
138 std::optional<NamedValuesEntry> lookup(StringRef Name) const {
139 if (auto I = Schema->lookupEntry(*this, Name))
140 return get(*I);
141 return std::nullopt;
142 }
143
144 /// Get the name of an entry by index.
145 LLVM_ABI StringRef getName(size_t I) const;
146
147 /// Get an entry by index.
148 NamedValuesEntry get(size_t I) const { return Schema->loadEntry(*this, I); }
149
150private:
152 : ObjectProxy(Node), Schema(&Schema) {}
153
154 friend class NamedValuesSchema;
155 const NamedValuesSchema *Schema;
156};
157
158} // namespace llvm::cas
159
160#endif
AMDGPU Mark last scratch load
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declaration of the ObjectStore class.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Inheritance utility for extensible RTTI.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:864
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
A proxy for a loaded CAS Object in NamedValuesSchema.
bool empty() const
Check if the object is empty.
Error forEachEntry(function_ref< Error(const NamedValuesEntry &)> Callback) const
Iterate over entries with a callback.
NamedValuesEntry get(size_t I) const
Get an entry by index.
const NamedValuesSchema & getSchema() const
Get the schema associated with this proxy.
std::optional< NamedValuesEntry > lookup(StringRef Name) const
Lookup an entry by name.
LLVM_ABI StringRef getName(size_t I) const
Get the name of an entry by index.
size_t size() const
Get the number of entries in the CAS object.
A schema for representing an array of named nodes in a CAS.
bool isRootNode(const ObjectProxy &Node) const final
bool isNode(const ObjectProxy &Node) const final
Check if a proxy represents a valid node.
Reference to an abstract hierarchical node, with data and references.
Reference to an object in an ObjectStore instance.
Content-addressable storage for objects.
Definition ObjectStore.h:90
An efficient, type-erasing, non-owning reference to a callable.
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
Represents an entry in NamedValuesSchema.
NamedValuesEntry(StringRef Name, ObjectRef Ref)
friend bool operator==(const NamedValuesEntry &LHS, const NamedValuesEntry &RHS)
friend bool operator<(const NamedValuesEntry &LHS, const NamedValuesEntry &RHS)
Ordering the entries by name. Items should have unique names.