| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * This file contains code to generate serialization/deserialization logic for | 6 * This file contains code to generate serialization/deserialization logic for |
| 7 * summaries based on an "IDL" description of the summary format (written in | 7 * summaries based on an "IDL" description of the summary format (written in |
| 8 * stylized Dart). | 8 * stylized Dart). |
| 9 * | 9 * |
| 10 * For each class in the "IDL" input, two corresponding classes are generated: | 10 * For each class in the "IDL" input, two corresponding classes are generated: |
| 11 * - A class with the same name which represents deserialized summary data in | 11 * - A class with the same name which represents deserialized summary data in |
| 12 * memory. This class has read-only semantics. | 12 * memory. This class has read-only semantics. |
| 13 * - A "builder" class which can be used to generate serialized summary data. | 13 * - A "builder" class which can be used to generate serialized summary data. |
| 14 * This class has write-only semantics. | 14 * This class has write-only semantics. |
| 15 * | 15 * |
| 16 * Each of the "builder" classess has a single `finish` method which finalizes | 16 * Each of the "builder" classes has a single `finish` method which finalizes |
| 17 * the entity being built and returns it as an [Object]. This object should | 17 * the entity being built and returns it as an [Object]. This object should |
| 18 * only be passed to other builders (or to [BuilderContext.getBuffer]); | 18 * only be passed to other builders (or to [BuilderContext.getBuffer]); |
| 19 * otherwise the client should treat it as opaque, since it exposes | 19 * otherwise the client should treat it as opaque, since it exposes |
| 20 * implementation details of the underlying summary infrastructure. | 20 * implementation details of the underlying summary infrastructure. |
| 21 */ | 21 */ |
| 22 library analyzer.tool.summary.generate; | 22 library analyzer.tool.summary.generate; |
| 23 | 23 |
| 24 import 'dart:convert'; | 24 import 'dart:convert'; |
| 25 import 'dart:io' hide File; | 25 import 'dart:io' hide File; |
| 26 | 26 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 * [extractIdl]). | 82 * [extractIdl]). |
| 83 */ | 83 */ |
| 84 void checkIdl() { | 84 void checkIdl() { |
| 85 _idl.classes.forEach((String name, idlModel.ClassDeclaration cls) { | 85 _idl.classes.forEach((String name, idlModel.ClassDeclaration cls) { |
| 86 cls.fields.forEach((String fieldName, idlModel.FieldType type) { | 86 cls.fields.forEach((String fieldName, idlModel.FieldType type) { |
| 87 if (type.isList) { | 87 if (type.isList) { |
| 88 if (_idl.classes.containsKey(type.typeName)) { | 88 if (_idl.classes.containsKey(type.typeName)) { |
| 89 // List of classes is ok | 89 // List of classes is ok |
| 90 } else if (type.typeName == 'int') { | 90 } else if (type.typeName == 'int') { |
| 91 // List of ints is ok | 91 // List of ints is ok |
| 92 } else if (type.typeName == 'String') { |
| 93 // List of strings is ok |
| 92 } else { | 94 } else { |
| 93 throw new Exception( | 95 throw new Exception( |
| 94 '$name.$fieldName: illegal type (list of ${type.typeName})'); | 96 '$name.$fieldName: illegal type (list of ${type.typeName})'); |
| 95 } | 97 } |
| 96 } | 98 } |
| 97 }); | 99 }); |
| 98 }); | 100 }); |
| 99 } | 101 } |
| 100 | 102 |
| 101 /** | 103 /** |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 throw new Exception('Unexpected class member `$classMember`'); | 190 throw new Exception('Unexpected class member `$classMember`'); |
| 189 } | 191 } |
| 190 } | 192 } |
| 191 } else if (decl is EnumDeclaration) { | 193 } else if (decl is EnumDeclaration) { |
| 192 idlModel.EnumDeclaration enm = new idlModel.EnumDeclaration(); | 194 idlModel.EnumDeclaration enm = new idlModel.EnumDeclaration(); |
| 193 _idl.enums[decl.name.name] = enm; | 195 _idl.enums[decl.name.name] = enm; |
| 194 for (EnumConstantDeclaration constDecl in decl.constants) { | 196 for (EnumConstantDeclaration constDecl in decl.constants) { |
| 195 enm.values.add(constDecl.name.name); | 197 enm.values.add(constDecl.name.name); |
| 196 } | 198 } |
| 197 } else if (decl is TopLevelVariableDeclaration) { | 199 } else if (decl is TopLevelVariableDeclaration) { |
| 198 // Ignore top leve variable declarations; they are present just to make | 200 // Ignore top level variable declarations; they are present just to make |
| 199 // the IDL analyze without warnings. | 201 // the IDL analyze without warnings. |
| 200 } else { | 202 } else { |
| 201 throw new Exception('Unexpected declaration `$decl`'); | 203 throw new Exception('Unexpected declaration `$decl`'); |
| 202 } | 204 } |
| 203 } | 205 } |
| 204 } | 206 } |
| 205 | 207 |
| 206 /** | 208 /** |
| 207 * Execute [callback] with two spaces added to [_indentation]. | 209 * Execute [callback] with two spaces added to [_indentation]. |
| 208 */ | 210 */ |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 indent(() { | 264 indent(() { |
| 263 cls.fields.forEach((String fieldName, idlModel.FieldType type) { | 265 cls.fields.forEach((String fieldName, idlModel.FieldType type) { |
| 264 out('${dartType(type)} _$fieldName;'); | 266 out('${dartType(type)} _$fieldName;'); |
| 265 }); | 267 }); |
| 266 out(); | 268 out(); |
| 267 out('$name.fromJson(Map json)'); | 269 out('$name.fromJson(Map json)'); |
| 268 indent(() { | 270 indent(() { |
| 269 List<String> initializers = <String>[]; | 271 List<String> initializers = <String>[]; |
| 270 cls.fields.forEach((String fieldName, idlModel.FieldType type) { | 272 cls.fields.forEach((String fieldName, idlModel.FieldType type) { |
| 271 String convert = 'json[${quoted(fieldName)}]'; | 273 String convert = 'json[${quoted(fieldName)}]'; |
| 272 if (type.isList && type.typeName == 'int') { | 274 if (type.isList) { |
| 273 // No conversion necessary. | 275 if (type.typeName == 'int' || type.typeName == 'String') { |
| 274 } else if (type.isList) { | 276 // No conversion necessary. |
| 275 convert = | 277 } else { |
| 276 '$convert?.map((x) => new ${type.typeName}.fromJson(x))?.toLis
t()'; | 278 convert = |
| 279 '$convert?.map((x) => new ${type.typeName}.fromJson(x))?.toL
ist()'; |
| 280 } |
| 277 } else if (_idl.classes.containsKey(type.typeName)) { | 281 } else if (_idl.classes.containsKey(type.typeName)) { |
| 278 convert = | 282 convert = |
| 279 '$convert == null ? null : new ${type.typeName}.fromJson($conv
ert)'; | 283 '$convert == null ? null : new ${type.typeName}.fromJson($conv
ert)'; |
| 280 } else if (_idl.enums.containsKey(type.typeName)) { | 284 } else if (_idl.enums.containsKey(type.typeName)) { |
| 281 convert = | 285 convert = |
| 282 '$convert == null ? null : ${type.typeName}.values[$convert]'; | 286 '$convert == null ? null : ${type.typeName}.values[$convert]'; |
| 283 } | 287 } |
| 284 initializers.add('_$fieldName = $convert'); | 288 initializers.add('_$fieldName = $convert'); |
| 285 }); | 289 }); |
| 286 for (int i = 0; i < initializers.length; i++) { | 290 for (int i = 0; i < initializers.length; i++) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 }); | 376 }); |
| 373 } | 377 } |
| 374 | 378 |
| 375 /** | 379 /** |
| 376 * Enclose [s] in quotes, escaping as necessary. | 380 * Enclose [s] in quotes, escaping as necessary. |
| 377 */ | 381 */ |
| 378 String quoted(String s) { | 382 String quoted(String s) { |
| 379 return JSON.encode(s); | 383 return JSON.encode(s); |
| 380 } | 384 } |
| 381 } | 385 } |
| OLD | NEW |