Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: mojo/public/interfaces/bindings/mojom_types.mojom

Issue 1417473002: Improves the representation of values in Mojom. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Remove some TODOs. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 [JavaPackage="org.chromium.mojo.bindings.types"] 5 [JavaPackage="org.chromium.mojo.bindings.types"]
6 module mojo.bindings.types; 6 module mojo.bindings.types;
7 7
8 8
9 /* 9 /*
10 * This file contains definitions of data structures used to represent 10 * This file contains definitions of data structures used to represent
11 * Mojom types and partially resolved Mojom type schema. The distinction between 11 * Mojom types and values.
12 * these is explained below.
13 * 12 *
14 * As described in the Mojom Language Specification, Mojom types are defined 13 * As described in the Mojom Language Specification, Mojom types are defined
15 * recursively and consequently a |Type| object may recursively contain 14 * recursively and consequently a |Type| object may recursively contain
16 * other |Type| objects. For example a |Type| object representing an array<int32> 15 * other |Type| objects. For example a |Type| object representing an array<int32>
17 * will contain a |Type| object representing an int32. 16 * will contain a |Type| object representing an int32.
18 * 17 *
19 * A Mojom type declaration may contain an identifier that resolves to a 18 * A Mojom type declaration may contain an identifier that resolves to a
20 * user-defined type: a struct, union, enum or interface. We use |TypeReference| 19 * user-defined type: a struct, union, enum or interface. We use |TypeReference|
21 * to represent an occurrence of such an identifier. A |TypeReference| may be 20 * to represent an occurrence of such an identifier. A |TypeReference| may be
22 * resolved or not. Resolved means that the user-defined type to which the 21 * resolved or not. Resolved means that the user-defined type to which the
23 * identifier refers has been found and associated with the |TypeReference|. 22 * identifier refers has been found and associated with the |TypeReference|.
24 * (We say more below about how this association works.) A |Type| object is 23 * A |Type| object is fully-resolved if it, and recursively all of its
25 * fully-resolved if it, and recursively all of its sub-components, do not 24 * sub-components, do not contain any unresolved TypeReferences.
26 * contain any unresolved TypeReferences. A |Type| object that is fully
27 * resolved represents a Mojom type. Otherwise we say that the |Type| object
28 * represents only a type schema, because until all of the identifiers have been
29 * resolved we don't know exactly which type it represents.
30 *
31 * The data structures defined in this file are useful in different contexts
32 * depending on whether the structures are fully-resolved or not. For example
33 * as a representation of partially resolved Mojom type declarations the
34 * structures may be useful as the intermediate representation output by the
35 * front end of a Mojom compiler. As a representation of fully-resolved Mojom
36 * types the structures may be useful at runtime for a service that deserializes
37 * arbitrary Mojo messages given only the name of the interface with which the
38 * message is associated.
39 * 25 *
40 * A resolved |TypeReference| does not literally contain a structure representing 26 * A resolved |TypeReference| does not literally contain a structure representing
41 * the type that it represents but rather refers to its target type indirectly 27 * the user-defined type that it represents but rather refers to its target type
42 * via a string called a |type_key|. This key may be exchanged (via a mechanism 28 * indirectly via a string called a |type_key|. The type_key may be used to
43 * described below) for an instance of a data structure that describes 29 * lookup the user-defined type to which it refers.
44 * the resolved type.
45 * 30 *
46 * The reason for this extra level of indirection is that Mojom types, being 31 * The mapping from |type_keys| to user-defined types is not
47 * recursive, are allowed to reference themselves. But Mojo messages are not 32 * represented by any structures in this file and instead must be maintained
48 * allowed to point to themselves. We give an example to clarify this. Consider 33 * by a higher layer context in which this file is used. For example the
49 * the following perfectly valid user-defined Mojom struct: 34 * |ServiceDescription| interface defined in service_describer.mojom includes
35 * the method:
36 * GetTypeDefinition(string type_key) => UserDefinedType? type);
37 * for this purpose.
38 * We refer to this higher-layer context as the *owning context.*
50 * 39 *
51 * struct TreeNode { 40 * In addition to types, Mojom values are also representd by structures in this
52 * TreeNode left_child; 41 * file. A |Value| may be a LiteralValue, a UserValueReference or a
53 * TreeNode right_child; 42 * BuiltinConstantValue. Similarly to the situation with TypeReferences,
54 * }; 43 * UserValueReferences contain a |value_key| which may be used to lookup
55 * 44 * a UserDefinedValue (an EnumValue or a UserDefinedConstant) in
56 * Consider how this type should be represented as an object from this file. 45 * the owning context. For example the |MojomFileGraph| struct in
57 * The |MojomStruct| structure defined below is used to represent structures. 46 * mojom_files.mojom contains the map:
58 * So let us attempt to define an instance of |MojomStruct| called |tree_node| 47 * map<string, UserDefinedValue> resolved_values;
59 * that represents the type |TreeNode|. A |MojomStruct| contains an array of 48 * for this purpose.
60 * |StructField|s each of which contains a |Type|. So |tree_node| would have
61 * two |StructField|s. But what should be the |Type| of these |StructField|s?
62 * It needs to be some instance of |Type|. At first we might suppose that the
63 * |Type| should be |tree_node| itself. But this is impossible because it
64 * would require that the definition of |tree_node| recursively pointed to
65 * itself, and this is forbidden in the definition of a Mojo message. We might
66 * think that we could get out of this dilemma by defining two copies of
67 * |tree_node| called |tree_node_2| and |tree_node_3| and setting the |Type|s of
68 * the |StructField|s of |tree_node| to be |tree_node_2| and |tree_node_3|.
69 * But we soon realize that this solution also does not work because we are
70 * then left with the problem of defining the |Type|s of the two |StructFields|
71 * of |tree_node_2| and |tree_node_3|.
72 *
73 * Our solution to this problem is to assign a |type_key| to the
74 * user-defined type TreeNode. Let's call this key |x|. Then we set the |Type|
75 * of the two |StructFields| of |tree_node| to be a resolved |TypeReference| with
76 * a |type_key| of x.
77 *
78 * The mapping from |type_keys| to user-defined type definitions is contained
79 * in a structure called a |MojomDescriptor| which is not defined in this file
80 * but rather in "mojom_descriptors.mojom" which imports this file. A |Type|
81 * object is associated with some instance of MojomDescriptor we call the owning
82 * MojomDescriptor. In order to recover the user-defined type associated with the
83 * |type_key| in a resolved |TypeReference| one uses the owning MojomDescriptor.
84 */ 49 */
85 50
86 // The different kinds of types. We divide the types into five categories: 51 // The different kinds of types. We divide the types into five categories:
87 // simple, string, compound, handle, and user-defined. 52 // simple, string, compound, handle, and user-defined.
88 union Type { 53 union Type {
89 SimpleType simple_type; 54 SimpleType simple_type;
90 55
91 StringType string_type; 56 StringType string_type;
92 57
93 // The compound types. These are built from simpler types. 58 // The compound types. These are built from simpler types.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 string? identifier; 137 string? identifier;
173 138
174 139
175 // This field is non-null if this reference has been resolved. 140 // This field is non-null if this reference has been resolved.
176 string? type_key; 141 string? type_key;
177 }; 142 };
178 143
179 //////////////////////////////////////////////////////////////////////////// 144 ////////////////////////////////////////////////////////////////////////////
180 // The data structures below represent user-defined types or type 145 // The data structures below represent user-defined types or type
181 // declarations. Instances of these are not literally contained in a 146 // declarations. Instances of these are not literally contained in a
182 // |Type| object. In a fully-resolved context these represent types and are 147 // |Type| object. Instead the owning context is used to lookup a UserDefinedType
183 // obtained from the owning |MojomDescriptor|. 148 // given a type_key.
184 //////////////////////////////////////////////////////////////////////////// 149 ////////////////////////////////////////////////////////////////////////////
185 150
186 // Represents a user-defined type referenced 151 // Represents a user-defined type referenced
187 // via its identifier from another Mojom object. 152 // via its identifier from another Mojom object.
188 union UserDefinedType { 153 union UserDefinedType {
189 MojomEnum enum_type; 154 MojomEnum enum_type;
190 MojomStruct struct_type; 155 MojomStruct struct_type;
191 MojomUnion union_type; 156 MojomUnion union_type;
192 MojomInterface interface_type; 157 MojomInterface interface_type;
193 }; 158 };
194 159
195 // A field of a struct. These structures are contained in the |fields| field 160 // A field of a struct. These structures are contained in the |fields| field
196 // of the |MojomStruct| struct. 161 // of the |MojomStruct| struct.
197 struct StructField { 162 struct StructField {
198 DeclarationData? decl_data; // Some implementations may not provide this. 163 DeclarationData? decl_data; // Some implementations may not provide this.
199 164
200 Type type; 165 Type type;
201 166
202 // The value must eventually resolve to a ConstantValue of type |field_type|. 167 DefaultFieldValue? default_value;
203 ConstantOccurrence? default_value;
204 168
205 // The offset in bytes from the start of the serialized struct, not including 169 // The offset in bytes from the start of the serialized struct, not including
206 // the eight-byte header, of the first byte of this field. In the case of 170 // the eight-byte header, of the first byte of this field. In the case of
207 // boolean fields, this refers to the byte in which the field's bit is 171 // boolean fields, this refers to the byte in which the field's bit is
208 // located but not which bit corresponds to the field. 172 // located but not which bit corresponds to the field.
209 // A negative value means unset. 173 // A negative value means unset.
210 int32 offset; 174 int32 offset;
211 }; 175 };
212 176
177 union DefaultFieldValue {
178 Value value;
179 DefaultKeyword default_keyword;
180 };
181
182 // A built-in pseudo-value, indicated by the keyword "default", that
183 // specifies that the default value of a user-defined type should be used.
184 struct DefaultKeyword{};
185
213 struct StructVersion { 186 struct StructVersion {
214 uint32 version_number; 187 uint32 version_number;
215 uint32 num_fields; 188 uint32 num_fields;
216 uint32 num_bytes; 189 uint32 num_bytes;
217 }; 190 };
218 191
219 struct MojomStruct { 192 struct MojomStruct {
220 DeclarationData? decl_data; // Some implementations may not provide this. 193 DeclarationData? decl_data; // Some implementations may not provide this.
221 194
222 // The fields are in ordinal order. Note that this may be different than 195 // The fields are in ordinal order. Note that this may be different than
(...skipping 19 matching lines...) Expand all
242 215
243 struct MojomUnion { 216 struct MojomUnion {
244 DeclarationData? decl_data; // Some implementations may not provide this. 217 DeclarationData? decl_data; // Some implementations may not provide this.
245 218
246 // The fields are in tag order. Note that this may be different than 219 // The fields are in tag order. Note that this may be different than
247 // the order in which the fields are declared in the .mojom file. 220 // the order in which the fields are declared in the .mojom file.
248 array<UnionField> fields; 221 array<UnionField> fields;
249 }; 222 };
250 223
251 struct EnumValue { 224 struct EnumValue {
252 DeclarationData? decl_data; // Some implementations may not provide this. 225 DeclarationData? decl_data;
253 226
254 // The value must eventually resolve to a ConstantValue of type integer or 227 // The type key of the enum that this value belongs to.
255 // EnumConstantValue. 228 string enum_type_key;
256 ConstantOccurrence value; 229
230 // The integer value corresponding to this enum value.
231 int32 int_value;
257 }; 232 };
258 233
259 struct MojomEnum { 234 struct MojomEnum {
260 DeclarationData? decl_data; // Some implementations may not provide this. 235 DeclarationData? decl_data; // Some implementations may not provide this.
261 236
262 // The MojomEnum is fully resolved just in case all of the EnumValues are.
263 array<EnumValue> values; 237 array<EnumValue> values;
264 }; 238 };
265 239
266 struct MojomMethod { 240 struct MojomMethod {
267 DeclarationData? decl_data; // Some implementations may not provide this. 241 DeclarationData? decl_data; // Some implementations may not provide this.
268 242
269 MojomStruct parameters; 243 MojomStruct parameters;
270 244
271 // Note that there is a difference between response_params being null and 245 // Note that there is a difference between response_params being null and
272 // it containing zero fields. The former means that the method does 246 // it containing zero fields. The former means that the method does
(...skipping 10 matching lines...) Expand all
283 // By definition, the name of an interface is the string that would be passed 257 // By definition, the name of an interface is the string that would be passed
284 // to the method ServiceProvider.ConnectToService() in order obtain a 258 // to the method ServiceProvider.ConnectToService() in order obtain a
285 // connection to the interface. 259 // connection to the interface.
286 string interface_name; 260 string interface_name;
287 261
288 // All the methods in the interface. The keys are the method ordinals. 262 // All the methods in the interface. The keys are the method ordinals.
289 map<uint32, MojomMethod> methods; 263 map<uint32, MojomMethod> methods;
290 }; 264 };
291 265
292 //////////////////////////////////////////////////////////////////////////// 266 ////////////////////////////////////////////////////////////////////////////
293 // Representations of Mojom Constants 267 // Mojom values
294 //////////////////////////////////////////////////////////////////////////// 268 ////////////////////////////////////////////////////////////////////////////
295 269
296 // Represents an occurrence of a constant. This is either a literal or as an 270 // A value may occur as the default value of a struct field, as the
297 // identifier that refers to a declared constant. 271 // right-hand-side of a constant declaration, or as the right-hand-side
298 struct ConstantOccurrence { 272 // of an enum value specifier.
299 // This constant occurrence is fully resolved just in case |value| is not 273 union Value {
300 // null. If the constant occurrence was a literal then it is necessarily 274 // A literal number, boolean or string
301 // resolved to the value of the literal. If the constant occurrence was an 275 LiteralValue literal_value;
302 // identifier then it may or may not be resolved.
303 ConstantValue? value;
304 276
305 // Either |value| or |identifier| must be non-null. Some implementations 277 // A reference to a user-defined value (a declared constant or enum value.)
306 // will maintain |identifier| even after the constant occurrence has been 278 UserValueReference user_value_reference;
307 // resolved. 279
308 ConstantReference? identifier; 280 // A built-in numeric constant.
281 BuiltinConstantValue builtin_value;
309 }; 282 };
310 283
311 union ConstantValue { 284 union LiteralValue {
312 bool bool_value; 285 bool bool_value;
313 double double_value; 286 double double_value;
314 float float_value; 287 float float_value;
315 int8 int8_value; 288 int8 int8_value;
316 int16 int16_value; 289 int16 int16_value;
317 int32 int32_value; 290 int32 int32_value;
318 int64 int64_value; 291 int64 int64_value;
319 string string_value; 292 string string_value;
320 uint8 uint8_value; 293 uint8 uint8_value;
321 uint16 uint16_value; 294 uint16 uint16_value;
322 uint32 uint32_value; 295 uint32 uint32_value;
323 uint64 uint64_value; 296 uint64 uint64_value;
324 EnumConstantValue enum_value;
325 BuiltinConstantValue builtin_value;
326 }; 297 };
327 298
328 // Represents the built-in constants. 299 // Represents the built-in floating-point constants.
329 enum BuiltinConstantValue { 300 enum BuiltinConstantValue {
330 DOUBLE_INFINITY, 301 DOUBLE_INFINITY,
331 DOUBLE_NEGATIVE_INFINITY, 302 DOUBLE_NEGATIVE_INFINITY,
332 DOUBLE_NAN, 303 DOUBLE_NAN,
333 FLOAT_INFINITY, 304 FLOAT_INFINITY,
334 FLOAT_NEGATIVE_INFINITY, 305 FLOAT_NEGATIVE_INFINITY,
335 FLOAT_NAN, 306 FLOAT_NAN,
336 }; 307 };
337 308
338 // Represents an occurrence of a user-defined identifier that should resolve to 309 // A reference to a user-defined value (a declared constant or enum value.)
339 // a constant. The constant reference may or may not be resolved. If it is 310 struct UserValueReference {
340 // resolved then |constant_key| is not null. 311 // The identifier, as it appears at the reference site.
341 struct ConstantReference {
342 // The identifier, as it appears in the occurrence. Note that this may be
343 // a short name, a fully-qualified identifier, or a partially qualified
344 // identifier.
345 string identifier; 312 string identifier;
346 313
347 // This field is non-null if this reference has been resolved. Note that 314 // The key to the resolved value of this identifier. It refers to
348 // because a constant may be defined in terms of another constant, it is 315 // an instance of |UserDefinedValue| and so an EnumValue or
349 // possible that this is non-null but yet the |ConstantOccurrence| containing 316 // DeclaredConstant.
350 // this reference is not fully resolved and so its |value| is null. Also 317 string? value_key;
351 // note that some implementations will not provide this field for a 318
352 // fully-resolved |ConstantOccurrence|. 319 // The resolved concrete value. This must be a LiteralValue or a
353 string? constant_key; 320 // BuiltinConstantValue, not a UserValueReference. The resolved
321 // concrete value is defined as follows: If |value_key| refers to an
322 // EnumValue then |resolved_concrete_value| is the |int_value| of that
323 // EnumValue. If |value_key| referes to a DeclaredConstant then
324 // |resolved_concrete_value| is defined recursively to be the resolved
325 // concrete value of the DeclaredConstant's |value|.
326 Value? resolved_concrete_value;
354 }; 327 };
355 328
356 struct EnumConstantValue { 329 union UserDefinedValue {
357 // The reference must be resolved to an MojomEnum. 330 EnumValue enum_value;
358 TypeReference enum_type; 331 DeclaredConstant declared_constant;
359
360 string? enum_value_name; // Some implementations may not provide this name.
361
362 // The integer value that the enum value name resolves to.
363 int32 int_value;
364 }; 332 };
365 333
366 // This represents a Mojom constant declaration. 334 // This represents a Mojom constant declaration.
367 struct DeclaredConstant { 335 struct DeclaredConstant {
368 DeclarationData decl_data; 336 DeclarationData decl_data;
369 337
370 // The type must be a string, bool, float, double, or integer type. 338 // The type must be a string, bool, or numeric type.
371 Type type; 339 Type type;
372 340
373 // The value must eventually resolve to the same type as |type|. 341 // This is the value specified in the right-hand-side of the constant
374 ConstantOccurrence value; 342 // declaration. The value must be a literal value or a built-in constant of
343 // the same type as |type| or a UserValueReference whose
344 // |resolved_concrete_value| is one of those.
345 Value value;
375 }; 346 };
376 347
377 //////////////////////////////////////////////////////////////////////////// 348 ////////////////////////////////////////////////////////////////////////////
378 // Declaration Data 349 // Declaration Data
379 //////////////////////////////////////////////////////////////////////////// 350 ////////////////////////////////////////////////////////////////////////////
380 351
381 // This structure contains additional data that may be present in 352 // This structure contains additional data that may be present in
382 // a Mojom declaration. Some implementations of |MojomDescriptor| may 353 // a Mojom declaration. Some owning contexts may
383 // provide some of this data. 354 // provide some of this data.
384 struct DeclarationData { 355 struct DeclarationData {
385 array<Attribute>? attributes; 356 array<Attribute>? attributes;
386 357
387 // The value of the "MinVersion" attribute, if any. This field is 358 // The value of the "MinVersion" attribute, if any. This field is
388 // for convenience as it can also be parsed from the |attributes| field. 359 // for convenience as it can also be parsed from the |attributes| field.
389 int32 min_version = -1; // Negative value means unset. 360 int32 min_version = -1; // Negative value means unset.
390 361
391 string? short_name; // Some implementations may not provide names. 362 string? short_name; // Some implementations may not provide names.
392 363
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 // The the constant keys of constants declared in this namespace. 400 // The the constant keys of constants declared in this namespace.
430 array<string>? constants; 401 array<string>? constants;
431 }; 402 };
432 403
433 struct Attribute { 404 struct Attribute {
434 string key; 405 string key;
435 string value; 406 string value;
436 }; 407 };
437 408
438 409
OLDNEW
« no previous file with comments | « mojo/public/interfaces/bindings/mojom_files.mojom ('k') | mojo/public/tools/bindings/pylib/mojom/generate/mojom_translator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698