annotate win32-mingw/include/capnp/raw-schema.h @ 149:279b18cc7785

Update Win32 capnp builds to v0.6
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 23 May 2017 09:16:54 +0100
parents
children
rev   line source
cannam@149 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@149 2 // Licensed under the MIT License:
cannam@149 3 //
cannam@149 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@149 5 // of this software and associated documentation files (the "Software"), to deal
cannam@149 6 // in the Software without restriction, including without limitation the rights
cannam@149 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@149 8 // copies of the Software, and to permit persons to whom the Software is
cannam@149 9 // furnished to do so, subject to the following conditions:
cannam@149 10 //
cannam@149 11 // The above copyright notice and this permission notice shall be included in
cannam@149 12 // all copies or substantial portions of the Software.
cannam@149 13 //
cannam@149 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@149 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@149 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@149 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@149 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@149 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@149 20 // THE SOFTWARE.
cannam@149 21
cannam@149 22 #ifndef CAPNP_RAW_SCHEMA_H_
cannam@149 23 #define CAPNP_RAW_SCHEMA_H_
cannam@149 24
cannam@149 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@149 26 #pragma GCC system_header
cannam@149 27 #endif
cannam@149 28
cannam@149 29 #include "common.h" // for uint and friends
cannam@149 30
cannam@149 31 #if _MSC_VER
cannam@149 32 #include <atomic>
cannam@149 33 #endif
cannam@149 34
cannam@149 35 namespace capnp {
cannam@149 36 namespace _ { // private
cannam@149 37
cannam@149 38 struct RawSchema;
cannam@149 39
cannam@149 40 struct RawBrandedSchema {
cannam@149 41 // Represents a combination of a schema and bindings for its generic parameters.
cannam@149 42 //
cannam@149 43 // Note that while we generate one `RawSchema` per type, we generate a `RawBrandedSchema` for
cannam@149 44 // every _instance_ of a generic type -- or, at least, every instance that is actually used. For
cannam@149 45 // generated-code types, we use template magic to initialize these.
cannam@149 46
cannam@149 47 const RawSchema* generic;
cannam@149 48 // Generic type which we're branding.
cannam@149 49
cannam@149 50 struct Binding {
cannam@149 51 uint8_t which; // Numeric value of one of schema::Type::Which.
cannam@149 52
cannam@149 53 bool isImplicitParameter;
cannam@149 54 // For AnyPointer, true if it's an implicit method parameter.
cannam@149 55
cannam@149 56 uint16_t listDepth; // Number of times to wrap the base type in List().
cannam@149 57
cannam@149 58 uint16_t paramIndex;
cannam@149 59 // For AnyPointer. If it's a type parameter (scopeId is non-zero) or it's an implicit parameter
cannam@149 60 // (isImplicitParameter is true), then this is the parameter index. Otherwise this is a numeric
cannam@149 61 // value of one of schema::Type::AnyPointer::Unconstrained::Which.
cannam@149 62
cannam@149 63 union {
cannam@149 64 const RawBrandedSchema* schema; // for struct, enum, interface
cannam@149 65 uint64_t scopeId; // for AnyPointer, if it's a type parameter
cannam@149 66 };
cannam@149 67
cannam@149 68 Binding() = default;
cannam@149 69 inline constexpr Binding(uint8_t which, uint16_t listDepth, const RawBrandedSchema* schema)
cannam@149 70 : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(0),
cannam@149 71 schema(schema) {}
cannam@149 72 inline constexpr Binding(uint8_t which, uint16_t listDepth,
cannam@149 73 uint64_t scopeId, uint16_t paramIndex)
cannam@149 74 : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(paramIndex),
cannam@149 75 scopeId(scopeId) {}
cannam@149 76 inline constexpr Binding(uint8_t which, uint16_t listDepth, uint16_t implicitParamIndex)
cannam@149 77 : which(which), isImplicitParameter(true), listDepth(listDepth),
cannam@149 78 paramIndex(implicitParamIndex), scopeId(0) {}
cannam@149 79 };
cannam@149 80
cannam@149 81 struct Scope {
cannam@149 82 uint64_t typeId;
cannam@149 83 // Type ID whose parameters are being bound.
cannam@149 84
cannam@149 85 const Binding* bindings;
cannam@149 86 uint bindingCount;
cannam@149 87 // Bindings for those parameters.
cannam@149 88
cannam@149 89 bool isUnbound;
cannam@149 90 // This scope is unbound, in the sense of SchemaLoader::getUnbound().
cannam@149 91 };
cannam@149 92
cannam@149 93 const Scope* scopes;
cannam@149 94 // Array of enclosing scopes for which generic variables have been bound, sorted by type ID.
cannam@149 95
cannam@149 96 struct Dependency {
cannam@149 97 uint location;
cannam@149 98 const RawBrandedSchema* schema;
cannam@149 99 };
cannam@149 100
cannam@149 101 const Dependency* dependencies;
cannam@149 102 // Map of branded schemas for dependencies of this type, given our brand. Only dependencies that
cannam@149 103 // are branded are included in this map; if a dependency is missing, use its `defaultBrand`.
cannam@149 104
cannam@149 105 uint32_t scopeCount;
cannam@149 106 uint32_t dependencyCount;
cannam@149 107
cannam@149 108 enum class DepKind {
cannam@149 109 // Component of a Dependency::location. Specifies what sort of dependency this is.
cannam@149 110
cannam@149 111 INVALID,
cannam@149 112 // Mostly defined to ensure that zero is not a valid location.
cannam@149 113
cannam@149 114 FIELD,
cannam@149 115 // Binding needed for a field's type. The index is the field index (NOT ordinal!).
cannam@149 116
cannam@149 117 METHOD_PARAMS,
cannam@149 118 // Bindings needed for a method's params type. The index is the method number.
cannam@149 119
cannam@149 120 METHOD_RESULTS,
cannam@149 121 // Bindings needed for a method's results type. The index is the method ordinal.
cannam@149 122
cannam@149 123 SUPERCLASS,
cannam@149 124 // Bindings needed for a superclass type. The index is the superclass's index in the
cannam@149 125 // "extends" list.
cannam@149 126
cannam@149 127 CONST_TYPE
cannam@149 128 // Bindings needed for the type of a constant. The index is zero.
cannam@149 129 };
cannam@149 130
cannam@149 131 static inline uint makeDepLocation(DepKind kind, uint index) {
cannam@149 132 // Make a number representing the location of a particular dependency within its parent
cannam@149 133 // schema.
cannam@149 134
cannam@149 135 return (static_cast<uint>(kind) << 24) | index;
cannam@149 136 }
cannam@149 137
cannam@149 138 class Initializer {
cannam@149 139 public:
cannam@149 140 virtual void init(const RawBrandedSchema* generic) const = 0;
cannam@149 141 };
cannam@149 142
cannam@149 143 const Initializer* lazyInitializer;
cannam@149 144 // Lazy initializer, invoked by ensureInitialized().
cannam@149 145
cannam@149 146 inline void ensureInitialized() const {
cannam@149 147 // Lazy initialization support. Invoke to ensure that initialization has taken place. This
cannam@149 148 // is required in particular when traversing the dependency list. RawSchemas for compiled-in
cannam@149 149 // types are always initialized; only dynamically-loaded schemas may be lazy.
cannam@149 150
cannam@149 151 #if __GNUC__
cannam@149 152 const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
cannam@149 153 #elif _MSC_VER
cannam@149 154 const Initializer* i = *static_cast<Initializer const* const volatile*>(&lazyInitializer);
cannam@149 155 std::atomic_thread_fence(std::memory_order_acquire);
cannam@149 156 #else
cannam@149 157 #error "Platform not supported"
cannam@149 158 #endif
cannam@149 159 if (i != nullptr) i->init(this);
cannam@149 160 }
cannam@149 161
cannam@149 162 inline bool isUnbound() const;
cannam@149 163 // Checks if this schema is the result of calling SchemaLoader::getUnbound(), in which case
cannam@149 164 // binding lookups need to be handled specially.
cannam@149 165 };
cannam@149 166
cannam@149 167 struct RawSchema {
cannam@149 168 // The generated code defines a constant RawSchema for every compiled declaration.
cannam@149 169 //
cannam@149 170 // This is an internal structure which could change in the future.
cannam@149 171
cannam@149 172 uint64_t id;
cannam@149 173
cannam@149 174 const word* encodedNode;
cannam@149 175 // Encoded SchemaNode, readable via readMessageUnchecked<schema::Node>(encodedNode).
cannam@149 176
cannam@149 177 uint32_t encodedSize;
cannam@149 178 // Size of encodedNode, in words.
cannam@149 179
cannam@149 180 const RawSchema* const* dependencies;
cannam@149 181 // Pointers to other types on which this one depends, sorted by ID. The schemas in this table
cannam@149 182 // may be uninitialized -- you must call ensureInitialized() on the one you wish to use before
cannam@149 183 // using it.
cannam@149 184 //
cannam@149 185 // TODO(someday): Make this a hashtable.
cannam@149 186
cannam@149 187 const uint16_t* membersByName;
cannam@149 188 // Indexes of members sorted by name. Used to implement name lookup.
cannam@149 189 // TODO(someday): Make this a hashtable.
cannam@149 190
cannam@149 191 uint32_t dependencyCount;
cannam@149 192 uint32_t memberCount;
cannam@149 193 // Sizes of above tables.
cannam@149 194
cannam@149 195 const uint16_t* membersByDiscriminant;
cannam@149 196 // List of all member indexes ordered by discriminant value. Those which don't have a
cannam@149 197 // discriminant value are listed at the end, in order by ordinal.
cannam@149 198
cannam@149 199 const RawSchema* canCastTo;
cannam@149 200 // Points to the RawSchema of a compiled-in type to which it is safe to cast any DynamicValue
cannam@149 201 // with this schema. This is null for all compiled-in types; it is only set by SchemaLoader on
cannam@149 202 // dynamically-loaded types.
cannam@149 203
cannam@149 204 class Initializer {
cannam@149 205 public:
cannam@149 206 virtual void init(const RawSchema* schema) const = 0;
cannam@149 207 };
cannam@149 208
cannam@149 209 const Initializer* lazyInitializer;
cannam@149 210 // Lazy initializer, invoked by ensureInitialized().
cannam@149 211
cannam@149 212 inline void ensureInitialized() const {
cannam@149 213 // Lazy initialization support. Invoke to ensure that initialization has taken place. This
cannam@149 214 // is required in particular when traversing the dependency list. RawSchemas for compiled-in
cannam@149 215 // types are always initialized; only dynamically-loaded schemas may be lazy.
cannam@149 216
cannam@149 217 #if __GNUC__
cannam@149 218 const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
cannam@149 219 #elif _MSC_VER
cannam@149 220 const Initializer* i = *static_cast<Initializer const* const volatile*>(&lazyInitializer);
cannam@149 221 std::atomic_thread_fence(std::memory_order_acquire);
cannam@149 222 #else
cannam@149 223 #error "Platform not supported"
cannam@149 224 #endif
cannam@149 225 if (i != nullptr) i->init(this);
cannam@149 226 }
cannam@149 227
cannam@149 228 RawBrandedSchema defaultBrand;
cannam@149 229 // Specifies the brand to use for this schema if no generic parameters have been bound to
cannam@149 230 // anything. Generally, in the default brand, all generic parameters are treated as if they were
cannam@149 231 // bound to `AnyPointer`.
cannam@149 232 };
cannam@149 233
cannam@149 234 inline bool RawBrandedSchema::isUnbound() const {
cannam@149 235 // The unbound schema is the only one that has no scopes but is not the default schema.
cannam@149 236 return scopeCount == 0 && this != &generic->defaultBrand;
cannam@149 237 }
cannam@149 238
cannam@149 239 } // namespace _ (private)
cannam@149 240 } // namespace capnp
cannam@149 241
cannam@149 242 #endif // CAPNP_RAW_SCHEMA_H_