| OLD | NEW |
| (Empty) |
| 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 """Code shared by the various language-specific code generators.""" | |
| 6 | |
| 7 import os | |
| 8 import mojom | |
| 9 import mojom_pack | |
| 10 import re | |
| 11 from functools import partial | |
| 12 | |
| 13 def GetStructFromMethod(interface, method): | |
| 14 """Converts a method's parameters into the fields of a struct.""" | |
| 15 params_class = "%s_%s_Params" % (interface.name, method.name) | |
| 16 struct = mojom.Struct(params_class) | |
| 17 for param in method.parameters: | |
| 18 struct.AddField(param.name, param.kind, param.ordinal) | |
| 19 struct.packed = mojom_pack.PackedStruct(struct) | |
| 20 return struct | |
| 21 | |
| 22 def GetResponseStructFromMethod(interface, method): | |
| 23 """Converts a method's response_parameters into the fields of a struct.""" | |
| 24 params_class = "%s_%s_ResponseParams" % (interface.name, method.name) | |
| 25 struct = mojom.Struct(params_class) | |
| 26 for param in method.response_parameters: | |
| 27 struct.AddField(param.name, param.kind, param.ordinal) | |
| 28 struct.packed = mojom_pack.PackedStruct(struct) | |
| 29 return struct | |
| 30 | |
| 31 def GetStructInfo(exported, struct): | |
| 32 struct.packed = mojom_pack.PackedStruct(struct) | |
| 33 struct.bytes = mojom_pack.GetByteLayout(struct.packed) | |
| 34 struct.exported = exported | |
| 35 return struct | |
| 36 | |
| 37 def IsStringKind(kind): | |
| 38 return kind.spec == 's' | |
| 39 | |
| 40 def IsEnumKind(kind): | |
| 41 return isinstance(kind, mojom.Enum) | |
| 42 | |
| 43 def IsObjectKind(kind): | |
| 44 return isinstance(kind, (mojom.Struct, mojom.Array)) or IsStringKind(kind) | |
| 45 | |
| 46 def IsHandleKind(kind): | |
| 47 return kind.spec.startswith('h') or isinstance(kind, mojom.Interface) | |
| 48 | |
| 49 def StudlyCapsToCamel(studly): | |
| 50 return studly[0].lower() + studly[1:] | |
| 51 | |
| 52 def VerifyTokenType(token, expected): | |
| 53 """Used to check that arrays and objects are used correctly as default | |
| 54 values. Arrays are tokens that look like ('ARRAY', element0, element1...). | |
| 55 See mojom_parser.py for their representation. | |
| 56 """ | |
| 57 if not isinstance(token, tuple): | |
| 58 raise Exception("Expected token type '%s'. Invalid token '%s'." % | |
| 59 (expected, token)) | |
| 60 if token[0] != expected: | |
| 61 raise Exception("Expected token type '%s'. Got '%s'." % | |
| 62 (expected, token[0])) | |
| 63 | |
| 64 def ExpressionMapper(expression, mapper): | |
| 65 if isinstance(expression, tuple) and expression[0] == 'EXPRESSION': | |
| 66 result = [] | |
| 67 for each in expression[1]: | |
| 68 result.extend(ExpressionMapper(each, mapper)) | |
| 69 return result | |
| 70 return [mapper(expression)] | |
| 71 | |
| 72 class Generator(object): | |
| 73 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all | |
| 74 # files to stdout. | |
| 75 def __init__(self, module, output_dir=None): | |
| 76 self.module = module | |
| 77 self.output_dir = output_dir | |
| 78 | |
| 79 def GetStructsFromMethods(self): | |
| 80 result = [] | |
| 81 for interface in self.module.interfaces: | |
| 82 for method in interface.methods: | |
| 83 result.append(GetStructFromMethod(interface, method)) | |
| 84 if method.response_parameters != None: | |
| 85 result.append(GetResponseStructFromMethod(interface, method)) | |
| 86 return map(partial(GetStructInfo, False), result) | |
| 87 | |
| 88 def GetStructs(self): | |
| 89 return map(partial(GetStructInfo, True), self.module.structs) | |
| 90 | |
| 91 def Write(self, contents, filename): | |
| 92 if self.output_dir is None: | |
| 93 print contents | |
| 94 return | |
| 95 with open(os.path.join(self.output_dir, filename), "w+") as f: | |
| 96 f.write(contents) | |
| 97 | |
| 98 def GenerateFiles(self): | |
| 99 raise NotImplementedError("Subclasses must override/implement this method") | |
| OLD | NEW |