summaryrefslogtreecommitdiff
path: root/src/library/scala/reflect/makro/Context.scala
blob: 96a41377b3a736975da2a06fd8be9e35edda3098 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package scala.reflect.makro

// todo. introduce context hierarchy
// the most lightweight context should just expose the stuff from the SIP
// the full context should include all traits from scala.reflect.makro (and probably reside in scala-compiler.jar)

trait Context extends Aliases
                 with CapturedVariables
                 with Enclosures
                 with Infrastructure
                 with Names
                 with Reifiers
                 with Reporters
                 with Settings
                 with Symbols
                 with Typers
                 with Util {

  /** The mirror that corresponds to the compile-time universe */
  val mirror: scala.reflect.api.Universe

  /** The type of the prefix tree from which the macro is selected */
  type PrefixType

  /** The prefix tree from which the macro is selected */
  val prefix: Expr[PrefixType]

  /** Alias to the underlying mirror's reify */
  def reify[T](expr: T): Expr[T] = macro Context.reify[T]
}

object Context {
  def reify[T](cc: Context{ type PrefixType = Context })(expr: cc.Expr[T]): cc.Expr[cc.prefix.value.Expr[T]] = {
    import cc.mirror._
    // [Eugene] how do I typecheck this without undergoing this tiresome (and, in general, incorrect) procedure?
    val prefix: Tree = Select(cc.prefix, newTermName("mirror"))
    val prefixTpe = cc.typeCheck(TypeApply(Select(prefix, newTermName("asInstanceOf")), List(SingletonTypeTree(prefix)))).tpe
    prefix setType prefixTpe
    try cc.reifyTree(prefix, expr)
    catch {
      case ex: Throwable =>
        // [Eugene] cannot pattern match on an abstract type, so had to do this
        if (ex.getClass.toString.endsWith("$ReificationError")) {
          ex match {
            case cc.ReificationError(pos, msg) =>
              cc.error(pos, msg)
              EmptyTree
          }
        } else if (ex.getClass.toString.endsWith("$UnexpectedReificationError")) {
          ex match {
            case cc.UnexpectedReificationError(pos, err, cause) =>
              if (cause != null) throw cause else throw ex
          }
        } else {
          throw ex
        }
    }
  }
}