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

Side by Side Diff: pkg/analyzer/lib/src/dart/ast/utilities.dart

Issue 1918923003: Remove unnecessary casts and general code clean-up (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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
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.ast.utilities; 5 library analyzer.src.dart.ast.utilities;
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 915 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 * into [_clonedTokens]. 926 * into [_clonedTokens].
927 * 927 *
928 * We cannot clone tokens as we visit nodes because not every token is a part 928 * We cannot clone tokens as we visit nodes because not every token is a part
929 * of a node, E.g. commas in argument lists are not represented in AST. But 929 * of a node, E.g. commas in argument lists are not represented in AST. But
930 * we need to the sequence of tokens that is identical to the original one. 930 * we need to the sequence of tokens that is identical to the original one.
931 */ 931 */
932 void _cloneTokens(Token token, int stopAfter) { 932 void _cloneTokens(Token token, int stopAfter) {
933 if (token == null) { 933 if (token == null) {
934 return; 934 return;
935 } 935 }
936 if (token is CommentToken) { 936 Token nonComment(Token token) {
937 token = (token as CommentToken).parent; 937 return token is CommentToken ? token.parent : token;
938 } 938 }
939 token = nonComment(token);
939 if (_lastCloned == null) { 940 if (_lastCloned == null) {
940 _lastCloned = new Token(TokenType.EOF, -1); 941 _lastCloned = new Token(TokenType.EOF, -1);
941 _lastCloned.setNext(_lastCloned); 942 _lastCloned.setNext(_lastCloned);
942 } 943 }
943 while (token != null) { 944 while (token != null) {
944 Token clone = token.copy(); 945 Token clone = token.copy();
945 { 946 {
946 CommentToken c1 = token.precedingComments; 947 CommentToken c1 = token.precedingComments;
947 CommentToken c2 = clone.precedingComments; 948 CommentToken c2 = clone.precedingComments;
948 while (c1 != null && c2 != null) { 949 while (c1 != null && c2 != null) {
(...skipping 1312 matching lines...) Expand 10 before | Expand all | Expand 10 after
2261 } else if (node.operator.type == TokenType.SLASH) { 2262 } else if (node.operator.type == TokenType.SLASH) {
2262 // numeric or {@code null} 2263 // numeric or {@code null}
2263 if (leftOperand is num && rightOperand is num) { 2264 if (leftOperand is num && rightOperand is num) {
2264 return leftOperand / rightOperand; 2265 return leftOperand / rightOperand;
2265 } 2266 }
2266 } else if (node.operator.type == TokenType.TILDE_SLASH) { 2267 } else if (node.operator.type == TokenType.TILDE_SLASH) {
2267 // numeric or {@code null} 2268 // numeric or {@code null}
2268 if (leftOperand is num && rightOperand is num) { 2269 if (leftOperand is num && rightOperand is num) {
2269 return leftOperand ~/ rightOperand; 2270 return leftOperand ~/ rightOperand;
2270 } 2271 }
2271 } else {} 2272 }
2272 break; 2273 break;
2273 } 2274 }
2274 // TODO(brianwilkerson) This doesn't handle numeric conversions. 2275 // TODO(brianwilkerson) This doesn't handle numeric conversions.
2275 return visitExpression(node); 2276 return visitExpression(node);
2276 } 2277 }
2277 2278
2278 @override 2279 @override
2279 Object visitBooleanLiteral(BooleanLiteral node) => node.value ? true : false; 2280 Object visitBooleanLiteral(BooleanLiteral node) => node.value ? true : false;
2280 2281
2281 @override 2282 @override
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
2404 } 2405 }
2405 return buffer.toString(); 2406 return buffer.toString();
2406 } 2407 }
2407 2408
2408 /** 2409 /**
2409 * Return the constant value of the static constant represented by the given 2410 * Return the constant value of the static constant represented by the given
2410 * [element]. 2411 * [element].
2411 */ 2412 */
2412 Object _getConstantValue(Element element) { 2413 Object _getConstantValue(Element element) {
2413 // TODO(brianwilkerson) Implement this 2414 // TODO(brianwilkerson) Implement this
2414 if (element is FieldElement) { 2415 // if (element is FieldElement) {
2415 FieldElement field = element; 2416 // FieldElement field = element;
2416 if (field.isStatic && field.isConst) { 2417 // if (field.isStatic && field.isConst) {
2417 //field.getConstantValue(); 2418 // //field.getConstantValue();
2418 } 2419 // }
2419 // } else if (element instanceof VariableElement) { 2420 // // } else if (element instanceof VariableElement) {
2420 // VariableElement variable = (VariableElement) element; 2421 // // VariableElement variable = (VariableElement) element;
2421 // if (variable.isStatic() && variable.isConst()) { 2422 // // if (variable.isStatic() && variable.isConst()) {
2422 // //variable.getConstantValue(); 2423 // // //variable.getConstantValue();
2423 // } 2424 // // }
2424 } 2425 // }
2425 return NOT_A_CONSTANT; 2426 return NOT_A_CONSTANT;
2426 } 2427 }
2427 } 2428 }
2428 2429
2429 /** 2430 /**
2430 * A recursive AST visitor that is used to run over [Expression]s to determine 2431 * A recursive AST visitor that is used to run over [Expression]s to determine
2431 * whether the expression is composed by at least one deferred 2432 * whether the expression is composed by at least one deferred
2432 * [PrefixedIdentifier]. 2433 * [PrefixedIdentifier].
2433 * 2434 *
2434 * See [PrefixedIdentifier.isDeferred]. 2435 * See [PrefixedIdentifier.isDeferred].
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2497 @override 2498 @override
2498 Element visitConstructorDeclaration(ConstructorDeclaration node) => 2499 Element visitConstructorDeclaration(ConstructorDeclaration node) =>
2499 node.element; 2500 node.element;
2500 2501
2501 @override 2502 @override
2502 Element visitFunctionDeclaration(FunctionDeclaration node) => node.element; 2503 Element visitFunctionDeclaration(FunctionDeclaration node) => node.element;
2503 2504
2504 @override 2505 @override
2505 Element visitIdentifier(Identifier node) { 2506 Element visitIdentifier(Identifier node) {
2506 AstNode parent = node.parent; 2507 AstNode parent = node.parent;
2507 // Type name in Annotation
2508 if (parent is Annotation) { 2508 if (parent is Annotation) {
2509 Annotation annotation = parent; 2509 // Type name in Annotation
2510 if (identical(annotation.name, node) && 2510 if (identical(parent.name, node) && parent.constructorName == null) {
2511 annotation.constructorName == null) { 2511 return parent.element;
2512 return annotation.element;
2513 } 2512 }
2514 } 2513 } else if (parent is ConstructorDeclaration) {
2515 // Extra work to map Constructor Declarations to their associated 2514 // Extra work to map Constructor Declarations to their associated
2516 // Constructor Elements 2515 // Constructor Elements
2517 if (parent is ConstructorDeclaration) {
2518 Identifier returnType = parent.returnType; 2516 Identifier returnType = parent.returnType;
2519 if (identical(returnType, node)) { 2517 if (identical(returnType, node)) {
2520 SimpleIdentifier name = parent.name; 2518 SimpleIdentifier name = parent.name;
2521 if (name != null) { 2519 if (name != null) {
2522 return name.bestElement; 2520 return name.bestElement;
2523 } 2521 }
2524 Element element = node.bestElement; 2522 Element element = node.bestElement;
2525 if (element is ClassElement) { 2523 if (element is ClassElement) {
2526 return element.unnamedConstructor; 2524 return element.unnamedConstructor;
2527 } 2525 }
2528 } 2526 }
2529 } 2527 } else if (parent is LibraryIdentifier) {
2530 if (parent is LibraryIdentifier) {
2531 AstNode grandParent = parent.parent; 2528 AstNode grandParent = parent.parent;
2532 if (grandParent is PartOfDirective) { 2529 if (grandParent is PartOfDirective) {
2533 Element element = grandParent.element; 2530 Element element = grandParent.element;
2534 if (element is LibraryElement) { 2531 if (element is LibraryElement) {
2535 return element.definingCompilationUnit; 2532 return element.definingCompilationUnit;
2536 } 2533 }
2537 } 2534 }
2538 } 2535 }
2539 return node.bestElement; 2536 return node.bestElement;
2540 } 2537 }
(...skipping 5244 matching lines...) Expand 10 before | Expand all | Expand 10 after
7785 * Safely visit the given [token], printing the [suffix] after the token if it 7782 * Safely visit the given [token], printing the [suffix] after the token if it
7786 * is non-`null`. 7783 * is non-`null`.
7787 */ 7784 */
7788 void _visitTokenWithSuffix(Token token, String suffix) { 7785 void _visitTokenWithSuffix(Token token, String suffix) {
7789 if (token != null) { 7786 if (token != null) {
7790 _writer.print(token.lexeme); 7787 _writer.print(token.lexeme);
7791 _writer.print(suffix); 7788 _writer.print(suffix);
7792 } 7789 }
7793 } 7790 }
7794 } 7791 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698