Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / tools / bindings / generators / mojom_cpp_generator.py
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.INT64:        "int64_t",
28   mojom.UINT64:       "uint64_t",
29   mojom.DOUBLE:       "double",
30 }
31
32
33 def NamespaceToArray(namespace):
34   return namespace.split('.') if namespace else []
35
36 def GetNameForKind(kind, internal = False):
37   parts = []
38   if kind.imported_from:
39     parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
40     if internal:
41       parts.append("internal")
42   if kind.parent_kind:
43     parts.append(kind.parent_kind.name)
44   parts.append(kind.name)
45   return "::".join(parts)
46
47 def GetCppType(kind):
48   if isinstance(kind, mojom.Struct):
49     return "%s_Data*" % GetNameForKind(kind, internal=True)
50   if isinstance(kind, mojom.Array):
51     return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind)
52   if isinstance(kind, mojom.Interface):
53     return "mojo::MessagePipeHandle"
54   if isinstance(kind, mojom.Enum):
55     return "int32_t"
56   if kind.spec == 's':
57     return "mojo::internal::String_Data*"
58   return _kind_to_cpp_type[kind]
59
60 def GetCppArrayArgWrapperType(kind):
61   if isinstance(kind, (mojom.Struct, mojom.Enum)):
62     return GetNameForKind(kind)
63   if isinstance(kind, mojom.Array):
64     return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind)
65   if isinstance(kind, mojom.Interface):
66     raise Exception("Arrays of interfaces not yet supported!")
67   if kind.spec == 's':
68     return "mojo::String"
69   return _kind_to_cpp_type[kind]
70
71 def GetCppResultWrapperType(kind):
72   if isinstance(kind, (mojom.Struct, mojom.Enum)):
73     return GetNameForKind(kind)
74   if isinstance(kind, mojom.Array):
75     return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind)
76   if isinstance(kind, mojom.Interface):
77     return "%sPtr" % kind.name
78   if kind.spec == 's':
79     return "mojo::String"
80   if kind.spec == 'h':
81     return "mojo::ScopedHandle"
82   if kind.spec == 'h:d:c':
83     return "mojo::ScopedDataPipeConsumerHandle"
84   if kind.spec == 'h:d:p':
85     return "mojo::ScopedDataPipeProducerHandle"
86   if kind.spec == 'h:m':
87     return "mojo::ScopedMessagePipeHandle"
88   if kind.spec == 'h:s':
89     return "mojo::ScopedSharedBufferHandle"
90   return _kind_to_cpp_type[kind]
91
92 def GetCppWrapperType(kind):
93   if isinstance(kind, (mojom.Struct, mojom.Enum)):
94     return GetNameForKind(kind)
95   if isinstance(kind, mojom.Array):
96     return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind)
97   if isinstance(kind, mojom.Interface):
98     return "mojo::Passable<mojo::MessagePipeHandle>"
99   if kind.spec == 's':
100     return "mojo::String"
101   if generator.IsHandleKind(kind):
102     return "mojo::Passable<%s>" % _kind_to_cpp_type[kind]
103   return _kind_to_cpp_type[kind]
104
105 def GetCppConstWrapperType(kind):
106   if isinstance(kind, mojom.Struct):
107     return "const %s&" % GetNameForKind(kind)
108   if isinstance(kind, mojom.Array):
109     return "const mojo::Array<%s >&" % GetCppArrayArgWrapperType(kind.kind)
110   if isinstance(kind, mojom.Interface):
111     return "%sPtr" % kind.name
112   if isinstance(kind, mojom.Enum):
113     return GetNameForKind(kind)
114   if kind.spec == 's':
115     return "const mojo::String&"
116   if kind.spec == 'h':
117     return "mojo::ScopedHandle"
118   if kind.spec == 'h:d:c':
119     return "mojo::ScopedDataPipeConsumerHandle"
120   if kind.spec == 'h:d:p':
121     return "mojo::ScopedDataPipeProducerHandle"
122   if kind.spec == 'h:m':
123     return "mojo::ScopedMessagePipeHandle"
124   if kind.spec == 'h:s':
125     return "mojo::ScopedSharedBufferHandle"
126   if not kind in _kind_to_cpp_type:
127     print "missing:", kind.spec
128   return _kind_to_cpp_type[kind]
129
130 def GetCppFieldType(kind):
131   if isinstance(kind, mojom.Struct):
132     return ("mojo::internal::StructPointer<%s_Data>" %
133         GetNameForKind(kind, internal=True))
134   if isinstance(kind, mojom.Array):
135     return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind)
136   if isinstance(kind, mojom.Interface):
137     return "mojo::MessagePipeHandle"
138   if isinstance(kind, mojom.Enum):
139     return GetNameForKind(kind)
140   if kind.spec == 's':
141     return "mojo::internal::StringPointer"
142   return _kind_to_cpp_type[kind]
143
144 def IsStructWithHandles(struct):
145   for pf in struct.packed.packed_fields:
146     if generator.IsHandleKind(pf.field.kind):
147       return True
148   return False
149
150 def TranslateConstants(token, module):
151   if isinstance(token, mojom.Constant):
152     # Enum constants are constructed like:
153     # Namespace::Struct::FIELD_NAME
154     name = []
155     if token.imported_from:
156       name.extend(NamespaceToArray(token.namespace))
157     if token.parent_kind:
158       name.append(token.parent_kind.name)
159     name.append(token.name[1])
160     return "::".join(name)
161   return token
162
163 def ExpressionToText(value, module):
164   if value[0] != "EXPRESSION":
165     raise Exception("Expected EXPRESSION, got" + value)
166   return "".join(generator.ExpressionMapper(value,
167       lambda token: TranslateConstants(token, module)))
168
169 _HEADER_SIZE = 8
170
171 class Generator(generator.Generator):
172
173   cpp_filters = {
174     "cpp_const_wrapper_type": GetCppConstWrapperType,
175     "cpp_field_type": GetCppFieldType,
176     "cpp_type": GetCppType,
177     "cpp_result_type": GetCppResultWrapperType,
178     "cpp_wrapper_type": GetCppWrapperType,
179     "expression_to_text": ExpressionToText,
180     "get_pad": pack.GetPad,
181     "is_enum_kind": generator.IsEnumKind,
182     "is_handle_kind": generator.IsHandleKind,
183     "is_interface_kind": generator.IsInterfaceKind,
184     "is_object_kind": generator.IsObjectKind,
185     "is_string_kind": generator.IsStringKind,
186     "is_array_kind": lambda kind: isinstance(kind, mojom.Array),
187     "is_struct_with_handles": IsStructWithHandles,
188     "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
189     "struct_from_method": generator.GetStructFromMethod,
190     "response_struct_from_method": generator.GetResponseStructFromMethod,
191     "stylize_method": generator.StudlyCapsToCamel,
192     "verify_token_type": generator.VerifyTokenType,
193   }
194
195   def GetJinjaExports(self):
196     return {
197       "module": self.module,
198       "namespace": self.module.namespace,
199       "namespaces_as_array": NamespaceToArray(self.module.namespace),
200       "imports": self.module.imports,
201       "kinds": self.module.kinds,
202       "enums": self.module.enums,
203       "structs": self.GetStructs(),
204       "interfaces": self.module.interfaces,
205     }
206
207   @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters)
208   def GenerateModuleHeader(self):
209     return self.GetJinjaExports()
210
211   @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters)
212   def GenerateModuleInternalHeader(self):
213     return self.GetJinjaExports()
214
215   @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters)
216   def GenerateModuleSource(self):
217     return self.GetJinjaExports()
218
219   def GenerateFiles(self):
220     self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name)
221     self.Write(self.GenerateModuleInternalHeader(),
222         "%s-internal.h" % self.module.name)
223     self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name)