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

Unified Diff: compiler/java/com/google/dart/compiler/ast/LibraryUnit.java

Issue 9148026: Recompile unit with potential conflict/dependency on some top-level symbol. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix for compiling corelib, so NPE in TreeShaker Created 8 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: compiler/java/com/google/dart/compiler/ast/LibraryUnit.java
diff --git a/compiler/java/com/google/dart/compiler/ast/LibraryUnit.java b/compiler/java/com/google/dart/compiler/ast/LibraryUnit.java
index 0a18d76b65df4eb3ef6a263392fb6873f6542ca5..bee120972813f246bbbd558ca627f74312a64507 100644
--- a/compiler/java/com/google/dart/compiler/ast/LibraryUnit.java
+++ b/compiler/java/com/google/dart/compiler/ast/LibraryUnit.java
@@ -4,33 +4,22 @@
package com.google.dart.compiler.ast;
-import com.google.common.io.CharStreams;
import com.google.dart.compiler.DartCompiler;
import com.google.dart.compiler.DartCompilerContext;
-import com.google.dart.compiler.DartCompilerListener;
import com.google.dart.compiler.DartSource;
import com.google.dart.compiler.LibraryDeps;
import com.google.dart.compiler.LibrarySource;
-import com.google.dart.compiler.common.SourceInfo;
-import com.google.dart.compiler.metrics.DartEventType;
-import com.google.dart.compiler.metrics.Tracer;
-import com.google.dart.compiler.metrics.Tracer.TraceEvent;
-import com.google.dart.compiler.parser.DartParser;
-import com.google.dart.compiler.parser.DartScannerParserContext;
import com.google.dart.compiler.resolver.Elements;
import com.google.dart.compiler.resolver.LibraryElement;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
-import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;
@@ -38,11 +27,6 @@ import java.util.concurrent.ConcurrentSkipListMap;
* Represents the parsed source from a {@link LibrarySource}.
*/
public class LibraryUnit {
-
- // This is intentionally unparseable as Dart.
- private static final String UNIT_SEPARATOR_NAME = "--- unit-name: ";
- private static final String UNIT_SEPARATOR_URI = "--- unit-uri: ";
-
private final LibrarySource libSource;
private final LibraryNode selfSourcePath;
private final Collection<LibraryNode> importPaths = new ArrayList<LibraryNode>();
@@ -56,7 +40,6 @@ public class LibraryUnit {
private final LibraryElement element;
- private Map<String, DartNode> topLevelNodes;
private LibraryDeps deps;
private LibraryNode entryNode;
@@ -235,136 +218,6 @@ public class LibraryUnit {
}
/**
- * Loads this library's associated api. If the api file exists, this will result in the library
- * being populated with "diet" units (i.e., {@link DartUnit#isDiet()} will return
- * <code>true</code>).
- *
- * @return <code>true</code> if the library was loaded from its api
- */
- public boolean loadApi(DartCompilerContext context, DartCompilerListener listener)
- throws IOException {
- TraceEvent parseEvent =
- Tracer.canTrace() ? Tracer.start(DartEventType.PARSE_API, "src", getSource().getUri()
- .toString()) : null;
- try {
- final Reader r = context.getArtifactReader(libSource, "", DartCompiler.EXTENSION_API);
- if (r == null) {
- return false;
- }
-
- // Read the API file.
- String srcCode = CharStreams.toString(r);
- r.close();
-
- // Split it up by unit.
- int idx = srcCode.indexOf(UNIT_SEPARATOR_NAME);
- while (idx != -1) {
- int endIdx;
-
- // Prepare unit name.
- idx += UNIT_SEPARATOR_NAME.length();
- endIdx = srcCode.indexOf('\n', idx);
- String unitName = srcCode.substring(idx, endIdx);
- idx = endIdx;
-
- // Prepare unit URI.
- idx = srcCode.indexOf(UNIT_SEPARATOR_URI, endIdx);
- idx += UNIT_SEPARATOR_URI.length();
- endIdx = srcCode.indexOf('\n', idx);
- String unitUri = srcCode.substring(idx, endIdx);
- idx = endIdx;
-
- // Find next unit, may be end string.
- endIdx = srcCode.indexOf(UNIT_SEPARATOR_NAME, idx);
-
- // Parse diet source unit.
- String code = endIdx != -1 ? srcCode.substring(idx, endIdx) : srcCode.substring(idx);
- parseApiUnit(unitName, unitUri, code, libSource, listener);
-
- // Process next unit.
- idx = endIdx;
- }
-
- return true;
- } finally {
- Tracer.end(parseEvent);
- }
- }
-
- /**
- * Saves this library's contents to its associated api file.
- */
- public void saveApi(DartCompilerContext context) throws IOException {
- Writer w = context.getArtifactWriter(libSource, "", DartCompiler.EXTENSION_API);
- for (Entry<String, DartUnit> entry : units.entrySet()) {
- String unitName = entry.getKey();
- DartUnit unit = entry.getValue();
- w.write(UNIT_SEPARATOR_NAME + unitName + "\n");
- w.write(UNIT_SEPARATOR_URI + unit.getSource().getUri() + "\n");
- w.write(unit.toDietSource());
- }
- w.close();
- }
-
- /**
- * Populates this unit's class map. This can be called only once per unit, and must be called
- * before {@link #getTopLevelNode(String)} and {@link #getTopLevelNodes()}.
- */
- public void populateTopLevelNodes() {
- assert topLevelNodes == null;
- topLevelNodes = new HashMap<String, DartNode>();
-
- DartNodeTraverser<Void> visitor = new DartNodeTraverser<Void>() {
- @Override
- public Void visitClass(DartClass node) {
- topLevelNodes.put(node.getClassName(), node);
- return null;
- }
-
- @Override
- public Void visitMethodDefinition(DartMethodDefinition node) {
- // Method names are always identifiers, except for factories, which cannot appear
- // in this context.
- DartExpression name = node.getName();
- if(name instanceof DartIdentifier) {
- topLevelNodes.put(((DartIdentifier) name).getTargetName(), node);
- } else {
- // Visit the unknown node to generate a string for our use.
- topLevelNodes.put(node.getName().toSource(), node);
- }
- return null;
- }
-
- @Override
- public Void visitField(DartField node) {
- topLevelNodes.put(node.getName().getTargetName(), node);
- return null;
- }
- };
-
- for (DartUnit unit : units.values()) {
- visitor.visitUnit(unit);
- }
- }
-
- /**
- * Get an unmodifiable collection of the classes in this library. You must call
- * {@link #populateTopLevelNodes()} before this method will work.
- */
- public Collection<DartNode> getTopLevelNodes() {
- return Collections.unmodifiableCollection(topLevelNodes.values());
- }
-
- /**
- * Gets the {@link DartClass} associated with the given name. You must call
- * {@link #populateTopLevelNodes()} before this method will work.
- */
- public DartNode getTopLevelNode(String name) {
- assert topLevelNodes != null;
- return topLevelNodes.get(name);
- }
-
- /**
* Return the declared entry method, if any
*
* @return the entry method or <code>null</code> if not defined
@@ -393,8 +246,11 @@ public class LibraryUnit {
Reader reader = context.getArtifactReader(libSource, "", DartCompiler.EXTENSION_DEPS);
if (reader != null) {
- deps = LibraryDeps.fromReader(reader);
- reader.close();
+ try {
codefu 2012/01/19 21:48:54 fromReader() doesn't throw, you can remove the try
scheglov 2012/01/20 15:59:24 Done.
+ deps = LibraryDeps.fromReader(reader);
+ } finally {
+ reader.close();
+ }
}
if (deps == null) {
@@ -411,65 +267,4 @@ public class LibraryUnit {
deps.write(writer);
writer.close();
}
-
- private void parseApiUnit(final String unitName,
- final String unitUri,
- String srcCode,
- final LibrarySource libSrc,
- DartCompilerListener listener) {
- // Dummy source for the api unit.
- DartSource src = new DartSource() {
- @Override
- public LibrarySource getLibrary() {
- return libSrc;
- }
-
- @Override
- public String getName() {
- return unitName;
- }
-
- @Override
- public Reader getSourceReader() {
- return null;
- }
-
- @Override
- public URI getUri() {
- return URI.create(unitUri);
- }
-
- @Override
- public boolean exists() {
- return true;
- }
-
- @Override
- public long getLastModified() {
- return 0;
- }
-
- @Override
- public String getRelativePath() {
- return unitName;
- }
- };
-
- DartScannerParserContext parserContext = new DartScannerParserContext(src, srcCode, listener);
- DartParser parser = new DartParser(parserContext, true);
- DartUnit unit = parser.parseUnit(src);
-
- // When parsing from an API file, generate and store the hash for top level
- // classes while we have the string available. Reduces the time needed to
- // recompute this later with a visitor.
- for (DartNode node : unit.getTopLevelNodes()) {
- if (node instanceof DartClass) {
- SourceInfo nodeInfo = node.getSourceInfo();
- String nodeString = srcCode.substring(nodeInfo.getSourceStart(),
- nodeInfo.getSourceStart()+nodeInfo.getSourceLength());
- ((DartClass)node).setHash(nodeString.hashCode());
- }
- }
- putUnit(unit);
- }
}

Powered by Google App Engine
This is Rietveld 408576698