Search in sources :

Example 1 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class ResolverService method resolve.

@Override
public Resolution resolve(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
    if (!result.valid() || !result.hasAst()) {
        return null;
    }
    final FileObject source = result.source();
    final IContext context = result.context();
    final IProject project = context.project();
    final ILanguageImpl language = context.language();
    final FacetContribution<ResolverFacet> facetContrib = facet(language);
    final ResolverFacet facet = facetContrib.facet;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
        final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple;
        try (IClosableLock lock = context.read()) {
            tuple = common.outputs(termFactory, interpreter, source, source, result.ast(), inRegion, strategy);
        }
        return resolve(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Reference resolution failed", e);
    }
}
Also used : IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 2 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class Builder method transform.

private Collection<T> transform(BuildInput input, ILanguageImpl langImpl, FileObject location, Multimap<IContext, A> allAnalysisUnits, Set<FileName> includeFiles, boolean pardoned, Set<FileName> removedResources, Collection<IMessage> extraMessages, RefBool success, IProgress progress, ICancel cancel) throws InterruptedException {
    final int size = allAnalysisUnits.size();
    progress.setWorkRemaining(size);
    final Collection<T> allTransformUnits = Lists.newArrayListWithCapacity(size);
    if (size == 0) {
        return allTransformUnits;
    }
    progress.setDescription("Compiling " + size + " file(s) of " + langImpl.belongsTo().name());
    logger.debug("Compiling {} analysis results", size);
    for (Entry<IContext, Collection<A>> entry : allAnalysisUnits.asMap().entrySet()) {
        cancel.throwIfCancelled();
        final IContext context = entry.getKey();
        final Iterable<A> analysisResults = entry.getValue();
        try (IClosableLock lock = context.read()) {
            for (A analysisResult : analysisResults) {
                cancel.throwIfCancelled();
                final FileObject source = analysisResult.source();
                final FileName name = source.getName();
                if (removedResources.contains(name) || includeFiles.contains(name)) {
                    // Don't compile removed resources, which the analysis results contain for legacy reasons.
                    // Don't transform included resources, they should just be parsed and analyzed.
                    progress.work(1);
                    continue;
                }
                if (!analysisResult.valid()) {
                    logger.warn("Input result for {} is invalid, cannot transform it", source != null ? source.getName().getPath() : "detached source");
                    progress.work(1);
                    continue;
                }
                for (ITransformGoal goal : input.transformGoals) {
                    cancel.throwIfCancelled();
                    if (!transformService.available(context, goal)) {
                        logger.trace("No {} transformation required for {}", goal, context.language());
                        progress.work(1);
                        continue;
                    }
                    try {
                        final Collection<TA> results = transformService.transform(analysisResult, context, goal);
                        for (TA result : results) {
                            final boolean noErrors = printMessages(result.messages(), goal + " transformation", input, pardoned);
                            success.and(noErrors);
                            @SuppressWarnings("unchecked") final T genericResult = (T) result;
                            allTransformUnits.add(genericResult);
                        }
                        progress.work(1);
                    } catch (TransformException e) {
                        final String message = String.format("Transformation failed unexpectedly for %s", name);
                        logger.error(message, e);
                        final boolean noErrors = printMessage(source, message, e, input, pardoned);
                        success.and(noErrors);
                        extraMessages.add(MessageFactory.newBuilderErrorAtTop(location, "Transformation failed unexpectedly", e));
                    }
                }
            }
        // GTODO: also compile any affected sources
        }
    }
    return allTransformUnits;
}
Also used : IContext(org.metaborg.core.context.IContext) FileName(org.apache.commons.vfs2.FileName) TransformException(org.metaborg.core.transform.TransformException) ITransformGoal(org.metaborg.core.action.ITransformGoal) Collection(java.util.Collection) IClosableLock(org.metaborg.util.concurrent.IClosableLock) FileObject(org.apache.commons.vfs2.FileObject)

Example 3 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class AnalysisResultProcessor method getUpdates.

