| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library kernel.analyzer.loader; | 4 library kernel.analyzer.loader; |
| 5 | 5 |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:io' as io; | 7 import 'dart:io' as io; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
| 10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 /// Classes that have been referenced, and must be promoted to type level | 102 /// Classes that have been referenced, and must be promoted to type level |
| 103 /// so as not to expose partially initialized classes. | 103 /// so as not to expose partially initialized classes. |
| 104 final List<ast.Class> temporaryClassWorklist = []; | 104 final List<ast.Class> temporaryClassWorklist = []; |
| 105 | 105 |
| 106 LibraryElement _libraryBeingLoaded = null; | 106 LibraryElement _libraryBeingLoaded = null; |
| 107 | 107 |
| 108 bool get strongMode => context.analysisOptions.strongMode; | 108 bool get strongMode => context.analysisOptions.strongMode; |
| 109 | 109 |
| 110 DartLoader(this.repository, DartOptions options, Packages packages, | 110 DartLoader(this.repository, DartOptions options, Packages packages, |
| 111 {DartSdk dartSdk, AnalysisContext context}) | 111 {DartSdk dartSdk, AnalysisContext context}) |
| 112 : this.context = | 112 : this.context = context ?? createContext(options, packages, dartSdk: dart
Sdk), |
| 113 context ?? createContext(options, packages, dartSdk: dartSdk), | |
| 114 this.applicationRoot = options.applicationRoot; | 113 this.applicationRoot = options.applicationRoot; |
| 115 | 114 |
| 116 String getLibraryName(LibraryElement element) { | 115 String getLibraryName(LibraryElement element) { |
| 117 return element.name.isEmpty ? null : element.name; | 116 return element.name.isEmpty ? null : element.name; |
| 118 } | 117 } |
| 119 | 118 |
| 120 ast.Library getLibraryReference(LibraryElement element) { | 119 ast.Library getLibraryReference(LibraryElement element) { |
| 121 var uri = applicationRoot.relativeUri(element.source.uri); | 120 var uri = applicationRoot.relativeUri(element.source.uri); |
| 122 return repository.getLibraryReference(uri) | 121 return repository.getLibraryReference(uri) |
| 123 ..name ??= getLibraryName(element) | 122 ..name ??= getLibraryName(element) |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 | 394 |
| 396 ast.TypeParameter tryGetClassTypeParameter(TypeParameterElement element) { | 395 ast.TypeParameter tryGetClassTypeParameter(TypeParameterElement element) { |
| 397 return _classTypeParameters[element]; | 396 return _classTypeParameters[element]; |
| 398 } | 397 } |
| 399 | 398 |
| 400 Element getMemberElement(ast.Member node) { | 399 Element getMemberElement(ast.Member node) { |
| 401 return _members.inverse[node]; | 400 return _members.inverse[node]; |
| 402 } | 401 } |
| 403 | 402 |
| 404 ast.Member getMemberReference(Element element) { | 403 ast.Member getMemberReference(Element element) { |
| 405 assert(element != null); | |
| 406 assert(element is! Member); // Use the "base element". | 404 assert(element is! Member); // Use the "base element". |
| 407 return _members[element] ??= _buildMemberReference(element); | 405 return _members[element] ??= _buildMemberReference(element); |
| 408 } | 406 } |
| 409 | 407 |
| 410 ast.Member _buildMemberReference(Element element) { | 408 ast.Member _buildMemberReference(Element element) { |
| 411 assert(element != null); | |
| 412 var node = _buildOrphanedMemberReference(element); | 409 var node = _buildOrphanedMemberReference(element); |
| 413 // Set the parent pointer and store it in the enclosing class or library. | 410 // Set the parent pointer and store it in the enclosing class or library. |
| 414 // If the enclosing library is being built from the AST, do not add the | 411 // If the enclosing library is being built from the AST, do not add the |
| 415 // member, since the AST builder will put it in there. | 412 // member, since the AST builder will put it in there. |
| 416 var parent = element.enclosingElement; | 413 var parent = element.enclosingElement; |
| 417 if (parent is ClassElement) { | 414 if (parent is ClassElement) { |
| 418 var class_ = getClassReference(parent); | 415 var class_ = getClassReference(parent); |
| 419 node.parent = class_; | 416 node.parent = class_; |
| 420 if (!isLibraryBeingLoaded(element.library)) { | 417 if (!isLibraryBeingLoaded(element.library)) { |
| 421 class_.addMember(node); | 418 class_.addMember(node); |
| 422 } | 419 } |
| 423 } else { | 420 } else { |
| 424 var library = getLibraryReference(element.library); | 421 var library = getLibraryReference(element.library); |
| 425 node.parent = library; | 422 node.parent = library; |
| 426 if (!isLibraryBeingLoaded(element.library)) { | 423 if (!isLibraryBeingLoaded(element.library)) { |
| 427 library.addMember(node); | 424 library.addMember(node); |
| 428 } | 425 } |
| 429 } | 426 } |
| 430 return node; | 427 return node; |
| 431 } | 428 } |
| 432 | 429 |
| 433 ast.Member _buildOrphanedMemberReference(Element element) { | 430 ast.Member _buildOrphanedMemberReference(Element element) { |
| 434 assert(element != null); | |
| 435 ClassElement classElement = element.enclosingElement is ClassElement | 431 ClassElement classElement = element.enclosingElement is ClassElement |
| 436 ? element.enclosingElement | 432 ? element.enclosingElement |
| 437 : null; | 433 : null; |
| 438 TypeScope scope = classElement != null | 434 TypeScope scope = classElement != null |
| 439 ? new ClassScope(this, getLibraryReference(element.library)) | 435 ? new ClassScope(this, getLibraryReference(element.library)) |
| 440 : new TypeScope(this); | 436 : new TypeScope(this); |
| 441 if (classElement != null) { | 437 if (classElement != null) { |
| 442 getClassReference(classElement); | 438 getClassReference(classElement); |
| 443 } | 439 } |
| 444 switch (element.kind) { | 440 switch (element.kind) { |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 return list; | 678 return list; |
| 683 } | 679 } |
| 684 | 680 |
| 685 void _iterateWorklist() { | 681 void _iterateWorklist() { |
| 686 while (temporaryClassWorklist.isNotEmpty) { | 682 while (temporaryClassWorklist.isNotEmpty) { |
| 687 var element = temporaryClassWorklist.removeLast(); | 683 var element = temporaryClassWorklist.removeLast(); |
| 688 promoteToTypeLevel(element); | 684 promoteToTypeLevel(element); |
| 689 } | 685 } |
| 690 } | 686 } |
| 691 | 687 |
| 692 ast.Procedure _getMainMethod(Uri uri) { | |
| 693 Source source = context.sourceFactory.forUri2(uri); | |
| 694 LibraryElement library = context.computeLibraryElement(source); | |
| 695 var mainElement = library.entryPoint; | |
| 696 if (mainElement == null) return null; | |
| 697 var mainMember = getMemberReference(mainElement); | |
| 698 if (mainMember is ast.Procedure && !mainMember.isAccessor) { | |
| 699 return mainMember; | |
| 700 } | |
| 701 // Top-level 'main' getters are not supported at the moment. | |
| 702 return null; | |
| 703 } | |
| 704 | |
| 705 ast.Procedure _makeMissingMainMethod(ast.Library library) { | |
| 706 var main = new ast.Procedure( | |
| 707 new ast.Name('main'), | |
| 708 ast.ProcedureKind.Method, | |
| 709 new ast.FunctionNode(new ast.ExpressionStatement(new ast.Throw( | |
| 710 new ast.StringLiteral('Program has no main method'))))) | |
| 711 ..fileUri = library.fileUri; | |
| 712 library.addMember(main); | |
| 713 return main; | |
| 714 } | |
| 715 | |
| 716 ast.Program loadProgram(Uri mainLibrary, {Target target, bool compileSdk}) { | 688 ast.Program loadProgram(Uri mainLibrary, {Target target, bool compileSdk}) { |
| 717 Uri uri = applicationRoot.relativeUri(mainLibrary); | 689 ast.Library library = repository |
| 718 ast.Library library = repository.getLibraryReference(uri); | 690 .getLibraryReference(applicationRoot.relativeUri(mainLibrary)); |
| 719 ensureLibraryIsLoaded(library); | 691 ensureLibraryIsLoaded(library); |
| 720 var mainMethod = _getMainMethod(mainLibrary); | |
| 721 loadEverything(target: target, compileSdk: compileSdk); | 692 loadEverything(target: target, compileSdk: compileSdk); |
| 722 var program = new ast.Program(repository.libraries); | 693 var program = new ast.Program(repository.libraries); |
| 723 if (mainMethod == null) { | 694 program.mainMethod = library.procedures.firstWhere( |
| 724 mainMethod = _makeMissingMainMethod(library); | 695 (member) => member.name?.name == 'main', |
| 725 } | 696 orElse: () => null); |
| 726 program.mainMethod = mainMethod; | |
| 727 for (LibraryElement libraryElement in libraryElements) { | 697 for (LibraryElement libraryElement in libraryElements) { |
| 728 for (CompilationUnitElement compilationUnitElement | 698 for (CompilationUnitElement compilationUnitElement |
| 729 in libraryElement.units) { | 699 in libraryElement.units) { |
| 730 var source = compilationUnitElement.source; | 700 var source = compilationUnitElement.source; |
| 731 LineInfo lineInfo = context.computeLineInfo(source); | 701 LineInfo lineInfo = context.computeLineInfo(source); |
| 732 String sourceCode; | 702 String sourceCode; |
| 733 try { | 703 try { |
| 734 sourceCode = context.getContents(source).data; | 704 sourceCode = context.getContents(source).data; |
| 735 } catch (e) { | 705 } catch (e) { |
| 736 // The source's contents could not be accessed. | 706 // The source's contents could not be accessed. |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 897 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext() | 867 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext() |
| 898 ..sourceFactory = new SourceFactory(resolvers) | 868 ..sourceFactory = new SourceFactory(resolvers) |
| 899 ..analysisOptions = createAnalysisOptions(options.strongMode); | 869 ..analysisOptions = createAnalysisOptions(options.strongMode); |
| 900 | 870 |
| 901 options.declaredVariables.forEach((String name, String value) { | 871 options.declaredVariables.forEach((String name, String value) { |
| 902 context.declaredVariables.define(name, value); | 872 context.declaredVariables.define(name, value); |
| 903 }); | 873 }); |
| 904 | 874 |
| 905 return context; | 875 return context; |
| 906 } | 876 } |
| OLD | NEW |