2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
9 #include "HsVersions.h"
12 import Class ( FunDep )
13 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
14 import Name ( Name, getName, isTyVarName )
16 import BasicTypes ( Boxity )
17 import SrcLoc ( Located(..), unLoc )
20 %************************************************************************
22 \subsection{Free variables}
24 %************************************************************************
26 These free-variable finders returns tycons and classes too.
29 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
30 charTyCon_name = getName charTyCon
31 listTyCon_name = getName listTyCon
32 parrTyCon_name = getName parrTyCon
34 tupleTyCon_name :: Boxity -> Int -> Name
35 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
37 extractHsTyVars :: LHsType Name -> NameSet
38 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
40 extractFunDepNames :: FunDep Name -> NameSet
41 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
43 extractHsTyNames :: LHsType Name -> NameSet
47 getl (L _ ty) = get ty
49 get (HsAppTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
50 get (HsListTy ty) = unitNameSet listTyCon_name `unionNameSets` getl ty
51 get (HsPArrTy ty) = unitNameSet parrTyCon_name `unionNameSets` getl ty
52 get (HsTupleTy con tys) = extractHsTyNames_s tys
53 get (HsFunTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
54 get (HsPredTy p) = extractHsPredTyNames (unLoc p)
55 get (HsOpTy ty1 op ty2) = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
56 get (HsParTy ty) = getl ty
57 get (HsNumTy n) = emptyNameSet
58 get (HsTyVar tv) = unitNameSet tv
59 get (HsKindSig ty k) = getl ty
61 ctxt ty) = (extractHsCtxtTyNames ctxt
62 `unionNameSets` getl ty)
64 mkNameSet (hsLTyVarNames tvs)
66 extractHsTyNames_s :: [LHsType Name] -> NameSet
67 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
69 extractHsCtxtTyNames :: LHsContext Name -> NameSet
70 extractHsCtxtTyNames (L _ ctxt)
71 = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
73 -- You don't import or export implicit parameters,
74 -- so don't mention the IP names
75 extractHsPredTyNames (HsClassP cls tys)
76 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
77 extractHsPredTyNames (HsIParam n ty)
82 %************************************************************************
84 \subsection{Free variables of declarations}
86 %************************************************************************
88 Return the Names that must be in scope if we are to use this declaration.
89 In all cases this is set up for interface-file declarations:
90 - for class decls we ignore the bindings
91 - for instance decls likewise, plus the pragmas
92 - for rule decls, we ignore HsRules
93 - for data decls, we ignore derivings
95 *** See "THE NAMING STORY" in HsDecls ****
99 hsSigsFVs :: [LSig Name] -> FreeVars
100 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
102 hsSigFVs (Sig v ty) = extractHsTyNames ty
103 hsSigFVs (SpecInstSig ty) = extractHsTyNames ty
104 hsSigFVs (SpecSig v ty) = extractHsTyNames ty
105 hsSigFVs other = emptyFVs
108 conDeclFVs (L _ (ConDecl _ tyvars context details))
109 = delFVs (map hsLTyVarName tyvars) $
110 extractHsCtxtTyNames context `plusFV`
111 conDetailsFVs details
113 conDetailsFVs (PrefixCon btys) = plusFVs (map bangTyFVs btys)
114 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
115 conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
117 bangTyFVs bty = extractHsTyNames (getBangType (unLoc bty))
121 %************************************************************************
123 \subsection{A few functions on generic defintions
125 %************************************************************************
127 These functions on generics are defined over Matches Name, which is
128 why they are here and not in HsMatches.
131 maybeGenericMatch :: LMatch Name -> Maybe (HsType Name, LMatch Name)
132 -- Tells whether a Match is for a generic definition
133 -- and extract the type from a generic match and put it at the front
135 maybeGenericMatch (L loc (Match (L _ (TypePat (L _ ty)) : pats) sig_ty grhss))
136 = Just (ty, L loc (Match pats sig_ty grhss))
138 maybeGenericMatch other_match = Nothing