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

Side by Side Diff: pkg/compiler/lib/src/serialization/resolved_ast_serialization.dart

Issue 2711183002: Store toString value in enum. (Closed)
Patch Set: Created 3 years, 10 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
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 library dart2js.serialization.resolved_ast; 5 library dart2js.serialization.resolved_ast;
6 6
7 import '../common.dart'; 7 import '../common.dart';
8 import '../common/resolution.dart'; 8 import '../common/resolution.dart';
9 import '../constants/expressions.dart'; 9 import '../constants/expressions.dart';
10 import '../elements/resolution_types.dart'; 10 import '../elements/resolution_types.dart';
(...skipping 28 matching lines...) Expand all
39 node.visitChildren(this); 39 node.visitChildren(this);
40 } 40 }
41 } 41 }
42 42
43 /// The kind of AST node. Used for determining how to deserialize 43 /// The kind of AST node. Used for determining how to deserialize
44 /// [ResolvedAst]s. 44 /// [ResolvedAst]s.
45 enum AstKind { 45 enum AstKind {
46 ENUM_CONSTRUCTOR, 46 ENUM_CONSTRUCTOR,
47 ENUM_CONSTANT, 47 ENUM_CONSTANT,
48 ENUM_INDEX_FIELD, 48 ENUM_INDEX_FIELD,
49 ENUM_NAME_FIELD,
49 ENUM_VALUES_FIELD, 50 ENUM_VALUES_FIELD,
50 ENUM_TO_STRING, 51 ENUM_TO_STRING,
51 FACTORY, 52 FACTORY,
52 FIELD, 53 FIELD,
53 FUNCTION, 54 FUNCTION,
54 } 55 }
55 56
56 /// Serializer for [ResolvedAst]s. 57 /// Serializer for [ResolvedAst]s.
57 class ResolvedAstSerializer extends Visitor { 58 class ResolvedAstSerializer extends Visitor {
58 final SerializerPlugin nativeDataSerializer; 59 final SerializerPlugin nativeDataSerializer;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 } 105 }
105 106
106 /// Serialize [ResolvedAst] that is defined in terms of an AST together with 107 /// Serialize [ResolvedAst] that is defined in terms of an AST together with
107 /// [TreeElements]. 108 /// [TreeElements].
108 void serializeParsed() { 109 void serializeParsed() {
109 objectEncoder.setUri(Key.URI, resolvedAst.sourceUri, resolvedAst.sourceUri); 110 objectEncoder.setUri(Key.URI, resolvedAst.sourceUri, resolvedAst.sourceUri);
110 AstKind kind; 111 AstKind kind;
111 if (element.enclosingClass is EnumClassElement) { 112 if (element.enclosingClass is EnumClassElement) {
112 if (element.name == 'index') { 113 if (element.name == 'index') {
113 kind = AstKind.ENUM_INDEX_FIELD; 114 kind = AstKind.ENUM_INDEX_FIELD;
115 } else if (element.name == '_name') {
116 kind = AstKind.ENUM_NAME_FIELD;
sra1 2017/02/24 01:01:50 How do I best test this? dart2js tests pass.
Johnni Winther 2017/02/24 08:52:45 No need to do more. The serialization tests alread
114 } else if (element.name == 'values') { 117 } else if (element.name == 'values') {
115 kind = AstKind.ENUM_VALUES_FIELD; 118 kind = AstKind.ENUM_VALUES_FIELD;
116 } else if (element.name == 'toString') { 119 } else if (element.name == 'toString') {
117 kind = AstKind.ENUM_TO_STRING; 120 kind = AstKind.ENUM_TO_STRING;
118 } else if (element.isConstructor) { 121 } else if (element.isConstructor) {
119 kind = AstKind.ENUM_CONSTRUCTOR; 122 kind = AstKind.ENUM_CONSTRUCTOR;
120 } else { 123 } else {
121 assert(invariant(element, element.isConst, 124 assert(invariant(element, element.isConst,
122 message: "Unexpected enum member: $element")); 125 message: "Unexpected enum member: $element"));
123 kind = AstKind.ENUM_CONSTANT; 126 kind = AstKind.ENUM_CONSTANT;
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 Node computeNode(AstKind kind) { 413 Node computeNode(AstKind kind) {
411 switch (kind) { 414 switch (kind) {
412 case AstKind.ENUM_INDEX_FIELD: 415 case AstKind.ENUM_INDEX_FIELD:
413 AstBuilder builder = new AstBuilder(element.sourcePosition.begin); 416 AstBuilder builder = new AstBuilder(element.sourcePosition.begin);
414 Identifier identifier = builder.identifier('index'); 417 Identifier identifier = builder.identifier('index');
415 VariableDefinitions node = new VariableDefinitions( 418 VariableDefinitions node = new VariableDefinitions(
416 null, 419 null,
417 builder.modifiers(isFinal: true), 420 builder.modifiers(isFinal: true),
418 new NodeList.singleton(identifier)); 421 new NodeList.singleton(identifier));
419 return node; 422 return node;
423 case AstKind.ENUM_NAME_FIELD:
424 AstBuilder builder = new AstBuilder(element.sourcePosition.begin);
425 Identifier identifier = builder.identifier('_name');
426 VariableDefinitions node = new VariableDefinitions(
427 null,
428 builder.modifiers(isFinal: true),
429 new NodeList.singleton(identifier));
430 return node;
420 case AstKind.ENUM_VALUES_FIELD: 431 case AstKind.ENUM_VALUES_FIELD:
421 EnumClassElement enumClass = element.enclosingClass; 432 EnumClassElement enumClass = element.enclosingClass;
422 AstBuilder builder = new AstBuilder(element.sourcePosition.begin); 433 AstBuilder builder = new AstBuilder(element.sourcePosition.begin);
423 List<Node> valueReferences = <Node>[]; 434 List<Node> valueReferences = <Node>[];
424 for (EnumConstantElement enumConstant in enumClass.enumValues) { 435 for (EnumConstantElement enumConstant in enumClass.enumValues) {
425 AstBuilder valueBuilder = 436 AstBuilder valueBuilder =
426 new AstBuilder(enumConstant.sourcePosition.begin); 437 new AstBuilder(enumConstant.sourcePosition.begin);
427 Identifier name = valueBuilder.identifier(enumConstant.name); 438 Identifier name = valueBuilder.identifier(enumConstant.name);
428 439
429 // Add reference for the `values` field. 440 // Add reference for the `values` field.
(...skipping 28 matching lines...) Expand all
458 .literalString('${enumClass.name}.${name.source}'))); 469 .literalString('${enumClass.name}.${name.source}')));
459 } 470 }
460 471
461 // TODO(johnniwinther): Support return type. Note `String` might be 472 // TODO(johnniwinther): Support return type. Note `String` might be
462 // prefixed or not imported within the current library. 473 // prefixed or not imported within the current library.
463 FunctionExpression toStringNode = builder.functionExpression( 474 FunctionExpression toStringNode = builder.functionExpression(
464 Modifiers.EMPTY, 475 Modifiers.EMPTY,
465 'toString', 476 'toString',
466 null, 477 null,
467 builder.argumentList([]), 478 builder.argumentList([]),
468 builder.returnStatement(builder.indexGet( 479 builder.returnStatement(
469 builder.mapLiteral(mapEntries, isConst: true), 480 builder.reference(builder.identifier('_name'))));
470 builder.reference(builder.identifier('index')))));
471 return toStringNode; 481 return toStringNode;
472 case AstKind.ENUM_CONSTRUCTOR: 482 case AstKind.ENUM_CONSTRUCTOR:
473 AstBuilder builder = new AstBuilder(element.sourcePosition.begin); 483 AstBuilder builder = new AstBuilder(element.sourcePosition.begin);
474 VariableDefinitions indexDefinition = 484 VariableDefinitions indexDefinition =
475 builder.initializingFormal('index'); 485 builder.initializingFormal('index');
486 VariableDefinitions nameDefinition =
487 builder.initializingFormal('_name');
476 FunctionExpression constructorNode = builder.functionExpression( 488 FunctionExpression constructorNode = builder.functionExpression(
477 builder.modifiers(isConst: true), 489 builder.modifiers(isConst: true),
478 element.enclosingClass.name, 490 element.enclosingClass.name,
479 null, 491 null,
480 builder.argumentList([indexDefinition]), 492 builder.argumentList([indexDefinition, nameDefinition]),
481 builder.emptyStatement()); 493 builder.emptyStatement());
482 return constructorNode; 494 return constructorNode;
483 case AstKind.ENUM_CONSTANT: 495 case AstKind.ENUM_CONSTANT:
484 EnumConstantElementZ enumConstant = element; 496 EnumConstantElementZ enumConstant = element;
485 EnumClassElement enumClass = element.enclosingClass; 497 EnumClassElement enumClass = element.enclosingClass;
486 int index = enumConstant.index; 498 int index = enumConstant.index;
487 AstBuilder builder = new AstBuilder(element.sourcePosition.begin); 499 AstBuilder builder = new AstBuilder(element.sourcePosition.begin);
488 Identifier name = builder.identifier(element.name); 500 Identifier name = builder.identifier(element.name);
489 501
502 String enumString = "${enumClass.name}.${element.name}";
490 Expression initializer = builder.newExpression( 503 Expression initializer = builder.newExpression(
491 enumClass.name, builder.argumentList([builder.literalInt(index)]), 504 enumClass.name,
505 builder.argumentList([
506 builder.literalInt(index),
507 builder.literalString(enumString)
508 ]),
492 isConst: true); 509 isConst: true);
493 SendSet definition = builder.createDefinition(name, initializer); 510 SendSet definition = builder.createDefinition(name, initializer);
494 511
495 VariableDefinitions node = new VariableDefinitions( 512 VariableDefinitions node = new VariableDefinitions(
496 null, 513 null,
497 builder.modifiers(isStatic: true, isConst: true), 514 builder.modifiers(isStatic: true, isConst: true),
498 new NodeList.singleton(definition)); 515 new NodeList.singleton(definition));
499 return node; 516 return node;
500 case AstKind.FACTORY: 517 case AstKind.FACTORY:
501 Token beginToken = readBeginToken(); 518 Token beginToken = readBeginToken();
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 } 698 }
682 } 699 }
683 } 700 }
684 element.resolvedAst = 701 element.resolvedAst =
685 new ParsedResolvedAst(element, root, body, elements, uri); 702 new ParsedResolvedAst(element, root, body, elements, uri);
686 } 703 }
687 } 704 }
688 705
689 const Key PARAMETER_NODE = const Key('parameter.node'); 706 const Key PARAMETER_NODE = const Key('parameter.node');
690 const Key PARAMETER_INITIALIZER = const Key('parameter.initializer'); 707 const Key PARAMETER_INITIALIZER = const Key('parameter.initializer');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698