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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 if isinstance(kind, mojom.Array): | 50 if isinstance(kind, mojom.Array): |
51 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) | 51 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) |
52 if isinstance(kind, mojom.Interface): | 52 if isinstance(kind, mojom.Interface): |
53 return "%sHandle" % kind.name | 53 return "%sHandle" % kind.name |
54 if isinstance(kind, mojom.Enum): | 54 if isinstance(kind, mojom.Enum): |
55 return "int32_t" | 55 return "int32_t" |
56 if kind.spec == 's': | 56 if kind.spec == 's': |
57 return "mojo::internal::String_Data*" | 57 return "mojo::internal::String_Data*" |
58 return _kind_to_cpp_type[kind] | 58 return _kind_to_cpp_type[kind] |
59 | 59 |
| 60 def GetCppPodType(kind): |
| 61 if kind.spec == 's': |
| 62 return "char*" |
| 63 return _kind_to_cpp_type[kind] |
| 64 |
60 def GetCppArrayArgWrapperType(kind): | 65 def GetCppArrayArgWrapperType(kind): |
61 if isinstance(kind, (mojom.Struct, mojom.Enum)): | 66 if isinstance(kind, (mojom.Struct, mojom.Enum)): |
62 return GetNameForKind(kind) | 67 return GetNameForKind(kind) |
63 if isinstance(kind, mojom.Array): | 68 if isinstance(kind, mojom.Array): |
64 return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind) | 69 return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind) |
65 if isinstance(kind, mojom.Interface): | 70 if isinstance(kind, mojom.Interface): |
66 return "%sHandle" % kind.name | 71 return "%sHandle" % kind.name |
67 if kind.spec == 's': | 72 if kind.spec == 's': |
68 return "mojo::String" | 73 return "mojo::String" |
69 return _kind_to_cpp_type[kind] | 74 return _kind_to_cpp_type[kind] |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 return "mojo::internal::StringPointer" | 146 return "mojo::internal::StringPointer" |
142 return _kind_to_cpp_type[kind] | 147 return _kind_to_cpp_type[kind] |
143 | 148 |
144 def IsStructWithHandles(struct): | 149 def IsStructWithHandles(struct): |
145 for pf in struct.packed.packed_fields: | 150 for pf in struct.packed.packed_fields: |
146 if generator.IsHandleKind(pf.field.kind): | 151 if generator.IsHandleKind(pf.field.kind): |
147 return True | 152 return True |
148 return False | 153 return False |
149 | 154 |
150 def TranslateConstants(token, module): | 155 def TranslateConstants(token, module): |
151 if isinstance(token, mojom.Constant): | 156 if isinstance(token, (mojom.NamedValue, mojom.EnumValue)): |
152 # Enum constants are constructed like: | 157 # Both variable and enum constants are constructed like: |
153 # Namespace::Struct::FIELD_NAME | 158 # Namespace::Struct::CONSTANT_NAME |
154 name = [] | 159 name = [] |
155 if token.imported_from: | 160 if token.imported_from: |
156 name.extend(NamespaceToArray(token.namespace)) | 161 name.extend(NamespaceToArray(token.namespace)) |
157 if token.parent_kind: | 162 if token.parent_kind: |
158 name.append(token.parent_kind.name) | 163 name.append(token.parent_kind.name) |
159 name.append(token.name[1]) | 164 name.append(token.name) |
160 return "::".join(name) | 165 return "::".join(name) |
161 return token | 166 return token |
162 | 167 |
163 def ExpressionToText(value, module): | 168 def ExpressionToText(value, module): |
164 if value[0] != "EXPRESSION": | 169 if value[0] != "EXPRESSION": |
165 raise Exception("Expected EXPRESSION, got" + value) | 170 raise Exception("Expected EXPRESSION, got" + value) |
166 return "".join(generator.ExpressionMapper(value, | 171 return "".join(generator.ExpressionMapper(value, |
167 lambda token: TranslateConstants(token, module))) | 172 lambda token: TranslateConstants(token, module))) |
168 | 173 |
169 _HEADER_SIZE = 8 | 174 _HEADER_SIZE = 8 |
170 | 175 |
171 class Generator(generator.Generator): | 176 class Generator(generator.Generator): |
172 | 177 |
173 cpp_filters = { | 178 cpp_filters = { |
174 "cpp_const_wrapper_type": GetCppConstWrapperType, | 179 "cpp_const_wrapper_type": GetCppConstWrapperType, |
175 "cpp_field_type": GetCppFieldType, | 180 "cpp_field_type": GetCppFieldType, |
| 181 "cpp_pod_type": GetCppPodType, |
| 182 "cpp_result_type": GetCppResultWrapperType, |
176 "cpp_type": GetCppType, | 183 "cpp_type": GetCppType, |
177 "cpp_result_type": GetCppResultWrapperType, | |
178 "cpp_wrapper_type": GetCppWrapperType, | 184 "cpp_wrapper_type": GetCppWrapperType, |
179 "expression_to_text": ExpressionToText, | 185 "expression_to_text": ExpressionToText, |
180 "get_pad": pack.GetPad, | 186 "get_pad": pack.GetPad, |
181 "is_enum_kind": generator.IsEnumKind, | 187 "is_enum_kind": generator.IsEnumKind, |
182 "is_handle_kind": generator.IsHandleKind, | 188 "is_handle_kind": generator.IsHandleKind, |
183 "is_object_kind": generator.IsObjectKind, | 189 "is_object_kind": generator.IsObjectKind, |
184 "is_string_kind": generator.IsStringKind, | 190 "is_string_kind": generator.IsStringKind, |
185 "is_array_kind": lambda kind: isinstance(kind, mojom.Array), | 191 "is_array_kind": lambda kind: isinstance(kind, mojom.Array), |
186 "is_struct_with_handles": IsStructWithHandles, | 192 "is_struct_with_handles": IsStructWithHandles, |
187 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 193 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
(...skipping 25 matching lines...) Expand all Loading... |
213 | 219 |
214 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) | 220 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) |
215 def GenerateModuleSource(self): | 221 def GenerateModuleSource(self): |
216 return self.GetJinjaExports() | 222 return self.GetJinjaExports() |
217 | 223 |
218 def GenerateFiles(self): | 224 def GenerateFiles(self): |
219 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) | 225 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) |
220 self.Write(self.GenerateModuleInternalHeader(), | 226 self.Write(self.GenerateModuleInternalHeader(), |
221 "%s-internal.h" % self.module.name) | 227 "%s-internal.h" % self.module.name) |
222 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) | 228 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) |
OLD | NEW |