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

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

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 if kind.parent_kind: 65 if kind.parent_kind:
66 parts.append(kind.parent_kind.name) 66 parts.append(kind.parent_kind.name)
67 parts.append(kind.name) 67 parts.append(kind.name)
68 return "::".join(parts) 68 return "::".join(parts)
69 69
70 def GetCppType(kind): 70 def GetCppType(kind):
71 if mojom.IsStructKind(kind): 71 if mojom.IsStructKind(kind):
72 return "%s_Data*" % GetNameForKind(kind, internal=True) 72 return "%s_Data*" % GetNameForKind(kind, internal=True)
73 if mojom.IsAnyArrayKind(kind): 73 if mojom.IsAnyArrayKind(kind):
74 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) 74 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind)
75 if mojom.IsMapKind(kind):
76 return ("mojo::internal::Map_Data<%s, %s>*" %
77 (GetCppType(kind.key_kind), GetCppType(kind.value_kind)))
75 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): 78 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
76 return "mojo::MessagePipeHandle" 79 return "mojo::MessagePipeHandle"
77 if mojom.IsEnumKind(kind): 80 if mojom.IsEnumKind(kind):
78 return "int32_t" 81 return "int32_t"
79 if mojom.IsStringKind(kind): 82 if mojom.IsStringKind(kind):
80 return "mojo::internal::String_Data*" 83 return "mojo::internal::String_Data*"
81 return _kind_to_cpp_type[kind] 84 return _kind_to_cpp_type[kind]
82 85
83 def GetCppPodType(kind): 86 def GetCppPodType(kind):
84 if mojom.IsStringKind(kind): 87 if mojom.IsStringKind(kind):
85 return "char*" 88 return "char*"
86 return _kind_to_cpp_type[kind] 89 return _kind_to_cpp_type[kind]
87 90
88 def GetCppArrayArgWrapperType(kind): 91 def GetCppArrayArgWrapperType(kind):
89 if mojom.IsEnumKind(kind): 92 if mojom.IsEnumKind(kind):
90 return GetNameForKind(kind) 93 return GetNameForKind(kind)
91 if mojom.IsStructKind(kind): 94 if mojom.IsStructKind(kind):
92 return "%sPtr" % GetNameForKind(kind) 95 return "%sPtr" % GetNameForKind(kind)
93 if mojom.IsAnyArrayKind(kind): 96 if mojom.IsAnyArrayKind(kind):
94 return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind) 97 return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind)
98 if mojom.IsMapKind(kind):
99 return "mojo::Map<%s, %s> " % (GetCppArrayArgWrapperType(kind.key_kind),
100 GetCppArrayArgWrapperType(kind.value_kind))
95 if mojom.IsInterfaceKind(kind): 101 if mojom.IsInterfaceKind(kind):
96 raise Exception("Arrays of interfaces not yet supported!") 102 raise Exception("Arrays of interfaces not yet supported!")
97 if mojom.IsInterfaceRequestKind(kind): 103 if mojom.IsInterfaceRequestKind(kind):
98 raise Exception("Arrays of interface requests not yet supported!") 104 raise Exception("Arrays of interface requests not yet supported!")
99 if mojom.IsStringKind(kind): 105 if mojom.IsStringKind(kind):
100 return "mojo::String" 106 return "mojo::String"
101 if mojom.IsHandleKind(kind): 107 if mojom.IsHandleKind(kind):
102 return "mojo::ScopedHandle" 108 return "mojo::ScopedHandle"
103 if mojom.IsDataPipeConsumerKind(kind): 109 if mojom.IsDataPipeConsumerKind(kind):
104 return "mojo::ScopedDataPipeConsumerHandle" 110 return "mojo::ScopedDataPipeConsumerHandle"
105 if mojom.IsDataPipeProducerKind(kind): 111 if mojom.IsDataPipeProducerKind(kind):
106 return "mojo::ScopedDataPipeProducerHandle" 112 return "mojo::ScopedDataPipeProducerHandle"
107 if mojom.IsMessagePipeKind(kind): 113 if mojom.IsMessagePipeKind(kind):
108 return "mojo::ScopedMessagePipeHandle" 114 return "mojo::ScopedMessagePipeHandle"
109 if mojom.IsSharedBufferKind(kind): 115 if mojom.IsSharedBufferKind(kind):
110 return "mojo::ScopedSharedBufferHandle" 116 return "mojo::ScopedSharedBufferHandle"
111 return _kind_to_cpp_type[kind] 117 return _kind_to_cpp_type[kind]
112 118
113 def GetCppResultWrapperType(kind): 119 def GetCppResultWrapperType(kind):
114 if mojom.IsEnumKind(kind): 120 if mojom.IsEnumKind(kind):
115 return GetNameForKind(kind) 121 return GetNameForKind(kind)
116 if mojom.IsStructKind(kind): 122 if mojom.IsStructKind(kind):
117 return "%sPtr" % GetNameForKind(kind) 123 return "%sPtr" % GetNameForKind(kind)
118 if mojom.IsAnyArrayKind(kind): 124 if mojom.IsAnyArrayKind(kind):
119 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) 125 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
126 if mojom.IsMapKind(kind):
127 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind),
128 GetCppArrayArgWrapperType(kind.value_kind))
120 if mojom.IsInterfaceKind(kind): 129 if mojom.IsInterfaceKind(kind):
121 return "%sPtr" % GetNameForKind(kind) 130 return "%sPtr" % GetNameForKind(kind)
122 if mojom.IsInterfaceRequestKind(kind): 131 if mojom.IsInterfaceRequestKind(kind):
123 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) 132 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind)
124 if mojom.IsStringKind(kind): 133 if mojom.IsStringKind(kind):
125 return "mojo::String" 134 return "mojo::String"
126 if mojom.IsHandleKind(kind): 135 if mojom.IsHandleKind(kind):
127 return "mojo::ScopedHandle" 136 return "mojo::ScopedHandle"
128 if mojom.IsDataPipeConsumerKind(kind): 137 if mojom.IsDataPipeConsumerKind(kind):
129 return "mojo::ScopedDataPipeConsumerHandle" 138 return "mojo::ScopedDataPipeConsumerHandle"
130 if mojom.IsDataPipeProducerKind(kind): 139 if mojom.IsDataPipeProducerKind(kind):
131 return "mojo::ScopedDataPipeProducerHandle" 140 return "mojo::ScopedDataPipeProducerHandle"
132 if mojom.IsMessagePipeKind(kind): 141 if mojom.IsMessagePipeKind(kind):
133 return "mojo::ScopedMessagePipeHandle" 142 return "mojo::ScopedMessagePipeHandle"
134 if mojom.IsSharedBufferKind(kind): 143 if mojom.IsSharedBufferKind(kind):
135 return "mojo::ScopedSharedBufferHandle" 144 return "mojo::ScopedSharedBufferHandle"
136 return _kind_to_cpp_type[kind] 145 return _kind_to_cpp_type[kind]
137 146
138 def GetCppWrapperType(kind): 147 def GetCppWrapperType(kind):
139 if mojom.IsEnumKind(kind): 148 if mojom.IsEnumKind(kind):
140 return GetNameForKind(kind) 149 return GetNameForKind(kind)
141 if mojom.IsStructKind(kind): 150 if mojom.IsStructKind(kind):
142 return "%sPtr" % GetNameForKind(kind) 151 return "%sPtr" % GetNameForKind(kind)
143 if mojom.IsAnyArrayKind(kind): 152 if mojom.IsAnyArrayKind(kind):
144 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) 153 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
154 if mojom.IsMapKind(kind):
155 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind),
156 GetCppArrayArgWrapperType(kind.value_kind))
145 if mojom.IsInterfaceKind(kind): 157 if mojom.IsInterfaceKind(kind):
146 return "%sPtr" % GetNameForKind(kind) 158 return "%sPtr" % GetNameForKind(kind)
147 if mojom.IsInterfaceRequestKind(kind): 159 if mojom.IsInterfaceRequestKind(kind):
148 raise Exception("InterfaceRequest fields not supported!") 160 raise Exception("InterfaceRequest fields not supported!")
149 if mojom.IsStringKind(kind): 161 if mojom.IsStringKind(kind):
150 return "mojo::String" 162 return "mojo::String"
151 if mojom.IsHandleKind(kind): 163 if mojom.IsHandleKind(kind):
152 return "mojo::ScopedHandle" 164 return "mojo::ScopedHandle"
153 if mojom.IsDataPipeConsumerKind(kind): 165 if mojom.IsDataPipeConsumerKind(kind):
154 return "mojo::ScopedDataPipeConsumerHandle" 166 return "mojo::ScopedDataPipeConsumerHandle"
155 if mojom.IsDataPipeProducerKind(kind): 167 if mojom.IsDataPipeProducerKind(kind):
156 return "mojo::ScopedDataPipeProducerHandle" 168 return "mojo::ScopedDataPipeProducerHandle"
157 if mojom.IsMessagePipeKind(kind): 169 if mojom.IsMessagePipeKind(kind):
158 return "mojo::ScopedMessagePipeHandle" 170 return "mojo::ScopedMessagePipeHandle"
159 if mojom.IsSharedBufferKind(kind): 171 if mojom.IsSharedBufferKind(kind):
160 return "mojo::ScopedSharedBufferHandle" 172 return "mojo::ScopedSharedBufferHandle"
161 return _kind_to_cpp_type[kind] 173 return _kind_to_cpp_type[kind]
162 174
163 def GetCppConstWrapperType(kind): 175 def GetCppConstWrapperType(kind):
164 if mojom.IsStructKind(kind): 176 if mojom.IsStructKind(kind):
165 return "%sPtr" % GetNameForKind(kind) 177 return "%sPtr" % GetNameForKind(kind)
166 if mojom.IsAnyArrayKind(kind): 178 if mojom.IsAnyArrayKind(kind):
167 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) 179 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
180 if mojom.IsMapKind(kind):
181 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind),
182 GetCppArrayArgWrapperType(kind.value_kind))
168 if mojom.IsInterfaceKind(kind): 183 if mojom.IsInterfaceKind(kind):
169 return "%sPtr" % GetNameForKind(kind) 184 return "%sPtr" % GetNameForKind(kind)
170 if mojom.IsInterfaceRequestKind(kind): 185 if mojom.IsInterfaceRequestKind(kind):
171 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) 186 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind)
172 if mojom.IsEnumKind(kind): 187 if mojom.IsEnumKind(kind):
173 return GetNameForKind(kind) 188 return GetNameForKind(kind)
174 if mojom.IsStringKind(kind): 189 if mojom.IsStringKind(kind):
175 return "const mojo::String&" 190 return "const mojo::String&"
176 if mojom.IsHandleKind(kind): 191 if mojom.IsHandleKind(kind):
177 return "mojo::ScopedHandle" 192 return "mojo::ScopedHandle"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 else: 276 else:
262 expected_num_elements = generator.ExpectedArraySize(kind) 277 expected_num_elements = generator.ExpectedArraySize(kind)
263 element_is_nullable = mojom.IsNullableKind(kind.kind) 278 element_is_nullable = mojom.IsNullableKind(kind.kind)
264 element_validate_params = GetArrayValidateParams(kind.kind) 279 element_validate_params = GetArrayValidateParams(kind.kind)
265 280
266 return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % ( 281 return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % (
267 expected_num_elements, 282 expected_num_elements,
268 'true' if element_is_nullable else 'false', 283 'true' if element_is_nullable else 'false',
269 element_validate_params) 284 element_validate_params)
270 285
286 def GetMapValidateParams(kind):
287 # Unlike GetArrayValidateParams, we are given the wrapped kind, instead of
288 # the raw array kind. So we wrap the return value of GetArrayValidateParams.
289 element_is_nullable = mojom.IsNullableKind(kind)
290 return "mojo::internal::ArrayValidateParams<0, %s,\n%s> " % (
291 'true' if element_is_nullable else 'false',
292 GetArrayValidateParams(kind))
293
271 _HEADER_SIZE = 8 294 _HEADER_SIZE = 8
272 295
273 class Generator(generator.Generator): 296 class Generator(generator.Generator):
274 297
275 cpp_filters = { 298 cpp_filters = {
276 "constant_value": ConstantValue, 299 "constant_value": ConstantValue,
277 "cpp_const_wrapper_type": GetCppConstWrapperType, 300 "cpp_const_wrapper_type": GetCppConstWrapperType,
278 "cpp_field_type": GetCppFieldType, 301 "cpp_field_type": GetCppFieldType,
279 "cpp_pod_type": GetCppPodType, 302 "cpp_pod_type": GetCppPodType,
280 "cpp_result_type": GetCppResultWrapperType, 303 "cpp_result_type": GetCppResultWrapperType,
281 "cpp_type": GetCppType, 304 "cpp_type": GetCppType,
282 "cpp_wrapper_type": GetCppWrapperType, 305 "cpp_wrapper_type": GetCppWrapperType,
283 "default_value": DefaultValue, 306 "default_value": DefaultValue,
284 "expected_array_size": generator.ExpectedArraySize, 307 "expected_array_size": generator.ExpectedArraySize,
285 "expression_to_text": ExpressionToText, 308 "expression_to_text": ExpressionToText,
286 "get_array_validate_params": GetArrayValidateParams, 309 "get_array_validate_params": GetArrayValidateParams,
310 "get_map_validate_params": GetMapValidateParams,
287 "get_name_for_kind": GetNameForKind, 311 "get_name_for_kind": GetNameForKind,
288 "get_pad": pack.GetPad, 312 "get_pad": pack.GetPad,
289 "has_callbacks": mojom.HasCallbacks, 313 "has_callbacks": mojom.HasCallbacks,
290 "should_inline": ShouldInlineStruct, 314 "should_inline": ShouldInlineStruct,
291 "is_any_array_kind": mojom.IsAnyArrayKind, 315 "is_any_array_kind": mojom.IsAnyArrayKind,
292 "is_enum_kind": mojom.IsEnumKind, 316 "is_enum_kind": mojom.IsEnumKind,
293 "is_move_only_kind": mojom.IsMoveOnlyKind, 317 "is_move_only_kind": mojom.IsMoveOnlyKind,
294 "is_any_handle_kind": mojom.IsAnyHandleKind, 318 "is_any_handle_kind": mojom.IsAnyHandleKind,
295 "is_interface_kind": mojom.IsInterfaceKind, 319 "is_interface_kind": mojom.IsInterfaceKind,
296 "is_interface_request_kind": mojom.IsInterfaceRequestKind, 320 "is_interface_request_kind": mojom.IsInterfaceRequestKind,
321 "is_map_kind": mojom.IsMapKind,
297 "is_nullable_kind": mojom.IsNullableKind, 322 "is_nullable_kind": mojom.IsNullableKind,
298 "is_object_kind": mojom.IsObjectKind, 323 "is_object_kind": mojom.IsObjectKind,
299 "is_string_kind": mojom.IsStringKind, 324 "is_string_kind": mojom.IsStringKind,
300 "is_struct_with_handles": IsStructWithHandles, 325 "is_struct_with_handles": IsStructWithHandles,
301 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, 326 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
302 "struct_from_method": generator.GetStructFromMethod, 327 "struct_from_method": generator.GetStructFromMethod,
303 "response_struct_from_method": generator.GetResponseStructFromMethod, 328 "response_struct_from_method": generator.GetResponseStructFromMethod,
304 "stylize_method": generator.StudlyCapsToCamel, 329 "stylize_method": generator.StudlyCapsToCamel,
305 "to_all_caps": generator.CamelCaseToAllCaps, 330 "to_all_caps": generator.CamelCaseToAllCaps,
306 } 331 }
(...skipping 20 matching lines...) Expand all
327 352
328 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) 353 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters)
329 def GenerateModuleSource(self): 354 def GenerateModuleSource(self):
330 return self.GetJinjaExports() 355 return self.GetJinjaExports()
331 356
332 def GenerateFiles(self, args): 357 def GenerateFiles(self, args):
333 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) 358 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name)
334 self.Write(self.GenerateModuleInternalHeader(), 359 self.Write(self.GenerateModuleInternalHeader(),
335 "%s-internal.h" % self.module.name) 360 "%s-internal.h" % self.module.name)
336 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) 361 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698