Chromium Code Reviews| Index: pkg/front_end/lib/src/fasta/type_inference/type_schema.dart |
| diff --git a/pkg/front_end/lib/src/fasta/type_inference/type_schema.dart b/pkg/front_end/lib/src/fasta/type_inference/type_schema.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6ecc80a9c0dfbded5fb7cd1fa290fe12120bbb33 |
| --- /dev/null |
| +++ b/pkg/front_end/lib/src/fasta/type_inference/type_schema.dart |
| @@ -0,0 +1,66 @@ |
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE.md file. |
| + |
| +import 'package:kernel/ast.dart'; |
| +import 'package:kernel/import_table.dart'; |
| +import 'package:kernel/text/ast_to_text.dart'; |
| + |
| +/// Converts a [DartType] to a string, representing the unknown type as `?`. |
| +String typeSchemaToString(DartType schema) { |
| + StringBuffer buffer = new StringBuffer(); |
| + new TypeSchemaPrinter(buffer, syntheticNames: globalDebuggingNames) |
| + .writeNode(schema); |
| + return '$buffer'; |
| +} |
| + |
| +/// Extension of [Printer] that represents the unknown type as `?`. |
| +class TypeSchemaPrinter extends Printer implements TypeSchemaVisitor<Null> { |
| + TypeSchemaPrinter(StringSink sink, |
| + {NameSystem syntheticNames, |
| + bool showExternal, |
| + bool showOffsets: false, |
| + ImportTable importTable, |
| + Annotator annotator}) |
| + : super(sink, |
| + syntheticNames: syntheticNames, |
| + showExternal: showExternal, |
| + showOffsets: showOffsets, |
| + importTable: importTable, |
| + annotator: annotator); |
| + |
| + @override |
| + visitUnknownType(UnknownType node) { |
| + writeWord('?'); |
| + } |
| +} |
| + |
| +/// Extension of [DartTypeVisitor] which can visit [UnknownType]. |
| +class TypeSchemaVisitor<R> extends DartTypeVisitor<R> { |
| + /// Called when [UnknownType] is visited. |
| + R visitUnknownType(UnknownType node) => defaultDartType(node); |
| +} |
| + |
| +/// The unknown type (denoted `?`) is an object which can appear anywhere that |
| +/// a type is expected. It represents a component of a type which has not yet |
| +/// been fixed by inference. |
| +/// |
| +/// The unknown type cannot appear in programs or in final inferred types: it is |
| +/// purely part of the local inference process. |
|
ahe
2017/04/24 13:21:07
I suggest that you extend pkg/front_end/lib/src/fa
Paul Berry
2017/04/24 18:33:32
Ooh, good idea. I will do that in a follow-up CL.
|
| +class UnknownType extends DartType { |
| + const UnknownType(); |
| + |
| + bool operator ==(Object other) => other is UnknownType; |
|
ahe
2017/04/24 13:21:07
Perhaps you should add a note that this class can'
Paul Berry
2017/04/24 18:33:32
Done.
|
| + |
| + @override |
| + accept(DartTypeVisitor v) { |
| + if (v is TypeSchemaVisitor) { |
| + return v.visitUnknownType(this); |
| + } else { |
| + return v.defaultDartType(this); |
|
ahe
2017/04/24 13:21:07
Perhaps this should throw?
Paul Berry
2017/04/24 18:33:32
That's what I had at first but it had a poor inter
ahe
2017/04/24 19:46:05
Good point. Perhaps add a comment about this probl
|
| + } |
| + } |
| + |
| + @override |
| + visitChildren(Visitor v) {} |
| +} |