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

Side by Side Diff: pkg/kernel/lib/analyzer/loader.dart

Issue 2665723002: Implement canonical name scheme in kernel. (Closed)
Patch Set: Address Kevin's comments Created 3 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
« no previous file with comments | « pkg/kernel/lib/analyzer/ast_from_analyzer.dart ('k') | pkg/kernel/lib/ast.dart » ('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) 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';
11 import 'package:analyzer/file_system/physical_file_system.dart'; 11 import 'package:analyzer/file_system/physical_file_system.dart';
12 import 'package:analyzer/source/package_map_resolver.dart'; 12 import 'package:analyzer/source/package_map_resolver.dart';
13 import 'package:analyzer/src/dart/scanner/scanner.dart'; 13 import 'package:analyzer/src/dart/scanner/scanner.dart';
14 import 'package:analyzer/src/dart/sdk/sdk.dart'; 14 import 'package:analyzer/src/dart/sdk/sdk.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 15 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/parser.dart'; 16 import 'package:analyzer/src/generated/parser.dart';
17 import 'package:analyzer/src/generated/sdk.dart'; 17 import 'package:analyzer/src/generated/sdk.dart';
18 import 'package:analyzer/src/generated/source_io.dart'; 18 import 'package:analyzer/src/generated/source_io.dart';
19 import 'package:analyzer/src/summary/summary_sdk.dart'; 19 import 'package:analyzer/src/summary/summary_sdk.dart';
20 import 'package:kernel/application_root.dart'; 20 import 'package:kernel/application_root.dart';
21 import 'package:package_config/discovery.dart'; 21 import 'package:package_config/discovery.dart';
22 import 'package:package_config/packages.dart'; 22 import 'package:package_config/packages.dart';
23 23
24 import '../ast.dart' as ast; 24 import '../ast.dart' as ast;
25 import '../repository.dart';
26 import '../target/targets.dart' show Target; 25 import '../target/targets.dart' show Target;
27 import '../type_algebra.dart'; 26 import '../type_algebra.dart';
28 import 'analyzer.dart'; 27 import 'analyzer.dart';
29 import 'ast_from_analyzer.dart'; 28 import 'ast_from_analyzer.dart';
30 29
31 /// Options passed to the Dart frontend. 30 /// Options passed to the Dart frontend.
32 class DartOptions { 31 class DartOptions {
33 /// True if user code should be loaded in strong mode. 32 /// True if user code should be loaded in strong mode.
34 bool strongMode; 33 bool strongMode;
35 34
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 ast.TypeParameter tryGetClassTypeParameter(TypeParameterElement element); 80 ast.TypeParameter tryGetClassTypeParameter(TypeParameterElement element);
82 ast.Class getSharedMixinApplicationClass( 81 ast.Class getSharedMixinApplicationClass(
83 ast.Library library, ast.Class supertype, ast.Class mixin); 82 ast.Library library, ast.Class supertype, ast.Class mixin);
84 bool get strongMode; 83 bool get strongMode;
85 84
86 /// Whether or not to include redirecting factories in the output. 85 /// Whether or not to include redirecting factories in the output.
87 bool get ignoreRedirectingFactories; 86 bool get ignoreRedirectingFactories;
88 } 87 }
89 88
90 class DartLoader implements ReferenceLevelLoader { 89 class DartLoader implements ReferenceLevelLoader {
91 final Repository repository; 90 final ast.Program program;
92 final ApplicationRoot applicationRoot; 91 final ApplicationRoot applicationRoot;
93 final Bimap<ClassElement, ast.Class> _classes = 92 final Bimap<ClassElement, ast.Class> _classes =
94 new Bimap<ClassElement, ast.Class>(); 93 new Bimap<ClassElement, ast.Class>();
95 final Bimap<Element, ast.Member> _members = new Bimap<Element, ast.Member>(); 94 final Bimap<Element, ast.Member> _members = new Bimap<Element, ast.Member>();
96 final Map<TypeParameterElement, ast.TypeParameter> _classTypeParameters = 95 final Map<TypeParameterElement, ast.TypeParameter> _classTypeParameters =
97 <TypeParameterElement, ast.TypeParameter>{}; 96 <TypeParameterElement, ast.TypeParameter>{};
98 final Map<ast.Library, Map<String, ast.Class>> _mixinApplications = 97 final Map<ast.Library, Map<String, ast.Class>> _mixinApplications =
99 <ast.Library, Map<String, ast.Class>>{}; 98 <ast.Library, Map<String, ast.Class>>{};
99 final Map<LibraryElement, ast.Library> _libraries =
100 <LibraryElement, ast.Library>{};
100 final AnalysisContext context; 101 final AnalysisContext context;
101 LibraryElement _dartCoreLibrary; 102 LibraryElement _dartCoreLibrary;
102 final List errors = []; 103 final List errors = [];
103 final List libraryElements = []; 104 final List libraryElements = [];
104 105
105 /// Classes that have been referenced, and must be promoted to type level 106 /// Classes that have been referenced, and must be promoted to type level
106 /// so as not to expose partially initialized classes. 107 /// so as not to expose partially initialized classes.
107 final List<ast.Class> temporaryClassWorklist = []; 108 final List<ast.Class> temporaryClassWorklist = [];
108 109
109 final Map<LibraryElement, List<ClassElement>> mixinLibraryWorklist = {}; 110 final Map<LibraryElement, List<ClassElement>> mixinLibraryWorklist = {};
110 111
111 final bool ignoreRedirectingFactories; 112 final bool ignoreRedirectingFactories;
112 113
113 LibraryElement _libraryBeingLoaded = null; 114 LibraryElement _libraryBeingLoaded = null;
114 115
115 bool get strongMode => context.analysisOptions.strongMode; 116 bool get strongMode => context.analysisOptions.strongMode;
116 117
117 DartLoader(this.repository, DartOptions options, Packages packages, 118 DartLoader(this.program, DartOptions options, Packages packages,
118 {DartSdk dartSdk, 119 {DartSdk dartSdk,
119 AnalysisContext context, 120 AnalysisContext context,
120 this.ignoreRedirectingFactories: true}) 121 this.ignoreRedirectingFactories: true})
121 : this.context = 122 : this.context =
122 context ?? createContext(options, packages, dartSdk: dartSdk), 123 context ?? createContext(options, packages, dartSdk: dartSdk),
123 this.applicationRoot = options.applicationRoot; 124 this.applicationRoot = options.applicationRoot;
124 125
125 String getLibraryName(LibraryElement element) { 126 String getLibraryName(LibraryElement element) {
126 return element.name.isEmpty ? null : element.name; 127 return element.name.isEmpty ? null : element.name;
127 } 128 }
128 129
130 LibraryElement getLibraryElementFromUri(Uri uri) {
131 var source = context.sourceFactory.forUri2(uri);
132 if (source == null) return null;
133 return context.computeLibraryElement(source);
134 }
135
129 ast.Library getLibraryReference(LibraryElement element) { 136 ast.Library getLibraryReference(LibraryElement element) {
130 var uri = applicationRoot.relativeUri(element.source.uri); 137 var uri = applicationRoot.relativeUri(element.source.uri);
131 return repository.getLibraryReference(uri) 138 var library = _libraries[element];
132 ..name ??= getLibraryName(element) 139 if (library == null) {
133 ..fileUri = '${element.source.uri}'; 140 library = new ast.Library(uri)
141 ..isExternal = true
142 ..name = getLibraryName(element)
143 ..fileUri = '${element.source.uri}';
144 program.libraries.add(library..parent = program);
145 _libraries[element] = library;
146 }
147 return library;
148 }
149
150 ast.Library getLibraryReferenceFromUri(Uri uri) {
151 return getLibraryReference(getLibraryElementFromUri(uri));
134 } 152 }
135 153
136 void _buildTopLevelMember( 154 void _buildTopLevelMember(
137 ast.Member member, Element element, Declaration astNode) { 155 ast.Member member, Element element, Declaration astNode) {
138 assert(member.parent != null); 156 assert(member.parent != null);
139 new MemberBodyBuilder(this, member, element).build(astNode); 157 new MemberBodyBuilder(this, member, element).build(astNode);
140 } 158 }
141 159
142 /// True if [element] is in the process of being loaded by 160 /// True if [element] is in the process of being loaded by
143 /// [_buildLibraryBody]. 161 /// [_buildLibraryBody].
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 var freshParameters = 359 var freshParameters =
342 getFreshTypeParameters(classNode.typeParameters); 360 getFreshTypeParameters(classNode.typeParameters);
343 var mixinClass = new ast.Class( 361 var mixinClass = new ast.Class(
344 name: '${classNode.name}^${mixinType.classNode.name}', 362 name: '${classNode.name}^${mixinType.classNode.name}',
345 isAbstract: true, 363 isAbstract: true,
346 typeParameters: freshParameters.freshTypeParameters, 364 typeParameters: freshParameters.freshTypeParameters,
347 supertype: freshParameters.substituteSuper(supertype), 365 supertype: freshParameters.substituteSuper(supertype),
348 mixedInType: freshParameters.substituteSuper(mixinType), 366 mixedInType: freshParameters.substituteSuper(mixinType),
349 fileUri: classNode.fileUri)..fileOffset = element.nameOffset; 367 fileUri: classNode.fileUri)..fileOffset = element.nameOffset;
350 mixinClass.level = ast.ClassLevel.Type; 368 mixinClass.level = ast.ClassLevel.Type;
369 addMixinClassToLibrary(mixinClass, classNode.enclosingLibrary);
351 supertype = new ast.Supertype(mixinClass, 370 supertype = new ast.Supertype(mixinClass,
352 classNode.typeParameters.map(makeTypeParameterType).toList()); 371 classNode.typeParameters.map(makeTypeParameterType).toList());
353 addMixinClassToLibrary(mixinClass, classNode.enclosingLibrary);
354 // This class cannot be used from anywhere else, so don't try to 372 // This class cannot be used from anywhere else, so don't try to
355 // generate shared mixin applications using it. 373 // generate shared mixin applications using it.
356 useSharedMixin = false; 374 useSharedMixin = false;
357 } 375 }
358 } 376 }
359 classNode.supertype = supertype; 377 classNode.supertype = supertype;
360 for (var implementedType in element.interfaces) { 378 for (var implementedType in element.interfaces) {
361 classNode.implementedTypes.add(scope.buildSupertype(implementedType)); 379 classNode.implementedTypes.add(scope.buildSupertype(implementedType));
362 } 380 }
363 } 381 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 } 457 }
440 458
441 ast.Member getMemberReference(Element element) { 459 ast.Member getMemberReference(Element element) {
442 assert(element != null); 460 assert(element != null);
443 assert(element is! Member); // Use the "base element". 461 assert(element is! Member); // Use the "base element".
444 return _members[element] ??= _buildMemberReference(element); 462 return _members[element] ??= _buildMemberReference(element);
445 } 463 }
446 464
447 ast.Member _buildMemberReference(Element element) { 465 ast.Member _buildMemberReference(Element element) {
448 assert(element != null); 466 assert(element != null);
449 var node = _buildOrphanedMemberReference(element); 467 var member = _buildOrphanedMemberReference(element);
450 // Set the parent pointer and store it in the enclosing class or library. 468 // Set the parent pointer and store it in the enclosing class or library.
451 // If the enclosing library is being built from the AST, do not add the 469 // If the enclosing library is being built from the AST, do not add the
452 // member, since the AST builder will put it in there. 470 // member, since the AST builder will put it in there.
453 var parent = element.enclosingElement; 471 var parent = element.enclosingElement;
454 if (parent is ClassElement) { 472 if (parent is ClassElement) {
455 var class_ = getClassReference(parent); 473 var class_ = getClassReference(parent);
456 node.parent = class_; 474 member.parent = class_;
457 if (!isLibraryBeingLoaded(element.library)) { 475 if (!isLibraryBeingLoaded(element.library)) {
458 class_.addMember(node); 476 class_.addMember(member);
459 } 477 }
460 } else { 478 } else {
461 var library = getLibraryReference(element.library); 479 var library = getLibraryReference(element.library);
462 node.parent = library; 480 member.parent = library;
463 if (!isLibraryBeingLoaded(element.library)) { 481 if (!isLibraryBeingLoaded(element.library)) {
464 library.addMember(node); 482 library.addMember(member);
465 } 483 }
466 } 484 }
467 return node; 485 return member;
468 } 486 }
469 487
470 ast.Member _buildOrphanedMemberReference(Element element) { 488 ast.Member _buildOrphanedMemberReference(Element element) {
471 assert(element != null); 489 assert(element != null);
472 ClassElement classElement = element.enclosingElement is ClassElement 490 ClassElement classElement = element.enclosingElement is ClassElement
473 ? element.enclosingElement 491 ? element.enclosingElement
474 : null; 492 : null;
475 TypeScope scope = classElement != null 493 TypeScope scope = classElement != null
476 ? new ClassScope(this, getLibraryReference(element.library)) 494 ? new ClassScope(this, getLibraryReference(element.library))
477 : new TypeScope(this); 495 : new TypeScope(this);
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 for (var uri in target.extraRequiredLibraries) { 728 for (var uri in target.extraRequiredLibraries) {
711 var library = _findLibraryElement(uri); 729 var library = _findLibraryElement(uri);
712 if (library == null) { 730 if (library == null) {
713 errors.add('Could not find required library $uri'); 731 errors.add('Could not find required library $uri');
714 continue; 732 continue;
715 } 733 }
716 ensureLibraryIsLoaded(getLibraryReference(library)); 734 ensureLibraryIsLoaded(getLibraryReference(library));
717 } 735 }
718 } 736 }
719 } 737 }
720 for (int i = 0; i < repository.libraries.length; ++i) { 738 for (int i = 0; i < program.libraries.length; ++i) {
721 var library = repository.libraries[i]; 739 var library = program.libraries[i];
722 if (compileSdk || library.importUri.scheme != 'dart') { 740 if (compileSdk || library.importUri.scheme != 'dart') {
723 ensureLibraryIsLoaded(library); 741 ensureLibraryIsLoaded(library);
724 } 742 }
725 } 743 }
726 } 744 }
727 745
728 /// Builds a list of sources that have been loaded. 746 /// Builds a list of sources that have been loaded.
729 /// 747 ///
730 /// This operation may be expensive and should only be used for diagnostics. 748 /// This operation may be expensive and should only be used for diagnostics.
731 List<String> getLoadedFileNames() { 749 List<String> getLoadedFileNames() {
732 var list = <String>[]; 750 var list = <String>[];
733 for (var library in repository.libraries) { 751 for (var library in program.libraries) {
734 LibraryElement element = context.computeLibraryElement(context 752 LibraryElement element = context.computeLibraryElement(context
735 .sourceFactory 753 .sourceFactory
736 .forUri2(applicationRoot.absoluteUri(library.importUri))); 754 .forUri2(applicationRoot.absoluteUri(library.importUri)));
737 for (var unit in element.units) { 755 for (var unit in element.units) {
738 list.add(unit.source.fullName); 756 list.add(unit.source.fullName);
739 } 757 }
740 } 758 }
741 return list; 759 return list;
742 } 760 }
743 761
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 var main = new ast.Procedure( 799 var main = new ast.Procedure(
782 new ast.Name('main'), 800 new ast.Name('main'),
783 ast.ProcedureKind.Method, 801 ast.ProcedureKind.Method,
784 new ast.FunctionNode(new ast.ExpressionStatement(new ast.Throw( 802 new ast.FunctionNode(new ast.ExpressionStatement(new ast.Throw(
785 new ast.StringLiteral('Program has no main method')))), 803 new ast.StringLiteral('Program has no main method')))),
786 isStatic: true)..fileUri = library.fileUri; 804 isStatic: true)..fileUri = library.fileUri;
787 library.addMember(main); 805 library.addMember(main);
788 return main; 806 return main;
789 } 807 }
790 808
791 ast.Program loadProgram(Uri mainLibrary, {Target target, bool compileSdk}) { 809 void loadProgram(Uri mainLibrary, {Target target, bool compileSdk}) {
792 Uri uri = applicationRoot.relativeUri(mainLibrary); 810 ast.Library library = getLibraryReferenceFromUri(mainLibrary);
793 ast.Library library = repository.getLibraryReference(uri);
794 ensureLibraryIsLoaded(library); 811 ensureLibraryIsLoaded(library);
795 var mainMethod = _getMainMethod(mainLibrary); 812 var mainMethod = _getMainMethod(mainLibrary);
796 loadEverything(target: target, compileSdk: compileSdk); 813 loadEverything(target: target, compileSdk: compileSdk);
797 var program = new ast.Program(repository.libraries);
798 if (mainMethod == null) { 814 if (mainMethod == null) {
799 mainMethod = _makeMissingMainMethod(library); 815 mainMethod = _makeMissingMainMethod(library);
800 } 816 }
801 program.mainMethod = mainMethod; 817 program.mainMethod = mainMethod;
802 for (LibraryElement libraryElement in libraryElements) { 818 for (LibraryElement libraryElement in libraryElements) {
803 for (CompilationUnitElement compilationUnitElement 819 for (CompilationUnitElement compilationUnitElement
804 in libraryElement.units) { 820 in libraryElement.units) {
805 var source = compilationUnitElement.source; 821 var source = compilationUnitElement.source;
806 LineInfo lineInfo = context.computeLineInfo(source); 822 LineInfo lineInfo = context.computeLineInfo(source);
807 String sourceCode; 823 String sourceCode;
808 try { 824 try {
809 sourceCode = context.getContents(source).data; 825 sourceCode = context.getContents(source).data;
810 } catch (e) { 826 } catch (e) {
811 // The source's contents could not be accessed. 827 // The source's contents could not be accessed.
812 sourceCode = ''; 828 sourceCode = '';
813 } 829 }
814 program.uriToSource['${source.uri}'] = 830 program.uriToSource['${source.uri}'] =
815 new ast.Source(lineInfo.lineStarts, sourceCode); 831 new ast.Source(lineInfo.lineStarts, sourceCode);
816 } 832 }
817 } 833 }
818 return program;
819 } 834 }
820 835
821 ast.Library loadLibrary(Uri uri) { 836 ast.Library loadLibrary(Uri uri) {
822 ast.Library library = 837 ast.Library library = getLibraryReferenceFromUri(uri);
823 repository.getLibraryReference(applicationRoot.relativeUri(uri));
824 ensureLibraryIsLoaded(library); 838 ensureLibraryIsLoaded(library);
825 return library; 839 return library;
826 } 840 }
827 } 841 }
828 842
829 class Bimap<K, V> { 843 class Bimap<K, V> {
830 final Map<K, V> nodeMap = <K, V>{}; 844 final Map<K, V> nodeMap = <K, V>{};
831 final Map<V, K> inverse = <V, K>{}; 845 final Map<V, K> inverse = <V, K>{};
832 846
833 bool containsKey(K key) => nodeMap.containsKey(key); 847 bool containsKey(K key) => nodeMap.containsKey(key);
(...skipping 10 matching lines...) Expand all
844 /// Creates [DartLoader]s for a given configuration, while reusing the 858 /// Creates [DartLoader]s for a given configuration, while reusing the
845 /// [DartSdk] and [Packages] object if possible. 859 /// [DartSdk] and [Packages] object if possible.
846 class DartLoaderBatch { 860 class DartLoaderBatch {
847 Packages packages; 861 Packages packages;
848 DartSdk dartSdk; 862 DartSdk dartSdk;
849 863
850 String lastSdk; 864 String lastSdk;
851 String lastPackagePath; 865 String lastPackagePath;
852 bool lastStrongMode; 866 bool lastStrongMode;
853 867
854 Future<DartLoader> getLoader(Repository repository, DartOptions options, 868 Future<DartLoader> getLoader(ast.Program program, DartOptions options,
855 {String packageDiscoveryPath}) async { 869 {String packageDiscoveryPath}) async {
856 if (dartSdk == null || 870 if (dartSdk == null ||
857 lastSdk != options.sdk || 871 lastSdk != options.sdk ||
858 lastStrongMode != options.strongMode) { 872 lastStrongMode != options.strongMode) {
859 lastSdk = options.sdk; 873 lastSdk = options.sdk;
860 lastStrongMode = options.strongMode; 874 lastStrongMode = options.strongMode;
861 dartSdk = createDartSdk(options.sdk, strongMode: options.strongModeSdk); 875 dartSdk = createDartSdk(options.sdk, strongMode: options.strongModeSdk);
862 } 876 }
863 if (packages == null || 877 if (packages == null ||
864 lastPackagePath != options.packagePath || 878 lastPackagePath != options.packagePath ||
865 packageDiscoveryPath != null) { 879 packageDiscoveryPath != null) {
866 lastPackagePath = options.packagePath; 880 lastPackagePath = options.packagePath;
867 packages = await createPackages(options.packagePath, 881 packages = await createPackages(options.packagePath,
868 discoveryPath: packageDiscoveryPath); 882 discoveryPath: packageDiscoveryPath);
869 } 883 }
870 return new DartLoader(repository, options, packages, dartSdk: dartSdk); 884 return new DartLoader(program, options, packages, dartSdk: dartSdk);
871 } 885 }
872 } 886 }
873 887
874 Future<Packages> createPackages(String packagePath, 888 Future<Packages> createPackages(String packagePath,
875 {String discoveryPath}) async { 889 {String discoveryPath}) async {
876 if (packagePath != null) { 890 if (packagePath != null) {
877 var absolutePath = new io.File(packagePath).absolute.path; 891 var absolutePath = new io.File(packagePath).absolute.path;
878 if (await new io.Directory(packagePath).exists()) { 892 if (await new io.Directory(packagePath).exists()) {
879 return getPackagesDirectory(new Uri.file(absolutePath)); 893 return getPackagesDirectory(new Uri.file(absolutePath));
880 } else if (await new io.File(packagePath).exists()) { 894 } else if (await new io.File(packagePath).exists()) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext() 986 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext()
973 ..sourceFactory = new SourceFactory(resolvers) 987 ..sourceFactory = new SourceFactory(resolvers)
974 ..analysisOptions = createAnalysisOptions(options.strongMode); 988 ..analysisOptions = createAnalysisOptions(options.strongMode);
975 989
976 options.declaredVariables.forEach((String name, String value) { 990 options.declaredVariables.forEach((String name, String value) {
977 context.declaredVariables.define(name, value); 991 context.declaredVariables.define(name, value);
978 }); 992 });
979 993
980 return context; 994 return context;
981 } 995 }
OLDNEW
« no previous file with comments | « pkg/kernel/lib/analyzer/ast_from_analyzer.dart ('k') | pkg/kernel/lib/ast.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698