|
| 1 | +-- This module copies parts of the driver code in GHC.Main.Driver to provide |
| 2 | +-- `hscTypecheckRenameWithDiagnostics`. |
| 3 | +module Development.IDE.GHC.Compat.Driver |
| 4 | + ( hscTypecheckRenameWithDiagnostics |
| 5 | + ) where |
| 6 | + |
| 7 | +import GHC.Driver.Main |
| 8 | +import GHC.Driver.Session |
| 9 | +import GHC.Driver.Env |
| 10 | +import GHC.Driver.Errors.Types |
| 11 | +import GHC.Hs |
| 12 | +import GHC.Hs.Dump |
| 13 | +import GHC.Iface.Ext.Ast ( mkHieFile ) |
| 14 | +import GHC.Iface.Ext.Types ( getAsts, hie_asts, hie_module ) |
| 15 | +import GHC.Iface.Ext.Binary ( readHieFile, writeHieFile , hie_file_result) |
| 16 | +import GHC.Iface.Ext.Debug ( diffFile, validateScopes ) |
| 17 | +import GHC.Core |
| 18 | +import GHC.Tc.Module |
| 19 | +import GHC.Tc.Utils.Monad |
| 20 | +import GHC.Unit |
| 21 | +import GHC.Unit.Module.ModDetails |
| 22 | +import GHC.Unit.Module.ModIface |
| 23 | +import GHC.Unit.Module.ModSummary |
| 24 | +import GHC.Types.SourceFile |
| 25 | +import GHC.Types.SrcLoc |
| 26 | +import GHC.Utils.Panic.Plain |
| 27 | +import GHC.Utils.Error |
| 28 | +import GHC.Utils.Outputable |
| 29 | +import GHC.Utils.Logger |
| 30 | +import GHC.Data.FastString |
| 31 | +import GHC.Data.Maybe |
| 32 | +import Control.Monad |
| 33 | + |
| 34 | +-- ----------------------------------------------------------------------------- |
| 35 | +-- | Rename and typecheck a module the same way that GHC does, additionally returning the renamed syntax and the diagnostics produced. |
| 36 | +hscTypecheckRenameWithDiagnostics :: HscEnv -> ModSummary -> HsParsedModule |
| 37 | + -> IO ((TcGblEnv, RenamedStuff), Messages GhcMessage) |
| 38 | +hscTypecheckRenameWithDiagnostics hsc_env mod_summary rdr_module = runHsc' hsc_env $ |
| 39 | + hsc_typecheck True mod_summary (Just rdr_module) |
| 40 | + |
| 41 | +-- | A bunch of logic piled around @tcRnModule'@, concerning a) backpack |
| 42 | +-- b) concerning dumping rename info and hie files. It would be nice to further |
| 43 | +-- separate this stuff out, probably in conjunction better separating renaming |
| 44 | +-- and type checking (#17781). |
| 45 | +hsc_typecheck :: Bool -- ^ Keep renamed source? |
| 46 | + -> ModSummary -> Maybe HsParsedModule |
| 47 | + -> Hsc (TcGblEnv, RenamedStuff) |
| 48 | +hsc_typecheck keep_rn mod_summary mb_rdr_module = do |
| 49 | + hsc_env <- getHscEnv |
| 50 | + let hsc_src = ms_hsc_src mod_summary |
| 51 | + dflags = hsc_dflags hsc_env |
| 52 | + home_unit = hsc_home_unit hsc_env |
| 53 | + outer_mod = ms_mod mod_summary |
| 54 | + mod_name = moduleName outer_mod |
| 55 | + outer_mod' = mkHomeModule home_unit mod_name |
| 56 | + inner_mod = homeModuleNameInstantiation home_unit mod_name |
| 57 | + src_filename = ms_hspp_file mod_summary |
| 58 | + real_loc = realSrcLocSpan $ mkRealSrcLoc (mkFastString src_filename) 1 1 |
| 59 | + keep_rn' = gopt Opt_WriteHie dflags || keep_rn |
| 60 | + massert (isHomeModule home_unit outer_mod) |
| 61 | + tc_result <- if hsc_src == HsigFile && not (isHoleModule inner_mod) |
| 62 | + then ioMsgMaybe $ hoistTcRnMessage $ tcRnInstantiateSignature hsc_env outer_mod' real_loc |
| 63 | + else |
| 64 | + do hpm <- case mb_rdr_module of |
| 65 | + Just hpm -> return hpm |
| 66 | + Nothing -> hscParse' mod_summary |
| 67 | + tc_result0 <- tcRnModule' mod_summary keep_rn' hpm |
| 68 | + if hsc_src == HsigFile |
| 69 | + then do (iface, _) <- liftIO $ hscSimpleIface hsc_env Nothing tc_result0 mod_summary |
| 70 | + ioMsgMaybe $ hoistTcRnMessage $ |
| 71 | + tcRnMergeSignatures hsc_env hpm tc_result0 iface |
| 72 | + else return tc_result0 |
| 73 | + -- TODO are we extracting anything when we merely instantiate a signature? |
| 74 | + -- If not, try to move this into the "else" case above. |
| 75 | + rn_info <- extract_renamed_stuff mod_summary tc_result |
| 76 | + return (tc_result, rn_info) |
| 77 | + |
| 78 | +extract_renamed_stuff :: ModSummary -> TcGblEnv -> Hsc RenamedStuff |
| 79 | +extract_renamed_stuff mod_summary tc_result = do |
| 80 | + let rn_info = getRenamedStuff tc_result |
| 81 | + |
| 82 | + dflags <- getDynFlags |
| 83 | + logger <- getLogger |
| 84 | + liftIO $ putDumpFileMaybe logger Opt_D_dump_rn_ast "Renamer" |
| 85 | + FormatHaskell (showAstData NoBlankSrcSpan NoBlankEpAnnotations rn_info) |
| 86 | + |
| 87 | + -- Create HIE files |
| 88 | + when (gopt Opt_WriteHie dflags) $ do |
| 89 | + -- I assume this fromJust is safe because `-fwrite-hie-file` |
| 90 | + -- enables the option which keeps the renamed source. |
| 91 | + hieFile <- mkHieFile mod_summary tc_result (fromJust rn_info) |
| 92 | + let out_file = ml_hie_file $ ms_location mod_summary |
| 93 | + liftIO $ writeHieFile out_file hieFile |
| 94 | + liftIO $ putDumpFileMaybe logger Opt_D_dump_hie "HIE AST" FormatHaskell (ppr $ hie_asts hieFile) |
| 95 | + |
| 96 | + -- Validate HIE files |
| 97 | + when (gopt Opt_ValidateHie dflags) $ do |
| 98 | + hs_env <- Hsc $ \e w -> return (e, w) |
| 99 | + liftIO $ do |
| 100 | + -- Validate Scopes |
| 101 | + case validateScopes (hie_module hieFile) $ getAsts $ hie_asts hieFile of |
| 102 | + [] -> putMsg logger $ text "Got valid scopes" |
| 103 | + xs -> do |
| 104 | + putMsg logger $ text "Got invalid scopes" |
| 105 | + mapM_ (putMsg logger) xs |
| 106 | + -- Roundtrip testing |
| 107 | + file' <- readHieFile (hsc_NC hs_env) out_file |
| 108 | + case diffFile hieFile (hie_file_result file') of |
| 109 | + [] -> |
| 110 | + putMsg logger $ text "Got no roundtrip errors" |
| 111 | + xs -> do |
| 112 | + putMsg logger $ text "Got roundtrip errors" |
| 113 | + let logger' = updateLogFlags logger (log_set_dopt Opt_D_ppr_debug) |
| 114 | + mapM_ (putMsg logger') xs |
| 115 | + return rn_info |
| 116 | + |
| 117 | +-- | Generate a stripped down interface file, e.g. for boot files or when ghci |
| 118 | +-- generates interface files. See Note [simpleTidyPgm - mkBootModDetailsTc] |
| 119 | +hscSimpleIface :: HscEnv |
| 120 | + -> Maybe CoreProgram |
| 121 | + -> TcGblEnv |
| 122 | + -> ModSummary |
| 123 | + -> IO (ModIface, ModDetails) |
| 124 | +hscSimpleIface hsc_env mb_core_program tc_result summary |
| 125 | + = runHsc hsc_env $ hscSimpleIface' mb_core_program tc_result summary |
0 commit comments