-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathImports.hs
266 lines (242 loc) · 8.52 KB
/
Imports.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
256
257
258
259
260
261
262
263
264
265
266
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-} -- ModName (ModuleName l)
module Language.Haskell.Names.Imports
( importTable
, annotateImportDecls
)
where
import Data.Maybe
import Data.Either
import Control.Monad.Writer
import Data.Map as Map (lookup)
import Language.Haskell.Exts (
Module(Module), ModuleName(ModuleName), ImportDecl(..),
ann,ImportSpecList(..),ImportSpec(..),Name(..),
Annotated)
import Language.Haskell.Exts.Extension (
Extension(DisableExtension), KnownExtension(ImplicitPrelude))
import Language.Haskell.Names.Types
import Language.Haskell.Names.ScopeUtils
import qualified Language.Haskell.Names.GlobalSymbolTable as Global
import Language.Haskell.Names.SyntaxUtils
import Data.List ((\\))
-- | Compute a table of symbols imported by the given module from the given
-- environment.
importTable :: Environment -> Module l -> Global.Table
importTable environment modul =
foldr Global.mergeTables perhapsPrelude tables where
tables = map (importDeclTable environment) importDecls
Module _ _ _ importDecls _ = modul
perhapsPrelude = if noImplicitPrelude
then Global.empty
else computeSymbolTable False preludeModuleName preludeSymbols
noImplicitPrelude =
DisableExtension ImplicitPrelude `elem` extensions || isPreludeImported
isPreludeImported = not (null (do
importDecl <- importDecls
guard (dropAnn (importModule importDecl) == preludeModuleName)))
preludeSymbols = fromMaybe [] (Map.lookup preludeModuleName environment)
(_, extensions) = getModuleExtensions modul
preludeModuleName :: ModuleName ()
preludeModuleName = ModuleName () "Prelude"
importDeclTable :: Environment -> ImportDecl l -> Global.Table
importDeclTable environment importDecl =
computeSymbolTable isQualified moduleName importSymbols where
ImportDecl _ importModuleName isQualified _ _ _ maybeAs maybeImportSpecList =
importDecl
moduleName = dropAnn (fromMaybe importModuleName maybeAs)
importSymbols = case maybeImportSpecList of
Nothing ->
importModuleSymbols
Just importSpecList ->
importSpecListSymbols importModuleName importModuleSymbols importSpecList
importModuleSymbols = fromMaybe [] (
Map.lookup (dropAnn importModuleName) environment)
importSpecListSymbols :: ModuleName l -> [Symbol] -> ImportSpecList l -> [Symbol]
importSpecListSymbols importModuleName allSymbols importSpecList =
if isHiding
then allSymbols \\ mentionedSymbols
else mentionedSymbols where
ImportSpecList _ isHiding importSpecs = importSpecList
annotatedImportSpecs =
map (resolveImportSpec importModuleName isHiding allSymbols) importSpecs
mentionedSymbols =
mconcat (rights (map ann2syms annotatedImportSpecs))
-- | Annotate the given list of import declarations with scoping information
-- against the given environment. We need the name of the module that contains
-- the import declarations for error annotations.
annotateImportDecls ::
ModuleName l -> Environment -> [ImportDecl l] -> [ImportDecl (Scoped l)]
annotateImportDecls moduleName environment importDecls =
map (annotateImportDecl moduleName environment) importDecls
annotateImportDecl ::
ModuleName l -> Environment -> ImportDecl l -> ImportDecl (Scoped l)
annotateImportDecl moduleName environment importDecl = importDecl' where
ImportDecl
l
importModuleName
isQualified
isSource
isSafe
importPackage
maybeAs
maybeImportSpecList = importDecl
importDecl' = case Map.lookup (dropAnn importModuleName) environment of
Nothing -> scopeError (EModNotFound importModuleName) importDecl
Just symbols ->
ImportDecl
l'
importModuleName'
isQualified
isSource
isSafe
importPackage
maybeAs'
maybeImportSpecList' where
l' = Scoped info l
importModuleName' = fmap (Scoped (ImportPart symbols)) importModuleName
maybeAs' = fmap noScope maybeAs
maybeImportSpecList' =
fmap (annotateImportSpecList moduleName symbols) maybeImportSpecList
info = case maybeImportSpecList' of
Just sl | Scoped (ScopeError e) _ <- ann sl ->
ScopeError e
_ -> Import table
table = computeSymbolTable isQualified qualificationName importSymbols
qualificationName = dropAnn (fromMaybe importModuleName maybeAs)
importSymbols =
maybe
symbols
(importSpecListSymbols moduleName symbols)
maybeImportSpecList
annotateImportSpecList ::
ModuleName l -> [Symbol] -> ImportSpecList l -> ImportSpecList (Scoped l)
annotateImportSpecList moduleName allSymbols importSpecList =
(ImportSpecList l' isHiding importSpecs') where
ImportSpecList l isHiding importSpecs = importSpecList
l' = Scoped (ImportPart importSymbols) l
importSpecs' = map (resolveImportSpec moduleName isHiding allSymbols) importSpecs
importSymbols = importSpecListSymbols moduleName allSymbols importSpecList
resolveImportSpec
:: ModuleName l
-> Bool
-> [Symbol]
-> ImportSpec l
-> ImportSpec (Scoped l)
-- NB: this can be made more efficient
resolveImportSpec mod isHiding symbols spec =
case spec of
IVar _ n ->
let
matches =
-- Strictly speaking, the isConstructor check is unnecessary
-- because constructors are lexically different from anything
-- else.
[ symbol
| symbol <- symbols
, not (isConstructor symbol)
, symbol ~~ n]
in
checkUnique
(ENotExported Nothing n mod)
matches
spec
-- FIXME think about data families etc.
IAbs _ _ n
| isHiding ->
-- This is a bit special. 'C' may match both types/classes and
-- data constructors.
-- FIXME Still check for uniqueness?
let
matches = [ symbol | symbol <- symbols, symbol ~~ n]
in
if null matches
then
scopeError (ENotExported Nothing n mod) spec
else
Scoped (ImportPart matches) <$> spec
| otherwise ->
let
matches = [symbol | symbol <- symbols, symbol ~~ n, not (isConstructor symbol)]
in
checkUnique
(ENotExported Nothing n mod)
matches
spec
IThingAll l n ->
let
matches = [ symbol | symbol <- symbols, symbol ~~ n, hasSubImports symbol]
subs = [ symbol
| n <- matches
, symbol <- symbols
, Just n' <- return $ symbolParent symbol
, n' == symbolName n ]
n' =
checkUnique
(ENotExported Nothing n mod)
matches
n
in
case ann n' of
e@(Scoped ScopeError{} _) -> IThingAll e n'
_ ->
IThingAll
(Scoped
(ImportPart (subs <> matches))
l
)
n'
IThingWith l n cns ->
let
matches = [symbol | symbol <- symbols, symbol ~~ n, hasSubImports symbol]
n' =
checkUnique
(ENotExported Nothing n mod)
matches
n
typeName = symbolName $ head matches -- should be safe
(cns', cnSyms) =
resolveCNames
symbols
typeName
(\cn -> ENotExported (Just n) (unCName cn) mod)
cns
in
IThingWith
(Scoped
(ImportPart (cnSyms <> matches))
l
)
n'
cns'
where
(~~) :: Symbol -> Name l -> Bool
symbol ~~ name = symbolName symbol == dropAnn name
isConstructor :: Symbol -> Bool
isConstructor Constructor {} = True
isConstructor _ = False
hasSubImports :: Symbol -> Bool
hasSubImports symbol = case symbol of
Data {} -> True
NewType {} -> True
DataFam {} -> True
Class {} -> True
_ -> False
ann2syms :: Annotated a => a (Scoped l) -> Either (Error l) ([Symbol])
ann2syms a =
case ann a of
Scoped (ScopeError e) _ -> Left e
Scoped (ImportPart syms) _ -> Right syms
_ -> Left $ EInternal "ann2syms"
checkUnique
:: Functor f =>
Error l ->
[Symbol] ->
f l ->
f (Scoped l)
checkUnique notFound symbols f =
case length symbols of
0 -> scopeError notFound f
1 -> Scoped (ImportPart symbols) <$> f
-- there should be no clashes, and it should be checked elsewhere
_ -> scopeError (EInternal ("ambiguous import: " ++ show symbols)) f