cannam@148: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@148: // Licensed under the MIT License: cannam@148: // cannam@148: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@148: // of this software and associated documentation files (the "Software"), to deal cannam@148: // in the Software without restriction, including without limitation the rights cannam@148: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@148: // copies of the Software, and to permit persons to whom the Software is cannam@148: // furnished to do so, subject to the following conditions: cannam@148: // cannam@148: // The above copyright notice and this permission notice shall be included in cannam@148: // all copies or substantial portions of the Software. cannam@148: // cannam@148: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@148: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@148: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@148: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@148: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@148: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@148: // THE SOFTWARE. cannam@148: cannam@148: #ifndef CAPNP_RAW_SCHEMA_H_ cannam@148: #define CAPNP_RAW_SCHEMA_H_ cannam@148: cannam@148: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@148: #pragma GCC system_header cannam@148: #endif cannam@148: cannam@148: #include "common.h" // for uint and friends cannam@148: cannam@148: #if _MSC_VER cannam@148: #include cannam@148: #endif cannam@148: cannam@148: namespace capnp { cannam@148: namespace _ { // private cannam@148: cannam@148: struct RawSchema; cannam@148: cannam@148: struct RawBrandedSchema { cannam@148: // Represents a combination of a schema and bindings for its generic parameters. cannam@148: // cannam@148: // Note that while we generate one `RawSchema` per type, we generate a `RawBrandedSchema` for cannam@148: // every _instance_ of a generic type -- or, at least, every instance that is actually used. For cannam@148: // generated-code types, we use template magic to initialize these. cannam@148: cannam@148: const RawSchema* generic; cannam@148: // Generic type which we're branding. cannam@148: cannam@148: struct Binding { cannam@148: uint8_t which; // Numeric value of one of schema::Type::Which. cannam@148: cannam@148: bool isImplicitParameter; cannam@148: // For AnyPointer, true if it's an implicit method parameter. cannam@148: cannam@148: uint16_t listDepth; // Number of times to wrap the base type in List(). cannam@148: cannam@148: uint16_t paramIndex; cannam@148: // For AnyPointer. If it's a type parameter (scopeId is non-zero) or it's an implicit parameter cannam@148: // (isImplicitParameter is true), then this is the parameter index. Otherwise this is a numeric cannam@148: // value of one of schema::Type::AnyPointer::Unconstrained::Which. cannam@148: cannam@148: union { cannam@148: const RawBrandedSchema* schema; // for struct, enum, interface cannam@148: uint64_t scopeId; // for AnyPointer, if it's a type parameter cannam@148: }; cannam@148: cannam@148: Binding() = default; cannam@148: inline constexpr Binding(uint8_t which, uint16_t listDepth, const RawBrandedSchema* schema) cannam@148: : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(0), cannam@148: schema(schema) {} cannam@148: inline constexpr Binding(uint8_t which, uint16_t listDepth, cannam@148: uint64_t scopeId, uint16_t paramIndex) cannam@148: : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(paramIndex), cannam@148: scopeId(scopeId) {} cannam@148: inline constexpr Binding(uint8_t which, uint16_t listDepth, uint16_t implicitParamIndex) cannam@148: : which(which), isImplicitParameter(true), listDepth(listDepth), cannam@148: paramIndex(implicitParamIndex), scopeId(0) {} cannam@148: }; cannam@148: cannam@148: struct Scope { cannam@148: uint64_t typeId; cannam@148: // Type ID whose parameters are being bound. cannam@148: cannam@148: const Binding* bindings; cannam@148: uint bindingCount; cannam@148: // Bindings for those parameters. cannam@148: cannam@148: bool isUnbound; cannam@148: // This scope is unbound, in the sense of SchemaLoader::getUnbound(). cannam@148: }; cannam@148: cannam@148: const Scope* scopes; cannam@148: // Array of enclosing scopes for which generic variables have been bound, sorted by type ID. cannam@148: cannam@148: struct Dependency { cannam@148: uint location; cannam@148: const RawBrandedSchema* schema; cannam@148: }; cannam@148: cannam@148: const Dependency* dependencies; cannam@148: // Map of branded schemas for dependencies of this type, given our brand. Only dependencies that cannam@148: // are branded are included in this map; if a dependency is missing, use its `defaultBrand`. cannam@148: cannam@148: uint32_t scopeCount; cannam@148: uint32_t dependencyCount; cannam@148: cannam@148: enum class DepKind { cannam@148: // Component of a Dependency::location. Specifies what sort of dependency this is. cannam@148: cannam@148: INVALID, cannam@148: // Mostly defined to ensure that zero is not a valid location. cannam@148: cannam@148: FIELD, cannam@148: // Binding needed for a field's type. The index is the field index (NOT ordinal!). cannam@148: cannam@148: METHOD_PARAMS, cannam@148: // Bindings needed for a method's params type. The index is the method number. cannam@148: cannam@148: METHOD_RESULTS, cannam@148: // Bindings needed for a method's results type. The index is the method ordinal. cannam@148: cannam@148: SUPERCLASS, cannam@148: // Bindings needed for a superclass type. The index is the superclass's index in the cannam@148: // "extends" list. cannam@148: cannam@148: CONST_TYPE cannam@148: // Bindings needed for the type of a constant. The index is zero. cannam@148: }; cannam@148: cannam@148: static inline uint makeDepLocation(DepKind kind, uint index) { cannam@148: // Make a number representing the location of a particular dependency within its parent cannam@148: // schema. cannam@148: cannam@148: return (static_cast(kind) << 24) | index; cannam@148: } cannam@148: cannam@148: class Initializer { cannam@148: public: cannam@148: virtual void init(const RawBrandedSchema* generic) const = 0; cannam@148: }; cannam@148: cannam@148: const Initializer* lazyInitializer; cannam@148: // Lazy initializer, invoked by ensureInitialized(). cannam@148: cannam@148: inline void ensureInitialized() const { cannam@148: // Lazy initialization support. Invoke to ensure that initialization has taken place. This cannam@148: // is required in particular when traversing the dependency list. RawSchemas for compiled-in cannam@148: // types are always initialized; only dynamically-loaded schemas may be lazy. cannam@148: cannam@148: #if __GNUC__ cannam@148: const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE); cannam@148: #elif _MSC_VER cannam@148: const Initializer* i = *static_cast(&lazyInitializer); cannam@148: std::atomic_thread_fence(std::memory_order_acquire); cannam@148: #else cannam@148: #error "Platform not supported" cannam@148: #endif cannam@148: if (i != nullptr) i->init(this); cannam@148: } cannam@148: cannam@148: inline bool isUnbound() const; cannam@148: // Checks if this schema is the result of calling SchemaLoader::getUnbound(), in which case cannam@148: // binding lookups need to be handled specially. cannam@148: }; cannam@148: cannam@148: struct RawSchema { cannam@148: // The generated code defines a constant RawSchema for every compiled declaration. cannam@148: // cannam@148: // This is an internal structure which could change in the future. cannam@148: cannam@148: uint64_t id; cannam@148: cannam@148: const word* encodedNode; cannam@148: // Encoded SchemaNode, readable via readMessageUnchecked(encodedNode). cannam@148: cannam@148: uint32_t encodedSize; cannam@148: // Size of encodedNode, in words. cannam@148: cannam@148: const RawSchema* const* dependencies; cannam@148: // Pointers to other types on which this one depends, sorted by ID. The schemas in this table cannam@148: // may be uninitialized -- you must call ensureInitialized() on the one you wish to use before cannam@148: // using it. cannam@148: // cannam@148: // TODO(someday): Make this a hashtable. cannam@148: cannam@148: const uint16_t* membersByName; cannam@148: // Indexes of members sorted by name. Used to implement name lookup. cannam@148: // TODO(someday): Make this a hashtable. cannam@148: cannam@148: uint32_t dependencyCount; cannam@148: uint32_t memberCount; cannam@148: // Sizes of above tables. cannam@148: cannam@148: const uint16_t* membersByDiscriminant; cannam@148: // List of all member indexes ordered by discriminant value. Those which don't have a cannam@148: // discriminant value are listed at the end, in order by ordinal. cannam@148: cannam@148: const RawSchema* canCastTo; cannam@148: // Points to the RawSchema of a compiled-in type to which it is safe to cast any DynamicValue cannam@148: // with this schema. This is null for all compiled-in types; it is only set by SchemaLoader on cannam@148: // dynamically-loaded types. cannam@148: cannam@148: class Initializer { cannam@148: public: cannam@148: virtual void init(const RawSchema* schema) const = 0; cannam@148: }; cannam@148: cannam@148: const Initializer* lazyInitializer; cannam@148: // Lazy initializer, invoked by ensureInitialized(). cannam@148: cannam@148: inline void ensureInitialized() const { cannam@148: // Lazy initialization support. Invoke to ensure that initialization has taken place. This cannam@148: // is required in particular when traversing the dependency list. RawSchemas for compiled-in cannam@148: // types are always initialized; only dynamically-loaded schemas may be lazy. cannam@148: cannam@148: #if __GNUC__ cannam@148: const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE); cannam@148: #elif _MSC_VER cannam@148: const Initializer* i = *static_cast(&lazyInitializer); cannam@148: std::atomic_thread_fence(std::memory_order_acquire); cannam@148: #else cannam@148: #error "Platform not supported" cannam@148: #endif cannam@148: if (i != nullptr) i->init(this); cannam@148: } cannam@148: cannam@148: RawBrandedSchema defaultBrand; cannam@148: // Specifies the brand to use for this schema if no generic parameters have been bound to cannam@148: // anything. Generally, in the default brand, all generic parameters are treated as if they were cannam@148: // bound to `AnyPointer`. cannam@148: }; cannam@148: cannam@148: inline bool RawBrandedSchema::isUnbound() const { cannam@148: // The unbound schema is the only one that has no scopes but is not the default schema. cannam@148: return scopeCount == 0 && this != &generic->defaultBrand; cannam@148: } cannam@148: cannam@148: } // namespace _ (private) cannam@148: } // namespace capnp cannam@148: cannam@148: #endif // CAPNP_RAW_SCHEMA_H_