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