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