Chris@50: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@50: // Licensed under the MIT License: Chris@50: // Chris@50: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@50: // of this software and associated documentation files (the "Software"), to deal Chris@50: // in the Software without restriction, including without limitation the rights Chris@50: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@50: // copies of the Software, and to permit persons to whom the Software is Chris@50: // furnished to do so, subject to the following conditions: Chris@50: // Chris@50: // The above copyright notice and this permission notice shall be included in Chris@50: // all copies or substantial portions of the Software. Chris@50: // Chris@50: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@50: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@50: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@50: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@50: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@50: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@50: // THE SOFTWARE. Chris@50: Chris@50: #ifndef CAPNP_SCHEMA_H_ Chris@50: #define CAPNP_SCHEMA_H_ Chris@50: Chris@50: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@50: #pragma GCC system_header Chris@50: #endif Chris@50: Chris@50: #if CAPNP_LITE Chris@50: #error "Reflection APIs, including this header, are not available in lite mode." Chris@50: #endif Chris@50: Chris@50: #include Chris@50: Chris@50: namespace capnp { Chris@50: Chris@50: class Schema; Chris@50: class StructSchema; Chris@50: class EnumSchema; Chris@50: class InterfaceSchema; Chris@50: class ConstSchema; Chris@50: class ListSchema; Chris@50: class Type; Chris@50: Chris@50: template ()> struct SchemaType_ { typedef Schema Type; }; Chris@50: template struct SchemaType_ { typedef schema::Type::Which Type; }; Chris@50: template struct SchemaType_ { typedef schema::Type::Which Type; }; Chris@50: template struct SchemaType_ { typedef EnumSchema Type; }; Chris@50: template struct SchemaType_ { typedef StructSchema Type; }; Chris@50: template struct SchemaType_ { typedef InterfaceSchema Type; }; Chris@50: template struct SchemaType_ { typedef ListSchema Type; }; Chris@50: Chris@50: template Chris@50: using SchemaType = typename SchemaType_::Type; Chris@50: // SchemaType is the type of T's schema, e.g. StructSchema if T is a struct. Chris@50: Chris@50: namespace _ { // private Chris@50: extern const RawSchema NULL_SCHEMA; Chris@50: extern const RawSchema NULL_STRUCT_SCHEMA; Chris@50: extern const RawSchema NULL_ENUM_SCHEMA; Chris@50: extern const RawSchema NULL_INTERFACE_SCHEMA; Chris@50: extern const RawSchema NULL_CONST_SCHEMA; Chris@50: // The schema types default to these null (empty) schemas in case of error, especially when Chris@50: // exceptions are disabled. Chris@50: } // namespace _ (private) Chris@50: Chris@50: class Schema { Chris@50: // Convenience wrapper around capnp::schema::Node. Chris@50: Chris@50: public: Chris@50: inline Schema(): raw(&_::NULL_SCHEMA.defaultBrand) {} Chris@50: Chris@50: template Chris@50: static inline SchemaType from() { return SchemaType::template fromImpl(); } Chris@50: // Get the Schema for a particular compiled-in type. Chris@50: Chris@50: schema::Node::Reader getProto() const; Chris@50: // Get the underlying Cap'n Proto representation of the schema node. (Note that this accessor Chris@50: // has performance comparable to accessors of struct-typed fields on Reader classes.) Chris@50: Chris@50: kj::ArrayPtr asUncheckedMessage() const; Chris@50: // Get the encoded schema node content as a single message segment. It is safe to read as an Chris@50: // unchecked message. Chris@50: Chris@50: Schema getDependency(uint64_t id) const KJ_DEPRECATED("Does not handle generics correctly."); Chris@50: // DEPRECATED: This method cannot correctly account for generic type parameter bindings that Chris@50: // may apply to the dependency. Instead of using this method, use a method of the Schema API Chris@50: // that corresponds to the exact kind of dependency. For example, to get a field type, use Chris@50: // StructSchema::Field::getType(). Chris@50: // Chris@50: // Gets the Schema for one of this Schema's dependencies. For example, if this Schema is for a Chris@50: // struct, you could look up the schema for one of its fields' types. Throws an exception if this Chris@50: // schema doesn't actually depend on the given id. Chris@50: // Chris@50: // Note that not all type IDs found in the schema node are considered "dependencies" -- only the Chris@50: // ones that are needed to implement the dynamic API are. That includes: Chris@50: // - Field types. Chris@50: // - Group types. Chris@50: // - scopeId for group nodes, but NOT otherwise. Chris@50: // - Method parameter and return types. Chris@50: // Chris@50: // The following are NOT considered dependencies: Chris@50: // - Nested nodes. Chris@50: // - scopeId for a non-group node. Chris@50: // - Annotations. Chris@50: // Chris@50: // To obtain schemas for those, you would need a SchemaLoader. Chris@50: Chris@50: bool isBranded() const; Chris@50: // Returns true if this schema represents a non-default parameterization of this type. Chris@50: Chris@50: Schema getGeneric() const; Chris@50: // Get the version of this schema with any brands removed. Chris@50: Chris@50: class BrandArgumentList; Chris@50: BrandArgumentList getBrandArgumentsAtScope(uint64_t scopeId) const; Chris@50: // Gets the values bound to the brand parameters at the given scope. Chris@50: Chris@50: StructSchema asStruct() const; Chris@50: EnumSchema asEnum() const; Chris@50: InterfaceSchema asInterface() const; Chris@50: ConstSchema asConst() const; Chris@50: // Cast the Schema to a specific type. Throws an exception if the type doesn't match. Use Chris@50: // getProto() to determine type, e.g. getProto().isStruct(). Chris@50: Chris@50: inline bool operator==(const Schema& other) const { return raw == other.raw; } Chris@50: inline bool operator!=(const Schema& other) const { return raw != other.raw; } Chris@50: // Determine whether two Schemas are wrapping the exact same underlying data, by identity. If Chris@50: // you want to check if two Schemas represent the same type (but possibly different versions of Chris@50: // it), compare their IDs instead. Chris@50: Chris@50: template Chris@50: void requireUsableAs() const; Chris@50: // Throws an exception if a value with this Schema cannot safely be cast to a native value of Chris@50: // the given type. This passes if either: Chris@50: // - *this == from() Chris@50: // - This schema was loaded with SchemaLoader, the type ID matches typeId(), and Chris@50: // loadCompiledTypeAndDependencies() was called on the SchemaLoader. Chris@50: Chris@50: kj::StringPtr getShortDisplayName() const; Chris@50: // Get the short version of the node's display name. Chris@50: Chris@50: private: Chris@50: const _::RawBrandedSchema* raw; Chris@50: Chris@50: inline explicit Schema(const _::RawBrandedSchema* raw): raw(raw) { Chris@50: KJ_IREQUIRE(raw->lazyInitializer == nullptr, Chris@50: "Must call ensureInitialized() on RawSchema before constructing Schema."); Chris@50: } Chris@50: Chris@50: template static inline Schema fromImpl() { Chris@50: return Schema(&_::rawSchema()); Chris@50: } Chris@50: Chris@50: void requireUsableAs(const _::RawSchema* expected) const; Chris@50: Chris@50: uint32_t getSchemaOffset(const schema::Value::Reader& value) const; Chris@50: Chris@50: Type getBrandBinding(uint64_t scopeId, uint index) const; Chris@50: // Look up the binding for a brand parameter used by this Schema. Returns `AnyPointer` if the Chris@50: // parameter is not bound. Chris@50: // Chris@50: // TODO(someday): Public interface for iterating over all bindings? Chris@50: Chris@50: Schema getDependency(uint64_t id, uint location) const; Chris@50: // Look up schema for a particular dependency of this schema. `location` is the dependency Chris@50: // location number as defined in _::RawBrandedSchema. Chris@50: Chris@50: Type interpretType(schema::Type::Reader proto, uint location) const; Chris@50: // Interpret a schema::Type in the given location within the schema, compiling it into a Chris@50: // Type object. Chris@50: Chris@50: friend class StructSchema; Chris@50: friend class EnumSchema; Chris@50: friend class InterfaceSchema; Chris@50: friend class ConstSchema; Chris@50: friend class ListSchema; Chris@50: friend class SchemaLoader; Chris@50: friend class Type; Chris@50: friend kj::StringTree _::structString( Chris@50: _::StructReader reader, const _::RawBrandedSchema& schema); Chris@50: friend kj::String _::enumString(uint16_t value, const _::RawBrandedSchema& schema); Chris@50: }; Chris@50: Chris@50: kj::StringPtr KJ_STRINGIFY(const Schema& schema); Chris@50: Chris@50: class Schema::BrandArgumentList { Chris@50: // A list of generic parameter bindings for parameters of some particular type. Note that since Chris@50: // parameters on an outer type apply to all inner types as well, a deeply-nested type can have Chris@50: // multiple BrandArgumentLists that apply to it. Chris@50: // Chris@50: // A BrandArgumentList only represents the arguments that the client of the type specified. Since Chris@50: // new parameters can be added over time, this list may not cover all defined parameters for the Chris@50: // type. Missing parameters should be treated as AnyPointer. This class's implementation of Chris@50: // operator[] already does this for you; out-of-bounds access will safely return AnyPointer. Chris@50: Chris@50: public: Chris@50: inline BrandArgumentList(): scopeId(0), size_(0), bindings(nullptr) {} Chris@50: Chris@50: inline uint size() const { return size_; } Chris@50: Type operator[](uint index) const; Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: uint64_t scopeId; Chris@50: uint size_; Chris@50: bool isUnbound; Chris@50: const _::RawBrandedSchema::Binding* bindings; Chris@50: Chris@50: inline BrandArgumentList(uint64_t scopeId, bool isUnbound) Chris@50: : scopeId(scopeId), size_(0), isUnbound(isUnbound), bindings(nullptr) {} Chris@50: inline BrandArgumentList(uint64_t scopeId, uint size, Chris@50: const _::RawBrandedSchema::Binding* bindings) Chris@50: : scopeId(scopeId), size_(size), isUnbound(false), bindings(bindings) {} Chris@50: Chris@50: friend class Schema; Chris@50: }; Chris@50: Chris@50: // ------------------------------------------------------------------- Chris@50: Chris@50: class StructSchema: public Schema { Chris@50: public: Chris@50: inline StructSchema(): Schema(&_::NULL_STRUCT_SCHEMA.defaultBrand) {} Chris@50: Chris@50: class Field; Chris@50: class FieldList; Chris@50: class FieldSubset; Chris@50: Chris@50: FieldList getFields() const; Chris@50: // List top-level fields of this struct. This list will contain top-level groups (including Chris@50: // named unions) but not the members of those groups. The list does, however, contain the Chris@50: // members of the unnamed union, if there is one. Chris@50: Chris@50: FieldSubset getUnionFields() const; Chris@50: // If the field contains an unnamed union, get a list of fields in the union, ordered by Chris@50: // ordinal. Since discriminant values are assigned sequentially by ordinal, you may index this Chris@50: // list by discriminant value. Chris@50: Chris@50: FieldSubset getNonUnionFields() const; Chris@50: // Get the fields of this struct which are not in an unnamed union, ordered by ordinal. Chris@50: Chris@50: kj::Maybe findFieldByName(kj::StringPtr name) const; Chris@50: // Find the field with the given name, or return null if there is no such field. If the struct Chris@50: // contains an unnamed union, then this will find fields of that union in addition to fields Chris@50: // of the outer struct, since they exist in the same namespace. It will not, however, find Chris@50: // members of groups (including named unions) -- you must first look up the group itself, Chris@50: // then dig into its type. Chris@50: Chris@50: Field getFieldByName(kj::StringPtr name) const; Chris@50: // Like findFieldByName() but throws an exception on failure. Chris@50: Chris@50: kj::Maybe getFieldByDiscriminant(uint16_t discriminant) const; Chris@50: // Finds the field whose `discriminantValue` is equal to the given value, or returns null if Chris@50: // there is no such field. (If the schema does not represent a union or a struct containing Chris@50: // an unnamed union, then this always returns null.) Chris@50: Chris@50: private: Chris@50: StructSchema(Schema base): Schema(base) {} Chris@50: template static inline StructSchema fromImpl() { Chris@50: return StructSchema(Schema(&_::rawBrandedSchema())); Chris@50: } Chris@50: friend class Schema; Chris@50: friend class Type; Chris@50: }; Chris@50: Chris@50: class StructSchema::Field { Chris@50: public: Chris@50: Field() = default; Chris@50: Chris@50: inline schema::Field::Reader getProto() const { return proto; } Chris@50: inline StructSchema getContainingStruct() const { return parent; } Chris@50: Chris@50: inline uint getIndex() const { return index; } Chris@50: // Get the index of this field within the containing struct or union. Chris@50: Chris@50: Type getType() const; Chris@50: // Get the type of this field. Note that this is preferred over getProto().getType() as this Chris@50: // method will apply generics. Chris@50: Chris@50: uint32_t getDefaultValueSchemaOffset() const; Chris@50: // For struct, list, and object fields, returns the offset, in words, within the first segment of Chris@50: // the struct's schema, where this field's default value pointer is located. The schema is Chris@50: // always stored as a single-segment unchecked message, which in turn means that the default Chris@50: // value pointer itself can be treated as the root of an unchecked message -- if you know where Chris@50: // to find it, which is what this method helps you with. Chris@50: // Chris@50: // For blobs, returns the offset of the beginning of the blob's content within the first segment Chris@50: // of the struct's schema. Chris@50: // Chris@50: // This is primarily useful for code generators. The C++ code generator, for example, embeds Chris@50: // the entire schema as a raw word array within the generated code. Of course, to implement Chris@50: // field accessors, it needs access to those fields' default values. Embedding separate copies Chris@50: // of those default values would be redundant since they are already included in the schema, but Chris@50: // seeking through the schema at runtime to find the default values would be ugly. Instead, Chris@50: // the code generator can use getDefaultValueSchemaOffset() to find the offset of the default Chris@50: // value within the schema, and can simply apply that offset at runtime. Chris@50: // Chris@50: // If the above does not make sense, you probably don't need this method. Chris@50: Chris@50: inline bool operator==(const Field& other) const; Chris@50: inline bool operator!=(const Field& other) const { return !(*this == other); } Chris@50: Chris@50: private: Chris@50: StructSchema parent; Chris@50: uint index; Chris@50: schema::Field::Reader proto; Chris@50: Chris@50: inline Field(StructSchema parent, uint index, schema::Field::Reader proto) Chris@50: : parent(parent), index(index), proto(proto) {} Chris@50: Chris@50: friend class StructSchema; Chris@50: }; Chris@50: Chris@50: kj::StringPtr KJ_STRINGIFY(const StructSchema::Field& field); Chris@50: Chris@50: class StructSchema::FieldList { Chris@50: public: Chris@50: FieldList() = default; // empty list Chris@50: Chris@50: inline uint size() const { return list.size(); } Chris@50: inline Field operator[](uint index) const { return Field(parent, index, list[index]); } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: StructSchema parent; Chris@50: List::Reader list; Chris@50: Chris@50: inline FieldList(StructSchema parent, List::Reader list) Chris@50: : parent(parent), list(list) {} Chris@50: Chris@50: friend class StructSchema; Chris@50: }; Chris@50: Chris@50: class StructSchema::FieldSubset { Chris@50: public: Chris@50: FieldSubset() = default; // empty list Chris@50: Chris@50: inline uint size() const { return size_; } Chris@50: inline Field operator[](uint index) const { Chris@50: return Field(parent, indices[index], list[indices[index]]); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: StructSchema parent; Chris@50: List::Reader list; Chris@50: const uint16_t* indices; Chris@50: uint size_; Chris@50: Chris@50: inline FieldSubset(StructSchema parent, List::Reader list, Chris@50: const uint16_t* indices, uint size) Chris@50: : parent(parent), list(list), indices(indices), size_(size) {} Chris@50: Chris@50: friend class StructSchema; Chris@50: }; Chris@50: Chris@50: // ------------------------------------------------------------------- Chris@50: Chris@50: class EnumSchema: public Schema { Chris@50: public: Chris@50: inline EnumSchema(): Schema(&_::NULL_ENUM_SCHEMA.defaultBrand) {} Chris@50: Chris@50: class Enumerant; Chris@50: class EnumerantList; Chris@50: Chris@50: EnumerantList getEnumerants() const; Chris@50: Chris@50: kj::Maybe findEnumerantByName(kj::StringPtr name) const; Chris@50: Chris@50: Enumerant getEnumerantByName(kj::StringPtr name) const; Chris@50: // Like findEnumerantByName() but throws an exception on failure. Chris@50: Chris@50: private: Chris@50: EnumSchema(Schema base): Schema(base) {} Chris@50: template static inline EnumSchema fromImpl() { Chris@50: return EnumSchema(Schema(&_::rawBrandedSchema())); Chris@50: } Chris@50: friend class Schema; Chris@50: friend class Type; Chris@50: }; Chris@50: Chris@50: class EnumSchema::Enumerant { Chris@50: public: Chris@50: Enumerant() = default; Chris@50: Chris@50: inline schema::Enumerant::Reader getProto() const { return proto; } Chris@50: inline EnumSchema getContainingEnum() const { return parent; } Chris@50: Chris@50: inline uint16_t getOrdinal() const { return ordinal; } Chris@50: inline uint getIndex() const { return ordinal; } Chris@50: Chris@50: inline bool operator==(const Enumerant& other) const; Chris@50: inline bool operator!=(const Enumerant& other) const { return !(*this == other); } Chris@50: Chris@50: private: Chris@50: EnumSchema parent; Chris@50: uint16_t ordinal; Chris@50: schema::Enumerant::Reader proto; Chris@50: Chris@50: inline Enumerant(EnumSchema parent, uint16_t ordinal, schema::Enumerant::Reader proto) Chris@50: : parent(parent), ordinal(ordinal), proto(proto) {} Chris@50: Chris@50: friend class EnumSchema; Chris@50: }; Chris@50: Chris@50: class EnumSchema::EnumerantList { Chris@50: public: Chris@50: EnumerantList() = default; // empty list Chris@50: Chris@50: inline uint size() const { return list.size(); } Chris@50: inline Enumerant operator[](uint index) const { return Enumerant(parent, index, list[index]); } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: EnumSchema parent; Chris@50: List::Reader list; Chris@50: Chris@50: inline EnumerantList(EnumSchema parent, List::Reader list) Chris@50: : parent(parent), list(list) {} Chris@50: Chris@50: friend class EnumSchema; Chris@50: }; Chris@50: Chris@50: // ------------------------------------------------------------------- Chris@50: Chris@50: class InterfaceSchema: public Schema { Chris@50: public: Chris@50: inline InterfaceSchema(): Schema(&_::NULL_INTERFACE_SCHEMA.defaultBrand) {} Chris@50: Chris@50: class Method; Chris@50: class MethodList; Chris@50: Chris@50: MethodList getMethods() const; Chris@50: Chris@50: kj::Maybe findMethodByName(kj::StringPtr name) const; Chris@50: Chris@50: Method getMethodByName(kj::StringPtr name) const; Chris@50: // Like findMethodByName() but throws an exception on failure. Chris@50: Chris@50: class SuperclassList; Chris@50: Chris@50: SuperclassList getSuperclasses() const; Chris@50: // Get the immediate superclasses of this type, after applying generics. Chris@50: Chris@50: bool extends(InterfaceSchema other) const; Chris@50: // Returns true if `other` is a superclass of this interface (including if `other == *this`). Chris@50: Chris@50: kj::Maybe findSuperclass(uint64_t typeId) const; Chris@50: // Find the superclass of this interface with the given type ID. Returns null if the interface Chris@50: // extends no such type. Chris@50: Chris@50: private: Chris@50: InterfaceSchema(Schema base): Schema(base) {} Chris@50: template static inline InterfaceSchema fromImpl() { Chris@50: return InterfaceSchema(Schema(&_::rawBrandedSchema())); Chris@50: } Chris@50: friend class Schema; Chris@50: friend class Type; Chris@50: Chris@50: kj::Maybe findMethodByName(kj::StringPtr name, uint& counter) const; Chris@50: bool extends(InterfaceSchema other, uint& counter) const; Chris@50: kj::Maybe findSuperclass(uint64_t typeId, uint& counter) const; Chris@50: // We protect against malicious schemas with large or cyclic hierarchies by cutting off the Chris@50: // search when the counter reaches a threshold. Chris@50: }; Chris@50: Chris@50: class InterfaceSchema::Method { Chris@50: public: Chris@50: Method() = default; Chris@50: Chris@50: inline schema::Method::Reader getProto() const { return proto; } Chris@50: inline InterfaceSchema getContainingInterface() const { return parent; } Chris@50: Chris@50: inline uint16_t getOrdinal() const { return ordinal; } Chris@50: inline uint getIndex() const { return ordinal; } Chris@50: Chris@50: StructSchema getParamType() const; Chris@50: StructSchema getResultType() const; Chris@50: // Get the parameter and result types, including substituting generic parameters. Chris@50: Chris@50: inline bool operator==(const Method& other) const; Chris@50: inline bool operator!=(const Method& other) const { return !(*this == other); } Chris@50: Chris@50: private: Chris@50: InterfaceSchema parent; Chris@50: uint16_t ordinal; Chris@50: schema::Method::Reader proto; Chris@50: Chris@50: inline Method(InterfaceSchema parent, uint16_t ordinal, Chris@50: schema::Method::Reader proto) Chris@50: : parent(parent), ordinal(ordinal), proto(proto) {} Chris@50: Chris@50: friend class InterfaceSchema; Chris@50: }; Chris@50: Chris@50: class InterfaceSchema::MethodList { Chris@50: public: Chris@50: MethodList() = default; // empty list Chris@50: Chris@50: inline uint size() const { return list.size(); } Chris@50: inline Method operator[](uint index) const { return Method(parent, index, list[index]); } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: InterfaceSchema parent; Chris@50: List::Reader list; Chris@50: Chris@50: inline MethodList(InterfaceSchema parent, List::Reader list) Chris@50: : parent(parent), list(list) {} Chris@50: Chris@50: friend class InterfaceSchema; Chris@50: }; Chris@50: Chris@50: class InterfaceSchema::SuperclassList { Chris@50: public: Chris@50: SuperclassList() = default; // empty list Chris@50: Chris@50: inline uint size() const { return list.size(); } Chris@50: InterfaceSchema operator[](uint index) const; Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: InterfaceSchema parent; Chris@50: List::Reader list; Chris@50: Chris@50: inline SuperclassList(InterfaceSchema parent, List::Reader list) Chris@50: : parent(parent), list(list) {} Chris@50: Chris@50: friend class InterfaceSchema; Chris@50: }; Chris@50: Chris@50: // ------------------------------------------------------------------- Chris@50: Chris@50: class ConstSchema: public Schema { Chris@50: // Represents a constant declaration. Chris@50: // Chris@50: // `ConstSchema` can be implicitly cast to DynamicValue to read its value. Chris@50: Chris@50: public: Chris@50: inline ConstSchema(): Schema(&_::NULL_CONST_SCHEMA.defaultBrand) {} Chris@50: Chris@50: template Chris@50: ReaderFor as() const; Chris@50: // Read the constant's value. This is a convenience method equivalent to casting the ConstSchema Chris@50: // to a DynamicValue and then calling its `as()` method. For dependency reasons, this method Chris@50: // is defined in , which you must #include explicitly. Chris@50: Chris@50: uint32_t getValueSchemaOffset() const; Chris@50: // Much like StructSchema::Field::getDefaultValueSchemaOffset(), if the constant has pointer Chris@50: // type, this gets the offset from the beginning of the constant's schema node to a pointer Chris@50: // representing the constant value. Chris@50: Chris@50: Type getType() const; Chris@50: Chris@50: private: Chris@50: ConstSchema(Schema base): Schema(base) {} Chris@50: friend class Schema; Chris@50: }; Chris@50: Chris@50: // ------------------------------------------------------------------- Chris@50: Chris@50: class Type { Chris@50: public: Chris@50: struct BrandParameter { Chris@50: uint64_t scopeId; Chris@50: uint index; Chris@50: }; Chris@50: struct ImplicitParameter { Chris@50: uint index; Chris@50: }; Chris@50: Chris@50: inline Type(); Chris@50: inline Type(schema::Type::Which primitive); Chris@50: inline Type(StructSchema schema); Chris@50: inline Type(EnumSchema schema); Chris@50: inline Type(InterfaceSchema schema); Chris@50: inline Type(ListSchema schema); Chris@50: inline Type(schema::Type::AnyPointer::Unconstrained::Which anyPointerKind); Chris@50: inline Type(BrandParameter param); Chris@50: inline Type(ImplicitParameter param); Chris@50: Chris@50: template Chris@50: inline static Type from(); Chris@50: Chris@50: inline schema::Type::Which which() const; Chris@50: Chris@50: StructSchema asStruct() const; Chris@50: EnumSchema asEnum() const; Chris@50: InterfaceSchema asInterface() const; Chris@50: ListSchema asList() const; Chris@50: // Each of these methods may only be called if which() returns the corresponding type. Chris@50: Chris@50: kj::Maybe getBrandParameter() const; Chris@50: // Only callable if which() returns ANY_POINTER. Returns null if the type is just a regular Chris@50: // AnyPointer and not a parameter. Chris@50: Chris@50: kj::Maybe getImplicitParameter() const; Chris@50: // Only callable if which() returns ANY_POINTER. Returns null if the type is just a regular Chris@50: // AnyPointer and not a parameter. "Implicit parameters" refer to type parameters on methods. Chris@50: Chris@50: inline schema::Type::AnyPointer::Unconstrained::Which whichAnyPointerKind() const; Chris@50: // Only callable if which() returns ANY_POINTER. Chris@50: Chris@50: inline bool isVoid() const; Chris@50: inline bool isBool() const; Chris@50: inline bool isInt8() const; Chris@50: inline bool isInt16() const; Chris@50: inline bool isInt32() const; Chris@50: inline bool isInt64() const; Chris@50: inline bool isUInt8() const; Chris@50: inline bool isUInt16() const; Chris@50: inline bool isUInt32() const; Chris@50: inline bool isUInt64() const; Chris@50: inline bool isFloat32() const; Chris@50: inline bool isFloat64() const; Chris@50: inline bool isText() const; Chris@50: inline bool isData() const; Chris@50: inline bool isList() const; Chris@50: inline bool isEnum() const; Chris@50: inline bool isStruct() const; Chris@50: inline bool isInterface() const; Chris@50: inline bool isAnyPointer() const; Chris@50: Chris@50: bool operator==(const Type& other) const; Chris@50: inline bool operator!=(const Type& other) const { return !(*this == other); } Chris@50: Chris@50: size_t hashCode() const; Chris@50: Chris@50: inline Type wrapInList(uint depth = 1) const; Chris@50: // Return the Type formed by wrapping this type in List() `depth` times. Chris@50: Chris@50: inline Type(schema::Type::Which derived, const _::RawBrandedSchema* schema); Chris@50: // For internal use. Chris@50: Chris@50: private: Chris@50: schema::Type::Which baseType; // type not including applications of List() Chris@50: uint8_t listDepth; // 0 for T, 1 for List(T), 2 for List(List(T)), ... Chris@50: Chris@50: bool isImplicitParam; Chris@50: // If true, this refers to an implicit method parameter. baseType must be ANY_POINTER, scopeId Chris@50: // must be zero, and paramIndex indicates the parameter index. Chris@50: Chris@50: union { Chris@50: uint16_t paramIndex; Chris@50: // If baseType is ANY_POINTER but this Type actually refers to a type parameter, this is the Chris@50: // index of the parameter among the parameters at its scope, and `scopeId` below is the type ID Chris@50: // of the scope where the parameter was defined. Chris@50: Chris@50: schema::Type::AnyPointer::Unconstrained::Which anyPointerKind; Chris@50: // If scopeId is zero and isImplicitParam is false. Chris@50: }; Chris@50: Chris@50: union { Chris@50: const _::RawBrandedSchema* schema; // if type is struct, enum, interface... Chris@50: uint64_t scopeId; // if type is AnyPointer but it's actually a type parameter... Chris@50: }; Chris@50: Chris@50: Type(schema::Type::Which baseType, uint8_t listDepth, const _::RawBrandedSchema* schema) Chris@50: : baseType(baseType), listDepth(listDepth), schema(schema) { Chris@50: KJ_IREQUIRE(baseType != schema::Type::ANY_POINTER); Chris@50: } Chris@50: Chris@50: void requireUsableAs(Type expected) const; Chris@50: Chris@50: friend class ListSchema; // only for requireUsableAs() Chris@50: }; Chris@50: Chris@50: // ------------------------------------------------------------------- Chris@50: Chris@50: class ListSchema { Chris@50: // ListSchema is a little different because list types are not described by schema nodes. So, Chris@50: // ListSchema doesn't subclass Schema. Chris@50: Chris@50: public: Chris@50: ListSchema() = default; Chris@50: Chris@50: static ListSchema of(schema::Type::Which primitiveType); Chris@50: static ListSchema of(StructSchema elementType); Chris@50: static ListSchema of(EnumSchema elementType); Chris@50: static ListSchema of(InterfaceSchema elementType); Chris@50: static ListSchema of(ListSchema elementType); Chris@50: static ListSchema of(Type elementType); Chris@50: // Construct the schema for a list of the given type. Chris@50: Chris@50: static ListSchema of(schema::Type::Reader elementType, Schema context) Chris@50: KJ_DEPRECATED("Does not handle generics correctly."); Chris@50: // DEPRECATED: This method cannot correctly account for generic type parameter bindings that Chris@50: // may apply to the input type. Instead of using this method, use a method of the Schema API Chris@50: // that corresponds to the exact kind of dependency. For example, to get a field type, use Chris@50: // StructSchema::Field::getType(). Chris@50: // Chris@50: // Construct from an element type schema. Requires a context which can handle getDependency() Chris@50: // requests for any type ID found in the schema. Chris@50: Chris@50: Type getElementType() const; Chris@50: Chris@50: inline schema::Type::Which whichElementType() const; Chris@50: // Get the element type's "which()". ListSchema does not actually store a schema::Type::Reader Chris@50: // describing the element type, but if it did, this would be equivalent to calling Chris@50: // .getBody().which() on that type. Chris@50: Chris@50: StructSchema getStructElementType() const; Chris@50: EnumSchema getEnumElementType() const; Chris@50: InterfaceSchema getInterfaceElementType() const; Chris@50: ListSchema getListElementType() const; Chris@50: // Get the schema for complex element types. Each of these throws an exception if the element Chris@50: // type is not of the requested kind. Chris@50: Chris@50: inline bool operator==(const ListSchema& other) const { return elementType == other.elementType; } Chris@50: inline bool operator!=(const ListSchema& other) const { return elementType != other.elementType; } Chris@50: Chris@50: template Chris@50: void requireUsableAs() const; Chris@50: Chris@50: private: Chris@50: Type elementType; Chris@50: Chris@50: inline explicit ListSchema(Type elementType): elementType(elementType) {} Chris@50: Chris@50: template Chris@50: struct FromImpl; Chris@50: template static inline ListSchema fromImpl() { Chris@50: return FromImpl::get(); Chris@50: } Chris@50: Chris@50: void requireUsableAs(ListSchema expected) const; Chris@50: Chris@50: friend class Schema; Chris@50: }; Chris@50: Chris@50: // ======================================================================================= Chris@50: // inline implementation Chris@50: Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::VOID; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::BOOL; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::INT8; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::INT16; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::INT32; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::INT64; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT8; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT16; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT32; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT64; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::FLOAT32; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::FLOAT64; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::TEXT; } Chris@50: template <> inline schema::Type::Which Schema::from() { return schema::Type::DATA; } Chris@50: Chris@50: inline Schema Schema::getDependency(uint64_t id) const { Chris@50: return getDependency(id, 0); Chris@50: } Chris@50: Chris@50: inline bool Schema::isBranded() const { Chris@50: return raw != &raw->generic->defaultBrand; Chris@50: } Chris@50: Chris@50: inline Schema Schema::getGeneric() const { Chris@50: return Schema(&raw->generic->defaultBrand); Chris@50: } Chris@50: Chris@50: template Chris@50: inline void Schema::requireUsableAs() const { Chris@50: requireUsableAs(&_::rawSchema()); Chris@50: } Chris@50: Chris@50: inline bool StructSchema::Field::operator==(const Field& other) const { Chris@50: return parent == other.parent && index == other.index; Chris@50: } Chris@50: inline bool EnumSchema::Enumerant::operator==(const Enumerant& other) const { Chris@50: return parent == other.parent && ordinal == other.ordinal; Chris@50: } Chris@50: inline bool InterfaceSchema::Method::operator==(const Method& other) const { Chris@50: return parent == other.parent && ordinal == other.ordinal; Chris@50: } Chris@50: Chris@50: inline ListSchema ListSchema::of(StructSchema elementType) { Chris@50: return ListSchema(Type(elementType)); Chris@50: } Chris@50: inline ListSchema ListSchema::of(EnumSchema elementType) { Chris@50: return ListSchema(Type(elementType)); Chris@50: } Chris@50: inline ListSchema ListSchema::of(InterfaceSchema elementType) { Chris@50: return ListSchema(Type(elementType)); Chris@50: } Chris@50: inline ListSchema ListSchema::of(ListSchema elementType) { Chris@50: return ListSchema(Type(elementType)); Chris@50: } Chris@50: inline ListSchema ListSchema::of(Type elementType) { Chris@50: return ListSchema(elementType); Chris@50: } Chris@50: Chris@50: inline Type ListSchema::getElementType() const { Chris@50: return elementType; Chris@50: } Chris@50: Chris@50: inline schema::Type::Which ListSchema::whichElementType() const { Chris@50: return elementType.which(); Chris@50: } Chris@50: Chris@50: inline StructSchema ListSchema::getStructElementType() const { Chris@50: return elementType.asStruct(); Chris@50: } Chris@50: Chris@50: inline EnumSchema ListSchema::getEnumElementType() const { Chris@50: return elementType.asEnum(); Chris@50: } Chris@50: Chris@50: inline InterfaceSchema ListSchema::getInterfaceElementType() const { Chris@50: return elementType.asInterface(); Chris@50: } Chris@50: Chris@50: inline ListSchema ListSchema::getListElementType() const { Chris@50: return elementType.asList(); Chris@50: } Chris@50: Chris@50: template Chris@50: inline void ListSchema::requireUsableAs() const { Chris@50: static_assert(kind() == Kind::LIST, Chris@50: "ListSchema::requireUsableAs() requires T is a list type."); Chris@50: requireUsableAs(Schema::from()); Chris@50: } Chris@50: Chris@50: inline void ListSchema::requireUsableAs(ListSchema expected) const { Chris@50: elementType.requireUsableAs(expected.elementType); Chris@50: } Chris@50: Chris@50: template Chris@50: struct ListSchema::FromImpl> { Chris@50: static inline ListSchema get() { return of(Schema::from()); } Chris@50: }; Chris@50: Chris@50: inline Type::Type(): baseType(schema::Type::VOID), listDepth(0), schema(nullptr) {} Chris@50: inline Type::Type(schema::Type::Which primitive) Chris@50: : baseType(primitive), listDepth(0), isImplicitParam(false) { Chris@50: KJ_IREQUIRE(primitive != schema::Type::STRUCT && Chris@50: primitive != schema::Type::ENUM && Chris@50: primitive != schema::Type::INTERFACE && Chris@50: primitive != schema::Type::LIST); Chris@50: if (primitive == schema::Type::ANY_POINTER) { Chris@50: scopeId = 0; Chris@50: anyPointerKind = schema::Type::AnyPointer::Unconstrained::ANY_KIND; Chris@50: } else { Chris@50: schema = nullptr; Chris@50: } Chris@50: } Chris@50: inline Type::Type(schema::Type::Which derived, const _::RawBrandedSchema* schema) Chris@50: : baseType(derived), listDepth(0), isImplicitParam(false), schema(schema) { Chris@50: KJ_IREQUIRE(derived == schema::Type::STRUCT || Chris@50: derived == schema::Type::ENUM || Chris@50: derived == schema::Type::INTERFACE); Chris@50: } Chris@50: Chris@50: inline Type::Type(StructSchema schema) Chris@50: : baseType(schema::Type::STRUCT), listDepth(0), schema(schema.raw) {} Chris@50: inline Type::Type(EnumSchema schema) Chris@50: : baseType(schema::Type::ENUM), listDepth(0), schema(schema.raw) {} Chris@50: inline Type::Type(InterfaceSchema schema) Chris@50: : baseType(schema::Type::INTERFACE), listDepth(0), schema(schema.raw) {} Chris@50: inline Type::Type(ListSchema schema) Chris@50: : Type(schema.getElementType()) { ++listDepth; } Chris@50: inline Type::Type(schema::Type::AnyPointer::Unconstrained::Which anyPointerKind) Chris@50: : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(false), Chris@50: anyPointerKind(anyPointerKind), scopeId(0) {} Chris@50: inline Type::Type(BrandParameter param) Chris@50: : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(false), Chris@50: paramIndex(param.index), scopeId(param.scopeId) {} Chris@50: inline Type::Type(ImplicitParameter param) Chris@50: : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(true), Chris@50: paramIndex(param.index), scopeId(0) {} Chris@50: Chris@50: inline schema::Type::Which Type::which() const { Chris@50: return listDepth > 0 ? schema::Type::LIST : baseType; Chris@50: } Chris@50: Chris@50: inline schema::Type::AnyPointer::Unconstrained::Which Type::whichAnyPointerKind() const { Chris@50: KJ_IREQUIRE(baseType == schema::Type::ANY_POINTER); Chris@50: return !isImplicitParam && scopeId == 0 ? anyPointerKind Chris@50: : schema::Type::AnyPointer::Unconstrained::ANY_KIND; Chris@50: } Chris@50: Chris@50: template Chris@50: inline Type Type::from() { return Type(Schema::from()); } Chris@50: Chris@50: inline bool Type::isVoid () const { return baseType == schema::Type::VOID && listDepth == 0; } Chris@50: inline bool Type::isBool () const { return baseType == schema::Type::BOOL && listDepth == 0; } Chris@50: inline bool Type::isInt8 () const { return baseType == schema::Type::INT8 && listDepth == 0; } Chris@50: inline bool Type::isInt16 () const { return baseType == schema::Type::INT16 && listDepth == 0; } Chris@50: inline bool Type::isInt32 () const { return baseType == schema::Type::INT32 && listDepth == 0; } Chris@50: inline bool Type::isInt64 () const { return baseType == schema::Type::INT64 && listDepth == 0; } Chris@50: inline bool Type::isUInt8 () const { return baseType == schema::Type::UINT8 && listDepth == 0; } Chris@50: inline bool Type::isUInt16 () const { return baseType == schema::Type::UINT16 && listDepth == 0; } Chris@50: inline bool Type::isUInt32 () const { return baseType == schema::Type::UINT32 && listDepth == 0; } Chris@50: inline bool Type::isUInt64 () const { return baseType == schema::Type::UINT64 && listDepth == 0; } Chris@50: inline bool Type::isFloat32() const { return baseType == schema::Type::FLOAT32 && listDepth == 0; } Chris@50: inline bool Type::isFloat64() const { return baseType == schema::Type::FLOAT64 && listDepth == 0; } Chris@50: inline bool Type::isText () const { return baseType == schema::Type::TEXT && listDepth == 0; } Chris@50: inline bool Type::isData () const { return baseType == schema::Type::DATA && listDepth == 0; } Chris@50: inline bool Type::isList () const { return listDepth > 0; } Chris@50: inline bool Type::isEnum () const { return baseType == schema::Type::ENUM && listDepth == 0; } Chris@50: inline bool Type::isStruct () const { return baseType == schema::Type::STRUCT && listDepth == 0; } Chris@50: inline bool Type::isInterface() const { Chris@50: return baseType == schema::Type::INTERFACE && listDepth == 0; Chris@50: } Chris@50: inline bool Type::isAnyPointer() const { Chris@50: return baseType == schema::Type::ANY_POINTER && listDepth == 0; Chris@50: } Chris@50: Chris@50: inline Type Type::wrapInList(uint depth) const { Chris@50: Type result = *this; Chris@50: result.listDepth += depth; Chris@50: return result; Chris@50: } Chris@50: Chris@50: } // namespace capnp Chris@50: Chris@50: #endif // CAPNP_SCHEMA_H_