2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 HsImpExp: Abstract syntax: imports, exports, interfaces
9 {-# OPTIONS -fno-warn-incomplete-patterns #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
18 import Module ( ModuleName )
19 import HsDoc ( HsDoc )
23 import SrcLoc ( Located(..) )
24 import Char ( isAlpha )
27 %************************************************************************
29 \subsection{Import and export declaration lists}
31 %************************************************************************
33 One per \tr{import} declaration in a module.
35 type LImportDecl name = Located (ImportDecl name)
38 = ImportDecl (Located ModuleName) -- module name
39 Bool -- True <=> {-# SOURCE #-} import
40 Bool -- True => qualified
41 (Maybe ModuleName) -- as Module
42 (Maybe (Bool, [LIE name])) -- (True => hiding, names)
46 instance (Outputable name) => Outputable (ImportDecl name) where
47 ppr (ImportDecl mod from qual as spec)
48 = hang (hsep [ptext (sLit "import"), ppr_imp from,
49 pp_qual qual, ppr mod, pp_as as])
53 pp_qual True = ptext (sLit "qualified")
56 pp_as (Just a) = ptext (sLit "as ") <+> ppr a
58 ppr_imp True = ptext (sLit "{-# SOURCE #-}")
61 pp_spec Nothing = empty
62 pp_spec (Just (False, spec))
63 = parens (interpp'SP spec)
64 pp_spec (Just (True, spec))
65 = ptext (sLit "hiding") <+> parens (interpp'SP spec)
67 ideclName :: ImportDecl name -> Located ModuleName
68 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
71 %************************************************************************
73 \subsection{Imported and exported entities}
75 %************************************************************************
78 type LIE name = Located (IE name)
82 | IEThingAbs name -- Class/Type (can't tell)
83 | IEThingAll name -- Class/Type plus all methods/constructors
84 | IEThingWith name [name] -- Class/Type plus some methods/constructors
85 | IEModuleContents ModuleName -- (Export Only)
86 | IEGroup Int (HsDoc name) -- Doc section heading
87 | IEDoc (HsDoc name) -- Some documentation
88 | IEDocNamed String -- Reference to named doc
92 ieName :: IE name -> name
94 ieName (IEThingAbs n) = n
95 ieName (IEThingWith n _) = n
96 ieName (IEThingAll n) = n
98 ieNames :: IE a -> [a]
99 ieNames (IEVar n ) = [n]
100 ieNames (IEThingAbs n ) = [n]
101 ieNames (IEThingAll n ) = [n]
102 ieNames (IEThingWith n ns) = n:ns
103 ieNames (IEModuleContents _ ) = []
104 ieNames (IEGroup _ _ ) = []
105 ieNames (IEDoc _ ) = []
106 ieNames (IEDocNamed _ ) = []
110 instance (Outputable name) => Outputable (IE name) where
111 ppr (IEVar var) = pprHsVar var
112 ppr (IEThingAbs thing) = ppr thing
113 ppr (IEThingAll thing) = hcat [ppr thing, text "(..)"]
114 ppr (IEThingWith thing withs)
115 = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
116 ppr (IEModuleContents mod)
117 = ptext (sLit "module") <+> ppr mod
118 ppr (IEGroup n _) = text ("<IEGroup: " ++ (show n) ++ ">")
119 ppr (IEDoc doc) = ppr doc
120 ppr (IEDocNamed string) = text ("<IEDocNamed: " ++ string ++ ">")
124 pprHsVar :: Outputable name => name -> SDoc
125 pprHsVar v | isOperator ppr_v = parens ppr_v
130 isOperator :: SDoc -> Bool
132 = case showSDocUnqual ppr_v of
133 ('(':_) -> False -- (), (,) etc
134 ('[':_) -> False -- []
135 ('$':c:_) -> not (isAlpha c) -- Don't treat $d as an operator
136 (':':c:_) -> not (isAlpha c) -- Don't treat :T as an operator
137 ('_':_) -> False -- Not an operator
138 (c:_) -> not (isAlpha c) -- Starts with non-alpha
140 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
141 -- that we don't need NamedThing in the context of all these functions.
142 -- Gruesome, but simple.