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

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

Issue 1751563002: Mojo C++ bindings: support mapping mojo string to WTF::String. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
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 # TODO(rockot): Get rid of this global. 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 _use_wtf_types = False
48 49
49 50
50 def ConstantValue(constant): 51 def ConstantValue(constant):
51 return ExpressionToText(constant.value, kind=constant.kind) 52 return ExpressionToText(constant.value, kind=constant.kind)
52 53
53 def DefaultValue(field): 54 def DefaultValue(field):
54 if field.default: 55 if field.default:
55 if mojom.IsStructKind(field.kind): 56 if mojom.IsStructKind(field.kind):
56 assert field.default == "default" 57 assert field.default == "default"
57 return "%s::New()" % GetNameForKind(field.kind) 58 return "%s::New()" % GetNameForKind(field.kind)
58 return ExpressionToText(field.default, kind=field.kind) 59 return ExpressionToText(field.default, kind=field.kind)
59 if (mojom.IsStringKind(field.kind) or mojom.IsArrayKind(field.kind) or 60 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind):
60 mojom.IsMapKind(field.kind)):
61 return "nullptr"; 61 return "nullptr";
62 if mojom.IsStringKind(field.kind):
63 return "" if _use_wtf_types else "nullptr"
62 return "" 64 return ""
63 65
64 def NamespaceToArray(namespace): 66 def NamespaceToArray(namespace):
65 return namespace.split(".") if namespace else [] 67 return namespace.split(".") if namespace else []
66 68
67 def GetNamePartsForKind(kind, add_same_module_namespaces, internal): 69 def GetNamePartsForKind(kind, add_same_module_namespaces, internal):
68 def MapKindName_(kind): 70 def MapKindName_(kind):
69 if not internal: 71 if not internal:
70 return kind.name 72 return kind.name
71 if mojom.IsStructKind(kind) and kind.native_only: 73 if mojom.IsStructKind(kind) and kind.native_only:
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 if mojom.IsInterfaceKind(kind): 179 if mojom.IsInterfaceKind(kind):
178 raise Exception("Arrays of interfaces not yet supported!") 180 raise Exception("Arrays of interfaces not yet supported!")
179 if mojom.IsInterfaceRequestKind(kind): 181 if mojom.IsInterfaceRequestKind(kind):
180 raise Exception("Arrays of interface requests not yet supported!") 182 raise Exception("Arrays of interface requests not yet supported!")
181 if mojom.IsAssociatedInterfaceKind(kind): 183 if mojom.IsAssociatedInterfaceKind(kind):
182 raise Exception("Arrays of associated interfaces not yet supported!") 184 raise Exception("Arrays of associated interfaces not yet supported!")
183 if mojom.IsAssociatedInterfaceRequestKind(kind): 185 if mojom.IsAssociatedInterfaceRequestKind(kind):
184 raise Exception("Arrays of associated interface requests not yet " 186 raise Exception("Arrays of associated interface requests not yet "
185 "supported!") 187 "supported!")
186 if mojom.IsStringKind(kind): 188 if mojom.IsStringKind(kind):
187 return "mojo::String" 189 return "WTF::String" if _use_wtf_types else "mojo::String"
188 if mojom.IsGenericHandleKind(kind): 190 if mojom.IsGenericHandleKind(kind):
189 return "mojo::ScopedHandle" 191 return "mojo::ScopedHandle"
190 if mojom.IsDataPipeConsumerKind(kind): 192 if mojom.IsDataPipeConsumerKind(kind):
191 return "mojo::ScopedDataPipeConsumerHandle" 193 return "mojo::ScopedDataPipeConsumerHandle"
192 if mojom.IsDataPipeProducerKind(kind): 194 if mojom.IsDataPipeProducerKind(kind):
193 return "mojo::ScopedDataPipeProducerHandle" 195 return "mojo::ScopedDataPipeProducerHandle"
194 if mojom.IsMessagePipeKind(kind): 196 if mojom.IsMessagePipeKind(kind):
195 return "mojo::ScopedMessagePipeHandle" 197 return "mojo::ScopedMessagePipeHandle"
196 if mojom.IsSharedBufferKind(kind): 198 if mojom.IsSharedBufferKind(kind):
197 return "mojo::ScopedSharedBufferHandle" 199 return "mojo::ScopedSharedBufferHandle"
(...skipping 15 matching lines...) Expand all
213 GetCppArrayArgWrapperType(kind.value_kind)) 215 GetCppArrayArgWrapperType(kind.value_kind))
214 if mojom.IsInterfaceKind(kind): 216 if mojom.IsInterfaceKind(kind):
215 return "%sPtr" % GetNameForKind(kind) 217 return "%sPtr" % GetNameForKind(kind)
216 if mojom.IsInterfaceRequestKind(kind): 218 if mojom.IsInterfaceRequestKind(kind):
217 return "%sRequest" % GetNameForKind(kind.kind) 219 return "%sRequest" % GetNameForKind(kind.kind)
218 if mojom.IsAssociatedInterfaceKind(kind): 220 if mojom.IsAssociatedInterfaceKind(kind):
219 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) 221 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind)
220 if mojom.IsAssociatedInterfaceRequestKind(kind): 222 if mojom.IsAssociatedInterfaceRequestKind(kind):
221 return "%sAssociatedRequest" % GetNameForKind(kind.kind) 223 return "%sAssociatedRequest" % GetNameForKind(kind.kind)
222 if mojom.IsStringKind(kind): 224 if mojom.IsStringKind(kind):
223 return "mojo::String" 225 return "WTF::String" if _use_wtf_types else "mojo::String"
224 if mojom.IsGenericHandleKind(kind): 226 if mojom.IsGenericHandleKind(kind):
225 return "mojo::ScopedHandle" 227 return "mojo::ScopedHandle"
226 if mojom.IsDataPipeConsumerKind(kind): 228 if mojom.IsDataPipeConsumerKind(kind):
227 return "mojo::ScopedDataPipeConsumerHandle" 229 return "mojo::ScopedDataPipeConsumerHandle"
228 if mojom.IsDataPipeProducerKind(kind): 230 if mojom.IsDataPipeProducerKind(kind):
229 return "mojo::ScopedDataPipeProducerHandle" 231 return "mojo::ScopedDataPipeProducerHandle"
230 if mojom.IsMessagePipeKind(kind): 232 if mojom.IsMessagePipeKind(kind):
231 return "mojo::ScopedMessagePipeHandle" 233 return "mojo::ScopedMessagePipeHandle"
232 if mojom.IsSharedBufferKind(kind): 234 if mojom.IsSharedBufferKind(kind):
233 return "mojo::ScopedSharedBufferHandle" 235 return "mojo::ScopedSharedBufferHandle"
(...skipping 21 matching lines...) Expand all
255 GetCppArrayArgWrapperType(kind.value_kind)) 257 GetCppArrayArgWrapperType(kind.value_kind))
256 if mojom.IsInterfaceKind(kind): 258 if mojom.IsInterfaceKind(kind):
257 return "%sPtr" % GetNameForKind(kind) 259 return "%sPtr" % GetNameForKind(kind)
258 if mojom.IsInterfaceRequestKind(kind): 260 if mojom.IsInterfaceRequestKind(kind):
259 return "%sRequest" % GetNameForKind(kind.kind) 261 return "%sRequest" % GetNameForKind(kind.kind)
260 if mojom.IsAssociatedInterfaceKind(kind): 262 if mojom.IsAssociatedInterfaceKind(kind):
261 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) 263 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind)
262 if mojom.IsAssociatedInterfaceRequestKind(kind): 264 if mojom.IsAssociatedInterfaceRequestKind(kind):
263 return "%sAssociatedRequest" % GetNameForKind(kind.kind) 265 return "%sAssociatedRequest" % GetNameForKind(kind.kind)
264 if mojom.IsStringKind(kind): 266 if mojom.IsStringKind(kind):
265 return "mojo::String" 267 return "WTF::String" if _use_wtf_types else "mojo::String"
266 if mojom.IsGenericHandleKind(kind): 268 if mojom.IsGenericHandleKind(kind):
267 return "mojo::ScopedHandle" 269 return "mojo::ScopedHandle"
268 if mojom.IsDataPipeConsumerKind(kind): 270 if mojom.IsDataPipeConsumerKind(kind):
269 return "mojo::ScopedDataPipeConsumerHandle" 271 return "mojo::ScopedDataPipeConsumerHandle"
270 if mojom.IsDataPipeProducerKind(kind): 272 if mojom.IsDataPipeProducerKind(kind):
271 return "mojo::ScopedDataPipeProducerHandle" 273 return "mojo::ScopedDataPipeProducerHandle"
272 if mojom.IsMessagePipeKind(kind): 274 if mojom.IsMessagePipeKind(kind):
273 return "mojo::ScopedMessagePipeHandle" 275 return "mojo::ScopedMessagePipeHandle"
274 if mojom.IsSharedBufferKind(kind): 276 if mojom.IsSharedBufferKind(kind):
275 return "mojo::ScopedSharedBufferHandle" 277 return "mojo::ScopedSharedBufferHandle"
(...skipping 15 matching lines...) Expand all
291 return "%sPtr" % GetNameForKind(kind) 293 return "%sPtr" % GetNameForKind(kind)
292 if mojom.IsInterfaceRequestKind(kind): 294 if mojom.IsInterfaceRequestKind(kind):
293 return "%sRequest" % GetNameForKind(kind.kind) 295 return "%sRequest" % GetNameForKind(kind.kind)
294 if mojom.IsAssociatedInterfaceKind(kind): 296 if mojom.IsAssociatedInterfaceKind(kind):
295 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) 297 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind)
296 if mojom.IsAssociatedInterfaceRequestKind(kind): 298 if mojom.IsAssociatedInterfaceRequestKind(kind):
297 return "%sAssociatedRequest" % GetNameForKind(kind.kind) 299 return "%sAssociatedRequest" % GetNameForKind(kind.kind)
298 if mojom.IsEnumKind(kind): 300 if mojom.IsEnumKind(kind):
299 return GetNameForKind(kind) 301 return GetNameForKind(kind)
300 if mojom.IsStringKind(kind): 302 if mojom.IsStringKind(kind):
301 return "const mojo::String&" 303 return "const WTF::String&" if _use_wtf_types else "const mojo::String&"
302 if mojom.IsGenericHandleKind(kind): 304 if mojom.IsGenericHandleKind(kind):
303 return "mojo::ScopedHandle" 305 return "mojo::ScopedHandle"
304 if mojom.IsDataPipeConsumerKind(kind): 306 if mojom.IsDataPipeConsumerKind(kind):
305 return "mojo::ScopedDataPipeConsumerHandle" 307 return "mojo::ScopedDataPipeConsumerHandle"
306 if mojom.IsDataPipeProducerKind(kind): 308 if mojom.IsDataPipeProducerKind(kind):
307 return "mojo::ScopedDataPipeProducerHandle" 309 return "mojo::ScopedDataPipeProducerHandle"
308 if mojom.IsMessagePipeKind(kind): 310 if mojom.IsMessagePipeKind(kind):
309 return "mojo::ScopedMessagePipeHandle" 311 return "mojo::ScopedMessagePipeHandle"
310 if mojom.IsSharedBufferKind(kind): 312 if mojom.IsSharedBufferKind(kind):
311 return "mojo::ScopedSharedBufferHandle" 313 return "mojo::ScopedSharedBufferHandle"
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 "namespace": self.module.namespace, 517 "namespace": self.module.namespace,
516 "namespaces_as_array": NamespaceToArray(self.module.namespace), 518 "namespaces_as_array": NamespaceToArray(self.module.namespace),
517 "imports": self.module.imports, 519 "imports": self.module.imports,
518 "kinds": self.module.kinds, 520 "kinds": self.module.kinds,
519 "enums": self.module.enums, 521 "enums": self.module.enums,
520 "structs": self.GetStructs(), 522 "structs": self.GetStructs(),
521 "unions": self.GetUnions(), 523 "unions": self.GetUnions(),
522 "interfaces": self.GetInterfaces(), 524 "interfaces": self.GetInterfaces(),
523 "variant": self.variant, 525 "variant": self.variant,
524 "extra_headers": self.GetExtraHeaders(), 526 "extra_headers": self.GetExtraHeaders(),
527 "use_wtf_types": self.use_wtf_types,
525 } 528 }
526 529
527 @staticmethod 530 @staticmethod
528 def GetTemplatePrefix(): 531 def GetTemplatePrefix():
529 return "cpp_templates" 532 return "cpp_templates"
530 533
531 @classmethod 534 @classmethod
532 def GetFilters(cls): 535 def GetFilters(cls):
533 return cls.cpp_filters 536 return cls.cpp_filters
534 537
535 @UseJinja("module.h.tmpl") 538 @UseJinja("module.h.tmpl")
536 def GenerateModuleHeader(self): 539 def GenerateModuleHeader(self):
537 return self.GetJinjaExports() 540 return self.GetJinjaExports()
538 541
539 @UseJinja("module-internal.h.tmpl") 542 @UseJinja("module-internal.h.tmpl")
540 def GenerateModuleInternalHeader(self): 543 def GenerateModuleInternalHeader(self):
541 return self.GetJinjaExports() 544 return self.GetJinjaExports()
542 545
543 @UseJinja("module.cc.tmpl") 546 @UseJinja("module.cc.tmpl")
544 def GenerateModuleSource(self): 547 def GenerateModuleSource(self):
545 return self.GetJinjaExports() 548 return self.GetJinjaExports()
546 549
547 def GenerateFiles(self, args): 550 def GenerateFiles(self, args):
548 global _current_typemap 551 global _current_typemap
549 _current_typemap = self.typemap 552 _current_typemap = self.typemap
553 global _use_wtf_types
554 _use_wtf_types = self.use_wtf_types
550 suffix = "-%s" % self.variant if self.variant else "" 555 suffix = "-%s" % self.variant if self.variant else ""
551 self.Write(self.GenerateModuleHeader(), 556 self.Write(self.GenerateModuleHeader(),
552 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) 557 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix)))
553 self.Write(self.GenerateModuleInternalHeader(), 558 self.Write(self.GenerateModuleInternalHeader(),
554 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) 559 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix)))
555 self.Write(self.GenerateModuleSource(), 560 self.Write(self.GenerateModuleSource(),
556 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) 561 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix)))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698