@@ -359,21 +359,14 @@ filterExports mn exps env = do
359 else throwError $ mkErrorStack ("Cannot export undefined value '" ++ show name ++ "'") Nothing
359 else throwError $ mkErrorStack ("Cannot export undefined value '" ++ show name ++ "'") Nothing
360 filterValues _ result _ = return result
360 filterValues _ result _ = return result
363−-- Type representing a set of declarations being explicitly imported from a module
365−type ExplicitImports = [DeclarationRef]
367−type HiddenImports = [DeclarationRef]
370-- Finds the imports within a module, mapping the imported module name to an optional set of
363-- Finds the imports within a module, mapping the imported module name to an optional set of
371-- explicitly imported declarations.
364-- explicitly imported declarations.
373−findImports :: [Declaration] -> M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe HiddenImports, Maybe ModuleName)
366+findImports :: [Declaration] -> M.Map ModuleName (Maybe SourcePos, ImportDeclarationType, Maybe ModuleName)
374findImports = foldl (findImports' Nothing) M.empty
367findImports = foldl (findImports' Nothing) M.empty
376− findImports' pos result (ImportDeclaration mn expl hidden qual) = M.insert mn (pos, expl, hidden, qual) result
369+ findImports' pos result (ImportDeclaration mn typ qual) = M.insert mn (pos, typ, qual) result
377 findImports' _ result (PositionedDeclaration pos d) = findImports' (Just pos) result d
370 findImports' _ result (PositionedDeclaration pos d) = findImports' (Just pos) result d
378 findImports' _ result _ = result
371 findImports' _ result _ = result
@@ -388,13 +381,13 @@ resolveImports env (Module currentModule decls _) =
388 -- A Map from module name to the source position for the import, the list of imports from that
381 -- A Map from module name to the source position for the import, the list of imports from that
389 -- module (where Nothing indicates everything is to be imported), and optionally a qualified name
382 -- module (where Nothing indicates everything is to be imported), and optionally a qualified name
390 -- for the module
383 -- for the module
391− scope :: M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe HiddenImports, Maybe ModuleName)
384+ scope :: M.Map ModuleName (Maybe SourcePos, ImportDeclarationType, Maybe ModuleName)
392− scope = M.insert currentModule (Nothing, Nothing, Nothing, Nothing) (findImports decls)
385+ scope = M.insert currentModule (Nothing, Unqualified, Nothing) (findImports decls)
394− resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourcePos, Maybe ExplicitImports, Maybe HiddenImports, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
387+ resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourcePos, ImportDeclarationType, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
395− resolveImport' imp (mn, (pos, explImports, hiddenImports, impQual)) = do
388+ resolveImport' imp (mn, (pos, typ, impQual)) = do
396 modExports <- positioned $ maybe (throwError $ mkErrorStack ("Cannot import unknown module '" ++ show mn ++ "'") Nothing) return $ mn `M.lookup` env
389 modExports <- positioned $ maybe (throwError $ mkErrorStack ("Cannot import unknown module '" ++ show mn ++ "'") Nothing) return $ mn `M.lookup` env
397− positioned $ resolveImport currentModule mn modExports imp impQual explImports hiddenImports
390+ positioned $ resolveImport currentModule mn modExports imp impQual typ
399 positioned err = case pos of
392 positioned err = case pos of
400 Nothing -> err
393 Nothing -> err
@@ -403,23 +396,41 @@ resolveImports env (Module currentModule decls _) =
404-- Extends the local environment for a module by resolving an import of another module.
397-- Extends the local environment for a module by resolving an import of another module.
406−resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ModuleName -> Maybe ExplicitImports -> Maybe HiddenImports -> Either ErrorStack ImportEnvironment
399+resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ModuleName -> ImportDeclarationType -> Either ErrorStack ImportEnvironment
407−resolveImport currentModule importModule exps imps impQual explImports hiddenImports =
400+resolveImport currentModule importModule exps imps impQual =
408− maybe importAll (foldM (\m -> checkRefIsValid >=> (importExplicit m)) imps) explImports
401+ resolveByType
411− -- Import everything from a module
404+ resolveByType :: ImportDeclarationType -> Either ErrorStack ImportEnvironment
405+ resolveByType Unqualified = importAll importExplicit
406+ resolveByType (Qualifying explImports) = (checkedRefs >=> foldM importExplicit imps) explImports
407+ resolveByType (Hiding hiddenImports) = do
408+ hiddenImports' <- checkedRefs hiddenImports
409+ importAll (importNonHidden hiddenImports')
411+ importNonHidden :: [DeclarationRef] -> ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
412+ importNonHidden hidden m ref =
413+ if isHidden hidden ref
415+ else importExplicit m ref
417+ isHidden :: [DeclarationRef] -> DeclarationRef -> Bool
418+ isHidden hidden ref@(TypeRef _ _) =
420+ checkTypeRef _ True _ = True
421+ checkTypeRef (TypeRef _ Nothing) acc (TypeRef _ (Just _)) = acc
422+ checkTypeRef (TypeRef name (Just dctor)) _ (TypeRef name' (Just dctor')) = name == name' && dctor == dctor'
423+ checkTypeRef (TypeRef name _) _ (TypeRef name' Nothing) = name == name'
424+ checkTypeRef _ acc _ = acc
425+ in foldl (checkTypeRef ref) False hidden
426+ isHidden hidden ref = ref `elem` hidden
428+ -- Import all symbols
412− importAll :: Either ErrorStack ImportEnvironment
429+ importAll :: (ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment) -> Either ErrorStack ImportEnvironment
413− importAll = do
430+ importAll importer = do
414− imp' <- foldM (\m (name, dctors) -> importExplicitNotHidden m (TypeRef name (Just dctors))) imps (exportedTypes exps)
431+ imp' <- foldM (\m (name, dctors) -> importer m (TypeRef name (Just dctors))) imps (exportedTypes exps)
415− imp'' <- foldM (\m name -> importExplicitNotHidden m (ValueRef name)) imp' (exportedValues exps)
432+ imp'' <- foldM (\m name -> importer m (ValueRef name)) imp' (exportedValues exps)
416− foldM (\m name -> importExplicitNotHidden m (TypeClassRef name)) imp'' (exportedTypeClasses exps)
433+ foldM (\m name -> importer m (TypeClassRef name)) imp'' (exportedTypeClasses exps)
418− -- Import something explicitly only if it's not hidden
419− importExplicitNotHidden :: ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
420− importExplicitNotHidden m value = do
421− hidden <- hiddenImports'
422− if elem value hidden then (Right m) else importExplicit m value
424 -- Import something explicitly
435 -- Import something explicitly
425 importExplicit :: ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
436 importExplicit :: ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
@@ -438,22 +449,22 @@ resolveImport currentModule importModule exps imps impQual explImports hiddenImp
438 return $ imp { importedTypeClasses = typeClasses' }
449 return $ imp { importedTypeClasses = typeClasses' }
439 importExplicit _ _ = error "Invalid argument to importExplicit"
450 importExplicit _ _ = error "Invalid argument to importExplicit"
441− -- hiddenImports checked for validness
443− mapM checkRefIsValid (fromMaybe [] hiddenImports)
445 -- Check if DeclarationRef points to a existent symbol
452 -- Check if DeclarationRef points to a existent symbol
446− checkRefIsValid :: DeclarationRef -> Either ErrorStack DeclarationRef
453+ checkedRefs :: [DeclarationRef] -> Either ErrorStack [DeclarationRef]
454+ checkedRefs refs = mapM check refs
447− checkRefIsValid ref@(PositionedDeclarationRef pos r) = do
456+ check (PositionedDeclarationRef pos r) = do
448− _ <- rethrowWithPosition pos $ checkRefIsValid r
457+ rethrowWithPosition pos $ check r
450− checkRefIsValid ref@(ValueRef name) =
458+ check ref@(ValueRef name) =
451− checkImportExists "value" values name >> return ref
459+ checkImportExists "value" values name >> return ref
452− checkRefIsValid ref@(TypeRef name _) =
460+ check ref@(TypeRef name dctors) = do
453− checkImportExists "type" availableTypes name >> return ref
461+ _ <- checkImportExists "type" availableTypes name
462+ let allDctors = allExportedDataConstructors name
463+ _ <- maybe (return allDctors) (mapM $ checkDctorExists allDctors) dctors
454− checkRefIsValid ref@(TypeClassRef name) =
465+ check ref@(TypeClassRef name) =
455− checkImportExists "type class" classes name >> return ref
466+ checkImportExists "type class" classes name >> return ref
456− checkRefIsValid _ = error "Invalid argument to checkRefIsValid"
467+ check _ = error "Invalid argument to checkRefIsValid"
458 -- Find all exported data constructors for a given type
469 -- Find all exported data constructors for a given type
459 allExportedDataConstructors :: ProperName -> [ProperName]
470 allExportedDataConstructors :: ProperName -> [ProperName]