<commit_before>package ml.wolfe.macros

import scala.language.existentials
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
import scala.reflect.macros.blackbox.Context


/**
 * An inliner takes a tree, inlines functions to anonymous functions and performs beta reduction (i.e. replaces
 * function calls of anonymous functions with the right hand side of the anonymous function where parameters are
 * replaced with the function application arguments.
 */
trait CodeRepository[C <: blackbox.Context] extends HasContext[C] with Transformers[C] with SymbolRepository[C] {

  import context.universe._

  //find inlineable definitions
  lazy val definitions = context.enclosingUnit.body.filter(_.symbol != NoSymbol).collect({
    case d: DefDef => d.symbol -> d
    //    case v: ValDef if !v.symbol.isParameter =>
    //      v.symbol -> DefDef(v.mods, v.name, Nil, Nil, v.tpt, v.rhs)
  }).toMap

  def get(symbol: Symbol) = {
    if (symbol == NoSymbol) None else definitions.get(symbol) match {
      case Some(d) => Some(d)
      case None =>
        val owner = symbol.owner
        owner.annotations.find(_.tpe.typeSymbol == wolfeSymbols.inlinable) match {
          case Some(inlinable) =>
            val Literal(Constant(TypeRef(_, sym, _))) = inlinable.scalaArgs.head
            val loader = getClass.getClassLoader
            val loaded = loader.loadClass(sym.fullName)
            val instance = loaded.newInstance()
            val shortName = symbol.name.encoded
            val methods = loaded.getDeclaredMethods
            val method = methods.find(_.getName == shortName).get
            val params = symbol.asMethod.paramss.map(_.map(p => {
              val vd = ValDef(NoMods, p.name.toTermName, TypeTree(p.typeSignature), EmptyTree)
              vd.symbol = p
              vd
            }))
            val args = params.flatMap(_.map(p => {
              val tree = Ident(p.symbol)
              val expr = context.Expr[Any](tree)
              expr
            }))
            val withContext = context :: args
            val result = method.invoke(instance, withContext: _*).asInstanceOf[context.Expr[Any]]
            val returnType = TypeTree(symbol.asMethod.returnType)
            Some(DefDef(NoMods, symbol.name, Nil, params, returnType, result.tree))
//            None
          case None => None
        }


      //        None<commit_msg>fixed one more macro compiler error<commit_after>package ml.wolfe.macros

import scala.language.existentials
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
import scala.reflect.macros.blackbox.Context


/**
 * An inliner takes a tree, inlines functions to anonymous functions and performs beta reduction (i.e. replaces
 * function calls of anonymous functions with the right hand side of the anonymous function where parameters are
 * replaced with the function application arguments.
 */
trait CodeRepository[C <: blackbox.Context] extends HasContext[C] with Transformers[C] with SymbolRepository[C] {

  import context.universe._

  //find inlineable definitions
  lazy val definitions = context.enclosingUnit.body.filter(_.symbol != NoSymbol).collect({
    case d: DefDef => d.symbol -> d
    //    case v: ValDef if !v.symbol.isParameter =>
    //      v.symbol -> DefDef(v.mods, v.name, Nil, Nil, v.tpt, v.rhs)
  }).toMap

  def get(symbol: Symbol) = {
    if (symbol == NoSymbol) None else definitions.get(symbol) match {
      case Some(d) => Some(d)
      case None =>
        val owner = symbol.owner
        owner.annotations.find(_.tpe.typeSymbol == wolfeSymbols.inlinable) match {
          case Some(inlinable) =>
            val Literal(Constant(TypeRef(_, sym, _))) = inlinable.scalaArgs.head
            val loader = getClass.getClassLoader
            val loaded = loader.loadClass(sym.fullName)
            val instance = loaded.newInstance()
            val shortName = symbol.name.encoded
            val methods = loaded.getDeclaredMethods
            val method = methods.find(_.getName == shortName).get
            val params = symbol.asMethod.paramss.map(_.map(p => {
              val vd = ValDef(NoMods, p.name.toTermName, TypeTree(p.typeSignature), EmptyTree)
              vd.symbol = p
              vd
            }))
            val args = params.flatMap(_.map(p => {
              val tree = Ident(p.symbol)
              val expr = context.Expr[Any](tree)
              expr
            }))
            val withContext = context :: args
            val result = method.invoke(instance, withContext: _*).asInstanceOf[context.Expr[Any]]
            val returnType = TypeTree(symbol.asMethod.returnType)
            Some(DefDef(NoMods, symbol.name.toTermName, Nil, params, returnType, result.tree))
//            None
          case None => None
        }


      //        None
{'fraction_non_alphanumeric': 0.10290781604454527, 'fraction_numerical': 0.0, 'mean_word_length': 2.4178999295278367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '677335', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1337, 'n_words': 358}