| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
| 6 | 6 |
| 7 import argparse | 7 import argparse |
| 8 import ast | 8 import ast |
| 9 import os | 9 import os |
| 10 import re | 10 import re |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 if mojom.IsNullableKind(kind): | 158 if mojom.IsNullableKind(kind): |
| 159 flags_to_set.append(ARRAY_NULLABLE) | 159 flags_to_set.append(ARRAY_NULLABLE) |
| 160 if mojom.IsNullableKind(kind.kind): | 160 if mojom.IsNullableKind(kind.kind): |
| 161 flags_to_set.append(ELEMENT_NULLABLE) | 161 flags_to_set.append(ELEMENT_NULLABLE) |
| 162 | 162 |
| 163 if not flags_to_set: | 163 if not flags_to_set: |
| 164 flags_to_set = [NOTHING_NULLABLE] | 164 flags_to_set = [NOTHING_NULLABLE] |
| 165 return " | ".join(flags_to_set) | 165 return " | ".join(flags_to_set) |
| 166 | 166 |
| 167 | 167 |
| 168 @contextfilter | 168 def AppendEncodeDecodeParams(initial_params, context, kind, bit): |
| 169 def DecodeMethod(context, kind, offset, bit): | 169 """ Appends standard parameters shared between encode and decode calls. """ |
| 170 def _DecodeMethodName(kind): | 170 params = list(initial_params) |
| 171 if mojom.IsAnyArrayKind(kind): | |
| 172 return _DecodeMethodName(kind.kind) + 's' | |
| 173 if mojom.IsEnumKind(kind): | |
| 174 return _DecodeMethodName(mojom.INT32) | |
| 175 if mojom.IsInterfaceRequestKind(kind): | |
| 176 return "readInterfaceRequest" | |
| 177 if mojom.IsInterfaceKind(kind): | |
| 178 return "readServiceInterface" | |
| 179 return _spec_to_decode_method[kind.spec] | |
| 180 methodName = _DecodeMethodName(kind) | |
| 181 params = [ str(offset) ] | |
| 182 if (kind == mojom.BOOL): | 171 if (kind == mojom.BOOL): |
| 183 params.append(str(bit)) | 172 params.append(str(bit)) |
| 184 if mojom.IsReferenceKind(kind): | 173 if mojom.IsReferenceKind(kind): |
| 185 if mojom.IsAnyArrayKind(kind): | 174 if mojom.IsAnyArrayKind(kind): |
| 186 params.append(GetArrayNullabilityFlags(kind)) | 175 params.append(GetArrayNullabilityFlags(kind)) |
| 187 else: | 176 else: |
| 188 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) | 177 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) |
| 189 if mojom.IsAnyArrayKind(kind): | 178 if mojom.IsAnyArrayKind(kind): |
| 190 if mojom.IsFixedArrayKind(kind): | 179 if mojom.IsFixedArrayKind(kind): |
| 191 params.append(str(kind.length)) | 180 params.append(str(kind.length)) |
| 192 else: | 181 else: |
| 193 params.append( | 182 params.append( |
| 194 "org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH"); | 183 "org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH"); |
| 195 if mojom.IsInterfaceKind(kind): | 184 if mojom.IsInterfaceKind(kind): |
| 196 params.append('%s.MANAGER' % GetJavaType(context, kind)) | 185 params.append('%s.MANAGER' % GetJavaType(context, kind)) |
| 197 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): | 186 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): |
| 198 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) | 187 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) |
| 188 return params |
| 189 |
| 190 |
| 191 @contextfilter |
| 192 def DecodeMethod(context, kind, offset, bit): |
| 193 def _DecodeMethodName(kind): |
| 194 if mojom.IsAnyArrayKind(kind): |
| 195 return _DecodeMethodName(kind.kind) + 's' |
| 196 if mojom.IsEnumKind(kind): |
| 197 return _DecodeMethodName(mojom.INT32) |
| 198 if mojom.IsInterfaceRequestKind(kind): |
| 199 return "readInterfaceRequest" |
| 200 if mojom.IsInterfaceKind(kind): |
| 201 return "readServiceInterface" |
| 202 return _spec_to_decode_method[kind.spec] |
| 203 methodName = _DecodeMethodName(kind) |
| 204 params = AppendEncodeDecodeParams([ str(offset) ], context, kind, bit) |
| 199 return '%s(%s)' % (methodName, ', '.join(params)) | 205 return '%s(%s)' % (methodName, ', '.join(params)) |
| 200 | 206 |
| 201 @contextfilter | 207 @contextfilter |
| 202 def EncodeMethod(context, kind, variable, offset, bit): | 208 def EncodeMethod(context, kind, variable, offset, bit): |
| 203 params = [ variable, str(offset) ] | 209 params = AppendEncodeDecodeParams( |
| 204 if (kind == mojom.BOOL): | 210 [ variable, str(offset) ], context, kind, bit) |
| 205 params.append(str(bit)) | |
| 206 if mojom.IsAnyArrayKind(kind): | |
| 207 if mojom.IsFixedArrayKind(kind): | |
| 208 params.append(str(kind.length)) | |
| 209 else: | |
| 210 params.append( | |
| 211 "org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH"); | |
| 212 if mojom.IsInterfaceKind(kind): | |
| 213 params.append('%s.MANAGER' % GetJavaType(context, kind)) | |
| 214 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): | |
| 215 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) | |
| 216 return 'encode(%s)' % ', '.join(params) | 211 return 'encode(%s)' % ', '.join(params) |
| 217 | 212 |
| 218 def GetPackage(module): | 213 def GetPackage(module): |
| 219 if 'JavaPackage' in module.attributes: | 214 if 'JavaPackage' in module.attributes: |
| 220 return ParseStringAttribute(module.attributes['JavaPackage']) | 215 return ParseStringAttribute(module.attributes['JavaPackage']) |
| 221 # Default package. | 216 # Default package. |
| 222 return "org.chromium.mojom." + module.namespace | 217 return "org.chromium.mojom." + module.namespace |
| 223 | 218 |
| 224 def GetNameForKind(context, kind): | 219 def GetNameForKind(context, kind): |
| 225 def _GetNameHierachy(kind): | 220 def _GetNameHierachy(kind): |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 def GetJinjaParameters(self): | 456 def GetJinjaParameters(self): |
| 462 return { | 457 return { |
| 463 'lstrip_blocks': True, | 458 'lstrip_blocks': True, |
| 464 'trim_blocks': True, | 459 'trim_blocks': True, |
| 465 } | 460 } |
| 466 | 461 |
| 467 def GetGlobals(self): | 462 def GetGlobals(self): |
| 468 return { | 463 return { |
| 469 'module': self.module, | 464 'module': self.module, |
| 470 } | 465 } |
| OLD | NEW |