OLD | NEW |
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 Loading... |
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 _for_blink = 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 _for_blink 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 if mojom.IsInterfaceKind(kind): | 182 if mojom.IsInterfaceKind(kind): |
181 raise Exception("Arrays of interfaces not yet supported!") | 183 raise Exception("Arrays of interfaces not yet supported!") |
182 if mojom.IsInterfaceRequestKind(kind): | 184 if mojom.IsInterfaceRequestKind(kind): |
183 raise Exception("Arrays of interface requests not yet supported!") | 185 raise Exception("Arrays of interface requests not yet supported!") |
184 if mojom.IsAssociatedInterfaceKind(kind): | 186 if mojom.IsAssociatedInterfaceKind(kind): |
185 raise Exception("Arrays of associated interfaces not yet supported!") | 187 raise Exception("Arrays of associated interfaces not yet supported!") |
186 if mojom.IsAssociatedInterfaceRequestKind(kind): | 188 if mojom.IsAssociatedInterfaceRequestKind(kind): |
187 raise Exception("Arrays of associated interface requests not yet " | 189 raise Exception("Arrays of associated interface requests not yet " |
188 "supported!") | 190 "supported!") |
189 if mojom.IsStringKind(kind): | 191 if mojom.IsStringKind(kind): |
190 return "mojo::String" | 192 return "WTF::String" if _for_blink else "mojo::String" |
191 if mojom.IsGenericHandleKind(kind): | 193 if mojom.IsGenericHandleKind(kind): |
192 return "mojo::ScopedHandle" | 194 return "mojo::ScopedHandle" |
193 if mojom.IsDataPipeConsumerKind(kind): | 195 if mojom.IsDataPipeConsumerKind(kind): |
194 return "mojo::ScopedDataPipeConsumerHandle" | 196 return "mojo::ScopedDataPipeConsumerHandle" |
195 if mojom.IsDataPipeProducerKind(kind): | 197 if mojom.IsDataPipeProducerKind(kind): |
196 return "mojo::ScopedDataPipeProducerHandle" | 198 return "mojo::ScopedDataPipeProducerHandle" |
197 if mojom.IsMessagePipeKind(kind): | 199 if mojom.IsMessagePipeKind(kind): |
198 return "mojo::ScopedMessagePipeHandle" | 200 return "mojo::ScopedMessagePipeHandle" |
199 if mojom.IsSharedBufferKind(kind): | 201 if mojom.IsSharedBufferKind(kind): |
200 return "mojo::ScopedSharedBufferHandle" | 202 return "mojo::ScopedSharedBufferHandle" |
(...skipping 15 matching lines...) Expand all Loading... |
216 GetCppArrayArgWrapperType(kind.value_kind)) | 218 GetCppArrayArgWrapperType(kind.value_kind)) |
217 if mojom.IsInterfaceKind(kind): | 219 if mojom.IsInterfaceKind(kind): |
218 return "%sPtr" % GetNameForKind(kind) | 220 return "%sPtr" % GetNameForKind(kind) |
219 if mojom.IsInterfaceRequestKind(kind): | 221 if mojom.IsInterfaceRequestKind(kind): |
220 return "%sRequest" % GetNameForKind(kind.kind) | 222 return "%sRequest" % GetNameForKind(kind.kind) |
221 if mojom.IsAssociatedInterfaceKind(kind): | 223 if mojom.IsAssociatedInterfaceKind(kind): |
222 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) | 224 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) |
223 if mojom.IsAssociatedInterfaceRequestKind(kind): | 225 if mojom.IsAssociatedInterfaceRequestKind(kind): |
224 return "%sAssociatedRequest" % GetNameForKind(kind.kind) | 226 return "%sAssociatedRequest" % GetNameForKind(kind.kind) |
225 if mojom.IsStringKind(kind): | 227 if mojom.IsStringKind(kind): |
226 return "mojo::String" | 228 return "WTF::String" if _for_blink else "mojo::String" |
227 if mojom.IsGenericHandleKind(kind): | 229 if mojom.IsGenericHandleKind(kind): |
228 return "mojo::ScopedHandle" | 230 return "mojo::ScopedHandle" |
229 if mojom.IsDataPipeConsumerKind(kind): | 231 if mojom.IsDataPipeConsumerKind(kind): |
230 return "mojo::ScopedDataPipeConsumerHandle" | 232 return "mojo::ScopedDataPipeConsumerHandle" |
231 if mojom.IsDataPipeProducerKind(kind): | 233 if mojom.IsDataPipeProducerKind(kind): |
232 return "mojo::ScopedDataPipeProducerHandle" | 234 return "mojo::ScopedDataPipeProducerHandle" |
233 if mojom.IsMessagePipeKind(kind): | 235 if mojom.IsMessagePipeKind(kind): |
234 return "mojo::ScopedMessagePipeHandle" | 236 return "mojo::ScopedMessagePipeHandle" |
235 if mojom.IsSharedBufferKind(kind): | 237 if mojom.IsSharedBufferKind(kind): |
236 return "mojo::ScopedSharedBufferHandle" | 238 return "mojo::ScopedSharedBufferHandle" |
(...skipping 21 matching lines...) Expand all Loading... |
258 GetCppArrayArgWrapperType(kind.value_kind)) | 260 GetCppArrayArgWrapperType(kind.value_kind)) |
259 if mojom.IsInterfaceKind(kind): | 261 if mojom.IsInterfaceKind(kind): |
260 return "%sPtr" % GetNameForKind(kind) | 262 return "%sPtr" % GetNameForKind(kind) |
261 if mojom.IsInterfaceRequestKind(kind): | 263 if mojom.IsInterfaceRequestKind(kind): |
262 return "%sRequest" % GetNameForKind(kind.kind) | 264 return "%sRequest" % GetNameForKind(kind.kind) |
263 if mojom.IsAssociatedInterfaceKind(kind): | 265 if mojom.IsAssociatedInterfaceKind(kind): |
264 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) | 266 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) |
265 if mojom.IsAssociatedInterfaceRequestKind(kind): | 267 if mojom.IsAssociatedInterfaceRequestKind(kind): |
266 return "%sAssociatedRequest" % GetNameForKind(kind.kind) | 268 return "%sAssociatedRequest" % GetNameForKind(kind.kind) |
267 if mojom.IsStringKind(kind): | 269 if mojom.IsStringKind(kind): |
268 return "mojo::String" | 270 return "WTF::String" if _for_blink else "mojo::String" |
269 if mojom.IsGenericHandleKind(kind): | 271 if mojom.IsGenericHandleKind(kind): |
270 return "mojo::ScopedHandle" | 272 return "mojo::ScopedHandle" |
271 if mojom.IsDataPipeConsumerKind(kind): | 273 if mojom.IsDataPipeConsumerKind(kind): |
272 return "mojo::ScopedDataPipeConsumerHandle" | 274 return "mojo::ScopedDataPipeConsumerHandle" |
273 if mojom.IsDataPipeProducerKind(kind): | 275 if mojom.IsDataPipeProducerKind(kind): |
274 return "mojo::ScopedDataPipeProducerHandle" | 276 return "mojo::ScopedDataPipeProducerHandle" |
275 if mojom.IsMessagePipeKind(kind): | 277 if mojom.IsMessagePipeKind(kind): |
276 return "mojo::ScopedMessagePipeHandle" | 278 return "mojo::ScopedMessagePipeHandle" |
277 if mojom.IsSharedBufferKind(kind): | 279 if mojom.IsSharedBufferKind(kind): |
278 return "mojo::ScopedSharedBufferHandle" | 280 return "mojo::ScopedSharedBufferHandle" |
(...skipping 15 matching lines...) Expand all Loading... |
294 return "%sPtr" % GetNameForKind(kind) | 296 return "%sPtr" % GetNameForKind(kind) |
295 if mojom.IsInterfaceRequestKind(kind): | 297 if mojom.IsInterfaceRequestKind(kind): |
296 return "%sRequest" % GetNameForKind(kind.kind) | 298 return "%sRequest" % GetNameForKind(kind.kind) |
297 if mojom.IsAssociatedInterfaceKind(kind): | 299 if mojom.IsAssociatedInterfaceKind(kind): |
298 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) | 300 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) |
299 if mojom.IsAssociatedInterfaceRequestKind(kind): | 301 if mojom.IsAssociatedInterfaceRequestKind(kind): |
300 return "%sAssociatedRequest" % GetNameForKind(kind.kind) | 302 return "%sAssociatedRequest" % GetNameForKind(kind.kind) |
301 if mojom.IsEnumKind(kind): | 303 if mojom.IsEnumKind(kind): |
302 return GetNameForKind(kind) | 304 return GetNameForKind(kind) |
303 if mojom.IsStringKind(kind): | 305 if mojom.IsStringKind(kind): |
304 return "const mojo::String&" | 306 return "const WTF::String&" if _for_blink else "const mojo::String&" |
305 if mojom.IsGenericHandleKind(kind): | 307 if mojom.IsGenericHandleKind(kind): |
306 return "mojo::ScopedHandle" | 308 return "mojo::ScopedHandle" |
307 if mojom.IsDataPipeConsumerKind(kind): | 309 if mojom.IsDataPipeConsumerKind(kind): |
308 return "mojo::ScopedDataPipeConsumerHandle" | 310 return "mojo::ScopedDataPipeConsumerHandle" |
309 if mojom.IsDataPipeProducerKind(kind): | 311 if mojom.IsDataPipeProducerKind(kind): |
310 return "mojo::ScopedDataPipeProducerHandle" | 312 return "mojo::ScopedDataPipeProducerHandle" |
311 if mojom.IsMessagePipeKind(kind): | 313 if mojom.IsMessagePipeKind(kind): |
312 return "mojo::ScopedMessagePipeHandle" | 314 return "mojo::ScopedMessagePipeHandle" |
313 if mojom.IsSharedBufferKind(kind): | 315 if mojom.IsSharedBufferKind(kind): |
314 return "mojo::ScopedSharedBufferHandle" | 316 return "mojo::ScopedSharedBufferHandle" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 "namespace": self.module.namespace, | 520 "namespace": self.module.namespace, |
519 "namespaces_as_array": NamespaceToArray(self.module.namespace), | 521 "namespaces_as_array": NamespaceToArray(self.module.namespace), |
520 "imports": self.module.imports, | 522 "imports": self.module.imports, |
521 "kinds": self.module.kinds, | 523 "kinds": self.module.kinds, |
522 "enums": self.module.enums, | 524 "enums": self.module.enums, |
523 "structs": self.GetStructs(), | 525 "structs": self.GetStructs(), |
524 "unions": self.GetUnions(), | 526 "unions": self.GetUnions(), |
525 "interfaces": self.GetInterfaces(), | 527 "interfaces": self.GetInterfaces(), |
526 "variant": self.variant, | 528 "variant": self.variant, |
527 "extra_headers": self.GetExtraHeaders(), | 529 "extra_headers": self.GetExtraHeaders(), |
| 530 "for_blink": self.for_blink, |
528 } | 531 } |
529 | 532 |
530 @staticmethod | 533 @staticmethod |
531 def GetTemplatePrefix(): | 534 def GetTemplatePrefix(): |
532 return "cpp_templates" | 535 return "cpp_templates" |
533 | 536 |
534 @classmethod | 537 @classmethod |
535 def GetFilters(cls): | 538 def GetFilters(cls): |
536 return cls.cpp_filters | 539 return cls.cpp_filters |
537 | 540 |
538 @UseJinja("module.h.tmpl") | 541 @UseJinja("module.h.tmpl") |
539 def GenerateModuleHeader(self): | 542 def GenerateModuleHeader(self): |
540 return self.GetJinjaExports() | 543 return self.GetJinjaExports() |
541 | 544 |
542 @UseJinja("module-internal.h.tmpl") | 545 @UseJinja("module-internal.h.tmpl") |
543 def GenerateModuleInternalHeader(self): | 546 def GenerateModuleInternalHeader(self): |
544 return self.GetJinjaExports() | 547 return self.GetJinjaExports() |
545 | 548 |
546 @UseJinja("module.cc.tmpl") | 549 @UseJinja("module.cc.tmpl") |
547 def GenerateModuleSource(self): | 550 def GenerateModuleSource(self): |
548 return self.GetJinjaExports() | 551 return self.GetJinjaExports() |
549 | 552 |
550 def GenerateFiles(self, args): | 553 def GenerateFiles(self, args): |
551 global _current_typemap | 554 global _current_typemap |
552 _current_typemap = self.typemap | 555 _current_typemap = self.typemap |
| 556 global _for_blink |
| 557 _for_blink = self.for_blink |
553 suffix = "-%s" % self.variant if self.variant else "" | 558 suffix = "-%s" % self.variant if self.variant else "" |
554 self.Write(self.GenerateModuleHeader(), | 559 self.Write(self.GenerateModuleHeader(), |
555 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 560 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
556 self.Write(self.GenerateModuleInternalHeader(), | 561 self.Write(self.GenerateModuleInternalHeader(), |
557 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) | 562 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) |
558 self.Write(self.GenerateModuleSource(), | 563 self.Write(self.GenerateModuleSource(), |
559 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 564 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |