HscMain.hs 65.9 KB
Newer Older
dterei's avatar
dterei committed
1 2
-------------------------------------------------------------------------------
--
3
-- | Main API for compiling plain Haskell source code.
4
--
dterei's avatar
dterei committed
5
-- This module implements compilation of a Haskell source. It is
6 7 8 9 10
-- /not/ concerned with preprocessing of source files; this is handled
-- in "DriverPipeline".
--
-- There are various entry points depending on what mode we're in:
-- "batch" mode (@--make@), "one-shot" mode (@-c@, @-S@ etc.), and
dterei's avatar
dterei committed
11
-- "interactive" mode (GHCi). There are also entry points for
12 13 14 15 16 17 18 19 20 21 22 23
-- 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).
--
-- Warning messages are dealt with consistently throughout this API:
-- during compilation warnings are collected, and before any function
-- in @HscMain@ returns, the warnings are either printed, or turned
-- into a real compialtion error if the @-Werror@ flag is enabled.
24
--
dterei's avatar
dterei committed
25 26 27 28
-- (c) The GRASP/AQUA Project, Glasgow University, 1993-2000
--
-------------------------------------------------------------------------------

29
module HscMain
dterei's avatar
dterei committed
30
    (
31 32 33 34 35
    -- * Making an HscEnv
      newHscEnv

    -- * Compiling complete source files
    , Compiler
Thomas Schilling's avatar
Thomas Schilling committed
36
    , HscStatus' (..)
37
    , InteractiveStatus, HscStatus
38 39 40 41 42 43 44 45
    , hscCompileOneShot
    , hscCompileBatch
    , hscCompileNothing
    , hscCompileInteractive
    , hscCompileCmmFile
    , hscCompileCore

    -- * Running passes separately
Thomas Schilling's avatar
Thomas Schilling committed
46 47 48
    , hscParse
    , hscTypecheckRename
    , hscDesugar
Simon Marlow's avatar
Simon Marlow committed
49 50
    , makeSimpleIface
    , makeSimpleDetails
51 52 53 54 55 56 57 58 59 60 61 62
    , hscSimplify -- ToDo, shouldn't really export this

    -- ** Backends
    , hscOneShotBackendOnly
    , hscBatchBackendOnly
    , hscNothingBackendOnly
    , hscInteractiveBackendOnly

    -- * Support for interactive evaluation
    , hscParseIdentifier
    , hscTcRcLookupName
    , hscTcRnGetInfo
63
    , hscCheckSafe
64
#ifdef GHCI
65
    , hscGetModuleInterface
66
    , hscRnImportDecls
67
    , hscTcRnLookupRdrName
vivian's avatar
vivian committed
68
    , hscStmt, hscStmtWithLocation
69
    , hscDecls, hscDeclsWithLocation
vivian's avatar
vivian committed
70
    , hscTcExpr, hscImport, hscKcType
71 72
    , hscCompileCoreExpr
#endif
73
    ) where
74

75
#ifdef GHCI
76
import ByteCodeGen      ( byteCodeGen, coreExprToBCOs )
77
import Linker
78 79
import CoreTidy         ( tidyExpr )
import Type             ( Type )
80
import PrelNames
81 82 83
import {- Kind parts of -} Type         ( Kind )
import CoreLint         ( lintUnfolding )
import DsMeta           ( templateHaskellNames )
84
import VarSet
85
import VarEnv           ( emptyTidyEnv )
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
86
import Panic
87 88

import GHC.Exts
89 90
#endif

91
import Id
92 93
import Module
import Packages
Simon Marlow's avatar
Simon Marlow committed
94 95
import RdrName
import HsSyn
96
import CoreSyn
Ian Lynagh's avatar
Ian Lynagh committed
97
import StringBuffer
98
import Parser
99
import Lexer
100 101
import SrcLoc
import TcRnDriver
102
import TcIface          ( typecheckIface )
103
import TcRnMonad
104 105
import IfaceEnv         ( initNameCache )
import LoadIface        ( ifaceStats, initExternalPackageState )
106
import PrelInfo
Simon Marlow's avatar
Simon Marlow committed
107
import MkIface
108 109
import Desugar
import SimplCore
Simon Marlow's avatar
Simon Marlow committed
110
import TidyPgm
111
import CorePrep
112 113
import CoreToStg        ( coreToStg )
import qualified StgCmm ( codeGen )
114 115
import StgSyn
import CostCentre
116
import ProfInit
117 118
import TyCon
import Name
119 120
import SimplStg         ( stg2stg )
import CodeGen          ( codeGen )
Simon Peyton Jones's avatar
Simon Peyton Jones committed
121
import OldCmm as Old    ( CmmGroup )
122 123
import PprCmm           ( pprCmms )
import CmmParse         ( parseCmmFile )
124
import CmmBuildInfoTables
Edward Z. Yang's avatar
Edward Z. Yang committed
125
import CmmPipeline
126
import CmmInfo
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
127
import OptimizationFuel ( initOptFuelState )
128
import CmmCvt
129
import CodeOutput
130
import NameEnv          ( emptyNameEnv )
131 132
import NameSet          ( emptyNameSet )
import InstEnv
133
import FamInstEnv
Thomas Schilling's avatar
Thomas Schilling committed
134
import Fingerprint      ( Fingerprint )
135

136
import DynFlags
137
import ErrUtils
138
import UniqSupply       ( mkSplitUniqSupply )
139

