HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Moduleghc-10.0.0.20260917GHC2024

GHC.Driver.Main

Main API for compiling plain Haskell source code.

This module implements compilation of a Haskell source. It is not concerned with preprocessing of source files; this is handled in GHC.Driver.Pipeline

There are various entry points depending on what mode we're in: "batch" mode (--make), "one-shot" mode (-c, -S etc.), and "interactive" mode (GHCi). There are also entry points for individual passes: parsing, typechecking/renaming, desugaring, and simplification.

All the functions here take an HscEnv as a parameter, but none of them return a new one: HscEnv is treated as an immutable value from here on in (although it has mutable components, for the caches).

We use the Hsc monad to deal with warning messages consistently: specifically, while executing within an Hsc monad, warnings are collected. When a Hsc monad returns to an IO monad, the warnings are printed, or compilation aborts if the -Werror flag is enabled.

(c) The GRASP/AQUA Project, Glasgow University, 1993-2000

Contents
  1. Making an HscEnv
  2. Compiling complete source files
  3. Running passes separately
  4. Safe Haskell
  5. Support for interactive evaluation
  6. Low-level exports for hooks
Synopsis

Making an HscEnv

Compiling complete source files

Action to perform in backend compilation

Constructors

Instances1Outputable

If the Linkable contains Core bindings loaded from an interface, replace them with a lazy IO thunk that compiles them to bytecode and foreign objects, using the supplied environment for type checking.

The laziness is necessary because this value is stored purely in a HomeModLinkable in the home package table, rather than some dedicated mutable state that would generate bytecode on demand, so we have to call this function even when we don't know that we'll need the bytecode.

In addition, the laziness has to be hidden inside BCOs because Linkable is used too generally, so that looking at the constructor to decide whether to discard it when linking native code would force the thunk otherwise, incurring a significant performance penalty.

This is sound because generateByteCode just depends on things already loaded in the interface file.

Return an IO that hydrates Core bindings and compiles them to bytecode if the interface contains any, using the supplied type env for typechecking.

Unlike initWholeCoreBindings, this does not use lazy IO. Instead, the IO is only evaluated (in get_link_deps) when it is clear that it will be used immediately (because we're linking TH with -fprefer-byte-code in oneshot mode), and the result is cached in LoaderState.

initWholeCoreBindings needs the laziness because it is used to populate HomeModInfo, which is done preemptively, in anticipation of downstream modules using the bytecode for TH in make mode, which might never happen.

Running passes separately

Safe Haskell

Check that a module is safe to import.

We return True to indicate the import is safe and False otherwise although in the False case an exception may be thrown first.

Support for interactive evaluation

datadata TcRnExprMode

How should we infer a type? See Note [TcRnExprMode]

Constructors

  • TM_Inst

    Instantiate inferred quantifiers only (:type)

  • TM_Default

    Instantiate all quantifiers, and do eager defaulting (:type +d)

Low-level exports for hooks

Deal with errors and warnings returned by a compilation step

In order to reduce dependencies to other parts of the compiler, functions outside the "main" parts of GHC return warnings and errors as a parameter and signal success via by wrapping the result in a Maybe type. This function logs the returned warnings and propagates errors as exceptions (of type SourceError).

This function assumes the following invariants:

  1. If the second result indicates success (is of the form 'Just x'), there must be no error messages in the first result.

  2. If there are no error messages, but the second result indicates failure there should be warnings in the first result. That is, if the action failed, it must have been due to the warnings (i.e., -Werror).

genModDetails is used to initialise ModDetails at the end of compilation. This has two main effects: 1. Increases memory usage by unloading a lot of the TypeEnv 2. Globalising certain parts (DFunIds) in the TypeEnv (which used to be achieved using UpdateIdInfos) For the second part to work, it's critical that we use initIfaceLoadModule here rather than initIfaceCheck as initIfaceLoadModule removes the module from the KnotVars, otherwise name lookups succeed by hitting the old TypeEnv, which missing out the critical globalisation step for DFuns.