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

Side by Side Diff: lib/src/compiler/code_generator.dart

Issue 1917863005: Qualify library names in packages (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Use restoreAbsolute Created 4 years, 7 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
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 import 'dart:collection' show HashMap, HashSet; 5 import 'dart:collection' show HashMap, HashSet;
6 6
7 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; 7 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator;
8 import 'package:analyzer/dart/ast/ast.dart' hide ConstantEvaluator; 8 import 'package:analyzer/dart/ast/ast.dart' hide ConstantEvaluator;
9 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; 9 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType;
10 import 'package:analyzer/dart/element/element.dart'; 10 import 'package:analyzer/dart/element/element.dart';
11 import 'package:analyzer/dart/element/type.dart'; 11 import 'package:analyzer/dart/element/type.dart';
12 import 'package:analyzer/src/dart/ast/token.dart' show StringToken; 12 import 'package:analyzer/src/dart/ast/token.dart' show StringToken;
13
14 // TODO(vsm): Delete
15 import 'package:analyzer/src/context/source.dart' show SourceFactoryImpl;
16
13 //TODO(leafp): Remove deprecated dependency 17 //TODO(leafp): Remove deprecated dependency
14 //ignore: DEPRECATED_MEMBER_USE 18 //ignore: DEPRECATED_MEMBER_USE
15 import 'package:analyzer/src/generated/element.dart' 19 import 'package:analyzer/src/generated/element.dart'
16 show DynamicTypeImpl, LocalVariableElementImpl; 20 show DynamicTypeImpl, LocalVariableElementImpl;
17 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; 21 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext;
18 import 'package:analyzer/src/generated/resolver.dart' 22 import 'package:analyzer/src/generated/resolver.dart'
19 show TypeProvider, NamespaceBuilder; 23 show TypeProvider, NamespaceBuilder;
20 import 'package:analyzer/src/generated/type_system.dart' 24 import 'package:analyzer/src/generated/type_system.dart'
21 show StrongTypeSystemImpl; 25 show StrongTypeSystemImpl;
22 import 'package:analyzer/src/summary/summarize_elements.dart' 26 import 'package:analyzer/src/summary/summarize_elements.dart'
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 compilationUnits = CoercionReifier.reify(compilationUnits); 187 compilationUnits = CoercionReifier.reify(compilationUnits);
184 188
185 // Initialize our library variables. 189 // Initialize our library variables.
186 var items = <JS.ModuleItem>[]; 190 var items = <JS.ModuleItem>[];
187 for (var unit in compilationUnits) { 191 for (var unit in compilationUnits) {
188 var library = unit.element.library; 192 var library = unit.element.library;
189 if (unit.element != library.definingCompilationUnit) continue; 193 if (unit.element != library.definingCompilationUnit) continue;
190 194
191 var libraryTemp = _isDartRuntime(library) 195 var libraryTemp = _isDartRuntime(library)
192 ? _runtimeLibVar 196 ? _runtimeLibVar
193 : new JS.TemporaryId(jsLibraryName(library)); 197 : new JS.TemporaryId(jsLibraryName(context, library));
194 _libraries[library] = libraryTemp; 198 _libraries[library] = libraryTemp;
195 items.add(new JS.ExportDeclaration( 199 items.add(new JS.ExportDeclaration(
196 js.call('const # = Object.create(null)', [libraryTemp]))); 200 js.call('const # = Object.create(null)', [libraryTemp])));
197 201
198 // dart:_runtime has a magic module that holds extenstion method symbols. 202 // dart:_runtime has a magic module that holds extenstion method symbols.
199 // TODO(jmesserly): find a cleaner design for this. 203 // TODO(jmesserly): find a cleaner design for this.
200 if (_isDartRuntime(library)) { 204 if (_isDartRuntime(library)) {
201 items.add(new JS.ExportDeclaration( 205 items.add(new JS.ExportDeclaration(
202 js.call('const # = Object.create(null)', [_dartxVar]))); 206 js.call('const # = Object.create(null)', [_dartxVar])));
203 } 207 }
(...skipping 3473 matching lines...) Expand 10 before | Expand all | Expand 10 after
3677 node.externalKeyword != null || _functionBody(node) is NativeFunctionBody; 3681 node.externalKeyword != null || _functionBody(node) is NativeFunctionBody;
3678 3682
3679 FunctionBody _functionBody(node) => 3683 FunctionBody _functionBody(node) =>
3680 node is FunctionDeclaration ? node.functionExpression.body : node.body; 3684 node is FunctionDeclaration ? node.functionExpression.body : node.body;
3681 3685
3682 /// Returns the canonical name to refer to the Dart library. 3686 /// Returns the canonical name to refer to the Dart library.
3683 JS.Identifier emitLibraryName(LibraryElement library) { 3687 JS.Identifier emitLibraryName(LibraryElement library) {
3684 // It's either one of the libraries in this module, or it's an import. 3688 // It's either one of the libraries in this module, or it's an import.
3685 return _libraries[library] ?? 3689 return _libraries[library] ??
3686 _imports.putIfAbsent( 3690 _imports.putIfAbsent(
3687 library, () => new JS.TemporaryId(jsLibraryName(library))); 3691 library, () => new JS.TemporaryId(jsLibraryName(context, library)));
3688 } 3692 }
3689 3693
3690 JS.Node annotate(JS.Node node, AstNode original, [Element element]) { 3694 JS.Node annotate(JS.Node node, AstNode original, [Element element]) {
3691 if (options.closure && element != null) { 3695 if (options.closure && element != null) {
3692 node = node.withClosureAnnotation(closureAnnotationFor( 3696 node = node.withClosureAnnotation(closureAnnotationFor(
3693 node, original, element, namedArgumentTemp.name)); 3697 node, original, element, namedArgumentTemp.name));
3694 } 3698 }
3695 return node..sourceInformation = original; 3699 return node..sourceInformation = original;
3696 } 3700 }
3697 3701
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
3753 // TODO(leafp): The above only handles the case where the return type 3757 // TODO(leafp): The above only handles the case where the return type
3754 // is exactly Future/Stream/Iterable. Handle the subtype case. 3758 // is exactly Future/Stream/Iterable. Handle the subtype case.
3755 return DynamicTypeImpl.instance; 3759 return DynamicTypeImpl.instance;
3756 } 3760 }
3757 } 3761 }
3758 } 3762 }
3759 3763
3760 /// Choose a canonical name from the library element. 3764 /// Choose a canonical name from the library element.
3761 /// This never uses the library's name (the identifier in the `library` 3765 /// This never uses the library's name (the identifier in the `library`
3762 /// declaration) as it doesn't have any meaningful rules enforced. 3766 /// declaration) as it doesn't have any meaningful rules enforced.
3763 String jsLibraryName(LibraryElement library) { 3767 String jsLibraryName(AnalysisContext context, LibraryElement library) {
3768 var sourceFactory = context.sourceFactory as SourceFactoryImpl;
vsm 2016/04/27 18:24:35 sourceFactory has it's own restoreAbsolute that wa
Jennifer Messerly 2016/04/27 18:30:05 Oh crazy! Is that hard coded? You could change the
Paul Berry 2016/04/27 19:19:57 Are you sure about this? Looking at the definitio
vsm 2016/04/27 20:56:08 yep, just need to set in the right order! will fi
3769 for (var resolver in sourceFactory.resolvers) {
3770 var uri = resolver.restoreAbsolute(library.source);
Paul Berry 2016/04/27 19:19:57 Why not just: var uri = library.source.uri; This
Jennifer Messerly 2016/04/27 19:24:21 ah, thanks, I wondered that too! Figured maybe I w
vsm 2016/04/27 20:56:08 that should actually work for the bazel use case.
Paul Berry 2016/04/27 21:13:49 Ah, fair point. Ok, I see why this is a use case
Jennifer Messerly 2016/04/27 21:52:51 yeah +1 to all that @Vijay -- the DDC spot to fix
3771 if (uri?.scheme == 'package') {
3772 // Strip the package name.
3773 // TODO(vsm): This is not unique if an escaped '/'appears in a filename.
3774 // E.g., "foo/bar.dart" and "foo$47bar.dart" would collide.
3775 var packageRelativePath = uri.pathSegments.skip(1).join('/');
3776 return pathToJSIdentifier(packageRelativePath);
3777 }
3778 }
3779 // TODO(vsm): This is not unique, but we're not in
3780 // a package. We need a mechanism for creating a unique library
3781 // name.
3764 return pathToJSIdentifier(library.source.uri.pathSegments.last); 3782 return pathToJSIdentifier(library.source.uri.pathSegments.last);
3765 } 3783 }
3766 3784
3767 /// Shorthand for identifier-like property names. 3785 /// Shorthand for identifier-like property names.
3768 /// For now, we emit them as strings and the printer restores them to 3786 /// For now, we emit them as strings and the printer restores them to
3769 /// identifiers if it can. 3787 /// identifiers if it can.
3770 // TODO(jmesserly): avoid the round tripping through quoted form. 3788 // TODO(jmesserly): avoid the round tripping through quoted form.
3771 JS.LiteralString _propertyName(String name) => js.string(name, "'"); 3789 JS.LiteralString _propertyName(String name) => js.string(name, "'");
3772 3790
3773 // TODO(jacobr): we would like to do something like the following 3791 // TODO(jacobr): we would like to do something like the following
(...skipping 14 matching lines...) Expand all
3788 } 3806 }
3789 3807
3790 bool isLibraryPrefix(Expression node) => 3808 bool isLibraryPrefix(Expression node) =>
3791 node is SimpleIdentifier && node.staticElement is PrefixElement; 3809 node is SimpleIdentifier && node.staticElement is PrefixElement;
3792 3810
3793 LibraryElement _getLibrary(AnalysisContext c, String uri) => 3811 LibraryElement _getLibrary(AnalysisContext c, String uri) =>
3794 c.computeLibraryElement(c.sourceFactory.forUri(uri)); 3812 c.computeLibraryElement(c.sourceFactory.forUri(uri));
3795 3813
3796 bool _isDartRuntime(LibraryElement l) => 3814 bool _isDartRuntime(LibraryElement l) =>
3797 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; 3815 l.isInSdk && l.source.uri.toString() == 'dart:_runtime';
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698