diff options
Diffstat (limited to 'src/compiler/scala/reflect/reify/codegen/GenSymbols.scala')
-rw-r--r-- | src/compiler/scala/reflect/reify/codegen/GenSymbols.scala | 62 |
1 files changed, 37 insertions, 25 deletions
diff --git a/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala b/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala index ca6e14cfd3..c4b674955a 100644 --- a/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala +++ b/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala @@ -90,49 +90,61 @@ trait GenSymbols { } } else { // todo. make sure that free methods and free local defs work correctly - if (sym.isTerm) reifyFreeTerm(sym, Ident(sym)) - else reifyFreeType(sym, Ident(sym)) + if (sym.isExistential) reifySymDef(sym) + else if (sym.isTerm) reifyFreeTerm(Ident(sym)) + else reifyFreeType(Ident(sym)) } } - def reifyFreeTerm(sym: Symbol, value: Tree): Tree = - reifyIntoSymtab(sym) { + def reifyFreeTerm(binding: Tree): Tree = + reifyIntoSymtab(binding.symbol) { sym => if (reifyDebug) println("Free term" + (if (sym.isCapturedVariable) " (captured)" else "") + ": " + sym + "(" + sym.accurateKindString + ")") - var name = newTermName(nme.REIFY_FREE_PREFIX + sym.name) - if (sym.isType) name = name.append(nme.REIFY_FREE_THIS_SUFFIX) + val name = newTermName(nme.REIFY_FREE_PREFIX + sym.name + (if (sym.isType) nme.REIFY_FREE_THIS_SUFFIX else "")) if (sym.isCapturedVariable) { - assert(value.isInstanceOf[Ident], showRaw(value)) - val capturedTpe = capturedVariableType(sym) - val capturedValue = referenceCapturedVariable(sym) - (name, mirrorBuildCall(nme.newFreeTerm, reify(sym.name.toString), reify(capturedTpe), capturedValue, mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(origin(sym)))) + assert(binding.isInstanceOf[Ident], showRaw(binding)) + val capturedBinding = referenceCapturedVariable(sym) + Reification(name, capturedBinding, mirrorBuildCall(nme.newFreeTerm, reify(sym.name.toString), capturedBinding, mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(origin(sym)))) } else { - (name, mirrorBuildCall(nme.newFreeTerm, reify(sym.name.toString), reify(sym.tpe), value, mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(origin(sym)))) + Reification(name, binding, mirrorBuildCall(nme.newFreeTerm, reify(sym.name.toString), binding, mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(origin(sym)))) } } - def reifyFreeType(sym: Symbol, value: Tree): Tree = - reifyIntoSymtab(sym) { + def reifyFreeType(binding: Tree): Tree = + reifyIntoSymtab(binding.symbol) { sym => if (reifyDebug) println("Free type: %s (%s)".format(sym, sym.accurateKindString)) - var name = newTermName(nme.REIFY_FREE_PREFIX + sym.name) - val phantomTypeTag = Apply(TypeApply(Select(Ident(nme.UNIVERSE_SHORT), nme.TypeTag), List(value)), List(Literal(Constant(null)), Literal(Constant(null)))) - val flavor = if (sym.isExistential) nme.newFreeExistential else nme.newFreeType - (name, mirrorBuildCall(flavor, reify(sym.name.toString), reify(sym.info), phantomTypeTag, mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(origin(sym)))) + state.reificationIsConcrete = false + val name = newTermName(nme.REIFY_FREE_PREFIX + sym.name) + Reification(name, binding, mirrorBuildCall(nme.newFreeType, reify(sym.name.toString), mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(origin(sym)))) } def reifySymDef(sym: Symbol): Tree = - reifyIntoSymtab(sym) { + reifyIntoSymtab(sym) { sym => if (reifyDebug) println("Sym def: %s (%s)".format(sym, sym.accurateKindString)) - assert(!sym.isLocatable, sym) // if this assertion fires, then tough type reification needs to be rethought - sym.owner.ownersIterator find (!_.isLocatable) foreach reifySymDef - var name = newTermName(nme.REIFY_SYMDEF_PREFIX + sym.name) - (name, mirrorBuildCall(nme.newNestedSymbol, reify(sym.owner), reify(sym.name), reify(sym.pos), mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(sym.isClass))) + val name = newTermName(nme.REIFY_SYMDEF_PREFIX + sym.name) + def reifiedOwner = if (sym.owner.isLocatable) reify(sym.owner) else reifySymDef(sym.owner) + Reification(name, Ident(sym), mirrorBuildCall(nme.newNestedSymbol, reifiedOwner, reify(sym.name), reify(sym.pos), mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(sym.isClass))) } - private def reifyIntoSymtab(sym: Symbol)(reificode: => (TermName, Tree)): Tree ={ + case class Reification(name: Name, binding: Tree, tree: Tree) + + private def reifyIntoSymtab(sym: Symbol)(reificode: Symbol => Reification): Tree = { def fromSymtab = symtab symRef sym if (fromSymtab == EmptyTree) { - val reification = reificode - state.symtab += (sym, reification._1, reification._2) + // reification is lazy, so that we can carefully choose where to evaluate it + // and we choose this place to be exactly here: + // + // reasons: + // 1) reification happens at maximum once per symbol to prevent repeated reifications + // 2) reification happens before putting the symbol itself into the symbol table to ensure correct initialization order: + // for example, if reification of symbol A refers to reification of symbol B + // (this might happen when we're doing `reifySymDef`, which expands into `newNestedSymbol`, which needs `sym.owner`) + // then we have to put reification-B into the symbol table before reification-A + // so that subsequent code generation that traverses the symbol table in the first-added first-codegenned order + // produces valid Scala code (with vals in a block depending only on lexically preceding vals) + val reification = reificode(sym) + import reification.{name, binding} + val tree = reification.tree addAttachment ReifyBindingAttachment(binding) + state.symtab += (sym, name, tree) } fromSymtab } |