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

Side by Side Diff: pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Issue 2828693003: Add local type inference logic for integer literals. (Closed)
Patch Set: Sort declarations Created 3 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
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 fasta.body_builder; 5 library fasta.body_builder;
6 6
7 import '../fasta_codes.dart' 7 import '../fasta_codes.dart'
8 show FastaMessage, codeExpectedButGot, codeExpectedFunctionBody; 8 show FastaMessage, codeExpectedButGot, codeExpectedFunctionBody;
9 9
10 import '../parser/parser.dart' show FormalParameterType, optional; 10 import '../parser/parser.dart' show FormalParameterType, optional;
11 11
12 import '../parser/identifier_context.dart' show IdentifierContext; 12 import '../parser/identifier_context.dart' show IdentifierContext;
13 13
14 import 'package:front_end/src/fasta/builder/ast_factory.dart' show AstFactory;
15
14 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' 16 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart'
15 show KernelVariableDeclaration; 17 show KernelVariableDeclaration;
16 18
17 import 'package:front_end/src/fasta/type_inference/type_inferrer.dart' 19 import 'package:front_end/src/fasta/type_inference/type_inferrer.dart'
18 show TypeInferrer; 20 show TypeInferrer;
19 21
20 import 'package:kernel/ast.dart'; 22 import 'package:kernel/ast.dart';
21 23
22 import 'package:kernel/clone.dart' show CloneVisitor; 24 import 'package:kernel/clone.dart' show CloneVisitor;
23 25
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 final Scope enclosingScope; 83 final Scope enclosingScope;
82 84
83 final bool isDartLibrary; 85 final bool isDartLibrary;
84 86
85 @override 87 @override
86 final Uri uri; 88 final Uri uri;
87 89
88 final TypeInferrer<Statement, Expression, KernelVariableDeclaration, Field> 90 final TypeInferrer<Statement, Expression, KernelVariableDeclaration, Field>
89 _typeInferrer; 91 _typeInferrer;
90 92
93 final AstFactory astFactory;
94
91 Scope formalParameterScope; 95 Scope formalParameterScope;
92 96
93 bool inInitializer = false; 97 bool inInitializer = false;
94 98
95 bool inCatchClause = false; 99 bool inCatchClause = false;
96 100
97 bool inCatchBlock = false; 101 bool inCatchBlock = false;
98 102
99 int functionNestingLevel = 0; 103 int functionNestingLevel = 0;
100 104
(...skipping 16 matching lines...) Expand all
117 BodyBuilder( 121 BodyBuilder(
118 KernelLibraryBuilder library, 122 KernelLibraryBuilder library,
119 this.member, 123 this.member,
120 Scope scope, 124 Scope scope,
121 this.formalParameterScope, 125 this.formalParameterScope,
122 this.hierarchy, 126 this.hierarchy,
123 this.coreTypes, 127 this.coreTypes,
124 this.classBuilder, 128 this.classBuilder,
125 this.isInstanceMember, 129 this.isInstanceMember,
126 this.uri, 130 this.uri,
127 this._typeInferrer) 131 this._typeInferrer,
132 this.astFactory)
128 : enclosingScope = scope, 133 : enclosingScope = scope,
129 library = library, 134 library = library,
130 isDartLibrary = library.uri.scheme == "dart", 135 isDartLibrary = library.uri.scheme == "dart",
131 super(scope); 136 super(scope);
132 137
133 bool get hasParserError => recoverableErrors.isNotEmpty; 138 bool get hasParserError => recoverableErrors.isNotEmpty;
134 139
135 bool get inConstructor { 140 bool get inConstructor {
136 return functionNestingLevel == 0 && member is KernelConstructorBuilder; 141 return functionNestingLevel == 0 && member is KernelConstructorBuilder;
137 } 142 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 var statement = statements[i]; 221 var statement = statements[i];
217 if (statement is List) { 222 if (statement is List) {
218 copy ??= new List<Statement>.from(statements.getRange(0, i)); 223 copy ??= new List<Statement>.from(statements.getRange(0, i));
219 // TODO(sigmund): remove this assignment (issue #28651) 224 // TODO(sigmund): remove this assignment (issue #28651)
220 Iterable subStatements = statement; 225 Iterable subStatements = statement;
221 copy.addAll(subStatements); 226 copy.addAll(subStatements);
222 } else if (copy != null) { 227 } else if (copy != null) {
223 copy.add(statement); 228 copy.add(statement);
224 } 229 }
225 } 230 }
226 return new Block(copy ?? statements)..fileOffset = charOffset; 231 return astFactory.block(copy ?? statements, charOffset);
227 } 232 }
228 233
229 Statement popStatementIfNotNull(Object value) { 234 Statement popStatementIfNotNull(Object value) {
230 return value == null ? null : popStatement(); 235 return value == null ? null : popStatement();
231 } 236 }
232 237
233 Statement popStatement() { 238 Statement popStatement() {
234 var statement = pop(); 239 var statement = pop();
235 if (statement is List) { 240 if (statement is List) {
236 return new Block(new List<Statement>.from(statement)); 241 return new Block(new List<Statement>.from(statement));
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 if (asyncModifier != AsyncMarker.Sync) { 433 if (asyncModifier != AsyncMarker.Sync) {
429 // TODO(ahe): Change this to a null check. 434 // TODO(ahe): Change this to a null check.
430 addCompileTimeError(body?.fileOffset, 435 addCompileTimeError(body?.fileOffset,
431 "Can't be marked as ${asyncModifier}: ${builder.name}"); 436 "Can't be marked as ${asyncModifier}: ${builder.name}");
432 } 437 }
433 } else if (builder is KernelProcedureBuilder) { 438 } else if (builder is KernelProcedureBuilder) {
434 builder.asyncModifier = asyncModifier; 439 builder.asyncModifier = asyncModifier;
435 } else { 440 } else {
436 internalError("Unhandled: ${builder.runtimeType}"); 441 internalError("Unhandled: ${builder.runtimeType}");
437 } 442 }
438 _typeInferrer.inferBody(body, uri); 443 _typeInferrer?.inferBody(body, uri);
439 builder.body = body; 444 builder.body = body;
440 if (formals?.optional != null) { 445 if (formals?.optional != null) {
441 Iterator<FormalParameterBuilder> formalBuilders = 446 Iterator<FormalParameterBuilder> formalBuilders =
442 builder.formals.skip(formals.required.length).iterator; 447 builder.formals.skip(formals.required.length).iterator;
443 for (VariableDeclaration parameter in formals.optional.formals) { 448 for (VariableDeclaration parameter in formals.optional.formals) {
444 bool hasMore = formalBuilders.moveNext(); 449 bool hasMore = formalBuilders.moveNext();
445 assert(hasMore); 450 assert(hasMore);
446 VariableDeclaration realParameter = formalBuilders.current.target; 451 VariableDeclaration realParameter = formalBuilders.current.target;
447 Expression initializer = parameter.initializer ?? new NullLiteral(); 452 Expression initializer = parameter.initializer ?? new NullLiteral();
448 realParameter.initializer = initializer..parent = realParameter; 453 realParameter.initializer = initializer..parent = realParameter;
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
913 expressions.add(part); 918 expressions.add(part);
914 } 919 }
915 } 920 }
916 } 921 }
917 push(new StringConcatenation(expressions ?? parts)); 922 push(new StringConcatenation(expressions ?? parts));
918 } 923 }
919 924
920 @override 925 @override
921 void handleLiteralInt(Token token) { 926 void handleLiteralInt(Token token) {
922 debugEvent("LiteralInt"); 927 debugEvent("LiteralInt");
923 push( 928 push(astFactory.intLiteral(int.parse(token.lexeme), token.charOffset));
924 new IntLiteral(int.parse(token.lexeme))..fileOffset = token.charOffset);
925 } 929 }
926 930
927 @override 931 @override
928 void handleEmptyFunctionBody(Token semicolon) { 932 void handleEmptyFunctionBody(Token semicolon) {
929 debugEvent("ExpressionFunctionBody"); 933 debugEvent("ExpressionFunctionBody");
930 endBlockFunctionBody(0, null, semicolon); 934 endBlockFunctionBody(0, null, semicolon);
931 } 935 }
932 936
933 @override 937 @override
934 void handleExpressionFunctionBody(Token arrowToken, Token endToken) { 938 void handleExpressionFunctionBody(Token arrowToken, Token endToken) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 pushNewLocalVariable(null); 975 pushNewLocalVariable(null);
972 } 976 }
973 977
974 void pushNewLocalVariable(Expression initializer, 978 void pushNewLocalVariable(Expression initializer,
975 {int equalsCharOffset: TreeNode.noOffset}) { 979 {int equalsCharOffset: TreeNode.noOffset}) {
976 Identifier identifier = pop(); 980 Identifier identifier = pop();
977 assert(currentLocalVariableModifiers != -1); 981 assert(currentLocalVariableModifiers != -1);
978 bool isConst = (currentLocalVariableModifiers & constMask) != 0; 982 bool isConst = (currentLocalVariableModifiers & constMask) != 0;
979 bool isFinal = (currentLocalVariableModifiers & finalMask) != 0; 983 bool isFinal = (currentLocalVariableModifiers & finalMask) != 0;
980 assert(isConst == constantExpressionRequired); 984 assert(isConst == constantExpressionRequired);
981 push(new VariableDeclaration(identifier.name, 985 push(astFactory.variableDeclaration(identifier.name,
982 initializer: initializer, 986 initializer: initializer,
983 type: currentLocalVariableType ?? const DynamicType(), 987 type: currentLocalVariableType,
984 isFinal: isFinal, 988 isFinal: isFinal,
985 isConst: isConst) 989 isConst: isConst,
986 ..fileEqualsOffset = equalsCharOffset); 990 equalsCharOffset: equalsCharOffset));
987 } 991 }
988 992
989 @override 993 @override
990 void endFieldInitializer(Token assignmentOperator) { 994 void endFieldInitializer(Token assignmentOperator) {
991 debugEvent("FieldInitializer"); 995 debugEvent("FieldInitializer");
992 assert(assignmentOperator.stringValue == "="); 996 assert(assignmentOperator.stringValue == "=");
993 push(popForValue()); 997 push(popForValue());
994 } 998 }
995 999
996 @override 1000 @override
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 } else if (thisKeyword == null) { 1434 } else if (thisKeyword == null) {
1431 variable = builder.build(library); 1435 variable = builder.build(library);
1432 variable.initializer = name.initializer; 1436 variable.initializer = name.initializer;
1433 } else if (builder.isField && builder.parent == classBuilder) { 1437 } else if (builder.isField && builder.parent == classBuilder) {
1434 FieldBuilder field = builder; 1438 FieldBuilder field = builder;
1435 if (type != null) { 1439 if (type != null) {
1436 nit("Ignoring type on 'this' parameter '${name.name}'.", 1440 nit("Ignoring type on 'this' parameter '${name.name}'.",
1437 thisKeyword.charOffset); 1441 thisKeyword.charOffset);
1438 } 1442 }
1439 type = field.target.type ?? const DynamicType(); 1443 type = field.target.type ?? const DynamicType();
1440 variable = new VariableDeclaration(name.name, 1444 variable = astFactory.variableDeclaration(name.name,
1441 type: type, 1445 type: type,
1442 initializer: name.initializer, 1446 initializer: name.initializer,
1443 isFinal: isFinal, 1447 isFinal: isFinal,
1444 isConst: isConst) 1448 isConst: isConst,
1445 ..fileOffset = name.fileOffset; 1449 charOffset: name.fileOffset);
1446 } else { 1450 } else {
1447 addCompileTimeError( 1451 addCompileTimeError(
1448 name.fileOffset, "'${name.name}' isn't a field in this class."); 1452 name.fileOffset, "'${name.name}' isn't a field in this class.");
1449 } 1453 }
1450 } 1454 }
1451 variable ??= new VariableDeclaration(name.name, 1455 variable ??= astFactory.variableDeclaration(name.name,
1452 type: type ?? const DynamicType(), 1456 type: type ?? const DynamicType(),
1453 initializer: name.initializer, 1457 initializer: name.initializer,
1454 isFinal: isFinal, 1458 isFinal: isFinal,
1455 isConst: isConst) 1459 isConst: isConst,
1456 ..fileOffset = name.fileOffset; 1460 charOffset: name.fileOffset);
1457 push(variable); 1461 push(variable);
1458 } 1462 }
1459 1463
1460 @override 1464 @override
1461 void endOptionalFormalParameters( 1465 void endOptionalFormalParameters(
1462 int count, Token beginToken, Token endToken) { 1466 int count, Token beginToken, Token endToken) {
1463 debugEvent("OptionalFormalParameters"); 1467 debugEvent("OptionalFormalParameters");
1464 FormalParameterType kind = optional("{", beginToken) 1468 FormalParameterType kind = optional("{", beginToken)
1465 ? FormalParameterType.NAMED 1469 ? FormalParameterType.NAMED
1466 : FormalParameterType.POSITIONAL; 1470 : FormalParameterType.POSITIONAL;
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
1905 Expression value = popForValue(); 1909 Expression value = popForValue();
1906 Identifier identifier = pop(); 1910 Identifier identifier = pop();
1907 push(new NamedExpression(identifier.name, value)); 1911 push(new NamedExpression(identifier.name, value));
1908 } 1912 }
1909 1913
1910 @override 1914 @override
1911 void endFunctionName(Token beginToken, Token token) { 1915 void endFunctionName(Token beginToken, Token token) {
1912 debugEvent("FunctionName"); 1916 debugEvent("FunctionName");
1913 Identifier name = pop(); 1917 Identifier name = pop();
1914 VariableDeclaration variable = 1918 VariableDeclaration variable =
1915 new VariableDeclaration(name.name, isFinal: true); 1919 astFactory.variableDeclaration(name.name, isFinal: true);
1916 push(new FunctionDeclaration( 1920 push(new FunctionDeclaration(
1917 variable, new FunctionNode(new InvalidStatement())) 1921 variable, new FunctionNode(new InvalidStatement()))
1918 ..fileOffset = beginToken.charOffset); 1922 ..fileOffset = beginToken.charOffset);
1919 scope[variable.name] = new KernelVariableBuilder( 1923 scope[variable.name] = new KernelVariableBuilder(
1920 variable, member ?? classBuilder ?? library, uri); 1924 variable, member ?? classBuilder ?? library, uri);
1921 enterLocalScope(); 1925 enterLocalScope();
1922 } 1926 }
1923 1927
1924 void enterFunction() { 1928 void enterFunction() {
1925 debugEvent("enterFunction"); 1929 debugEvent("enterFunction");
(...skipping 1039 matching lines...) Expand 10 before | Expand all | Expand 10 after
2965 } else if (node is PrefixBuilder) { 2969 } else if (node is PrefixBuilder) {
2966 return node.name; 2970 return node.name;
2967 } else if (node is ThisAccessor) { 2971 } else if (node is ThisAccessor) {
2968 return node.isSuper ? "super" : "this"; 2972 return node.isSuper ? "super" : "this";
2969 } else if (node is FastaAccessor) { 2973 } else if (node is FastaAccessor) {
2970 return node.plainNameForRead; 2974 return node.plainNameForRead;
2971 } else { 2975 } else {
2972 return internalError("Unhandled: ${node.runtimeType}"); 2976 return internalError("Unhandled: ${node.runtimeType}");
2973 } 2977 }
2974 } 2978 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698