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

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: Updated cf. comments. Created 7 years, 3 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
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/warnings.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 3322 matching lines...) Expand 10 before | Expand all | Expand 10 after
3333 scope = new TypeDeclarationScope(scope, element); 3333 scope = new TypeDeclarationScope(scope, element);
3334 resolveTypeVariableBounds(node.typeParameters); 3334 resolveTypeVariableBounds(node.typeParameters);
3335 3335
3336 element.functionSignature = SignatureResolver.analyze( 3336 element.functionSignature = SignatureResolver.analyze(
3337 compiler, node.formals, node.returnType, element, 3337 compiler, node.formals, node.returnType, element,
3338 defaultValuesAllowed: false); 3338 defaultValuesAllowed: false);
3339 3339
3340 element.alias = compiler.computeFunctionType( 3340 element.alias = compiler.computeFunctionType(
3341 element, element.functionSignature); 3341 element, element.functionSignature);
3342 3342
3343 // TODO(johnniwinther): Check for cyclic references in the typedef alias. 3343 void checkCyclicReference() {
3344 var visitor = new TypedefCyclicVisitor(compiler, element);
3345 type.accept(visitor, null);
3346 }
3347 compiler.enqueuer.resolution.addPostProcessAction(element,
3348 checkCyclicReference);
3344 } 3349 }
3345 } 3350 }
3346 3351
3352 // TODO(johnniwinther): Replace with a traversal on the AST when the type
3353 // annotations in typedef alias are stored in a [TreeElements] mapping.
3354 class TypedefCyclicVisitor extends DartTypeVisitor {
3355 final Compiler compiler;
3356 final TypedefElement element;
3357 bool hasCyclicReference = false;
3358
3359 /// Counter for how many bounds the visitor currently has on the call-stack.
3360 /// Used to detect when to report [Messagekind.CYCLIC_TYPEDEF_TYPEVAR].
3361 int seenBoundsCount = 0;
3362
3363 Link<TypedefElement> seenTypedefs = const Link<TypedefElement>();
3364
3365 int seenTypedefsCount = 0;
3366
3367 Link<TypeVariableElement> seenTypeVariables =
3368 const Link<TypeVariableElement>();
3369
3370 TypedefCyclicVisitor(Compiler this.compiler, TypedefElement this.element);
3371
3372 visitType(DartType type, _) {
3373 // Do nothing.
3374 }
3375
3376 visitTypedefType(TypedefType type, _) {
3377 TypedefElement typedefElement = type.element;
3378 if (seenTypedefs.contains(typedefElement)) {
3379 if (!hasCyclicReference && identical(element, typedefElement)) {
3380 // Only report an error on the checked typedef to avoid generating
3381 // multiple errors for the same cyclicity.
3382 hasCyclicReference = true;
3383 if (seenBoundsCount > 0) {
3384 compiler.reportError(element, MessageKind.CYCLIC_TYPEDEF_TYPEVAR);
3385 } else if (seenTypedefsCount == 1) {
3386 // Direct cyclicity.
3387 compiler.reportError(element,
3388 MessageKind.CYCLIC_TYPEDEF,
3389 {'typedefName': element.name});
3390 } else if (seenTypedefsCount == 2) {
3391 // Cyclicity through one other typedef.
3392 compiler.reportError(element,
3393 MessageKind.CYCLIC_TYPEDEF_ONE,
3394 {'typedefName': element.name,
3395 'otherTypedefName': seenTypedefs.head.name});
3396 } else {
3397 // Cyclicity through more than one other typedef.
3398 for (TypedefElement cycle in seenTypedefs) {
3399 if (!identical(typedefElement, cycle)) {
3400 compiler.reportError(element,
3401 MessageKind.CYCLIC_TYPEDEF_ONE,
3402 {'typedefName': element.name,
3403 'otherTypedefName': cycle.name});
3404 }
3405 }
3406 }
3407 }
3408 } else {
3409 seenTypedefs = seenTypedefs.prepend(typedefElement);
3410 seenTypedefsCount++;
3411 type.visitChildren(this, null);
3412 typedefElement.alias.accept(this, null);
3413 seenTypedefs = seenTypedefs.tail;
3414 seenTypedefsCount--;
3415 }
3416 }
3417
3418 visitFunctionType(FunctionType type, _) {
3419 type.visitChildren(this, null);
3420 }
3421
3422 visitInterfaceType(InterfaceType type, _) {
3423 type.visitChildren(this, null);
3424 }
3425
3426 visitTypeVariableType(TypeVariableType type, _) {
3427 TypeVariableElement typeVariableElement = type.element;
3428 if (seenTypeVariables.contains(typeVariableElement)) {
3429 // Avoid running in cycles on cyclic type variable bounds.
3430 // Cyclicity is reported elsewhere.
3431 return;
3432 }
3433 seenTypeVariables = seenTypeVariables.prepend(typeVariableElement);
3434 seenBoundsCount++;
3435 typeVariableElement.bound.accept(this, null);
3436 seenBoundsCount--;
3437 seenTypeVariables = seenTypeVariables.tail;
3438 }
3439 }
3440
3347 /** 3441 /**
3348 * The implementation of [ResolverTask.resolveClass]. 3442 * The implementation of [ResolverTask.resolveClass].
3349 * 3443 *
3350 * This visitor has to be extra careful as it is building the basic 3444 * This visitor has to be extra careful as it is building the basic
3351 * element information, and cannot safely look at other elements as 3445 * element information, and cannot safely look at other elements as
3352 * this may lead to cycles. 3446 * this may lead to cycles.
3353 * 3447 *
3354 * This visitor can assume that the supertypes have already been 3448 * This visitor can assume that the supertypes have already been
3355 * resolved, but it cannot call [ResolverTask.resolveClass] directly 3449 * resolved, but it cannot call [ResolverTask.resolveClass] directly
3356 * or indirectly (through [ClassElement.ensureResolved]) for any other 3450 * or indirectly (through [ClassElement.ensureResolved]) for any other
(...skipping 866 matching lines...) Expand 10 before | Expand all | Expand 10 after
4223 return e; 4317 return e;
4224 } 4318 }
4225 4319
4226 /// Assumed to be called by [resolveRedirectingFactory]. 4320 /// Assumed to be called by [resolveRedirectingFactory].
4227 Element visitReturn(Return node) { 4321 Element visitReturn(Return node) {
4228 Node expression = node.expression; 4322 Node expression = node.expression;
4229 return finishConstructorReference(visit(expression), 4323 return finishConstructorReference(visit(expression),
4230 expression, expression); 4324 expression, expression);
4231 } 4325 }
4232 } 4326 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/warnings.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698