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 #include "vm/parser.h" | 5 #include "vm/parser.h" |
6 #include "vm/flags.h" | 6 #include "vm/flags.h" |
7 | 7 |
8 #ifndef DART_PRECOMPILED_RUNTIME | 8 #ifndef DART_PRECOMPILED_RUNTIME |
9 | 9 |
10 #include "lib/invocation_mirror.h" | 10 #include "lib/invocation_mirror.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 "Enable conditional directives"); | 56 "Enable conditional directives"); |
57 DEFINE_FLAG(bool, generic_method_syntax, true, "Enable generic functions."); | 57 DEFINE_FLAG(bool, generic_method_syntax, true, "Enable generic functions."); |
58 DEFINE_FLAG(bool, | 58 DEFINE_FLAG(bool, |
59 initializing_formal_access, | 59 initializing_formal_access, |
60 true, | 60 true, |
61 "Make initializing formal parameters visible in initializer list."); | 61 "Make initializing formal parameters visible in initializer list."); |
62 DEFINE_FLAG(bool, | 62 DEFINE_FLAG(bool, |
63 warn_super, | 63 warn_super, |
64 false, | 64 false, |
65 "Warning if super initializer not last in initializer list."); | 65 "Warning if super initializer not last in initializer list."); |
66 DEFINE_FLAG(bool, warn_patch, false, "Warn on old-style patch syntax."); | |
67 DEFINE_FLAG( | 66 DEFINE_FLAG( |
68 bool, | 67 bool, |
69 await_is_keyword, | 68 await_is_keyword, |
70 false, | 69 false, |
71 "await and yield are treated as proper keywords in synchronous code."); | 70 "await and yield are treated as proper keywords in synchronous code."); |
72 DEFINE_FLAG(bool, | 71 DEFINE_FLAG(bool, |
73 assert_initializer, | 72 assert_initializer, |
74 false, | 73 false, |
75 "Allow asserts in initializer lists."); | 74 "Allow asserts in initializer lists."); |
76 | 75 |
(...skipping 911 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
988 Thread* thread = Thread::Current(); | 987 Thread* thread = Thread::Current(); |
989 StackZone stack_zone(thread); | 988 StackZone stack_zone(thread); |
990 Zone* zone = stack_zone.GetZone(); | 989 Zone* zone = stack_zone.GetZone(); |
991 const Script& script = Script::Handle(zone, func.script()); | 990 const Script& script = Script::Handle(zone, func.script()); |
992 const Class& owner = Class::Handle(zone, func.Owner()); | 991 const Class& owner = Class::Handle(zone, func.Owner()); |
993 ASSERT(!owner.IsNull()); | 992 ASSERT(!owner.IsNull()); |
994 ParsedFunction* parsed_function = | 993 ParsedFunction* parsed_function = |
995 new ParsedFunction(thread, Function::ZoneHandle(zone, func.raw())); | 994 new ParsedFunction(thread, Function::ZoneHandle(zone, func.raw())); |
996 Parser parser(script, parsed_function, func.token_pos()); | 995 Parser parser(script, parsed_function, func.token_pos()); |
997 parser.SkipFunctionPreamble(); | 996 parser.SkipFunctionPreamble(); |
997 const bool use_function_type_syntax = false; | |
998 const bool allow_explicit_default_values = true; | |
999 const bool evaluate_metadata = true; | |
998 ParamList params; | 1000 ParamList params; |
999 parser.ParseFormalParameterList(true, true, ¶ms); | 1001 parser.ParseFormalParameterList(use_function_type_syntax, |
1002 allow_explicit_default_values, | |
1003 evaluate_metadata, ¶ms); | |
1000 ParamDesc* param = params.parameters->data(); | 1004 ParamDesc* param = params.parameters->data(); |
1001 const int param_cnt = | 1005 const int param_cnt = |
1002 params.num_fixed_parameters + params.num_optional_parameters; | 1006 params.num_fixed_parameters + params.num_optional_parameters; |
1003 const Array& param_descriptor = | 1007 const Array& param_descriptor = |
1004 Array::Handle(Array::New(param_cnt * kParameterEntrySize, Heap::kOld)); | 1008 Array::Handle(Array::New(param_cnt * kParameterEntrySize, Heap::kOld)); |
1005 for (int i = 0, j = 0; i < param_cnt; i++, j += kParameterEntrySize) { | 1009 for (int i = 0, j = 0; i < param_cnt; i++, j += kParameterEntrySize) { |
1006 param_descriptor.SetAt(j + kParameterIsFinalOffset, | 1010 param_descriptor.SetAt(j + kParameterIsFinalOffset, |
1007 param[i].is_final ? Bool::True() : Bool::False()); | 1011 param[i].is_final ? Bool::True() : Bool::False()); |
1008 param_descriptor.SetAt(j + kParameterDefaultValueOffset, | 1012 param_descriptor.SetAt(j + kParameterDefaultValueOffset, |
1009 (param[i].default_value == NULL) | 1013 (param[i].default_value == NULL) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1044 } | 1048 } |
1045 LongJumpScope jump; | 1049 LongJumpScope jump; |
1046 if (setjmp(*jump.Set()) == 0) { | 1050 if (setjmp(*jump.Set()) == 0) { |
1047 const Script& script = Script::Handle(func.script()); | 1051 const Script& script = Script::Handle(func.script()); |
1048 const Class& owner = Class::Handle(func.Owner()); | 1052 const Class& owner = Class::Handle(func.Owner()); |
1049 ASSERT(!owner.IsNull()); | 1053 ASSERT(!owner.IsNull()); |
1050 ParsedFunction* parsed_function = | 1054 ParsedFunction* parsed_function = |
1051 new ParsedFunction(Thread::Current(), Function::ZoneHandle(func.raw())); | 1055 new ParsedFunction(Thread::Current(), Function::ZoneHandle(func.raw())); |
1052 Parser parser(script, parsed_function, func.token_pos()); | 1056 Parser parser(script, parsed_function, func.token_pos()); |
1053 parser.SkipFunctionPreamble(); | 1057 parser.SkipFunctionPreamble(); |
1054 parser.ParseFormalParameterList(true, true, params); | 1058 const bool use_function_type_syntax = false; |
1059 const bool allow_explicit_default_values = true; | |
1060 const bool evaluate_metadata = true; | |
1061 parser.ParseFormalParameterList(use_function_type_syntax, | |
1062 allow_explicit_default_values, | |
1063 evaluate_metadata, params); | |
1055 return true; | 1064 return true; |
1056 } else { | 1065 } else { |
1057 Thread::Current()->clear_sticky_error(); | 1066 Thread::Current()->clear_sticky_error(); |
1058 params->Clear(); | 1067 params->Clear(); |
1059 return false; | 1068 return false; |
1060 } | 1069 } |
1061 UNREACHABLE(); | 1070 UNREACHABLE(); |
1062 return false; | 1071 return false; |
1063 } | 1072 } |
1064 | 1073 |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1351 } | 1360 } |
1352 // The instantiator is not required in a static expression. | 1361 // The instantiator is not required in a static expression. |
1353 ASSERT(!parser.IsInstantiatorRequired()); | 1362 ASSERT(!parser.IsInstantiatorRequired()); |
1354 | 1363 |
1355 return parsed_function; | 1364 return parsed_function; |
1356 } | 1365 } |
1357 | 1366 |
1358 | 1367 |
1359 SequenceNode* Parser::ParseStaticFinalGetter(const Function& func) { | 1368 SequenceNode* Parser::ParseStaticFinalGetter(const Function& func) { |
1360 TRACE_PARSER("ParseStaticFinalGetter"); | 1369 TRACE_PARSER("ParseStaticFinalGetter"); |
1361 ParamList params; | |
1362 ASSERT(func.num_fixed_parameters() == 0); // static. | 1370 ASSERT(func.num_fixed_parameters() == 0); // static. |
1363 ASSERT(!func.HasOptionalParameters()); | 1371 ASSERT(!func.HasOptionalParameters()); |
1364 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); | 1372 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); |
1365 | |
1366 // Build local scope for function and populate with the formal parameters. | |
1367 OpenFunctionBlock(func); | 1373 OpenFunctionBlock(func); |
1368 AddFormalParamsToScope(¶ms, current_block_->scope); | |
1369 | |
1370 TokenPosition ident_pos = TokenPos(); | 1374 TokenPosition ident_pos = TokenPos(); |
1371 const String& field_name = *ExpectIdentifier("field name expected"); | 1375 const String& field_name = *ExpectIdentifier("field name expected"); |
1372 const Class& field_class = Class::Handle(Z, func.Owner()); | 1376 const Class& field_class = Class::Handle(Z, func.Owner()); |
1373 const Field& field = | 1377 const Field& field = |
1374 Field::ZoneHandle(Z, field_class.LookupStaticField(field_name)); | 1378 Field::ZoneHandle(Z, field_class.LookupStaticField(field_name)); |
1375 ASSERT(!field.IsNull()); | 1379 ASSERT(!field.IsNull()); |
1376 | 1380 |
1377 // Static final fields must have an initializer. | 1381 // Static final fields must have an initializer. |
1378 ExpectToken(Token::kASSIGN); | 1382 ExpectToken(Token::kASSIGN); |
1379 | 1383 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1553 const Function& parent = Function::Handle(func.parent_function()); | 1557 const Function& parent = Function::Handle(func.parent_function()); |
1554 if (parent.IsImplicitSetterFunction()) { | 1558 if (parent.IsImplicitSetterFunction()) { |
1555 const TokenPosition ident_pos = func.token_pos(); | 1559 const TokenPosition ident_pos = func.token_pos(); |
1556 ASSERT(IsIdentifier()); | 1560 ASSERT(IsIdentifier()); |
1557 params.AddFinalParameter(ident_pos, &Symbols::Value(), | 1561 params.AddFinalParameter(ident_pos, &Symbols::Value(), |
1558 &Object::dynamic_type()); | 1562 &Object::dynamic_type()); |
1559 ASSERT(func.num_fixed_parameters() == 2); // closure, value. | 1563 ASSERT(func.num_fixed_parameters() == 2); // closure, value. |
1560 } else if (!parent.IsGetterFunction() && !parent.IsImplicitGetterFunction()) { | 1564 } else if (!parent.IsGetterFunction() && !parent.IsImplicitGetterFunction()) { |
1561 // NOTE: For the `kernel -> flowgraph` we don't use the parser. | 1565 // NOTE: For the `kernel -> flowgraph` we don't use the parser. |
1562 if (parent.kernel_function() == NULL) { | 1566 if (parent.kernel_function() == NULL) { |
1567 SkipFunctionPreamble(); | |
1568 const bool use_function_type_syntax = false; | |
1563 const bool allow_explicit_default_values = true; | 1569 const bool allow_explicit_default_values = true; |
1564 SkipFunctionPreamble(); | 1570 const bool evaluate_metadata = false; |
1565 ParseFormalParameterList(allow_explicit_default_values, false, ¶ms); | 1571 ParseFormalParameterList(use_function_type_syntax, |
1572 allow_explicit_default_values, evaluate_metadata, | |
1573 ¶ms); | |
1574 FinalizeFormalParameterTypes(¶ms); | |
1566 SetupDefaultsForOptionalParams(params); | 1575 SetupDefaultsForOptionalParams(params); |
1567 } | 1576 } |
1568 } | 1577 } |
1569 | 1578 |
1570 // Populate function scope with the formal parameters. | 1579 // Populate function scope with the formal parameters. |
1571 LocalScope* scope = current_block_->scope; | 1580 LocalScope* scope = current_block_->scope; |
1572 AddFormalParamsToScope(¶ms, scope); | 1581 AddFormalParamsToScope(¶ms, scope); |
1573 | 1582 |
1574 ArgumentListNode* func_args = new ArgumentListNode(token_pos); | 1583 ArgumentListNode* func_args = new ArgumentListNode(token_pos); |
1575 if (!func.is_static()) { | 1584 if (!func.is_static()) { |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1930 | 1939 |
1931 // Skips tokens up to and including matching closing parenthesis. | 1940 // Skips tokens up to and including matching closing parenthesis. |
1932 void Parser::SkipToMatchingParenthesis() { | 1941 void Parser::SkipToMatchingParenthesis() { |
1933 ASSERT(CurrentToken() == Token::kLPAREN); | 1942 ASSERT(CurrentToken() == Token::kLPAREN); |
1934 SkipToMatching(); | 1943 SkipToMatching(); |
1935 ASSERT(CurrentToken() == Token::kRPAREN); | 1944 ASSERT(CurrentToken() == Token::kRPAREN); |
1936 ConsumeToken(); | 1945 ConsumeToken(); |
1937 } | 1946 } |
1938 | 1947 |
1939 | 1948 |
1940 void Parser::ParseFormalParameter(bool allow_explicit_default_value, | 1949 void Parser::ParseFormalParameter(bool use_function_type_syntax, |
hausner
2017/01/18 18:54:53
I'd consider to duplicate this function and separa
regis
2017/01/18 20:24:24
Agreed. I have added a TODO.
| |
1950 bool allow_explicit_default_value, | |
1941 bool evaluate_metadata, | 1951 bool evaluate_metadata, |
1942 ParamList* params) { | 1952 ParamList* params) { |
1943 TRACE_PARSER("ParseFormalParameter"); | 1953 TRACE_PARSER("ParseFormalParameter"); |
1944 ParamDesc parameter; | 1954 ParamDesc parameter; |
1945 bool var_seen = false; | 1955 bool var_seen = false; |
1946 bool final_seen = false; | 1956 bool final_seen = false; |
1947 bool this_seen = false; | 1957 bool this_seen = false; |
1948 | 1958 |
1949 if (evaluate_metadata && (CurrentToken() == Token::kAT)) { | 1959 if (use_function_type_syntax) { |
1950 parameter.metadata = &Array::ZoneHandle(Z, EvaluateMetadata()); | 1960 parameter.has_explicit_type = true; // The type is required by the syntax. |
1961 // It is too early to resolve the type here, since it can be a result type | |
1962 // referring to a not yet declared function type parameter. | |
1963 parameter.type = &AbstractType::ZoneHandle( | |
1964 Z, ParseTypeOrFunctionType(false, ClassFinalizer::kDoNotResolve)); | |
1951 } else { | 1965 } else { |
1952 SkipMetadata(); | 1966 if (evaluate_metadata && (CurrentToken() == Token::kAT)) { |
1953 } | 1967 parameter.metadata = &Array::ZoneHandle(Z, EvaluateMetadata()); |
1954 | 1968 } else { |
1955 if (CurrentToken() == Token::kCOVARIANT && | 1969 SkipMetadata(); |
1956 (LookaheadToken(1) == Token::kFINAL || LookaheadToken(1) == Token::kVAR || | 1970 } |
1957 Token::IsIdentifier(LookaheadToken(1)))) { | 1971 if (CurrentToken() == Token::kCOVARIANT && |
1958 parameter.is_covariant = true; | 1972 (LookaheadToken(1) == Token::kFINAL || |
1959 ConsumeToken(); | 1973 LookaheadToken(1) == Token::kVAR || |
1960 } | 1974 Token::IsIdentifier(LookaheadToken(1)))) { |
1961 if (CurrentToken() == Token::kFINAL) { | 1975 parameter.is_covariant = true; |
1962 ConsumeToken(); | 1976 ConsumeToken(); |
1963 final_seen = true; | 1977 } |
1964 parameter.is_final = true; | 1978 if (CurrentToken() == Token::kFINAL) { |
1965 } else if (CurrentToken() == Token::kVAR) { | 1979 ConsumeToken(); |
1966 ConsumeToken(); | 1980 final_seen = true; |
1967 var_seen = true; | |
1968 // The parameter type is the 'dynamic' type. | |
1969 // If this is an initializing formal, its type will be set to the type of | |
1970 // the respective field when the constructor is fully parsed. | |
1971 parameter.type = &Object::dynamic_type(); | |
1972 } | |
1973 if (CurrentToken() == Token::kTHIS) { | |
1974 ConsumeToken(); | |
1975 ExpectToken(Token::kPERIOD); | |
1976 this_seen = true; | |
1977 parameter.is_field_initializer = true; | |
1978 if (FLAG_initializing_formal_access) { | |
1979 parameter.is_final = true; | 1981 parameter.is_final = true; |
1980 } | 1982 } else if (CurrentToken() == Token::kVAR) { |
1981 } | 1983 ConsumeToken(); |
1982 if ((parameter.type == NULL) && (CurrentToken() == Token::kVOID)) { | 1984 var_seen = true; |
1983 ConsumeToken(); | 1985 // The parameter type is the 'dynamic' type. |
1984 // This must later be changed to a closure type if we recognize | |
1985 // a closure/function type parameter. We check this at the end | |
1986 // of ParseFormalParameter. | |
1987 parameter.type = &Object::void_type(); | |
1988 } | |
1989 if (parameter.type == NULL) { | |
1990 // At this point, we must see an identifier for the type or the | |
1991 // function parameter. | |
1992 if (!IsIdentifier()) { | |
1993 ReportError("parameter name or type expected"); | |
1994 } | |
1995 | |
1996 // Lookahead to determine whether the next tokens are a return type | |
1997 // followed by a parameter name. | |
1998 bool found_type = false; | |
1999 { | |
2000 TokenPosScope saved_pos(this); | |
2001 if (TryParseReturnType()) { | |
2002 if (IsIdentifier() || (CurrentToken() == Token::kTHIS)) { | |
2003 found_type = true; | |
2004 } | |
2005 } | |
2006 } | |
2007 if (found_type) { | |
2008 // The types of formal parameters are never ignored, even in unchecked | |
2009 // mode, because they are part of the function type of closurized | |
2010 // functions appearing in type tests with typedefs. | |
2011 parameter.has_explicit_type = true; | |
2012 // It is too early to resolve the type here, since it can be a result type | |
2013 // referring to a not yet declared function type parameter. | |
2014 parameter.type = &AbstractType::ZoneHandle( | |
2015 Z, ParseType(ClassFinalizer::kDoNotResolve)); | |
2016 } else { | |
2017 // If this is an initializing formal, its type will be set to the type of | 1986 // If this is an initializing formal, its type will be set to the type of |
2018 // the respective field when the constructor is fully parsed. | 1987 // the respective field when the constructor is fully parsed. |
2019 parameter.type = &Object::dynamic_type(); | 1988 parameter.type = &Object::dynamic_type(); |
2020 } | 1989 } |
2021 } | 1990 if (CurrentToken() == Token::kTHIS) { |
2022 if (!this_seen && (CurrentToken() == Token::kTHIS)) { | 1991 ConsumeToken(); |
2023 ConsumeToken(); | 1992 ExpectToken(Token::kPERIOD); |
2024 ExpectToken(Token::kPERIOD); | 1993 this_seen = true; |
2025 this_seen = true; | 1994 parameter.is_field_initializer = true; |
2026 parameter.is_field_initializer = true; | 1995 if (FLAG_initializing_formal_access) { |
2027 if (FLAG_initializing_formal_access) { | 1996 parameter.is_final = true; |
2028 parameter.is_final = true; | 1997 } |
1998 } | |
1999 if ((parameter.type == NULL) && (CurrentToken() == Token::kVOID)) { | |
2000 ConsumeToken(); | |
2001 // This must later be changed to a closure type if we recognize | |
2002 // a closure/function type parameter. We check this at the end | |
2003 // of ParseFormalParameter. | |
2004 parameter.type = &Object::void_type(); | |
2005 } | |
2006 if (parameter.type == NULL) { | |
2007 // At this point, we must see an identifier for the type or the | |
2008 // function parameter. The identifier may be 'Function'. | |
2009 if (!IsIdentifier()) { | |
2010 ReportError("parameter name or type expected"); | |
2011 } | |
2012 | |
2013 // Lookahead to determine whether the next tokens are a return type | |
2014 // followed by a parameter name. | |
2015 bool found_type = false; | |
2016 { | |
2017 TokenPosScope saved_pos(this); | |
2018 if (TryParseType(true)) { | |
2019 if (IsIdentifier() || (CurrentToken() == Token::kTHIS)) { | |
2020 found_type = true; | |
2021 } | |
2022 } | |
2023 } | |
2024 if (found_type) { | |
2025 // The types of formal parameters are never ignored, even in unchecked | |
2026 // mode, because they are part of the function type of closurized | |
2027 // functions appearing in type tests with typedefs. | |
2028 parameter.has_explicit_type = true; | |
2029 // It is too early to resolve the type here, since it can be a result | |
2030 // type referring to a not yet declared function type parameter. | |
2031 parameter.type = &AbstractType::ZoneHandle( | |
2032 Z, ParseTypeOrFunctionType(true, ClassFinalizer::kDoNotResolve)); | |
2033 } else { | |
2034 // If this is an initializing formal, its type will be set to the type | |
2035 // of the respective field when the constructor is fully parsed. | |
2036 parameter.type = &Object::dynamic_type(); | |
2037 } | |
2038 } | |
2039 if (!this_seen && (CurrentToken() == Token::kTHIS)) { | |
2040 ConsumeToken(); | |
2041 ExpectToken(Token::kPERIOD); | |
2042 this_seen = true; | |
2043 parameter.is_field_initializer = true; | |
2044 if (FLAG_initializing_formal_access) { | |
2045 parameter.is_final = true; | |
2046 } | |
2029 } | 2047 } |
2030 } | 2048 } |
2031 | 2049 |
2032 // At this point, we must see an identifier for the parameter name. | 2050 // At this point, we must see an identifier for the parameter name, unless |
2033 parameter.name_pos = TokenPos(); | 2051 // we are using the function type syntax (in which case the name is optional, |
2034 parameter.name = ExpectIdentifier("parameter name expected"); | 2052 // unless we expect optional named parameters). |
2035 if (parameter.is_field_initializer) { | 2053 if (IsIdentifier()) { |
2036 params->has_field_initializer = true; | 2054 parameter.name_pos = TokenPos(); |
2055 parameter.name = CurrentLiteral(); | |
2056 ConsumeToken(); | |
2057 if (parameter.is_field_initializer) { | |
2058 ASSERT(!use_function_type_syntax); | |
2059 params->has_field_initializer = true; | |
2060 } | |
2061 | |
2062 if (params->has_optional_named_parameters && | |
2063 (parameter.name->CharAt(0) == Library::kPrivateIdentifierStart)) { | |
2064 ReportError(parameter.name_pos, "named parameter must not be private"); | |
2065 } | |
2066 | |
2067 // Check for duplicate formal parameters. | |
2068 const intptr_t num_existing_parameters = | |
2069 params->num_fixed_parameters + params->num_optional_parameters; | |
2070 for (intptr_t i = 0; i < num_existing_parameters; i++) { | |
2071 ParamDesc& existing_parameter = (*params->parameters)[i]; | |
2072 if (existing_parameter.name->Equals(*parameter.name)) { | |
2073 ReportError(parameter.name_pos, "duplicate formal parameter '%s'", | |
2074 parameter.name->ToCString()); | |
2075 } | |
2076 } | |
2077 } else if (!use_function_type_syntax || | |
2078 params->has_optional_named_parameters) { | |
2079 ExpectIdentifier("parameter name expected"); | |
2080 } else { | |
2081 parameter.name_pos = TokenPos(); | |
2082 parameter.name = &Symbols::NotNamed(); | |
2037 } | 2083 } |
2038 | 2084 |
2039 if (params->has_optional_named_parameters && | 2085 // The function type syntax does not allow the signature type syntax. |
2040 (parameter.name->CharAt(0) == Library::kPrivateIdentifierStart)) { | 2086 if (!use_function_type_syntax && IsParameterPart()) { |
2041 ReportError(parameter.name_pos, "named parameter must not be private"); | |
2042 } | |
2043 | |
2044 // Check for duplicate formal parameters. | |
2045 const intptr_t num_existing_parameters = | |
2046 params->num_fixed_parameters + params->num_optional_parameters; | |
2047 for (intptr_t i = 0; i < num_existing_parameters; i++) { | |
2048 ParamDesc& existing_parameter = (*params->parameters)[i]; | |
2049 if (existing_parameter.name->Equals(*parameter.name)) { | |
2050 ReportError(parameter.name_pos, "duplicate formal parameter '%s'", | |
2051 parameter.name->ToCString()); | |
2052 } | |
2053 } | |
2054 | |
2055 if (IsParameterPart()) { | |
2056 // This parameter is probably a closure. If we saw the keyword 'var' | 2087 // This parameter is probably a closure. If we saw the keyword 'var' |
2057 // or 'final', a closure is not legal here and we ignore the | 2088 // or 'final', a closure is not legal here and we ignore the |
2058 // opening parens. | 2089 // opening parens. |
2059 // TODO(hausner): The language spec appears to allow var and final | 2090 // TODO(hausner): The language spec appears to allow var and final |
2060 // in signature types when used with initializing formals: | 2091 // in signature types when used with initializing formals: |
2061 // fieldFormalParameter: | 2092 // fieldFormalParameter: |
2062 // metadata finalConstVarOrType? this ‘.’ identifier formalParameterList? ; | 2093 // metadata finalConstVarOrType? this ‘.’ identifier formalParameterList? ; |
2063 if (!var_seen && !final_seen) { | 2094 if (!var_seen && !final_seen) { |
2064 // The parsed parameter type is actually the function result type. | 2095 // The parsed parameter type is actually the function result type. |
2065 AbstractType& result_type = | 2096 AbstractType& result_type = |
(...skipping 11 matching lines...) Expand all Loading... | |
2077 innermost_function_ = signature_function.raw(); | 2108 innermost_function_ = signature_function.raw(); |
2078 | 2109 |
2079 // Finish parsing the function type parameter. | 2110 // Finish parsing the function type parameter. |
2080 if (CurrentToken() == Token::kLT) { | 2111 if (CurrentToken() == Token::kLT) { |
2081 if (!FLAG_generic_method_syntax) { | 2112 if (!FLAG_generic_method_syntax) { |
2082 ReportError("generic function types not supported"); | 2113 ReportError("generic function types not supported"); |
2083 } | 2114 } |
2084 ParseTypeParameters(false); // Not parameterizing class, but function. | 2115 ParseTypeParameters(false); // Not parameterizing class, but function. |
2085 } | 2116 } |
2086 | 2117 |
2087 // Now that type parameters are declared, the result type can be resolved. | |
2088 ResolveType(is_top_level_ ? ClassFinalizer::kResolveTypeParameters | |
2089 : ClassFinalizer::kCanonicalize, | |
2090 &result_type); | |
2091 | |
2092 ASSERT(CurrentToken() == Token::kLPAREN); | 2118 ASSERT(CurrentToken() == Token::kLPAREN); |
2093 ParamList func_params; | 2119 ParamList func_params; |
2094 | 2120 |
2095 // Add implicit closure object parameter. | 2121 // Add implicit closure object parameter. |
2096 func_params.AddFinalParameter(TokenPos(), &Symbols::ClosureParameter(), | 2122 func_params.AddFinalParameter(TokenPos(), &Symbols::ClosureParameter(), |
2097 &Object::dynamic_type()); | 2123 &Object::dynamic_type()); |
2098 | 2124 |
2099 const bool no_explicit_default_values = false; | 2125 const bool use_function_type_syntax = false; |
2100 ParseFormalParameterList(no_explicit_default_values, false, &func_params); | 2126 const bool allow_explicit_default_values = false; |
2127 const bool evaluate_metadata = false; | |
2128 ParseFormalParameterList(use_function_type_syntax, | |
2129 allow_explicit_default_values, evaluate_metadata, | |
2130 &func_params); | |
2101 | 2131 |
2102 signature_function.set_result_type(result_type); | 2132 signature_function.set_result_type(result_type); |
2103 AddFormalParamsToFunction(&func_params, signature_function); | 2133 AddFormalParamsToFunction(&func_params, signature_function); |
2104 | 2134 |
2105 ASSERT(innermost_function().raw() == signature_function.raw()); | 2135 ASSERT(innermost_function().raw() == signature_function.raw()); |
2106 innermost_function_ = signature_function.parent_function(); | 2136 innermost_function_ = signature_function.parent_function(); |
2107 | 2137 |
2108 Type& signature_type = | 2138 Type& signature_type = |
2109 Type::ZoneHandle(Z, signature_function.SignatureType()); | 2139 Type::ZoneHandle(Z, signature_function.SignatureType()); |
2110 if (!is_top_level_) { | 2140 |
2111 signature_type ^= ClassFinalizer::FinalizeType( | |
2112 current_class(), signature_type, ClassFinalizer::kCanonicalize); | |
2113 // Do not refer to signature_function anymore, since it may have been | |
2114 // replaced during canonicalization. | |
2115 signature_function = Function::null(); | |
2116 } | |
2117 ASSERT(is_top_level_ || signature_type.IsFinalized()); | |
2118 // A signature type itself cannot be malformed or malbounded, only its | 2141 // A signature type itself cannot be malformed or malbounded, only its |
2119 // signature function's result type or parameter types may be. | 2142 // signature function's result type or parameter types may be. |
2120 ASSERT(!signature_type.IsMalformed()); | 2143 ASSERT(!signature_type.IsMalformed()); |
2121 ASSERT(!signature_type.IsMalbounded()); | 2144 ASSERT(!signature_type.IsMalbounded()); |
2122 // The type of the parameter is now the signature type. | 2145 // The type of the parameter is now the signature type. |
2123 parameter.type = &signature_type; | 2146 parameter.type = &signature_type; |
2124 } | 2147 } |
2125 } else { | |
2126 if (!parameter.type->IsFinalized()) { | |
2127 AbstractType& type = AbstractType::ZoneHandle(Z, parameter.type->raw()); | |
2128 if (is_top_level_) { | |
2129 ResolveType(ClassFinalizer::kResolveTypeParameters, &type); | |
2130 } else { | |
2131 ResolveType(ClassFinalizer::kCanonicalize, &type); | |
2132 type = ClassFinalizer::FinalizeType(current_class(), type, | |
2133 ClassFinalizer::kCanonicalize); | |
2134 } | |
2135 parameter.type = &type; | |
2136 } | |
2137 } | 2148 } |
2138 | 2149 |
2139 if ((CurrentToken() == Token::kASSIGN) || (CurrentToken() == Token::kCOLON)) { | 2150 if ((CurrentToken() == Token::kASSIGN) || (CurrentToken() == Token::kCOLON)) { |
2140 if ((!params->has_optional_positional_parameters && | 2151 if ((!params->has_optional_positional_parameters && |
2141 !params->has_optional_named_parameters) || | 2152 !params->has_optional_named_parameters) || |
2142 !allow_explicit_default_value) { | 2153 !allow_explicit_default_value) { |
2143 ReportError("parameter must not specify a default value"); | 2154 ReportError("parameter must not specify a default value"); |
2144 } | 2155 } |
2145 if (params->has_optional_positional_parameters) { | 2156 if (params->has_optional_positional_parameters) { |
2146 ExpectToken(Token::kASSIGN); | 2157 ExpectToken(Token::kASSIGN); |
(...skipping 28 matching lines...) Expand all Loading... | |
2175 parameter.is_final = true; | 2186 parameter.is_final = true; |
2176 } | 2187 } |
2177 params->parameters->Add(parameter); | 2188 params->parameters->Add(parameter); |
2178 if (parameter.is_covariant) { | 2189 if (parameter.is_covariant) { |
2179 params->has_covariant = true; | 2190 params->has_covariant = true; |
2180 } | 2191 } |
2181 } | 2192 } |
2182 | 2193 |
2183 | 2194 |
2184 // Parses a sequence of normal or optional formal parameters. | 2195 // Parses a sequence of normal or optional formal parameters. |
2185 void Parser::ParseFormalParameters(bool allow_explicit_default_values, | 2196 void Parser::ParseFormalParameters(bool use_function_type_syntax, |
2197 bool allow_explicit_default_values, | |
2186 bool evaluate_metadata, | 2198 bool evaluate_metadata, |
2187 ParamList* params) { | 2199 ParamList* params) { |
2188 TRACE_PARSER("ParseFormalParameters"); | 2200 TRACE_PARSER("ParseFormalParameters"); |
2189 // Optional parameter lists cannot be empty. | 2201 // Optional parameter lists cannot be empty. |
2190 // The completely empty parameter list is handled before getting here. | 2202 // The completely empty parameter list is handled before getting here. |
2191 bool has_seen_parameter = false; | 2203 bool has_seen_parameter = false; |
2192 do { | 2204 do { |
2193 ConsumeToken(); | 2205 ConsumeToken(); |
2194 if (!params->has_optional_positional_parameters && | 2206 if (!params->has_optional_positional_parameters && |
2195 !params->has_optional_named_parameters && | 2207 !params->has_optional_named_parameters && |
(...skipping 11 matching lines...) Expand all Loading... | |
2207 } | 2219 } |
2208 Token::Kind terminator = params->has_optional_positional_parameters | 2220 Token::Kind terminator = params->has_optional_positional_parameters |
2209 ? Token::kRBRACK | 2221 ? Token::kRBRACK |
2210 : params->has_optional_named_parameters | 2222 : params->has_optional_named_parameters |
2211 ? Token::kRBRACE | 2223 ? Token::kRBRACE |
2212 : Token::kRPAREN; | 2224 : Token::kRPAREN; |
2213 if (has_seen_parameter && CurrentToken() == terminator) { | 2225 if (has_seen_parameter && CurrentToken() == terminator) { |
2214 // Allow a trailing comma. | 2226 // Allow a trailing comma. |
2215 break; | 2227 break; |
2216 } | 2228 } |
2217 ParseFormalParameter(allow_explicit_default_values, evaluate_metadata, | 2229 ParseFormalParameter(use_function_type_syntax, |
2230 allow_explicit_default_values, evaluate_metadata, | |
2218 params); | 2231 params); |
2219 has_seen_parameter = true; | 2232 has_seen_parameter = true; |
2220 } while (CurrentToken() == Token::kCOMMA); | 2233 } while (CurrentToken() == Token::kCOMMA); |
2221 } | 2234 } |
2222 | 2235 |
2223 | 2236 |
2224 void Parser::ParseFormalParameterList(bool allow_explicit_default_values, | 2237 void Parser::ParseFormalParameterList(bool use_function_type_syntax, |
2238 bool allow_explicit_default_values, | |
2225 bool evaluate_metadata, | 2239 bool evaluate_metadata, |
2226 ParamList* params) { | 2240 ParamList* params) { |
2227 TRACE_PARSER("ParseFormalParameterList"); | 2241 TRACE_PARSER("ParseFormalParameterList"); |
2228 ASSERT(CurrentToken() == Token::kLPAREN); | 2242 ASSERT(CurrentToken() == Token::kLPAREN); |
2229 | 2243 |
2230 if (LookaheadToken(1) != Token::kRPAREN) { | 2244 if (LookaheadToken(1) != Token::kRPAREN) { |
2231 // Parse fixed parameters. | 2245 // Parse fixed parameters. |
2232 ParseFormalParameters(allow_explicit_default_values, evaluate_metadata, | 2246 ParseFormalParameters(use_function_type_syntax, |
2247 allow_explicit_default_values, evaluate_metadata, | |
2233 params); | 2248 params); |
2234 if (params->has_optional_positional_parameters || | 2249 if (params->has_optional_positional_parameters || |
2235 params->has_optional_named_parameters) { | 2250 params->has_optional_named_parameters) { |
2236 // Parse optional parameters. | 2251 // Parse optional parameters. |
2237 ParseFormalParameters(allow_explicit_default_values, evaluate_metadata, | 2252 ParseFormalParameters(use_function_type_syntax, |
2253 allow_explicit_default_values, evaluate_metadata, | |
2238 params); | 2254 params); |
2239 if (params->has_optional_positional_parameters) { | 2255 if (params->has_optional_positional_parameters) { |
2240 CheckToken(Token::kRBRACK, "',' or ']' expected"); | 2256 CheckToken(Token::kRBRACK, "',' or ']' expected"); |
2241 } else { | 2257 } else { |
2242 CheckToken(Token::kRBRACE, "',' or '}' expected"); | 2258 CheckToken(Token::kRBRACE, "',' or '}' expected"); |
2243 } | 2259 } |
2244 ConsumeToken(); // ']' or '}'. | 2260 ConsumeToken(); // ']' or '}'. |
2245 } | 2261 } |
2246 if ((CurrentToken() != Token::kRPAREN) && | 2262 if ((CurrentToken() != Token::kRPAREN) && |
2247 !params->has_optional_positional_parameters && | 2263 !params->has_optional_positional_parameters && |
(...skipping 935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3183 // The parser adds an implicit default constructor when a class | 3199 // The parser adds an implicit default constructor when a class |
3184 // does not have any explicit constructor or factory (see | 3200 // does not have any explicit constructor or factory (see |
3185 // Parser::AddImplicitConstructor). | 3201 // Parser::AddImplicitConstructor). |
3186 // There is no source text to parse. We just build the | 3202 // There is no source text to parse. We just build the |
3187 // sequence node by hand. | 3203 // sequence node by hand. |
3188 return MakeImplicitConstructor(func); | 3204 return MakeImplicitConstructor(func); |
3189 } | 3205 } |
3190 | 3206 |
3191 OpenFunctionBlock(func); | 3207 OpenFunctionBlock(func); |
3192 ParamList params; | 3208 ParamList params; |
3193 const bool allow_explicit_default_values = true; | |
3194 ASSERT(CurrentToken() == Token::kLPAREN); | 3209 ASSERT(CurrentToken() == Token::kLPAREN); |
3195 | 3210 |
3196 // Add implicit receiver parameter which is passed the allocated | 3211 // Add implicit receiver parameter which is passed the allocated |
3197 // but uninitialized instance to construct. | 3212 // but uninitialized instance to construct. |
3198 ASSERT(current_class().raw() == func.Owner()); | 3213 ASSERT(current_class().raw() == func.Owner()); |
3199 params.AddReceiver(ReceiverType(current_class()), func.token_pos()); | 3214 params.AddReceiver(ReceiverType(current_class()), func.token_pos()); |
3200 | 3215 |
3201 if (func.is_const()) { | 3216 if (func.is_const()) { |
3202 params.SetImplicitlyFinal(); | 3217 params.SetImplicitlyFinal(); |
3203 } | 3218 } |
3204 ParseFormalParameterList(allow_explicit_default_values, false, ¶ms); | 3219 const bool use_function_type_syntax = false; |
3220 const bool allow_explicit_default_values = true; | |
3221 const bool evaluate_metadata = false; | |
3222 ParseFormalParameterList(use_function_type_syntax, | |
3223 allow_explicit_default_values, evaluate_metadata, | |
3224 ¶ms); | |
3225 FinalizeFormalParameterTypes(¶ms); | |
3205 | 3226 |
3206 SetupDefaultsForOptionalParams(params); | 3227 SetupDefaultsForOptionalParams(params); |
3207 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); | 3228 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); |
3208 ASSERT(func.NumParameters() == params.parameters->length()); | |
3209 | 3229 |
3210 // Now populate function scope with the formal parameters. | 3230 // Now populate function scope with the formal parameters. |
3211 AddFormalParamsToScope(¶ms, current_block_->scope); | 3231 AddFormalParamsToScope(¶ms, current_block_->scope); |
3212 | 3232 |
3213 const bool is_redirecting_constructor = | 3233 const bool is_redirecting_constructor = |
3214 (CurrentToken() == Token::kCOLON) && | 3234 (CurrentToken() == Token::kCOLON) && |
3215 ((LookaheadToken(1) == Token::kTHIS) && | 3235 ((LookaheadToken(1) == Token::kTHIS) && |
3216 ((LookaheadToken(2) == Token::kLPAREN) || | 3236 ((LookaheadToken(2) == Token::kLPAREN) || |
3217 ((LookaheadToken(2) == Token::kPERIOD) && | 3237 ((LookaheadToken(2) == Token::kPERIOD) && |
3218 (LookaheadToken(4) == Token::kLPAREN)))); | 3238 (LookaheadToken(4) == Token::kLPAREN)))); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3376 // The first parameter of a factory is the TypeArguments vector of | 3396 // The first parameter of a factory is the TypeArguments vector of |
3377 // the type of the instance to be allocated. | 3397 // the type of the instance to be allocated. |
3378 params.AddFinalParameter(TokenPos(), &Symbols::TypeArgumentsParameter(), | 3398 params.AddFinalParameter(TokenPos(), &Symbols::TypeArgumentsParameter(), |
3379 &Object::dynamic_type()); | 3399 &Object::dynamic_type()); |
3380 } | 3400 } |
3381 // Expect the parameter list unless this is a getter function, or the | 3401 // Expect the parameter list unless this is a getter function, or the |
3382 // body closure of an async or generator getter function. | 3402 // body closure of an async or generator getter function. |
3383 ASSERT((CurrentToken() == Token::kLPAREN) || func.IsGetterFunction() || | 3403 ASSERT((CurrentToken() == Token::kLPAREN) || func.IsGetterFunction() || |
3384 (func.is_generated_body() && | 3404 (func.is_generated_body() && |
3385 Function::Handle(func.parent_function()).IsGetterFunction())); | 3405 Function::Handle(func.parent_function()).IsGetterFunction())); |
3386 const bool allow_explicit_default_values = true; | |
3387 if (func.IsGetterFunction()) { | 3406 if (func.IsGetterFunction()) { |
3388 // Populate function scope with the formal parameters. Since in this case | 3407 // Populate function scope with the formal parameters. Since in this case |
3389 // we are compiling a getter this will at most populate the receiver. | 3408 // we are compiling a getter this will at most populate the receiver. |
3390 AddFormalParamsToScope(¶ms, current_block_->scope); | 3409 AddFormalParamsToScope(¶ms, current_block_->scope); |
3391 } else if (func.IsAsyncClosure()) { | 3410 } else if (func.IsAsyncClosure()) { |
3392 AddAsyncClosureParameters(¶ms); | 3411 AddAsyncClosureParameters(¶ms); |
3393 SetupDefaultsForOptionalParams(params); | 3412 SetupDefaultsForOptionalParams(params); |
3394 AddFormalParamsToScope(¶ms, current_block_->scope); | 3413 AddFormalParamsToScope(¶ms, current_block_->scope); |
3395 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); | 3414 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); |
3396 ASSERT(func.NumParameters() == params.parameters->length()); | 3415 ASSERT(func.NumParameters() == params.parameters->length()); |
(...skipping 20 matching lines...) Expand all Loading... | |
3417 AddFormalParamsToScope(¶ms, current_block_->scope); | 3436 AddFormalParamsToScope(¶ms, current_block_->scope); |
3418 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); | 3437 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); |
3419 ASSERT(func.NumParameters() == params.parameters->length()); | 3438 ASSERT(func.NumParameters() == params.parameters->length()); |
3420 if (!Function::Handle(func.parent_function()).IsGetterFunction()) { | 3439 if (!Function::Handle(func.parent_function()).IsGetterFunction()) { |
3421 // Skip formal parameters. They are accessed as context variables. | 3440 // Skip formal parameters. They are accessed as context variables. |
3422 // Parsing them again (and discarding them) does not work in case of | 3441 // Parsing them again (and discarding them) does not work in case of |
3423 // default values with same name as already parsed formal parameter. | 3442 // default values with same name as already parsed formal parameter. |
3424 SkipToMatchingParenthesis(); | 3443 SkipToMatchingParenthesis(); |
3425 } | 3444 } |
3426 } else { | 3445 } else { |
3427 ParseFormalParameterList(allow_explicit_default_values, false, ¶ms); | 3446 const bool use_function_type_syntax = false; |
3447 const bool allow_explicit_default_values = true; | |
3448 const bool evaluate_metadata = false; | |
3449 ParseFormalParameterList(use_function_type_syntax, | |
3450 allow_explicit_default_values, evaluate_metadata, | |
3451 ¶ms); | |
3452 if (!is_top_level_) { | |
3453 FinalizeFormalParameterTypes(¶ms); | |
3454 } | |
3428 | 3455 |
3429 // The number of parameters and their type are not yet set in local | 3456 // The number of parameters and their type are not yet set in local |
3430 // functions, since they are not 'top-level' parsed. | 3457 // functions, since they are not 'top-level' parsed. |
3431 // However, they are already set when the local function is compiled, since | 3458 // However, they are already set when the local function is compiled, since |
3432 // the local function was parsed when its parent was compiled. | 3459 // the local function was parsed when its parent was compiled. |
3433 if (func.parameter_types() == Object::empty_array().raw()) { | 3460 if (func.parameter_types() == Object::empty_array().raw()) { |
3434 AddFormalParamsToFunction(¶ms, func); | 3461 AddFormalParamsToFunction(¶ms, func); |
3435 } | 3462 } |
3463 ResolveSignature(ClassFinalizer::kResolveTypeParameters, func); | |
3464 if (!is_top_level_) { | |
3465 ClassFinalizer::FinalizeSignature(current_class(), func); | |
3466 } | |
3436 SetupDefaultsForOptionalParams(params); | 3467 SetupDefaultsForOptionalParams(params); |
3437 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); | 3468 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); |
3438 ASSERT(func.NumParameters() == params.parameters->length()); | |
3439 | 3469 |
3440 // Populate function scope with the formal parameters. | 3470 // Populate function scope with the formal parameters. |
3441 AddFormalParamsToScope(¶ms, current_block_->scope); | 3471 AddFormalParamsToScope(¶ms, current_block_->scope); |
3442 | 3472 |
3443 if (I->type_checks() && (FunctionLevel() > 0)) { | 3473 if (I->type_checks() && (FunctionLevel() > 0)) { |
3444 // We are parsing, but not compiling, a local function. | 3474 // We are parsing, but not compiling, a local function. |
3445 // The instantiator may be required at run time for generic type checks. | 3475 // The instantiator may be required at run time for generic type checks. |
3446 if (IsInstantiatorRequired()) { | 3476 if (IsInstantiatorRequired()) { |
3447 // Make sure that the receiver of the enclosing instance function | 3477 // Make sure that the receiver of the enclosing instance function |
3448 // (or implicit first parameter of an enclosing factory) is marked as | 3478 // (or implicit first parameter of an enclosing factory) is marked as |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3733 if (method->IsFactoryOrConstructor()) { | 3763 if (method->IsFactoryOrConstructor()) { |
3734 ReportError(method->name_pos, "constructor cannot be generic"); | 3764 ReportError(method->name_pos, "constructor cannot be generic"); |
3735 } | 3765 } |
3736 if (method->IsGetter() || method->IsSetter()) { | 3766 if (method->IsGetter() || method->IsSetter()) { |
3737 ReportError(type_param_pos, "%s cannot be generic", | 3767 ReportError(type_param_pos, "%s cannot be generic", |
3738 method->IsGetter() ? "getter" : "setter"); | 3768 method->IsGetter() ? "getter" : "setter"); |
3739 } | 3769 } |
3740 ParseTypeParameters(false); // Not parameterizing class, but function. | 3770 ParseTypeParameters(false); // Not parameterizing class, but function. |
3741 } | 3771 } |
3742 | 3772 |
3743 // Now that type parameters are declared, the result type can be resolved. | |
3744 if (!method->type->IsResolved()) { | |
3745 AbstractType& type = AbstractType::ZoneHandle(Z, method->type->raw()); | |
3746 ResolveType(ClassFinalizer::kResolveTypeParameters, &type); | |
3747 method->type = &type; | |
3748 } | |
3749 | |
3750 // Parse the formal parameters. | 3773 // Parse the formal parameters. |
3751 const bool are_implicitly_final = method->has_const; | |
3752 const bool allow_explicit_default_values = true; | |
3753 const TokenPosition formal_param_pos = TokenPos(); | 3774 const TokenPosition formal_param_pos = TokenPos(); |
3754 method->params.Clear(); | 3775 method->params.Clear(); |
3755 // Static functions do not have a receiver. | 3776 // Static functions do not have a receiver. |
3756 // The first parameter of a factory is the TypeArguments vector of | 3777 // The first parameter of a factory is the TypeArguments vector of |
3757 // the type of the instance to be allocated. | 3778 // the type of the instance to be allocated. |
3758 if (!method->has_static || method->IsConstructor()) { | 3779 if (!method->has_static || method->IsConstructor()) { |
3759 method->params.AddReceiver(ReceiverType(current_class()), formal_param_pos); | 3780 method->params.AddReceiver(ReceiverType(current_class()), formal_param_pos); |
3760 } else if (method->IsFactory()) { | 3781 } else if (method->IsFactory()) { |
3761 method->params.AddFinalParameter(formal_param_pos, | 3782 method->params.AddFinalParameter(formal_param_pos, |
3762 &Symbols::TypeArgumentsParameter(), | 3783 &Symbols::TypeArgumentsParameter(), |
3763 &Object::dynamic_type()); | 3784 &Object::dynamic_type()); |
3764 } | 3785 } |
3765 if (are_implicitly_final) { | 3786 if (method->has_const) { |
3766 method->params.SetImplicitlyFinal(); | 3787 method->params.SetImplicitlyFinal(); |
3767 } | 3788 } |
3768 if (!method->IsGetter()) { | 3789 if (!method->IsGetter()) { |
3769 ParseFormalParameterList(allow_explicit_default_values, false, | 3790 const bool use_function_type_syntax = false; |
3791 const bool allow_explicit_default_values = true; | |
3792 const bool evaluate_metadata = false; | |
3793 ParseFormalParameterList(use_function_type_syntax, | |
3794 allow_explicit_default_values, evaluate_metadata, | |
3770 &method->params); | 3795 &method->params); |
3771 } | 3796 } |
3772 | 3797 |
3773 // Now that we know the parameter list, we can distinguish between the | 3798 // Now that we know the parameter list, we can distinguish between the |
3774 // unary and binary operator -. | 3799 // unary and binary operator -. |
3775 if (method->has_operator) { | 3800 if (method->has_operator) { |
3776 if ((method->operator_token == Token::kSUB) && | 3801 if ((method->operator_token == Token::kSUB) && |
3777 (method->params.num_fixed_parameters == 1)) { | 3802 (method->params.num_fixed_parameters == 1)) { |
3778 // Patch up name for unary operator - so it does not clash with the | 3803 // Patch up name for unary operator - so it does not clash with the |
3779 // name for binary operator -. | 3804 // name for binary operator -. |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4003 if (method->has_abstract && (async_modifier != RawFunction::kNoModifier)) { | 4028 if (method->has_abstract && (async_modifier != RawFunction::kNoModifier)) { |
4004 ReportError(modifier_pos, | 4029 ReportError(modifier_pos, |
4005 "abstract function '%s' may not be async, async* or sync*", | 4030 "abstract function '%s' may not be async, async* or sync*", |
4006 method->name->ToCString()); | 4031 method->name->ToCString()); |
4007 } | 4032 } |
4008 | 4033 |
4009 // Update function object. | 4034 // Update function object. |
4010 func.set_name(*method->name); | 4035 func.set_name(*method->name); |
4011 func.set_is_abstract(method->has_abstract); | 4036 func.set_is_abstract(method->has_abstract); |
4012 func.set_is_native(method->has_native); | 4037 func.set_is_native(method->has_native); |
4013 func.set_result_type(*method->type); | 4038 func.set_result_type(*method->type); // May set parent_function in type. |
4014 func.set_end_token_pos(method_end_pos); | 4039 func.set_end_token_pos(method_end_pos); |
4015 func.set_is_redirecting(is_redirecting); | 4040 func.set_is_redirecting(is_redirecting); |
4016 func.set_modifier(async_modifier); | 4041 func.set_modifier(async_modifier); |
4017 if (library_.is_dart_scheme() && library_.IsPrivate(*method->name)) { | 4042 if (library_.is_dart_scheme() && library_.IsPrivate(*method->name)) { |
4018 func.set_is_reflectable(false); | 4043 func.set_is_reflectable(false); |
4019 } | 4044 } |
4020 if (is_patch_source() && IsPatchAnnotation(method->metadata_pos)) { | 4045 if (is_patch_source() && IsPatchAnnotation(method->metadata_pos)) { |
4021 // Currently, we just ignore the patch annotation. If the function | 4046 // Currently, we just ignore the patch annotation. If the function |
4022 // name already exists in the patched class, this function will replace | 4047 // name already exists in the patched class, this function will replace |
4023 // the one in the patched class. | 4048 // the one in the patched class. |
4024 method->metadata_pos = TokenPosition::kNoSource; | 4049 method->metadata_pos = TokenPosition::kNoSource; |
4025 } | 4050 } |
4026 if (FLAG_enable_mirrors && (method->metadata_pos.IsReal())) { | 4051 if (FLAG_enable_mirrors && (method->metadata_pos.IsReal())) { |
4027 library_.AddFunctionMetadata(func, method->metadata_pos); | 4052 library_.AddFunctionMetadata(func, method->metadata_pos); |
4028 } | 4053 } |
4029 if (method->has_native) { | 4054 if (method->has_native) { |
4030 func.set_native_name(*native_name); | 4055 func.set_native_name(*native_name); |
4031 } | 4056 } |
4032 | 4057 |
4033 // If this method is a redirecting factory, set the redirection information. | 4058 // If this method is a redirecting factory, set the redirection information. |
4034 if (!redirection_type.IsNull()) { | 4059 if (!redirection_type.IsNull()) { |
4035 ASSERT(func.IsFactory()); | 4060 ASSERT(func.IsFactory()); |
4036 func.SetRedirectionType(redirection_type); | 4061 func.SetRedirectionType(redirection_type); |
4037 if (!redirection_identifier.IsNull()) { | 4062 if (!redirection_identifier.IsNull()) { |
4038 func.SetRedirectionIdentifier(redirection_identifier); | 4063 func.SetRedirectionIdentifier(redirection_identifier); |
4039 } | 4064 } |
4040 } | 4065 } |
4041 | 4066 |
4042 // No need to resolve parameter types yet, or add parameters to local scope. | |
4043 ASSERT(is_top_level_); | 4067 ASSERT(is_top_level_); |
4044 AddFormalParamsToFunction(&method->params, func); | 4068 AddFormalParamsToFunction(&method->params, func); |
4045 ASSERT(innermost_function().raw() == func.raw()); | 4069 ASSERT(innermost_function().raw() == func.raw()); |
4046 innermost_function_ = Function::null(); | 4070 innermost_function_ = Function::null(); |
4071 ResolveSignature(ClassFinalizer::kResolveTypeParameters, func); | |
4047 members->AddFunction(func); | 4072 members->AddFunction(func); |
4048 } | 4073 } |
4049 | 4074 |
4050 | 4075 |
4051 void Parser::ParseFieldDefinition(ClassDesc* members, MemberDesc* field) { | 4076 void Parser::ParseFieldDefinition(ClassDesc* members, MemberDesc* field) { |
4052 TRACE_PARSER("ParseFieldDefinition"); | 4077 TRACE_PARSER("ParseFieldDefinition"); |
4053 // The parser has read the first field name and is now at the token | 4078 // The parser has read the first field name and is now at the token |
4054 // after the field name. | 4079 // after the field name. |
4055 ASSERT(CurrentToken() == Token::kSEMICOLON || | 4080 ASSERT(CurrentToken() == Token::kSEMICOLON || |
4056 CurrentToken() == Token::kCOMMA || CurrentToken() == Token::kASSIGN); | 4081 CurrentToken() == Token::kCOMMA || CurrentToken() == Token::kASSIGN); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4176 /* is_abstract = */ false, | 4201 /* is_abstract = */ false, |
4177 /* is_external = */ false, | 4202 /* is_external = */ false, |
4178 /* is_native = */ false, current_class(), | 4203 /* is_native = */ false, current_class(), |
4179 field->name_pos); | 4204 field->name_pos); |
4180 ParamList params; | 4205 ParamList params; |
4181 ASSERT(current_class().raw() == getter.Owner()); | 4206 ASSERT(current_class().raw() == getter.Owner()); |
4182 params.AddReceiver(ReceiverType(current_class()), field->name_pos); | 4207 params.AddReceiver(ReceiverType(current_class()), field->name_pos); |
4183 getter.set_result_type(*field->type); | 4208 getter.set_result_type(*field->type); |
4184 getter.set_is_debuggable(false); | 4209 getter.set_is_debuggable(false); |
4185 AddFormalParamsToFunction(¶ms, getter); | 4210 AddFormalParamsToFunction(¶ms, getter); |
4211 ResolveSignature(ClassFinalizer::kResolveTypeParameters, getter); | |
4186 members->AddFunction(getter); | 4212 members->AddFunction(getter); |
4187 if (!field->has_final) { | 4213 if (!field->has_final) { |
4188 // Build a setter accessor for non-const fields. | 4214 // Build a setter accessor for non-const fields. |
4189 String& setter_name = | 4215 String& setter_name = |
4190 String::Handle(Z, Field::SetterSymbol(*field->name)); | 4216 String::Handle(Z, Field::SetterSymbol(*field->name)); |
4191 setter = Function::New(setter_name, RawFunction::kImplicitSetter, | 4217 setter = Function::New(setter_name, RawFunction::kImplicitSetter, |
4192 field->has_static, field->has_final, | 4218 field->has_static, field->has_final, |
4193 /* is_abstract = */ false, | 4219 /* is_abstract = */ false, |
4194 /* is_external = */ false, | 4220 /* is_external = */ false, |
4195 /* is_native = */ false, current_class(), | 4221 /* is_native = */ false, current_class(), |
4196 field->name_pos); | 4222 field->name_pos); |
4197 ParamList params; | 4223 ParamList params; |
4198 ASSERT(current_class().raw() == setter.Owner()); | 4224 ASSERT(current_class().raw() == setter.Owner()); |
4199 params.AddReceiver(ReceiverType(current_class()), field->name_pos); | 4225 params.AddReceiver(ReceiverType(current_class()), field->name_pos); |
4200 params.AddFinalParameter(TokenPos(), &Symbols::Value(), field->type); | 4226 params.AddFinalParameter(TokenPos(), &Symbols::Value(), field->type); |
4201 setter.set_result_type(Object::void_type()); | 4227 setter.set_result_type(Object::void_type()); |
4202 setter.set_is_debuggable(false); | 4228 setter.set_is_debuggable(false); |
4203 if (library_.is_dart_scheme() && library_.IsPrivate(*field->name)) { | 4229 if (library_.is_dart_scheme() && library_.IsPrivate(*field->name)) { |
4204 setter.set_is_reflectable(false); | 4230 setter.set_is_reflectable(false); |
4205 } | 4231 } |
4206 AddFormalParamsToFunction(¶ms, setter); | 4232 AddFormalParamsToFunction(¶ms, setter); |
4233 ResolveSignature(ClassFinalizer::kResolveTypeParameters, setter); | |
4207 members->AddFunction(setter); | 4234 members->AddFunction(setter); |
4208 } | 4235 } |
4209 } | 4236 } |
4210 | 4237 |
4211 if (CurrentToken() != Token::kCOMMA) { | 4238 if (CurrentToken() != Token::kCOMMA) { |
4212 break; | 4239 break; |
4213 } | 4240 } |
4214 ConsumeToken(); | 4241 ConsumeToken(); |
4215 field->name_pos = this->TokenPos(); | 4242 field->name_pos = this->TokenPos(); |
4216 field->name = ExpectIdentifier("field name expected"); | 4243 field->name = ExpectIdentifier("field name expected"); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4317 ReportError("void not expected"); | 4344 ReportError("void not expected"); |
4318 } | 4345 } |
4319 ConsumeToken(); | 4346 ConsumeToken(); |
4320 ASSERT(member.type == NULL); | 4347 ASSERT(member.type == NULL); |
4321 member.type = &Object::void_type(); | 4348 member.type = &Object::void_type(); |
4322 } else { | 4349 } else { |
4323 bool found_type = false; | 4350 bool found_type = false; |
4324 { | 4351 { |
4325 // Lookahead to determine whether the next tokens are a return type. | 4352 // Lookahead to determine whether the next tokens are a return type. |
4326 TokenPosScope saved_pos(this); | 4353 TokenPosScope saved_pos(this); |
4327 if (TryParseReturnType()) { | 4354 if (TryParseType(true)) { |
4328 if (IsIdentifier() || (CurrentToken() == Token::kGET) || | 4355 if (IsIdentifier() || (CurrentToken() == Token::kGET) || |
4329 (CurrentToken() == Token::kSET) || | 4356 (CurrentToken() == Token::kSET) || |
4330 (CurrentToken() == Token::kOPERATOR)) { | 4357 (CurrentToken() == Token::kOPERATOR)) { |
4331 found_type = true; | 4358 found_type = true; |
4332 } | 4359 } |
4333 } | 4360 } |
4334 } | 4361 } |
4335 if (found_type) { | 4362 if (found_type) { |
4336 // It is too early to resolve the type here, since it can be a result type | 4363 // It is too early to resolve the type here, since it can be a result type |
4337 // referring to a not yet declared function type parameter. | 4364 // referring to a not yet declared function type parameter. |
4338 member.type = &AbstractType::ZoneHandle( | 4365 member.type = &AbstractType::ZoneHandle( |
4339 Z, ParseType(ClassFinalizer::kDoNotResolve)); | 4366 Z, ParseTypeOrFunctionType(false, ClassFinalizer::kDoNotResolve)); |
4340 } | 4367 } |
4341 } | 4368 } |
4342 | 4369 |
4343 // Optionally parse a (possibly named) constructor name or factory. | 4370 // Optionally parse a (possibly named) constructor name or factory. |
4344 if (IsIdentifier() && | 4371 if (IsIdentifier() && |
4345 (CurrentLiteral()->Equals(members->class_name()) || member.has_factory)) { | 4372 (CurrentLiteral()->Equals(members->class_name()) || member.has_factory)) { |
4346 member.name_pos = TokenPos(); | 4373 member.name_pos = TokenPos(); |
4347 member.name = CurrentLiteral(); // Unqualified identifier. | 4374 member.name = CurrentLiteral(); // Unqualified identifier. |
4348 ConsumeToken(); | 4375 ConsumeToken(); |
4349 if (member.has_factory) { | 4376 if (member.has_factory) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4442 ReportError("identifier expected"); | 4469 ReportError("identifier expected"); |
4443 } | 4470 } |
4444 | 4471 |
4445 ASSERT(member.name != NULL); | 4472 ASSERT(member.name != NULL); |
4446 if (IsParameterPart() || member.IsGetter()) { | 4473 if (IsParameterPart() || member.IsGetter()) { |
4447 // Constructor or method. | 4474 // Constructor or method. |
4448 if (member.type == NULL) { | 4475 if (member.type == NULL) { |
4449 member.type = &Object::dynamic_type(); | 4476 member.type = &Object::dynamic_type(); |
4450 } | 4477 } |
4451 ASSERT(member.IsFactory() == member.has_factory); | 4478 ASSERT(member.IsFactory() == member.has_factory); |
4452 // Note that member.type may still be unresolved. | 4479 // Note that member.type may still be unresolved and may refer to not yet |
4480 // parsed function type parameters. | |
4453 ParseMethodOrConstructor(members, &member); | 4481 ParseMethodOrConstructor(members, &member); |
4454 } else if (CurrentToken() == Token::kSEMICOLON || | 4482 } else if (CurrentToken() == Token::kSEMICOLON || |
4455 CurrentToken() == Token::kCOMMA || | 4483 CurrentToken() == Token::kCOMMA || |
4456 CurrentToken() == Token::kASSIGN) { | 4484 CurrentToken() == Token::kASSIGN) { |
4457 // Field definition. | 4485 // Field definition. |
4458 if (member.has_const) { | 4486 if (member.has_const) { |
4459 // const fields are implicitly final. | 4487 // const fields are implicitly final. |
4460 member.has_final = true; | 4488 member.has_final = true; |
4461 } | 4489 } |
4462 if (member.type == NULL) { | 4490 if (member.type == NULL) { |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4821 /* is_static = */ false, | 4849 /* is_static = */ false, |
4822 /* is_const = */ true, | 4850 /* is_const = */ true, |
4823 /* is_abstract = */ false, | 4851 /* is_abstract = */ false, |
4824 /* is_external = */ false, | 4852 /* is_external = */ false, |
4825 /* is_native = */ false, cls, cls.token_pos()); | 4853 /* is_native = */ false, cls, cls.token_pos()); |
4826 getter.set_result_type(int_type); | 4854 getter.set_result_type(int_type); |
4827 getter.set_is_debuggable(false); | 4855 getter.set_is_debuggable(false); |
4828 ParamList params; | 4856 ParamList params; |
4829 params.AddReceiver(&Object::dynamic_type(), cls.token_pos()); | 4857 params.AddReceiver(&Object::dynamic_type(), cls.token_pos()); |
4830 AddFormalParamsToFunction(¶ms, getter); | 4858 AddFormalParamsToFunction(¶ms, getter); |
4859 ResolveSignature(ClassFinalizer::kResolveTypeParameters, getter); | |
4831 enum_members.AddFunction(getter); | 4860 enum_members.AddFunction(getter); |
4832 | 4861 |
4833 ASSERT(IsIdentifier()); | 4862 ASSERT(IsIdentifier()); |
4834 ASSERT(CurrentLiteral()->raw() == cls.Name()); | 4863 ASSERT(CurrentLiteral()->raw() == cls.Name()); |
4835 | 4864 |
4836 ConsumeToken(); // Enum type name. | 4865 ConsumeToken(); // Enum type name. |
4837 ExpectToken(Token::kLBRACE); | 4866 ExpectToken(Token::kLBRACE); |
4838 Field& enum_value = Field::Handle(Z); | 4867 Field& enum_value = Field::Handle(Z); |
4839 intptr_t i = 0; | 4868 intptr_t i = 0; |
4840 GrowableArray<String*> declared_names(8); | 4869 GrowableArray<String*> declared_names(8); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4935 /* is_static = */ false, | 4964 /* is_static = */ false, |
4936 /* is_const = */ true, | 4965 /* is_const = */ true, |
4937 /* is_abstract = */ false, | 4966 /* is_abstract = */ false, |
4938 /* is_external = */ false, | 4967 /* is_external = */ false, |
4939 /* is_native = */ false, cls, cls.token_pos()); | 4968 /* is_native = */ false, cls, cls.token_pos()); |
4940 name_getter.set_result_type(string_type); | 4969 name_getter.set_result_type(string_type); |
4941 name_getter.set_is_debuggable(false); | 4970 name_getter.set_is_debuggable(false); |
4942 ParamList name_params; | 4971 ParamList name_params; |
4943 name_params.AddReceiver(&Object::dynamic_type(), cls.token_pos()); | 4972 name_params.AddReceiver(&Object::dynamic_type(), cls.token_pos()); |
4944 AddFormalParamsToFunction(&name_params, name_getter); | 4973 AddFormalParamsToFunction(&name_params, name_getter); |
4974 ResolveSignature(ClassFinalizer::kResolveTypeParameters, name_getter); | |
4945 enum_members.AddFunction(name_getter); | 4975 enum_members.AddFunction(name_getter); |
4946 | 4976 |
4947 // Clone the toString() function from the helper class. | 4977 // Clone the toString() function from the helper class. |
4948 Function& to_string_func = Function::Handle( | 4978 Function& to_string_func = Function::Handle( |
4949 Z, helper_class.LookupDynamicFunctionAllowPrivate(Symbols::toString())); | 4979 Z, helper_class.LookupDynamicFunctionAllowPrivate(Symbols::toString())); |
4950 ASSERT(!to_string_func.IsNull()); | 4980 ASSERT(!to_string_func.IsNull()); |
4951 to_string_func = to_string_func.Clone(cls); | 4981 to_string_func = to_string_func.Clone(cls); |
4952 enum_members.AddFunction(to_string_func); | 4982 enum_members.AddFunction(to_string_func); |
4953 | 4983 |
4954 // Clone the hashCode getter function from the helper class. | 4984 // Clone the hashCode getter function from the helper class. |
(...skipping 29 matching lines...) Expand all Loading... | |
4984 if (library_.is_dart_scheme() && library_.IsPrivate(ctor_name)) { | 5014 if (library_.is_dart_scheme() && library_.IsPrivate(ctor_name)) { |
4985 ctor.set_is_reflectable(false); | 5015 ctor.set_is_reflectable(false); |
4986 } | 5016 } |
4987 | 5017 |
4988 ParamList params; | 5018 ParamList params; |
4989 // Add implicit 'this' parameter. | 5019 // Add implicit 'this' parameter. |
4990 const AbstractType* receiver_type = ReceiverType(cls); | 5020 const AbstractType* receiver_type = ReceiverType(cls); |
4991 params.AddReceiver(receiver_type, cls.token_pos()); | 5021 params.AddReceiver(receiver_type, cls.token_pos()); |
4992 | 5022 |
4993 AddFormalParamsToFunction(¶ms, ctor); | 5023 AddFormalParamsToFunction(¶ms, ctor); |
5024 ctor.set_result_type(Object::dynamic_type()); | |
5025 ResolveSignature(ClassFinalizer::kResolveTypeParameters, ctor); | |
4994 // The body of the constructor cannot modify the type of the constructed | 5026 // The body of the constructor cannot modify the type of the constructed |
4995 // instance, which is passed in as the receiver. | 5027 // instance, which is passed in as the receiver. |
4996 ctor.set_result_type(*receiver_type); | 5028 ctor.set_result_type(*receiver_type); |
4997 cls.AddFunction(ctor); | 5029 cls.AddFunction(ctor); |
4998 } | 5030 } |
4999 | 5031 |
5000 | 5032 |
5001 // Check for cycles in constructor redirection. | 5033 // Check for cycles in constructor redirection. |
5002 void Parser::CheckConstructors(ClassDesc* class_desc) { | 5034 void Parser::CheckConstructors(ClassDesc* class_desc) { |
5003 // Check for cycles in constructor redirection. | 5035 // Check for cycles in constructor redirection. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5080 ParseInterfaceList(mixin_application); | 5112 ParseInterfaceList(mixin_application); |
5081 } | 5113 } |
5082 ExpectSemicolon(); | 5114 ExpectSemicolon(); |
5083 pending_classes.Add(mixin_application, Heap::kOld); | 5115 pending_classes.Add(mixin_application, Heap::kOld); |
5084 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { | 5116 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { |
5085 library_.AddClassMetadata(mixin_application, tl_owner, metadata_pos); | 5117 library_.AddClassMetadata(mixin_application, tl_owner, metadata_pos); |
5086 } | 5118 } |
5087 } | 5119 } |
5088 | 5120 |
5089 | 5121 |
5090 // Look ahead to detect if we are seeing ident [ TypeParameters ] "(". | 5122 // Look ahead to detect if we are seeing ident [ TypeParameters ] ("(" | "="). |
5091 // We need this lookahead to distinguish between the optional return type | 5123 // We need this lookahead to distinguish between the optional return type |
5092 // and the alias name of a function type alias. | 5124 // and the alias name of a function type alias. |
5093 // Token position remains unchanged. | 5125 // Token position remains unchanged. |
5094 bool Parser::IsFunctionTypeAliasName() { | 5126 bool Parser::IsFunctionTypeAliasName(bool* use_function_type_syntax) { |
5095 if (IsIdentifier() && (LookaheadToken(1) == Token::kLPAREN)) { | 5127 if (IsIdentifier()) { |
5096 return true; | 5128 const Token::Kind ahead = LookaheadToken(1); |
5129 if ((ahead == Token::kLPAREN) || (ahead == Token::kASSIGN)) { | |
5130 *use_function_type_syntax = (ahead == Token::kASSIGN); | |
5131 return true; | |
5132 } | |
5097 } | 5133 } |
5098 const TokenPosScope saved_pos(this); | 5134 const TokenPosScope saved_pos(this); |
5099 if (IsIdentifier() && (LookaheadToken(1) == Token::kLT)) { | 5135 if (IsIdentifier() && (LookaheadToken(1) == Token::kLT)) { |
5100 ConsumeToken(); | 5136 ConsumeToken(); |
5101 if (TryParseTypeParameters() && (CurrentToken() == Token::kLPAREN)) { | 5137 if (TryParseTypeParameters()) { |
5102 return true; | 5138 const Token::Kind current = CurrentToken(); |
5139 if ((current == Token::kLPAREN) || (current == Token::kASSIGN)) { | |
5140 *use_function_type_syntax = (current == Token::kASSIGN); | |
5141 return true; | |
5142 } | |
5103 } | 5143 } |
5104 } | 5144 } |
5145 *use_function_type_syntax = false; | |
5105 return false; | 5146 return false; |
5106 } | 5147 } |
5107 | 5148 |
5108 | 5149 |
5109 void Parser::ParseTypedef(const GrowableObjectArray& pending_classes, | 5150 void Parser::ParseTypedef(const GrowableObjectArray& pending_classes, |
5110 const Object& tl_owner, | 5151 const Object& tl_owner, |
5111 TokenPosition metadata_pos) { | 5152 TokenPosition metadata_pos) { |
5112 TRACE_PARSER("ParseTypedef"); | 5153 TRACE_PARSER("ParseTypedef"); |
5113 TokenPosition declaration_pos = | 5154 TokenPosition declaration_pos = |
5114 metadata_pos.IsReal() ? metadata_pos : TokenPos(); | 5155 metadata_pos.IsReal() ? metadata_pos : TokenPos(); |
5115 ExpectToken(Token::kTYPEDEF); | 5156 ExpectToken(Token::kTYPEDEF); |
5116 | 5157 |
5117 // Parse the result type of the function type. | 5158 // Distinguish between two possible typedef forms: |
5118 AbstractType& result_type = Type::Handle(Z, Type::DynamicType()); | 5159 // 1) returnType? identifier typeParameters? formalParameterList ’;’ |
5160 // 2) identifier typeParameters? '=' functionType ’;’ | |
5161 | |
5162 bool use_function_type_syntax; // Set to false for form 1, true for form 2. | |
5163 | |
5164 // If present, parse the result type of the function type. | |
5165 AbstractType& result_type = Type::Handle(Z); | |
5119 if (CurrentToken() == Token::kVOID) { | 5166 if (CurrentToken() == Token::kVOID) { |
5120 ConsumeToken(); | 5167 ConsumeToken(); |
5121 result_type = Type::VoidType(); | 5168 result_type = Type::VoidType(); |
5122 } else if (!IsFunctionTypeAliasName()) { | 5169 use_function_type_syntax = false; |
5170 } else if (!IsFunctionTypeAliasName(&use_function_type_syntax)) { | |
5123 // Type annotations in typedef are never ignored, even in production mode. | 5171 // Type annotations in typedef are never ignored, even in production mode. |
5124 // Wait until we have an owner class before resolving the result type. | 5172 // Wait until we have an owner class before resolving the result type. |
5125 result_type = ParseType(ClassFinalizer::kDoNotResolve); | 5173 result_type = ParseType(ClassFinalizer::kDoNotResolve); |
5174 ASSERT(!use_function_type_syntax); | |
5126 } | 5175 } |
5127 | 5176 |
5128 const TokenPosition alias_name_pos = TokenPos(); | 5177 const TokenPosition alias_name_pos = TokenPos(); |
5129 const String* alias_name = | 5178 const String* alias_name = |
5130 ExpectUserDefinedTypeIdentifier("function alias name expected"); | 5179 ExpectUserDefinedTypeIdentifier("function alias name expected"); |
5131 | 5180 |
5132 // Lookup alias name and report an error if it is already defined in | 5181 // Lookup alias name and report an error if it is already defined in |
5133 // the library scope. | 5182 // the library scope. |
5134 const Object& obj = | 5183 const Object& obj = |
5135 Object::Handle(Z, library_.LookupLocalObject(*alias_name)); | 5184 Object::Handle(Z, library_.LookupLocalObject(*alias_name)); |
5136 if (!obj.IsNull()) { | 5185 if (!obj.IsNull()) { |
5137 ReportError(alias_name_pos, "'%s' is already defined", | 5186 ReportError(alias_name_pos, "'%s' is already defined", |
5138 alias_name->ToCString()); | 5187 alias_name->ToCString()); |
5139 } | 5188 } |
5140 | 5189 |
5141 // Create the function type alias scope class. It will be linked to its | 5190 // Create the function type alias scope class. It will be linked to its |
5142 // signature function after it has been parsed. The type parameters, in order | 5191 // signature function after it has been parsed. The type parameters, in order |
5143 // to be properly finalized, need to be associated to this scope class as | 5192 // to be properly finalized, need to be associated to this scope class as |
5144 // they are parsed. | 5193 // they are parsed. |
5145 const Class& function_type_alias = Class::Handle( | 5194 const Class& function_type_alias = Class::Handle( |
5146 Z, Class::New(library_, *alias_name, script_, declaration_pos)); | 5195 Z, Class::New(library_, *alias_name, script_, declaration_pos)); |
5147 function_type_alias.set_is_synthesized_class(); | 5196 function_type_alias.set_is_synthesized_class(); |
5148 function_type_alias.set_is_abstract(); | 5197 function_type_alias.set_is_abstract(); |
5149 function_type_alias.set_is_prefinalized(); | 5198 function_type_alias.set_is_prefinalized(); |
5150 library_.AddClass(function_type_alias); | 5199 library_.AddClass(function_type_alias); |
5200 ASSERT(current_class().IsTopLevel()); | |
5151 set_current_class(function_type_alias); | 5201 set_current_class(function_type_alias); |
5152 // Parse the type parameters of the typedef class. | 5202 // Parse the type parameters of the typedef class. |
5153 ParseTypeParameters(true); // Parameterizing current class. | 5203 ParseTypeParameters(true); // Parameterizing current class. |
5154 // At this point, the type parameters have been parsed, so we can resolve the | 5204 Function& signature_function = Function::Handle(Z); |
5155 // result type. | 5205 ASSERT(innermost_function().IsNull()); |
5156 if (!result_type.IsNull()) { | 5206 if (use_function_type_syntax) { |
5157 ResolveType(ClassFinalizer::kResolveTypeParameters, &result_type); | 5207 ExpectToken(Token::kASSIGN); |
5208 ASSERT(result_type.IsNull()); // Not parsed yet. | |
5209 // Do not resolve types before the function type alias can be recognized as | |
5210 // a typedef class, so that correct promotion of function types can occur. | |
5211 const Type& function_type = Type::Handle( | |
5212 Z, ParseFunctionType(result_type, ClassFinalizer::kDoNotResolve)); | |
5213 signature_function = function_type.signature(); | |
5214 } else { | |
5215 signature_function = | |
5216 Function::NewSignatureFunction(function_type_alias, alias_name_pos); | |
5217 innermost_function_ = signature_function.raw(); | |
5218 ParamList params; | |
5219 // Parse the formal parameters of the function type. | |
5220 CheckToken(Token::kLPAREN, "formal parameter list expected"); | |
5221 // Add implicit closure object parameter. | |
5222 params.AddFinalParameter(TokenPos(), &Symbols::ClosureParameter(), | |
5223 &Object::dynamic_type()); | |
5224 const bool allow_explicit_default_values = false; | |
5225 const bool evaluate_metadata = false; | |
5226 ParseFormalParameterList(use_function_type_syntax, | |
5227 allow_explicit_default_values, evaluate_metadata, | |
5228 ¶ms); | |
5229 if (result_type.IsNull()) { | |
5230 result_type = Type::DynamicType(); | |
5231 } | |
5232 signature_function.set_result_type(result_type); | |
5233 AddFormalParamsToFunction(¶ms, signature_function); | |
5234 ASSERT(innermost_function().raw() == signature_function.raw()); | |
5235 innermost_function_ = Function::null(); | |
5158 } | 5236 } |
5159 // Parse the formal parameters of the function type. | 5237 ExpectSemicolon(); |
5160 CheckToken(Token::kLPAREN, "formal parameter list expected"); | 5238 ASSERT(innermost_function().IsNull()); |
5161 ParamList func_params; | |
5162 | 5239 |
5163 // Add implicit closure object parameter. | |
5164 func_params.AddFinalParameter(TokenPos(), &Symbols::ClosureParameter(), | |
5165 &Object::dynamic_type()); | |
5166 | |
5167 // Mark the current class as a typedef class (by setting its signature | |
5168 // function field to a non-null function) before parsing its formal parameters | |
5169 // so that parsed function types are aware that their owner class is a | |
5170 // typedef class. | |
5171 Function& signature_function = Function::Handle( | |
5172 Z, Function::NewSignatureFunction(function_type_alias, alias_name_pos)); | |
5173 ASSERT(innermost_function().IsNull()); | |
5174 innermost_function_ = signature_function.raw(); | |
5175 // Set the signature function in the function type alias class. | 5240 // Set the signature function in the function type alias class. |
5176 function_type_alias.set_signature_function(signature_function); | 5241 function_type_alias.set_signature_function(signature_function); |
5177 | 5242 |
5178 const bool no_explicit_default_values = false; | 5243 // At this point, all function type parameters have been parsed and the class |
5179 ParseFormalParameterList(no_explicit_default_values, false, &func_params); | 5244 // function_type_alias is recognized as a typedef, so we can resolve all type |
5180 ExpectSemicolon(); | 5245 // parameters in the signature type defined by the typedef. |
5181 signature_function.set_result_type(result_type); | 5246 AbstractType& function_type = |
5182 AddFormalParamsToFunction(&func_params, signature_function); | 5247 Type::Handle(Z, signature_function.SignatureType()); |
5183 | 5248 ASSERT(current_class().raw() == function_type_alias.raw()); |
5184 ASSERT(innermost_function().raw() == signature_function.raw()); | 5249 ResolveType(ClassFinalizer::kResolveTypeParameters, &function_type); |
5185 innermost_function_ = Function::null(); | 5250 // Resolving does not replace type or signature. |
5251 ASSERT(function_type_alias.signature_function() == | |
5252 Type::Cast(function_type).signature()); | |
5186 | 5253 |
5187 if (FLAG_trace_parser) { | 5254 if (FLAG_trace_parser) { |
5188 OS::Print("TopLevel parsing function type alias '%s'\n", | 5255 OS::Print("TopLevel parsing function type alias '%s'\n", |
5189 String::Handle(Z, signature_function.Signature()).ToCString()); | 5256 String::Handle(Z, signature_function.Signature()).ToCString()); |
5190 } | 5257 } |
5191 // The alias should not be marked as finalized yet, since it needs to be | 5258 // The alias should not be marked as finalized yet, since it needs to be |
5192 // checked in the class finalizer for illegal self references. | 5259 // checked in the class finalizer for illegal self references. |
5193 ASSERT(!function_type_alias.is_finalized()); | 5260 ASSERT(!function_type_alias.is_finalized()); |
5194 pending_classes.Add(function_type_alias, Heap::kOld); | 5261 pending_classes.Add(function_type_alias, Heap::kOld); |
5195 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { | 5262 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5245 } | 5312 } |
5246 } | 5313 } |
5247 return metadata_pos; | 5314 return metadata_pos; |
5248 } | 5315 } |
5249 | 5316 |
5250 | 5317 |
5251 void Parser::SkipTypeArguments() { | 5318 void Parser::SkipTypeArguments() { |
5252 if (CurrentToken() == Token::kLT) { | 5319 if (CurrentToken() == Token::kLT) { |
5253 do { | 5320 do { |
5254 ConsumeToken(); | 5321 ConsumeToken(); |
5255 SkipType(false); | 5322 SkipTypeOrFunctionType(false); |
5256 } while (CurrentToken() == Token::kCOMMA); | 5323 } while (CurrentToken() == Token::kCOMMA); |
5257 Token::Kind token = CurrentToken(); | 5324 Token::Kind token = CurrentToken(); |
5258 if ((token == Token::kGT) || (token == Token::kSHR)) { | 5325 if ((token == Token::kGT) || (token == Token::kSHR)) { |
5259 ConsumeRightAngleBracket(); | 5326 ConsumeRightAngleBracket(); |
5260 } else { | 5327 } else { |
5261 ReportError("right angle bracket expected"); | 5328 ReportError("right angle bracket expected"); |
5262 } | 5329 } |
5263 } | 5330 } |
5264 } | 5331 } |
5265 | 5332 |
5266 | 5333 |
5267 void Parser::SkipType(bool allow_void) { | 5334 void Parser::SkipType(bool allow_void) { |
5268 if (CurrentToken() == Token::kVOID) { | 5335 if (CurrentToken() == Token::kVOID) { |
5269 if (!allow_void) { | 5336 if (!allow_void) { |
5270 ReportError("'void' not allowed here"); | 5337 ReportError("'void' not allowed here"); |
5271 } | 5338 } |
5272 ConsumeToken(); | 5339 ConsumeToken(); |
5273 } else { | 5340 } else { |
5274 ExpectIdentifier("type name expected"); | 5341 ExpectIdentifier("type name expected"); |
5275 if (CurrentToken() == Token::kPERIOD) { | 5342 if (CurrentToken() == Token::kPERIOD) { |
5276 ConsumeToken(); | 5343 ConsumeToken(); |
5277 ExpectIdentifier("name expected"); | 5344 ExpectIdentifier("name expected"); |
5278 } | 5345 } |
5279 SkipTypeArguments(); | 5346 SkipTypeArguments(); |
5280 } | 5347 } |
5281 } | 5348 } |
5282 | 5349 |
5283 | 5350 |
5351 void Parser::SkipTypeOrFunctionType(bool allow_void) { | |
5352 if (CurrentToken() == Token::kVOID) { | |
5353 TokenPosition void_pos = TokenPos(); | |
5354 ConsumeToken(); | |
5355 // 'void' is always allowed as result type of a function type. | |
5356 if (!allow_void && !IsFunctionTypeSymbol()) { | |
5357 ReportError(void_pos, "'void' not allowed here"); | |
5358 } | |
5359 } else if (!IsFunctionTypeSymbol()) { | |
5360 // Including 'Function' not followed by '(' or '<'. | |
5361 SkipType(false); | |
5362 } | |
5363 while (IsSymbol(Symbols::Function())) { | |
5364 ConsumeToken(); | |
5365 SkipTypeArguments(); | |
5366 if (CurrentToken() == Token::kLPAREN) { | |
5367 SkipToMatchingParenthesis(); | |
5368 } else { | |
5369 ReportError("'(' expected"); | |
5370 } | |
5371 } | |
5372 } | |
5373 | |
5374 | |
5284 void Parser::ParseTypeParameters(bool parameterizing_class) { | 5375 void Parser::ParseTypeParameters(bool parameterizing_class) { |
5285 TRACE_PARSER("ParseTypeParameters"); | 5376 TRACE_PARSER("ParseTypeParameters"); |
5286 if (CurrentToken() == Token::kLT) { | 5377 if (CurrentToken() == Token::kLT) { |
5287 GrowableArray<AbstractType*> type_parameters_array(Z, 2); | 5378 GrowableArray<AbstractType*> type_parameters_array(Z, 2); |
5288 intptr_t index = 0; | 5379 intptr_t index = 0; |
5289 TypeParameter& type_parameter = TypeParameter::Handle(Z); | 5380 TypeParameter& type_parameter = TypeParameter::Handle(Z); |
5290 TypeParameter& existing_type_parameter = TypeParameter::Handle(Z); | 5381 TypeParameter& existing_type_parameter = TypeParameter::Handle(Z); |
5291 String& existing_type_parameter_name = String::Handle(Z); | 5382 String& existing_type_parameter_name = String::Handle(Z); |
5292 AbstractType& type_parameter_bound = Type::Handle(Z); | 5383 AbstractType& type_parameter_bound = Type::Handle(Z); |
5293 do { | 5384 do { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5369 | 5460 |
5370 | 5461 |
5371 RawTypeArguments* Parser::ParseTypeArguments( | 5462 RawTypeArguments* Parser::ParseTypeArguments( |
5372 ClassFinalizer::FinalizationKind finalization) { | 5463 ClassFinalizer::FinalizationKind finalization) { |
5373 TRACE_PARSER("ParseTypeArguments"); | 5464 TRACE_PARSER("ParseTypeArguments"); |
5374 if (CurrentToken() == Token::kLT) { | 5465 if (CurrentToken() == Token::kLT) { |
5375 GrowableArray<AbstractType*> types; | 5466 GrowableArray<AbstractType*> types; |
5376 AbstractType& type = AbstractType::Handle(Z); | 5467 AbstractType& type = AbstractType::Handle(Z); |
5377 do { | 5468 do { |
5378 ConsumeToken(); | 5469 ConsumeToken(); |
5379 type = ParseType(finalization); | 5470 type = ParseTypeOrFunctionType(false, finalization); |
5380 // Map a malformed type argument to dynamic. | 5471 // Map a malformed type argument to dynamic. |
5381 if (type.IsMalformed()) { | 5472 if (type.IsMalformed()) { |
5382 type = Type::DynamicType(); | 5473 type = Type::DynamicType(); |
5383 } | 5474 } |
5384 types.Add(&AbstractType::ZoneHandle(Z, type.raw())); | 5475 types.Add(&AbstractType::ZoneHandle(Z, type.raw())); |
5385 } while (CurrentToken() == Token::kCOMMA); | 5476 } while (CurrentToken() == Token::kCOMMA); |
5386 Token::Kind token = CurrentToken(); | 5477 Token::Kind token = CurrentToken(); |
5387 if ((token == Token::kGT) || (token == Token::kSHR)) { | 5478 if ((token == Token::kGT) || (token == Token::kSHR)) { |
5388 ConsumeRightAngleBracket(); | 5479 ConsumeRightAngleBracket(); |
5389 } else { | 5480 } else { |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5575 AbstractType& result_type = Type::Handle(Z, Type::DynamicType()); | 5666 AbstractType& result_type = Type::Handle(Z, Type::DynamicType()); |
5576 bool is_external = false; | 5667 bool is_external = false; |
5577 bool is_patch = false; | 5668 bool is_patch = false; |
5578 if (is_patch_source() && IsPatchAnnotation(metadata_pos)) { | 5669 if (is_patch_source() && IsPatchAnnotation(metadata_pos)) { |
5579 is_patch = true; | 5670 is_patch = true; |
5580 metadata_pos = TokenPosition::kNoSource; | 5671 metadata_pos = TokenPosition::kNoSource; |
5581 } else if (CurrentToken() == Token::kEXTERNAL) { | 5672 } else if (CurrentToken() == Token::kEXTERNAL) { |
5582 ConsumeToken(); | 5673 ConsumeToken(); |
5583 is_external = true; | 5674 is_external = true; |
5584 } | 5675 } |
5585 if (CurrentToken() == Token::kVOID) { | 5676 // Parse optional result type. |
5586 ConsumeToken(); | 5677 if (IsFunctionReturnType()) { |
5587 result_type = Type::VoidType(); | 5678 // It is too early to resolve the type here, since it can be a result type |
5588 } else { | 5679 // referring to a not yet declared function type parameter. |
5589 // Parse optional type. | 5680 result_type = ParseTypeOrFunctionType(true, ClassFinalizer::kDoNotResolve); |
5590 if (IsFunctionReturnType()) { | |
5591 // It is too early to resolve the type here, since it can be a result type | |
5592 // referring to a not yet declared function type parameter. | |
5593 result_type = ParseType(ClassFinalizer::kDoNotResolve); | |
5594 } | |
5595 } | 5681 } |
5596 const TokenPosition name_pos = TokenPos(); | 5682 const TokenPosition name_pos = TokenPos(); |
5597 const String& func_name = *ExpectIdentifier("function name expected"); | 5683 const String& func_name = *ExpectIdentifier("function name expected"); |
5598 | 5684 |
5599 bool found = library_.LookupLocalObject(func_name) != Object::null(); | 5685 bool found = library_.LookupLocalObject(func_name) != Object::null(); |
5600 if (found && !is_patch) { | 5686 if (found && !is_patch) { |
5601 ReportError(name_pos, "'%s' is already defined", func_name.ToCString()); | 5687 ReportError(name_pos, "'%s' is already defined", func_name.ToCString()); |
5602 } else if (!found && is_patch) { | 5688 } else if (!found && is_patch) { |
5603 ReportError(name_pos, "missing '%s' cannot be patched", | 5689 ReportError(name_pos, "missing '%s' cannot be patched", |
5604 func_name.ToCString()); | 5690 func_name.ToCString()); |
(...skipping 16 matching lines...) Expand all Loading... | |
5621 | 5707 |
5622 ASSERT(innermost_function().IsNull()); | 5708 ASSERT(innermost_function().IsNull()); |
5623 innermost_function_ = func.raw(); | 5709 innermost_function_ = func.raw(); |
5624 | 5710 |
5625 if (CurrentToken() == Token::kLT) { | 5711 if (CurrentToken() == Token::kLT) { |
5626 if (!FLAG_generic_method_syntax) { | 5712 if (!FLAG_generic_method_syntax) { |
5627 ReportError("generic functions not supported"); | 5713 ReportError("generic functions not supported"); |
5628 } | 5714 } |
5629 ParseTypeParameters(false); // Not parameterizing class, but function. | 5715 ParseTypeParameters(false); // Not parameterizing class, but function. |
5630 } | 5716 } |
5631 // At this point, the type parameters have been parsed, so we can resolve the | |
5632 // result type. | |
5633 if (!result_type.IsNull()) { | |
5634 ResolveType(ClassFinalizer::kResolveTypeParameters, &result_type); | |
5635 } | |
5636 | 5717 |
5637 CheckToken(Token::kLPAREN); | 5718 CheckToken(Token::kLPAREN); |
5638 const TokenPosition function_pos = TokenPos(); | 5719 const TokenPosition function_pos = TokenPos(); |
5639 ParamList params; | 5720 ParamList params; |
5721 const bool use_function_type_syntax = false; | |
5640 const bool allow_explicit_default_values = true; | 5722 const bool allow_explicit_default_values = true; |
5641 ParseFormalParameterList(allow_explicit_default_values, false, ¶ms); | 5723 const bool evaluate_metadata = false; |
5724 ParseFormalParameterList(use_function_type_syntax, | |
5725 allow_explicit_default_values, evaluate_metadata, | |
5726 ¶ms); | |
5642 | 5727 |
5643 const TokenPosition modifier_pos = TokenPos(); | 5728 const TokenPosition modifier_pos = TokenPos(); |
5644 RawFunction::AsyncModifier func_modifier = ParseFunctionModifier(); | 5729 RawFunction::AsyncModifier func_modifier = ParseFunctionModifier(); |
5645 | 5730 |
5646 TokenPosition function_end_pos = function_pos; | 5731 TokenPosition function_end_pos = function_pos; |
5647 bool is_native = false; | 5732 bool is_native = false; |
5648 String* native_name = NULL; | 5733 String* native_name = NULL; |
5649 if (is_external) { | 5734 if (is_external) { |
5650 function_end_pos = TokenPos(); | 5735 function_end_pos = TokenPos(); |
5651 ExpectSemicolon(); | 5736 ExpectSemicolon(); |
(...skipping 26 matching lines...) Expand all Loading... | |
5678 func.set_modifier(func_modifier); | 5763 func.set_modifier(func_modifier); |
5679 if (library_.is_dart_scheme() && library_.IsPrivate(func_name)) { | 5764 if (library_.is_dart_scheme() && library_.IsPrivate(func_name)) { |
5680 func.set_is_reflectable(false); | 5765 func.set_is_reflectable(false); |
5681 } | 5766 } |
5682 if (is_native) { | 5767 if (is_native) { |
5683 func.set_native_name(*native_name); | 5768 func.set_native_name(*native_name); |
5684 } | 5769 } |
5685 AddFormalParamsToFunction(¶ms, func); | 5770 AddFormalParamsToFunction(¶ms, func); |
5686 ASSERT(innermost_function().raw() == func.raw()); | 5771 ASSERT(innermost_function().raw() == func.raw()); |
5687 innermost_function_ = Function::null(); | 5772 innermost_function_ = Function::null(); |
5773 ResolveSignature(ClassFinalizer::kResolveTypeParameters, func); | |
5688 top_level->AddFunction(func); | 5774 top_level->AddFunction(func); |
5689 if (!is_patch) { | 5775 if (!is_patch) { |
5690 library_.AddObject(func, func_name); | 5776 library_.AddObject(func, func_name); |
5691 } else { | 5777 } else { |
5692 // Need to remove the previously added function that is being patched. | 5778 // Need to remove the previously added function that is being patched. |
5693 const Class& toplevel_cls = Class::Handle(Z, library_.toplevel_class()); | 5779 const Class& toplevel_cls = Class::Handle(Z, library_.toplevel_class()); |
5694 const Function& replaced_func = | 5780 const Function& replaced_func = |
5695 Function::Handle(Z, toplevel_cls.LookupStaticFunction(func_name)); | 5781 Function::Handle(Z, toplevel_cls.LookupStaticFunction(func_name)); |
5696 ASSERT(!replaced_func.IsNull()); | 5782 ASSERT(!replaced_func.IsNull()); |
5697 toplevel_cls.RemoveFunction(replaced_func); | 5783 toplevel_cls.RemoveFunction(replaced_func); |
(...skipping 19 matching lines...) Expand all Loading... | |
5717 metadata_pos = TokenPosition::kNoSource; | 5803 metadata_pos = TokenPosition::kNoSource; |
5718 } else if (CurrentToken() == Token::kEXTERNAL) { | 5804 } else if (CurrentToken() == Token::kEXTERNAL) { |
5719 ConsumeToken(); | 5805 ConsumeToken(); |
5720 is_external = true; | 5806 is_external = true; |
5721 } | 5807 } |
5722 bool is_getter = (CurrentToken() == Token::kGET); | 5808 bool is_getter = (CurrentToken() == Token::kGET); |
5723 if (CurrentToken() == Token::kGET || CurrentToken() == Token::kSET) { | 5809 if (CurrentToken() == Token::kGET || CurrentToken() == Token::kSET) { |
5724 ConsumeToken(); | 5810 ConsumeToken(); |
5725 result_type = Type::DynamicType(); | 5811 result_type = Type::DynamicType(); |
5726 } else { | 5812 } else { |
5727 if (CurrentToken() == Token::kVOID) { | 5813 result_type = |
5728 ConsumeToken(); | 5814 ParseTypeOrFunctionType(true, ClassFinalizer::kResolveTypeParameters); |
5729 result_type = Type::VoidType(); | |
5730 } else { | |
5731 result_type = ParseType(ClassFinalizer::kResolveTypeParameters); | |
5732 } | |
5733 is_getter = (CurrentToken() == Token::kGET); | 5815 is_getter = (CurrentToken() == Token::kGET); |
5734 if (CurrentToken() == Token::kGET || CurrentToken() == Token::kSET) { | 5816 if (CurrentToken() == Token::kGET || CurrentToken() == Token::kSET) { |
5735 ConsumeToken(); | 5817 ConsumeToken(); |
5736 } else { | 5818 } else { |
5737 UnexpectedToken(); | 5819 UnexpectedToken(); |
5738 } | 5820 } |
5739 } | 5821 } |
5740 const TokenPosition name_pos = TokenPos(); | 5822 const TokenPosition name_pos = TokenPos(); |
5741 const String* field_name = ExpectIdentifier("accessor name expected"); | 5823 const String* field_name = ExpectIdentifier("accessor name expected"); |
5742 | 5824 |
5743 const TokenPosition accessor_pos = TokenPos(); | 5825 const TokenPosition accessor_pos = TokenPos(); |
5744 ParamList params; | 5826 ParamList params; |
5745 | 5827 |
5746 if (!is_getter) { | 5828 if (!is_getter) { |
5829 const bool use_function_type_syntax = false; | |
5747 const bool allow_explicit_default_values = true; | 5830 const bool allow_explicit_default_values = true; |
5748 ParseFormalParameterList(allow_explicit_default_values, false, ¶ms); | 5831 const bool evaluate_metadata = false; |
5832 ParseFormalParameterList(use_function_type_syntax, | |
5833 allow_explicit_default_values, evaluate_metadata, | |
5834 ¶ms); | |
5749 } | 5835 } |
5750 String& accessor_name = String::ZoneHandle(Z); | 5836 String& accessor_name = String::ZoneHandle(Z); |
5751 int expected_num_parameters = -1; | 5837 int expected_num_parameters = -1; |
5752 if (is_getter) { | 5838 if (is_getter) { |
5753 expected_num_parameters = 0; | 5839 expected_num_parameters = 0; |
5754 accessor_name = Field::GetterSymbol(*field_name); | 5840 accessor_name = Field::GetterSymbol(*field_name); |
5755 } else { | 5841 } else { |
5756 expected_num_parameters = 1; | 5842 expected_num_parameters = 1; |
5757 accessor_name = Field::SetterSymbol(*field_name); | 5843 accessor_name = Field::SetterSymbol(*field_name); |
5758 } | 5844 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5832 func.set_end_token_pos(accessor_end_pos); | 5918 func.set_end_token_pos(accessor_end_pos); |
5833 func.set_modifier(func_modifier); | 5919 func.set_modifier(func_modifier); |
5834 if (is_native) { | 5920 if (is_native) { |
5835 func.set_is_debuggable(false); | 5921 func.set_is_debuggable(false); |
5836 func.set_native_name(*native_name); | 5922 func.set_native_name(*native_name); |
5837 } | 5923 } |
5838 if (library_.is_dart_scheme() && library_.IsPrivate(accessor_name)) { | 5924 if (library_.is_dart_scheme() && library_.IsPrivate(accessor_name)) { |
5839 func.set_is_reflectable(false); | 5925 func.set_is_reflectable(false); |
5840 } | 5926 } |
5841 AddFormalParamsToFunction(¶ms, func); | 5927 AddFormalParamsToFunction(¶ms, func); |
5928 ResolveSignature(ClassFinalizer::kResolveTypeParameters, func); | |
5842 top_level->AddFunction(func); | 5929 top_level->AddFunction(func); |
5843 if (!is_patch) { | 5930 if (!is_patch) { |
5844 library_.AddObject(func, accessor_name); | 5931 library_.AddObject(func, accessor_name); |
5845 } else { | 5932 } else { |
5846 // Need to remove the previously added accessor that is being patched. | 5933 // Need to remove the previously added accessor that is being patched. |
5847 const Class& toplevel_cls = Class::Handle( | 5934 const Class& toplevel_cls = Class::Handle( |
5848 Z, owner.IsClass() ? Class::Cast(owner).raw() | 5935 Z, owner.IsClass() ? Class::Cast(owner).raw() |
5849 : PatchClass::Cast(owner).patched_class()); | 5936 : PatchClass::Cast(owner).patched_class()); |
5850 const Function& replaced_func = | 5937 const Function& replaced_func = |
5851 Function::Handle(Z, toplevel_cls.LookupFunction(accessor_name)); | 5938 Function::Handle(Z, toplevel_cls.LookupFunction(accessor_name)); |
(...skipping 830 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6682 body.set_result_type(Object::dynamic_type()); | 6769 body.set_result_type(Object::dynamic_type()); |
6683 is_new_closure = true; | 6770 is_new_closure = true; |
6684 } | 6771 } |
6685 | 6772 |
6686 ParamList closure_params; | 6773 ParamList closure_params; |
6687 AddSyncGenClosureParameters(&closure_params); | 6774 AddSyncGenClosureParameters(&closure_params); |
6688 | 6775 |
6689 if (is_new_closure) { | 6776 if (is_new_closure) { |
6690 // Add the parameters to the newly created closure. | 6777 // Add the parameters to the newly created closure. |
6691 AddFormalParamsToFunction(&closure_params, body); | 6778 AddFormalParamsToFunction(&closure_params, body); |
6692 | 6779 ResolveSignature(ClassFinalizer::kResolveTypeParameters, body); |
6693 // Finalize function type. | 6780 // Finalize function type. |
6694 Type& signature_type = Type::Handle(Z, body.SignatureType()); | 6781 Type& signature_type = Type::Handle(Z, body.SignatureType()); |
6695 signature_type ^= ClassFinalizer::FinalizeType( | 6782 signature_type ^= ClassFinalizer::FinalizeType( |
6696 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 6783 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
6697 body.SetSignatureType(signature_type); | 6784 body.SetSignatureType(signature_type); |
6698 ASSERT(AbstractType::Handle(Z, body.result_type()).IsResolved()); | 6785 ASSERT(AbstractType::Handle(Z, body.result_type()).IsResolved()); |
6699 ASSERT(body.NumParameters() == closure_params.parameters->length()); | 6786 ASSERT(body.NumParameters() == closure_params.parameters->length()); |
6700 } | 6787 } |
6701 | 6788 |
6702 OpenFunctionBlock(body); | 6789 OpenFunctionBlock(body); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6810 closure.set_is_generated_body(true); | 6897 closure.set_is_generated_body(true); |
6811 closure.set_result_type(Object::dynamic_type()); | 6898 closure.set_result_type(Object::dynamic_type()); |
6812 is_new_closure = true; | 6899 is_new_closure = true; |
6813 } | 6900 } |
6814 // Create the parameter list for the async body closure. | 6901 // Create the parameter list for the async body closure. |
6815 ParamList closure_params; | 6902 ParamList closure_params; |
6816 AddAsyncClosureParameters(&closure_params); | 6903 AddAsyncClosureParameters(&closure_params); |
6817 if (is_new_closure) { | 6904 if (is_new_closure) { |
6818 // Add the parameters to the newly created closure. | 6905 // Add the parameters to the newly created closure. |
6819 AddFormalParamsToFunction(&closure_params, closure); | 6906 AddFormalParamsToFunction(&closure_params, closure); |
6907 ResolveSignature(ClassFinalizer::kResolveTypeParameters, closure); | |
6820 | 6908 |
6821 // Finalize function type. | 6909 // Finalize function type. |
6822 Type& signature_type = Type::Handle(Z, closure.SignatureType()); | 6910 Type& signature_type = Type::Handle(Z, closure.SignatureType()); |
6823 signature_type ^= ClassFinalizer::FinalizeType( | 6911 signature_type ^= ClassFinalizer::FinalizeType( |
6824 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 6912 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
6825 closure.SetSignatureType(signature_type); | 6913 closure.SetSignatureType(signature_type); |
6826 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); | 6914 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); |
6827 ASSERT(closure.NumParameters() == closure_params.parameters->length()); | 6915 ASSERT(closure.NumParameters() == closure_params.parameters->length()); |
6828 } | 6916 } |
6829 OpenFunctionBlock(closure); | 6917 OpenFunctionBlock(closure); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6939 closure.set_result_type(Object::dynamic_type()); | 7027 closure.set_result_type(Object::dynamic_type()); |
6940 is_new_closure = true; | 7028 is_new_closure = true; |
6941 } | 7029 } |
6942 | 7030 |
6943 ParamList closure_params; | 7031 ParamList closure_params; |
6944 AddAsyncGenClosureParameters(&closure_params); | 7032 AddAsyncGenClosureParameters(&closure_params); |
6945 | 7033 |
6946 if (is_new_closure) { | 7034 if (is_new_closure) { |
6947 // Add the parameters to the newly created closure. | 7035 // Add the parameters to the newly created closure. |
6948 AddFormalParamsToFunction(&closure_params, closure); | 7036 AddFormalParamsToFunction(&closure_params, closure); |
7037 ResolveSignature(ClassFinalizer::kResolveTypeParameters, closure); | |
6949 | 7038 |
6950 // Finalize function type. | 7039 // Finalize function type. |
6951 Type& signature_type = Type::Handle(Z, closure.SignatureType()); | 7040 Type& signature_type = Type::Handle(Z, closure.SignatureType()); |
6952 signature_type ^= ClassFinalizer::FinalizeType( | 7041 signature_type ^= ClassFinalizer::FinalizeType( |
6953 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 7042 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
6954 closure.SetSignatureType(signature_type); | 7043 closure.SetSignatureType(signature_type); |
6955 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); | 7044 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); |
6956 ASSERT(closure.NumParameters() == closure_params.parameters->length()); | 7045 ASSERT(closure.NumParameters() == closure_params.parameters->length()); |
6957 } | 7046 } |
6958 | 7047 |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7301 for (int i = 0; i < params.num_optional_parameters; i++) { | 7390 for (int i = 0; i < params.num_optional_parameters; i++) { |
7302 const Instance* default_value = | 7391 const Instance* default_value = |
7303 parameters[i + first_opt_param_offset].default_value; | 7392 parameters[i + first_opt_param_offset].default_value; |
7304 default_values->Add(default_value); | 7393 default_values->Add(default_value); |
7305 } | 7394 } |
7306 parsed_function()->set_default_parameter_values(default_values); | 7395 parsed_function()->set_default_parameter_values(default_values); |
7307 } | 7396 } |
7308 } | 7397 } |
7309 | 7398 |
7310 | 7399 |
7400 void Parser::FinalizeFormalParameterTypes(const ParamList* params) { | |
7401 ASSERT((params != NULL) && (params->parameters != NULL)); | |
7402 const int num_parameters = params->parameters->length(); | |
7403 AbstractType& type = AbstractType::Handle(Z); | |
7404 for (int i = 0; i < num_parameters; i++) { | |
7405 ParamDesc& param_desc = (*params->parameters)[i]; | |
7406 type = param_desc.type->raw(); | |
7407 ResolveType(ClassFinalizer::kCanonicalize, &type); | |
7408 type = ClassFinalizer::FinalizeType(current_class(), type, | |
7409 ClassFinalizer::kCanonicalize); | |
7410 if (type.raw() != param_desc.type->raw()) { | |
7411 param_desc.type = &AbstractType::ZoneHandle(Z, type.raw()); | |
7412 } | |
7413 } | |
7414 } | |
7415 | |
7416 | |
7311 // Populate the parameter type array and parameter name array of the function | 7417 // Populate the parameter type array and parameter name array of the function |
7312 // with the formal parameter types and names. | 7418 // with the formal parameter types and names. |
7313 void Parser::AddFormalParamsToFunction(const ParamList* params, | 7419 void Parser::AddFormalParamsToFunction(const ParamList* params, |
7314 const Function& func) { | 7420 const Function& func) { |
7315 ASSERT((params != NULL) && (params->parameters != NULL)); | 7421 ASSERT((params != NULL) && (params->parameters != NULL)); |
7316 ASSERT((params->num_optional_parameters > 0) == | 7422 ASSERT((params->num_optional_parameters > 0) == |
7317 (params->has_optional_positional_parameters || | 7423 (params->has_optional_positional_parameters || |
7318 params->has_optional_named_parameters)); | 7424 params->has_optional_named_parameters)); |
7319 if (!Utils::IsInt(16, params->num_fixed_parameters) || | 7425 if (!Utils::IsInt(16, params->num_fixed_parameters) || |
7320 !Utils::IsInt(16, params->num_optional_parameters)) { | 7426 !Utils::IsInt(16, params->num_optional_parameters)) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7355 | 7461 |
7356 | 7462 |
7357 // Populate local scope with the formal parameters. | 7463 // Populate local scope with the formal parameters. |
7358 void Parser::AddFormalParamsToScope(const ParamList* params, | 7464 void Parser::AddFormalParamsToScope(const ParamList* params, |
7359 LocalScope* scope) { | 7465 LocalScope* scope) { |
7360 ASSERT((params != NULL) && (params->parameters != NULL)); | 7466 ASSERT((params != NULL) && (params->parameters != NULL)); |
7361 ASSERT(scope != NULL); | 7467 ASSERT(scope != NULL); |
7362 const int num_parameters = params->parameters->length(); | 7468 const int num_parameters = params->parameters->length(); |
7363 for (int i = 0; i < num_parameters; i++) { | 7469 for (int i = 0; i < num_parameters; i++) { |
7364 ParamDesc& param_desc = (*params->parameters)[i]; | 7470 ParamDesc& param_desc = (*params->parameters)[i]; |
7365 ASSERT(!is_top_level_ || param_desc.type->IsResolved()); | |
7366 const String* name = param_desc.name; | 7471 const String* name = param_desc.name; |
7367 LocalVariable* parameter = new (Z) LocalVariable( | 7472 LocalVariable* parameter = new (Z) LocalVariable( |
7368 param_desc.name_pos, param_desc.name_pos, *name, *param_desc.type); | 7473 param_desc.name_pos, param_desc.name_pos, *name, *param_desc.type); |
7369 if (!scope->InsertParameterAt(i, parameter)) { | 7474 if (!scope->InsertParameterAt(i, parameter)) { |
7370 ReportError(param_desc.name_pos, "name '%s' already exists in scope", | 7475 ReportError(param_desc.name_pos, "name '%s' already exists in scope", |
7371 param_desc.name->ToCString()); | 7476 param_desc.name->ToCString()); |
7372 } | 7477 } |
7373 param_desc.var = parameter; | 7478 param_desc.var = parameter; |
7374 if (param_desc.is_final) { | 7479 if (param_desc.is_final) { |
7375 parameter->set_is_final(); | 7480 parameter->set_is_final(); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7518 ReportError(ident_pos, "identifier '%s' already defined", | 7623 ReportError(ident_pos, "identifier '%s' already defined", |
7519 variable->name().ToCString()); | 7624 variable->name().ToCString()); |
7520 } | 7625 } |
7521 if (is_final || is_const) { | 7626 if (is_final || is_const) { |
7522 variable->set_is_final(); | 7627 variable->set_is_final(); |
7523 } | 7628 } |
7524 return initialization; | 7629 return initialization; |
7525 } | 7630 } |
7526 | 7631 |
7527 | 7632 |
7528 // Parses ('var' | 'final' [type] | 'const' [type] | type). | 7633 // Parses ('var' | 'final' [type] | 'const' [type] | type). |
hausner
2017/01/18 18:54:53
Does the comment need to be updated? Or is the fun
regis
2017/01/18 20:24:24
The official spec has not been changed yet, but th
| |
7529 // The presence of 'final' or 'const' must be detected and remembered | 7634 // The presence of 'final' or 'const' must be detected and remembered |
7530 // before the call. If a type is parsed, it may be resolved and finalized | 7635 // before the call. If a type is parsed, it may be resolved and finalized |
7531 // according to the given type finalization mode. | 7636 // according to the given type finalization mode. |
7532 RawAbstractType* Parser::ParseConstFinalVarOrType( | 7637 RawAbstractType* Parser::ParseConstFinalVarOrType( |
7533 ClassFinalizer::FinalizationKind finalization) { | 7638 ClassFinalizer::FinalizationKind finalization) { |
7534 TRACE_PARSER("ParseConstFinalVarOrType"); | 7639 TRACE_PARSER("ParseConstFinalVarOrType"); |
7535 if (CurrentToken() == Token::kVAR) { | 7640 if (CurrentToken() == Token::kVAR) { |
7536 ConsumeToken(); | 7641 ConsumeToken(); |
7537 return Type::DynamicType(); | 7642 return Type::DynamicType(); |
7538 } | 7643 } |
7539 bool type_is_optional = false; | 7644 bool type_is_optional = false; |
7540 if ((CurrentToken() == Token::kFINAL) || (CurrentToken() == Token::kCONST)) { | 7645 if ((CurrentToken() == Token::kFINAL) || (CurrentToken() == Token::kCONST)) { |
7541 ConsumeToken(); | 7646 ConsumeToken(); |
7542 type_is_optional = true; | 7647 type_is_optional = true; |
7543 } | 7648 } |
7649 if ((CurrentToken() == Token::kVOID) || IsFunctionTypeSymbol()) { | |
7650 return ParseFunctionType(AbstractType::Handle(Z), finalization); | |
7651 } | |
7544 if (CurrentToken() != Token::kIDENT) { | 7652 if (CurrentToken() != Token::kIDENT) { |
7545 if (type_is_optional) { | 7653 if (type_is_optional) { |
7546 return Type::DynamicType(); | 7654 return Type::DynamicType(); |
7547 } else { | 7655 } else { |
7548 ReportError("type name expected"); | 7656 ReportError("type name expected"); |
7549 } | 7657 } |
7550 } | 7658 } |
7551 if (type_is_optional) { | 7659 if (type_is_optional) { |
7552 Token::Kind follower = LookaheadToken(1); | 7660 Token::Kind follower = LookaheadToken(1); |
7553 // We have an identifier followed by a 'follower' token. | 7661 // We have an identifier followed by a 'follower' token. |
7554 // We either parse a type or return now. | 7662 // We either parse a type or return now. |
7555 if ((follower != Token::kLT) && // Parameterized type. | 7663 if ((follower != Token::kLT) && // Parameterized type. |
7556 (follower != Token::kPERIOD) && // Qualified class name of type. | 7664 (follower != Token::kPERIOD) && // Qualified class name of type. |
7557 !Token::IsIdentifier(follower) && // Variable name following a type. | 7665 !Token::IsIdentifier(follower) && // Variable name following a type. |
7558 (follower != Token::kTHIS)) { // Field parameter following a type. | 7666 (follower != Token::kTHIS)) { // Field parameter following a type. |
7559 return Type::DynamicType(); | 7667 return Type::DynamicType(); |
7560 } | 7668 } |
7561 } | 7669 } |
7562 return ParseType(finalization); | 7670 return ParseTypeOrFunctionType(false, finalization); |
7563 } | 7671 } |
7564 | 7672 |
7565 | 7673 |
7566 // Returns ast nodes of the variable initialization. Variables without an | 7674 // Returns ast nodes of the variable initialization. Variables without an |
7567 // explicit initializer are initialized to null. If several variables are | 7675 // explicit initializer are initialized to null. If several variables are |
7568 // declared, the individual initializers are collected in a sequence node. | 7676 // declared, the individual initializers are collected in a sequence node. |
7569 AstNode* Parser::ParseVariableDeclarationList() { | 7677 AstNode* Parser::ParseVariableDeclarationList() { |
7570 TRACE_PARSER("ParseVariableDeclarationList"); | 7678 TRACE_PARSER("ParseVariableDeclarationList"); |
7571 SkipMetadata(); | 7679 SkipMetadata(); |
7572 bool is_final = (CurrentToken() == Token::kFINAL); | 7680 bool is_final = (CurrentToken() == Token::kFINAL); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7604 } | 7712 } |
7605 sequence->Add(declaration); | 7713 sequence->Add(declaration); |
7606 initializers = sequence; | 7714 initializers = sequence; |
7607 } | 7715 } |
7608 return initializers; | 7716 return initializers; |
7609 } | 7717 } |
7610 | 7718 |
7611 | 7719 |
7612 AstNode* Parser::ParseFunctionStatement(bool is_literal) { | 7720 AstNode* Parser::ParseFunctionStatement(bool is_literal) { |
7613 TRACE_PARSER("ParseFunctionStatement"); | 7721 TRACE_PARSER("ParseFunctionStatement"); |
7614 AbstractType& result_type = AbstractType::Handle(Z); | 7722 AbstractType& result_type = AbstractType::Handle(Z, Type::DynamicType()); |
7615 const String* function_name = NULL; | 7723 const String* function_name = NULL; |
7616 | |
7617 result_type = Type::DynamicType(); | |
7618 | |
7619 const TokenPosition function_pos = TokenPos(); | 7724 const TokenPosition function_pos = TokenPos(); |
7620 TokenPosition function_name_pos = TokenPosition::kNoSource; | 7725 TokenPosition function_name_pos = TokenPosition::kNoSource; |
7621 TokenPosition metadata_pos = TokenPosition::kNoSource; | 7726 TokenPosition metadata_pos = TokenPosition::kNoSource; |
7622 if (is_literal) { | 7727 if (is_literal) { |
7623 ASSERT(CurrentToken() == Token::kLPAREN || CurrentToken() == Token::kLT); | 7728 ASSERT(CurrentToken() == Token::kLPAREN || CurrentToken() == Token::kLT); |
7624 function_name = &Symbols::AnonymousClosure(); | 7729 function_name = &Symbols::AnonymousClosure(); |
7625 } else { | 7730 } else { |
7626 metadata_pos = SkipMetadata(); | 7731 metadata_pos = SkipMetadata(); |
7627 if (CurrentToken() == Token::kVOID) { | 7732 // Parse optional result type. |
7628 ConsumeToken(); | 7733 if (IsFunctionReturnType()) { |
7629 result_type = Type::VoidType(); | |
7630 } else if (IsFunctionReturnType()) { | |
7631 // It is too early to resolve the type here, since it can be a result type | 7734 // It is too early to resolve the type here, since it can be a result type |
7632 // referring to a not yet declared function type parameter. | 7735 // referring to a not yet declared function type parameter. |
7633 result_type = ParseType(ClassFinalizer::kDoNotResolve); | 7736 result_type = |
7737 ParseTypeOrFunctionType(true, ClassFinalizer::kDoNotResolve); | |
7634 } | 7738 } |
7635 function_name_pos = TokenPos(); | 7739 function_name_pos = TokenPos(); |
7636 function_name = ExpectIdentifier("function name expected"); | 7740 function_name = ExpectIdentifier("function name expected"); |
7637 | 7741 |
7638 // Check that the function name has not been referenced | 7742 // Check that the function name has not been referenced |
7639 // before this declaration. | 7743 // before this declaration. |
7640 ASSERT(current_block_ != NULL); | 7744 ASSERT(current_block_ != NULL); |
7641 const TokenPosition previous_pos = | 7745 const TokenPosition previous_pos = |
7642 current_block_->scope->PreviousReferencePos(*function_name); | 7746 current_block_->scope->PreviousReferencePos(*function_name); |
7643 if (previous_pos.IsReal()) { | 7747 if (previous_pos.IsReal()) { |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7993 (CurrentLiteral()->raw() == Symbols::YieldKw().raw()))); | 8097 (CurrentLiteral()->raw() == Symbols::YieldKw().raw()))); |
7994 } | 8098 } |
7995 | 8099 |
7996 | 8100 |
7997 bool Parser::IsSymbol(const String& symbol) { | 8101 bool Parser::IsSymbol(const String& symbol) { |
7998 return (CurrentLiteral()->raw() == symbol.raw()) && | 8102 return (CurrentLiteral()->raw() == symbol.raw()) && |
7999 (CurrentToken() == Token::kIDENT); | 8103 (CurrentToken() == Token::kIDENT); |
8000 } | 8104 } |
8001 | 8105 |
8002 | 8106 |
8107 // Returns true if the current token is 'Function' followed by '<' or '('. | |
8108 // 'Function' not followed by '<' or '(' denotes the Function class. | |
8109 bool Parser::IsFunctionTypeSymbol() { | |
8110 return IsSymbol(Symbols::Function()) && | |
8111 ((LookaheadToken(1) == Token::kLPAREN) || | |
8112 (LookaheadToken(1) == Token::kLT)); | |
8113 } | |
8114 | |
8115 | |
8003 // Returns true if the next tokens can be parsed as a an optionally | 8116 // Returns true if the next tokens can be parsed as a an optionally |
8004 // qualified identifier: [ident '.'] ident. | 8117 // qualified identifier: [ident '.'] ident. |
8005 // Current token position is not restored. | 8118 // Current token position is not restored. |
8006 bool Parser::TryParseQualIdent() { | 8119 bool Parser::TryParseQualIdent() { |
8007 if (CurrentToken() != Token::kIDENT) { | 8120 if (CurrentToken() != Token::kIDENT) { |
8008 return false; | 8121 return false; |
8009 } | 8122 } |
8010 ConsumeToken(); | 8123 ConsumeToken(); |
8011 if (CurrentToken() == Token::kPERIOD) { | 8124 if (CurrentToken() == Token::kPERIOD) { |
8012 ConsumeToken(); | 8125 ConsumeToken(); |
8013 if (CurrentToken() != Token::kIDENT) { | 8126 if (CurrentToken() != Token::kIDENT) { |
8014 return false; | 8127 return false; |
8015 } | 8128 } |
8016 ConsumeToken(); | 8129 ConsumeToken(); |
8017 } | 8130 } |
8018 return true; | 8131 return true; |
8019 } | 8132 } |
8020 | 8133 |
8021 | 8134 |
8022 // Returns true if the next tokens can be parsed as a type with optional | 8135 // Returns true if the next tokens can be parsed as a type with optional |
8023 // type parameters. Current token position is not restored. | 8136 // type parameters. Current token position is not restored. |
8024 bool Parser::TryParseOptionalType() { | 8137 // Allow 'void' as type if 'allow_void' is true. |
8025 if (CurrentToken() == Token::kIDENT) { | 8138 // Note that 'void Function()' is always allowed, since it is a function type |
8139 // and not the void type. | |
8140 bool Parser::TryParseType(bool allow_void) { | |
8141 bool found = false; | |
8142 if (CurrentToken() == Token::kVOID) { | |
8143 ConsumeToken(); | |
8144 if (allow_void) { | |
8145 found = true; | |
8146 } else if (!IsFunctionTypeSymbol()) { | |
8147 return false; | |
8148 } | |
8149 } else if ((CurrentToken() == Token::kIDENT) && !IsFunctionTypeSymbol()) { | |
8150 // 'Function' not followed by '(' or '<' means the Function class. | |
8026 if (!TryParseQualIdent()) { | 8151 if (!TryParseQualIdent()) { |
8027 return false; | 8152 return false; |
8028 } | 8153 } |
8029 if ((CurrentToken() == Token::kLT) && !TryParseTypeParameters()) { | 8154 if ((CurrentToken() == Token::kLT) && !TryParseTypeParameters()) { |
8030 return false; | 8155 return false; |
8031 } | 8156 } |
8157 found = true; | |
8032 } | 8158 } |
8033 return true; | 8159 while (IsSymbol(Symbols::Function())) { |
8160 ConsumeToken(); | |
8161 if ((CurrentToken() == Token::kLT) && !TryParseTypeParameters()) { | |
8162 return false; | |
8163 } | |
8164 if (CurrentToken() == Token::kLPAREN) { | |
8165 SkipToMatchingParenthesis(); | |
8166 } else { | |
8167 return false; | |
8168 } | |
8169 found = true; | |
8170 } | |
8171 return found; | |
8034 } | 8172 } |
8035 | 8173 |
8036 | 8174 |
8037 // Returns true if the next tokens can be parsed as a type with optional | |
8038 // type parameters, or keyword "void". | |
8039 // Current token position is not restored. | |
8040 bool Parser::TryParseReturnType() { | |
8041 if (CurrentToken() == Token::kVOID) { | |
8042 ConsumeToken(); | |
8043 return true; | |
8044 } else if (CurrentToken() == Token::kIDENT) { | |
8045 return TryParseOptionalType(); | |
8046 } | |
8047 return false; | |
8048 } | |
8049 | |
8050 | |
8051 // Look ahead to detect whether the next tokens should be parsed as | 8175 // Look ahead to detect whether the next tokens should be parsed as |
8052 // a variable declaration. Ignores optional metadata. | 8176 // a variable declaration. Ignores optional metadata. |
8053 // Returns true if we detect the token pattern: | 8177 // Returns true if we detect the token pattern: |
8054 // 'var' | 8178 // 'var' |
8055 // | 'final' | 8179 // | 'final' |
8056 // | const [type] ident (';' | '=' | ',') | 8180 // | const [type] ident (';' | '=' | ',') |
8057 // | type ident (';' | '=' | ',') | 8181 // | type ident (';' | '=' | ',') |
8058 // Token position remains unchanged. | 8182 // Token position remains unchanged. |
8059 bool Parser::IsVariableDeclaration() { | 8183 bool Parser::IsVariableDeclaration() { |
8060 if ((CurrentToken() == Token::kVAR) || (CurrentToken() == Token::kFINAL)) { | 8184 if ((CurrentToken() == Token::kVAR) || (CurrentToken() == Token::kFINAL)) { |
8061 return true; | 8185 return true; |
8062 } | 8186 } |
8063 // Skip optional metadata. | 8187 // Skip optional metadata. |
8064 if (CurrentToken() == Token::kAT) { | 8188 if (CurrentToken() == Token::kAT) { |
8065 const TokenPosition saved_pos = TokenPos(); | 8189 const TokenPosition saved_pos = TokenPos(); |
8066 SkipMetadata(); | 8190 SkipMetadata(); |
8067 const bool is_var_decl = IsVariableDeclaration(); | 8191 const bool is_var_decl = IsVariableDeclaration(); |
8068 SetPosition(saved_pos); | 8192 SetPosition(saved_pos); |
8069 return is_var_decl; | 8193 return is_var_decl; |
8070 } | 8194 } |
8071 if ((CurrentToken() != Token::kIDENT) && (CurrentToken() != Token::kCONST)) { | 8195 if ((CurrentToken() != Token::kIDENT) && (CurrentToken() != Token::kVOID) && |
8072 // Not a legal type identifier or const keyword or metadata. | 8196 (CurrentToken() != Token::kCONST)) { |
8197 // Not a legal type identifier or void (result type of function type) | |
8198 // or const keyword or metadata. | |
8073 return false; | 8199 return false; |
8074 } | 8200 } |
8075 const TokenPosition saved_pos = TokenPos(); | 8201 const TokenPosition saved_pos = TokenPos(); |
8076 bool is_var_decl = false; | 8202 bool is_var_decl = false; |
8077 bool have_type = false; | 8203 bool have_type = false; |
8078 if (CurrentToken() == Token::kCONST) { | 8204 if (CurrentToken() == Token::kCONST) { |
8079 ConsumeToken(); | 8205 ConsumeToken(); |
8080 have_type = true; // Type is dynamic. | 8206 have_type = true; // Type is dynamic if 'const' is not followed by a type. |
8081 } | 8207 } |
8082 if (IsIdentifier()) { // Type or variable name. | 8208 if ((CurrentToken() == Token::kVOID) || IsFunctionTypeSymbol()) { |
8209 if (TryParseType(false)) { | |
8210 have_type = true; | |
8211 } | |
8212 } else if (IsIdentifier()) { // Type or variable name. | |
8083 Token::Kind follower = LookaheadToken(1); | 8213 Token::Kind follower = LookaheadToken(1); |
8084 if ((follower == Token::kLT) || // Parameterized type. | 8214 if ((follower == Token::kLT) || // Parameterized type. |
8085 (follower == Token::kPERIOD) || // Qualified class name of type. | 8215 (follower == Token::kPERIOD) || // Qualified class name of type. |
8086 Token::IsIdentifier(follower)) { // Variable name following a type. | 8216 Token::IsIdentifier(follower)) { // Variable name following a type. |
8087 // We see the beginning of something that could be a type. | 8217 // We see the beginning of something that could be a type. |
8088 const TokenPosition type_pos = TokenPos(); | 8218 const TokenPosition type_pos = TokenPos(); |
8089 if (TryParseOptionalType()) { | 8219 if (TryParseType(false)) { |
8090 have_type = true; | 8220 have_type = true; |
8091 } else { | 8221 } else { |
8092 SetPosition(type_pos); | 8222 SetPosition(type_pos); |
8093 } | 8223 } |
8094 } | 8224 } |
8095 if (have_type && IsIdentifier()) { | 8225 } |
8096 ConsumeToken(); | 8226 if (have_type && IsIdentifier()) { |
8097 if ((CurrentToken() == Token::kSEMICOLON) || | 8227 ConsumeToken(); |
8098 (CurrentToken() == Token::kCOMMA) || | 8228 if ((CurrentToken() == Token::kSEMICOLON) || |
8099 (CurrentToken() == Token::kASSIGN)) { | 8229 (CurrentToken() == Token::kCOMMA) || |
8100 is_var_decl = true; | 8230 (CurrentToken() == Token::kASSIGN)) { |
8101 } | 8231 is_var_decl = true; |
8102 } | 8232 } |
8103 } | 8233 } |
8104 SetPosition(saved_pos); | 8234 SetPosition(saved_pos); |
8105 return is_var_decl; | 8235 return is_var_decl; |
8106 } | 8236 } |
8107 | 8237 |
8108 | 8238 |
8109 // Look ahead to see if the following tokens are a return type followed | 8239 // Look ahead to see if the following tokens are a return type followed |
8110 // by an identifier. | 8240 // by an identifier. |
8111 bool Parser::IsFunctionReturnType() { | 8241 bool Parser::IsFunctionReturnType() { |
8112 TokenPosScope decl_pos(this); | 8242 TokenPosScope decl_pos(this); |
8113 if (TryParseReturnType()) { | 8243 if (TryParseType(true)) { |
8114 if (IsIdentifier()) { | 8244 if (IsIdentifier()) { |
8115 // Return type followed by function name. | 8245 // Return type followed by function name. |
8116 return true; | 8246 return true; |
8117 } | 8247 } |
8118 } | 8248 } |
8119 return false; | 8249 return false; |
8120 } | 8250 } |
8121 | 8251 |
8122 | 8252 |
8123 // Look ahead to detect whether the next tokens should be parsed as | 8253 // Look ahead to detect whether the next tokens should be parsed as |
8124 // a function declaration. Token position remains unchanged. | 8254 // a function declaration. Token position remains unchanged. |
8125 bool Parser::IsFunctionDeclaration() { | 8255 bool Parser::IsFunctionDeclaration() { |
8126 bool is_external = false; | 8256 bool is_external = false; |
8127 TokenPosScope decl_pos(this); | 8257 TokenPosScope decl_pos(this); |
8128 SkipMetadata(); | 8258 SkipMetadata(); |
8129 if ((is_top_level_) && (CurrentToken() == Token::kEXTERNAL)) { | 8259 if ((is_top_level_) && (CurrentToken() == Token::kEXTERNAL)) { |
8130 // Skip over 'external' for top-level function declarations. | 8260 // Skip over 'external' for top-level function declarations. |
8131 is_external = true; | 8261 is_external = true; |
8132 ConsumeToken(); | 8262 ConsumeToken(); |
8133 } | 8263 } |
8134 const TokenPosition type_or_name_pos = TokenPos(); | 8264 const TokenPosition type_or_name_pos = TokenPos(); |
8135 if (TryParseReturnType()) { | 8265 if (TryParseType(true)) { |
8136 if (!IsIdentifier()) { | 8266 if (!IsIdentifier()) { |
8137 SetPosition(type_or_name_pos); | 8267 SetPosition(type_or_name_pos); |
8138 } | 8268 } |
8139 } else { | 8269 } else { |
8140 SetPosition(type_or_name_pos); | 8270 SetPosition(type_or_name_pos); |
8141 } | 8271 } |
8142 // Check for function name followed by optional type parameters. | 8272 // Check for function name followed by optional type parameters. |
8143 if (!IsIdentifier()) { | 8273 if (!IsIdentifier()) { |
8144 return false; | 8274 return false; |
8145 } | 8275 } |
(...skipping 19 matching lines...) Expand all Loading... | |
8165 | 8295 |
8166 | 8296 |
8167 bool Parser::IsTopLevelAccessor() { | 8297 bool Parser::IsTopLevelAccessor() { |
8168 const TokenPosScope saved_pos(this); | 8298 const TokenPosScope saved_pos(this); |
8169 if (CurrentToken() == Token::kEXTERNAL) { | 8299 if (CurrentToken() == Token::kEXTERNAL) { |
8170 ConsumeToken(); | 8300 ConsumeToken(); |
8171 } | 8301 } |
8172 if ((CurrentToken() == Token::kGET) || (CurrentToken() == Token::kSET)) { | 8302 if ((CurrentToken() == Token::kGET) || (CurrentToken() == Token::kSET)) { |
8173 return true; | 8303 return true; |
8174 } | 8304 } |
8175 if (TryParseReturnType()) { | 8305 if (TryParseType(true)) { |
8176 if ((CurrentToken() == Token::kGET) || (CurrentToken() == Token::kSET)) { | 8306 if ((CurrentToken() == Token::kGET) || (CurrentToken() == Token::kSET)) { |
8177 if (Token::IsIdentifier(LookaheadToken(1))) { // Accessor name. | 8307 if (Token::IsIdentifier(LookaheadToken(1))) { // Accessor name. |
8178 return true; | 8308 return true; |
8179 } | 8309 } |
8180 } | 8310 } |
8181 } | 8311 } |
8182 return false; | 8312 return false; |
8183 } | 8313 } |
8184 | 8314 |
8185 | 8315 |
(...skipping 28 matching lines...) Expand all Loading... | |
8214 // Allow const modifier as well when recognizing a for-in statement | 8344 // Allow const modifier as well when recognizing a for-in statement |
8215 // pattern. We will get an error later if the loop variable is | 8345 // pattern. We will get an error later if the loop variable is |
8216 // declared with const. | 8346 // declared with const. |
8217 if (CurrentToken() == Token::kVAR || CurrentToken() == Token::kFINAL || | 8347 if (CurrentToken() == Token::kVAR || CurrentToken() == Token::kFINAL || |
8218 CurrentToken() == Token::kCONST) { | 8348 CurrentToken() == Token::kCONST) { |
8219 ConsumeToken(); | 8349 ConsumeToken(); |
8220 } | 8350 } |
8221 if (IsIdentifier()) { | 8351 if (IsIdentifier()) { |
8222 if (LookaheadToken(1) == Token::kIN) { | 8352 if (LookaheadToken(1) == Token::kIN) { |
8223 return true; | 8353 return true; |
8224 } else if (TryParseOptionalType()) { | 8354 } else if (TryParseType(false)) { |
8225 if (IsIdentifier()) { | 8355 if (IsIdentifier()) { |
8226 ConsumeToken(); | 8356 ConsumeToken(); |
8227 } | 8357 } |
8228 return CurrentToken() == Token::kIN; | 8358 return CurrentToken() == Token::kIN; |
8229 } | 8359 } |
8230 } | 8360 } |
8231 return false; | 8361 return false; |
8232 } | 8362 } |
8233 | 8363 |
8234 | 8364 |
(...skipping 1278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
9513 while ((CurrentToken() == Token::kCATCH) || IsSymbol(Symbols::On())) { | 9643 while ((CurrentToken() == Token::kCATCH) || IsSymbol(Symbols::On())) { |
9514 // Open a block that contains the if or an unconditional body. It's | 9644 // Open a block that contains the if or an unconditional body. It's |
9515 // closed in the loop that builds the if-then-else nest. | 9645 // closed in the loop that builds the if-then-else nest. |
9516 OpenBlock(); | 9646 OpenBlock(); |
9517 const TokenPosition catch_pos = TokenPos(); | 9647 const TokenPosition catch_pos = TokenPos(); |
9518 CatchParamDesc exception_param; | 9648 CatchParamDesc exception_param; |
9519 CatchParamDesc stack_trace_param; | 9649 CatchParamDesc stack_trace_param; |
9520 if (IsSymbol(Symbols::On())) { | 9650 if (IsSymbol(Symbols::On())) { |
9521 ConsumeToken(); | 9651 ConsumeToken(); |
9522 exception_param.type = &AbstractType::ZoneHandle( | 9652 exception_param.type = &AbstractType::ZoneHandle( |
9523 Z, ParseType(ClassFinalizer::kCanonicalize)); | 9653 Z, ParseTypeOrFunctionType(false, ClassFinalizer::kCanonicalize)); |
9524 } else { | 9654 } else { |
9525 exception_param.type = &Object::dynamic_type(); | 9655 exception_param.type = &Object::dynamic_type(); |
9526 } | 9656 } |
9527 if (CurrentToken() == Token::kCATCH) { | 9657 if (CurrentToken() == Token::kCATCH) { |
9528 ConsumeToken(); // Consume the 'catch'. | 9658 ConsumeToken(); // Consume the 'catch'. |
9529 ExpectToken(Token::kLPAREN); | 9659 ExpectToken(Token::kLPAREN); |
9530 exception_param.token_pos = TokenPos(); | 9660 exception_param.token_pos = TokenPos(); |
9531 exception_param.name = ExpectIdentifier("identifier expected"); | 9661 exception_param.name = ExpectIdentifier("identifier expected"); |
9532 if (CurrentToken() == Token::kCOMMA) { | 9662 if (CurrentToken() == Token::kCOMMA) { |
9533 ConsumeToken(); | 9663 ConsumeToken(); |
(...skipping 1085 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
10619 if ((op_kind != Token::kIS) && (op_kind != Token::kAS)) { | 10749 if ((op_kind != Token::kIS) && (op_kind != Token::kAS)) { |
10620 right_operand = ParseBinaryExpr(current_preced + 1); | 10750 right_operand = ParseBinaryExpr(current_preced + 1); |
10621 } else { | 10751 } else { |
10622 // For 'is' and 'as' we expect the right operand to be a type. | 10752 // For 'is' and 'as' we expect the right operand to be a type. |
10623 if ((op_kind == Token::kIS) && (CurrentToken() == Token::kNOT)) { | 10753 if ((op_kind == Token::kIS) && (CurrentToken() == Token::kNOT)) { |
10624 ConsumeToken(); | 10754 ConsumeToken(); |
10625 op_kind = Token::kISNOT; | 10755 op_kind = Token::kISNOT; |
10626 } | 10756 } |
10627 const TokenPosition type_pos = TokenPos(); | 10757 const TokenPosition type_pos = TokenPos(); |
10628 const AbstractType& type = AbstractType::ZoneHandle( | 10758 const AbstractType& type = AbstractType::ZoneHandle( |
10629 Z, ParseType(ClassFinalizer::kCanonicalize)); | 10759 Z, ParseTypeOrFunctionType(false, ClassFinalizer::kCanonicalize)); |
10630 if (!type.IsInstantiated() && (FunctionLevel() > 0)) { | 10760 if (!type.IsInstantiated() && (FunctionLevel() > 0)) { |
10631 // Make sure that the instantiator is captured. | 10761 // Make sure that the instantiator is captured. |
10632 CaptureInstantiator(); | 10762 CaptureInstantiator(); |
10633 } | 10763 } |
10634 right_operand = new (Z) TypeNode(type_pos, type); | 10764 right_operand = new (Z) TypeNode(type_pos, type); |
10635 // In production mode, the type may be malformed. | 10765 // In production mode, the type may be malformed. |
10636 // In checked mode, the type may be malformed or malbounded. | 10766 // In checked mode, the type may be malformed or malbounded. |
10637 if (type.IsMalformedOrMalbounded()) { | 10767 if (type.IsMalformedOrMalbounded()) { |
10638 // Note that a type error is thrown in a type test or in | 10768 // Note that a type error is thrown in a type test or in |
10639 // a type cast even if the tested value is null. | 10769 // a type cast even if the tested value is null. |
(...skipping 1308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
11948 // The result is a pair of the (side effects of the) store followed by | 12078 // The result is a pair of the (side effects of the) store followed by |
11949 // the (value of the) initial value temp variable load. | 12079 // the (value of the) initial value temp variable load. |
11950 let_expr->AddNode(store); | 12080 let_expr->AddNode(store); |
11951 let_expr->AddNode(new (Z) LoadLocalNode(op_pos, temp)); | 12081 let_expr->AddNode(new (Z) LoadLocalNode(op_pos, temp)); |
11952 return let_expr; | 12082 return let_expr; |
11953 } | 12083 } |
11954 return expr; | 12084 return expr; |
11955 } | 12085 } |
11956 | 12086 |
11957 | 12087 |
12088 // Resolve the types of the given signature from the current class according to | |
12089 // the given type finalization mode. | |
12090 // Not all involved type classes may get resolved yet, but at least type | |
12091 // parameters will get resolved, thereby relieving the class | |
12092 // finalizer from resolving type parameters out of context. | |
12093 // TODO(regis): Refactor this code which is partially duplicated in the class | |
12094 // finalizer, paying attention to type parameter resolution and mixin library. | |
12095 void Parser::ResolveSignature(ClassFinalizer::FinalizationKind finalization, | |
12096 const Function& signature) { | |
12097 const Function& saved_innermost_function = | |
12098 Function::Handle(Z, innermost_function().raw()); | |
12099 innermost_function_ = signature.raw(); | |
12100 // TODO(regis): Resolve upper bounds of function type parameters. | |
12101 AbstractType& type = AbstractType::Handle(signature.result_type()); | |
12102 ResolveType(finalization, &type); | |
12103 signature.set_result_type(type); | |
12104 const intptr_t num_parameters = signature.NumParameters(); | |
12105 for (intptr_t i = 0; i < num_parameters; i++) { | |
12106 type = signature.ParameterTypeAt(i); | |
12107 ResolveType(finalization, &type); | |
12108 signature.SetParameterTypeAt(i, type); | |
12109 } | |
12110 innermost_function_ = saved_innermost_function.raw(); | |
12111 } | |
12112 | |
12113 | |
11958 // Resolve the given type and its type arguments from the current function and | 12114 // Resolve the given type and its type arguments from the current function and |
11959 // current class according to the given type finalization mode. | 12115 // current class according to the given type finalization mode. |
11960 // Not all involved type classes may get resolved yet, but at least type | 12116 // Not all involved type classes may get resolved yet, but at least type |
11961 // parameters will get resolved, thereby relieving the class | 12117 // parameters will get resolved, thereby relieving the class |
11962 // finalizer from resolving type parameters out of context. | 12118 // finalizer from resolving type parameters out of context. |
11963 // TODO(regis): Refactor this code which is partially duplicated in the class | 12119 // TODO(regis): Refactor this code which is partially duplicated in the class |
11964 // finalizer, paying attention to type parameter resolution and mixin library. | 12120 // finalizer, paying attention to type parameter resolution and mixin library. |
11965 void Parser::ResolveType(ClassFinalizer::FinalizationKind finalization, | 12121 void Parser::ResolveType(ClassFinalizer::FinalizationKind finalization, |
11966 AbstractType* type) { | 12122 AbstractType* type) { |
11967 ASSERT(finalization >= ClassFinalizer::kResolveTypeParameters); | 12123 ASSERT(finalization >= ClassFinalizer::kResolveTypeParameters); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
12075 arguments.SetTypeAt(i, type_argument); | 12231 arguments.SetTypeAt(i, type_argument); |
12076 } | 12232 } |
12077 } | 12233 } |
12078 if (type->IsFunctionType()) { | 12234 if (type->IsFunctionType()) { |
12079 const Function& signature = | 12235 const Function& signature = |
12080 Function::Handle(Z, Type::Cast(*type).signature()); | 12236 Function::Handle(Z, Type::Cast(*type).signature()); |
12081 Type& signature_type = Type::Handle(Z, signature.SignatureType()); | 12237 Type& signature_type = Type::Handle(Z, signature.SignatureType()); |
12082 if (signature_type.raw() != type->raw()) { | 12238 if (signature_type.raw() != type->raw()) { |
12083 ResolveType(finalization, &signature_type); | 12239 ResolveType(finalization, &signature_type); |
12084 } else { | 12240 } else { |
12085 AbstractType& type = AbstractType::Handle(signature.result_type()); | 12241 ResolveSignature(finalization, signature); |
12086 ResolveType(finalization, &type); | |
12087 signature.set_result_type(type); | |
12088 const intptr_t num_parameters = signature.NumParameters(); | |
12089 for (intptr_t i = 0; i < num_parameters; i++) { | |
12090 type = signature.ParameterTypeAt(i); | |
12091 ResolveType(finalization, &type); | |
12092 signature.SetParameterTypeAt(i, type); | |
12093 } | |
12094 if (signature.IsSignatureFunction()) { | 12242 if (signature.IsSignatureFunction()) { |
12095 // Drop fields that are not necessary anymore after resolution. | 12243 // Drop fields that are not necessary anymore after resolution. |
12096 // The parent function, owner, and token position of a shared | 12244 // The parent function, owner, and token position of a shared |
12097 // canonical function type are meaningless, since the canonical | 12245 // canonical function type are meaningless, since the canonical |
12098 // representent is picked arbitrarily. | 12246 // representent is picked arbitrarily. |
12099 signature.set_parent_function(Function::Handle(Z)); | 12247 signature.set_parent_function(Function::Handle(Z)); |
12100 // TODO(regis): As long as we support metadata in typedef signatures, | 12248 // TODO(regis): As long as we support metadata in typedef signatures, |
12101 // we cannot reset these fields used to reparse a typedef. | 12249 // we cannot reset these fields used to reparse a typedef. |
12102 // Note that the scope class of a typedef function type is always | 12250 // Note that the scope class of a typedef function type is always |
12103 // preserved as the typedef class (not reset to _Closure class), thereby | 12251 // preserved as the typedef class (not reset to _Closure class), thereby |
12104 // preventing sharing of canonical function types between typedefs. | 12252 // preventing sharing of canonical function types between typedefs. |
12105 // Not being shared, these fields are therefore always meaningful for | 12253 // Not being shared, these fields are therefore always meaningful for |
12106 // typedefs. | 12254 // typedefs. |
12107 if (type.HasResolvedTypeClass()) { | 12255 if (type->HasResolvedTypeClass()) { |
12108 const Class& scope_class = Class::Handle(Z, type.type_class()); | 12256 const Class& scope_class = Class::Handle(Z, type->type_class()); |
12109 if (!scope_class.IsTypedefClass()) { | 12257 if (!scope_class.IsTypedefClass()) { |
12110 signature.set_owner(Object::Handle(Z)); | 12258 signature.set_owner(Object::Handle(Z)); |
12111 signature.set_token_pos(TokenPosition::kNoSource); | 12259 signature.set_token_pos(TokenPosition::kNoSource); |
12260 if ((type->arguments() != TypeArguments::null()) && | |
12261 signature.HasInstantiatedSignature()) { | |
12262 ASSERT(scope_class.IsGeneric()); | |
12263 // Although the scope class of this function type is generic, | |
12264 // the signature of this function type does not refer to any | |
12265 // of its type parameters. Reset its scope class to _Closure. | |
12266 Type::Cast(*type).set_type_class(Class::Handle( | |
12267 Z, Isolate::Current()->object_store()->closure_class())); | |
12268 type->set_arguments(Object::null_type_arguments()); | |
12269 } | |
12112 } | 12270 } |
12113 } | 12271 } |
12114 } | 12272 } |
12115 } | 12273 } |
12116 } | 12274 } |
12117 } | 12275 } |
12118 | 12276 |
12119 | 12277 |
12120 LocalVariable* Parser::LookupLocalScope(const String& ident) { | 12278 LocalVariable* Parser::LookupLocalScope(const String& ident) { |
12121 if (current_block_ == NULL) { | 12279 if (current_block_ == NULL) { |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
12714 | 12872 |
12715 RawAbstractType* Parser::ParseType( | 12873 RawAbstractType* Parser::ParseType( |
12716 ClassFinalizer::FinalizationKind finalization, | 12874 ClassFinalizer::FinalizationKind finalization, |
12717 bool allow_deferred_type, | 12875 bool allow_deferred_type, |
12718 bool consume_unresolved_prefix) { | 12876 bool consume_unresolved_prefix) { |
12719 LibraryPrefix& prefix = LibraryPrefix::Handle(Z); | 12877 LibraryPrefix& prefix = LibraryPrefix::Handle(Z); |
12720 return ParseType(finalization, allow_deferred_type, consume_unresolved_prefix, | 12878 return ParseType(finalization, allow_deferred_type, consume_unresolved_prefix, |
12721 &prefix); | 12879 &prefix); |
12722 } | 12880 } |
12723 | 12881 |
12882 | |
12883 // Parses and returns a type or a functionType. | |
hausner
2017/01/18 18:54:53
function type
regis
2017/01/18 20:24:24
Done.
| |
12884 RawAbstractType* Parser::ParseTypeOrFunctionType( | |
12885 bool allow_void, | |
12886 ClassFinalizer::FinalizationKind finalization) { | |
12887 TRACE_PARSER("ParseTypeOrFunctionType"); | |
12888 AbstractType& type = AbstractType::Handle(Z); | |
12889 if (CurrentToken() == Token::kVOID) { | |
12890 TokenPosition void_pos = TokenPos(); | |
12891 type = Type::VoidType(); | |
12892 ConsumeToken(); | |
12893 // 'void' is always allowed as result type of a function type. | |
12894 if (!allow_void && !IsFunctionTypeSymbol()) { | |
12895 ReportError(void_pos, "'void' not allowed here"); | |
12896 } | |
12897 } else if (!IsFunctionTypeSymbol()) { | |
12898 // Including 'Function' not followed by '(' or '<'. | |
12899 // It is too early to resolve the type here, since it can | |
12900 // refer to a not yet declared function type parameter. | |
12901 type = ParseType(ClassFinalizer::kDoNotResolve); | |
12902 } | |
12903 while (IsSymbol(Symbols::Function())) { | |
hausner
2017/01/18 18:54:53
Would it make sense to change this to while(IsFunc
regis
2017/01/18 20:24:24
No, I had it that way and changed it to this, beca
| |
12904 if (type.IsNull()) { | |
12905 type = Type::DynamicType(); | |
12906 } | |
12907 // 'type' is the result type of the function type. | |
12908 type = ParseFunctionType(type, ClassFinalizer::kDoNotResolve); | |
12909 } | |
12910 // At this point, all type parameters have been parsed, resolve the type. | |
12911 if (finalization == ClassFinalizer::kIgnore) { | |
12912 return Type::DynamicType(); | |
12913 } | |
12914 if (finalization >= ClassFinalizer::kResolveTypeParameters) { | |
12915 ResolveType(finalization, &type); | |
12916 if (finalization >= ClassFinalizer::kCanonicalize) { | |
12917 type ^= ClassFinalizer::FinalizeType(current_class(), type, finalization); | |
12918 } | |
12919 } | |
12920 return type.raw(); | |
12921 } | |
12922 | |
12923 | |
12924 // Parses and returns a function type. | |
12925 // If 'result_type' is not null, parsing of the result type is skipped. | |
12926 RawType* Parser::ParseFunctionType( | |
12927 const AbstractType& result_type, | |
12928 ClassFinalizer::FinalizationKind finalization) { | |
12929 TRACE_PARSER("ParseFunctionType"); | |
12930 AbstractType& type = AbstractType::Handle(Z, result_type.raw()); | |
12931 if (type.IsNull()) { | |
12932 if (CurrentToken() == Token::kVOID) { | |
12933 ConsumeToken(); | |
12934 type = Type::VoidType(); | |
12935 } else if (IsFunctionTypeSymbol()) { | |
12936 type = Type::DynamicType(); | |
12937 } else { | |
12938 // Including 'Function' not followed by '(' or '<'. | |
12939 // It is too early to resolve the type here, since it can | |
12940 // refer to a not yet declared function type parameter. | |
12941 type = ParseType(ClassFinalizer::kDoNotResolve); | |
12942 } | |
12943 } | |
12944 if (!IsSymbol(Symbols::Function())) { | |
12945 ReportError("'Function' expected"); | |
12946 } | |
12947 do { | |
12948 ConsumeToken(); | |
12949 const Function& signature_function = | |
12950 Function::Handle(Z, Function::NewSignatureFunction( | |
12951 current_class(), TokenPosition::kNoSource)); | |
12952 signature_function.set_parent_function(innermost_function()); | |
12953 innermost_function_ = signature_function.raw(); | |
12954 signature_function.set_result_type(type); | |
12955 // Parse optional type parameters. | |
12956 if (CurrentToken() == Token::kLT) { | |
12957 if (!FLAG_generic_method_syntax) { | |
12958 ReportError("generic type arguments not supported."); | |
12959 } | |
12960 ParseTypeParameters(false); // Not parameterizing class, but function. | |
12961 } | |
12962 ParamList params; | |
12963 // We do not yet allow Function of any arity, so expect parameter list. | |
12964 CheckToken(Token::kLPAREN, "formal parameter list expected"); | |
12965 | |
12966 // Add implicit closure object parameter. Do not specify a token position, | |
12967 // since it would make no sense after function type canonicalization. | |
12968 params.AddFinalParameter(TokenPosition::kNoSource, | |
12969 &Symbols::ClosureParameter(), | |
12970 &Object::dynamic_type()); | |
12971 | |
12972 const bool use_function_type_syntax = true; | |
12973 const bool allow_explicit_default_values = false; | |
12974 const bool evaluate_metadata = false; | |
12975 ParseFormalParameterList(use_function_type_syntax, | |
12976 allow_explicit_default_values, evaluate_metadata, | |
12977 ¶ms); | |
12978 AddFormalParamsToFunction(¶ms, signature_function); | |
12979 innermost_function_ = innermost_function_.parent_function(); | |
12980 type = signature_function.SignatureType(); | |
12981 } while (IsSymbol(Symbols::Function())); | |
12982 // At this point, all type parameters have been parsed, resolve the type. | |
12983 if (finalization == ClassFinalizer::kIgnore) { | |
12984 return Type::DynamicType(); | |
12985 } | |
12986 if (finalization >= ClassFinalizer::kResolveTypeParameters) { | |
12987 ResolveType(finalization, &type); | |
12988 if (finalization >= ClassFinalizer::kCanonicalize) { | |
12989 type ^= ClassFinalizer::FinalizeType(current_class(), type, finalization); | |
12990 } | |
12991 } | |
12992 return Type::RawCast(type.raw()); | |
12993 } | |
12994 | |
12995 | |
12724 // Parses type = [ident "."] ident ["<" type { "," type } ">"], then resolve and | 12996 // Parses type = [ident "."] ident ["<" type { "," type } ">"], then resolve and |
12725 // finalize it according to the given type finalization mode. Returns prefix. | 12997 // finalize it according to the given type finalization mode. |
12998 // Returns type and sets prefix. | |
12726 RawAbstractType* Parser::ParseType( | 12999 RawAbstractType* Parser::ParseType( |
12727 ClassFinalizer::FinalizationKind finalization, | 13000 ClassFinalizer::FinalizationKind finalization, |
12728 bool allow_deferred_type, | 13001 bool allow_deferred_type, |
12729 bool consume_unresolved_prefix, | 13002 bool consume_unresolved_prefix, |
12730 LibraryPrefix* prefix) { | 13003 LibraryPrefix* prefix) { |
12731 TRACE_PARSER("ParseType"); | 13004 TRACE_PARSER("ParseType"); |
12732 CheckToken(Token::kIDENT, "type name expected"); | 13005 CheckToken(Token::kIDENT, "type name expected"); |
12733 TokenPosition ident_pos = TokenPos(); | 13006 TokenPosition ident_pos = TokenPos(); |
12734 String& type_name = String::Handle(Z); | 13007 String& type_name = String::Handle(Z); |
12735 | 13008 |
(...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
13365 // Replace the types parsed from the constructor. | 13638 // Replace the types parsed from the constructor. |
13366 params.EraseParameterTypes(); | 13639 params.EraseParameterTypes(); |
13367 | 13640 |
13368 closure = Function::NewClosureFunction(closure_name, innermost_function(), | 13641 closure = Function::NewClosureFunction(closure_name, innermost_function(), |
13369 token_pos); | 13642 token_pos); |
13370 closure.set_is_generated_body(true); | 13643 closure.set_is_generated_body(true); |
13371 closure.set_is_debuggable(false); | 13644 closure.set_is_debuggable(false); |
13372 closure.set_is_visible(false); | 13645 closure.set_is_visible(false); |
13373 closure.set_result_type(Object::dynamic_type()); | 13646 closure.set_result_type(Object::dynamic_type()); |
13374 AddFormalParamsToFunction(¶ms, closure); | 13647 AddFormalParamsToFunction(¶ms, closure); |
13648 ResolveSignature(ClassFinalizer::kResolveTypeParameters, closure); | |
13375 | 13649 |
13376 // Finalize function type. | 13650 // Finalize function type. |
13377 Type& signature_type = Type::Handle(Z, closure.SignatureType()); | 13651 Type& signature_type = Type::Handle(Z, closure.SignatureType()); |
13378 signature_type ^= ClassFinalizer::FinalizeType( | 13652 signature_type ^= ClassFinalizer::FinalizeType( |
13379 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 13653 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
13380 closure.SetSignatureType(signature_type); | 13654 closure.SetSignatureType(signature_type); |
13381 // Finalization would be premature when top-level parsing. | 13655 // Finalization would be premature when top-level parsing. |
13382 ASSERT(!is_top_level_); | 13656 ASSERT(!is_top_level_); |
13383 return closure.raw(); | 13657 return closure.raw(); |
13384 } | 13658 } |
(...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
14209 value = TryCanonicalize(value, expr_pos); | 14483 value = TryCanonicalize(value, expr_pos); |
14210 CacheConstantValue(expr_pos, value); | 14484 CacheConstantValue(expr_pos, value); |
14211 return value; | 14485 return value; |
14212 } | 14486 } |
14213 } | 14487 } |
14214 | 14488 |
14215 | 14489 |
14216 void Parser::SkipFunctionLiteral() { | 14490 void Parser::SkipFunctionLiteral() { |
14217 if (IsIdentifier()) { | 14491 if (IsIdentifier()) { |
14218 if (LookaheadToken(1) != Token::kLPAREN) { | 14492 if (LookaheadToken(1) != Token::kLPAREN) { |
14219 SkipType(true); | 14493 SkipTypeOrFunctionType(true); |
14220 } | 14494 } |
14221 ExpectIdentifier("function name expected"); | 14495 ExpectIdentifier("function name expected"); |
14222 } | 14496 } |
14223 if (CurrentToken() == Token::kLPAREN) { | 14497 if (CurrentToken() == Token::kLPAREN) { |
14224 SkipToMatchingParenthesis(); | 14498 SkipToMatchingParenthesis(); |
14225 } | 14499 } |
14226 RawFunction::AsyncModifier async_modifier = ParseFunctionModifier(); | 14500 RawFunction::AsyncModifier async_modifier = ParseFunctionModifier(); |
14227 BoolScope allow_await(&this->await_is_keyword_, | 14501 BoolScope allow_await(&this->await_is_keyword_, |
14228 async_modifier != RawFunction::kNoModifier); | 14502 async_modifier != RawFunction::kNoModifier); |
14229 if (CurrentToken() == Token::kLBRACE) { | 14503 if (CurrentToken() == Token::kLBRACE) { |
14230 SkipBlock(); | 14504 SkipBlock(); |
14231 ExpectToken(Token::kRBRACE); | 14505 ExpectToken(Token::kRBRACE); |
14232 } else if (CurrentToken() == Token::kARROW) { | 14506 } else if (CurrentToken() == Token::kARROW) { |
14233 ConsumeToken(); | 14507 ConsumeToken(); |
14234 SkipExpr(); | 14508 SkipExpr(); |
14235 } | 14509 } |
14236 } | 14510 } |
14237 | 14511 |
14238 | 14512 |
14239 // Skips function/method/constructor/getter/setter preambles until the formal | 14513 // Skips function/method/constructor/getter/setter preambles until the formal |
14240 // parameter list. It is enough to skip the tokens, since we have already | 14514 // parameter list. It is enough to skip the tokens, since we have already |
14241 // previously parsed the function. | 14515 // previously parsed the function. |
14242 void Parser::SkipFunctionPreamble() { | 14516 void Parser::SkipFunctionPreamble() { |
14243 while (true) { | 14517 while (true) { |
14244 const Token::Kind token = CurrentToken(); | 14518 const Token::Kind token = CurrentToken(); |
14519 if (IsFunctionTypeSymbol()) { | |
14520 ConsumeToken(); | |
14521 SkipTypeArguments(); | |
14522 SkipToMatchingParenthesis(); | |
14523 continue; | |
14524 } | |
14245 if (token == Token::kLPAREN) { | 14525 if (token == Token::kLPAREN) { |
14246 return; | 14526 return; |
14247 } | 14527 } |
14248 if (token == Token::kGET) { | 14528 if (token == Token::kGET) { |
14249 if (LookaheadToken(1) == Token::kLPAREN) { | 14529 if (LookaheadToken(1) == Token::kLPAREN) { |
14250 // Case: Function/method named get. | 14530 // Case: Function/method named get. |
14251 ConsumeToken(); // Parse away 'get' (the function's name). | 14531 ConsumeToken(); // Parse away 'get' (the function's name). |
14252 return; | 14532 return; |
14253 } | 14533 } |
14254 // Case: Getter. | 14534 // Case: Getter. |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
14509 SkipUnaryExpr(); | 14789 SkipUnaryExpr(); |
14510 const int min_prec = Token::Precedence(Token::kIFNULL); | 14790 const int min_prec = Token::Precedence(Token::kIFNULL); |
14511 const int max_prec = Token::Precedence(Token::kMUL); | 14791 const int max_prec = Token::Precedence(Token::kMUL); |
14512 while (((min_prec <= Token::Precedence(CurrentToken())) && | 14792 while (((min_prec <= Token::Precedence(CurrentToken())) && |
14513 (Token::Precedence(CurrentToken()) <= max_prec))) { | 14793 (Token::Precedence(CurrentToken()) <= max_prec))) { |
14514 if (CurrentToken() == Token::kIS) { | 14794 if (CurrentToken() == Token::kIS) { |
14515 ConsumeToken(); | 14795 ConsumeToken(); |
14516 if (CurrentToken() == Token::kNOT) { | 14796 if (CurrentToken() == Token::kNOT) { |
14517 ConsumeToken(); | 14797 ConsumeToken(); |
14518 } | 14798 } |
14519 SkipType(false); | 14799 SkipTypeOrFunctionType(false); |
14520 } else if (CurrentToken() == Token::kAS) { | 14800 } else if (CurrentToken() == Token::kAS) { |
14521 ConsumeToken(); | 14801 ConsumeToken(); |
14522 SkipType(false); | 14802 SkipTypeOrFunctionType(false); |
14523 } else { | 14803 } else { |
14524 ConsumeToken(); | 14804 ConsumeToken(); |
14525 SkipUnaryExpr(); | 14805 SkipUnaryExpr(); |
14526 } | 14806 } |
14527 } | 14807 } |
14528 } | 14808 } |
14529 | 14809 |
14530 | 14810 |
14531 void Parser::SkipConditionalExpr() { | 14811 void Parser::SkipConditionalExpr() { |
14532 SkipBinaryExpr(); | 14812 SkipBinaryExpr(); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
14668 const ArgumentListNode& function_args, | 14948 const ArgumentListNode& function_args, |
14669 const LocalVariable* temp_for_last_arg, | 14949 const LocalVariable* temp_for_last_arg, |
14670 bool is_super_invocation) { | 14950 bool is_super_invocation) { |
14671 UNREACHABLE(); | 14951 UNREACHABLE(); |
14672 return NULL; | 14952 return NULL; |
14673 } | 14953 } |
14674 | 14954 |
14675 } // namespace dart | 14955 } // namespace dart |
14676 | 14956 |
14677 #endif // DART_PRECOMPILED_RUNTIME | 14957 #endif // DART_PRECOMPILED_RUNTIME |
OLD | NEW |