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

Unified Diff: pkg/compiler/lib/src/resolution/enum_creator.dart

Issue 718703002: Update enum encoding to latest spec. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/language/enum_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/resolution/enum_creator.dart
diff --git a/pkg/compiler/lib/src/resolution/enum_creator.dart b/pkg/compiler/lib/src/resolution/enum_creator.dart
index fcc179682edea72902cb83327c827a339d076224..302a6005a2f1e176242dc8a89464a015ecc4e19c 100644
--- a/pkg/compiler/lib/src/resolution/enum_creator.dart
+++ b/pkg/compiler/lib/src/resolution/enum_creator.dart
@@ -20,13 +20,25 @@ class AstBuilder {
int get charOffset => position.charOffset;
- final Modifiers finalModifiers =
- new Modifiers.withFlags(null, Modifiers.FLAG_FINAL);
- final Modifiers constModifiers =
- new Modifiers.withFlags(null, Modifiers.FLAG_CONST);
- final Modifiers staticConstModifiers =
- new Modifiers.withFlags(null,
- Modifiers.FLAG_STATIC | Modifiers.FLAG_CONST);
+ Modifiers finalModifiers() {
floitsch 2014/11/11 13:22:09 make getter or rename.
Johnni Winther 2014/11/12 10:16:29 Unified these methods to one [modifiers] method.
+ return new Modifiers.withFlags(
+ new NodeList.singleton(identifier('final')),
+ Modifiers.FLAG_FINAL);
+ }
+
+ Modifiers constModifiers() {
+ return new Modifiers.withFlags(
+ new NodeList.singleton(identifier('const')),
+ Modifiers.FLAG_CONST);
+ }
+
+ Modifiers staticConstModifiers() {
+ return new Modifiers.withFlags(
+ new NodeList(null,
+ linkedList([identifier('static'), identifier('const')]),
+ null, ''),
+ Modifiers.FLAG_STATIC | Modifiers.FLAG_CONST);
+ }
Token keywordToken(String text) {
return new KeywordToken(Keyword.keywords[text], position.charOffset);
@@ -135,6 +147,29 @@ class AstBuilder {
new Send(null, identifier(typeName), arguments));
}
+ Send reference(Identifier identifier) {
+ return new Send(null, identifier);
+ }
+
+ Send indexGet(Expression receiver, Expression index) {
+ return new Send(receiver,
+ new Operator(symbolToken(INDEX_INFO)),
+ new NodeList.singleton(index));
+ }
+
+ LiteralMapEntry mapLiteralEntry(Expression key, Expression value) {
+ return new LiteralMapEntry(key, symbolToken(COLON_INFO), value);
+ }
+
+ LiteralMap mapLiteral(List<LiteralMapEntry> entries, {bool isConst: false}) {
+ return new LiteralMap(
+ null, // Type arguments.
+ new NodeList(symbolToken(OPEN_CURLY_BRACKET_INFO),
+ linkedList(entries),
+ symbolToken(CLOSE_CURLY_BRACKET_INFO),
+ ','),
+ isConst ? keywordToken('const') : null);
+ }
}
class EnumCreator {
@@ -153,7 +188,7 @@ class EnumCreator {
EnumFieldElementX addInstanceMember(String name, InterfaceType type) {
Identifier identifier = builder.identifier(name);
- VariableList variableList = new VariableList(builder.finalModifiers);
+ VariableList variableList = new VariableList(builder.finalModifiers());
variableList.type = type;
EnumFieldElementX variable = new EnumFieldElementX(
identifier, enumClass, variableList, identifier);
@@ -162,20 +197,18 @@ class EnumCreator {
}
EnumFieldElementX indexVariable = addInstanceMember('index', intType);
- EnumFieldElementX nameVariable = addInstanceMember('_name', stringType);
VariableDefinitions indexDefinition = builder.initializingFormal('index');
- VariableDefinitions nameDefinition = builder.initializingFormal('_name');
FunctionExpression constructorNode = builder.functionExpression(
- builder.constModifiers,
+ builder.constModifiers(),
enumClass.name,
- builder.argumentList([indexDefinition, nameDefinition]),
+ builder.argumentList([indexDefinition]),
builder.emptyStatement());
EnumConstructorElementX constructor = new EnumConstructorElementX(
enumClass,
- builder.constModifiers,
+ builder.constModifiers(),
constructorNode);
EnumFormalElementX indexFormal = new EnumFormalElementX(
@@ -184,37 +217,37 @@ class EnumCreator {
builder.identifier('index'),
indexVariable);
- EnumFormalElementX nameFormal = new EnumFormalElementX(
- constructor,
- nameDefinition,
- builder.identifier('_name'),
- nameVariable);
-
FunctionSignatureX constructorSignature = new FunctionSignatureX(
- requiredParameters: builder.linkedList([indexFormal, nameFormal]),
- requiredParameterCount: 2,
+ requiredParameters: builder.linkedList([indexFormal]),
+ requiredParameterCount: 1,
type: new FunctionType(constructor, const VoidType(),
- <DartType>[intType, stringType]));
+ <DartType>[intType]));
constructor.functionSignatureCache = constructorSignature;
enumClass.addMember(constructor, compiler);
- VariableList variableList = new VariableList(builder.staticConstModifiers);
+ VariableList variableList =
+ new VariableList(builder.staticConstModifiers());
variableList.type = enumType;
int index = 0;
List<Node> valueReferences = <Node>[];
+ List<LiteralMapEntry> mapEntries = <LiteralMapEntry>[];
for (Link<Node> link = node.names.nodes;
!link.isEmpty;
link = link.tail) {
Identifier name = link.head;
AstBuilder valueBuilder = new AstBuilder(name.token);
- valueReferences.add(new Send(null, name));
+
+ // Add reference for the `values` field.
+ valueReferences.add(valueBuilder.reference(name));
+
+ // Add map entry for `toString` implementation.
+ mapEntries.add(valueBuilder.mapLiteralEntry(
+ valueBuilder.literalInt(index),
+ valueBuilder.literalString('${enumClass.name}.${name.source}')));
Expression initializer = valueBuilder.newExpression(
enumClass.name,
- valueBuilder.argumentList([
- valueBuilder.literalInt(index),
- valueBuilder.literalString('${name.source}')
- ]),
+ valueBuilder.argumentList([valueBuilder.literalInt(index)]),
isConst: true);
SendSet definition = valueBuilder.createDefinition(name, initializer);
@@ -225,7 +258,7 @@ class EnumCreator {
}
VariableList valuesVariableList =
- new VariableList(builder.staticConstModifiers);
+ new VariableList(builder.staticConstModifiers());
InterfaceType listType = compiler.listClass.computeType(compiler);
valuesVariableList.type = listType.createInstantiation([enumType]);
@@ -249,12 +282,10 @@ class EnumCreator {
'toString',
builder.argumentList([]),
builder.returnStatement(
- new StringInterpolation(
- builder.literalString('${enumClass.name}.', suffix: ''),
- new NodeList.singleton(new StringInterpolationPart(
- new Send(null, builder.identifier('_name')),
- builder.literalString('', prefix: '')))
- ))
+ builder.indexGet(
+ builder.mapLiteral(mapEntries, isConst: true),
+ builder.reference(builder.identifier('index')))
+ )
);
EnumMethodElementX toString = new EnumMethodElementX('toString',
« no previous file with comments | « no previous file | tests/language/enum_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698