2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnSource]{Main pass of renamer}
7 module RnTypes ( rnHsType, rnContext,
8 rnHsSigType, rnHsTypeFVs, rnHsSigTypeFVs,
9 rnPat, rnPats, rnPatsAndThen, -- Here because it's not part
10 rnOverLit, litFVs, -- of any mutual recursion
11 precParseErr, sectionPrecErr, dupFieldErr, patSigErr, checkTupSize
14 import CmdLineOpts ( DynFlag(Opt_WarnUnusedMatches, Opt_GlasgowExts) )
17 import RdrHsSyn ( RdrNameContext, RdrNameHsType, RdrNamePat,
18 extractHsRhoRdrTyVars )
19 import RnHsSyn ( RenamedContext, RenamedHsType, RenamedPat,
21 parrTyCon_name, tupleTyCon_name, listTyCon_name, charTyCon_name )
22 import RnEnv ( lookupOccRn, lookupBndrRn, lookupSyntaxName, lookupGlobalOccRn,
23 bindTyVarsRn, lookupFixityRn, mapFvRn, newIPNameRn,
24 bindPatSigTyVarsFV, bindLocalsFV, warnUnusedMatches )
26 import RdrName ( elemLocalRdrEnv )
27 import PrelNames( eqStringName, eqClassName, integralClassName,
28 negateName, minusName, lengthPName, indexPName, plusIntegerName, fromIntegerName,
29 timesIntegerName, ratioDataConName, fromRationalName )
30 import Constants ( mAX_TUPLE_SIZE )
31 import TysWiredIn ( intTyCon )
32 import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
33 floatPrimTyCon, doublePrimTyCon )
34 import Name ( Name, NamedThing(..) )
37 import Literal ( inIntRange, inCharRange )
38 import BasicTypes ( compareFixity )
39 import ListSetOps ( removeDups )
42 #include "HsVersions.h"
45 These type renamers are in a separate module, rather than in (say) RnSource,
46 to break several loop.
48 %*********************************************************
50 \subsection{Renaming types}
52 %*********************************************************
55 rnHsTypeFVs :: SDoc -> RdrNameHsType -> RnM (RenamedHsType, FreeVars)
56 rnHsTypeFVs doc_str ty
57 = rnHsType doc_str ty `thenM` \ ty' ->
58 returnM (ty', extractHsTyNames ty')
60 rnHsSigTypeFVs :: SDoc -> RdrNameHsType -> RnM (RenamedHsType, FreeVars)
61 rnHsSigTypeFVs doc_str ty
62 = rnHsSigType doc_str ty `thenM` \ ty' ->
63 returnM (ty', extractHsTyNames ty')
65 rnHsSigType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
66 -- rnHsSigType is used for source-language type signatures,
67 -- which use *implicit* universal quantification.
68 rnHsSigType doc_str ty
69 = rnHsType (text "In the type signature for" <+> doc_str) ty
72 rnHsType is here because we call it from loadInstDecl, and I didn't
73 want a gratuitous knot.
76 rnHsType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
78 rnHsType doc (HsForAllTy Nothing ctxt ty)
79 -- Implicit quantifiction in source code (no kinds on tyvars)
80 -- Given the signature C => T we universally quantify
81 -- over FV(T) \ {in-scope-tyvars}
82 = getLocalRdrEnv `thenM` \ name_env ->
84 mentioned = extractHsRhoRdrTyVars ctxt ty
86 -- Don't quantify over type variables that are in scope;
87 -- when GlasgowExts is off, there usually won't be any, except for
89 -- class C a where { op :: a -> a }
90 forall_tyvars = filter (not . (`elemLocalRdrEnv` name_env)) mentioned
92 rnForAll doc (map UserTyVar forall_tyvars) ctxt ty
94 rnHsType doc (HsForAllTy (Just forall_tyvars) ctxt tau)
95 -- Explicit quantification.
96 -- Check that the forall'd tyvars are actually
97 -- mentioned in the type, and produce a warning if not
99 mentioned = extractHsRhoRdrTyVars ctxt tau
100 forall_tyvar_names = hsTyVarNames forall_tyvars
102 -- Explicitly quantified but not mentioned in ctxt or tau
103 warn_guys = filter (`notElem` mentioned) forall_tyvar_names
105 mappM_ (forAllWarn doc tau) warn_guys `thenM_`
106 rnForAll doc forall_tyvars ctxt tau
108 rnHsType doc (HsTyVar tyvar)
109 = lookupOccRn tyvar `thenM` \ tyvar' ->
110 returnM (HsTyVar tyvar')
112 rnHsType doc (HsOpTy ty1 op ty2)
113 = lookupOccRn op `thenM` \ op' ->
114 rnHsType doc ty1 `thenM` \ ty1' ->
115 rnHsType doc ty2 `thenM` \ ty2' ->
116 lookupTyFixityRn op' `thenM` \ fix ->
117 mkHsOpTyRn op' fix ty1' ty2'
119 rnHsType doc (HsParTy ty)
120 = rnHsType doc ty `thenM` \ ty' ->
121 returnM (HsParTy ty')
123 rnHsType doc (HsNumTy i)
124 | i == 1 = returnM (HsNumTy i)
125 | otherwise = addErr err_msg `thenM_` returnM (HsNumTy i)
127 err_msg = ptext SLIT("Only unit numeric type pattern is valid")
130 rnHsType doc (HsFunTy ty1 ty2)
131 = rnHsType doc ty1 `thenM` \ ty1' ->
132 -- Might find a for-all as the arg of a function type
133 rnHsType doc ty2 `thenM` \ ty2' ->
134 -- Or as the result. This happens when reading Prelude.hi
135 -- when we find return :: forall m. Monad m -> forall a. a -> m a
136 returnM (HsFunTy ty1' ty2')
138 rnHsType doc (HsListTy ty)
139 = rnHsType doc ty `thenM` \ ty' ->
140 returnM (HsListTy ty')
142 rnHsType doc (HsKindSig ty k)
143 = rnHsType doc ty `thenM` \ ty' ->
144 returnM (HsKindSig ty' k)
146 rnHsType doc (HsPArrTy ty)
147 = rnHsType doc ty `thenM` \ ty' ->
148 returnM (HsPArrTy ty')
150 -- Unboxed tuples are allowed to have poly-typed arguments. These
151 -- sometimes crop up as a result of CPR worker-wrappering dictionaries.
152 rnHsType doc (HsTupleTy tup_con tys)
153 = mappM (rnHsType doc) tys `thenM` \ tys' ->
154 returnM (HsTupleTy tup_con tys')
156 rnHsType doc (HsAppTy ty1 ty2)
157 = rnHsType doc ty1 `thenM` \ ty1' ->
158 rnHsType doc ty2 `thenM` \ ty2' ->
159 returnM (HsAppTy ty1' ty2')
161 rnHsType doc (HsPredTy pred)
162 = rnPred doc pred `thenM` \ pred' ->
163 returnM (HsPredTy pred')
165 rnHsTypes doc tys = mappM (rnHsType doc) tys
170 rnForAll doc forall_tyvars ctxt ty
171 = bindTyVarsRn doc forall_tyvars $ \ new_tyvars ->
172 rnContext doc ctxt `thenM` \ new_ctxt ->
173 rnHsType doc ty `thenM` \ new_ty ->
174 returnM (mkHsForAllTy (Just new_tyvars) new_ctxt new_ty)
178 %*********************************************************
180 \subsection{Fixities}
182 %*********************************************************
184 Infix types are read in a *right-associative* way, so that
189 mkHsOpTyRn rearranges where necessary. The two arguments
190 have already been renamed and rearranged. It's made rather tiresome
191 by the presence of ->
195 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
196 warnIf (not glaExts) (infixTyConWarn n) `thenM_`
199 -- Building (ty1 `op1` (ty21 `op2` ty22))
200 mkHsOpTyRn :: Name -> Fixity
201 -> RenamedHsType -> RenamedHsType
204 mkHsOpTyRn op1 fix1 ty1 ty2@(HsOpTy ty21 op2 ty22)
205 = lookupTyFixityRn op2 `thenM` \ fix2 ->
207 (nofix_error, associate_right) = compareFixity fix1 fix2
210 addErr (precParseErr (quotes (ppr op1),fix1)
211 (quotes (ppr op2),fix2)) `thenM_`
212 returnM (HsOpTy ty1 op1 ty2)
214 if not associate_right then
215 -- Rearrange to ((ty1 `op1` ty21) `op2` ty22)
216 mkHsOpTyRn op1 fix1 ty1 ty21 `thenM` \ new_ty ->
217 returnM (HsOpTy new_ty op2 ty22)
219 returnM (HsOpTy ty1 op1 ty2)
221 mkHsOpTyRn op fix ty1 ty2 -- Default case, no rearrangment
222 = returnM (HsOpTy ty1 op ty2)
225 %*********************************************************
227 \subsection{Contexts and predicates}
229 %*********************************************************
232 rnContext :: SDoc -> RdrNameContext -> RnM RenamedContext
233 rnContext doc ctxt = mappM (rnPred doc) ctxt
235 rnPred doc (HsClassP clas tys)
236 = lookupOccRn clas `thenM` \ clas_name ->
237 rnHsTypes doc tys `thenM` \ tys' ->
238 returnM (HsClassP clas_name tys')
240 rnPred doc (HsIParam n ty)
241 = newIPNameRn n `thenM` \ name ->
242 rnHsType doc ty `thenM` \ ty' ->
243 returnM (HsIParam name ty')
247 *********************************************************
249 \subsection{Patterns}
251 *********************************************************
254 rnPatsAndThen :: HsMatchContext Name
257 -> ([RenamedPat] -> RnM (a, FreeVars))
259 -- Bring into scope all the binders and type variables
260 -- bound by the patterns; then rename the patterns; then
261 -- do the thing inside.
263 -- Note that we do a single bindLocalsRn for all the
264 -- matches together, so that we spot the repeated variable in
267 rnPatsAndThen ctxt repUnused pats thing_inside
268 = bindPatSigTyVarsFV pat_sig_tys $
269 bindLocalsFV doc_pat bndrs $ \ new_bndrs ->
270 rnPats pats `thenM` \ (pats', pat_fvs) ->
271 thing_inside pats' `thenM` \ (res, res_fvs) ->
274 unused_binders = filter (not . (`elemNameSet` res_fvs)) new_bndrs
277 then warnUnusedMatches unused_binders
278 else returnM ()) `thenM_`
279 returnM (res, res_fvs `plusFV` pat_fvs)
281 pat_sig_tys = collectSigTysFromPats pats
282 bndrs = collectPatsBinders pats
283 doc_pat = ptext SLIT("In") <+> pprMatchContext ctxt
285 rnPats :: [RdrNamePat] -> RnM ([RenamedPat], FreeVars)
286 rnPats ps = mapFvRn rnPat ps
288 rnPat :: RdrNamePat -> RnM (RenamedPat, FreeVars)
290 rnPat (WildPat _) = returnM (WildPat placeHolderType, emptyFVs)
293 = lookupBndrRn name `thenM` \ vname ->
294 returnM (VarPat vname, emptyFVs)
296 rnPat (SigPatIn pat ty)
297 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
300 then rnPat pat `thenM` \ (pat', fvs1) ->
301 rnHsTypeFVs doc ty `thenM` \ (ty', fvs2) ->
302 returnM (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
304 else addErr (patSigErr ty) `thenM_`
307 doc = text "In a pattern type-signature"
309 rnPat (LitPat s@(HsString _))
310 = returnM (LitPat s, unitFV eqStringName)
313 = litFVs lit `thenM` \ fvs ->
314 returnM (LitPat lit, fvs)
316 rnPat (NPatIn lit mb_neg)
317 = rnOverLit lit `thenM` \ (lit', fvs1) ->
319 Nothing -> returnM (Nothing, emptyFVs)
320 Just _ -> lookupSyntaxName negateName `thenM` \ (neg, fvs) ->
321 returnM (Just neg, fvs)
322 ) `thenM` \ (mb_neg', fvs2) ->
323 returnM (NPatIn lit' mb_neg',
324 fvs1 `plusFV` fvs2 `addOneFV` eqClassName)
325 -- Needed to find equality on pattern
327 rnPat (NPlusKPatIn name lit _)
328 = rnOverLit lit `thenM` \ (lit', fvs1) ->
329 lookupBndrRn name `thenM` \ name' ->
330 lookupSyntaxName minusName `thenM` \ (minus, fvs2) ->
331 returnM (NPlusKPatIn name' lit' minus,
332 fvs1 `plusFV` fvs2 `addOneFV` integralClassName)
333 -- The Report says that n+k patterns must be in Integral
336 = rnPat pat `thenM` \ (pat', fvs) ->
337 returnM (LazyPat pat', fvs)
339 rnPat (AsPat name pat)
340 = rnPat pat `thenM` \ (pat', fvs) ->
341 lookupBndrRn name `thenM` \ vname ->
342 returnM (AsPat vname pat', fvs)
344 rnPat (ConPatIn con stuff) = rnConPat con stuff
348 = rnPat pat `thenM` \ (pat', fvs) ->
349 returnM (ParPat pat', fvs)
351 rnPat (ListPat pats _)
352 = rnPats pats `thenM` \ (patslist, fvs) ->
353 returnM (ListPat patslist placeHolderType, fvs `addOneFV` listTyCon_name)
355 rnPat (PArrPat pats _)
356 = rnPats pats `thenM` \ (patslist, fvs) ->
357 returnM (PArrPat patslist placeHolderType,
358 fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
360 implicit_fvs = mkFVs [lengthPName, indexPName]
362 rnPat (TuplePat pats boxed)
363 = checkTupSize tup_size `thenM_`
364 rnPats pats `thenM` \ (patslist, fvs) ->
365 returnM (TuplePat patslist boxed, fvs `addOneFV` tycon_name)
367 tup_size = length pats
368 tycon_name = tupleTyCon_name boxed tup_size
370 rnPat (TypePat name) =
371 rnHsTypeFVs (text "In a type pattern") name `thenM` \ (name', fvs) ->
372 returnM (TypePat name', fvs)
374 ------------------------------
375 rnConPat con (PrefixCon pats)
376 = lookupOccRn con `thenM` \ con' ->
377 rnPats pats `thenM` \ (pats', fvs) ->
378 returnM (ConPatIn con' (PrefixCon pats'), fvs `addOneFV` con')
380 rnConPat con (RecCon rpats)
381 = lookupOccRn con `thenM` \ con' ->
382 rnRpats rpats `thenM` \ (rpats', fvs) ->
383 returnM (ConPatIn con' (RecCon rpats'), fvs `addOneFV` con')
385 rnConPat con (InfixCon pat1 pat2)
386 = lookupOccRn con `thenM` \ con' ->
387 rnPat pat1 `thenM` \ (pat1', fvs1) ->
388 rnPat pat2 `thenM` \ (pat2', fvs2) ->
389 lookupFixityRn con' `thenM` \ fixity ->
390 mkConOpPatRn con' fixity pat1' pat2' `thenM` \ pat' ->
391 returnM (pat', fvs1 `plusFV` fvs2 `addOneFV` con')
393 ------------------------
395 = mappM_ field_dup_err dup_fields `thenM_`
396 mapFvRn rn_rpat rpats `thenM` \ (rpats', fvs) ->
397 returnM (rpats', fvs)
399 (_, dup_fields) = removeDups compare [ f | (f,_) <- rpats ]
401 field_dup_err dups = addErr (dupFieldErr "pattern" dups)
404 = lookupGlobalOccRn field `thenM` \ fieldname ->
405 rnPat pat `thenM` \ (pat', fvs) ->
406 returnM ((fieldname, pat'), fvs `addOneFV` fieldname)
410 mkConOpPatRn :: Name -> Fixity -> RenamedPat -> RenamedPat
413 mkConOpPatRn op2 fix2 p1@(ConPatIn op1 (InfixCon p11 p12)) p2
414 = lookupFixityRn op1 `thenM` \ fix1 ->
416 (nofix_error, associate_right) = compareFixity fix1 fix2
419 addErr (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenM_`
420 returnM (ConPatIn op2 (InfixCon p1 p2))
422 if associate_right then
423 mkConOpPatRn op2 fix2 p12 p2 `thenM` \ new_p ->
424 returnM (ConPatIn op1 (InfixCon p11 new_p))
426 returnM (ConPatIn op2 (InfixCon p1 p2))
428 mkConOpPatRn op fix p1 p2 -- Default case, no rearrangment
429 = ASSERT( not_op_pat p2 )
430 returnM (ConPatIn op (InfixCon p1 p2))
432 not_op_pat (ConPatIn _ (InfixCon _ _)) = False
433 not_op_pat other = True
437 %************************************************************************
439 \subsubsection{Literals}
441 %************************************************************************
443 When literals occur we have to make sure
444 that the types and classes they involve
449 = checkErr (inCharRange c) (bogusCharError c) `thenM_`
450 returnM (unitFV charTyCon_name)
452 litFVs (HsCharPrim c) = returnM (unitFV (getName charPrimTyCon))
453 litFVs (HsString s) = returnM (mkFVs [listTyCon_name, charTyCon_name])
454 litFVs (HsStringPrim s) = returnM (unitFV (getName addrPrimTyCon))
455 litFVs (HsInt i) = returnM (unitFV (getName intTyCon))
456 litFVs (HsIntPrim i) = returnM (unitFV (getName intPrimTyCon))
457 litFVs (HsFloatPrim f) = returnM (unitFV (getName floatPrimTyCon))
458 litFVs (HsDoublePrim d) = returnM (unitFV (getName doublePrimTyCon))
459 litFVs lit = pprPanic "RnExpr.litFVs" (ppr lit) -- HsInteger and HsRat only appear
460 -- in post-typechecker translations
462 = ptext SLIT("character literal out of range: '\\") <> int c <> char '\''
464 rnOverLit (HsIntegral i _)
465 = lookupSyntaxName fromIntegerName `thenM` \ (from_integer_name, fvs) ->
467 returnM (HsIntegral i from_integer_name, fvs)
469 extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
470 -- Big integer literals are built, using + and *,
471 -- out of small integers (DsUtils.mkIntegerLit)
472 -- [NB: plusInteger, timesInteger aren't rebindable...
473 -- they are used to construct the argument to fromInteger,
474 -- which is the rebindable one.]
476 returnM (HsIntegral i from_integer_name, fvs `plusFV` extra_fvs)
478 rnOverLit (HsFractional i _)
479 = lookupSyntaxName fromRationalName `thenM` \ (from_rat_name, fvs) ->
481 extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
482 -- We have to make sure that the Ratio type is imported with
483 -- its constructor, because literals of type Ratio t are
484 -- built with that constructor.
485 -- The Rational type is needed too, but that will come in
486 -- as part of the type for fromRational.
487 -- The plus/times integer operations may be needed to construct the numerator
488 -- and denominator (see DsUtils.mkIntegerLit)
490 returnM (HsFractional i from_rat_name, fvs `plusFV` extra_fvs)
495 %*********************************************************
499 %*********************************************************
502 checkTupSize :: Int -> RnM ()
503 checkTupSize tup_size
504 | tup_size <= mAX_TUPLE_SIZE
507 = addErr (sep [ptext SLIT("A") <+> int tup_size <> ptext SLIT("-tuple is too large for GHC"),
508 nest 2 (parens (ptext SLIT("max size is") <+> int mAX_TUPLE_SIZE)),
509 nest 2 (ptext SLIT("Workaround: use nested tuples or define a data type"))])
511 forAllWarn doc ty tyvar
512 = ifOptM Opt_WarnUnusedMatches $
513 addWarn (sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
514 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
520 = hang (ptext SLIT("precedence parsing error"))
521 4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"),
523 ptext SLIT("in the same infix expression")])
525 sectionPrecErr op arg_op section
526 = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
527 nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
528 nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
531 = ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op)
534 = (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
535 $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
537 dupFieldErr str (dup:rest)
538 = hsep [ptext SLIT("duplicate field name"),
540 ptext SLIT("in record"), text str]
542 ppr_op op = quotes (ppr op) -- Here, op can be a Name or a (Var n), where n is a Name
543 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)