CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-scala-lang--scala3-compiler-3

The Scala 3 compiler (Dotty) providing advanced type inference, metaprogramming capabilities, and enhanced performance for modern Scala development

Overview
Eval results
Files

context-management.mddocs/

Context Management

Context system providing compilation state management, phase execution control, and configuration handling throughout the Scala 3 compilation pipeline.

Capabilities

Context Core API

Central context management providing access to compilation state, settings, and execution control.

/**
 * Context management for compilation phases
 * Provides access to compilation state, settings, and phase information
 */
object Contexts {
  /**
   * Get current compilation context
   * Implicit context parameter pattern for context threading
   * @param ctx Implicit context parameter
   * @return The provided context (identity function for context threading)
   */
  def ctx(using ctx: Context): Context
  
  /**
   * Run operation with given context
   * Executes the provided operation with a specific context in scope
   * @param c Context to use for the operation
   * @param op Operation to execute with the context
   * @return Result of the operation
   */
  def inContext[T](c: Context)(inline op: Context ?=> T): T
  
  /**
   * Execute operation at specific compilation phase
   * Temporarily switches to the specified phase for the duration of the operation
   * @param phase Target compilation phase
   * @param op Operation to execute at the specified phase
   * @param ctx Current compilation context
   * @return Result of the operation executed at the target phase
   */
  def atPhase[T](phase: Phase)(inline op: Context ?=> T)(using Context): T
  
  /**
   * Execute operation with fresh context
   * Creates a new context with modified settings for the operation
   * @param modifications Context modifications to apply
   * @param op Operation to execute with fresh context
   * @param ctx Base context to derive from
   * @return Result of the operation
   */
  def freshContext[T](modifications: Context => Context)(op: Context ?=> T)(using Context): T
}

Usage Examples:

import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases.Phase

// Basic context usage
def analyzeTree(tree: Tree)(using Context): Unit = {
  val currentPhase = ctx.phase
  println(s"Analyzing tree in phase: ${currentPhase.phaseName}")
}

// Execute at specific phase
def getTypeAtPhase(tree: Tree, targetPhase: Phase)(using Context): Type = {
  atPhase(targetPhase) {
    tree.tpe // Get type as it exists at target phase
  }
}

// Create fresh context with modifications
def compileWithWarnings(source: SourceFile)(using Context): Unit = {
  freshContext(_.fresh.setSetting(ctx.settings.Xfatal_warnings, true)) {
    // Compile with fatal warnings enabled
    compile(source)
  }
}

Context Creation and Management

Context lifecycle management and creation utilities for different compilation scenarios.

/**
 * Context creation and initialization utilities
 */
object ContextOps {
  /**
   * Create initial context for compilation
   * Sets up base context with default settings and definitions
   * @return Freshly initialized compilation context
   */
  def initialContext(): Context
  
  /**
   * Create context for specific compilation unit
   * @param unit Compilation unit to create context for
   * @param base Base context to derive from
   * @return Context configured for the compilation unit
   */
  def contextFor(unit: CompilationUnit, base: Context): Context
  
  /**
   * Create child context with modified phase
   * @param parent Parent context
   * @param newPhase Phase to set in child context
   * @return Child context with updated phase
   */
  def withPhase(parent: Context, newPhase: Phase): Context
  
  /**
   * Create context with additional imports
   * @param base Base context
   * @param imports Additional imports to add to scope
   * @return Context with expanded import scope
   */
  def withImports(base: Context, imports: List[Symbol]): Context
}

Context Properties

Access to context properties including settings, definitions, and phase information.

/**
 * Compilation context containing settings, symbols, and phase information
 * Central state container for compilation process
 */
abstract class Context {
  /**
   * Current compilation settings
   * @return Settings object containing all compiler configuration
   */
  def settings: Settings
  
  /**
   * Built-in definitions and standard library symbols
   * @return Definitions object providing access to standard symbols
   */
  def definitions: Definitions
  
  /**
   * Current compilation phase
   * @return Phase object representing current compilation stage
   */
  def phase: Phase
  
  /**
   * Current compilation run
   * @return Run object managing current compilation execution
   */
  def run: Run
  
  /**
   * Symbol table and scope information
   * @return Current scope containing visible symbols
   */
  def scope: Scope
  
  /**
   * Reporter for diagnostic messages
   * @return Reporter instance for this context
   */
  def reporter: Reporter
  
  /**
   * Source file being compiled (if applicable)
   * @return Optional source file for contexts associated with specific files
   */
  def source: Option[SourceFile]
  
  /**
   * Compilation unit being processed (if applicable)
   * @return Optional compilation unit for contexts associated with specific units
   */
  def compilationUnit: Option[CompilationUnit]
  
  /**
   * Create fresh child context
   * @return New context derived from this context
   */
  def fresh: Context
  
