Viet-Trung Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 1 | # Mojom language reference |
Viet-Trung Luu | c7f9e25 | 2016-03-08 08:00:24 -0800 | [diff] [blame] | 2 | |
Viet-Trung Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 3 | This is a reference for the Mojom interface definition language (IDL). See |
| 4 | [Mojom IDL](../intro/mojom_idl.md) for a shorter introduction. |
Viet-Trung Luu | c7f9e25 | 2016-03-08 08:00:24 -0800 | [diff] [blame] | 5 | |
Viet-Trung Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 6 | The Mojom language is ultimately about defining *types* (and things associated |
| 7 | to types), including in particular *interface* types (hence "interface |
| 8 | definition language"). It also allows "constant" values to be defined for some |
| 9 | simple types, though this is mostly in support of the former role. |
Viet-Trung Luu | c7f9e25 | 2016-03-08 08:00:24 -0800 | [diff] [blame] | 10 | |
Viet-Trung Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 11 | ## Mojom files |
| 12 | |
| 13 | Mojom files are Unicode text files, encoded in UTF-8. *Whitespace* (spaces, |
| 14 | tabs, newlines, carriage returns) is not significant in Mojom files, except |
| 15 | insofar as they separate tokens. Thus any consecutive sequence of whitespace |
| 16 | characters may be replaced by a single whitespace character without any semantic |
| 17 | change. |
| 18 | |
| 19 | ### Comments |
| 20 | |
| 21 | There are two kinds of *comments*. Both are ignored, except that they too |
| 22 | separate tokens (so any comment may be replaced by a single whitespace |
| 23 | character). |
| 24 | |
| 25 | The first is the *single-line* (C++-style) comment. It is started by a `//` |
| 26 | outside of a *string literal* and outside another comment and terminated by a |
| 27 | newline. For example: |
| 28 | ```mojom |
| 29 | // This is a comment. |
| 30 | |
| 31 | interface// This "separates" tokens. |
| 32 | AnInterface {}; |
| 33 | |
| 34 | const string kAConstString = "// This is not a comment."; |
| 35 | |
| 36 | [AnAttribute="// This is also not a comment either."] |
| 37 | interface AnotherInterface {}; |
| 38 | ``` |
| 39 | |
| 40 | The second is the *multi-line* (C-style) comment. It is started by a `/*` |
| 41 | outside 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 | |
| 52 | interface/*This_separates_tokens*/AnInterface {}; |
| 53 | |
| 54 | const string kAConstString = "/* This is not a comment. */"; |
| 55 | ``` |
| 56 | |
| 57 | ### File structure |
| 58 | |
| 59 | Apart 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). |
| 64 | These elements will be described in following sections. |
| 65 | |
| 66 | As stated above, the order of struct/interface/union/enum/const declarations is |
| 67 | not important. This is required to allow "cyclic" structures to be defined. |
| 68 | Nonetheless, 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 | |
| 73 | const MyEnum kMyConst = kMyOtherConst; |
| 74 | const MyEnum kMyOtherConst = A_VALUE; |
| 75 | |
| 76 | enum 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 |
| 85 | number of letters, digits (`0`-`9`), or underscores (`_`). For example: |
| 86 | `MyThing`, `MyThing123`, `MyThing_123`, `my_thing`, `myThing`, `MY_THING`. (See |
| 87 | below for naming conventions, however.) |
| 88 | |
| 89 | Various 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 |
| 97 | module declarations, as well as in referencing other named things. |
| 98 | |
| 99 | ### Namespacing and name resolution |
| 100 | |
| 101 | As mentioned above, not only are types named, but things associated with a given |
| 102 | type may be named. For example, consider: |
| 103 | ```mojom |
| 104 | enum 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 |
| 113 | may be referred to using the identifier `MyEnum.A_VALUE`. |
| 114 | |
| 115 | Some type definitions allow (some) other type definitions to be nested within. |
| 116 | For example: |
| 117 | ```mojom |
| 118 | struct 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 | ``` |
| 127 | Within `MyStruct`, `MyEnum` may be referred to without qualification (e.g., to |
| 128 | define the field `my_field1`). Outside, it may be referred to using the |
| 129 | identifier `MyStruct.MyEnum`. Notice that `my_field1` is assigned a default |
| 130 | value of `A_VALUE`, which is resolved correctly since there is an implied scope |
| 131 | of `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 | |
| 134 | Thus names live in a name hierarchy, with the "enclosing" scopes often being |
| 135 | other type names. Additionally, *module names* (see below) can be used to define |
| 136 | artificial outermost scopes. |
| 137 | |
| 138 | Names (or, more properly, identifiers) are resolved in a C++-like way: Scopes |
| 139 | are searched from inside outwards, i.e., starting with the current, innermost |
| 140 | scope and then working outwards. |
| 141 | |
| 142 | ### Standard naming style |
| 143 | |
| 144 | Though Mojom allows arbitrary names, as indicated above, there are standard |
| 145 | stylistic conventions for naming different things. Code generators for different |
| 146 | languages typically expect these styles to be followed, since they will often |
| 147 | convert the standard style to one appropriate for their target language. Thus |
| 148 | following the standard style is highly recommended. |
| 149 | |
| 150 | The 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 | |
| 164 | The Mojom *module* statement is a way of logically grouping Mojom declarations. |
| 165 | For example: |
| 166 | ```mojom |
| 167 | module my_module; |
| 168 | ``` |
| 169 | Mojom modules are similar to C++ namespaces (and the standard C++ code generator |
| 170 | would put generated code into the `my_module` namespace), in that there is no |
| 171 | implication that the file contains the entirety of the "module" definition; |
| 172 | multiple files may have the same module statement. (There is also no requirement |
| 173 | that the module name have anything to do with the file path containing the Mojom |
| 174 | file.) |
| 175 | |
| 176 | The specified Mojom module name is an identifier: it can consist of multiple |
| 177 | parts separated by `.`. For example: |
| 178 | ```mojom |
| 179 | module my_module.my_submodule; |
| 180 | |
| 181 | struct MyStruct { |
| 182 | }; |
| 183 | ``` |
| 184 | Recall 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 |
| 187 | current module is just `my_module` then only the latter two do. |
| 188 | |
| 189 | Note that "module name" is really a misnomer, since Mojom does not actually |
| 190 | define modules per se. Instead, as suggested above, module names play only a |
| 191 | namespacing role, defining the "root" namespace for the contents of a file. |
| 192 | |
| 193 | ## Import statements |
| 194 | |
| 195 | An *import* statement makes the declarations from another Mojom file available |
| 196 | in the current Mojom file. Moreover, it operates transitively, in that it also |
| 197 | makes the imports of the imported file available, etc. The "argument" to the |
| 198 | import statement is a string literal that is interpreted as the path to the file |
| 199 | to import. Tools that work with Mojom files are typically provided with a search |
| 200 | path 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 |
| 202 | always includes the current Mojom file's path, right? Is the current path the |
| 203 | first path that's searched?) |
| 204 | |
| 205 | For example: |
| 206 | ```mojom |
| 207 | module my_module; |
| 208 | |
| 209 | import "path/to/another.mojom"; |
| 210 | import "path/to/yet/a/different.mojom"; |
| 211 | ``` |
| 212 | This makes the contents of the two specified Mojom files available, together |
| 213 | with whatever they import, transitively. (Names are resolved in the way |
| 214 | described in the previous section.) |
| 215 | |
| 216 | Import 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 |
| 218 | entirely 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 Luu | 93a38a2 | 2016-03-18 15:26:52 -0700 | [diff] [blame] | 224 | *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. |
| 228 | The first way really covers a lot of ground, however. Type identifiers (i.e., |
| 229 | identifiers 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 Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 236 | ### Reference and non-reference types |
| 237 | |
Viet-Trung Luu | 93a38a2 | 2016-03-18 15:26:52 -0700 | [diff] [blame] | 238 | There are two basic classes of types, *reference* types and *non-reference* |
| 239 | types. The latter class is easier to understand, consisting of the built-in |
| 240 | number (integer and floating-point) types, as well as user-defined enum types. |
| 241 | All other types are reference types: they have some notion of *null* (i.e., |
| 242 | non-presence). |
| 243 | |
| 244 | #### Nullability |
| 245 | |
| 246 | When an identifier is used (in another type definition, including in message |
| 247 | parameters) to refer to a reference type, by default the instance of the type is |
| 248 | taken to be *non-nullable*, i.e., required to not be null. One may allow that |
| 249 | instance to be null (i.e., specify a *nullable* instance) by appending `?` to |
| 250 | the identifier. For example, if `Foo` is a reference type: |
| 251 | ```mojom |
| 252 | struct MyStruct { |
| 253 | Foo foo1; |
| 254 | Foo? foo2; |
| 255 | }; |
| 256 | ``` |
| 257 | In an instance of `MyStruct`, the `foo1` field may never be null while the |
| 258 | `foo2` field may be null. |
| 259 | |
| 260 | This 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 Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 269 | ### Built-in types |
| 270 | |
Viet-Trung Luu | 93a38a2 | 2016-03-18 15:26:52 -0700 | [diff] [blame] | 271 | #### Simple types |
Viet-Trung Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 272 | |
Viet-Trung Luu | 93a38a2 | 2016-03-18 15:26:52 -0700 | [diff] [blame] | 273 | #### Strings |
Viet-Trung Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 274 | |
Viet-Trung Luu | 93a38a2 | 2016-03-18 15:26:52 -0700 | [diff] [blame] | 275 | #### Arrays |
| 276 | |
| 277 | #### Maps |
| 278 | |
| 279 | #### Raw handle types |
Viet-Trung Luu | 840291d | 2016-03-16 14:56:25 -0700 | [diff] [blame] | 280 | |
| 281 | |
| 282 | **TODO(vtl)**: The stuff below is old stuff to be reorganized/rewritten. |
Viet-Trung Luu | c7f9e25 | 2016-03-08 08:00:24 -0800 | [diff] [blame] | 283 | |
| 284 | ## Interfaces |
| 285 | |
| 286 | A Mojom *interface* is (typically) used to describe communication on a message |
| 287 | pipe. Typically, message pipes are created with a particular interface in mind, |
| 288 | with one endpoint designated the *client* (which sends *request* messages and |
| 289 | receives *response* messages) and the other designed that *server* or *impl* |
| 290 | (which receives request messages and sends response messages). |
| 291 | |
| 292 | For example, take the following Mojom interface declaration: |
| 293 | ```mojom |
| 294 | interface MyInterface { |
| 295 | Foo(int32 a, string b); |
| 296 | Bar() => (bool x, uint32 y); |
| 297 | Baz() => (); |
| 298 | }; |
| 299 | ``` |
| 300 | This specifies a Mojom interface in which the client may send three types of |
| 301 | messages, namely `Foo`, `Bar`, and `Baz` (see the note below about names in |
| 302 | Mojom). The first does not have a response message defined, whereas the latter |
| 303 | two do. Whenever the server receives a `Bar` or `Baz` message, it *must* |
| 304 | (eventually) send a (single) corresponding response message. |
| 305 | |
| 306 | The `Foo` request message contains two pieces of data: a signed (two's |
| 307 | complement) 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 | |
| 311 | The `Bar` request message contains no data, so on the wire it's just metadata |
| 312 | and an empty struct. It has a response message, containing a boolean value `x` |
| 313 | and an unsigned 32-bit integer `y`, which on the wire consists of metadata and a |
| 314 | struct with `x` and `y`. Each time the server receives a `Bar` message, it is |
| 315 | supposed to (eventually) respond by sending the response message. (Note: The |
| 316 | client may include as part of the request message's metadata an identifier for |
| 317 | the request; the response's metadata will then include this identifier, allowing |
| 318 | it to match responses to requests.) |
| 319 | |
| 320 | The `Baz` request message also contains no data. It requires a response, also |
| 321 | containing no data. Note that even though the response has no data, a response |
| 322 | message must nonetheless be sent, functioning as an "ack". (Thus this is |
| 323 | different from not having a response, as was the case for `Foo`.) |
| 324 | |
| 325 | ## Structs |
| 326 | |
| 327 | Mojom defines a way of serializing data structures (with the Mojom IDL providing |
| 328 | a way of specifying those data structures). A Mojom *struct* is the basic unit |
| 329 | of serialization. As we saw above, messages are basically just structs, with a |
| 330 | small amount of additional metadata. |
| 331 | |
| 332 | Here is a simple example of a struct declaration: |
| 333 | ```mojom |
| 334 | struct MyStruct { |
| 335 | int32 a; |
| 336 | string b; |
| 337 | }; |
| 338 | ``` |
| 339 | We will discuss in greater detail how structs are declared later. |
| 340 | |
| 341 | ### Names in Mojom |
| 342 | |
| 343 | Names in Mojom are not important. Except in affecting compatibility at the level |
| 344 | of source code (when generating bindings), names in a Mojom file may be changed |
| 345 | arbitrarily without any effect on the "meaning" of the Mojom file (subject to |
| 346 | basic language requirements, e.g., avoiding collisions with keywords and other |
| 347 | names). E.g., the following is completely equivalent to the interface discussed |
| 348 | above: |
| 349 | ```mojom |
| 350 | interface Something { |
| 351 | One(int32 an_integer, string a_string); |
| 352 | Two() => (bool a_boolean, uint32 an_unsigned); |
| 353 | Three() => (); |
| 354 | }; |
| 355 | ``` |
| 356 | The `Something` interface is compatible at a binary level with `MyInterface`. A |
| 357 | client using the `Something` interface may communicate with a server |
| 358 | implementing the `MyInterface` with no issues, and vice versa. |
| 359 | |
| 360 | The reason for this is that elements (messages, parameters, struct members, |
| 361 | etc.) are actually identified by *ordinal* value. They may be specified |
| 362 | explicitly (using `@123` notation; see below). If they are not specified |
| 363 | explicitly, they are automatically assigned. (The ordinal values for each |
| 364 | interface/struct/etc. must assign distinct values for each item, in a |
| 365 | consecutive range starting at 0.) |
| 366 | |
| 367 | Explicitly assigning ordinals allows Mojom files to be rearranged "physically" |
| 368 | without changing their meaning. E.g., perhaps one would write: |
| 369 | ```mojom |
| 370 | interface 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 | |
| 379 | Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which |
| 380 | allows Mojom files to be evolved in a backwards-compatible way. We will not |
| 381 | discuss this matter further here. |
| 382 | |
| 383 | **TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g., |
| 384 | `ServiceName`). |
| 385 | |
Viet-Trung Luu | c7f9e25 | 2016-03-08 08:00:24 -0800 | [diff] [blame] | 386 | ### Struct declarations |
| 387 | |
| 388 | A Mojom *struct* declaration consists of a finite sequence of *field |
| 389 | declaration*, 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 |
| 391 | declared, then the default is the default value for the field type, typically 0, |
| 392 | null, or similar.) |
| 393 | |
| 394 | Additionally, a struct may contain enum and const declarations (**TODO(vtl)**: |
| 395 | why not struct/union/interface declarations?). While the order of the field |
| 396 | declarations (with respect to one another) is important, the ordering of the |
| 397 | enum/const declarations (with respect to both the field declarations and other |
| 398 | enum/const declarations) is not. (But as before, we recommend declaring things |
| 399 | before "use".) |
| 400 | |
| 401 | Here is an example with these elements: |
| 402 | ```mojom |
| 403 | struct 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, |
| 419 | similarly, `MyEnum` as `Foo.MyEnum`. This is required outside of the `Foo` |
| 420 | declaration.) |
| 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 |