blob: 92b1dcb657c89b64c1607974401ee2718a43236f [file] [log] [blame] [view]
Viet-Trung Luu840291d2016-03-16 14:56:25 -07001# Mojom language reference
Viet-Trung Luuc7f9e252016-03-08 08:00:24 -08002
Viet-Trung Luu840291d2016-03-16 14:56:25 -07003This is a reference for the Mojom interface definition language (IDL). See
4[Mojom IDL](../intro/mojom_idl.md) for a shorter introduction.
Viet-Trung Luuc7f9e252016-03-08 08:00:24 -08005
Viet-Trung Luu840291d2016-03-16 14:56:25 -07006The Mojom language is ultimately about defining *types* (and things associated
7to types), including in particular *interface* types (hence "interface
8definition language"). It also allows "constant" values to be defined for some
9simple types, though this is mostly in support of the former role.
Viet-Trung Luuc7f9e252016-03-08 08:00:24 -080010
Viet-Trung Luu840291d2016-03-16 14:56:25 -070011## Mojom files
12
13Mojom files are Unicode text files, encoded in UTF-8. *Whitespace* (spaces,
14tabs, newlines, carriage returns) is not significant in Mojom files, except
15insofar as they separate tokens. Thus any consecutive sequence of whitespace
16characters may be replaced by a single whitespace character without any semantic
17change.
18
19### Comments
20
21There are two kinds of *comments*. Both are ignored, except that they too
22separate tokens (so any comment may be replaced by a single whitespace
23character).
24
25The first is the *single-line* (C++-style) comment. It is started by a `//`
26outside of a *string literal* and outside another comment and terminated by a
27newline. For example:
28```mojom
29// This is a comment.
30
31interface// This "separates" tokens.
32AnInterface {};
33
34const string kAConstString = "// This is not a comment.";
35
36[AnAttribute="// This is also not a comment either."]
37interface AnotherInterface {};
38```
39
40The second is the *multi-line* (C-style) comment. It is started by a `/*`
41outside of a string literal and terminated by a `*/` (anywhere!). For example:
42```mojom
43/* This is a
44 multi-line comment. */
45
46/* /* Comments don't nest. */
47
48/* // The "//" is meaningless here. */
49
50/* "This isn't a string literal. */
51
52interface/*This_separates_tokens*/AnInterface {};
53
54const string kAConstString = "/* This is not a comment. */";
55```
56
57### File structure
58
59Apart from comments and whitespace, a Mojom file consists of, in order:
60* an optional *module* declaration;
61* zero or more *import* statements (the order of these is not important); and
62* zero or more declarations of *struct*s, *interface*s, *union*s, *enum*s, or
63 *const*s (the order of these is not important).
64These elements will be described in following sections.
65
66As stated above, the order of struct/interface/union/enum/const declarations is
67not important. This is required to allow "cyclic" structures to be defined.
68Nonetheless, whenever possible, one should declare things before they are
69"used". For example, the following is valid but not recommended:
70```mojom
71// NOT recommended.
72
73const MyEnum kMyConst = kMyOtherConst;
74const MyEnum kMyOtherConst = A_VALUE;
75
76enum MyEnum {
77 A_VALUE,
78 ANOTHER_VALUE,
79};
80```
81
82## Names and identifiers
83
84*Names* in Mojom start with a letter (`a`-`z`, `A`-`Z`) and are followed by any
85number of letters, digits (`0`-`9`), or underscores (`_`). For example:
86`MyThing`, `MyThing123`, `MyThing_123`, `my_thing`, `myThing`, `MY_THING`. (See
87below for naming conventions, however.)
88
89Various things in Mojom are named (i.e., assigned names):
90* types (e.g., interfaces, structs, unions, and enums),
91* things associated with particular types (e.g., messages in interfaces,
92 parameters in messages, fields in structs and unions, and values in enums),
93 and
94* const values.
95
96*Identifiers* consist of one or more names, separated by `.`. These are used in
97module declarations, as well as in referencing other named things.
98
99### Namespacing and name resolution
100
101As mentioned above, not only are types named, but things associated with a given
102type may be named. For example, consider:
103```mojom
104enum MyEnum {
105 A_VALUE,
106 ANOTHER_VALUE,
107 A_DUPLICATE_VALUE = A_VALUE,
108};
109```
110`MyEnum` is the name of an enum type, and `A_VALUE` is the name of a value of
111`MyEnum`. Within the *scope* of `MyEnum` (or where that scope is implied),
112`A_VALUE` may be used without additional qualification. Outside that scope, it
113may be referred to using the identifier `MyEnum.A_VALUE`.
114
115Some type definitions allow (some) other type definitions to be nested within.
116For example:
117```mojom
118struct MyStruct {
119 enum MyEnum {
120 A_VALUE,
121 };
122
123 MyEnum my_field1 = A_VALUE;
124 MyStruct.MyEnum my_field2 = MyStruct.MyEnum.A_VALUE;
125};
126```
127Within `MyStruct`, `MyEnum` may be referred to without qualification (e.g., to
128define the field `my_field1`). Outside, it may be referred to using the
129identifier `MyStruct.MyEnum`. Notice that `my_field1` is assigned a default
130value of `A_VALUE`, which is resolved correctly since there is an implied scope
131of `MyEnum`. It would also be legal to write the default value as
132`MyEnum.A_VALUE` or even `MyStruct.MyEnum.A_VALUE`, as is done for `my_field2`.
133
134Thus names live in a name hierarchy, with the "enclosing" scopes often being
135other type names. Additionally, *module names* (see below) can be used to define
136artificial outermost scopes.
137
138Names (or, more properly, identifiers) are resolved in a C++-like way: Scopes
139are searched from inside outwards, i.e., starting with the current, innermost
140scope and then working outwards.
141
142### Standard naming style
143
144Though Mojom allows arbitrary names, as indicated above, there are standard
145stylistic conventions for naming different things. Code generators for different
146languages typically expect these styles to be followed, since they will often
147convert the standard style to one appropriate for their target language. Thus
148following the standard style is highly recommended.
149
150The standard styles are (getting ahead of ourselves slightly):
151* `StudlyCaps` (i.e., concatenated capitalized words), used for user-defined
152 (struct, interface, union, enum) type names and message (a.k.a. function or
153 method) names;
154* `unix_hacker_style` (i.e., lowercase words joined by underscores), used for
155 field (a.k.a. "parameter" for messages) names in structs, unions, and
156 messages;
157* `ALL_CAPS_UNIX_HACKER_STYLE` (i.e., uppercase words joined by underscores),
158 used for enum value names; and
159* `kStudlyCaps` (i.e., `k` followed by concatenated capitalized words), used for
160 const names.
161
162## Module statements
163
164The Mojom *module* statement is a way of logically grouping Mojom declarations.
165For example:
166```mojom
167module my_module;
168```
169Mojom modules are similar to C++ namespaces (and the standard C++ code generator
170would put generated code into the `my_module` namespace), in that there is no
171implication that the file contains the entirety of the "module" definition;
172multiple files may have the same module statement. (There is also no requirement
173that the module name have anything to do with the file path containing the Mojom
174file.)
175
176The specified Mojom module name is an identifier: it can consist of multiple
177parts separated by `.`. For example:
178```mojom
179module my_module.my_submodule;
180
181struct MyStruct {
182};
183```
184Recall that name look-up is similar to C++: E.g., if the current module is
185`my_module.my_submodule` then `MyStruct`, `my_submodule.MyStruct`, and
186`my_module.my_submodule.MyStruct` all refer to the above struct, whereas if the
187current module is just `my_module` then only the latter two do.
188
189Note that "module name" is really a misnomer, since Mojom does not actually
190define modules per se. Instead, as suggested above, module names play only a
191namespacing role, defining the "root" namespace for the contents of a file.
192
193## Import statements
194
195An *import* statement makes the declarations from another Mojom file available
196in the current Mojom file. Moreover, it operates transitively, in that it also
197makes the imports of the imported file available, etc. The "argument" to the
198import statement is a string literal that is interpreted as the path to the file
199to import. Tools that work with Mojom files are typically provided with a search
200path for importing files (just as a C++ compiler can be provided with an
201"include path"), for the purposes of resolving these paths. (**TODO(vtl)**: This
202always includes the current Mojom file's path, right? Is the current path the
203first path that's searched?)
204
205For example:
206```mojom
207module my_module;
208
209import "path/to/another.mojom";
210import "path/to/yet/a/different.mojom";
211```
212This makes the contents of the two specified Mojom files available, together
213with whatever they import, transitively. (Names are resolved in the way
214described in the previous section.)
215
216Import cycles are not permitted (so, e.g., it would be an error if
217`path/to/another.mojom` imported the current Mojom file). However, it is
218entirely valid for Mojom files to be imported (transitively) multiple times
219(e.g., it is fine for `path/to/another.mojom` to also import
220`path/to/yet/a/different.mojom`).
221
222## Types in Mojom
223
Viet-Trung Luu93a38a22016-03-18 15:26:52 -0700224*Types* in Mojom are really only used in two ways:
225* first, in declaring additional types (recall that the Mojom language is nearly
226 entirely about defining types!); and
227* second, in declaring const values.
228The first way really covers a lot of ground, however. Type identifiers (i.e.,
229identifiers that resolve to some type definition) may occur in:
230* field declarations within struct and union declarations;
231* in message declarations (in both request and response parameters) in interface
232 declarations (this is really very similar to the use in struct field
233 declarations); and
234* in "composite" type specifiers (e.g., to specify an array of a given type).
235
Viet-Trung Luu840291d2016-03-16 14:56:25 -0700236### Reference and non-reference types
237
Viet-Trung Luu93a38a22016-03-18 15:26:52 -0700238There are two basic classes of types, *reference* types and *non-reference*
239types. The latter class is easier to understand, consisting of the built-in
240number (integer and floating-point) types, as well as user-defined enum types.
241All other types are reference types: they have some notion of *null* (i.e.,
242non-presence).
243
244#### Nullability
245
246When an identifier is used (in another type definition, including in message
247parameters) to refer to a reference type, by default the instance of the type is
248taken to be *non-nullable*, i.e., required to not be null. One may allow that
249instance to be null (i.e., specify a *nullable* instance) by appending `?` to
250the identifier. For example, if `Foo` is a reference type:
251```mojom
252struct MyStruct {
253 Foo foo1;
254 Foo? foo2;
255};
256```
257In an instance of `MyStruct`, the `foo1` field may never be null while the
258`foo2` field may be null.
259
260This also applies to composite type specifiers. For example:
261* `array<Foo>` is a non-nullable array of non-nullable `Foo` (the array itself
262 may not be null, nor can any element of the array);
263* `array<Foo?>` is a non-nullable array of nullable `Foo` (the array itself may
264 not be null, but any element of the array may be null);
265* `array<Foo>?` is a nullable array of non-nullable `Foo`; and
266* `array<Foo?>?` is a nullable array of nullable `Foo`.
267(See below for details on arrays.)
268
Viet-Trung Luu840291d2016-03-16 14:56:25 -0700269### Built-in types
270
Viet-Trung Luu93a38a22016-03-18 15:26:52 -0700271#### Simple types
Viet-Trung Luu840291d2016-03-16 14:56:25 -0700272
Viet-Trung Luu93a38a22016-03-18 15:26:52 -0700273#### Strings
Viet-Trung Luu840291d2016-03-16 14:56:25 -0700274
Viet-Trung Luu93a38a22016-03-18 15:26:52 -0700275#### Arrays
276
277#### Maps
278
279#### Raw handle types
Viet-Trung Luu840291d2016-03-16 14:56:25 -0700280
281
282**TODO(vtl)**: The stuff below is old stuff to be reorganized/rewritten.
Viet-Trung Luuc7f9e252016-03-08 08:00:24 -0800283
284## Interfaces
285
286A Mojom *interface* is (typically) used to describe communication on a message
287pipe. Typically, message pipes are created with a particular interface in mind,
288with one endpoint designated the *client* (which sends *request* messages and
289receives *response* messages) and the other designed that *server* or *impl*
290(which receives request messages and sends response messages).
291
292For example, take the following Mojom interface declaration:
293```mojom
294interface MyInterface {
295 Foo(int32 a, string b);
296 Bar() => (bool x, uint32 y);
297 Baz() => ();
298};
299```
300This specifies a Mojom interface in which the client may send three types of
301messages, namely `Foo`, `Bar`, and `Baz` (see the note below about names in
302Mojom). The first does not have a response message defined, whereas the latter
303two do. Whenever the server receives a `Bar` or `Baz` message, it *must*
304(eventually) send a (single) corresponding response message.
305
306The `Foo` request message contains two pieces of data: a signed (two's
307complement) 32-bit integer called `a` and a Unicode string called `b`. On the
308"wire", the message basically consists of metadata and a (serialized) *struct*
309(see below) containing `a` and `b`.
310
311The `Bar` request message contains no data, so on the wire it's just metadata
312and an empty struct. It has a response message, containing a boolean value `x`
313and an unsigned 32-bit integer `y`, which on the wire consists of metadata and a
314struct with `x` and `y`. Each time the server receives a `Bar` message, it is
315supposed to (eventually) respond by sending the response message. (Note: The
316client may include as part of the request message's metadata an identifier for
317the request; the response's metadata will then include this identifier, allowing
318it to match responses to requests.)
319
320The `Baz` request message also contains no data. It requires a response, also
321containing no data. Note that even though the response has no data, a response
322message must nonetheless be sent, functioning as an "ack". (Thus this is
323different from not having a response, as was the case for `Foo`.)
324
325## Structs
326
327Mojom defines a way of serializing data structures (with the Mojom IDL providing
328a way of specifying those data structures). A Mojom *struct* is the basic unit
329of serialization. As we saw above, messages are basically just structs, with a
330small amount of additional metadata.
331
332Here is a simple example of a struct declaration:
333```mojom
334struct MyStruct {
335 int32 a;
336 string b;
337};
338```
339We will discuss in greater detail how structs are declared later.
340
341### Names in Mojom
342
343Names in Mojom are not important. Except in affecting compatibility at the level
344of source code (when generating bindings), names in a Mojom file may be changed
345arbitrarily without any effect on the "meaning" of the Mojom file (subject to
346basic language requirements, e.g., avoiding collisions with keywords and other
347names). E.g., the following is completely equivalent to the interface discussed
348above:
349```mojom
350interface Something {
351 One(int32 an_integer, string a_string);
352 Two() => (bool a_boolean, uint32 an_unsigned);
353 Three() => ();
354};
355```
356The `Something` interface is compatible at a binary level with `MyInterface`. A
357client using the `Something` interface may communicate with a server
358implementing the `MyInterface` with no issues, and vice versa.
359
360The reason for this is that elements (messages, parameters, struct members,
361etc.) are actually identified by *ordinal* value. They may be specified
362explicitly (using `@123` notation; see below). If they are not specified
363explicitly, they are automatically assigned. (The ordinal values for each
364interface/struct/etc. must assign distinct values for each item, in a
365consecutive range starting at 0.)
366
367Explicitly assigning ordinals allows Mojom files to be rearranged "physically"
368without changing their meaning. E.g., perhaps one would write:
369```mojom
370interface MyInterface {
371 Bar@1() => (bool x@0, uint32 y@1);
372 Baz@2() => ();
373
374 // Please don't use this in new code!
375 FooDeprecated@0(int32 a@0, string b@1);
376};
377```
378
379Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which
380allows Mojom files to be evolved in a backwards-compatible way. We will not
381discuss this matter further here.
382
383**TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g.,
384`ServiceName`).
385
Viet-Trung Luuc7f9e252016-03-08 08:00:24 -0800386### Struct declarations
387
388A Mojom *struct* declaration consists of a finite sequence of *field
389declaration*, each of which consists of a *type*, a *name*, and optionally a
390*default value* (if applicable for the given type). (If no default value is
391declared, then the default is the default value for the field type, typically 0,
392null, or similar.)
393
394Additionally, a struct may contain enum and const declarations (**TODO(vtl)**:
395why not struct/union/interface declarations?). While the order of the field
396declarations (with respect to one another) is important, the ordering of the
397enum/const declarations (with respect to both the field declarations and other
398enum/const declarations) is not. (But as before, we recommend declaring things
399before "use".)
400
401Here is an example with these elements:
402```mojom
403struct Foo {
404 const int8 kSomeConstant = 123;
405
406 enum MyEnum {
407 A_VALUE,
408 ANOTHER_VALUE
409 };
410
411 int8 first_field = kSomeConstant;
412 uint32 second_field = 123;
413 MyEnum etc_etc = A_VALUE;
414 float a; // Default value is 0.
415 string? b; // Default value is null.
416};
417```
418(Note that `kSomeConstant` may be referred to as `Foo.kSomeConstant` and,
419similarly, `MyEnum` as `Foo.MyEnum`. This is required outside of the `Foo`
420declaration.)
421
422### Interface declarations
423
424**TODO(vtl)**
425
426### Union declarations
427
428**TODO(vtl)**
429
430### Enum declarations
431
432**TODO(vtl)**
433
434### Const declarations
435
436**TODO(vtl)**
437
438**TODO(vtl)**: Write/(re)organize the sections below.
439
440## Data types
441
442### Primitive types
443
444#### Standard types
445
446#### Enum types
447
448### "Pointer" types
449
450#### Nullability
451
452#### Strings
453
454#### Maps
455
456#### Structs
457
458#### Arrays
459
460#### Unions
461
462### Handle types
463
464#### Raw handle types
465
466#### Interface types
467
468#### Interface request types
469
470## Annotations
471
472## Pipelining