2 % (c) The AQUA Project, Glasgow University, 1996
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 dummyRdrVarName, dummyRdrTcName,
49 showRdr, rdrNameOcc, rdrNameModule, ieOcc,
50 cmpRdr, prefixRdrName,
51 mkOpApp, mkClassDecl, isClassDataConRdrName
55 #include "HsVersions.h"
59 import BasicTypes ( Module, IfaceFlavour(..), Unused )
60 import Name ( pprModule, OccName(..), pprOccName,
61 prefixOccName, NamedThing(..) )
62 import Util ( thenCmp )
63 import HsPragmas ( GenPragmas, ClassPragmas, DataPragmas, ClassOpPragmas, InstancePragmas )
67 import Char ( isUpper )
71 type RdrNameArithSeqInfo = ArithSeqInfo Unused RdrName RdrNamePat
72 type RdrNameBangType = BangType RdrName
73 type RdrNameClassDecl = ClassDecl Unused RdrName RdrNamePat
74 type RdrNameClassOpSig = Sig RdrName
75 type RdrNameConDecl = ConDecl RdrName
76 type RdrNameContext = Context RdrName
77 type RdrNameHsDecl = HsDecl Unused RdrName RdrNamePat
78 type RdrNameSpecDataSig = SpecDataSig RdrName
79 type RdrNameDefaultDecl = DefaultDecl RdrName
80 type RdrNameForeignDecl = ForeignDecl RdrName
81 type RdrNameFixityDecl = FixityDecl RdrName
82 type RdrNameGRHS = GRHS Unused RdrName RdrNamePat
83 type RdrNameGRHSsAndBinds = GRHSsAndBinds Unused RdrName RdrNamePat
84 type RdrNameHsBinds = HsBinds Unused RdrName RdrNamePat
85 type RdrNameHsExpr = HsExpr Unused RdrName RdrNamePat
86 type RdrNameHsModule = HsModule Unused RdrName RdrNamePat
87 type RdrNameIE = IE RdrName
88 type RdrNameImportDecl = ImportDecl RdrName
89 type RdrNameInstDecl = InstDecl Unused RdrName RdrNamePat
90 type RdrNameMatch = Match Unused RdrName RdrNamePat
91 type RdrNameMonoBinds = MonoBinds Unused RdrName RdrNamePat
92 type RdrNamePat = InPat RdrName
93 type RdrNameHsType = HsType RdrName
94 type RdrNameSig = Sig RdrName
95 type RdrNameStmt = Stmt Unused RdrName RdrNamePat
96 type RdrNameTyDecl = TyDecl RdrName
98 type RdrNameClassOpPragmas = ClassOpPragmas RdrName
99 type RdrNameClassPragmas = ClassPragmas RdrName
100 type RdrNameDataPragmas = DataPragmas RdrName
101 type RdrNameGenPragmas = GenPragmas RdrName
102 type RdrNameInstancePragmas = InstancePragmas RdrName
105 @extractHsTyVars@ looks just for things that could be type variables.
106 It's used when making the for-alls explicit.
109 extractHsTyVars :: HsType RdrName -> [RdrName]
110 extractHsTyVars ty = nub (extract_ty ty [])
112 extractHsCtxtTyVars :: Context RdrName -> [RdrName]
113 extractHsCtxtTyVars ty = nub (extract_ctxt ty [])
115 extract_ctxt ctxt acc = foldr extract_ass [] ctxt
117 extract_ass (cls, tys) acc = foldr extract_ty acc tys
119 extract_ty (MonoTyApp ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
120 extract_ty (MonoListTy tc ty) acc = extract_ty ty acc
121 extract_ty (MonoTupleTy tc tys) acc = foldr extract_ty acc tys
122 extract_ty (MonoFunTy ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
123 extract_ty (MonoDictTy cls tys) acc = foldr extract_ty acc tys
124 extract_ty (MonoTyVar tv) acc = insert tv acc
126 -- In (All a => a -> a) -> Int, there are no free tyvars
127 -- We just assume that we quantify over all type variables mentioned in the context.
128 extract_ty (HsPreForAllTy ctxt ty) acc = filter (`notElem` locals) (extract_ty ty [])
131 locals = extract_ctxt ctxt []
133 extract_ty (HsForAllTy tvs ctxt ty) acc = acc ++
134 (filter (`notElem` locals) $
135 extract_ctxt ctxt (extract_ty ty []))
137 locals = map getTyVarName tvs
140 insert (Qual _ _ _) acc = acc
141 insert (Unqual (TCOcc _)) acc = acc
142 insert other acc = other : acc
146 A useful function for building @OpApps@. The operator is always a variable,
147 and we don't know the fixity yet.
150 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
153 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
154 by deriving them from the name of the class.
157 mkClassDecl cxt cname tyvars sigs mbinds prags loc
158 = ClassDecl cxt cname tyvars sigs mbinds prags tname dname loc
160 -- The datacon and tycon are called ":C" where the class is C
161 -- This prevents name clashes with user-defined tycons or datacons C
162 (dname, tname) = case cname of
163 Qual m (TCOcc s) hif -> (Qual m (VarOcc s1) hif, Qual m (TCOcc s1) hif)
165 s1 = SLIT(":") _APPEND_ s
167 Unqual (TCOcc s) -> (Unqual (VarOcc s1), Unqual (TCOcc s1))
169 s1 = SLIT(":") _APPEND_ s
171 -- This nasty little function tests for whether a RdrName was
172 -- constructed by the above process. It's used only for filtering
173 -- out duff error messages. Maybe there's a tidier way of doing this
174 -- but I can't work up the energy to find it.
176 isClassDataConRdrName rdr_name
177 = case rdrNameOcc rdr_name of
178 TCOcc s -> case _UNPK_ s of
179 ':' : c : _ -> isUpper c
184 %************************************************************************
186 \subsection[RdrName]{The @RdrName@ datatype; names read from files}
188 %************************************************************************
193 | Qual Module OccName IfaceFlavour -- HiBootFile for M!.t (interface files only),
194 -- HiFile for the common M.t
196 qual (m,n) = Qual m n HiFile
197 tcQual (m,n) = Qual m (TCOcc n) HiFile
198 varQual (m,n) = Qual m (VarOcc n) HiFile
200 lexTcQual (m,n,hif) = Qual m (TCOcc n) hif
201 lexVarQual (m,n,hif) = Qual m (VarOcc n) hif
203 -- This guy is used by the reader when HsSyn has a slot for
204 -- an implicit name that's going to be filled in by
205 -- the renamer. We can't just put "error..." because
206 -- we sometimes want to print out stuff after reading but
208 dummyRdrVarName = Unqual (VarOcc SLIT("V-DUMMY"))
209 dummyRdrTcName = Unqual (VarOcc SLIT("TC-DUMMY"))
212 varUnqual n = Unqual (VarOcc n)
214 isUnqual (Unqual _) = True
215 isUnqual (Qual _ _ _) = False
217 isQual (Unqual _) = False
218 isQual (Qual _ _ _) = True
220 -- Used for adding a prefix to a RdrName
221 prefixRdrName :: FAST_STRING -> RdrName -> RdrName
222 prefixRdrName prefix (Qual m n hif) = Qual m (prefixOccName prefix n) hif
223 prefixRdrName prefix (Unqual n) = Unqual (prefixOccName prefix n)
225 cmpRdr (Unqual n1) (Unqual n2) = n1 `compare` n2
226 cmpRdr (Unqual n1) (Qual m2 n2 _) = LT
227 cmpRdr (Qual m1 n1 _) (Unqual n2) = GT
228 cmpRdr (Qual m1 n1 _) (Qual m2 n2 _) = (n1 `compare` n2) `thenCmp` (m1 `compare` m2)
229 -- always compare module-names *second*
231 rdrNameOcc :: RdrName -> OccName
232 rdrNameOcc (Unqual occ) = occ
233 rdrNameOcc (Qual _ occ _) = occ
235 rdrNameModule :: RdrName -> Module
236 rdrNameModule (Qual m _ _) = m
238 ieOcc :: RdrNameIE -> OccName
239 ieOcc ie = rdrNameOcc (ieName ie)
241 instance Text RdrName where -- debugging
242 showsPrec _ rn = showString (showSDoc (ppr rn))
244 instance Eq RdrName where
245 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
246 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
248 instance Ord RdrName where
249 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
250 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
251 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
252 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
253 compare a b = cmpRdr a b
255 instance Outputable RdrName where
256 ppr (Unqual n) = pprOccName n
257 ppr (Qual m n _) = hcat [pprModule m, char '.', pprOccName n]
259 instance NamedThing RdrName where -- Just so that pretty-printing of expressions works
260 getOccName = rdrNameOcc
261 getName = panic "no getName for RdrNames"
263 showRdr rdr = showSDoc (ppr rdr)