forked from haskell/haskell-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrphans.hs
255 lines (203 loc) · 7.23 KB
/
Orphans.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
-- | Orphan instances for GHC.
-- Note that the 'NFData' instances may not be law abiding.
module Development.IDE.GHC.Orphans() where
#if MIN_VERSION_ghc(9,2,0)
import GHC.Parser.Annotation
#endif
#if MIN_VERSION_ghc(9,0,0)
import GHC.Data.Bag
import GHC.Data.FastString
import qualified GHC.Data.StringBuffer as SB
import GHC.Types.Name.Occurrence
import GHC.Types.SrcLoc
import GHC.Types.Unique (getKey)
import GHC.Unit.Info
import GHC.Utils.Outputable
#else
import Bag
import GhcPlugins hiding (UniqFM)
import qualified StringBuffer as SB
import Unique (getKey)
#endif
import Development.IDE.GHC.Compat
import Development.IDE.GHC.Util
import Control.DeepSeq
import Data.Aeson
import Data.Bifunctor (Bifunctor (..))
import Data.Hashable
import Data.String (IsString (fromString))
import Data.Text (unpack)
#if MIN_VERSION_ghc(9,0,0)
import GHC (ModuleGraph)
import GHC.ByteCode.Types
#else
import ByteCodeTypes
#endif
#if MIN_VERSION_ghc(9,3,0)
import GHC.Types.PkgQual
#endif
#if MIN_VERSION_ghc(9,5,0)
import GHC.Unit.Home.ModInfo
#endif
-- Orphan instances for types from the GHC API.
instance Show CoreModule where show = unpack . printOutputable
instance NFData CoreModule where rnf = rwhnf
instance Show CgGuts where show = unpack . printOutputable . cg_module
instance NFData CgGuts where rnf = rwhnf
instance Show ModDetails where show = const "<moddetails>"
instance NFData ModDetails where rnf = rwhnf
instance NFData SafeHaskellMode where rnf = rwhnf
instance Show Linkable where show = unpack . printOutputable
instance NFData Linkable where rnf (LM a b c) = rnf a `seq` rnf b `seq` rnf c
instance NFData Unlinked where
rnf (DotO f) = rnf f
rnf (DotA f) = rnf f
rnf (DotDLL f) = rnf f
rnf (BCOs a b) = seqCompiledByteCode a `seq` liftRnf rwhnf b
instance Show PackageFlag where show = unpack . printOutputable
instance Show InteractiveImport where show = unpack . printOutputable
instance Show PackageName where show = unpack . printOutputable
#if !MIN_VERSION_ghc(9,0,1)
instance Show ComponentId where show = unpack . printOutputable
instance Show SourcePackageId where show = unpack . printOutputable
instance Show GhcPlugins.InstalledUnitId where
show = installedUnitIdString
instance NFData GhcPlugins.InstalledUnitId where rnf = rwhnf . installedUnitIdFS
instance Hashable GhcPlugins.InstalledUnitId where
hashWithSalt salt = hashWithSalt salt . installedUnitIdString
#else
instance Show UnitId where show = unpack . printOutputable
deriving instance Ord SrcSpan
deriving instance Ord UnhelpfulSpanReason
#endif
instance NFData SB.StringBuffer where rnf = rwhnf
instance Show Module where
show = moduleNameString . moduleName
#if !MIN_VERSION_ghc(9,3,0)
instance Outputable a => Show (GenLocated SrcSpan a) where show = unpack . printOutputable
#endif
#if !MIN_VERSION_ghc(9,5,0)
instance (NFData l, NFData e) => NFData (GenLocated l e) where
rnf (L l e) = rnf l `seq` rnf e
#endif
instance Show ModSummary where
show = show . ms_mod
instance Show ParsedModule where
show = show . pm_mod_summary
instance NFData ModSummary where
rnf = rwhnf
#if MIN_VERSION_ghc(9,2,0)
instance Ord FastString where
compare a b = if a == b then EQ else compare (fs_sbs a) (fs_sbs b)
instance NFData (SrcSpanAnn' a) where
rnf = rwhnf
instance Bifunctor (GenLocated) where
bimap f g (L l x) = L (f l) (g x)
deriving instance Functor SrcSpanAnn'
#endif
instance NFData ParsedModule where
rnf = rwhnf
instance Show HieFile where
show = show . hie_module
instance NFData HieFile where
rnf = rwhnf
#if !MIN_VERSION_ghc(9,3,0)
deriving instance Eq SourceModified
deriving instance Show SourceModified
instance NFData SourceModified where
rnf = rwhnf
#endif
#if !MIN_VERSION_ghc(9,2,0)
instance Show ModuleName where
show = moduleNameString
#endif
instance Hashable ModuleName where
hashWithSalt salt = hashWithSalt salt . show
instance NFData a => NFData (IdentifierDetails a) where
rnf (IdentifierDetails a b) = rnf a `seq` rnf (length b)
instance NFData RealSrcSpan where
rnf = rwhnf
srcSpanFileTag, srcSpanStartLineTag, srcSpanStartColTag,
srcSpanEndLineTag, srcSpanEndColTag :: String
srcSpanFileTag = "srcSpanFile"
srcSpanStartLineTag = "srcSpanStartLine"
srcSpanStartColTag = "srcSpanStartCol"
srcSpanEndLineTag = "srcSpanEndLine"
srcSpanEndColTag = "srcSpanEndCol"
instance ToJSON RealSrcSpan where
toJSON spn =
object
[ fromString srcSpanFileTag .= unpackFS (srcSpanFile spn)
, fromString srcSpanStartLineTag .= srcSpanStartLine spn
, fromString srcSpanStartColTag .= srcSpanStartCol spn
, fromString srcSpanEndLineTag .= srcSpanEndLine spn
, fromString srcSpanEndColTag .= srcSpanEndCol spn
]
instance FromJSON RealSrcSpan where
parseJSON = withObject "object" $ \obj -> do
file <- fromString <$> (obj .: fromString srcSpanFileTag)
mkRealSrcSpan
<$> (mkRealSrcLoc file
<$> obj .: fromString srcSpanStartLineTag
<*> obj .: fromString srcSpanStartColTag
)
<*> (mkRealSrcLoc file
<$> obj .: fromString srcSpanEndLineTag
<*> obj .: fromString srcSpanEndColTag
)
instance NFData Type where
rnf = rwhnf
instance Show a => Show (Bag a) where
show = show . bagToList
#if !MIN_VERSION_ghc(9,5,0)
instance NFData HsDocString where
rnf = rwhnf
#endif
instance Show ModGuts where
show _ = "modguts"
instance NFData ModGuts where
rnf = rwhnf
instance NFData (ImportDecl GhcPs) where
rnf = rwhnf
#if MIN_VERSION_ghc(9,5,0)
instance (NFData (HsModule a)) where
#elif MIN_VERSION_ghc(9,0,1)
instance (NFData HsModule) where
#else
instance (NFData (HsModule a)) where
#endif
rnf = rwhnf
instance Show OccName where show = unpack . printOutputable
instance Hashable OccName where hashWithSalt s n = hashWithSalt s (getKey $ getUnique n)
instance Show HomeModInfo where show = show . mi_module . hm_iface
instance Show ModuleGraph where show _ = "ModuleGraph {..}"
instance NFData ModuleGraph where rnf = rwhnf
instance NFData HomeModInfo where
rnf (HomeModInfo iface dets link) = rwhnf iface `seq` rnf dets `seq` rnf link
#if MIN_VERSION_ghc(9,3,0)
instance NFData PkgQual where
rnf NoPkgQual = ()
rnf (ThisPkg uid) = rnf uid
rnf (OtherPkg uid) = rnf uid
instance NFData UnitId where
rnf = rwhnf
instance NFData NodeKey where
rnf = rwhnf
#endif
#if MIN_VERSION_ghc(9,5,0)
instance NFData HomeModLinkable where
rnf = rwhnf
#endif
instance NFData (HsExpr (GhcPass 'Renamed)) where
rnf = rwhnf
instance NFData (Pat (GhcPass 'Renamed)) where
rnf = rwhnf
instance NFData Extension where
rnf = rwhnf
instance NFData (UniqFM Name [Name]) where
rnf (ufmToIntMap -> m) = rnf m