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 #include "HsVersions.h"
20 import Module ( ModuleName )
21 import HsDoc ( HsDoc )
25 import SrcLoc ( Located(..) )
26 import Char ( isAlpha )
29 %************************************************************************
31 \subsection{Import and export declaration lists}
33 %************************************************************************
35 One per \tr{import} declaration in a module.
37 type LImportDecl name = Located (ImportDecl name)
40 = ImportDecl (Located ModuleName) -- module name
41 Bool -- True <=> {-# SOURCE #-} import
42 Bool -- True => qualified
43 (Maybe ModuleName) -- as Module
44 (Maybe (Bool, [LIE name])) -- (True => hiding, names)
48 instance (Outputable name) => Outputable (ImportDecl name) where
49 ppr (ImportDecl mod from qual as spec)
50 = hang (hsep [ptext SLIT("import"), ppr_imp from,
51 pp_qual qual, ppr mod, pp_as as])
55 pp_qual True = ptext SLIT("qualified")
58 pp_as (Just a) = ptext SLIT("as ") <+> ppr a
60 ppr_imp True = ptext SLIT("{-# SOURCE #-}")
63 pp_spec Nothing = empty
64 pp_spec (Just (False, spec))
65 = parens (interpp'SP spec)
66 pp_spec (Just (True, spec))
67 = ptext SLIT("hiding") <+> parens (interpp'SP spec)
69 ideclName :: ImportDecl name -> Located ModuleName
70 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
73 %************************************************************************
75 \subsection{Imported and exported entities}
77 %************************************************************************
80 type LIE name = Located (IE name)
84 | IEThingAbs name -- Class/Type (can't tell)
85 | IEThingAll name -- Class/Type plus all methods/constructors
86 | IEThingWith name [name] -- Class/Type plus some methods/constructors
87 | IEModuleContents ModuleName -- (Export Only)
88 | IEGroup Int (HsDoc name) -- Doc section heading
89 | IEDoc (HsDoc name) -- Some documentation
90 | IEDocNamed String -- Reference to named doc
94 ieName :: IE name -> name
96 ieName (IEThingAbs n) = n
97 ieName (IEThingWith n _) = n
98 ieName (IEThingAll n) = n
100 ieNames :: IE a -> [a]
101 ieNames (IEVar n ) = [n]
102 ieNames (IEThingAbs n ) = [n]
103 ieNames (IEThingAll n ) = [n]
104 ieNames (IEThingWith n ns) = n:ns
105 ieNames (IEModuleContents _ ) = []
106 ieNames (IEGroup _ _ ) = []
107 ieNames (IEDoc _ ) = []
108 ieNames (IEDocNamed _ ) = []
112 instance (Outputable name) => Outputable (IE name) where
113 ppr (IEVar var) = pprHsVar var
114 ppr (IEThingAbs thing) = ppr thing
115 ppr (IEThingAll thing) = hcat [ppr thing, text "(..)"]
116 ppr (IEThingWith thing withs)
117 = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
118 ppr (IEModuleContents mod)
119 = ptext SLIT("module") <+> ppr mod
120 ppr (IEGroup n _) = text ("<IEGroup: " ++ (show n) ++ ">")
121 ppr (IEDoc doc) = ppr doc
122 ppr (IEDocNamed string) = text ("<IEDocNamed: " ++ string ++ ">")
126 pprHsVar :: Outputable name => name -> SDoc
127 pprHsVar v | isOperator ppr_v = parens ppr_v
132 isOperator :: SDoc -> Bool
134 = case showSDocUnqual ppr_v of
135 ('(':_) -> False -- (), (,) etc
136 ('[':_) -> False -- []
137 ('$':c:_) -> not (isAlpha c) -- Don't treat $d as an operator
138 (':':c:_) -> not (isAlpha c) -- Don't treat :T as an operator
139 ('_':_) -> False -- Not an operator
140 (c:_) -> not (isAlpha c) -- Starts with non-alpha
142 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
143 -- that we don't need NamedThing in the context of all these functions.
144 -- Gruesome, but simple.