aboutsummaryrefslogtreecommitdiff
path: root/src/HaskellCodeExplorer/ModuleInfo.hs
blob: cc81a365e0f677000d5160cacc6fe9fceacb7625 (plain) (blame)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
{-# LANGUAGE CPP #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StrictData #-}

module HaskellCodeExplorer.ModuleInfo
  ( createModuleInfo
  , ModuleDependencies
  ) where

import qualified Data.Generics.Uniplate.Data as U
import Control.Monad.State.Strict (execState,evalState,get,put,State)
import qualified Data.Aeson as Aeson
import Data.Aeson.Text(encodeToLazyText)
import qualified Data.Vector as V
import qualified Data.HashMap.Strict as HM
import qualified Data.Map.Strict as M
import qualified Data.IntMap.Strict as IM
import qualified Data.IntervalMap.Strict as IVM
import qualified Data.List as L hiding (span)
import Data.Maybe(fromMaybe,mapMaybe)
import Data.Ord(comparing)
import qualified Data.Set as S
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Data.Text.Lazy (toStrict)
import Documentation.Haddock.Types (DocH)
import DynFlags(DynFlags)
import GHC
  ( GenLocated(..)
  , ModSummary
  , ModuleInfo
  , ModuleName
  , SrcSpan
  , TyThing(..)
  , Type
  , TypecheckedModule
  , getLoc
  , isGoodSrcSpan
  , modInfoExportsWithSelectors
  , modInfoInstances
  , moduleInfo
  , moduleNameString
  , ms_hspp_buf
  , ms_mod
  , renamedSource
  , tm_internals_
  , tm_typechecked_source
  , unLoc
  )
import Type(expandTypeSynonyms)
import TyCon (isFamInstTyCon,tyConName)
import HaskellCodeExplorer.AST.RenamedSource
import HaskellCodeExplorer.AST.TypecheckedSource
import HaskellCodeExplorer.GhcUtils
import HaskellCodeExplorer.Preprocessor (createSourceCodeTransformation)
import qualified HaskellCodeExplorer.Types as HCE
import HsBinds(HsBindLR)
import HsDecls
  ( ForeignDecl(..)
  , HsDecl(..)
  , HsGroup(..)
  , InstDecl
  , InstDecl(..)
  , TyClDecl
  , group_tyclds
  , tyClDeclLName
  , tcdName
#if MIN_VERSION_GLASGOW_HASKELL(8,2,2,0)
  , hsGroupInstDecls
#endif
  )
import HsDoc(HsDocString)
import HsImpExp (IE(..), ImportDecl(..))
import HsUtils(collectHsBindBinders)
import HscTypes
  ( ExternalPackageState
  , HomePackageTable
  , TypeEnv
  , eps_PTE
  , eps_inst_env
  , hm_details
  , md_types
  , mkTypeEnv
  , typeEnvElts
  )
import InstEnv (InstEnvs(..), is_dfun)
import Module(Module(..))
import Name (Name, OccName, getSrcSpan, nameOccName, nameSrcSpan, nameUnique)
import Prelude hiding(id,span)
import RdrName(GlobalRdrEnv)
import SrcLoc (isOneLineSpan)  
import TcRnTypes (tcVisibleOrphanMods, tcg_inst_env, tcg_rdr_env, tcg_type_env)
import qualified Text.Blaze.Html5 as H 
import qualified Text.Blaze.Html5.Attributes as A
#if MIN_VERSION_GLASGOW_HASKELL(8,2,2,0)
import UniqDFM (eltsUDFM)
#else
import UniqFM (eltsUFM)
#endif
import Unique (getKey)
import Var (varName, varType,Id)
import VarEnv (emptyTidyEnv)

type ModuleDependencies
   = ( HM.HashMap HCE.HaskellFilePath HCE.HaskellModulePath
     , HM.HashMap HCE.HaskellModulePath HCE.DefinitionSiteMap
     , HM.HashMap HCE.HaskellModuleName (HM.HashMap HCE.ComponentId HCE.HaskellModulePath))
  
type ModuleGhcData
   = ( DynFlags
     , TypecheckedModule
     , HomePackageTable
     , ExternalPackageState
     , ModSummary)
 
createModuleInfo ::
     ModuleDependencies -- ^ Modules that have already been indexed
  -> ModuleGhcData -- ^ Data types from GHC
  -> HCE.HaskellModulePath -- ^ Current module path
  -> HCE.PackageId -- ^ Current package id
  -> HCE.ComponentId -- ^ Current build component id
  -> (T.Text, HCE.SourceCodePreprocessing) -- ^ Source code
  -> (HCE.ModuleInfo, ModuleDependencies, [TypeError])
createModuleInfo (fileMap, defSiteMap, moduleNameMap) (flags, typecheckedModule, homePackageTable, externalPackageState, modSum) modulePath currentPackageId compId (originalSourceCode, sourceCodePreprocessing) =
  let globalRdrEnv = tcg_rdr_env . fst . tm_internals_ $ typecheckedModule
      modInfo = moduleInfo typecheckedModule
      (Just (hsGroup, _, _, _)) = renamedSource typecheckedModule
      exportedNamesSet = S.fromList $ modInfoExportsWithSelectors modInfo
      --------------------------------------------------------------------------------
      -- Preprocessed source
      --------------------------------------------------------------------------------
      (transformation, sourceCode') =
        prepareSourceCode
          sourceCodePreprocessing
          originalSourceCode
          modSum
          modulePath
      includedFiles = HM.keys $ HCE.fileIndex transformation
      --------------------------------------------------------------------------------
      -- Type environment
      --------------------------------------------------------------------------------
      (tcGblEnv, _) = tm_internals_ typecheckedModule
      currentModuleTyThings = typeEnvElts $ tcg_type_env tcGblEnv
      homePackageTyThings =
        concatMap (typeEnvElts . md_types . hm_details) $
#if MIN_VERSION_GLASGOW_HASKELL(8,2,2,0)        
        eltsUDFM homePackageTable
#else
        eltsUFM homePackageTable
#endif
      externalPackagesTyThings = typeEnvElts $ eps_PTE externalPackageState
      typeEnv =
        mkTypeEnv
          (currentModuleTyThings ++
           homePackageTyThings ++ externalPackagesTyThings)
      --------------------------------------------------------------------------------
      -- Exported entities
      --------------------------------------------------------------------------------
      dataFamTyCons =
        mapMaybe
          (\case
             ATyCon tc
               | isFamInstTyCon tc -> Just $ tyConName tc
             _ -> Nothing)
          currentModuleTyThings
      (defSites, allNames) =
        createDefinitionSiteMap
          flags
          currentPackageId
          compId
          defSiteMap
          fileMap          
          globalRdrEnv
          transformation
          modInfo
          dataFamTyCons
          hsGroup
      --------------------------------------------------------------------------------
      -- Instance environment
      --------------------------------------------------------------------------------
      homeInstEnv = tcg_inst_env tcGblEnv
      visOrphanModules = tcVisibleOrphanMods tcGblEnv
      packageInstEnv = eps_inst_env externalPackageState
      instEnv = InstEnvs packageInstEnv homeInstEnv visOrphanModules
      --------------------------------------------------------------------------------
      declarations =
        createDeclarations flags hsGroup typeEnv exportedNamesSet transformation
      environment =
        Environment
          { envDynFlags = flags
          , envInstEnv = instEnv
          , envTypeEnv = typeEnv
          , envTransformation = transformation
          , envCurrentModuleDefSites = defSites
          , envFileMap = fileMap
          , envDefSiteMap = defSiteMap
          , envModuleNameMap = moduleNameMap
          , envPackageId = currentPackageId
          , envComponentId = compId
          , envExportedNames = exportedNamesSet
          }
      externalIds =
        L.foldl'
          (\acc name ->
             maybe
               acc
               (\id -> (HCE.ExternalIdentifierInfo $ mkIdentifierInfo environment id (Just name)) : acc)
               (lookupIdInTypeEnv typeEnv name))
          []
          allNames
      currentModuleName =
        (\(Module _ name) ->
           HCE.HaskellModuleName . T.pack . moduleNameString $ name) .
        ms_mod $
        modSum
      SourceInfo {..} = foldAST environment typecheckedModule
   in (tidyInternalIds HCE.ModuleInfo
          { id = modulePath
          , transformation = transformation
          , name = currentModuleName
          , declarations = declarations
          , exprInfoMap = sourceInfoExprMap
          , idInfoMap = sourceInfoIdMap
          , idOccMap = sourceInfoIdOccMap
          , definitionSiteMap = defSites
          , source = V.fromList . T.splitOn "\n" $ sourceCode'
          , externalIds = externalIds
          }
      , if not $ isHsBoot modulePath
          then  (HM.union
                   (HM.fromList .
                    (( HCE.HaskellFilePath $ HCE.getHaskellModulePath modulePath
                     , modulePath) :) .
                    map (\includedFile -> (includedFile, modulePath)) $
                    includedFiles)
                   fileMap
               , HM.union (HM.singleton modulePath defSites) defSiteMap
               , HM.insertWith HM.union currentModuleName
                   (HM.singleton compId modulePath) moduleNameMap)
          else (fileMap, defSiteMap, moduleNameMap)
       , sourceInfoTypeErrors)

data SourceInfo = SourceInfo
  { sourceInfoExprMap :: HCE.ExpressionInfoMap
  , sourceInfoIdMap :: HCE.IdentifierInfoMap
  , sourceInfoIdOccMap :: HCE.IdentifierOccurrenceMap
  , sourceInfoTypeErrors :: [TypeError]
  } deriving (Show, Eq)

tidyInternalIds :: HCE.ModuleInfo -> HCE.ModuleInfo
tidyInternalIds modInfo = evalState (U.transformBiM tidy modInfo) (HM.empty, 0)
  where
    tidy ::
         HCE.InternalId -> State (HM.HashMap T.Text T.Text, Int) HCE.InternalId
    tidy (HCE.InternalId text) = do
      (hmap, number) <- get
      case HM.lookup text hmap of
        Just val -> return $ HCE.InternalId val
        Nothing -> do
          let nextInternalId = T.pack . show $ number
          put (HM.insert text nextInternalId hmap, number + 1)
          return $ HCE.InternalId nextInternalId

prepareSourceCode ::
     HCE.SourceCodePreprocessing
  -> T.Text
  -> ModSummary
  -> HCE.HaskellModulePath
  -> (HCE.SourceCodeTransformation, T.Text)
prepareSourceCode sourceCodePreprocessing originalSourceCode modSum modulePath =
  let sourceCodeAfterPreprocessing =
        case TE.decodeUtf8'
               (fromMaybe (error "ms_hspp_buf is Nothing") $
                stringBufferToByteString <$> ms_hspp_buf modSum) of
          Right text -> T.replace "\t" "        " text
          Left err ->
            error $
            "decodeUtf8' : " ++ show err ++ " , file : " ++ show modulePath
   in case sourceCodePreprocessing of
        HCE.BeforePreprocessing ->
          let sourceCodeLines = T.splitOn "\n" originalSourceCode
           in ( HCE.SourceCodeTransformation
                  (length sourceCodeLines)
                  modulePath
                  S.empty
                  HM.empty
              , originalSourceCode)
        HCE.AfterPreprocessing ->
          createSourceCodeTransformation
            modulePath
            originalSourceCode
            sourceCodeAfterPreprocessing

createDefinitionSiteMap ::
     DynFlags
  -> HCE.PackageId
  -> HCE.ComponentId
  -> HM.HashMap HCE.HaskellModulePath HCE.DefinitionSiteMap
  -> HM.HashMap HCE.HaskellFilePath HCE.HaskellModulePath
  -> GlobalRdrEnv
  -> HCE.SourceCodeTransformation
  -> ModuleInfo
  -> [Name]
  -> HsGroup Name
  -> (HCE.DefinitionSiteMap, [Name])
createDefinitionSiteMap flags currentPackageId compId defSiteMap fileMap globalRdrEnv transformation modInfo dataFamTyCons hsGroup =
  let allDecls :: [GenLocated SrcSpan (HsDecl Name)]
      allDecls = L.sortBy (comparing getLoc) . ungroup $ hsGroup
      (instanceDeclsWithDocs, valueAndTypeDeclsWithDocs) =
        L.partition
          (\(L _ decl, _) ->
             case decl of
               InstD _ -> True
               _ -> False) $
        collectDocs allDecls
      --------------------------------------------------------------------------------
      -- Instances
      --------------------------------------------------------------------------------
      -- No type instances or data instances here for now
      instanceDocMap :: M.Map SrcSpan [HsDocString]
      instanceDocMap =
        M.fromList .
        mapMaybe
          (\(L _n decl, docs) ->
             case decl of
               InstD (ClsInstD inst) -> Just (clsInstDeclSrcSpan inst, docs)
               _ -> Nothing) $
        instanceDeclsWithDocs
      nameLocation :: Maybe SrcSpan -> Name -> HCE.LocationInfo
      nameLocation =
        nameLocationInfo
          flags
          currentPackageId
          compId
          transformation
          fileMap
          defSiteMap
          Nothing
      docHToHtml :: DocH (ModuleName, OccName) Name -> HCE.HTML
      docHToHtml =
        docWithNamesToHtml
          flags
          currentPackageId
          compId
          transformation
          fileMap
          defSiteMap
      instancesWithDocumentation =
        HM.fromList .
        map
          (\clsInst ->
             ( instanceToText flags clsInst
             , let location =
                     nameLocation Nothing (Var.varName . is_dfun $ clsInst)
                in case M.lookup (getSrcSpan clsInst) instanceDocMap of
                     Just hsDocString ->
                       HCE.DefinitionSite
                         location
                         (Just . docHToHtml . hsDocsToDocH flags globalRdrEnv $
                          hsDocString)
                     Nothing -> HCE.DefinitionSite location Nothing)) $
        modInfoInstances modInfo -- all instances (including derived)
      --------------------------------------------------------------------------------
      -- Values and types
      --------------------------------------------------------------------------------
      mainDeclNamesWithDocumentation =
        concatMap
          (\(L span decl, docs) -> map (, docs, span) $ getMainDeclBinder decl)
          valueAndTypeDeclsWithDocs
      dataFamTyConsWithoutDocs =
        map (\name -> (name, [], nameSrcSpan name)) dataFamTyCons
      allNamesWithDocumentation =
        mainDeclNamesWithDocumentation ++
        subordinateNamesWithDocs allDecls ++ dataFamTyConsWithoutDocs
      (valuesWithDocumentation, typesWithDocumentation) =
        L.partition
          (\(name, _doc, _srcSpan) ->
             case occNameNameSpace . nameOccName $ name of
               HCE.VarName -> True
               HCE.DataName -> True
               _ -> False)
          allNamesWithDocumentation
      toHashMap ::
           [(Name, [HsDocString], SrcSpan)]
        -> HM.HashMap HCE.OccName HCE.DefinitionSite
      toHashMap =
        HM.fromListWith
          (\(HCE.DefinitionSite loc newDoc) (HCE.DefinitionSite _ oldDoc) ->
             (HCE.DefinitionSite loc $ mappend newDoc oldDoc)) .
        map
          (\(name, docs, srcSpan) ->
             let location = nameLocation (Just srcSpan) name
                 htmlDoc =
                   if not . null $ docs
                     then Just . docHToHtml . hsDocsToDocH flags globalRdrEnv $
                          docs
                     else Nothing
              in (HCE.OccName $ toText flags name, HCE.DefinitionSite location htmlDoc))
      --------------------------------------------------------------------------------
   in ( HCE.DefinitionSiteMap
          { HCE.values = toHashMap valuesWithDocumentation
          , HCE.types =
              toHashMap $ typesWithDocumentation ++ dataFamTyConsWithoutDocs
          , HCE.instances = instancesWithDocumentation
          }
      , map (\(n, _, _) -> n) allNamesWithDocumentation)

occNameToHtml ::
     DynFlags  
  -> HCE.PackageId
  -> HCE.ComponentId
  -> (ModuleName, OccName)
  -> H.Html
occNameToHtml flags packageId compId (modName, occName) =
  let location =
        H.textValue . toStrict . encodeToLazyText . Aeson.toJSON $
        occNameLocationInfo flags packageId compId (modName, occName)
   in (H.span H.! H.dataAttribute "location" location) H.! A.class_ "link" $
      H.toHtml (toText flags occName)

nameToHtml ::
     DynFlags
  -> HCE.PackageId
  -> HCE.ComponentId
  -> HCE.SourceCodeTransformation
  -> HM.HashMap HCE.HaskellFilePath HCE.HaskellModulePath
  -> HM.HashMap HCE.HaskellModulePath HCE.DefinitionSiteMap
  -> Name
  -> H.Html
nameToHtml flags packageId compId transformation files defSiteMap name =
  let location =
        H.textValue . toStrict . encodeToLazyText . Aeson.toJSON $
        nameLocationInfo
          flags
          packageId
          compId
          transformation
          files
          defSiteMap
          Nothing
          Nothing
          name
   in H.span H.! H.dataAttribute "location" location H.! A.class_ "link" $
      H.toHtml (nameToText name)
  
docWithNamesToHtml ::
     DynFlags
  -> HCE.PackageId
  -> HCE.ComponentId
  -> HCE.SourceCodeTransformation
  -> HM.HashMap HCE.HaskellFilePath HCE.HaskellModulePath
  -> HM.HashMap HCE.HaskellModulePath HCE.DefinitionSiteMap
  -> DocH (ModuleName, OccName) Name
  -> HCE.HTML
docWithNamesToHtml flags packageId compId transformation fileMap defSiteMap =
  HCE.docToHtml
    (occNameToHtml flags packageId compId)
    (nameToHtml flags packageId compId transformation fileMap defSiteMap)

createDeclarations ::
     DynFlags
  -> HsGroup Name
  -> TypeEnv
  -> S.Set Name
  -> HCE.SourceCodeTransformation
  -> [HCE.Declaration]
createDeclarations flags hsGroup typeEnv exportedSet transformation =
  let lineNumber :: SrcSpan -> Int
      lineNumber srcSpan =
        case srcSpanToLineAndColNumbers transformation srcSpan of
          Just (_file,(lineNum, _), (_, _)) -> lineNum
          Nothing -> 1
      nameType :: Name -> Maybe HCE.Type
      nameType n =
        case lookupIdInTypeEnv typeEnv n of
          Just i -> Just . mkType flags . varType $ i
          Nothing -> Nothing
      -- | Top-level functions
      --------------------------------------------------------------------------------
      valToDeclarations ::
           GenLocated SrcSpan (HsBindLR Name Name) -> [HCE.Declaration]
      valToDeclarations (L loc bind) =
        map
          (\name ->
             HCE.Declaration
               HCE.ValD
               (toText flags name)
               (nameType name)
               (S.member name exportedSet)
               (lineNumber loc)) $
        collectHsBindBinders bind
      vals = concatMap valToDeclarations $ hsGroupVals hsGroup
      -- | Data, newtype, type, type family, data family or class declaration
      --------------------------------------------------------------------------------
      tyClToDeclaration :: GenLocated SrcSpan (TyClDecl Name) -> HCE.Declaration
      tyClToDeclaration (L loc tyClDecl) =
        HCE.Declaration
          HCE.TyClD
          (T.append (tyClDeclPrefix tyClDecl) (toText flags $ tcdName tyClDecl))
          (nameType $ tcdName tyClDecl)
          (S.member (unLoc $ tyClDeclLName tyClDecl) exportedSet)
          (lineNumber loc)
      tyclds =
        map tyClToDeclaration .
        filter (isGoodSrcSpan . getLoc) . concatMap group_tyclds . hs_tyclds $
        hsGroup
      -- | Instances
      --------------------------------------------------------------------------------
      instToDeclaration :: GenLocated SrcSpan (InstDecl Name) -> HCE.Declaration
      instToDeclaration (L loc inst) =
        HCE.Declaration
          HCE.InstD
          (instanceDeclToText flags inst)
          Nothing
          True
          (lineNumber loc)
      insts =
#if MIN_VERSION_GLASGOW_HASKELL(8,2,2,0)        
        map instToDeclaration . filter (isGoodSrcSpan . getLoc) . hsGroupInstDecls $
#else
        map instToDeclaration . filter (isGoodSrcSpan . getLoc) . hs_instds $
#endif
        hsGroup
      -- | Foreign functions
      --------------------------------------------------------------------------------
      foreignFunToDeclaration ::
           GenLocated SrcSpan (ForeignDecl Name) -> HCE.Declaration
      foreignFunToDeclaration (L loc fd) =
        let name = unLoc $ fd_name fd
         in HCE.Declaration
              HCE.ForD
              (toText flags name)
              (nameType name)
              True
              (lineNumber loc)
      fords = map foreignFunToDeclaration $ hs_fords hsGroup
      --------------------------------------------------------------------------------
   in L.sortBy (comparing HCE.lineNumber) $ vals ++ tyclds ++ insts ++ fords

foldAST :: Environment -> TypecheckedModule -> SourceInfo
foldAST environment typecheckedModule =
  let (Just renamed@(_, importDecls, mbExported, _)) =
        renamedSource typecheckedModule
      emptyASTState =
        ASTState IVM.empty IM.empty M.empty emptyTidyEnv Nothing environment []
      ASTState {..} =
        execState
          (foldTypecheckedSource $ tm_typechecked_source typecheckedModule)
          emptyASTState
      -- A few things that are not in the output of the typechecker:
      --     - the export list
      --     - the imports
      --     - type signatures
      --     - type/data/newtype declarations
      --     - class declarations
      
      -- Both typechecked source and renamed source are used to populate
      -- 'IdentifierInfoMap' and 'IdentifierOccurrenceMap'
      (idInfoMap, idOccMap) =
        L.foldl'
          (addIdentifierToMaps environment astStateIdSrcSpanMap)
          (HM.empty, astStateIdOccMap)
          (namesFromRenamedSource renamed)          
      flags = envDynFlags environment
      packageId = envPackageId environment
      compId = envComponentId environment
      importedModules =
        map
          ((\(L span modName) ->
              ( modName
              , span
              , moduleLocationInfo
                  flags
                  (envModuleNameMap environment)
                  packageId
                  compId
                  modName)) .
           ideclName . unLoc) .
        filter (not . ideclImplicit . unLoc) $
        importDecls
      exportedModules =
        case mbExported of
          Just lieNames ->
            mapMaybe
              (\(L span ie) ->
                 case ie of
                   IEModuleContents (L _ modName) ->
                     Just
                       ( modName
                       , span
                       , moduleLocationInfo
                           flags
                           (envModuleNameMap environment)
                           packageId
                           compId
                           modName)
                   _ -> Nothing)
              lieNames
          Nothing -> []
      addImportedAndExportedModulesToIdOccMap ::
           HCE.IdentifierOccurrenceMap -> HCE.IdentifierOccurrenceMap
      addImportedAndExportedModulesToIdOccMap =
        IM.map (L.sortBy $ comparing fst) .
        addModules
          (envTransformation environment)
          (importedModules ++ exportedModules)
   in SourceInfo
        { sourceInfoExprMap = astStateExprInfoMap
        , sourceInfoIdMap = idInfoMap
        , sourceInfoIdOccMap = addImportedAndExportedModulesToIdOccMap idOccMap
        , sourceInfoTypeErrors = astStateTypeErrors
        }

-- | Updates 'IdentifierOccurrenceMap' and 'IdentifierInfoMap' using information
-- from typechecked source and renamed source
addIdentifierToMaps ::
     Environment
  -> M.Map SrcSpan (Id, Maybe (Type, [Type]))
  -> (HCE.IdentifierInfoMap, HCE.IdentifierOccurrenceMap)
  -> NameOccurrence
  -> (HCE.IdentifierInfoMap, HCE.IdentifierOccurrenceMap)
addIdentifierToMaps environment idSrcSpanMap idMaps@(idInfoMap, idOccMap) nameOcc
  | isGoodSrcSpan (getLoc $ locatedName nameOcc) &&
      isOneLineSpan (getLoc $ locatedName nameOcc)
  , Just (_, (lineNumber, startCol), (_, endCol)) <-
     srcSpanToLineAndColNumbers (envTransformation environment) .
     getLoc . locatedName $
     nameOcc =
    case nameOcc of
      TyLitOccurrence {kind = kind} ->
        addTypeToMaps
          environment
          idMaps
          (Just kind)
          Nothing
          (description nameOcc)
          lineNumber
          startCol
          endCol
      NameOccurrence {isBinder = isBinder} ->
        case lookupIdByNameOccurrence environment idSrcSpanMap nameOcc of
          Just (identifier, mbTypes) ->
            let name =
                  fromMaybe
                    (Var.varName identifier)
                    (unLoc $ locatedName nameOcc)
                identifierType = varType identifier
                identifierTypeExpanded = expandTypeSynonyms identifierType
                tyConsAndTyVars =
                  map
                    (, Nothing)
                    (tyConsOfType identifierType ++
                     tyVarsOfType identifierType ++
                     tyConsOfType identifierTypeExpanded ++
                     tyVarsOfType identifierTypeExpanded ++
                     maybe [] (tyConsOfType . fst) mbTypes ++
                     maybe [] (tyVarsOfType . fst) mbTypes)
                idInfoMap' =
                  updateIdMap
                    environment
                    ((identifier, unLoc $ locatedName nameOcc) : tyConsAndTyVars)
                    idInfoMap
                idOcc =
                  mkIdentifierOccurrence
                    environment
                    identifier
                    name
                    mbTypes
                    isBinder
                    (description nameOcc)
                idOccMap' =
                  IM.insertWith
                    removeOverlappingInterval
                    lineNumber
                    [((startCol, endCol), idOcc)]
                    idOccMap
             in (idInfoMap', idOccMap')
          Nothing -- type variable
           ->
            case unLoc $ locatedName nameOcc of
              Just name ->
                addTypeToMaps
                  environment
                  idMaps
                  Nothing
                  (Just name)
                  (description nameOcc)
                  lineNumber
                  startCol
                  endCol
              Nothing -> idMaps             
addIdentifierToMaps _ _ idMaps _ = idMaps

addTypeToMaps ::
     Environment
  -> (HCE.IdentifierInfoMap, HCE.IdentifierOccurrenceMap)
  -> Maybe Type
  -> Maybe Name
  -> T.Text
  -> Int
  -> Int
  -> Int
  -> (HCE.IdentifierInfoMap, HCE.IdentifierOccurrenceMap)
addTypeToMaps environment (idInfoMap, idOccMap) mbKind mbName descr lineNumber colStart colEnd =
  let flags = envDynFlags environment
      idInfoMap' =
        maybe
          idInfoMap
          (\kind ->
             updateIdMap
               environment
               (map (, Nothing) $ tyConsOfType kind ++ tyVarsOfType kind)
               idInfoMap)
          mbKind
      idOcc =
        HCE.IdentifierOccurrence
          { internalId = fmap (HCE.InternalId . nameKey) mbName
          , internalIdFromRenamedSource =
              HCE.InternalId . T.pack . show . getKey . nameUnique <$> mbName
          , isBinder = False
          , instanceResolution = Nothing
          , idOccType = mkType flags <$> mbKind
          , typeArguments = Nothing
          , description = descr
          , sort = HCE.TypeId
          }
      idOccMap' =
        IM.insertWith
          removeOverlappingInterval
          lineNumber
          [((colStart, colEnd), idOcc)]
          idOccMap
   in (idInfoMap', idOccMap')

lookupIdByNameOccurrence ::
     Environment
  -> M.Map SrcSpan (Id, Maybe (Type, [Type]))
  -> NameOccurrence
  -> Maybe (Id, Maybe (Type, [Type]))
lookupIdByNameOccurrence environment idSrcSpanMap (NameOccurrence (L span mbName) _ _) =
  case M.lookup span idSrcSpanMap of    
    Just (identifier, mbTypes) -> Just (identifier, mbTypes)
    Nothing ->
      case mbName of
        Just name ->
          case M.lookup (nameSrcSpan name) idSrcSpanMap of
            -- LHS of a Match
            Just (identifier, mbTypes) -> Just (identifier, mbTypes)
            Nothing ->
              -- Things that are not in the typechecked source
              case lookupIdInTypeEnv (envTypeEnv environment) name of
                Just t -> Just (t, Nothing)
                Nothing -> Nothing
        Nothing -> Nothing
lookupIdByNameOccurrence _ _ TyLitOccurrence {..} = Nothing
      
updateIdMap ::
     Environment
  -> [(Id, Maybe Name)]
  -> HCE.IdentifierInfoMap
  -> HCE.IdentifierInfoMap
updateIdMap environment ids identifiersMap =
  let flags = envDynFlags environment
      update ::
           HCE.IdentifierInfoMap -> (Id, Maybe Name) -> HCE.IdentifierInfoMap
      update idMap (identifier, mbName) =
        let info = mkIdentifierInfo environment identifier mbName
         in HM.insertWith
              (flip const)
              (HCE.InternalId $ identifierKey flags identifier)
              info
              idMap
   in L.foldl' update identifiersMap ids

addModules ::
     HCE.SourceCodeTransformation
  -> [(ModuleName, SrcSpan, HCE.LocationInfo)]
  -> HCE.IdentifierOccurrenceMap
  -> HCE.IdentifierOccurrenceMap
addModules transformation modules idMap =
  let update ::
           IM.IntMap [((Int, Int), HCE.IdentifierOccurrence)]
        -> (a, SrcSpan, HCE.LocationInfo)
        -> IM.IntMap [((Int, Int), HCE.IdentifierOccurrence)]
      update idOccMap (_modInfo, span, locInfo)
        | Just (_file,(lineNumber, colStart), (_, colEnd)) <-
           srcSpanToLineAndColNumbers transformation span =
          let idOcc =
                HCE.IdentifierOccurrence
                  { internalId = Nothing
                  , internalIdFromRenamedSource = Nothing
                  , isBinder = False
                  , instanceResolution = Nothing
                  , idOccType = Nothing
                  , typeArguments = Nothing
                  , description = "ImportDecl"
                  , sort = HCE.ModuleId locInfo
                  }
           in IM.insertWith
                removeOverlappingInterval
                lineNumber
                [((colStart, colEnd), idOcc)]
                idOccMap
      update idOccMap _ = idOccMap
   in L.foldl' update idMap modules