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

Side by Side Diff: runtime/vm/parser.cc

Issue 9233039: Store resolved library prefix in unresolved class object in order not to repeat (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/object.cc ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 } 263 }
264 264
265 265
266 // A QualIdent is an optionally qualified identifier. 266 // A QualIdent is an optionally qualified identifier.
267 struct QualIdent { 267 struct QualIdent {
268 QualIdent() { 268 QualIdent() {
269 Clear(); 269 Clear();
270 } 270 }
271 void Clear() { 271 void Clear() {
272 lib_prefix = NULL; 272 lib_prefix = NULL;
273 qualifier = NULL;
274 ident_pos = 0; 273 ident_pos = 0;
275 ident = NULL; 274 ident = NULL;
276 } 275 }
277 LibraryPrefix* lib_prefix; 276 LibraryPrefix* lib_prefix;
278 String* qualifier;
279 intptr_t ident_pos; 277 intptr_t ident_pos;
280 String* ident; 278 String* ident;
281 }; 279 };
282 280
283 281
284 struct ParamDesc { 282 struct ParamDesc {
285 ParamDesc() 283 ParamDesc()
286 : type(NULL), 284 : type(NULL),
287 name_pos(0), 285 name_pos(0),
288 name(NULL), 286 name(NULL),
(...skipping 1610 matching lines...) Expand 10 before | Expand all | Expand 10 after
1899 SetAllowFunctionLiterals(false); 1897 SetAllowFunctionLiterals(false);
1900 SkipExpr(); 1898 SkipExpr();
1901 SetAllowFunctionLiterals(true); 1899 SetAllowFunctionLiterals(true);
1902 } 1900 }
1903 } while (CurrentToken() == Token::kCOMMA); 1901 } while (CurrentToken() == Token::kCOMMA);
1904 } 1902 }
1905 1903
1906 1904
1907 void Parser::ParseQualIdent(QualIdent* qual_ident) { 1905 void Parser::ParseQualIdent(QualIdent* qual_ident) {
1908 ASSERT(IsIdentifier()); 1906 ASSERT(IsIdentifier());
1909 if (!is_top_level_) { 1907 qual_ident->ident_pos = token_index_;
1910 qual_ident->ident_pos = token_index_; 1908 qual_ident->ident = CurrentLiteral();
1911 qual_ident->ident = CurrentLiteral(); 1909 qual_ident->lib_prefix = NULL;
1912 qual_ident->lib_prefix = NULL; 1910 ConsumeToken();
1913 qual_ident->qualifier = NULL; 1911 if (CurrentToken() == Token::kPERIOD) {
1914 ConsumeToken(); 1912 // An identifier cannot be resolved in a local scope when top level parsing.
1915 if (CurrentToken() == Token::kPERIOD) { 1913 if (is_top_level_ ||
1916 if (!ResolveIdentInLocalScope(qual_ident->ident_pos, 1914 !ResolveIdentInLocalScope(qual_ident->ident_pos,
1917 *(qual_ident->ident), 1915 *(qual_ident->ident),
1918 NULL)) { 1916 NULL)) {
1919 LibraryPrefix& lib_prefix = LibraryPrefix::ZoneHandle(); 1917 LibraryPrefix& lib_prefix = LibraryPrefix::ZoneHandle();
1920 lib_prefix = current_class().LookupLibraryPrefix(*(qual_ident->ident)); 1918 lib_prefix = current_class().LookupLibraryPrefix(*(qual_ident->ident));
1921 if (!lib_prefix.IsNull()) { 1919 if (!lib_prefix.IsNull()) {
1922 // We have a library prefix qualified identifier, unless the prefix is 1920 // We have a library prefix qualified identifier, unless the prefix is
1923 // shadowed by a type parameter in scope. 1921 // shadowed by a type parameter in scope.
1924 const Class& scope_class = Class::Handle(TypeParametersScopeClass()); 1922 const Class& scope_class = Class::Handle(TypeParametersScopeClass());
1925 if (scope_class.IsNull() || 1923 if (scope_class.IsNull() ||
1926 (scope_class.LookupTypeParameter(*(qual_ident->ident)) == 1924 (scope_class.LookupTypeParameter(*(qual_ident->ident)) ==
1927 TypeParameter::null())) { 1925 TypeParameter::null())) {
1928 ConsumeToken(); // Consume the kPERIOD token. 1926 ConsumeToken(); // Consume the kPERIOD token.
1929 qual_ident->lib_prefix = &lib_prefix; 1927 qual_ident->lib_prefix = &lib_prefix;
1930 qual_ident->qualifier = qual_ident->ident; 1928 qual_ident->ident_pos = token_index_;
1931 qual_ident->ident_pos = token_index_; 1929 qual_ident->ident =
1932 qual_ident->ident = 1930 ExpectIdentifier("identifier expected after '.'");
1933 ExpectIdentifier("identifier expected after '.'");
1934 }
1935 } 1931 }
1936 } 1932 }
1937 } 1933 }
1938 } else {
1939 qual_ident->ident_pos = token_index_;
1940 qual_ident->ident = CurrentLiteral();
1941 qual_ident->lib_prefix = NULL;
1942 qual_ident->qualifier = NULL;
1943 ConsumeToken();
1944 if (CurrentToken() == Token::kPERIOD) {
1945 ConsumeToken(); // Consume the kPERIOD token.
1946 qual_ident->qualifier = qual_ident->ident;
1947 qual_ident->ident_pos = token_index_;
1948 qual_ident->ident = ExpectIdentifier("identifier expected after '.'");
1949 }
1950 } 1934 }
1951 } 1935 }
1952 1936
1953 1937
1954 void Parser::ParseMethodOrConstructor(ClassDesc* members, MemberDesc* method) { 1938 void Parser::ParseMethodOrConstructor(ClassDesc* members, MemberDesc* method) {
1955 ASSERT(CurrentToken() == Token::kLPAREN); 1939 ASSERT(CurrentToken() == Token::kLPAREN);
1956 intptr_t method_pos = this->token_index_; 1940 intptr_t method_pos = this->token_index_;
1957 ASSERT(method->type != NULL); 1941 ASSERT(method->type != NULL);
1958 ASSERT(method->name_pos > 0); 1942 ASSERT(method->name_pos > 0);
1959 ASSERT(current_member_ == method); 1943 ASSERT(current_member_ == method);
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
2331 // The declared type of fields is never ignored, even in unchecked mode, 2315 // The declared type of fields is never ignored, even in unchecked mode,
2332 // because getters and setters could be closurized at some time (not 2316 // because getters and setters could be closurized at some time (not
2333 // supported yet). 2317 // supported yet).
2334 member.type = &AbstractType::ZoneHandle(ParseType(kCanResolve)); 2318 member.type = &AbstractType::ZoneHandle(ParseType(kCanResolve));
2335 } 2319 }
2336 } 2320 }
2337 } 2321 }
2338 // Optionally parse a (possibly named) constructor name or factory. 2322 // Optionally parse a (possibly named) constructor name or factory.
2339 if (IsIdentifier() && 2323 if (IsIdentifier() &&
2340 (CurrentLiteral()->Equals(members->class_name()) || member.has_factory)) { 2324 (CurrentLiteral()->Equals(members->class_name()) || member.has_factory)) {
2341 member.name = CurrentLiteral();
2342 member.name_pos = this->token_index_;
2343 // TODO(regis): Simplify the following code by calling ParseQualIdent().
2344 ConsumeToken();
2345 if (member.has_factory) { 2325 if (member.has_factory) {
2346 String& qualifier = String::Handle(); 2326 // The factory name may be qualified.
2347 if (CurrentToken() == Token::kPERIOD) { 2327 QualIdent factory_name;
2348 LibraryPrefix& lib_prefix = LibraryPrefix::ZoneHandle(); 2328 ParseQualIdent(&factory_name);
2349 lib_prefix = current_class().LookupLibraryPrefix(*member.name); 2329 member.name_pos = factory_name.ident_pos;
2350 if (!lib_prefix.IsNull()) { 2330 member.name = factory_name.ident; // Unqualified identifier.
2351 // We have a library prefix qualified identifier. 2331 // The class of the factory result type is specified by the factory name.
2352 ConsumeToken(); // Consume the kPERIOD token. 2332 const Object& result_type_class = Object::Handle(
2353 qualifier ^= member.name->raw(); 2333 LookupTypeClass(factory_name, kCanResolve));
2354 member.name = ExpectIdentifier("identifier expected");
2355 }
2356 }
2357 // TODO(regis): Stop postponing resolution of the factory result type
2358 // until class finalization. Use ResolveTypeFromClass instead.
2359 // Factory result type is the same as the type name of the factory.
2360 const UnresolvedClass& unresolved_factory_class =
2361 UnresolvedClass::Handle(UnresolvedClass::New(member.name_pos,
2362 qualifier,
2363 *member.name));
2364 // The type arguments of the result type are set during finalization. 2334 // The type arguments of the result type are set during finalization.
2365 member.type = &Type::ZoneHandle( 2335 member.type = &Type::ZoneHandle(
2366 Type::NewParameterizedType(unresolved_factory_class, 2336 Type::New(result_type_class, TypeArguments::Handle()));
2367 TypeArguments::Handle())); 2337 } else {
2338 member.name_pos = token_index_;
2339 member.name = CurrentLiteral();
2340 ConsumeToken();
2368 } 2341 }
2369 // We must be dealing with a constructor or named constructor. 2342 // We must be dealing with a constructor or named constructor.
2370 member.kind = RawFunction::kConstructor; 2343 member.kind = RawFunction::kConstructor;
2371 String& ctor_suffix = String::ZoneHandle(String::NewSymbol(".")); 2344 String& ctor_suffix = String::ZoneHandle(String::NewSymbol("."));
2372 if (CurrentToken() == Token::kPERIOD) { 2345 if (CurrentToken() == Token::kPERIOD) {
2373 // Named constructor. 2346 // Named constructor.
2374 ConsumeToken(); 2347 ConsumeToken();
2375 const String* name = ExpectIdentifier("identifier expected"); 2348 const String* name = ExpectIdentifier("identifier expected");
2376 ctor_suffix = String::Concat(ctor_suffix, *name); 2349 ctor_suffix = String::Concat(ctor_suffix, *name);
2377 } 2350 }
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
2639 } 2612 }
2640 SetPosition(saved_pos); 2613 SetPosition(saved_pos);
2641 return is_alias_name; 2614 return is_alias_name;
2642 } 2615 }
2643 2616
2644 2617
2645 void Parser::ParseFunctionTypeAlias(GrowableArray<const Class*>* classes) { 2618 void Parser::ParseFunctionTypeAlias(GrowableArray<const Class*>* classes) {
2646 TRACE_PARSER("ParseFunctionTypeAlias"); 2619 TRACE_PARSER("ParseFunctionTypeAlias");
2647 ExpectToken(Token::kTYPEDEF); 2620 ExpectToken(Token::kTYPEDEF);
2648 2621
2622 // Allocate an interface to hold the type parameters and their 'extends'
2623 // constraints. Make it the owner of the function type descriptor.
2624 const Class& alias_owner = Class::Handle(
2625 Class::New(String::Handle(String::NewSymbol(":alias_owner")),
2626 Script::Handle()));
2627 alias_owner.set_is_interface();
2628 alias_owner.set_library(library_);
2629 set_current_class(alias_owner);
2630
2631 // Parse the result type of the function type.
2649 AbstractType& result_type = Type::Handle(Type::DynamicType()); 2632 AbstractType& result_type = Type::Handle(Type::DynamicType());
2650 const intptr_t result_type_pos = token_index_; 2633 const intptr_t result_type_pos = token_index_;
2651 if (CurrentToken() == Token::kVOID) { 2634 if (CurrentToken() == Token::kVOID) {
2652 ConsumeToken(); 2635 ConsumeToken();
2653 result_type = Type::VoidType(); 2636 result_type = Type::VoidType();
2654 } else if (!IsFunctionTypeAliasName()) { 2637 } else if (!IsFunctionTypeAliasName()) {
2655 // Type annotations in typedef are never ignored, even in unchecked mode. 2638 // Type annotations in typedef are never ignored, even in unchecked mode.
2656 // Wait until we have an owner class before resolving the result type. 2639 // Wait until we have an owner class before resolving the result type.
2657 result_type = ParseType(kDoNotResolve); 2640 result_type = ParseType(kDoNotResolve);
2658 } 2641 }
2659 2642
2660 const intptr_t alias_name_pos = token_index_; 2643 const intptr_t alias_name_pos = token_index_;
2661 const String* alias_name = 2644 const String* alias_name =
2662 ExpectTypeIdentifier("function alias name expected"); 2645 ExpectTypeIdentifier("function alias name expected");
2663 2646
2664 // Allocate an interface to hold the type parameters and their 'extends' 2647 // Parse the type parameters of the function type.
2665 // constraints. Make it the owner of the function type descriptor.
2666 const Class& alias_owner = Class::Handle(
2667 Class::New(String::Handle(String::NewSymbol(":alias_owner")),
2668 Script::Handle()));
2669 alias_owner.set_is_interface();
2670 set_current_class(alias_owner);
2671 ParseTypeParameters(alias_owner); 2648 ParseTypeParameters(alias_owner);
2672 if (CurrentToken() != Token::kLPAREN) {
2673 ErrorMsg("formal parameter list expected");
2674 }
2675
2676 // At this point, the type parameters have been parsed, so we can resolve the 2649 // At this point, the type parameters have been parsed, so we can resolve the
2677 // result type. 2650 // result type.
2678 if (!result_type.IsNull()) { 2651 if (!result_type.IsNull()) {
2679 ResolveTypeFromClass(result_type_pos, 2652 ResolveTypeFromClass(result_type_pos,
2680 alias_owner, 2653 alias_owner,
2681 kCanResolve, 2654 kCanResolve,
2682 &result_type); 2655 &result_type);
2683 } 2656 }
2657 // Parse the formal parameters of the function type.
2658 if (CurrentToken() != Token::kLPAREN) {
2659 ErrorMsg("formal parameter list expected");
2660 }
2684 ParamList func_params; 2661 ParamList func_params;
2685 const bool no_explicit_default_values = false; 2662 const bool no_explicit_default_values = false;
2686 ParseFormalParameterList(no_explicit_default_values, &func_params); 2663 ParseFormalParameterList(no_explicit_default_values, &func_params);
2687 // The field 'is_static' has no meaning for signature functions. 2664 // The field 'is_static' has no meaning for signature functions.
2688 Function& signature_function = Function::Handle( 2665 Function& signature_function = Function::Handle(
2689 Function::New(*alias_name, 2666 Function::New(*alias_name,
2690 RawFunction::kSignatureFunction, 2667 RawFunction::kSignatureFunction,
2691 /* is_static = */ false, 2668 /* is_static = */ false,
2692 /* is_const = */ false, 2669 /* is_const = */ false,
2693 alias_name_pos)); 2670 alias_name_pos));
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
2777 } 2754 }
2778 2755
2779 if (CurrentToken() == Token::kDEFAULT) { 2756 if (CurrentToken() == Token::kDEFAULT) {
2780 ConsumeToken(); 2757 ConsumeToken();
2781 if (CurrentToken() != Token::kIDENT) { 2758 if (CurrentToken() != Token::kIDENT) {
2782 ErrorMsg("class name expected"); 2759 ErrorMsg("class name expected");
2783 } 2760 }
2784 const intptr_t factory_pos = token_index_; 2761 const intptr_t factory_pos = token_index_;
2785 QualIdent factory_name; 2762 QualIdent factory_name;
2786 ParseQualIdent(&factory_name); 2763 ParseQualIdent(&factory_name);
2787 String& qualifier = String::Handle(); 2764 LibraryPrefix& lib_prefix = LibraryPrefix::Handle();
2788 if (factory_name.qualifier != NULL) { 2765 if (factory_name.lib_prefix != NULL) {
2789 qualifier ^= factory_name.qualifier->raw(); 2766 lib_prefix = factory_name.lib_prefix->raw();
2790 } 2767 }
2791 const UnresolvedClass& unresolved_factory_class = UnresolvedClass::Handle( 2768 const UnresolvedClass& unresolved_factory_class = UnresolvedClass::Handle(
2792 UnresolvedClass::New(factory_pos, qualifier, *factory_name.ident)); 2769 UnresolvedClass::New(factory_pos, lib_prefix, *factory_name.ident));
2793 const Class& factory_class = Class::Handle( 2770 const Class& factory_class = Class::Handle(
2794 Class::New(String::Handle(String::NewSymbol(":factory_signature")), 2771 Class::New(String::Handle(String::NewSymbol(":factory_signature")),
2795 script_)); 2772 script_));
2796 factory_class.set_library(library_); 2773 factory_class.set_library(library_);
2797 factory_class.set_is_finalized(); 2774 factory_class.set_is_finalized();
2798 ParseTypeParameters(factory_class); 2775 ParseTypeParameters(factory_class);
2799 unresolved_factory_class.set_factory_signature_class(factory_class); 2776 unresolved_factory_class.set_factory_signature_class(factory_class);
2800 interface.set_factory_class(unresolved_factory_class); 2777 interface.set_factory_class(unresolved_factory_class);
2801 // If a type parameter list is included in the default factory clause (it 2778 // If a type parameter list is included in the default factory clause (it
2802 // can be omitted), verify that it matches the list of type parameters of 2779 // can be omitted), verify that it matches the list of type parameters of
(...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after
3765 LocalVariable* function_variable = NULL; 3742 LocalVariable* function_variable = NULL;
3766 Type& function_type = Type::ZoneHandle(); 3743 Type& function_type = Type::ZoneHandle();
3767 if (variable_name != NULL) { 3744 if (variable_name != NULL) {
3768 // Since the function type depends on the signature of the closure function, 3745 // Since the function type depends on the signature of the closure function,
3769 // it cannot be determined before the formal parameter list of the closure 3746 // it cannot be determined before the formal parameter list of the closure
3770 // function is parsed. Therefore, we set the function type to a new 3747 // function is parsed. Therefore, we set the function type to a new
3771 // parameterized type to be patched after the actual type is known. 3748 // parameterized type to be patched after the actual type is known.
3772 // We temporarily use the class of the Function interface. 3749 // We temporarily use the class of the Function interface.
3773 const Class& unknown_signature_class = Class::Handle( 3750 const Class& unknown_signature_class = Class::Handle(
3774 Type::Handle(Type::FunctionInterface()).type_class()); 3751 Type::Handle(Type::FunctionInterface()).type_class());
3775 function_type = Type::New(unknown_signature_class, 3752 function_type = Type::New(unknown_signature_class, TypeArguments::Handle());
3776 TypeArguments::Handle());
3777 function_type.set_is_finalized(); // No real finalization needed. 3753 function_type.set_is_finalized(); // No real finalization needed.
3778 3754
3779 // Add the function variable to the scope before parsing the function in 3755 // Add the function variable to the scope before parsing the function in
3780 // order to allow self reference from inside the function. 3756 // order to allow self reference from inside the function.
3781 function_variable = new LocalVariable(ident_pos, 3757 function_variable = new LocalVariable(ident_pos,
3782 *variable_name, 3758 *variable_name,
3783 function_type); 3759 function_type);
3784 function_variable->set_is_final(); 3760 function_variable->set_is_final();
3785 ASSERT(current_block_ != NULL); 3761 ASSERT(current_block_ != NULL);
3786 ASSERT(current_block_->scope != NULL); 3762 ASSERT(current_block_->scope != NULL);
(...skipping 2346 matching lines...) Expand 10 before | Expand all | Expand 10 after
6133 postfix_expr->MakeIncrOpNode(postfix_expr_pos, incr_op, false); 6109 postfix_expr->MakeIncrOpNode(postfix_expr_pos, incr_op, false);
6134 if (incr_op_node == NULL) { 6110 if (incr_op_node == NULL) {
6135 Unimplemented("incr op not implemented"); 6111 Unimplemented("incr op not implemented");
6136 } 6112 }
6137 postfix_expr = incr_op_node; 6113 postfix_expr = incr_op_node;
6138 } 6114 }
6139 return postfix_expr; 6115 return postfix_expr;
6140 } 6116 }
6141 6117
6142 6118
6143 // Resolve the given type and its type arguments from the given class according 6119 // Resolve the given type and its type arguments from the given scope class
6144 // to the given type_resolution. 6120 // according to the given type_resolution.
6121 // If the given scope class is null, use the current library, but do not try to
6122 // resolve type parameters.
6145 // Not all involved type classes may get resolved yet, but at least the type 6123 // Not all involved type classes may get resolved yet, but at least the type
6146 // parameters of the given class will get resolved, thereby relieving the class 6124 // parameters of the given class will get resolved, thereby relieving the class
6147 // finalizer from resolving type parameters out of context. 6125 // finalizer from resolving type parameters out of context.
6148 void Parser::ResolveTypeFromClass(intptr_t type_pos, 6126 void Parser::ResolveTypeFromClass(intptr_t type_pos,
6149 const Class& cls, 6127 const Class& scope_class,
6150 TypeResolution type_resolution, 6128 TypeResolution type_resolution,
6151 AbstractType* type) { 6129 AbstractType* type) {
6152 // TODO(regis): Implement kMustResolve functionality and consolidate with 6130 ASSERT((type_resolution == kCanResolve) || (type_resolution == kMustResolve));
6153 // other resolution code. For now, only support kCanResolve functionality.
6154 ASSERT(type_resolution == kCanResolve);
6155 ASSERT(type != NULL); 6131 ASSERT(type != NULL);
6156 // Resolve class. 6132 // Resolve class.
6157 if (!type->HasResolvedTypeClass()) { 6133 if (!type->HasResolvedTypeClass()) {
6158 const UnresolvedClass& unresolved_class = 6134 const UnresolvedClass& unresolved_class =
6159 UnresolvedClass::Handle(type->unresolved_class()); 6135 UnresolvedClass::Handle(type->unresolved_class());
6160 const String& unresolved_class_name = 6136 const String& unresolved_class_name =
6161 String::Handle(unresolved_class.ident()); 6137 String::Handle(unresolved_class.ident());
6162 // First check if the type is a type parameter of the given class. 6138 // First resolve library prefix if any.
6163 const TypeParameter& type_parameter = TypeParameter::Handle( 6139 Library& lib = Library::Handle();
6164 cls.LookupTypeParameter(unresolved_class_name)); 6140 if (unresolved_class.library_prefix() == LibraryPrefix::null()) {
6165 if (!type_parameter.IsNull()) { 6141 if (scope_class.IsNull()) {
6166 // A type parameter cannot be parameterized, so report an error if type 6142 lib = library_.raw();
6167 // arguments have previously been parsed. 6143 } else {
6168 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) { 6144 lib = scope_class.library();
6169 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized", 6145 // First check if the type is a type parameter of the given scope class.
6170 type_parameter.ToCString()); 6146 const TypeParameter& type_parameter = TypeParameter::Handle(
6147 scope_class.LookupTypeParameter(unresolved_class_name));
6148 if (!type_parameter.IsNull()) {
6149 // A type parameter cannot be parameterized, so report an error if
6150 // type arguments have previously been parsed.
6151 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) {
6152 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized",
6153 String::Handle(type_parameter.Name()).ToCString());
6154 }
6155 *type = type_parameter.raw();
6156 return;
6157 }
6171 } 6158 }
6172 *type = type_parameter.raw(); 6159 } else {
6173 return; 6160 LibraryPrefix& lib_prefix =
6161 LibraryPrefix::Handle(unresolved_class.library_prefix());
6162 lib = lib_prefix.library();
6174 } 6163 }
6175 const Class& resolved_type_class = 6164 if (!lib.IsNull()) {
6176 Class::Handle(LookupClass(unresolved_class_name)); 6165 const Class& resolved_type_class = Class::Handle(
6177 if (!resolved_type_class.IsNull()) { 6166 lib.LookupLocalClass(unresolved_class_name));
6178 Object& type_class = Object::Handle(resolved_type_class.raw()); 6167 if (!resolved_type_class.IsNull()) {
6179 ASSERT(type->IsType()); 6168 Object& type_class = Object::Handle(resolved_type_class.raw());
6180 // Replace unresolved class with resolved type class. 6169 ASSERT(type->IsType());
6181 Type& parameterized_type = Type::Handle(); 6170 // Replace unresolved class with resolved type class.
6182 parameterized_type ^= type->raw(); 6171 Type& parameterized_type = Type::Handle();
6183 parameterized_type.set_type_class(type_class); 6172 parameterized_type ^= type->raw();
6173 parameterized_type.set_type_class(type_class);
6174 } else if (type_resolution == kMustResolve) {
6175 ErrorMsg(type_pos, "type '%s' is not loaded",
6176 String::Handle(type->Name()).ToCString());
6177 }
6184 } 6178 }
6185 } 6179 }
6186 // Resolve type arguments, if any. 6180 // Resolve type arguments, if any.
6187 const AbstractTypeArguments& arguments = 6181 const AbstractTypeArguments& arguments =
6188 AbstractTypeArguments::Handle(type->arguments()); 6182 AbstractTypeArguments::Handle(type->arguments());
6189 if (!arguments.IsNull()) { 6183 if (!arguments.IsNull()) {
6190 const intptr_t num_arguments = arguments.Length(); 6184 const intptr_t num_arguments = arguments.Length();
6191 for (intptr_t i = 0; i < num_arguments; i++) { 6185 for (intptr_t i = 0; i < num_arguments; i++) {
6192 AbstractType& type_argument = AbstractType::Handle(arguments.TypeAt(i)); 6186 AbstractType& type_argument = AbstractType::Handle(arguments.TypeAt(i));
6193 ResolveTypeFromClass(type_pos, cls, type_resolution, &type_argument); 6187 ResolveTypeFromClass(type_pos,
6188 scope_class,
6189 type_resolution,
6190 &type_argument);
6194 arguments.SetTypeAt(i, type_argument); 6191 arguments.SetTypeAt(i, type_argument);
6195 } 6192 }
6196 } 6193 }
6197 } 6194 }
6198 6195
6199 6196
6200 // Return class for type name. If the name cannot be resolved (yet), give an 6197 // Return class for type name. If the name cannot be resolved (yet), give an
6201 // error (if type_resolution == kMustResolve) or return the unresolved name. 6198 // error (if type_resolution == kMustResolve) or return the unresolved name.
6202 RawObject* Parser::LookupTypeClass(const QualIdent& type_name, 6199 RawObject* Parser::LookupTypeClass(const QualIdent& type_name,
6203 TypeResolution type_resolution) { 6200 TypeResolution type_resolution) {
6204 ASSERT(type_name.ident != NULL); 6201 ASSERT(type_name.ident != NULL);
6205 ASSERT((type_resolution == kCanResolve) || (type_resolution == kMustResolve)); 6202 ASSERT((type_resolution == kCanResolve) || (type_resolution == kMustResolve));
6206 Class& type_class = Class::Handle(); 6203 Class& type_class = Class::Handle();
6207 if (type_name.lib_prefix != NULL) { 6204 if (type_name.lib_prefix != NULL) {
6208 Library& lib = Library::Handle(type_name.lib_prefix->library()); 6205 Library& lib = Library::Handle(type_name.lib_prefix->library());
6209 type_class = lib.LookupLocalClass(*type_name.ident); 6206 type_class = lib.LookupLocalClass(*type_name.ident);
6210 } else { 6207 } else {
6211 type_class = LookupClass(*type_name.ident); 6208 type_class = LookupClass(*type_name.ident);
6212 } 6209 }
6213 if (!type_class.IsNull()) { 6210 if (!type_class.IsNull()) {
6214 return type_class.raw(); 6211 return type_class.raw();
6215 } 6212 }
6216 // Type name could not be resolved (yet). 6213 // Type name could not be resolved (yet).
6217 if (type_resolution == kMustResolve) { 6214 if (type_resolution == kMustResolve) {
6218 ErrorMsg(type_name.ident_pos, "type '%s' is not loaded", 6215 ErrorMsg(type_name.ident_pos, "type '%s' is not loaded",
6219 type_name.ident->ToCString()); 6216 type_name.ident->ToCString());
6220 return Object::null_class(); 6217 return Object::null_class();
6221 } 6218 }
6222 // We have an unresolved name, create an UnresolvedClass object 6219 // We have an unresolved name, create an UnresolvedClass object for this case.
6223 // for this case. 6220 LibraryPrefix& lib_prefix = LibraryPrefix::Handle();
6224 String& qualifier = String::Handle(); 6221 if (type_name.lib_prefix != NULL) {
6225 if (type_name.qualifier != NULL) { 6222 lib_prefix = type_name.lib_prefix->raw();
6226 qualifier ^= type_name.qualifier->raw();
6227 } 6223 }
6228 return UnresolvedClass::New(type_name.ident_pos, qualifier, *type_name.ident); 6224 return UnresolvedClass::New(type_name.ident_pos,
6225 lib_prefix,
6226 *type_name.ident);
6229 } 6227 }
6230 6228
6231 6229
6232 LocalVariable* Parser::LookupLocalScope(const String& ident) { 6230 LocalVariable* Parser::LookupLocalScope(const String& ident) {
6233 if (current_block_ == NULL) { 6231 if (current_block_ == NULL) {
6234 return NULL; 6232 return NULL;
6235 } 6233 }
6236 // A found name is treated as accessed and possibly marked as captured. 6234 // A found name is treated as accessed and possibly marked as captured.
6237 const bool kTestOnly = false; 6235 const bool kTestOnly = false;
6238 return current_block_->scope->LookupVariable(ident, kTestOnly); 6236 return current_block_->scope->LookupVariable(ident, kTestOnly);
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
6538 } 6536 }
6539 if (!obj.IsNull()) { 6537 if (!obj.IsNull()) {
6540 ASSERT(obj.IsFunction()); 6538 ASSERT(obj.IsFunction());
6541 func ^= obj.raw(); 6539 func ^= obj.raw();
6542 ASSERT(func.is_static()); 6540 ASSERT(func.is_static());
6543 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); 6541 ASSERT(AbstractType::Handle(func.result_type()).IsResolved());
6544 return new StaticGetterNode(qual_ident.ident_pos, 6542 return new StaticGetterNode(qual_ident.ident_pos,
6545 Class::ZoneHandle(func.owner()), 6543 Class::ZoneHandle(func.owner()),
6546 *qual_ident.ident); 6544 *qual_ident.ident);
6547 } 6545 }
6548 if (qual_ident.qualifier != NULL) { 6546 if (qual_ident.lib_prefix != NULL) {
6549 // This is an unresolved prefixed primary identifier, need to report 6547 // This is an unresolved prefixed primary identifier, need to report
6550 // an error. 6548 // an error.
6551 ErrorMsg(qual_ident.ident_pos, "identifier '%s.%s' cannot be resolved", 6549 ErrorMsg(qual_ident.ident_pos, "identifier '%s.%s' cannot be resolved",
6552 (qual_ident.qualifier)->ToCString(), 6550 String::Handle(qual_ident.lib_prefix->name()).ToCString(),
6553 (qual_ident.ident)->ToCString()); 6551 qual_ident.ident->ToCString());
6554 } 6552 }
6555 // Lexically unresolved primary identifiers are referenced by their name. 6553 // Lexically unresolved primary identifiers are referenced by their name.
6556 return new PrimaryNode(qual_ident.ident_pos, *qual_ident.ident); 6554 return new PrimaryNode(qual_ident.ident_pos, *qual_ident.ident);
6557 } 6555 }
6558 6556
6559 6557
6560 // Resolve identifier, issue an error message if the name refers to 6558 // Resolve identifier, issue an error message if the name refers to
6561 // a method or a class/interface. 6559 // a method or a class/interface.
6562 // If the name cannot be resolved, turn it into an instance field access 6560 // If the name cannot be resolved, turn it into an instance field access
6563 // if we're compiling an instance method, or issue an error message 6561 // if we're compiling an instance method, or issue an error message
6564 // if we're compiling a static method. 6562 // if we're compiling a static method.
6565 AstNode* Parser::ResolveVarOrField(intptr_t ident_pos, const String& ident) { 6563 AstNode* Parser::ResolveVarOrField(intptr_t ident_pos, const String& ident) {
6566 TRACE_PARSER("ResolveVarOrField"); 6564 TRACE_PARSER("ResolveVarOrField");
6567 // First try to find the variable in the local scope (block scope or 6565 // First try to find the variable in the local scope (block scope or
6568 // class scope). 6566 // class scope).
6569 AstNode* var_or_field = NULL; 6567 AstNode* var_or_field = NULL;
6570 ResolveIdentInLocalScope(ident_pos, ident, &var_or_field); 6568 ResolveIdentInLocalScope(ident_pos, ident, &var_or_field);
6571 if (var_or_field == NULL) { 6569 if (var_or_field == NULL) {
6572 // Not found in the local scope, so try finding the variable in the 6570 // Not found in the local scope, so try finding the variable in the
6573 // library scope (current library and all libraries imported by it). 6571 // library scope (current library and all libraries imported by it).
6574 QualIdent qual_ident; 6572 QualIdent qual_ident;
6575 qual_ident.qualifier = NULL;
6576 qual_ident.lib_prefix = NULL; 6573 qual_ident.lib_prefix = NULL;
6577 qual_ident.ident_pos = ident_pos; 6574 qual_ident.ident_pos = ident_pos;
6578 qual_ident.ident = &(String::ZoneHandle(ident.raw())); 6575 qual_ident.ident = &(String::ZoneHandle(ident.raw()));
6579 var_or_field = ResolveIdentInLibraryScope(library_, 6576 var_or_field = ResolveIdentInLibraryScope(library_,
6580 qual_ident, 6577 qual_ident,
6581 kResolveIncludingImports); 6578 kResolveIncludingImports);
6582 } 6579 }
6583 if (var_or_field->IsPrimaryNode()) { 6580 if (var_or_field->IsPrimaryNode()) {
6584 PrimaryNode* primary = var_or_field->AsPrimaryNode(); 6581 PrimaryNode* primary = var_or_field->AsPrimaryNode();
6585 if (primary->primary().IsString()) { 6582 if (primary->primary().IsString()) {
(...skipping 28 matching lines...) Expand all
6614 RawAbstractType* Parser::ParseType(TypeResolution type_resolution) { 6611 RawAbstractType* Parser::ParseType(TypeResolution type_resolution) {
6615 if (CurrentToken() != Token::kIDENT) { 6612 if (CurrentToken() != Token::kIDENT) {
6616 ErrorMsg("type name expected"); 6613 ErrorMsg("type name expected");
6617 } 6614 }
6618 QualIdent type_name; 6615 QualIdent type_name;
6619 const intptr_t type_pos = token_index_; 6616 const intptr_t type_pos = token_index_;
6620 if (type_resolution == kIgnore) { 6617 if (type_resolution == kIgnore) {
6621 SkipQualIdent(); 6618 SkipQualIdent();
6622 } else { 6619 } else {
6623 ParseQualIdent(&type_name); 6620 ParseQualIdent(&type_name);
6624 if (!is_top_level_ && (type_name.qualifier == NULL) && 6621 // An identifier cannot be resolved in a local scope when top level parsing.
6622 if (!is_top_level_ &&
6623 (type_name.lib_prefix == NULL) &&
6625 ResolveIdentInLocalScope(type_pos, *type_name.ident, NULL)) { 6624 ResolveIdentInLocalScope(type_pos, *type_name.ident, NULL)) {
6626 ErrorMsg(type_pos, "using '%s' in this context is invalid", 6625 ErrorMsg(type_pos, "using '%s' in this context is invalid",
6627 type_name.ident->ToCString()); 6626 type_name.ident->ToCString());
6628 } 6627 }
6629 } 6628 }
6630 Class& scope_class = Class::Handle(); 6629 Class& scope_class = Class::Handle();
6631 Object& type_class = Object::Handle(); 6630 Object& type_class = Object::Handle();
6632 if (type_resolution == kIgnore) { 6631 if (type_resolution == kIgnore) {
6633 // Leave type_class as null. 6632 // Leave type_class as null.
6634 } else if (type_resolution == kDoNotResolve) { 6633 } else if (type_resolution == kDoNotResolve) {
6635 String& qualifier = String::Handle(); 6634 LibraryPrefix& lib_prefix = LibraryPrefix::Handle();
6636 if (type_name.qualifier != NULL) { 6635 if (type_name.lib_prefix != NULL) {
6637 qualifier ^= type_name.qualifier->raw(); 6636 lib_prefix = type_name.lib_prefix->raw();
6638 } 6637 }
6639 type_class = UnresolvedClass::New(type_pos, qualifier, *type_name.ident); 6638 type_class = UnresolvedClass::New(type_pos, lib_prefix, *type_name.ident);
6640 } else { 6639 } else {
6640 // TODO(regis): Use ResolveTypeFromClass().
6641 ASSERT((type_resolution == kCanResolve) || 6641 ASSERT((type_resolution == kCanResolve) ||
6642 (type_resolution == kMustResolve)); 6642 (type_resolution == kMustResolve));
6643 scope_class = TypeParametersScopeClass(); 6643 scope_class = TypeParametersScopeClass();
6644 if (!scope_class.IsNull()) { 6644 if (!scope_class.IsNull()) {
6645 if (type_name.qualifier != NULL) { 6645 if (type_name.lib_prefix == NULL) {
6646 // Check if qualifier is a type parameter in scope, unless it was
6647 // already checked by ParseQualIdent when !is_top_level_.
6648 if (is_top_level_) {
6649 const TypeParameter& type_parameter = TypeParameter::Handle(
6650 scope_class.LookupTypeParameter(*type_name.qualifier));
6651 if (!type_parameter.IsNull()) {
6652 ErrorMsg(type_pos,
6653 "type Parameter '%s' cannot be used as qualifier",
6654 type_name.qualifier->ToCString());
6655 }
6656 }
6657 } else {
6658 // Check if ident is a type parameter in scope. 6646 // Check if ident is a type parameter in scope.
6659 TypeParameter& type_parameter = TypeParameter::Handle( 6647 TypeParameter& type_parameter = TypeParameter::Handle(
6660 scope_class.LookupTypeParameter(*type_name.ident)); 6648 scope_class.LookupTypeParameter(*type_name.ident));
6661 if (!type_parameter.IsNull()) { 6649 if (!type_parameter.IsNull()) {
6662 if (CurrentToken() == Token::kLT) { 6650 if (CurrentToken() == Token::kLT) {
6663 // A type parameter cannot be parameterized. 6651 // A type parameter cannot be parameterized.
6664 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized", 6652 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized",
6665 String::Handle(type_parameter.Name()).ToCString()); 6653 String::Handle(type_parameter.Name()).ToCString());
6666 } 6654 }
6667 if (type_resolution == kMustResolve) { 6655 if (type_resolution == kMustResolve) {
(...skipping 11 matching lines...) Expand all
6679 } 6667 }
6680 } 6668 }
6681 // Try to resolve the type class. 6669 // Try to resolve the type class.
6682 type_class = LookupTypeClass(type_name, type_resolution); 6670 type_class = LookupTypeClass(type_name, type_resolution);
6683 } 6671 }
6684 AbstractTypeArguments& type_arguments = 6672 AbstractTypeArguments& type_arguments =
6685 AbstractTypeArguments::Handle(ParseTypeArguments(type_resolution)); 6673 AbstractTypeArguments::Handle(ParseTypeArguments(type_resolution));
6686 if (type_resolution == kIgnore) { 6674 if (type_resolution == kIgnore) {
6687 return Type::DynamicType(); 6675 return Type::DynamicType();
6688 } 6676 }
6689 Type& type = Type::Handle( 6677 Type& type = Type::Handle(Type::New(type_class, type_arguments));
6690 Type::NewParameterizedType(type_class, type_arguments));
6691 if (type_resolution == kMustResolve) { 6678 if (type_resolution == kMustResolve) {
6692 ASSERT(type_class.IsClass()); // Must be resolved. 6679 ASSERT(type_class.IsClass()); // Must be resolved.
6693 Error& error = Error::Handle(); 6680 Error& error = Error::Handle();
6694 type ^= ClassFinalizer::FinalizeAndCanonicalizeType(scope_class, 6681 type ^= ClassFinalizer::FinalizeAndCanonicalizeType(scope_class,
6695 type, 6682 type,
6696 &error); 6683 &error);
6697 if (!error.IsNull()) { 6684 if (!error.IsNull()) {
6698 ErrorMsg(error.ToErrorCString()); 6685 ErrorMsg(error.ToErrorCString());
6699 } 6686 }
6700 } 6687 }
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after
7227 if (i < num_type_arguments) { 7214 if (i < num_type_arguments) {
7228 type_argument = type_arguments.TypeAt(i); 7215 type_argument = type_arguments.TypeAt(i);
7229 } else { 7216 } else {
7230 type_argument = Type::DynamicType(); 7217 type_argument = Type::DynamicType();
7231 } 7218 }
7232 temp_type_arguments.SetTypeAt(i, type_argument); 7219 temp_type_arguments.SetTypeAt(i, type_argument);
7233 } 7220 }
7234 } 7221 }
7235 // TODO(regis): Temporary type should be allocated in new gen heap. 7222 // TODO(regis): Temporary type should be allocated in new gen heap.
7236 Type& temp_type = Type::Handle( 7223 Type& temp_type = Type::Handle(
7237 Type::NewParameterizedType(constructor_class, temp_type_arguments)); 7224 Type::New(constructor_class, temp_type_arguments));
7238 Error& error = Error::Handle(); 7225 Error& error = Error::Handle();
7239 const Class& scope_class = Class::Handle(TypeParametersScopeClass()); 7226 const Class& scope_class = Class::Handle(TypeParametersScopeClass());
7240 temp_type ^= ClassFinalizer::FinalizeAndCanonicalizeType(scope_class, 7227 temp_type ^= ClassFinalizer::FinalizeAndCanonicalizeType(scope_class,
7241 temp_type, 7228 temp_type,
7242 &error); 7229 &error);
7243 if (!error.IsNull()) { 7230 if (!error.IsNull()) {
7244 ErrorMsg(error.ToErrorCString()); 7231 ErrorMsg(error.ToErrorCString());
7245 } 7232 }
7246 // The type argument vector may have been expanded with the type arguments 7233 // The type argument vector may have been expanded with the type arguments
7247 // of the super type when finalizing the temporary type. 7234 // of the super type when finalizing the temporary type.
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
7344 AstNode* primary = NULL; 7331 AstNode* primary = NULL;
7345 if (IsFunctionLiteral()) { 7332 if (IsFunctionLiteral()) {
7346 // The name of a literal function is visible from inside the function, but 7333 // The name of a literal function is visible from inside the function, but
7347 // must not collide with names in the scope declaring the literal. 7334 // must not collide with names in the scope declaring the literal.
7348 OpenBlock(); 7335 OpenBlock();
7349 primary = ParseFunctionStatement(true); 7336 primary = ParseFunctionStatement(true);
7350 CloseBlock(); 7337 CloseBlock();
7351 } else if (IsIdentifier()) { 7338 } else if (IsIdentifier()) {
7352 QualIdent qual_ident; 7339 QualIdent qual_ident;
7353 ParseQualIdent(&qual_ident); 7340 ParseQualIdent(&qual_ident);
7354 if (qual_ident.qualifier == NULL) { 7341 if (qual_ident.lib_prefix == NULL) {
7355 if (!ResolveIdentInLocalScope(qual_ident.ident_pos, 7342 if (!ResolveIdentInLocalScope(qual_ident.ident_pos,
7356 *qual_ident.ident, 7343 *qual_ident.ident,
7357 &primary)) { 7344 &primary)) {
7358 // This is a non-local unqualified identifier so resolve the identifier 7345 // This is a non-local unqualified identifier so resolve the identifier
7359 // locally in the main app library and all libraries imported by it. 7346 // locally in the main app library and all libraries imported by it.
7360 primary = ResolveIdentInLibraryScope(library_, 7347 primary = ResolveIdentInLibraryScope(library_,
7361 qual_ident, 7348 qual_ident,
7362 kResolveIncludingImports); 7349 kResolveIncludingImports);
7363 } 7350 }
7364 } else { 7351 } else {
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
7729 void Parser::SkipQualIdent() { 7716 void Parser::SkipQualIdent() {
7730 ASSERT(IsIdentifier()); 7717 ASSERT(IsIdentifier());
7731 ConsumeToken(); 7718 ConsumeToken();
7732 if (CurrentToken() == Token::kPERIOD) { 7719 if (CurrentToken() == Token::kPERIOD) {
7733 ConsumeToken(); // Consume the kPERIOD token. 7720 ConsumeToken(); // Consume the kPERIOD token.
7734 ExpectIdentifier("identifier expected after '.'"); 7721 ExpectIdentifier("identifier expected after '.'");
7735 } 7722 }
7736 } 7723 }
7737 7724
7738 } // namespace dart 7725 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.cc ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698