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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 | 44 |
45 # TODO(rockot): Get rid of these globals. This requires some refactoring of the | 45 # TODO(rockot): Get rid of these globals. This requires some refactoring of the |
46 # generator library code so that filters can use the generator as context. | 46 # generator library code so that filters can use the generator as context. |
47 _current_typemap = {} | 47 _current_typemap = {} |
48 _for_blink = False | 48 _for_blink = False |
49 # TODO(rockot, yzshen): The variant handling is kind of a hack currently. Make | 49 # TODO(rockot, yzshen): The variant handling is kind of a hack currently. Make |
50 # it right. | 50 # it right. |
51 _variant = None | 51 _variant = None |
52 | 52 |
53 | 53 |
54 class _NameFormatter(object): | |
55 """A formatter for the names of kinds or values.""" | |
56 | |
57 def __init__(self, token, variant): | |
58 self._token = token | |
59 self._variant = variant | |
60 | |
yzshen1
2016/03/22 20:29:14
nit: one empty line (instead of two) between class
Sam McNally
2016/03/22 21:54:30
Done.
| |
61 | |
62 def Format(self, separator, prefixed=False, internal=False, | |
63 include_variant=False, add_same_module_namespaces=False): | |
64 parts = [] | |
65 if self._ShouldIncludeNamespace(add_same_module_namespaces): | |
66 if prefixed: | |
67 parts.append("") | |
68 parts.extend(self._GetNamespace()) | |
69 if include_variant and self._variant: | |
70 parts.append(self._variant) | |
71 parts.extend(self._GetName(internal)) | |
72 return separator.join(parts) | |
73 | |
74 def FormatForCpp(self, add_same_module_namespaces=False, internal=False): | |
75 return self.Format( | |
76 "::", prefixed=True, | |
77 add_same_module_namespaces=add_same_module_namespaces, | |
78 internal=internal, include_variant=True) | |
79 | |
80 def FormatForTypemap(self): | |
yzshen1
2016/03/22 20:29:14
How about FormatForMojom? It is actually the forma
Sam McNally
2016/03/22 21:54:30
Done.
| |
81 return self.Format(".", add_same_module_namespaces=True) | |
82 | |
83 def _MapKindName(self, token, internal): | |
84 if not internal: | |
85 return token.name | |
86 if mojom.IsStructKind(token) and token.native_only: | |
87 return "mojo::Array_Data<uint8_t>" | |
88 if (mojom.IsStructKind(token) or mojom.IsUnionKind(token) or | |
89 mojom.IsInterfaceKind(token) or mojom.IsEnumKind(token)): | |
90 return token.name + "_Data" | |
91 return token.name | |
92 | |
93 def _GetName(self, internal): | |
94 name = [] | |
95 if internal: | |
96 name.append("internal") | |
97 if self._token.parent_kind: | |
98 name.append(self._MapKindName(self._token.parent_kind, internal)) | |
99 # Both variable and enum constants are constructed like: | |
100 # Namespace::Struct::CONSTANT_NAME | |
101 # For enums, CONSTANT_NAME is EnumName::ENUM_VALUE. | |
102 if isinstance(self._token, mojom.EnumValue): | |
103 name.extend([self._token.enum.name, self._token.name]) | |
104 else: | |
105 name.append(self._MapKindName(self._token, internal)) | |
106 return name | |
107 | |
108 def _ShouldIncludeNamespace(self, add_same_module_namespaces): | |
109 return add_same_module_namespaces or self._token.imported_from | |
110 | |
111 def _GetNamespace(self): | |
112 if self._token.imported_from: | |
113 return NamespaceToArray(self._token.imported_from["namespace"]) | |
114 elif hasattr(self._token, "module"): | |
115 return NamespaceToArray(self._token.module.namespace) | |
116 return [] | |
117 | |
118 | |
54 def ConstantValue(constant): | 119 def ConstantValue(constant): |
55 return ExpressionToText(constant.value, kind=constant.kind) | 120 return ExpressionToText(constant.value, kind=constant.kind) |
56 | 121 |
57 def DefaultValue(field): | 122 def DefaultValue(field): |
58 if field.default: | 123 if field.default: |
59 if mojom.IsStructKind(field.kind): | 124 if mojom.IsStructKind(field.kind): |
60 assert field.default == "default" | 125 assert field.default == "default" |
61 return "%s::New()" % GetNameForKind(field.kind) | 126 return "%s::New()" % GetNameForKind(field.kind) |
62 return ExpressionToText(field.default, kind=field.kind) | 127 return ExpressionToText(field.default, kind=field.kind) |
63 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): | 128 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): |
64 return "nullptr"; | 129 return "nullptr"; |
65 if mojom.IsStringKind(field.kind): | 130 if mojom.IsStringKind(field.kind): |
66 return "" if _for_blink else "nullptr" | 131 return "" if _for_blink else "nullptr" |
67 return "" | 132 return "" |
68 | 133 |
69 def NamespaceToArray(namespace): | 134 def NamespaceToArray(namespace): |
70 return namespace.split(".") if namespace else [] | 135 return namespace.split(".") if namespace else [] |
71 | 136 |
72 def GetNamePartsForKind(kind, add_same_module_namespaces, add_variant, | |
73 internal): | |
74 def MapKindName_(kind): | |
75 if not internal: | |
76 return kind.name | |
77 if mojom.IsStructKind(kind) and kind.native_only: | |
78 return "mojo::Array_Data<uint8_t>" | |
79 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or | |
80 mojom.IsInterfaceKind(kind) or mojom.IsEnumKind(kind)): | |
81 return kind.name + "_Data" | |
82 return kind.name | |
83 | |
84 parts = [] | |
85 if kind.imported_from: | |
86 parts.extend(NamespaceToArray(kind.imported_from["namespace"])) | |
87 if _variant and add_variant: | |
88 parts.append(_variant) | |
89 elif add_same_module_namespaces: | |
90 if hasattr(kind, "module"): | |
91 parts.extend(NamespaceToArray(kind.module.namespace)) | |
92 if _variant and add_variant: | |
93 parts.append(_variant) | |
94 if internal: | |
95 parts.append("internal") | |
96 if kind.parent_kind: | |
97 parts.append(MapKindName_(kind.parent_kind)) | |
98 parts.append(MapKindName_(kind)) | |
99 return parts | |
100 | |
101 def GetNameForKind(kind, internal=False): | 137 def GetNameForKind(kind, internal=False): |
102 parts = GetNamePartsForKind(kind, False, True, internal) | 138 return _NameFormatter(kind, _variant).FormatForCpp(internal=internal) |
103 return "::".join(parts) | |
104 | 139 |
105 def GetQualifiedNameForKind(kind, internal=False): | 140 def GetQualifiedNameForKind(kind, internal=False): |
106 # Always start with an empty part to force a leading "::" on output. | 141 return _NameFormatter(kind, _variant).FormatForCpp( |
107 parts = [""] | 142 internal=internal, add_same_module_namespaces=True) |
108 parts.extend(GetNamePartsForKind(kind, True, True, internal)) | |
109 return "::".join(parts) | |
110 | 143 |
111 def GetFullMojomNameForKind(kind): | 144 def GetFullMojomNameForKind(kind): |
112 parts = GetNamePartsForKind(kind, True, False, False) | 145 return _NameFormatter(kind, _variant).FormatForTypemap() |
113 return ".".join(parts) | |
114 | 146 |
115 def IsTypemappedKind(kind): | 147 def IsTypemappedKind(kind): |
116 return hasattr(kind, "name") and \ | 148 return hasattr(kind, "name") and \ |
117 GetFullMojomNameForKind(kind) in _current_typemap | 149 GetFullMojomNameForKind(kind) in _current_typemap |
118 | 150 |
119 def IsCloneableKind(kind): | 151 def IsCloneableKind(kind): |
120 return mojom.IsCloneableKind(kind, IsTypemappedKind) | 152 return mojom.IsCloneableKind(kind, IsTypemappedKind) |
121 | 153 |
122 def IsNativeOnlyKind(kind): | 154 def IsNativeOnlyKind(kind): |
123 return mojom.IsStructKind(kind) and kind.native_only | 155 return mojom.IsStructKind(kind) and kind.native_only |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 def GetUnionGetterReturnType(kind): | 403 def GetUnionGetterReturnType(kind): |
372 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or | 404 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or |
373 mojom.IsArrayKind(kind) or mojom.IsMapKind(kind) or | 405 mojom.IsArrayKind(kind) or mojom.IsMapKind(kind) or |
374 mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind) | 406 mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind) |
375 or mojom.IsAssociatedKind(kind)): | 407 or mojom.IsAssociatedKind(kind)): |
376 return "%s&" % GetCppWrapperType(kind) | 408 return "%s&" % GetCppWrapperType(kind) |
377 return GetCppResultWrapperType(kind) | 409 return GetCppResultWrapperType(kind) |
378 | 410 |
379 def TranslateConstants(token, kind): | 411 def TranslateConstants(token, kind): |
380 if isinstance(token, mojom.NamedValue): | 412 if isinstance(token, mojom.NamedValue): |
381 # Both variable and enum constants are constructed like: | 413 return _NameFormatter(token, _variant).FormatForCpp() |
382 # Namespace::Struct::CONSTANT_NAME | |
383 # For enums, CONSTANT_NAME is ENUM_NAME_ENUM_VALUE. | |
384 name = [] | |
385 if token.imported_from: | |
386 name.extend(NamespaceToArray(token.namespace)) | |
387 if _variant: | |
388 name.append(_variant) | |
389 if token.parent_kind: | |
390 name.append(token.parent_kind.name) | |
391 if isinstance(token, mojom.EnumValue): | |
392 name.extend([token.enum.name, token.name]) | |
393 else: | |
394 name.append(token.name) | |
395 return "::".join(name) | |
396 | 414 |
397 if isinstance(token, mojom.BuiltinValue): | 415 if isinstance(token, mojom.BuiltinValue): |
398 if token.value == "double.INFINITY" or token.value == "float.INFINITY": | 416 if token.value == "double.INFINITY" or token.value == "float.INFINITY": |
399 return "INFINITY"; | 417 return "INFINITY"; |
400 if token.value == "double.NEGATIVE_INFINITY" or \ | 418 if token.value == "double.NEGATIVE_INFINITY" or \ |
401 token.value == "float.NEGATIVE_INFINITY": | 419 token.value == "float.NEGATIVE_INFINITY": |
402 return "-INFINITY"; | 420 return "-INFINITY"; |
403 if token.value == "double.NAN" or token.value == "float.NAN": | 421 if token.value == "double.NAN" or token.value == "float.NAN": |
404 return "NAN"; | 422 return "NAN"; |
405 | 423 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
516 "is_struct_kind": mojom.IsStructKind, | 534 "is_struct_kind": mojom.IsStructKind, |
517 "is_typemapped_kind": IsTypemappedKind, | 535 "is_typemapped_kind": IsTypemappedKind, |
518 "is_union_kind": mojom.IsUnionKind, | 536 "is_union_kind": mojom.IsUnionKind, |
519 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 537 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
520 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 538 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
521 "stylize_method": generator.StudlyCapsToCamel, | 539 "stylize_method": generator.StudlyCapsToCamel, |
522 "supports_equality": DoesKindSupportEquality, | 540 "supports_equality": DoesKindSupportEquality, |
523 "under_to_camel": generator.UnderToCamel, | 541 "under_to_camel": generator.UnderToCamel, |
524 } | 542 } |
525 | 543 |
526 def GetExtraHeaders(self): | 544 def GetExtraTraitsHeaders(self): |
527 extra_headers = set() | 545 extra_headers = set() |
528 for name, entry in self.typemap.iteritems(): | 546 for entry in self.typemap.itervalues(): |
529 extra_headers.update(entry["headers"]) | 547 extra_headers.update(entry.get("traits_headers", [])) |
548 return list(extra_headers) | |
549 | |
550 def GetExtraPublicHeaders(self): | |
551 extra_headers = set() | |
552 for entry in self.typemap.itervalues(): | |
553 extra_headers.update(entry.get("public_headers", [])) | |
530 return list(extra_headers) | 554 return list(extra_headers) |
531 | 555 |
532 def GetJinjaExports(self): | 556 def GetJinjaExports(self): |
533 return { | 557 return { |
534 "module": self.module, | 558 "module": self.module, |
535 "namespace": self.module.namespace, | 559 "namespace": self.module.namespace, |
536 "namespaces_as_array": NamespaceToArray(self.module.namespace), | 560 "namespaces_as_array": NamespaceToArray(self.module.namespace), |
537 "imports": self.module.imports, | 561 "imports": self.module.imports, |
538 "kinds": self.module.kinds, | 562 "kinds": self.module.kinds, |
539 "enums": self.module.enums, | 563 "enums": self.module.enums, |
540 "structs": self.GetStructs(), | 564 "structs": self.GetStructs(), |
541 "unions": self.GetUnions(), | 565 "unions": self.GetUnions(), |
542 "interfaces": self.GetInterfaces(), | 566 "interfaces": self.GetInterfaces(), |
543 "variant": self.variant, | 567 "variant": self.variant, |
544 "extra_headers": self.GetExtraHeaders(), | 568 "extra_traits_headers": self.GetExtraTraitsHeaders(), |
569 "extra_public_headers": self.GetExtraPublicHeaders(), | |
545 "for_blink": self.for_blink, | 570 "for_blink": self.for_blink, |
546 } | 571 } |
547 | 572 |
548 @staticmethod | 573 @staticmethod |
549 def GetTemplatePrefix(): | 574 def GetTemplatePrefix(): |
550 return "cpp_templates" | 575 return "cpp_templates" |
551 | 576 |
552 @classmethod | 577 @classmethod |
553 def GetFilters(cls): | 578 def GetFilters(cls): |
554 return cls.cpp_filters | 579 return cls.cpp_filters |
(...skipping 17 matching lines...) Expand all Loading... | |
572 _for_blink = self.for_blink | 597 _for_blink = self.for_blink |
573 global _variant | 598 global _variant |
574 _variant = self.variant | 599 _variant = self.variant |
575 suffix = "-%s" % self.variant if self.variant else "" | 600 suffix = "-%s" % self.variant if self.variant else "" |
576 self.Write(self.GenerateModuleHeader(), | 601 self.Write(self.GenerateModuleHeader(), |
577 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 602 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
578 self.Write(self.GenerateModuleInternalHeader(), | 603 self.Write(self.GenerateModuleInternalHeader(), |
579 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) | 604 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) |
580 self.Write(self.GenerateModuleSource(), | 605 self.Write(self.GenerateModuleSource(), |
581 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 606 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |