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.
5 """Generates C++ source files from a mojom.Module."""
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
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",
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",
33 def NamespaceToArray(namespace):
34 return namespace.split('.') if namespace else []
36 def GetNameForKind(kind, internal = False):
38 if kind.imported_from:
39 parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
41 parts.append("internal")
43 parts.append(kind.parent_kind.name)
44 parts.append(kind.name)
45 return "::".join(parts)
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):
57 return "mojo::internal::String_Data*"
58 return _kind_to_cpp_type[kind]
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!")
69 return _kind_to_cpp_type[kind]
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
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]
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>"
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]
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)
115 return "const mojo::String&"
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]
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)
141 return "mojo::internal::StringPointer"
142 return _kind_to_cpp_type[kind]
144 def IsStructWithHandles(struct):
145 for pf in struct.packed.packed_fields:
146 if generator.IsHandleKind(pf.field.kind):
150 def TranslateConstants(token, module):
151 if isinstance(token, mojom.Constant):
152 # Enum constants are constructed like:
153 # Namespace::Struct::FIELD_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)
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)))
171 class Generator(generator.Generator):
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,
195 def GetJinjaExports(self):
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,
207 @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters)
208 def GenerateModuleHeader(self):
209 return self.GetJinjaExports()
211 @UseJinja("cpp_templates/module-internal.h.tmpl", filters=cpp_filters)
212 def GenerateModuleInternalHeader(self):
213 return self.GetJinjaExports()
215 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters)
216 def GenerateModuleSource(self):
217 return self.GetJinjaExports()
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)