| 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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 String valueName = '_' + field.name; | 394 String valueName = '_' + field.name; |
| 395 String condition = '$valueName != null'; | 395 String condition = '$valueName != null'; |
| 396 String writeCode; | 396 String writeCode; |
| 397 if (fieldType.isList || | 397 if (fieldType.isList || |
| 398 fieldType.typeName == 'String' || | 398 fieldType.typeName == 'String' || |
| 399 _idl.classes.containsKey(fieldType.typeName)) { | 399 _idl.classes.containsKey(fieldType.typeName)) { |
| 400 String offsetName = 'offset_' + field.name; | 400 String offsetName = 'offset_' + field.name; |
| 401 condition = '$offsetName != null'; | 401 condition = '$offsetName != null'; |
| 402 writeCode = 'fbBuilder.addOffset($index, $offsetName);'; | 402 writeCode = 'fbBuilder.addOffset($index, $offsetName);'; |
| 403 } else if (fieldType.typeName == 'bool') { | 403 } else if (fieldType.typeName == 'bool') { |
| 404 // TODO(scheglov) implement booleans merging? | |
| 405 condition = '$valueName == true'; | 404 condition = '$valueName == true'; |
| 406 writeCode = 'fbBuilder.addInt8($index, 1);'; | 405 writeCode = 'fbBuilder.addBool($index, true);'; |
| 407 } else if (fieldType.typeName == 'int') { | 406 } else if (fieldType.typeName == 'int') { |
| 408 condition += ' && $valueName != ${defaultValue(fieldType)}'; | 407 condition += ' && $valueName != ${defaultValue(fieldType)}'; |
| 409 writeCode = 'fbBuilder.addInt32($index, $valueName);'; | 408 writeCode = 'fbBuilder.addInt32($index, $valueName);'; |
| 410 } else if (_idl.enums.containsKey(fieldType.typeName)) { | 409 } else if (_idl.enums.containsKey(fieldType.typeName)) { |
| 411 condition += ' && $valueName != ${defaultValue(fieldType)}'; | 410 condition += ' && $valueName != ${defaultValue(fieldType)}'; |
| 412 writeCode = 'fbBuilder.addInt32($index, $valueName.index);'; | 411 writeCode = 'fbBuilder.addInt32($index, $valueName.index);'; |
| 413 } | 412 } |
| 414 if (writeCode == null) { | 413 if (writeCode == null) { |
| 415 throw new UnimplementedError('Writing type ${fieldType.typeName}'); | 414 throw new UnimplementedError('Writing type ${fieldType.typeName}'); |
| 416 } | 415 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 out('};'); | 491 out('};'); |
| 493 // Write getters. | 492 // Write getters. |
| 494 cls.fields.asMap().forEach((index, field) { | 493 cls.fields.asMap().forEach((index, field) { |
| 495 String fieldName = field.name; | 494 String fieldName = field.name; |
| 496 idlModel.FieldType type = field.type; | 495 idlModel.FieldType type = field.type; |
| 497 String typeName = type.typeName; | 496 String typeName = type.typeName; |
| 498 // Prepare "readLines" or "readCode" + "def" + "readSuffix" | 497 // Prepare "readLines" or "readCode" + "def" + "readSuffix" |
| 499 List<String> readLines; | 498 List<String> readLines; |
| 500 String readCode; | 499 String readCode; |
| 501 String def = defaultValue(type); | 500 String def = defaultValue(type); |
| 502 String readSuffix = ''; | |
| 503 if (type.isList) { | 501 if (type.isList) { |
| 504 if (typeName == 'int') { | 502 if (typeName == 'int') { |
| 505 String itemCode = 'const fb.Int32Reader()'; | 503 String itemCode = 'const fb.Int32Reader()'; |
| 506 readCode = 'const fb.ListReader<int>($itemCode)'; | 504 readCode = 'const fb.ListReader<int>($itemCode)'; |
| 507 } else if (typeName == 'String') { | 505 } else if (typeName == 'String') { |
| 508 String itemCode = 'const fb.StringReader()'; | 506 String itemCode = 'const fb.StringReader()'; |
| 509 readCode = 'const fb.ListReader<String>($itemCode)'; | 507 readCode = 'const fb.ListReader<String>($itemCode)'; |
| 510 } else { | 508 } else { |
| 511 String itemCode = '$typeName>(const _${typeName}Reader()'; | 509 String itemCode = '$typeName>(const _${typeName}Reader()'; |
| 512 readCode = 'const fb.ListReader<$itemCode)'; | 510 readCode = 'const fb.ListReader<$itemCode)'; |
| 513 } | 511 } |
| 514 } else if (typeName == 'bool') { | 512 } else if (typeName == 'bool') { |
| 515 // TODO(scheglov) implement booleans merging? | 513 readCode = 'const fb.BoolReader()'; |
| 516 def = '0'; | |
| 517 readCode = 'const fb.Int8Reader()'; | |
| 518 readSuffix = ' == 1'; | |
| 519 } else if (typeName == 'int') { | 514 } else if (typeName == 'int') { |
| 520 readCode = 'const fb.Int32Reader()'; | 515 readCode = 'const fb.Int32Reader()'; |
| 521 } else if (typeName == 'String') { | 516 } else if (typeName == 'String') { |
| 522 readCode = 'const fb.StringReader()'; | 517 readCode = 'const fb.StringReader()'; |
| 523 } else if (_idl.enums.containsKey(typeName)) { | 518 } else if (_idl.enums.containsKey(typeName)) { |
| 524 readLines = <String>[ | 519 readLines = <String>[ |
| 525 'int index = const fb.Int32Reader().vTableGet(_bp, $index, 0);', | 520 'int index = const fb.Int32Reader().vTableGet(_bp, $index, 0);', |
| 526 'return $typeName.values[index];' | 521 'return $typeName.values[index];' |
| 527 ]; | 522 ]; |
| 528 } else if (_idl.classes.containsKey(typeName)) { | 523 } else if (_idl.classes.containsKey(typeName)) { |
| 529 readCode = 'const _${typeName}Reader()'; | 524 readCode = 'const _${typeName}Reader()'; |
| 530 } | 525 } |
| 531 assert(readCode != null || readLines != null); | 526 assert(readCode != null || readLines != null); |
| 532 // Write the getter implementation. | 527 // Write the getter implementation. |
| 533 out(); | 528 out(); |
| 534 out('@override'); | 529 out('@override'); |
| 535 String returnType = dartType(type); | 530 String returnType = dartType(type); |
| 536 if (readLines != null) { | 531 if (readLines != null) { |
| 537 out('$returnType get $fieldName {'); | 532 out('$returnType get $fieldName {'); |
| 538 indent(() { | 533 indent(() { |
| 539 readLines.forEach(out); | 534 readLines.forEach(out); |
| 540 }); | 535 }); |
| 541 out('}'); | 536 out('}'); |
| 542 } else { | 537 } else { |
| 543 String expr = '$readCode.vTableGet(_bp, $index, $def)$readSuffix'; | 538 String expr = '$readCode.vTableGet(_bp, $index, $def)'; |
| 544 out('$returnType get $fieldName => $expr;'); | 539 out('$returnType get $fieldName => $expr;'); |
| 545 } | 540 } |
| 546 }); | 541 }); |
| 547 }); | 542 }); |
| 548 out('}'); | 543 out('}'); |
| 549 } | 544 } |
| 550 | 545 |
| 551 /** | 546 /** |
| 552 * Return the documentation text of the given [node], or `null` if the [node] | 547 * Return the documentation text of the given [node], or `null` if the [node] |
| 553 * does not have a comment. Each line is `\n` separated. | 548 * does not have a comment. Each line is `\n` separated. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 564 return token.lexeme.split('\n').map((String line) { | 559 return token.lexeme.split('\n').map((String line) { |
| 565 if (line.startsWith(indent)) { | 560 if (line.startsWith(indent)) { |
| 566 line = line.substring(indent.length); | 561 line = line.substring(indent.length); |
| 567 } | 562 } |
| 568 return line; | 563 return line; |
| 569 }).join('\n'); | 564 }).join('\n'); |
| 570 } | 565 } |
| 571 return null; | 566 return null; |
| 572 } | 567 } |
| 573 } | 568 } |
| OLD | NEW |