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