Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(734)

Side by Side Diff: pkg/analyzer/tool/summary/generate.dart

Issue 1589823007: Generate enum readers. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/summary/format.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 out('// BSD-style license that can be found in the LICENSE file.'); 254 out('// BSD-style license that can be found in the LICENSE file.');
255 out('//'); 255 out('//');
256 out('// This file has been automatically generated. Please do not edit it m anually.'); 256 out('// This file has been automatically generated. Please do not edit it m anually.');
257 out('// To regenerate the file, use the script "pkg/analyzer/tool/generate_f iles".'); 257 out('// To regenerate the file, use the script "pkg/analyzer/tool/generate_f iles".');
258 out(); 258 out();
259 out('library analyzer.src.summary.format;'); 259 out('library analyzer.src.summary.format;');
260 out(); 260 out();
261 out("import 'base.dart' as base;"); 261 out("import 'base.dart' as base;");
262 out("import 'flat_buffers.dart' as fb;"); 262 out("import 'flat_buffers.dart' as fb;");
263 out(); 263 out();
264 _idl.enums.forEach((String name, idlModel.EnumDeclaration enm) { 264 for (idlModel.EnumDeclaration enm in _idl.enums.values) {
265 outDoc(enm.documentation); 265 _generateEnum(enm);
266 out('enum $name {');
267 indent(() {
268 for (String value in enm.values) {
269 out('$value,');
270 }
271 });
272 out('}');
273 out(); 266 out();
274 }); 267 _generateEnumReader(enm);
275 for (var cls in _idl.classes.values) { 268 out();
269 }
270 for (idlModel.ClassDeclaration cls in _idl.classes.values) {
276 _generateBuilder(cls); 271 _generateBuilder(cls);
277 out(); 272 out();
278 _generateInterface(cls); 273 _generateInterface(cls);
279 out(); 274 out();
280 _generateReader(cls); 275 _generateReader(cls);
281 out(); 276 out();
282 _generateImpl(cls); 277 _generateImpl(cls);
283 out(); 278 out();
284 _generateMixin(cls); 279 _generateMixin(cls);
285 out(); 280 out();
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 }); 428 });
434 out('}'); 429 out('}');
435 }); 430 });
436 out('return fbBuilder.endTable();'); 431 out('return fbBuilder.endTable();');
437 }); 432 });
438 out('}'); 433 out('}');
439 }); 434 });
440 out('}'); 435 out('}');
441 } 436 }
442 437
438 void _generateEnum(idlModel.EnumDeclaration enm) {
439 String name = enm.name;
440 outDoc(enm.documentation);
441 out('enum $name {');
442 indent(() {
443 for (String value in enm.values) {
444 out('$value,');
445 }
446 });
447 out('}');
448 }
449
450 void _generateEnumReader(idlModel.EnumDeclaration enm) {
451 String name = enm.name;
452 String readerName = '_${name}Reader';
453 out('class $readerName extends fb.Reader<$name> {');
454 indent(() {
455 out('const $readerName() : super();');
456 out();
457 out('@override');
458 out('int get size => 4;');
459 out();
460 out('@override');
461 out('$name read(fb.BufferPointer bp) {');
462 indent(() {
463 out('int index = const fb.Int32Reader().read(bp);');
464 out('return $name.values[index];');
465 });
466 out('}');
467 });
468 out('}');
469 }
470
443 void _generateImpl(idlModel.ClassDeclaration cls) { 471 void _generateImpl(idlModel.ClassDeclaration cls) {
444 String name = cls.name; 472 String name = cls.name;
445 String implName = '_${name}Impl'; 473 String implName = '_${name}Impl';
446 String mixinName = '_${name}Mixin'; 474 String mixinName = '_${name}Mixin';
447 out('class $implName extends Object with $mixinName implements $name {'); 475 out('class $implName extends Object with $mixinName implements $name {');
448 indent(() { 476 indent(() {
449 out('final fb.BufferPointer _bp;'); 477 out('final fb.BufferPointer _bp;');
450 out(); 478 out();
451 out('$implName(this._bp);'); 479 out('$implName(this._bp);');
452 out(); 480 out();
453 // Write cache fields. 481 // Write cache fields.
454 for (idlModel.FieldDeclaration field in cls.fields) { 482 for (idlModel.FieldDeclaration field in cls.fields) {
455 String returnType = dartType(field.type); 483 String returnType = dartType(field.type);
456 String fieldName = field.name; 484 String fieldName = field.name;
457 out('$returnType _$fieldName;'); 485 out('$returnType _$fieldName;');
458 } 486 }
459 // Write getters. 487 // Write getters.
460 cls.fields.asMap().forEach((index, field) { 488 cls.fields.asMap().forEach((index, field) {
461 String fieldName = field.name; 489 String fieldName = field.name;
462 idlModel.FieldType type = field.type; 490 idlModel.FieldType type = field.type;
463 String typeName = type.typeName; 491 String typeName = type.typeName;
464 // Prepare "readExpr" or "readCode" + "def" 492 // Prepare "readCode" + "def"
465 String readExpr;
466 String readCode; 493 String readCode;
467 String def = defaultValue(type); 494 String def = defaultValue(type);
468 if (type.isList) { 495 if (type.isList) {
469 if (typeName == 'int') { 496 if (typeName == 'int') {
470 String itemCode = 'const fb.Int32Reader()'; 497 String itemCode = 'const fb.Int32Reader()';
471 readCode = 'const fb.ListReader<int>($itemCode)'; 498 readCode = 'const fb.ListReader<int>($itemCode)';
472 } else if (typeName == 'String') { 499 } else if (typeName == 'String') {
473 String itemCode = 'const fb.StringReader()'; 500 String itemCode = 'const fb.StringReader()';
474 readCode = 'const fb.ListReader<String>($itemCode)'; 501 readCode = 'const fb.ListReader<String>($itemCode)';
475 } else { 502 } else {
476 String itemCode = '$typeName>(const _${typeName}Reader()'; 503 String itemCode = '$typeName>(const _${typeName}Reader()';
477 readCode = 'const fb.ListReader<$itemCode)'; 504 readCode = 'const fb.ListReader<$itemCode)';
478 } 505 }
479 } else if (typeName == 'bool') { 506 } else if (typeName == 'bool') {
480 readCode = 'const fb.BoolReader()'; 507 readCode = 'const fb.BoolReader()';
481 } else if (typeName == 'int') { 508 } else if (typeName == 'int') {
482 readCode = 'const fb.Int32Reader()'; 509 readCode = 'const fb.Int32Reader()';
483 } else if (typeName == 'String') { 510 } else if (typeName == 'String') {
484 readCode = 'const fb.StringReader()'; 511 readCode = 'const fb.StringReader()';
485 } else if (_idl.enums.containsKey(typeName)) { 512 } else if (_idl.enums.containsKey(typeName)) {
486 readExpr = 513 readCode = 'const _${typeName}Reader()';
487 '$typeName.values[const fb.Int32Reader().vTableGet(_bp, $index, 0) ]';
488 } else if (_idl.classes.containsKey(typeName)) { 514 } else if (_idl.classes.containsKey(typeName)) {
489 readCode = 'const _${typeName}Reader()'; 515 readCode = 'const _${typeName}Reader()';
490 } 516 }
491 if (readExpr == null) { 517 assert(readCode != null);
492 assert(readCode != null);
493 readExpr = '$readCode.vTableGet(_bp, $index, $def)';
494 }
495 // Write the getter implementation. 518 // Write the getter implementation.
496 out(); 519 out();
497 out('@override'); 520 out('@override');
498 String returnType = dartType(type); 521 String returnType = dartType(type);
499 out('$returnType get $fieldName {'); 522 out('$returnType get $fieldName {');
500 indent(() { 523 indent(() {
524 String readExpr = '$readCode.vTableGet(_bp, $index, $def)';
501 out('_$fieldName ??= $readExpr;'); 525 out('_$fieldName ??= $readExpr;');
502 out('return _$fieldName;'); 526 out('return _$fieldName;');
503 }); 527 });
504 out('}'); 528 out('}');
505 }); 529 });
506 }); 530 });
507 out('}'); 531 out('}');
508 } 532 }
509 533
510 void _generateInterface(idlModel.ClassDeclaration cls) { 534 void _generateInterface(idlModel.ClassDeclaration cls) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 return token.lexeme.split('\n').map((String line) { 604 return token.lexeme.split('\n').map((String line) {
581 if (line.startsWith(indent)) { 605 if (line.startsWith(indent)) {
582 line = line.substring(indent.length); 606 line = line.substring(indent.length);
583 } 607 }
584 return line; 608 return line;
585 }).join('\n'); 609 }).join('\n');
586 } 610 }
587 return null; 611 return null;
588 } 612 }
589 } 613 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/format.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698