1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)# found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)"""Generates C++ source files from a mojom.Module."""
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport mojom.generate.generator as generator
80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport mojom.generate.module as mojom
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport mojom.generate.pack as pack
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochfrom mojom.generate.template_expander import UseJinja
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)_kind_to_cpp_type = {
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.BOOL:                  "bool",
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.INT8:                  "int8_t",
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.UINT8:                 "uint8_t",
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.INT16:                 "int16_t",
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.UINT16:                "uint16_t",
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.INT32:                 "int32_t",
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.UINT32:                "uint32_t",
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.FLOAT:                 "float",
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.HANDLE:                "mojo::Handle",
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.DCPIPE:                "mojo::DataPipeConsumerHandle",
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.DPPIPE:                "mojo::DataPipeProducerHandle",
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.MSGPIPE:               "mojo::MessagePipeHandle",
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.SHAREDBUFFER:          "mojo::SharedBufferHandle",
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.NULLABLE_HANDLE:       "mojo::Handle",
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.NULLABLE_DCPIPE:       "mojo::DataPipeConsumerHandle",
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.NULLABLE_DPPIPE:       "mojo::DataPipeProducerHandle",
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.NULLABLE_MSGPIPE:      "mojo::MessagePipeHandle",
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.NULLABLE_SHAREDBUFFER: "mojo::SharedBufferHandle",
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.INT64:                 "int64_t",
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.UINT64:                "uint64_t",
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  mojom.DOUBLE:                "double",
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch_kind_to_cpp_literal_suffix = {
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  mojom.UINT8:        "U",
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  mojom.UINT16:       "U",
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  mojom.UINT32:       "U",
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  mojom.FLOAT:        "f",
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  mojom.UINT64:       "ULL",
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdochdef ConstantValue(constant):
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return ExpressionToText(constant.value, kind=constant.kind)
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
4846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)def DefaultValue(field):
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if field.default:
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if mojom.IsStructKind(field.kind):
5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      assert field.default == "default"
5246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      return "%s::New()" % GetNameForKind(field.kind)
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return ExpressionToText(field.default, kind=field.kind)
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return ""
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochdef NamespaceToArray(namespace):
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return namespace.split('.') if namespace else []
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def GetNameForKind(kind, internal = False):
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  parts = []
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if kind.imported_from:
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if internal:
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    parts.append("internal")
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if kind.parent_kind:
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    parts.append(kind.parent_kind.name)
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  parts.append(kind.name)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return "::".join(parts)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GetCppType(kind):
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStructKind(kind):
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "%s_Data*" % GetNameForKind(kind, internal=True)
736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsAnyArrayKind(kind):
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind)
756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
760de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    return "mojo::MessagePipeHandle"
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsEnumKind(kind):
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "int32_t"
796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::internal::String_Data*"
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return _kind_to_cpp_type[kind]
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)def GetCppPodType(kind):
846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "char*"
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return _kind_to_cpp_type[kind]
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GetCppArrayArgWrapperType(kind):
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsEnumKind(kind):
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return GetNameForKind(kind)
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStructKind(kind):
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "%sPtr" % GetNameForKind(kind)
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsAnyArrayKind(kind):
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind)
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceKind(kind):
960de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    raise Exception("Arrays of interfaces not yet supported!")
976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceRequestKind(kind):
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    raise Exception("Arrays of interface requests not yet supported!")
996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::String"
1016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsHandleKind(kind):
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedHandle"
1036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeConsumerKind(kind):
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedDataPipeConsumerHandle"
1056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeProducerKind(kind):
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedDataPipeProducerHandle"
1076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsMessagePipeKind(kind):
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedMessagePipeHandle"
1096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsSharedBufferKind(kind):
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedSharedBufferHandle"
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return _kind_to_cpp_type[kind]
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def GetCppResultWrapperType(kind):
1146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsEnumKind(kind):
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return GetNameForKind(kind)
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStructKind(kind):
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "%sPtr" % GetNameForKind(kind)
1186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsAnyArrayKind(kind):
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceKind(kind):
121f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return "%sPtr" % GetNameForKind(kind)
1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceRequestKind(kind):
123f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind)
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "mojo::String"
1266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsHandleKind(kind):
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "mojo::ScopedHandle"
1286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeConsumerKind(kind):
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "mojo::ScopedDataPipeConsumerHandle"
1306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeProducerKind(kind):
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "mojo::ScopedDataPipeProducerHandle"
1326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsMessagePipeKind(kind):
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "mojo::ScopedMessagePipeHandle"
1346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsSharedBufferKind(kind):
135e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    return "mojo::ScopedSharedBufferHandle"
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return _kind_to_cpp_type[kind]
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GetCppWrapperType(kind):
1396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsEnumKind(kind):
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return GetNameForKind(kind)
1416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStructKind(kind):
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "%sPtr" % GetNameForKind(kind)
1436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsAnyArrayKind(kind):
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
1456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceKind(kind):
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return "%sPtr" % GetNameForKind(kind)
1476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceRequestKind(kind):
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    raise Exception("InterfaceRequest fields not supported!")
1496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::String"
1516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsHandleKind(kind):
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedHandle"
1536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeConsumerKind(kind):
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedDataPipeConsumerHandle"
1556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeProducerKind(kind):
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedDataPipeProducerHandle"
1576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsMessagePipeKind(kind):
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedMessagePipeHandle"
1596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsSharedBufferKind(kind):
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::ScopedSharedBufferHandle"
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return _kind_to_cpp_type[kind]
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GetCppConstWrapperType(kind):
1646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStructKind(kind):
165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "%sPtr" % GetNameForKind(kind)
1666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsAnyArrayKind(kind):
167cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
1686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceKind(kind):
169f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return "%sPtr" % GetNameForKind(kind)
1706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceRequestKind(kind):
171f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind)
1726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsEnumKind(kind):
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return GetNameForKind(kind)
1746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "const mojo::String&"
1766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsHandleKind(kind):
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::ScopedHandle"
1786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeConsumerKind(kind):
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::ScopedDataPipeConsumerHandle"
1806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsDataPipeProducerKind(kind):
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::ScopedDataPipeProducerHandle"
1826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsMessagePipeKind(kind):
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::ScopedMessagePipeHandle"
1846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsSharedBufferKind(kind):
185e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    return "mojo::ScopedSharedBufferHandle"
186a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if not kind in _kind_to_cpp_type:
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    print "missing:", kind.spec
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return _kind_to_cpp_type[kind]
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GetCppFieldType(kind):
1916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStructKind(kind):
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return ("mojo::internal::StructPointer<%s_Data>" %
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GetNameForKind(kind, internal=True))
1946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsAnyArrayKind(kind):
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind)
1966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
1970de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    return "mojo::MessagePipeHandle"
1986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsEnumKind(kind):
199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return GetNameForKind(kind)
2006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return "mojo::internal::StringPointer"
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return _kind_to_cpp_type[kind]
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def IsStructWithHandles(struct):
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for pf in struct.packed.packed_fields:
2066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if mojom.IsAnyHandleKind(pf.field.kind):
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return True
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return False
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
210116680a4aac90f2aa7413d9095a592090648e557Ben Murdochdef TranslateConstants(token, kind):
2116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if isinstance(token, mojom.NamedValue):
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    # Both variable and enum constants are constructed like:
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    # Namespace::Struct::CONSTANT_NAME
214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    # For enums, CONSTANT_NAME is ENUM_NAME_ENUM_VALUE.
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    name = []
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if token.imported_from:
2170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      name.extend(NamespaceToArray(token.namespace))
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if token.parent_kind:
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      name.append(token.parent_kind.name)
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if isinstance(token, mojom.EnumValue):
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      name.append(
2221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          "%s_%s" % (generator.CamelCaseToAllCaps(token.enum.name), token.name))
223116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    else:
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      name.append(token.name)
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return "::".join(name)
2261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if isinstance(token, mojom.BuiltinValue):
2281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if token.value == "double.INFINITY" or token.value == "float.INFINITY":
2291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return "INFINITY";
2301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if token.value == "double.NEGATIVE_INFINITY" or \
2311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       token.value == "float.NEGATIVE_INFINITY":
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return "-INFINITY";
2331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if token.value == "double.NAN" or token.value == "float.NAN":
2341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return "NAN";
2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (kind is not None and mojom.IsFloatKind(kind)):
2371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return token if token.isdigit() else token + "f";
2381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
239116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return '%s%s' % (token, _kind_to_cpp_literal_suffix.get(kind, ''))
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
241116680a4aac90f2aa7413d9095a592090648e557Ben Murdochdef ExpressionToText(value, kind=None):
242116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return TranslateConstants(value, kind)
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
244cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)def ShouldInlineStruct(struct):
245cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  # TODO(darin): Base this on the size of the wrapper class.
246cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if len(struct.fields) > 4:
247cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return False
248cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for field in struct.fields:
2496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if mojom.IsMoveOnlyKind(field.kind):
250cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return False
251cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return True
252cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)def GetArrayValidateParams(kind):
2546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if not mojom.IsAnyArrayKind(kind) and not mojom.IsStringKind(kind):
2556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return "mojo::internal::NoValidateParams"
2566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if mojom.IsStringKind(kind):
2586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    expected_num_elements = 0
25903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    element_is_nullable = False
2606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    element_validate_params = "mojo::internal::NoValidateParams"
2616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  else:
2626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    expected_num_elements = generator.ExpectedArraySize(kind)
26303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    element_is_nullable = mojom.IsNullableKind(kind.kind)
2646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    element_validate_params = GetArrayValidateParams(kind.kind)
2656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % (
2676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      expected_num_elements,
26803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      'true' if element_is_nullable else 'false',
2696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      element_validate_params)
2706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)_HEADER_SIZE = 8
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass Generator(generator.Generator):
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  cpp_filters = {
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    "constant_value": ConstantValue,
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "cpp_const_wrapper_type": GetCppConstWrapperType,
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "cpp_field_type": GetCppFieldType,
279cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "cpp_pod_type": GetCppPodType,
280a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    "cpp_result_type": GetCppResultWrapperType,
281cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "cpp_type": GetCppType,
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "cpp_wrapper_type": GetCppWrapperType,
28346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    "default_value": DefaultValue,
284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    "expected_array_size": generator.ExpectedArraySize,
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "expression_to_text": ExpressionToText,
2866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "get_array_validate_params": GetArrayValidateParams,
287f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    "get_name_for_kind": GetNameForKind,
2880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    "get_pad": pack.GetPad,
28903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    "has_callbacks": mojom.HasCallbacks,
290cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "should_inline": ShouldInlineStruct,
2916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_any_array_kind": mojom.IsAnyArrayKind,
2926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_enum_kind": mojom.IsEnumKind,
2936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_move_only_kind": mojom.IsMoveOnlyKind,
2946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_any_handle_kind": mojom.IsAnyHandleKind,
2956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_interface_kind": mojom.IsInterfaceKind,
2966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_interface_request_kind": mojom.IsInterfaceRequestKind,
2976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_nullable_kind": mojom.IsNullableKind,
2986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_object_kind": mojom.IsObjectKind,
2996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "is_string_kind": mojom.IsStringKind,
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "is_struct_with_handles": IsStructWithHandles,
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
3020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    "struct_from_method": generator.GetStructFromMethod,
3030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    "response_struct_from_method": generator.GetResponseStructFromMethod,
3040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    "stylize_method": generator.StudlyCapsToCamel,
305116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    "to_all_caps": generator.CamelCaseToAllCaps,
3061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
3071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def GetJinjaExports(self):
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return {
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "module": self.module,
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "namespace": self.module.namespace,
3120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "namespaces_as_array": NamespaceToArray(self.module.namespace),
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "imports": self.module.imports,
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "kinds": self.module.kinds,
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "enums": self.module.enums,
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "structs": self.GetStructs(),
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "interfaces": self.module.interfaces,
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters)
321f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def GenerateModuleHeader(self):
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return self.GetJinjaExports()
323a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
324a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters)
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def GenerateModuleInternalHeader(self):
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return self.GetJinjaExports()
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters)
329f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def GenerateModuleSource(self):
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return self.GetJinjaExports()
331f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
332cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  def GenerateFiles(self, args):
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name)
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    self.Write(self.GenerateModuleInternalHeader(),
335a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        "%s-internal.h" % self.module.name)
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name)
337