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