| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 dart source files from a mojom.Module.""" | 5 """Generates dart source files from a mojom.Module.""" |
| 6 | 6 |
| 7 import re | 7 import re |
| 8 | 8 |
| 9 import mojom.generate.generator as generator | 9 import mojom.generate.generator as generator |
| 10 import mojom.generate.module as mojom | 10 import mojom.generate.module as mojom |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 _kind_to_dart_decl_type = { | 44 _kind_to_dart_decl_type = { |
| 45 mojom.BOOL: "bool", | 45 mojom.BOOL: "bool", |
| 46 mojom.INT8: "int", | 46 mojom.INT8: "int", |
| 47 mojom.UINT8: "int", | 47 mojom.UINT8: "int", |
| 48 mojom.INT16: "int", | 48 mojom.INT16: "int", |
| 49 mojom.UINT16: "int", | 49 mojom.UINT16: "int", |
| 50 mojom.INT32: "int", | 50 mojom.INT32: "int", |
| 51 mojom.UINT32: "int", | 51 mojom.UINT32: "int", |
| 52 mojom.FLOAT: "double", | 52 mojom.FLOAT: "double", |
| 53 mojom.HANDLE: "core.MojoHandle", | 53 mojom.HANDLE: "core.MojoHandle", |
| 54 mojom.DCPIPE: "core.MojoHandle", | 54 mojom.DCPIPE: "core.MojoDataPipeConsumer", |
| 55 mojom.DPPIPE: "core.MojoHandle", | 55 mojom.DPPIPE: "core.MojoDataPipeProducer", |
| 56 mojom.MSGPIPE: "core.MojoHandle", | 56 mojom.MSGPIPE: "core.MojoMessagePipeEndpoint", |
| 57 mojom.SHAREDBUFFER: "core.MojoHandle", | 57 mojom.SHAREDBUFFER: "core.MojoSharedBuffer", |
| 58 mojom.NULLABLE_HANDLE: "core.MojoHandle", | 58 mojom.NULLABLE_HANDLE: "core.MojoHandle", |
| 59 mojom.NULLABLE_DCPIPE: "core.MojoHandle", | 59 mojom.NULLABLE_DCPIPE: "core.MojoDataPipeConsumer", |
| 60 mojom.NULLABLE_DPPIPE: "core.MojoHandle", | 60 mojom.NULLABLE_DPPIPE: "core.MojoDataPipeProducer", |
| 61 mojom.NULLABLE_MSGPIPE: "core.MojoHandle", | 61 mojom.NULLABLE_MSGPIPE: "core.MojoMessagePipeEndpoint", |
| 62 mojom.NULLABLE_SHAREDBUFFER: "core.MojoHandle", | 62 mojom.NULLABLE_SHAREDBUFFER: "core.MojoSharedBuffer", |
| 63 mojom.INT64: "int", | 63 mojom.INT64: "int", |
| 64 mojom.UINT64: "int", | 64 mojom.UINT64: "int", |
| 65 mojom.DOUBLE: "double", | 65 mojom.DOUBLE: "double", |
| 66 mojom.STRING: "String", | 66 mojom.STRING: "String", |
| 67 mojom.NULLABLE_STRING: "String" | 67 mojom.NULLABLE_STRING: "String" |
| 68 } | 68 } |
| 69 | 69 |
| 70 _spec_to_decode_method = { | 70 _spec_to_decode_method = { |
| 71 mojom.BOOL.spec: 'decodeBool', | 71 mojom.BOOL.spec: 'decodeBool', |
| 72 mojom.DCPIPE.spec: 'decodeHandle', | 72 mojom.DCPIPE.spec: 'decodeHandle', |
| 73 mojom.DOUBLE.spec: 'decodeDouble', | 73 mojom.DOUBLE.spec: 'decodeDouble', |
| 74 mojom.DPPIPE.spec: 'decodeHandle', | 74 mojom.DPPIPE.spec: 'decodeHandle', |
| 75 mojom.FLOAT.spec: 'decodeFloat', | 75 mojom.FLOAT.spec: 'decodeFloat', |
| 76 mojom.HANDLE.spec: 'decodeHandle', | 76 mojom.HANDLE.spec: 'decodeHandle', |
| 77 mojom.INT16.spec: 'decodeInt16', | 77 mojom.INT16.spec: 'decodeInt16', |
| 78 mojom.INT32.spec: 'decodeInt32', | 78 mojom.INT32.spec: 'decodeInt32', |
| 79 mojom.INT64.spec: 'decodeInt64', | 79 mojom.INT64.spec: 'decodeInt64', |
| 80 mojom.INT8.spec: 'decodeInt8', | 80 mojom.INT8.spec: 'decodeInt8', |
| 81 mojom.MSGPIPE.spec: 'decodeHandle', | 81 mojom.MSGPIPE.spec: 'decodeMessagePipeHandle', |
| 82 mojom.NULLABLE_DCPIPE.spec: 'decodeHandle', | 82 mojom.NULLABLE_DCPIPE.spec: 'decodeConsumerHandle', |
| 83 mojom.NULLABLE_DPPIPE.spec: 'decodeHandle', | 83 mojom.NULLABLE_DPPIPE.spec: 'decodeProducerHandle', |
| 84 mojom.NULLABLE_HANDLE.spec: 'decodeHandle', | 84 mojom.NULLABLE_HANDLE.spec: 'decodeHandle', |
| 85 mojom.NULLABLE_MSGPIPE.spec: 'decodeHandle', | 85 mojom.NULLABLE_MSGPIPE.spec: 'decodeMessagePipeHandle', |
| 86 mojom.NULLABLE_SHAREDBUFFER.spec: 'decodeHandle', | 86 mojom.NULLABLE_SHAREDBUFFER.spec: 'decodeSharedBufferHandle', |
| 87 mojom.NULLABLE_STRING.spec: 'decodeString', | 87 mojom.NULLABLE_STRING.spec: 'decodeString', |
| 88 mojom.SHAREDBUFFER.spec: 'decodeHandle', | 88 mojom.SHAREDBUFFER.spec: 'decodeSharedBufferHandle', |
| 89 mojom.STRING.spec: 'decodeString', | 89 mojom.STRING.spec: 'decodeString', |
| 90 mojom.UINT16.spec: 'decodeUint16', | 90 mojom.UINT16.spec: 'decodeUint16', |
| 91 mojom.UINT32.spec: 'decodeUint32', | 91 mojom.UINT32.spec: 'decodeUint32', |
| 92 mojom.UINT64.spec: 'decodeUint64', | 92 mojom.UINT64.spec: 'decodeUint64', |
| 93 mojom.UINT8.spec: 'decodeUint8', | 93 mojom.UINT8.spec: 'decodeUint8', |
| 94 } | 94 } |
| 95 | 95 |
| 96 _spec_to_encode_method = { | 96 _spec_to_encode_method = { |
| 97 mojom.BOOL.spec: 'encodeBool', | 97 mojom.BOOL.spec: 'encodeBool', |
| 98 mojom.DCPIPE.spec: 'encodeHandle', | 98 mojom.DCPIPE.spec: 'encodeHandle', |
| 99 mojom.DOUBLE.spec: 'encodeDouble', | 99 mojom.DOUBLE.spec: 'encodeDouble', |
| 100 mojom.DPPIPE.spec: 'encodeHandle', | 100 mojom.DPPIPE.spec: 'encodeHandle', |
| 101 mojom.FLOAT.spec: 'encodeFloat', | 101 mojom.FLOAT.spec: 'encodeFloat', |
| 102 mojom.HANDLE.spec: 'encodeHandle', | 102 mojom.HANDLE.spec: 'encodeHandle', |
| 103 mojom.INT16.spec: 'encodeInt16', | 103 mojom.INT16.spec: 'encodeInt16', |
| 104 mojom.INT32.spec: 'encodeInt32', | 104 mojom.INT32.spec: 'encodeInt32', |
| 105 mojom.INT64.spec: 'encodeInt64', | 105 mojom.INT64.spec: 'encodeInt64', |
| 106 mojom.INT8.spec: 'encodeInt8', | 106 mojom.INT8.spec: 'encodeInt8', |
| 107 mojom.MSGPIPE.spec: 'encodeHandle', | 107 mojom.MSGPIPE.spec: 'encodeMessagePipeHandle', |
| 108 mojom.NULLABLE_DCPIPE.spec: 'encodeHandle', | 108 mojom.NULLABLE_DCPIPE.spec: 'encodeConsumerHandle', |
| 109 mojom.NULLABLE_DPPIPE.spec: 'encodeHandle', | 109 mojom.NULLABLE_DPPIPE.spec: 'encodeProducerHandle', |
| 110 mojom.NULLABLE_HANDLE.spec: 'encodeHandle', | 110 mojom.NULLABLE_HANDLE.spec: 'encodeHandle', |
| 111 mojom.NULLABLE_MSGPIPE.spec: 'encodeHandle', | 111 mojom.NULLABLE_MSGPIPE.spec: 'encodeMessagePipeHandle', |
| 112 mojom.NULLABLE_SHAREDBUFFER.spec: 'encodeHandle', | 112 mojom.NULLABLE_SHAREDBUFFER.spec: 'encodeSharedBufferHandle', |
| 113 mojom.NULLABLE_STRING.spec: 'encodeString', | 113 mojom.NULLABLE_STRING.spec: 'encodeString', |
| 114 mojom.SHAREDBUFFER.spec: 'encodeHandle', | 114 mojom.SHAREDBUFFER.spec: 'encodeSharedBufferHandle', |
| 115 mojom.STRING.spec: 'encodeString', | 115 mojom.STRING.spec: 'encodeString', |
| 116 mojom.UINT16.spec: 'encodeUint16', | 116 mojom.UINT16.spec: 'encodeUint16', |
| 117 mojom.UINT32.spec: 'encodeUint32', | 117 mojom.UINT32.spec: 'encodeUint32', |
| 118 mojom.UINT64.spec: 'encodeUint64', | 118 mojom.UINT64.spec: 'encodeUint64', |
| 119 mojom.UINT8.spec: 'encodeUint8', | 119 mojom.UINT8.spec: 'encodeUint8', |
| 120 } | 120 } |
| 121 | 121 |
| 122 def GetDartType(kind): | 122 def GetDartType(kind): |
| 123 if kind.imported_from: | 123 if kind.imported_from: |
| 124 return kind.imported_from["unique_name"] + "." + kind.name | 124 return kind.imported_from["unique_name"] + "." + kind.name |
| 125 return kind.name | 125 return kind.name |
| 126 | 126 |
| 127 def DartDefaultValue(field): | 127 def DartDefaultValue(field): |
| 128 if field.default: | 128 if field.default: |
| 129 if mojom.IsStructKind(field.kind): | 129 if mojom.IsStructKind(field.kind): |
| 130 assert field.default == "default" | 130 assert field.default == "default" |
| 131 return "new %s()" % GetDartType(field.kind) | 131 return "new %s()" % GetDartType(field.kind) |
| 132 return ExpressionToText(field.default) | 132 return ExpressionToText(field.default) |
| 133 if field.kind in mojom.PRIMITIVES: | 133 if field.kind in mojom.PRIMITIVES: |
| 134 return _kind_to_dart_default_value[field.kind] | 134 return _kind_to_dart_default_value[field.kind] |
| 135 if mojom.IsStructKind(field.kind): | 135 if mojom.IsStructKind(field.kind): |
| 136 return "null" | 136 return "null" |
| 137 if mojom.IsArrayKind(field.kind): | 137 if mojom.IsArrayKind(field.kind): |
| 138 return "null" | 138 return "null" |
| 139 if mojom.IsMapKind(field.kind): | 139 if mojom.IsMapKind(field.kind): |
| 140 return "null" | 140 return "null" |
| 141 if mojom.IsInterfaceKind(field.kind) or \ | 141 if mojom.IsInterfaceKind(field.kind) or \ |
| 142 mojom.IsInterfaceRequestKind(field.kind): | 142 mojom.IsInterfaceRequestKind(field.kind): |
| 143 return _kind_to_dart_default_value[mojom.MSGPIPE] | 143 return "null" |
| 144 if mojom.IsEnumKind(field.kind): | 144 if mojom.IsEnumKind(field.kind): |
| 145 return "0" | 145 return "0" |
| 146 | 146 |
| 147 def DartDeclType(kind): | 147 def DartDeclType(kind): |
| 148 if kind in mojom.PRIMITIVES: | 148 if kind in mojom.PRIMITIVES: |
| 149 return _kind_to_dart_decl_type[kind] | 149 return _kind_to_dart_decl_type[kind] |
| 150 if mojom.IsStructKind(kind): | 150 if mojom.IsStructKind(kind): |
| 151 return GetDartType(kind) | 151 return GetDartType(kind) |
| 152 if mojom.IsArrayKind(kind): | 152 if mojom.IsArrayKind(kind): |
| 153 array_type = DartDeclType(kind.kind) | 153 array_type = DartDeclType(kind.kind) |
| 154 return "List<" + array_type + ">" | 154 return "List<" + array_type + ">" |
| 155 if mojom.IsMapKind(kind): | 155 if mojom.IsMapKind(kind): |
| 156 key_type = DartDeclType(kind.key_kind) | 156 key_type = DartDeclType(kind.key_kind) |
| 157 value_type = DartDeclType(kind.value_kind) | 157 value_type = DartDeclType(kind.value_kind) |
| 158 return "Map<"+ key_type + ", " + value_type + ">" | 158 return "Map<"+ key_type + ", " + value_type + ">" |
| 159 if mojom.IsInterfaceKind(kind) or \ | 159 if mojom.IsInterfaceKind(kind) or \ |
| 160 mojom.IsInterfaceRequestKind(kind): | 160 mojom.IsInterfaceRequestKind(kind): |
| 161 return _kind_to_dart_decl_type[mojom.MSGPIPE] | 161 return "Object" |
| 162 if mojom.IsEnumKind(kind): | 162 if mojom.IsEnumKind(kind): |
| 163 return "int" | 163 return "int" |
| 164 | 164 |
| 165 def NameToComponent(name): | 165 def NameToComponent(name): |
| 166 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> | 166 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> |
| 167 # HTTP_Entry2_FooBar) | 167 # HTTP_Entry2_FooBar) |
| 168 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name) | 168 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name) |
| 169 # insert '_' between non upper and start of upper blocks (e.g., | 169 # insert '_' between non upper and start of upper blocks (e.g., |
| 170 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar) | 170 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar) |
| 171 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) | 171 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 flags_to_set = [] | 235 flags_to_set = [] |
| 236 if mojom.IsNullableKind(kind): | 236 if mojom.IsNullableKind(kind): |
| 237 flags_to_set.append(ARRAY_NULLABLE) | 237 flags_to_set.append(ARRAY_NULLABLE) |
| 238 if mojom.IsNullableKind(kind.kind): | 238 if mojom.IsNullableKind(kind.kind): |
| 239 flags_to_set.append(ELEMENT_NULLABLE) | 239 flags_to_set.append(ELEMENT_NULLABLE) |
| 240 | 240 |
| 241 if not flags_to_set: | 241 if not flags_to_set: |
| 242 flags_to_set = [NOTHING_NULLABLE] | 242 flags_to_set = [NOTHING_NULLABLE] |
| 243 return ' | '.join(flags_to_set) | 243 return ' | '.join(flags_to_set) |
| 244 | 244 |
| 245 def AppendEncodeDecodeParams(initial_params, kind, bit): | 245 def AppendDecodeParams(initial_params, kind, bit): |
| 246 """ Appends standard parameters for decode calls. """ |
| 247 params = list(initial_params) |
| 248 if (kind == mojom.BOOL): |
| 249 params.append(str(bit)) |
| 250 if mojom.IsReferenceKind(kind): |
| 251 if mojom.IsArrayKind(kind): |
| 252 params.append(GetArrayNullabilityFlags(kind)) |
| 253 else: |
| 254 params.append(GetDartTrueFalse(mojom.IsNullableKind(kind))) |
| 255 if mojom.IsInterfaceKind(kind): |
| 256 params.append('%sClient.newFromEndpoint' % GetDartType(kind)) |
| 257 if mojom.IsArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): |
| 258 params.append('%sClient.newFromEndpoint' % GetDartType(kind.kind)) |
| 259 if mojom.IsInterfaceRequestKind(kind): |
| 260 params.append('%sInterface.newFromEndpoint' % GetDartType(kind.kind)) |
| 261 if mojom.IsArrayKind(kind) and mojom.IsInterfaceRequestKind(kind.kind): |
| 262 params.append('%sInterface.newFromEndpoint' % GetDartType(kind.kind.kind)) |
| 263 if mojom.IsArrayKind(kind): |
| 264 params.append(GetArrayExpectedLength(kind)) |
| 265 return params |
| 266 |
| 267 def AppendEncodeParams(initial_params, kind, bit): |
| 246 """ Appends standard parameters shared between encode and decode calls. """ | 268 """ Appends standard parameters shared between encode and decode calls. """ |
| 247 params = list(initial_params) | 269 params = list(initial_params) |
| 248 if (kind == mojom.BOOL): | 270 if (kind == mojom.BOOL): |
| 249 params.append(str(bit)) | 271 params.append(str(bit)) |
| 250 if mojom.IsReferenceKind(kind): | 272 if mojom.IsReferenceKind(kind): |
| 251 if mojom.IsArrayKind(kind): | 273 if mojom.IsArrayKind(kind): |
| 252 params.append(GetArrayNullabilityFlags(kind)) | 274 params.append(GetArrayNullabilityFlags(kind)) |
| 253 else: | 275 else: |
| 254 params.append(GetDartTrueFalse(mojom.IsNullableKind(kind))) | 276 params.append(GetDartTrueFalse(mojom.IsNullableKind(kind))) |
| 255 if mojom.IsArrayKind(kind): | 277 if mojom.IsArrayKind(kind): |
| 256 params.append(GetArrayExpectedLength(kind)) | 278 params.append(GetArrayExpectedLength(kind)) |
| 257 return params | 279 return params |
| 258 | 280 |
| 259 def DecodeMethod(kind, offset, bit): | 281 def DecodeMethod(kind, offset, bit): |
| 260 def _DecodeMethodName(kind): | 282 def _DecodeMethodName(kind): |
| 261 if mojom.IsArrayKind(kind): | 283 if mojom.IsArrayKind(kind): |
| 262 return _DecodeMethodName(kind.kind) + 'Array' | 284 return _DecodeMethodName(kind.kind) + 'Array' |
| 263 if mojom.IsEnumKind(kind): | 285 if mojom.IsEnumKind(kind): |
| 264 return _DecodeMethodName(mojom.INT32) | 286 return _DecodeMethodName(mojom.INT32) |
| 265 if mojom.IsInterfaceRequestKind(kind): | 287 if mojom.IsInterfaceRequestKind(kind): |
| 266 return 'decodeHandle' | 288 return 'decodeInterfaceRequest' |
| 267 if mojom.IsInterfaceKind(kind): | 289 if mojom.IsInterfaceKind(kind): |
| 268 return 'decodeHandle' | 290 return 'decodeServiceInterface' |
| 269 return _spec_to_decode_method[kind.spec] | 291 return _spec_to_decode_method[kind.spec] |
| 270 methodName = _DecodeMethodName(kind) | 292 methodName = _DecodeMethodName(kind) |
| 271 params = AppendEncodeDecodeParams([ str(offset) ], kind, bit) | 293 params = AppendDecodeParams([ str(offset) ], kind, bit) |
| 272 return '%s(%s)' % (methodName, ', '.join(params)) | 294 return '%s(%s)' % (methodName, ', '.join(params)) |
| 273 | 295 |
| 274 def EncodeMethod(kind, variable, offset, bit): | 296 def EncodeMethod(kind, variable, offset, bit): |
| 275 def _EncodeMethodName(kind): | 297 def _EncodeMethodName(kind): |
| 276 if mojom.IsStructKind(kind): | 298 if mojom.IsStructKind(kind): |
| 277 return 'encodeStruct' | 299 return 'encodeStruct' |
| 278 if mojom.IsArrayKind(kind): | 300 if mojom.IsArrayKind(kind): |
| 279 return _EncodeMethodName(kind.kind) + 'Array' | 301 return _EncodeMethodName(kind.kind) + 'Array' |
| 280 if mojom.IsEnumKind(kind): | 302 if mojom.IsEnumKind(kind): |
| 281 return _EncodeMethodName(mojom.INT32) | 303 return _EncodeMethodName(mojom.INT32) |
| 282 if mojom.IsInterfaceRequestKind(kind): | 304 if mojom.IsInterfaceRequestKind(kind): |
| 283 return 'encodeHandle' | 305 return 'encodeInterfaceRequest' |
| 284 if mojom.IsInterfaceKind(kind): | 306 if mojom.IsInterfaceKind(kind): |
| 285 return 'encodeHandle' | 307 return 'encodeInterface' |
| 286 return _spec_to_encode_method[kind.spec] | 308 return _spec_to_encode_method[kind.spec] |
| 287 methodName = _EncodeMethodName(kind) | 309 methodName = _EncodeMethodName(kind) |
| 288 params = AppendEncodeDecodeParams([ variable, str(offset) ], kind, bit) | 310 params = AppendEncodeParams([ variable, str(offset) ], kind, bit) |
| 289 return '%s(%s)' % (methodName, ', '.join(params)) | 311 return '%s(%s)' % (methodName, ', '.join(params)) |
| 290 | 312 |
| 291 def TranslateConstants(token): | 313 def TranslateConstants(token): |
| 292 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): | 314 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): |
| 293 # Both variable and enum constants are constructed like: | 315 # Both variable and enum constants are constructed like: |
| 294 # NamespaceUid.Struct.Enum_CONSTANT_NAME | 316 # NamespaceUid.Struct.Enum_CONSTANT_NAME |
| 295 name = "" | 317 name = "" |
| 296 if token.imported_from: | 318 if token.imported_from: |
| 297 name = token.imported_from["unique_name"] + "." | 319 name = token.imported_from["unique_name"] + "." |
| 298 if token.parent_kind: | 320 if token.parent_kind: |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 interface_to_import[name] = each_import["unique_name"] + "." + name | 432 interface_to_import[name] = each_import["unique_name"] + "." + name |
| 411 return interface_to_import | 433 return interface_to_import |
| 412 | 434 |
| 413 def ImportedFrom(self): | 435 def ImportedFrom(self): |
| 414 interface_to_import = {} | 436 interface_to_import = {} |
| 415 for each_import in self.module.imports: | 437 for each_import in self.module.imports: |
| 416 for each_interface in each_import["module"].interfaces: | 438 for each_interface in each_import["module"].interfaces: |
| 417 name = each_interface.name | 439 name = each_interface.name |
| 418 interface_to_import[name] = each_import["unique_name"] + "." | 440 interface_to_import[name] = each_import["unique_name"] + "." |
| 419 return interface_to_import | 441 return interface_to_import |
| OLD | NEW |