diff options
-rw-r--r-- | src/dotty/tools/dotc/core/SymDenotations.scala | 20 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/SymbolLoaders.scala | 300 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/Symbols.scala | 36 | ||||
-rw-r--r-- | src/dotty/tools/io/ClassPath.scala | 417 | ||||
-rw-r--r-- | src/dotty/tools/io/DaemonThreadFactory.scala | 16 | ||||
-rw-r--r-- | src/dotty/tools/io/Fileish.scala | 33 | ||||
-rw-r--r-- | src/dotty/tools/io/Jar.scala | 174 | ||||
-rw-r--r-- | src/dotty/tools/io/package.scala | 58 | ||||
-rw-r--r-- | src/dotty/tools/package.scala | 11 |
9 files changed, 1050 insertions, 15 deletions
diff --git a/src/dotty/tools/dotc/core/SymDenotations.scala b/src/dotty/tools/dotc/core/SymDenotations.scala index 06c0d9593..ea3ab36c5 100644 --- a/src/dotty/tools/dotc/core/SymDenotations.scala +++ b/src/dotty/tools/dotc/core/SymDenotations.scala @@ -328,6 +328,12 @@ object SymDenotations { type SymCompleter = Completer[LazySymDenotation] type ClassCompleter = Completer[LazyClassDenotation] + class ModuleCompleter(moduleClass: ClassSymbol) extends Completer[LazySymDenotation] { + def handleCycle(denot: LazySymDenotation): Unit = ??? + def load(denot: LazySymDenotation): Unit = ??? + def complete(denot: LazySymDenotation): Unit = ??? + } + trait isLazy[Denot <: SymDenotation] extends SymDenotation { this: Denot => protected def completer: Completer[Denot] @@ -360,7 +366,19 @@ object SymDenotations { val info: Type ) extends SymDenotation(initFlags) with isComplete - abstract class LazySymDenotation( + class LazyModuleDenotation( + symbol: Symbol, + owner: Symbol, + name: Name, + initFlags: FlagSet, + val moduleClass: ClassSymbol + ) extends LazySymDenotation(symbol, owner, name, initFlags, new ModuleCompleter(moduleClass)) { + + override val info: Type = ??? + } + + + class LazySymDenotation( val symbol: Symbol, val owner: Symbol, val name: Name, diff --git a/src/dotty/tools/dotc/core/SymbolLoaders.scala b/src/dotty/tools/dotc/core/SymbolLoaders.scala new file mode 100644 index 000000000..f947a23d8 --- /dev/null +++ b/src/dotty/tools/dotc/core/SymbolLoaders.scala @@ -0,0 +1,300 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2012 LAMP/EPFL + * @author Martin Odersky + */ + +package dotty.tools +package dotc +package core + +import java.io.IOException +import scala.compat.Platform.currentTime +import dotty.tools.io.{ClassPath, AbstractFile} +import Contexts._, Symbols._, Flags._, SymDenotations._ +import Decorators.StringDecorator +//import classfile.ClassfileParser + + +abstract class SymbolLoaders(implicit ctx: Context) { + + protected def enterIfNew(owner: Symbol, member: Symbol, completer: SymbolLoader): Symbol = { + assert(owner.info.decls.lookup(member.name) == NoSymbol, owner.fullName + "." + member.name) + owner.info.decls enter member + member + } + + /** Enter class with given `name` into scope of `root` + * and give them `completer` as type. + */ + def enterClass(owner: Symbol, name: String, completer: SymbolLoader): Symbol = { + val cls = ctx.newLazyClassSymbol(owner, name.toTypeName, EmptyFlags, completer) + enterIfNew(owner, cls, completer) + } + + abstract class SymbolLoader extends ClassCompleter +} +/* + /** Enter module with given `name` into scope of `root` + * and give them `completer` as type. + */ + def enterModule(owner: Symbol, name: String, completer: SymbolLoader): Symbol = { + val module = owner.newModule(newTermName(name)) + module setInfo completer + module.moduleClass setInfo moduleClassLoader + enterIfNew(owner, module, completer) + } + + /** Enter package with given `name` into scope of `root` + * and give them `completer` as type. + */ + def enterPackage(root: Symbol, name: String, completer: SymbolLoader): Symbol = { + val pname = newTermName(name) + val preExisting = root.info.decls lookup pname + if (preExisting != NoSymbol) { + // Some jars (often, obfuscated ones) include a package and + // object with the same name. Rather than render them unusable, + // offer a setting to resolve the conflict one way or the other. + // This was motivated by the desire to use YourKit probes, which + // require yjp.jar at runtime. See SI-2089. + if (settings.termConflict.isDefault) + throw new TypeError( + root+" contains object and package with same name: "+ + name+"\none of them needs to be removed from classpath" + ) + else if (settings.termConflict.value == "package") { + global.warning( + "Resolving package/object name conflict in favor of package " + + preExisting.fullName + ". The object will be inaccessible." + ) + root.info.decls.unlink(preExisting) + } + else { + global.warning( + "Resolving package/object name conflict in favor of object " + + preExisting.fullName + ". The package will be inaccessible." + ) + return NoSymbol + } + } + // todo: find out initialization sequence for pkg/pkg.moduleClass is different from enterModule + val pkg = root.newPackage(pname) + pkg.moduleClass setInfo completer + pkg setInfo pkg.moduleClass.tpe + root.info.decls enter pkg + pkg + } + + /** Enter class and module with given `name` into scope of `root` + * and give them `completer` as type. + */ + def enterClassAndModule(root: Symbol, name: String, completer: SymbolLoader) { + val clazz = enterClass(root, name, completer) + val module = enterModule(root, name, completer) + if (!clazz.isAnonymousClass) { + assert(clazz.companionModule == module, module) + assert(module.companionClass == clazz, clazz) + } + } + + /** In batch mode: Enter class and module with given `name` into scope of `root` + * and give them a source completer for given `src` as type. + * In IDE mode: Find all toplevel definitions in `src` and enter then into scope of `root` + * with source completer for given `src` as type. + * (overridden in interactive.Global). + */ + def enterToplevelsFromSource(root: Symbol, name: String, src: AbstractFile) { + enterClassAndModule(root, name, new SourcefileLoader(src)) + } + + /** The package objects of scala and scala.reflect should always + * be loaded in binary if classfiles are available, even if sourcefiles + * are newer. Late-compiling these objects from source leads to compilation + * order issues. + * Note: We do a name-base comparison here because the method is called before we even + * have ReflectPackage defined. + */ + def binaryOnly(owner: Symbol, name: String): Boolean = + name == "package" && + (owner.fullName == "scala" || owner.fullName == "scala.reflect") + + /** Initialize toplevel class and module symbols in `owner` from class path representation `classRep` + */ + def initializeFromClassPath(owner: Symbol, classRep: ClassPath[platform.BinaryRepr]#ClassRep) { + ((classRep.binary, classRep.source) : @unchecked) match { + case (Some(bin), Some(src)) + if platform.needCompile(bin, src) && !binaryOnly(owner, classRep.name) => + if (settings.verbose.value) inform("[symloader] picked up newer source file for " + src.path) + global.loaders.enterToplevelsFromSource(owner, classRep.name, src) + case (None, Some(src)) => + if (settings.verbose.value) inform("[symloader] no class, picked up source file for " + src.path) + global.loaders.enterToplevelsFromSource(owner, classRep.name, src) + case (Some(bin), _) => + global.loaders.enterClassAndModule(owner, classRep.name, platform.newClassLoader(bin)) + } + } + + /** + * A lazy type that completes itself by calling parameter doComplete. + * Any linked modules/classes or module classes are also initialized. + * Todo: consider factoring out behavior from TopClassCompleter/SymbolLoader into + * supertrait SymLoader + */ + abstract class SymbolLoader extends ClassCompleter { + + /** Load source or class file for `root`, return */ + protected def doComplete(root: Symbol): Unit + + def sourcefile: Option[AbstractFile] = None + + /** + * Description of the resource (ClassPath, AbstractFile, MsilFile) + * being processed by this loader + */ + protected def description: String + + private var ok = false + + private def setSource(sym: Symbol) { + sourcefile foreach (sf => sym match { + case cls: ClassSymbol => cls.sourceFile = sf + case mod: ModuleSymbol => mod.moduleClass.sourceFile = sf + case _ => () + }) + } + + override def complete(root: Symbol) { + def signalError(ex: Exception) { + ok = false + if (settings.debug.value) ex.printStackTrace() + val msg = ex.getMessage() + // SI-5593 Scaladoc's current strategy is to visit all packages in search of user code that can be documented + // therefore, it will rummage through the classpath triggering errors whenever it encounters package objects + // that are not in their correct place (see bug for details) + if (!settings.isScaladoc) + globalError( + if (msg eq null) "i/o error while loading " + root.name + else "error while loading " + root.name + ", " + msg); + } + try { + val start = currentTime + val currentphase = phase + doComplete(root) + phase = currentphase + informTime("loaded " + description, start) + ok = true + setSource(root) + setSource(root.companionSymbol) // module -> class, class -> module + } catch { + case ex: IOException => + signalError(ex) + case ex: MissingRequirementError => + signalError(ex) + } + initRoot(root) + if (!root.isPackageClass) initRoot(root.companionSymbol) + } + + override def load(root: Symbol) { complete(root) } + + private def markAbsent(sym: Symbol): Unit = { + val tpe: Type = if (ok) NoType else ErrorType + + if (sym != NoSymbol) + sym setInfo tpe + } + private def initRoot(root: Symbol) { + if (root.rawInfo == this) + List(root, root.moduleClass) foreach markAbsent + else if (root.isClass && !root.isModuleClass) + root.rawInfo.load(root) + } + } + + /** + * Load contents of a package + */ + class PackageLoader(classpath: ClassPath[platform.BinaryRepr]) extends SymbolLoader with FlagAgnosticCompleter { + protected def description = "package loader "+ classpath.name + + protected def doComplete(root: Symbol) { + assert(root.isPackageClass, root) + root.setInfo(new PackageClassInfoType(newScope, root)) + + val sourcepaths = classpath.sourcepaths + if (!root.isRoot) { + for (classRep <- classpath.classes if platform.doLoad(classRep)) { + initializeFromClassPath(root, classRep) + } + } + if (!root.isEmptyPackageClass) { + for (pkg <- classpath.packages) { + enterPackage(root, pkg.name, new PackageLoader(pkg)) + } + + openPackageModule(root) + } + } + } + + class ClassfileLoader(val classfile: AbstractFile) extends SymbolLoader with FlagAssigningCompleter { + private object classfileParser extends ClassfileParser { + val global: SymbolLoaders.this.global.type = SymbolLoaders.this.global + } + + protected def description = "class file "+ classfile.toString + + protected def doComplete(root: Symbol) { + val start = if (Statistics.canEnable) Statistics.startTimer(classReadNanos) else null + classfileParser.parse(classfile, root) + if (root.associatedFile eq null) { + root match { + // In fact, the ModuleSymbol forwards its setter to the module class + case _: ClassSymbol | _: ModuleSymbol => + debuglog("ClassfileLoader setting %s.associatedFile = %s".format(root.name, classfile)) + root.associatedFile = classfile + case _ => + debuglog("Not setting associatedFile to %s because %s is a %s".format(classfile, root.name, root.shortSymbolClass)) + } + } + if (Statistics.canEnable) Statistics.stopTimer(classReadNanos, start) + } + override def sourcefile: Option[AbstractFile] = classfileParser.srcfile + } + + class MsilFileLoader(msilFile: MsilFile) extends SymbolLoader with FlagAssigningCompleter { + private def typ = msilFile.msilType + private object typeParser extends clr.TypeParser { + val global: SymbolLoaders.this.global.type = SymbolLoaders.this.global + } + + protected def description = "MsilFile "+ typ.FullName + ", assembly "+ typ.Assembly.FullName + protected def doComplete(root: Symbol) { typeParser.parse(typ, root) } + } + + class SourcefileLoader(val srcfile: AbstractFile) extends SymbolLoader with FlagAssigningCompleter { + protected def description = "source file "+ srcfile.toString + override def fromSource = true + override def sourcefile = Some(srcfile) + protected def doComplete(root: Symbol): Unit = global.currentRun.compileLate(srcfile) + } + + object moduleClassLoader extends SymbolLoader with FlagAssigningCompleter { + protected def description = "module class loader" + protected def doComplete(root: Symbol) { root.sourceModule.initialize } + } + + object clrTypes extends clr.CLRTypes { + val global: SymbolLoaders.this.global.type = SymbolLoaders.this.global + if (global.forMSIL) init() + } + + /** used from classfile parser to avoid cyclies */ + var parentsLevel = 0 + var pendingLoadActions: List[() => Unit] = Nil +} + +object SymbolLoadersStats { + import scala.reflect.internal.TypesStats.typerNanos + val classReadNanos = Statistics.newSubTimer ("time classfilereading", typerNanos) +} +*/
\ No newline at end of file diff --git a/src/dotty/tools/dotc/core/Symbols.scala b/src/dotty/tools/dotc/core/Symbols.scala index 58f7ed4f8..468e36cfd 100644 --- a/src/dotty/tools/dotc/core/Symbols.scala +++ b/src/dotty/tools/dotc/core/Symbols.scala @@ -1,4 +1,5 @@ -package dotty.tools.dotc +package dotty.tools +package dotc package core import Periods._ @@ -13,23 +14,25 @@ import SymDenotations._ import Types._, Annotations._ import Denotations.{ Denotation, SingleDenotation, MultiDenotation } import collection.mutable -import reflect.io.AbstractFile +import io.AbstractFile trait Symbols { this: Context => + def newLazyTermSymbol(owner: Symbol, name: TermName, initFlags: FlagSet, completer: SymCompleter) = + new TermSymbol(new LazySymDenotation(_, owner, name, initFlags, completer)) + + def newLazyTypeSymbol(owner: Symbol, name: TypeName, initFlags: FlagSet, completer: SymCompleter) = + new TypeSymbol(new LazySymDenotation(_, owner, name, initFlags, completer)) + def newLazyClassSymbol(owner: Symbol, name: TypeName, initFlags: FlagSet, completer: ClassCompleter, assocfile: AbstractFile = null) = new ClassSymbol(new LazyClassDenotation(_, owner, name, initFlags, completer, assocfile)) - def newCompleteClassSymbol( + def newCompleteTermSymbol( owner: Symbol, - name: TypeName, + name: TermName, flags: FlagSet, - parents: List[TypeRef], - optSelfType: Type = NoType, - decls: Scope = newScope, - assocFile: AbstractFile = null) = - new ClassSymbol(new CompleteClassDenotation( - _, owner, name, flags, parents, optSelfType, decls, assocFile)) + info: Type) = + new TermSymbol(new CompleteSymDenotation(_, owner, name, flags, info)) def newCompleteTypeSymbol( owner: Symbol, @@ -41,12 +44,16 @@ trait Symbols { this: Context => def newAliasTypeSymbol(owner: Symbol, name: TypeName, alias: Type, flags: FlagSet = EmptyFlags) = newCompleteTypeSymbol(owner, name, flags, TypeBounds(alias, alias)) - def newCompleteTermSymbol( + def newCompleteClassSymbol( owner: Symbol, - name: TermName, + name: TypeName, flags: FlagSet, - info: Type) = - new TermSymbol(new CompleteSymDenotation(_, owner, name, flags, info)) + parents: List[TypeRef], + optSelfType: Type = NoType, + decls: Scope = newScope, + assocFile: AbstractFile = null) = + new ClassSymbol(new CompleteClassDenotation( + _, owner, name, flags, parents, optSelfType, decls, assocFile)) } object Symbols { @@ -164,6 +171,7 @@ object Symbols { * Drops package objects. */ final def fullName(separator: Char)(implicit ctx: Context): Name = denot.fullName(separator) + final def fullName(implicit ctx: Context): Name = fullName('.') /** The source or class file from which this symbol was generated, null if not applicable. */ final def associatedFile(implicit ctx: Context): AbstractFile = denot.associatedFile diff --git a/src/dotty/tools/io/ClassPath.scala b/src/dotty/tools/io/ClassPath.scala new file mode 100644 index 000000000..5a2fdad41 --- /dev/null +++ b/src/dotty/tools/io/ClassPath.scala @@ -0,0 +1,417 @@ +/* NSC -- new Scala compiler + * Copyright 2006-2012 LAMP/EPFL + * @author Martin Odersky + */ + +package dotty.tools +package io + +import java.net.URL +import scala.collection.{ mutable, immutable } +import scala.reflect.internal.util.StringOps.splitWhere +import File.pathSeparator +import java.net.MalformedURLException +import Jar.isJarOrZip +import ClassPath._ +import scala.Option.option2Iterable +import scala.reflect.io.Path.string2path + +/** <p> + * This module provides star expansion of '-classpath' option arguments, behaves the same as + * java, see [http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html] + * </p> + * + * @author Stepan Koltsov + */ +object ClassPath { + /** Expand single path entry */ + private def expandS(pattern: String): List[String] = { + val wildSuffix = File.separator + "*" + + /** Get all subdirectories, jars, zips out of a directory. */ + def lsDir(dir: Directory, filt: String => Boolean = _ => true) = + dir.list filter (x => filt(x.name) && (x.isDirectory || isJarOrZip(x))) map (_.path) toList + + def basedir(s: String) = + if (s contains File.separator) s.substring(0, s.lastIndexOf(File.separator)) + else "." + + if (pattern == "*") lsDir(Directory(".")) + else if (pattern endsWith wildSuffix) lsDir(Directory(pattern dropRight 2)) + else if (pattern contains '*') { + val regexp = ("^%s$" format pattern.replaceAll("""\*""", """.*""")).r + lsDir(Directory(pattern).parent, regexp findFirstIn _ isDefined) + } + else List(pattern) + } + + /** Split classpath using platform-dependent path separator */ + def split(path: String): List[String] = (path split pathSeparator).toList filterNot (_ == "") distinct + + /** Join classpath using platform-dependent path separator */ + def join(paths: String*): String = paths filterNot (_ == "") mkString pathSeparator + + /** Split the classpath, apply a transformation function, and reassemble it. */ + def map(cp: String, f: String => String): String = join(split(cp) map f: _*) + + /** Split the classpath, filter according to predicate, and reassemble. */ + def filter(cp: String, p: String => Boolean): String = join(split(cp) filter p: _*) + + /** Split the classpath and map them into Paths */ + def toPaths(cp: String): List[Path] = split(cp) map (x => Path(x).toAbsolute) + + /** Make all classpath components absolute. */ + def makeAbsolute(cp: String): String = fromPaths(toPaths(cp): _*) + + /** Join the paths as a classpath */ + def fromPaths(paths: Path*): String = join(paths map (_.path): _*) + def fromURLs(urls: URL*): String = fromPaths(urls map (x => Path(x.getPath)) : _*) + + /** Split the classpath and map them into URLs */ + def toURLs(cp: String): List[URL] = toPaths(cp) map (_.toURL) + + /** Expand path and possibly expanding stars */ + def expandPath(path: String, expandStar: Boolean = true): List[String] = + if (expandStar) split(path) flatMap expandS + else split(path) + + /** Expand dir out to contents, a la extdir */ + def expandDir(extdir: String): List[String] = { + AbstractFile getDirectory extdir match { + case null => Nil + case dir => dir filter (_.isClassContainer) map (x => new java.io.File(dir.file, x.name) getPath) toList + } + } + /** Expand manifest jar classpath entries: these are either urls, or paths + * relative to the location of the jar. + */ + def expandManifestPath(jarPath: String): List[URL] = { + val file = File(jarPath) + if (!file.isFile) return Nil + + val baseDir = file.parent + new Jar(file).classPathElements map (elem => + specToURL(elem) getOrElse (baseDir / elem).toURL + ) + } + + /** A useful name filter. */ + def isTraitImplementation(name: String) = name endsWith "$class.class" + + def specToURL(spec: String): Option[URL] = + try Some(new URL(spec)) + catch { case _: MalformedURLException => None } + + /** A class modeling aspects of a ClassPath which should be + * propagated to any classpaths it creates. + */ + abstract class ClassPathContext[T] { + /** A filter which can be used to exclude entities from the classpath + * based on their name. + */ + def isValidName(name: String): Boolean = true + + /** From the representation to its identifier. + */ + def toBinaryName(rep: T): String + + /** Create a new classpath based on the abstract file. + */ + def newClassPath(file: AbstractFile): ClassPath[T] + + /** Creators for sub classpaths which preserve this context. + */ + def sourcesInPath(path: String): List[ClassPath[T]] = + for (file <- expandPath(path, false) ; dir <- Option(AbstractFile getDirectory file)) yield + new SourcePath[T](dir, this) + + def contentsOfDirsInPath(path: String): List[ClassPath[T]] = + for (dir <- expandPath(path, false) ; name <- expandDir(dir) ; entry <- Option(AbstractFile getDirectory name)) yield + newClassPath(entry) + + def classesAtAllURLS(path: String): List[ClassPath[T]] = + (path split " ").toList flatMap classesAtURL + + def classesAtURL(spec: String) = + for (url <- specToURL(spec).toList ; location <- Option(AbstractFile getURL url)) yield + newClassPath(location) + + def classesInExpandedPath(path: String): IndexedSeq[ClassPath[T]] = + classesInPathImpl(path, true).toIndexedSeq + + def classesInPath(path: String) = classesInPathImpl(path, false) + + // Internal + private def classesInPathImpl(path: String, expand: Boolean) = + for (file <- expandPath(path, expand) ; dir <- Option(AbstractFile getDirectory file)) yield + newClassPath(dir) + } + + class JavaContext extends ClassPathContext[AbstractFile] { + def toBinaryName(rep: AbstractFile) = { + val name = rep.name + assert(endsClass(name), name) + name.substring(0, name.length - 6) + } + def newClassPath(dir: AbstractFile) = new DirectoryClassPath(dir, this) + } + + object DefaultJavaContext extends JavaContext { + override def isValidName(name: String) = !isTraitImplementation(name) + } + + private def endsClass(s: String) = s.length > 6 && s.substring(s.length - 6) == ".class" + private def endsScala(s: String) = s.length > 6 && s.substring(s.length - 6) == ".scala" + private def endsJava(s: String) = s.length > 5 && s.substring(s.length - 5) == ".java" + + /** From the source file to its identifier. + */ + def toSourceName(f: AbstractFile): String = { + val name = f.name + + if (endsScala(name)) name.substring(0, name.length - 6) + else if (endsJava(name)) name.substring(0, name.length - 5) + else throw new FatalError("Unexpected source file ending: " + name) + } +} + +/** + * Represents a package which contains classes and other packages + */ +abstract class ClassPath[T] { + type AnyClassRep = ClassPath[T]#ClassRep + + /** + * The short name of the package (without prefix) + */ + def name: String + + /** + * A String representing the origin of this classpath element, if known. + * For example, the path of the directory or jar. + */ + def origin: Option[String] = None + + /** A list of URLs representing this classpath. + */ + def asURLs: List[URL] + + /** The whole classpath in the form of one String. + */ + def asClasspathString: String + + /** Info which should be propagated to any sub-classpaths. + */ + def context: ClassPathContext[T] + + /** Lists of entities. + */ + def classes: IndexedSeq[AnyClassRep] + def packages: IndexedSeq[ClassPath[T]] + def sourcepaths: IndexedSeq[AbstractFile] + + /** + * Represents classes which can be loaded with a ClassfileLoader/MsilFileLoader + * and / or a SourcefileLoader. + */ + case class ClassRep(binary: Option[T], source: Option[AbstractFile]) { + def name: String = binary match { + case Some(x) => context.toBinaryName(x) + case _ => + assert(source.isDefined) + toSourceName(source.get) + } + } + + /** Filters for assessing validity of various entities. + */ + def validClassFile(name: String) = endsClass(name) && context.isValidName(name) + def validPackage(name: String) = (name != "META-INF") && (name != "") && (name.charAt(0) != '.') + def validSourceFile(name: String) = endsScala(name) || endsJava(name) + + /** + * Find a ClassRep given a class name of the form "package.subpackage.ClassName". + * Does not support nested classes on .NET + */ + def findClass(name: String): Option[AnyClassRep] = + splitWhere(name, _ == '.', true) match { + case Some((pkg, rest)) => + val rep = packages find (_.name == pkg) flatMap (_ findClass rest) + rep map { + case x: ClassRep => x + case x => throw new FatalError("Unexpected ClassRep '%s' found searching for name '%s'".format(x, name)) + } + case _ => + classes find (_.name == name) + } + + def findSourceFile(name: String): Option[AbstractFile] = + findClass(name) match { + case Some(ClassRep(Some(x: AbstractFile), _)) => Some(x) + case _ => None + } + + def sortString = join(split(asClasspathString).sorted: _*) + override def equals(that: Any) = that match { + case x: ClassPath[_] => this.sortString == x.sortString + case _ => false + } + override def hashCode = sortString.hashCode() +} + +/** + * A Classpath containing source files + */ +class SourcePath[T](dir: AbstractFile, val context: ClassPathContext[T]) extends ClassPath[T] { + def name = dir.name + override def origin = dir.underlyingSource map (_.path) + def asURLs = if (dir.file == null) Nil else List(dir.toURL) + def asClasspathString = dir.path + val sourcepaths: IndexedSeq[AbstractFile] = IndexedSeq(dir) + + private def traverse() = { + val classBuf = immutable.Vector.newBuilder[ClassRep] + val packageBuf = immutable.Vector.newBuilder[SourcePath[T]] + dir foreach { f => + if (!f.isDirectory && validSourceFile(f.name)) + classBuf += ClassRep(None, Some(f)) + else if (f.isDirectory && validPackage(f.name)) + packageBuf += new SourcePath[T](f, context) + } + (packageBuf.result, classBuf.result) + } + + lazy val (packages, classes) = traverse() + override def toString() = "sourcepath: "+ dir.toString() +} + +/** + * A directory (or a .jar file) containing classfiles and packages + */ +class DirectoryClassPath(val dir: AbstractFile, val context: ClassPathContext[AbstractFile]) extends ClassPath[AbstractFile] { + def name = dir.name + override def origin = dir.underlyingSource map (_.path) + def asURLs = if (dir.file == null) Nil else List(dir.toURL) + def asClasspathString = dir.path + val sourcepaths: IndexedSeq[AbstractFile] = IndexedSeq() + + // calculates (packages, classes) in one traversal. + private def traverse() = { + val classBuf = immutable.Vector.newBuilder[ClassRep] + val packageBuf = immutable.Vector.newBuilder[DirectoryClassPath] + dir foreach { f => + if (!f.isDirectory && validClassFile(f.name)) + classBuf += ClassRep(Some(f), None) + else if (f.isDirectory && validPackage(f.name)) + packageBuf += new DirectoryClassPath(f, context) + } + (packageBuf.result, classBuf.result) + } + + lazy val (packages, classes) = traverse() + override def toString() = "directory classpath: "+ origin.getOrElse("?") +} + +class DeltaClassPath[T](original: MergedClassPath[T], subst: Map[ClassPath[T], ClassPath[T]]) +extends MergedClassPath[T](original.entries map (e => subst getOrElse (e, e)), original.context) { + // not sure we should require that here. Commented out for now. + // require(subst.keySet subsetOf original.entries.toSet) + // We might add specialized operations for computing classes packages here. Not sure it's worth it. +} + +/** + * A classpath unifying multiple class- and sourcepath entries. + */ +class MergedClassPath[T]( + val entries: IndexedSeq[ClassPath[T]], + val context: ClassPathContext[T]) +extends ClassPath[T] { + def this(entries: TraversableOnce[ClassPath[T]], context: ClassPathContext[T]) = + this(entries.toIndexedSeq, context) + + def name = entries.head.name + def asURLs = (entries flatMap (_.asURLs)).toList + lazy val sourcepaths: IndexedSeq[AbstractFile] = entries flatMap (_.sourcepaths) + + override def origin = Some(entries map (x => x.origin getOrElse x.name) mkString ("Merged(", ", ", ")")) + override def asClasspathString: String = join(entries map (_.asClasspathString) : _*) + + lazy val classes: IndexedSeq[AnyClassRep] = { + var count = 0 + val indices = mutable.HashMap[String, Int]() + val cls = new mutable.ArrayBuffer[AnyClassRep](1024) + + for (e <- entries; c <- e.classes) { + val name = c.name + if (indices contains name) { + val idx = indices(name) + val existing = cls(idx) + + if (existing.binary.isEmpty && c.binary.isDefined) + cls(idx) = existing.copy(binary = c.binary) + if (existing.source.isEmpty && c.source.isDefined) + cls(idx) = existing.copy(source = c.source) + } + else { + indices(name) = count + cls += c + count += 1 + } + } + cls.toIndexedSeq + } + + lazy val packages: IndexedSeq[ClassPath[T]] = { + var count = 0 + val indices = mutable.HashMap[String, Int]() + val pkg = new mutable.ArrayBuffer[ClassPath[T]](256) + + for (e <- entries; p <- e.packages) { + val name = p.name + if (indices contains name) { + val idx = indices(name) + pkg(idx) = addPackage(pkg(idx), p) + } + else { + indices(name) = count + pkg += p + count += 1 + } + } + pkg.toIndexedSeq + } + + private def addPackage(to: ClassPath[T], pkg: ClassPath[T]) = { + val newEntries: IndexedSeq[ClassPath[T]] = to match { + case cp: MergedClassPath[_] => cp.entries :+ pkg + case _ => IndexedSeq(to, pkg) + } + new MergedClassPath[T](newEntries, context) + } + def show() { + println("ClassPath %s has %d entries and results in:\n".format(name, entries.size)) + asClasspathString split ':' foreach (x => println(" " + x)) + } + override def toString() = "merged classpath "+ entries.mkString("(", "\n", ")") +} + +/** + * The classpath when compiling with target:jvm. Binary files (classfiles) are represented + * as AbstractFile. nsc.io.ZipArchive is used to view zip/jar archives as directories. + */ +class JavaClassPath( + containers: IndexedSeq[ClassPath[AbstractFile]], + context: JavaContext) +extends MergedClassPath[AbstractFile](containers, context) { } + +object JavaClassPath { + def fromURLs(urls: Seq[URL], context: JavaContext): JavaClassPath = { + val containers = { + for (url <- urls ; f = AbstractFile getURL url ; if f != null) yield + new DirectoryClassPath(f, context) + } + new JavaClassPath(containers.toIndexedSeq, context) + } + def fromURLs(urls: Seq[URL]): JavaClassPath = + fromURLs(urls, ClassPath.DefaultJavaContext) +} diff --git a/src/dotty/tools/io/DaemonThreadFactory.scala b/src/dotty/tools/io/DaemonThreadFactory.scala new file mode 100644 index 000000000..ae0cda260 --- /dev/null +++ b/src/dotty/tools/io/DaemonThreadFactory.scala @@ -0,0 +1,16 @@ +package dotty.tools +package io + +import java.util.concurrent._ + +class DaemonThreadFactory extends ThreadFactory { + def newThread(r: Runnable): Thread = { + val thread = new Thread(r) + thread setDaemon true + thread + } +} + +object DaemonThreadFactory { + def newPool() = Executors.newCachedThreadPool(new DaemonThreadFactory) +} diff --git a/src/dotty/tools/io/Fileish.scala b/src/dotty/tools/io/Fileish.scala new file mode 100644 index 000000000..4f17ec4d0 --- /dev/null +++ b/src/dotty/tools/io/Fileish.scala @@ -0,0 +1,33 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2012 LAMP/EPFL + * @author Paul Phillips + */ + +package dotty.tools +package io + +import java.io.{ InputStream } +import java.util.jar.JarEntry + +/** A common interface for File-based things and Stream-based things. + * (In particular, io.File and JarEntry.) + */ +class Fileish(val path: Path, val input: () => InputStream) extends Streamable.Chars { + def inputStream() = input() + + def parent = path.parent + def name = path.name + def isSourceFile = path.hasExtension("java", "scala") + + private lazy val pkgLines = lines() collect { case x if x startsWith "package " => x stripPrefix "package" trim } + lazy val pkgFromPath = parent.path.replaceAll("""[/\\]""", ".") + lazy val pkgFromSource = pkgLines map (_ stripSuffix ";") mkString "." + + override def toString = path.path +} + +object Fileish { + def apply(f: File): Fileish = new Fileish(f, () => f.inputStream()) + def apply(f: JarEntry, in: () => InputStream): Fileish = new Fileish(Path(f.getName), in) + def apply(path: String, in: () => InputStream): Fileish = new Fileish(Path(path), in) +} diff --git a/src/dotty/tools/io/Jar.scala b/src/dotty/tools/io/Jar.scala new file mode 100644 index 000000000..449602409 --- /dev/null +++ b/src/dotty/tools/io/Jar.scala @@ -0,0 +1,174 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2012 LAMP/EPFL + * @author Paul Phillips + */ + + +package dotty.tools +package io + +import java.io.{ InputStream, OutputStream, IOException, FileNotFoundException, FileInputStream, DataOutputStream } +import java.util.jar._ +import scala.collection.JavaConverters._ +import Attributes.Name +import dotty.tools.io.ClassPath +import reflect.io._ +import scala.language.implicitConversions + +// Attributes.Name instances: +// +// static Attributes.Name CLASS_PATH +// static Attributes.Name CONTENT_TYPE +// static Attributes.Name EXTENSION_INSTALLATION +// static Attributes.Name EXTENSION_LIST +// static Attributes.Name EXTENSION_NAME +// static Attributes.Name IMPLEMENTATION_TITLE +// static Attributes.Name IMPLEMENTATION_URL +// static Attributes.Name IMPLEMENTATION_VENDOR +// static Attributes.Name IMPLEMENTATION_VENDOR_ID +// static Attributes.Name IMPLEMENTATION_VERSION +// static Attributes.Name MAIN_CLASS +// static Attributes.Name MANIFEST_VERSION +// static Attributes.Name SEALED +// static Attributes.Name SIGNATURE_VERSION +// static Attributes.Name SPECIFICATION_TITLE +// static Attributes.Name SPECIFICATION_VENDOR +// static Attributes.Name SPECIFICATION_VERSION + +class Jar(file: File) extends Iterable[JarEntry] { + def this(jfile: JFile) = this(File(jfile)) + def this(path: String) = this(File(path)) + + protected def errorFn(msg: String): Unit = Console println msg + + lazy val jarFile = new JarFile(file.jfile) + lazy val manifest = withJarInput(s => Option(s.getManifest)) + + def mainClass = manifest map (f => f(Name.MAIN_CLASS)) + /** The manifest-defined classpath String if available. */ + def classPathString: Option[String] = + for (m <- manifest ; cp <- m.attrs get Name.CLASS_PATH) yield cp + def classPathElements: List[String] = classPathString match { + case Some(s) => s split "\\s+" toList + case _ => Nil + } + + def withJarInput[T](f: JarInputStream => T): T = { + val in = new JarInputStream(file.inputStream()) + try f(in) + finally in.close() + } + def jarWriter(mainAttrs: (Attributes.Name, String)*) = { + new JarWriter(file, Jar.WManifest(mainAttrs: _*).underlying) + } + + override def foreach[U](f: JarEntry => U): Unit = withJarInput { in => + Iterator continually in.getNextJarEntry() takeWhile (_ != null) foreach f + } + override def iterator: Iterator[JarEntry] = this.toList.iterator + def fileishIterator: Iterator[Fileish] = jarFile.entries.asScala map (x => Fileish(x, () => getEntryStream(x))) + + private def getEntryStream(entry: JarEntry) = jarFile getInputStream entry match { + case null => errorFn("No such entry: " + entry) ; null + case x => x + } + override def toString = "" + file +} + +class JarWriter(val file: File, val manifest: Manifest) { + private lazy val out = new JarOutputStream(file.outputStream(), manifest) + + /** Adds a jar entry for the given path and returns an output + * stream to which the data should immediately be written. + * This unusual interface exists to work with fjbg. + */ + def newOutputStream(path: String): DataOutputStream = { + val entry = new JarEntry(path) + out putNextEntry entry + new DataOutputStream(out) + } + + def writeAllFrom(dir: Directory) { + try dir.list foreach (x => addEntry(x, "")) + finally out.close() + } + def addStream(entry: JarEntry, in: InputStream) { + out putNextEntry entry + try transfer(in, out) + finally out.closeEntry() + } + def addFile(file: File, prefix: String) { + val entry = new JarEntry(prefix + file.name) + addStream(entry, file.inputStream()) + } + def addEntry(entry: Path, prefix: String) { + if (entry.isFile) addFile(entry.toFile, prefix) + else addDirectory(entry.toDirectory, prefix + entry.name + "/") + } + def addDirectory(entry: Directory, prefix: String) { + entry.list foreach (p => addEntry(p, prefix)) + } + + private def transfer(in: InputStream, out: OutputStream) = { + val buf = new Array[Byte](10240) + def loop(): Unit = in.read(buf, 0, buf.length) match { + case -1 => in.close() + case n => out.write(buf, 0, n) ; loop + } + loop + } + + def close() = out.close() +} + +object Jar { + type AttributeMap = java.util.Map[Attributes.Name, String] + + object WManifest { + def apply(mainAttrs: (Attributes.Name, String)*): WManifest = { + val m = WManifest(new JManifest) + for ((k, v) <- mainAttrs) + m(k) = v + + m + } + def apply(manifest: JManifest): WManifest = new WManifest(manifest) + implicit def unenrichManifest(x: WManifest): JManifest = x.underlying + } + class WManifest(manifest: JManifest) { + for ((k, v) <- initialMainAttrs) + this(k) = v + + def underlying = manifest + def attrs = manifest.getMainAttributes().asInstanceOf[AttributeMap].asScala withDefaultValue null + def initialMainAttrs: Map[Attributes.Name, String] = { + import scala.util.Properties._ + Map( + Name.MANIFEST_VERSION -> "1.0", + ScalaCompilerVersion -> versionNumberString + ) + } + + def apply(name: Attributes.Name): String = attrs(name) + def apply(name: String): String = apply(new Attributes.Name(name)) + def update(key: Attributes.Name, value: String) = attrs.put(key, value) + def update(key: String, value: String) = attrs.put(new Attributes.Name(key), value) + + def mainClass: String = apply(Name.MAIN_CLASS) + def mainClass_=(value: String) = update(Name.MAIN_CLASS, value) + } + + // See http://download.java.net/jdk7/docs/api/java/nio/file/Path.html + // for some ideas. + private val ZipMagicNumber = List[Byte](80, 75, 3, 4) + private def magicNumberIsZip(f: Path) = f.isFile && (f.toFile.bytes().take(4).toList == ZipMagicNumber) + + def isJarOrZip(f: Path): Boolean = isJarOrZip(f, true) + def isJarOrZip(f: Path, examineFile: Boolean): Boolean = + f.hasExtension("zip", "jar") || (examineFile && magicNumberIsZip(f)) + + def create(file: File, sourceDir: Directory, mainClass: String) { + val writer = new Jar(file).jarWriter(Name.MAIN_CLASS -> mainClass) + writer writeAllFrom sourceDir + } +} diff --git a/src/dotty/tools/io/package.scala b/src/dotty/tools/io/package.scala new file mode 100644 index 000000000..2543c38d2 --- /dev/null +++ b/src/dotty/tools/io/package.scala @@ -0,0 +1,58 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2012 LAMP/EPFL + * @author Paul Phillips + */ + +package dotty.tools + +import java.util.concurrent.{ Future, Callable } +import java.util.{ Timer, TimerTask } +import java.util.jar.{ Attributes } +import scala.language.implicitConversions + +package object io { + // Forwarders from scala.reflect.io + type AbstractFile = scala.reflect.io.AbstractFile + val AbstractFile = scala.reflect.io.AbstractFile + type Directory = scala.reflect.io.Directory + val Directory = scala.reflect.io.Directory + type File = scala.reflect.io.File + val File = scala.reflect.io.File + type Path = scala.reflect.io.Path + val Path = scala.reflect.io.Path + type PlainFile = scala.reflect.io.PlainFile + val PlainFile = scala.reflect.io.PlainFile + val Streamable = scala.reflect.io.Streamable + type VirtualDirectory = scala.reflect.io.VirtualDirectory + type VirtualFile = scala.reflect.io.VirtualFile + val ZipArchive = scala.reflect.io.ZipArchive + type ZipArchive = scala.reflect.io.ZipArchive + type JManifest = java.util.jar.Manifest + type JFile = java.io.File + + implicit def enrichManifest(m: JManifest): Jar.WManifest = Jar.WManifest(m) + private lazy val daemonThreadPool = DaemonThreadFactory.newPool() + + def runnable(body: => Unit): Runnable = new Runnable { override def run() = body } + def callable[T](body: => T): Callable[T] = new Callable[T] { override def call() = body } + def spawn[T](body: => T): Future[T] = daemonThreadPool submit callable(body) + def submit(runnable: Runnable) = daemonThreadPool submit runnable + + // Create, start, and return a daemon thread + def daemonize(body: => Unit): Thread = newThread(_ setDaemon true)(body) + def newThread(f: Thread => Unit)(body: => Unit): Thread = { + val thread = new Thread(runnable(body)) + f(thread) + thread.start + thread + } + + // Set a timer to execute the given code. + def timer(seconds: Int)(body: => Unit): Timer = { + val alarm = new Timer(true) // daemon + val tt = new TimerTask { def run() = body } + + alarm.schedule(tt, seconds * 1000) + alarm + } +} diff --git a/src/dotty/tools/package.scala b/src/dotty/tools/package.scala new file mode 100644 index 000000000..eca4216ad --- /dev/null +++ b/src/dotty/tools/package.scala @@ -0,0 +1,11 @@ +package dotty + +package object tools { + type FatalError = scala.reflect.internal.FatalError + val FatalError = scala.reflect.internal.FatalError + + type MissingRequirementError = scala.reflect.internal.MissingRequirementError + val MissingRequirementError = scala.reflect.internal.MissingRequirementError + + val ListOfNil = List(Nil) +} |