58a1acc024776e1ea155777987d3cbd049e7e79c
[ghc-hetmet.git] / ghc / compiler / rename / RnHsSyn.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
5
6 \begin{code}
7 module RnHsSyn where
8
9 #include "HsVersions.h"
10
11 import HsSyn
12 import HsCore
13 import Class            ( FunDep, DefMeth(..) )
14 import TysWiredIn       ( tupleTyCon, listTyCon, charTyCon )
15 import Name             ( Name, getName, isTyVarName )
16 import NameSet
17 import BasicTypes       ( Boxity )
18 import Outputable
19 \end{code}
20
21
22 \begin{code}
23 type RenamedHsDecl              = HsDecl                Name RenamedPat
24 type RenamedArithSeqInfo        = ArithSeqInfo          Name RenamedPat
25 type RenamedClassOpSig          = Sig                   Name
26 type RenamedConDecl             = ConDecl               Name
27 type RenamedContext             = HsContext             Name
28 type RenamedRuleDecl            = RuleDecl              Name RenamedPat
29 type RenamedTyClDecl            = TyClDecl              Name RenamedPat
30 type RenamedDefaultDecl         = DefaultDecl           Name
31 type RenamedForeignDecl         = ForeignDecl           Name
32 type RenamedGRHS                = GRHS                  Name RenamedPat
33 type RenamedGRHSs               = GRHSs                 Name RenamedPat
34 type RenamedHsBinds             = HsBinds               Name RenamedPat
35 type RenamedHsExpr              = HsExpr                Name RenamedPat
36 type RenamedInstDecl            = InstDecl              Name RenamedPat
37 type RenamedMatchContext        = HsMatchContext        Name
38 type RenamedMatch               = Match                 Name RenamedPat
39 type RenamedMonoBinds           = MonoBinds             Name RenamedPat
40 type RenamedPat                 = InPat                 Name
41 type RenamedHsType              = HsType                Name
42 type RenamedHsPred              = HsPred                Name
43 type RenamedRecordBinds         = HsRecordBinds         Name RenamedPat
44 type RenamedSig                 = Sig                   Name
45 type RenamedStmt                = Stmt                  Name RenamedPat
46 type RenamedFixitySig           = FixitySig             Name
47 type RenamedDeprecation         = DeprecDecl            Name
48 \end{code}
49
50 %************************************************************************
51 %*                                                                      *
52 \subsection{Free variables}
53 %*                                                                      *
54 %************************************************************************
55
56 These free-variable finders returns tycons and classes too.
57
58 \begin{code}
59 charTyCon_name, listTyCon_name :: Name
60 charTyCon_name    = getName charTyCon
61 listTyCon_name    = getName listTyCon
62
63 tupleTyCon_name :: Boxity -> Int -> Name
64 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
65
66 extractHsTyVars :: RenamedHsType -> NameSet
67 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
68
69 extractFunDepNames :: FunDep Name -> NameSet
70 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
71
72 extractHsTyNames   :: RenamedHsType -> NameSet
73 extractHsTyNames ty
74   = get ty
75   where
76     get (HsAppTy ty1 ty2)      = get ty1 `unionNameSets` get ty2
77     get (HsListTy ty)          = unitNameSet listTyCon_name `unionNameSets` get ty
78     get (HsTupleTy con tys)    = hsTupConFVs con `unionNameSets` extractHsTyNames_s tys
79     get (HsFunTy ty1 ty2)      = get ty1 `unionNameSets` get ty2
80     get (HsPredTy p)           = extractHsPredTyNames p
81     get (HsOpTy ty1 tycon ty2) = get ty1 `unionNameSets` get ty2 `unionNameSets`
82                                  unitNameSet tycon
83     get (HsNumTy n)            = emptyNameSet
84     get (HsTyVar tv)           = unitNameSet tv
85     get (HsForAllTy (Just tvs) 
86                     ctxt ty)   = (extractHsCtxtTyNames ctxt `unionNameSets` get ty)
87                                             `minusNameSet`
88                                   mkNameSet (hsTyVarNames tvs)
89     get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
90
91 extractHsTyNames_s  :: [RenamedHsType] -> NameSet
92 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
93
94 extractHsCtxtTyNames :: RenamedContext -> NameSet
95 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
96
97 -- You don't import or export implicit parameters,
98 -- so don't mention the IP names
99 extractHsPredTyNames (HsClassP cls tys)
100   = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
101 extractHsPredTyNames (HsIParam n ty)
102   = extractHsTyNames ty
103 \end{code}
104
105
106 %************************************************************************
107 %*                                                                      *
108 \subsection{Free variables of declarations}
109 %*                                                                      *
110 %************************************************************************
111
112 Return the Names that must be in scope if we are to use this declaration.
113 In all cases this is set up for interface-file declarations:
114         - for class decls we ignore the bindings
115         - for instance decls likewise, plus the pragmas
116         - for rule decls, we ignore HsRules
117         - for data decls, we ignore derivings
118
119         *** See "THE NAMING STORY" in HsDecls ****
120
121 \begin{code}
122 tyClDeclFVs :: RenamedTyClDecl -> NameSet
123 tyClDeclFVs (ForeignType {})
124   = emptyFVs
125
126 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
127   = extractHsTyNames ty                 `plusFV` 
128     plusFVs (map hsIdInfoFVs id_infos)
129
130 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls})
131   = delFVs (map hsTyVarName tyvars) $
132     extractHsCtxtTyNames context                `plusFV`
133     plusFVs (map conDeclFVs condecls)
134
135 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
136   = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
137
138 tyClDeclFVs (ClassDecl {tcdCtxt = context, tcdTyVars = tyvars, tcdFDs = fds, 
139                         tcdSigs = sigs, tcdMeths = maybe_meths})
140   = delFVs (map hsTyVarName tyvars) $
141     extractHsCtxtTyNames context          `plusFV`
142     plusFVs (map extractFunDepNames fds)  `plusFV`
143     hsSigsFVs sigs                        `plusFV`
144     dm_fvs
145   where
146     dm_fvs = case maybe_meths of
147                 Nothing -> mkFVs [v | ClassOpSig _ (DefMeth v) _ _ <- sigs]
148                   -- No method bindings, so this class decl comes from an interface file, 
149                   -- So we want to treat the default-method names as free (they should
150                   -- be defined somewhere else).  [In source code this is not so; the class
151                   -- decl will bind whatever default-methods are necessary.]
152                 Just _ -> emptyFVs      -- Source code, so the default methods
153                                         -- are *bound* not *free*
154
155 ----------------
156 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
157
158 hsSigFVs (Sig v ty _)       = extractHsTyNames ty
159 hsSigFVs (SpecInstSig ty _) = extractHsTyNames ty
160 hsSigFVs (SpecSig v ty _)   = extractHsTyNames ty
161 hsSigFVs (ClassOpSig _ _ ty _) = extractHsTyNames ty
162 hsSigFVs other              = emptyFVs
163
164 ----------------
165 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
166   = extractHsTyNames inst_ty    `plusFV` 
167     (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
168
169 ----------------
170 ruleDeclFVs (HsRule _ _ _ _ _ _) = emptyFVs
171 ruleDeclFVs (IfaceRuleOut _ _)   = emptyFVs
172 ruleDeclFVs (IfaceRule _ _ vars _ args rhs _)
173   = delFVs (map ufBinderName vars) $
174     ufExprFVs rhs `plusFV` plusFVs (map ufExprFVs args)
175
176 ----------------
177 conDeclFVs (ConDecl _ _ tyvars context details _)
178   = delFVs (map hsTyVarName tyvars) $
179     extractHsCtxtTyNames context          `plusFV`
180     conDetailsFVs details
181
182 conDetailsFVs (VanillaCon btys)    = plusFVs (map bangTyFVs btys)
183 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
184 conDetailsFVs (RecCon flds)        = plusFVs [bangTyFVs bty | (_, bty) <- flds]
185
186 bangTyFVs bty = extractHsTyNames (getBangType bty)
187
188 ----------------
189 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
190 hsIdInfoFVs (HsWorker n a)   = unitFV n
191 hsIdInfoFVs other            = emptyFVs
192
193 ----------------
194 ufExprFVs (UfVar n)       = unitFV n
195 ufExprFVs (UfLit l)       = emptyFVs
196 ufExprFVs (UfLitLit l ty) = extractHsTyNames ty
197 ufExprFVs (UfFCall cc ty) = extractHsTyNames ty
198 ufExprFVs (UfType ty)     = extractHsTyNames ty
199 ufExprFVs (UfTuple tc es) = hsTupConFVs tc `plusFV` plusFVs (map ufExprFVs es)
200 ufExprFVs (UfLam v e)     = ufBndrFVs v (ufExprFVs e)
201 ufExprFVs (UfApp e1 e2)   = ufExprFVs e1 `plusFV` ufExprFVs e2
202 ufExprFVs (UfCase e n as) = ufExprFVs e `plusFV` delFV n (plusFVs (map ufAltFVs as))
203 ufExprFVs (UfNote n e)    = ufNoteFVs n `plusFV` ufExprFVs e
204 ufExprFVs (UfLet (UfNonRec b r) e) = ufExprFVs r `plusFV` ufBndrFVs b (ufExprFVs e)
205 ufExprFVs (UfLet (UfRec prs)    e) = foldr ufBndrFVs 
206                                            (foldr (plusFV . ufExprFVs . snd) (ufExprFVs e) prs)
207                                            (map fst prs) 
208
209 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
210 ufBndrFVs (UfTyBinder  n k)  fvs = delFV n fvs
211
212 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
213
214 ufConFVs (UfDataAlt n)      = unitFV n
215 ufConFVs (UfTupleAlt t)     = hsTupConFVs t
216 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
217 ufConFVs other              = emptyFVs
218
219 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
220 ufNoteFVs note          = emptyFVs
221
222 hsTupConFVs (HsTupCon n _ _) = unitFV n
223 \end{code}
224
225 %************************************************************************
226 %*                                                                      *
227 \subsection{A few functions on generic defintions
228 %*                                                                      *
229 %************************************************************************
230
231 These functions on generics are defined over RenamedMatches, which is
232 why they are here and not in HsMatches.
233
234 \begin{code}
235 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
236   -- Tells whether a Match is for a generic definition
237   -- and extract the type from a generic match and put it at the front
238
239 maybeGenericMatch (Match (TypePatIn ty : pats) sig_ty grhss)
240   = Just (ty, Match pats sig_ty grhss)
241
242 maybeGenericMatch other_match = Nothing
243 \end{code}