Migrate cvs diff from fptools-assoc branch
[ghc-hetmet.git] / compiler / hsSyn / HsImpExp.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[HsImpExp]{Abstract syntax: imports, exports, interfaces}
5
6 \begin{code}
7 module HsImpExp where
8
9 #include "HsVersions.h"
10
11 import Module           ( ModuleName )
12 import Outputable
13 import FastString
14 import SrcLoc           ( Located(..) )
15 import Char             ( isAlpha )
16 \end{code}
17
18 %************************************************************************
19 %*                                                                      *
20 \subsection{Import and export declaration lists}
21 %*                                                                      *
22 %************************************************************************
23
24 One per \tr{import} declaration in a module.
25 \begin{code}
26 type LImportDecl name = Located (ImportDecl name)
27
28 data ImportDecl name
29   = ImportDecl    (Located ModuleName)          -- module name
30                   Bool                          -- True <=> {-# SOURCE #-} import
31                   Bool                          -- True => qualified
32                   (Maybe ModuleName)            -- as Module
33                   (Maybe (Bool, [LIE name]))    -- (True => hiding, names)
34 \end{code}
35
36 \begin{code}
37 instance (Outputable name) => Outputable (ImportDecl name) where
38     ppr (ImportDecl mod from qual as spec)
39       = hang (hsep [ptext SLIT("import"), ppr_imp from, 
40                     pp_qual qual, ppr mod, pp_as as])
41              4 (pp_spec spec)
42       where
43         pp_qual False   = empty
44         pp_qual True    = ptext SLIT("qualified")
45
46         pp_as Nothing   = empty
47         pp_as (Just a)  = ptext SLIT("as ") <+> ppr a
48
49         ppr_imp True  = ptext SLIT("{-# SOURCE #-}")
50         ppr_imp False = empty
51
52         pp_spec Nothing = empty
53         pp_spec (Just (False, spec))
54                         = parens (interpp'SP spec)
55         pp_spec (Just (True, spec))
56                         = ptext SLIT("hiding") <+> parens (interpp'SP spec)
57
58 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{Imported and exported entities}
64 %*                                                                      *
65 %************************************************************************
66
67 \begin{code}
68 type LIE name = Located (IE name)
69
70 data IE name
71   = IEVar               name
72   | IEThingAbs          name            -- Class/Type (can't tell)
73   | IEThingAll          name            -- Class/Type plus all methods/constructors
74   | IEThingWith         name [name]     -- Class/Type plus some methods/constructors
75   | IEModuleContents    ModuleName      -- (Export Only)
76 \end{code}
77
78 \begin{code}
79 ieName :: IE name -> name
80 ieName (IEVar n)         = n
81 ieName (IEThingAbs  n)   = n
82 ieName (IEThingWith n _) = n
83 ieName (IEThingAll  n)   = n
84
85 ieNames :: IE a -> [a]
86 ieNames (IEVar            n   ) = [n]
87 ieNames (IEThingAbs       n   ) = [n]
88 ieNames (IEThingAll       n   ) = [n]
89 ieNames (IEThingWith      n ns) = n:ns
90 ieNames (IEModuleContents _   ) = []
91 \end{code}
92
93 \begin{code}
94 instance (Outputable name) => Outputable (IE name) where
95     ppr (IEVar          var)    = pprHsVar var
96     ppr (IEThingAbs     thing)  = ppr thing
97     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
98     ppr (IEThingWith thing withs)
99         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
100     ppr (IEModuleContents mod)
101         = ptext SLIT("module") <+> ppr mod
102 \end{code}
103
104 \begin{code}
105 pprHsVar :: Outputable name => name -> SDoc
106 pprHsVar v | isOperator ppr_v = parens ppr_v
107            | otherwise        = ppr_v
108            where
109              ppr_v = ppr v
110
111 isOperator :: SDoc -> Bool
112 isOperator ppr_v 
113   = case showSDocUnqual ppr_v of
114         ('(':s)   -> False              -- (), (,) etc
115         ('[':s)   -> False              -- []
116         ('$':c:s) -> not (isAlpha c)    -- Don't treat $d as an operator
117         (':':c:s) -> not (isAlpha c)    -- Don't treat :T as an operator
118         ('_':s)   -> False              -- Not an operator
119         (c:s)     -> not (isAlpha c)    -- Starts with non-alpha
120         other     -> False
121     -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
122     -- that we don't need NamedThing in the context of all these functions.
123     -- Gruesome, but simple.
124 \end{code}
125