2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[RdrHsSyn]{Specialisations of the @HsSyn@ syntax for the reader}
6 (Well, really, for specialisations involving @RdrName@s, even if
7 they are used somewhat later on in the compiler...)
38 RdrNameClassOpPragmas,
42 RdrNameInstancePragmas,
43 extractHsTyVars, extractHsCtxtTyVars,
46 qual, varQual, tcQual, varUnqual, lexVarQual, lexTcQual,
47 mkTupConRdrName, mkUbxTupConRdrName,
48 dummyRdrVarName, dummyRdrTcName,
50 rdrNameOcc, rdrNameModule, ieOcc,
51 cmpRdr, prefixRdrName,
52 mkOpApp, mkClassDecl, isClassDataConRdrName
56 #include "HsVersions.h"
59 import BasicTypes ( Module, IfaceFlavour(..), Unused )
60 import Name ( pprModule, OccName(..), pprOccName,
61 mkTupNameStr, mkUbxTupNameStr,
62 prefixOccName, NamedThing(..),
63 mkClassTyConStr, mkClassDataConStr )
64 import Util ( thenCmp )
65 import HsPragmas ( GenPragmas, ClassPragmas, DataPragmas, ClassOpPragmas, InstancePragmas )
69 import Char ( isUpper )
73 type RdrNameArithSeqInfo = ArithSeqInfo Unused RdrName RdrNamePat
74 type RdrNameBangType = BangType RdrName
75 type RdrNameClassDecl = ClassDecl Unused RdrName RdrNamePat
76 type RdrNameClassOpSig = Sig RdrName
77 type RdrNameConDecl = ConDecl RdrName
78 type RdrNameContext = Context RdrName
79 type RdrNameHsDecl = HsDecl Unused RdrName RdrNamePat
80 type RdrNameSpecDataSig = SpecDataSig RdrName
81 type RdrNameDefaultDecl = DefaultDecl RdrName
82 type RdrNameForeignDecl = ForeignDecl RdrName
83 type RdrNameFixityDecl = FixityDecl RdrName
84 type RdrNameGRHS = GRHS Unused RdrName RdrNamePat
85 type RdrNameGRHSsAndBinds = GRHSsAndBinds Unused RdrName RdrNamePat
86 type RdrNameHsBinds = HsBinds Unused RdrName RdrNamePat
87 type RdrNameHsExpr = HsExpr Unused RdrName RdrNamePat
88 type RdrNameHsModule = HsModule Unused RdrName RdrNamePat
89 type RdrNameIE = IE RdrName
90 type RdrNameImportDecl = ImportDecl RdrName
91 type RdrNameInstDecl = InstDecl Unused RdrName RdrNamePat
92 type RdrNameMatch = Match Unused RdrName RdrNamePat
93 type RdrNameMonoBinds = MonoBinds Unused RdrName RdrNamePat
94 type RdrNamePat = InPat RdrName
95 type RdrNameHsType = HsType RdrName
96 type RdrNameSig = Sig RdrName
97 type RdrNameStmt = Stmt Unused RdrName RdrNamePat
98 type RdrNameTyDecl = TyDecl RdrName
100 type RdrNameClassOpPragmas = ClassOpPragmas RdrName
101 type RdrNameClassPragmas = ClassPragmas RdrName
102 type RdrNameDataPragmas = DataPragmas RdrName
103 type RdrNameGenPragmas = GenPragmas RdrName
104 type RdrNameInstancePragmas = InstancePragmas RdrName
107 @extractHsTyVars@ looks just for things that could be type variables.
108 It's used when making the for-alls explicit.
111 extractHsTyVars :: HsType RdrName -> [RdrName]
112 extractHsTyVars ty = nub (extract_ty ty [])
114 extractHsCtxtTyVars :: Context RdrName -> [RdrName]
115 extractHsCtxtTyVars ty = nub (extract_ctxt ty [])
117 extract_ctxt ctxt acc = foldr extract_ass acc ctxt
119 extract_ass (cls, tys) acc = foldr extract_ty acc tys
121 extract_ty (MonoTyApp ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
122 extract_ty (MonoListTy ty) acc = extract_ty ty acc
123 extract_ty (MonoTupleTy tys _) acc = foldr extract_ty acc tys
124 extract_ty (MonoFunTy ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
125 extract_ty (MonoDictTy cls tys) acc = foldr extract_ty acc tys
126 extract_ty (MonoTyVar tv) acc = insert tv acc
127 extract_ty (HsForAllTy tvs ctxt ty) acc = acc ++
128 (filter (`notElem` locals) $
129 extract_ctxt ctxt (extract_ty ty []))
131 locals = map getTyVarName tvs
133 insert (Qual _ _ _) acc = acc
134 insert (Unqual (TCOcc _)) acc = acc
135 insert other acc = other : acc
139 A useful function for building @OpApps@. The operator is always a variable,
140 and we don't know the fixity yet.
143 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
146 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
147 by deriving them from the name of the class.
150 mkClassDecl cxt cname tyvars sigs mbinds prags loc
151 = ClassDecl cxt cname tyvars sigs mbinds prags tname dname loc
153 -- The datacon and tycon are called ":C" where the class is C
154 -- This prevents name clashes with user-defined tycons or datacons C
155 (dname, tname) = case cname of
156 Qual m (TCOcc s) hif -> (Qual m (VarOcc s1) hif, Qual m (TCOcc s1) hif)
158 s1 = mkClassTyConStr s
160 Unqual (TCOcc s) -> (Unqual (VarOcc s1), Unqual (TCOcc s1))
162 s1 = mkClassDataConStr s
164 -- This nasty little function tests for whether a RdrName was
165 -- constructed by the above process. It's used only for filtering
166 -- out duff error messages. Maybe there's a tidier way of doing this
167 -- but I can't work up the energy to find it.
169 isClassDataConRdrName rdr_name
170 = case rdrNameOcc rdr_name of
171 TCOcc s -> case _UNPK_ s of
172 ':' : c : _ -> isUpper c
177 %************************************************************************
179 \subsection[RdrName]{The @RdrName@ datatype; names read from files}
181 %************************************************************************
186 | Qual Module OccName IfaceFlavour -- HiBootFile for M!.t (interface files only),
187 -- HiFile for the common M.t
189 qual (m,n) = Qual m n HiFile
190 tcQual (m,n) = Qual m (TCOcc n) HiFile
191 varQual (m,n) = Qual m (VarOcc n) HiFile
193 mkTupConRdrName :: Int -> RdrName -- The name for the tuple data construtor
195 mkTupConRdrName arity = case mkTupNameStr arity of
196 (mod, occ) -> Qual mod (VarOcc occ) HiFile
198 mkUbxTupConRdrName :: Int -> RdrName -- The name for the tuple data construtor
200 mkUbxTupConRdrName arity = case mkUbxTupNameStr arity of
201 (mod, occ) -> Qual mod (VarOcc occ) HiFile
203 lexTcQual (m,n,hif) = Qual m (TCOcc n) hif
204 lexVarQual (m,n,hif) = Qual m (VarOcc n) hif
206 -- This guy is used by the reader when HsSyn has a slot for
207 -- an implicit name that's going to be filled in by
208 -- the renamer. We can't just put "error..." because
209 -- we sometimes want to print out stuff after reading but
211 dummyRdrVarName = Unqual (VarOcc SLIT("V-DUMMY"))
212 dummyRdrTcName = Unqual (VarOcc SLIT("TC-DUMMY"))
215 varUnqual n = Unqual (VarOcc n)
217 isUnqual (Unqual _) = True
218 isUnqual (Qual _ _ _) = False
220 isQual (Unqual _) = False
221 isQual (Qual _ _ _) = True
224 -- Used for adding a prefix to a RdrName
225 prefixRdrName :: FAST_STRING -> RdrName -> RdrName
226 prefixRdrName prefix (Qual m n hif) = Qual m (prefixOccName prefix n) hif
227 prefixRdrName prefix (Unqual n) = Unqual (prefixOccName prefix n)
229 cmpRdr (Unqual n1) (Unqual n2) = n1 `compare` n2
230 cmpRdr (Unqual n1) (Qual m2 n2 _) = LT
231 cmpRdr (Qual m1 n1 _) (Unqual n2) = GT
232 cmpRdr (Qual m1 n1 _) (Qual m2 n2 _) = (n1 `compare` n2) `thenCmp` (m1 `compare` m2)
233 -- always compare module-names *second*
235 rdrNameOcc :: RdrName -> OccName
236 rdrNameOcc (Unqual occ) = occ
237 rdrNameOcc (Qual _ occ _) = occ
239 rdrNameModule :: RdrName -> Module
240 rdrNameModule (Qual m _ _) = m
242 ieOcc :: RdrNameIE -> OccName
243 ieOcc ie = rdrNameOcc (ieName ie)
245 instance Show RdrName where -- debugging
246 showsPrec p rn = showsPrecSDoc p (ppr rn)
248 instance Eq RdrName where
249 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
250 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
252 instance Ord RdrName where
253 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
254 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
255 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
256 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
257 compare a b = cmpRdr a b
259 instance Outputable RdrName where
260 ppr (Unqual n) = pprOccName n
261 ppr (Qual m n _) = hcat [pprModule m, char '.', pprOccName n]
263 instance NamedThing RdrName where -- Just so that pretty-printing of expressions works
264 getOccName = rdrNameOcc
265 getName = panic "no getName for RdrNames"