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

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

Issue 1618963006: Mojo C++ bindings: support enum validation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 if field.default: 54 if field.default:
55 if mojom.IsStructKind(field.kind): 55 if mojom.IsStructKind(field.kind):
56 assert field.default == "default" 56 assert field.default == "default"
57 return "%s::New()" % GetNameForKind(field.kind) 57 return "%s::New()" % GetNameForKind(field.kind)
58 return ExpressionToText(field.default, kind=field.kind) 58 return ExpressionToText(field.default, kind=field.kind)
59 return "" 59 return ""
60 60
61 def NamespaceToArray(namespace): 61 def NamespaceToArray(namespace):
62 return namespace.split(".") if namespace else [] 62 return namespace.split(".") if namespace else []
63 63
64 def GetNameForKind(kind, internal = False): 64 def GetNamePartsForKind(kind, add_same_module_namespaces, internal):
65 def MapKindName_(kind):
66 if not internal:
67 return kind.name
68 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or
69 mojom.IsInterfaceKind(kind) or mojom.IsEnumKind(kind)):
70 return kind.name + "_Data"
71 return kind.name
72
65 parts = [] 73 parts = []
66 if kind.imported_from: 74 if kind.imported_from:
67 parts.extend(NamespaceToArray(kind.imported_from["namespace"])) 75 parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
76 elif hasattr(kind, "module") and add_same_module_namespaces:
77 parts.extend(NamespaceToArray(kind.module.namespace))
68 if internal: 78 if internal:
69 parts.append("internal") 79 parts.append("internal")
70 if kind.parent_kind: 80 if kind.parent_kind:
71 parts.append(kind.parent_kind.name) 81 parts.append(MapKindName_(kind.parent_kind))
72 parts.append(kind.name) 82 parts.append(MapKindName_(kind))
83 return parts
84
85 def GetNameForKind(kind, internal=False):
86 parts = GetNamePartsForKind(kind, False, internal)
87 return "::".join(parts)
88
89 def GetQualifiedNameForKind(kind, internal=False):
90 # Always start with an empty part to force a leading "::" on output.
91 parts = [""]
92 parts.extend(GetNamePartsForKind(kind, True, internal))
73 return "::".join(parts) 93 return "::".join(parts)
74 94
75 def GetFullMojomNameForKind(kind): 95 def GetFullMojomNameForKind(kind):
76 parts = [] 96 parts = GetNamePartsForKind(kind, True, False)
77 if kind.imported_from:
78 parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
79 elif hasattr(kind, "module"):
80 parts.extend(NamespaceToArray(kind.module.namespace))
81 parts.append(kind.name)
82 return ".".join(parts) 97 return ".".join(parts)
83 98
84 def IsTypemappedKind(kind): 99 def IsTypemappedKind(kind):
85 return hasattr(kind, "name") and \ 100 return hasattr(kind, "name") and \
86 GetFullMojomNameForKind(kind) in _current_typemap 101 GetFullMojomNameForKind(kind) in _current_typemap
87 102
88 def IsNativeOnlyKind(kind): 103 def IsNativeOnlyKind(kind):
89 return IsTypemappedKind(kind) and kind.native_only 104 return IsTypemappedKind(kind) and kind.native_only
90 105
91 def GetNativeTypeName(typemapped_kind): 106 def GetNativeTypeName(typemapped_kind):
92 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] 107 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"]
93 108
94 def GetQualifiedNameForKind(kind):
95 # Always start with an empty part to force a leading "::" on output.
96 parts = [""]
97 parts.extend(NamespaceToArray(kind.module.namespace))
98 parts.append(kind.name)
99 return "::".join(parts)
100
101 def GetCppType(kind): 109 def GetCppType(kind):
102 if mojom.IsStructKind(kind) and kind.native_only: 110 if mojom.IsStructKind(kind) and kind.native_only:
103 raise Exception("Should not be reached!") 111 raise Exception("Should not be reached!")
104 if mojom.IsArrayKind(kind): 112 if mojom.IsArrayKind(kind):
105 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) 113 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind)
106 if mojom.IsMapKind(kind): 114 if mojom.IsMapKind(kind):
107 return "mojo::internal::Map_Data<%s, %s>*" % ( 115 return "mojo::internal::Map_Data<%s, %s>*" % (
108 GetCppType(kind.key_kind), GetCppType(kind.value_kind)) 116 GetCppType(kind.key_kind), GetCppType(kind.value_kind))
109 if mojom.IsStructKind(kind): 117 if mojom.IsStructKind(kind):
110 return "%s_Data*" % GetNameForKind(kind, internal=True) 118 return "%s*" % GetNameForKind(kind, internal=True)
111 if mojom.IsUnionKind(kind): 119 if mojom.IsUnionKind(kind):
112 return "%s_Data" % GetNameForKind(kind, internal=True) 120 return "%s" % GetNameForKind(kind, internal=True)
113 if mojom.IsInterfaceKind(kind): 121 if mojom.IsInterfaceKind(kind):
114 return "mojo::internal::Interface_Data" 122 return "mojo::internal::Interface_Data"
115 if mojom.IsInterfaceRequestKind(kind): 123 if mojom.IsInterfaceRequestKind(kind):
116 return "mojo::MessagePipeHandle" 124 return "mojo::MessagePipeHandle"
117 if mojom.IsAssociatedInterfaceKind(kind): 125 if mojom.IsAssociatedInterfaceKind(kind):
118 return "mojo::internal::AssociatedInterface_Data" 126 return "mojo::internal::AssociatedInterface_Data"
119 if mojom.IsAssociatedInterfaceRequestKind(kind): 127 if mojom.IsAssociatedInterfaceRequestKind(kind):
120 return "mojo::internal::AssociatedInterfaceRequest_Data" 128 return "mojo::internal::AssociatedInterfaceRequest_Data"
121 if mojom.IsEnumKind(kind): 129 if mojom.IsEnumKind(kind):
122 return "int32_t" 130 return GetNameForKind(kind, internal=True)
123 if mojom.IsStringKind(kind): 131 if mojom.IsStringKind(kind):
124 return "mojo::internal::String_Data*" 132 return "mojo::internal::String_Data*"
125 return _kind_to_cpp_type[kind] 133 return _kind_to_cpp_type[kind]
126 134
127 def GetCppPodType(kind): 135 def GetCppPodType(kind):
128 if mojom.IsStringKind(kind): 136 if mojom.IsStringKind(kind):
129 return "char*" 137 return "char*"
130 return _kind_to_cpp_type[kind] 138 return _kind_to_cpp_type[kind]
131 139
132 def GetCppArrayArgWrapperType(kind): 140 def GetCppArrayArgWrapperType(kind):
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 if mojom.IsSharedBufferKind(kind): 281 if mojom.IsSharedBufferKind(kind):
274 return "mojo::ScopedSharedBufferHandle" 282 return "mojo::ScopedSharedBufferHandle"
275 if not kind in _kind_to_cpp_type: 283 if not kind in _kind_to_cpp_type:
276 print "missing:", kind.spec 284 print "missing:", kind.spec
277 return _kind_to_cpp_type[kind] 285 return _kind_to_cpp_type[kind]
278 286
279 def GetCppFieldType(kind): 287 def GetCppFieldType(kind):
280 if IsNativeOnlyKind(kind): 288 if IsNativeOnlyKind(kind):
281 return "mojo::internal::ArrayPointer<uint8_t>" 289 return "mojo::internal::ArrayPointer<uint8_t>"
282 if mojom.IsStructKind(kind): 290 if mojom.IsStructKind(kind):
283 return ("mojo::internal::StructPointer<%s_Data>" % 291 return ("mojo::internal::StructPointer<%s>" %
284 GetNameForKind(kind, internal=True)) 292 GetNameForKind(kind, internal=True))
285 if mojom.IsUnionKind(kind): 293 if mojom.IsUnionKind(kind):
286 return "%s_Data" % GetNameForKind(kind, internal=True) 294 return "%s" % GetNameForKind(kind, internal=True)
287 if mojom.IsArrayKind(kind): 295 if mojom.IsArrayKind(kind):
288 return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind) 296 return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind)
289 if mojom.IsMapKind(kind): 297 if mojom.IsMapKind(kind):
290 return ("mojo::internal::StructPointer<mojo::internal::Map_Data<%s, %s>>" % 298 return ("mojo::internal::StructPointer<mojo::internal::Map_Data<%s, %s>>" %
291 (GetCppType(kind.key_kind), GetCppType(kind.value_kind))) 299 (GetCppType(kind.key_kind), GetCppType(kind.value_kind)))
292 if mojom.IsInterfaceKind(kind): 300 if mojom.IsInterfaceKind(kind):
293 return "mojo::internal::Interface_Data" 301 return "mojo::internal::Interface_Data"
294 if mojom.IsInterfaceRequestKind(kind): 302 if mojom.IsInterfaceRequestKind(kind):
295 return "mojo::MessagePipeHandle" 303 return "mojo::MessagePipeHandle"
296 if mojom.IsAssociatedInterfaceKind(kind): 304 if mojom.IsAssociatedInterfaceKind(kind):
297 return "mojo::internal::AssociatedInterface_Data" 305 return "mojo::internal::AssociatedInterface_Data"
298 if mojom.IsAssociatedInterfaceRequestKind(kind): 306 if mojom.IsAssociatedInterfaceRequestKind(kind):
299 return "mojo::internal::AssociatedInterfaceRequest_Data" 307 return "mojo::internal::AssociatedInterfaceRequest_Data"
300 if mojom.IsEnumKind(kind): 308 if mojom.IsEnumKind(kind):
301 return GetNameForKind(kind) 309 return GetNameForKind(kind, internal=True)
302 if mojom.IsStringKind(kind): 310 if mojom.IsStringKind(kind):
303 return "mojo::internal::StringPointer" 311 return "mojo::internal::StringPointer"
304 return _kind_to_cpp_type[kind] 312 return _kind_to_cpp_type[kind]
305 313
306 def GetCppUnionFieldType(kind): 314 def GetCppUnionFieldType(kind):
307 if mojom.IsAnyHandleKind(kind): 315 if mojom.IsAnyHandleKind(kind):
308 return "MojoHandle" 316 return "MojoHandle"
309 if mojom.IsInterfaceKind(kind): 317 if mojom.IsInterfaceKind(kind):
310 return "uint64_t" 318 return "uint64_t"
311 if mojom.IsEnumKind(kind):
312 return "int32_t"
313 if mojom.IsUnionKind(kind): 319 if mojom.IsUnionKind(kind):
314 return ("mojo::internal::UnionPointer<%s_Data>" % 320 return ("mojo::internal::UnionPointer<%s>" %
315 GetNameForKind(kind, internal=True)) 321 GetNameForKind(kind, internal=True))
316 return GetCppFieldType(kind) 322 return GetCppFieldType(kind)
317 323
318 def GetUnionGetterReturnType(kind): 324 def GetUnionGetterReturnType(kind):
319 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or 325 if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or
320 mojom.IsArrayKind(kind) or mojom.IsMapKind(kind) or 326 mojom.IsArrayKind(kind) or mojom.IsMapKind(kind) or
321 mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind) 327 mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind)
322 or mojom.IsAssociatedKind(kind)): 328 or mojom.IsAssociatedKind(kind)):
323 return "%s&" % GetCppWrapperType(kind) 329 return "%s&" % GetCppWrapperType(kind)
324 return GetCppResultWrapperType(kind) 330 return GetCppResultWrapperType(kind)
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 def GenerateFiles(self, args): 515 def GenerateFiles(self, args):
510 global _current_typemap 516 global _current_typemap
511 _current_typemap = self.typemap 517 _current_typemap = self.typemap
512 suffix = "-%s" % self.variant if self.variant else "" 518 suffix = "-%s" % self.variant if self.variant else ""
513 self.Write(self.GenerateModuleHeader(), 519 self.Write(self.GenerateModuleHeader(),
514 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) 520 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix)))
515 self.Write(self.GenerateModuleInternalHeader(), 521 self.Write(self.GenerateModuleInternalHeader(),
516 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) 522 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix)))
517 self.Write(self.GenerateModuleSource(), 523 self.Write(self.GenerateModuleSource(),
518 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) 524 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix)))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698