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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/resolution/members.dart

Issue 13261008: Check for cyclic reference in typedefs (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased Created 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of resolution; 5 part of resolution;
6 6
7 abstract class TreeElements { 7 abstract class TreeElements {
8 Element get currentElement; 8 Element get currentElement;
9 Set<Node> get superUses; 9 Set<Node> get superUses;
10 10
(...skipping 2988 matching lines...) Expand 10 before | Expand all | Expand 10 after
2999 TypedefType type = element.computeType(compiler); 2999 TypedefType type = element.computeType(compiler);
3000 scope = new TypeDeclarationScope(scope, element); 3000 scope = new TypeDeclarationScope(scope, element);
3001 resolveTypeVariableBounds(node.typeParameters); 3001 resolveTypeVariableBounds(node.typeParameters);
3002 3002
3003 element.functionSignature = SignatureResolver.analyze( 3003 element.functionSignature = SignatureResolver.analyze(
3004 compiler, node.formals, node.returnType, element); 3004 compiler, node.formals, node.returnType, element);
3005 3005
3006 element.alias = compiler.computeFunctionType( 3006 element.alias = compiler.computeFunctionType(
3007 element, element.functionSignature); 3007 element, element.functionSignature);
3008 3008
3009 // TODO(johnniwinther): Check for cyclic references in the typedef alias. 3009 void checkCyclicReference() {
3010 var visitor = new TypedefCyclicVisitor(compiler, element);
3011 visitor.visit(type);
ahe 2013/04/02 08:22:39 This is probably slightly faster: type.accept(vis
Johnni Winther 2013/04/02 09:40:14 Done.
3012 }
3013 compiler.enqueuer.resolution.addPostProcessing(element,
3014 checkCyclicReference);
3010 } 3015 }
3011 } 3016 }
3012 3017
3018 // TODO(johnniwinther): Replace with a traversal on the AST when the type
3019 // annotations in typedef alias are stored in a [TreeElements] mapping.
3020 class TypedefCyclicVisitor extends DartTypeTraversal {
3021 final Compiler compiler;
3022 final TypedefElement element;
3023 bool hasCyclicReference = false;
3024 Link<TypedefElement> seenTypedefs = const Link<TypedefElement>();
3025
3026 TypedefCyclicVisitor(Compiler this.compiler, TypedefElement this.element);
3027
3028 visitType(DartType type, _) {
3029 // Do nothing.
3030 }
3031
3032 visitTypedefType(TypedefType type, _) {
3033 TypedefElement typedefElement = type.element;
3034 if (seenTypedefs.contains(typedefElement)) {
3035 if (!hasCyclicReference && identical(element, typedefElement)) {
3036 // Only report an error on the checked typedef to avoid generating
3037 // multiple errors for the same cyclicity.
3038 hasCyclicReference = true;
3039 if (seenTypedefs.tail.isEmpty) {
3040 // Direct cyclicity.
ahe 2013/04/02 08:22:39 I don't think "cyclicity" is a word.
Johnni Winther 2013/04/02 09:40:14 It is! (and not just in my vocabulary: http://en.w
3041 compiler.reportErrorCode(element,
3042 MessageKind.CYCLIC_TYPEDEF, {'typedefName': element.name});
3043 } else if (seenTypedefs.tail.tail.isEmpty) {
3044 // Cyclicity through one other typedef.
ahe 2013/04/02 08:22:39 "cyclicity"
Johnni Winther 2013/04/02 09:40:14 Ditto.
3045 compiler.reportErrorCode(element,
3046 MessageKind.CYCLIC_TYPEDEF_ONE,
3047 {'typedefName': element.name,
3048 'otherTypedefName': seenTypedefs.head.name});
3049 } else {
3050 // Cyclicity through more than one other typedef.
3051 compiler.reportErrorCode(element,
ahe 2013/04/02 08:22:39 I'd drop this error code and use: for (TypedefEle
Johnni Winther 2013/04/02 09:40:14 Done.
3052 MessageKind.CYCLIC_TYPEDEF_MORE, {'typedefName': element.name});
3053 }
3054 }
3055 return;
3056 }
3057 seenTypedefs = seenTypedefs.prepend(typedefElement);
3058 visitList(type.typeArguments);
3059 visit(typedefElement.alias);
3060 seenTypedefs = seenTypedefs.tail;
3061 }
3062 }
3063
3013 /** 3064 /**
3014 * The implementation of [ResolverTask.resolveClass]. 3065 * The implementation of [ResolverTask.resolveClass].
3015 * 3066 *
3016 * This visitor has to be extra careful as it is building the basic 3067 * This visitor has to be extra careful as it is building the basic
3017 * element information, and cannot safely look at other elements as 3068 * element information, and cannot safely look at other elements as
3018 * this may lead to cycles. 3069 * this may lead to cycles.
3019 * 3070 *
3020 * This visitor can assume that the supertypes have already been 3071 * This visitor can assume that the supertypes have already been
3021 * resolved, but it cannot call [ResolverTask.resolveClass] directly 3072 * resolved, but it cannot call [ResolverTask.resolveClass] directly
3022 * or indirectly (through [ClassElement.ensureResolved]) for any other 3073 * or indirectly (through [ClassElement.ensureResolved]) for any other
(...skipping 804 matching lines...) Expand 10 before | Expand all | Expand 10 after
3827 return e; 3878 return e;
3828 } 3879 }
3829 3880
3830 /// Assumed to be called by [resolveRedirectingFactory]. 3881 /// Assumed to be called by [resolveRedirectingFactory].
3831 Element visitReturn(Return node) { 3882 Element visitReturn(Return node) {
3832 Node expression = node.expression; 3883 Node expression = node.expression;
3833 return finishConstructorReference(visit(expression), 3884 return finishConstructorReference(visit(expression),
3834 expression, expression); 3885 expression, expression);
3835 } 3886 }
3836 } 3887 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698