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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 if not _use_new_wrapper_types: | 145 if not _use_new_wrapper_types: |
146 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): | 146 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): |
147 return "nullptr"; | 147 return "nullptr"; |
148 if mojom.IsStringKind(field.kind): | 148 if mojom.IsStringKind(field.kind): |
149 return "" if _for_blink else "nullptr" | 149 return "" if _for_blink else "nullptr" |
150 return "" | 150 return "" |
151 | 151 |
152 def NamespaceToArray(namespace): | 152 def NamespaceToArray(namespace): |
153 return namespace.split(".") if namespace else [] | 153 return namespace.split(".") if namespace else [] |
154 | 154 |
155 def GetNameForKind(kind, internal=False, flatten_nested_kind=False): | 155 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, |
156 add_same_module_namespaces=False): | |
156 return _NameFormatter(kind, _variant).FormatForCpp( | 157 return _NameFormatter(kind, _variant).FormatForCpp( |
157 internal=internal, flatten_nested_kind=flatten_nested_kind) | 158 internal=internal, flatten_nested_kind=flatten_nested_kind, |
159 add_same_module_namespaces=add_same_module_namespaces) | |
158 | 160 |
159 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False): | 161 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False): |
yzshen1
2016/09/22 23:48:56
Just something to think about, we could remove thi
tibell
2016/09/23 00:07:08
Acknowledged.
| |
160 return _NameFormatter(kind, _variant).FormatForCpp( | 162 return _NameFormatter(kind, _variant).FormatForCpp( |
161 internal=internal, add_same_module_namespaces=True, | 163 internal=internal, add_same_module_namespaces=True, |
162 flatten_nested_kind=flatten_nested_kind) | 164 flatten_nested_kind=flatten_nested_kind) |
163 | 165 |
164 def GetFullMojomNameForKind(kind): | 166 def GetFullMojomNameForKind(kind): |
165 return _NameFormatter(kind, _variant).FormatForMojom() | 167 return _NameFormatter(kind, _variant).FormatForMojom() |
166 | 168 |
167 def IsTypemappedKind(kind): | 169 def IsTypemappedKind(kind): |
168 return hasattr(kind, "name") and \ | 170 return hasattr(kind, "name") and \ |
169 GetFullMojomNameForKind(kind) in _current_typemap | 171 GetFullMojomNameForKind(kind) in _current_typemap |
170 | 172 |
171 def IsNativeOnlyKind(kind): | 173 def IsNativeOnlyKind(kind): |
172 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ | 174 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ |
173 kind.native_only | 175 kind.native_only |
174 | 176 |
177 | |
178 def IsHashableKind(kind): | |
179 """Check if the kind can be hashed. | |
180 | |
181 Args: | |
182 kind: {Kind} The kind to check. | |
183 | |
184 Returns: | |
185 {bool} True if a value of this kind can be hashed. | |
186 """ | |
187 checked = set() | |
188 def Check(kind): | |
189 if kind.spec in checked: | |
190 return True | |
191 checked.add(kind.spec) | |
192 if mojom.IsNullableKind(kind): | |
193 return False | |
194 elif mojom.IsStructKind(kind): | |
195 if (kind.native_only or | |
yzshen1
2016/09/22 23:48:56
As we discussed, native-only types may be hashable
tibell
2016/09/23 00:07:08
Done.
| |
196 (IsTypemappedKind(kind) and | |
197 not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"])): | |
198 return False | |
199 return all(Check(field.kind) for field in kind.fields) | |
200 elif mojom.IsUnionKind(kind): | |
201 return all(Check(field.kind) for field in kind.fields) | |
202 elif mojom.IsAnyHandleKind(kind): | |
203 return False | |
204 elif mojom.IsAnyInterfaceKind(kind): | |
205 return False | |
206 elif mojom.IsArrayKind(kind): | |
yzshen1
2016/09/22 23:48:56
Why array/map are not hashable?
tibell
2016/09/23 00:07:08
We didn't think we'd need it. I've added a TODO. I
| |
207 return False | |
208 elif mojom.IsMapKind(kind): | |
209 return False | |
210 else: | |
211 return True | |
212 return Check(kind) | |
213 | |
214 | |
175 def GetNativeTypeName(typemapped_kind): | 215 def GetNativeTypeName(typemapped_kind): |
176 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] | 216 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] |
177 | 217 |
178 def GetCppPodType(kind): | 218 def GetCppPodType(kind): |
179 if mojom.IsStringKind(kind): | 219 if mojom.IsStringKind(kind): |
180 return "char*" | 220 return "char*" |
181 return _kind_to_cpp_type[kind] | 221 return _kind_to_cpp_type[kind] |
182 | 222 |
183 def GetCppWrapperType(kind): | 223 def GetCppWrapperType(kind, add_same_module_namespaces=False): |
184 def _AddOptional(type_name): | 224 def _AddOptional(type_name): |
185 pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>" | 225 pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>" |
186 return pattern % type_name | 226 return pattern % type_name |
187 | 227 |
188 if IsTypemappedKind(kind): | 228 if IsTypemappedKind(kind): |
189 type_name = GetNativeTypeName(kind) | 229 type_name = GetNativeTypeName(kind) |
190 if (mojom.IsNullableKind(kind) and | 230 if (mojom.IsNullableKind(kind) and |
191 not _current_typemap[GetFullMojomNameForKind(kind)][ | 231 not _current_typemap[GetFullMojomNameForKind(kind)][ |
192 "nullable_is_same_type"]): | 232 "nullable_is_same_type"]): |
193 type_name = _AddOptional(type_name) | 233 type_name = _AddOptional(type_name) |
194 return type_name | 234 return type_name |
195 if mojom.IsEnumKind(kind): | 235 if mojom.IsEnumKind(kind): |
196 return GetNameForKind(kind) | 236 return GetNameForKind( |
237 kind, add_same_module_namespaces=add_same_module_namespaces) | |
197 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 238 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
198 return "%sPtr" % GetNameForKind(kind) | 239 return "%sPtr" % GetNameForKind( |
240 kind, add_same_module_namespaces=add_same_module_namespaces) | |
199 if mojom.IsArrayKind(kind): | 241 if mojom.IsArrayKind(kind): |
200 pattern = None | 242 pattern = None |
201 if _use_new_wrapper_types: | 243 if _use_new_wrapper_types: |
202 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" | 244 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" |
203 if mojom.IsNullableKind(kind): | 245 if mojom.IsNullableKind(kind): |
204 pattern = _AddOptional(pattern) | 246 pattern = _AddOptional(pattern) |
205 else: | 247 else: |
206 pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" | 248 pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" |
207 return pattern % GetCppWrapperType(kind.kind) | 249 return pattern % GetCppWrapperType( |
250 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | |
208 if mojom.IsMapKind(kind): | 251 if mojom.IsMapKind(kind): |
209 pattern = None | 252 pattern = None |
210 if _use_new_wrapper_types: | 253 if _use_new_wrapper_types: |
211 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else | 254 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else |
212 "std::unordered_map<%s, %s>") | 255 "std::unordered_map<%s, %s>") |
213 if mojom.IsNullableKind(kind): | 256 if mojom.IsNullableKind(kind): |
214 pattern = _AddOptional(pattern) | 257 pattern = _AddOptional(pattern) |
215 else: | 258 else: |
216 pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" | 259 pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" |
217 return pattern % (GetCppWrapperType(kind.key_kind), | 260 return pattern % ( |
218 GetCppWrapperType(kind.value_kind)) | 261 GetCppWrapperType( |
262 kind.key_kind, | |
263 add_same_module_namespaces=add_same_module_namespaces), | |
264 GetCppWrapperType( | |
265 kind.value_kind, | |
266 add_same_module_namespaces=add_same_module_namespaces)) | |
219 if mojom.IsInterfaceKind(kind): | 267 if mojom.IsInterfaceKind(kind): |
220 return "%sPtr" % GetNameForKind(kind) | 268 return "%sPtr" % GetNameForKind( |
269 kind, add_same_module_namespaces=add_same_module_namespaces) | |
221 if mojom.IsInterfaceRequestKind(kind): | 270 if mojom.IsInterfaceRequestKind(kind): |
222 return "%sRequest" % GetNameForKind(kind.kind) | 271 return "%sRequest" % GetNameForKind( |
272 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | |
223 if mojom.IsAssociatedInterfaceKind(kind): | 273 if mojom.IsAssociatedInterfaceKind(kind): |
224 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) | 274 return "%sAssociatedPtrInfo" % GetNameForKind( |
275 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | |
225 if mojom.IsAssociatedInterfaceRequestKind(kind): | 276 if mojom.IsAssociatedInterfaceRequestKind(kind): |
226 return "%sAssociatedRequest" % GetNameForKind(kind.kind) | 277 return "%sAssociatedRequest" % GetNameForKind( |
278 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | |
227 if mojom.IsStringKind(kind): | 279 if mojom.IsStringKind(kind): |
228 if _for_blink: | 280 if _for_blink: |
229 return "WTF::String" | 281 return "WTF::String" |
230 if not _use_new_wrapper_types: | 282 if not _use_new_wrapper_types: |
231 return "mojo::String" | 283 return "mojo::String" |
232 type_name = "std::string" | 284 type_name = "std::string" |
233 return _AddOptional(type_name) if mojom.IsNullableKind(kind) else type_name | 285 return _AddOptional(type_name) if mojom.IsNullableKind(kind) else type_name |
234 if mojom.IsGenericHandleKind(kind): | 286 if mojom.IsGenericHandleKind(kind): |
235 return "mojo::ScopedHandle" | 287 return "mojo::ScopedHandle" |
236 if mojom.IsDataPipeConsumerKind(kind): | 288 if mojom.IsDataPipeConsumerKind(kind): |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 def GetCppUnionFieldType(kind): | 358 def GetCppUnionFieldType(kind): |
307 if mojom.IsUnionKind(kind): | 359 if mojom.IsUnionKind(kind): |
308 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) | 360 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) |
309 return GetCppFieldType(kind) | 361 return GetCppFieldType(kind) |
310 | 362 |
311 def GetUnionGetterReturnType(kind): | 363 def GetUnionGetterReturnType(kind): |
312 if mojom.IsReferenceKind(kind): | 364 if mojom.IsReferenceKind(kind): |
313 return "%s&" % GetCppWrapperType(kind) | 365 return "%s&" % GetCppWrapperType(kind) |
314 return GetCppWrapperType(kind) | 366 return GetCppWrapperType(kind) |
315 | 367 |
368 def GetUnionTraitGetterReturnType(kind): | |
369 """Get field type used in UnionTraits template specialization. | |
370 | |
371 The type may be qualified as UnionTraits specializations live outside the | |
372 namespace where e.g. structs are defined. | |
373 | |
374 Args: | |
375 kind: {Kind} The type of the field. | |
376 | |
377 Returns: | |
378 {str} The C++ type to use for the field. | |
379 """ | |
380 if mojom.IsReferenceKind(kind): | |
381 return "%s&" % GetCppWrapperType(kind, add_same_module_namespaces=True) | |
382 return GetCppWrapperType(kind, add_same_module_namespaces=True) | |
383 | |
316 def GetCppDataViewType(kind, qualified=False): | 384 def GetCppDataViewType(kind, qualified=False): |
317 def _GetName(input_kind): | 385 def _GetName(input_kind): |
318 return _NameFormatter(input_kind, None).FormatForCpp( | 386 return _NameFormatter(input_kind, None).FormatForCpp( |
319 add_same_module_namespaces=qualified, flatten_nested_kind=True) | 387 add_same_module_namespaces=qualified, flatten_nested_kind=True) |
320 | 388 |
321 if mojom.IsEnumKind(kind): | 389 if mojom.IsEnumKind(kind): |
322 return _GetName(kind) | 390 return _GetName(kind) |
323 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 391 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
324 return "%sDataView" % _GetName(kind) | 392 return "%sDataView" % _GetName(kind) |
325 if mojom.IsArrayKind(kind): | 393 if mojom.IsArrayKind(kind): |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 not mojom.IsStringKind(kind)): | 526 not mojom.IsStringKind(kind)): |
459 return "nullptr" | 527 return "nullptr" |
460 | 528 |
461 return "new mojo::internal::ContainerValidateParams(%s)" % ( | 529 return "new mojo::internal::ContainerValidateParams(%s)" % ( |
462 GetContainerValidateParamsCtorArgs(kind)) | 530 GetContainerValidateParamsCtorArgs(kind)) |
463 | 531 |
464 class Generator(generator.Generator): | 532 class Generator(generator.Generator): |
465 | 533 |
466 cpp_filters = { | 534 cpp_filters = { |
467 "constant_value": ConstantValue, | 535 "constant_value": ConstantValue, |
536 "contains_handles": mojom.ContainsHandles, | |
468 "contains_move_only_members": ContainsMoveOnlyMembers, | 537 "contains_move_only_members": ContainsMoveOnlyMembers, |
469 "cpp_wrapper_param_type": GetCppWrapperParamType, | 538 "cpp_wrapper_param_type": GetCppWrapperParamType, |
470 "cpp_data_view_type": GetCppDataViewType, | 539 "cpp_data_view_type": GetCppDataViewType, |
471 "cpp_field_type": GetCppFieldType, | 540 "cpp_field_type": GetCppFieldType, |
472 "cpp_union_field_type": GetCppUnionFieldType, | 541 "cpp_union_field_type": GetCppUnionFieldType, |
473 "cpp_pod_type": GetCppPodType, | 542 "cpp_pod_type": GetCppPodType, |
474 "cpp_union_getter_return_type": GetUnionGetterReturnType, | 543 "cpp_union_getter_return_type": GetUnionGetterReturnType, |
544 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType, | |
475 "cpp_wrapper_type": GetCppWrapperType, | 545 "cpp_wrapper_type": GetCppWrapperType, |
476 "default_value": DefaultValue, | 546 "default_value": DefaultValue, |
477 "expression_to_text": ExpressionToText, | 547 "expression_to_text": ExpressionToText, |
478 "get_container_validate_params_ctor_args": | 548 "get_container_validate_params_ctor_args": |
479 GetContainerValidateParamsCtorArgs, | 549 GetContainerValidateParamsCtorArgs, |
480 "get_name_for_kind": GetNameForKind, | 550 "get_name_for_kind": GetNameForKind, |
481 "get_pad": pack.GetPad, | 551 "get_pad": pack.GetPad, |
482 "get_qualified_name_for_kind": GetQualifiedNameForKind, | 552 "get_qualified_name_for_kind": GetQualifiedNameForKind, |
483 "has_callbacks": mojom.HasCallbacks, | 553 "has_callbacks": mojom.HasCallbacks, |
484 "has_sync_methods": mojom.HasSyncMethods, | 554 "has_sync_methods": mojom.HasSyncMethods, |
485 "requires_context_for_data_view": RequiresContextForDataView, | 555 "requires_context_for_data_view": RequiresContextForDataView, |
486 "should_inline": ShouldInlineStruct, | 556 "should_inline": ShouldInlineStruct, |
487 "should_inline_union": ShouldInlineUnion, | 557 "should_inline_union": ShouldInlineUnion, |
488 "is_array_kind": mojom.IsArrayKind, | 558 "is_array_kind": mojom.IsArrayKind, |
489 "is_enum_kind": mojom.IsEnumKind, | 559 "is_enum_kind": mojom.IsEnumKind, |
490 "is_integral_kind": mojom.IsIntegralKind, | 560 "is_integral_kind": mojom.IsIntegralKind, |
491 "is_native_only_kind": IsNativeOnlyKind, | 561 "is_native_only_kind": IsNativeOnlyKind, |
492 "is_any_handle_kind": mojom.IsAnyHandleKind, | 562 "is_any_handle_kind": mojom.IsAnyHandleKind, |
493 "is_any_interface_kind": mojom.IsAnyInterfaceKind, | 563 "is_any_interface_kind": mojom.IsAnyInterfaceKind, |
494 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, | 564 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, |
495 "is_associated_kind": mojom.IsAssociatedKind, | 565 "is_associated_kind": mojom.IsAssociatedKind, |
566 "is_hashable": IsHashableKind, | |
496 "is_map_kind": mojom.IsMapKind, | 567 "is_map_kind": mojom.IsMapKind, |
497 "is_nullable_kind": mojom.IsNullableKind, | 568 "is_nullable_kind": mojom.IsNullableKind, |
498 "is_object_kind": mojom.IsObjectKind, | 569 "is_object_kind": mojom.IsObjectKind, |
570 "is_reference_kind": mojom.IsReferenceKind, | |
499 "is_string_kind": mojom.IsStringKind, | 571 "is_string_kind": mojom.IsStringKind, |
500 "is_struct_kind": mojom.IsStructKind, | 572 "is_struct_kind": mojom.IsStructKind, |
501 "is_typemapped_kind": IsTypemappedKind, | 573 "is_typemapped_kind": IsTypemappedKind, |
502 "is_union_kind": mojom.IsUnionKind, | 574 "is_union_kind": mojom.IsUnionKind, |
503 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 575 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
504 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 576 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
505 "stylize_method": generator.StudlyCapsToCamel, | 577 "stylize_method": generator.StudlyCapsToCamel, |
506 "under_to_camel": generator.UnderToCamel, | 578 "under_to_camel": generator.UnderToCamel, |
507 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, | 579 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, |
508 } | 580 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 global _use_new_wrapper_types | 665 global _use_new_wrapper_types |
594 _use_new_wrapper_types = self.use_new_wrapper_types | 666 _use_new_wrapper_types = self.use_new_wrapper_types |
595 global _variant | 667 global _variant |
596 _variant = self.variant | 668 _variant = self.variant |
597 suffix = "-%s" % self.variant if self.variant else "" | 669 suffix = "-%s" % self.variant if self.variant else "" |
598 self.Write(self.GenerateModuleHeader(), | 670 self.Write(self.GenerateModuleHeader(), |
599 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 671 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
600 self.Write( | 672 self.Write( |
601 self.GenerateModuleSource(), | 673 self.GenerateModuleSource(), |
602 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 674 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |