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 | |
| 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): |
| 18 def __init__(self, go_type, encode_suffix, decode_suffix, bit_size): | 19 def __init__(self, go_type, encode_suffix, decode_suffix, bit_size, type_templ ate): |
| 19 self.go_type = go_type | 20 self.go_type = go_type |
| 20 self.encode_suffix = encode_suffix | 21 self.encode_suffix = encode_suffix |
| 21 self.decode_suffix = decode_suffix | 22 self.decode_suffix = decode_suffix |
| 22 self.bit_size = bit_size | 23 self.bit_size = bit_size |
| 24 self.type_template = type_template | |
| 23 | 25 |
| 24 _kind_infos = { | 26 _kind_infos = { |
| 25 mojom.BOOL: KindInfo('bool', 'Bool', 'Bool', 1), | 27 mojom.BOOL: KindInfo('bool', 'Bool', 'Bool', 1, '%sTypeSimple Type{%sSimpleType_Bool}'), |
| 26 mojom.INT8: KindInfo('int8', 'Int8', 'Int8', 8), | 28 mojom.INT8: KindInfo('int8', 'Int8', 'Int8', 8, '%sTypeSimple Type{%sSimpleType_InT8}'), |
| 27 mojom.UINT8: KindInfo('uint8', 'Uint8', 'Uint8', 8), | 29 mojom.UINT8: KindInfo('uint8', 'Uint8', 'Uint8', 8, '%sTypeSim pleType{%sSimpleType_UinT8}'), |
| 28 mojom.INT16: KindInfo('int16', 'Int16', 'Int16', 16), | 30 mojom.INT16: KindInfo('int16', 'Int16', 'Int16', 16, '%sTypeSi mpleType{%sSimpleType_InT16}'), |
| 29 mojom.UINT16: KindInfo('uint16', 'Uint16', 'Uint16', 16), | 31 mojom.UINT16: KindInfo('uint16', 'Uint16', 'Uint16', 16, '%sTyp eSimpleType{%sSimpleType_UinT16}'), |
| 30 mojom.INT32: KindInfo('int32', 'Int32', 'Int32', 32), | 32 mojom.INT32: KindInfo('int32', 'Int32', 'Int32', 32, '%sTypeSi mpleType{%sSimpleType_InT32}'), |
| 31 mojom.UINT32: KindInfo('uint32', 'Uint32', 'Uint32', 32), | 33 mojom.UINT32: KindInfo('uint32', 'Uint32', 'Uint32', 32, '%sTyp eSimpleType{%sSimpleType_UinT32}'), |
| 32 mojom.FLOAT: KindInfo('float32', 'Float32', 'Float32', 32), | 34 mojom.FLOAT: KindInfo('float32', 'Float32', 'Float32', 32, '%s TypeSimpleType{%sSimpleType_Float}'), |
| 33 mojom.HANDLE: KindInfo( | 35 mojom.HANDLE: KindInfo( |
| 34 'system.Handle', 'Handle', 'Handle', 32), | 36 'system.Handle', 'Handle', 'Handle', 32, '%sTypeHandleType{%sHandleType{Nu llable: false, Kind: %sHandleType_Kind_Unspecified}}'), |
| 35 mojom.DCPIPE: KindInfo( | 37 mojom.DCPIPE: KindInfo( |
| 36 'system.ConsumerHandle', 'Handle', 'ConsumerHandle', 32), | 38 'system.ConsumerHandle', 'Handle', 'ConsumerHandle', 32, '%sTypeHandleType {%sHandleType{Nullable: false, Kind: %sHandleType_Kind_DataPipeConsumer}}'), |
| 37 mojom.DPPIPE: KindInfo( | 39 mojom.DPPIPE: KindInfo( |
| 38 'system.ProducerHandle', 'Handle', 'ProducerHandle', 32), | 40 'system.ProducerHandle', 'Handle', 'ProducerHandle', 32, '%sTypeHandleType {%sHandleType{Nullable: false, Kind: %sHandleType_Kind_DataPipeProducer}}'), |
| 39 mojom.MSGPIPE: KindInfo( | 41 mojom.MSGPIPE: KindInfo( |
| 40 'system.MessagePipeHandle', 'Handle', 'MessagePipeHandle', 32), | 42 'system.MessagePipeHandle', 'Handle', 'MessagePipeHandle', 32, '%sTypeHand leType{%sHandleType{Nullable: false, Kind: %sHandleType_Kind_MessagePipe}}'), |
| 41 mojom.SHAREDBUFFER: KindInfo( | 43 mojom.SHAREDBUFFER: KindInfo( |
| 42 'system.SharedBufferHandle', 'Handle', 'SharedBufferHandle', 32), | 44 'system.SharedBufferHandle', 'Handle', 'SharedBufferHandle', 32, '%sTypeHa ndleType{%sHandleType{Nullable: false, Kind: %sHandleType_Kind_SharedBuffer}}'), |
| 43 mojom.NULLABLE_HANDLE: KindInfo( | 45 mojom.NULLABLE_HANDLE: KindInfo( |
| 44 'system.Handle', 'Handle', 'Handle', 32), | 46 'system.Handle', 'Handle', 'Handle', 32, '%sTypeHandleType{%sHandleType{Nu llable: true, Kind: %sHandleType_Kind_Unspecified}}'), |
| 45 mojom.NULLABLE_DCPIPE: KindInfo( | 47 mojom.NULLABLE_DCPIPE: KindInfo( |
| 46 'system.ConsumerHandle', 'Handle', 'ConsumerHandle', 32), | 48 'system.ConsumerHandle', 'Handle', 'ConsumerHandle', 32, '%sTypeHandleType {%sHandleType{Nullable: true, Kind: %sHandleType_Kind_DataPipeConsumer}}'), |
| 47 mojom.NULLABLE_DPPIPE: KindInfo( | 49 mojom.NULLABLE_DPPIPE: KindInfo( |
| 48 'system.ProducerHandle', 'Handle', 'ProducerHandle', 32), | 50 'system.ProducerHandle', 'Handle', 'ProducerHandle', 32, '%sTypeHandleType {%sHandleType{Nullable: true, Kind: %sHandleType_Kind_DataPipeProducer}}'), |
| 49 mojom.NULLABLE_MSGPIPE: KindInfo( | 51 mojom.NULLABLE_MSGPIPE: KindInfo( |
| 50 'system.MessagePipeHandle', 'Handle', 'MessagePipeHandle', 32), | 52 'system.MessagePipeHandle', 'Handle', 'MessagePipeHandle', 32, '%sTypeHand leType{%sHandleType{Nullable: true, Kind: %sHandleType_Kind_MessagePipe}}'), |
| 51 mojom.NULLABLE_SHAREDBUFFER: KindInfo( | 53 mojom.NULLABLE_SHAREDBUFFER: KindInfo( |
| 52 'system.SharedBufferHandle', 'Handle', 'SharedBufferHandle', 32), | 54 'system.SharedBufferHandle', 'Handle', 'SharedBufferHandle', 32, '%sTypeHa ndleType{%sHandleType{Nullable: true, Kind: %sHandleType_Kind_SharedBuffer}}'), |
| 53 mojom.INT64: KindInfo('int64', 'Int64', 'Int64', 64), | 55 mojom.INT64: KindInfo('int64', 'Int64', 'Int64', 64, '%sTypeSi mpleType{%sSimpleType_InT64}'), |
| 54 mojom.UINT64: KindInfo('uint64', 'Uint64', 'Uint64', 64), | 56 mojom.UINT64: KindInfo('uint64', 'Uint64', 'Uint64', 64, '%sTyp eSimpleType{%sSimpleType_UinT64}'), |
| 55 mojom.DOUBLE: KindInfo('float64', 'Float64', 'Float64', 64), | 57 mojom.DOUBLE: KindInfo('float64', 'Float64', 'Float64', 64, '%s TypeSimpleType{%sSimpleType_Double}'), |
| 56 mojom.STRING: KindInfo('string', 'String', 'String', 64), | 58 mojom.STRING: KindInfo('string', 'String', 'String', 64, '%sTyp eStringType{%sStringType{false}}'), |
| 57 mojom.NULLABLE_STRING: KindInfo('string', 'String', 'String', 64), | 59 mojom.NULLABLE_STRING: KindInfo('string', 'String', 'String', 64, '%sTyp eStringType{%sStringType{true}}'), |
| 58 } | 60 } |
| 59 | 61 |
| 60 _imports = {} | 62 _imports = {} |
| 63 _mojom_imports = {} | |
|
mattr
2015/09/17 00:37:35
Can you explain the difference between imports and
alexfandrianto
2015/10/09 17:43:55
Added comments.
_imports contains the raw text fo
| |
| 64 | |
| 65 _mojom_descriptors_pkg = "mojo/public/interfaces/bindings/mojom_descriptors" | |
| 66 _mojom_descriptors_pkg_short = "mojom_descriptors" | |
| 67 _mojom_types_pkg = "mojo/public/interfaces/bindings/mojom_types" | |
| 68 _mojom_types_pkg_short = "mojom_types" | |
| 61 | 69 |
| 62 def GetBitSize(kind): | 70 def GetBitSize(kind): |
| 63 if isinstance(kind, (mojom.Union)): | 71 if isinstance(kind, (mojom.Union)): |
| 64 return 128 | 72 return 128 |
| 65 if isinstance(kind, (mojom.Array, mojom.Map, mojom.Struct, mojom.Interface)): | 73 if isinstance(kind, (mojom.Array, mojom.Map, mojom.Struct, mojom.Interface)): |
| 66 return 64 | 74 return 64 |
| 67 if mojom.IsUnionKind(kind): | 75 if mojom.IsUnionKind(kind): |
| 68 return 2*64 | 76 return 2*64 |
| 69 if isinstance(kind, (mojom.InterfaceRequest)): | 77 if isinstance(kind, (mojom.InterfaceRequest)): |
| 70 kind = mojom.MSGPIPE | 78 kind = mojom.MSGPIPE |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 | 184 |
| 177 def EncodeSuffix(kind): | 185 def EncodeSuffix(kind): |
| 178 if mojom.IsEnumKind(kind): | 186 if mojom.IsEnumKind(kind): |
| 179 return EncodeSuffix(mojom.INT32) | 187 return EncodeSuffix(mojom.INT32) |
| 180 if mojom.IsInterfaceKind(kind): | 188 if mojom.IsInterfaceKind(kind): |
| 181 return 'Interface' | 189 return 'Interface' |
| 182 if mojom.IsInterfaceRequestKind(kind): | 190 if mojom.IsInterfaceRequestKind(kind): |
| 183 return EncodeSuffix(mojom.MSGPIPE) | 191 return EncodeSuffix(mojom.MSGPIPE) |
| 184 return _kind_infos[kind].encode_suffix | 192 return _kind_infos[kind].encode_suffix |
| 185 | 193 |
| 194 def GetIdentifier(kind, default): | |
| 195 package = GetPackageNameForElement(kind, default) | |
| 196 #print(package) | |
| 197 #print(kind.name if hasattr(kind, 'name') else kind.spec) | |
| 198 #return base64.b32encode(package + ':' + (kind.name if hasattr(kind, 'name') e lse kind.spec)).replace('=', '') | |
| 199 # Replace all suspicious characters with _. | |
| 200 # Remove ? since this info is captured in TypeReference. | |
| 201 package_unique = (kind.name if hasattr(kind, 'name') else kind.spec).replace(' :', '_').replace('?', '') | |
| 202 return '%s_%s' % (package, package_unique) | |
| 203 | |
| 204 #def GetImportShortName(imp): | |
| 205 # i = imp.replace('"', '') | |
| 206 # print(i) | |
| 207 # print(i.split('/')[-1]) | |
| 208 # print(_imports[i] if i in _imports else '') | |
| 209 # return _imports[i] if i in _imports else i.split('/')[-1] | |
| 210 | |
| 211 def GetMojomTypeValue(kind, typepkg=''): | |
| 212 if not kind in _kind_infos: | |
| 213 return '' | |
| 214 if mojom.IsAnyHandleKind(kind): | |
| 215 return _kind_infos[kind].type_template % (typepkg, typepkg, typepkg) | |
| 216 return _kind_infos[kind].type_template % (typepkg, typepkg) | |
| 217 | |
| 186 def GetPackageName(module): | 218 def GetPackageName(module): |
| 187 return module.name.split('.')[0] | 219 return module.name.split('.')[0] |
| 188 | 220 |
| 189 def GetPackageNameForElement(element): | 221 def GetPackageNameForElement(element, default=''): |
| 190 if not hasattr(element, 'imported_from') or not element.imported_from: | 222 if not hasattr(element, 'imported_from') or not element.imported_from: |
| 191 return '' | 223 return default |
| 192 path = '' | 224 path = '' |
| 193 if element.imported_from['module'].path: | 225 if element.imported_from['module'].path: |
| 194 path += GetPackagePath(element.imported_from['module']) | 226 path += GetPackagePath(element.imported_from['module']) |
| 195 if path in _imports: | 227 if path in _imports: |
| 196 return _imports[path] | 228 return _imports[path] |
| 197 return '' | 229 return default |
| 198 | 230 |
| 199 def GetQualifiedName(name, package=None, exported=True): | 231 def GetQualifiedName(name, package=None, exported=True): |
| 200 if not package: | 232 if not package: |
| 201 return FormatName(name, exported) | 233 return FormatName(name, exported) |
| 202 return '%s.%s' % (package, FormatName(name, exported)) | 234 return '%s.%s' % (package, FormatName(name, exported)) |
| 203 | 235 |
| 204 def GetPackagePath(module): | 236 def GetPackagePath(module): |
| 205 name = module.name.split('.')[0] | 237 name = module.name.split('.')[0] |
| 206 return '/'.join(module.path.split('/')[:-1] + [name]) | 238 return '/'.join(module.path.split('/')[:-1] + [name]) |
| 207 | 239 |
| 208 def GetAllConstants(module): | 240 def GetAllConstants(module): |
| 209 data = [module] + module.structs + module.interfaces | 241 data = [module] + module.structs + module.interfaces |
| 210 constants = [x.constants for x in data] | 242 constants = [x.constants for x in data] |
| 211 return [i for i in chain.from_iterable(constants)] | 243 return [i for i in chain.from_iterable(constants)] |
| 212 | 244 |
| 213 def GetAllEnums(module): | 245 def GetAllEnums(module): |
| 214 data = [module] + module.structs + module.interfaces | 246 data = [module] + module.structs + module.interfaces |
| 215 enums = [x.enums for x in data] | 247 enums = [x.enums for x in data] |
| 216 return [i for i in chain.from_iterable(enums)] | 248 return [i for i in chain.from_iterable(enums)] |
| 217 | 249 |
| 218 # Adds an import required to use the provided |element|. | 250 # Adds an import required to use the provided |element|. |
| 219 # The required import is stored at '_imports'. | 251 # The required import is stored at '_imports'. |
| 252 # The mojom imports are also stored separately in '_mojom_imports'. | |
| 220 def AddImport(module, element): | 253 def AddImport(module, element): |
| 221 if not isinstance(element, mojom.Kind): | 254 if not isinstance(element, mojom.Kind): |
| 222 return | 255 return |
| 223 | 256 |
| 224 if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element): | 257 if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element): |
| 225 AddImport(module, element.kind) | 258 AddImport(module, element.kind) |
| 226 return | 259 return |
| 227 if mojom.IsMapKind(element): | 260 if mojom.IsMapKind(element): |
| 228 AddImport(module, element.key_kind) | 261 AddImport(module, element.key_kind) |
| 229 AddImport(module, element.value_kind) | 262 AddImport(module, element.value_kind) |
| 230 return | 263 return |
| 231 if mojom.IsAnyHandleKind(element): | 264 if mojom.IsAnyHandleKind(element): |
| 232 _imports['mojo/public/go/system'] = 'system' | 265 _imports['mojo/public/go/system'] = 'system' |
| 233 return | 266 return |
| 234 | 267 |
| 235 if not hasattr(element, 'imported_from') or not element.imported_from: | 268 if not hasattr(element, 'imported_from') or not element.imported_from: |
| 236 return | 269 return |
| 237 imported = element.imported_from | 270 imported = element.imported_from |
| 238 if GetPackagePath(imported['module']) == GetPackagePath(module): | 271 if GetPackagePath(imported['module']) == GetPackagePath(module): |
| 239 return | 272 return |
| 240 path = GetPackagePath(imported['module']) | 273 path = GetPackagePath(imported['module']) |
| 241 if path in _imports: | 274 if path in _imports: |
| 242 return | 275 return |
| 243 name = GetPackageName(imported['module']) | 276 name = GetPackageName(imported['module']) |
| 244 while name in _imports.values(): | 277 while name in _imports.values(): # This avoids repeated names. |
| 245 name += '_' | 278 name += '_' |
| 246 _imports[path] = name | 279 _imports[path] = name |
| 280 _mojom_imports[path] = name | |
| 247 | 281 |
| 248 | 282 |
| 283 def NeedsMojomDescriptor(module): | |
| 284 return GetPackageName(module) != _mojom_descriptors_pkg_short and \ | |
|
mattr
2015/09/17 00:37:36
I think you can use parens to break this line.
alexfandrianto
2015/10/09 17:43:55
I never knew about that in Python.
Ended up delet
| |
| 285 GetPackageName(module) != _mojom_types_pkg_short | |
| 286 | |
| 287 identifier_cache = {} | |
| 288 def StoreIdentifier(identifier, cache_name): | |
| 289 if not cache_name in identifier_cache: | |
| 290 identifier_cache[cache_name] = {} | |
| 291 identifier_cache[cache_name][identifier] = True | |
| 292 return '' | |
| 293 | |
| 294 def CheckIdentifier(identifier, cache_name): | |
| 295 if not cache_name in identifier_cache: | |
| 296 identifier_cache[cache_name] = {} | |
| 297 return identifier in identifier_cache[cache_name] | |
| 298 | |
| 249 class Generator(generator.Generator): | 299 class Generator(generator.Generator): |
| 250 go_filters = { | 300 go_filters = { |
| 251 'array': lambda kind: mojom.Array(kind), | 301 'array': lambda kind: mojom.Array(kind), |
| 252 'bit_size': GetBitSize, | 302 'bit_size': GetBitSize, |
| 253 'decode_suffix': DecodeSuffix, | 303 'decode_suffix': DecodeSuffix, |
| 254 'encode_suffix': EncodeSuffix, | 304 'encode_suffix': EncodeSuffix, |
| 255 'go_type': GetGoType, | 305 'go_type': GetGoType, |
| 256 'expression_to_text': ExpressionToText, | 306 'expression_to_text': ExpressionToText, |
| 307 'identifier': GetIdentifier, | |
| 308 'identifier_check': CheckIdentifier, | |
| 309 'identifier_store': StoreIdentifier, | |
| 257 'is_array': mojom.IsArrayKind, | 310 'is_array': mojom.IsArrayKind, |
| 258 'is_enum': mojom.IsEnumKind, | 311 'is_enum': mojom.IsEnumKind, |
| 259 'is_handle': mojom.IsAnyHandleKind, | 312 'is_handle': mojom.IsAnyHandleKind, |
| 260 'is_interface': mojom.IsInterfaceKind, | 313 'is_interface': mojom.IsInterfaceKind, |
| 261 'is_interface_request': mojom.IsInterfaceRequestKind, | 314 'is_interface_request': mojom.IsInterfaceRequestKind, |
| 262 'is_map': mojom.IsMapKind, | 315 'is_map': mojom.IsMapKind, |
| 263 'is_none_or_empty': lambda array: array == None or len(array) == 0, | 316 'is_none_or_empty': lambda array: array == None or len(array) == 0, |
| 264 'is_nullable': mojom.IsNullableKind, | 317 'is_nullable': mojom.IsNullableKind, |
| 265 'is_pointer': IsPointer, | 318 'is_pointer': IsPointer, |
| 266 'is_object': mojom.IsObjectKind, | 319 'is_object': mojom.IsObjectKind, |
| 267 'is_struct': mojom.IsStructKind, | 320 'is_struct': mojom.IsStructKind, |
| 268 'is_union': mojom.IsUnionKind, | 321 'is_union': mojom.IsUnionKind, |
| 269 'qualified': GetQualifiedName, | 322 'qualified': GetQualifiedName, |
| 323 'mojom_type': GetMojomTypeValue, | |
| 270 'name': GetNameForElement, | 324 'name': GetNameForElement, |
| 271 'package': GetPackageNameForElement, | 325 'package': GetPackageNameForElement, |
| 272 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) | 326 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) |
| 273 } | 327 } |
| 274 | 328 |
| 275 def GetParameters(self): | 329 def GetParameters(self): |
| 330 package = GetPackageName(self.module) | |
| 276 return { | 331 return { |
| 277 'enums': GetAllEnums(self.module), | 332 'enums': GetAllEnums(self.module), |
| 278 'imports': self.GetImports(), | 333 'imports': self.GetImports(), |
| 279 'interfaces': self.GetInterfaces(), | 334 'interfaces': self.GetInterfaces(), |
| 280 'package': GetPackageName(self.module), | 335 'mojom_imports': self.GetMojomImports(), |
| 336 'package': package, | |
| 281 'structs': self.GetStructs(), | 337 'structs': self.GetStructs(), |
| 282 'unions': self.GetUnions(), | 338 'need_mojom_desc': NeedsMojomDescriptor(self.module), |
|
mattr
2015/09/17 00:37:35
It seems odd we need this and the variable descpkg
alexfandrianto
2015/10/09 17:43:55
OLD:
Unfortunately, we needed both. The MojomDescr
| |
| 339 'descpkg': '%s.' % _mojom_descriptors_pkg_short if package != _mojom_descr iptors_pkg_short else '', | |
| 340 'typepkg': '%s.' % _mojom_types_pkg_short if package != _mojom_types_pkg_s hort else '', | |
| 341 'unions': self.GetUnions() | |
| 283 } | 342 } |
| 284 | 343 |
| 285 @UseJinja('go_templates/source.tmpl', filters=go_filters) | 344 @UseJinja('go_templates/source.tmpl', filters=go_filters) |
| 286 def GenerateSource(self): | 345 def GenerateSource(self): |
| 287 return self.GetParameters() | 346 return self.GetParameters() |
| 288 | 347 |
| 289 def GenerateFiles(self, args): | 348 def GenerateFiles(self, args): |
| 290 self.Write(self.GenerateSource(), os.path.join("go", "src", | 349 self.Write(self.GenerateSource(), os.path.join("go", "src", |
| 291 GetPackagePath(self.module), "%s.go" % self.module.name)) | 350 GetPackagePath(self.module), "%s.go" % self.module.name)) |
| 292 | 351 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 AddImport(self.module, field.kind) | 390 AddImport(self.module, field.kind) |
| 332 # TODO(rogulenko): add these after generating constants and struct defaults. | 391 # TODO(rogulenko): add these after generating constants and struct defaults. |
| 333 # if field.default: | 392 # if field.default: |
| 334 # AddImport(self.module, field.default) | 393 # AddImport(self.module, field.default) |
| 335 | 394 |
| 336 for enum in GetAllEnums(self.module): | 395 for enum in GetAllEnums(self.module): |
| 337 for field in enum.fields: | 396 for field in enum.fields: |
| 338 if field.value: | 397 if field.value: |
| 339 AddImport(self.module, field.value) | 398 AddImport(self.module, field.value) |
| 340 | 399 |
| 400 | |
| 401 num_user_defined_types = len(self.module.interfaces) + \ | |
|
mattr
2015/09/17 00:37:35
Interfaces define types too right? The request/re
alexfandrianto
2015/10/09 17:43:55
That's right, interfaces define types as well. Sin
| |
| 402 len(self.module.unions) + len(all_structs) + len(GetAllEnums(self.module)) | |
| 403 if num_user_defined_types > 0 \ | |
| 404 and GetPackageName(self.module) != _mojom_types_pkg_short: | |
| 405 _imports[_mojom_types_pkg] = _mojom_types_pkg_short | |
| 406 | |
| 407 if NeedsMojomDescriptor(self.module): | |
| 408 _imports[_mojom_descriptors_pkg] = _mojom_descriptors_pkg_short | |
| 409 | |
| 341 # TODO(rogulenko): add these after generating constants and struct defaults. | 410 # TODO(rogulenko): add these after generating constants and struct defaults. |
| 342 # for constant in GetAllConstants(self.module): | 411 # for constant in GetAllConstants(self.module): |
| 343 # AddImport(self.module, constant.value) | 412 # AddImport(self.module, constant.value) |
| 344 | 413 |
| 345 imports_list = [] | 414 imports_list = [] |
| 346 for i in _imports: | 415 for i in _imports: |
| 347 if i.split('/')[-1] == _imports[i]: | 416 if i.split('/')[-1] == _imports[i]: |
| 348 imports_list.append('"%s"' % i) | 417 imports_list.append('"%s"' % i) |
| 349 else: | 418 else: |
| 350 imports_list.append('%s "%s"' % (_imports[i], i)) | 419 imports_list.append('%s "%s"' % (_imports[i], i)) |
| 351 return sorted(imports_list) | 420 return sorted(imports_list) |
| 352 | 421 |
| 422 def GetMojomImports(self): | |
| 423 self.GetImports() # Can call GetImports multiple times since it only sets th e internal _imports map. | |
| 424 return _mojom_imports | |
| 425 | |
| 353 # Overrides the implementation from the base class in order to customize the | 426 # Overrides the implementation from the base class in order to customize the |
| 354 # struct and field names. | 427 # struct and field names. |
| 355 def _GetStructFromMethod(self, method): | 428 def _GetStructFromMethod(self, method): |
| 356 params_class = "%s_%s_Params" % (GetNameForElement(method.interface), | 429 params_class = "%s_%s_Params" % (GetNameForElement(method.interface), |
| 357 GetNameForElement(method)) | 430 GetNameForElement(method)) |
| 358 struct = mojom.Struct(params_class, module=method.interface.module) | 431 struct = mojom.Struct(params_class, module=method.interface.module) |
| 359 for param in method.parameters: | 432 for param in method.parameters: |
| 360 struct.AddField("in%s" % GetNameForElement(param), | 433 struct.AddField("in%s" % GetNameForElement(param), |
| 361 param.kind, param.ordinal, attributes=param.attributes) | 434 param.kind, param.ordinal, attributes=param.attributes) |
| 362 return self._AddStructComputedData(False, struct) | 435 return self._AddStructComputedData(False, struct) |
| 363 | 436 |
| 364 # Overrides the implementation from the base class in order to customize the | 437 # Overrides the implementation from the base class in order to customize the |
| 365 # struct and field names. | 438 # struct and field names. |
| 366 def _GetResponseStructFromMethod(self, method): | 439 def _GetResponseStructFromMethod(self, method): |
| 367 params_class = "%s_%s_ResponseParams" % ( | 440 params_class = "%s_%s_ResponseParams" % ( |
| 368 GetNameForElement(method.interface), GetNameForElement(method)) | 441 GetNameForElement(method.interface), GetNameForElement(method)) |
| 369 struct = mojom.Struct(params_class, module=method.interface.module) | 442 struct = mojom.Struct(params_class, module=method.interface.module) |
| 370 for param in method.response_parameters: | 443 for param in method.response_parameters: |
| 371 struct.AddField("out%s" % GetNameForElement(param), | 444 struct.AddField("out%s" % GetNameForElement(param), |
| 372 param.kind, param.ordinal, attributes=param.attributes) | 445 param.kind, param.ordinal, attributes=param.attributes) |
| 373 return self._AddStructComputedData(False, struct) | 446 return self._AddStructComputedData(False, struct) |
| OLD | NEW |