1
+ // ==-- PropertySetIO.h -- models a sequence of property sets and their I/O -==//
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
+ // Models a sequence of property sets and their input and output operations.
9
+ // TODO use Yaml as I/O engine.
10
+ // PropertyValue set format:
11
+ // '['<PropertyValue set name>']'
12
+ // <property name>=<property type>'|'<property value>
13
+ // <property name>=<property type>'|'<property value>
14
+ // ...
15
+ // '['<PropertyValue set name>']'
16
+ // <property name>=<property type>'|'<property value>
17
+ // where
18
+ // <PropertyValue set name>, <property name> are strings
19
+ // <property type> - string representation of the property type
20
+ // <property value> - string representation of the property value.
21
+ //
22
+ // For example:
23
+ // [Staff/Ages]
24
+ // person1=1|20
25
+ // person2=1|25
26
+ // [Staff/Experience]
27
+ // person1=1|1
28
+ // person2=1|2
29
+ // person3=1|12
30
+ //
31
+ // ===----------------------------------------------------------------------===//
32
+
33
+ #ifndef LLVM_SUPPORT_PROPERTYSETIO_H
34
+ #define LLVM_SUPPORT_PROPERTYSETIO_H
35
+
36
+ #include " llvm/ADT/MapVector.h"
37
+ #include " llvm/ADT/StringRef.h"
38
+ #include " llvm/Support/Error.h"
39
+ #include " llvm/Support/MemoryBuffer.h"
40
+ #include " llvm/Support/raw_ostream.h"
41
+
42
+ #include < istream>
43
+ #include < map>
44
+ #include < memory>
45
+ #include < string>
46
+
47
+ namespace llvm {
48
+ namespace util {
49
+
50
+ // Represents a property value. PropertyValue name is stored in the encompassing
51
+ // container.
52
+ class PropertyValue {
53
+ public:
54
+ // Defines supported property types
55
+ enum Type { first = 0 , NONE = first, UINT32, last = UINT32 };
56
+
57
+ // Translates C++ type to the corresponding type tag.
58
+ template <typename T> static Type getTypeTag ();
59
+
60
+ // Casts from int value to a type tag.
61
+ static Expected<Type> getTypeTag (int T) {
62
+ if (T < first || T > last)
63
+ return createStringError (std::error_code (), " bad property type " + T);
64
+ return static_cast <Type>(T);
65
+ }
66
+
67
+ PropertyValue () = default ;
68
+ PropertyValue (Type T) : Ty(T) {}
69
+
70
+ PropertyValue (uint32_t Val) : Ty(UINT32), Val({Val}) {}
71
+ PropertyValue (const PropertyValue &P) = default ;
72
+ PropertyValue (PropertyValue &&P) = default ;
73
+
74
+ PropertyValue &operator =(PropertyValue &&P) = default ;
75
+
76
+ PropertyValue &operator =(const PropertyValue &P) = default ;
77
+
78
+ // get property value as unsigned 32-bit integer
79
+ uint32_t asUint32 () const {
80
+ assert (Ty == UINT32);
81
+ return Val.UInt32Val ;
82
+ }
83
+
84
+ bool isValid () const { return getType () != NONE; }
85
+
86
+ // set property value; the 'T' type must be convertible to a property type tag
87
+ template <typename T> void set (T V) {
88
+ assert (getTypeTag<T>() == Ty);
89
+ getValueRef<T>() = V;
90
+ }
91
+
92
+ Type getType () const { return Ty; }
93
+
94
+ size_t size () const {
95
+ switch (Ty) {
96
+ case UINT32:
97
+ return sizeof (Val.UInt32Val );
98
+ default :
99
+ llvm_unreachable_internal (" unsupported property type" );
100
+ }
101
+ }
102
+
103
+ private:
104
+ template <typename T> T &getValueRef ();
105
+
106
+ Type Ty = NONE;
107
+ union {
108
+ uint32_t UInt32Val;
109
+ } Val;
110
+ };
111
+
112
+ std::ostream &operator <<(std::ostream &Out, const PropertyValue &V);
113
+
114
+ // A property set. Preserves insertion order when iterating elements.
115
+ using PropertySet = MapVector<StringRef, PropertyValue>;
116
+
117
+ // A "registry" of multiple property sets. Maps a property set name to its
118
+ // contents. Can be read/written.
119
+ class PropertySetRegistry {
120
+ public:
121
+ using MapTy = MapVector<StringRef, PropertySet>;
122
+
123
+ // Specific property category names used by tools.
124
+ static constexpr char SYCL_SPECIALIZATION_CONSTANTS[] =
125
+ " SYCL/specialization constants" ;
126
+
127
+ // Function for bulk addition of an entire property set under given category
128
+ // (property set name).
129
+ template <typename T>
130
+ void add (StringRef Category, const std::map<StringRef, T> &Props) {
131
+ assert (PropSetMap.find (Category) == PropSetMap.end () &&
132
+ " category already added" );
133
+ auto &PropSet = PropSetMap[Category];
134
+
135
+ for (const auto &Prop : Props)
136
+ PropSet.insert (std::make_pair (Prop.first , PropertyValue (Prop.second )));
137
+ }
138
+
139
+ // Parses and creates a property set registry.
140
+ static Expected<std::unique_ptr<PropertySetRegistry>>
141
+ read (const MemoryBuffer *Buf);
142
+
143
+ // Dumps a property set registry to a stream.
144
+ void write (raw_ostream &Out) const ;
145
+
146
+ // Start iterator of all preperty sets in the registry.
147
+ MapTy::const_iterator begin () const { return PropSetMap.begin (); }
148
+ // End iterator of all preperty sets in the registry.
149
+ MapTy::const_iterator end () const { return PropSetMap.end (); }
150
+
151
+ // Retrieves a property set with given name.
152
+ PropertySet &operator [](StringRef Name) { return PropSetMap[Name]; }
153
+ // Constant access to the underlying map.
154
+ const MapTy &getPropSets () const { return PropSetMap; }
155
+
156
+ private:
157
+ MapTy PropSetMap;
158
+ };
159
+
160
+ } // namespace util
161
+ } // namespace llvm
162
+
163
+ #endif // #define LLVM_SUPPORT_PROPERTYSETIO_H
0 commit comments