swap <[]> and <{}> syntax
[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   ) where
16
17 #include "HsVersions.h"
18
19 import HsSyn
20 import Class            ( FunDep )
21 import TysWiredIn       ( tupleTyCon, listTyCon, parrTyCon, hetMetCodeTypeTyCon, charTyCon )
22 import Name             ( Name, getName, isTyVarName )
23 import NameSet
24 import BasicTypes       ( Boxity )
25 import SrcLoc
26 \end{code}
27
28 %************************************************************************
29 %*                                                                      *
30 \subsection{Free variables}
31 %*                                                                      *
32 %************************************************************************
33
34 These free-variable finders returns tycons and classes too.
35
36 \begin{code}
37 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
38 charTyCon_name    = getName charTyCon
39 listTyCon_name    = getName listTyCon
40 parrTyCon_name    = getName parrTyCon
41 hetMetCodeTypeTyCon_name :: Name
42 hetMetCodeTypeTyCon_name  = getName hetMetCodeTypeTyCon
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 (HsModalBoxType  ecn ty) = (unitNameSet ecn)
63                                    `unionNameSets`
64                                    (unitNameSet hetMetCodeTypeTyCon_name)
65                                    `unionNameSets`
66                                    (getl ty)
67     get (HsKappaTy ty1 ty2)    = getl ty1 `unionNameSets` getl ty2
68     get (HsTupleTy _ tys)      = extractHsTyNames_s tys
69     get (HsFunTy ty1 ty2)      = getl ty1 `unionNameSets` getl ty2
70     get (HsPredTy p)           = extractHsPredTyNames p
71     get (HsOpTy ty1 op ty2)    = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
72     get (HsParTy ty)           = getl ty
73     get (HsBangTy _ ty)        = getl ty
74     get (HsRecTy flds)         = extractHsTyNames_s (map cd_fld_type flds)
75     get (HsTyVar tv)           = unitNameSet tv
76     get (HsSpliceTy _ fvs _)   = fvs
77     get (HsQuasiQuoteTy {})    = emptyNameSet
78     get (HsKindSig ty _)       = getl ty
79     get (HsForAllTy _ tvs
80                     ctxt ty)   = (extractHsCtxtTyNames ctxt
81                                          `unionNameSets` getl ty)
82                                             `minusNameSet`
83                                   mkNameSet (hsLTyVarNames tvs)
84     get (HsDocTy ty _)         = getl ty
85     get (HsCoreTy {})          = emptyNameSet   -- This probably isn't quite right
86                                                 -- but I don't think it matters
87
88 extractHsTyNames_s  :: [LHsType Name] -> NameSet
89 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
90
91 extractHsCtxtTyNames :: LHsContext Name -> NameSet
92 extractHsCtxtTyNames (L _ ctxt)
93   = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
94
95 -- You don't import or export implicit parameters,
96 -- so don't mention the IP names
97 extractHsPredTyNames :: HsPred Name -> NameSet
98 extractHsPredTyNames (HsClassP cls tys)
99   = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
100 extractHsPredTyNames (HsEqualP ty1 ty2)
101   = extractHsTyNames ty1 `unionNameSets` extractHsTyNames ty2
102 extractHsPredTyNames (HsIParam _ ty)
103   = extractHsTyNames ty
104 \end{code}
105
106
107 %************************************************************************
108 %*                                                                      *
109 \subsection{Free variables of declarations}
110 %*                                                                      *
111 %************************************************************************
112
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
118         - for data decls, we ignore derivings
119
120         *** See "THE NAMING STORY" in HsDecls ****
121
122 \begin{code}
123 ----------------
124 hsSigsFVs :: [LSig Name] -> FreeVars
125 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
126
127 hsSigFVs :: Sig Name -> FreeVars
128 hsSigFVs (TypeSig _ ty)    = extractHsTyNames ty
129 hsSigFVs (GenericSig _ ty) = extractHsTyNames ty
130 hsSigFVs (SpecInstSig ty)  = extractHsTyNames ty
131 hsSigFVs (SpecSig _ ty _)  = extractHsTyNames ty
132 hsSigFVs _                 = emptyFVs
133
134 ----------------
135 conDeclFVs :: LConDecl Name -> FreeVars
136 conDeclFVs (L _ (ConDecl { con_qvars = tyvars, con_cxt = context,
137                            con_details = details, con_res = res_ty}))
138   = delFVs (map hsLTyVarName tyvars) $
139     extractHsCtxtTyNames context  `plusFV`
140     conDetailsFVs details         `plusFV`
141     conResTyFVs res_ty
142
143 conResTyFVs :: ResType Name -> FreeVars
144 conResTyFVs ResTyH98       = emptyFVs
145 conResTyFVs (ResTyGADT ty) = extractHsTyNames ty
146
147 conDetailsFVs :: HsConDeclDetails Name -> FreeVars
148 conDetailsFVs details = plusFVs (map bangTyFVs (hsConDeclArgTys details))
149
150 bangTyFVs :: LHsType Name -> FreeVars
151 bangTyFVs bty = extractHsTyNames (getBangType bty)
152 \end{code}