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 java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
6 | 6 |
7 import argparse | 7 import argparse |
| 8 import ast |
8 import os | 9 import os |
9 import re | 10 import re |
10 | 11 |
11 from jinja2 import contextfilter | 12 from jinja2 import contextfilter |
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 from mojom.generate.template_expander import UseJinja | 16 from mojom.generate.template_expander import UseJinja |
16 | 17 |
17 | 18 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 | 202 |
202 def IsHandle(kind): | 203 def IsHandle(kind): |
203 return kind.spec[0] == 'h' | 204 return kind.spec[0] == 'h' |
204 | 205 |
205 @contextfilter | 206 @contextfilter |
206 def DefaultValue(context, field): | 207 def DefaultValue(context, field): |
207 assert field.default | 208 assert field.default |
208 if isinstance(field.kind, mojom.Struct): | 209 if isinstance(field.kind, mojom.Struct): |
209 assert field.default == "default" | 210 assert field.default == "default" |
210 return "new %s()" % GetJavaType(context, field.kind) | 211 return "new %s()" % GetJavaType(context, field.kind) |
211 return "(%s) %s" % (GetJavaType(context, field.kind), | 212 return "(%s) %s" % ( |
212 ExpressionToText(context, field.default)) | 213 GetJavaType(context, field.kind), |
| 214 ExpressionToText(context, field.default, kind_spec=field.kind.spec)) |
| 215 |
| 216 @contextfilter |
| 217 def ConstantValue(context, constant): |
| 218 return "(%s) %s" % ( |
| 219 GetJavaType(context, constant.kind), |
| 220 ExpressionToText(context, constant.value, kind_spec=constant.kind.spec)) |
213 | 221 |
214 @contextfilter | 222 @contextfilter |
215 def NewArray(context, kind, size): | 223 def NewArray(context, kind, size): |
216 if IsArray(kind.kind): | 224 if IsArray(kind.kind): |
217 return NewArray(context, kind.kind, size) + '[]' | 225 return NewArray(context, kind.kind, size) + '[]' |
218 return 'new %s[%s]' % (GetJavaType(context, kind.kind), size) | 226 return 'new %s[%s]' % (GetJavaType(context, kind.kind), size) |
219 | 227 |
220 @contextfilter | 228 @contextfilter |
221 def ExpressionToText(context, token): | 229 def ExpressionToText(context, token, kind_spec=''): |
222 def _TranslateNamedValue(named_value): | 230 def _TranslateNamedValue(named_value): |
223 entity_name = GetNameForElement(named_value) | 231 entity_name = GetNameForElement(named_value) |
224 if named_value.parent_kind: | 232 if named_value.parent_kind: |
225 return GetJavaType(context, named_value.parent_kind) + '.' + entity_name | 233 return GetJavaType(context, named_value.parent_kind) + '.' + entity_name |
226 # Handle the case where named_value is a module level constant: | 234 # Handle the case where named_value is a module level constant: |
227 if not isinstance(named_value, mojom.EnumValue): | 235 if not isinstance(named_value, mojom.EnumValue): |
228 entity_name = (GetConstantsMainEntityName(named_value.module) + '.' + | 236 entity_name = (GetConstantsMainEntityName(named_value.module) + '.' + |
229 entity_name) | 237 entity_name) |
230 if GetPackage(named_value.module) == GetPackage(context.resolve('module')): | 238 if GetPackage(named_value.module) == GetPackage(context.resolve('module')): |
231 return entity_name | 239 return entity_name |
232 return GetPackage(named_value.module) + '.' + entity_name | 240 return GetPackage(named_value.module) + '.' + entity_name |
233 | 241 |
234 if isinstance(token, mojom.NamedValue): | 242 if isinstance(token, mojom.NamedValue): |
235 return _TranslateNamedValue(token) | 243 return _TranslateNamedValue(token) |
236 # Add Long suffix to all number literals. | 244 if kind_spec.startswith('i') or kind_spec.startswith('u'): |
237 if re.match('^[0-9]+$', token): | 245 # Add Long suffix to all integer literals. |
238 number = int(token) | 246 number = ast.literal_eval(token.lstrip('+ ')) |
| 247 if not isinstance(number, (int, long)): |
| 248 raise ValueError('got unexpected type %r for int literal %r' % ( |
| 249 type(number), token)) |
239 # If the literal is too large to fit a signed long, convert it to the | 250 # If the literal is too large to fit a signed long, convert it to the |
240 # equivalent signed long. | 251 # equivalent signed long. |
241 if number >= 2 ** 63: | 252 if number >= 2 ** 63: |
242 number -= 2 ** 64 | 253 number -= 2 ** 64 |
243 return '%dL' % number | 254 return '%dL' % number |
244 return token | 255 return token |
245 | 256 |
246 def IsPointerArrayKind(kind): | 257 def IsPointerArrayKind(kind): |
247 if not IsArray(kind): | 258 if not IsArray(kind): |
248 return False | 259 return False |
249 sub_kind = kind.kind | 260 sub_kind = kind.kind |
250 return generator.IsObjectKind(sub_kind) | 261 return generator.IsObjectKind(sub_kind) |
251 | 262 |
252 def GetConstantsMainEntityName(module): | 263 def GetConstantsMainEntityName(module): |
253 if 'JavaConstantsClassName' in module.attributes: | 264 if 'JavaConstantsClassName' in module.attributes: |
254 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) | 265 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
255 # This constructs the name of the embedding classes for module level constants | 266 # This constructs the name of the embedding classes for module level constants |
256 # by extracting the mojom's filename and prepending it to Constants. | 267 # by extracting the mojom's filename and prepending it to Constants. |
257 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 268 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
258 'Constants') | 269 'Constants') |
259 | 270 |
260 class Generator(generator.Generator): | 271 class Generator(generator.Generator): |
261 | 272 |
262 java_filters = { | 273 java_filters = { |
263 "interface_response_name": GetInterfaceResponseName, | 274 "interface_response_name": GetInterfaceResponseName, |
| 275 "constant_value": ConstantValue, |
264 "default_value": DefaultValue, | 276 "default_value": DefaultValue, |
265 "decode_method": DecodeMethod, | 277 "decode_method": DecodeMethod, |
266 "expression_to_text": ExpressionToText, | 278 "expression_to_text": ExpressionToText, |
267 "encode_method": EncodeMethod, | 279 "encode_method": EncodeMethod, |
268 "is_handle": IsHandle, | 280 "is_handle": IsHandle, |
269 "is_pointer_array_kind": IsPointerArrayKind, | 281 "is_pointer_array_kind": IsPointerArrayKind, |
270 "is_struct_kind": lambda kind: isinstance(kind, mojom.Struct), | 282 "is_struct_kind": lambda kind: isinstance(kind, mojom.Struct), |
271 "java_type": GetJavaType, | 283 "java_type": GetJavaType, |
272 "name": GetNameForElement, | 284 "name": GetNameForElement, |
273 "new_array": NewArray, | 285 "new_array": NewArray, |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 def GetJinjaParameters(self): | 354 def GetJinjaParameters(self): |
343 return { | 355 return { |
344 'lstrip_blocks': True, | 356 'lstrip_blocks': True, |
345 'trim_blocks': True, | 357 'trim_blocks': True, |
346 } | 358 } |
347 | 359 |
348 def GetGlobals(self): | 360 def GetGlobals(self): |
349 return { | 361 return { |
350 'module': self.module, | 362 'module': self.module, |
351 } | 363 } |
OLD | NEW |