
com.avsystem.commons.macros.misc.MiscMacros.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-macros_2.12.0-RC2 Show documentation
Show all versions of commons-macros_2.12.0-RC2 Show documentation
AVSystem commons library for Scala
The newest version!
package com.avsystem.commons
package macros.misc
import com.avsystem.commons.macros.AbstractMacroCommons
import scala.reflect.macros.{TypecheckException, blackbox}
/**
* Author: ghik
* Created: 19/09/16.
*/
class MiscMacros(ctx: blackbox.Context) extends AbstractMacroCommons(ctx) {
class C[+A, +B <: A]
val aa: C[Any, Any] = new C[String, String]
import c.universe._
def infer[T: c.WeakTypeTag](clue: Tree): Tree =
unwrapAndAddClue(clue)(c.typecheck(q"implicitly[${weakTypeOf[T]}]"))
def inferNonMacro[T: c.WeakTypeTag](clue: Tree): Tree =
unwrapAndAddClue(clue)(c.typecheck(q"implicitly[${weakTypeOf[T]}]", withMacrosDisabled = true))
private def unwrapAndAddClue(clueTree: Tree)(expr: => Tree): Tree = clueTree match {
case Literal(Constant(clue: String)) =>
val wrapped = try expr catch {
case TypecheckException(_, msg) => abort(clue + msg)
}
wrapped match {
case Apply(_, List(arg)) => arg
case tree => tree
}
case _ => abort(s"clue must be a String literal, $clueTree is not")
}
def sourceInfo: Tree = {
def enclosingSymName(sym: Symbol) =
sym.filter(_.isTerm).map(_.asTerm.getter).orElse(sym).name.decodedName.toString
val pos = c.enclosingPosition
q"""
$CommonsPackage.misc.SourceInfo(
${pos.source.path},
${pos.source.file.name},
${pos.point},
${pos.line},
${pos.column},
${pos.source.lineToString(pos.line - 1)},
$ListObj(..${ownerChain.takeWhile(_ != rootMirror.RootClass).map(enclosingSymName)})
)
"""
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy