Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_dart_generator.py

Issue 830593003: Update mojo sdk to rev 9fbbc4f0fef1187312316c0ed992342474e139f1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cherry-pick mojo 9d3b8dd17f12d20035a14737fdc38dd926890ff8 Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 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
7 import re
6 8
7 import mojom.generate.generator as generator 9 import mojom.generate.generator as generator
8 import mojom.generate.module as mojom 10 import mojom.generate.module as mojom
9 import mojom.generate.pack as pack 11 import mojom.generate.pack as pack
10 from mojom.generate.template_expander import UseJinja 12 from mojom.generate.template_expander import UseJinja
11 13
14 GENERATOR_PREFIX = 'dart'
15
16 _HEADER_SIZE = 8
17
12 _kind_to_dart_default_value = { 18 _kind_to_dart_default_value = {
13 mojom.BOOL: "false", 19 mojom.BOOL: "false",
14 mojom.INT8: "0", 20 mojom.INT8: "0",
15 mojom.UINT8: "0", 21 mojom.UINT8: "0",
16 mojom.INT16: "0", 22 mojom.INT16: "0",
17 mojom.UINT16: "0", 23 mojom.UINT16: "0",
18 mojom.INT32: "0", 24 mojom.INT32: "0",
19 mojom.UINT32: "0", 25 mojom.UINT32: "0",
20 mojom.FLOAT: "0.0", 26 mojom.FLOAT: "0.0",
21 mojom.HANDLE: "null", 27 mojom.HANDLE: "null",
22 mojom.DCPIPE: "null", 28 mojom.DCPIPE: "null",
23 mojom.DPPIPE: "null", 29 mojom.DPPIPE: "null",
24 mojom.MSGPIPE: "null", 30 mojom.MSGPIPE: "null",
25 mojom.SHAREDBUFFER: "null", 31 mojom.SHAREDBUFFER: "null",
26 mojom.NULLABLE_HANDLE: "null", 32 mojom.NULLABLE_HANDLE: "null",
27 mojom.NULLABLE_DCPIPE: "null", 33 mojom.NULLABLE_DCPIPE: "null",
28 mojom.NULLABLE_DPPIPE: "null", 34 mojom.NULLABLE_DPPIPE: "null",
29 mojom.NULLABLE_MSGPIPE: "null", 35 mojom.NULLABLE_MSGPIPE: "null",
30 mojom.NULLABLE_SHAREDBUFFER: "null", 36 mojom.NULLABLE_SHAREDBUFFER: "null",
31 mojom.INT64: "0", 37 mojom.INT64: "0",
32 mojom.UINT64: "0", 38 mojom.UINT64: "0",
33 mojom.DOUBLE: "0.0", 39 mojom.DOUBLE: "0.0",
34 mojom.STRING: "null", 40 mojom.STRING: "null",
35 mojom.NULLABLE_STRING: "null" 41 mojom.NULLABLE_STRING: "null"
36 } 42 }
37 43
38
39 _kind_to_dart_decl_type = { 44 _kind_to_dart_decl_type = {
40 mojom.BOOL: "bool", 45 mojom.BOOL: "bool",
41 mojom.INT8: "int", 46 mojom.INT8: "int",
42 mojom.UINT8: "int", 47 mojom.UINT8: "int",
43 mojom.INT16: "int", 48 mojom.INT16: "int",
44 mojom.UINT16: "int", 49 mojom.UINT16: "int",
45 mojom.INT32: "int", 50 mojom.INT32: "int",
46 mojom.UINT32: "int", 51 mojom.UINT32: "int",
47 mojom.FLOAT: "double", 52 mojom.FLOAT: "double",
48 mojom.HANDLE: "core.RawMojoHandle", 53 mojom.HANDLE: "core.MojoHandle",
49 mojom.DCPIPE: "core.RawMojoHandle", 54 mojom.DCPIPE: "core.MojoHandle",
50 mojom.DPPIPE: "core.RawMojoHandle", 55 mojom.DPPIPE: "core.MojoHandle",
51 mojom.MSGPIPE: "core.RawMojoHandle", 56 mojom.MSGPIPE: "core.MojoHandle",
52 mojom.SHAREDBUFFER: "core.RawMojoHandle", 57 mojom.SHAREDBUFFER: "core.MojoHandle",
53 mojom.NULLABLE_HANDLE: "core.RawMojoHandle", 58 mojom.NULLABLE_HANDLE: "core.MojoHandle",
54 mojom.NULLABLE_DCPIPE: "core.RawMojoHandle", 59 mojom.NULLABLE_DCPIPE: "core.MojoHandle",
55 mojom.NULLABLE_DPPIPE: "core.RawMojoHandle", 60 mojom.NULLABLE_DPPIPE: "core.MojoHandle",
56 mojom.NULLABLE_MSGPIPE: "core.RawMojoHandle", 61 mojom.NULLABLE_MSGPIPE: "core.MojoHandle",
57 mojom.NULLABLE_SHAREDBUFFER: "core.RawMojoHandle", 62 mojom.NULLABLE_SHAREDBUFFER: "core.MojoHandle",
58 mojom.INT64: "int", 63 mojom.INT64: "int",
59 mojom.UINT64: "int", 64 mojom.UINT64: "int",
60 mojom.DOUBLE: "double", 65 mojom.DOUBLE: "double",
61 mojom.STRING: "String", 66 mojom.STRING: "String",
62 mojom.NULLABLE_STRING: "String" 67 mojom.NULLABLE_STRING: "String"
63 } 68 }
64 69
70 _spec_to_decode_method = {
71 mojom.BOOL.spec: 'decodeBool',
72 mojom.DCPIPE.spec: 'decodeHandle',
73 mojom.DOUBLE.spec: 'decodeDouble',
74 mojom.DPPIPE.spec: 'decodeHandle',
75 mojom.FLOAT.spec: 'decodeFloat',
76 mojom.HANDLE.spec: 'decodeHandle',
77 mojom.INT16.spec: 'decodeInt16',
78 mojom.INT32.spec: 'decodeInt32',
79 mojom.INT64.spec: 'decodeInt64',
80 mojom.INT8.spec: 'decodeInt8',
81 mojom.MSGPIPE.spec: 'decodeHandle',
82 mojom.NULLABLE_DCPIPE.spec: 'decodeHandle',
83 mojom.NULLABLE_DPPIPE.spec: 'decodeHandle',
84 mojom.NULLABLE_HANDLE.spec: 'decodeHandle',
85 mojom.NULLABLE_MSGPIPE.spec: 'decodeHandle',
86 mojom.NULLABLE_SHAREDBUFFER.spec: 'decodeHandle',
87 mojom.NULLABLE_STRING.spec: 'decodeString',
88 mojom.SHAREDBUFFER.spec: 'decodeHandle',
89 mojom.STRING.spec: 'decodeString',
90 mojom.UINT16.spec: 'decodeUint16',
91 mojom.UINT32.spec: 'decodeUint32',
92 mojom.UINT64.spec: 'decodeUint64',
93 mojom.UINT8.spec: 'decodeUint8',
94 }
65 95
66 def DartType(kind): 96 _spec_to_encode_method = {
97 mojom.BOOL.spec: 'encodeBool',
98 mojom.DCPIPE.spec: 'encodeHandle',
99 mojom.DOUBLE.spec: 'encodeDouble',
100 mojom.DPPIPE.spec: 'encodeHandle',
101 mojom.FLOAT.spec: 'encodeFloat',
102 mojom.HANDLE.spec: 'encodeHandle',
103 mojom.INT16.spec: 'encodeInt16',
104 mojom.INT32.spec: 'encodeInt32',
105 mojom.INT64.spec: 'encodeInt64',
106 mojom.INT8.spec: 'encodeInt8',
107 mojom.MSGPIPE.spec: 'encodeHandle',
108 mojom.NULLABLE_DCPIPE.spec: 'encodeHandle',
109 mojom.NULLABLE_DPPIPE.spec: 'encodeHandle',
110 mojom.NULLABLE_HANDLE.spec: 'encodeHandle',
111 mojom.NULLABLE_MSGPIPE.spec: 'encodeHandle',
112 mojom.NULLABLE_SHAREDBUFFER.spec: 'encodeHandle',
113 mojom.NULLABLE_STRING.spec: 'encodeString',
114 mojom.SHAREDBUFFER.spec: 'encodeHandle',
115 mojom.STRING.spec: 'encodeString',
116 mojom.UINT16.spec: 'encodeUint16',
117 mojom.UINT32.spec: 'encodeUint32',
118 mojom.UINT64.spec: 'encodeUint64',
119 mojom.UINT8.spec: 'encodeUint8',
120 }
121
122 def GetDartType(kind):
67 if kind.imported_from: 123 if kind.imported_from:
68 return kind.imported_from["unique_name"] + "." + kind.name 124 return kind.imported_from["unique_name"] + "." + kind.name
69 return kind.name 125 return kind.name
70 126
71
72 def DartDefaultValue(field): 127 def DartDefaultValue(field):
73 if field.default: 128 if field.default:
74 if mojom.IsStructKind(field.kind): 129 if mojom.IsStructKind(field.kind):
75 assert field.default == "default" 130 assert field.default == "default"
76 return "new %s()" % DartType(field.kind) 131 return "new %s()" % GetDartType(field.kind)
77 return ExpressionToText(field.default) 132 return ExpressionToText(field.default)
78 if field.kind in mojom.PRIMITIVES: 133 if field.kind in mojom.PRIMITIVES:
79 return _kind_to_dart_default_value[field.kind] 134 return _kind_to_dart_default_value[field.kind]
80 if mojom.IsStructKind(field.kind): 135 if mojom.IsStructKind(field.kind):
81 return "null" 136 return "null"
82 if mojom.IsArrayKind(field.kind): 137 if mojom.IsArrayKind(field.kind):
83 return "null" 138 return "null"
84 if mojom.IsMapKind(field.kind): 139 if mojom.IsMapKind(field.kind):
85 return "null" 140 return "null"
86 if mojom.IsInterfaceKind(field.kind) or \ 141 if mojom.IsInterfaceKind(field.kind) or \
87 mojom.IsInterfaceRequestKind(field.kind): 142 mojom.IsInterfaceRequestKind(field.kind):
88 return _kind_to_dart_default_value[mojom.MSGPIPE] 143 return _kind_to_dart_default_value[mojom.MSGPIPE]
89 if mojom.IsEnumKind(field.kind): 144 if mojom.IsEnumKind(field.kind):
90 return "0" 145 return "0"
91 146
92
93 def DartDeclType(kind): 147 def DartDeclType(kind):
94 if kind in mojom.PRIMITIVES: 148 if kind in mojom.PRIMITIVES:
95 return _kind_to_dart_decl_type[kind] 149 return _kind_to_dart_decl_type[kind]
96 if mojom.IsStructKind(kind): 150 if mojom.IsStructKind(kind):
97 return DartType(kind) 151 return GetDartType(kind)
98 if mojom.IsArrayKind(kind): 152 if mojom.IsArrayKind(kind):
99 array_type = DartDeclType(kind.kind) 153 array_type = DartDeclType(kind.kind)
100 return "List<" + array_type + ">" 154 return "List<" + array_type + ">"
101 if mojom.IsMapKind(kind): 155 if mojom.IsMapKind(kind):
102 key_type = DartDeclType(kind.key_kind) 156 key_type = DartDeclType(kind.key_kind)
103 value_type = DartDeclType(kind.value_kind) 157 value_type = DartDeclType(kind.value_kind)
104 return "Map<"+ key_type + ", " + value_type + ">" 158 return "Map<"+ key_type + ", " + value_type + ">"
105 if mojom.IsInterfaceKind(kind) or \ 159 if mojom.IsInterfaceKind(kind) or \
106 mojom.IsInterfaceRequestKind(kind): 160 mojom.IsInterfaceRequestKind(kind):
107 return _kind_to_dart_decl_type[mojom.MSGPIPE] 161 return _kind_to_dart_decl_type[mojom.MSGPIPE]
108 if mojom.IsEnumKind(kind): 162 if mojom.IsEnumKind(kind):
109 return "int" 163 return "int"
110 164
111 def DartPayloadSize(packed): 165 def NameToComponent(name):
112 packed_fields = packed.packed_fields 166 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar ->
113 if not packed_fields: 167 # HTTP_Entry2_FooBar)
114 return 0 168 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name)
115 last_field = packed_fields[-1] 169 # insert '_' between non upper and start of upper blocks (e.g.,
116 offset = last_field.offset + last_field.size 170 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar)
117 pad = pack.GetPad(offset, 8) 171 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name)
118 return offset + pad 172 return [x.lower() for x in name.split('_')]
119 173
174 def UpperCamelCase(name):
175 return ''.join([x.capitalize() for x in NameToComponent(name)])
120 176
121 _kind_to_codec_type = { 177 def CamelCase(name):
122 mojom.BOOL: "bindings.Uint8", 178 uccc = UpperCamelCase(name)
123 mojom.INT8: "bindings.Int8", 179 return uccc[0].lower() + uccc[1:]
124 mojom.UINT8: "bindings.Uint8",
125 mojom.INT16: "bindings.Int16",
126 mojom.UINT16: "bindings.Uint16",
127 mojom.INT32: "bindings.Int32",
128 mojom.UINT32: "bindings.Uint32",
129 mojom.FLOAT: "bindings.Float",
130 mojom.HANDLE: "bindings.Handle",
131 mojom.DCPIPE: "bindings.Handle",
132 mojom.DPPIPE: "bindings.Handle",
133 mojom.MSGPIPE: "bindings.Handle",
134 mojom.SHAREDBUFFER: "bindings.Handle",
135 mojom.NULLABLE_HANDLE: "bindings.NullableHandle",
136 mojom.NULLABLE_DCPIPE: "bindings.NullableHandle",
137 mojom.NULLABLE_DPPIPE: "bindings.NullableHandle",
138 mojom.NULLABLE_MSGPIPE: "bindings.NullableHandle",
139 mojom.NULLABLE_SHAREDBUFFER: "bindings.NullableHandle",
140 mojom.INT64: "bindings.Int64",
141 mojom.UINT64: "bindings.Uint64",
142 mojom.DOUBLE: "bindings.Double",
143 mojom.STRING: "bindings.MojoString",
144 mojom.NULLABLE_STRING: "bindings.NullableMojoString",
145 }
146 180
181 def ConstantStyle(name):
182 components = NameToComponent(name)
183 if components[0] == 'k' and len(components) > 1:
184 components = components[1:]
185 # variable cannot starts with a digit.
186 if components[0][0].isdigit():
187 components[0] = '_' + components[0]
188 return '_'.join([x.upper() for x in components])
147 189
148 def CodecType(kind): 190 def GetNameForElement(element):
149 if kind in mojom.PRIMITIVES: 191 if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element) or
150 return _kind_to_codec_type[kind] 192 mojom.IsStructKind(element)):
151 if mojom.IsStructKind(kind): 193 return UpperCamelCase(element.name)
152 pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ 194 if mojom.IsInterfaceRequestKind(element):
153 else "PointerTo" 195 return GetNameForElement(element.kind)
154 return "new bindings.%s(%s)" % (pointer_type, DartType(kind)) 196 if isinstance(element, (mojom.Method,
197 mojom.Parameter,
198 mojom.Field)):
199 return CamelCase(element.name)
200 if isinstance(element, mojom.EnumValue):
201 return (GetNameForElement(element.enum) + '.' +
202 ConstantStyle(element.name))
203 if isinstance(element, (mojom.NamedValue,
204 mojom.Constant,
205 mojom.EnumField)):
206 return ConstantStyle(element.name)
207 raise Exception('Unexpected element: %s' % element)
208
209 def GetInterfaceResponseName(method):
210 return UpperCamelCase(method.name + 'Response')
211
212 def GetResponseStructFromMethod(method):
213 return generator.GetDataHeader(
214 False, generator.GetResponseStructFromMethod(method))
215
216 def GetStructFromMethod(method):
217 return generator.GetDataHeader(
218 False, generator.GetStructFromMethod(method))
219
220 def GetDartTrueFalse(value):
221 return 'true' if value else 'false'
222
223 def GetArrayNullabilityFlags(kind):
224 """Returns nullability flags for an array type, see codec.dart.
225
226 As we have dedicated decoding functions for arrays, we have to pass
227 nullability information about both the array itself, as well as the array
228 element type there.
229 """
230 assert mojom.IsArrayKind(kind)
231 ARRAY_NULLABLE = 'bindings.kArrayNullable'
232 ELEMENT_NULLABLE = 'bindings.kElementNullable'
233 NOTHING_NULLABLE = 'bindings.kNothingNullable'
234
235 flags_to_set = []
236 if mojom.IsNullableKind(kind):
237 flags_to_set.append(ARRAY_NULLABLE)
238 if mojom.IsNullableKind(kind.kind):
239 flags_to_set.append(ELEMENT_NULLABLE)
240
241 if not flags_to_set:
242 flags_to_set = [NOTHING_NULLABLE]
243 return ' | '.join(flags_to_set)
244
245 def AppendEncodeDecodeParams(initial_params, kind, bit):
246 """ Appends standard parameters shared between encode and 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)))
155 if mojom.IsArrayKind(kind): 255 if mojom.IsArrayKind(kind):
156 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" 256 params.append(GetArrayExpectedLength(kind))
157 array_length = "" if kind.length is None else ", %d" % kind.length 257 return params
158 element_type = ElementCodecType(kind.kind)
159 return "new bindings.%s(%s%s)" % (array_type, element_type, array_length)
160 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
161 return CodecType(mojom.MSGPIPE)
162 if mojom.IsEnumKind(kind):
163 return _kind_to_codec_type[mojom.INT32]
164 if mojom.IsMapKind(kind):
165 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf"
166 key_type = ElementCodecType(kind.key_kind)
167 value_type = ElementCodecType(kind.value_kind)
168 return "new bindings.%s(%s, %s)" % (map_type, key_type, value_type)
169 return kind
170 258
171 def ElementCodecType(kind): 259 def DecodeMethod(kind, offset, bit):
172 return "bindings.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) 260 def _DecodeMethodName(kind):
261 if mojom.IsArrayKind(kind):
262 return _DecodeMethodName(kind.kind) + 'Array'
263 if mojom.IsEnumKind(kind):
264 return _DecodeMethodName(mojom.INT32)
265 if mojom.IsInterfaceRequestKind(kind):
266 return 'decodeHandle'
267 if mojom.IsInterfaceKind(kind):
268 return 'decodeHandle'
269 return _spec_to_decode_method[kind.spec]
270 methodName = _DecodeMethodName(kind)
271 params = AppendEncodeDecodeParams([ str(offset) ], kind, bit)
272 return '%s(%s)' % (methodName, ', '.join(params))
173 273
174 def DartDecodeSnippet(kind): 274 def EncodeMethod(kind, variable, offset, bit):
175 if kind in mojom.PRIMITIVES: 275 def _EncodeMethodName(kind):
176 return "decodeStruct(%s)" % CodecType(kind) 276 if mojom.IsStructKind(kind):
177 if mojom.IsStructKind(kind): 277 return 'encodeStruct'
178 return "decodeStructPointer(%s)" % DartType(kind) 278 if mojom.IsArrayKind(kind):
179 if mojom.IsMapKind(kind): 279 return _EncodeMethodName(kind.kind) + 'Array'
180 return "decodeMapPointer(%s, %s)" % \ 280 if mojom.IsEnumKind(kind):
181 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) 281 return _EncodeMethodName(mojom.INT32)
182 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): 282 if mojom.IsInterfaceRequestKind(kind):
183 return "decodeArrayPointer(bindings.PackedBool)" 283 return 'encodeHandle'
184 if mojom.IsArrayKind(kind): 284 if mojom.IsInterfaceKind(kind):
185 return "decodeArrayPointer(%s)" % CodecType(kind.kind) 285 return 'encodeHandle'
186 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): 286 return _spec_to_encode_method[kind.spec]
187 return DartDecodeSnippet(mojom.MSGPIPE) 287 methodName = _EncodeMethodName(kind)
188 if mojom.IsEnumKind(kind): 288 params = AppendEncodeDecodeParams([ variable, str(offset) ], kind, bit)
189 return DartDecodeSnippet(mojom.INT32) 289 return '%s(%s)' % (methodName, ', '.join(params))
190
191
192 def DartEncodeSnippet(kind):
193 if kind in mojom.PRIMITIVES:
194 return "encodeStruct(%s, " % CodecType(kind)
195 if mojom.IsStructKind(kind):
196 return "encodeStructPointer(%s, " % DartType(kind)
197 if mojom.IsMapKind(kind):
198 return "encodeMapPointer(%s, %s, " % \
199 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind))
200 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
201 return "encodeArrayPointer(bindings.PackedBool, ";
202 if mojom.IsArrayKind(kind):
203 return "encodeArrayPointer(%s, " % CodecType(kind.kind)
204 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
205 return DartEncodeSnippet(mojom.MSGPIPE)
206 if mojom.IsEnumKind(kind):
207 return DartEncodeSnippet(mojom.INT32)
208
209 290
210 def TranslateConstants(token): 291 def TranslateConstants(token):
211 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): 292 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)):
212 # Both variable and enum constants are constructed like: 293 # Both variable and enum constants are constructed like:
213 # NamespaceUid.Struct.Enum_CONSTANT_NAME 294 # NamespaceUid.Struct.Enum_CONSTANT_NAME
214 name = "" 295 name = ""
215 if token.imported_from: 296 if token.imported_from:
216 name = token.imported_from["unique_name"] + "." 297 name = token.imported_from["unique_name"] + "."
217 if token.parent_kind: 298 if token.parent_kind:
218 name = name + token.parent_kind.name + "." 299 name = name + token.parent_kind.name + "."
219 if isinstance(token, mojom.EnumValue): 300 if isinstance(token, mojom.EnumValue):
220 name = name + token.enum.name + "_" 301 name = name + token.enum.name + "_"
221 return name + token.name 302 return name + token.name
222 303
223 if isinstance(token, mojom.BuiltinValue): 304 if isinstance(token, mojom.BuiltinValue):
224 if token.value == "double.INFINITY" or token.value == "float.INFINITY": 305 if token.value == "double.INFINITY" or token.value == "float.INFINITY":
225 return "double.INFINITY"; 306 return "double.INFINITY";
226 if token.value == "double.NEGATIVE_INFINITY" or \ 307 if token.value == "double.NEGATIVE_INFINITY" or \
227 token.value == "float.NEGATIVE_INFINITY": 308 token.value == "float.NEGATIVE_INFINITY":
228 return "double.NEGATIVE_INFINITY"; 309 return "double.NEGATIVE_INFINITY";
229 if token.value == "double.NAN" or token.value == "float.NAN": 310 if token.value == "double.NAN" or token.value == "float.NAN":
230 return "double.NAN"; 311 return "double.NAN";
231 312
232 # Strip leading '+'. 313 # Strip leading '+'.
233 if token[0] == '+': 314 if token[0] == '+':
234 token = token[1:] 315 token = token[1:]
235 316
236 return token 317 return token
237 318
238
239 def ExpressionToText(value): 319 def ExpressionToText(value):
240 return TranslateConstants(value) 320 return TranslateConstants(value)
241 321
322 def GetArrayKind(kind, size = None):
323 if size is None:
324 return mojom.Array(kind)
325 else:
326 array = mojom.Array(kind, 0)
327 array.dart_map_size = size
328 return array
329
330 def GetArrayExpectedLength(kind):
331 if mojom.IsArrayKind(kind) and kind.length is not None:
332 return getattr(kind, 'dart_map_size', str(kind.length))
333 else:
334 return 'bindings.kUnspecifiedArrayLength'
335
336 def IsPointerArrayKind(kind):
337 if not mojom.IsArrayKind(kind):
338 return False
339 sub_kind = kind.kind
340 return mojom.IsObjectKind(sub_kind)
242 341
243 class Generator(generator.Generator): 342 class Generator(generator.Generator):
244 343
245 dart_filters = { 344 dart_filters = {
246 "default_value": DartDefaultValue, 345 'array_expected_length': GetArrayExpectedLength,
247 "payload_size": DartPayloadSize, 346 'array': GetArrayKind,
248 "decode_snippet": DartDecodeSnippet, 347 'decode_method': DecodeMethod,
249 "encode_snippet": DartEncodeSnippet, 348 'default_value': DartDefaultValue,
250 "expression_to_text": ExpressionToText, 349 'encode_method': EncodeMethod,
251 "dart_decl_type": DartDeclType, 350 'expression_to_text': ExpressionToText,
252 "stylize_method": generator.StudlyCapsToCamel, 351 'is_handle': mojom.IsNonInterfaceHandleKind,
352 'is_map_kind': mojom.IsMapKind,
353 'is_nullable_kind': mojom.IsNullableKind,
354 'is_pointer_array_kind': IsPointerArrayKind,
355 'is_struct_kind': mojom.IsStructKind,
356 'dart_true_false': GetDartTrueFalse,
357 'dart_type': DartDeclType,
358 'name': GetNameForElement,
359 'interface_response_name': GetInterfaceResponseName,
360 'response_struct_from_method': GetResponseStructFromMethod,
361 'struct_from_method': GetStructFromMethod,
362 'upper_camel_case': UpperCamelCase,
363 'struct_size': lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
253 } 364 }
254 365
255 def GetParameters(self): 366 def GetParameters(self):
256 return { 367 return {
257 "namespace": self.module.namespace, 368 "namespace": self.module.namespace,
258 "imports": self.GetImports(), 369 "imports": self.GetImports(),
259 "kinds": self.module.kinds, 370 "kinds": self.module.kinds,
260 "enums": self.module.enums, 371 "enums": self.module.enums,
261 "module": self.module, 372 "module": self.module,
262 "structs": self.GetStructs() + self.GetStructsFromMethods(), 373 "structs": self.GetStructs() + self.GetStructsFromMethods(),
263 "interfaces": self.module.interfaces, 374 "interfaces": self.module.interfaces,
264 "imported_interfaces": self.GetImportedInterfaces(), 375 "imported_interfaces": self.GetImportedInterfaces(),
265 "imported_from": self.ImportedFrom(), 376 "imported_from": self.ImportedFrom(),
266 } 377 }
267 378
268 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) 379 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters)
269 def GenerateLibModule(self): 380 def GenerateLibModule(self):
270 return self.GetParameters() 381 return self.GetParameters()
271 382
272 def GenerateFiles(self, args): 383 def GenerateFiles(self, args):
273 self.Write(self.GenerateLibModule(), 384 self.Write(self.GenerateLibModule(),
274 self.MatchMojomFilePath("%s.dart" % self.module.name)) 385 self.MatchMojomFilePath("%s.dart" % self.module.name))
275 386
276 def GetImports(self): 387 def GetImports(self):
277 used_names = set() 388 used_names = set()
278 for each_import in self.module.imports: 389 for each_import in self.module.imports:
279 simple_name = each_import["module_name"].split(".")[0] 390 simple_name = each_import["module_name"].split(".")[0]
280 391
281 # Since each import is assigned a variable in JS, they need to have unique 392 # Since each import is assigned a library in Dart, they need to have
282 # names. 393 # unique names.
283 unique_name = simple_name 394 unique_name = simple_name
284 counter = 0 395 counter = 0
285 while unique_name in used_names: 396 while unique_name in used_names:
286 counter += 1 397 counter += 1
287 unique_name = simple_name + str(counter) 398 unique_name = simple_name + str(counter)
288 399
289 used_names.add(unique_name) 400 used_names.add(unique_name)
290 each_import["unique_name"] = unique_name 401 each_import["unique_name"] = unique_name
291 counter += 1 402 counter += 1
292 return self.module.imports 403 return self.module.imports
293 404
294 def GetImportedInterfaces(self): 405 def GetImportedInterfaces(self):
295 interface_to_import = {} 406 interface_to_import = {}
296 for each_import in self.module.imports: 407 for each_import in self.module.imports:
297 for each_interface in each_import["module"].interfaces: 408 for each_interface in each_import["module"].interfaces:
298 name = each_interface.name 409 name = each_interface.name
299 interface_to_import[name] = each_import["unique_name"] + "." + name 410 interface_to_import[name] = each_import["unique_name"] + "." + name
300 return interface_to_import 411 return interface_to_import
301 412
302 def ImportedFrom(self): 413 def ImportedFrom(self):
303 interface_to_import = {} 414 interface_to_import = {}
304 for each_import in self.module.imports: 415 for each_import in self.module.imports:
305 for each_interface in each_import["module"].interfaces: 416 for each_interface in each_import["module"].interfaces:
306 name = each_interface.name 417 name = each_interface.name
307 interface_to_import[name] = each_import["unique_name"] + "." 418 interface_to_import[name] = each_import["unique_name"] + "."
308 return interface_to_import 419 return interface_to_import
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698