| 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 3278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3289 library = Library::LookupLibrary(canon_url); | 3289 library = Library::LookupLibrary(canon_url); |
| 3290 if (library.IsNull()) { | 3290 if (library.IsNull()) { |
| 3291 library = Library::New(canon_url); | 3291 library = Library::New(canon_url); |
| 3292 library.Register(); | 3292 library.Register(); |
| 3293 } | 3293 } |
| 3294 } | 3294 } |
| 3295 // Add the import to the library. | 3295 // Add the import to the library. |
| 3296 if (prefix.IsNull() || (prefix.Length() == 0)) { | 3296 if (prefix.IsNull() || (prefix.Length() == 0)) { |
| 3297 library_.AddImport(library); | 3297 library_.AddImport(library); |
| 3298 } else { | 3298 } else { |
| 3299 if (library_.LookupLocalObject(prefix) != Object::null()) { | 3299 LibraryPrefix& library_prefix = LibraryPrefix::Handle(); |
| 3300 ErrorMsg(token_index_, "'%s' is already defined", prefix.ToCString()); | 3300 library_prefix = library_.LookupLocalLibraryPrefix(prefix); |
| 3301 if (!library_prefix.IsNull()) { |
| 3302 library_prefix.AddLibrary(library); |
| 3303 } else { |
| 3304 library_prefix = LibraryPrefix::New(prefix, library); |
| 3305 library_.AddObject(library_prefix, prefix); |
| 3301 } | 3306 } |
| 3302 const LibraryPrefix& library_prefix = | |
| 3303 LibraryPrefix::Handle(LibraryPrefix::New(prefix, library)); | |
| 3304 library_.AddObject(library_prefix, prefix); | |
| 3305 } | 3307 } |
| 3306 } | 3308 } |
| 3307 } | 3309 } |
| 3308 | 3310 |
| 3309 | 3311 |
| 3310 void Parser::ParseLibraryInclude() { | 3312 void Parser::ParseLibraryInclude() { |
| 3311 const Array& import_map = Array::Handle(library_.import_map()); | 3313 const Array& import_map = Array::Handle(library_.import_map()); |
| 3312 while (CurrentToken() == Token::kSOURCE) { | 3314 while (CurrentToken() == Token::kSOURCE) { |
| 3313 const intptr_t source_pos = token_index_; | 3315 const intptr_t source_pos = token_index_; |
| 3314 ConsumeToken(); | 3316 ConsumeToken(); |
| (...skipping 2842 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6157 ASSERT(type != NULL); | 6159 ASSERT(type != NULL); |
| 6158 if (type->IsResolved()) { | 6160 if (type->IsResolved()) { |
| 6159 return; | 6161 return; |
| 6160 } | 6162 } |
| 6161 // Resolve class. | 6163 // Resolve class. |
| 6162 if (!type->HasResolvedTypeClass()) { | 6164 if (!type->HasResolvedTypeClass()) { |
| 6163 const UnresolvedClass& unresolved_class = | 6165 const UnresolvedClass& unresolved_class = |
| 6164 UnresolvedClass::Handle(type->unresolved_class()); | 6166 UnresolvedClass::Handle(type->unresolved_class()); |
| 6165 const String& unresolved_class_name = | 6167 const String& unresolved_class_name = |
| 6166 String::Handle(unresolved_class.ident()); | 6168 String::Handle(unresolved_class.ident()); |
| 6167 Library& lib = Library::Handle(); | 6169 Class& resolved_type_class = Class::Handle(); |
| 6168 if (unresolved_class.library_prefix() == LibraryPrefix::null()) { | 6170 if (unresolved_class.library_prefix() == LibraryPrefix::null()) { |
| 6169 if (!scope_class.IsNull()) { | 6171 if (!scope_class.IsNull()) { |
| 6170 // First check if the type is a type parameter of the given scope class. | 6172 // First check if the type is a type parameter of the given scope class. |
| 6171 const TypeParameter& type_parameter = TypeParameter::Handle( | 6173 const TypeParameter& type_parameter = TypeParameter::Handle( |
| 6172 scope_class.LookupTypeParameter(unresolved_class_name, | 6174 scope_class.LookupTypeParameter(unresolved_class_name, |
| 6173 type->token_index())); | 6175 type->token_index())); |
| 6174 if (!type_parameter.IsNull()) { | 6176 if (!type_parameter.IsNull()) { |
| 6175 // A type parameter cannot be parameterized, so report an error if | 6177 // A type parameter cannot be parameterized, so report an error if |
| 6176 // type arguments have previously been parsed. | 6178 // type arguments have previously been parsed. |
| 6177 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) { | 6179 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) { |
| 6178 ErrorMsg(type_parameter.token_index(), | 6180 ErrorMsg(type_parameter.token_index(), |
| 6179 "type parameter '%s' cannot be parameterized", | 6181 "type parameter '%s' cannot be parameterized", |
| 6180 String::Handle(type_parameter.Name()).ToCString()); | 6182 String::Handle(type_parameter.Name()).ToCString()); |
| 6181 } | 6183 } |
| 6182 *type = type_parameter.raw(); | 6184 *type = type_parameter.raw(); |
| 6183 return; | 6185 return; |
| 6184 } | 6186 } |
| 6185 } | 6187 } |
| 6188 // Global lookup in current library. |
| 6189 resolved_type_class = library_.LookupClass(unresolved_class_name); |
| 6186 } else { | 6190 } else { |
| 6187 LibraryPrefix& lib_prefix = | 6191 LibraryPrefix& lib_prefix = |
| 6188 LibraryPrefix::Handle(unresolved_class.library_prefix()); | 6192 LibraryPrefix::Handle(unresolved_class.library_prefix()); |
| 6189 lib = lib_prefix.library(); | 6193 // Local lookup in library prefix scope. |
| 6190 } | 6194 resolved_type_class = lib_prefix.LookupLocalClass(unresolved_class_name); |
| 6191 Class& resolved_type_class = Class::Handle(); | |
| 6192 if (lib.IsNull()) { | |
| 6193 // Global lookup in current library. | |
| 6194 resolved_type_class = library_.LookupClass(unresolved_class_name); | |
| 6195 } else { | |
| 6196 // Local lookup in imported library. | |
| 6197 resolved_type_class = lib.LookupLocalClass(unresolved_class_name); | |
| 6198 } | 6195 } |
| 6199 if (!resolved_type_class.IsNull()) { | 6196 if (!resolved_type_class.IsNull()) { |
| 6200 Object& type_class = Object::Handle(resolved_type_class.raw()); | 6197 Object& type_class = Object::Handle(resolved_type_class.raw()); |
| 6201 ASSERT(type->IsType()); | 6198 ASSERT(type->IsType()); |
| 6202 // Replace unresolved class with resolved type class. | 6199 // Replace unresolved class with resolved type class. |
| 6203 Type& parameterized_type = Type::Handle(); | 6200 Type& parameterized_type = Type::Handle(); |
| 6204 parameterized_type ^= type->raw(); | 6201 parameterized_type ^= type->raw(); |
| 6205 parameterized_type.set_type_class(type_class); | 6202 parameterized_type.set_type_class(type_class); |
| 6206 } else if (type_resolution == kMustResolve) { | 6203 } else if (type_resolution == kMustResolve) { |
| 6207 ErrorMsg(type->token_index(), "type '%s' is not loaded", | 6204 ErrorMsg(type->token_index(), "type '%s' is not loaded", |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6551 if (!obj.IsNull()) { | 6548 if (!obj.IsNull()) { |
| 6552 ASSERT(obj.IsFunction()); | 6549 ASSERT(obj.IsFunction()); |
| 6553 func ^= obj.raw(); | 6550 func ^= obj.raw(); |
| 6554 ASSERT(func.is_static()); | 6551 ASSERT(func.is_static()); |
| 6555 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); | 6552 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); |
| 6556 return new StaticGetterNode(qual_ident.ident_pos, | 6553 return new StaticGetterNode(qual_ident.ident_pos, |
| 6557 Class::ZoneHandle(func.owner()), | 6554 Class::ZoneHandle(func.owner()), |
| 6558 *qual_ident.ident); | 6555 *qual_ident.ident); |
| 6559 } | 6556 } |
| 6560 if (qual_ident.lib_prefix != NULL) { | 6557 if (qual_ident.lib_prefix != NULL) { |
| 6558 return NULL; |
| 6559 } |
| 6560 // Lexically unresolved primary identifiers are referenced by their name. |
| 6561 return new PrimaryNode(qual_ident.ident_pos, *qual_ident.ident); |
| 6562 } |
| 6563 |
| 6564 |
| 6565 // Do a lookup for the identifier in the library prefix scope of the specified |
| 6566 // library prefix. This would mean trying to resolve it locally in any of the |
| 6567 // libraries present in the library prefix. |
| 6568 AstNode* Parser::ResolveIdentInLibraryPrefixScope(const LibraryPrefix& prefix, |
| 6569 const QualIdent& qual_ident) { |
| 6570 TRACE_PARSER("ResolveIdentInLibraryPrefixScope"); |
| 6571 Library& lib = Library::Handle(); |
| 6572 AstNode* result = NULL; |
| 6573 for (intptr_t i = 0; ((i < prefix.num_libs()) && (result == NULL)); i++) { |
| 6574 lib = prefix.GetLibrary(i); |
| 6575 ASSERT(!lib.IsNull()); |
| 6576 result = ResolveIdentInLibraryScope(lib, qual_ident, kResolveLocally); |
| 6577 } |
| 6578 if (result == NULL) { |
| 6561 // This is an unresolved prefixed primary identifier, need to report | 6579 // This is an unresolved prefixed primary identifier, need to report |
| 6562 // an error. | 6580 // an error. |
| 6563 ErrorMsg(qual_ident.ident_pos, "identifier '%s.%s' cannot be resolved", | 6581 ErrorMsg(qual_ident.ident_pos, "identifier '%s.%s' cannot be resolved", |
| 6564 String::Handle(qual_ident.lib_prefix->name()).ToCString(), | 6582 String::Handle(qual_ident.lib_prefix->name()).ToCString(), |
| 6565 qual_ident.ident->ToCString()); | 6583 qual_ident.ident->ToCString()); |
| 6566 } | 6584 } |
| 6567 // Lexically unresolved primary identifiers are referenced by their name. | 6585 return result; |
| 6568 return new PrimaryNode(qual_ident.ident_pos, *qual_ident.ident); | |
| 6569 } | 6586 } |
| 6570 | 6587 |
| 6571 | 6588 |
| 6572 // Resolve identifier, issue an error message if the name refers to | 6589 // Resolve identifier, issue an error message if the name refers to |
| 6573 // a method or a class/interface. | 6590 // a method or a class/interface. |
| 6574 // If the name cannot be resolved, turn it into an instance field access | 6591 // If the name cannot be resolved, turn it into an instance field access |
| 6575 // if we're compiling an instance method, or issue an error message | 6592 // if we're compiling an instance method, or issue an error message |
| 6576 // if we're compiling a static method. | 6593 // if we're compiling a static method. |
| 6577 AstNode* Parser::ResolveVarOrField(intptr_t ident_pos, const String& ident) { | 6594 AstNode* Parser::ResolveVarOrField(intptr_t ident_pos, const String& ident) { |
| 6578 TRACE_PARSER("ResolveVarOrField"); | 6595 TRACE_PARSER("ResolveVarOrField"); |
| (...skipping 813 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7392 // This is a non-local unqualified identifier so resolve the identifier | 7409 // This is a non-local unqualified identifier so resolve the identifier |
| 7393 // locally in the main app library and all libraries imported by it. | 7410 // locally in the main app library and all libraries imported by it. |
| 7394 primary = ResolveIdentInLibraryScope(library_, | 7411 primary = ResolveIdentInLibraryScope(library_, |
| 7395 qual_ident, | 7412 qual_ident, |
| 7396 kResolveIncludingImports); | 7413 kResolveIncludingImports); |
| 7397 } | 7414 } |
| 7398 } else { | 7415 } else { |
| 7399 // This is a qualified identifier with a library prefix so resolve | 7416 // This is a qualified identifier with a library prefix so resolve |
| 7400 // the identifier locally in that library (we do not include the | 7417 // the identifier locally in that library (we do not include the |
| 7401 // libraries imported by that library). | 7418 // libraries imported by that library). |
| 7402 const Library& lib = Library::Handle(qual_ident.lib_prefix->library()); | 7419 primary = ResolveIdentInLibraryPrefixScope(*(qual_ident.lib_prefix), |
| 7403 primary = ResolveIdentInLibraryScope(lib, | 7420 qual_ident); |
| 7404 qual_ident, | |
| 7405 kResolveLocally); | |
| 7406 } | 7421 } |
| 7407 ASSERT(primary != NULL); | 7422 ASSERT(primary != NULL); |
| 7408 } else if (CurrentToken() == Token::kTHIS) { | 7423 } else if (CurrentToken() == Token::kTHIS) { |
| 7409 const String& this_name = String::Handle(String::NewSymbol(kThisName)); | 7424 const String& this_name = String::Handle(String::NewSymbol(kThisName)); |
| 7410 LocalVariable* local = LookupLocalScope(this_name); | 7425 LocalVariable* local = LookupLocalScope(this_name); |
| 7411 if (local == NULL) { | 7426 if (local == NULL) { |
| 7412 ErrorMsg("unexpected use of 'this' in primary expression"); | 7427 ErrorMsg("unexpected use of 'this' in primary expression"); |
| 7413 } | 7428 } |
| 7414 primary = new LoadLocalNode(token_index_, *local); | 7429 primary = new LoadLocalNode(token_index_, *local); |
| 7415 ConsumeToken(); | 7430 ConsumeToken(); |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7773 void Parser::SkipQualIdent() { | 7788 void Parser::SkipQualIdent() { |
| 7774 ASSERT(IsIdentifier()); | 7789 ASSERT(IsIdentifier()); |
| 7775 ConsumeToken(); | 7790 ConsumeToken(); |
| 7776 if (CurrentToken() == Token::kPERIOD) { | 7791 if (CurrentToken() == Token::kPERIOD) { |
| 7777 ConsumeToken(); // Consume the kPERIOD token. | 7792 ConsumeToken(); // Consume the kPERIOD token. |
| 7778 ExpectIdentifier("identifier expected after '.'"); | 7793 ExpectIdentifier("identifier expected after '.'"); |
| 7779 } | 7794 } |
| 7780 } | 7795 } |
| 7781 | 7796 |
| 7782 } // namespace dart | 7797 } // namespace dart |
| OLD | NEW |