OLD | NEW |
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 package com.google.dart.compiler.resolver; | 5 package com.google.dart.compiler.resolver; |
6 | 6 |
7 import com.google.common.collect.Maps; | 7 import com.google.common.collect.Maps; |
8 import com.google.common.collect.Sets; | 8 import com.google.common.collect.Sets; |
9 import com.google.dart.compiler.DartCompilationError; | 9 import com.google.dart.compiler.DartCompilationError; |
10 import com.google.dart.compiler.DartCompilationPhase; | 10 import com.google.dart.compiler.DartCompilationPhase; |
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 return null; | 615 return null; |
616 } | 616 } |
617 return super.visitArrayLiteral(node); | 617 return super.visitArrayLiteral(node); |
618 } | 618 } |
619 | 619 |
620 @Override | 620 @Override |
621 public Void visitField(DartField node) { | 621 public Void visitField(DartField node) { |
622 if (node.getParent() != null) { | 622 if (node.getParent() != null) { |
623 DartNode pp = node.getParent().getParent(); | 623 DartNode pp = node.getParent().getParent(); |
624 boolean isFinalTopLevelField = node.getModifiers().isFinal() && pp insta
nceof DartUnit; | 624 boolean isFinalTopLevelField = node.getModifiers().isFinal() && pp insta
nceof DartUnit; |
625 boolean isInstanceField = pp instanceof DartClass; | 625 boolean isClassField = pp instanceof DartClass; |
626 if (isFinalTopLevelField || isInstanceField) { | 626 boolean isStatic = node.getModifiers().isStatic(); |
| 627 boolean isConst = node.getModifiers().isConstant(); |
| 628 if (isFinalTopLevelField || (isClassField && isStatic) || isConst) { |
627 Type type = checkConstantExpression(node.getValue()); | 629 Type type = checkConstantExpression(node.getValue()); |
628 if (node.getElement().getType().equals(dynamicType)) { | 630 if (node.getElement().getType().equals(dynamicType)) { |
629 node.getElement().setConstantType(type); | 631 node.getElement().setConstantType(type); |
630 } | 632 } |
631 return null; | 633 return null; |
632 } | 634 } |
| 635 if (isClassField && !isStatic) { |
| 636 DartExpression value = node.getValue(); |
| 637 checkInstanceFieldInitializer(value); |
| 638 } |
633 } | 639 } |
634 return super.visitField(node); | 640 return super.visitField(node); |
635 } | 641 } |
636 | 642 |
637 @Override | 643 @Override |
638 public Void visitClass(DartClass node) { | 644 public Void visitClass(DartClass node) { |
639 ClassElement oldClassElement = currentClass; | 645 ClassElement oldClassElement = currentClass; |
640 currentClass = node.getElement(); | 646 currentClass = node.getElement(); |
641 try { | 647 try { |
642 return super.visitClass(node); | 648 return super.visitClass(node); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 | 775 |
770 private Type checkConstantExpression(DartExpression expression) { | 776 private Type checkConstantExpression(DartExpression expression) { |
771 if (expression != null) { | 777 if (expression != null) { |
772 ExpressionVisitor visitor = new ExpressionVisitor(); | 778 ExpressionVisitor visitor = new ExpressionVisitor(); |
773 expression.accept(visitor); | 779 expression.accept(visitor); |
774 return visitor.getMostSpecificType(expression); | 780 return visitor.getMostSpecificType(expression); |
775 } | 781 } |
776 return null; | 782 return null; |
777 } | 783 } |
778 | 784 |
779 public void exec (DartUnit unit) { | 785 public void exec(DartUnit unit) { |
780 unit.accept(new FindCompileTimeConstantExpressionsVisitor()); | 786 unit.accept(new FindCompileTimeConstantExpressionsVisitor()); |
781 } | 787 } |
| 788 |
| 789 private void checkInstanceFieldInitializer(DartExpression value) { |
| 790 if (value != null) { |
| 791 value.accept(new ASTVisitor<Void>() { |
| 792 @Override |
| 793 public Void visitThisExpression(DartThisExpression node) { |
| 794 context.onError(new DartCompilationError(node, |
| 795 ResolverErrorCode.CANNOT_USE_THIS_IN_INSTANCE_FIELD_INITIALIZER)); |
| 796 return null; |
| 797 } |
| 798 @Override |
| 799 public Void visitIdentifier(DartIdentifier node) { |
| 800 NodeElement element = node.getElement(); |
| 801 if (ElementKind.of(element) == ElementKind.FIELD) { |
| 802 FieldElement fieldElement = (FieldElement) element; |
| 803 if (!fieldElement.isStatic()) { |
| 804 context.onError(new DartCompilationError(node, |
| 805 ResolverErrorCode.CANNOT_USE_INSTANCE_FIELD_IN_INSTANCE_FIELD_
INITIALIZER)); |
| 806 } |
| 807 } |
| 808 return null; |
| 809 } |
| 810 }); |
| 811 } |
| 812 } |
782 } | 813 } |
OLD | NEW |