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

Side by Side Diff: lib/src/info.dart

Issue 1038213003: Downward inference (Closed) Base URL: git@github.com:dart-lang/dart-dev-compiler.git@master
Patch Set: Address comments Created 5 years, 8 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) 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 /// Defines static information collected by the type checker and used later by 5 /// Defines static information collected by the type checker and used later by
6 /// emitters to generate code. 6 /// emitters to generate code.
7 library dev_compiler.src.info; 7 library dev_compiler.src.info;
8 8
9 import 'dart:mirrors'; 9 import 'dart:mirrors';
10 10
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 354
355 // A down cast to a non-ground type. These behave differently from standard 355 // A down cast to a non-ground type. These behave differently from standard
356 // Dart and may be more likely to fail at runtime. 356 // Dart and may be more likely to fail at runtime.
357 class DownCastImplicit extends DownCast { 357 class DownCastImplicit extends DownCast {
358 DownCastImplicit(TypeRules rules, Expression expression, Cast cast) 358 DownCastImplicit(TypeRules rules, Expression expression, Cast cast)
359 : super._internal(rules, expression, cast); 359 : super._internal(rules, expression, cast);
360 360
361 final Level level = Level.WARNING; 361 final Level level = Level.WARNING;
362 } 362 }
363 363
364 // An inferred type for the wrapped expression, which may need to be
365 // reified into the term
366 abstract class InferredTypeBase extends Conversion {
367 DartType _type;
368
369 InferredTypeBase._internal(TypeRules rules, Expression expression, this._type)
370 : super(rules, expression);
371
372 DartType get type => _type;
373
374 DartType _getConvertedType() => type;
375
376 String get message => '$expression has inferred type $type';
377
378 Level get level => Level.INFO;
379
380 accept(AstVisitor visitor) {
381 if (visitor is ConversionVisitor) {
382 return visitor.visitInferredTypeBase(this);
383 } else {
384 return expression.accept(visitor);
385 }
386 }
387 }
388
389 // Standard / unspecialized inferred type
390 class InferredType extends InferredTypeBase {
391 InferredType(TypeRules rules, Expression expression, DartType type)
392 : super._internal(rules, expression, type);
393
394 // Factory to create correct InferredType variant.
395 static InferredTypeBase create(
396 TypeRules rules, Expression expression, DartType type) {
397
398 // Specialized inference:
399 if (expression is Literal) {
400 return new InferredTypeLiteral(rules, expression, type);
401 }
402 if (expression is InstanceCreationExpression) {
403 return new InferredTypeAllocation(rules, expression, type);
404 }
405 return new InferredType(rules, expression, type);
406 }
407 }
408
409 // An infered type for a literal expression.
410 class InferredTypeLiteral extends InferredTypeBase {
411 InferredTypeLiteral(TypeRules rules, Expression expression, DartType type)
412 : super._internal(rules, expression, type);
413 }
414
415 // An inferred type for a non-literal allocation site.
416 class InferredTypeAllocation extends InferredTypeBase {
417 InferredTypeAllocation(TypeRules rules, Expression expression, DartType type)
418 : super._internal(rules, expression, type);
419 }
420
364 // TODO(vsm): Remove these. 421 // TODO(vsm): Remove these.
365 422
366 // A wrapped closure coerces the underlying type to the desired type. 423 // A wrapped closure coerces the underlying type to the desired type.
367 class ClosureWrapBase extends Conversion { 424 class ClosureWrapBase extends Conversion {
368 FunctionType _wrappedType; 425 FunctionType _wrappedType;
369 Wrapper _wrapper; 426 Wrapper _wrapper;
370 427
371 ClosureWrapBase._internal( 428 ClosureWrapBase._internal(
372 TypeRules rules, Expression expression, this._wrapper, this._wrappedType) 429 TypeRules rules, Expression expression, this._wrapper, this._wrappedType)
373 : super(rules, expression) { 430 : super(rules, expression) {
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 R visitRuntimeOperation(RuntimeOperation node) => visitNode(node); 707 R visitRuntimeOperation(RuntimeOperation node) => visitNode(node);
651 708
652 /// The catch-all for any kind of conversion 709 /// The catch-all for any kind of conversion
653 R visitConversion(Conversion node) => visitNode(node); 710 R visitConversion(Conversion node) => visitNode(node);
654 711
655 // Methods for conversion subtypes: 712 // Methods for conversion subtypes:
656 R visitDownCast(DownCast node) => visitConversion(node); 713 R visitDownCast(DownCast node) => visitConversion(node);
657 R visitClosureWrapBase(ClosureWrapBase node) => visitConversion(node); 714 R visitClosureWrapBase(ClosureWrapBase node) => visitConversion(node);
658 R visitClosureWrap(ClosureWrap node) => visitClosureWrapBase(node); 715 R visitClosureWrap(ClosureWrap node) => visitClosureWrapBase(node);
659 R visitDynamicInvoke(DynamicInvoke node) => visitConversion(node); 716 R visitDynamicInvoke(DynamicInvoke node) => visitConversion(node);
717 R visitInferredTypeBase(InferredTypeBase node) => visitConversion(node);
660 } 718 }
661 719
662 /// Automatically infer list of types by scanning this library using mirrors. 720 /// Automatically infer list of types by scanning this library using mirrors.
663 final List<Type> infoTypes = () { 721 final List<Type> infoTypes = () {
664 var allTypes = new Set(); 722 var allTypes = new Set();
665 var baseTypes = new Set(); 723 var baseTypes = new Set();
666 var infoMirror = reflectClass(StaticInfo); 724 var infoMirror = reflectClass(StaticInfo);
667 var declarations = infoMirror.owner.declarations.values; 725 var declarations = infoMirror.owner.declarations.values;
668 for (var cls in declarations.where((d) => d is ClassMirror)) { 726 for (var cls in declarations.where((d) => d is ClassMirror)) {
669 if (cls.isSubtypeOf(infoMirror)) { 727 if (cls.isSubtypeOf(infoMirror)) {
670 allTypes.add(cls); 728 allTypes.add(cls);
671 baseTypes.add(cls.superclass); 729 baseTypes.add(cls.superclass);
672 } 730 }
673 } 731 }
674 allTypes.removeAll(baseTypes); 732 allTypes.removeAll(baseTypes);
675 return new List<Type>.from(allTypes.map((mirror) => mirror.reflectedType)) 733 return new List<Type>.from(allTypes.map((mirror) => mirror.reflectedType))
676 ..sort((t1, t2) => '$t1'.compareTo('$t2')); 734 ..sort((t1, t2) => '$t1'.compareTo('$t2'));
677 }(); 735 }();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698