OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 """Generates C++ source files from a mojom.Module.""" | |
6 | |
7 import mojom.generate.generator as generator | |
8 import mojom.generate.module as mojom | |
9 import mojom.generate.pack as pack | |
10 from mojom.generate.template_expander import UseJinja | |
11 | |
12 | |
13 _kind_to_cpp_type = { | |
14 mojom.BOOL: "bool", | |
15 mojom.INT8: "int8_t", | |
16 mojom.UINT8: "uint8_t", | |
17 mojom.INT16: "int16_t", | |
18 mojom.UINT16: "uint16_t", | |
19 mojom.INT32: "int32_t", | |
20 mojom.UINT32: "uint32_t", | |
21 mojom.FLOAT: "float", | |
22 mojom.HANDLE: "mojo::Handle", | |
23 mojom.DCPIPE: "mojo::DataPipeConsumerHandle", | |
24 mojom.DPPIPE: "mojo::DataPipeProducerHandle", | |
25 mojom.MSGPIPE: "mojo::MessagePipeHandle", | |
26 mojom.SHAREDBUFFER: "mojo::SharedBufferHandle", | |
27 mojom.NULLABLE_HANDLE: "mojo::Handle", | |
28 mojom.NULLABLE_DCPIPE: "mojo::DataPipeConsumerHandle", | |
29 mojom.NULLABLE_DPPIPE: "mojo::DataPipeProducerHandle", | |
30 mojom.NULLABLE_MSGPIPE: "mojo::MessagePipeHandle", | |
31 mojom.NULLABLE_SHAREDBUFFER: "mojo::SharedBufferHandle", | |
32 mojom.INT64: "int64_t", | |
33 mojom.UINT64: "uint64_t", | |
34 mojom.DOUBLE: "double", | |
35 } | |
36 | |
37 _kind_to_cpp_literal_suffix = { | |
38 mojom.UINT8: "U", | |
39 mojom.UINT16: "U", | |
40 mojom.UINT32: "U", | |
41 mojom.FLOAT: "f", | |
42 mojom.UINT64: "ULL", | |
43 } | |
44 | |
45 def ConstantValue(constant): | |
46 return ExpressionToText(constant.value, kind=constant.kind) | |
47 | |
48 def DefaultValue(field): | |
49 if field.default: | |
50 if mojom.IsStructKind(field.kind): | |
51 assert field.default == "default" | |
52 return "%s::New()" % GetNameForKind(field.kind) | |
53 return ExpressionToText(field.default, kind=field.kind) | |
54 return "" | |
55 | |
56 def NamespaceToArray(namespace): | |
57 return namespace.split('.') if namespace else [] | |
58 | |
59 def GetNameForKind(kind, internal = False): | |
60 parts = [] | |
61 if kind.imported_from: | |
62 parts.extend(NamespaceToArray(kind.imported_from["namespace"])) | |
63 if internal: | |
64 parts.append("internal") | |
65 if kind.parent_kind: | |
66 parts.append(kind.parent_kind.name) | |
67 parts.append(kind.name) | |
68 return "::".join(parts) | |
69 | |
70 def GetCppType(kind): | |
71 if mojom.IsArrayKind(kind): | |
72 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) | |
73 if mojom.IsMapKind(kind): | |
74 return "mojo::internal::Map_Data<%s, %s>*" % ( | |
75 GetCppType(kind.key_kind), GetCppType(kind.value_kind)) | |
76 if mojom.IsStructKind(kind): | |
77 return "%s_Data*" % GetNameForKind(kind, internal=True) | |
78 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | |
79 return "mojo::MessagePipeHandle" | |
80 if mojom.IsEnumKind(kind): | |
81 return "int32_t" | |
82 if mojom.IsStringKind(kind): | |
83 return "mojo::internal::String_Data*" | |
84 return _kind_to_cpp_type[kind] | |
85 | |
86 def GetCppPodType(kind): | |
87 if mojom.IsStringKind(kind): | |
88 return "char*" | |
89 return _kind_to_cpp_type[kind] | |
90 | |
91 def GetCppArrayArgWrapperType(kind): | |
92 if mojom.IsEnumKind(kind): | |
93 return GetNameForKind(kind) | |
94 if mojom.IsStructKind(kind): | |
95 return "%sPtr" % GetNameForKind(kind) | |
96 if mojom.IsArrayKind(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)) | |
101 if mojom.IsInterfaceKind(kind): | |
102 raise Exception("Arrays of interfaces not yet supported!") | |
103 if mojom.IsInterfaceRequestKind(kind): | |
104 raise Exception("Arrays of interface requests not yet supported!") | |
105 if mojom.IsStringKind(kind): | |
106 return "mojo::String" | |
107 if mojom.IsHandleKind(kind): | |
108 return "mojo::ScopedHandle" | |
109 if mojom.IsDataPipeConsumerKind(kind): | |
110 return "mojo::ScopedDataPipeConsumerHandle" | |
111 if mojom.IsDataPipeProducerKind(kind): | |
112 return "mojo::ScopedDataPipeProducerHandle" | |
113 if mojom.IsMessagePipeKind(kind): | |
114 return "mojo::ScopedMessagePipeHandle" | |
115 if mojom.IsSharedBufferKind(kind): | |
116 return "mojo::ScopedSharedBufferHandle" | |
117 return _kind_to_cpp_type[kind] | |
118 | |
119 def GetCppResultWrapperType(kind): | |
120 if mojom.IsEnumKind(kind): | |
121 return GetNameForKind(kind) | |
122 if mojom.IsStructKind(kind): | |
123 return "%sPtr" % GetNameForKind(kind) | |
124 if mojom.IsArrayKind(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)) | |
129 if mojom.IsInterfaceKind(kind): | |
130 return "%sPtr" % GetNameForKind(kind) | |
131 if mojom.IsInterfaceRequestKind(kind): | |
132 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) | |
133 if mojom.IsStringKind(kind): | |
134 return "mojo::String" | |
135 if mojom.IsHandleKind(kind): | |
136 return "mojo::ScopedHandle" | |
137 if mojom.IsDataPipeConsumerKind(kind): | |
138 return "mojo::ScopedDataPipeConsumerHandle" | |
139 if mojom.IsDataPipeProducerKind(kind): | |
140 return "mojo::ScopedDataPipeProducerHandle" | |
141 if mojom.IsMessagePipeKind(kind): | |
142 return "mojo::ScopedMessagePipeHandle" | |
143 if mojom.IsSharedBufferKind(kind): | |
144 return "mojo::ScopedSharedBufferHandle" | |
145 return _kind_to_cpp_type[kind] | |
146 | |
147 def GetCppWrapperType(kind): | |
148 if mojom.IsEnumKind(kind): | |
149 return GetNameForKind(kind) | |
150 if mojom.IsStructKind(kind): | |
151 return "%sPtr" % GetNameForKind(kind) | |
152 if mojom.IsArrayKind(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)) | |
157 if mojom.IsInterfaceKind(kind): | |
158 return "%sPtr" % GetNameForKind(kind) | |
159 if mojom.IsInterfaceRequestKind(kind): | |
160 raise Exception("InterfaceRequest fields not supported!") | |
161 if mojom.IsStringKind(kind): | |
162 return "mojo::String" | |
163 if mojom.IsHandleKind(kind): | |
164 return "mojo::ScopedHandle" | |
165 if mojom.IsDataPipeConsumerKind(kind): | |
166 return "mojo::ScopedDataPipeConsumerHandle" | |
167 if mojom.IsDataPipeProducerKind(kind): | |
168 return "mojo::ScopedDataPipeProducerHandle" | |
169 if mojom.IsMessagePipeKind(kind): | |
170 return "mojo::ScopedMessagePipeHandle" | |
171 if mojom.IsSharedBufferKind(kind): | |
172 return "mojo::ScopedSharedBufferHandle" | |
173 return _kind_to_cpp_type[kind] | |
174 | |
175 def GetCppConstWrapperType(kind): | |
176 if mojom.IsStructKind(kind): | |
177 return "%sPtr" % GetNameForKind(kind) | |
178 if mojom.IsArrayKind(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)) | |
183 if mojom.IsInterfaceKind(kind): | |
184 return "%sPtr" % GetNameForKind(kind) | |
185 if mojom.IsInterfaceRequestKind(kind): | |
186 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) | |
187 if mojom.IsEnumKind(kind): | |
188 return GetNameForKind(kind) | |
189 if mojom.IsStringKind(kind): | |
190 return "const mojo::String&" | |
191 if mojom.IsHandleKind(kind): | |
192 return "mojo::ScopedHandle" | |
193 if mojom.IsDataPipeConsumerKind(kind): | |
194 return "mojo::ScopedDataPipeConsumerHandle" | |
195 if mojom.IsDataPipeProducerKind(kind): | |
196 return "mojo::ScopedDataPipeProducerHandle" | |
197 if mojom.IsMessagePipeKind(kind): | |
198 return "mojo::ScopedMessagePipeHandle" | |
199 if mojom.IsSharedBufferKind(kind): | |
200 return "mojo::ScopedSharedBufferHandle" | |
201 if not kind in _kind_to_cpp_type: | |
202 print "missing:", kind.spec | |
203 return _kind_to_cpp_type[kind] | |
204 | |
205 def GetCppFieldType(kind): | |
206 if mojom.IsStructKind(kind): | |
207 return ("mojo::internal::StructPointer<%s_Data>" % | |
208 GetNameForKind(kind, internal=True)) | |
209 if mojom.IsArrayKind(kind): | |
210 return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind) | |
211 if mojom.IsMapKind(kind): | |
212 return ("mojo::internal::StructPointer<mojo::internal::Map_Data<%s, %s>>" % | |
213 (GetCppType(kind.key_kind), GetCppType(kind.value_kind))) | |
214 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | |
215 return "mojo::MessagePipeHandle" | |
216 if mojom.IsEnumKind(kind): | |
217 return GetNameForKind(kind) | |
218 if mojom.IsStringKind(kind): | |
219 return "mojo::internal::StringPointer" | |
220 return _kind_to_cpp_type[kind] | |
221 | |
222 def IsStructWithHandles(struct): | |
223 for pf in struct.packed.packed_fields: | |
224 if mojom.IsAnyHandleKind(pf.field.kind): | |
225 return True | |
226 return False | |
227 | |
228 def TranslateConstants(token, kind): | |
229 if isinstance(token, mojom.NamedValue): | |
230 # Both variable and enum constants are constructed like: | |
231 # Namespace::Struct::CONSTANT_NAME | |
232 # For enums, CONSTANT_NAME is ENUM_NAME_ENUM_VALUE. | |
233 name = [] | |
234 if token.imported_from: | |
235 name.extend(NamespaceToArray(token.namespace)) | |
236 if token.parent_kind: | |
237 name.append(token.parent_kind.name) | |
238 if isinstance(token, mojom.EnumValue): | |
239 name.append( | |
240 "%s_%s" % (generator.CamelCaseToAllCaps(token.enum.name), token.name)) | |
241 else: | |
242 name.append(token.name) | |
243 return "::".join(name) | |
244 | |
245 if isinstance(token, mojom.BuiltinValue): | |
246 if token.value == "double.INFINITY" or token.value == "float.INFINITY": | |
247 return "INFINITY"; | |
248 if token.value == "double.NEGATIVE_INFINITY" or \ | |
249 token.value == "float.NEGATIVE_INFINITY": | |
250 return "-INFINITY"; | |
251 if token.value == "double.NAN" or token.value == "float.NAN": | |
252 return "NAN"; | |
253 | |
254 if (kind is not None and mojom.IsFloatKind(kind)): | |
255 return token if token.isdigit() else token + "f"; | |
256 | |
257 # Per C++11, 2.14.2, the type of an integer literal is the first of the | |
258 # corresponding list in Table 6 in which its value can be represented. In this | |
259 # case, the list for decimal constants with no suffix is: | |
260 # int, long int, long long int | |
261 # The standard considers a program ill-formed if it contains an integer | |
262 # literal that cannot be represented by any of the allowed types. | |
263 # | |
264 # As it turns out, MSVC doesn't bother trying to fall back to long long int, | |
265 # so the integral constant -2147483648 causes it grief: it decides to | |
266 # represent 2147483648 as an unsigned integer, and then warns that the unary | |
267 # minus operator doesn't make sense on unsigned types. Doh! | |
268 if kind == mojom.INT32 and token == '-2147483648': | |
269 return '(-%d - 1) /* %s */' % ( | |
270 2**31 - 1, 'Workaround for MSVC bug; see https://crbug.com/445618') | |
271 | |
272 return '%s%s' % (token, _kind_to_cpp_literal_suffix.get(kind, '')) | |
273 | |
274 def ExpressionToText(value, kind=None): | |
275 return TranslateConstants(value, kind) | |
276 | |
277 def ShouldInlineStruct(struct): | |
278 # TODO(darin): Base this on the size of the wrapper class. | |
279 if len(struct.fields) > 4: | |
280 return False | |
281 for field in struct.fields: | |
282 if mojom.IsMoveOnlyKind(field.kind): | |
283 return False | |
284 return True | |
285 | |
286 def GetArrayValidateParams(kind): | |
287 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and | |
288 not mojom.IsStringKind(kind)): | |
289 return "mojo::internal::NoValidateParams" | |
290 | |
291 if mojom.IsStringKind(kind): | |
292 expected_num_elements = 0 | |
293 element_is_nullable = False | |
294 element_validate_params = "mojo::internal::NoValidateParams" | |
295 elif mojom.IsMapKind(kind): | |
296 expected_num_elements = 0 | |
297 element_is_nullable = mojom.IsNullableKind(kind.value_kind) | |
298 element_validate_params = GetArrayValidateParams(kind.value_kind) | |
299 else: | |
300 expected_num_elements = generator.ExpectedArraySize(kind) or 0 | |
301 element_is_nullable = mojom.IsNullableKind(kind.kind) | |
302 element_validate_params = GetArrayValidateParams(kind.kind) | |
303 | |
304 return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % ( | |
305 expected_num_elements, | |
306 'true' if element_is_nullable else 'false', | |
307 element_validate_params) | |
308 | |
309 def GetMapValidateParams(value_kind): | |
310 # Unlike GetArrayValidateParams, we are given the wrapped kind, instead of | |
311 # the raw array kind. So we wrap the return value of GetArrayValidateParams. | |
312 element_is_nullable = mojom.IsNullableKind(value_kind) | |
313 return "mojo::internal::ArrayValidateParams<0, %s,\n%s> " % ( | |
314 'true' if element_is_nullable else 'false', | |
315 GetArrayValidateParams(value_kind)) | |
316 | |
317 _HEADER_SIZE = 8 | |
318 | |
319 class Generator(generator.Generator): | |
320 | |
321 cpp_filters = { | |
322 "constant_value": ConstantValue, | |
323 "cpp_const_wrapper_type": GetCppConstWrapperType, | |
324 "cpp_field_type": GetCppFieldType, | |
325 "cpp_pod_type": GetCppPodType, | |
326 "cpp_result_type": GetCppResultWrapperType, | |
327 "cpp_type": GetCppType, | |
328 "cpp_wrapper_type": GetCppWrapperType, | |
329 "default_value": DefaultValue, | |
330 "expression_to_text": ExpressionToText, | |
331 "get_array_validate_params": GetArrayValidateParams, | |
332 "get_map_validate_params": GetMapValidateParams, | |
333 "get_name_for_kind": GetNameForKind, | |
334 "get_pad": pack.GetPad, | |
335 "has_callbacks": mojom.HasCallbacks, | |
336 "should_inline": ShouldInlineStruct, | |
337 "is_array_kind": mojom.IsArrayKind, | |
338 "is_cloneable_kind": mojom.IsCloneableKind, | |
339 "is_enum_kind": mojom.IsEnumKind, | |
340 "is_move_only_kind": mojom.IsMoveOnlyKind, | |
341 "is_any_handle_kind": mojom.IsAnyHandleKind, | |
342 "is_interface_kind": mojom.IsInterfaceKind, | |
343 "is_interface_request_kind": mojom.IsInterfaceRequestKind, | |
344 "is_map_kind": mojom.IsMapKind, | |
345 "is_nullable_kind": mojom.IsNullableKind, | |
346 "is_object_kind": mojom.IsObjectKind, | |
347 "is_string_kind": mojom.IsStringKind, | |
348 "is_struct_kind": mojom.IsStructKind, | |
349 "is_struct_with_handles": IsStructWithHandles, | |
350 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | |
351 "struct_from_method": generator.GetStructFromMethod, | |
352 "response_struct_from_method": generator.GetResponseStructFromMethod, | |
353 "stylize_method": generator.StudlyCapsToCamel, | |
354 "to_all_caps": generator.CamelCaseToAllCaps, | |
355 } | |
356 | |
357 def GetJinjaExports(self): | |
358 return { | |
359 "module": self.module, | |
360 "namespace": self.module.namespace, | |
361 "namespaces_as_array": NamespaceToArray(self.module.namespace), | |
362 "imports": self.module.imports, | |
363 "kinds": self.module.kinds, | |
364 "enums": self.module.enums, | |
365 "structs": self.GetStructs(), | |
366 "interfaces": self.module.interfaces, | |
367 } | |
368 | |
369 @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters) | |
370 def GenerateModuleHeader(self): | |
371 return self.GetJinjaExports() | |
372 | |
373 @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters) | |
374 def GenerateModuleInternalHeader(self): | |
375 return self.GetJinjaExports() | |
376 | |
377 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) | |
378 def GenerateModuleSource(self): | |
379 return self.GetJinjaExports() | |
380 | |
381 def GenerateFiles(self, args): | |
382 self.Write(self.GenerateModuleHeader(), | |
383 self.MatchMojomFilePath("%s.h" % self.module.name)) | |
384 self.Write(self.GenerateModuleInternalHeader(), | |
385 self.MatchMojomFilePath("%s-internal.h" % self.module.name)) | |
386 self.Write(self.GenerateModuleSource(), | |
387 self.MatchMojomFilePath("%s.cc" % self.module.name)) | |
OLD | NEW |