  /**
   * Check if context is in given phase
   * @param phase Phase to check against
   * @return True if context is currently in the specified phase
   */
  def isAfterPhase(phase: Phase): Boolean
  
  /**
   * Get symbol at current scope
   * @param name Name to lookup
   * @return Symbol matching the name, or NoSymbol if not found
   */
  def lookupSymbol(name: Name): Symbol
}

Settings Management

Compiler settings access and modification within contexts.

/**
 * Compiler settings and configuration
 * Provides access to all compilation parameters and flags
 */
abstract class Settings {
  /**
   * Output directory for generated class files
   */
  def outputDir: String
  
  /**
   * Compilation classpath
   */
  def classpath: List[String]
  
  /**
   * Source path for finding source files
   */
  def sourcepath: List[String]
  
  /**
   * Scala language version
   */
  def scalaVersion: String
  
  /**
   * Enable verbose output
   */
  def verbose: Boolean
  
  /**
   * Treat warnings as errors
   */
  def Xfatal_warnings: Boolean
  
  /**
   * Enable experimental features
   */
  def experimental: Boolean
  
  /**
   * Target JVM version
   */
  def target: String
  
  /**
   * Enable specific language features
   */
  def feature: Boolean
  
  /**
   * Show deprecation warnings
   */
  def deprecation: Boolean
  
  /**
   * Enable unchecked warnings
   */
  def unchecked: Boolean
}

Phase Context Operations

Specialized operations for working with phase-specific contexts.

/**
 * Phase-specific context operations
 */
object PhaseContext {
  /**
   * Execute operation in all phases
   * Runs the operation once for each compilation phase
   * @param op Operation to execute in each phase
   * @param ctx Base context
   * @return List of results from each phase
   */
  def inAllPhases[T](op: Context ?=> T)(using Context): List[T]
  
  /**
   * Execute operation from current phase to end
   * @param op Operation to execute
   * @param ctx Current context
   * @return List of results from current phase onwards
   */
  def fromCurrentPhase[T](op: Context ?=> T)(using Context): List[T]
  
  /**
   * Execute operation in phase range
   * @param startPhase Starting phase (inclusive)
   * @param endPhase Ending phase (inclusive)
   * @param op Operation to execute
   * @param ctx Current context
   * @return List of results from the phase range
   */
  def inPhaseRange[T](startPhase: Phase, endPhase: Phase)(op: Context ?=> T)(using Context): List[T]
  
  /**
   * Get previous phase context
   * @param ctx Current context
   * @return Context for the previous phase, or None if at first phase
   */
  def previousPhase(using Context): Option[Context]
  
  /**
   * Get next phase context
   * @param ctx Current context
   * @return Context for the next phase, or None if at last phase
   */
  def nextPhase(using Context): Option[Context]
}

Usage Examples:

import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases._

// Execute in multiple phases
def analyzeAcrossPhases(tree: Tree)(using Context): Unit = {
  val results = PhaseContext.inAllPhases {
    (tree.symbol.name.toString, ctx.phase.phaseName, tree.tpe.toString)
  }
  results.foreach { case (name, phase, tpe) =>
    println(s"$name in $phase: $tpe")
  }
}

// Check phase-specific conditions
def isAfterTyper(using Context): Boolean = {
  ctx.isAfterPhase(typerPhase)
}

// Context-dependent symbol lookup
def findSymbolInCurrentScope(name: String)(using Context): Symbol = {
  val termName = name.toTermName
  ctx.lookupSymbol(termName)
}

Types

Phase

/**
 * Compilation phase with name and execution logic
 */
abstract class Phase {
  def phaseName: String
  def run(using Context): Unit
  def isRunnable(using Context): Boolean
  def description: String
}

Symbol

/**
 * Symbol representing a definition (class, method, value, etc.)
 */
abstract class Symbol {
  def name: Name
  def fullName: String
  def pos: Position
  def flags: FlagSet
  def info: Type
  def owner: Symbol
}

Scope

/**
 * Symbol scope for name resolution
 */
abstract class Scope {
  def lookup(name: Name): Symbol
  def enter(sym: Symbol): Unit
  def symbols: List[Symbol]
  def isEmpty: Boolean
}

Name

/**
 * Name representation for symbols
 */
abstract class Name {
  def toString: String
  def length: Int
  def isEmpty: Boolean
}

/**
 * Term name (for values, methods, objects)
 */
abstract class TermName extends Name

/**
 * Type name (for classes, traits, type aliases)
 */  
abstract class TypeName extends Name

Type

/**
 * Type representation in the type system
 */
abstract class Type {
  def show: String
  def widen: Type
  def dealias: Type
  def symbol: Symbol
}

Install with Tessl CLI

npx tessl i tessl/maven-org-scala-lang--scala3-compiler-3

docs

context-management.md

core-compilation.md

index.md

interactive-compilation.md

java-interfaces.md

plugin-development.md

repl-scripting.md

tile.json