OLD | NEW |
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, MemberKind, 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; | 14 import 'package:front_end/src/fasta/builder/ast_factory.dart' show AstFactory; |
15 | 15 |
16 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' | 16 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' |
17 show | 17 show |
18 KernelArguments, | 18 KernelArguments, |
19 KernelField, | 19 KernelField, |
20 KernelFunctionDeclaration, | 20 KernelFunctionDeclaration, |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 @override | 330 @override |
331 void endTopLevelFields(int count, Token beginToken, Token endToken) { | 331 void endTopLevelFields(int count, Token beginToken, Token endToken) { |
332 debugEvent("TopLevelFields"); | 332 debugEvent("TopLevelFields"); |
333 doFields(count); | 333 doFields(count); |
334 // There's no metadata here because of a slight asymmetry between | 334 // There's no metadata here because of a slight asymmetry between |
335 // [parseTopLevelMember] and [parseMember]. This asymmetry leads to | 335 // [parseTopLevelMember] and [parseMember]. This asymmetry leads to |
336 // DietListener discarding top-level member metadata. | 336 // DietListener discarding top-level member metadata. |
337 } | 337 } |
338 | 338 |
339 @override | 339 @override |
340 void endFields( | 340 void endFields(int count, Token beginToken, Token endToken) { |
341 int count, Token covariantKeyword, Token beginToken, Token endToken) { | |
342 debugEvent("Fields"); | 341 debugEvent("Fields"); |
343 doFields(count); | 342 doFields(count); |
344 pop(); // Metadata. | 343 pop(); // Metadata. |
345 } | 344 } |
346 | 345 |
347 void doFields(int count) { | 346 void doFields(int count) { |
348 for (int i = 0; i < count; i++) { | 347 for (int i = 0; i < count; i++) { |
349 Expression initializer = pop(); | 348 Expression initializer = pop(); |
350 Identifier identifier = pop(); | 349 Identifier identifier = pop(); |
351 if (initializer != null) { | 350 if (initializer != null) { |
(...skipping 1120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1472 if (!isInstanceContext && type.parameter.parent is Class) { | 1471 if (!isInstanceContext && type.parameter.parent is Class) { |
1473 pop(); | 1472 pop(); |
1474 warning("Type variables can only be used in instance methods.", | 1473 warning("Type variables can only be used in instance methods.", |
1475 beginToken.charOffset); | 1474 beginToken.charOffset); |
1476 push(const DynamicType()); | 1475 push(const DynamicType()); |
1477 } | 1476 } |
1478 } | 1477 } |
1479 } | 1478 } |
1480 | 1479 |
1481 @override | 1480 @override |
| 1481 void handleFunctionType(Token functionToken, Token endToken) { |
| 1482 FormalParameters formals = pop(); |
| 1483 ignore(Unhandled.TypeVariables); |
| 1484 DartType returnType = pop(); |
| 1485 push(formals.toFunctionType(returnType)); |
| 1486 } |
| 1487 |
| 1488 @override |
1482 void handleVoidKeyword(Token token) { | 1489 void handleVoidKeyword(Token token) { |
1483 debugEvent("VoidKeyword"); | 1490 debugEvent("VoidKeyword"); |
1484 push(const VoidType()); | 1491 push(const VoidType()); |
1485 } | 1492 } |
1486 | 1493 |
1487 @override | 1494 @override |
1488 void handleAsOperator(Token operator, Token endToken) { | 1495 void handleAsOperator(Token operator, Token endToken) { |
1489 debugEvent("AsOperator"); | 1496 debugEvent("AsOperator"); |
1490 DartType type = pop(); | 1497 DartType type = pop(); |
1491 Expression expression = popForValue(); | 1498 Expression expression = popForValue(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1523 Expression expression = popForValue(); | 1530 Expression expression = popForValue(); |
1524 if (constantExpressionRequired) { | 1531 if (constantExpressionRequired) { |
1525 push(buildCompileTimeError( | 1532 push(buildCompileTimeError( |
1526 "Not a constant expression.", throwToken.charOffset)); | 1533 "Not a constant expression.", throwToken.charOffset)); |
1527 } else { | 1534 } else { |
1528 push(astFactory.throwExpression(throwToken, expression)); | 1535 push(astFactory.throwExpression(throwToken, expression)); |
1529 } | 1536 } |
1530 } | 1537 } |
1531 | 1538 |
1532 @override | 1539 @override |
1533 void endFormalParameter(Token covariantKeyword, Token thisKeyword, | 1540 void endFormalParameter(Token thisKeyword, Token nameToken, |
1534 Token nameToken, FormalParameterType kind) { | 1541 FormalParameterType kind, MemberKind memberKind) { |
1535 debugEvent("FormalParameter"); | 1542 debugEvent("FormalParameter"); |
1536 // TODO(ahe): Need beginToken here. | |
1537 int charOffset = thisKeyword?.charOffset; | |
1538 if (thisKeyword != null) { | 1543 if (thisKeyword != null) { |
1539 if (!inConstructor) { | 1544 if (!inConstructor) { |
1540 addCompileTimeError(thisKeyword.charOffset, | 1545 addCompileTimeError(thisKeyword.charOffset, |
1541 "'this' parameters can only be used on constructors."); | 1546 "'this' parameters can only be used on constructors."); |
1542 thisKeyword = null; | 1547 thisKeyword = null; |
1543 } | 1548 } |
1544 } | 1549 } |
1545 Identifier name = pop(); | 1550 Identifier name = pop(); |
1546 DartType type = pop(); | 1551 DartType type = pop(); |
1547 int modifiers = Modifier.validate(pop()); | 1552 int modifiers = Modifier.validate(pop()); |
1548 if (inCatchClause) { | 1553 if (inCatchClause) { |
1549 modifiers |= finalMask; | 1554 modifiers |= finalMask; |
1550 } | 1555 } |
1551 bool isConst = (modifiers & constMask) != 0; | 1556 bool isConst = (modifiers & constMask) != 0; |
1552 bool isFinal = (modifiers & finalMask) != 0; | 1557 bool isFinal = (modifiers & finalMask) != 0; |
1553 ignore(Unhandled.Metadata); | 1558 ignore(Unhandled.Metadata); |
1554 VariableDeclaration variable; | 1559 VariableDeclaration variable; |
1555 if (!inCatchClause && functionNestingLevel == 0) { | 1560 if (!inCatchClause && |
1556 dynamic builder = formalParameterScope.lookup(name.name, charOffset, uri); | 1561 functionNestingLevel == 0 && |
| 1562 memberKind != MemberKind.GeneralizedFunctionType) { |
| 1563 dynamic builder = formalParameterScope.lookup( |
| 1564 name.name, offsetForToken(name.token), uri); |
1557 if (builder == null) { | 1565 if (builder == null) { |
1558 if (thisKeyword == null) { | 1566 if (thisKeyword == null) { |
1559 internalError("Internal error: formal missing for '${name.name}'"); | 1567 internalError("Internal error: formal missing for '${name.name}'"); |
1560 } else { | 1568 } else { |
1561 addCompileTimeError(thisKeyword.charOffset, | 1569 addCompileTimeError(thisKeyword.charOffset, |
1562 "'${name.name}' isn't a field in this class."); | 1570 "'${name.name}' isn't a field in this class."); |
1563 thisKeyword = null; | 1571 thisKeyword = null; |
1564 } | 1572 } |
1565 } else if (thisKeyword == null) { | 1573 } else if (thisKeyword == null) { |
1566 variable = builder.build(library); | 1574 variable = builder.build(library); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1603 } | 1611 } |
1604 | 1612 |
1605 @override | 1613 @override |
1606 void beginFunctionTypedFormalParameter(Token token) { | 1614 void beginFunctionTypedFormalParameter(Token token) { |
1607 debugEvent("beginFunctionTypedFormalParameter"); | 1615 debugEvent("beginFunctionTypedFormalParameter"); |
1608 functionNestingLevel++; | 1616 functionNestingLevel++; |
1609 } | 1617 } |
1610 | 1618 |
1611 @override | 1619 @override |
1612 void endFunctionTypedFormalParameter( | 1620 void endFunctionTypedFormalParameter( |
1613 Token covariantKeyword, Token thisKeyword, FormalParameterType kind) { | 1621 Token thisKeyword, FormalParameterType kind) { |
1614 debugEvent("FunctionTypedFormalParameter"); | 1622 debugEvent("FunctionTypedFormalParameter"); |
1615 if (inCatchClause || functionNestingLevel != 0) { | 1623 if (inCatchClause || functionNestingLevel != 0) { |
1616 exitLocalScope(); | 1624 exitLocalScope(); |
1617 } | 1625 } |
1618 FormalParameters formals = pop(); | 1626 FormalParameters formals = pop(); |
1619 ignore(Unhandled.TypeVariables); | 1627 ignore(Unhandled.TypeVariables); |
1620 Identifier name = pop(); | 1628 Identifier name = pop(); |
1621 DartType returnType = pop(); | 1629 DartType returnType = pop(); |
1622 push(formals.toFunctionType(returnType)); | 1630 push(formals.toFunctionType(returnType)); |
1623 push(name); | 1631 push(name); |
1624 functionNestingLevel--; | 1632 functionNestingLevel--; |
1625 } | 1633 } |
1626 | 1634 |
1627 @override | 1635 @override |
1628 void handleValuedFormalParameter(Token equals, Token token) { | 1636 void handleValuedFormalParameter(Token equals, Token token) { |
1629 debugEvent("ValuedFormalParameter"); | 1637 debugEvent("ValuedFormalParameter"); |
1630 Expression initializer = popForValue(); | 1638 Expression initializer = popForValue(); |
1631 Identifier name = pop(); | 1639 Identifier name = pop(); |
1632 push(new InitializedIdentifier(name.token, initializer)); | 1640 push(new InitializedIdentifier(name.token, initializer)); |
1633 } | 1641 } |
1634 | 1642 |
1635 @override | 1643 @override |
1636 void handleFormalParameterWithoutValue(Token token) { | 1644 void handleFormalParameterWithoutValue(Token token) { |
1637 debugEvent("FormalParameterWithoutValue"); | 1645 debugEvent("FormalParameterWithoutValue"); |
1638 } | 1646 } |
1639 | 1647 |
1640 @override | 1648 @override |
1641 void endFormalParameters(int count, Token beginToken, Token endToken) { | 1649 void endFormalParameters( |
| 1650 int count, Token beginToken, Token endToken, MemberKind kind) { |
1642 debugEvent("FormalParameters"); | 1651 debugEvent("FormalParameters"); |
1643 OptionalFormals optional; | 1652 OptionalFormals optional; |
1644 if (count > 0 && peek() is OptionalFormals) { | 1653 if (count > 0 && peek() is OptionalFormals) { |
1645 optional = pop(); | 1654 optional = pop(); |
1646 count--; | 1655 count--; |
1647 } | 1656 } |
1648 FormalParameters formals = new FormalParameters( | 1657 FormalParameters formals = new FormalParameters( |
1649 popList(count) ?? <VariableDeclaration>[], | 1658 popList(count) ?? <VariableDeclaration>[], |
1650 optional, | 1659 optional, |
1651 beginToken.charOffset); | 1660 beginToken.charOffset); |
(...skipping 868 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2520 @override | 2529 @override |
2521 void handleModifiers(int count) { | 2530 void handleModifiers(int count) { |
2522 debugEvent("Modifiers"); | 2531 debugEvent("Modifiers"); |
2523 // TODO(ahe): Copied from outline_builder.dart. | 2532 // TODO(ahe): Copied from outline_builder.dart. |
2524 push(popList(count) ?? NullValue.Modifiers); | 2533 push(popList(count) ?? NullValue.Modifiers); |
2525 } | 2534 } |
2526 | 2535 |
2527 @override | 2536 @override |
2528 void handleRecoverableError(Token token, FastaMessage message) { | 2537 void handleRecoverableError(Token token, FastaMessage message) { |
2529 bool silent = hasParserError; | 2538 bool silent = hasParserError; |
2530 super.handleRecoverableError(token, message); | |
2531 addCompileTimeError(message.charOffset, message.message, silent: silent); | 2539 addCompileTimeError(message.charOffset, message.message, silent: silent); |
2532 } | 2540 } |
2533 | 2541 |
2534 @override | 2542 @override |
2535 Token handleUnrecoverableError(Token token, FastaMessage message) { | 2543 Token handleUnrecoverableError(Token token, FastaMessage message) { |
2536 if (isDartLibrary && message.code == codeExpectedFunctionBody) { | 2544 if (isDartLibrary && message.code == codeExpectedFunctionBody) { |
2537 Token recover = library.loader.target.skipNativeClause(token); | 2545 Token recover = library.loader.target.skipNativeClause(token); |
2538 if (recover != null) return recover; | 2546 if (recover != null) return recover; |
2539 } else if (message.code == codeExpectedButGot) { | 2547 } else if (message.code == codeExpectedButGot) { |
2540 String expected = message.arguments["string"]; | 2548 String expected = message.arguments["string"]; |
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3138 } else if (node is PrefixBuilder) { | 3146 } else if (node is PrefixBuilder) { |
3139 return node.name; | 3147 return node.name; |
3140 } else if (node is ThisAccessor) { | 3148 } else if (node is ThisAccessor) { |
3141 return node.isSuper ? "super" : "this"; | 3149 return node.isSuper ? "super" : "this"; |
3142 } else if (node is FastaAccessor) { | 3150 } else if (node is FastaAccessor) { |
3143 return node.plainNameForRead; | 3151 return node.plainNameForRead; |
3144 } else { | 3152 } else { |
3145 return internalError("Unhandled: ${node.runtimeType}"); | 3153 return internalError("Unhandled: ${node.runtimeType}"); |
3146 } | 3154 } |
3147 } | 3155 } |
OLD | NEW |