HsImpExp.lhs 3.76 KB
Newer Older
1
%
2
% (c) The University of Glasgow 2006
3
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
%
5 6

HsImpExp: Abstract syntax: imports, exports, interfaces
7 8

\begin{code}
9
{-# OPTIONS -fno-warn-incomplete-patterns #-}
10 11 12
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and fix
-- any warnings in the module. See
Ian Lynagh's avatar
Ian Lynagh committed
13
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
14 15
-- for details

16 17
module HsImpExp where

Simon Marlow's avatar
Simon Marlow committed
18
import Module		( ModuleName )
19 20
import HsDoc		( HsDoc )

21
import Outputable
22
import FastString
23
import SrcLoc		( Located(..) )
24 25 26 27 28 29 30 31 32 33
\end{code}

%************************************************************************
%*									*
\subsection{Import and export declaration lists}
%*									*
%************************************************************************

One per \tr{import} declaration in a module.
\begin{code}
34 35
type LImportDecl name = Located (ImportDecl name)

36
data ImportDecl name
Simon Marlow's avatar
Simon Marlow committed
37
  = ImportDecl	  (Located ModuleName)		-- module name
38
		  Bool				-- True <=> {-# SOURCE #-} import
39
		  Bool				-- True => qualified
Simon Marlow's avatar
Simon Marlow committed
40
		  (Maybe ModuleName)		-- as Module
41
		  (Maybe (Bool, [LIE name]))	-- (True => hiding, names)
42 43 44
\end{code}

\begin{code}
45
instance (Outputable name) => Outputable (ImportDecl name) where
46
    ppr (ImportDecl mod from qual as spec)
Ian Lynagh's avatar
Ian Lynagh committed
47
      = hang (hsep [ptext (sLit "import"), ppr_imp from, 
48
                    pp_qual qual, ppr mod, pp_as as])
49
	     4 (pp_spec spec)
50
      where
sof's avatar
sof committed
51
	pp_qual False   = empty
Ian Lynagh's avatar
Ian Lynagh committed
52
	pp_qual True	= ptext (sLit "qualified")
53

sof's avatar
sof committed
54
	pp_as Nothing   = empty
Ian Lynagh's avatar
Ian Lynagh committed
55
	pp_as (Just a)  = ptext (sLit "as ") <+> ppr a
56

Ian Lynagh's avatar
Ian Lynagh committed
57
	ppr_imp True  = ptext (sLit "{-# SOURCE #-}")
58 59
	ppr_imp False = empty

sof's avatar
sof committed
60
	pp_spec Nothing = empty
61
	pp_spec (Just (False, spec))
62
			= parens (interpp'SP spec)
63
	pp_spec (Just (True, spec))
Ian Lynagh's avatar
Ian Lynagh committed
64
			= ptext (sLit "hiding") <+> parens (interpp'SP spec)
65

66
ideclName :: ImportDecl name -> Located ModuleName
67
ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
68 69 70 71 72 73 74
\end{code}

%************************************************************************
%*									*
\subsection{Imported and exported entities}
%*									*
%************************************************************************
75

76
\begin{code}
77 78
type LIE name = Located (IE name)

79
data IE name
80 81 82 83 84 85 86 87
  = IEVar               name
  | IEThingAbs          name		 -- Class/Type (can't tell)
  | IEThingAll          name		 -- Class/Type plus all methods/constructors
  | IEThingWith         name [name]	 -- Class/Type plus some methods/constructors
  | IEModuleContents    ModuleName	 -- (Export Only)
  | IEGroup             Int (HsDoc name) -- Doc section heading
  | IEDoc               (HsDoc name)     -- Some documentation
  | IEDocNamed          String           -- Reference to named doc
88 89
\end{code}

90 91 92 93 94 95
\begin{code}
ieName :: IE name -> name
ieName (IEVar n) 	 = n
ieName (IEThingAbs  n)   = n
ieName (IEThingWith n _) = n
ieName (IEThingAll  n)   = n
96 97 98 99 100 101 102

ieNames :: IE a -> [a]
ieNames (IEVar            n   ) = [n]
ieNames (IEThingAbs       n   ) = [n]
ieNames (IEThingAll       n   ) = [n]
ieNames (IEThingWith      n ns) = n:ns
ieNames (IEModuleContents _   ) = []
103 104 105
ieNames (IEGroup          _ _ ) = []
ieNames (IEDoc            _   ) = []
ieNames (IEDocNamed       _   ) = []        
106 107
\end{code}

108
\begin{code}
109
instance (Outputable name) => Outputable (IE name) where
110
    ppr (IEVar	        var)	= pprHsVar var
111 112 113
    ppr (IEThingAbs	thing)	= ppr thing
    ppr (IEThingAll	thing)	= hcat [ppr thing, text "(..)"]
    ppr (IEThingWith thing withs)
114
	= ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
115
    ppr (IEModuleContents mod)
Ian Lynagh's avatar
Ian Lynagh committed
116
	= ptext (sLit "module") <+> ppr mod
117
    ppr (IEGroup n _)           = text ("<IEGroup: " ++ (show n) ++ ">")
118 119
    ppr (IEDoc doc)             = ppr doc
    ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
120 121
\end{code}

122