[project @ 2001-03-27 14:05:09 by simonpj]
[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 Name             ( isLexSym )
12 import Module           ( ModuleName, WhereFrom )
13 import Outputable
14 import SrcLoc           ( SrcLoc )
15 \end{code}
16
17 %************************************************************************
18 %*                                                                      *
19 \subsection{Import and export declaration lists}
20 %*                                                                      *
21 %************************************************************************
22
23 One per \tr{import} declaration in a module.
24 \begin{code}
25 data ImportDecl name
26   = ImportDecl    ModuleName                    -- module name
27                   WhereFrom
28                   Bool                          -- True => qualified
29                   (Maybe ModuleName)            -- as Module
30                   (Maybe (Bool, [IE name]))     -- (True => hiding, names)
31                   SrcLoc
32 \end{code}
33
34 \begin{code}
35 instance (Outputable name) => Outputable (ImportDecl name) where
36     ppr (ImportDecl mod from qual as spec _)
37       = hang (hsep [ptext SLIT("import"), ppr from, 
38                     pp_qual qual, ppr mod, pp_as as])
39              4 (pp_spec spec)
40       where
41         pp_qual False   = empty
42         pp_qual True    = ptext SLIT("qualified")
43
44         pp_as Nothing   = empty
45         pp_as (Just a)  = ptext SLIT("as ") <+> ppr a
46
47         pp_spec Nothing = empty
48         pp_spec (Just (False, spec))
49                         = parens (interpp'SP spec)
50         pp_spec (Just (True, spec))
51                         = ptext SLIT("hiding") <+> parens (interpp'SP spec)
52
53 ideclName (ImportDecl mod_nm _ _ _ _ _) = mod_nm
54 \end{code}
55
56 %************************************************************************
57 %*                                                                      *
58 \subsection{Imported and exported entities}
59 %*                                                                      *
60 %************************************************************************
61
62 \begin{code}
63 data IE name
64   = IEVar               name
65   | IEThingAbs          name            -- Class/Type (can't tell)
66   | IEThingAll          name            -- Class/Type plus all methods/constructors
67   | IEThingWith         name [name]     -- Class/Type plus some methods/constructors
68   | IEModuleContents    ModuleName      -- (Export Only)
69 \end{code}
70
71 \begin{code}
72 ieName :: IE name -> name
73 ieName (IEVar n)         = n
74 ieName (IEThingAbs  n)   = n
75 ieName (IEThingWith n _) = n
76 ieName (IEThingAll  n)   = n
77
78 ieNames :: IE a -> [a]
79 ieNames (IEVar            n   ) = [n]
80 ieNames (IEThingAbs       n   ) = [n]
81 ieNames (IEThingAll       n   ) = [n]
82 ieNames (IEThingWith      n ns) = n:ns
83 ieNames (IEModuleContents _   ) = []
84 \end{code}
85
86 \begin{code}
87 instance (Outputable name) => Outputable (IE name) where
88     ppr (IEVar          var)    = ppr_var var
89     ppr (IEThingAbs     thing)  = ppr thing
90     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
91     ppr (IEThingWith thing withs)
92         = ppr thing <> parens (fsep (punctuate comma (map ppr_var withs)))
93     ppr (IEModuleContents mod)
94         = ptext SLIT("module") <+> ppr mod
95
96 ppr_var v | isOperator v = parens (ppr v)
97           | otherwise    = ppr v
98 \end{code}
99
100 \begin{code}
101 isOperator :: Outputable a => a -> Bool
102 isOperator v = isLexSym (_PK_ (showSDocUnqual (ppr v)))
103         -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
104         -- that we don't need NamedThing in the context of all these functions.
105         -- Gruesome, but simple.
106 \end{code}
107