|
| 1 | +package dotty.tools.dotc |
| 2 | +package interpreter |
| 3 | + |
| 4 | +import dotty.tools.dotc.ast.tpd |
| 5 | +import dotty.tools.dotc.ast.Trees._ |
| 6 | +import dotty.tools.dotc.core.Constants._ |
| 7 | +import dotty.tools.dotc.core.Contexts._ |
| 8 | +import dotty.tools.dotc.core.Flags._ |
| 9 | +import dotty.tools.dotc.core.Decorators._ |
| 10 | +import dotty.tools.dotc.core.Symbols._ |
| 11 | + |
| 12 | +import scala.reflect.ClassTag |
| 13 | +import java.net.URLClassLoader |
| 14 | + |
| 15 | +/** Tree interpreter that can interpret |
| 16 | + * * Literal constants |
| 17 | + * * Calls to static methods |
| 18 | + * * New objects with explicit `new` keyword |
| 19 | + * * Quoted code |
| 20 | + * |
| 21 | + * The interpreter assumes that all calls in the trees are to code that was |
| 22 | + * previously compiled and is present in the classpath of the current context. |
| 23 | + */ |
| 24 | +class Interpreter(implicit ctx: Context) { |
| 25 | + import tpd._ |
| 26 | + |
| 27 | + private[this] val classLoader = { |
| 28 | + val urls = ctx.settings.classpath.value.split(':').map(cp => java.nio.file.Paths.get(cp).toUri.toURL) |
| 29 | + new URLClassLoader(urls, getClass.getClassLoader) |
| 30 | + } |
| 31 | + |
| 32 | + /** Returns the interpreted result of interpreting the code represented by the tree. |
| 33 | + * Return Some of the result or None if some error happen during the interpretation. |
| 34 | + */ |
| 35 | + def interpretTree[T](tree: Tree)(implicit ct: ClassTag[T]): Option[T] = { |
| 36 | + try { |
| 37 | + interpretTreeImpl(tree) match { |
| 38 | + case obj: T => Some(obj) |
| 39 | + case obj => |
| 40 | + ctx.error(s"Interpreted tree returned a result of an unexpected type. Expected ${ct.runtimeClass} but was ${obj.getClass}", tree.pos) |
| 41 | + throw new StopInterpretation |
| 42 | + } |
| 43 | + } catch { |
| 44 | + case _: StopInterpretation => None |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** Returns the interpreted result of interpreting the code represented by the tree. |
| 49 | + * Returns the result of the interpreted tree. |
| 50 | + * |
| 51 | + * If some error is encountered while interpreting a ctx.error is emited and a StopInterpretation is thrown. |
| 52 | + */ |
| 53 | + private def interpretTreeImpl(tree: Tree): Object = { |
| 54 | + try { |
| 55 | + tree match { |
| 56 | + case Apply(_, quote :: Nil) if tree.symbol eq defn.quoteMethod => |
| 57 | + new RawExpr(quote) |
| 58 | + case TypeApply(_, quote :: Nil) if tree.symbol eq defn.typeQuoteMethod => |
| 59 | + new RawType(quote) |
| 60 | + |
| 61 | + case Literal(Constant(c)) => |
| 62 | + c.asInstanceOf[AnyRef] |
| 63 | + |
| 64 | + case Apply(fn, args) if fn.symbol.isConstructor => |
| 65 | + val cls = fn.symbol.owner |
| 66 | + val clazz = classLoader.loadClass(cls.symbol.fullName.toString) |
| 67 | + val paramClasses = paramsSig(fn.symbol) |
| 68 | + val args1: List[Object] = args.map(arg => interpretTreeImpl(arg)) |
| 69 | + clazz.getConstructor(paramClasses: _*).newInstance(args1: _*).asInstanceOf[Object] |
| 70 | + |
| 71 | + case Apply(fun, args) if fun.symbol.isStatic => |
| 72 | + val clazz = classLoader.loadClass(fun.symbol.owner.companionModule.fullName.toString) |
| 73 | + val paramClasses = paramsSig(fun.symbol) |
| 74 | + val args1: List[Object] = args.map(arg => interpretTreeImpl(arg)) |
| 75 | + val method = clazz.getMethod(fun.symbol.name.toString, paramClasses: _*) |
| 76 | + method.invoke(null, args1: _*) |
| 77 | + |
| 78 | + case tree: RefTree if tree.symbol.isStatic => |
| 79 | + val clazz = classLoader.loadClass(tree.symbol.owner.companionModule.fullName.toString) |
| 80 | + val method = clazz.getMethod(tree.name.toString) |
| 81 | + method.invoke(null) |
| 82 | + |
| 83 | + case tree: RefTree if tree.symbol.is(Module) => |
| 84 | + ??? // TODO |
| 85 | + |
| 86 | + case Inlined(_, bindings, expansion) => |
| 87 | + if (bindings.nonEmpty) ??? // TODO evaluate bindings and add environment |
| 88 | + interpretTreeImpl(expansion) |
| 89 | + case _ => |
| 90 | + val msg = |
| 91 | + if (tree.tpe.derivesFrom(defn.QuotedExprClass)) "Quote needs to be explicit or a call to a static method" |
| 92 | + else "Value needs to be a explicit or a call to a static method" |
| 93 | + ctx.error(msg, tree.pos) |
| 94 | + throw new StopInterpretation |
| 95 | + } |
| 96 | + } catch { |
| 97 | + case ex: NoSuchMethodException => |
| 98 | + ctx.error("Could not find interpreted method in classpath: " + ex.getMessage, tree.pos) |
| 99 | + throw new StopInterpretation |
| 100 | + case ex: ClassNotFoundException => |
| 101 | + ctx.error("Could not find interpreted class in classpath: " + ex.getMessage, tree.pos) |
| 102 | + throw new StopInterpretation |
| 103 | + case ex: RuntimeException => |
| 104 | + ex.printStackTrace() |
| 105 | + ctx.error("A runtime exception occurred while interpreting: " + ex.getMessage, tree.pos) |
| 106 | + throw new StopInterpretation |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** List of classes of the parameters of the signature of `sym` */ |
| 111 | + private def paramsSig(sym: Symbol): List[Class[_]] = { |
| 112 | + sym.signature.paramsSig.map { param => |
| 113 | + val paramString = param.toString |
| 114 | + if (paramString == defn.BooleanClass.showFullName) classOf[Boolean] |
| 115 | + else if (paramString == defn.ByteClass.showFullName) classOf[Byte] |
| 116 | + else if (paramString == defn.CharClass.showFullName) classOf[Char] |
| 117 | + else if (paramString == defn.ShortClass.showFullName) classOf[Short] |
| 118 | + else if (paramString == defn.IntClass.showFullName) classOf[Int] |
| 119 | + else if (paramString == defn.LongClass.showFullName) classOf[Long] |
| 120 | + else if (paramString == defn.DoubleClass.showFullName) classOf[Float] |
| 121 | + else if (paramString == defn.DoubleClass.showFullName) classOf[Double] |
| 122 | + else classLoader.loadClass(paramString) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** Exception that stops interpretation if some issue is found */ |
| 127 | + private class StopInterpretation extends Exception |
| 128 | + |
| 129 | +} |
0 commit comments