cb0727b7cc3ec456689afc9a513cbf61ff8961b2
[ghc-hetmet.git] / compiler / rename / RnHsSyn.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
5
6 \begin{code}
7 module RnHsSyn(
8         -- Names
9         charTyCon_name, listTyCon_name, parrTyCon_name, tupleTyCon_name,
10         extractHsTyVars, extractHsTyNames, extractHsTyNames_s,
11         extractFunDepNames, extractHsCtxtTyNames, extractHsPredTyNames,
12
13         -- Free variables
14         hsSigsFVs, hsSigFVs, conDeclFVs, bangTyFVs,
15
16         maybeGenericMatch
17   ) where
18
19 #include "HsVersions.h"
20
21 import HsSyn
22 import Class            ( FunDep )
23 import TysWiredIn       ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
24 import Name             ( Name, getName, isTyVarName )
25 import NameSet
26 import BasicTypes       ( Boxity )
27 import SrcLoc           ( Located(..), unLoc )
28 \end{code}
29
30 %************************************************************************
31 %*                                                                      *
32 \subsection{Free variables}
33 %*                                                                      *
34 %************************************************************************
35
36 These free-variable finders returns tycons and classes too.
37
38 \begin{code}
39 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
40 charTyCon_name    = getName charTyCon
41 listTyCon_name    = getName listTyCon
42 parrTyCon_name    = getName parrTyCon
43
44 tupleTyCon_name :: Boxity -> Int -> Name
45 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
46
47 extractHsTyVars :: LHsType Name -> NameSet
48 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
49
50 extractFunDepNames :: FunDep Name -> NameSet
51 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
52
53 extractHsTyNames   :: LHsType Name -> NameSet
54 extractHsTyNames ty
55   = getl ty
56   where
57     getl (L _ ty) = get ty
58
59     get (HsAppTy ty1 ty2)      = getl ty1 `unionNameSets` getl ty2
60     get (HsListTy ty)          = unitNameSet listTyCon_name `unionNameSets` getl ty
61     get (HsPArrTy ty)          = unitNameSet parrTyCon_name `unionNameSets` getl ty
62     get (HsTupleTy _ tys)      = extractHsTyNames_s tys
63     get (HsFunTy ty1 ty2)      = getl ty1 `unionNameSets` getl ty2
64     get (HsPredTy p)           = extractHsPredTyNames p
65     get (HsOpTy ty1 op ty2)    = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
66     get (HsParTy ty)           = getl ty
67     get (HsBangTy _ ty)        = getl ty
68     get (HsRecTy flds)         = extractHsTyNames_s (map cd_fld_type flds)
69     get (HsNumTy _)            = emptyNameSet
70     get (HsTyVar tv)           = unitNameSet tv
71     get (HsSpliceTy {})        = emptyNameSet   -- Type splices mention no type variables
72     get (HsSpliceTyOut {})     = emptyNameSet   -- Ditto
73     get (HsQuasiQuoteTy {})    = emptyNameSet   -- Ditto
74     get (HsKindSig ty _)       = getl ty
75     get (HsForAllTy _ tvs
76                     ctxt ty)   = (extractHsCtxtTyNames ctxt
77                                          `unionNameSets` getl ty)
78                                             `minusNameSet`
79                                   mkNameSet (hsLTyVarNames tvs)
80     get (HsDocTy ty _)         = getl ty
81
82 extractHsTyNames_s  :: [LHsType Name] -> NameSet
83 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
84
85 extractHsCtxtTyNames :: LHsContext Name -> NameSet
86 extractHsCtxtTyNames (L _ ctxt)
87   = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
88
89 -- You don't import or export implicit parameters,
90 -- so don't mention the IP names
91 extractHsPredTyNames :: HsPred Name -> NameSet
92 extractHsPredTyNames (HsClassP cls tys)
93   = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
94 extractHsPredTyNames (HsEqualP ty1 ty2)
95   = extractHsTyNames ty1 `unionNameSets` extractHsTyNames ty2
96 extractHsPredTyNames (HsIParam _ ty)
97   = extractHsTyNames ty
98 \end{code}
99
100
101 %************************************************************************
102 %*                                                                      *
103 \subsection{Free variables of declarations}
104 %*                                                                      *
105 %************************************************************************
106
107 Return the Names that must be in scope if we are to use this declaration.
108 In all cases this is set up for interface-file declarations:
109         - for class decls we ignore the bindings
110         - for instance decls likewise, plus the pragmas
111         - for rule decls, we ignore HsRules
112         - for data decls, we ignore derivings
113
114         *** See "THE NAMING STORY" in HsDecls ****
115
116 \begin{code}
117 ----------------
118 hsSigsFVs :: [LSig Name] -> FreeVars
119 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
120
121 hsSigFVs :: Sig Name -> FreeVars
122 hsSigFVs (TypeSig _ ty)   = extractHsTyNames ty
123 hsSigFVs (SpecInstSig ty) = extractHsTyNames ty
124 hsSigFVs (SpecSig _ ty _) = extractHsTyNames ty
125 hsSigFVs _                = emptyFVs
126
127 ----------------
128 conDeclFVs :: LConDecl Name -> FreeVars
129 conDeclFVs (L _ (ConDecl { con_qvars = tyvars, con_cxt = context,
130                            con_details = details, con_res = res_ty}))
131   = delFVs (map hsLTyVarName tyvars) $
132     extractHsCtxtTyNames context  `plusFV`
133     conDetailsFVs details         `plusFV`
134     conResTyFVs res_ty
135
136 conResTyFVs :: ResType Name -> FreeVars
137 conResTyFVs ResTyH98       = emptyFVs
138 conResTyFVs (ResTyGADT ty) = extractHsTyNames ty
139
140 conDetailsFVs :: HsConDeclDetails Name -> FreeVars
141 conDetailsFVs details = plusFVs (map bangTyFVs (hsConDeclArgTys details))
142
143 bangTyFVs :: LHsType Name -> FreeVars
144 bangTyFVs bty = extractHsTyNames (getBangType bty)
145 \end{code}
146
147
148 %************************************************************************
149 %*                                                                      *
150 \subsection{A few functions on generic defintions
151 %*                                                                      *
152 %************************************************************************
153
154 These functions on generics are defined over Matches Name, which is
155 why they are here and not in HsMatches.
156
157 \begin{code}
158 maybeGenericMatch :: LMatch Name -> Maybe (HsType Name, LMatch Name)
159   -- Tells whether a Match is for a generic definition
160   -- and extract the type from a generic match and put it at the front
161
162 maybeGenericMatch (L loc (Match (L _ (TypePat (L _ ty)) : pats) sig_ty grhss))
163   = Just (ty, L loc (Match pats sig_ty grhss))
164
165 maybeGenericMatch _ = Nothing
166 \end{code}