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

Side by Side Diff: pkg/compiler/lib/src/resolution/send_resolver.dart

Issue 1047673002: Add SemanticDeclVisitor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Correct long lines 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 | Annotate | Revision Log
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 part of dart2js.semantics_visitor; 5 part of dart2js.semantics_visitor;
6 6
7 enum SendStructureKind { 7 enum SendStructureKind {
8 GET, 8 GET,
9 SET, 9 SET,
10 INVOKE, 10 INVOKE,
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 Element element = elements[node.send]; 458 Element element = elements[node.send];
459 Selector selector = elements.getSelector(node.send); 459 Selector selector = elements.getSelector(node.send);
460 DartType type = elements.getType(node); 460 DartType type = elements.getType(node);
461 461
462 ConstructorAccessSemantics constructorAccessSemantics = 462 ConstructorAccessSemantics constructorAccessSemantics =
463 computeConstructorAccessSemantics(element, type); 463 computeConstructorAccessSemantics(element, type);
464 return new NewInvokeStructure(constructorAccessSemantics, selector); 464 return new NewInvokeStructure(constructorAccessSemantics, selector);
465 } 465 }
466 } 466 }
467 467
468 abstract class DeclStructure<R, A> {
469 final FunctionElement element;
470
471 DeclStructure(this.element);
472
473 /// Calls the matching visit method on [visitor] with [node] and [arg].
474 R dispatch(SemanticDeclVisitor<R, A> visitor,
475 FunctionExpression node,
476 A arg);
477 }
478
479 enum ConstructorKind {
480 GENERATIVE,
481 REDIRECTING_GENERATIVE,
482 FACTORY,
483 REDIRECTING_FACTORY,
484 }
485
486 class ConstructorDeclStructure<R, A> extends DeclStructure<R, A> {
487 final ConstructorKind kind;
488
489 ConstructorDeclStructure(this.kind, ConstructorElement constructor)
490 : super(constructor);
491
492 R dispatch(SemanticDeclVisitor<R, A> visitor,
493 FunctionExpression node,
494 A arg) {
495 switch (kind) {
496 case ConstructorKind.GENERATIVE:
497 return visitor.visitGenerativeConstructorDecl(
498 node, element, node.parameters, node.initializers, node.body, arg);
499 case ConstructorKind.REDIRECTING_GENERATIVE:
500 return visitor.visitRedirectingGenerativeConstructorDecl(
501 node, element, node.parameters, node.initializers, arg);
502 case ConstructorKind.FACTORY:
503 return visitor.visitFactoryConstructorDecl(
504 node, element, node.parameters, node.body, arg);
505 default:
506 break;
507 }
508 throw new SpannableAssertionFailure(node,
509 "Unhandled constructor declaration kind: ${kind}");
510 }
511 }
512
513 class RedirectingFactoryConstructorDeclStructure<R, A>
514 extends DeclStructure<R, A> {
515 InterfaceType redirectionTargetType;
516 ConstructorElement redirectionTarget;
517
518 RedirectingFactoryConstructorDeclStructure(
519 ConstructorElement constructor,
520 this.redirectionTargetType,
521 this.redirectionTarget)
522 : super(constructor);
523
524 R dispatch(SemanticDeclVisitor<R, A> visitor,
525 FunctionExpression node,
526 A arg) {
527 return visitor.visitRedirectingFactoryConstructorDecl(
528 node, element, node.parameters,
529 redirectionTargetType, redirectionTarget, arg);
530 }
531 }
532
533 enum FunctionKind {
534 TOP_LEVEL_GETTER,
535 TOP_LEVEL_SETTER,
536 TOP_LEVEL_FUNCTION,
537 STATIC_GETTER,
538 STATIC_SETTER,
539 STATIC_FUNCTION,
540 ABSTRACT_GETTER,
541 ABSTRACT_SETTER,
542 ABSTRACT_METHOD,
543 INSTANCE_GETTER,
544 INSTANCE_SETTER,
545 INSTANCE_METHOD,
546 LOCAL_FUNCTION,
547 CLOSURE,
548 }
549
550 class FunctionDeclStructure<R, A>
551 extends DeclStructure<R, A> {
552 final FunctionKind kind;
553
554 FunctionDeclStructure(this.kind, FunctionElement function)
555 : super(function);
556
557 R dispatch(SemanticDeclVisitor<R, A> visitor,
558 FunctionExpression node,
559 A arg) {
560 switch (kind) {
561 case FunctionKind.TOP_LEVEL_GETTER:
562 return visitor.visitTopLevelGetterDecl(
563 node, element, node.body, arg);
564 case FunctionKind.TOP_LEVEL_SETTER:
565 return visitor.visitTopLevelSetterDecl(
566 node, element, node.parameters, node.body, arg);
567 case FunctionKind.TOP_LEVEL_FUNCTION:
568 return visitor.visitTopLevelFunctionDecl(
569 node, element, node.parameters, node.body, arg);
570 case FunctionKind.STATIC_GETTER:
571 return visitor.visitStaticGetterDecl(
572 node, element, node.body, arg);
573 case FunctionKind.STATIC_SETTER:
574 return visitor.visitStaticSetterDecl(
575 node, element, node.parameters, node.body, arg);
576 case FunctionKind.STATIC_FUNCTION:
577 return visitor.visitStaticFunctionDecl(
578 node, element, node.parameters, node.body, arg);
579 case FunctionKind.ABSTRACT_GETTER:
580 return visitor.visitAbstractGetterDecl(
581 node, element, arg);
582 case FunctionKind.ABSTRACT_SETTER:
583 return visitor.visitAbstractSetterDecl(
584 node, element, node.parameters, arg);
585 case FunctionKind.ABSTRACT_METHOD:
586 return visitor.visitAbstractMethodDecl(
587 node, element, node.parameters, arg);
588 case FunctionKind.INSTANCE_GETTER:
589 return visitor.visitInstanceGetterDecl(
590 node, element, node.body, arg);
591 case FunctionKind.INSTANCE_SETTER:
592 return visitor.visitInstanceSetterDecl(
593 node, element, node.parameters, node.body, arg);
594 case FunctionKind.INSTANCE_METHOD:
595 return visitor.visitInstanceMethodDecl(
596 node, element, node.parameters, node.body, arg);
597 case FunctionKind.LOCAL_FUNCTION:
598 return visitor.visitLocalFunctionDecl(
599 node, element, node.parameters, node.body, arg);
600 case FunctionKind.CLOSURE:
601 return visitor.visitClosureDecl(
602 node, element, node.parameters, node.body, arg);
603 }
604 }
605 }
606
607 abstract class DeclResolverMixin {
608 TreeElements get elements;
609
610 internalError(Spannable spannable, String message);
611
612 ConstructorKind computeConstructorKind(ConstructorElement constructor) {
613 if (constructor.isRedirectingFactory) {
614 return ConstructorKind.REDIRECTING_FACTORY;
615 } else if (constructor.isFactoryConstructor) {
616 return ConstructorKind.FACTORY;
617 } else if (constructor.isRedirectingGenerative) {
618 return ConstructorKind.REDIRECTING_GENERATIVE;
619 } else {
620 return ConstructorKind.GENERATIVE;
621 }
622 }
623
624 DeclStructure computeFunctionStructure(FunctionExpression node) {
625 FunctionElement element = elements.getFunctionDefinition(node);
626 if (element.isConstructor) {
627 ConstructorElement constructor = element;
628 ConstructorKind kind = computeConstructorKind(constructor);
629 if (kind == ConstructorKind.REDIRECTING_FACTORY) {
630 return new RedirectingFactoryConstructorDeclStructure(
631 constructor,
632 elements.getType(node.body),
633 constructor.immediateRedirectionTarget);
634 } else {
635 return new ConstructorDeclStructure(kind, element);
636 }
637 } else {
638 FunctionKind kind;
639 if (element.isLocal) {
640 if (element.name.isEmpty) {
641 kind = FunctionKind.CLOSURE;
642 } else {
643 kind = FunctionKind.LOCAL_FUNCTION;
644 }
645 } else if (element.isInstanceMember) {
646 if (element.isGetter) {
647 kind = element.isAbstract
648 ? FunctionKind.ABSTRACT_GETTER
649 : FunctionKind.INSTANCE_GETTER;
650 } else if (element.isSetter) {
651 kind = element.isAbstract
652 ? FunctionKind.ABSTRACT_SETTER
653 : FunctionKind.INSTANCE_SETTER;
654 } else {
655 kind = element.isAbstract
656 ? FunctionKind.ABSTRACT_METHOD
657 : FunctionKind.INSTANCE_METHOD;
658 }
659 } else if (element.isStatic) {
660 if (element.isGetter) {
661 kind = FunctionKind.STATIC_GETTER;
662 } else if (element.isSetter) {
663 kind = FunctionKind.STATIC_SETTER;
664 } else {
665 kind = FunctionKind.STATIC_FUNCTION;
666 }
667 } else if (element.isTopLevel) {
668 if (element.isGetter) {
669 kind = FunctionKind.TOP_LEVEL_GETTER;
670 } else if (element.isSetter) {
671 kind = FunctionKind.TOP_LEVEL_SETTER;
672 } else {
673 kind = FunctionKind.TOP_LEVEL_FUNCTION;
674 }
675 } else {
676 return internalError(node, "Unhandled function expression.");
677 }
678 return new FunctionDeclStructure(kind, element);
679 }
680 }
681
682 InitializerStructure computeInitializerStructure(Send node) {
683 Element element = elements[node];
684 if (node.asSendSet() != null) {
685 return new FieldInitializerStructure(element);
686 } else if (Initializers.isConstructorRedirect(node)) {
687 return new ThisConstructorInvokeStructure(
688 element, elements.getSelector(node));
689 } else if (Initializers.isSuperConstructorCall(node)) {
690 return new SuperConstructorInvokeStructure(
691 element,
692 elements.analyzedElement.enclosingClass.supertype,
693 elements.getSelector(node));
694 }
695 return internalError(node, "Unhandled initializer.");
696 }
697
698 void computeParameterStructures(
699 NodeList parameters,
700 void callback(VariableDefinitions definitions,
701 ParameterStructure structure)) {
702 int index = 0;
703 for (Node node in parameters) {
704 NodeList optionalParameters = node.asNodeList();
705 if (optionalParameters != null) {
706 bool isNamed = optionalParameters.beginToken.stringValue == '{';
707 for (Node node in optionalParameters) {
708 callback(node,
709 computeParameterStructure(node, index++,
710 isRequired: false, isNamed: isNamed));
711 }
712 } else {
713 callback(node, computeParameterStructure(node, index++));
714 }
715 }
716 }
717
718 ParameterStructure computeParameterStructure(
719 VariableDefinitions definitions,
720 int index,
721 {bool isRequired: true, bool isNamed: false}) {
722 Node node = definitions.definitions.nodes.single;
723 ParameterElement element = elements[node];
724 if (element == null) {
725 throw new SpannableAssertionFailure(
726 node, "No parameter structure for $node.");
727 }
728 if (isRequired) {
729 return new RequiredParameterStructure(node, element, index);
730 } else {
731 ConstantExpression defaultValue;
732 if (element.initializer != null) {
733 defaultValue = elements.getConstant(element.initializer);
734 }
735 if (isNamed) {
736 return new NamedParameterStructure(node, element, defaultValue);
737 } else {
738 return new OptionalParameterStructure(
739 node, element, defaultValue, index);
740 }
741 }
742 }
743
744 void computeVariableStructures(
745 VariableDefinitions definitions,
746 void callback(Node node, VariableStructure structure)) {
747 for (Node node in definitions.definitions) {
748 callback(definitions, computeVariableStructure(node));
749 }
750 }
751
752 VariableStructure computeVariableStructure(Node node) {
753 VariableElement element = elements[node];
754 VariableKind kind;
755 if (element.isLocal) {
756 kind = VariableKind.LOCAL_VARIABLE;
757 } else if (element.isInstanceMember) {
758 kind = VariableKind.INSTANCE_FIELD;
759 } else if (element.isStatic) {
760 kind = VariableKind.STATIC_FIELD;
761 } else if (element.isTopLevel) {
762 kind = VariableKind.TOP_LEVEL_FIELD;
763 } else {
764 return internalError(node, "Unexpected variable $element.");
765 }
766 if (element.isConst) {
767 ConstantExpression constant = elements.getConstant(element.initializer);
768 return new ConstantVariableStructure(kind, node, element, constant);
769 } else {
770 return new NonConstantVariableStructure(kind, node, element);
771 }
772 }
773 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698