| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 # | 5 # |
| 6 # This module is responsible for translating a MojomFileGraph (see | 6 # This module is responsible for translating a MojomFileGraph (see |
| 7 # mojom_files.mojom) to one or more module.Module. | 7 # mojom_files.mojom) to one or more module.Module. |
| 8 # | 8 # |
| 9 # This module takes the output of the mojom parser, a MojomFileGraph and | 9 # This module takes the output of the mojom parser, a MojomFileGraph and |
| 10 # translates it to the input of the code generators, a module.Module object. | 10 # translates it to the input of the code generators, a module.Module object. |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 138 |
| 139 Args: | 139 Args: |
| 140 struct: {module.Struct} to be populated. | 140 struct: {module.Struct} to be populated. |
| 141 mojom_type: {UserDefinedType} referring to the MojomStruct to be | 141 mojom_type: {UserDefinedType} referring to the MojomStruct to be |
| 142 translated. | 142 translated. |
| 143 """ | 143 """ |
| 144 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.struct_type | 144 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.struct_type |
| 145 mojom_struct = mojom_type.struct_type | 145 mojom_struct = mojom_type.struct_type |
| 146 self.PopulateUserDefinedType(struct, mojom_struct) | 146 self.PopulateUserDefinedType(struct, mojom_struct) |
| 147 struct.fields = [self.StructFieldFromMojom(f) for f in mojom_struct.fields] | 147 struct.fields = [self.StructFieldFromMojom(f) for f in mojom_struct.fields] |
| 148 # TODO(azani): Handle mojom_struct.decl_data.contained_declarations. | 148 self.PopulateContainedDeclarationsFromMojom( |
| 149 struct, mojom_struct.decl_data.contained_declarations) |
| 149 | 150 |
| 150 def UnionFieldFromMojom(self, mojom_field): | 151 def UnionFieldFromMojom(self, mojom_field): |
| 151 """Translates a mojom_types_mojom.UnionField to a module.UnionField. | 152 """Translates a mojom_types_mojom.UnionField to a module.UnionField. |
| 152 | 153 |
| 153 Args: | 154 Args: |
| 154 mojom_field: {mojom_types_mojom.UnionField} to be translated. | 155 mojom_field: {mojom_types_mojom.UnionField} to be translated. |
| 155 | 156 |
| 156 Returns: | 157 Returns: |
| 157 {module.UnionField} translated from mojom_field. | 158 {module.UnionField} translated from mojom_field. |
| 158 """ | 159 """ |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 """Populates a number of common field values based on a mojom field. | 205 """Populates a number of common field values based on a mojom field. |
| 205 | 206 |
| 206 Args: | 207 Args: |
| 207 field: {module.Field|module.Parameter} to be populated. | 208 field: {module.Field|module.Parameter} to be populated. |
| 208 mojom_field: {StructField|UnionField} to be translated. | 209 mojom_field: {StructField|UnionField} to be translated. |
| 209 """ | 210 """ |
| 210 field.name = mojom_field.decl_data.short_name | 211 field.name = mojom_field.decl_data.short_name |
| 211 field.kind = self.KindFromMojom(mojom_field.type) | 212 field.kind = self.KindFromMojom(mojom_field.type) |
| 212 field.attributes = self.AttributesFromMojom(mojom_field) | 213 field.attributes = self.AttributesFromMojom(mojom_field) |
| 213 | 214 |
| 214 def EnumFromMojom(self, enum, mojom_type, parent_kind=None): | 215 def PopulateContainedDeclarationsFromMojom( |
| 216 self, parent_kind, contained_declarations): |
| 217 """Populates a module.Struct|module.Interface with contained declarations. |
| 218 |
| 219 Args: |
| 220 parent_kind: {module.Struct|module.Interface} to be populated. |
| 221 contained_declarations: {mojom_types_mojom.ContainedDeclarations} from |
| 222 which the contained types need to be extracted. |
| 223 """ |
| 224 if not contained_declarations: |
| 225 return |
| 226 |
| 227 if contained_declarations.enums: |
| 228 for enum_key in contained_declarations.enums: |
| 229 enum = self.UserDefinedFromTypeKey(enum_key) |
| 230 enum.name = '%s.%s' % (parent_kind.name, enum.name) |
| 231 enum.parent_kind = parent_kind |
| 232 parent_kind.enums.append(enum) |
| 233 |
| 234 if contained_declarations.constants: |
| 235 for const_key in contained_declarations.constants: |
| 236 const = self.ConstFromMojom( |
| 237 self._graph.resolved_values[const_key].declared_constant, |
| 238 parent_kind) |
| 239 parent_kind.constants.append(const) |
| 240 |
| 241 def EnumFromMojom(self, enum, mojom_type): |
| 215 """Populates a module.Enum based on a MojomEnum. | 242 """Populates a module.Enum based on a MojomEnum. |
| 216 | 243 |
| 217 Args: | 244 Args: |
| 218 enum: {module.Enum} to be populated. | 245 enum: {module.Enum} to be populated. |
| 219 mojom_type: {mojom_types_mojom.Type} referring to the MojomEnum to be | 246 mojom_type: {mojom_types_mojom.Type} referring to the MojomEnum to be |
| 220 translated. | 247 translated. |
| 221 parent_kind: {MojomStruct|MojomInterface} in which the enum is nested. | |
| 222 """ | 248 """ |
| 223 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.enum_type | 249 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.enum_type |
| 224 mojom_enum = mojom_type.enum_type | 250 mojom_enum = mojom_type.enum_type |
| 225 enum.parent_kind = parent_kind | |
| 226 self.PopulateUserDefinedType(enum, mojom_enum) | 251 self.PopulateUserDefinedType(enum, mojom_enum) |
| 227 enum.fields = [self.EnumFieldFromMojom(value) | 252 enum.fields = [self.EnumFieldFromMojom(value) |
| 228 for value in mojom_enum.values] | 253 for value in mojom_enum.values] |
| 229 | 254 |
| 230 def EnumFieldFromMojom(self, mojom_field): | 255 def EnumFieldFromMojom(self, mojom_field): |
| 231 """Translates an mojom_types_mojom.EnumValue to a module.EnumField. | 256 """Translates an mojom_types_mojom.EnumValue to a module.EnumField. |
| 232 | 257 |
| 233 mojom_field: {mojom_types_mojom.EnumValue} to be translated. | 258 mojom_field: {mojom_types_mojom.EnumValue} to be translated. |
| 234 | 259 |
| 235 Returns: | 260 Returns: |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 translated. | 333 translated. |
| 309 """ | 334 """ |
| 310 assert (mojom_type.tag | 335 assert (mojom_type.tag |
| 311 == mojom_types_mojom.UserDefinedType.Tags.interface_type) | 336 == mojom_types_mojom.UserDefinedType.Tags.interface_type) |
| 312 mojom_interface = mojom_type.interface_type | 337 mojom_interface = mojom_type.interface_type |
| 313 interface.attributes = self.AttributesFromMojom(mojom_interface) | 338 interface.attributes = self.AttributesFromMojom(mojom_interface) |
| 314 self.PopulateModuleOrImportedFrom(interface, mojom_interface) | 339 self.PopulateModuleOrImportedFrom(interface, mojom_interface) |
| 315 interface.name = mojom_interface.interface_name | 340 interface.name = mojom_interface.interface_name |
| 316 interface.methods = [self.MethodFromMojom(mojom_method, interface) | 341 interface.methods = [self.MethodFromMojom(mojom_method, interface) |
| 317 for mojom_method in mojom_interface.methods.itervalues()] | 342 for mojom_method in mojom_interface.methods.itervalues()] |
| 343 self.PopulateContainedDeclarationsFromMojom( |
| 344 interface, mojom_interface.decl_data.contained_declarations) |
| 318 | 345 |
| 319 def MethodFromMojom(self, mojom_method, interface): | 346 def MethodFromMojom(self, mojom_method, interface): |
| 320 """Translates a mojom_types_mojom.MojomMethod to a module.Method. | 347 """Translates a mojom_types_mojom.MojomMethod to a module.Method. |
| 321 | 348 |
| 322 Args: | 349 Args: |
| 323 mojom_method: {mojom_types_mojom.MojomMethod} to be translated. | 350 mojom_method: {mojom_types_mojom.MojomMethod} to be translated. |
| 324 interface: {module.Interface} the method is a member of. | 351 interface: {module.Interface} the method is a member of. |
| 325 | 352 |
| 326 Returns: | 353 Returns: |
| 327 {module.Method} translated from mojom_method. | 354 {module.Method} translated from mojom_method. |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 | 621 |
| 595 Args: | 622 Args: |
| 596 graph: {mojom_types_mojom.MojomFileGraph} to be translated. | 623 graph: {mojom_types_mojom.MojomFileGraph} to be translated. |
| 597 | 624 |
| 598 Return: | 625 Return: |
| 599 {dict<str, module.Module>} mapping the file's name to its module.Module | 626 {dict<str, module.Module>} mapping the file's name to its module.Module |
| 600 translation for all files in graph.files. | 627 translation for all files in graph.files. |
| 601 """ | 628 """ |
| 602 return {file_name: FileTranslator(graph, file_name).Translate() | 629 return {file_name: FileTranslator(graph, file_name).Translate() |
| 603 for file_name in graph.files} | 630 for file_name in graph.files} |
| OLD | NEW |