OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 C++ source files from a mojom.Module.""" | 5 """Generates C++ source files from a mojom.Module.""" |
6 | 6 |
7 import mojom.generate.generator as generator | 7 import mojom.generate.generator as generator |
8 import mojom.generate.module as mojom | 8 import mojom.generate.module as mojom |
9 import mojom.generate.pack as pack | 9 import mojom.generate.pack as pack |
10 from mojom.generate.template_expander import UseJinja | 10 from mojom.generate.template_expander import UseJinja |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 236 |
237 def ShouldInlineStruct(struct): | 237 def ShouldInlineStruct(struct): |
238 # TODO(darin): Base this on the size of the wrapper class. | 238 # TODO(darin): Base this on the size of the wrapper class. |
239 if len(struct.fields) > 4: | 239 if len(struct.fields) > 4: |
240 return False | 240 return False |
241 for field in struct.fields: | 241 for field in struct.fields: |
242 if mojom.IsMoveOnlyKind(field.kind): | 242 if mojom.IsMoveOnlyKind(field.kind): |
243 return False | 243 return False |
244 return True | 244 return True |
245 | 245 |
| 246 def GetArrayValidateParams(kind): |
| 247 if not mojom.IsAnyArrayKind(kind) and not mojom.IsStringKind(kind): |
| 248 return "mojo::internal::NoValidateParams" |
| 249 |
| 250 if mojom.IsStringKind(kind): |
| 251 expected_num_elements = 0 |
| 252 element_nullable = False |
| 253 element_validate_params = "mojo::internal::NoValidateParams" |
| 254 else: |
| 255 expected_num_elements = generator.ExpectedArraySize(kind) |
| 256 element_nullable = mojom.IsNullableKind(kind.kind) |
| 257 element_validate_params = GetArrayValidateParams(kind.kind) |
| 258 |
| 259 return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % ( |
| 260 expected_num_elements, |
| 261 'true' if element_nullable else 'false', |
| 262 element_validate_params) |
| 263 |
246 _HEADER_SIZE = 8 | 264 _HEADER_SIZE = 8 |
247 | 265 |
248 class Generator(generator.Generator): | 266 class Generator(generator.Generator): |
249 | 267 |
250 cpp_filters = { | 268 cpp_filters = { |
251 "constant_value": ConstantValue, | 269 "constant_value": ConstantValue, |
252 "cpp_const_wrapper_type": GetCppConstWrapperType, | 270 "cpp_const_wrapper_type": GetCppConstWrapperType, |
253 "cpp_field_type": GetCppFieldType, | 271 "cpp_field_type": GetCppFieldType, |
254 "cpp_pod_type": GetCppPodType, | 272 "cpp_pod_type": GetCppPodType, |
255 "cpp_result_type": GetCppResultWrapperType, | 273 "cpp_result_type": GetCppResultWrapperType, |
256 "cpp_type": GetCppType, | 274 "cpp_type": GetCppType, |
257 "cpp_wrapper_type": GetCppWrapperType, | 275 "cpp_wrapper_type": GetCppWrapperType, |
258 "default_value": DefaultValue, | 276 "default_value": DefaultValue, |
259 "expected_array_size": generator.ExpectedArraySize, | 277 "expected_array_size": generator.ExpectedArraySize, |
260 "expression_to_text": ExpressionToText, | 278 "expression_to_text": ExpressionToText, |
| 279 "get_array_validate_params": GetArrayValidateParams, |
261 "get_name_for_kind": GetNameForKind, | 280 "get_name_for_kind": GetNameForKind, |
262 "get_pad": pack.GetPad, | 281 "get_pad": pack.GetPad, |
263 "has_callbacks": HasCallbacks, | 282 "has_callbacks": HasCallbacks, |
264 "should_inline": ShouldInlineStruct, | 283 "should_inline": ShouldInlineStruct, |
265 "is_any_array_kind": mojom.IsAnyArrayKind, | 284 "is_any_array_kind": mojom.IsAnyArrayKind, |
266 "is_enum_kind": mojom.IsEnumKind, | 285 "is_enum_kind": mojom.IsEnumKind, |
267 "is_move_only_kind": mojom.IsMoveOnlyKind, | 286 "is_move_only_kind": mojom.IsMoveOnlyKind, |
268 "is_any_handle_kind": mojom.IsAnyHandleKind, | 287 "is_any_handle_kind": mojom.IsAnyHandleKind, |
269 "is_interface_kind": mojom.IsInterfaceKind, | 288 "is_interface_kind": mojom.IsInterfaceKind, |
270 "is_interface_request_kind": mojom.IsInterfaceRequestKind, | 289 "is_interface_request_kind": mojom.IsInterfaceRequestKind, |
| 290 "is_nullable_kind": mojom.IsNullableKind, |
271 "is_object_kind": mojom.IsObjectKind, | 291 "is_object_kind": mojom.IsObjectKind, |
272 "is_string_kind": mojom.IsStringKind, | 292 "is_string_kind": mojom.IsStringKind, |
273 "is_struct_with_handles": IsStructWithHandles, | 293 "is_struct_with_handles": IsStructWithHandles, |
274 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 294 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
275 "struct_from_method": generator.GetStructFromMethod, | 295 "struct_from_method": generator.GetStructFromMethod, |
276 "response_struct_from_method": generator.GetResponseStructFromMethod, | 296 "response_struct_from_method": generator.GetResponseStructFromMethod, |
277 "stylize_method": generator.StudlyCapsToCamel, | 297 "stylize_method": generator.StudlyCapsToCamel, |
278 "to_all_caps": generator.CamelCaseToAllCaps, | 298 "to_all_caps": generator.CamelCaseToAllCaps, |
279 } | 299 } |
280 | 300 |
(...skipping 19 matching lines...) Expand all Loading... |
300 | 320 |
301 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) | 321 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) |
302 def GenerateModuleSource(self): | 322 def GenerateModuleSource(self): |
303 return self.GetJinjaExports() | 323 return self.GetJinjaExports() |
304 | 324 |
305 def GenerateFiles(self, args): | 325 def GenerateFiles(self, args): |
306 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) | 326 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) |
307 self.Write(self.GenerateModuleInternalHeader(), | 327 self.Write(self.GenerateModuleInternalHeader(), |
308 "%s-internal.h" % self.module.name) | 328 "%s-internal.h" % self.module.name) |
309 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) | 329 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) |
OLD | NEW |