Skip to content

Commit 7d14d44

Browse files
Merge pull request #9560 from dotty-staging/bootstrapped-internal-quoted-sources
Move internal.quoted sources to bootstrapped lib
2 parents 88d2546 + e9deded commit 7d14d44

File tree

8 files changed

+168
-0
lines changed

8 files changed

+168
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package scala.internal.quoted
2+
3+
import scala.quoted._
4+
5+
/** An Expr backed by a tree. Only the current compiler trees are allowed.
6+
*
7+
* These expressions are used for arguments of macros. They contain and actual tree
8+
* from the program that is being expanded by the macro.
9+
*
10+
* May contain references to code defined outside this Expr instance.
11+
*/
12+
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
13+
override def equals(that: Any): Boolean = that match {
14+
case that: Expr[_] =>
15+
// Expr are wrappers around trees, therfore they are equals if their trees are equal.
16+
// All scopeId should be equal unless two different runs of the compiler created the trees.
17+
tree == that.tree && scopeId == that.scopeId
18+
case _ => false
19+
}
20+
21+
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
22+
if (qctx.tasty.internal.compilerId != scopeId)
23+
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
24+
tree.asInstanceOf[qctx.tasty.Term]
25+
26+
override def hashCode: Int = tree.hashCode
27+
override def toString: String = "'{ ... }"
28+
}
29+
30+
object Expr {
31+
32+
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[Any])(using patternExpr: scala.quoted.Expr[Any],
33+
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] =
34+
throw new Exception("Non bootstrapped lib")
35+
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package scala.internal.quoted
2+
3+
import scala.annotation.internal.sharable
4+
import scala.annotation.{Annotation, compileTimeOnly}
5+
6+
import scala.quoted._
7+
8+
object Matcher {
9+
10+
/** A splice in a quoted pattern is desugared by the compiler into a call to this method */
11+
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternHole`")
12+
def patternHole[T]: T = ???
13+
14+
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternHigherOrderHole`")
15+
/** A higher order splice in a quoted pattern is desugared by the compiler into a call to this method */
16+
def patternHigherOrderHole[U](pat: Any, args: Any*): U = ???
17+
18+
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.higherOrderHole`")
19+
/** A higher order splice in a quoted pattern is desugared by the compiler into a call to this method */
20+
def higherOrderHole[U](args: Any*): U = ???
21+
22+
// TODO remove
23+
/** A splice of a name in a quoted pattern is desugared by wrapping getting this annotation */
24+
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternBindHole`")
25+
class patternBindHole extends Annotation
26+
27+
/** A splice of a name in a quoted pattern is that marks the definition of a type splice */
28+
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternType`")
29+
class patternType extends Annotation
30+
31+
/** A type pattern that must be aproximated from above */
32+
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.fromAbove`")
33+
class fromAbove extends Annotation
34+
35+
class QuoteMatcher[QCtx <: QuoteContext & Singleton](using val qctx: QCtx) {
36+
import qctx.tasty._
37+
38+
def termMatch(scrutineeTerm: Term, patternTerm: Term, hasTypeSplices: Boolean): Option[Tuple] =
39+
throw new Exception("Non bootstrapped lib")
40+
41+
def typeTreeMatch(scrutineeTypeTree: TypeTree, patternTypeTree: TypeTree, hasTypeSplices: Boolean): Option[Tuple] =
42+
throw new Exception("Non bootstrapped lib")
43+
}
44+
45+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package scala.internal.quoted
2+
3+
import scala.quoted._
4+
5+
/** Quoted type (or kind) `T` backed by a tree */
6+
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {
7+
override def equals(that: Any): Boolean = that match {
8+
case that: Type[_] => typeTree ==
9+
// TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal.
10+
// All scopeId should be equal unless two different runs of the compiler created the trees.
11+
that.typeTree && scopeId == that.scopeId
12+
case _ => false
13+
}
14+
15+
/** View this expression `quoted.Type[T]` as a `TypeTree` */
16+
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
17+
if (qctx.tasty.internal.compilerId != scopeId)
18+
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
19+
typeTree.asInstanceOf[qctx.tasty.TypeTree]
20+
21+
override def hashCode: Int = typeTree.hashCode
22+
override def toString: String = "'[ ... ]"
23+
}
24+
25+
object Type {
26+
27+
/** Pattern matches an the scrutineeType against the patternType and returns a tuple
28+
* with the matched holes if successful.
29+
*
30+
* Holes:
31+
* - scala.internal.Quoted.patternHole[T]: hole that matches an expression `x` of type `Type[U]`
32+
* if `U <:< T` and returns `x` as part of the match.
33+
*
34+
* @param scrutineeType `Type[_]` on which we are pattern matching
35+
* @param patternType `Type[_]` containing the pattern tree
36+
* @param hasTypeSplices `Boolean` notify if the pattern has type splices (if so we use a GADT context)
37+
* @param qctx the current QuoteContext
38+
* @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Type[Ti]``
39+
*/
40+
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeType: scala.quoted.Type[_])(using patternType: scala.quoted.Type[_],
41+
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] =
42+
throw new Exception("Non bootstrapped lib")
43+
44+
def Unit: QuoteContext ?=> quoted.Type[Unit] =
45+
throw new Exception("Non bootstrapped lib")
46+
47+
def Boolean: QuoteContext ?=> quoted.Type[Boolean] =
48+
throw new Exception("Non bootstrapped lib")
49+
50+
def Byte: QuoteContext ?=> quoted.Type[Byte] =
51+
throw new Exception("Non bootstrapped lib")
52+
53+
def Char: QuoteContext ?=> quoted.Type[Char] =
54+
throw new Exception("Non bootstrapped lib")
55+
56+
def Short: QuoteContext ?=> quoted.Type[Short] =
57+
throw new Exception("Non bootstrapped lib")
58+
59+
def Int: QuoteContext ?=> quoted.Type[Int] =
60+
throw new Exception("Non bootstrapped lib")
61+
62+
def Long: QuoteContext ?=> quoted.Type[Long] =
63+
throw new Exception("Non bootstrapped lib")
64+
65+
def Float: QuoteContext ?=> quoted.Type[Float] =
66+
throw new Exception("Non bootstrapped lib")
67+
68+
def Double: QuoteContext ?=> quoted.Type[Double] =
69+
throw new Exception("Non bootstrapped lib")
70+
71+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package scala.internal.quoted
2+
3+
import scala.quoted.{Expr, QuoteContext, Type}
4+
5+
object Unpickler {
6+
7+
type PickledQuote = List[String]
8+
type PickledArgs = Seq[Seq[Any] => Any/*(([QCtx <: QuoteContext] =>> QCtx ?=> Expr[Any]) | Type[_])*/]
9+
10+
def unpickleExpr[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Expr[T] =
11+
throw new Exception("Non bootstrapped lib")
12+
13+
def unpickleType[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Type[T] =
14+
throw new Exception("Non bootstrapped lib")
15+
16+
}

0 commit comments

Comments
 (0)