140
import Outputable
141
import HscStats         ( ppSourceStats )
142
import HscTypes
143
import MkExternalCore   ( emitExternalCore )
144
import FastString
145
import UniqFM           ( emptyUFM )
146
import UniqSupply       ( initUs_ )
147
import Bag
Thomas Schilling's avatar
Thomas Schilling committed
148
import Exception
Simon Marlow's avatar
Simon Marlow committed
149

150
import Data.List
Simon Marlow's avatar
Simon Marlow committed
151
import Control.Monad
152
import Data.Maybe
Simon Marlow's avatar
Simon Marlow committed
153
import Data.IORef
154
import System.FilePath as FilePath
155
import System.Directory
dterei's avatar
dterei committed
156

Thomas Schilling's avatar
Thomas Schilling committed
157
#include "HsVersions.h"
158

159

dterei's avatar
dterei committed
160 161 162 163 164
{- **********************************************************************
%*                                                                      *
                Initialisation
%*                                                                      *
%********************************************************************* -}
165

166
newHscEnv :: DynFlags -> IO HscEnv
167
newHscEnv dflags = do
dterei's avatar
dterei committed
168 169 170 171 172 173 174
    eps_var <- newIORef initExternalPackageState
    us      <- mkSplitUniqSupply 'r'
    nc_var  <- newIORef (initNameCache us knownKeyNames)
    fc_var  <- newIORef emptyUFM
    mlc_var <- newIORef emptyModuleEnv
    optFuel <- initOptFuelState
    safe_var <- newIORef True
175
    return HscEnv {  hsc_dflags       = dflags,
dterei's avatar
dterei committed
176 177
                     hsc_targets      = [],
                     hsc_mod_graph    = [],
178
                     hsc_IC           = emptyInteractiveContext dflags,
dterei's avatar
dterei committed
179 180 181 182 183 184
                     hsc_HPT          = emptyHomePackageTable,
                     hsc_EPS          = eps_var,
                     hsc_NC           = nc_var,
                     hsc_FC           = fc_var,
                     hsc_MLC          = mlc_var,
                     hsc_OptFuel      = optFuel,
dterei's avatar
dterei committed
185
                     hsc_type_env_var = Nothing,
dterei's avatar
dterei committed
186
                     hsc_safeInf      = safe_var }
Thomas Schilling's avatar
Thomas Schilling committed
187

188

189
knownKeyNames :: [Name]      -- Put here to avoid loops involving DsMeta,
190
knownKeyNames =              -- where templateHaskellNames are defined
dterei's avatar
dterei committed
191
    map getName wiredInThings
dterei's avatar
dterei committed
192
        ++ basicKnownKeyNames
193
#ifdef GHCI
dterei's avatar
dterei committed
194
        ++ templateHaskellNames
195 196
#endif

197
-- -----------------------------------------------------------------------------
198
-- The Hsc monad: Passing an enviornment and warning state
199

dterei's avatar
dterei committed
200
newtype Hsc a = Hsc (HscEnv -> WarningMessages -> IO (a, WarningMessages))
201 202

instance Monad Hsc where
dterei's avatar
dterei committed
203 204 205 206
    return a    = Hsc $ \_ w -> return (a, w)
    Hsc m >>= k = Hsc $ \e w -> do (a, w1) <- m e w
                                   case k a of
                                       Hsc k' -> k' e w1
207 208

instance MonadIO Hsc where
dterei's avatar
dterei committed
209
    liftIO io = Hsc $ \_ w -> do a <- io; return (a, w)
210

dterei's avatar
dterei committed
211 212 213
instance Functor Hsc where
    fmap f m = m >>= \a -> return $ f a

214 215
runHsc :: HscEnv -> Hsc a -> IO a
runHsc hsc_env (Hsc hsc) = do
dterei's avatar
dterei committed
216 217 218
    (a, w) <- hsc hsc_env emptyBag
    printOrThrowWarnings (hsc_dflags hsc_env) w
    return a
219

220 221 222 223 224 225 226
-- A variant of runHsc that switches in the DynFlags from the
-- InteractiveContext before running the Hsc computation.
--
runInteractiveHsc :: HscEnv -> Hsc a -> IO a
runInteractiveHsc hsc_env =
  runHsc (hsc_env { hsc_dflags = ic_dflags (hsc_IC hsc_env) })

227
getWarnings :: Hsc WarningMessages
dterei's avatar
dterei committed
228
getWarnings = Hsc $ \_ w -> return (w, w)
229 230

clearWarnings :: Hsc ()
dterei's avatar
dterei committed
231
clearWarnings = Hsc $ \_ _ -> return ((), emptyBag)
232 233

logWarnings :: WarningMessages -> Hsc ()
dterei's avatar
dterei committed
234
logWarnings w = Hsc $ \_ w0 -> return ((), w0 `unionBags` w)
235 236

getHscEnv :: Hsc HscEnv
dterei's avatar
dterei committed
237
getHscEnv = Hsc $ \e w -> return (e, w)
238

239 240
instance HasDynFlags Hsc where
    getDynFlags = Hsc $ \e w -> return (hsc_dflags e, w)
241 242 243

handleWarnings :: Hsc ()
handleWarnings = do
dterei's avatar
dterei committed
244 245 246 247
    dflags <- getDynFlags
    w <- getWarnings
    liftIO $ printOrThrowWarnings dflags w
    clearWarnings
248 249 250 251 252

-- | log warning in the monad, and if there are errors then
-- throw a SourceError exception.
logWarningsReportErrors :: Messages -> Hsc ()
logWarningsReportErrors (warns,errs) = do
dterei's avatar
dterei committed
253 254
    logWarnings warns
    when (not $ isEmptyBag errs) $ throwErrors errs
