Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring for using the ModuleValidator with the compiler

Also for using possibly several artifact providers (for -repo option)
  • Loading branch information...
commit fd91d6f718b03d3f931b913bb7276968a4c0d4e3 1 parent c0e4ae1
@davidfestal davidfestal authored
View
2  src/com/redhat/ceylon/compiler/typechecker/TypeChecker.java
@@ -111,7 +111,7 @@ public void process() throws RuntimeException {
private void executePhases(PhasedUnits phasedUnits, boolean forceSilence) {
final List<PhasedUnit> listOfUnits = phasedUnits.getPhasedUnits();
- phasedUnits.getModuleManager().visitModules(listOfUnits);
+ phasedUnits.visitModules();
//By now le language module version should be known (as local)
//or we should use the default one.
View
42 src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleManager.java
@@ -14,6 +14,8 @@
import com.redhat.ceylon.compiler.typechecker.context.Context;
import com.redhat.ceylon.compiler.typechecker.context.PhasedUnit;
+import com.redhat.ceylon.compiler.typechecker.context.PhasedUnits;
+import com.redhat.ceylon.compiler.typechecker.io.VirtualFile;
import com.redhat.ceylon.compiler.typechecker.model.Module;
import com.redhat.ceylon.compiler.typechecker.model.ModuleImport;
import com.redhat.ceylon.compiler.typechecker.model.Modules;
@@ -250,15 +252,6 @@ public void addLinkBetweenModuleAndNode(Module module, Node unit) {
moduleToNode.put(module,unit);
}
- public void visitModules(List<PhasedUnit> listOfUnits) {
- for (PhasedUnit pu : listOfUnits) {
- pu.visitSrcModulePhase();
- }
- for (PhasedUnit pu : listOfUnits) {
- pu.visitRemainingModulePhase();
- }
- }
-
public ModuleImport findImport(Module owner, Module dependency) {
for (ModuleImport modImprt : owner.getImports()) {
if (equalsForModules(modImprt.getModule(), dependency, true)) return modImprt;
@@ -284,4 +277,35 @@ public Module findModule(Module module, List<Module> listOfModules, boolean exac
}
return null;
}
+
+ public Module findLoadedModule(String moduleName, String searchedVersion) {
+ return findLoadedModule(moduleName, searchedVersion, modules);
+ }
+
+ public Module findLoadedModule(String moduleName, String searchedVersion, Modules modules) {
+ for(Module module : modules.getListOfModules()){
+ if(module.getNameAsString().equals(moduleName)) {
+ if (searchedVersion != null && searchedVersion.equals(module.getVersion())){
+ return module;
+ }
+ }
+ }
+ return null;
+ }
+
+ public void resolveModule(Module module, VirtualFile artifact, List<PhasedUnits> phasedUnitsOfDependencies) {
+ PhasedUnits modulePhasedUnit = new PhasedUnits(context);
+ phasedUnitsOfDependencies.add(modulePhasedUnit);
+ modulePhasedUnit.parseUnit(artifact);
+ //populate module.getDependencies()
+ modulePhasedUnit.visitModules();
+ }
+
+ public Iterable<String> getSearchedArtifactExtensions() {
+ return Arrays.asList("src");
+ }
+
+ public static List<String> splitModuleName(String moduleName) {
+ return Arrays.asList(moduleName.split("[\\.]"));
+ }
}
View
5 src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleResolver.java
@@ -0,0 +1,5 @@
+package com.redhat.ceylon.compiler.typechecker.analyzer;
+
+public interface ModuleResolver {
+
+}
View
65 src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleValidator.java
@@ -2,6 +2,7 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@@ -12,6 +13,7 @@
import com.redhat.ceylon.compiler.typechecker.exceptions.LanguageModuleNotFoundException;
import com.redhat.ceylon.compiler.typechecker.io.ArtifactProvider;
import com.redhat.ceylon.compiler.typechecker.io.ClosableVirtualFile;
+import com.redhat.ceylon.compiler.typechecker.io.VirtualFile;
import com.redhat.ceylon.compiler.typechecker.model.Module;
import com.redhat.ceylon.compiler.typechecker.model.ModuleImport;
@@ -28,6 +30,7 @@
private List<PhasedUnits> phasedUnitsOfDependencies;
private final ModuleManager moduleManager;
+
public ModuleValidator(Context context, PhasedUnits phasedUnits) {
this.context = context;
this.moduleManager = phasedUnits.getModuleManager();
@@ -79,16 +82,46 @@ private void verifyModuleDependencyTree(
moduleManager.addErrorToModule( dependencyTree.getFirst(), error.toString() );
return;
}
+ List<String> searchedArtifacts = new ArrayList<String>();
+ Iterable<String> searchedArtifactExtensions = moduleManager.getSearchedArtifactExtensions();
+
if ( ! module.isAvailable() ) {
//try and load the module from the repository
- final ArtifactProvider artifactProvider = context.getArtifactProvider();
- final ClosableVirtualFile src = artifactProvider.getArtifact(module.getName(), module.getVersion(), "src");
- if (src == null) {
+ VirtualFile artifact = null;
+ List<ArtifactProvider> artifactProviders = context.getArtifactProviders();
+ for (final ArtifactProvider artifactProvider : artifactProviders) {
+ for (String extension : searchedArtifactExtensions) {
+ searchedArtifacts.add(artifactProvider.getArtifactName(module.getName(),
+ module.getVersion(), extension));
+ }
+ artifact = artifactProvider.getArtifact(module.getName(),
+ module.getVersion(),
+ searchedArtifactExtensions);
+ if (artifact != null) {
+ break;
+ }
+ }
+ if (artifact == null) {
//not there => error
- StringBuilder error = new StringBuilder("Cannot find module artifact ");
- error.append( artifactProvider.getArtifactName(module.getName(), module.getVersion(), "src") )
- .append(" in local repository ('~/.ceylon/repo')")
- .append("\n\tDependency tree: ");
+ StringBuilder error = new StringBuilder("Cannot find module artifact(s) : ");
+ if (searchedArtifacts.size() > 0) {
+ error.append(searchedArtifacts.get(0));
+ }
+ for (String searchedArtifact : searchedArtifacts.subList(1, searchedArtifacts.size())) {
+ error.append(", ");
+ error.append("\n\t");
+ error.append(searchedArtifact);
+ }
+ error.append("\n\t in repositories : ");
+ if (artifactProviders.size() > 0) {
+ error.append(artifactProviders.get(0));
+ }
+ for (ArtifactProvider searchedProvider : artifactProviders.subList(1, artifactProviders.size())) {
+ error.append(", ");
+ error.append("\n\t");
+ error.append(searchedProvider);
+ }
+ error.append("\n\tDependency tree: ");
buildDependencyString(dependencyTree, module, error);
error.append(".");
if ( module.getLanguageModule() == module ) {
@@ -102,16 +135,14 @@ private void verifyModuleDependencyTree(
}
}
else {
- //parse module units and build module dependency and carry on
- PhasedUnits modulePhasedUnit = new PhasedUnits(context);
- phasedUnitsOfDependencies.add(modulePhasedUnit);
- modulePhasedUnit.parseUnit(src);
- src.close();
- module.setAvailable(true); // TODO : not necessary anymore ? since at least on module.ceylon
- // should have been parsed and should be applied buildModuleImport()
- final List<PhasedUnit> listOfUnits = modulePhasedUnit.getPhasedUnits();
- //populate module.getDependencies()
- moduleManager.visitModules(listOfUnits);
+ try {
+ //parse module units and build module dependency and carry on
+ moduleManager.resolveModule(module, artifact, phasedUnitsOfDependencies);
+ } finally {
+ if (artifact instanceof ClosableVirtualFile) {
+ ((ClosableVirtualFile)artifact).close();
+ }
+ }
}
}
dependencyTree.addLast(module);
View
14 src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleVisitor.java
@@ -1,7 +1,6 @@
package com.redhat.ceylon.compiler.typechecker.analyzer;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import com.redhat.ceylon.compiler.typechecker.model.Module;
@@ -130,7 +129,7 @@ private void visitForRemainingPhase(Tree.InvocationExpression that, Identifier i
if (version.isEmpty()) {
vsa.addError("empty version identifier");
}
- Module importedModule = moduleManager.getOrCreateModule(splitModuleName(moduleName),version);
+ Module importedModule = moduleManager.getOrCreateModule(ModuleManager.splitModuleName(moduleName),version);
if (importedModule == null) {
nsa.addError("A module cannot be defined at the top level of the hierarchy");
}
@@ -138,11 +137,11 @@ else if (mainModule != null) {
if (importedModule.getVersion() == null) {
importedModule.setVersion(version);
}
+ String optionalString = argumentToString(getArgument(that, "optional"));
+ String exportString = argumentToString(getArgument(that, "export"));
ModuleImport moduleImport = moduleManager.findImport(mainModule, importedModule);
if (moduleImport == null) {
- String optionalString = argumentToString(getArgument(that, "optional"));
boolean optional = optionalString!=null && optionalString.equals("true");
- String exportString = argumentToString(getArgument(that, "export"));
boolean export = exportString!=null && exportString.equals("true");
moduleImport = new ModuleImport(importedModule, optional, export);
mainModule.getImports().add(moduleImport);
@@ -175,10 +174,6 @@ else if (mainModule != null) {
}
}
- private static List<String> splitModuleName(String moduleName) {
- return Arrays.asList(moduleName.split("[\\.]"));
- }
-
private Tree.SpecifiedArgument getArgument(Tree.InvocationExpression that, String name) {
for (Tree.NamedArgument arg: that.getNamedArgumentList().getNamedArguments()) {
if (arg instanceof Tree.SpecifiedArgument) {
@@ -250,4 +245,7 @@ else if ( term instanceof Tree.BaseMemberExpression) {
SRC_MODULE,
REMAINING
}
+ public Module getMainModule() {
+ return mainModule;
+ }
}
View
21 src/com/redhat/ceylon/compiler/typechecker/context/Context.java
@@ -1,7 +1,12 @@
package com.redhat.ceylon.compiler.typechecker.context;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
import com.redhat.ceylon.compiler.typechecker.io.ArtifactProvider;
import com.redhat.ceylon.compiler.typechecker.io.VFS;
+import com.redhat.ceylon.compiler.typechecker.io.VirtualFile;
import com.redhat.ceylon.compiler.typechecker.model.Modules;
/**
@@ -11,13 +16,21 @@
*/
public class Context {
- private ArtifactProvider artifactProvider;
+ private List<ArtifactProvider> artifactProviders;
private Modules modules;
private VFS vfs;
public Context(VFS vfs) {
this.vfs = vfs;
- this.artifactProvider = new ArtifactProvider(vfs);
+ this.artifactProviders = Arrays.asList(new ArtifactProvider(vfs));
+ }
+
+ public Context(Iterable<VirtualFile> repos, VFS vfs) {
+ this.vfs = vfs;
+ this.artifactProviders = new ArrayList<ArtifactProvider>();
+ for (VirtualFile repo : repos) {
+ artifactProviders.add(new ArtifactProvider(repo, vfs));
+ }
}
public Modules getModules() {
@@ -28,8 +41,8 @@ public void setModules(Modules modules) {
this.modules = modules;
}
- public ArtifactProvider getArtifactProvider() {
- return artifactProvider;
+ public List<ArtifactProvider> getArtifactProviders() {
+ return artifactProviders;
}
public VFS getVfs() {
View
5 src/com/redhat/ceylon/compiler/typechecker/context/PhasedUnit.java
@@ -21,6 +21,7 @@
import com.redhat.ceylon.compiler.typechecker.analyzer.ValueVisitor;
import com.redhat.ceylon.compiler.typechecker.io.VirtualFile;
import com.redhat.ceylon.compiler.typechecker.model.Declaration;
+import com.redhat.ceylon.compiler.typechecker.model.Module;
import com.redhat.ceylon.compiler.typechecker.model.Package;
import com.redhat.ceylon.compiler.typechecker.model.Setter;
import com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration;
@@ -81,12 +82,14 @@ else if ( rawRelativePath.startsWith("!/") ) {
}
}
- public void visitSrcModulePhase() {
+ public Module visitSrcModulePhase() {
if ( ModuleManager.MODULE_FILE.equals(fileName) ||
ModuleManager.PACKAGE_FILE.equals(fileName) ) {
moduleVisitor = new ModuleVisitor(moduleManager, pkg);
compilationUnit.visit(moduleVisitor);
+ return moduleVisitor.getMainModule();
}
+ return null;
}
public void visitRemainingModulePhase() {
View
10 src/com/redhat/ceylon/compiler/typechecker/context/PhasedUnits.java
@@ -159,4 +159,14 @@ private void processDirectory(VirtualFile dir, VirtualFile srcDir) throws Except
}
moduleManager.pop();
}
+
+ public void visitModules() {
+ List<PhasedUnit> listOfUnits = getPhasedUnits();
+ for (PhasedUnit pu : listOfUnits) {
+ pu.visitSrcModulePhase();
+ }
+ for (PhasedUnit pu : listOfUnits) {
+ pu.visitRemainingModulePhase();
+ }
+ }
}
View
23 src/com/redhat/ceylon/compiler/typechecker/io/ArtifactProvider.java
@@ -19,18 +19,28 @@ public ArtifactProvider(VFS vfs) {
this.vfs = vfs;
}
+ public ArtifactProvider(VirtualFile homeRepo, VFS vfs) {
+ this.homeRepo = homeRepo;
+ this.vfs = vfs;
+ }
+
/**
* Return the artifact or null if not present
* Must be closed when done with it
*/
- public ClosableVirtualFile getArtifact(List<String> moduleName, String version, String extension) {
+ public ClosableVirtualFile getArtifact(List<String> moduleName, String version, Iterable<String> extensions) {
VirtualFile moduleDirectory = getModuleDirectory(moduleName, version);
if (moduleDirectory == null) {
return null;
}
- final VirtualFile child = getChild(moduleDirectory, getArtifactName(moduleName, version, extension) );
- //build sha1 and compare it
- return child == null ? null : vfs.openAsContainer(child);
+ for (String extension : extensions) {
+ final VirtualFile child = getChild(moduleDirectory, getArtifactName(moduleName, version, extension) );
+ if (child != null) {
+ //build sha1 and compare it
+ return vfs.openAsContainer(child);
+ }
+ }
+ return null;
}
/**
@@ -70,4 +80,9 @@ private VirtualFile getChild(VirtualFile parent, String name) {
}
return null;
}
+
+ @Override
+ public String toString() {
+ return homeRepo.getPath();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.