OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // This file contains structs used for constructing type descriptors for |
| 6 // generated mojom types. A type descriptor for a mojom struct is a table |
| 7 // describing the byte-offsets of all the pointers and handles in the struct, |
| 8 // and has references to the type descriptorss that further describe the |
| 9 // pointers. The table is used for doing all computations for the struct -- |
| 10 // determining serialized size, encoding and decoding recursively, etc. A type |
| 11 // descriptor is generated for each struct, union, array and map. Note that |
| 12 // mojom maps are just mojom structs with two mojom arrays, so there is no |
| 13 // separate descriptor for it. |
| 14 // |
| 15 // The user is not expected to construct type descriptors -- a bindings |
| 16 // generator will do this when it generates bindings for a mojom file. |
| 17 // |
| 18 // A type descriptor for a mojom struct is a |MojomTypeDescriptorStruct| |
| 19 // containing an array of entries of types of |MojomTypeDescriptorStructEntry|. |
| 20 // Similarly, unions are described with |MojomTypeDescriptorUnion| with entries |
| 21 // of |MojomTypeDescriptorUnionEntry|. Arrays are described with |
| 22 // |MojomTypeDescriptorArray|. |
| 23 |
| 24 #ifndef MOJO_PUBLIC_C_BINDINGS_LIB_TYPE_DESCRIPTOR_H_ |
| 25 #define MOJO_PUBLIC_C_BINDINGS_LIB_TYPE_DESCRIPTOR_H_ |
| 26 |
| 27 #include <stdbool.h> |
| 28 #include <stdint.h> |
| 29 |
| 30 #include "mojo/public/c/system/macros.h" |
| 31 |
| 32 MOJO_BEGIN_EXTERN_C |
| 33 |
| 34 // This enum is used in a type descriptor entry for the |elem_type| field, and |
| 35 // indicates which type the accompanying |elem_descriptor| is describing (if it |
| 36 // is describing a reference type), or to indicate that it's a handle type. |
| 37 // Values that correspond to an |elem_descriptor|'s pointer type: |
| 38 // - If MOJOM_TYPE_DESCRIPTOR_TYPE_STRUCT, then |elem_descriptor| points to a |
| 39 // |MojomTypeDescriptorStruct|. |
| 40 // - If MOJOM_TYPE_DESCRIPTOR_TYPE_UNION, then |elem_descriptor| points to a |
| 41 // |MojomTypeDescriptorUnion|. |
| 42 // - If MOJOM_TYPE_DESCRIPTOR_TYPE_ARRAY, then |elem_descriptor| points to a |
| 43 // |MojomTypeDescriptorArray|. |
| 44 // - For any other value, |elem_descriptor| is NULL. |
| 45 enum MojomTypeDescriptorType { |
| 46 // Note: A map is a mojom struct with 2 mojom arrays, so we don't have a |
| 47 // separate descriptor type for it. |
| 48 MOJOM_TYPE_DESCRIPTOR_TYPE_STRUCT = 0, |
| 49 MOJOM_TYPE_DESCRIPTOR_TYPE_ARRAY = 1, |
| 50 MOJOM_TYPE_DESCRIPTOR_TYPE_UNION = 2, |
| 51 MOJOM_TYPE_DESCRIPTOR_TYPE_HANDLE = 3, |
| 52 MOJOM_TYPE_DESCRIPTOR_TYPE_INTERFACE = 4, |
| 53 // This is only used in an array descriptor, and serves as a way to terminate |
| 54 // a chain of array descriptors; the last entry in the chain always contains a |
| 55 // plain-old-data type. |
| 56 MOJOM_TYPE_DESCRIPTOR_TYPE_POD = 5, |
| 57 }; |
| 58 |
| 59 // Mojom structs are described using this struct. |
| 60 struct MojomTypeDescriptorStruct { |
| 61 size_t num_entries; |
| 62 // |entries| is an array of |num_entries|, each describing a field of |
| 63 // reference or handle type. |
| 64 const struct MojomTypeDescriptorStructEntry* entries; |
| 65 }; |
| 66 |
| 67 // This struct is used to describe each entry in a mojom struct. Each entry |
| 68 // indicates whether it is describing a reference type, or a handle type through |
| 69 // the field |elem_type|. |elem_descriptor| points to the type descriptor |
| 70 // describing the field, if the type has one (handles don't have type |
| 71 // descriptors). |offset| indicates the starting byte offset of the element |
| 72 // within the struct. |
| 73 struct MojomTypeDescriptorStructEntry { |
| 74 // See comments for |MojomTypeDescriptorType| on possible values and |
| 75 // corresponding behaviour with |elem_descriptor|. |
| 76 enum MojomTypeDescriptorType elem_type; |
| 77 // Use |elem_type| to decide which type |elem_descriptor| should be casted to. |
| 78 const void* elem_descriptor; |
| 79 // |offset| does not account for the struct header. Offset 0 always refers to |
| 80 // the first element. |
| 81 uint32_t offset; |
| 82 // Corresponds to the '[MinVersion]' attribute in mojom IDL. This determines |
| 83 // if this field should be ignored if its min_version < version of the struct |
| 84 // we are dealing with. |
| 85 uint32_t min_version; |
| 86 // Is this field nullable? |
| 87 bool nullable; |
| 88 }; |
| 89 |
| 90 // Mojom unions are described using this struct. |
| 91 struct MojomTypeDescriptorUnion { |
| 92 size_t num_entries; |
| 93 const struct MojomTypeDescriptorUnionEntry* entries; |
| 94 }; |
| 95 |
| 96 // Like |MojomTypeDescriptorStructEntry|, this variant is used to construct a |
| 97 // type descriptor for a union. Instead of an offset, it describes a union field |
| 98 // by its |tag|. |
| 99 struct MojomTypeDescriptorUnionEntry { |
| 100 // See comments for |MojomTypeDescriptorType| on possible values and |
| 101 // corresponding behaviour with |elem_descriptor|. |
| 102 enum MojomTypeDescriptorType elem_type; |
| 103 const void* elem_descriptor; |
| 104 // The tag of the union field. |
| 105 uint32_t tag; |
| 106 bool nullable; |
| 107 }; |
| 108 |
| 109 // Describes a mojom array. To describe an array, we don't need a table of |
| 110 // entries, since arrays can only describe 1 type. However, that one type can |
| 111 // recursively be an array (e.g, array<array<int>>), in which case a chain of |
| 112 // array entries are constructed. |
| 113 struct MojomTypeDescriptorArray { |
| 114 enum MojomTypeDescriptorType elem_type; |
| 115 const void* elem_descriptor; |
| 116 // How many elements is this array expected to hold? |
| 117 // 0 means unspecified. |
| 118 uint32_t num_elements; |
| 119 bool nullable; |
| 120 }; |
| 121 |
| 122 // This describes a mojom string. |
| 123 // A mojom string is a mojom array of chars without a fixed-sized. |
| 124 extern const struct MojomTypeDescriptorArray g_mojom_string_type_description; |
| 125 |
| 126 MOJO_END_EXTERN_C |
| 127 |
| 128 #endif // MOJO_PUBLIC_C_BINDINGS_LIB_TYPE_DESCRIPTOR_H_ |
OLD | NEW |