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

Unified Diff: pkg/analyzer/lib/src/summary/summarize_const_expr.dart

Issue 1674073002: Add UnlinkedConst.isValid and set it during summarizing. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/summary/summarize_const_expr.dart
diff --git a/pkg/analyzer/lib/src/summary/summarize_const_expr.dart b/pkg/analyzer/lib/src/summary/summarize_const_expr.dart
index b523b8b3c8c40e0bd6633ea4a6cfd0bf4c94bd1f..807e9abd3345d897273b347081a80dbd7f4096e1 100644
--- a/pkg/analyzer/lib/src/summary/summarize_const_expr.dart
+++ b/pkg/analyzer/lib/src/summary/summarize_const_expr.dart
@@ -40,9 +40,68 @@ abstract class AbstractConstExprSerializer {
final List<EntityRefBuilder> references = <EntityRefBuilder>[];
/**
+ * Return the [UnlinkedConstBuilder] that corresponds to the given [expr].
+ */
+ UnlinkedConstBuilder serialize(Expression expr) {
+ try {
+ _serialize(expr);
+ return new UnlinkedConstBuilder(
+ isValid: true,
+ operations: operations,
+ ints: ints,
+ doubles: doubles,
+ strings: strings,
+ references: references);
+ } on StateError {
+ return new UnlinkedConstBuilder(isValid: false);
+ }
+ }
+
+ /**
+ * Return [EntityRefBuilder] that corresponds to the given [constructor].
+ */
+ EntityRefBuilder serializeConstructorName(ConstructorName constructor);
+
+ /**
+ * Return [EntityRefBuilder] that corresponds to the given [identifier].
+ */
+ EntityRefBuilder serializeIdentifier(Identifier identifier);
+
+ /**
+ * Return [EntityRefBuilder] that corresponds to the given [access].
+ */
+ EntityRefBuilder serializePropertyAccess(PropertyAccess access);
+
+ /**
+ * Return [EntityRefBuilder] that corresponds to the given [type].
+ */
+ EntityRefBuilder serializeType(TypeName type);
+
+ void _pushInt(int value) {
+ assert(value >= 0);
+ if (value >= (1 << 32)) {
+ int numOfComponents = 0;
+ ints.add(numOfComponents);
+ void pushComponents(int value) {
+ if (value >= (1 << 32)) {
+ pushComponents(value >> 32);
+ }
+ numOfComponents++;
+ ints.add(value & 0xFFFFFFFF);
+ }
+ pushComponents(value);
+ ints[ints.length - 1 - numOfComponents] = numOfComponents;
+ operations.add(UnlinkedConstOperation.pushLongInt);
+ } else {
+ operations.add(UnlinkedConstOperation.pushInt);
+ ints.add(value);
+ }
+ }
+
+ /**
* Serialize the given [expr] expression into this serializer state.
*/
- void serialize(Expression expr) {
+ void _serialize(Expression expr) {
if (expr is IntegerLiteral) {
_pushInt(expr.value);
} else if (expr is DoubleLiteral) {
@@ -73,98 +132,43 @@ abstract class AbstractConstExprSerializer {
} else if (expr is MethodInvocation) {
String name = expr.methodName.name;
if (name != 'identical') {
- throw new _ConstExprSerializationError(
- 'Only "identity" function invocation is allowed.');
+ throw new StateError('Only "identity" function invocation is allowed.');
}
if (expr.argumentList == null ||
expr.argumentList.arguments.length != 2) {
- throw new _ConstExprSerializationError(
+ throw new StateError(
'The function "identity" requires exactly 2 arguments.');
}
- expr.argumentList.arguments.forEach(serialize);
+ expr.argumentList.arguments.forEach(_serialize);
operations.add(UnlinkedConstOperation.identical);
} else if (expr is BinaryExpression) {
_serializeBinaryExpression(expr);
} else if (expr is ConditionalExpression) {
- serialize(expr.condition);
- serialize(expr.thenExpression);
- serialize(expr.elseExpression);
+ _serialize(expr.condition);
+ _serialize(expr.thenExpression);
+ _serialize(expr.elseExpression);
operations.add(UnlinkedConstOperation.conditional);
} else if (expr is PrefixExpression) {
_serializePrefixExpression(expr);
} else if (expr is PropertyAccess) {
if (expr.target is! PrefixedIdentifier &&
expr.propertyName.name == 'length') {
- serialize(expr.target);
+ _serialize(expr.target);
operations.add(UnlinkedConstOperation.length);
} else {
references.add(serializePropertyAccess(expr));
operations.add(UnlinkedConstOperation.pushReference);
}
} else if (expr is ParenthesizedExpression) {
- serialize(expr.expression);
+ _serialize(expr.expression);
} else {
- throw new _ConstExprSerializationError('Unknown expression type: $expr');
- }
- }
-
- /**
- * Return [EntityRefBuilder] that corresponds to the given [constructor].
- */
- EntityRefBuilder serializeConstructorName(ConstructorName constructor);
-
- /**
- * Return [EntityRefBuilder] that corresponds to the given [identifier].
- */
- EntityRefBuilder serializeIdentifier(Identifier identifier);
-
- /**
- * Return [EntityRefBuilder] that corresponds to the given [access].
- */
- EntityRefBuilder serializePropertyAccess(PropertyAccess access);
-
- /**
- * Return [EntityRefBuilder] that corresponds to the given [type].
- */
- EntityRefBuilder serializeType(TypeName type);
-
- /**
- * Return the [UnlinkedConstBuilder] that corresponds to the state of this
- * serializer.
- */
- UnlinkedConstBuilder toBuilder() {
- return new UnlinkedConstBuilder(
- operations: operations,
- ints: ints,
- doubles: doubles,
- strings: strings,
- references: references);
- }
-
- void _pushInt(int value) {
- assert(value >= 0);
- if (value >= (1 << 32)) {
- int numOfComponents = 0;
- ints.add(numOfComponents);
- void pushComponents(int value) {
- if (value >= (1 << 32)) {
- pushComponents(value >> 32);
- }
- numOfComponents++;
- ints.add(value & 0xFFFFFFFF);
- }
- pushComponents(value);
- ints[ints.length - 1 - numOfComponents] = numOfComponents;
- operations.add(UnlinkedConstOperation.pushLongInt);
- } else {
- operations.add(UnlinkedConstOperation.pushInt);
- ints.add(value);
+ throw new StateError('Unknown expression type: $expr');
}
}
void _serializeBinaryExpression(BinaryExpression expr) {
- serialize(expr.leftOperand);
- serialize(expr.rightOperand);
+ _serialize(expr.leftOperand);
+ _serialize(expr.rightOperand);
TokenType operator = expr.operator.type;
if (operator == TokenType.EQ_EQ) {
operations.add(UnlinkedConstOperation.equal);
@@ -205,7 +209,7 @@ abstract class AbstractConstExprSerializer {
} else if (operator == TokenType.PERCENT) {
operations.add(UnlinkedConstOperation.modulo);
} else {
- throw new _ConstExprSerializationError('Unknown operator: $operator');
+ throw new StateError('Unknown operator: $operator');
}
}
@@ -217,9 +221,9 @@ abstract class AbstractConstExprSerializer {
arguments.forEach((arg) {
if (arg is NamedExpression) {
argumentNames.add(arg.name.label.name);
- serialize(arg.expression);
+ _serialize(arg.expression);
} else {
- serialize(arg);
+ _serialize(arg);
}
});
// Add the op-code and numbers of named and positional arguments.
@@ -233,7 +237,7 @@ abstract class AbstractConstExprSerializer {
void _serializeListLiteral(ListLiteral expr) {
List<Expression> elements = expr.elements;
- elements.forEach(serialize);
+ elements.forEach(_serialize);
ints.add(elements.length);
if (expr.typeArguments != null &&
expr.typeArguments.arguments.length == 1) {
@@ -246,8 +250,8 @@ abstract class AbstractConstExprSerializer {
void _serializeMapLiteral(MapLiteral expr) {
for (MapLiteralEntry entry in expr.entries) {
- serialize(entry.key);
- serialize(entry.value);
+ _serialize(entry.key);
+ _serialize(entry.value);
}
ints.add(expr.entries.length);
if (expr.typeArguments != null &&
@@ -261,7 +265,7 @@ abstract class AbstractConstExprSerializer {
}
void _serializePrefixExpression(PrefixExpression expr) {
- serialize(expr.operand);
+ _serialize(expr.operand);
TokenType operator = expr.operator.type;
if (operator == TokenType.BANG) {
operations.add(UnlinkedConstOperation.not);
@@ -270,7 +274,7 @@ abstract class AbstractConstExprSerializer {
} else if (operator == TokenType.TILDE) {
operations.add(UnlinkedConstOperation.complement);
} else {
- throw new _ConstExprSerializationError('Unknown operator: $operator');
+ throw new StateError('Unknown operator: $operator');
}
}
@@ -294,7 +298,7 @@ abstract class AbstractConstExprSerializer {
operations.add(UnlinkedConstOperation.pushString);
strings.add(element.value);
} else {
- serialize((element as InterpolationExpression).expression);
+ _serialize((element as InterpolationExpression).expression);
}
}
operations.add(UnlinkedConstOperation.concatenate);
@@ -302,15 +306,3 @@ abstract class AbstractConstExprSerializer {
}
}
}
-
-/**
- * Error that describes a problem during a constant expression serialization.
- */
-class _ConstExprSerializationError {
- final String message;
-
- _ConstExprSerializationError(this.message);
-
- @override
- String toString() => message;
-}

Powered by Google App Engine
This is Rietveld 408576698