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(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): | |
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): | 170 if (kind == mojom.BOOL): |
183 params.append(str(bit)) | 171 params.append(str(bit)) |
184 if mojom.IsReferenceKind(kind): | 172 if mojom.IsReferenceKind(kind): |
185 if mojom.IsAnyArrayKind(kind): | 173 if mojom.IsAnyArrayKind(kind): |
186 params.append(GetArrayNullabilityFlags(kind)) | 174 params.append(GetArrayNullabilityFlags(kind)) |
187 else: | 175 else: |
188 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) | 176 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) |
189 if mojom.IsAnyArrayKind(kind): | 177 if mojom.IsAnyArrayKind(kind): |
190 if mojom.IsFixedArrayKind(kind): | 178 if mojom.IsFixedArrayKind(kind): |
191 params.append(str(kind.length)) | 179 params.append(str(kind.length)) |
192 else: | 180 else: |
193 params.append( | 181 params.append( |
194 "org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH"); | 182 "org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH"); |
195 if mojom.IsInterfaceKind(kind): | 183 if mojom.IsInterfaceKind(kind): |
196 params.append('%s.MANAGER' % GetJavaType(context, kind)) | 184 params.append('%s.MANAGER' % GetJavaType(context, kind)) |
197 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): | 185 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): |
198 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) | 186 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) |
187 return params | |
qsr
2014/09/03 07:56:28
This is modifying its argument, you might not want
ppi
2014/09/03 08:53:46
Done.
| |
188 | |
189 | |
190 @contextfilter | |
191 def DecodeMethod(context, kind, offset, bit): | |
192 def _DecodeMethodName(kind): | |
193 if mojom.IsAnyArrayKind(kind): | |
194 return _DecodeMethodName(kind.kind) + 's' | |
195 if mojom.IsEnumKind(kind): | |
196 return _DecodeMethodName(mojom.INT32) | |
197 if mojom.IsInterfaceRequestKind(kind): | |
198 return "readInterfaceRequest" | |
199 if mojom.IsInterfaceKind(kind): | |
200 return "readServiceInterface" | |
201 return _spec_to_decode_method[kind.spec] | |
202 methodName = _DecodeMethodName(kind) | |
203 params = AppendEncodeDecodeParams([ str(offset) ], context, kind, bit) | |
199 return '%s(%s)' % (methodName, ', '.join(params)) | 204 return '%s(%s)' % (methodName, ', '.join(params)) |
200 | 205 |
201 @contextfilter | 206 @contextfilter |
202 def EncodeMethod(context, kind, variable, offset, bit): | 207 def EncodeMethod(context, kind, variable, offset, bit): |
203 params = [ variable, str(offset) ] | 208 params = AppendEncodeDecodeParams( |
204 if (kind == mojom.BOOL): | 209 [ 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) | 210 return 'encode(%s)' % ', '.join(params) |
217 | 211 |
218 def GetPackage(module): | 212 def GetPackage(module): |
219 if 'JavaPackage' in module.attributes: | 213 if 'JavaPackage' in module.attributes: |
220 return ParseStringAttribute(module.attributes['JavaPackage']) | 214 return ParseStringAttribute(module.attributes['JavaPackage']) |
221 # Default package. | 215 # Default package. |
222 return "org.chromium.mojom." + module.namespace | 216 return "org.chromium.mojom." + module.namespace |
223 | 217 |
224 def GetNameForKind(context, kind): | 218 def GetNameForKind(context, kind): |
225 def _GetNameHierachy(kind): | 219 def _GetNameHierachy(kind): |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 def GetJinjaParameters(self): | 455 def GetJinjaParameters(self): |
462 return { | 456 return { |
463 'lstrip_blocks': True, | 457 'lstrip_blocks': True, |
464 'trim_blocks': True, | 458 'trim_blocks': True, |
465 } | 459 } |
466 | 460 |
467 def GetGlobals(self): | 461 def GetGlobals(self): |
468 return { | 462 return { |
469 'module': self.module, | 463 'module': self.module, |
470 } | 464 } |
OLD | NEW |