summaryrefslogtreecommitdiff
path: root/src/main/scala/hbt/StaticContext.scala
blob: 60298a3fe9c3659c39735400aab9074b07a13dc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package hbt


import scala.language.experimental.macros
import scala.reflect.macros._

case class StaticContext(value: Boolean)
object StaticContext {
  implicit def default: StaticContext = macro applyImpl
  def rec(c: Context)(expr: c.Symbol): Boolean = {
    import c.universe._
    // Classes and traits and such
    if(!expr.isModuleClass && expr.isClass) false
    // Method contents
    else if(expr.isMethod) false
    else if(expr.owner == NoSymbol) true
    else rec(c)(expr.owner)
  }

  def applyImpl(c: Context): c.Expr[StaticContext] = {
    import c.universe._
    val staticContext = rec(c)(c.internal.enclosingOwner)
    c.Expr[StaticContext](q"hbt.StaticContext($staticContext)")
  }
}
case class DefCtx(staticEnclosing: Option[String])
object DefCtx{
  implicit def default(implicit enc: sourcecode.Enclosing,
                       sc: StaticContext) = {
    if (sc.value) DefCtx(Some(enc.value))
    else DefCtx(None)
  }
}