/** * Parse the specified source. Any exceptions thrown by the {@link DartParser} will be logged and * a {@link DartModelException} thrown. * * @param librarySource the source for the library containing the compilation unit being parsed * @param unitUri the URI of the compilation unit being parsed * @param parseErrors a collection to which parse errors are appended or <code>null</code> if * parse errors should be ignored * @return the parse result */ public static DartUnit resolveUnit( LibrarySource librarySource, URI unitUri, Map<URI, String> suppliedSources, final Collection<DartCompilationError> parseErrors) throws DartModelException { long start = System.currentTimeMillis(); ResolverRunnable runnable = new ResolverRunnable(librarySource, unitUri, suppliedSources, false, parseErrors); runnable.runSafe(); long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-resolveUnit", elapsed).log(); Instrumentation.operation("DartCompilerUtils-resolveUnit", elapsed) .with("librarySource-ElementName", librarySource.getName()) .with("librarySource.LastModified", librarySource.getLastModified()) .log(); if (runnable.exception != null) { throw new DartModelException( new CoreException( new Status( IStatus.ERROR, DartCore.PLUGIN_ID, "Failed to parse " + unitUri, runnable.exception))); } return runnable.unitResult; }
/** * Parse the compilation units in the specified library. Any exceptions thrown by the {@link * DartParser} will be logged and a {@link DartModelException} thrown. * * @param library the library to be parsed (not <code>null</code>) * @param parseErrors a collection to which parse errors are appended or <code>null</code> if * parse errors should be ignored * @return the parse result * @throws DartModelException if the library could not be parsed */ public static LibraryUnit resolveLibrary( LibrarySource library, Collection<DartUnit> suppliedUnits, final Collection<DartCompilationError> parseErrors) throws DartModelException { long start = System.currentTimeMillis(); ResolverRunnable runnable = new ResolverRunnable(library, createMap(suppliedUnits), false, parseErrors); runnable.runSafe(); long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-resolveLibrary", elapsed).log(); Instrumentation.operation("DartCompilerUtils-resolveLibrary", elapsed) .with("librarySource.Name", library.getName()) .with("librarySource.LastModified", library.getLastModified()) .log(); if (runnable.exception != null) { throw new DartModelException( new CoreException( new Status( IStatus.ERROR, DartCore.PLUGIN_ID, "Failed to parse " + library.getName(), runnable.exception))); } return runnable.libraryResult; }
/** A synchronized call to {@link DartCompiler#analyzeLibraries} */ public static Map<URI, LibraryUnit> secureAnalyzeLibraries( LibrarySource librarySource, SelectiveCache selectiveCache, CompilerConfiguration config, DartArtifactProvider provider, DartCompilerListener listener, boolean resolveAllNewLibs) throws IOException { // All calls to DartC must be synchronized long start = System.currentTimeMillis(); Map<URI, LibraryUnit> ret; synchronized (compilerLock) { ret = DartCompiler.analyzeLibraries( librarySource, selectiveCache, config, provider, listener, resolveAllNewLibs); } long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibraries", elapsed).log(); Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibraries", elapsed) .with("librarySource.Name", librarySource.getName()) .with("librarySource.LastModified", librarySource.getLastModified()) .log(); return ret; }
/** * Process the given resource within the context of the given working set in order to record the * data and relationships found within the resource. */ public void indexResource(File libraryFile, File sourceFile, DartUnit dartUnit) throws DartModelException { // Get the LibrarySource LibrarySource librarySource = dartUnit.getLibrary().getSource(); // Get the DartLibrary DartLibraryImpl library; IResource resource = ResourceUtil.getResource(libraryFile); if (resource == null) { library = new DartLibraryImpl(librarySource); } else { DartElement element = DartCore.create(resource); if (element instanceof CompilationUnitImpl) { element = ((CompilationUnitImpl) element).getLibrary(); } if (!(element instanceof DartLibrary)) { DartCore.logError("Expected library to be associated with " + libraryFile); return; } library = (DartLibraryImpl) element; } // Get the CompilationUnit DartSource unitSource = (DartSource) dartUnit.getSourceInfo().getSource(); CompilationUnit compilationUnit; IResource res = ResourceUtil.getResource(sourceFile); if (res != null) { DefaultWorkingCopyOwner workingCopy = DefaultWorkingCopyOwner.getInstance(); compilationUnit = new CompilationUnitImpl(library, (IFile) res, workingCopy); } else { String relPath = unitSource.getRelativePath(); compilationUnit = new ExternalCompilationUnitImpl(library, relPath, unitSource); } URI unitUri = unitSource.getUri(); Resource indexerResource; if (PackageLibraryManager.isDartUri(unitUri)) { indexerResource = new Resource( ResourceFactory.composeResourceId( librarySource.getUri().toString(), unitUri.toString())); } else if (PackageLibraryManager.isPackageUri(unitUri)) { indexerResource = new Resource( ResourceFactory.composeResourceId( libraryFile.toURI().toString(), sourceFile.toURI().toString())); } else { indexerResource = ResourceFactory.getResource(compilationUnit); } // Queue the resource to be indexed indexResource(indexerResource, libraryFile, compilationUnit, dartUnit); }
@Override public Void visitStringLiteral(DartStringLiteral node) { if (foundElement == null) { int start = node.getSourceInfo().getOffset(); int length = node.getSourceInfo().getLength(); int end = start + length; if (end == 0) { return null; } if (start <= startOffset && end >= endOffset) { wordRegion = computeInternalStringRegion(start, length); DartNode parent = node.getParent(); if (parent instanceof DartSourceDirective && ((DartSourceDirective) parent).getSourceUri() == node) { // resolvedElement = ((DartSourceDirective) parent).getElement(); DartLibrary library = compilationUnit.getLibrary(); String fileName = getFileName(library, node.getValue()); CompilationUnit sourcedUnit = library.getCompilationUnit(fileName); if (sourcedUnit != null && sourcedUnit.exists()) { foundElement = sourcedUnit; } } else if (parent instanceof DartResourceDirective && ((DartResourceDirective) parent).getResourceUri() == node) { // resolvedElement = ((DartResourceDirective) parent).getElement(); DartLibrary library = compilationUnit.getLibrary(); try { DartSource unitSource = compilationUnit.getSourceRef(); if (unitSource != null) { LibrarySource librarySource = unitSource.getLibrary(); if (librarySource != null) { DartSource resourceSource = librarySource.getSourceFor(node.getValue()); if (resourceSource != null) { URI resourceUri = resourceSource.getUri(); DartResource resource = library.getResource(resourceUri); if (resource != null && resource.exists()) { foundElement = resource; } } } } } catch (DartModelException exception) { foundElement = null; } } throw new DartElementFoundException(); } } return null; }
/** * A synchronized call to {@link DartCompiler#compileLib(LibrarySource, CompilerConfiguration, * DartArtifactProvider, DartCompilerListener)} */ public static void secureCompileLib( LibrarySource libSource, CompilerConfiguration config, DartArtifactProvider provider, DartCompilerListener listener) throws IOException { long start = System.currentTimeMillis(); if (!DartSdkManager.getManager().hasSdk()) { return; } List<LibrarySource> embeddedLibraries = new ArrayList<LibrarySource>(); // All calls to DartC must be synchronized synchronized (compilerLock) { DartCompiler.compileLib(libSource, embeddedLibraries, config, provider, listener); } long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-secureCompileLib", elapsed).log(); Instrumentation.operation("DartCompilerUtils-secureCompileLib", elapsed) .with("librarySource.Name", libSource.getName()) .log(); }
@Override public LibrarySource getImportFor(String relPath) throws IOException { try { return wrappedSource.getImportFor(relPath); } catch (Exception exception) { DartCore.logInformation("Could not find import: '" + relPath + "'", exception); return null; } }
@Override public DartSource getSourceFor(String relPath) { URI uri; try { uri = wrappedSource.getUri().resolve(new URI(null, null, relPath, null)); } catch (URISyntaxException e) { uri = null; } if (uri != null) { for (URI key : suppliedSources.keySet()) { if (equalUris(libraryManager, key, uri)) { String source = suppliedSources.get(key); return new DartURIStringSource(this, uri, relPath, source); } } } return wrappedSource.getSourceFor(relPath); }
DeltaAnalysisRunnable( LibrarySource librarySource, URI unitUri, Map<URI, String> suppliedSources, DartUnit suppliedUnit, DartNode completionNode, int completionLocation, Collection<DartCompilationError> parseErrors) { super(parseErrors); librarySource.getClass(); // quick null check unitUri.getClass(); // quick null check suppliedSources.getClass(); // quick null check suppliedUnit.getClass(); // quick null check completionNode.getClass(); // quick null check parseErrors.getClass(); // quick null check this.librarySource = new LibraryWithSuppliedSources(librarySource, suppliedSources); this.parsedUnit = suppliedUnit; this.completionNode = completionNode; this.completionLocation = completionLocation; String sourceString = suppliedSources.get(unitUri); this.source = new DartSourceString(unitUri.getPath(), sourceString); this.unitUri = unitUri; }
/** A synchronized call to {@link DartCompiler#analyzeLibrary} */ public static LibraryUnit secureAnalyzeLibrary( LibrarySource librarySource, final Map<URI, DartUnit> parsedUnits, final CompilerConfiguration config, DartArtifactProvider provider, DartCompilerListener listener) throws IOException { long start = System.currentTimeMillis(); if (!DartSdkManager.getManager().hasSdk()) { return null; } final PackageLibraryManager manager = PackageLibraryManagerProvider.getPackageLibraryManager(); AnalysisServer server = PackageLibraryManagerProvider.getDefaultAnalysisServer(); URI librarySourceUri = librarySource.getUri(); if (parsedUnits == null && !(librarySource instanceof LibraryWithSuppliedSources)) { // Resolve dart:<libname> to file URI before calling AnalysisServer URI libraryFileUri = manager.resolveDartUri(librarySourceUri); File libraryFile = new File(libraryFileUri.getPath()); LibraryUnit ret = server.getSavedContext().resolve(libraryFile, 30000); long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed).log(); Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed) .with("librarySource.Name", librarySource.getName()) .with("librarySource.LastModified", librarySource.getLastModified()) .log(); return ret; } // Resolve the specified library against all currently cached libraries final Map<URI, LibraryUnit> resolvedLibs = server.getSavedContext().getResolvedLibraries(50); resolvedLibs.remove(librarySourceUri); // Construct the selective cache SelectiveCache selectiveCache = new DartCompiler.SelectiveCache() { @Override public Map<URI, LibraryUnit> getResolvedLibraries() { return resolvedLibs; } @Override public DartUnit getUnresolvedDartUnit(DartSource dartSrc) { if (parsedUnits == null) { return null; } URI srcUri = dartSrc.getUri(); DartUnit parsedUnit = parsedUnits.remove(srcUri); if (parsedUnit != null) { return parsedUnit; } URI fileUri = manager.resolveDartUri(srcUri); return parsedUnits.remove(fileUri); } }; Map<URI, LibraryUnit> libMap; // All calls to DartC must be synchronized synchronized (compilerLock) { libMap = DartCompiler.analyzeLibraries( librarySource, selectiveCache, config, provider, listener, false); } long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed) .with("libMapHasResult", Boolean.toString(libMap != null)) .log(); Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed) .with("librarySource.Name", librarySource.getName()) .with("libMapHasResult", Boolean.toString(libMap != null)) .with("librarySource.LastModified", librarySource.getLastModified()) .log(); return libMap != null ? libMap.get(librarySourceUri) : null; }
@Override public long getLastModified() { return wrappedSource.getLastModified(); }
@Override public boolean exists() { return wrappedSource.exists(); }
public static DartNode analyzeDelta( LibrarySource library, String sourceString, DartUnit suppliedUnit, DartNode completionNode, int completionLocation, final Collection<DartCompilationError> parseErrors) throws DartModelException { long start = System.currentTimeMillis(); if (cachedLibraries.get(library) == null) { Collection<DartUnit> parsedUnits = new ArrayList<DartUnit>(); parsedUnits.add(suppliedUnit); LibraryUnit resolvedLib = resolveLibrary(library, parsedUnits, parseErrors); synchronized (cachedLibraries) { LibraryUnit newLib = cachedLibraries.get(library); if (newLib == null) { cachedLibraries.put(library, resolvedLib); } } long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-analyzeDelta", elapsed) .with("Cached", "false") .with("ParsedUnits", parsedUnits.size()) .with("ParseErrors", parseErrors != null ? parseErrors.size() : 0) .with("sourceString-length", sourceString.length()) .with("CachedLibraries", "false") .log(); Instrumentation.operation("DartCompilerUtils-analyzeDelta", elapsed) .with("analyzeDelta.Name", library.getName()) .with("librarySource.LastModified", library.getLastModified()) .with("sourceString", sourceString) .with("sourceString-length", sourceString.length()) .with("CachedLibraries", "true") .log(); return completionNode; } DartSource src = (DartSource) suppliedUnit.getSourceInfo().getSource(); URI unitUri = src.getUri(); Map<URI, String> suppliedSources = new HashMap<URI, String>(); suppliedSources.put(unitUri, sourceString); DeltaAnalysisRunnable runnable = new DeltaAnalysisRunnable( library, unitUri, suppliedSources, suppliedUnit, completionNode, completionLocation, parseErrors); runnable.runSafe(); long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-analyzeDelta", elapsed) .with("Cached", "false") .with("ParseErrors", parseErrors != null ? parseErrors.size() : 0) .with("sourceString-length", sourceString.length()) .with("CachedLibraries", "false") .log(); Instrumentation.operation("DartCompilerUtils-analyzeDelta", elapsed) .with("analyzeDelta.Name", library.getName()) .with("librarySource.LastModified", library.getLastModified()) .with("sourceString", sourceString) .with("sourceString-length", sourceString.length()) .with("CachedLibraries", "true") .log(); if (runnable.exception != null) { throw new DartModelException( new CoreException( new Status( IStatus.ERROR, DartCore.PLUGIN_ID, "Failed to parse " + library.getName(), runnable.exception))); } return runnable.analyzedNode; }
@Override public URI getUri() { return wrappedSource.getUri(); }
@Override public String getUniqueIdentifier() { return wrappedSource.getUniqueIdentifier(); }
@Override public Reader getSourceReader() throws IOException { return wrappedSource.getSourceReader(); }
@Override public String getName() { return wrappedSource.getName(); }