| 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 | 6 |
| 7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/compiler_stats.h" | 10 #include "vm/compiler_stats.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 114 |
| 115 | 115 |
| 116 void ParsedFunction::SetNodeSequence(SequenceNode* node_sequence) { | 116 void ParsedFunction::SetNodeSequence(SequenceNode* node_sequence) { |
| 117 ASSERT(node_sequence_ == NULL); | 117 ASSERT(node_sequence_ == NULL); |
| 118 ASSERT(node_sequence != NULL); | 118 ASSERT(node_sequence != NULL); |
| 119 node_sequence_ = node_sequence; | 119 node_sequence_ = node_sequence; |
| 120 } | 120 } |
| 121 | 121 |
| 122 | 122 |
| 123 LocalVariable* ParsedFunction::GetSavedArgumentsDescriptorVar() const { | 123 LocalVariable* ParsedFunction::GetSavedArgumentsDescriptorVar() const { |
| 124 const int num_parameters = function().NumberOfParameters(); | 124 const int num_parameters = function().NumParameters(); |
| 125 LocalScope* scope = node_sequence()->scope(); | 125 LocalScope* scope = node_sequence()->scope(); |
| 126 if (scope->num_variables() > num_parameters) { | 126 if (scope->num_variables() > num_parameters) { |
| 127 LocalVariable* saved_args_desc_var = scope->VariableAt(num_parameters); | 127 LocalVariable* saved_args_desc_var = scope->VariableAt(num_parameters); |
| 128 ASSERT(saved_args_desc_var != NULL); | 128 ASSERT(saved_args_desc_var != NULL); |
| 129 // The scope of the formal parameters may also contain at this position | 129 // The scope of the formal parameters may also contain at this position |
| 130 // an alias for the saved arguments descriptor variable of the enclosing | 130 // an alias for the saved arguments descriptor variable of the enclosing |
| 131 // function (check its scope owner) or an internal variable such as the | 131 // function (check its scope owner) or an internal variable such as the |
| 132 // expression temp variable or the saved entry context variable (check its | 132 // expression temp variable or the saved entry context variable (check its |
| 133 // name). | 133 // name). |
| 134 if ((saved_args_desc_var->owner() == scope) && | 134 if ((saved_args_desc_var->owner() == scope) && |
| 135 saved_args_desc_var->name().StartsWith( | 135 saved_args_desc_var->name().StartsWith( |
| 136 String::Handle(Symbols::SavedArgDescVarPrefix()))) { | 136 String::Handle(Symbols::SavedArgDescVarPrefix()))) { |
| 137 return saved_args_desc_var; | 137 return saved_args_desc_var; |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 return NULL; | 140 return NULL; |
| 141 } | 141 } |
| 142 | 142 |
| 143 | 143 |
| 144 void ParsedFunction::AllocateVariables() { | 144 void ParsedFunction::AllocateVariables() { |
| 145 LocalScope* scope = node_sequence()->scope(); | 145 LocalScope* scope = node_sequence()->scope(); |
| 146 const intptr_t num_fixed_params = function().num_fixed_parameters(); | 146 const intptr_t num_fixed_params = function().num_fixed_parameters(); |
| 147 const intptr_t num_opt_pos_params = | 147 const intptr_t num_opt_params = function().NumOptionalParameters(); |
| 148 function().num_optional_positional_parameters(); | |
| 149 const intptr_t num_opt_named_params = | |
| 150 function().num_optional_named_parameters(); | |
| 151 const intptr_t num_opt_params = num_opt_pos_params + num_opt_named_params; | |
| 152 intptr_t num_params = num_fixed_params + num_opt_params; | 148 intptr_t num_params = num_fixed_params + num_opt_params; |
| 153 const bool is_native_instance_closure = | 149 const bool is_native_instance_closure = |
| 154 function().is_native() && function().IsImplicitInstanceClosureFunction(); | 150 function().is_native() && function().IsImplicitInstanceClosureFunction(); |
| 155 // Compute start indices to parameters and locals, and the number of | 151 // Compute start indices to parameters and locals, and the number of |
| 156 // parameters to copy. | 152 // parameters to copy. |
| 157 if ((num_opt_params == 0) && !is_native_instance_closure) { | 153 if ((num_opt_params == 0) && !is_native_instance_closure) { |
| 158 // Parameter i will be at fp[1 + num_params - i] and local variable | 154 // Parameter i will be at fp[1 + num_params - i] and local variable |
| 159 // j will be at fp[kFirstLocalSlotIndex - j]. | 155 // j will be at fp[kFirstLocalSlotIndex - j]. |
| 160 ASSERT(GetSavedArgumentsDescriptorVar() == NULL); | 156 ASSERT(GetSavedArgumentsDescriptorVar() == NULL); |
| 161 first_parameter_index_ = 1 + num_params; | 157 first_parameter_index_ = 1 + num_params; |
| (...skipping 1767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1929 &String::ZoneHandle(Symbols::PhaseParameter()), | 1925 &String::ZoneHandle(Symbols::PhaseParameter()), |
| 1930 &Type::ZoneHandle(Type::SmiType())); | 1926 &Type::ZoneHandle(Type::SmiType())); |
| 1931 | 1927 |
| 1932 if (func.is_const()) { | 1928 if (func.is_const()) { |
| 1933 params.SetImplicitlyFinal(); | 1929 params.SetImplicitlyFinal(); |
| 1934 } | 1930 } |
| 1935 ParseFormalParameterList(allow_explicit_default_values, ¶ms); | 1931 ParseFormalParameterList(allow_explicit_default_values, ¶ms); |
| 1936 | 1932 |
| 1937 SetupDefaultsForOptionalParams(¶ms, default_parameter_values); | 1933 SetupDefaultsForOptionalParams(¶ms, default_parameter_values); |
| 1938 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); | 1934 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); |
| 1939 ASSERT(func.NumberOfParameters() == params.parameters->length()); | 1935 ASSERT(func.NumParameters() == params.parameters->length()); |
| 1940 | 1936 |
| 1941 // Now populate function scope with the formal parameters. | 1937 // Now populate function scope with the formal parameters. |
| 1942 AddFormalParamsToScope(¶ms, current_block_->scope); | 1938 AddFormalParamsToScope(¶ms, current_block_->scope); |
| 1943 | 1939 |
| 1944 // Initialize instance fields that have an explicit initializer expression. | 1940 // Initialize instance fields that have an explicit initializer expression. |
| 1945 // The formal parameter names must not be visible to the instance | 1941 // The formal parameter names must not be visible to the instance |
| 1946 // field initializer expressions, yet the parameters must be added to | 1942 // field initializer expressions, yet the parameters must be added to |
| 1947 // the scope so the expressions use the correct offsets for 'this' when | 1943 // the scope so the expressions use the correct offsets for 'this' when |
| 1948 // storing values. We make the formal parameters temporarily invisible | 1944 // storing values. We make the formal parameters temporarily invisible |
| 1949 // while parsing the instance field initializer expressions. | 1945 // while parsing the instance field initializer expressions. |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2194 } | 2190 } |
| 2195 } | 2191 } |
| 2196 | 2192 |
| 2197 // The number of parameters and their type are not yet set in local functions, | 2193 // The number of parameters and their type are not yet set in local functions, |
| 2198 // since they are not 'top-level' parsed. | 2194 // since they are not 'top-level' parsed. |
| 2199 if (func.IsLocalFunction()) { | 2195 if (func.IsLocalFunction()) { |
| 2200 AddFormalParamsToFunction(¶ms, func); | 2196 AddFormalParamsToFunction(¶ms, func); |
| 2201 } | 2197 } |
| 2202 SetupDefaultsForOptionalParams(¶ms, default_parameter_values); | 2198 SetupDefaultsForOptionalParams(¶ms, default_parameter_values); |
| 2203 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); | 2199 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); |
| 2204 ASSERT(func.NumberOfParameters() == params.parameters->length()); | 2200 ASSERT(func.NumParameters() == params.parameters->length()); |
| 2205 | 2201 |
| 2206 // Check whether the function has any field initializer formal parameters, | 2202 // Check whether the function has any field initializer formal parameters, |
| 2207 // which are not allowed in non-constructor functions. | 2203 // which are not allowed in non-constructor functions. |
| 2208 if (params.has_field_initializer) { | 2204 if (params.has_field_initializer) { |
| 2209 for (int i = 0; i < params.parameters->length(); i++) { | 2205 for (int i = 0; i < params.parameters->length(); i++) { |
| 2210 ParamDesc& param = (*params.parameters)[i]; | 2206 ParamDesc& param = (*params.parameters)[i]; |
| 2211 if (param.is_field_initializer) { | 2207 if (param.is_field_initializer) { |
| 2212 ErrorMsg(param.name_pos, | 2208 ErrorMsg(param.name_pos, |
| 2213 "field initializer only allowed in constructors"); | 2209 "field initializer only allowed in constructors"); |
| 2214 } | 2210 } |
| (...skipping 2226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4441 | 4437 |
| 4442 | 4438 |
| 4443 // Populate the parameter type array and parameter name array of the function | 4439 // Populate the parameter type array and parameter name array of the function |
| 4444 // with the formal parameter types and names. | 4440 // with the formal parameter types and names. |
| 4445 void Parser::AddFormalParamsToFunction(const ParamList* params, | 4441 void Parser::AddFormalParamsToFunction(const ParamList* params, |
| 4446 const Function& func) { | 4442 const Function& func) { |
| 4447 ASSERT((params != NULL) && (params->parameters != NULL)); | 4443 ASSERT((params != NULL) && (params->parameters != NULL)); |
| 4448 ASSERT((params->num_optional_parameters > 0) == | 4444 ASSERT((params->num_optional_parameters > 0) == |
| 4449 (params->has_optional_positional_parameters || | 4445 (params->has_optional_positional_parameters || |
| 4450 params->has_optional_named_parameters)); | 4446 params->has_optional_named_parameters)); |
| 4451 func.SetNumberOfParameters(params->num_fixed_parameters, | 4447 if (!Utils::IsInt(16, params->num_fixed_parameters) || |
| 4452 params->num_optional_parameters, | 4448 !Utils::IsInt(16, params->num_optional_parameters)) { |
| 4453 params->has_optional_positional_parameters); | 4449 ErrorMsg("too many formal parameters"); |
| 4450 } |
| 4451 func.set_num_fixed_parameters(params->num_fixed_parameters); |
| 4452 func.SetNumOptionalParameters(params->has_optional_positional_parameters ? |
| 4453 params->num_optional_parameters : 0, |
| 4454 params->has_optional_named_parameters ? |
| 4455 params->num_optional_parameters : 0); |
| 4454 const int num_parameters = params->parameters->length(); | 4456 const int num_parameters = params->parameters->length(); |
| 4455 ASSERT(num_parameters == func.NumberOfParameters()); | 4457 ASSERT(num_parameters == func.NumParameters()); |
| 4456 func.set_parameter_types(Array::Handle(Array::New(num_parameters, | 4458 func.set_parameter_types(Array::Handle(Array::New(num_parameters, |
| 4457 Heap::kOld))); | 4459 Heap::kOld))); |
| 4458 func.set_parameter_names(Array::Handle(Array::New(num_parameters, | 4460 func.set_parameter_names(Array::Handle(Array::New(num_parameters, |
| 4459 Heap::kOld))); | 4461 Heap::kOld))); |
| 4460 for (int i = 0; i < num_parameters; i++) { | 4462 for (int i = 0; i < num_parameters; i++) { |
| 4461 ParamDesc& param_desc = (*params->parameters)[i]; | 4463 ParamDesc& param_desc = (*params->parameters)[i]; |
| 4462 ASSERT(is_top_level_ || param_desc.type->IsResolved()); | 4464 ASSERT(is_top_level_ || param_desc.type->IsResolved()); |
| 4463 func.SetParameterTypeAt(i, *param_desc.type); | 4465 func.SetParameterTypeAt(i, *param_desc.type); |
| 4464 func.SetParameterNameAt(i, *param_desc.name); | 4466 func.SetParameterNameAt(i, *param_desc.name); |
| 4465 } | 4467 } |
| (...skipping 3157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7623 // enclosing functions, or a compile error would have prevented the | 7625 // enclosing functions, or a compile error would have prevented the |
| 7624 // outermost enclosing function to be executed and we would not be compiling | 7626 // outermost enclosing function to be executed and we would not be compiling |
| 7625 // this local function. | 7627 // this local function. |
| 7626 // Therefore, look for ident directly in the formal parameter lists of the | 7628 // Therefore, look for ident directly in the formal parameter lists of the |
| 7627 // enclosing functions. | 7629 // enclosing functions. |
| 7628 // There is no need to return the owner_scope, since the caller will not | 7630 // There is no need to return the owner_scope, since the caller will not |
| 7629 // create the saved_arguments_descriptor variable, which already exists. | 7631 // create the saved_arguments_descriptor variable, which already exists. |
| 7630 Function& function = Function::Handle(innermost_function().raw()); | 7632 Function& function = Function::Handle(innermost_function().raw()); |
| 7631 String& param_name = String::Handle(); | 7633 String& param_name = String::Handle(); |
| 7632 do { | 7634 do { |
| 7633 const int num_parameters = function.NumberOfParameters(); | 7635 const int num_parameters = function.NumParameters(); |
| 7634 for (intptr_t i = 0; i < num_parameters; i++) { | 7636 for (intptr_t i = 0; i < num_parameters; i++) { |
| 7635 param_name = function.ParameterNameAt(i); | 7637 param_name = function.ParameterNameAt(i); |
| 7636 if (ident.Equals(param_name)) { | 7638 if (ident.Equals(param_name)) { |
| 7637 *owner_function = function.raw(); | 7639 *owner_function = function.raw(); |
| 7638 *owner_scope = NULL; | 7640 *owner_scope = NULL; |
| 7639 *local_index = i; | 7641 *local_index = i; |
| 7640 return true; | 7642 return true; |
| 7641 } | 7643 } |
| 7642 } | 7644 } |
| 7643 function = function.parent_function(); | 7645 function = function.parent_function(); |
| 7644 } while (!function.IsNull()); | 7646 } while (!function.IsNull()); |
| 7645 UNREACHABLE(); | 7647 UNREACHABLE(); |
| 7646 } | 7648 } |
| 7647 // Verify that local is a formal parameter of the current function or of one | 7649 // Verify that local is a formal parameter of the current function or of one |
| 7648 // of its enclosing functions. | 7650 // of its enclosing functions. |
| 7649 // Note that scopes are not yet associated to functions. | 7651 // Note that scopes are not yet associated to functions. |
| 7650 Function& function = Function::Handle(innermost_function().raw()); | 7652 Function& function = Function::Handle(innermost_function().raw()); |
| 7651 LocalScope* scope = current_block_->scope; | 7653 LocalScope* scope = current_block_->scope; |
| 7652 while (scope != NULL) { | 7654 while (scope != NULL) { |
| 7653 ASSERT(!function.IsNull()); | 7655 ASSERT(!function.IsNull()); |
| 7654 // Find the top scope for this function level. | 7656 // Find the top scope for this function level. |
| 7655 while ((scope->parent() != NULL) && | 7657 while ((scope->parent() != NULL) && |
| 7656 (scope->parent()->function_level() == scope->function_level())) { | 7658 (scope->parent()->function_level() == scope->function_level())) { |
| 7657 scope = scope->parent(); | 7659 scope = scope->parent(); |
| 7658 } | 7660 } |
| 7659 if (scope == local->owner()) { | 7661 if (scope == local->owner()) { |
| 7660 // Scope contains 'local' and the formal parameters of 'function'. | 7662 // Scope contains 'local' and the formal parameters of 'function'. |
| 7661 const int num_parameters = function.NumberOfParameters(); | 7663 const int num_parameters = function.NumParameters(); |
| 7662 for (intptr_t i = 0; i < num_parameters; i++) { | 7664 for (intptr_t i = 0; i < num_parameters; i++) { |
| 7663 if (scope->VariableAt(i) == local) { | 7665 if (scope->VariableAt(i) == local) { |
| 7664 *owner_function = function.raw(); | 7666 *owner_function = function.raw(); |
| 7665 *owner_scope = scope; | 7667 *owner_scope = scope; |
| 7666 *local_index = i; | 7668 *local_index = i; |
| 7667 return true; | 7669 return true; |
| 7668 } | 7670 } |
| 7669 } | 7671 } |
| 7670 // The variable 'local' is not a formal parameter. | 7672 // The variable 'local' is not a formal parameter. |
| 7671 return false; | 7673 return false; |
| (...skipping 1436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9108 ASSERT(owner_scope != NULL); | 9110 ASSERT(owner_scope != NULL); |
| 9109 saved_args_desc_var = | 9111 saved_args_desc_var = |
| 9110 new LocalVariable(owner_function.token_pos(), | 9112 new LocalVariable(owner_function.token_pos(), |
| 9111 saved_args_desc_name, | 9113 saved_args_desc_name, |
| 9112 Type::ZoneHandle(Type::ListInterface())); | 9114 Type::ZoneHandle(Type::ListInterface())); |
| 9113 saved_args_desc_var->set_is_final(); | 9115 saved_args_desc_var->set_is_final(); |
| 9114 // The saved arguments descriptor variable must be added just after the | 9116 // The saved arguments descriptor variable must be added just after the |
| 9115 // formal parameters. This simplifies the 2-step saving of a captured | 9117 // formal parameters. This simplifies the 2-step saving of a captured |
| 9116 // arguments descriptor. | 9118 // arguments descriptor. |
| 9117 // At this time, the owner scope should only contain formal parameters. | 9119 // At this time, the owner scope should only contain formal parameters. |
| 9118 ASSERT(owner_scope->num_variables() == owner_function.NumberOfParameters()); | 9120 ASSERT(owner_scope->num_variables() == owner_function.NumParameters()); |
| 9119 bool success = owner_scope->AddVariable(saved_args_desc_var); | 9121 bool success = owner_scope->AddVariable(saved_args_desc_var); |
| 9120 ASSERT(success); | 9122 ASSERT(success); |
| 9121 // Capture the saved argument descriptor variable if necessary. | 9123 // Capture the saved argument descriptor variable if necessary. |
| 9122 LocalVariable* local = LookupLocalScope(saved_args_desc_name); | 9124 LocalVariable* local = LookupLocalScope(saved_args_desc_name); |
| 9123 ASSERT(local == saved_args_desc_var); | 9125 ASSERT(local == saved_args_desc_var); |
| 9124 } | 9126 } |
| 9125 // If we currently generate code for the local function of an enclosing owner | 9127 // If we currently generate code for the local function of an enclosing owner |
| 9126 // function, the saved arguments descriptor variable must have been captured | 9128 // function, the saved arguments descriptor variable must have been captured |
| 9127 // by the above lookup. | 9129 // by the above lookup. |
| 9128 ASSERT((owner_function.raw() == innermost_function().raw()) || | 9130 ASSERT((owner_function.raw() == innermost_function().raw()) || |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9557 void Parser::SkipQualIdent() { | 9559 void Parser::SkipQualIdent() { |
| 9558 ASSERT(IsIdentifier()); | 9560 ASSERT(IsIdentifier()); |
| 9559 ConsumeToken(); | 9561 ConsumeToken(); |
| 9560 if (CurrentToken() == Token::kPERIOD) { | 9562 if (CurrentToken() == Token::kPERIOD) { |
| 9561 ConsumeToken(); // Consume the kPERIOD token. | 9563 ConsumeToken(); // Consume the kPERIOD token. |
| 9562 ExpectIdentifier("identifier expected after '.'"); | 9564 ExpectIdentifier("identifier expected after '.'"); |
| 9563 } | 9565 } |
| 9564 } | 9566 } |
| 9565 | 9567 |
| 9566 } // namespace dart | 9568 } // namespace dart |
| OLD | NEW |