6

我有一个表面上简单的宏观问题,我一直在努力解决几个小时,但没有运气。也许有更多经验的人可以提供帮助。

我有以下宏:

import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context

object MacroObject {
  def run(s: String): Unit =
    macro runImpl

  def runImpl(c: Context)(s: c.Tree): c.Tree = {
    import c.universe._
    println(s)    // <-- I need the macro to know the value of s at compile time
    q"()"
  }
}

问题是这样的:我希望宏知道s传递给它的值——不是 的 AST s,而是s自身的值。具体来说,我希望它有这种行为:

def runTheMacro(str: String): Unit = MacroObject.run(str)

final val HardCodedString1 = "Hello, world!"
runTheMacro(HardCodedString1)    // the macro should print "Hello, world!"
                                 // to the console during macro expansion

final val HardCodedString2 = "So long!"
runTheMacro(HardCodedString2)    // the macro should print "So long!"
                                 // to the console during macro expansion

保证将传递给的唯一字符串是runTheMacro硬编码的常量值(即,在编译时已知)。

这可能吗,如何做到这一点?

--

编辑:还有以下限制:

  1. 它必须是一个黑盒宏。
  2. 宏签名必须使用c.Trees,而不是c.Expr[_]s(遗留代码;不能更改该部分)
  3. 如果需要,我确实可以使用toolbox宏内部:
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
private val toolbox = currentMirror.mkToolBox()

/** Evaluate the given code fragment at compile time. */
private def eval[A](code: String): A = {
  import scala.reflect.runtime.{universe => u}
  val uTree: u.Tree = toolbox.parse(code)
  toolbox.eval(uTree).asInstanceOf[A]
}
4

1 回答 1

1

eval的是运行时反射eval,编译时宏evalc.eval

"Hello, world!"

final val HardCodedString1 = "Hello, world!"
runTheMacro(HardCodedString1) 

是 的运行时值HardCodedString1

您无法在编译时访问运行时值。

在编译时,字符串树对树的HardCodedString1右侧一无所知val

Scala:在 Context.eval 参考中可以编码什么?

如果您确实需要在程序树中使用运行时值,则必须将其编译推迟到运行时

import scala.reflect.runtime.currentMirror
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox

object MacroObject {
  val toolbox = currentMirror.mkToolBox()

  def run(s: String): Unit = {
    toolbox.eval(q"""
      println($s)
      ()
    """)
  }
}

runTheMacro(HardCodedString1)//Hello, world!
runTheMacro(HardCodedString2)//So long!

或者在编译时,您可以以某种方式找到封闭类的树并在其中查找val树并取其右侧

def runImpl(c: blackbox.Context)(s: c.Tree): c.Tree = {
  import c.universe._

  var rhs: Tree = null

  val traverser = new Traverser {
    override def traverse(tree: Tree): Unit = {
      tree match {
        case q"$mods val $tname: $tpt = $expr" if tname == TermName("HardCodedString1") =>
          rhs = expr
        case _ => ()
      }
      super.traverse(tree)
    }
  }

  traverser.traverse(c.enclosingClass) // deprecated

  val rhsStr =
    if (rhs != null) c.eval[String](c.Expr(c.untypecheck(rhs.duplicate)))
    else c.abort(c.enclosingPosition, "no val HardCodedString1 defined")

  println(rhsStr)

  q"()"
}

runTheMacro(HardCodedString1)//Warning:scalac: Hello, world!

或者对于所有这些变量

def runImpl(c: blackbox.Context)(s: c.Tree): c.Tree = {
  import c.universe._

  val sEvaluated =
    try {
      c.eval[String](c.Expr(c.untypecheck(s.duplicate)))
    } catch {
      case e: IllegalArgumentException if e.getMessage.startsWith("Could not find proxy") =>
        s match {
          case q"$sName" =>
            var rhs: Tree = null

            val traverser = new Traverser {
              override def traverse(tree: Tree): Unit = {
                tree match {
                  case q"$mods val $tname: $tpt = $expr" if tname == sName =>
                    rhs = expr
                  case _ => ()
                }
                super.traverse(tree)
              }
            }

            traverser.traverse(c.enclosingClass)

            if (rhs != null) c.eval[String](c.Expr(c.untypecheck(rhs.duplicate)))
            else c.abort(c.enclosingPosition, s"no val $sName defined")

          case _ => c.abort(c.enclosingPosition, s"unsupported tree $s")
        }

    }

  println(sEvaluated)

  q"()"
}

MacroObject.run(HardCodedString1) //Warning:scalac: Hello, world!
MacroObject.run(HardCodedString2) //Warning:scalac: So long!

runTheMacro在这种情况下将不起作用:Error: no val str defined. 为了让它工作,你也可以把它变成一个宏

def runTheMacro(str: String): Unit = macro runTheMacroImpl

def runTheMacroImpl(c: blackbox.Context)(str: c.Tree): c.Tree = {
  import c.universe._
  q"MacroObject.run($str)"
}

runTheMacro(HardCodedString1) //Warning:scalac: Hello, world!
runTheMacro(HardCodedString2) //Warning:scalac: So long!
于 2019-12-18T21:08:40.480 回答