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