Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Aug 11, 2023
1 parent 1612275 commit 8ea1b07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class PickleQuotes extends MacroTransform {
private def getPicklableHoleType(tpe: Type, isStagedClasses: Symbol => Boolean)(using Context) =
new TypeOps.AvoidMap {
def toAvoid(tp: NamedType) = !isStagedClasses(tp.typeSymbol) && !isStaticPrefix(tp)
override def apply(tp: Type): Type = tp match
case tp: ThisType /*if isStagedClasses(tp.typeSymbol)*/ => super.apply(tp.widen) // FIXME: isStagedClasses is empty when transforming the inner splice.
case _ => super.apply(tp)
}.apply(tpe)
}

Expand Down
16 changes: 11 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/Splicing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,15 @@ class Splicing extends MacroTransform:
* ```
*/
private class SpliceTransformer(spliceOwner: Symbol, isCaptured: Symbol => Boolean) extends Transformer:
private var refBindingMap = mutable.LinkedHashMap.empty[Symbol, (Tree, Symbol)]
private var refTermBindingMap = mutable.LinkedHashMap.empty[Symbol, (Tree, Symbol)]
private var refTypeBindingMap = mutable.LinkedHashMap.empty[Symbol, (Tree, Symbol)]
/** Reference to the `Quotes` instance of the current level 1 splice */
private var quotes: Tree | Null = null // TODO: add to the context

def transformSplice(tree: tpd.Tree, tpe: Type, holeIdx: Int)(using Context): tpd.Tree =
assert(level == 0)
val newTree = transform(tree)
val (refs, bindings) = refBindingMap.values.toList.unzip
val (refs, bindings) = (refTypeBindingMap.values ++ refTermBindingMap.values).toList.unzip
val bindingsTypes = bindings.map(_.termRef.widenTermRefExpr)
val methType = MethodType(bindingsTypes, newTree.tpe)
val meth = newSymbol(spliceOwner, nme.ANON_FUN, Synthetic | Method, methType)
Expand Down Expand Up @@ -212,6 +213,11 @@ class Splicing extends MacroTransform:
case _: TypeTree | _: SingletonTypeTree =>
if containsCapturedType(tree.tpe) && level >= 1 then getTagRefFor(tree)
else tree
case _: This =>
if isCaptured(tree.symbol) then
val tag = getTagRefFor(tree)
spliced(tag.tpe)(capturedTerm(tree))
else super.transform(tree)
case tree @ Assign(lhs: RefTree, rhs) =>
if isCaptured(lhs.symbol) then transformSplicedAssign(tree)
else super.transform(tree)
Expand Down Expand Up @@ -302,7 +308,7 @@ class Splicing extends MacroTransform:
Param,
defn.QuotedExprClass.typeRef.appliedTo(tpe),
)
val bindingSym = refBindingMap.getOrElseUpdate(tree.symbol, (tree, newBinding))._2
val bindingSym = refTermBindingMap.getOrElseUpdate(tree.symbol, (tree, newBinding))._2
ref(bindingSym)

private def newQuotedTypeClassBinding(tpe: Type)(using Context) =
Expand All @@ -314,14 +320,14 @@ class Splicing extends MacroTransform:
)

private def capturedType(tree: Tree)(using Context): Symbol =
refBindingMap.getOrElseUpdate(tree.symbol, (TypeTree(tree.tpe), newQuotedTypeClassBinding(tree.tpe)))._2
refTypeBindingMap.getOrElseUpdate(tree.symbol, (TypeTree(tree.tpe), newQuotedTypeClassBinding(tree.tpe)))._2

private def capturedPartTypes(quote: Quote)(using Context): Tree =
val (tags, body1) = inContextWithQuoteTypeTags {
val capturePartTypes = new TypeMap {
def apply(tp: Type) = tp match {
case typeRef: TypeRef if containsCapturedType(typeRef) =>
val termRef = refBindingMap
val termRef = refTypeBindingMap
.getOrElseUpdate(typeRef.symbol, (TypeTree(typeRef), newQuotedTypeClassBinding(typeRef)))._2.termRef
val tagRef = getTagRef(termRef)
tagRef
Expand Down

0 comments on commit 8ea1b07

Please sign in to comment.