Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 '''Generates Go source files from a mojom.Module.''' | 5 '''Generates Go source files from a mojom.Module.''' |
| 6 | 6 |
| 7 from itertools import chain | 7 from itertools import chain |
| 8 import base64 | |
|
azani
2015/10/19 18:27:02
unused?
alexfandrianto
2015/10/19 21:36:30
Done. Yes, it was unused. I was using it earlier f
| |
| 8 import os | 9 import os |
| 9 import re | 10 import re |
| 10 | 11 |
| 11 from mojom.generate.template_expander import UseJinja | 12 from mojom.generate.template_expander import UseJinja |
| 12 | 13 |
| 13 import mojom.generate.generator as generator | 14 import mojom.generate.generator as generator |
| 14 import mojom.generate.module as mojom | 15 import mojom.generate.module as mojom |
| 15 import mojom.generate.pack as pack | 16 import mojom.generate.pack as pack |
| 16 | 17 |
| 17 class KindInfo(object): | 18 class KindInfo(object): |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 'system.MessagePipeHandle', 'Handle', 'MessagePipeHandle', 32), | 51 'system.MessagePipeHandle', 'Handle', 'MessagePipeHandle', 32), |
| 51 mojom.NULLABLE_SHAREDBUFFER: KindInfo( | 52 mojom.NULLABLE_SHAREDBUFFER: KindInfo( |
| 52 'system.SharedBufferHandle', 'Handle', 'SharedBufferHandle', 32), | 53 'system.SharedBufferHandle', 'Handle', 'SharedBufferHandle', 32), |
| 53 mojom.INT64: KindInfo('int64', 'Int64', 'Int64', 64), | 54 mojom.INT64: KindInfo('int64', 'Int64', 'Int64', 64), |
| 54 mojom.UINT64: KindInfo('uint64', 'Uint64', 'Uint64', 64), | 55 mojom.UINT64: KindInfo('uint64', 'Uint64', 'Uint64', 64), |
| 55 mojom.DOUBLE: KindInfo('float64', 'Float64', 'Float64', 64), | 56 mojom.DOUBLE: KindInfo('float64', 'Float64', 'Float64', 64), |
| 56 mojom.STRING: KindInfo('string', 'String', 'String', 64), | 57 mojom.STRING: KindInfo('string', 'String', 'String', 64), |
| 57 mojom.NULLABLE_STRING: KindInfo('string', 'String', 'String', 64), | 58 mojom.NULLABLE_STRING: KindInfo('string', 'String', 'String', 64), |
| 58 } | 59 } |
| 59 | 60 |
| 61 # _imports keeps track of the imports that the .go.mojom file needs to import. | |
| 60 _imports = {} | 62 _imports = {} |
| 61 | 63 |
| 64 # _mojom_imports keeps a list of the other .mojom files imported by this one. | |
| 65 _mojom_imports = {} | |
| 66 | |
| 67 # The mojom_types.mojom and service_describer.mojom files are special because | |
| 68 # they are used to generate mojom Type's and ServiceDescription implementations. | |
| 69 _service_describer_pkg_short = "service_describer" | |
| 70 _service_describer_pkg = "mojo/public/interfaces/bindings/%s" % \ | |
| 71 _service_describer_pkg_short | |
| 72 _mojom_types_pkg_short = "mojom_types" | |
| 73 _mojom_types_pkg = "mojo/public/interfaces/bindings/%s" % _mojom_types_pkg_short | |
| 74 | |
| 62 def GetBitSize(kind): | 75 def GetBitSize(kind): |
| 63 if isinstance(kind, (mojom.Union)): | 76 if isinstance(kind, (mojom.Union)): |
| 64 return 128 | 77 return 128 |
| 65 if isinstance(kind, (mojom.Array, mojom.Map, mojom.Struct, mojom.Interface)): | 78 if isinstance(kind, (mojom.Array, mojom.Map, mojom.Struct, mojom.Interface)): |
| 66 return 64 | 79 return 64 |
| 67 if mojom.IsUnionKind(kind): | 80 if mojom.IsUnionKind(kind): |
| 68 return 2*64 | 81 return 2*64 |
| 69 if isinstance(kind, (mojom.InterfaceRequest)): | 82 if isinstance(kind, (mojom.InterfaceRequest)): |
| 70 kind = mojom.MSGPIPE | 83 kind = mojom.MSGPIPE |
| 71 if isinstance(kind, mojom.Enum): | 84 if isinstance(kind, mojom.Enum): |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 | 192 |
| 180 def EncodeSuffix(kind): | 193 def EncodeSuffix(kind): |
| 181 if mojom.IsEnumKind(kind): | 194 if mojom.IsEnumKind(kind): |
| 182 return EncodeSuffix(mojom.INT32) | 195 return EncodeSuffix(mojom.INT32) |
| 183 if mojom.IsInterfaceKind(kind): | 196 if mojom.IsInterfaceKind(kind): |
| 184 return 'Interface' | 197 return 'Interface' |
| 185 if mojom.IsInterfaceRequestKind(kind): | 198 if mojom.IsInterfaceRequestKind(kind): |
| 186 return EncodeSuffix(mojom.MSGPIPE) | 199 return EncodeSuffix(mojom.MSGPIPE) |
| 187 return _kind_infos[kind].encode_suffix | 200 return _kind_infos[kind].encode_suffix |
| 188 | 201 |
| 202 # This helper assists in the production of mojom_types.Type for simple kinds. | |
| 203 # See _kind_infos above. | |
| 204 def GetMojomTypeValue(kind, typepkg=''): | |
| 205 if not kind in _kind_infos: | |
| 206 return '' | |
| 207 if kind == mojom.BOOL: | |
| 208 return '%sTypeSimpleType{%sSimpleType_Bool}' % (typepkg, typepkg) | |
|
azani
2015/10/19 18:27:02
There is a lot of repetition in those strings. You
alexfandrianto
2015/10/19 21:36:30
It's a bit smaller now. Unfortunately, I had to ha
| |
| 209 elif kind == mojom.INT8: | |
| 210 return '%sTypeSimpleType{%sSimpleType_InT8}' % (typepkg, typepkg) | |
| 211 elif kind == mojom.UINT8: | |
| 212 return '%sTypeSimpleType{%sSimpleType_UinT8}' % (typepkg, typepkg) | |
| 213 elif kind == mojom.INT16: | |
| 214 return '%sTypeSimpleType{%sSimpleType_InT16}' % (typepkg, typepkg) | |
| 215 elif kind == mojom.UINT16: | |
| 216 return '%sTypeSimpleType{%sSimpleType_UinT16}' % (typepkg, typepkg) | |
| 217 elif kind == mojom.INT32: | |
| 218 return '%sTypeSimpleType{%sSimpleType_InT32}' % (typepkg, typepkg) | |
| 219 elif kind == mojom.UINT32: | |
| 220 return '%sTypeSimpleType{%sSimpleType_UinT32}' % (typepkg, typepkg) | |
| 221 elif kind == mojom.INT64: | |
| 222 return '%sTypeSimpleType{%sSimpleType_InT64}' % (typepkg, typepkg) | |
|
azani
2015/10/19 18:27:02
I could be wrong, but InT64 does not look right to
alexfandrianto
2015/10/19 21:36:30
It looks weird to me too. It's an artifact of send
| |
| 223 elif kind == mojom.UINT64: | |
| 224 return '%sTypeSimpleType{%sSimpleType_UinT64}' % (typepkg, typepkg) | |
| 225 elif kind == mojom.FLOAT: | |
| 226 return '%sTypeSimpleType{%sSimpleType_Float}' % (typepkg, typepkg) | |
| 227 elif kind == mojom.DOUBLE: | |
| 228 return '%sTypeSimpleType{%sSimpleType_Double}' % (typepkg, typepkg) | |
| 229 elif kind == mojom.HANDLE: | |
| 230 return '%sTypeHandleType{%sHandleType{Nullable: false, Kind: %sHandleType_Ki nd_Unspecified}}' % (typepkg, typepkg, typepkg) | |
| 231 elif kind == mojom.DCPIPE: | |
| 232 return '%sTypeHandleType{%sHandleType{Nullable: false, Kind: %sHandleType_Ki nd_DataPipeConsumer}}' % (typepkg, typepkg, typepkg) | |
| 233 elif kind == mojom.DPPIPE: | |
| 234 return '%sTypeHandleType{%sHandleType{Nullable: false, Kind: %sHandleType_Ki nd_DataPipeProducer}}' % (typepkg, typepkg, typepkg) | |
| 235 elif kind == mojom.MSGPIPE: | |
| 236 return '%sTypeHandleType{%sHandleType{Nullable: false, Kind: %sHandleType_Ki nd_MessagePipe}}' % (typepkg, typepkg, typepkg) | |
| 237 elif kind == mojom.SHAREDBUFFER: | |
| 238 return '%sTypeHandleType{%sHandleType{Nullable: false, Kind: %sHandleType_Ki nd_SharedBuffer}}' % (typepkg, typepkg, typepkg) | |
| 239 elif kind == mojom.NULLABLE_HANDLE: | |
| 240 return '%sTypeHandleType{%sHandleType{Nullable: true, Kind: %sHandleType_Kin d_Unspecified}}' % (typepkg, typepkg, typepkg) | |
| 241 elif kind == mojom.NULLABLE_DCPIPE: | |
| 242 return '%sTypeHandleType{%sHandleType{Nullable: true, Kind: %sHandleType_Kin d_DataPipeConsumer}}' % (typepkg, typepkg, typepkg) | |
| 243 elif kind == mojom.NULLABLE_DPPIPE: | |
| 244 return '%sTypeHandleType{%sHandleType{Nullable: true, Kind: %sHandleType_Kin d_DataPipeProducer}}' % (typepkg, typepkg, typepkg) | |
| 245 elif kind == mojom.NULLABLE_MSGPIPE: | |
| 246 return '%sTypeHandleType{%sHandleType{Nullable: true, Kind: %sHandleType_Kin d_MessagePipe}}' % (typepkg, typepkg, typepkg) | |
| 247 elif kind == mojom.NULLABLE_SHAREDBUFFER: | |
| 248 return '%sTypeHandleType{%sHandleType{Nullable: true, Kind: %sHandleType_Kin d_SharedBuffer}}' % (typepkg, typepkg, typepkg) | |
| 249 elif kind == mojom.STRING: | |
| 250 return '%sTypeStringType{%sStringType{false}}' % (typepkg, typepkg) | |
| 251 elif kind == mojom.NULLABLE_STRING: | |
| 252 return '%sTypeStringType{%sStringType{true}}' % (typepkg, typepkg) | |
| 253 else: | |
| 254 raise Exception('Missing case for kind: %s' % kind) | |
| 255 | |
| 189 def GetPackageName(module): | 256 def GetPackageName(module): |
| 190 return module.name.split('.')[0] | 257 return module.name.split('.')[0] |
| 191 | 258 |
| 192 def GetPackageNameForElement(element): | 259 def GetPackageNameForElement(element, default=''): |
|
azani
2015/10/19 18:27:02
It looks like the default logic is only used when
alexfandrianto
2015/10/19 21:36:30
Done.
| |
| 193 if not hasattr(element, 'imported_from') or not element.imported_from: | 260 if not hasattr(element, 'imported_from') or not element.imported_from: |
| 194 return '' | 261 return default |
| 195 path = '' | 262 path = '' |
| 196 if element.imported_from['module'].path: | 263 if element.imported_from['module'].path: |
| 197 path += GetPackagePath(element.imported_from['module']) | 264 path += GetPackagePath(element.imported_from['module']) |
| 198 if path in _imports: | 265 if path in _imports: |
| 199 return _imports[path] | 266 return _imports[path] |
| 200 return '' | 267 return default |
| 201 | 268 |
| 202 def GetQualifiedName(name, package=None, exported=True): | 269 def GetQualifiedName(name, package=None, exported=True): |
| 203 if not package: | 270 if not package: |
| 204 return FormatName(name, exported) | 271 return FormatName(name, exported) |
| 205 return '%s.%s' % (package, FormatName(name, exported)) | 272 return '%s.%s' % (package, FormatName(name, exported)) |
| 206 | 273 |
| 207 def GetPackagePath(module): | 274 def GetPackagePath(module): |
| 208 name = module.name.split('.')[0] | 275 name = module.name.split('.')[0] |
| 209 return '/'.join(module.path.split('/')[:-1] + [name]) | 276 return '/'.join(module.path.split('/')[:-1] + [name]) |
| 210 | 277 |
| 211 def GetAllConstants(module): | 278 def GetAllConstants(module): |
| 212 data = [module] + module.structs + module.interfaces | 279 data = [module] + module.structs + module.interfaces |
| 213 constants = [x.constants for x in data] | 280 constants = [x.constants for x in data] |
| 214 return [i for i in chain.from_iterable(constants)] | 281 return [i for i in chain.from_iterable(constants)] |
| 215 | 282 |
| 216 def GetAllEnums(module): | 283 def GetAllEnums(module): |
| 217 data = [module] + module.structs + module.interfaces | 284 data = [module] + module.structs + module.interfaces |
| 218 enums = [x.enums for x in data] | 285 enums = [x.enums for x in data] |
| 219 return [i for i in chain.from_iterable(enums)] | 286 return [i for i in chain.from_iterable(enums)] |
| 220 | 287 |
| 221 # Adds an import required to use the provided |element|. | 288 # Adds an import required to use the provided |element|. |
| 222 # The required import is stored at '_imports'. | 289 # The required import is stored at '_imports'. |
| 290 # The mojom imports are also stored separately in '_mojom_imports'. | |
| 223 def AddImport(module, element): | 291 def AddImport(module, element): |
| 224 if not isinstance(element, mojom.Kind): | 292 if not isinstance(element, mojom.Kind): |
| 225 return | 293 return |
| 226 | 294 |
| 227 if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element): | 295 if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element): |
| 228 AddImport(module, element.kind) | 296 AddImport(module, element.kind) |
| 229 return | 297 return |
| 230 if mojom.IsMapKind(element): | 298 if mojom.IsMapKind(element): |
| 231 AddImport(module, element.key_kind) | 299 AddImport(module, element.key_kind) |
| 232 AddImport(module, element.value_kind) | 300 AddImport(module, element.value_kind) |
| 233 return | 301 return |
| 234 if mojom.IsAnyHandleKind(element): | 302 if mojom.IsAnyHandleKind(element): |
| 235 _imports['mojo/public/go/system'] = 'system' | 303 _imports['mojo/public/go/system'] = 'system' |
| 236 return | 304 return |
| 237 | 305 |
| 238 if not hasattr(element, 'imported_from') or not element.imported_from: | 306 if not hasattr(element, 'imported_from') or not element.imported_from: |
| 239 return | 307 return |
| 240 imported = element.imported_from | 308 imported = element.imported_from |
| 241 if GetPackagePath(imported['module']) == GetPackagePath(module): | 309 if GetPackagePath(imported['module']) == GetPackagePath(module): |
| 242 return | 310 return |
| 243 path = GetPackagePath(imported['module']) | 311 path = GetPackagePath(imported['module']) |
| 244 if path in _imports: | 312 if path in _imports: |
| 245 return | 313 return |
| 246 name = GetPackageName(imported['module']) | 314 name = GetPackageName(imported['module']) |
| 247 while name in _imports.values(): | 315 while name in _imports.values(): # This avoids repeated names. |
| 248 name += '_' | 316 name += '_' |
| 249 _imports[path] = name | 317 _imports[path] = name |
| 318 _mojom_imports[path] = name | |
| 250 | 319 |
| 320 # The identifier cache is used by the Type generator to determine if a type has | |
| 321 # already been generated or not. This prevents over-generation of the same type | |
| 322 # when it is referred to in multiple ways. | |
| 323 identifier_cache = {} | |
| 324 def GetIdentifier(kind, default): | |
| 325 package = GetPackageNameForElement(kind, default) | |
|
azani
2015/10/19 18:27:02
I think what you are looking for that you pass in
alexfandrianto
2015/10/19 21:36:30
Changed a little bit. Unfortunately, kind.module d
azani
2015/10/19 21:43:07
I would expect that kind.module would exist for al
| |
| 326 | |
| 327 # Most kinds have a name, but those that don't should rely on their spec. | |
| 328 # Since spec can have : and ? characters, these must be replaced. Since ? is | |
| 329 # replaced with '', the caller must keep track of optionality on its own. | |
| 330 package_unique = (kind.name if hasattr(kind, 'name') else kind.spec).replace(' :', '_').replace('?', '') | |
| 331 return '%s_%s' % (package, package_unique) | |
| 332 | |
| 333 def StoreIdentifier(identifier, cache_name): | |
| 334 if not cache_name in identifier_cache: | |
| 335 identifier_cache[cache_name] = {} | |
| 336 identifier_cache[cache_name][identifier] = True | |
| 337 return '' | |
| 338 | |
| 339 def CheckIdentifier(identifier, cache_name): | |
| 340 if not cache_name in identifier_cache: | |
| 341 identifier_cache[cache_name] = {} | |
| 342 return identifier in identifier_cache[cache_name] | |
| 343 | |
| 344 # Get the mojom type's identifier suffix. | |
| 345 def GetMojomTypeIdentifier(kind, default): | |
| 346 # Since this should be unique, it is based on the type's identifier. | |
| 347 return "%s__" % GetIdentifier(kind, default) | |
| 251 | 348 |
| 252 class Generator(generator.Generator): | 349 class Generator(generator.Generator): |
| 253 go_filters = { | 350 go_filters = { |
| 254 'array': lambda kind: mojom.Array(kind), | 351 'array': lambda kind: mojom.Array(kind), |
| 255 'bit_size': GetBitSize, | 352 'bit_size': GetBitSize, |
| 256 'decode_suffix': DecodeSuffix, | 353 'decode_suffix': DecodeSuffix, |
| 257 'encode_suffix': EncodeSuffix, | 354 'encode_suffix': EncodeSuffix, |
| 258 'go_type': GetGoType, | 355 'go_type': GetGoType, |
| 259 'expression_to_text': ExpressionToText, | 356 'expression_to_text': ExpressionToText, |
| 260 'has_response': lambda method: method.response_parameters is not None, | 357 'has_response': lambda method: method.response_parameters is not None, |
| 358 'identifier': GetIdentifier, | |
| 359 'identifier_check': CheckIdentifier, | |
| 360 'identifier_store': StoreIdentifier, | |
| 261 'is_array': mojom.IsArrayKind, | 361 'is_array': mojom.IsArrayKind, |
| 262 'is_enum': mojom.IsEnumKind, | 362 'is_enum': mojom.IsEnumKind, |
| 263 'is_handle': mojom.IsAnyHandleKind, | 363 'is_handle': mojom.IsAnyHandleKind, |
| 264 'is_interface': mojom.IsInterfaceKind, | 364 'is_interface': mojom.IsInterfaceKind, |
| 265 'is_interface_request': mojom.IsInterfaceRequestKind, | 365 'is_interface_request': mojom.IsInterfaceRequestKind, |
| 266 'is_map': mojom.IsMapKind, | 366 'is_map': mojom.IsMapKind, |
| 267 'is_none_or_empty': lambda array: array is None or len(array) == 0, | 367 'is_none_or_empty': lambda array: array is None or len(array) == 0, |
| 268 'is_nullable': mojom.IsNullableKind, | 368 'is_nullable': mojom.IsNullableKind, |
| 269 'is_pointer': IsPointer, | 369 'is_pointer': IsPointer, |
| 270 'is_object': mojom.IsObjectKind, | 370 'is_object': mojom.IsObjectKind, |
| 271 'is_struct': mojom.IsStructKind, | 371 'is_struct': mojom.IsStructKind, |
| 272 'is_union': mojom.IsUnionKind, | 372 'is_union': mojom.IsUnionKind, |
| 273 'qualified': GetQualifiedName, | 373 'qualified': GetQualifiedName, |
| 374 'mojom_type': GetMojomTypeValue, | |
| 375 'mojom_type_identifier': GetMojomTypeIdentifier, | |
| 274 'name': GetNameForElement, | 376 'name': GetNameForElement, |
| 275 'unqualified_name': GetUnqualifiedNameForElement, | 377 'unqualified_name': GetUnqualifiedNameForElement, |
| 276 'package': GetPackageNameForElement, | 378 'package': GetPackageNameForElement, |
| 277 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) | 379 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) |
| 278 } | 380 } |
| 279 | 381 |
| 280 def GetParameters(self): | 382 def GetParameters(self): |
| 383 package = GetPackageName(self.module) | |
| 281 return { | 384 return { |
| 282 'enums': GetAllEnums(self.module), | 385 'enums': GetAllEnums(self.module), |
| 283 'imports': self.GetImports(), | 386 'imports': self.GetImports()[0], |
| 284 'interfaces': self.GetInterfaces(), | 387 'interfaces': self.GetInterfaces(), |
| 285 'package': GetPackageName(self.module), | 388 'mojom_imports': self.GetMojomImports(), |
| 389 'package': package, | |
| 286 'structs': self.GetStructs(), | 390 'structs': self.GetStructs(), |
| 287 'unions': self.GetUnions(), | 391 'descpkg': '%s.' % _service_describer_pkg_short if package != _service_des criber_pkg_short else '', |
| 392 'typepkg': '%s.' % _mojom_types_pkg_short if package != _mojom_types_pkg_s hort else '', | |
| 393 'unions': self.GetUnions() | |
| 288 } | 394 } |
| 289 | 395 |
| 290 @UseJinja('go_templates/source.tmpl', filters=go_filters) | 396 @UseJinja('go_templates/source.tmpl', filters=go_filters) |
| 291 def GenerateSource(self): | 397 def GenerateSource(self): |
| 292 return self.GetParameters() | 398 return self.GetParameters() |
| 293 | 399 |
| 294 def GenerateFiles(self, args): | 400 def GenerateFiles(self, args): |
| 295 self.Write(self.GenerateSource(), os.path.join("go", "src", | 401 self.Write(self.GenerateSource(), os.path.join("go", "src", |
| 296 GetPackagePath(self.module), "%s.go" % self.module.name)) | 402 GetPackagePath(self.module), "%s.go" % self.module.name)) |
| 297 | 403 |
| 298 def GetJinjaParameters(self): | 404 def GetJinjaParameters(self): |
| 299 return { | 405 return { |
| 300 'lstrip_blocks': True, | 406 'lstrip_blocks': True, |
| 301 'trim_blocks': True, | 407 'trim_blocks': True, |
| 302 } | 408 } |
| 303 | 409 |
| 304 def GetGlobals(self): | 410 def GetGlobals(self): |
| 305 return { | 411 return { |
| 306 'namespace': self.module.namespace, | 412 'namespace': self.module.namespace, |
| 307 'module': self.module, | 413 'module': self.module, |
| 308 } | 414 } |
| 309 | 415 |
| 310 # Scans |self.module| for elements that require imports and adds all found | 416 # Scans |self.module| for elements that require imports and adds all found |
| 311 # imports to '_imports' dict. Returns a list of imports that should include | 417 # imports to '_imports' dict. Mojom imports are stored in the '_mojom_imports' |
| 312 # the generated go file. | 418 # dict. This operation is idempotent. |
| 419 # Returns a tuple: | |
| 420 # - list of imports that should include the generated go file | |
| 421 # - the dictionary of _mojom_imports | |
| 313 def GetImports(self): | 422 def GetImports(self): |
| 314 # Imports can only be used in structs, constants, enums, interfaces. | 423 # Imports can only be used in structs, constants, enums, interfaces. |
| 315 all_structs = list(self.module.structs) | 424 all_structs = list(self.module.structs) |
| 316 for i in self.module.interfaces: | 425 for i in self.module.interfaces: |
| 317 for method in i.methods: | 426 for method in i.methods: |
| 318 all_structs.append(self._GetStructFromMethod(method)) | 427 all_structs.append(self._GetStructFromMethod(method)) |
| 319 if method.response_parameters: | 428 if method.response_parameters: |
| 320 all_structs.append(self._GetResponseStructFromMethod(method)) | 429 all_structs.append(self._GetResponseStructFromMethod(method)) |
| 321 | 430 |
| 322 if (len(all_structs) > 0 or len(self.module.interfaces) > 0 | 431 if (len(all_structs) > 0 or len(self.module.interfaces) > 0 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 337 AddImport(self.module, field.kind) | 446 AddImport(self.module, field.kind) |
| 338 # TODO(rogulenko): add these after generating constants and struct defaults. | 447 # TODO(rogulenko): add these after generating constants and struct defaults. |
| 339 # if field.default: | 448 # if field.default: |
| 340 # AddImport(self.module, field.default) | 449 # AddImport(self.module, field.default) |
| 341 | 450 |
| 342 for enum in GetAllEnums(self.module): | 451 for enum in GetAllEnums(self.module): |
| 343 for field in enum.fields: | 452 for field in enum.fields: |
| 344 if field.value: | 453 if field.value: |
| 345 AddImport(self.module, field.value) | 454 AddImport(self.module, field.value) |
| 346 | 455 |
| 456 num_user_defined_types = len(self.module.interfaces) + \ | |
| 457 len(self.module.unions) + len(all_structs) + len(GetAllEnums(self.module)) | |
| 458 if num_user_defined_types > 0 \ | |
| 459 and GetPackageName(self.module) != _mojom_types_pkg_short: | |
| 460 _imports[_mojom_types_pkg] = _mojom_types_pkg_short | |
| 461 | |
| 462 if len(self.module.interfaces) > 0 \ | |
| 463 and GetPackageName(self.module) != _mojom_types_pkg_short \ | |
| 464 and GetPackageName(self.module) != _service_describer_pkg_short: | |
| 465 _imports[_service_describer_pkg] = _service_describer_pkg_short | |
| 466 | |
| 347 # TODO(rogulenko): add these after generating constants and struct defaults. | 467 # TODO(rogulenko): add these after generating constants and struct defaults. |
| 348 # for constant in GetAllConstants(self.module): | 468 # for constant in GetAllConstants(self.module): |
| 349 # AddImport(self.module, constant.value) | 469 # AddImport(self.module, constant.value) |
| 350 | 470 |
| 351 imports_list = [] | 471 imports_list = [] |
| 352 for i in _imports: | 472 for i in _imports: |
| 353 if i.split('/')[-1] == _imports[i]: | 473 if i.split('/')[-1] == _imports[i]: |
| 354 imports_list.append('"%s"' % i) | 474 imports_list.append('"%s"' % i) |
| 355 else: | 475 else: |
| 356 imports_list.append('%s "%s"' % (_imports[i], i)) | 476 imports_list.append('%s "%s"' % (_imports[i], i)) |
| 357 return sorted(imports_list) | 477 return sorted(imports_list), _mojom_imports |
| 478 | |
| 479 def GetMojomImports(self): | |
| 480 # GetImports (idempotent) prepares the _imports and _mojom_imports maps. | |
| 481 return self.GetImports()[1] | |
| 358 | 482 |
| 359 # Overrides the implementation from the base class in order to customize the | 483 # Overrides the implementation from the base class in order to customize the |
| 360 # struct and field names. | 484 # struct and field names. |
| 361 def _GetStructFromMethod(self, method): | 485 def _GetStructFromMethod(self, method): |
| 362 params_class = "%s_%s_Params" % (GetNameForElement(method.interface), | 486 params_class = "%s_%s_Params" % (GetNameForElement(method.interface), |
| 363 GetNameForElement(method)) | 487 GetNameForElement(method)) |
| 364 struct = mojom.Struct(params_class, module=method.interface.module) | 488 struct = mojom.Struct(params_class, module=method.interface.module) |
| 365 for param in method.parameters: | 489 for param in method.parameters: |
| 366 struct.AddField("in%s" % GetNameForElement(param), | 490 struct.AddField("in%s" % GetNameForElement(param), |
| 367 param.kind, param.ordinal, attributes=param.attributes) | 491 param.kind, param.ordinal, attributes=param.attributes) |
| 368 return self._AddStructComputedData(False, struct) | 492 return self._AddStructComputedData(False, struct) |
| 369 | 493 |
| 370 # Overrides the implementation from the base class in order to customize the | 494 # Overrides the implementation from the base class in order to customize the |
| 371 # struct and field names. | 495 # struct and field names. |
| 372 def _GetResponseStructFromMethod(self, method): | 496 def _GetResponseStructFromMethod(self, method): |
| 373 params_class = "%s_%s_ResponseParams" % ( | 497 params_class = "%s_%s_ResponseParams" % ( |
| 374 GetNameForElement(method.interface), GetNameForElement(method)) | 498 GetNameForElement(method.interface), GetNameForElement(method)) |
| 375 struct = mojom.Struct(params_class, module=method.interface.module) | 499 struct = mojom.Struct(params_class, module=method.interface.module) |
| 376 for param in method.response_parameters: | 500 for param in method.response_parameters: |
| 377 struct.AddField("out%s" % GetNameForElement(param), | 501 struct.AddField("out%s" % GetNameForElement(param), |
| 378 param.kind, param.ordinal, attributes=param.attributes) | 502 param.kind, param.ordinal, attributes=param.attributes) |
| 379 return self._AddStructComputedData(False, struct) | 503 return self._AddStructComputedData(False, struct) |
| OLD | NEW |