alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (MacroRuntimes.scala)

This example Scala source code file (MacroRuntimes.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

any, is_error, javareflectionresolvers, javareflectionruntimes, macro, macroargs, macroruntime, macroruntimeresolver, macroruntimes, reflection, runtime, symbol, tree

The MacroRuntimes.scala Scala example source code

package scala.reflect.macros
package runtime

import scala.reflect.internal.Flags._
import scala.reflect.runtime.ReflectionUtils

trait MacroRuntimes extends JavaReflectionRuntimes {
  self: scala.tools.nsc.typechecker.Analyzer =>

  import global._
  import definitions._

  /** Produces a function that can be used to invoke macro implementation for a given macro definition:
   *    1) Looks up macro implementation symbol in this universe.
   *    2) Loads its enclosing class from the macro classloader.
   *    3) Loads the companion of that enclosing class from the macro classloader.
   *    4) Resolves macro implementation within the loaded companion.
   *
   *  @return Requested runtime if macro implementation can be loaded successfully from either of the mirrors,
   *          `null` otherwise.
   */
  def macroRuntime(expandee: Tree): MacroRuntime = pluginsMacroRuntime(expandee)

  /** Default implementation of `macroRuntime`.
   *  Can be overridden by analyzer plugins (see AnalyzerPlugins.pluginsMacroRuntime for more details)
   */
  private val macroRuntimesCache = perRunCaches.newWeakMap[Symbol, MacroRuntime]
  def standardMacroRuntime(expandee: Tree): MacroRuntime = {
    val macroDef = expandee.symbol
    macroLogVerbose(s"looking for macro implementation: $macroDef")
    if (fastTrack contains macroDef) {
      macroLogVerbose("macro expansion is serviced by a fast track")
      fastTrack(macroDef)
    } else {
      macroRuntimesCache.getOrElseUpdate(macroDef, new MacroRuntimeResolver(macroDef).resolveRuntime())
    }
  }

  /** Macro classloader that is used to resolve and run macro implementations.
   *  Loads classes from from -cp (aka the library classpath).
   *  Is also capable of detecting REPL and reusing its classloader.
   *
   *  When -Xmacro-jit is enabled, we sometimes fallback to on-the-fly compilation of macro implementations,
   *  which compiles implementations into a virtual directory (very much like REPL does) and then conjures
   *  a classloader mapped to that virtual directory.
   */
  lazy val defaultMacroClassloader: ClassLoader = findMacroClassLoader()

  /** Abstracts away resolution of macro runtimes.
   */
  type MacroRuntime = MacroArgs => Any
  class MacroRuntimeResolver(val macroDef: Symbol) extends JavaReflectionResolvers {
    val binding = loadMacroImplBinding(macroDef).get
    val isBundle = binding.isBundle
    val className = binding.className
    val methName = binding.methName

    def resolveRuntime(): MacroRuntime = {
      if (className == Predef_???.owner.javaClassName && methName == Predef_???.name.encoded) {
        args => throw new AbortMacroException(args.c.enclosingPosition, "macro implementation is missing")
      } else {
        try {
          macroLogVerbose(s"resolving macro implementation as $className.$methName (isBundle = $isBundle)")
          macroLogVerbose(s"classloader is: ${ReflectionUtils.show(defaultMacroClassloader)}")
          resolveJavaReflectionRuntime(defaultMacroClassloader)
        } catch {
          case ex: Exception =>
            macroLogVerbose(s"macro runtime failed to load: ${ex.toString}")
            macroDef setFlag IS_ERROR
            null
        }
      }
    }
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala MacroRuntimes.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.