Source code position macros in Dotty

So I’ve got some simple debug macros at the bottom of my module stack. Creating a dotty version of this file would seem like the logical first step to cross compiling for Dotty and 2.13. Are then any guides or tutorials for this? Here’s the Scala 2 file:

package ostrat
import language.experimental.macros, reflect.macros.blackbox.Context

/** Macro function object, prints out string preceded by source code position. */
object deb
{
  /** Simple macro, prints out string preceded by source code position. */
  def apply(str: String): Unit = macro debImpl
   
  def debImpl(c: Context)(str: c.Expr[String]): c.Expr[Unit] = 
  { import c.universe._     

    val s1 = macroPosn(c)
    val Predef_println = typeOf[Predef.type].decl(TermName("println"))
    c.Expr(q"""$Predef_println($s1 + ": " + $str)""")
  }  
   
  def macroPosn(c: Context): String =
  { import c.universe._
    val posn: Position  = c.macroApplication.pos
    val fileName = posn.source.toString
    val lineNum: String = posn.line.toString
    val column: String = posn.column.toString
    fileName + "." + lineNum + "." + column
  }

  def str: String = macro strImpl

  def strImpl(c: Context): c.Expr[String] =
  { import c.universe._
    val s1 = macroPosn(c)
    c.Expr(q"""$s1""")
  }
}

/** Macro function object, prints source code position. */
object debb
{
  /** Simplest Macro shows source code position. Must include parenthesis debb(). Without the parenthesis the macro will not print. */
  def apply(): Unit = macro debbImpl
  
  def debbImpl(c: Context)(): c.Expr[Unit] = 
  { import c.universe._
    val s1 = deb.macroPosn(c)
    val Predef_println = typeOf[Predef.type].decl(TermName("println"))
    c.Expr(q"""$Predef_println($s1)""")
  }  
}

/** Macro function object, Prints out source code position followed by expression name, followed by expression value. */
object debvar
{
  /** An expression debug macro. Prints out source code position followed by expression name, followed by expression value. */
  def apply(inputExpr: Any): Unit = macro debvarImpl  
   
  /** Simple macro, prints out source code position, variable name, variable value */
  def debvarImpl(c: Context)(inputExpr: c.Expr[Any]): c.Expr[Unit] =
  {
    import c.universe._      
    val name1: String = show(inputExpr.tree).reverse.takeWhile(_ != '.').reverse
    val str: String = deb.macroPosn(c) + ". " + name1 + " = "
    val Predef_println: Symbol = typeOf[Predef.type].decl(TermName("println"))
    val tree: c.Tree = q"""$Predef_println($str + $inputExpr)"""
    c.Expr(tree)      
  }
}
1 Like

This doesn’t answer your question, but I hope it’s possible because it has some really useful applications. One I have is 0 overhead source locations in logging, which otherwise is very expensive due to creating a stack trace. This was in log4j2.

Not a direct answer, but I believe folks are working on porting Haoyi’s sourcecode library to Dotty. I suspect you could find useful inspiration there…