@@ -1,10 +1,7 @@
1-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
1-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2-- SPDX-License-Identifier: Apache-2.0
2-- SPDX-License-Identifier: Apache-2.0
4−{-# LANGUAGE FlexibleInstances #-}
5−{-# LANGUAGE PatternSynonyms #-}
6{-# OPTIONS_GHC -Wno-missing-fields #-} -- to enable prettyPrint
4{-# OPTIONS_GHC -Wno-missing-fields #-} -- to enable prettyPrint
7−{-# OPTIONS_GHC -Wno-orphans #-}
9-- | GHC utility functions. Importantly, code using our GHC should never:
6-- | GHC utility functions. Importantly, code using our GHC should never:
@@ -14,18 +11,11 @@
14module Development.IDE.UtilGHC(
11module Development.IDE.UtilGHC(
15 PackageDynFlags(..), setPackageDynFlags, getPackageDynFlags,
12 PackageDynFlags(..), setPackageDynFlags, getPackageDynFlags,
16 modifyDynFlags,
13 modifyDynFlags,
18 setPackageImports,
14 setPackageImports,
19 setPackageDbs,
15 setPackageDbs,
20− fakeSettings,
16+ fakeDynFlags,
22 prettyPrint,
17 prettyPrint,
25− runGhcFast,
18+ runGhcFast
27− setThisInstalledUnitId,
31import Config
21import Config
@@ -37,12 +27,8 @@ import HscMain
37import qualified Packages
27import qualified Packages
38import Platform
28import Platform
39import qualified EnumSet
29import qualified EnumSet
41−import Control.DeepSeq
42import Data.IORef
30import Data.IORef
44import GHC.Generics (Generic)
31import GHC.Generics (Generic)
45−import qualified StringBuffer as SB
47----------------------------------------------------------------------
33----------------------------------------------------------------------
48-- GHC setup
34-- GHC setup
@@ -84,10 +70,7 @@ data PackageDynFlags = PackageDynFlags
84 { pdfPkgDatabase :: !(Maybe [(FilePath, [Packages.PackageConfig])])
70 { pdfPkgDatabase :: !(Maybe [(FilePath, [Packages.PackageConfig])])
85 , pdfPkgState :: !Packages.PackageState
71 , pdfPkgState :: !Packages.PackageState
86 , pdfThisUnitIdInsts :: !(Maybe [(ModuleName, Module)])
72 , pdfThisUnitIdInsts :: !(Maybe [(ModuleName, Module)])
87− } deriving (Generic, Show)
73+ } deriving (Generic)
89−instance NFData PackageDynFlags where
90− rnf (PackageDynFlags db state insts) = db `seq` state `seq` rnf insts
92setPackageDynFlags :: PackageDynFlags -> DynFlags -> DynFlags
75setPackageDynFlags :: PackageDynFlags -> DynFlags -> DynFlags
93setPackageDynFlags PackageDynFlags{..} dflags = dflags
76setPackageDynFlags PackageDynFlags{..} dflags = dflags
@@ -104,48 +87,8 @@ getPackageDynFlags DynFlags{..} = PackageDynFlags
107−-- | A version of `showSDoc` that uses default flags (to avoid uses of
108−-- `showSDocUnsafe`).
109−showSDocDefault :: SDoc -> String
110−showSDocDefault = showSDoc dynFlags
111− where dynFlags = defaultDynFlags fakeSettings fakeLlvmConfig
113prettyPrint :: Outputable a => a -> String
90prettyPrint :: Outputable a => a -> String
114−prettyPrint = showSDocDefault . ppr
91+prettyPrint = showSDoc fakeDynFlags . ppr
116−-- FIXME(#1203): This must move out of `haskell-ide-core` and into `damlc`.
117−internalModules :: [String]
121− , "GHC.Integer.Type"
127−-- | Checks if a given module is internal, i.e. gets removed in the Core->LF
128−-- translation. TODO where should this live?
129−modIsInternal :: Module -> Bool
130−modIsInternal m = moduleNameString (moduleName m) `elem` internalModules
131− -- TODO should we consider DA.Internal.* internal? Difference to GHC.*
132− -- modules is that these do not disappear in the LF conversion.
134−-- | This import was generated, not user written, so should not produce unused import warnings
135−importGenerated :: Bool -> ImportDecl phase -> ImportDecl phase
136−importGenerated qual i = i{ideclImplicit=True, ideclQualified=qual}
138−mkImport :: Located ModuleName -> ImportDecl GhcPs
139−mkImport mname = GHC.ImportDecl GHC.NoExt GHC.NoSourceText mname Nothing False False False False Nothing Nothing
141−-- FIXME(#1203): This needs to move out of haskell-ide-core.
142−removeTypeableInfo :: ModGuts -> ModGuts
143−removeTypeableInfo guts =
144− guts{mg_binds = filter (not . isTypeableInfo) (mg_binds guts)}
146− isTypeableInfo = \case
147− NonRec name _ -> any (`isPrefixOf` getOccString name) ["$krep", "$tc", "$trModule"]
150-- | Like 'runGhc' but much faster (400x), with less IO and no file dependency
93-- | Like 'runGhc' but much faster (400x), with less IO and no file dependency
151runGhcFast :: Ghc a -> IO a
94runGhcFast :: Ghc a -> IO a
@@ -154,61 +97,27 @@ runGhcFast act = do
154 ref <- newIORef (error "empty session")
97 ref <- newIORef (error "empty session")
155 let session = Session ref
98 let session = Session ref
156 flip unGhc session $ do
99 flip unGhc session $ do
157− dflags <- liftIO $ initDynFlags $ defaultDynFlags fakeSettings fakeLlvmConfig
100+ dflags <- liftIO $ initDynFlags fakeDynFlags
158 liftIO $ setUnsafeGlobalDynFlags dflags
101 liftIO $ setUnsafeGlobalDynFlags dflags
159 env <- liftIO $ newHscEnv dflags
102 env <- liftIO $ newHscEnv dflags
160 setSession env
103 setSession env
161 withCleanupSession act
104 withCleanupSession act
163−-- These settings are mostly undefined, but define just enough for what we want to do (which isn't code gen)
106+-- Fake DynFlags which are mostly undefined, but define enough to do a little bit
164−fakeSettings :: Settings
107+fakeDynFlags :: DynFlags
108+fakeDynFlags = defaultDynFlags settings ([], [])
165−fakeSettings = Settings
110+ settings = Settings
166− {sTargetPlatform=platform
111+ {sTargetPlatform = Platform
167− ,sPlatformConstants=platformConstants
112+ {platformWordSize = 8
168− ,sProjectVersion=cProjectVersion
113+ ,platformOS = OSUnknown
170− ,sOpt_P_fingerprint=fingerprint0
173− platform = Platform{platformWordSize=8, platformOS=OSUnknown, platformUnregisterised=True}
114+ ,platformUnregisterised = True
116+ ,sPlatformConstants = PlatformConstants
174− platformConstants = PlatformConstants{pc_DYNAMIC_BY_DEFAULT=False,pc_WORD_SIZE=8}
117+ {pc_DYNAMIC_BY_DEFAULT = False
175−
118+ ,pc_WORD_SIZE = 8
176−fakeLlvmConfig :: (LlvmTargets, LlvmPasses)
119+ }
177−fakeLlvmConfig = ([], [])
120+ ,sProjectVersion = cProjectVersion
178−
121+ ,sProgramName = "ghc"
179−
122+ ,sOpt_P_fingerprint = fingerprint0
180−setThisInstalledUnitId :: UnitId -> DynFlags -> DynFlags
123+ }
181−setThisInstalledUnitId unitId dflags =
182− dflags {thisInstalledUnitId = toInstalledUnitId unitId}
184−setImports :: [FilePath] -> DynFlags -> DynFlags
185−setImports paths dflags = dflags { importPaths = paths }
189−-- Orphan instances for types from the GHC API.
190−instance Show CoreModule where show = prettyPrint
191−instance NFData CoreModule where rnf !_ = ()
193−instance Show RdrName where show = prettyPrint
194−instance NFData RdrName where rnf !_ = ()
196−instance Show InstalledUnitId where
197− show = installedUnitIdString
199−instance NFData InstalledUnitId where
202−instance NFData SB.StringBuffer where
205−instance Show Module where
206− show = moduleNameString . moduleName
208−instance Show ComponentId where show = prettyPrint
209−instance Show SourcePackageId where show = prettyPrint
210−instance Show ModuleName where show = prettyPrint
211−instance Show (GenLocated SrcSpan ModuleName) where show = prettyPrint
212−instance Show PackageName where show = prettyPrint
213−instance Show Packages.PackageState where show _ = "PackageState"
214−instance Show Name where show = prettyPrint