| 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 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 List<String> builderParams = _generateBuilder(cls); | 276 List<String> builderParams = _generateBuilder(cls); |
| 277 out(); | 277 out(); |
| 278 _generateEncodeFunction(cls, builderParams); | 278 _generateEncodeFunction(cls, builderParams); |
| 279 out(); | 279 out(); |
| 280 _generateInterface(cls); | 280 _generateInterface(cls); |
| 281 out(); | 281 out(); |
| 282 _generateReader(cls); | 282 _generateReader(cls); |
| 283 out(); | 283 out(); |
| 284 _generateImpl(cls); | 284 _generateImpl(cls); |
| 285 out(); | 285 out(); |
| 286 _generateMixin(cls); |
| 287 out(); |
| 286 } | 288 } |
| 287 } | 289 } |
| 288 | 290 |
| 289 /** | 291 /** |
| 290 * Enclose [s] in quotes, escaping as necessary. | 292 * Enclose [s] in quotes, escaping as necessary. |
| 291 */ | 293 */ |
| 292 String quoted(String s) { | 294 String quoted(String s) { |
| 293 return JSON.encode(s); | 295 return JSON.encode(s); |
| 294 } | 296 } |
| 295 | 297 |
| 296 List<String> _generateBuilder(idlModel.ClassDeclaration cls) { | 298 List<String> _generateBuilder(idlModel.ClassDeclaration cls) { |
| 297 String builderName = cls.name + 'Builder'; | 299 String name = cls.name; |
| 300 String builderName = name + 'Builder'; |
| 301 String mixinName = '_${name}Mixin'; |
| 298 List<String> builderParams = <String>[]; | 302 List<String> builderParams = <String>[]; |
| 299 out('class $builderName {'); | 303 out('class $builderName extends Object with $mixinName ' |
| 304 'implements $name {'); |
| 300 indent(() { | 305 indent(() { |
| 301 out('bool _finished = false;'); | 306 out('bool _finished = false;'); |
| 302 // Generate fields. | 307 // Generate fields. |
| 303 out(); | 308 out(); |
| 304 for (idlModel.FieldDeclaration field in cls.fields) { | 309 for (idlModel.FieldDeclaration field in cls.fields) { |
| 305 String fieldName = field.name; | 310 String fieldName = field.name; |
| 306 idlModel.FieldType type = field.type; | 311 idlModel.FieldType type = field.type; |
| 307 String typeStr = encodedType(type); | 312 String typeStr = encodedType(type); |
| 308 out('$typeStr _$fieldName;'); | 313 out('$typeStr _$fieldName;'); |
| 309 } | 314 } |
| 310 // Generate constructor. | 315 // Generate constructor. |
| 311 out(); | 316 out(); |
| 312 out('$builderName();'); | 317 out('$builderName();'); |
| 313 // Generate setters. | 318 // Generate getters and setters. |
| 314 for (idlModel.FieldDeclaration field in cls.fields) { | 319 for (idlModel.FieldDeclaration field in cls.fields) { |
| 315 String fieldName = field.name; | 320 String fieldName = field.name; |
| 316 String typeStr = encodedType(field.type); | 321 String typeStr = encodedType(field.type); |
| 322 String def = defaultValue(field.type); |
| 323 String defSuffix = def == null ? '' : ' ?? $def'; |
| 324 out(); |
| 325 out('@override'); |
| 326 out('${dartType(field.type)} get $fieldName => _$fieldName$defSuffix;'); |
| 317 out(); | 327 out(); |
| 318 outDoc(field.documentation); | 328 outDoc(field.documentation); |
| 319 builderParams.add('$typeStr $fieldName'); | 329 builderParams.add('$typeStr $fieldName'); |
| 320 out('void set $fieldName($typeStr _value) {'); | 330 out('void set $fieldName($typeStr _value) {'); |
| 321 indent(() { | 331 indent(() { |
| 322 String stateFieldName = '_' + fieldName; | 332 String stateFieldName = '_' + fieldName; |
| 323 out('assert(!_finished);'); | 333 out('assert(!_finished);'); |
| 324 out('$stateFieldName = _value;'); | 334 out('$stateFieldName = _value;'); |
| 325 }); | 335 }); |
| 326 out('}'); | 336 out('}'); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 out('builder.$fieldName = $fieldName;'); | 449 out('builder.$fieldName = $fieldName;'); |
| 440 } | 450 } |
| 441 out('return builder;'); | 451 out('return builder;'); |
| 442 }); | 452 }); |
| 443 out('}'); | 453 out('}'); |
| 444 } | 454 } |
| 445 | 455 |
| 446 void _generateImpl(idlModel.ClassDeclaration cls) { | 456 void _generateImpl(idlModel.ClassDeclaration cls) { |
| 447 String name = cls.name; | 457 String name = cls.name; |
| 448 String implName = '_${name}Impl'; | 458 String implName = '_${name}Impl'; |
| 449 out('class $implName implements $name {'); | 459 String mixinName = '_${name}Mixin'; |
| 460 out('class $implName extends Object with $mixinName implements $name {'); |
| 450 indent(() { | 461 indent(() { |
| 451 out('final fb.BufferPointer _bp;'); | 462 out('final fb.BufferPointer _bp;'); |
| 452 out(); | 463 out(); |
| 453 out('$implName(this._bp);'); | 464 out('$implName(this._bp);'); |
| 454 out(); | 465 out(); |
| 455 // Write cache fields. | 466 // Write cache fields. |
| 456 for (idlModel.FieldDeclaration field in cls.fields) { | 467 for (idlModel.FieldDeclaration field in cls.fields) { |
| 457 String returnType = dartType(field.type); | 468 String returnType = dartType(field.type); |
| 458 String fieldName = field.name; | 469 String fieldName = field.name; |
| 459 out('$returnType _$fieldName;'); | 470 out('$returnType _$fieldName;'); |
| 460 } | 471 } |
| 461 out(); | |
| 462 // Write toMap(). | |
| 463 out('@override'); | |
| 464 out('Map<String, Object> toMap() => {'); | |
| 465 indent(() { | |
| 466 for (idlModel.FieldDeclaration field in cls.fields) { | |
| 467 String fieldName = field.name; | |
| 468 out('${quoted(fieldName)}: $fieldName,'); | |
| 469 } | |
| 470 }); | |
| 471 out('};'); | |
| 472 // Write getters. | 472 // Write getters. |
| 473 cls.fields.asMap().forEach((index, field) { | 473 cls.fields.asMap().forEach((index, field) { |
| 474 String fieldName = field.name; | 474 String fieldName = field.name; |
| 475 idlModel.FieldType type = field.type; | 475 idlModel.FieldType type = field.type; |
| 476 String typeName = type.typeName; | 476 String typeName = type.typeName; |
| 477 // Prepare "readExpr" or "readCode" + "def" | 477 // Prepare "readExpr" or "readCode" + "def" |
| 478 String readExpr; | 478 String readExpr; |
| 479 String readCode; | 479 String readCode; |
| 480 String def = defaultValue(type); | 480 String def = defaultValue(type); |
| 481 if (type.isList) { | 481 if (type.isList) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 String fieldName = field.name; | 537 String fieldName = field.name; |
| 538 idlModel.FieldType type = field.type; | 538 idlModel.FieldType type = field.type; |
| 539 out(); | 539 out(); |
| 540 outDoc(field.documentation); | 540 outDoc(field.documentation); |
| 541 out('${dartType(type)} get $fieldName;'); | 541 out('${dartType(type)} get $fieldName;'); |
| 542 }); | 542 }); |
| 543 }); | 543 }); |
| 544 out('}'); | 544 out('}'); |
| 545 } | 545 } |
| 546 | 546 |
| 547 void _generateMixin(idlModel.ClassDeclaration cls) { |
| 548 String name = cls.name; |
| 549 String mixinName = '_${name}Mixin'; |
| 550 out('abstract class $mixinName implements $name {'); |
| 551 indent(() { |
| 552 // Write toMap(). |
| 553 out('@override'); |
| 554 out('Map<String, Object> toMap() => {'); |
| 555 indent(() { |
| 556 for (idlModel.FieldDeclaration field in cls.fields) { |
| 557 String fieldName = field.name; |
| 558 out('${quoted(fieldName)}: $fieldName,'); |
| 559 } |
| 560 }); |
| 561 out('};'); |
| 562 }); |
| 563 out('}'); |
| 564 } |
| 565 |
| 547 void _generateReader(idlModel.ClassDeclaration cls) { | 566 void _generateReader(idlModel.ClassDeclaration cls) { |
| 548 String name = cls.name; | 567 String name = cls.name; |
| 549 String readerName = '_${name}Reader'; | 568 String readerName = '_${name}Reader'; |
| 550 String implName = '_${name}Impl'; | 569 String implName = '_${name}Impl'; |
| 551 out('class $readerName extends fb.TableReader<$implName> {'); | 570 out('class $readerName extends fb.TableReader<$implName> {'); |
| 552 indent(() { | 571 indent(() { |
| 553 out('const $readerName();'); | 572 out('const $readerName();'); |
| 554 out(); | 573 out(); |
| 555 out('@override'); | 574 out('@override'); |
| 556 out('$implName createObject(fb.BufferPointer bp) => new $implName(bp);'); | 575 out('$implName createObject(fb.BufferPointer bp) => new $implName(bp);'); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 574 return token.lexeme.split('\n').map((String line) { | 593 return token.lexeme.split('\n').map((String line) { |
| 575 if (line.startsWith(indent)) { | 594 if (line.startsWith(indent)) { |
| 576 line = line.substring(indent.length); | 595 line = line.substring(indent.length); |
| 577 } | 596 } |
| 578 return line; | 597 return line; |
| 579 }).join('\n'); | 598 }).join('\n'); |
| 580 } | 599 } |
| 581 return null; | 600 return null; |
| 582 } | 601 } |
| 583 } | 602 } |
| OLD | NEW |