private BehaviorSubject<AnalysisChange<A>> getUpdates(I input, IContext context) {
    if (input.detached()) {
        throw new MetaborgRuntimeException("Cannot get updates for detached (no source) units");
    }
    final FileObject source = input.source();
    final FileName name = source.getName();
    // THREADING: it is possible that two different threads asking for a subject may do the parsing twice here, as
    // this is not an atomic operation. However, the chance is very low and it does not break anything (only
    // duplicates some work), so it is acceptable.
    BehaviorSubject<AnalysisChange<A>> updates = updatesPerResource.get(name);
    if (updates == null) {
        updates = BehaviorSubject.create();
        updatesPerResource.put(name, updates);
        try {
            logger.trace("Requesting parse result for {}", source);
            final P parseResult = parseResultRequester.request(input).toBlocking().single();
            if (!parseResult.valid()) {
                updates.onNext(AnalysisChange.<A>error(source, new AnalysisException(context, "Parsing failed")));
                return updates;
            }
            logger.trace("Analysing for {}", source);
            final IAnalyzeResult<A, AU> result;
            try (IClosableLock lock = context.write()) {
                result = analysisService.analyze(parseResult, context);
            }
            updates.onNext(AnalysisChange.<A>update(source, result.result()));
        // HACK: ignore analyze unit updates from result.updates(), may cause incrementality problems.
        } catch (AnalysisException e) {
            final String message = logger.format("Analysis for {} failed", name);
            logger.error(message, e);
            updates.onNext(AnalysisChange.<A>error(source, e));
        } catch (Exception e) {
            final String message = logger.format("Analysis for {} failed", name);
            logger.error(message, e);
            updates.onNext(AnalysisChange.<A>error(source, new AnalysisException(context, message, e)));
        }
    }
    return updates;
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) FileName(org.apache.commons.vfs2.FileName) MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) AnalysisException(org.metaborg.core.analysis.AnalysisException) AnalysisException(org.metaborg.core.analysis.AnalysisException) FileObject(org.apache.commons.vfs2.FileObject) IClosableLock(org.metaborg.util.concurrent.IClosableLock)

Example 4 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class IndexTaskContext method readLock.

private IClosableLock readLock() {
    final Lock readLock = lock.readLock();
    final IClosableLock lock = new ClosableLock(readLock);
    return lock;
}
Also used : IClosableLock(org.metaborg.util.concurrent.IClosableLock) ClosableLock(org.metaborg.util.concurrent.ClosableLock) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) IClosableLock(org.metaborg.util.concurrent.IClosableLock) Lock(java.util.concurrent.locks.Lock) ClosableLock(org.metaborg.util.concurrent.ClosableLock)

Example 5 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class IndexTaskContext method writeLock.

private IClosableLock writeLock() {
    final Lock writeLock = lock.writeLock();
    final IClosableLock lock = new ClosableLock(writeLock);
    return lock;
}
Also used : IClosableLock(org.metaborg.util.concurrent.IClosableLock) ClosableLock(org.metaborg.util.concurrent.ClosableLock) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) IClosableLock(org.metaborg.util.concurrent.IClosableLock) Lock(java.util.concurrent.locks.Lock) ClosableLock(org.metaborg.util.concurrent.ClosableLock)

Aggregations

IClosableLock (org.metaborg.util.concurrent.IClosableLock)15 FileObject (org.apache.commons.vfs2.FileObject)6 Lock (java.util.concurrent.locks.Lock)4 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)4 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)4 IContext (org.metaborg.core.context.IContext)4 ClosableLock (org.metaborg.util.concurrent.ClosableLock)4 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)3 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)3 Collection (java.util.Collection)2 FileName (org.apache.commons.vfs2.FileName)2 MetaborgException (org.metaborg.core.MetaborgException)2 AnalysisException (org.metaborg.core.analysis.AnalysisException)2 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)2 IProject (org.metaborg.core.project.IProject)2 ISourceRegion (org.metaborg.core.source.ISourceRegion)2 SourceRegion (org.metaborg.core.source.SourceRegion)2 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)2 HybridInterpreter (org.strategoxt.HybridInterpreter)2 IOException (java.io.IOException)1