on master help sign in
Clean up UtilGHC (#963)

* Remove a duplicated comment * Remove traverse from the convertor * Move removing typeable to the converter * Don't reexport getGhcCore * Move coresForFile to its usage * Just print out the GHC Core for everything, including internal modules * Move modIsInternal out of haskell-ide-core * Move some DynFlags setters over to Config * Remove an unused orphan instance * Move the orphans out to a different module * Move functions for generating import syntax out of haskell-ide-core * Expose fakeDynFlags as a blob, not the pieces * Clean up the definition of fakeDynFlags * Inline showSDocDefault * HLint * Fix the comment on IDE.Orphans

commitd3aacb316ef384017ce78ae89fd5a73dcb480f16 authorNeil Mitchell <35463327+neil-da@users.noreply.github.com> dateTue 7 May 2019, 12:42 (7 years ago) parent1c27ffe
9 files changed+91−133
src/Development/IDE/Functions/AtPoint.hs+1 src/Development/IDE/Functions/Compile.hs+2−2 src/Development/IDE/Functions/DependencyInformation.hs+1 src/Development/IDE/Functions/FindImports.hs+1−1 src/Development/IDE/Functions/GHCError.hs+1−1 src/Development/IDE/Orphans.hs+48 src/Development/IDE/State/FileStore.hs+1−2 src/Development/IDE/State/Rules.hs+13−13 src/Development/IDE/UtilGHC.hs+23−114
src/Development/IDE/Functions/AtPoint.hs+1
@@ -11,6 +11,7 @@ module Development.IDE.Functions.AtPoint (
1111
1212import Development.IDE.Functions.Documentation
1313import Development.IDE.Functions.GHCError
14+import Development.IDE.Orphans()
1415
1516-- DAML compiler and infrastructure
1617import Development.IDE.Types.Diagnostics
src/Development/IDE/Functions/Compile.hs+2−2
@@ -197,7 +197,7 @@ compileModule opt mod packageState uniqSupply deps pkgs tmr =
197197 let pm = tm_parsed_module tm
198198 let pm' = pm{pm_mod_summary = tweak $ pm_mod_summary pm}
199199 let tm' = tm{tm_parsed_module = pm'}
200− removeTypeableInfo . GHC.dm_core_module <$> GHC.desugarModule tm'
200+ GHC.dm_core_module <$> GHC.desugarModule tm'
201201
202202 -- give variables unique OccNames
203203 (tidy, details) <- liftIO $ tidyProgram session desugar
@@ -479,7 +479,7 @@ parsePragmasIntoDynFlags fp contents = catchSrcErrors $ do
479479
480480generatePackageState :: [FilePath] -> Bool -> [(String, ModRenaming)] -> IO PackageDynFlags
481481generatePackageState paths hideAllPkgs pkgImports = do
482− let dflags = setPackageImports hideAllPkgs pkgImports $ setPackageDbs paths (defaultDynFlags fakeSettings fakeLlvmConfig)
482+ let dflags = setPackageImports hideAllPkgs pkgImports $ setPackageDbs paths fakeDynFlags
483483 (newDynFlags, _) <- initPackages dflags
484484 pure $ getPackageDynFlags newDynFlags
485485
src/Development/IDE/Functions/DependencyInformation.hs+1
@@ -13,6 +13,7 @@ module Development.IDE.Functions.DependencyInformation
1313
1414import Control.DeepSeq
1515import Data.Bifunctor
16+import Development.IDE.Orphans()
1617import Data.Either
1718import Data.Foldable
1819import Data.Graph
src/Development/IDE/Functions/FindImports.hs+1−1
@@ -10,7 +10,7 @@ module Development.IDE.Functions.FindImports
1010 ) where
1111
1212import Development.IDE.Functions.GHCError as ErrUtils
13−
13+import Development.IDE.Orphans()
1414-- GHC imports
1515import BasicTypes (StringLiteral(..))
1616import DynFlags
src/Development/IDE/Functions/GHCError.hs+1−1
@@ -30,7 +30,7 @@ module Development.IDE.Functions.GHCError
3030
3131import Development.IDE.Types.Diagnostics as D
3232import qualified Data.Text as T
33−import Development.IDE.UtilGHC()
33+import Development.IDE.Orphans()
3434import qualified FastString as FS
3535import GHC
3636import Bag
src/Development/IDE/Orphans.hsnew file+48
@@ -0,0 +1,48 @@
1+-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+-- SPDX-License-Identifier: Apache-2.0
3+
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# OPTIONS_GHC -Wno-orphans #-}
6+
7+-- | Orphan instances for GHC.
8+-- Note that the 'NFData' instances may not be law abiding.
9+module Development.IDE.Orphans() where
10+
11+import GHC hiding (convertLit)
12+import GhcPlugins as GHC hiding (fst3, (<>))
13+import qualified StringBuffer as SB
14+import Control.DeepSeq
15+import Development.IDE.UtilGHC
16+
17+
18+-- Orphan instances for types from the GHC API.
19+instance Show CoreModule where show = prettyPrint
20+instance NFData CoreModule where rnf = rwhnf
21+
22+
23+instance Show InstalledUnitId where
24+ show = installedUnitIdString
25+
26+instance NFData InstalledUnitId where rnf = rwhnf
27+
28+instance NFData SB.StringBuffer where rnf = rwhnf
29+
30+instance Show Module where
31+ show = moduleNameString . moduleName
32+
33+instance Show RdrName where show = prettyPrint
34+instance Show ComponentId where show = prettyPrint
35+instance Show SourcePackageId where show = prettyPrint
36+instance Show ModuleName where show = prettyPrint
37+instance Show (GenLocated SrcSpan ModuleName) where show = prettyPrint
38+instance Show PackageName where show = prettyPrint
39+instance Show PackageState where show _ = "PackageState"
40+instance Show Name where show = prettyPrint
41+
42+
43+-- Things which are defined in this module, but still orphan since I need
44+-- the definitions in this module
45+
46+deriving instance Show PackageDynFlags
47+instance NFData PackageDynFlags where
48+ rnf (PackageDynFlags db state insts) = db `seq` state `seq` rnf insts
src/Development/IDE/State/FileStore.hs+1−2
@@ -12,7 +12,7 @@ module Development.IDE.State.FileStore(
1212
1313
1414import StringBuffer
15−import Development.IDE.UtilGHC()
15+import Development.IDE.Orphans()
1616
1717import qualified Data.Map.Strict as Map
1818import qualified Data.Text as T
@@ -150,5 +150,4 @@ setBufferModified state absFile (mcontents, !time) = do
150150
151151-- would be nice to do this more efficiently...
152152textToStringBuffer :: T.Text -> SB.StringBuffer
153−-- would be nice to do this more efficiently...
154153textToStringBuffer = SB.stringToStringBuffer . T.unpack
src/Development/IDE/State/Rules.hs+13−13
@@ -33,7 +33,6 @@ import Development.IDE.Functions.DependencyInformation
3333import Development.IDE.Functions.FindImports
3434import Development.IDE.State.FileStore
3535import Development.IDE.Types.Diagnostics as Base
36−import Development.IDE.UtilGHC
3736import Data.Bifunctor
3837import Data.Either.Extra
3938import Data.Maybe
@@ -88,6 +87,19 @@ getUniqSupplyFrom Env{..} =
8887getGhcCore :: FilePath -> Action (Maybe [CoreModule])
8988getGhcCore file = eitherToMaybe <$> runExceptT (coresForFile file)
9089
90+-- | Generate the GHC Core for the supplied file and its dependencies.
91+coresForFile :: FilePath -> ExceptT [FileDiagnostic] Action [CoreModule]
92+coresForFile file = do
93+ files <- transitiveModuleDeps <$> useE GetDependencies file
94+ pms <- usesE GetParsedModule $ files ++ [file]
95+ fs <- liftIO
96+ . mapM fileFromParsedModule
97+ $ pms
98+ cores <- usesE GenerateCore fs
99+ pure (map Compile.gmCore cores)
100+
101+
102+
91103-- | Get all transitive file dependencies of a given module.
92104-- Does not include the file itself.
93105getDependencies :: FilePath -> Action (Maybe [FilePath])
@@ -125,18 +137,6 @@ usesE
125137 => k -> [FilePath] -> ExceptT [FileDiagnostic] Action [v]
126138usesE k = ExceptT . fmap (mapM toIdeResultSilent) . uses k
127139
128−-- | Generate the GHC Core for the supplied file and its dependencies.
129−coresForFile :: FilePath -> ExceptT [FileDiagnostic] Action [CoreModule]
130−coresForFile file = do
131− files <- transitiveModuleDeps <$> useE GetDependencies file
132− pms <- usesE GetParsedModule $ files ++ [file]
133− fs <- liftIO
134− . mapM fileFromParsedModule
135− . filter (not . modIsInternal . ms_mod . pm_mod_summary)
136− $ pms
137− cores <- usesE GenerateCore fs
138− pure (map Compile.gmCore cores)
139−
140140-- | Try to get hover text for the name under point.
141141getAtPointForFile
142142 :: FilePath
src/Development/IDE/UtilGHC.hs+23−114
@@ -1,10 +1,7 @@
11-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
22-- SPDX-License-Identifier: Apache-2.0
33
4−{-# LANGUAGE FlexibleInstances #-}
5−{-# LANGUAGE PatternSynonyms #-}
64{-# OPTIONS_GHC -Wno-missing-fields #-} -- to enable prettyPrint
7−{-# OPTIONS_GHC -Wno-orphans #-}
85
96-- | GHC utility functions. Importantly, code using our GHC should never:
107--
@@ -14,18 +11,11 @@
1411module Development.IDE.UtilGHC(
1512 PackageDynFlags(..), setPackageDynFlags, getPackageDynFlags,
1613 modifyDynFlags,
17− removeTypeableInfo,
1814 setPackageImports,
1915 setPackageDbs,
20− fakeSettings,
21− fakeLlvmConfig,
16+ fakeDynFlags,
2217 prettyPrint,
23− importGenerated,
24− mkImport,
25− runGhcFast,
26− setImports,
27− setThisInstalledUnitId,
28− modIsInternal
18+ runGhcFast
2919 ) where
3020
3121import Config
@@ -37,12 +27,8 @@ import HscMain
3727import qualified Packages
3828import Platform
3929import qualified EnumSet
40−
41−import Control.DeepSeq
4230import Data.IORef
43−import Data.List
4431import GHC.Generics (Generic)
45−import qualified StringBuffer as SB
4632
4733----------------------------------------------------------------------
4834-- GHC setup
@@ -84,10 +70,7 @@ data PackageDynFlags = PackageDynFlags
8470 { pdfPkgDatabase :: !(Maybe [(FilePath, [Packages.PackageConfig])])
8571 , pdfPkgState :: !Packages.PackageState
8672 , pdfThisUnitIdInsts :: !(Maybe [(ModuleName, Module)])
87− } deriving (Generic, Show)
88−
89−instance NFData PackageDynFlags where
90− rnf (PackageDynFlags db state insts) = db `seq` state `seq` rnf insts
73+ } deriving (Generic)
9174
9275setPackageDynFlags :: PackageDynFlags -> DynFlags -> DynFlags
9376setPackageDynFlags PackageDynFlags{..} dflags = dflags
@@ -104,48 +87,8 @@ getPackageDynFlags DynFlags{..} = PackageDynFlags
10487 }
10588
10689
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
112−
11390prettyPrint :: Outputable a => a -> String
114−prettyPrint = showSDocDefault . ppr
115−
116−-- FIXME(#1203): This must move out of `haskell-ide-core` and into `damlc`.
117−internalModules :: [String]
118−internalModules =
119− [ "Data.String"
120− , "GHC.CString"
121− , "GHC.Integer.Type"
122− , "GHC.Natural"
123− , "GHC.Real"
124− , "GHC.Types"
125− ]
126−
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.
133−
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}
137−
138−mkImport :: Located ModuleName -> ImportDecl GhcPs
139−mkImport mname = GHC.ImportDecl GHC.NoExt GHC.NoSourceText mname Nothing False False False False Nothing Nothing
140−
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)}
145− where
146− isTypeableInfo = \case
147− NonRec name _ -> any (`isPrefixOf` getOccString name) ["$krep", "$tc", "$trModule"]
148− Rec _ -> False
91+prettyPrint = showSDoc fakeDynFlags . ppr
14992
15093-- | Like 'runGhc' but much faster (400x), with less IO and no file dependency
15194runGhcFast :: Ghc a -> IO a
@@ -154,61 +97,27 @@ runGhcFast act = do
15497 ref <- newIORef (error "empty session")
15598 let session = Session ref
15699 flip unGhc session $ do
157− dflags <- liftIO $ initDynFlags $ defaultDynFlags fakeSettings fakeLlvmConfig
100+ dflags <- liftIO $ initDynFlags fakeDynFlags
158101 liftIO $ setUnsafeGlobalDynFlags dflags
159102 env <- liftIO $ newHscEnv dflags
160103 setSession env
161104 withCleanupSession act
162105
163−-- These settings are mostly undefined, but define just enough for what we want to do (which isn't code gen)
164−fakeSettings :: Settings
165−fakeSettings = Settings
166− {sTargetPlatform=platform
167− ,sPlatformConstants=platformConstants
168− ,sProjectVersion=cProjectVersion
169− ,sProgramName="ghc"
170− ,sOpt_P_fingerprint=fingerprint0
171− }
172− where
173− platform = Platform{platformWordSize=8, platformOS=OSUnknown, platformUnregisterised=True}
174− platformConstants = PlatformConstants{pc_DYNAMIC_BY_DEFAULT=False,pc_WORD_SIZE=8}
175−
176−fakeLlvmConfig :: (LlvmTargets, LlvmPasses)
177−fakeLlvmConfig = ([], [])
178−
179−
180−setThisInstalledUnitId :: UnitId -> DynFlags -> DynFlags
181−setThisInstalledUnitId unitId dflags =
182− dflags {thisInstalledUnitId = toInstalledUnitId unitId}
183−
184−setImports :: [FilePath] -> DynFlags -> DynFlags
185−setImports paths dflags = dflags { importPaths = paths }
186−
187−
188−
189−-- Orphan instances for types from the GHC API.
190−instance Show CoreModule where show = prettyPrint
191−instance NFData CoreModule where rnf !_ = ()
192−
193−instance Show RdrName where show = prettyPrint
194−instance NFData RdrName where rnf !_ = ()
195−
196−instance Show InstalledUnitId where
197− show = installedUnitIdString
198−
199−instance NFData InstalledUnitId where
200− rnf = rwhnf
201−
202−instance NFData SB.StringBuffer where
203− rnf = rwhnf
204−
205−instance Show Module where
206− show = moduleNameString . moduleName
207−
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
106+-- Fake DynFlags which are mostly undefined, but define enough to do a little bit
107+fakeDynFlags :: DynFlags
108+fakeDynFlags = defaultDynFlags settings ([], [])
109+ where
110+ settings = Settings
111+ {sTargetPlatform = Platform
112+ {platformWordSize = 8
113+ ,platformOS = OSUnknown
114+ ,platformUnregisterised = True
115+ }
116+ ,sPlatformConstants = PlatformConstants
117+ {pc_DYNAMIC_BY_DEFAULT = False
118+ ,pc_WORD_SIZE = 8
119+ }
120+ ,sProjectVersion = cProjectVersion
121+ ,sProgramName = "ghc"
122+ ,sOpt_P_fingerprint = fingerprint0
123+ }