[project @ 2002-04-29 14:03:38 by simonmar]
[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 FastString
15 import SrcLoc           ( SrcLoc )
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                   WhereFrom
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 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         pp_spec Nothing = empty
49         pp_spec (Just (False, spec))
50                         = parens (interpp'SP spec)
51         pp_spec (Just (True, spec))
52                         = ptext SLIT("hiding") <+> parens (interpp'SP spec)
53
54 ideclName (ImportDecl mod_nm _ _ _ _ _) = mod_nm
55 \end{code}
56
57 %************************************************************************
58 %*                                                                      *
59 \subsection{Imported and exported entities}
60 %*                                                                      *
61 %************************************************************************
62
63 \begin{code}
64 data IE name
65   = IEVar               name
66   | IEThingAbs          name            -- Class/Type (can't tell)
67   | IEThingAll          name            -- Class/Type plus all methods/constructors
68   | IEThingWith         name [name]     -- Class/Type plus some methods/constructors
69   | IEModuleContents    ModuleName      -- (Export Only)
70 \end{code}
71
72 \begin{code}
73 ieName :: IE name -> name
74 ieName (IEVar n)         = n
75 ieName (IEThingAbs  n)   = n
76 ieName (IEThingWith n _) = n
77 ieName (IEThingAll  n)   = n
78
79 ieNames :: IE a -> [a]
80 ieNames (IEVar            n   ) = [n]
81 ieNames (IEThingAbs       n   ) = [n]
82 ieNames (IEThingAll       n   ) = [n]
83 ieNames (IEThingWith      n ns) = n:ns
84 ieNames (IEModuleContents _   ) = []
85 \end{code}
86
87 \begin{code}
88 instance (Outputable name) => Outputable (IE name) where
89     ppr (IEVar          var)    = ppr_var var
90     ppr (IEThingAbs     thing)  = ppr thing
91     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
92     ppr (IEThingWith thing withs)
93         = ppr thing <> parens (fsep (punctuate comma (map ppr_var withs)))
94     ppr (IEModuleContents mod)
95         = ptext SLIT("module") <+> ppr mod
96
97 ppr_var v | isOperator v = parens (ppr v)
98           | otherwise    = ppr v
99 \end{code}
100
101 \begin{code}
102 isOperator :: Outputable a => a -> Bool
103 isOperator v = isLexSym (mkFastString (showSDocUnqual (ppr v)))
104         -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
105         -- that we don't need NamedThing in the context of all these functions.
106         -- Gruesome, but simple.
107 \end{code}
108