comparison win32-mingw/include/capnp/generated-header-support.h @ 50:37d53a7e8262

Headers for KJ/Capnp Win32
author Chris Cannam
date Wed, 26 Oct 2016 13:18:45 +0100
parents
children eccd51b72864
comparison
equal deleted inserted replaced
49:3ab5a40c4e3b 50:37d53a7e8262
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 // This file is included form all generated headers.
23
24 #ifndef CAPNP_GENERATED_HEADER_SUPPORT_H_
25 #define CAPNP_GENERATED_HEADER_SUPPORT_H_
26
27 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
28 #pragma GCC system_header
29 #endif
30
31 #include "layout.h"
32 #include "list.h"
33 #include "orphan.h"
34 #include "pointer-helpers.h"
35 #include "any.h"
36 #include <kj/string.h>
37 #include <kj/string-tree.h>
38
39 namespace capnp {
40
41 class MessageBuilder; // So that it can be declared a friend.
42
43 template <typename T, Kind k = CAPNP_KIND(T)>
44 struct ToDynamic_; // Defined in dynamic.h, needs to be declared as everyone's friend.
45
46 struct DynamicStruct; // So that it can be declared a friend.
47
48 struct Capability; // To declare brandBindingFor<Capability>()
49
50 namespace _ { // private
51
52 #if !CAPNP_LITE
53
54 struct RawSchema;
55
56 struct RawBrandedSchema {
57 // Represents a combination of a schema and bindings for its generic parameters.
58 //
59 // Note that while we generate one `RawSchema` per type, we generate a `RawBrandedSchema` for
60 // every _instance_ of a generic type -- or, at least, every instance that is actually used. For
61 // generated-code types, we use template magic to initialize these.
62
63 const RawSchema* generic;
64 // Generic type which we're branding.
65
66 struct Binding {
67 uint8_t which; // Numeric value of one of schema::Type::Which.
68
69 bool isImplicitParameter;
70 // For AnyPointer, true if it's an implicit method parameter.
71
72 uint16_t listDepth; // Number of times to wrap the base type in List().
73
74 uint16_t paramIndex;
75 // For AnyPointer. If it's a type parameter (scopeId is non-zero) or it's an implicit parameter
76 // (isImplicitParameter is true), then this is the parameter index. Otherwise this is a numeric
77 // value of one of schema::Type::AnyPointer::Unconstrained::Which.
78
79 union {
80 const RawBrandedSchema* schema; // for struct, enum, interface
81 uint64_t scopeId; // for AnyPointer, if it's a type parameter
82 };
83
84 Binding() = default;
85 inline constexpr Binding(uint8_t which, uint16_t listDepth, const RawBrandedSchema* schema)
86 : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(0),
87 schema(schema) {}
88 inline constexpr Binding(uint8_t which, uint16_t listDepth,
89 uint64_t scopeId, uint16_t paramIndex)
90 : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(paramIndex),
91 scopeId(scopeId) {}
92 inline constexpr Binding(uint8_t which, uint16_t listDepth, uint16_t implicitParamIndex)
93 : which(which), isImplicitParameter(true), listDepth(listDepth),
94 paramIndex(implicitParamIndex), scopeId(0) {}
95 };
96
97 struct Scope {
98 uint64_t typeId;
99 // Type ID whose parameters are being bound.
100
101 const Binding* bindings;
102 uint bindingCount;
103 // Bindings for those parameters.
104
105 bool isUnbound;
106 // This scope is unbound, in the sense of SchemaLoader::getUnbound().
107 };
108
109 const Scope* scopes;
110 // Array of enclosing scopes for which generic variables have been bound, sorted by type ID.
111
112 struct Dependency {
113 uint location;
114 const RawBrandedSchema* schema;
115 };
116
117 const Dependency* dependencies;
118 // Map of branded schemas for dependencies of this type, given our brand. Only dependencies that
119 // are branded are included in this map; if a dependency is missing, use its `defaultBrand`.
120
121 uint32_t scopeCount;
122 uint32_t dependencyCount;
123
124 enum class DepKind {
125 // Component of a Dependency::location. Specifies what sort of dependency this is.
126
127 INVALID,
128 // Mostly defined to ensure that zero is not a valid location.
129
130 FIELD,
131 // Binding needed for a field's type. The index is the field index (NOT ordinal!).
132
133 METHOD_PARAMS,
134 // Bindings needed for a method's params type. The index is the method number.
135
136 METHOD_RESULTS,
137 // Bindings needed for a method's results type. The index is the method ordinal.
138
139 SUPERCLASS,
140 // Bindings needed for a superclass type. The index is the superclass's index in the
141 // "extends" list.
142
143 CONST_TYPE
144 // Bindings needed for the type of a constant. The index is zero.
145 };
146
147 static inline uint makeDepLocation(DepKind kind, uint index) {
148 // Make a number representing the location of a particular dependency within its parent
149 // schema.
150
151 return (static_cast<uint>(kind) << 24) | index;
152 }
153
154 class Initializer {
155 public:
156 virtual void init(const RawBrandedSchema* generic) const = 0;
157 };
158
159 const Initializer* lazyInitializer;
160 // Lazy initializer, invoked by ensureInitialized().
161
162 inline void ensureInitialized() const {
163 // Lazy initialization support. Invoke to ensure that initialization has taken place. This
164 // is required in particular when traversing the dependency list. RawSchemas for compiled-in
165 // types are always initialized; only dynamically-loaded schemas may be lazy.
166
167 const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
168 if (i != nullptr) i->init(this);
169 }
170
171 inline bool isUnbound() const;
172 // Checks if this schema is the result of calling SchemaLoader::getUnbound(), in which case
173 // binding lookups need to be handled specially.
174 };
175
176 struct RawSchema {
177 // The generated code defines a constant RawSchema for every compiled declaration.
178 //
179 // This is an internal structure which could change in the future.
180
181 uint64_t id;
182
183 const word* encodedNode;
184 // Encoded SchemaNode, readable via readMessageUnchecked<schema::Node>(encodedNode).
185
186 uint32_t encodedSize;
187 // Size of encodedNode, in words.
188
189 const RawSchema* const* dependencies;
190 // Pointers to other types on which this one depends, sorted by ID. The schemas in this table
191 // may be uninitialized -- you must call ensureInitialized() on the one you wish to use before
192 // using it.
193 //
194 // TODO(someday): Make this a hashtable.
195
196 const uint16_t* membersByName;
197 // Indexes of members sorted by name. Used to implement name lookup.
198 // TODO(someday): Make this a hashtable.
199
200 uint32_t dependencyCount;
201 uint32_t memberCount;
202 // Sizes of above tables.
203
204 const uint16_t* membersByDiscriminant;
205 // List of all member indexes ordered by discriminant value. Those which don't have a
206 // discriminant value are listed at the end, in order by ordinal.
207
208 const RawSchema* canCastTo;
209 // Points to the RawSchema of a compiled-in type to which it is safe to cast any DynamicValue
210 // with this schema. This is null for all compiled-in types; it is only set by SchemaLoader on
211 // dynamically-loaded types.
212
213 class Initializer {
214 public:
215 virtual void init(const RawSchema* schema) const = 0;
216 };
217
218 const Initializer* lazyInitializer;
219 // Lazy initializer, invoked by ensureInitialized().
220
221 inline void ensureInitialized() const {
222 // Lazy initialization support. Invoke to ensure that initialization has taken place. This
223 // is required in particular when traversing the dependency list. RawSchemas for compiled-in
224 // types are always initialized; only dynamically-loaded schemas may be lazy.
225
226 const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
227 if (i != nullptr) i->init(this);
228 }
229
230 RawBrandedSchema defaultBrand;
231 // Specifies the brand to use for this schema if no generic parameters have been bound to
232 // anything. Generally, in the default brand, all generic parameters are treated as if they were
233 // bound to `AnyPointer`.
234 };
235
236 inline bool RawBrandedSchema::isUnbound() const {
237 // The unbound schema is the only one that has no scopes but is not the default schema.
238 return scopeCount == 0 && this != &generic->defaultBrand;
239 }
240
241 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate, bool = false>
242 inline const RawSchema& rawSchema() {
243 return *CapnpPrivate::schema;
244 }
245 template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
246 inline const RawSchema& rawSchema() {
247 return *schemas::EnumInfo<T>::schema;
248 }
249
250 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
251 inline const RawBrandedSchema& rawBrandedSchema() {
252 return *CapnpPrivate::brand;
253 }
254 template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
255 inline const RawBrandedSchema& rawBrandedSchema() {
256 return schemas::EnumInfo<T>::schema->defaultBrand;
257 }
258
259 template <typename TypeTag, typename... Params>
260 struct ChooseBrand;
261 // If all of `Params` are `AnyPointer`, return the type's default brand. Otherwise, return a
262 // specific brand instance. TypeTag is the _capnpPrivate struct for the type in question.
263
264 template <typename TypeTag>
265 struct ChooseBrand<TypeTag> {
266 // All params were AnyPointer. No specific brand needed.
267 static constexpr _::RawBrandedSchema const* brand = &TypeTag::schema->defaultBrand;
268 };
269
270 template <typename TypeTag, typename... Rest>
271 struct ChooseBrand<TypeTag, AnyPointer, Rest...>: public ChooseBrand<TypeTag, Rest...> {};
272 // The first parameter is AnyPointer, so recurse to check the rest.
273
274 template <typename TypeTag, typename First, typename... Rest>
275 struct ChooseBrand<TypeTag, First, Rest...> {
276 // At least one parameter is not AnyPointer, so use the specificBrand constant.
277 static constexpr _::RawBrandedSchema const* brand = &TypeTag::specificBrand;
278 };
279
280 template <typename T, Kind k = kind<T>()>
281 struct BrandBindingFor_;
282
283 #define HANDLE_TYPE(Type, which) \
284 template <> \
285 struct BrandBindingFor_<Type, Kind::PRIMITIVE> { \
286 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { \
287 return { which, listDepth, nullptr }; \
288 } \
289 }
290 HANDLE_TYPE(Void, 0);
291 HANDLE_TYPE(bool, 1);
292 HANDLE_TYPE(int8_t, 2);
293 HANDLE_TYPE(int16_t, 3);
294 HANDLE_TYPE(int32_t, 4);
295 HANDLE_TYPE(int64_t, 5);
296 HANDLE_TYPE(uint8_t, 6);
297 HANDLE_TYPE(uint16_t, 7);
298 HANDLE_TYPE(uint32_t, 8);
299 HANDLE_TYPE(uint64_t, 9);
300 HANDLE_TYPE(float, 10);
301 HANDLE_TYPE(double, 11);
302 #undef HANDLE_TYPE
303
304 template <>
305 struct BrandBindingFor_<Text, Kind::BLOB> {
306 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
307 return { 12, listDepth, nullptr };
308 }
309 };
310
311 template <>
312 struct BrandBindingFor_<Data, Kind::BLOB> {
313 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
314 return { 13, listDepth, nullptr };
315 }
316 };
317
318 template <typename T>
319 struct BrandBindingFor_<List<T>, Kind::LIST> {
320 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
321 return BrandBindingFor_<T>::get(listDepth + 1);
322 }
323 };
324
325 template <typename T>
326 struct BrandBindingFor_<T, Kind::ENUM> {
327 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
328 return { 15, listDepth, nullptr };
329 }
330 };
331
332 template <typename T>
333 struct BrandBindingFor_<T, Kind::STRUCT> {
334 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
335 return { 16, listDepth, T::_capnpPrivate::brand };
336 }
337 };
338
339 template <typename T>
340 struct BrandBindingFor_<T, Kind::INTERFACE> {
341 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
342 return { 17, listDepth, T::_capnpPrivate::brand };
343 }
344 };
345
346 template <>
347 struct BrandBindingFor_<AnyPointer, Kind::OTHER> {
348 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
349 return { 18, listDepth, 0, 0 };
350 }
351 };
352
353 template <>
354 struct BrandBindingFor_<AnyStruct, Kind::OTHER> {
355 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
356 return { 18, listDepth, 0, 1 };
357 }
358 };
359
360 template <>
361 struct BrandBindingFor_<AnyList, Kind::OTHER> {
362 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
363 return { 18, listDepth, 0, 2 };
364 }
365 };
366
367 template <>
368 struct BrandBindingFor_<Capability, Kind::OTHER> {
369 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
370 return { 18, listDepth, 0, 3 };
371 }
372 };
373
374 template <typename T>
375 constexpr RawBrandedSchema::Binding brandBindingFor() {
376 return BrandBindingFor_<T>::get(0);
377 }
378
379 kj::StringTree structString(StructReader reader, const RawBrandedSchema& schema);
380 kj::String enumString(uint16_t value, const RawBrandedSchema& schema);
381 // Declared here so that we can declare inline stringify methods on generated types.
382 // Defined in stringify.c++, which depends on dynamic.c++, which is allowed not to be linked in.
383
384 template <typename T>
385 inline kj::StringTree structString(StructReader reader) {
386 return structString(reader, rawBrandedSchema<T>());
387 }
388 template <typename T>
389 inline kj::String enumString(T value) {
390 return enumString(static_cast<uint16_t>(value), rawBrandedSchema<T>());
391 }
392
393 #endif // !CAPNP_LITE
394
395 // TODO(cleanup): Unify ConstStruct and ConstList.
396 template <typename T>
397 class ConstStruct {
398 public:
399 ConstStruct() = delete;
400 KJ_DISALLOW_COPY(ConstStruct);
401 inline explicit constexpr ConstStruct(const word* ptr): ptr(ptr) {}
402
403 inline typename T::Reader get() const {
404 return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<T>();
405 }
406
407 inline operator typename T::Reader() const { return get(); }
408 inline typename T::Reader operator*() const { return get(); }
409 inline TemporaryPointer<typename T::Reader> operator->() const { return get(); }
410
411 private:
412 const word* ptr;
413 };
414
415 template <typename T>
416 class ConstList {
417 public:
418 ConstList() = delete;
419 KJ_DISALLOW_COPY(ConstList);
420 inline explicit constexpr ConstList(const word* ptr): ptr(ptr) {}
421
422 inline typename List<T>::Reader get() const {
423 return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<List<T>>();
424 }
425
426 inline operator typename List<T>::Reader() const { return get(); }
427 inline typename List<T>::Reader operator*() const { return get(); }
428 inline TemporaryPointer<typename List<T>::Reader> operator->() const { return get(); }
429
430 private:
431 const word* ptr;
432 };
433
434 template <size_t size>
435 class ConstText {
436 public:
437 ConstText() = delete;
438 KJ_DISALLOW_COPY(ConstText);
439 inline explicit constexpr ConstText(const word* ptr): ptr(ptr) {}
440
441 inline Text::Reader get() const {
442 return Text::Reader(reinterpret_cast<const char*>(ptr), size);
443 }
444
445 inline operator Text::Reader() const { return get(); }
446 inline Text::Reader operator*() const { return get(); }
447 inline TemporaryPointer<Text::Reader> operator->() const { return get(); }
448
449 inline kj::StringPtr toString() const {
450 return get();
451 }
452
453 private:
454 const word* ptr;
455 };
456
457 template <size_t size>
458 inline kj::StringPtr KJ_STRINGIFY(const ConstText<size>& s) {
459 return s.get();
460 }
461
462 template <size_t size>
463 class ConstData {
464 public:
465 ConstData() = delete;
466 KJ_DISALLOW_COPY(ConstData);
467 inline explicit constexpr ConstData(const word* ptr): ptr(ptr) {}
468
469 inline Data::Reader get() const {
470 return Data::Reader(reinterpret_cast<const byte*>(ptr), size);
471 }
472
473 inline operator Data::Reader() const { return get(); }
474 inline Data::Reader operator*() const { return get(); }
475 inline TemporaryPointer<Data::Reader> operator->() const { return get(); }
476
477 private:
478 const word* ptr;
479 };
480
481 template <size_t size>
482 inline auto KJ_STRINGIFY(const ConstData<size>& s) -> decltype(kj::toCharSequence(s.get())) {
483 return kj::toCharSequence(s.get());
484 }
485
486 } // namespace _ (private)
487
488 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
489 inline constexpr uint64_t typeId() { return CapnpPrivate::typeId; }
490 template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
491 inline constexpr uint64_t typeId() { return id; }
492 // typeId<MyType>() returns the type ID as defined in the schema. Works with structs, enums, and
493 // interfaces.
494
495 template <typename T>
496 inline constexpr uint sizeInWords() {
497 // Return the size, in words, of a Struct type, if allocated free-standing (not in a list).
498 // May be useful for pre-computing space needed in order to precisely allocate messages.
499
500 return (WordCount32(_::structSize<T>().data) +
501 _::structSize<T>().pointers * WORDS_PER_POINTER) / WORDS;
502 }
503
504 } // namespace capnp
505
506 #if _MSC_VER
507 // MSVC doesn't understand floating-point constexpr yet.
508 //
509 // TODO(msvc): Remove this hack when MSVC is fixed.
510 #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value)
511 #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) = value
512 #else
513 #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) = value
514 #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value)
515 #endif
516
517 #if CAPNP_LITE
518
519 #define CAPNP_DECLARE_SCHEMA(id) \
520 extern ::capnp::word const* const bp_##id
521
522 #define CAPNP_DECLARE_ENUM(type, id) \
523 inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
524 return ::kj::str(static_cast<uint16_t>(value)); \
525 } \
526 template <> struct EnumInfo<type##_##id> { \
527 struct IsEnum; \
528 static constexpr uint64_t typeId = 0x##id; \
529 static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
530 }
531
532 #if _MSC_VER
533 // TODO(msvc): MSVC dosen't expect constexprs to have definitions.
534 #define CAPNP_DEFINE_ENUM(type, id)
535 #else
536 #define CAPNP_DEFINE_ENUM(type, id) \
537 constexpr uint64_t EnumInfo<type>::typeId
538 #endif
539
540 #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
541 struct IsStruct; \
542 static constexpr uint64_t typeId = 0x##id; \
543 static constexpr uint16_t dataWordSize = dataWordSize_; \
544 static constexpr uint16_t pointerCount = pointerCount_; \
545 static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; }
546
547 #else // CAPNP_LITE
548
549 #define CAPNP_DECLARE_SCHEMA(id) \
550 extern ::capnp::word const* const bp_##id; \
551 extern const ::capnp::_::RawSchema s_##id
552
553 #define CAPNP_DECLARE_ENUM(type, id) \
554 inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
555 return ::capnp::_::enumString(value); \
556 } \
557 template <> struct EnumInfo<type##_##id> { \
558 struct IsEnum; \
559 static constexpr uint64_t typeId = 0x##id; \
560 static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
561 static constexpr ::capnp::_::RawSchema const* schema = &s_##id; \
562 }
563 #define CAPNP_DEFINE_ENUM(type, id) \
564 constexpr uint64_t EnumInfo<type>::typeId; \
565 constexpr ::capnp::_::RawSchema const* EnumInfo<type>::schema
566
567 #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
568 struct IsStruct; \
569 static constexpr uint64_t typeId = 0x##id; \
570 static constexpr ::capnp::Kind kind = ::capnp::Kind::STRUCT; \
571 static constexpr uint16_t dataWordSize = dataWordSize_; \
572 static constexpr uint16_t pointerCount = pointerCount_; \
573 static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
574 static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
575
576 #define CAPNP_DECLARE_INTERFACE_HEADER(id) \
577 struct IsInterface; \
578 static constexpr uint64_t typeId = 0x##id; \
579 static constexpr ::capnp::Kind kind = ::capnp::Kind::INTERFACE; \
580 static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
581 static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
582
583 #endif // CAPNP_LITE, else
584
585 #endif // CAPNP_GENERATED_HEADER_SUPPORT_H_