| 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 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 276 |
| 277 def ShouldInlineStruct(struct): | 277 def ShouldInlineStruct(struct): |
| 278 # TODO(darin): Base this on the size of the wrapper class. | 278 # TODO(darin): Base this on the size of the wrapper class. |
| 279 if len(struct.fields) > 4: | 279 if len(struct.fields) > 4: |
| 280 return False | 280 return False |
| 281 for field in struct.fields: | 281 for field in struct.fields: |
| 282 if mojom.IsMoveOnlyKind(field.kind): | 282 if mojom.IsMoveOnlyKind(field.kind): |
| 283 return False | 283 return False |
| 284 return True | 284 return True |
| 285 | 285 |
| 286 def ShouldInlineUnion(union): | |
| 287 return not all(not mojom.IsMoveOnlyKind(field.kind) for field in union.fields) | |
| 288 | |
| 289 def GetArrayValidateParams(kind): | 286 def GetArrayValidateParams(kind): |
| 290 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and | 287 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and |
| 291 not mojom.IsStringKind(kind)): | 288 not mojom.IsStringKind(kind)): |
| 292 return "mojo::internal::NoValidateParams" | 289 return "mojo::internal::NoValidateParams" |
| 293 | 290 |
| 294 if mojom.IsStringKind(kind): | 291 if mojom.IsStringKind(kind): |
| 295 expected_num_elements = 0 | 292 expected_num_elements = 0 |
| 296 element_is_nullable = False | 293 element_is_nullable = False |
| 297 element_validate_params = "mojo::internal::NoValidateParams" | 294 element_validate_params = "mojo::internal::NoValidateParams" |
| 298 elif mojom.IsMapKind(kind): | 295 elif mojom.IsMapKind(kind): |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 "cpp_type": GetCppType, | 327 "cpp_type": GetCppType, |
| 331 "cpp_wrapper_type": GetCppWrapperType, | 328 "cpp_wrapper_type": GetCppWrapperType, |
| 332 "default_value": DefaultValue, | 329 "default_value": DefaultValue, |
| 333 "expression_to_text": ExpressionToText, | 330 "expression_to_text": ExpressionToText, |
| 334 "get_array_validate_params": GetArrayValidateParams, | 331 "get_array_validate_params": GetArrayValidateParams, |
| 335 "get_map_validate_params": GetMapValidateParams, | 332 "get_map_validate_params": GetMapValidateParams, |
| 336 "get_name_for_kind": GetNameForKind, | 333 "get_name_for_kind": GetNameForKind, |
| 337 "get_pad": pack.GetPad, | 334 "get_pad": pack.GetPad, |
| 338 "has_callbacks": mojom.HasCallbacks, | 335 "has_callbacks": mojom.HasCallbacks, |
| 339 "should_inline": ShouldInlineStruct, | 336 "should_inline": ShouldInlineStruct, |
| 340 "should_inline_union": ShouldInlineUnion, | |
| 341 "is_array_kind": mojom.IsArrayKind, | 337 "is_array_kind": mojom.IsArrayKind, |
| 342 "is_cloneable_kind": mojom.IsCloneableKind, | 338 "is_cloneable_kind": mojom.IsCloneableKind, |
| 343 "is_enum_kind": mojom.IsEnumKind, | 339 "is_enum_kind": mojom.IsEnumKind, |
| 344 "is_move_only_kind": mojom.IsMoveOnlyKind, | 340 "is_move_only_kind": mojom.IsMoveOnlyKind, |
| 345 "is_any_handle_kind": mojom.IsAnyHandleKind, | 341 "is_any_handle_kind": mojom.IsAnyHandleKind, |
| 346 "is_interface_kind": mojom.IsInterfaceKind, | 342 "is_interface_kind": mojom.IsInterfaceKind, |
| 347 "is_interface_request_kind": mojom.IsInterfaceRequestKind, | 343 "is_interface_request_kind": mojom.IsInterfaceRequestKind, |
| 348 "is_map_kind": mojom.IsMapKind, | 344 "is_map_kind": mojom.IsMapKind, |
| 349 "is_nullable_kind": mojom.IsNullableKind, | 345 "is_nullable_kind": mojom.IsNullableKind, |
| 350 "is_object_kind": mojom.IsObjectKind, | 346 "is_object_kind": mojom.IsObjectKind, |
| 351 "is_string_kind": mojom.IsStringKind, | 347 "is_string_kind": mojom.IsStringKind, |
| 352 "is_struct_kind": mojom.IsStructKind, | 348 "is_struct_kind": mojom.IsStructKind, |
| 353 "is_struct_with_handles": IsStructWithHandles, | 349 "is_struct_with_handles": IsStructWithHandles, |
| 354 "is_union_kind": mojom.IsUnionKind, | |
| 355 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 350 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
| 356 "struct_from_method": generator.GetStructFromMethod, | 351 "struct_from_method": generator.GetStructFromMethod, |
| 357 "response_struct_from_method": generator.GetResponseStructFromMethod, | 352 "response_struct_from_method": generator.GetResponseStructFromMethod, |
| 358 "stylize_method": generator.StudlyCapsToCamel, | 353 "stylize_method": generator.StudlyCapsToCamel, |
| 359 "to_all_caps": generator.CamelCaseToAllCaps, | 354 "to_all_caps": generator.CamelCaseToAllCaps, |
| 360 "under_to_camel": generator.UnderToCamel, | |
| 361 } | 355 } |
| 362 | 356 |
| 363 def GetJinjaExports(self): | 357 def GetJinjaExports(self): |
| 364 return { | 358 return { |
| 365 "module": self.module, | 359 "module": self.module, |
| 366 "namespace": self.module.namespace, | 360 "namespace": self.module.namespace, |
| 367 "namespaces_as_array": NamespaceToArray(self.module.namespace), | 361 "namespaces_as_array": NamespaceToArray(self.module.namespace), |
| 368 "imports": self.module.imports, | 362 "imports": self.module.imports, |
| 369 "kinds": self.module.kinds, | 363 "kinds": self.module.kinds, |
| 370 "enums": self.module.enums, | 364 "enums": self.module.enums, |
| 371 "structs": self.GetStructs(), | 365 "structs": self.GetStructs(), |
| 372 "unions": self.module.unions, | |
| 373 "interfaces": self.module.interfaces, | 366 "interfaces": self.module.interfaces, |
| 374 } | 367 } |
| 375 | 368 |
| 376 @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters) | 369 @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters) |
| 377 def GenerateModuleHeader(self): | 370 def GenerateModuleHeader(self): |
| 378 return self.GetJinjaExports() | 371 return self.GetJinjaExports() |
| 379 | 372 |
| 380 @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters) | 373 @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters) |
| 381 def GenerateModuleInternalHeader(self): | 374 def GenerateModuleInternalHeader(self): |
| 382 return self.GetJinjaExports() | 375 return self.GetJinjaExports() |
| 383 | 376 |
| 384 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) | 377 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) |
| 385 def GenerateModuleSource(self): | 378 def GenerateModuleSource(self): |
| 386 return self.GetJinjaExports() | 379 return self.GetJinjaExports() |
| 387 | 380 |
| 388 def GenerateFiles(self, args): | 381 def GenerateFiles(self, args): |
| 389 self.Write(self.GenerateModuleHeader(), | 382 self.Write(self.GenerateModuleHeader(), |
| 390 self.MatchMojomFilePath("%s.h" % self.module.name)) | 383 self.MatchMojomFilePath("%s.h" % self.module.name)) |
| 391 self.Write(self.GenerateModuleInternalHeader(), | 384 self.Write(self.GenerateModuleInternalHeader(), |
| 392 self.MatchMojomFilePath("%s-internal.h" % self.module.name)) | 385 self.MatchMojomFilePath("%s-internal.h" % self.module.name)) |
| 393 self.Write(self.GenerateModuleSource(), | 386 self.Write(self.GenerateModuleSource(), |
| 394 self.MatchMojomFilePath("%s.cc" % self.module.name)) | 387 self.MatchMojomFilePath("%s.cc" % self.module.name)) |
| OLD | NEW |