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

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

Issue 1811853004: Mojo C++ bindigns: fix generated namespace when variant is specified. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add missing new file Created 4 years, 9 months 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/interfaces/bindings/tests/test_variant.mojom ('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 28 matching lines...) Expand all
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 # 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
50 # it right.
51 _variant = None
49 52
50 53
51 def ConstantValue(constant): 54 def ConstantValue(constant):
52 return ExpressionToText(constant.value, kind=constant.kind) 55 return ExpressionToText(constant.value, kind=constant.kind)
53 56
54 def DefaultValue(field): 57 def DefaultValue(field):
55 if field.default: 58 if field.default:
56 if mojom.IsStructKind(field.kind): 59 if mojom.IsStructKind(field.kind):
57 assert field.default == "default" 60 assert field.default == "default"
58 return "%s::New()" % GetNameForKind(field.kind) 61 return "%s::New()" % GetNameForKind(field.kind)
59 return ExpressionToText(field.default, kind=field.kind) 62 return ExpressionToText(field.default, kind=field.kind)
60 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): 63 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind):
61 return "nullptr"; 64 return "nullptr";
62 if mojom.IsStringKind(field.kind): 65 if mojom.IsStringKind(field.kind):
63 return "" if _for_blink else "nullptr" 66 return "" if _for_blink else "nullptr"
64 return "" 67 return ""
65 68
66 def NamespaceToArray(namespace): 69 def NamespaceToArray(namespace):
67 return namespace.split(".") if namespace else [] 70 return namespace.split(".") if namespace else []
68 71
69 def GetNamePartsForKind(kind, add_same_module_namespaces, internal): 72 def GetNamePartsForKind(kind, add_same_module_namespaces, add_variant,
73 internal):
70 def MapKindName_(kind): 74 def MapKindName_(kind):
71 if not internal: 75 if not internal:
72 return kind.name 76 return kind.name
73 if mojom.IsStructKind(kind) and kind.native_only: 77 if mojom.IsStructKind(kind) and kind.native_only:
74 return "mojo::Array_Data<uint8_t>" 78 return "mojo::Array_Data<uint8_t>"
75 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or 79 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or
76 mojom.IsInterfaceKind(kind) or mojom.IsEnumKind(kind)): 80 mojom.IsInterfaceKind(kind) or mojom.IsEnumKind(kind)):
77 return kind.name + "_Data" 81 return kind.name + "_Data"
78 return kind.name 82 return kind.name
79 83
80 parts = [] 84 parts = []
81 if kind.imported_from: 85 if kind.imported_from:
Sam McNally 2016/03/17 17:37:58 We'll want to add the variant here too, but it can
yzshen1 2016/03/17 17:46:36 Right. I know that part is incorrect currently. To
82 parts.extend(NamespaceToArray(kind.imported_from["namespace"])) 86 parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
83 elif hasattr(kind, "module") and add_same_module_namespaces: 87 elif add_same_module_namespaces:
84 parts.extend(NamespaceToArray(kind.module.namespace)) 88 if hasattr(kind, "module"):
Sam McNally 2016/03/17 17:37:58 I don't think it makes sense to add the variant if
yzshen1 2016/03/17 17:46:36 I am not quite sure I have understood the problem:
Sam McNally 2016/03/17 17:52:55 Yes, but if kind.module doesn't exist when |add_sa
89 parts.extend(NamespaceToArray(kind.module.namespace))
90 if _variant and add_variant:
91 parts.append(_variant)
85 if internal: 92 if internal:
86 parts.append("internal") 93 parts.append("internal")
87 if kind.parent_kind: 94 if kind.parent_kind:
88 parts.append(MapKindName_(kind.parent_kind)) 95 parts.append(MapKindName_(kind.parent_kind))
89 parts.append(MapKindName_(kind)) 96 parts.append(MapKindName_(kind))
90 return parts 97 return parts
91 98
92 def GetNameForKind(kind, internal=False): 99 def GetNameForKind(kind, internal=False):
93 parts = GetNamePartsForKind(kind, False, internal) 100 parts = GetNamePartsForKind(kind, False, False, internal)
94 return "::".join(parts) 101 return "::".join(parts)
95 102
96 def GetQualifiedNameForKind(kind, internal=False): 103 def GetQualifiedNameForKind(kind, internal=False):
97 # Always start with an empty part to force a leading "::" on output. 104 # Always start with an empty part to force a leading "::" on output.
98 parts = [""] 105 parts = [""]
99 parts.extend(GetNamePartsForKind(kind, True, internal)) 106 parts.extend(GetNamePartsForKind(kind, True, True, internal))
100 return "::".join(parts) 107 return "::".join(parts)
101 108
102 def GetFullMojomNameForKind(kind): 109 def GetFullMojomNameForKind(kind):
103 parts = GetNamePartsForKind(kind, True, False) 110 parts = GetNamePartsForKind(kind, True, False, False)
104 return ".".join(parts) 111 return ".".join(parts)
105 112
106 def IsTypemappedKind(kind): 113 def IsTypemappedKind(kind):
107 return hasattr(kind, "name") and \ 114 return hasattr(kind, "name") and \
108 GetFullMojomNameForKind(kind) in _current_typemap 115 GetFullMojomNameForKind(kind) in _current_typemap
109 116
110 def IsCloneableKind(kind): 117 def IsCloneableKind(kind):
111 return mojom.IsCloneableKind(kind, IsTypemappedKind) 118 return mojom.IsCloneableKind(kind, IsTypemappedKind)
112 119
113 def IsNativeOnlyKind(kind): 120 def IsNativeOnlyKind(kind):
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 559
553 @UseJinja("module.cc.tmpl") 560 @UseJinja("module.cc.tmpl")
554 def GenerateModuleSource(self): 561 def GenerateModuleSource(self):
555 return self.GetJinjaExports() 562 return self.GetJinjaExports()
556 563
557 def GenerateFiles(self, args): 564 def GenerateFiles(self, args):
558 global _current_typemap 565 global _current_typemap
559 _current_typemap = self.typemap 566 _current_typemap = self.typemap
560 global _for_blink 567 global _for_blink
561 _for_blink = self.for_blink 568 _for_blink = self.for_blink
569 global _variant
570 _variant = self.variant
562 suffix = "-%s" % self.variant if self.variant else "" 571 suffix = "-%s" % self.variant if self.variant else ""
563 self.Write(self.GenerateModuleHeader(), 572 self.Write(self.GenerateModuleHeader(),
564 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) 573 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix)))
565 self.Write(self.GenerateModuleInternalHeader(), 574 self.Write(self.GenerateModuleInternalHeader(),
566 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) 575 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix)))
567 self.Write(self.GenerateModuleSource(), 576 self.Write(self.GenerateModuleSource(),
568 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) 577 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix)))
OLDNEW
« no previous file with comments | « mojo/public/interfaces/bindings/tests/test_variant.mojom ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698