Browse files

refactors handling of macros in repl

Macros now have a dedicated member handler, so that the logic of their
processing doesn't get mixed up with vanilla DefHandler.

I've also factored out an abstract MacroHandler to provides a basis
to build the upcoming type macro handler upon.
  • Loading branch information...
1 parent 66acf36 commit fa4531e9f28a0e49fdb63406307826c57b19f4bb @xeno-by xeno-by committed Jan 5, 2013
View
38 src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala
@@ -48,16 +48,19 @@ trait MemberHandlers {
}
}
+ private def isTermMacro(ddef: DefDef): Boolean = ddef.mods.isMacro
+
def chooseHandler(member: Tree): MemberHandler = member match {
- case member: DefDef => new DefHandler(member)
- case member: ValDef => new ValHandler(member)
- case member: ModuleDef => new ModuleHandler(member)
- case member: ClassDef => new ClassHandler(member)
- case member: TypeDef => new TypeAliasHandler(member)
- case member: Assign => new AssignHandler(member)
- case member: Import => new ImportHandler(member)
- case DocDef(_, documented) => chooseHandler(documented)
- case member => new GenericHandler(member)
+ case member: DefDef if isTermMacro(member) => new TermMacroHandler(member)
+ case member: DefDef => new DefHandler(member)
+ case member: ValDef => new ValHandler(member)
+ case member: ModuleDef => new ModuleHandler(member)
+ case member: ClassDef => new ClassHandler(member)
+ case member: TypeDef => new TypeAliasHandler(member)
+ case member: Assign => new AssignHandler(member)
+ case member: Import => new ImportHandler(member)
+ case DocDef(_, documented) => chooseHandler(documented)
+ case member => new GenericHandler(member)
}
sealed abstract class MemberDefHandler(override val member: MemberDef) extends MemberHandler(member) {
@@ -122,14 +125,23 @@ trait MemberHandlers {
}
class DefHandler(member: DefDef) extends MemberDefHandler(member) {
- private def vparamss = member.vparamss
- private def isMacro = member.symbol hasFlag MACRO
- // true if not a macro and 0-arity
- override def definesValue = !isMacro && flattensToEmpty(vparamss)
+ override def definesValue = flattensToEmpty(member.vparamss) // true if 0-arity
override def resultExtractionCode(req: Request) =
if (mods.isPublic) codegenln(name, ": ", req.typeOf(name)) else ""
}
+ abstract class MacroHandler(member: DefDef) extends MemberDefHandler(member) {
+ override def definesValue = false
+ override def definesTerm: Option[TermName] = Some(name.toTermName)
+ override def definesType: Option[TypeName] = None
+ override def resultExtractionCode(req: Request) = if (mods.isPublic) codegenln(notification(req)) else ""
+ def notification(req: Request): String
+ }
+
+ class TermMacroHandler(member: DefDef) extends MacroHandler(member) {
+ def notification(req: Request) = s"defined term macro $name: ${req.typeOf(name)}"
+ }
+
class AssignHandler(member: Assign) extends MemberHandler(member) {
val Assign(lhs, rhs) = member
override lazy val name = newTermName(freshInternalVarName())
View
2 test/files/run/macro-repl-dontexpand.check
@@ -7,6 +7,6 @@ scala> def bar(c: scala.reflect.macros.Context) = ???
bar: (c: scala.reflect.macros.Context)Nothing
scala> def foo = macro bar
-foo: Any
+defined term macro foo: Any
scala>
View
2 test/files/run/t6381.check
@@ -9,7 +9,7 @@ scala> def pos_impl(c: reflect.macros.Context): c.Expr[String] =
pos_impl: (c: scala.reflect.macros.Context)c.Expr[String]
scala> def pos = macro pos_impl
-pos: String
+defined term macro pos: String
scala> pos
res0: String = class scala.reflect.internal.util.RangePosition

0 comments on commit fa4531e

Please sign in to comment.