Chromium Code Reviews| 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 from functools import partial | 7 from functools import partial |
| 8 import os.path | 8 import os.path |
| 9 import re | 9 import re |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 | 22 |
| 23 def UnderToCamel(under): | 23 def UnderToCamel(under): |
| 24 """Converts underscore_separated strings to CamelCase strings.""" | 24 """Converts underscore_separated strings to CamelCase strings.""" |
| 25 return ''.join(word.capitalize() for word in under.split('_')) | 25 return ''.join(word.capitalize() for word in under.split('_')) |
| 26 | 26 |
| 27 def WriteFile(contents, full_path): | 27 def WriteFile(contents, full_path): |
| 28 # Make sure the containing directory exists. | 28 # Make sure the containing directory exists. |
| 29 full_dir = os.path.dirname(full_path) | 29 full_dir = os.path.dirname(full_path) |
| 30 fileutil.EnsureDirectoryExists(full_dir) | 30 fileutil.EnsureDirectoryExists(full_dir) |
| 31 | 31 |
| 32 try: | |
| 33 with open(full_path, "r") as f: | |
| 34 if f.read() == contents: | |
|
yzshen1
2017/02/01 20:25:57
Why do we need this check? It seems to be a fairly
Sam McNally
2017/02/02 22:01:46
Only when changing the bindings generator.
| |
| 35 return | |
| 36 except IOError: | |
| 37 pass | |
| 38 | |
| 32 # Dump the data to disk. | 39 # Dump the data to disk. |
| 33 with open(full_path, "w+") as f: | 40 with open(full_path, "w+") as f: |
| 34 f.write(contents) | 41 f.write(contents) |
| 35 | 42 |
| 43 | |
| 44 def NamespaceToArray(namespace): | |
| 45 return namespace.split(".") if namespace else [] | |
| 46 | |
| 47 | |
| 48 class NameFormatter(object): | |
| 49 """A formatter for the names of kinds or values.""" | |
| 50 | |
| 51 def __init__(self, token, variant): | |
| 52 self._token = token | |
| 53 self._variant = variant | |
| 54 | |
| 55 def Format(self, separator, prefixed=False, | |
|
yzshen1
2017/02/01 20:25:57
Could you please explain why we need to introduce
Sam McNally
2017/02/02 22:01:46
This was a leftover from a previous approach. Remo
| |
| 56 include_variant=False, add_same_module_namespaces=False, **kwargs): | |
| 57 parts = [] | |
| 58 if self._ShouldIncludeNamespace(add_same_module_namespaces): | |
| 59 if prefixed: | |
| 60 parts.append("") | |
| 61 parts.extend(self._GetNamespace()) | |
| 62 if include_variant and self._variant: | |
| 63 parts.append(self._variant) | |
| 64 parts.extend(self._GetName(**kwargs)) | |
| 65 return separator.join(parts) | |
| 66 | |
| 67 def FormatForMojom(self): | |
| 68 return self.Format(".", add_same_module_namespaces=True) | |
| 69 | |
| 70 def _MapKindName(self, token, **_): | |
| 71 return token.name | |
| 72 | |
| 73 def _GetName(self, flatten_nested_kind=False, **kwargs): | |
| 74 name = [] | |
| 75 if flatten_nested_kind and self._token.parent_kind: | |
| 76 if mojom.IsEnumKind(self._token): | |
| 77 name.append("%s_%s" % (self._token.parent_kind.name, | |
| 78 self._MapKindName(self._token, **kwargs))) | |
| 79 return name | |
| 80 elif isinstance(self._token, mojom.EnumValue): | |
| 81 return [self.__class__(self._token.enum, self._variant).Format( | |
| 82 '', flatten_nested_kind=flatten_nested_kind, **kwargs), | |
| 83 self._token.name] | |
| 84 | |
| 85 if self._token.parent_kind: | |
| 86 name.append(self._MapKindName(self._token.parent_kind, **kwargs)) | |
| 87 | |
| 88 # Both variable and enum constants are constructed like: | |
| 89 # Namespace::Struct::CONSTANT_NAME | |
| 90 # For enums, CONSTANT_NAME is EnumName::ENUM_VALUE. | |
| 91 if isinstance(self._token, mojom.EnumValue): | |
| 92 name.extend([self._token.enum.name, self._token.name]) | |
| 93 else: | |
| 94 name.append(self._MapKindName(self._token, **kwargs)) | |
| 95 return name | |
| 96 | |
| 97 def _ShouldIncludeNamespace(self, add_same_module_namespaces): | |
| 98 return add_same_module_namespaces or self._token.imported_from | |
| 99 | |
| 100 def _GetNamespace(self): | |
| 101 if self._token.imported_from: | |
| 102 return NamespaceToArray(self._token.imported_from["namespace"]) | |
| 103 elif hasattr(self._token, "module"): | |
| 104 return NamespaceToArray(self._token.module.namespace) | |
| 105 return [] | |
| 106 | |
| 107 | |
| 108 def GetFullMojomNameForKind(kind, variant=None): | |
| 109 return NameFormatter(kind, variant).FormatForMojom() | |
| 110 | |
| 111 | |
| 36 class Generator(object): | 112 class Generator(object): |
| 37 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all | 113 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all |
| 38 # files to stdout. | 114 # files to stdout. |
| 39 def __init__(self, module, output_dir=None, typemap=None, variant=None, | 115 def __init__(self, module, output_dir=None, typemap=None, variant=None, |
| 40 bytecode_path=None, for_blink=False, use_once_callback=False, | 116 bytecode_path=None, for_blink=False, use_once_callback=False, |
| 41 export_attribute=None, export_header=None, | 117 export_attribute=None, export_header=None, |
| 42 generate_non_variant_code=False): | 118 generate_non_variant_code=False): |
| 43 self.module = module | 119 self.module = module |
| 44 self.output_dir = output_dir | 120 self.output_dir = output_dir |
| 45 self.typemap = typemap or {} | 121 self.typemap = typemap or {} |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 return self._AddStructComputedData(False, struct) | 219 return self._AddStructComputedData(False, struct) |
| 144 | 220 |
| 145 def _GetResponseStructFromMethod(self, method): | 221 def _GetResponseStructFromMethod(self, method): |
| 146 """Converts a method's response_parameters into the fields of a struct.""" | 222 """Converts a method's response_parameters into the fields of a struct.""" |
| 147 params_class = "%s_%s_ResponseParams" % (method.interface.name, method.name) | 223 params_class = "%s_%s_ResponseParams" % (method.interface.name, method.name) |
| 148 struct = mojom.Struct(params_class, module=method.interface.module) | 224 struct = mojom.Struct(params_class, module=method.interface.module) |
| 149 for param in method.response_parameters: | 225 for param in method.response_parameters: |
| 150 struct.AddField(param.name, param.kind, param.ordinal, | 226 struct.AddField(param.name, param.kind, param.ordinal, |
| 151 attributes=param.attributes) | 227 attributes=param.attributes) |
| 152 return self._AddStructComputedData(False, struct) | 228 return self._AddStructComputedData(False, struct) |
| OLD | NEW |