2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
9 #include "HsVersions.h"
13 import Class ( FunDep, DefMeth(..) )
14 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
15 import Name ( Name, getName, isTyVarName )
17 import BasicTypes ( Boxity )
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
50 %************************************************************************
52 \subsection{Free variables}
54 %************************************************************************
56 These free-variable finders returns tycons and classes too.
59 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
60 charTyCon_name = getName charTyCon
61 listTyCon_name = getName listTyCon
62 parrTyCon_name = getName parrTyCon
64 tupleTyCon_name :: Boxity -> Int -> Name
65 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
67 extractHsTyVars :: RenamedHsType -> NameSet
68 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
70 extractFunDepNames :: FunDep Name -> NameSet
71 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
73 extractHsTyNames :: RenamedHsType -> NameSet
77 get (HsAppTy ty1 ty2) = get ty1 `unionNameSets` get ty2
78 get (HsListTy ty) = unitNameSet listTyCon_name `unionNameSets` get ty
79 get (HsPArrTy ty) = unitNameSet parrTyCon_name `unionNameSets` get ty
80 get (HsTupleTy con tys) = hsTupConFVs con `unionNameSets` extractHsTyNames_s tys
81 get (HsFunTy ty1 ty2) = get ty1 `unionNameSets` get ty2
82 get (HsPredTy p) = extractHsPredTyNames p
83 get (HsOpTy ty1 tycon ty2) = get ty1 `unionNameSets` get ty2 `unionNameSets`
85 get (HsNumTy n) = emptyNameSet
86 get (HsTyVar tv) = unitNameSet tv
87 get (HsKindSig ty k) = get ty
88 get (HsForAllTy (Just tvs)
89 ctxt ty) = (extractHsCtxtTyNames ctxt `unionNameSets` get ty)
91 mkNameSet (hsTyVarNames tvs)
92 get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
94 extractHsTyNames_s :: [RenamedHsType] -> NameSet
95 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
97 extractHsCtxtTyNames :: RenamedContext -> NameSet
98 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
100 -- You don't import or export implicit parameters,
101 -- so don't mention the IP names
102 extractHsPredTyNames (HsClassP cls tys)
103 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
104 extractHsPredTyNames (HsIParam n ty)
105 = extractHsTyNames ty
109 %************************************************************************
111 \subsection{Free variables of declarations}
113 %************************************************************************
115 Return the Names that must be in scope if we are to use this declaration.
116 In all cases this is set up for interface-file declarations:
117 - for class decls we ignore the bindings
118 - for instance decls likewise, plus the pragmas
119 - for rule decls, we ignore HsRules
120 - for data decls, we ignore derivings
122 *** See "THE NAMING STORY" in HsDecls ****
125 tyClDeclFVs :: RenamedTyClDecl -> NameSet
126 tyClDeclFVs (ForeignType {})
129 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
130 = extractHsTyNames ty `plusFV`
131 plusFVs (map hsIdInfoFVs id_infos)
133 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls})
134 = delFVs (map hsTyVarName tyvars) $
135 extractHsCtxtTyNames context `plusFV`
136 plusFVs (map conDeclFVs condecls)
138 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
139 = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
141 tyClDeclFVs (ClassDecl {tcdCtxt = context, tcdTyVars = tyvars, tcdFDs = fds,
142 tcdSigs = sigs, tcdMeths = maybe_meths})
143 = delFVs (map hsTyVarName tyvars) $
144 extractHsCtxtTyNames context `plusFV`
145 plusFVs (map extractFunDepNames fds) `plusFV`
146 hsSigsFVs sigs `plusFV`
149 dm_fvs = case maybe_meths of
150 Nothing -> mkFVs [v | ClassOpSig _ (DefMeth v) _ _ <- sigs]
151 -- No method bindings, so this class decl comes from an interface file,
152 -- So we want to treat the default-method names as free (they should
153 -- be defined somewhere else). [In source code this is not so; the class
154 -- decl will bind whatever default-methods are necessary.]
155 Just _ -> emptyFVs -- Source code, so the default methods
156 -- are *bound* not *free*
159 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
161 hsSigFVs (Sig v ty _) = extractHsTyNames ty
162 hsSigFVs (SpecInstSig ty _) = extractHsTyNames ty
163 hsSigFVs (SpecSig v ty _) = extractHsTyNames ty
164 hsSigFVs (ClassOpSig _ _ ty _) = extractHsTyNames ty
165 hsSigFVs other = emptyFVs
168 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
169 = extractHsTyNames inst_ty `plusFV`
170 (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
173 ruleDeclFVs (HsRule _ _ _ _ _ _) = emptyFVs
174 ruleDeclFVs (IfaceRuleOut _ _) = emptyFVs
175 ruleDeclFVs (IfaceRule _ _ vars _ args rhs _)
176 = delFVs (map ufBinderName vars) $
177 ufExprFVs rhs `plusFV` plusFVs (map ufExprFVs args)
180 conDeclFVs (ConDecl _ _ tyvars context details _)
181 = delFVs (map hsTyVarName tyvars) $
182 extractHsCtxtTyNames context `plusFV`
183 conDetailsFVs details
185 conDetailsFVs (VanillaCon btys) = plusFVs (map bangTyFVs btys)
186 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
187 conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
189 bangTyFVs bty = extractHsTyNames (getBangType bty)
192 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
193 hsIdInfoFVs (HsWorker n a) = unitFV n
194 hsIdInfoFVs other = emptyFVs
197 ufExprFVs (UfVar n) = unitFV n
198 ufExprFVs (UfLit l) = emptyFVs
199 ufExprFVs (UfLitLit l ty) = extractHsTyNames ty
200 ufExprFVs (UfFCall cc ty) = extractHsTyNames ty
201 ufExprFVs (UfType ty) = extractHsTyNames ty
202 ufExprFVs (UfTuple tc es) = hsTupConFVs tc `plusFV` plusFVs (map ufExprFVs es)
203 ufExprFVs (UfLam v e) = ufBndrFVs v (ufExprFVs e)
204 ufExprFVs (UfApp e1 e2) = ufExprFVs e1 `plusFV` ufExprFVs e2
205 ufExprFVs (UfCase e n as) = ufExprFVs e `plusFV` delFV n (plusFVs (map ufAltFVs as))
206 ufExprFVs (UfNote n e) = ufNoteFVs n `plusFV` ufExprFVs e
207 ufExprFVs (UfLet (UfNonRec b r) e) = ufExprFVs r `plusFV` ufBndrFVs b (ufExprFVs e)
208 ufExprFVs (UfLet (UfRec prs) e) = foldr ufBndrFVs
209 (foldr (plusFV . ufExprFVs . snd) (ufExprFVs e) prs)
212 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
213 ufBndrFVs (UfTyBinder n k) fvs = delFV n fvs
215 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
217 ufConFVs (UfDataAlt n) = unitFV n
218 ufConFVs (UfTupleAlt t) = hsTupConFVs t
219 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
220 ufConFVs other = emptyFVs
222 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
223 ufNoteFVs note = emptyFVs
225 hsTupConFVs (HsTupCon n _ _) = unitFV n
228 %************************************************************************
230 \subsection{A few functions on generic defintions
232 %************************************************************************
234 These functions on generics are defined over RenamedMatches, which is
235 why they are here and not in HsMatches.
238 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
239 -- Tells whether a Match is for a generic definition
240 -- and extract the type from a generic match and put it at the front
242 maybeGenericMatch (Match (TypePatIn ty : pats) sig_ty grhss)
243 = Just (ty, Match pats sig_ty grhss)
245 maybeGenericMatch other_match = Nothing