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, charTyCon )
15 import Name ( Name, getName, isTyVarName )
17 import BasicTypes ( Boxity )
18 import Maybes ( orElse )
24 type RenamedArithSeqInfo = ArithSeqInfo Name RenamedPat
25 type RenamedClassOpSig = Sig Name
26 type RenamedConDecl = ConDecl Name
27 type RenamedContext = HsContext Name
28 type RenamedHsDecl = HsDecl Name RenamedPat
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 RenamedHsModule = HsModule Name RenamedPat
38 type RenamedInstDecl = InstDecl Name RenamedPat
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 type RenamedHsOverLit = HsOverLit Name
52 %************************************************************************
54 \subsection{Free variables}
56 %************************************************************************
58 These free-variable finders returns tycons and classes too.
61 charTyCon_name, listTyCon_name :: Name
62 charTyCon_name = getName charTyCon
63 listTyCon_name = getName listTyCon
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 (HsTupleTy (HsTupCon n _) tys) = unitNameSet n
81 `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`
86 get (HsNumTy n) = emptyNameSet
87 get (HsTyVar tv) = unitNameSet tv
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 (HsPClass cls tys)
103 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
104 extractHsPredTyNames (HsPIParam 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 ignroe the bindings
118 - for instance decls likewise, plus the pragmas
119 - for rule decls, we ignore HsRules
122 tyClDeclFVs :: RenamedTyClDecl -> NameSet
123 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
124 = extractHsTyNames ty `plusFV`
125 plusFVs (map hsIdInfoFVs id_infos)
127 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls, tcdDerivs = derivings})
128 = delFVs (map hsTyVarName tyvars) $
129 extractHsCtxtTyNames context `plusFV`
130 plusFVs (map conDeclFVs condecls) `plusFV`
131 mkNameSet (derivings `orElse` [])
133 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
134 = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
136 tyClDeclFVs (ClassDecl {tcdCtxt = context, tcdTyVars = tyvars, tcdFDs = fds, tcdSigs = sigs})
137 = delFVs (map hsTyVarName tyvars) $
138 extractHsCtxtTyNames context `plusFV`
139 plusFVs (map extractFunDepNames fds) `plusFV`
143 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
145 hsSigFVs (Sig v ty _) = extractHsTyNames ty
146 hsSigFVs (SpecInstSig ty _) = extractHsTyNames ty
147 hsSigFVs (SpecSig v ty _) = extractHsTyNames ty
148 hsSigFVs other = emptyFVs
151 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
152 = extractHsTyNames inst_ty `plusFV`
153 (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
156 ruleDeclFVs (HsRule _ _ _ _ _ _) = emptyFVs
157 ruleDeclFVs (IfaceRule _ vars _ args rhs _)
158 = delFVs (map ufBinderName vars) $
159 ufExprFVs rhs `plusFV` plusFVs (map ufExprFVs args)
162 conDeclFVs (ConDecl _ _ tyvars context details _)
163 = delFVs (map hsTyVarName tyvars) $
164 extractHsCtxtTyNames context `plusFV`
165 conDetailsFVs details
167 conDetailsFVs (VanillaCon btys) = plusFVs (map bangTyFVs btys)
168 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
169 conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
171 bangTyFVs bty = extractHsTyNames (getBangType bty)
174 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
175 hsIdInfoFVs (HsWorker n) = unitFV n
176 hsIdInfoFVs other = emptyFVs
179 ufExprFVs (UfVar n) = unitFV n
180 ufExprFVs (UfLit l) = emptyFVs
181 ufExprFVs (UfLitLit l ty) = extractHsTyNames ty
182 ufExprFVs (UfCCall cc ty) = extractHsTyNames ty
183 ufExprFVs (UfType ty) = extractHsTyNames ty
184 ufExprFVs (UfTuple tc es) = hsTupConFVs tc `plusFV` plusFVs (map ufExprFVs es)
185 ufExprFVs (UfLam v e) = ufBndrFVs v (ufExprFVs e)
186 ufExprFVs (UfApp e1 e2) = ufExprFVs e1 `plusFV` ufExprFVs e2
187 ufExprFVs (UfCase e n as) = ufExprFVs e `plusFV` delFV n (plusFVs (map ufAltFVs as))
188 ufExprFVs (UfNote n e) = ufNoteFVs n `plusFV` ufExprFVs e
189 ufExprFVs (UfLet (UfNonRec b r) e) = ufExprFVs r `plusFV` ufBndrFVs b (ufExprFVs e)
190 ufExprFVs (UfLet (UfRec prs) e) = foldr ufBndrFVs
191 (foldr (plusFV . ufExprFVs . snd) (ufExprFVs e) prs)
194 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
195 ufBndrFVs (UfTyBinder n k) fvs = delFV n fvs
197 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
199 ufConFVs (UfDataAlt n) = unitFV n
200 ufConFVs (UfTupleAlt t) = hsTupConFVs t
201 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
202 ufConFVs other = emptyFVs
204 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
205 ufNoteFVs note = emptyFVs
207 hsTupConFVs (HsTupCon n _) = unitFV n
210 %************************************************************************
212 \subsection{A few functions on generic defintions
214 %************************************************************************
216 These functions on generics are defined over RenamedMatches, which is
217 why they are here and not in HsMatches.
220 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
221 -- Tells whether a Match is for a generic definition
222 -- and extract the type from a generic match and put it at the front
224 maybeGenericMatch (Match tvs (TypePatIn ty : pats) sig_ty grhss)
225 = Just (ty, Match tvs pats sig_ty grhss)
227 maybeGenericMatch other_match = Nothing