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 )
23 type RenamedArithSeqInfo = ArithSeqInfo Name RenamedPat
24 type RenamedClassOpSig = Sig Name
25 type RenamedConDecl = ConDecl Name
26 type RenamedContext = HsContext Name
27 type RenamedHsDecl = HsDecl Name RenamedPat
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 RenamedHsModule = HsModule 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 :: Name
61 charTyCon_name = getName charTyCon
62 listTyCon_name = getName listTyCon
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 (HsTupleTy con tys) = hsTupConFVs con `unionNameSets` extractHsTyNames_s tys
80 get (HsFunTy ty1 ty2) = get ty1 `unionNameSets` get ty2
81 get (HsPredTy p) = extractHsPredTyNames p
82 get (HsOpTy ty1 tycon ty2) = get ty1 `unionNameSets` get ty2 `unionNameSets`
84 get (HsNumTy n) = emptyNameSet
85 get (HsTyVar tv) = unitNameSet tv
86 get (HsForAllTy (Just tvs)
87 ctxt ty) = (extractHsCtxtTyNames ctxt `unionNameSets` get ty)
89 mkNameSet (hsTyVarNames tvs)
90 get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
92 extractHsTyNames_s :: [RenamedHsType] -> NameSet
93 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
95 extractHsCtxtTyNames :: RenamedContext -> NameSet
96 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
98 -- You don't import or export implicit parameters,
99 -- so don't mention the IP names
100 extractHsPredTyNames (HsClassP cls tys)
101 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
102 extractHsPredTyNames (HsIParam n ty)
103 = extractHsTyNames ty
107 %************************************************************************
109 \subsection{Free variables of declarations}
111 %************************************************************************
113 Return the Names that must be in scope if we are to use this declaration.
114 In all cases this is set up for interface-file declarations:
115 - for class decls we ignore the bindings
116 - for instance decls likewise, plus the pragmas
117 - for rule decls, we ignore HsRules
119 *** See "THE NAMING STORY" in HsDecls ****
122 tyClDeclFVs :: RenamedTyClDecl -> NameSet
123 tyClDeclFVs (ForeignType {})
126 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
127 = extractHsTyNames ty `plusFV`
128 plusFVs (map hsIdInfoFVs id_infos)
130 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls, tcdDerivs = derivings})
131 = delFVs (map hsTyVarName tyvars) $
132 extractHsCtxtTyNames context `plusFV`
133 plusFVs (map conDeclFVs condecls)
135 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
136 = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
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`
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*
156 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
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
165 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
166 = extractHsTyNames inst_ty `plusFV`
167 (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
170 ruleDeclFVs (HsRule _ _ _ _ _ _) = emptyFVs
171 ruleDeclFVs (IfaceRule _ vars _ args rhs _)
172 = delFVs (map ufBinderName vars) $
173 ufExprFVs rhs `plusFV` plusFVs (map ufExprFVs args)
176 conDeclFVs (ConDecl _ _ tyvars context details _)
177 = delFVs (map hsTyVarName tyvars) $
178 extractHsCtxtTyNames context `plusFV`
179 conDetailsFVs details
181 conDetailsFVs (VanillaCon btys) = plusFVs (map bangTyFVs btys)
182 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
183 conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
185 bangTyFVs bty = extractHsTyNames (getBangType bty)
188 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
189 hsIdInfoFVs (HsWorker n a) = unitFV n
190 hsIdInfoFVs other = emptyFVs
193 ufExprFVs (UfVar n) = unitFV n
194 ufExprFVs (UfLit l) = emptyFVs
195 ufExprFVs (UfLitLit l ty) = extractHsTyNames ty
196 ufExprFVs (UfFCall cc ty) = extractHsTyNames ty
197 ufExprFVs (UfType ty) = extractHsTyNames ty
198 ufExprFVs (UfTuple tc es) = hsTupConFVs tc `plusFV` plusFVs (map ufExprFVs es)
199 ufExprFVs (UfLam v e) = ufBndrFVs v (ufExprFVs e)
200 ufExprFVs (UfApp e1 e2) = ufExprFVs e1 `plusFV` ufExprFVs e2
201 ufExprFVs (UfCase e n as) = ufExprFVs e `plusFV` delFV n (plusFVs (map ufAltFVs as))
202 ufExprFVs (UfNote n e) = ufNoteFVs n `plusFV` ufExprFVs e
203 ufExprFVs (UfLet (UfNonRec b r) e) = ufExprFVs r `plusFV` ufBndrFVs b (ufExprFVs e)
204 ufExprFVs (UfLet (UfRec prs) e) = foldr ufBndrFVs
205 (foldr (plusFV . ufExprFVs . snd) (ufExprFVs e) prs)
208 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
209 ufBndrFVs (UfTyBinder n k) fvs = delFV n fvs
211 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
213 ufConFVs (UfDataAlt n) = unitFV n
214 ufConFVs (UfTupleAlt t) = hsTupConFVs t
215 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
216 ufConFVs other = emptyFVs
218 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
219 ufNoteFVs note = emptyFVs
221 hsTupConFVs (HsTupCon n _ _) = unitFV n
224 %************************************************************************
226 \subsection{A few functions on generic defintions
228 %************************************************************************
230 These functions on generics are defined over RenamedMatches, which is
231 why they are here and not in HsMatches.
234 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
235 -- Tells whether a Match is for a generic definition
236 -- and extract the type from a generic match and put it at the front
238 maybeGenericMatch (Match tvs (TypePatIn ty : pats) sig_ty grhss)
239 = Just (ty, Match tvs pats sig_ty grhss)
241 maybeGenericMatch other_match = Nothing