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