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

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

Issue 11778039: Report arity errors on user definable operators. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments Created 7 years, 11 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 operator[](Node node); 8 Element operator[](Node node);
9 Selector getSelector(Send send); 9 Selector getSelector(Send send);
10 DartType getType(Node node); 10 DartType getType(Node node);
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 metadata.ensureResolved(compiler); 488 metadata.ensureResolved(compiler);
489 } 489 }
490 } 490 }
491 491
492 void checkMembers(ClassElement cls) { 492 void checkMembers(ClassElement cls) {
493 assert(invariant(cls, cls.isDeclaration)); 493 assert(invariant(cls, cls.isDeclaration));
494 if (cls.isObject(compiler)) return; 494 if (cls.isObject(compiler)) return;
495 // TODO(johnniwinther): Should this be done on the implementation element as 495 // TODO(johnniwinther): Should this be done on the implementation element as
496 // well? 496 // well?
497 cls.forEachMember((holder, member) { 497 cls.forEachMember((holder, member) {
498 // Perform various checks as side effect of "computing" the type. 498 compiler.withCurrentElement(member, () {
499 member.computeType(compiler); 499 // Perform various checks as side effect of "computing" the type.
500 member.computeType(compiler);
500 501
501 // Check modifiers. 502 // Check modifiers.
502 if (member.isFunction() && member.modifiers.isFinal()) { 503 if (member.isFunction() && member.modifiers.isFinal()) {
503 compiler.reportMessage(
504 compiler.spanFromElement(member),
505 MessageKind.ILLEGAL_FINAL_METHOD_MODIFIER.error(),
506 Diagnostic.ERROR);
507 }
508 if (member.isConstructor()) {
509 final mismatchedFlagsBits =
510 member.modifiers.flags &
511 (Modifiers.FLAG_STATIC | Modifiers.FLAG_ABSTRACT);
512 if (mismatchedFlagsBits != 0) {
513 final mismatchedFlags =
514 new Modifiers.withFlags(null, mismatchedFlagsBits);
515 compiler.reportMessage( 504 compiler.reportMessage(
516 compiler.spanFromElement(member), 505 compiler.spanFromElement(member),
517 MessageKind.ILLEGAL_CONSTRUCTOR_MODIFIERS.error([mismatchedFlags]), 506 MessageKind.ILLEGAL_FINAL_METHOD_MODIFIER.error(),
518 Diagnostic.ERROR); 507 Diagnostic.ERROR);
519 } 508 }
520 checkConstructorNameHack(holder, member); 509 if (member.isConstructor()) {
521 } 510 final mismatchedFlagsBits =
522 checkAbstractField(member); 511 member.modifiers.flags &
523 checkValidOverride(member, cls.lookupSuperMember(member.name)); 512 (Modifiers.FLAG_STATIC | Modifiers.FLAG_ABSTRACT);
513 if (mismatchedFlagsBits != 0) {
514 final mismatchedFlags =
515 new Modifiers.withFlags(null, mismatchedFlagsBits);
516 compiler.reportMessage(
517 compiler.spanFromElement(member),
518 MessageKind.ILLEGAL_CONSTRUCTOR_MODIFIERS.error([mismatchedFlags]) ,
ahe 2013/01/09 13:35:46 Strange newline in review tool here.
Johnni Winther 2013/01/09 14:29:11 Done.
519 Diagnostic.ERROR);
520 }
521 checkConstructorNameHack(holder, member);
522 }
523 checkAbstractField(member);
524 checkValidOverride(member, cls.lookupSuperMember(member.name));
525 checkUserDefinableOperator(member);
526 });
524 }); 527 });
525 } 528 }
526 529
527 // TODO(ahe): Remove this method. It is only needed while we store 530 // TODO(ahe): Remove this method. It is only needed while we store
528 // constructor names as ClassName$id. Once we start storing 531 // constructor names as ClassName$id. Once we start storing
529 // constructors as just id, this will be caught by the general 532 // constructors as just id, this will be caught by the general
530 // mechanism for duplicate members. 533 // mechanism for duplicate members.
531 /// Check that a constructor name does not conflict with a member. 534 /// Check that a constructor name does not conflict with a member.
532 void checkConstructorNameHack(ClassElement holder, FunctionElement member) { 535 void checkConstructorNameHack(ClassElement holder, FunctionElement member) {
533 // If the name of the constructor is the same as the name of the 536 // If the name of the constructor is the same as the name of the
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 compiler.spanFromElement(field.getter), 585 compiler.spanFromElement(field.getter),
583 MessageKind.GETTER_MISMATCH.error([mismatchedFlags]), 586 MessageKind.GETTER_MISMATCH.error([mismatchedFlags]),
584 Diagnostic.ERROR); 587 Diagnostic.ERROR);
585 compiler.reportMessage( 588 compiler.reportMessage(
586 compiler.spanFromElement(field.setter), 589 compiler.spanFromElement(field.setter),
587 MessageKind.SETTER_MISMATCH.error([mismatchedFlags]), 590 MessageKind.SETTER_MISMATCH.error([mismatchedFlags]),
588 Diagnostic.ERROR); 591 Diagnostic.ERROR);
589 } 592 }
590 } 593 }
591 594
595 void checkUserDefinableOperator(Element member) {
596 FunctionElement function = member.asFunctionElement();
597 if (function == null) return;
598 String value = member.name.stringValue;
599 if (value == null) return;
600 if (!(isUserDefinableOperator(value) || identical(value, 'unary-'))) return;
601
602 int requiredParameterCount;
603 MessageKind messageKind;
604 FunctionSignature signature = function.computeSignature(compiler);
605 if (identical(value, 'unary-')) {
606 messageKind = MessageKind.MINUS_OPERATOR_BAD_ARITY;
607 requiredParameterCount = 0;
608 } else if (isMinusOperator(value)) {
609 messageKind = MessageKind.MINUS_OPERATOR_BAD_ARITY;
610 requiredParameterCount = 1;
611 } else if (isUnaryOperator(value)) {
612 messageKind = MessageKind.UNARY_OPERATOR_BAD_ARITY;
613 requiredParameterCount = 0;
614 } else if (isBinaryOperator(value)) {
615 messageKind = MessageKind.BINARY_OPERATOR_BAD_ARITY;
616 requiredParameterCount = 1;
617 } else if (isTernaryOperator(value)) {
618 messageKind = MessageKind.TERNARY_OPERATOR_BAD_ARITY;
619 requiredParameterCount = 2;
620 } else {
621 compiler.internalErrorOnElement(function,
622 'Unexpected user defined operator $value');
623 }
624 checkArity(function, requiredParameterCount, messageKind);
625 }
626
627 void checkArity(FunctionElement function,
628 int requiredParameterCount, MessageKind messageKind) {
629 FunctionExpression node = function.parseNode(compiler);
630 FunctionSignature signature = function.computeSignature(compiler);
631 if (signature.requiredParameterCount != requiredParameterCount) {
632 Node errorNode = node;
633 if (node.parameters != null) {
634 if (signature.requiredParameterCount < requiredParameterCount) {
635 errorNode = node.parameters;
636 } else {
637 errorNode = node.parameters.nodes.skip(requiredParameterCount).head;
638 }
639 }
640 compiler.reportMessage(
641 compiler.spanFromNode(errorNode),
642 messageKind.error([function.name]),
643 Diagnostic.ERROR);
644 }
645 if (signature.optionalParameterCount != 0) {
646 Node errorNode =
647 node.parameters.nodes.skip(signature.requiredParameterCount).head;
648 if (signature.optionalParametersAreNamed) {
649 compiler.reportMessage(
650 compiler.spanFromNode(errorNode),
651 MessageKind.OPERATOR_NAMED_PARAMETERS.error([function.name]),
652 Diagnostic.ERROR);
653 } else {
654 compiler.reportMessage(
655 compiler.spanFromNode(errorNode),
656 MessageKind.OPERATOR_OPTIONAL_PARAMETERS.error([function.name]),
657 Diagnostic.ERROR);
658 }
659 }
660 }
661
ahe 2013/01/09 13:35:46 Extra line.
Johnni Winther 2013/01/09 14:29:11 Done.
662
592 reportErrorWithContext(Element errorneousElement, 663 reportErrorWithContext(Element errorneousElement,
593 MessageKind errorMessage, 664 MessageKind errorMessage,
594 Element contextElement, 665 Element contextElement,
595 MessageKind contextMessage) { 666 MessageKind contextMessage) {
596 compiler.reportMessage( 667 compiler.reportMessage(
597 compiler.spanFromElement(errorneousElement), 668 compiler.spanFromElement(errorneousElement),
598 errorMessage.error([contextElement.name, 669 errorMessage.error([contextElement.name,
599 contextElement.getEnclosingClass().name]), 670 contextElement.getEnclosingClass().name]),
600 Diagnostic.ERROR); 671 Diagnostic.ERROR);
601 compiler.reportMessage( 672 compiler.reportMessage(
(...skipping 2716 matching lines...) Expand 10 before | Expand all | Expand 10 after
3318 return e; 3389 return e;
3319 } 3390 }
3320 3391
3321 /// Assumed to be called by [resolveRedirectingFactory]. 3392 /// Assumed to be called by [resolveRedirectingFactory].
3322 Element visitReturn(Return node) { 3393 Element visitReturn(Return node) {
3323 Node expression = node.expression; 3394 Node expression = node.expression;
3324 return finishConstructorReference(visit(expression), 3395 return finishConstructorReference(visit(expression),
3325 expression, expression); 3396 expression, expression);
3326 } 3397 }
3327 } 3398 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698