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 TyCon ( visibleDataCons )
15 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
16 import Name ( Name, getName, isTyVarName )
18 import BasicTypes ( Boxity )
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
51 %************************************************************************
53 \subsection{Free variables}
55 %************************************************************************
57 These free-variable finders returns tycons and classes too.
60 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
61 charTyCon_name = getName charTyCon
62 listTyCon_name = getName listTyCon
63 parrTyCon_name = getName parrTyCon
65 tupleTyCon_name :: Boxity -> Int -> Name
66 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
68 extractHsTyVars :: RenamedHsType -> NameSet
69 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
71 extractFunDepNames :: FunDep Name -> NameSet
72 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
74 extractHsTyNames :: RenamedHsType -> NameSet
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)
94 mkNameSet (hsTyVarNames tvs)
95 get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
97 extractHsTyNames_s :: [RenamedHsType] -> NameSet
98 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
100 extractHsCtxtTyNames :: RenamedContext -> NameSet
101 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
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
112 %************************************************************************
114 \subsection{Free variables of declarations}
116 %************************************************************************
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
125 *** See "THE NAMING STORY" in HsDecls ****
128 tyClDeclFVs :: RenamedTyClDecl -> NameSet
129 tyClDeclFVs (ForeignType {})
132 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
133 = extractHsTyNames ty `plusFV`
134 plusFVs (map hsIdInfoFVs id_infos)
136 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls})
137 = delFVs (map hsTyVarName tyvars) $
138 extractHsCtxtTyNames context `plusFV`
139 plusFVs (map conDeclFVs (visibleDataCons condecls))
141 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
142 = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
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`
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*
161 tyClDeclFVs (CoreDecl {tcdType = ty, tcdRhs = rhs})
162 = extractHsTyNames ty `plusFV` ufExprFVs rhs
165 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
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
174 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
175 = extractHsTyNames inst_ty `plusFV`
176 (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
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)
186 conDeclFVs (ConDecl _ _ tyvars context details _)
187 = delFVs (map hsTyVarName tyvars) $
188 extractHsCtxtTyNames context `plusFV`
189 conDetailsFVs details
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]
195 bangTyFVs bty = extractHsTyNames (getBangType bty)
198 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
199 hsIdInfoFVs (HsWorker n a) = unitFV n
200 hsIdInfoFVs other = emptyFVs
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)
218 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
219 ufBndrFVs (UfTyBinder n k) fvs = delFV n fvs
221 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
223 ufConFVs (UfDataAlt n) = unitFV n
224 ufConFVs (UfTupleAlt t) = hsTupConFVs t
225 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
226 ufConFVs other = emptyFVs
228 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
229 ufNoteFVs note = emptyFVs
231 hsTupConFVs (HsTupCon n _ _) = unitFV n
234 %************************************************************************
236 \subsection{A few functions on generic defintions
238 %************************************************************************
240 These functions on generics are defined over RenamedMatches, which is
241 why they are here and not in HsMatches.
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
248 maybeGenericMatch (Match (TypePatIn ty : pats) sig_ty grhss)
249 = Just (ty, Match pats sig_ty grhss)
251 maybeGenericMatch other_match = Nothing