255 256 257 258

-- | Throw some errors.
throwErrors :: ErrorMessages -> Hsc a
throwErrors = liftIO . throwIO . mkSrcErr
259 260 261 262 263

-- | 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
dterei's avatar
dterei committed
264
-- and signal success via by wrapping the result in a 'Maybe' type. This
265 266 267 268 269 270 271 272 273
-- 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
dterei's avatar
dterei committed
274
--     there should be warnings in the first result. That is, if the action
275 276 277
--     failed, it must have been due to the warnings (i.e., @-Werror@).
ioMsgMaybe :: IO (Messages, Maybe a) -> Hsc a
ioMsgMaybe ioA = do
278
    ((warns,errs), mb_r) <- liftIO ioA
dterei's avatar
dterei committed
279 280 281 282
    logWarnings warns
    case mb_r of
        Nothing -> throwErrors errs
        Just r  -> ASSERT( isEmptyBag errs ) return r
283 284 285 286 287

-- | like ioMsgMaybe, except that we ignore error messages and return
-- 'Nothing' instead.
ioMsgMaybe' :: IO (Messages, Maybe a) -> Hsc (Maybe a)
ioMsgMaybe' ioA = do
dterei's avatar
dterei committed
288 289 290
    ((warns,_errs), mb_r) <- liftIO $ ioA
    logWarnings warns
    return mb_r
291 292 293 294 295 296

-- -----------------------------------------------------------------------------
-- | Lookup things in the compiler's environment

#ifdef GHCI
hscTcRnLookupRdrName :: HscEnv -> RdrName -> IO [Name]
297 298 299
hscTcRnLookupRdrName hsc_env0 rdr_name = runInteractiveHsc hsc_env0 $ do
   hsc_env <- getHscEnv
   ioMsgMaybe $ tcRnLookupRdrName hsc_env rdr_name
300 301 302
#endif

hscTcRcLookupName :: HscEnv -> Name -> IO (Maybe TyThing)
303 304 305
hscTcRcLookupName hsc_env0 name = runInteractiveHsc hsc_env0 $ do
  hsc_env <- getHscEnv
  ioMsgMaybe' $ tcRnLookupName hsc_env name
dterei's avatar
dterei committed
306 307 308
      -- ignore errors: the only error we're likely to get is
      -- "name not found", and the Maybe in the return type
      -- is used to indicate that.
309

310
hscTcRnGetInfo :: HscEnv -> Name -> IO (Maybe (TyThing, Fixity, [ClsInst]))
311 312 313
hscTcRnGetInfo hsc_env0 name = runInteractiveHsc hsc_env0 $ do
  hsc_env <- getHscEnv
  ioMsgMaybe' $ tcRnGetInfo hsc_env name
314 315

#ifdef GHCI
316
hscGetModuleInterface :: HscEnv -> Module -> IO ModIface
317 318 319
hscGetModuleInterface hsc_env0 mod = runInteractiveHsc hsc_env0 $ do
  hsc_env <- getHscEnv
  ioMsgMaybe $ getModuleInterface hsc_env mod
320 321 322

-- -----------------------------------------------------------------------------
-- | Rename some import declarations
323
hscRnImportDecls :: HscEnv -> [LImportDecl RdrName] -> IO GlobalRdrEnv
324 325 326
hscRnImportDecls hsc_env0 import_decls = runInteractiveHsc hsc_env0 $ do
  hsc_env <- getHscEnv
  ioMsgMaybe $ tcRnImportDecls hsc_env import_decls
327 328
#endif

329
-- -----------------------------------------------------------------------------
Simon Marlow's avatar
Simon Marlow committed
330
-- | parse a file, returning the abstract syntax
331

332
hscParse :: HscEnv -> ModSummary -> IO HsParsedModule
333 334 335
hscParse hsc_env mod_summary = runHsc hsc_env $ hscParse' mod_summary

-- internal version, that doesn't fail due to -Werror
336
hscParse' :: ModSummary -> Hsc HsParsedModule
337
hscParse' mod_summary = do
dterei's avatar
dterei committed
338 339 340
    dflags <- getDynFlags
    let src_filename  = ms_hspp_file mod_summary
        maybe_src_buf = ms_hspp_buf  mod_summary
341

