@@ -8,9 +8,7 @@
8-- | Based on https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/API.
8-- | Based on https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/API.
9-- Given a list of paths to find libraries, and a file to compile, produce a list of 'CoreModule' values.
9-- Given a list of paths to find libraries, and a file to compile, produce a list of 'CoreModule' values.
10module Development.IDE.Functions.Compile
10module Development.IDE.Functions.Compile
13− , GhcModule(..)
11+ ( GhcModule(..)
14 , TcModuleResult(..)
12 , TcModuleResult(..)
15 , LoadPackageResult(..)
13 , LoadPackageResult(..)
16 , getGhcDynFlags
14 , getGhcDynFlags
@@ -29,6 +27,7 @@ import qualified Development.IDE.Functions.FindImports as FindImports
29import Development.IDE.Functions.GHCError
27import Development.IDE.Functions.GHCError
30import Development.IDE.Functions.SpanInfo
28import Development.IDE.Functions.SpanInfo
31import Development.IDE.UtilGHC
29import Development.IDE.UtilGHC
30+import Development.IDE.Types.Options
33import GHC hiding (parseModule, typecheckModule)
32import GHC hiding (parseModule, typecheckModule)
34import qualified Parser
33import qualified Parser
@@ -38,7 +37,7 @@ import Bag
38import qualified GHC
37import qualified GHC
39import Panic
38import Panic
40import GhcMonad
39import GhcMonad
41−import GhcPlugins as GHC hiding (PackageState, fst3, (<>))
40+import GhcPlugins as GHC hiding (fst3, (<>))
42import qualified HeaderInfo as Hdr
41import qualified HeaderInfo as Hdr
43import MkIface
42import MkIface
44import NameCache
43import NameCache
@@ -61,24 +60,6 @@ import Development.IDE.Types.SpanInfo
61import GHC.Generics (Generic)
60import GHC.Generics (Generic)
62import System.FilePath
61import System.FilePath
64−-- TODO (MK) Move to a separate Options module
65−data CompileOpts = CompileOpts
66− { optPreprocessor :: GHC.ParsedSource -> ([(GHC.SrcSpan, String)], GHC.ParsedSource)
67− , optRunGhcSession :: forall a. Maybe ParsedModule -> PackageState -> Ghc a -> IO a
68− -- ^ Setup a GHC session using a given package state. If a `ParsedModule` is supplied,
69− -- the import path should be setup for that module.
70− , optWriteIface :: Bool
72− , optMbPackageName :: Maybe String
74− , optPackageDbs :: [FilePath]
75− , optHideAllPkgs :: Bool
76− , optPackageImports :: [(String, ModRenaming)]
79− , optShakeProfiling :: Maybe FilePath
82-- | 'CoreModule' together with some additional information required for the
63-- | 'CoreModule' together with some additional information required for the
83-- conversion to DAML-LF.
64-- conversion to DAML-LF.
84data GhcModule = GhcModule
65data GhcModule = GhcModule
@@ -106,9 +87,9 @@ data LoadPackageResult = LoadPackageResult
107-- | Get source span info, used for e.g. AtPoint and Goto Definition.
88-- | Get source span info, used for e.g. AtPoint and Goto Definition.
108getSrcSpanInfos
89getSrcSpanInfos
109− :: CompileOpts
90+ :: IdeOptions
110 -> ParsedModule
91 -> ParsedModule
111− -> PackageState
92+ -> PackageDynFlags
112 -> [(Located ModuleName, Maybe FilePath)]
93 -> [(Located ModuleName, Maybe FilePath)]
113 -> TcModuleResult
94 -> TcModuleResult
114 -> IO [SpanInfo]
95 -> IO [SpanInfo]
@@ -120,18 +101,18 @@ getSrcSpanInfos opt mod packageState imports tc =
121-- | Given a string buffer, return a pre-processed @ParsedModule@.
102-- | Given a string buffer, return a pre-processed @ParsedModule@.
122parseModule
103parseModule
123− :: CompileOpts
104+ :: IdeOptions
124− -> PackageState
105+ -> PackageDynFlags
125 -> FilePath
106 -> FilePath
126 -> (UTCTime, SB.StringBuffer)
107 -> (UTCTime, SB.StringBuffer)
127 -> IO ([FileDiagnostic], Maybe ParsedModule)
108 -> IO ([FileDiagnostic], Maybe ParsedModule)
128−parseModule opt@CompileOpts{..} packageState file =
109+parseModule opt@IdeOptions{..} packageState file =
129 fmap (either (, Nothing) (second Just)) . Ex.runExceptT .
110 fmap (either (, Nothing) (second Just)) . Ex.runExceptT .
130 -- We need packages since imports fail to resolve otherwise.
111 -- We need packages since imports fail to resolve otherwise.
131 runGhcSessionExcept opt Nothing packageState . parseFileContents optPreprocessor file
112 runGhcSessionExcept opt Nothing packageState . parseFileContents optPreprocessor file
133computePackageDeps ::
114computePackageDeps ::
134− CompileOpts -> PackageState -> InstalledUnitId -> IO (Either [FileDiagnostic] [InstalledUnitId])
115+ IdeOptions -> PackageDynFlags -> InstalledUnitId -> IO (Either [FileDiagnostic] [InstalledUnitId])
135computePackageDeps opts packageState iuid =
116computePackageDeps opts packageState iuid =
136 Ex.runExceptT $
117 Ex.runExceptT $
137 runGhcSessionExcept opts Nothing packageState $
118 runGhcSessionExcept opts Nothing packageState $
@@ -149,9 +130,9 @@ getPackage dflags p =
150-- | Typecheck a single module using the supplied dependencies and packages.
131-- | Typecheck a single module using the supplied dependencies and packages.
151typecheckModule
132typecheckModule
152− :: CompileOpts
133+ :: IdeOptions
153 -> ParsedModule
134 -> ParsedModule
154− -> PackageState
135+ -> PackageDynFlags
155 -> UniqSupply
136 -> UniqSupply
156 -> [TcModuleResult]
137 -> [TcModuleResult]
157 -> [LoadPackageResult]
138 -> [LoadPackageResult]
@@ -169,8 +150,8 @@ typecheckModule opt mod packageState uniqSupply deps pkgs pm =
170-- | Load a pkg and populate the name cache and external package state.
151-- | Load a pkg and populate the name cache and external package state.
171loadPackage ::
152loadPackage ::
172− CompileOpts
153+ IdeOptions
173− -> PackageState
154+ -> PackageDynFlags
174 -> UniqSupply
155 -> UniqSupply
175 -> [LoadPackageResult]
156 -> [LoadPackageResult]
176 -> InstalledUnitId
157 -> InstalledUnitId
@@ -196,9 +177,9 @@ loadPackage opt packageState us lps p =
196-- | Compile a single type-checked module to a 'CoreModule' value, or
177-- | Compile a single type-checked module to a 'CoreModule' value, or
197-- provide errors.
178-- provide errors.
198compileModule
179compileModule
199− :: CompileOpts
180+ :: IdeOptions
200 -> ParsedModule
181 -> ParsedModule
201− -> PackageState
182+ -> PackageDynFlags
202 -> UniqSupply
183 -> UniqSupply
203 -> [TcModuleResult]
184 -> [TcModuleResult]
204 -> [LoadPackageResult]
185 -> [LoadPackageResult]
@@ -233,27 +214,27 @@ compileModule opt mod packageState uniqSupply deps pkgs tmr =
233-- | Evaluate a GHC session using a new environment constructed with
214-- | Evaluate a GHC session using a new environment constructed with
234-- the supplied options.
215-- the supplied options.
235runGhcSessionExcept
216runGhcSessionExcept
236− :: CompileOpts
217+ :: IdeOptions
237 -> Maybe ParsedModule
218 -> Maybe ParsedModule
238− -> PackageState
219+ -> PackageDynFlags
239 -> Ex.ExceptT e Ghc a
220 -> Ex.ExceptT e Ghc a
240 -> Ex.ExceptT e IO a
221 -> Ex.ExceptT e IO a
241runGhcSessionExcept opts mbMod pkg m =
222runGhcSessionExcept opts mbMod pkg m =
242 Ex.ExceptT $ runGhcSession opts mbMod pkg $ Ex.runExceptT m
223 Ex.ExceptT $ runGhcSession opts mbMod pkg $ Ex.runExceptT m
245−getGhcDynFlags :: CompileOpts -> ParsedModule -> PackageState -> IO DynFlags
226+getGhcDynFlags :: IdeOptions -> ParsedModule -> PackageDynFlags -> IO DynFlags
246getGhcDynFlags opts mod pkg = runGhcSession opts (Just mod) pkg getSessionDynFlags
227getGhcDynFlags opts mod pkg = runGhcSession opts (Just mod) pkg getSessionDynFlags
248-- | Evaluate a GHC session using a new environment constructed with
229-- | Evaluate a GHC session using a new environment constructed with
249-- the supplied options.
230-- the supplied options.
250runGhcSession
231runGhcSession
251− :: CompileOpts
232+ :: IdeOptions
252 -> Maybe ParsedModule
233 -> Maybe ParsedModule
253− -> PackageState
234+ -> PackageDynFlags
256−runGhcSession CompileOpts{..} = optRunGhcSession
237+runGhcSession IdeOptions{..} = optRunGhcSession
258-- When we make a fresh GHC environment, the OrigNameCache comes already partially
239-- When we make a fresh GHC environment, the OrigNameCache comes already partially
259-- populated. So to be safe, we simply extend this one.
240-- populated. So to be safe, we simply extend this one.
@@ -496,11 +477,11 @@ parsePragmasIntoDynFlags fp contents = catchSrcErrors $ do
496 (dflags, _, _) <- parseDynamicFilePragma dflags0 opts
477 (dflags, _, _) <- parseDynamicFilePragma dflags0 opts
497 return dflags
478 return dflags
499−generatePackageState :: [FilePath] -> Bool -> [(String, ModRenaming)] -> IO PackageState
480+generatePackageState :: [FilePath] -> Bool -> [(String, ModRenaming)] -> IO PackageDynFlags
500generatePackageState paths hideAllPkgs pkgImports = do
481generatePackageState paths hideAllPkgs pkgImports = do
501 let dflags = setPackageImports hideAllPkgs pkgImports $ setPackageDbs paths (defaultDynFlags fakeSettings fakeLlvmConfig)
482 let dflags = setPackageImports hideAllPkgs pkgImports $ setPackageDbs paths (defaultDynFlags fakeSettings fakeLlvmConfig)
502 (newDynFlags, _) <- initPackages dflags
483 (newDynFlags, _) <- initPackages dflags
503− pure $ PackageState (pkgDatabase newDynFlags) (pkgState newDynFlags) (thisUnitIdInsts_ newDynFlags)
484+ pure $ getPackageDynFlags newDynFlags
505-- | Run something in a Ghc monad and catch the errors (SourceErrors and
486-- | Run something in a Ghc monad and catch the errors (SourceErrors and
506-- compiler-internal exceptions like Panic or InstallationError).
487-- compiler-internal exceptions like Panic or InstallationError).