2010-10-23 03:55:16 +02:00
|
|
|
package xsbt.api
|
|
|
|
|
|
2011-10-06 00:09:27 +02:00
|
|
|
import xsbti.SafeLazy
|
2010-10-23 03:55:16 +02:00
|
|
|
import xsbti.api._
|
|
|
|
|
import scala.collection.mutable.HashSet
|
|
|
|
|
|
|
|
|
|
object APIUtil
|
|
|
|
|
{
|
|
|
|
|
val modifiersToByte = (m: Modifiers) => {
|
|
|
|
|
import m._
|
|
|
|
|
def x(b: Boolean, bit: Int) = if(b) 1 << bit else 0
|
|
|
|
|
( x(isAbstract, 0) | x(isOverride, 1) | x(isFinal, 2) | x(isSealed, 3) | x(isImplicit, 4) | x(isLazy, 5) ).toByte
|
|
|
|
|
}
|
|
|
|
|
val byteToModifiers = (b: Byte) => {
|
|
|
|
|
def x(bit: Int) = (b & (1 << bit)) != 0
|
|
|
|
|
new Modifiers( x(0), x(1), x(2), x(3), x(4), x(5) )
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-01 08:19:46 +02:00
|
|
|
def verifyTypeParameters(s: SourceAPI): Boolean =
|
2010-10-23 03:55:16 +02:00
|
|
|
{
|
|
|
|
|
val check = new CheckTypeParameters
|
|
|
|
|
val invalid = check(s)
|
|
|
|
|
if(!invalid.isEmpty) println("References to undefined type parameters: " + invalid.mkString(", "))
|
|
|
|
|
invalid.isEmpty
|
|
|
|
|
}
|
|
|
|
|
private[this] class CheckTypeParameters extends Visit
|
|
|
|
|
{
|
|
|
|
|
private val defined = new HashSet[Int]
|
|
|
|
|
private val referenced = new HashSet[Int]
|
2011-06-01 08:19:46 +02:00
|
|
|
def apply(s: SourceAPI): List[Int] =
|
2010-10-23 03:55:16 +02:00
|
|
|
{
|
2011-06-01 08:19:46 +02:00
|
|
|
super.visitAPI(s)
|
2010-10-23 03:55:16 +02:00
|
|
|
(referenced filterNot defined).toList
|
|
|
|
|
}
|
|
|
|
|
override def visitTypeParameter(parameter: TypeParameter)
|
|
|
|
|
{
|
|
|
|
|
defined += parameter.id
|
|
|
|
|
super.visitTypeParameter(parameter)
|
|
|
|
|
}
|
|
|
|
|
override def visitParameterRef(ref: ParameterRef)
|
|
|
|
|
{
|
|
|
|
|
referenced += ref.id
|
|
|
|
|
super.visitParameterRef(ref)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-06 00:09:27 +02:00
|
|
|
def minimize(api: SourceAPI): SourceAPI =
|
|
|
|
|
new SourceAPI(api.packages, minimizeDefinitions(api.definitions))
|
|
|
|
|
def minimizeDefinitions(ds: Array[Definition]): Array[Definition] =
|
|
|
|
|
ds flatMap minimizeDefinition
|
|
|
|
|
def minimizeDefinition(d: Definition): Array[Definition] =
|
|
|
|
|
d match
|
|
|
|
|
{
|
|
|
|
|
case c: ClassLike => Array(minimizeClass(c))
|
|
|
|
|
case _ => Array()
|
|
|
|
|
}
|
|
|
|
|
def minimizeClass(c: ClassLike): ClassLike =
|
|
|
|
|
{
|
2011-10-20 04:23:47 +02:00
|
|
|
val savedAnnotations = Discovery.defAnnotations(c.structure, (_: Any) => true).toArray[String]
|
2011-10-06 00:09:27 +02:00
|
|
|
val struct = minimizeStructure(c.structure, c.definitionType == DefinitionType.Module)
|
2011-10-20 04:23:47 +02:00
|
|
|
new ClassLike(c.definitionType, lzy(emptyType), lzy(struct), savedAnnotations, c.typeParameters, c.name, c.access, c.modifiers, c.annotations)
|
2011-10-06 00:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def minimizeStructure(s: Structure, isModule: Boolean): Structure =
|
|
|
|
|
new Structure(lzy(s.parents), filterDefinitions(s.declared, isModule), filterDefinitions(s.inherited, isModule))
|
|
|
|
|
def filterDefinitions(ds: Array[Definition], isModule: Boolean): Lazy[Array[Definition]] =
|
|
|
|
|
lzy(if(isModule) ds filter Discovery.isMainMethod else Array())
|
|
|
|
|
private[this] def lzy[T <: AnyRef](t: T): Lazy[T] = SafeLazy.strict(t)
|
|
|
|
|
|
|
|
|
|
private[this] val emptyType = new EmptyType
|
2010-10-23 03:55:16 +02:00
|
|
|
}
|