/
Type.scala
347 lines (307 loc) · 12.3 KB
/
Type.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package scala.slick.ast
import scala.language.implicitConversions
import scala.slick.SlickException
import scala.collection.generic.CanBuildFrom
import scala.reflect.ClassTag
import Util._
import scala.collection.mutable.ArrayBuffer
/** Super-trait for all types */
trait Type {
/** All children of this Type. */
def children: Seq[Type]
/** Apply a transformation to all type children and reconstruct this
* type with the new children, or return the original object if no
* child is changed. */
def mapChildren(f: Type => Type): Type
def select(sym: Symbol): Type =
throw new SlickException("No type for symbol "+sym+" found in "+this)
/** The structural view of this type */
def structural: Type = this
}
/** An atomic type (i.e. a type which does not contain other types) */
trait AtomicType extends Type {
final def mapChildren(f: Type => Type): this.type = this
def children: Seq[Type] = Seq.empty
}
final case class StructType(elements: Seq[(Symbol, Type)]) extends Type {
override def toString = "{" + elements.iterator.map{ case (s, t) => s + ": " + t }.mkString(", ") + "}"
lazy val symbolToIndex: Map[Symbol, Int] =
elements.zipWithIndex.map { case ((sym, _), idx) => (sym, idx) }(collection.breakOut)
def children: Seq[Type] = elements.map(_._2)
def mapChildren(f: Type => Type): StructType =
mapOrNone(elements.map(_._2))(f) match {
case Some(types2) => StructType((elements, types2).zipped.map((e, t) => (e._1, t)))
case None => this
}
override def select(sym: Symbol) = sym match {
case ElementSymbol(idx) => elements(idx-1)._2
case _ => elements.find(x => x._1 == sym).map(_._2).getOrElse(super.select(sym))
}
}
trait OptionType extends Type {
override def toString = "Option[" + elementType + "]"
def elementType: Type
def children: Seq[Type] = Seq(elementType)
}
object OptionType {
def apply(tpe: Type): OptionType = new OptionType {
def elementType = tpe
def mapChildren(f: Type => Type): OptionType = {
val e2 = f(elementType)
if(e2 eq elementType) this
else OptionType(e2)
}
}
}
final case class ProductType(elements: IndexedSeq[Type]) extends Type {
override def toString = "(" + elements.mkString(", ") + ")"
def mapChildren(f: Type => Type): ProductType =
mapOrNone(elements)(f) match {
case Some(e2) => ProductType(e2)
case None => this
}
override def select(sym: Symbol) = sym match {
case ElementSymbol(i) if i <= elements.length => elements(i-1)
case _ => super.select(sym)
}
def children: Seq[Type] = elements
def numberedElements: Iterator[(ElementSymbol, Type)] =
elements.iterator.zipWithIndex.map { case (t, i) => (new ElementSymbol(i+1), t) }
}
final case class CollectionType(cons: CollectionTypeConstructor, elementType: Type) extends Type {
override def toString = cons + "[" + elementType + "]"
def mapChildren(f: Type => Type): CollectionType = {
val e2 = f(elementType)
if(e2 eq elementType) this
else CollectionType(cons, e2)
}
def children: Seq[Type] = Seq(elementType)
}
case class CollectionTypeConstructor(dummy: String = "") {
def canBuildFrom = implicitly[CanBuildFrom[Vector[Any], Any, Vector[Any]]]
override def toString = "Coll"
}
object CollectionTypeConstructor {
def default = new CollectionTypeConstructor
}
final class MappedScalaType(val baseType: Type, _toBase: Any => Any, _toMapped: Any => Any) extends Type {
def toBase(v: Any): Any = _toBase(v)
def toMapped(v: Any): Any = _toMapped(v)
override def toString = s"Mapped[$baseType]"
def mapChildren(f: Type => Type): MappedScalaType = {
val e2 = f(baseType)
if(e2 eq baseType) this
else new MappedScalaType(e2, _toBase, _toMapped)
}
def children: Seq[Type] = Seq(baseType)
override def select(sym: Symbol) = baseType.select(sym)
}
@deprecated("Use UnassignedType or UnassignedStructuralType", "2.0.1")
final case object NoType extends AtomicType
/** The standard type for freshly constructed nodes without an explicit type. */
final case object UnassignedType extends AtomicType
/** The type of a structural view of a NominalType before computing the
* proper type in the `inferTypes` phase. */
final case class UnassignedStructuralType(sym: TypeSymbol) extends AtomicType
/* A type with a name, as used by tables.
*
* Compiler phases which change types may keep their own representation
* of the structural view but must update the AST at the end of the phase
* so that all NominalTypes with the same symbol have the same structural
* view. */
final case class NominalType(sym: TypeSymbol)(val structuralView: Type) extends Type {
def toShortString = s"NominalType($sym)"
override def toString = s"$toShortString($structuralView)"
def withStructuralView(t: Type): NominalType =
if(t == structuralView) this else copy()(t)
override def structural: Type = structuralView.structural
override def select(sym: Symbol): Type = structuralView.select(sym)
def mapChildren(f: Type => Type): NominalType = {
val struct2 = f(structuralView)
if(struct2 eq structuralView) this
else new NominalType(sym)(struct2)
}
def children: Seq[Type] = Seq(structuralView)
def sourceNominalType: NominalType = structuralView match {
case n: NominalType => n.sourceNominalType
case _ => this
}
}
/** Something that has a type */
trait Typed {
def tpe: Type
}
object Typed {
def unapply(t: Typed) = Some(t.tpe)
}
/* A Type that carries a Scala type argument */
trait TypedType[T] extends Type { self =>
def optionType: OptionTypedType[T] = new OptionTypedType[T] {
val elementType = self
def scalaType = new ScalaOptionType[T](self.scalaType)
def mapChildren(f: Type => Type): OptionTypedType[T] = {
val e2 = f(elementType)
if(e2 eq elementType) this
else e2.asInstanceOf[TypedType[T]].optionType
}
}
def scalaType: ScalaType[T]
}
trait BaseTypedType[T] extends TypedType[T] with AtomicType
trait OptionTypedType[T] extends TypedType[Option[T]] with OptionType {
val elementType: TypedType[T]
}
/** Mark a TypedType as eligible for numeric operators. */
trait NumericTypedType
object TypedType {
@inline implicit def typedTypeToOptionTypedType[T](implicit t: TypedType[T]): OptionTypedType[T] = t.optionType
}
class TypeUtil(val tpe: Type) extends AnyVal {
def asCollectionType: CollectionType = tpe match {
case c: CollectionType => c
case _ => throw new SlickException("Expected a collection type, found "+tpe)
}
def asOptionType: OptionType = tpe match {
case o: OptionType => o
case _ => throw new SlickException("Expected an option type, found "+tpe)
}
def foreach[U](f: (Type => U)) {
def g(n: Type) {
f(n)
n.children.foreach(g)
}
g(tpe)
}
@inline def replace(f: PartialFunction[Type, Type]): Type = TypeUtilOps.replace(tpe, f)
@inline def collect[T](pf: PartialFunction[Type, T]): Iterable[T] = TypeUtilOps.collect(tpe, pf)
@inline def collectAll[T](pf: PartialFunction[Type, Seq[T]]): Iterable[T] = collect[Seq[T]](pf).flatten
}
object TypeUtil {
implicit def typeToTypeUtil(tpe: Type) = new TypeUtil(tpe)
/* An extractor for node types */
object :@ {
def unapply(n: Node) = Some((n, n.nodeType))
}
}
object TypeUtilOps {
import TypeUtil.typeToTypeUtil
def replace(tpe: Type, f: PartialFunction[Type, Type]): Type =
f.applyOrElse(tpe, ({ case t: Type => t.mapChildren(_.replace(f)) }): PartialFunction[Type, Type])
def collect[T](tpe: Type, pf: PartialFunction[Type, T]): Iterable[T] = {
val b = new ArrayBuffer[T]
tpe.foreach(pf.andThen[Unit]{ case t => b += t }.orElse[Type, Unit]{ case _ => () })
b
}
}
trait SymbolScope {
def + (entry: (Symbol, Type)): SymbolScope
def get(sym: Symbol): Option[Type]
def withDefault(f: (Symbol => Type)): SymbolScope
}
object SymbolScope {
val empty = new DefaultSymbolScope(Map.empty)
}
class DefaultSymbolScope(val m: Map[Symbol, Type]) extends SymbolScope {
def + (entry: (Symbol, Type)) = new DefaultSymbolScope(m + entry)
def get(sym: Symbol): Option[Type] = m.get(sym)
def withDefault(f: (Symbol => Type)) = new DefaultSymbolScope(m.withDefault(f))
}
/** A Slick Type encoding of plain Scala types.
*
* This is used by QueryInterpreter and MemoryDriver. Values stored in
* HeapBackend columns are also expected to use these types.
*
* All drivers should support the following types which are used internally
* by the lifted embedding and the query compiler: Boolean, Char, Int, Long,
* Null, String. */
trait ScalaType[T] extends TypedType[T] {
override def optionType: ScalaOptionType[T] = new ScalaOptionType[T](this)
def nullable: Boolean
def ordered: Boolean
def zero: T
def scalaOrderingFor(ord: Ordering): scala.math.Ordering[T]
final def scalaType = this
}
class ScalaBaseType[T](val zero: T)(implicit val tag: ClassTag[T], val ordering: scala.math.Ordering[T]) extends ScalaType[T] with BaseTypedType[T] {
override def toString = "ScalaType[" + tag.runtimeClass.getName + "]"
def nullable = false
def ordered = ordering ne null
def scalaOrderingFor(ord: Ordering) = {
if(ordering eq null) throw new SlickException("No ordering defined for "+this)
val base = if(ord.direction == Ordering.Desc) ordering.reverse else ordering
val nullsFirst = if(ord.nulls == Ordering.NullsFirst) -1 else 1
new scala.math.Ordering[T] {
def compare(x: T, y: T): Int = {
if((x.asInstanceOf[AnyRef] eq null) && (y.asInstanceOf[AnyRef] eq null)) 0
else if(x.asInstanceOf[AnyRef] eq null) nullsFirst
else if(y.asInstanceOf[AnyRef] eq null) -nullsFirst
else base.compare(x, y)
}
}
}
override def hashCode = tag.hashCode
override def equals(o: Any) = o match {
case t: ScalaBaseType[_] => tag == t.tag
case _ => false
}
}
object ScalaBaseType {
implicit val booleanType = new ScalaBaseType[Boolean](false)
implicit val bigDecimalType: ScalaNumericType[BigDecimal] = new ScalaNumericType[BigDecimal] {
def fromDouble(v: Double) = BigDecimal(v)
}
implicit val byteType: ScalaNumericType[Byte] = new ScalaNumericType[Byte] {
def fromDouble(v: Double) = v.toByte
}
implicit val charType = new ScalaBaseType[Char](' ')
implicit val doubleType: ScalaNumericType[Double] = new ScalaNumericType[Double] {
def fromDouble(v: Double) = v
}
implicit val floatType: ScalaNumericType[Float] = new ScalaNumericType[Float] {
def fromDouble(v: Double) = v.toFloat
}
implicit val intType: ScalaNumericType[Int] = new ScalaNumericType[Int] {
def fromDouble(v: Double) = v.toInt
}
implicit val longType: ScalaNumericType[Long] = new ScalaNumericType[Long] {
def fromDouble(v: Double) = v.toLong
}
implicit val nullType = new ScalaBaseType[Null](null)
implicit val shortType: ScalaNumericType[Short] = new ScalaNumericType[Short] {
def fromDouble(v: Double) = v.toShort
}
implicit val stringType = new ScalaBaseType[String]("")
private[this] val all: Map[ClassTag[_], ScalaBaseType[_]] =
Seq(booleanType, bigDecimalType, byteType, charType, doubleType,
floatType, intType, longType, nullType, shortType, stringType).map(s => (s.tag, s)).toMap
def apply[T](implicit tag: ClassTag[T], ord: scala.math.Ordering[T] = null): ScalaBaseType[T] =
all.getOrElse(tag, new ScalaBaseType[T](null.asInstanceOf[T])).asInstanceOf[ScalaBaseType[T]]
}
abstract class ScalaNumericType[T](implicit tag: ClassTag[T], val numeric: Numeric[T])
extends ScalaBaseType[T](numeric.zero)(tag, numeric) with NumericTypedType {
def fromDouble(v: Double): T
def toDouble(v: T) = numeric.toDouble(v)
}
class ScalaOptionType[T](val elementType: ScalaType[T]) extends ScalaType[Option[T]] with OptionTypedType[T] {
override def toString = "ScalaOptionType[" + elementType + "]"
def nullable = true
def ordered = elementType.ordered
def zero = None
def scalaOrderingFor(ord: Ordering) = {
val nullsFirst = if(ord.nulls == Ordering.NullsFirst) -1 else 1
val base = elementType.scalaOrderingFor(ord)
new scala.math.Ordering[Option[T]] {
def compare(x: Option[T], y: Option[T]): Int = {
if(x == None && y == None) 0
else if(x == None) nullsFirst
else if(y == None) -nullsFirst
else base.compare(x.get, y.get)
}
}
}
def mapChildren(f: Type => Type): ScalaOptionType[T] = {
val e2 = f(elementType)
if(e2 eq elementType) this
else e2.asInstanceOf[ScalaType[T]].optionType
}
}