Search in sources :

Example 1 with PhasedUnits

use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits in project ceylon by eclipse.

the class ModuleSourceMapper method resolveModule.

public void resolveModule(ArtifactResult artifact, Module module, ModuleImport moduleImport, LinkedList<Module> dependencyTree, List<PhasedUnits> phasedUnitsOfDependencies, boolean forCompiledModule) {
    // This implementation relies on the ability to read the model from source
    // the compiler for example subclasses this to read lazily and from the compiled model
    ArtifactContext artifactContext = new ArtifactContext(null, module.getNameAsString(), module.getVersion(), ArtifactContext.SRC);
    RepositoryManager repositoryManager = context.getRepositoryManager();
    Exception exceptionOnGetArtifact = null;
    ArtifactResult sourceArtifact = null;
    try {
        sourceArtifact = repositoryManager.getArtifactResult(artifactContext);
    } catch (Exception e) {
        exceptionOnGetArtifact = e;
    }
    if (sourceArtifact == null) {
        ModuleHelper.buildErrorOnMissingArtifact(artifactContext, module, moduleImport, dependencyTree, exceptionOnGetArtifact, this, true);
    } else {
        PhasedUnits modulePhasedUnits = createPhasedUnits();
        ClosableVirtualFile virtualArtifact = null;
        try {
            virtualArtifact = context.getVfs().getFromZipFile(sourceArtifact.artifact());
            modulePhasedUnits.parseUnit(virtualArtifact);
            // populate module.getDependencies()
            modulePhasedUnits.visitModules();
            addToPhasedUnitsOfDependencies(modulePhasedUnits, phasedUnitsOfDependencies, module);
        } catch (Exception e) {
            StringBuilder error = new StringBuilder("unable to read source artifact for ");
            error.append(artifactContext.toString());
            error.append("\ndue to connection error: ").append(e.getMessage());
            attachErrorToDependencyDeclaration(moduleImport, dependencyTree, error.toString(), true);
        } finally {
            if (virtualArtifact != null) {
                virtualArtifact.close();
            }
        }
    }
}
Also used : ClosableVirtualFile(org.eclipse.ceylon.compiler.typechecker.io.ClosableVirtualFile) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 2 with PhasedUnits

use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits in project ceylon by eclipse.

the class NamingTests method getDecls.

protected List<Declaration> getDecls(String resource) throws Exception {
    final String name = PKGNAME.replace('.', '/') + "/" + resource;
    File file = new File("test/src", name);
    if (!file.exists()) {
        throw new RuntimeException("Unable to find resource " + name);
    }
    RepositoryManagerBuilder builder = new RepositoryManagerBuilder(new NullLogger(), false, 20000, java.net.Proxy.NO_PROXY);
    RepositoryManager repoManager = builder.buildRepository();
    VFS vfs = new VFS();
    Context context = new Context(repoManager, vfs);
    PhasedUnits pus = new PhasedUnits(context);
    // Make the module manager think we're looking at this package
    // even though there's no module descriptor
    pus.getModuleSourceMapper().push(PKGNAME);
    pus.parseUnit(vfs.getFromFile(file), vfs.getFromFile(new File("test-src")));
    final java.util.List<PhasedUnit> listOfUnits = pus.getPhasedUnits();
    PhasedUnit pu = listOfUnits.get(0);
    pu.validateTree();
    pu.scanDeclarations();
    pu.scanTypeDeclarations();
    pu.validateRefinement();
    pu.analyseTypes();
    pu.analyseFlow();
    return pu.getDeclarations();
}
Also used : Context(org.eclipse.ceylon.compiler.typechecker.context.Context) VFS(org.eclipse.ceylon.compiler.typechecker.io.VFS) RepositoryManagerBuilder(org.eclipse.ceylon.cmr.api.RepositoryManagerBuilder) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) File(java.io.File) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 3 with PhasedUnits

use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits in project ceylon by eclipse.

