summaryrefslogtreecommitdiff
path: root/core/src/main/scala/forge/util/LocalDef.scala
blob: 2a58bbd2d54174c794acc5e8e8856b7ed9b5c969 (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
package forge.util

import scala.reflect.macros.blackbox
import language.experimental.macros
class LocalDef
object LocalDef {
  implicit def default: LocalDef = macro enclosing
  def enclosing(c: blackbox.Context): c.Expr[LocalDef] = {

    import c.universe._
    val current = c.internal.enclosingOwner

    if (
      !current.isMethod ||
      // We can't do this right now because it causes recursive method errors
      // current.asMethod.paramLists.nonEmpty ||
      !(current.owner.isClass || current.owner.isModuleClass)
    ) {
      c.abort(
        c.enclosingPosition,
        "T{} can only be used directly within a zero-arg method defined in a class body"
      )
    }else{

      c.Expr[LocalDef](q"""new forge.util.LocalDef()""")
    }
  }
}