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

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

Issue 1518293002: [mojo] Add Mojo bindings support for IPC::ParamTraits (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pickle4
Patch Set: Created 5 years 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
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/wrapper_class_definition.tmpl ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 24 matching lines...) Expand all
35 } 35 }
36 36
37 _kind_to_cpp_literal_suffix = { 37 _kind_to_cpp_literal_suffix = {
38 mojom.UINT8: "U", 38 mojom.UINT8: "U",
39 mojom.UINT16: "U", 39 mojom.UINT16: "U",
40 mojom.UINT32: "U", 40 mojom.UINT32: "U",
41 mojom.FLOAT: "f", 41 mojom.FLOAT: "f",
42 mojom.UINT64: "ULL", 42 mojom.UINT64: "ULL",
43 } 43 }
44 44
45
45 _current_variant = None 46 _current_variant = None
46 47
47 48
49 _variant_metadata_attributes = {
50 "chromium": {
51 "typename": "ChromiumType",
52 "headers": "ChromiumHeaders",
53 },
54 "blink": {
55 "typename": "BlinkType",
56 "headers": "BlinkHeaders",
57 }
58 }
59
60
61 _variant_typemap = {}
62
63
48 def ConstantValue(constant): 64 def ConstantValue(constant):
49 return ExpressionToText(constant.value, kind=constant.kind) 65 return ExpressionToText(constant.value, kind=constant.kind)
50 66
51 def DefaultValue(field): 67 def DefaultValue(field):
52 if field.default: 68 if field.default:
53 if mojom.IsStructKind(field.kind): 69 if mojom.IsStructKind(field.kind):
54 assert field.default == "default" 70 assert field.default == "default"
55 return "%s::New()" % GetNameForKind(field.kind) 71 return "%s::New()" % GetNameForKind(field.kind)
56 return ExpressionToText(field.default, kind=field.kind) 72 return ExpressionToText(field.default, kind=field.kind)
57 return "" 73 return ""
58 74
75 def IsNativeKind(kind):
76 if hasattr(kind, "name") and kind.name in _variant_typemap:
77 return True
78
79 def GetNativeTypeName(kind):
80 return _variant_typemap[kind.name]
81
59 def NamespaceToArray(namespace): 82 def NamespaceToArray(namespace):
60 return namespace.split(".") if namespace else [] 83 return namespace.split(".") if namespace else []
61 84
62 def GetNameForKind(kind, internal = False): 85 def GetNameForKind(kind, internal = False):
63 parts = [] 86 parts = []
64 if kind.imported_from: 87 if kind.imported_from:
65 parts.extend(NamespaceToArray(kind.imported_from["namespace"])) 88 parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
66 if _current_variant: 89 if _current_variant:
67 parts.append(_current_variant) 90 parts.append(_current_variant)
68 if internal: 91 if internal:
69 parts.append("internal") 92 parts.append("internal")
70 if kind.parent_kind: 93 if kind.parent_kind:
71 parts.append(kind.parent_kind.name) 94 parts.append(kind.parent_kind.name)
72 parts.append(kind.name) 95 parts.append(kind.name)
73 return "::".join(parts) 96 return "::".join(parts)
74 97
98 def GetQualifiedGenericInterfaceName(interface_kind):
99 parts = NamespaceToArray(interface_kind.module.namespace)
100 parts.append(interface_kind.name)
101 return "::".join(parts)
102
75 def GetCppType(kind): 103 def GetCppType(kind):
76 if mojom.IsArrayKind(kind): 104 if mojom.IsArrayKind(kind):
77 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) 105 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind)
78 if mojom.IsMapKind(kind): 106 if mojom.IsMapKind(kind):
79 return "mojo::internal::Map_Data<%s, %s>*" % ( 107 return "mojo::internal::Map_Data<%s, %s>*" % (
80 GetCppType(kind.key_kind), GetCppType(kind.value_kind)) 108 GetCppType(kind.key_kind), GetCppType(kind.value_kind))
81 if mojom.IsStructKind(kind): 109 if mojom.IsStructKind(kind):
82 return "%s_Data*" % GetNameForKind(kind, internal=True) 110 return "%s_Data*" % GetNameForKind(kind, internal=True)
83 if mojom.IsUnionKind(kind): 111 if mojom.IsUnionKind(kind):
84 return "%s_Data" % GetNameForKind(kind, internal=True) 112 return "%s_Data" % GetNameForKind(kind, internal=True)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 return "mojo::ScopedDataPipeConsumerHandle" 156 return "mojo::ScopedDataPipeConsumerHandle"
129 if mojom.IsDataPipeProducerKind(kind): 157 if mojom.IsDataPipeProducerKind(kind):
130 return "mojo::ScopedDataPipeProducerHandle" 158 return "mojo::ScopedDataPipeProducerHandle"
131 if mojom.IsMessagePipeKind(kind): 159 if mojom.IsMessagePipeKind(kind):
132 return "mojo::ScopedMessagePipeHandle" 160 return "mojo::ScopedMessagePipeHandle"
133 if mojom.IsSharedBufferKind(kind): 161 if mojom.IsSharedBufferKind(kind):
134 return "mojo::ScopedSharedBufferHandle" 162 return "mojo::ScopedSharedBufferHandle"
135 return _kind_to_cpp_type[kind] 163 return _kind_to_cpp_type[kind]
136 164
137 def GetCppResultWrapperType(kind): 165 def GetCppResultWrapperType(kind):
166 if IsNativeKind(kind):
167 return "const %s&" % GetNativeTypeName(kind)
138 if mojom.IsEnumKind(kind): 168 if mojom.IsEnumKind(kind):
139 return GetNameForKind(kind) 169 return GetNameForKind(kind)
140 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): 170 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
141 return "%sPtr" % GetNameForKind(kind) 171 return "%sPtr" % GetNameForKind(kind)
142 if mojom.IsArrayKind(kind): 172 if mojom.IsArrayKind(kind):
143 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) 173 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
144 if mojom.IsMapKind(kind): 174 if mojom.IsMapKind(kind):
145 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind), 175 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind),
146 GetCppArrayArgWrapperType(kind.value_kind)) 176 GetCppArrayArgWrapperType(kind.value_kind))
147 if mojom.IsInterfaceKind(kind): 177 if mojom.IsInterfaceKind(kind):
(...skipping 18 matching lines...) Expand all
166 return "mojo::ScopedSharedBufferHandle" 196 return "mojo::ScopedSharedBufferHandle"
167 # TODO(rudominer) After improvements to compiler front end have landed, 197 # TODO(rudominer) After improvements to compiler front end have landed,
168 # revisit strategy used below for emitting a useful error message when an 198 # revisit strategy used below for emitting a useful error message when an
169 # undefined identifier is referenced. 199 # undefined identifier is referenced.
170 val = _kind_to_cpp_type.get(kind) 200 val = _kind_to_cpp_type.get(kind)
171 if (val is not None): 201 if (val is not None):
172 return val 202 return val
173 raise Exception("Unrecognized kind %s" % kind.spec) 203 raise Exception("Unrecognized kind %s" % kind.spec)
174 204
175 def GetCppWrapperType(kind): 205 def GetCppWrapperType(kind):
206 if IsNativeKind(kind):
207 return GetNativeTypeName(kind)
176 if mojom.IsEnumKind(kind): 208 if mojom.IsEnumKind(kind):
177 return GetNameForKind(kind) 209 return GetNameForKind(kind)
178 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): 210 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
179 return "%sPtr" % GetNameForKind(kind) 211 return "%sPtr" % GetNameForKind(kind)
180 if mojom.IsArrayKind(kind): 212 if mojom.IsArrayKind(kind):
181 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) 213 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
182 if mojom.IsMapKind(kind): 214 if mojom.IsMapKind(kind):
183 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind), 215 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind),
184 GetCppArrayArgWrapperType(kind.value_kind)) 216 GetCppArrayArgWrapperType(kind.value_kind))
185 if mojom.IsInterfaceKind(kind): 217 if mojom.IsInterfaceKind(kind):
(...skipping 12 matching lines...) Expand all
198 return "mojo::ScopedDataPipeConsumerHandle" 230 return "mojo::ScopedDataPipeConsumerHandle"
199 if mojom.IsDataPipeProducerKind(kind): 231 if mojom.IsDataPipeProducerKind(kind):
200 return "mojo::ScopedDataPipeProducerHandle" 232 return "mojo::ScopedDataPipeProducerHandle"
201 if mojom.IsMessagePipeKind(kind): 233 if mojom.IsMessagePipeKind(kind):
202 return "mojo::ScopedMessagePipeHandle" 234 return "mojo::ScopedMessagePipeHandle"
203 if mojom.IsSharedBufferKind(kind): 235 if mojom.IsSharedBufferKind(kind):
204 return "mojo::ScopedSharedBufferHandle" 236 return "mojo::ScopedSharedBufferHandle"
205 return _kind_to_cpp_type.get(kind) 237 return _kind_to_cpp_type.get(kind)
206 238
207 def GetCppConstWrapperType(kind): 239 def GetCppConstWrapperType(kind):
240 if IsNativeKind(kind):
241 return "const %s&" % GetNativeTypeName(kind)
208 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): 242 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
209 return "%sPtr" % GetNameForKind(kind) 243 return "%sPtr" % GetNameForKind(kind)
210 if mojom.IsArrayKind(kind): 244 if mojom.IsArrayKind(kind):
211 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) 245 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
212 if mojom.IsMapKind(kind): 246 if mojom.IsMapKind(kind):
213 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind), 247 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind),
214 GetCppArrayArgWrapperType(kind.value_kind)) 248 GetCppArrayArgWrapperType(kind.value_kind))
215 if mojom.IsInterfaceKind(kind): 249 if mojom.IsInterfaceKind(kind):
216 return "%sPtr" % GetNameForKind(kind) 250 return "%sPtr" % GetNameForKind(kind)
217 if mojom.IsInterfaceRequestKind(kind): 251 if mojom.IsInterfaceRequestKind(kind):
(...skipping 14 matching lines...) Expand all
232 return "mojo::ScopedDataPipeProducerHandle" 266 return "mojo::ScopedDataPipeProducerHandle"
233 if mojom.IsMessagePipeKind(kind): 267 if mojom.IsMessagePipeKind(kind):
234 return "mojo::ScopedMessagePipeHandle" 268 return "mojo::ScopedMessagePipeHandle"
235 if mojom.IsSharedBufferKind(kind): 269 if mojom.IsSharedBufferKind(kind):
236 return "mojo::ScopedSharedBufferHandle" 270 return "mojo::ScopedSharedBufferHandle"
237 if not kind in _kind_to_cpp_type: 271 if not kind in _kind_to_cpp_type:
238 print "missing:", kind.spec 272 print "missing:", kind.spec
239 return _kind_to_cpp_type[kind] 273 return _kind_to_cpp_type[kind]
240 274
241 def GetCppFieldType(kind): 275 def GetCppFieldType(kind):
276 if IsNativeKind(kind):
277 return "mojo::internal::NativePointer<%s>" % GetNativeTypeName(kind)
242 if mojom.IsStructKind(kind): 278 if mojom.IsStructKind(kind):
243 return ("mojo::internal::StructPointer<%s_Data>" % 279 return ("mojo::internal::StructPointer<%s_Data>" %
244 GetNameForKind(kind, internal=True)) 280 GetNameForKind(kind, internal=True))
245 if mojom.IsUnionKind(kind): 281 if mojom.IsUnionKind(kind):
246 return "%s_Data" % GetNameForKind(kind, internal=True) 282 return "%s_Data" % GetNameForKind(kind, internal=True)
247 if mojom.IsArrayKind(kind): 283 if mojom.IsArrayKind(kind):
248 return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind) 284 return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind)
249 if mojom.IsMapKind(kind): 285 if mojom.IsMapKind(kind):
250 return ("mojo::internal::StructPointer<mojo::internal::Map_Data<%s, %s>>" % 286 return ("mojo::internal::StructPointer<mojo::internal::Map_Data<%s, %s>>" %
251 (GetCppType(kind.key_kind), GetCppType(kind.value_kind))) 287 (GetCppType(kind.key_kind), GetCppType(kind.value_kind)))
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 "cpp_result_type": GetCppResultWrapperType, 426 "cpp_result_type": GetCppResultWrapperType,
391 "cpp_type": GetCppType, 427 "cpp_type": GetCppType,
392 "cpp_union_getter_return_type": GetUnionGetterReturnType, 428 "cpp_union_getter_return_type": GetUnionGetterReturnType,
393 "cpp_wrapper_type": GetCppWrapperType, 429 "cpp_wrapper_type": GetCppWrapperType,
394 "default_value": DefaultValue, 430 "default_value": DefaultValue,
395 "expression_to_text": ExpressionToText, 431 "expression_to_text": ExpressionToText,
396 "get_array_validate_params_ctor_args": GetArrayValidateParamsCtorArgs, 432 "get_array_validate_params_ctor_args": GetArrayValidateParamsCtorArgs,
397 "get_map_validate_params_ctor_args": GetMapValidateParamsCtorArgs, 433 "get_map_validate_params_ctor_args": GetMapValidateParamsCtorArgs,
398 "get_name_for_kind": GetNameForKind, 434 "get_name_for_kind": GetNameForKind,
399 "get_pad": pack.GetPad, 435 "get_pad": pack.GetPad,
436 "get_qualified_generic_interface_name": GetQualifiedGenericInterfaceName,
400 "has_callbacks": mojom.HasCallbacks, 437 "has_callbacks": mojom.HasCallbacks,
401 "should_inline": ShouldInlineStruct, 438 "should_inline": ShouldInlineStruct,
402 "should_inline_union": ShouldInlineUnion, 439 "should_inline_union": ShouldInlineUnion,
403 "is_array_kind": mojom.IsArrayKind, 440 "is_array_kind": mojom.IsArrayKind,
404 "is_cloneable_kind": mojom.IsCloneableKind, 441 "is_cloneable_kind": mojom.IsCloneableKind,
405 "is_enum_kind": mojom.IsEnumKind, 442 "is_enum_kind": mojom.IsEnumKind,
406 "is_integral_kind": mojom.IsIntegralKind, 443 "is_integral_kind": mojom.IsIntegralKind,
407 "is_move_only_kind": mojom.IsMoveOnlyKind, 444 "is_move_only_kind": mojom.IsMoveOnlyKind,
445 "is_native_kind": IsNativeKind,
408 "is_any_handle_kind": mojom.IsAnyHandleKind, 446 "is_any_handle_kind": mojom.IsAnyHandleKind,
409 "is_interface_kind": mojom.IsInterfaceKind, 447 "is_interface_kind": mojom.IsInterfaceKind,
410 "is_interface_request_kind": mojom.IsInterfaceRequestKind, 448 "is_interface_request_kind": mojom.IsInterfaceRequestKind,
411 "is_associated_interface_kind": mojom.IsAssociatedInterfaceKind, 449 "is_associated_interface_kind": mojom.IsAssociatedInterfaceKind,
412 "is_associated_interface_request_kind": 450 "is_associated_interface_request_kind":
413 mojom.IsAssociatedInterfaceRequestKind, 451 mojom.IsAssociatedInterfaceRequestKind,
414 "is_associated_kind": mojom.IsAssociatedKind, 452 "is_associated_kind": mojom.IsAssociatedKind,
415 "is_map_kind": mojom.IsMapKind, 453 "is_map_kind": mojom.IsMapKind,
416 "is_nullable_kind": mojom.IsNullableKind, 454 "is_nullable_kind": mojom.IsNullableKind,
417 "is_object_kind": mojom.IsObjectKind, 455 "is_object_kind": mojom.IsObjectKind,
418 "is_string_kind": mojom.IsStringKind, 456 "is_string_kind": mojom.IsStringKind,
419 "is_struct_kind": mojom.IsStructKind, 457 "is_struct_kind": mojom.IsStructKind,
420 "is_union_kind": mojom.IsUnionKind, 458 "is_union_kind": mojom.IsUnionKind,
421 "passes_associated_kinds": mojom.PassesAssociatedKinds, 459 "passes_associated_kinds": mojom.PassesAssociatedKinds,
422 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, 460 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
423 "stylize_method": generator.StudlyCapsToCamel, 461 "stylize_method": generator.StudlyCapsToCamel,
424 "to_all_caps": generator.CamelCaseToAllCaps, 462 "to_all_caps": generator.CamelCaseToAllCaps,
425 "under_to_camel": generator.UnderToCamel, 463 "under_to_camel": generator.UnderToCamel,
426 } 464 }
427 465
428 chromium_cpp_filters = dict(cpp_filters) 466 chromium_cpp_filters = dict(cpp_filters)
429 blink_cpp_filters = dict(cpp_filters) 467 blink_cpp_filters = dict(cpp_filters)
430 468
469 def GetExtraHeaders(self):
470 if not _current_variant:
471 return []
472 extra_headers = []
473 headers_attribute = \
474 _variant_metadata_attributes[_current_variant]["headers"]
475 for struct in self.GetStructs():
476 if struct.attributes:
477 headers = struct.attributes.get(headers_attribute, [])
478 extra_headers.extend(headers)
479 return extra_headers
480
431 def GetJinjaExports(self): 481 def GetJinjaExports(self):
482 global _variant_typemap
483 _variant_typemap = {}
484 if _current_variant:
485 typename_attribute = \
486 _variant_metadata_attributes[_current_variant]["typename"]
487 for struct in self.GetStructs():
488 if struct.attributes:
489 typename = struct.attributes.get(typename_attribute)
490 if typename:
491 _variant_typemap[struct.name] = typename
492
432 return { 493 return {
433 "module": self.module, 494 "module": self.module,
434 "namespace": self.module.namespace, 495 "namespace": self.module.namespace,
435 "namespaces_as_array": NamespaceToArray(self.module.namespace), 496 "namespaces_as_array": NamespaceToArray(self.module.namespace),
436 "imports": self.module.imports, 497 "imports": self.module.imports,
437 "kinds": self.module.kinds, 498 "kinds": self.module.kinds,
438 "enums": self.module.enums, 499 "enums": self.module.enums,
439 "structs": self.GetStructs(), 500 "structs": self.GetStructs(),
440 "unions": self.GetUnions(), 501 "unions": self.GetUnions(),
441 "interfaces": self.GetInterfaces(), 502 "interfaces": self.GetInterfaces(),
442 "variant": _current_variant, 503 "variant": _current_variant,
504 "extra_headers": self.GetExtraHeaders(),
443 } 505 }
444 506
445 @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters) 507 @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters)
446 def GenerateModuleHeader(self): 508 def GenerateModuleHeader(self):
447 return self.GetJinjaExports() 509 return self.GetJinjaExports()
448 510
449 @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters) 511 @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters)
450 def GenerateModuleInternalHeader(self): 512 def GenerateModuleInternalHeader(self):
451 return self.GetJinjaExports() 513 return self.GetJinjaExports()
452 514
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 self.Write(self.GenerateChromiumModuleSource(), 558 self.Write(self.GenerateChromiumModuleSource(),
497 self.MatchMojomFilePath("%s-chromium.cc" % self.module.name)) 559 self.MatchMojomFilePath("%s-chromium.cc" % self.module.name))
498 560
499 _current_variant = "blink" 561 _current_variant = "blink"
500 self.Write(self.GenerateBlinkModuleHeader(), 562 self.Write(self.GenerateBlinkModuleHeader(),
501 self.MatchMojomFilePath("%s-blink.h" % self.module.name)) 563 self.MatchMojomFilePath("%s-blink.h" % self.module.name))
502 self.Write(self.GenerateBlinkModuleInternalHeader(), 564 self.Write(self.GenerateBlinkModuleInternalHeader(),
503 self.MatchMojomFilePath("%s-blink-internal.h" % self.module.name)) 565 self.MatchMojomFilePath("%s-blink-internal.h" % self.module.name))
504 self.Write(self.GenerateBlinkModuleSource(), 566 self.Write(self.GenerateBlinkModuleSource(),
505 self.MatchMojomFilePath("%s-blink.cc" % self.module.name)) 567 self.MatchMojomFilePath("%s-blink.cc" % self.module.name))
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/wrapper_class_definition.tmpl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698