the class ModelLoaderTests method verifyCompilerClassLoading.

protected void verifyCompilerClassLoading(String ceylon, final ModelComparison modelCompare) {
    // now compile the ceylon decl file
    CeyloncTaskImpl task = getCompilerTask(ceylon);
    // get the context to grab the phased units
    Context context = task.getContext();
    if (simpleAnnotationModels) {
        CeylonEnter.instance(context);
        ExpressionTransformer.getInstance(context).simpleAnnotationModels = true;
        CeylonTransformer.getInstance(context).simpleAnnotationModels = true;
        StatementTransformer.getInstance(context).simpleAnnotationModels = true;
        ClassTransformer.getInstance(context).simpleAnnotationModels = true;
    }
    Boolean success = task.call();
    Assert.assertTrue(success);
    PhasedUnits phasedUnits = LanguageCompiler.getPhasedUnitsInstance(context);
    // find out what was in that file
    Assert.assertEquals(2, phasedUnits.getPhasedUnits().size());
    PhasedUnit one = phasedUnits.getPhasedUnits().get(0);
    PhasedUnit two = phasedUnits.getPhasedUnits().get(1);
    PhasedUnit phasedUnit = one.getUnitFile().getName().endsWith("module.ceylon") ? two : one;
    final Map<String, Declaration> decls = new HashMap<String, Declaration>();
    for (Declaration decl : phasedUnit.getUnit().getDeclarations()) {
        if (decl.isToplevel()) {
            decls.put(getQualifiedPrefixedName(decl), decl);
        }
    }
    // now compile the ceylon usage file
    // remove the extension, make lowercase and add "test"
    String testfile = ceylon.substring(0, ceylon.length() - 7).toLowerCase() + "test.ceylon";
    JavacTaskImpl task2 = getCompilerTask(testfile);
    // get the context to grab the declarations
    final Context context2 = task2.getContext();
    // declarations from the jar anymore because we've overridden the jar and the javac jar index is corrupted
    class Listener implements TaskListener {

        @Override
        public void started(TaskEvent e) {
        }

        @Override
        public void finished(TaskEvent e) {
            if (e.getKind() == Kind.ENTER) {
                AbstractModelLoader modelLoader = CeylonModelLoader.instance(context2);
                Modules modules = LanguageCompiler.getCeylonContextInstance(context2).getModules();
                // now see if we can find our declarations
                compareDeclarations(modelCompare, decls, modelLoader, modules);
            }
        }
    }
    Listener listener = new Listener();
    task2.setTaskListener(listener);
    success = task2.call();
    Assert.assertTrue("Compilation failed", success);
    // now check with the runtime model loader too
    String module = moduleForJavaModelLoading();
    String version = "1";
    ModuleWithArtifact moduleWithArtifact = new ModuleWithArtifact(module, version);
    synchronized (RUN_LOCK) {
        // this initialises the metamodel, even if we don't use the resulting ClassLoader
        URLClassLoader classLoader;
        try {
            classLoader = getClassLoader("runtime model loader tests", moduleWithArtifact);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        try {
            RuntimeModuleManager moduleManager = Metamodel.getModuleManager();
            RuntimeModelLoader modelLoader = moduleManager.getModelLoader();
            Modules modules = moduleManager.getModules();
            // now see if we can find our declarations
            compareDeclarations(modelCompare, decls, modelLoader, modules);
        } finally {
            try {
                classLoader.close();
            } catch (IOException e) {
                // ignore
                e.printStackTrace();
            }
        }
    }
}
Also used : Context(org.eclipse.ceylon.langtools.tools.javac.util.Context) AbstractModelLoader(org.eclipse.ceylon.model.loader.AbstractModelLoader) JavacTaskImpl(org.eclipse.ceylon.langtools.tools.javac.api.JavacTaskImpl) MalformedURLException(java.net.MalformedURLException) RuntimeModuleManager(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModuleManager) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) HashMap(java.util.HashMap) Modules(org.eclipse.ceylon.model.typechecker.model.Modules) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) IOException(java.io.IOException) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) URLClassLoader(java.net.URLClassLoader) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) RuntimeModelLoader(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModelLoader) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 4 with PhasedUnits

