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