dterei's avatar
dterei committed
342 343 344
    --------------------------  Parser  ----------------
    liftIO $ showPass dflags "Parser"
    {-# SCC "Parser" #-} do
345 346 347 348

    -- sometimes we already have the buffer in memory, perhaps
    -- because we needed to parse the imports out of it, or get the
    -- module name.
dterei's avatar
dterei committed
349 350 351
    buf <- case maybe_src_buf of
               Just b  -> return b
               Nothing -> liftIO $ hGetStringBuffer src_filename
352

dterei's avatar
dterei committed
353
    let loc = mkRealSrcLoc (mkFastString src_filename) 1 1
354

dterei's avatar
dterei committed
355 356 357
    case unP parseModule (mkPState dflags buf loc) of
        PFailed span err ->
            liftIO $ throwOneError (mkPlainErrMsg span err)
358

dterei's avatar
dterei committed
359 360 361 362 363 364
        POk pst rdr_module -> do
            logWarningsReportErrors (getMessages pst)
            liftIO $ dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" $
                                   ppr rdr_module
            liftIO $ dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics" $
                                   ppSourceStats False rdr_module
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

            -- To get the list of extra source files, we take the list
            -- that the parser gave us,
            --   - eliminate files beginning with '<'.  gcc likes to use
            --     pseudo-filenames like "<built-in>" and "<command-line>"
            --   - normalise them (elimiante differences between ./f and f)
            --   - filter out the preprocessed source file
            --   - filter out anything beginning with tmpdir
            --   - remove duplicates
            --   - filter out the .hs/.lhs source filename if we have one
            --
            let n_hspp  = FilePath.normalise src_filename
                srcs0 = nub $ filter (not . (tmpDir dflags `isPrefixOf`))
                            $ filter (not . (== n_hspp))
                            $ map FilePath.normalise
                            $ filter (not . (== '<') . head)
                            $ map unpackFS
                            $ srcfiles pst
                srcs1 = case ml_hs_file (ms_location mod_summary) of
                          Just f  -> filter (/= FilePath.normalise f) srcs0
                          Nothing -> srcs0

387 388 389 390 391
            -- sometimes we see source files from earlier
            -- preprocessing stages that cannot be found, so just
            -- filter them out:
            srcs2 <- liftIO $ filterM doesFileExist srcs1

392 393
            return HsParsedModule {
                      hpm_module    = rdr_module,
394
                      hpm_src_files = srcs2
395
                   }
Simon Marlow's avatar
Simon Marlow committed
396

Thomas Schilling's avatar
Thomas Schilling committed
397 398 399
-- XXX: should this really be a Maybe X?  Check under which circumstances this
-- can become a Nothing and decide whether this should instead throw an
-- exception/signal an error.
dterei's avatar
dterei committed
400
type RenamedStuff =
Simon Marlow's avatar
Simon Marlow committed
401
        (Maybe (HsGroup Name, [LImportDecl Name], Maybe [LIE Name],
402
                Maybe LHsDocString))
Simon Marlow's avatar
Simon Marlow committed
403

Thomas Schilling's avatar
Thomas Schilling committed
404
-- | Rename and typecheck a module, additionally returning the renamed syntax
405
hscTypecheckRename :: HscEnv -> ModSummary -> HsParsedModule
406
                   -> IO (TcGblEnv, RenamedStuff)
dterei's avatar
dterei committed
407
hscTypecheckRename hsc_env mod_summary rdr_module = runHsc hsc_env $ do
408
    tc_result <- {-# SCC "Typecheck-Rename" #-}
dterei's avatar
dterei committed
409
                 ioMsgMaybe $
dterei's avatar
dterei committed
410 411
                     tcRnModule hsc_env (ms_hsc_src mod_summary)
                                True rdr_module
Thomas Schilling's avatar
Thomas Schilling committed
412

413 414 415 416 417 418
        -- This 'do' is in the Maybe monad!
    let rn_info = do decl <- tcg_rn_decls tc_result
                     let imports = tcg_rn_imports tc_result
                         exports = tcg_rn_exports tc_result
                         doc_hdr = tcg_doc_hdr tc_result
                     return (decl,imports,exports,doc_hdr)
Thomas Schilling's avatar
Thomas Schilling committed
419

420
    return (tc_result, rn_info)
Simon Marlow's avatar
Simon Marlow committed
421 422

-- | Convert a typechecked module to Core
423
hscDesugar :: HscEnv -> ModSummary -> TcGblEnv -> IO ModGuts
424
hscDesugar hsc_env mod_summary tc_result =
Ian Lynagh's avatar
Ian Lynagh committed
425
    runHsc hsc_env $ hscDesugar' (ms_location mod_summary) tc_result
426

Ian Lynagh's avatar
Ian Lynagh committed
427 428
hscDesugar' :: ModLocation -> TcGblEnv -> Hsc ModGuts
hscDesugar' mod_location tc_result = do
dterei's avatar
dterei committed
429
    hsc_env <- getHscEnv
Simon Marlow's avatar
Simon Marlow committed
430 431
    r <- ioMsgMaybe $
      {-# SCC "deSugar" #-}
Ian Lynagh's avatar
Ian Lynagh committed
432
      deSugar hsc_env mod_location tc_result
433

dterei's avatar
dterei committed
434 435 436 437
    -- always check -Werror after desugaring, this is the last opportunity for
    -- warnings to arise before the backend.
    handleWarnings
    return r
Simon Marlow's avatar
Simon Marlow committed
438

dterei's avatar
dterei committed
439
-- | Make a 'ModIface' from the results of typechecking. Used when
Simon Marlow's avatar
Simon Marlow committed
440 441 442 443
-- not optimising, and the interface doesn't need to contain any
-- unfoldings or other cross-module optimisation info.
-- ToDo: the old interface is only needed to get the version numbers,
-- we should use fingerprint versions instead.
444
makeSimpleIface :: HscEnv -> Maybe ModIface -> TcGblEnv -> ModDetails
445
                -> IO (ModIface,Bool)
446
makeSimpleIface hsc_env maybe_old_iface tc_result details =
dterei's avatar
dterei committed
447
    runHsc hsc_env $ ioMsgMaybe $
dterei's avatar
dterei committed
448
        mkIfaceTc hsc_env (fmap mi_iface_hash maybe_old_iface) details tc_result
Simon Marlow's avatar
Simon Marlow committed
449

dterei's avatar
dterei committed
450
-- | Make a 'ModDetails' from the results of typechecking. Used when
Simon Marlow's avatar
Simon Marlow committed
451
-- typechecking only, as opposed to full compilation.
452 453
makeSimpleDetails :: HscEnv -> TcGblEnv -> IO ModDetails
makeSimpleDetails hsc_env tc_result = mkBootModDetailsTc hsc_env tc_result
Simon Marlow's avatar
Simon Marlow committed
454

455

dterei's avatar
dterei committed
456 457 458 459 460 461 462
{- **********************************************************************
%*                                                                      *
                The main compiler pipeline
%*                                                                      *
%********************************************************************* -}

{-
463 464 465 466
                   --------------------------------
                        The compilation proper
                   --------------------------------

dterei's avatar
dterei committed
467 468 469 470 471 472
It's the task of the compilation proper to compile Haskell, hs-boot and core
files to either byte-code, hard-code (C, asm, LLVM, ect) or to nothing at all
(the module is still parsed and type-checked. This feature is mostly used by
IDE's and the likes). Compilation can happen in either 'one-shot', 'batch',
'nothing', or 'interactive' mode. 'One-shot' mode targets hard-code, 'batch'
mode targets hard-code, 'nothing' mode targets nothing and 'interactive' mode
473
targets byte-code.
dterei's avatar
dterei committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

The modes are kept separate because of their different types and meanings:

 * In 'one-shot' mode, we're only compiling a single file and can therefore
 discard the new ModIface and ModDetails. This is also the reason it only
 targets hard-code; compiling to byte-code or nothing doesn't make sense when
 we discard the result.

 * 'Batch' mode is like 'one-shot' except that we keep the resulting ModIface
 and ModDetails. 'Batch' mode doesn't target byte-code since that require us to
 return the newly compiled byte-code.

 * 'Nothing' mode has exactly the same type as 'batch' mode but they're still
 kept separate. This is because compiling to nothing is fairly special: We
 don't output any interface files, we don't run the simplifier and we don't
 generate any code.

 * 'Interactive' mode is similar to 'batch' mode except that we return the
 compiled byte-code together with the ModIface and ModDetails.

Trying to compile a hs-boot file to byte-code will result in a run-time error.
This is the only thing that isn't caught by the type-system.
-}


-- | Status of a compilation to hard-code or nothing.
Thomas Schilling's avatar
Thomas Schilling committed
500
data HscStatus' a
501
    = HscNoRecomp
Thomas Schilling's avatar
Thomas Schilling committed
502
    | HscRecomp
dterei's avatar
dterei committed
503 504 505 506 507
          (Maybe FilePath) -- Has stub files. This is a hack. We can't compile
                           -- C files here since it's done in DriverPipeline.
                           -- For now we just return True if we want the caller
                           -- to compile them for us.
          a
Thomas Schilling's avatar
Thomas Schilling committed
508

dterei's avatar
dterei committed
509
-- This is a bit ugly. Since we use a typeclass below and would like to avoid
Thomas Schilling's avatar
Thomas Schilling committed
510
-- functional dependencies, we have to parameterise the typeclass over the
dterei's avatar
dterei committed
511 512
-- result type. Therefore we need to artificially distinguish some types. We do
-- this by adding type tags which will simply be ignored by the caller.
513
type HscStatus         = HscStatus' ()
514 515
type InteractiveStatus = HscStatus' (Maybe (CompiledByteCode, ModBreaks))
    -- INVARIANT: result is @Nothing@ <=> input was a boot file
Thomas Schilling's avatar
Thomas Schilling committed
516

517 518 519
type OneShotResult     = HscStatus
type BatchResult       = (HscStatus, ModIface, ModDetails)
type NothingResult     = (HscStatus, ModIface, ModDetails)
Thomas Schilling's avatar
Thomas Schilling committed
520
type InteractiveResult = (InteractiveStatus, ModIface, ModDetails)
521

522 523
-- ToDo: The old interface and module index are only using in 'batch' and
--       'interactive' mode. They should be removed from 'oneshot' mode.
524
type Compiler result =  HscEnv
525
                     -> ModSummary
526
                     -> SourceModified
527 528
                     -> Maybe ModIface  -- Old interface, if available
                     -> Maybe (Int,Int) -- Just (i,n) <=> module i of n (for msgs)
529
                     -> IO result
530

dterei's avatar
dterei committed
531
data HsCompiler a = HsCompiler {
532
    -- | Called when no recompilation is necessary.
533 534
    hscNoRecomp :: ModIface
                -> Hsc a,
535 536

    -- | Called to recompile the module.
537 538
    hscRecompile :: ModSummary -> Maybe Fingerprint
                 -> Hsc a,
539

540 541
    hscBackend :: TcGblEnv -> ModSummary -> Maybe Fingerprint
               -> Hsc a,
542

543
    -- | Code generation for Boot modules.
544 545
    hscGenBootOutput :: TcGblEnv -> ModSummary -> Maybe Fingerprint
                     -> Hsc a,
546 547

    -- | Code generation for normal modules.
548 549
    hscGenOutput :: ModGuts  -> ModSummary -> Maybe Fingerprint
                 -> Hsc a
550 551
  }

552
genericHscCompile :: HsCompiler a
553
                  -> (HscEnv -> Maybe (Int,Int) -> RecompileRequired -> ModSummary -> IO ())
554
                  -> HscEnv -> ModSummary -> SourceModified
Thomas Schilling's avatar
Thomas Schilling committed
555
                  -> Maybe ModIface -> Maybe (Int, Int)
556 557
                  -> IO a
genericHscCompile compiler hscMessage hsc_env
558
                  mod_summary source_modified
559
                  mb_old_iface0 mb_mod_index
dterei's avatar
dterei committed
560 561 562
  = do
    (recomp_reqd, mb_checked_iface)
        <- {-# SCC "checkOldIface" #-}
dterei's avatar
dterei committed
563
           checkOldIface hsc_env mod_summary
dterei's avatar
dterei committed
564 565 566 567 568 569 570
                         source_modified mb_old_iface0
    -- save the interface that comes back from checkOldIface.
    -- In one-shot mode we don't have the old iface until this
    -- point, when checkOldIface reads it from the disk.
    let mb_old_hash = fmap mi_iface_hash mb_checked_iface

    let skip iface = do
571
            hscMessage hsc_env mb_mod_index UpToDate mod_summary
dterei's avatar
dterei committed
572 573 574 575 576 577 578 579 580
            runHsc hsc_env $ hscNoRecomp compiler iface

        compile reason = do
            hscMessage hsc_env mb_mod_index reason mod_summary
            runHsc hsc_env $ hscRecompile compiler mod_summary mb_old_hash

        stable = case source_modified of
                     SourceUnmodifiedAndStable -> True
                     _                         -> False
581 582 583

        -- If the module used TH splices when it was last compiled,
        -- then the recompilation check is not accurate enough (#481)
dterei's avatar
dterei committed
584
        -- and we must ignore it. However, if the module is stable
585
        -- (none of the modules it depends on, directly or indirectly,
dterei's avatar
dterei committed
586
        -- changed), then we *can* skip recompilation. This is why
587 588 589
        -- the SourceModified type contains SourceUnmodifiedAndStable,
        -- and it's pretty important: otherwise ghc --make would
        -- always recompile TH modules, even if nothing at all has
dterei's avatar
dterei committed
590
        -- changed. Stability is just the same check that make is
591 592
        -- doing for us in one-shot mode.

dterei's avatar
dterei committed
593
    case mb_checked_iface of
594
        Just iface | not (recompileRequired recomp_reqd) ->
dterei's avatar
dterei committed
595 596 597 598
            if mi_used_th iface && not stable
                then compile RecompForcedByTH
                else skip iface
        _otherwise ->
599
            compile recomp_reqd
Thomas Schilling's avatar
Thomas Schilling committed
600

601
hscCheckRecompBackend :: HsCompiler a -> TcGblEnv -> Compiler a
dterei's avatar
dterei committed
602 603
hscCheckRecompBackend compiler tc_result hsc_env mod_summary
                      source_modified mb_old_iface _m_of_n
604
  = do
dterei's avatar
dterei committed
605 606 607 608 609 610 611
    (recomp_reqd, mb_checked_iface)
        <- {-# SCC "checkOldIface" #-}
           checkOldIface hsc_env mod_summary
                         source_modified mb_old_iface

    let mb_old_hash = fmap mi_iface_hash mb_checked_iface
    case mb_checked_iface of
612
        Just iface | not (recompileRequired recomp_reqd)
dterei's avatar
dterei committed
613
            -> runHsc hsc_env $
dterei's avatar
dterei committed
614 615 616 617 618
                   hscNoRecomp compiler
                       iface{ mi_globals = Just (tcg_rdr_env tc_result) }
        _otherwise
            -> runHsc hsc_env $
                   hscBackend compiler tc_result mod_summary mb_old_hash
619

620
genericHscRecompile :: HsCompiler a
621
                    -> ModSummary -> Maybe Fingerprint
622
                    -> Hsc a
623
genericHscRecompile compiler mod_summary mb_old_hash
dterei's avatar
dterei committed
624 625 626 627 628
    | ExtCoreFile <- ms_hsc_src mod_summary =
        panic "GHC does not currently support reading External Core files"
    | otherwise = do
        tc_result <- hscFileFrontEnd mod_summary
        hscBackend compiler tc_result mod_summary mb_old_hash
629

630
genericHscBackend :: HsCompiler a
631
                  -> TcGblEnv -> ModSummary -> Maybe Fingerprint
632
                  -> Hsc a
633
genericHscBackend compiler tc_result mod_summary mb_old_hash
dterei's avatar
dterei committed
634 635 636
    | HsBootFile <- ms_hsc_src mod_summary =
        hscGenBootOutput compiler tc_result mod_summary mb_old_hash
    | otherwise = do
Ian Lynagh's avatar
Ian Lynagh committed
637
        guts <- hscDesugar' (ms_location mod_summary) tc_result
dterei's avatar
dterei committed
638
        hscGenOutput compiler guts mod_summary mb_old_hash
Thomas Schilling's avatar
Thomas Schilling committed
639

640 641
compilerBackend :: HsCompiler a -> TcGblEnv -> Compiler a
compilerBackend comp tcg hsc_env ms' _ _mb_old_iface _ =
dterei's avatar
dterei committed
642
    runHsc hsc_env $ hscBackend comp tcg ms' Nothing
643

644 645 646 647
--------------------------------------------------------------
-- Compilers
--------------------------------------------------------------

648
hscOneShotCompiler :: HsCompiler OneShotResult
dterei's avatar
dterei committed
649
hscOneShotCompiler = HsCompiler {
650

651
    hscNoRecomp = \_old_iface -> do
dterei's avatar
dterei committed
652 653 654
        hsc_env <- getHscEnv
        liftIO $ dumpIfaceStats hsc_env
        return HscNoRecomp
655 656 657

  , hscRecompile = genericHscRecompile hscOneShotCompiler

dterei's avatar
dterei committed
658
  , hscBackend = \tc_result mod_summary mb_old_hash -> do
dterei's avatar
dterei committed
659 660 661 662 663
        dflags <- getDynFlags
        case hscTarget dflags of
            HscNothing -> return (HscRecomp Nothing ())
            _otherw    -> genericHscBackend hscOneShotCompiler
                              tc_result mod_summary mb_old_hash
664

665
  , hscGenBootOutput = \tc_result mod_summary mb_old_iface -> do
dterei's avatar
dterei committed
666 667 668
        (iface, changed, _) <- hscSimpleIface tc_result mb_old_iface
        hscWriteIface iface changed mod_summary
        return (HscRecomp Nothing ())
669 670

  , hscGenOutput = \guts0 mod_summary mb_old_iface -> do
dterei's avatar
dterei committed
671 672 673 674 675
        guts <- hscSimplify' guts0
        (iface, changed, _details, cgguts) <- hscNormalIface guts mb_old_iface
        hscWriteIface iface changed mod_summary
        hasStub <- hscGenHardCode cgguts mod_summary
        return (HscRecomp hasStub ())
676
  }
677

Thomas Schilling's avatar
Thomas Schilling committed
678
-- Compile Haskell, boot and extCore in OneShot mode.
679
hscCompileOneShot :: Compiler OneShotResult
680 681
hscCompileOneShot hsc_env mod_summary src_changed mb_old_iface mb_i_of_n
  = do
dterei's avatar
dterei committed
682 683 684 685 686
    -- One-shot mode needs a knot-tying mutable variable for interface
    -- files. See TcRnTypes.TcGblEnv.tcg_type_env_var.
    type_env_var <- newIORef emptyNameEnv
    let mod = ms_mod mod_summary
        hsc_env' = hsc_env{ hsc_type_env_var = Just (mod, type_env_var) }
687

dterei's avatar
dterei committed
688 689 690
    genericHscCompile hscOneShotCompiler
                      oneShotMsg hsc_env' mod_summary src_changed
                      mb_old_iface mb_i_of_n
Thomas Schilling's avatar
Thomas Schilling committed
691

692 693 694
hscOneShotBackendOnly :: TcGblEnv -> Compiler OneShotResult
hscOneShotBackendOnly = compilerBackend hscOneShotCompiler

Thomas Schilling's avatar
Thomas Schilling committed
695 696
--------------------------------------------------------------

697
hscBatchCompiler :: HsCompiler BatchResult
dterei's avatar
dterei committed
698
hscBatchCompiler = HsCompiler {
Thomas Schilling's avatar
Thomas Schilling committed
699

700
    hscNoRecomp = \iface -> do
dterei's avatar
dterei committed
701 702
        details <- genModDetails iface
        return (HscNoRecomp, iface, details)
Thomas Schilling's avatar
Thomas Schilling committed
703

704
  , hscRecompile = genericHscRecompile hscBatchCompiler
Thomas Schilling's avatar
Thomas Schilling committed
705

706 707
  , hscBackend = genericHscBackend hscBatchCompiler

708
  , hscGenBootOutput = \tc_result mod_summary mb_old_iface -> do
dterei's avatar
dterei committed
709 710 711
        (iface, changed, details) <- hscSimpleIface tc_result mb_old_iface
        hscWriteIface iface changed mod_summary
        return (HscRecomp Nothing (), iface, details)
Thomas Schilling's avatar
Thomas Schilling committed
712

713
  , hscGenOutput = \guts0 mod_summary mb_old_iface -> do
dterei's avatar
dterei committed
714 715 716 717 718
        guts <- hscSimplify' guts0
        (iface, changed, details, cgguts) <- hscNormalIface guts mb_old_iface
        hscWriteIface iface changed mod_summary
        hasStub <- hscGenHardCode cgguts mod_summary
        return (HscRecomp hasStub (), iface, details)
719
  }
720

dterei's avatar
dterei committed
721
-- | Compile Haskell, boot and extCore in batch mode.
722
hscCompileBatch :: Compiler (HscStatus, ModIface, ModDetails)
723
hscCompileBatch = genericHscCompile hscBatchCompiler batchMsg
Thomas Schilling's avatar
Thomas Schilling committed
724

725 726 727
hscBatchBackendOnly :: TcGblEnv -> Compiler BatchResult
hscBatchBackendOnly = hscCheckRecompBackend hscBatchCompiler

Thomas Schilling's avatar
Thomas Schilling committed
728 729
--------------------------------------------------------------

730
hscInteractiveCompiler :: HsCompiler InteractiveResult
dterei's avatar
dterei committed
731
hscInteractiveCompiler = HsCompiler {
732
    hscNoRecomp = \iface -> do
dterei's avatar
dterei committed
733 734
        details <- genModDetails iface
        return (HscNoRecomp, iface, details)
Thomas Schilling's avatar
Thomas Schilling committed
735

736
  , hscRecompile = genericHscRecompile hscInteractiveCompiler
Thomas Schilling's avatar
Thomas Schilling committed
737

738 739 740
  , hscBackend = genericHscBackend hscInteractiveCompiler

  , hscGenBootOutput = \tc_result _mod_summary mb_old_iface -> do
dterei's avatar
dterei committed
741 742
        (iface, _changed, details) <- hscSimpleIface tc_result mb_old_iface
        return (HscRecomp Nothing Nothing, iface, details)
Thomas Schilling's avatar
Thomas Schilling committed
743

744
  , hscGenOutput = \guts0 mod_summary mb_old_iface -> do
dterei's avatar
dterei committed
745 746 747
        guts <- hscSimplify' guts0
        (iface, _changed, details, cgguts) <- hscNormalIface guts mb_old_iface
        hscInteractive (iface, details, cgguts) mod_summary
748
  }
749 750 751

-- Compile Haskell, extCore to bytecode.
hscCompileInteractive :: Compiler (InteractiveStatus, ModIface, ModDetails)
752
hscCompileInteractive = genericHscCompile hscInteractiveCompiler batchMsg
Thomas Schilling's avatar
Thomas Schilling committed
753

754 755 756
hscInteractiveBackendOnly :: TcGblEnv -> Compiler InteractiveResult
hscInteractiveBackendOnly = compilerBackend hscInteractiveCompiler

Thomas Schilling's avatar
Thomas Schilling committed
757 758
--------------------------------------------------------------

759
hscNothingCompiler :: HsCompiler NothingResult
dterei's avatar
dterei committed
760
hscNothingCompiler = HsCompiler {
761
    hscNoRecomp = \iface -> do
dterei's avatar
dterei committed
762 763
        details <- genModDetails iface
        return (HscNoRecomp, iface, details)
Simon Marlow's avatar
Simon Marlow committed
764

765
  , hscRecompile = genericHscRecompile hscNothingCompiler
766

767
  , hscBackend = \tc_result _mod_summary mb_old_iface -> do
dterei's avatar
dterei committed
768 769 770
        handleWarnings
        (iface, _changed, details) <- hscSimpleIface tc_result mb_old_iface
        return (HscRecomp Nothing (), iface, details)
771

772 773 774
  , hscGenBootOutput = \_ _ _ ->
        panic "hscCompileNothing: hscGenBootOutput should not be called"

775 776 777
  , hscGenOutput = \_ _ _ ->
        panic "hscCompileNothing: hscGenOutput should not be called"
  }
778

779
-- Type-check Haskell and .hs-boot only (no external core)
780
hscCompileNothing :: Compiler (HscStatus, ModIface, ModDetails)
781
hscCompileNothing = genericHscCompile hscNothingCompiler batchMsg
782

783 784 785
hscNothingBackendOnly :: TcGblEnv -> Compiler NothingResult
hscNothingBackendOnly = compilerBackend hscNothingCompiler

786 787 788 789
--------------------------------------------------------------
-- NoRecomp handlers
--------------------------------------------------------------

790 791 792
genModDetails :: ModIface -> Hsc ModDetails
genModDetails old_iface
  = do
dterei's avatar
dterei committed
793 794 795 796 797
    hsc_env <- getHscEnv
    new_details <- {-# SCC "tcRnIface" #-}
                   liftIO $ initIfaceCheck hsc_env (typecheckIface old_iface)
    liftIO $ dumpIfaceStats hsc_env
    return new_details
798

799 800 801 802
--------------------------------------------------------------
-- Progress displayers.
--------------------------------------------------------------

803 804
oneShotMsg :: HscEnv -> Maybe (Int,Int) -> RecompileRequired -> ModSummary
            -> IO ()
805
oneShotMsg hsc_env _mb_mod_index recomp _mod_summary =
dterei's avatar
dterei committed
806
    case recomp of
807
        UpToDate ->
808 809
            compilationProgressMsg (hsc_dflags hsc_env) $
                   "compilation IS NOT required"
dterei's avatar
dterei committed
810
        _other ->
811 812
            return ()

813 814
batchMsg :: HscEnv -> Maybe (Int,Int) -> RecompileRequired -> ModSummary
         -> IO ()
dterei's avatar
dterei committed
815 816
batchMsg hsc_env mb_mod_index recomp mod_summary =
    case recomp of
817 818 819
        MustCompile -> showMsg "Compiling " ""
        UpToDate
            | verbosity (hsc_dflags hsc_env) >= 2 -> showMsg "Skipping  " ""
dterei's avatar
dterei committed
820
            | otherwise -> return ()
821 822
        RecompBecause reason -> showMsg "Compiling " (" [" ++ reason ++ "]")
        RecompForcedByTH -> showMsg "Compiling " " [TH]"
dterei's avatar
dterei committed
823
    where
824
        showMsg msg reason =
dterei's avatar
dterei committed
825 826 827
            compilationProgressMsg (hsc_dflags hsc_env) $
            (showModuleIndex mb_mod_index ++
            msg ++ showModMsg (hscTarget (hsc_dflags hsc_env))
828 829
                              (recompileRequired recomp) mod_summary)
                ++ reason
830

831 832 833
--------------------------------------------------------------
-- FrontEnds
--------------------------------------------------------------
834 835

hscFileFrontEnd :: ModSummary -> Hsc TcGblEnv
836
hscFileFrontEnd mod_summary = do
837
    hpm <- hscParse' mod_summary
838
    hsc_env <- getHscEnv
839
    dflags  <- getDynFlags
dterei's avatar
dterei committed
840 841 842
    tcg_env <-
        {-# SCC "Typecheck-Rename" #-}
        ioMsgMaybe $
843
            tcRnModule hsc_env (ms_hsc_src mod_summary) False hpm
844
    tcSafeOK <- liftIO $ readIORef (tcg_safeInfer tcg_env)
845 846

    -- end of the Safe Haskell line, how to respond to user?
847
    if not (safeHaskellOn dflags) || (safeInferOn dflags && not tcSafeOK)
848 849 850 851 852 853 854 855 856