| 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 = context ?? createContext(options, packages, dartSdk: dart
Sdk), | 112 : this.context = |
| 113 context ?? createContext(options, packages, dartSdk: dartSdk), |
| 113 this.applicationRoot = options.applicationRoot; | 114 this.applicationRoot = options.applicationRoot; |
| 114 | 115 |
| 115 String getLibraryName(LibraryElement element) { | 116 String getLibraryName(LibraryElement element) { |
| 116 return element.name.isEmpty ? null : element.name; | 117 return element.name.isEmpty ? null : element.name; |
| 117 } | 118 } |
| 118 | 119 |
| 119 ast.Library getLibraryReference(LibraryElement element) { | 120 ast.Library getLibraryReference(LibraryElement element) { |
| 120 var uri = applicationRoot.relativeUri(element.source.uri); | 121 var uri = applicationRoot.relativeUri(element.source.uri); |
| 121 return repository.getLibraryReference(uri) | 122 return repository.getLibraryReference(uri) |
| 122 ..name ??= getLibraryName(element) | 123 ..name ??= getLibraryName(element) |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 | 395 |
| 395 ast.TypeParameter tryGetClassTypeParameter(TypeParameterElement element) { | 396 ast.TypeParameter tryGetClassTypeParameter(TypeParameterElement element) { |
| 396 return _classTypeParameters[element]; | 397 return _classTypeParameters[element]; |
| 397 } | 398 } |
| 398 | 399 |
| 399 Element getMemberElement(ast.Member node) { | 400 Element getMemberElement(ast.Member node) { |
| 400 return _members.inverse[node]; | 401 return _members.inverse[node]; |
| 401 } | 402 } |
| 402 | 403 |
| 403 ast.Member getMemberReference(Element element) { | 404 ast.Member getMemberReference(Element element) { |
| 405 assert(element != null); |
| 404 assert(element is! Member); // Use the "base element". | 406 assert(element is! Member); // Use the "base element". |
| 405 return _members[element] ??= _buildMemberReference(element); | 407 return _members[element] ??= _buildMemberReference(element); |
| 406 } | 408 } |
| 407 | 409 |
| 408 ast.Member _buildMemberReference(Element element) { | 410 ast.Member _buildMemberReference(Element element) { |
| 411 assert(element != null); |
| 409 var node = _buildOrphanedMemberReference(element); | 412 var node = _buildOrphanedMemberReference(element); |
| 410 // Set the parent pointer and store it in the enclosing class or library. | 413 // Set the parent pointer and store it in the enclosing class or library. |
| 411 // If the enclosing library is being built from the AST, do not add the | 414 // If the enclosing library is being built from the AST, do not add the |
| 412 // member, since the AST builder will put it in there. | 415 // member, since the AST builder will put it in there. |
| 413 var parent = element.enclosingElement; | 416 var parent = element.enclosingElement; |
| 414 if (parent is ClassElement) { | 417 if (parent is ClassElement) { |
| 415 var class_ = getClassReference(parent); | 418 var class_ = getClassReference(parent); |
| 416 node.parent = class_; | 419 node.parent = class_; |
| 417 if (!isLibraryBeingLoaded(element.library)) { | 420 if (!isLibraryBeingLoaded(element.library)) { |
| 418 class_.addMember(node); | 421 class_.addMember(node); |
| 419 } | 422 } |
| 420 } else { | 423 } else { |
| 421 var library = getLibraryReference(element.library); | 424 var library = getLibraryReference(element.library); |
| 422 node.parent = library; | 425 node.parent = library; |
| 423 if (!isLibraryBeingLoaded(element.library)) { | 426 if (!isLibraryBeingLoaded(element.library)) { |
| 424 library.addMember(node); | 427 library.addMember(node); |
| 425 } | 428 } |
| 426 } | 429 } |
| 427 return node; | 430 return node; |
| 428 } | 431 } |
| 429 | 432 |
| 430 ast.Member _buildOrphanedMemberReference(Element element) { | 433 ast.Member _buildOrphanedMemberReference(Element element) { |
| 434 assert(element != null); |
| 431 ClassElement classElement = element.enclosingElement is ClassElement | 435 ClassElement classElement = element.enclosingElement is ClassElement |
| 432 ? element.enclosingElement | 436 ? element.enclosingElement |
| 433 : null; | 437 : null; |
| 434 TypeScope scope = classElement != null | 438 TypeScope scope = classElement != null |
| 435 ? new ClassScope(this, getLibraryReference(element.library)) | 439 ? new ClassScope(this, getLibraryReference(element.library)) |
| 436 : new TypeScope(this); | 440 : new TypeScope(this); |
| 437 if (classElement != null) { | 441 if (classElement != null) { |
| 438 getClassReference(classElement); | 442 getClassReference(classElement); |
| 439 } | 443 } |
| 440 switch (element.kind) { | 444 switch (element.kind) { |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 return list; | 682 return list; |
| 679 } | 683 } |
| 680 | 684 |
| 681 void _iterateWorklist() { | 685 void _iterateWorklist() { |
| 682 while (temporaryClassWorklist.isNotEmpty) { | 686 while (temporaryClassWorklist.isNotEmpty) { |
| 683 var element = temporaryClassWorklist.removeLast(); | 687 var element = temporaryClassWorklist.removeLast(); |
| 684 promoteToTypeLevel(element); | 688 promoteToTypeLevel(element); |
| 685 } | 689 } |
| 686 } | 690 } |
| 687 | 691 |
| 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 isStatic: true) |
| 712 ..fileUri = library.fileUri; |
| 713 library.addMember(main); |
| 714 return main; |
| 715 } |
| 716 |
| 688 ast.Program loadProgram(Uri mainLibrary, {Target target, bool compileSdk}) { | 717 ast.Program loadProgram(Uri mainLibrary, {Target target, bool compileSdk}) { |
| 689 ast.Library library = repository | 718 Uri uri = applicationRoot.relativeUri(mainLibrary); |
| 690 .getLibraryReference(applicationRoot.relativeUri(mainLibrary)); | 719 ast.Library library = repository.getLibraryReference(uri); |
| 691 ensureLibraryIsLoaded(library); | 720 ensureLibraryIsLoaded(library); |
| 721 var mainMethod = _getMainMethod(mainLibrary); |
| 692 loadEverything(target: target, compileSdk: compileSdk); | 722 loadEverything(target: target, compileSdk: compileSdk); |
| 693 var program = new ast.Program(repository.libraries); | 723 var program = new ast.Program(repository.libraries); |
| 694 program.mainMethod = library.procedures.firstWhere( | 724 if (mainMethod == null) { |
| 695 (member) => member.name?.name == 'main', | 725 mainMethod = _makeMissingMainMethod(library); |
| 696 orElse: () => null); | 726 } |
| 727 program.mainMethod = mainMethod; |
| 697 for (LibraryElement libraryElement in libraryElements) { | 728 for (LibraryElement libraryElement in libraryElements) { |
| 698 for (CompilationUnitElement compilationUnitElement | 729 for (CompilationUnitElement compilationUnitElement |
| 699 in libraryElement.units) { | 730 in libraryElement.units) { |
| 700 var source = compilationUnitElement.source; | 731 var source = compilationUnitElement.source; |
| 701 LineInfo lineInfo = context.computeLineInfo(source); | 732 LineInfo lineInfo = context.computeLineInfo(source); |
| 702 String sourceCode; | 733 String sourceCode; |
| 703 try { | 734 try { |
| 704 sourceCode = context.getContents(source).data; | 735 sourceCode = context.getContents(source).data; |
| 705 } catch (e) { | 736 } catch (e) { |
| 706 // The source's contents could not be accessed. | 737 // The source's contents could not be accessed. |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 867 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext() | 898 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext() |
| 868 ..sourceFactory = new SourceFactory(resolvers) | 899 ..sourceFactory = new SourceFactory(resolvers) |
| 869 ..analysisOptions = createAnalysisOptions(options.strongMode); | 900 ..analysisOptions = createAnalysisOptions(options.strongMode); |
| 870 | 901 |
| 871 options.declaredVariables.forEach((String name, String value) { | 902 options.declaredVariables.forEach((String name, String value) { |
| 872 context.declaredVariables.define(name, value); | 903 context.declaredVariables.define(name, value); |
| 873 }); | 904 }); |
| 874 | 905 |
| 875 return context; | 906 return context; |
| 876 } | 907 } |
| OLD | NEW |