@@ -1,8 +1,8 @@
11-----------------------------------------------------------------------------
22--
33-- Module : Language.PureScript.Sugar.Names
4−-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5−-- License : MIT
4+-- Copyright : (c) 2013-15 Phil Freeman, (c) 2014-15 Gary Burgess
5+-- License : MIT (http://opensource.org/licenses/MIT)
66--
77-- Maintainer : Phil Freeman <paf31@cantab.net>, Gary Burgess <gary.burgess@gmail.com>
88-- Stability : experimental
@@ -49,9 +49,13 @@ type ExportEnvironment = M.Map ModuleName Exports
4949data Exports = Exports
5050 {
5151 -- |
52+ -- The filename of the module these exports are for
53+ --
54+ moduleSourceSpan :: SourceSpan
55+ -- |
5256 -- The types exported from each module
5357 --
54− exportedTypes :: [(ProperName, [ProperName])]
58+ , exportedTypes :: [(ProperName, [ProperName])]
5559 -- |
5660 -- The classes exported from each module
5761 --
@@ -102,11 +106,11 @@ updateExportedModule env mn update = do
102106-- |
103107-- Adds an empty module to an ExportEnvironment.
104108--
105−addEmptyModule :: (Applicative m, MonadError MultipleErrors m) => ExportEnvironment -> ModuleName -> m ExportEnvironment
106−addEmptyModule env name =
107− if name `M.member` env
108− then throwError . errorMessage $ RedefinedModule name
109− else return $ M.insert name (Exports [] [] [] []) env
109+addEmptyModule :: (Applicative m, MonadError MultipleErrors m) => ExportEnvironment -> ModuleName -> SourceSpan -> m ExportEnvironment
110+addEmptyModule env name ss =
111+ case name `M.lookup` env of
112+ Just m -> throwError . errorMessage $ RedefinedModule name [moduleSourceSpan m, ss]
113+ Nothing -> return $ M.insert name (Exports ss [] [] [] []) env
110114
111115-- |
112116-- Adds a type belonging to a module to the export environment.
@@ -164,9 +168,9 @@ desugarImports modules = do
164168 mapM (renameInModule' unfilteredExports exports) modules'
165169 where
166170 moduleReexports :: Module -> Module
167− moduleReexports (Module coms mn decls exps) =
171+ moduleReexports (Module ss coms mn decls exps) =
168172 let importedMods = catMaybes findImports'
169− in (Module coms mn (decls ++ (concatMap reexports importedMods)) exps)
173+ in (Module ss coms mn (decls ++ (concatMap reexports importedMods)) exps)
170174 where
171175 imports :: [Declaration]
172176 imports = filter isImportDecl decls
@@ -174,7 +178,7 @@ desugarImports modules = do
174178 findImports' = go <$> modules
175179 where
176180 go :: Module -> Maybe (Declaration, Module)
177− go m@(Module _ mn' _ (Just exps'))
181+ go m@(Module _ _ mn' _ (Just exps'))
178182 | any isModExport exps', Just d <- find ((== mn') . importedModName) imports = Just (d, m)
179183 where
180184 importedModName :: Declaration -> ModuleName
@@ -183,14 +187,14 @@ desugarImports modules = do
183187 importedModName _ = error "Not an import decl"
184188 go _ = Nothing
185189 reexports :: (Declaration, Module) -> [Declaration]
186− reexports (ImportDeclaration _ (Hiding refs) _, (Module coms' mn' ds' (Just exps'))) =
190+ reexports (ImportDeclaration _ (Hiding refs) _, (Module ss' coms' mn' ds' (Just exps'))) =
187191 case nonHiddenRefs of
188192 [] -> []
189− _ -> reexports (ImportDeclaration mn' Implicit Nothing, Module coms' mn' ds' (Just nonHiddenRefs))
193+ _ -> reexports (ImportDeclaration mn' Implicit Nothing, Module ss' coms' mn' ds' (Just nonHiddenRefs))
190194 where
191195 nonHiddenRefs :: [DeclarationRef]
192196 nonHiddenRefs = filter isModExport exps' \\ filter isModExport refs
193− reexports (ImportDeclaration _ ty qual, Module _ _ _ (Just exps')) =
197+ reexports (ImportDeclaration _ ty qual, Module _ _ _ _ (Just exps')) =
194198 let ty' = case ty of
195199 Explicit _ -> Explicit []
196200 _ -> ty
@@ -199,7 +203,7 @@ desugarImports modules = do
199203 go :: DeclarationRef -> Maybe ModuleName
200204 go (ModuleRef mn') = Just mn'
201205 go _ = Nothing
202− reexports (PositionedDeclaration _ _ d, m@(Module _ _ _ (Just _))) = reexports (d, m)
206+ reexports (PositionedDeclaration _ _ d, m@(Module _ _ _ _ (Just _))) = reexports (d, m)
203207 reexports _ = []
204208
205209 isModExport :: DeclarationRef -> Bool
@@ -210,7 +214,7 @@ desugarImports modules = do
210214 -- exported members remain. If the module does not explicitly export anything, everything is
211215 -- exported.
212216 filterModuleExports :: ExportEnvironment -> Module -> m ExportEnvironment
213− filterModuleExports env (Module _ mn _ (Just exps))
217+ filterModuleExports env (Module _ _ mn _ (Just exps))
214218 | any isSelfModuleExport exps, Just exps' <- M.lookup mn env =
215219 let moduleNames = filter (/= mn) $ (\(ModuleRef mn') -> mn') <$> filter isModExport exps
216220 in return $ M.insert mn (exps' {exportedModules = moduleNames}) env
@@ -219,13 +223,13 @@ desugarImports modules = do
219223 isSelfModuleExport (ModuleRef mn') | mn' == mn = True
220224 isSelfModuleExport (PositionedDeclarationRef _ _ ref) = isSelfModuleExport ref
221225 isSelfModuleExport _ = False
222− filterModuleExports env (Module _ mn _ (Just exps)) = filterExports mn exps env
226+ filterModuleExports env (Module _ _ mn _ (Just exps)) = filterExports mn exps env
223227 filterModuleExports env _ = return env
224228
225229 -- Rename and check all the names within a module. We tweak the global exports environment so
226230 -- the module has access to an unfiltered list of its own members.
227231 renameInModule' :: ExportEnvironment -> ExportEnvironment -> Module -> m Module
228− renameInModule' unfilteredExports exports m@(Module _ mn _ _) =
232+ renameInModule' unfilteredExports exports m@(Module _ _ mn _ _) =
229233 rethrow (onErrorMessages (ErrorInModule mn)) $ do
230234 let env = M.update (\_ -> M.lookup mn unfilteredExports) mn exports
231235 let exps = fromMaybe (error "Module is missing in renameInModule'") $ M.lookup mn exports
@@ -237,7 +241,7 @@ desugarImports modules = do
237241-- as it will also make all data constructor exports explicit.
238242--
239243elaborateExports :: Exports -> Module -> Module
240−elaborateExports exps (Module coms mn decls _) = Module coms mn decls (Just $
244+elaborateExports exps (Module ss coms mn decls _) = Module ss coms mn decls (Just $
241245 map (\(ctor, dctors) -> TypeRef ctor (Just dctors)) (exportedTypes exps) ++
242246 map TypeClassRef (exportedTypeClasses exps) ++
243247 map ValueRef (exportedValues exps) ++
@@ -248,7 +252,7 @@ elaborateExports exps (Module coms mn decls _) = Module coms mn decls (Just $
248252-- This ensures transitive instances are included when using a member from a module.
249253--
250254elaborateImports :: Module -> Module
251−elaborateImports (Module coms mn decls exps) = Module coms mn decls' exps
255+elaborateImports (Module ss coms mn decls exps) = Module ss coms mn decls' exps
252256 where
253257 decls' :: [Declaration]
254258 decls' =
@@ -265,8 +269,8 @@ elaborateImports (Module coms mn decls exps) = Module coms mn decls' exps
265269-- qualified names are valid.
266270--
267271renameInModule :: forall m. (Applicative m, MonadError MultipleErrors m) => ImportEnvironment -> ExportEnvironment -> Module -> m Module
268−renameInModule imports exports (Module coms mn decls exps) =
269− Module coms mn <$> parU decls go <*> pure exps
272+renameInModule imports exports (Module ss coms mn decls exps) =
273+ Module ss coms mn <$> parU decls go <*> pure exps
270274 where
271275 (go, _, _, _, _) = everywhereWithContextOnValuesM (Nothing, []) updateDecl updateValue updateBinder updateCase defS
272276
@@ -372,14 +376,14 @@ findExports = foldM addModule $ M.singleton (ModuleName [ProperName C.prim]) pri
372376 where
373377
374378 -- The exported types from the Prim module
375− primExports = Exports (mkTypeEntry `map` M.keys primTypes) [] [] []
379+ primExports = Exports (internalModuleSourceSpan "<Prim>") (mkTypeEntry `map` M.keys primTypes) [] [] []
376380 where
377381 mkTypeEntry (Qualified _ name) = (name, [])
378382
379383 -- Add all of the exported declarations from a module to the global export environment
380384 addModule :: ExportEnvironment -> Module -> m ExportEnvironment
381− addModule env (Module _ mn ds _) = do
382− env' <- addEmptyModule env mn
385+ addModule env (Module ss _ mn ds _) = do
386+ env' <- addEmptyModule env mn ss
383387 rethrow (onErrorMessages (ErrorInModule mn)) $ foldM (addDecl mn) env' ds
384388
385389 -- Add a declaration from a module to the global export environment
@@ -475,7 +479,7 @@ findImports = foldl (findImports' Nothing) M.empty
475479-- Constructs a local environment for a module.
476480--
477481resolveImports :: forall m. (Applicative m, MonadError MultipleErrors m) => ExportEnvironment -> Module -> m ImportEnvironment
478−resolveImports env (Module _ currentModule decls _) =
482+resolveImports env (Module _ _ currentModule decls _) =
479483 foldM resolveImport' (ImportEnvironment M.empty M.empty M.empty M.empty) (M.toList scope)
480484 where
481485