OLD | NEW |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 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 datetime | 7 import datetime |
8 import mojom | 8 import mojom |
9 import mojom_pack | 9 import mojom_pack |
10 import os | 10 import os |
| 11 import re |
11 import sys | 12 import sys |
12 | 13 |
13 from string import Template | 14 from string import Template |
14 | 15 |
15 # mojom_cpp_generator provides a way to generate c++ code from a mojom.Module. | 16 # mojom_cpp_generator provides a way to generate c++ code from a mojom.Module. |
16 # cpp = mojom_cpp_generator.CPPGenerator(module) | 17 # cpp = mojom_cpp_generator.CPPGenerator(module) |
17 # cpp.GenerateFiles("/tmp/g") | 18 # cpp.GenerateFiles("/tmp/g") |
18 | 19 |
19 class DependentKinds(set): | 20 class DependentKinds(set): |
20 """Set subclass to find the unique set of non POD types.""" | 21 """Set subclass to find the unique set of non POD types.""" |
21 def AddKind(self, kind): | 22 def AddKind(self, kind): |
22 if isinstance(kind, mojom.Struct): | 23 if isinstance(kind, mojom.Struct): |
23 self.add(kind) | 24 self.add(kind) |
24 if isinstance(kind, mojom.Array): | 25 if isinstance(kind, mojom.Array): |
25 self.AddKind(kind.kind) | 26 self.AddKind(kind.kind) |
26 | 27 |
27 | 28 |
28 class Forwards(object): | 29 class Forwards(object): |
29 """Helper class to maintain unique set of forward declarations.""" | 30 """Helper class to maintain unique set of forward declarations.""" |
30 def __init__(self): | 31 def __init__(self): |
31 self.kinds = DependentKinds() | 32 self.kinds = DependentKinds() |
32 | 33 |
33 def Add(self, kind): | 34 def Add(self, kind): |
34 self.kinds.AddKind(kind) | 35 self.kinds.AddKind(kind) |
35 | 36 |
36 def __repr__(self): | 37 def __repr__(self): |
37 return '\n'.join( | 38 return '\n'.join( |
38 sorted(map( | 39 sorted(map(lambda kind: "class %s;" % kind.name, self.kinds))) |
39 lambda kind: "class %s;" % kind.name.capitalize(), self.kinds))) | |
40 | 40 |
41 | 41 |
42 class Lines(object): | 42 class Lines(object): |
43 """Helper class to maintain list of template expanded lines.""" | 43 """Helper class to maintain list of template expanded lines.""" |
44 def __init__(self, template): | 44 def __init__(self, template): |
45 self.template = template | 45 self.template = template |
46 self.lines = [] | 46 self.lines = [] |
47 | 47 |
48 def Add(self, map = {}, **substitutions): | 48 def Add(self, map = {}, **substitutions): |
49 if len(substitutions) > 0: | 49 if len(substitutions) > 0: |
50 map = map.copy() | 50 map = map.copy() |
51 map.update(substitutions) | 51 map.update(substitutions) |
52 | 52 |
53 self.lines.append(self.template.substitute(map)) | 53 self.lines.append(self.template.substitute(map)) |
54 | 54 |
55 def __repr__(self): | 55 def __repr__(self): |
56 return '\n'.join(self.lines) | 56 return '\n'.join(self.lines) |
57 | 57 |
58 | 58 |
59 def GetStructFromMethod(interface, method): | 59 def GetStructFromMethod(interface, method): |
60 """Converts a method's parameters into the fields of a struct.""" | 60 """Converts a method's parameters into the fields of a struct.""" |
61 params_class = \ | 61 params_class = "%s_%s_Params" % (interface.name, method.name) |
62 "%s_%s_Params" % (interface.name.capitalize(), method.name.capitalize()) | |
63 struct = mojom.Struct(params_class) | 62 struct = mojom.Struct(params_class) |
64 for param in method.parameters: | 63 for param in method.parameters: |
65 struct.AddField(param.name, param.kind, param.ordinal) | 64 struct.AddField(param.name, param.kind, param.ordinal) |
66 return struct | 65 return struct |
67 | 66 |
68 def IsPointerKind(kind): | 67 def IsPointerKind(kind): |
69 return isinstance(kind, (mojom.Struct, mojom.Array)) or kind.spec == 's' | 68 return isinstance(kind, (mojom.Struct, mojom.Array)) or kind.spec == 's' |
70 | 69 |
| 70 def CamelToUnderscores(camel): |
| 71 s = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', camel) |
| 72 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s).lower() |
| 73 |
71 class CPPGenerator(object): | 74 class CPPGenerator(object): |
72 | 75 |
73 struct_serialization_compute_template = \ | 76 struct_serialization_compute_template = \ |
74 Template(" +\n mojo::internal::ComputeSizeOf($NAME->$FIELD())") | 77 Template(" +\n mojo::internal::ComputeSizeOf($NAME->$FIELD())") |
75 struct_serialization_clone_template = Template( | 78 struct_serialization_clone_template = Template( |
76 " clone->set_$FIELD(mojo::internal::Clone($NAME->$FIELD(), buf));") | 79 " clone->set_$FIELD(mojo::internal::Clone($NAME->$FIELD(), buf));") |
77 struct_serialization_encode_template = Template( | 80 struct_serialization_encode_template = Template( |
78 " Encode(&$NAME->${FIELD}_, handles);") | 81 " Encode(&$NAME->${FIELD}_, handles);") |
79 struct_serialization_encode_handle_template = Template( | 82 struct_serialization_encode_handle_template = Template( |
80 " EncodeHandle(&$NAME->${FIELD}_, handles);") | 83 " EncodeHandle(&$NAME->${FIELD}_, handles);") |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 filename = filename.replace('.h', '.h-template') | 131 filename = filename.replace('.h', '.h-template') |
129 filename = filename.replace('.cc', '.cc-template') | 132 filename = filename.replace('.cc', '.cc-template') |
130 with open(filename, 'r') as file: | 133 with open(filename, 'r') as file: |
131 template = Template(file.read()) | 134 template = Template(file.read()) |
132 cls.templates[template_name] = template | 135 cls.templates[template_name] = template |
133 return cls.templates[template_name] | 136 return cls.templates[template_name] |
134 | 137 |
135 @classmethod | 138 @classmethod |
136 def GetType(cls, kind): | 139 def GetType(cls, kind): |
137 if isinstance(kind, mojom.Struct): | 140 if isinstance(kind, mojom.Struct): |
138 return "%s*" % kind.name.capitalize() | 141 return "%s*" % kind.name |
139 if isinstance(kind, mojom.Array): | 142 if isinstance(kind, mojom.Array): |
140 return "mojo::Array<%s>*" % cls.GetType(kind.kind) | 143 return "mojo::Array<%s>*" % cls.GetType(kind.kind) |
141 if kind.spec == 's': | 144 if kind.spec == 's': |
142 return "mojo::String*" | 145 return "mojo::String*" |
143 return cls.kind_to_type[kind] | 146 return cls.kind_to_type[kind] |
144 | 147 |
145 @classmethod | 148 @classmethod |
146 def GetConstType(cls, kind): | 149 def GetConstType(cls, kind): |
147 if isinstance(kind, mojom.Struct): | 150 if isinstance(kind, mojom.Struct): |
148 return "const %s*" % kind.name.capitalize() | 151 return "const %s*" % kind.name |
149 if isinstance(kind, mojom.Array): | 152 if isinstance(kind, mojom.Array): |
150 return "const mojo::Array<%s>*" % cls.GetConstType(kind.kind) | 153 return "const mojo::Array<%s>*" % cls.GetConstType(kind.kind) |
151 if kind.spec == 's': | 154 if kind.spec == 's': |
152 return "const mojo::String*" | 155 return "const mojo::String*" |
153 return cls.kind_to_type[kind] | 156 return cls.kind_to_type[kind] |
154 | 157 |
155 @classmethod | 158 @classmethod |
156 def GetGetterLine(cls, field): | 159 def GetGetterLine(cls, field): |
157 subs = {'FIELD': field.name, 'TYPE': cls.GetType(field.kind)} | 160 subs = {'FIELD': field.name, 'TYPE': cls.GetType(field.kind)} |
158 if IsPointerKind(field.kind): | 161 if IsPointerKind(field.kind): |
159 return cls.ptr_getter_template.substitute(subs) | 162 return cls.ptr_getter_template.substitute(subs) |
160 else: | 163 else: |
161 return cls.getter_template.substitute(subs) | 164 return cls.getter_template.substitute(subs) |
162 | 165 |
163 @classmethod | 166 @classmethod |
164 def GetSetterLine(cls, field): | 167 def GetSetterLine(cls, field): |
165 subs = {'FIELD': field.name, 'TYPE': cls.GetType(field.kind)} | 168 subs = {'FIELD': field.name, 'TYPE': cls.GetType(field.kind)} |
166 if IsPointerKind(field.kind): | 169 if IsPointerKind(field.kind): |
167 return cls.ptr_setter_template.substitute(subs) | 170 return cls.ptr_setter_template.substitute(subs) |
168 else: | 171 else: |
169 return cls.setter_template.substitute(subs) | 172 return cls.setter_template.substitute(subs) |
170 | 173 |
171 @classmethod | 174 @classmethod |
172 def GetFieldLine(cls, field): | 175 def GetFieldLine(cls, field): |
173 kind = field.kind | 176 kind = field.kind |
174 if kind.spec == 'b': | 177 if kind.spec == 'b': |
175 return cls.bool_field_template.substitute(FIELD=field.name) | 178 return cls.bool_field_template.substitute(FIELD=field.name) |
176 itype = None | 179 itype = None |
177 if isinstance(kind, mojom.Struct): | 180 if isinstance(kind, mojom.Struct): |
178 itype = "mojo::internal::StructPointer<%s>" % kind.name.capitalize() | 181 itype = "mojo::internal::StructPointer<%s>" % kind.name |
179 elif isinstance(kind, mojom.Array): | 182 elif isinstance(kind, mojom.Array): |
180 itype = "mojo::internal::ArrayPointer<%s>" % cls.GetType(kind.kind) | 183 itype = "mojo::internal::ArrayPointer<%s>" % cls.GetType(kind.kind) |
181 elif kind.spec == 's': | 184 elif kind.spec == 's': |
182 itype = "mojo::internal::StringPointer" | 185 itype = "mojo::internal::StringPointer" |
183 else: | 186 else: |
184 itype = cls.kind_to_type[kind] | 187 itype = cls.kind_to_type[kind] |
185 return cls.field_template.substitute(FIELD=field.name, TYPE=itype) | 188 return cls.field_template.substitute(FIELD=field.name, TYPE=itype) |
186 | 189 |
187 @classmethod | 190 @classmethod |
188 def GetCaseLine(cls, interface, method): | 191 def GetCaseLine(cls, interface, method): |
(...skipping 19 matching lines...) Expand all Loading... |
208 for pf in ps.packed_fields: | 211 for pf in ps.packed_fields: |
209 if pf.field.kind.spec == 'h': | 212 if pf.field.kind.spec == 'h': |
210 fields.append(pf.field) | 213 fields.append(pf.field) |
211 return fields | 214 return fields |
212 | 215 |
213 def GetHeaderGuard(self, name): | 216 def GetHeaderGuard(self, name): |
214 return "MOJO_GENERATED_BINDINGS_%s_%s_H_" % \ | 217 return "MOJO_GENERATED_BINDINGS_%s_%s_H_" % \ |
215 (self.module.name.upper(), name.upper()) | 218 (self.module.name.upper(), name.upper()) |
216 | 219 |
217 def GetHeaderFile(self, *components): | 220 def GetHeaderFile(self, *components): |
| 221 components = map(lambda c: CamelToUnderscores(c), components) |
218 component_string = '_'.join(components) | 222 component_string = '_'.join(components) |
219 return os.path.join( | 223 return os.path.join( |
220 self.header_dir, | 224 self.header_dir, |
221 "%s_%s.h" % (self.module.name.lower(), component_string.lower())) | 225 "%s_%s.h" % (CamelToUnderscores(self.module.name), component_string)) |
222 | 226 |
223 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all files | 227 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all files |
224 # to stdout. | 228 # to stdout. |
225 def __init__(self, module, header_dir, output_dir=None): | 229 def __init__(self, module, header_dir, output_dir=None): |
226 self.module = module | 230 self.module = module |
227 self.header_dir = header_dir | 231 self.header_dir = header_dir |
228 self.output_dir = output_dir | 232 self.output_dir = output_dir |
229 | 233 |
230 def WriteTemplateToFile(self, template_name, name, **substitutions): | 234 def WriteTemplateToFile(self, template_name, name, **substitutions): |
| 235 template_name = CamelToUnderscores(template_name) |
| 236 name = CamelToUnderscores(name) |
231 template = self.GetTemplate(template_name) | 237 template = self.GetTemplate(template_name) |
232 filename = "%s_%s" % (self.module.name.lower(), template_name) | 238 filename = "%s_%s" % (CamelToUnderscores(self.module.name), template_name) |
233 filename = filename.replace("interface", name.lower()) | 239 filename = filename.replace("interface", name) |
234 filename = filename.replace("struct", name.lower()) | 240 filename = filename.replace("struct", name) |
235 substitutions['YEAR'] = datetime.date.today().year | 241 substitutions['YEAR'] = datetime.date.today().year |
236 substitutions['NAMESPACE'] = self.module.namespace | 242 substitutions['NAMESPACE'] = self.module.namespace |
237 if self.output_dir is None: | 243 if self.output_dir is None: |
238 file = sys.stdout | 244 file = sys.stdout |
239 else: | 245 else: |
240 file = open(os.path.join(self.output_dir, filename), "w+") | 246 file = open(os.path.join(self.output_dir, filename), "w+") |
241 try: | 247 try: |
242 file.write(template.substitute(substitutions)) | 248 file.write(template.substitute(substitutions)) |
243 finally: | 249 finally: |
244 if self.output_dir is not None: | 250 if self.output_dir is not None: |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 | 294 |
289 def GetStructSerialization(self, class_name, param_name, ps): | 295 def GetStructSerialization(self, class_name, param_name, ps): |
290 struct = ps.struct | 296 struct = ps.struct |
291 encodes = Lines(self.struct_serialization_encode_template) | 297 encodes = Lines(self.struct_serialization_encode_template) |
292 encode_handles = Lines(self.struct_serialization_encode_handle_template) | 298 encode_handles = Lines(self.struct_serialization_encode_handle_template) |
293 decodes = Lines(self.struct_serialization_decode_template) | 299 decodes = Lines(self.struct_serialization_decode_template) |
294 decode_handles = Lines(self.struct_serialization_decode_handle_template) | 300 decode_handles = Lines(self.struct_serialization_decode_handle_template) |
295 fields = self.GetSerializedFields(ps) | 301 fields = self.GetSerializedFields(ps) |
296 handle_fields = self.GetHandleFields(ps) | 302 handle_fields = self.GetHandleFields(ps) |
297 for field in fields: | 303 for field in fields: |
298 substitutions = {'NAME': param_name, 'FIELD': field.name.lower()} | 304 substitutions = {'NAME': param_name, 'FIELD': field.name} |
299 encodes.Add(substitutions) | 305 encodes.Add(substitutions) |
300 decodes.Add(substitutions) | 306 decodes.Add(substitutions) |
301 for field in handle_fields: | 307 for field in handle_fields: |
302 substitutions = {'NAME': param_name, 'FIELD': field.name.lower()} | 308 substitutions = {'NAME': param_name, 'FIELD': field.name} |
303 encode_handles.Add(substitutions) | 309 encode_handles.Add(substitutions) |
304 decode_handles.Add(substitutions) | 310 decode_handles.Add(substitutions) |
305 return self.GetTemplate("struct_serialization").substitute( | 311 return self.GetTemplate("struct_serialization").substitute( |
306 CLASS = "%s::%s" % (self.module.namespace.lower(), class_name), | 312 CLASS = "%s::%s" % (self.module.namespace, class_name), |
307 NAME = param_name, | 313 NAME = param_name, |
308 ENCODES = encodes, | 314 ENCODES = encodes, |
309 DECODES = decodes, | 315 DECODES = decodes, |
310 ENCODE_HANDLES = encode_handles, | 316 ENCODE_HANDLES = encode_handles, |
311 DECODE_HANDLES = decode_handles) | 317 DECODE_HANDLES = decode_handles) |
312 | 318 |
313 def GenerateStructHeader(self, ps): | 319 def GenerateStructHeader(self, ps): |
314 struct = ps.struct | 320 struct = ps.struct |
315 forwards = Forwards() | 321 forwards = Forwards() |
316 for field in struct.fields: | 322 for field in struct.fields: |
317 forwards.Add(field.kind) | 323 forwards.Add(field.kind) |
318 | 324 |
319 self.WriteTemplateToFile("struct.h", struct.name, | 325 self.WriteTemplateToFile("struct.h", struct.name, |
320 HEADER_GUARD = self.GetHeaderGuard(struct.name), | 326 HEADER_GUARD = self.GetHeaderGuard(struct.name), |
321 CLASS = struct.name.capitalize(), | 327 CLASS = struct.name, |
322 FORWARDS = forwards, | 328 FORWARDS = forwards, |
323 DECLARATION = self.GetStructDeclaration(struct.name.capitalize(), ps)) | 329 DECLARATION = self.GetStructDeclaration(struct.name, ps)) |
324 | 330 |
325 def GenerateStructSource(self, ps): | 331 def GenerateStructSource(self, ps): |
326 struct = ps.struct | 332 struct = ps.struct |
327 header = self.GetHeaderFile(struct.name) | 333 header = self.GetHeaderFile(struct.name) |
328 implementation = self.GetStructImplementation(struct.name.capitalize(), ps) | 334 implementation = self.GetStructImplementation(struct.name, ps) |
329 self.WriteTemplateToFile("struct.cc", struct.name, | 335 self.WriteTemplateToFile("struct.cc", struct.name, |
330 CLASS = struct.name.capitalize(), | 336 CLASS = struct.name, |
331 NUM_FIELDS = len(struct.fields), | 337 NUM_FIELDS = len(struct.fields), |
332 HEADER = header, | 338 HEADER = header, |
333 IMPLEMENTATION = implementation) | 339 IMPLEMENTATION = implementation) |
334 | 340 |
335 def GenerateStructSerializationHeader(self, ps): | 341 def GenerateStructSerializationHeader(self, ps): |
336 struct = ps.struct | 342 struct = ps.struct |
337 self.WriteTemplateToFile("struct_serialization.h", struct.name, | 343 self.WriteTemplateToFile("struct_serialization.h", struct.name, |
338 HEADER_GUARD = self.GetHeaderGuard(struct.name + "_SERIALIZATION"), | 344 HEADER_GUARD = self.GetHeaderGuard(struct.name + "_SERIALIZATION"), |
339 CLASS = struct.name.capitalize(), | 345 CLASS = struct.name, |
| 346 NAME = CamelToUnderscores(struct.name), |
340 FULL_CLASS = "%s::%s" % \ | 347 FULL_CLASS = "%s::%s" % \ |
341 (self.module.namespace, struct.name.capitalize())) | 348 (self.module.namespace, struct.name)) |
342 | 349 |
343 def GenerateStructSerializationSource(self, ps): | 350 def GenerateStructSerializationSource(self, ps): |
344 struct = ps.struct | 351 struct = ps.struct |
345 serialization_header = self.GetHeaderFile(struct.name, "serialization") | 352 serialization_header = self.GetHeaderFile(struct.name, "serialization") |
| 353 param_name = CamelToUnderscores(struct.name) |
346 | 354 |
347 kinds = DependentKinds() | 355 kinds = DependentKinds() |
348 for field in struct.fields: | 356 for field in struct.fields: |
349 kinds.AddKind(field.kind) | 357 kinds.AddKind(field.kind) |
350 headers = \ | 358 headers = \ |
351 map(lambda kind: self.GetHeaderFile(kind.name, "serialization"), kinds) | 359 map(lambda kind: self.GetHeaderFile(kind.name, "serialization"), kinds) |
352 headers.append(self.GetHeaderFile(struct.name)) | 360 headers.append(self.GetHeaderFile(struct.name)) |
353 includes = map(lambda header: "#include \"%s\"" % header, sorted(headers)) | 361 includes = map(lambda header: "#include \"%s\"" % header, sorted(headers)) |
354 | 362 |
355 class_header = self.GetHeaderFile(struct.name) | 363 class_header = self.GetHeaderFile(struct.name) |
356 clones = Lines(self.struct_serialization_clone_template) | 364 clones = Lines(self.struct_serialization_clone_template) |
357 sizes = " return sizeof(*%s)" % struct.name.lower() | 365 sizes = " return sizeof(*%s)" % param_name |
358 fields = self.GetSerializedFields(ps) | 366 fields = self.GetSerializedFields(ps) |
359 for field in fields: | 367 for field in fields: |
360 substitutions = {'NAME': struct.name.lower(), 'FIELD': field.name.lower()} | 368 substitutions = {'NAME': param_name, 'FIELD': field.name} |
361 sizes += \ | 369 sizes += \ |
362 self.struct_serialization_compute_template.substitute(substitutions) | 370 self.struct_serialization_compute_template.substitute(substitutions) |
363 clones.Add(substitutions) | 371 clones.Add(substitutions) |
364 sizes += ";" | 372 sizes += ";" |
365 serialization = \ | 373 serialization = self.GetStructSerialization(struct.name, param_name, ps) |
366 self.GetStructSerialization(struct.name.capitalize(), struct.name, ps) | |
367 self.WriteTemplateToFile("struct_serialization.cc", struct.name, | 374 self.WriteTemplateToFile("struct_serialization.cc", struct.name, |
368 NAME = struct.name.lower(), | 375 NAME = param_name, |
369 CLASS = "%s::%s" % \ | 376 CLASS = "%s::%s" % (self.module.namespace, struct.name), |
370 (self.module.namespace.lower(), struct.name.capitalize()), | |
371 SERIALIZATION_HEADER = serialization_header, | 377 SERIALIZATION_HEADER = serialization_header, |
372 INCLUDES = '\n'.join(includes), | 378 INCLUDES = '\n'.join(includes), |
373 SIZES = sizes, | 379 SIZES = sizes, |
374 CLONES = clones, | 380 CLONES = clones, |
375 SERIALIZATION = serialization) | 381 SERIALIZATION = serialization) |
376 | 382 |
377 def GenerateInterfaceHeader(self, interface): | 383 def GenerateInterfaceHeader(self, interface): |
378 methods = [] | 384 methods = [] |
379 forwards = Forwards() | 385 forwards = Forwards() |
380 for method in interface.methods: | 386 for method in interface.methods: |
381 params = [] | 387 params = [] |
382 for param in method.parameters: | 388 for param in method.parameters: |
383 forwards.Add(param.kind) | 389 forwards.Add(param.kind) |
384 params.append("%s %s" % (self.GetConstType(param.kind), param.name)) | 390 params.append("%s %s" % (self.GetConstType(param.kind), param.name)) |
385 methods.append( | 391 methods.append( |
386 " virtual void %s(%s) = 0;" % (method.name, ", ".join(params))) | 392 " virtual void %s(%s) = 0;" % (method.name, ", ".join(params))) |
387 | 393 |
388 self.WriteTemplateToFile("interface.h", interface.name, | 394 self.WriteTemplateToFile("interface.h", interface.name, |
389 HEADER_GUARD = self.GetHeaderGuard(interface.name), | 395 HEADER_GUARD = self.GetHeaderGuard(interface.name), |
390 CLASS = interface.name.capitalize(), | 396 CLASS = interface.name, |
391 FORWARDS = forwards, | 397 FORWARDS = forwards, |
392 METHODS = '\n'.join(methods)) | 398 METHODS = '\n'.join(methods)) |
393 | 399 |
394 def GenerateInterfaceStubHeader(self, interface): | 400 def GenerateInterfaceStubHeader(self, interface): |
395 header = self.GetHeaderFile(interface.name) | 401 header = self.GetHeaderFile(interface.name) |
396 self.WriteTemplateToFile("interface_stub.h", interface.name, | 402 self.WriteTemplateToFile("interface_stub.h", interface.name, |
397 HEADER_GUARD = self.GetHeaderGuard(interface.name + "_STUB"), | 403 HEADER_GUARD = self.GetHeaderGuard(interface.name + "_STUB"), |
398 CLASS = interface.name.capitalize(), | 404 CLASS = interface.name, |
399 HEADER = header) | 405 HEADER = header) |
400 | 406 |
401 def GenerateInterfaceStubSource(self, interface): | 407 def GenerateInterfaceStubSource(self, interface): |
402 stub_header = self.GetHeaderFile(interface.name, "stub") | 408 stub_header = self.GetHeaderFile(interface.name, "stub") |
403 serialization_header = self.GetHeaderFile(interface.name, "serialization") | 409 serialization_header = self.GetHeaderFile(interface.name, "serialization") |
404 cases = [] | 410 cases = [] |
405 for method in interface.methods: | 411 for method in interface.methods: |
406 cases.append(self.GetCaseLine(interface, method)) | 412 cases.append(self.GetCaseLine(interface, method)) |
407 self.WriteTemplateToFile("interface_stub.cc", interface.name, | 413 self.WriteTemplateToFile("interface_stub.cc", interface.name, |
408 CLASS = interface.name.capitalize(), | 414 CLASS = interface.name, |
409 CASES = '\n'.join(cases), | 415 CASES = '\n'.join(cases), |
410 STUB_HEADER = stub_header, | 416 STUB_HEADER = stub_header, |
411 SERIALIZATION_HEADER = serialization_header) | 417 SERIALIZATION_HEADER = serialization_header) |
412 | 418 |
413 def GenerateInterfaceSerializationHeader(self, interface): | 419 def GenerateInterfaceSerializationHeader(self, interface): |
414 kinds = DependentKinds() | 420 kinds = DependentKinds() |
415 for method in interface.methods: | 421 for method in interface.methods: |
416 for param in method.parameters: | 422 for param in method.parameters: |
417 kinds.AddKind(param.kind) | 423 kinds.AddKind(param.kind) |
418 headers = \ | 424 headers = \ |
419 map(lambda kind: self.GetHeaderFile(kind.name, "serialization"), kinds) | 425 map(lambda kind: self.GetHeaderFile(kind.name, "serialization"), kinds) |
420 headers.append(self.GetHeaderFile(interface.name)) | 426 headers.append(self.GetHeaderFile(interface.name)) |
421 headers.append("mojo/public/bindings/lib/bindings_serialization.h") | 427 headers.append("mojo/public/bindings/lib/bindings_serialization.h") |
422 includes = map(lambda header: "#include \"%s\"" % header, sorted(headers)) | 428 includes = map(lambda header: "#include \"%s\"" % header, sorted(headers)) |
423 | 429 |
424 names = [] | 430 names = [] |
425 name = 1 | 431 name = 1 |
426 param_classes = [] | 432 param_classes = [] |
427 param_templates = [] | 433 param_templates = [] |
428 template_declaration = self.GetTemplate("template_declaration") | 434 template_declaration = self.GetTemplate("template_declaration") |
429 for method in interface.methods: | 435 for method in interface.methods: |
430 names.append(self.name_template.substitute( | 436 names.append(self.name_template.substitute( |
431 INTERFACE = interface.name.capitalize(), | 437 INTERFACE = interface.name, |
432 METHOD = method.name.capitalize(), | 438 METHOD = method.name, |
433 NAME = name)) | 439 NAME = name)) |
434 name += 1 | 440 name += 1 |
435 | 441 |
436 struct = GetStructFromMethod(interface, method) | 442 struct = GetStructFromMethod(interface, method) |
437 ps = mojom_pack.PackedStruct(struct) | 443 ps = mojom_pack.PackedStruct(struct) |
438 param_classes.append(self.GetStructDeclaration(struct.name, ps)) | 444 param_classes.append(self.GetStructDeclaration(struct.name, ps)) |
439 param_templates.append(template_declaration.substitute(CLASS=struct.name)) | 445 param_templates.append(template_declaration.substitute(CLASS=struct.name)) |
440 | 446 |
441 self.WriteTemplateToFile("interface_serialization.h", interface.name, | 447 self.WriteTemplateToFile("interface_serialization.h", interface.name, |
442 HEADER_GUARD = self.GetHeaderGuard(interface.name + "_SERIALIZATION"), | 448 HEADER_GUARD = self.GetHeaderGuard(interface.name + "_SERIALIZATION"), |
443 INCLUDES = '\n'.join(includes), | 449 INCLUDES = '\n'.join(includes), |
444 NAMES = '\n'.join(names), | 450 NAMES = '\n'.join(names), |
445 PARAM_CLASSES = '\n'.join(param_classes), | 451 PARAM_CLASSES = '\n'.join(param_classes), |
446 PARAM_TEMPLATES = '\n'.join(param_templates)) | 452 PARAM_TEMPLATES = '\n'.join(param_templates)) |
447 | 453 |
448 def GenerateInterfaceSerializationSource(self, interface): | 454 def GenerateInterfaceSerializationSource(self, interface): |
449 implementations = [] | 455 implementations = [] |
450 serializations = [] | 456 serializations = [] |
451 for method in interface.methods: | 457 for method in interface.methods: |
452 struct = GetStructFromMethod(interface, method) | 458 struct = GetStructFromMethod(interface, method) |
453 ps = mojom_pack.PackedStruct(struct) | 459 ps = mojom_pack.PackedStruct(struct) |
454 implementations.append(self.GetStructImplementation(struct.name, ps)) | 460 implementations.append(self.GetStructImplementation(struct.name, ps)) |
455 serializations.append( | 461 serializations.append( |
456 self.GetStructSerialization("internal::" + struct.name, "params", ps)) | 462 self.GetStructSerialization("internal::" + struct.name, "params", ps)) |
457 self.WriteTemplateToFile("interface_serialization.cc", interface.name, | 463 self.WriteTemplateToFile("interface_serialization.cc", interface.name, |
458 HEADER = self.GetHeaderFile(interface.name.lower(), "serialization"), | 464 HEADER = self.GetHeaderFile(interface.name, "serialization"), |
459 IMPLEMENTATIONS = '\n'.join(implementations), | 465 IMPLEMENTATIONS = '\n'.join(implementations), |
460 SERIALIZATIONS = '\n'.join(serializations)) | 466 SERIALIZATIONS = '\n'.join(serializations)) |
461 | 467 |
462 def GenerateInterfaceProxyHeader(self, interface): | 468 def GenerateInterfaceProxyHeader(self, interface): |
463 methods = [] | 469 methods = [] |
464 for method in interface.methods: | 470 for method in interface.methods: |
465 params = map( | 471 params = map( |
466 lambda param: "%s %s" % (self.GetConstType(param.kind), param.name), | 472 lambda param: "%s %s" % (self.GetConstType(param.kind), param.name), |
467 method.parameters) | 473 method.parameters) |
468 methods.append( | 474 methods.append( |
469 " virtual void %s(%s) MOJO_OVERRIDE;" \ | 475 " virtual void %s(%s) MOJO_OVERRIDE;" \ |
470 % (method.name, ", ".join(params))) | 476 % (method.name, ", ".join(params))) |
471 | 477 |
472 self.WriteTemplateToFile("interface_proxy.h", interface.name, | 478 self.WriteTemplateToFile("interface_proxy.h", interface.name, |
473 HEADER_GUARD = self.GetHeaderGuard(interface.name + "_PROXY"), | 479 HEADER_GUARD = self.GetHeaderGuard(interface.name + "_PROXY"), |
474 HEADER = self.GetHeaderFile(interface.name), | 480 HEADER = self.GetHeaderFile(interface.name), |
475 CLASS = interface.name.capitalize(), | 481 CLASS = interface.name, |
476 METHODS = '\n'.join(methods)) | 482 METHODS = '\n'.join(methods)) |
477 | 483 |
478 def GenerateInterfaceProxySource(self, interface): | 484 def GenerateInterfaceProxySource(self, interface): |
479 implementations = Lines(self.GetTemplate("proxy_implementation")) | 485 implementations = Lines(self.GetTemplate("proxy_implementation")) |
480 for method in interface.methods: | 486 for method in interface.methods: |
481 sets = [] | 487 sets = [] |
482 computes = Lines(self.param_struct_compute_template) | 488 computes = Lines(self.param_struct_compute_template) |
483 for param in method.parameters: | 489 for param in method.parameters: |
484 if IsPointerKind(param.kind): | 490 if IsPointerKind(param.kind): |
485 sets.append( | 491 sets.append( |
486 self.param_struct_set_template.substitute(NAME=param.name)) | 492 self.param_struct_set_template.substitute(NAME=param.name)) |
487 computes.Add(NAME=param.name) | 493 computes.Add(NAME=param.name) |
488 else: | 494 else: |
489 sets.append(self.param_set_template.substitute(NAME=param.name)) | 495 sets.append(self.param_set_template.substitute(NAME=param.name)) |
490 params_list = map( | 496 params_list = map( |
491 lambda param: "%s %s" % (self.GetConstType(param.kind), param.name), | 497 lambda param: "%s %s" % (self.GetConstType(param.kind), param.name), |
492 method.parameters) | 498 method.parameters) |
493 name = \ | 499 name = "internal::k%s_%s_Name" % (interface.name, method.name) |
494 "internal::k%s_%s_Name" % (interface.name.capitalize(), method.name) | 500 params_name = "internal::%s_%s_Params" % (interface.name, method.name) |
495 params_name = \ | |
496 "internal::%s_%s_Params" % (interface.name.capitalize(), method.name) | |
497 | 501 |
498 implementations.Add( | 502 implementations.Add( |
499 CLASS = interface.name.capitalize(), | 503 CLASS = interface.name, |
500 METHOD = method.name, | 504 METHOD = method.name, |
501 NAME = name, | 505 NAME = name, |
502 PARAMS = params_name, | 506 PARAMS = params_name, |
503 PARAMS_LIST = ', '.join(params_list), | 507 PARAMS_LIST = ', '.join(params_list), |
504 COMPUTES = computes, | 508 COMPUTES = computes, |
505 SETS = '\n'.join(sets)) | 509 SETS = '\n'.join(sets)) |
506 self.WriteTemplateToFile("interface_proxy.cc", interface.name, | 510 self.WriteTemplateToFile("interface_proxy.cc", interface.name, |
507 HEADER = self.GetHeaderFile(interface.name, "proxy"), | 511 HEADER = self.GetHeaderFile(interface.name, "proxy"), |
508 SERIALIZATION_HEADER = \ | 512 SERIALIZATION_HEADER = \ |
509 self.GetHeaderFile(interface.name, "serialization"), | 513 self.GetHeaderFile(interface.name, "serialization"), |
510 CLASS = interface.name.capitalize(), | 514 CLASS = interface.name, |
511 IMPLEMENTATIONS = implementations) | 515 IMPLEMENTATIONS = implementations) |
512 | 516 |
513 def GenerateFiles(self): | 517 def GenerateFiles(self): |
514 for struct in self.module.structs: | 518 for struct in self.module.structs: |
515 ps = mojom_pack.PackedStruct(struct) | 519 ps = mojom_pack.PackedStruct(struct) |
516 self.GenerateStructHeader(ps) | 520 self.GenerateStructHeader(ps) |
517 self.GenerateStructSource(ps) | 521 self.GenerateStructSource(ps) |
518 self.GenerateStructSerializationHeader(ps) | 522 self.GenerateStructSerializationHeader(ps) |
519 self.GenerateStructSerializationSource(ps) | 523 self.GenerateStructSerializationSource(ps) |
520 for interface in self.module.interfaces: | 524 for interface in self.module.interfaces: |
521 self.GenerateInterfaceHeader(interface) | 525 self.GenerateInterfaceHeader(interface) |
522 self.GenerateInterfaceStubHeader(interface) | 526 self.GenerateInterfaceStubHeader(interface) |
523 self.GenerateInterfaceStubSource(interface) | 527 self.GenerateInterfaceStubSource(interface) |
524 self.GenerateInterfaceSerializationHeader(interface) | 528 self.GenerateInterfaceSerializationHeader(interface) |
525 self.GenerateInterfaceSerializationSource(interface) | 529 self.GenerateInterfaceSerializationSource(interface) |
526 self.GenerateInterfaceProxyHeader(interface) | 530 self.GenerateInterfaceProxyHeader(interface) |
527 self.GenerateInterfaceProxySource(interface) | 531 self.GenerateInterfaceProxySource(interface) |
OLD | NEW |