Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 **TODO(vtl)**: Reorganize this to be properly structured. | 1 # Mojom language reference |
| 2 | 2 |
| 3 # Mojom IDL | 3 This is a reference for the Mojom interface definition language (IDL). See |
| 4 | 4 [Mojom IDL](../intro/mojom_idl.md) for a shorter introduction. |
| 5 The Mojom IDL (interface definition language) is primarily used to describe | 5 |
| 6 *interfaces* to be used on [message pipes](message_pipes.md). Below, we describe | 6 The Mojom language is ultimately about defining *types* (and things associated |
| 7 practical aspects of the Mojom language. Elsewhere, we describe the [Mojom | 7 to types), including in particular *interface* types (hence "interface |
| 8 protocol](mojom_protocol.md). (**TODO(vtl)**: Also, serialization format? | 8 definition language"). It also allows "constant" values to be defined for some |
| 9 Versioning?) | 9 simple types, though this is mostly in support of the former role. |
| 10 | 10 |
| 11 Text files written in Mojom IDL are given the `.mojom` extension by convention | 11 ## Mojom files |
| 12 (and are usually referred to as Mojom/mojom/`.mojom` files). The Mojom bindings | 12 |
| 13 generator (**TODO(vtl)**: link?) may be used to generate code in a variety of | 13 Mojom files are Unicode text files, encoded in UTF-8. *Whitespace* (spaces, |
| 14 languages (including C++, Dart, and Go) from a Mojom file. Such generated code | 14 tabs, newlines, carriage returns) is not significant in Mojom files, except |
| 15 "implements" the things specified in the Mojom file, in a way that's appropriate | 15 insofar as they separate tokens. Thus any consecutive sequence of whitespace |
| 16 for the particular target language. | 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 | |
|
vardhan
2016/03/16 20:17:29
rudominer@ should be able to answer this TODO's qu
rudominer
2016/03/16 21:07:34
The comment from parser_driver.go says:
// We att
| |
| 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 | |
| 224 ### Reference and non-reference types | |
| 225 | |
| 226 ### Built-in types | |
| 227 | |
| 228 ### Simple types | |
| 229 | |
| 230 ### Arrays | |
| 231 | |
| 232 ### Maps | |
| 233 | |
| 234 | |
| 235 **TODO(vtl)**: The stuff below is old stuff to be reorganized/rewritten. | |
| 17 | 236 |
| 18 ## Interfaces | 237 ## Interfaces |
| 19 | 238 |
| 20 A Mojom *interface* is (typically) used to describe communication on a message | 239 A Mojom *interface* is (typically) used to describe communication on a message |
| 21 pipe. Typically, message pipes are created with a particular interface in mind, | 240 pipe. Typically, message pipes are created with a particular interface in mind, |
| 22 with one endpoint designated the *client* (which sends *request* messages and | 241 with one endpoint designated the *client* (which sends *request* messages and |
| 23 receives *response* messages) and the other designed that *server* or *impl* | 242 receives *response* messages) and the other designed that *server* or *impl* |
| 24 (which receives request messages and sends response messages). | 243 (which receives request messages and sends response messages). |
| 25 | 244 |
| 26 For example, take the following Mojom interface declaration: | 245 For example, take the following Mojom interface declaration: |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 }; | 329 }; |
| 111 ``` | 330 ``` |
| 112 | 331 |
| 113 Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which | 332 Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which |
| 114 allows Mojom files to be evolved in a backwards-compatible way. We will not | 333 allows Mojom files to be evolved in a backwards-compatible way. We will not |
| 115 discuss this matter further here. | 334 discuss this matter further here. |
| 116 | 335 |
| 117 **TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g., | 336 **TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g., |
| 118 `ServiceName`). | 337 `ServiceName`). |
| 119 | 338 |
| 120 ## Mojom files | |
| 121 | |
| 122 A Mojom file consists of, in order: | |
| 123 * an optional *module* declaration; | |
| 124 * zero or more *import* statements (the order of these is not important); and | |
| 125 * zero or more declarations of *struct*s, *interface*s, *union*s, *enum*s, or | |
| 126 *const*s (the order of these is not important). | |
| 127 (These are all described further below.) | |
| 128 | |
| 129 Additionally, C/C++-style comments are supported (i.e., single-line comments | |
| 130 starting with `//` or multi-line comments of the form `/* ... */`). | |
| 131 | |
| 132 As stated above, the order of struct/interface/union/enum/const declarations is | |
| 133 not important. This is required to allow "cyclic" structures to be defined. | |
| 134 Nonetheless, whenever possible, one should declare things before they are | |
| 135 "used". For example, the following is valid but not recommended: | |
| 136 ```mojom | |
| 137 // NOT recommended. | |
| 138 | |
| 139 const MyEnum kMyConst = kMyOtherConst; | |
| 140 const MyEnum kMyOtherConst = A_VALUE; | |
| 141 | |
| 142 enum MyEnum { | |
| 143 A_VALUE, | |
| 144 ANOTHER_VALUE, | |
| 145 }; | |
| 146 ``` | |
| 147 | |
| 148 ### Naming style | |
| 149 | |
| 150 There is a standard style for naming things: | |
| 151 * `StudlyCaps` (a.k.a. `CapitalizedCamelCase`) for: (struct, interface, union, | |
| 152 and enum) type names and message (a.k.a. function or method) names; | |
| 153 * `unix_hacker_style` for field names (in structs and unions) and "parameter" | |
| 154 names; | |
| 155 * `ALL_CAPS_UNIX_HACKER_STYLE` for enum value names; and | |
| 156 * `kStudlyCaps` for const names. | |
| 157 | |
| 158 Following this style is highly recommended, since code generators for various | |
| 159 languages will expect this style, in order to transform the names into a more | |
| 160 language-appropriate style. | |
| 161 | |
| 162 ### Module statement | |
| 163 | |
| 164 The Mojom *module* statement is just a way of logically grouping Mojom | |
| 165 declarations. 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" definiton; | |
| 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 Mojom module names are hierarchical in that they can be composed of multiple | |
| 177 parts separated by `.`. For example: | |
| 178 ```mojom | |
| 179 module my_module.my_submodule; | |
| 180 | |
| 181 struct MyStruct { | |
| 182 }; | |
| 183 ``` | |
| 184 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 ### Import declarations | |
| 190 | |
| 191 An *import* declaration makes the declarations from another Mojom file available | |
| 192 in the current Mojom file. Moreover, it operates transitively, in the sense that | |
| 193 it also makes the imports of the imported file available, etc. The "argument" to | |
| 194 the import statement is a path to a file. Tools that work with Mojom files are | |
| 195 typically provided with a search path for importing files (just as a C++ | |
| 196 compiler can be provided with an "include path"), for the purposes of resolving | |
| 197 these paths. (**TODO(vtl)**: This always includes the current Mojom file's path, | |
| 198 right? Is the current path the first path that's searched?) | |
| 199 | |
| 200 For example: | |
| 201 ```mojom | |
| 202 module my_module; | |
| 203 | |
| 204 import "path/to/another.mojom"; | |
| 205 import "path/to/yet/a/different.mojom"; | |
| 206 ``` | |
| 207 This makes the contents of the two mentioned Mojom files available, together | |
| 208 with whatever they import, transitively. (Note that names are resolved in the | |
| 209 way described in the previous section.) | |
| 210 | |
| 211 Import cycles are not permitted (so, e.g., it would be an error if | |
| 212 `path/to/another.mojom` imported the current Mojom file). However, it is | |
| 213 entirely valid for Mojom files to be imported (transitively) multiple times | |
| 214 (e.g., it is fine for `path/to/another.mojom` to also import | |
| 215 `path/to/yet/a/different.mojom`). | |
| 216 | |
| 217 ### Struct declarations | 339 ### Struct declarations |
| 218 | 340 |
| 219 A Mojom *struct* declaration consists of a finite sequence of *field | 341 A Mojom *struct* declaration consists of a finite sequence of *field |
| 220 declaration*, each of which consists of a *type*, a *name*, and optionally a | 342 declaration*, each of which consists of a *type*, a *name*, and optionally a |
| 221 *default value* (if applicable for the given type). (If no default value is | 343 *default value* (if applicable for the given type). (If no default value is |
| 222 declared, then the default is the default value for the field type, typically 0, | 344 declared, then the default is the default value for the field type, typically 0, |
| 223 null, or similar.) | 345 null, or similar.) |
| 224 | 346 |
| 225 Additionally, a struct may contain enum and const declarations (**TODO(vtl)**: | 347 Additionally, a struct may contain enum and const declarations (**TODO(vtl)**: |
| 226 why not struct/union/interface declarations?). While the order of the field | 348 why not struct/union/interface declarations?). While the order of the field |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 | 416 |
| 295 #### Raw handle types | 417 #### Raw handle types |
| 296 | 418 |
| 297 #### Interface types | 419 #### Interface types |
| 298 | 420 |
| 299 #### Interface request types | 421 #### Interface request types |
| 300 | 422 |
| 301 ## Annotations | 423 ## Annotations |
| 302 | 424 |
| 303 ## Pipelining | 425 ## Pipelining |
| OLD | NEW |