use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits in project ceylon by eclipse.

the class JsModuleSourceMapper method createPhasedUnits.

@Override
protected PhasedUnits createPhasedUnits() {
    PhasedUnits units = super.createPhasedUnits();
    String fileEncoding = encoding;
    if (fileEncoding == null) {
        fileEncoding = CeylonConfig.get(DefaultToolOptions.DEFAULTS_ENCODING);
    }
    if (fileEncoding != null) {
        units.setEncoding(fileEncoding);
    }
    return units;
}
Also used : PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits)

Example 5 with PhasedUnits

use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits in project ceylon by eclipse.

the class CeylonVersionTool method run.

@Override
public void run() throws IOException, RecognitionException {
    // TODO if version is empty? Prompt? Or should --set have an optional argument?
    TypeCheckerBuilder tcb = new TypeCheckerBuilder();
    for (File path : this.sourceFolders) {
        tcb.addSrcDirectory(applyCwd(path));
    }
    TypeChecker tc = tcb.getTypeChecker();
    PhasedUnits pus = tc.getPhasedUnits();
    pus.visitModules();
    ArrayList<Module> moduleList = new ArrayList<Module>(pus.getModuleSourceMapper().getCompiledModules());
    Collections.sort(moduleList, new Comparator<Module>() {

        @Override
        public int compare(Module m1, Module m2) {
            if (match(m1) && !match(m2)) {
                return -1;
            } else if (!match(m1) && match(m2)) {
                return +1;
            }
            int cmp = m1.getNameAsString().compareToIgnoreCase(m2.getNameAsString());
            if (cmp == 0) {
                cmp = m1.getVersion().compareTo(m2.getVersion());
            }
            return cmp;
        }
    });
    // first update all module versions and remember which version we assigned to which module
    // because the user can update every individual version
    Map<String, String> updatedModuleVersions = new HashMap<String, String>();
    for (Module module : moduleList) {
        boolean isMatch = match(module);
        if (newVersion == null) {
            output(module, isMatch);
        } else if (isMatch) {
            if (!updateModuleVersion(module, updatedModuleVersions)) {
                return;
            }
        }
    }
    if (updateDistribution) {
        updatedModuleVersions.put("ceylon.bootstrap", newVersion);
        updatedModuleVersions.put("ceylon.language", newVersion);
        updatedModuleVersions.put("ceylon.runtime", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.compiler.java", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.compiler.js", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.typechecker", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.common", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.cli", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.model", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-loader", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-aether", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-webdav", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-javascript", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.langtools.classfile", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.tool.provider", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.tools", newVersion);
    }
    // now do dependencies because we know which modules we did update
    if (newVersion != null && !noUpdateDependencies) {
        for (Module module : moduleList) {
            if (!updateModuleImports(module, updatedModuleVersions)) {
                return;
            }
        }
    }
}
Also used : TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) ImportModule(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ImportModule) Module(org.eclipse.ceylon.model.typechecker.model.Module) File(java.io.File)

Aggregations

PhasedUnits (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits)9 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)4 File (java.io.File)3 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)3 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)3 ModuleManager (org.eclipse.ceylon.model.typechecker.util.ModuleManager)3 IOException (java.io.IOException)2 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)2 ModuleSourceMapper (org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper)2 Context (org.eclipse.ceylon.compiler.typechecker.context.Context)2 ModuleManagerFactory (org.eclipse.ceylon.compiler.typechecker.util.ModuleManagerFactory)2 Context (org.eclipse.ceylon.langtools.tools.javac.util.Context)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 RepositoryManagerBuilder (org.eclipse.ceylon.cmr.api.RepositoryManagerBuilder)1