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

Side by Side Diff: pkg/analyzer/lib/src/dart/element/builder.dart

Issue 3008453002: Build / resynthesize final fields as ConstFieldElementImpl only if the enclosing class has a consta… (Closed)
Patch Set: Created 3 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
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/dart/element/element.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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 library analyzer.src.dart.element.builder; 5 library analyzer.src.dart.element.builder;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
(...skipping 17 matching lines...) Expand all
28 * build elements outside of function bodies and initializers. 28 * build elements outside of function bodies and initializers.
29 */ 29 */
30 class ApiElementBuilder extends _BaseElementBuilder { 30 class ApiElementBuilder extends _BaseElementBuilder {
31 /** 31 /**
32 * A table mapping field names to field elements for the fields defined in the current class, or 32 * A table mapping field names to field elements for the fields defined in the current class, or
33 * `null` if we are not in the scope of a class. 33 * `null` if we are not in the scope of a class.
34 */ 34 */
35 HashMap<String, FieldElement> _fieldMap; 35 HashMap<String, FieldElement> _fieldMap;
36 36
37 /** 37 /**
38 * Whether the class being built has a constant constructor.
39 */
40 bool _enclosingClassHasConstConstructor = false;
41
42 /**
38 * Initialize a newly created element builder to build the elements for a 43 * Initialize a newly created element builder to build the elements for a
39 * compilation unit. The [initialHolder] is the element holder to which the 44 * compilation unit. The [initialHolder] is the element holder to which the
40 * children of the visited compilation unit node will be added. 45 * children of the visited compilation unit node will be added.
41 */ 46 */
42 ApiElementBuilder(ElementHolder initialHolder, 47 ApiElementBuilder(ElementHolder initialHolder,
43 CompilationUnitElementImpl compilationUnitElement) 48 CompilationUnitElementImpl compilationUnitElement)
44 : super(initialHolder, compilationUnitElement); 49 : super(initialHolder, compilationUnitElement);
45 50
46 @override 51 @override
47 Object visitAnnotation(Annotation node) { 52 Object visitAnnotation(Annotation node) {
(...skipping 13 matching lines...) Expand all
61 return null; 66 return null;
62 } 67 }
63 68
64 @override 69 @override
65 Object visitBlockFunctionBody(BlockFunctionBody node) { 70 Object visitBlockFunctionBody(BlockFunctionBody node) {
66 return null; 71 return null;
67 } 72 }
68 73
69 @override 74 @override
70 Object visitClassDeclaration(ClassDeclaration node) { 75 Object visitClassDeclaration(ClassDeclaration node) {
76 _enclosingClassHasConstConstructor = false;
77 for (var constructor in node.members) {
78 if (constructor is ConstructorDeclaration &&
79 constructor.constKeyword != null) {
80 _enclosingClassHasConstConstructor = true;
81 break;
82 }
83 }
84
71 ElementHolder holder = new ElementHolder(); 85 ElementHolder holder = new ElementHolder();
72 // 86 //
73 // Process field declarations before constructors and methods so that field 87 // Process field declarations before constructors and methods so that field
74 // formal parameters can be correctly resolved to their fields. 88 // formal parameters can be correctly resolved to their fields.
75 // 89 //
76 ElementHolder previousHolder = _currentHolder; 90 ElementHolder previousHolder = _currentHolder;
77 _currentHolder = holder; 91 _currentHolder = holder;
78 try { 92 try {
79 List<ClassMember> nonFields = new List<ClassMember>(); 93 List<ClassMember> nonFields = new List<ClassMember>();
80 node.visitChildren( 94 node.visitChildren(
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 bool isFinal = node.isFinal; 613 bool isFinal = node.isFinal;
600 Expression initializerNode = node.initializer; 614 Expression initializerNode = node.initializer;
601 bool hasInitializer = initializerNode != null; 615 bool hasInitializer = initializerNode != null;
602 VariableDeclarationList varList = node.parent; 616 VariableDeclarationList varList = node.parent;
603 FieldDeclaration fieldNode = 617 FieldDeclaration fieldNode =
604 varList.parent is FieldDeclaration ? varList.parent : null; 618 varList.parent is FieldDeclaration ? varList.parent : null;
605 VariableElementImpl element; 619 VariableElementImpl element;
606 if (fieldNode != null) { 620 if (fieldNode != null) {
607 SimpleIdentifier fieldName = node.name; 621 SimpleIdentifier fieldName = node.name;
608 FieldElementImpl field; 622 FieldElementImpl field;
609 if ((isConst || isFinal && !fieldNode.isStatic) && hasInitializer) { 623 if ((isConst ||
624 isFinal &&
625 !fieldNode.isStatic &&
626 _enclosingClassHasConstConstructor) &&
627 hasInitializer) {
610 field = new ConstFieldElementImpl.forNode(fieldName); 628 field = new ConstFieldElementImpl.forNode(fieldName);
611 } else { 629 } else {
612 field = new FieldElementImpl.forNode(fieldName); 630 field = new FieldElementImpl.forNode(fieldName);
613 } 631 }
614 element = field; 632 element = field;
615 field.isCovariant = fieldNode.covariantKeyword != null; 633 field.isCovariant = fieldNode.covariantKeyword != null;
616 field.isStatic = fieldNode.isStatic; 634 field.isStatic = fieldNode.isStatic;
617 _setCodeRange(element, node); 635 _setCodeRange(element, node);
618 setElementDocumentationComment(element, fieldNode); 636 setElementDocumentationComment(element, fieldNode);
619 field.hasImplicitType = varList.type == null; 637 field.hasImplicitType = varList.type == null;
(...skipping 1048 matching lines...) Expand 10 before | Expand all | Expand 10 after
1668 return null; 1686 return null;
1669 } 1687 }
1670 1688
1671 /** 1689 /**
1672 * Return the lexical identifiers associated with the given [identifiers]. 1690 * Return the lexical identifiers associated with the given [identifiers].
1673 */ 1691 */
1674 static List<String> _getIdentifiers(NodeList<SimpleIdentifier> identifiers) { 1692 static List<String> _getIdentifiers(NodeList<SimpleIdentifier> identifiers) {
1675 return identifiers.map((identifier) => identifier.name).toList(); 1693 return identifiers.map((identifier) => identifier.name).toList();
1676 } 1694 }
1677 } 1695 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/dart/element/element.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698