| 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: |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 out('enum $name {'); | 266 out('enum $name {'); |
| 267 indent(() { | 267 indent(() { |
| 268 for (String value in enm.values) { | 268 for (String value in enm.values) { |
| 269 out('$value,'); | 269 out('$value,'); |
| 270 } | 270 } |
| 271 }); | 271 }); |
| 272 out('}'); | 272 out('}'); |
| 273 out(); | 273 out(); |
| 274 }); | 274 }); |
| 275 for (var cls in _idl.classes.values) { | 275 for (var cls in _idl.classes.values) { |
| 276 List<String> builderParams = _generateBuilder(cls); | 276 _generateBuilder(cls); |
| 277 out(); | |
| 278 _generateEncodeFunction(cls, builderParams); | |
| 279 out(); | 277 out(); |
| 280 _generateInterface(cls); | 278 _generateInterface(cls); |
| 281 out(); | 279 out(); |
| 282 _generateReader(cls); | 280 _generateReader(cls); |
| 283 out(); | 281 out(); |
| 284 _generateImpl(cls); | 282 _generateImpl(cls); |
| 285 out(); | 283 out(); |
| 286 _generateMixin(cls); | 284 _generateMixin(cls); |
| 287 out(); | 285 out(); |
| 288 } | 286 } |
| 289 } | 287 } |
| 290 | 288 |
| 291 /** | 289 /** |
| 292 * Enclose [s] in quotes, escaping as necessary. | 290 * Enclose [s] in quotes, escaping as necessary. |
| 293 */ | 291 */ |
| 294 String quoted(String s) { | 292 String quoted(String s) { |
| 295 return JSON.encode(s); | 293 return JSON.encode(s); |
| 296 } | 294 } |
| 297 | 295 |
| 298 List<String> _generateBuilder(idlModel.ClassDeclaration cls) { | 296 void _generateBuilder(idlModel.ClassDeclaration cls) { |
| 299 String name = cls.name; | 297 String name = cls.name; |
| 300 String builderName = name + 'Builder'; | 298 String builderName = name + 'Builder'; |
| 301 String mixinName = '_${name}Mixin'; | 299 String mixinName = '_${name}Mixin'; |
| 302 List<String> builderParams = <String>[]; | 300 List<String> constructorParams = <String>[]; |
| 303 out('class $builderName extends Object with $mixinName ' | 301 out('class $builderName extends Object with $mixinName ' |
| 304 'implements $name {'); | 302 'implements $name {'); |
| 305 indent(() { | 303 indent(() { |
| 306 out('bool _finished = false;'); | 304 out('bool _finished = false;'); |
| 307 // Generate fields. | 305 // Generate fields. |
| 308 out(); | 306 out(); |
| 309 for (idlModel.FieldDeclaration field in cls.fields) { | 307 for (idlModel.FieldDeclaration field in cls.fields) { |
| 310 String fieldName = field.name; | 308 String fieldName = field.name; |
| 311 idlModel.FieldType type = field.type; | 309 idlModel.FieldType type = field.type; |
| 312 String typeStr = encodedType(type); | 310 String typeStr = encodedType(type); |
| 313 out('$typeStr _$fieldName;'); | 311 out('$typeStr _$fieldName;'); |
| 314 } | 312 } |
| 315 // Generate constructor. | |
| 316 out(); | |
| 317 out('$builderName();'); | |
| 318 // Generate getters and setters. | 313 // Generate getters and setters. |
| 319 for (idlModel.FieldDeclaration field in cls.fields) { | 314 for (idlModel.FieldDeclaration field in cls.fields) { |
| 320 String fieldName = field.name; | 315 String fieldName = field.name; |
| 321 String typeStr = encodedType(field.type); | 316 String typeStr = encodedType(field.type); |
| 322 String def = defaultValue(field.type); | 317 String def = defaultValue(field.type); |
| 323 String defSuffix = def == null ? '' : ' ?? $def'; | 318 String defSuffix = def == null ? '' : ' ?? $def'; |
| 324 out(); | 319 out(); |
| 325 out('@override'); | 320 out('@override'); |
| 326 out('${dartType(field.type)} get $fieldName => _$fieldName$defSuffix;'); | 321 out('${dartType(field.type)} get $fieldName => _$fieldName$defSuffix;'); |
| 327 out(); | 322 out(); |
| 328 outDoc(field.documentation); | 323 outDoc(field.documentation); |
| 329 builderParams.add('$typeStr $fieldName'); | 324 constructorParams.add('$typeStr $fieldName'); |
| 330 out('void set $fieldName($typeStr _value) {'); | 325 out('void set $fieldName($typeStr _value) {'); |
| 331 indent(() { | 326 indent(() { |
| 332 String stateFieldName = '_' + fieldName; | 327 String stateFieldName = '_' + fieldName; |
| 333 out('assert(!_finished);'); | 328 out('assert(!_finished);'); |
| 334 out('$stateFieldName = _value;'); | 329 out('$stateFieldName = _value;'); |
| 335 }); | 330 }); |
| 336 out('}'); | 331 out('}'); |
| 337 } | 332 } |
| 333 // Generate constructor. |
| 334 out(); |
| 335 out('$builderName({${constructorParams.join(', ')}})'); |
| 336 for (int i = 0; i < cls.fields.length; i++) { |
| 337 idlModel.FieldDeclaration field = cls.fields[i]; |
| 338 String prefix = i == 0 ? ' : ' : ' '; |
| 339 String suffix = i == cls.fields.length - 1 ? ';' : ','; |
| 340 out('${prefix}_${field.name} = ${field.name}$suffix'); |
| 341 } |
| 338 // Generate finish. | 342 // Generate finish. |
| 339 if (cls.isTopLevel) { | 343 if (cls.isTopLevel) { |
| 340 out(); | 344 out(); |
| 341 out('List<int> toBuffer() {'); | 345 out('List<int> toBuffer() {'); |
| 342 indent(() { | 346 indent(() { |
| 343 out('fb.Builder fbBuilder = new fb.Builder();'); | 347 out('fb.Builder fbBuilder = new fb.Builder();'); |
| 344 out('return fbBuilder.finish(finish(fbBuilder));'); | 348 out('return fbBuilder.finish(finish(fbBuilder));'); |
| 345 }); | 349 }); |
| 346 out('}'); | 350 out('}'); |
| 347 } | 351 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 indent(() { | 431 indent(() { |
| 428 out(writeCode); | 432 out(writeCode); |
| 429 }); | 433 }); |
| 430 out('}'); | 434 out('}'); |
| 431 }); | 435 }); |
| 432 out('return fbBuilder.endTable();'); | 436 out('return fbBuilder.endTable();'); |
| 433 }); | 437 }); |
| 434 out('}'); | 438 out('}'); |
| 435 }); | 439 }); |
| 436 out('}'); | 440 out('}'); |
| 437 return builderParams; | |
| 438 } | |
| 439 | |
| 440 void _generateEncodeFunction( | |
| 441 idlModel.ClassDeclaration cls, List<String> builderParams) { | |
| 442 String className = cls.name; | |
| 443 String builderName = className + 'Builder'; | |
| 444 out('$builderName encode$className({${builderParams.join(', ')}}) {'); | |
| 445 indent(() { | |
| 446 out('$builderName builder = new $builderName();'); | |
| 447 for (idlModel.FieldDeclaration field in cls.fields) { | |
| 448 String fieldName = field.name; | |
| 449 out('builder.$fieldName = $fieldName;'); | |
| 450 } | |
| 451 out('return builder;'); | |
| 452 }); | |
| 453 out('}'); | |
| 454 } | 441 } |
| 455 | 442 |
| 456 void _generateImpl(idlModel.ClassDeclaration cls) { | 443 void _generateImpl(idlModel.ClassDeclaration cls) { |
| 457 String name = cls.name; | 444 String name = cls.name; |
| 458 String implName = '_${name}Impl'; | 445 String implName = '_${name}Impl'; |
| 459 String mixinName = '_${name}Mixin'; | 446 String mixinName = '_${name}Mixin'; |
| 460 out('class $implName extends Object with $mixinName implements $name {'); | 447 out('class $implName extends Object with $mixinName implements $name {'); |
| 461 indent(() { | 448 indent(() { |
| 462 out('final fb.BufferPointer _bp;'); | 449 out('final fb.BufferPointer _bp;'); |
| 463 out(); | 450 out(); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 return token.lexeme.split('\n').map((String line) { | 580 return token.lexeme.split('\n').map((String line) { |
| 594 if (line.startsWith(indent)) { | 581 if (line.startsWith(indent)) { |
| 595 line = line.substring(indent.length); | 582 line = line.substring(indent.length); |
| 596 } | 583 } |
| 597 return line; | 584 return line; |
| 598 }).join('\n'); | 585 }).join('\n'); |
| 599 } | 586 } |
| 600 return null; | 587 return null; |
| 601 } | 588 } |
| 602 } | 589 } |
| OLD | NEW |