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