52ce08bb9e6f6ff2bdeade220c2aee5881123d49
[ghc-hetmet.git] / ghc / 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           ( SrcLoc )
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 data ImportDecl name
27   = ImportDecl    ModuleName                    -- module name
28                   Bool                          -- True <=> {-# SOURCE #-} import
29                   Bool                          -- True => qualified
30                   (Maybe ModuleName)            -- as Module
31                   (Maybe (Bool, [IE name]))     -- (True => hiding, names)
32                   SrcLoc
33 \end{code}
34
35 \begin{code}
36 instance (Outputable name) => Outputable (ImportDecl name) where
37     ppr (ImportDecl mod from qual as spec _)
38       = hang (hsep [ptext SLIT("import"), ppr_imp from, 
39                     pp_qual qual, ppr mod, pp_as as])
40              4 (pp_spec spec)
41       where
42         pp_qual False   = empty
43         pp_qual True    = ptext SLIT("qualified")
44
45         pp_as Nothing   = empty
46         pp_as (Just a)  = ptext SLIT("as ") <+> ppr a
47
48         ppr_imp True  = ptext SLIT("{-# SOURCE #-}")
49         ppr_imp False = empty
50
51         pp_spec Nothing = empty
52         pp_spec (Just (False, spec))
53                         = parens (interpp'SP spec)
54         pp_spec (Just (True, spec))
55                         = ptext SLIT("hiding") <+> parens (interpp'SP spec)
56
57 ideclName (ImportDecl mod_nm _ _ _ _ _) = mod_nm
58 \end{code}
59
60 %************************************************************************
61 %*                                                                      *
62 \subsection{Imported and exported entities}
63 %*                                                                      *
64 %************************************************************************
65
66 \begin{code}
67 data IE name
68   = IEVar               name
69   | IEThingAbs          name            -- Class/Type (can't tell)
70   | IEThingAll          name            -- Class/Type plus all methods/constructors
71   | IEThingWith         name [name]     -- Class/Type plus some methods/constructors
72   | IEModuleContents    ModuleName      -- (Export Only)
73 \end{code}
74
75 \begin{code}
76 ieName :: IE name -> name
77 ieName (IEVar n)         = n
78 ieName (IEThingAbs  n)   = n
79 ieName (IEThingWith n _) = n
80 ieName (IEThingAll  n)   = n
81
82 ieNames :: IE a -> [a]
83 ieNames (IEVar            n   ) = [n]
84 ieNames (IEThingAbs       n   ) = [n]
85 ieNames (IEThingAll       n   ) = [n]
86 ieNames (IEThingWith      n ns) = n:ns
87 ieNames (IEModuleContents _   ) = []
88 \end{code}
89
90 \begin{code}
91 instance (Outputable name) => Outputable (IE name) where
92     ppr (IEVar          var)    = pprHsVar var
93     ppr (IEThingAbs     thing)  = ppr thing
94     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
95     ppr (IEThingWith thing withs)
96         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
97     ppr (IEModuleContents mod)
98         = ptext SLIT("module") <+> ppr mod
99 \end{code}
100
101 \begin{code}
102 pprHsVar :: Outputable name => name -> SDoc
103 pprHsVar v | isOperator ppr_v = parens ppr_v
104            | otherwise        = ppr_v
105            where
106              ppr_v = ppr v
107
108 isOperator :: SDoc -> Bool
109 isOperator ppr_v 
110   = case showSDocUnqual ppr_v of
111         ('(':s)   -> False              -- (), (,) etc
112         ('[':s)   -> False              -- []
113         ('$':c:s) -> not (isAlpha c)    -- Don't treat $d as an operator
114         (':':c:s) -> not (isAlpha c)    -- Don't treat :T as an operator
115         (c:s)     -> not (isAlpha c)    -- Starts with non-alpha
116         other     -> False
117     -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
118     -- that we don't need NamedThing in the context of all these functions.
119     -- Gruesome, but simple.
120 \end{code}
121