2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 charTyCon_name, listTyCon_name, parrTyCon_name, tupleTyCon_name,
17 extractHsTyVars, extractHsTyNames, extractHsTyNames_s,
18 extractFunDepNames, extractHsCtxtTyNames, extractHsPredTyNames,
21 hsSigsFVs, hsSigFVs, conDeclFVs, bangTyFVs,
26 #include "HsVersions.h"
29 import Class ( FunDep )
30 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
31 import Name ( Name, getName, isTyVarName )
33 import BasicTypes ( Boxity )
34 import SrcLoc ( Located(..), unLoc )
37 %************************************************************************
39 \subsection{Free variables}
41 %************************************************************************
43 These free-variable finders returns tycons and classes too.
46 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
47 charTyCon_name = getName charTyCon
48 listTyCon_name = getName listTyCon
49 parrTyCon_name = getName parrTyCon
51 tupleTyCon_name :: Boxity -> Int -> Name
52 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
54 extractHsTyVars :: LHsType Name -> NameSet
55 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
57 extractFunDepNames :: FunDep Name -> NameSet
58 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
60 extractHsTyNames :: LHsType Name -> NameSet
64 getl (L _ ty) = get ty
66 get (HsAppTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
67 get (HsListTy ty) = unitNameSet listTyCon_name `unionNameSets` getl ty
68 get (HsPArrTy ty) = unitNameSet parrTyCon_name `unionNameSets` getl ty
69 get (HsTupleTy con tys) = extractHsTyNames_s tys
70 get (HsFunTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
71 get (HsPredTy p) = extractHsPredTyNames p
72 get (HsOpTy ty1 op ty2) = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
73 get (HsParTy ty) = getl ty
74 get (HsBangTy _ ty) = getl ty
75 get (HsNumTy n) = emptyNameSet
76 get (HsTyVar tv) = unitNameSet tv
77 get (HsSpliceTy _) = emptyNameSet -- Type splices mention no type variables
78 get (HsKindSig ty k) = getl ty
80 ctxt ty) = (extractHsCtxtTyNames ctxt
81 `unionNameSets` getl ty)
83 mkNameSet (hsLTyVarNames tvs)
84 get (HsDocTy ty _) = getl ty
86 extractHsTyNames_s :: [LHsType Name] -> NameSet
87 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
89 extractHsCtxtTyNames :: LHsContext Name -> NameSet
90 extractHsCtxtTyNames (L _ ctxt)
91 = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
93 -- You don't import or export implicit parameters,
94 -- so don't mention the IP names
95 extractHsPredTyNames (HsClassP cls tys)
96 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
97 extractHsPredTyNames (HsEqualP ty1 ty2)
98 = extractHsTyNames ty1 `unionNameSets` extractHsTyNames ty2
99 extractHsPredTyNames (HsIParam n ty)
100 = extractHsTyNames ty
104 %************************************************************************
106 \subsection{Free variables of declarations}
108 %************************************************************************
110 Return the Names that must be in scope if we are to use this declaration.
111 In all cases this is set up for interface-file declarations:
112 - for class decls we ignore the bindings
113 - for instance decls likewise, plus the pragmas
114 - for rule decls, we ignore HsRules
115 - for data decls, we ignore derivings
117 *** See "THE NAMING STORY" in HsDecls ****
121 hsSigsFVs :: [LSig Name] -> FreeVars
122 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
124 hsSigFVs (TypeSig v ty) = extractHsTyNames ty
125 hsSigFVs (SpecInstSig ty) = extractHsTyNames ty
126 hsSigFVs (SpecSig v ty inl) = extractHsTyNames ty
127 hsSigFVs other = emptyFVs
130 conDeclFVs (L _ (ConDecl { con_qvars = tyvars, con_cxt = context,
131 con_details = details, con_res = res_ty}))
132 = delFVs (map hsLTyVarName tyvars) $
133 extractHsCtxtTyNames context `plusFV`
134 conDetailsFVs details `plusFV`
137 conResTyFVs ResTyH98 = emptyFVs
138 conResTyFVs (ResTyGADT ty) = extractHsTyNames ty
140 conDetailsFVs :: HsConDeclDetails Name -> FreeVars
141 conDetailsFVs details = plusFVs (map bangTyFVs (hsConDeclArgTys details))
143 bangTyFVs bty = extractHsTyNames (getBangType bty)
147 %************************************************************************
149 \subsection{A few functions on generic defintions
151 %************************************************************************
153 These functions on generics are defined over Matches Name, which is
154 why they are here and not in HsMatches.
157 maybeGenericMatch :: LMatch Name -> Maybe (HsType Name, LMatch Name)
158 -- Tells whether a Match is for a generic definition
159 -- and extract the type from a generic match and put it at the front
161 maybeGenericMatch (L loc (Match (L _ (TypePat (L _ ty)) : pats) sig_ty grhss))
162 = Just (ty, L loc (Match pats sig_ty grhss))
164 maybeGenericMatch other_match = Nothing