[project @ 2001-06-13 15:50:57 by rrt]
[ghc-hetmet.git] / ghc / compiler / rename / RnExpr.lhs
index eb92cd3..e7add89 100644 (file)
@@ -146,8 +146,9 @@ rnPat (RecPatIn con rpats)
   = lookupOccRn con    `thenRn` \ con' ->
     rnRpats rpats      `thenRn` \ (rpats', fvs) ->
     returnRn (RecPatIn con' rpats', fvs `addOneFV` con')
+
 rnPat (TypePatIn name) =
-    (rnHsTypeFVs (text "type pattern") name) `thenRn` \ (name', fvs) ->
+    rnHsTypeFVs (text "type pattern") name     `thenRn` \ (name', fvs) ->
     returnRn (TypePatIn name', fvs)
 \end{code}
 
@@ -158,30 +159,26 @@ rnPat (TypePatIn name) =
 ************************************************************************
 
 \begin{code}
-rnMatch :: RdrNameMatch -> RnMS (RenamedMatch, FreeVars)
+rnMatch :: HsMatchContext RdrName -> RdrNameMatch -> RnMS (RenamedMatch, FreeVars)
 
-rnMatch match@(Match _ pats maybe_rhs_sig grhss)
+rnMatch ctxt match@(Match _ pats maybe_rhs_sig grhss)
   = pushSrcLocRn (getMatchLoc match)   $
 
-       -- Find the universally quantified type variables
-       -- in the pattern type signatures
-    getLocalNameEnv                    `thenRn` \ name_env ->
+       -- Bind pattern-bound type variables
     let
-       tyvars_in_sigs = rhs_sig_tyvars `unionLists` tyvars_in_pats
-       rhs_sig_tyvars = case maybe_rhs_sig of
+       rhs_sig_tys =  case maybe_rhs_sig of
                                Nothing -> []
-                               Just ty -> extractHsTyRdrTyVars ty
-       tyvars_in_pats = extractPatsTyVars pats
-       forall_tyvars  = filter (not . (`elemFM` name_env)) tyvars_in_sigs
-       doc_sig        = text "a pattern type-signature"
-       doc_pats       = text "a pattern match"
+                               Just ty -> [ty]
+       pat_sig_tys = collectSigTysFromPats pats
+       doc_sig     = text "In a result type-signature"
+       doc_pat     = pprMatchContext ctxt
     in
-    bindNakedTyVarsFVRn doc_sig forall_tyvars  $ \ sig_tyvars ->
+    bindPatSigTyVars (rhs_sig_tys ++ pat_sig_tys)      $ \ sig_tyvars ->
 
        -- Note that we do a single bindLocalsRn for all the
        -- matches together, so that we spot the repeated variable in
        --      f x x = 1
-    bindLocalsFVRn doc_pats (collectPatsBinders pats) $ \ new_binders ->
+    bindLocalsFVRn doc_pat (collectPatsBinders pats)   $ \ new_binders ->
 
     mapFvRn rnPat pats                 `thenRn` \ (pats', pat_fvs) ->
     rnGRHSs grhss                      `thenRn` \ (grhss', grhss_fvs) ->
@@ -203,6 +200,21 @@ rnMatch match@(Match _ pats maybe_rhs_sig grhss)
     
     returnRn (Match sig_tyvars pats' maybe_rhs_sig' grhss', all_fvs)
        -- The bindLocals and bindTyVars will remove the bound FVs
+
+
+bindPatSigTyVars :: [RdrNameHsType]
+                -> ([Name] -> RnMS (a, FreeVars))
+                -> RnMS (a, FreeVars)
+  -- Find the type variables in the pattern type 
+  -- signatures that must be brought into scope
+bindPatSigTyVars tys thing_inside
+  = getLocalNameEnv                    `thenRn` \ name_env ->
+    let
+       tyvars_in_sigs = extractHsTysRdrTyVars tys
+       forall_tyvars  = filter (not . (`elemFM` name_env)) tyvars_in_sigs
+       doc_sig        = text "In a pattern type-signature"
+    in
+    bindNakedTyVarsFVRn doc_sig forall_tyvars thing_inside
 \end{code}
 
 %************************************************************************
@@ -235,9 +247,9 @@ rnGRHS (GRHS guarded locn)
        -- Standard Haskell 1.4 guards are just a single boolean
        -- expression, rather than a list of qualifiers as in the
        -- Glasgow extension
-    is_standard_guard [ExprStmt _ _]               = True
-    is_standard_guard [ExprStmt _ _, ExprStmt _ _] = True
-    is_standard_guard other                       = False
+    is_standard_guard [ResultStmt _ _]               = True
+    is_standard_guard [ExprStmt _ _, ResultStmt _ _] = True
+    is_standard_guard other                         = False
 \end{code}
 
 %************************************************************************
@@ -294,7 +306,7 @@ rnExpr (HsOverLit lit)
     returnRn (HsOverLit lit', fvs)
 
 rnExpr (HsLam match)
-  = rnMatch match      `thenRn` \ (match', fvMatch) ->
+  = rnMatch LambdaExpr match   `thenRn` \ (match', fvMatch) ->
     returnRn (HsLam match', fvMatch)
 
 rnExpr (HsApp fun arg)
@@ -358,8 +370,8 @@ rnExpr (HsSCC lbl expr)
 
 rnExpr (HsCase expr ms src_loc)
   = pushSrcLocRn src_loc $
-    rnExpr expr                        `thenRn` \ (new_expr, e_fvs) ->
-    mapFvRn rnMatch ms         `thenRn` \ (new_ms, ms_fvs) ->
+    rnExpr expr                                `thenRn` \ (new_expr, e_fvs) ->
+    mapFvRn (rnMatch CaseAlt) ms       `thenRn` \ (new_ms, ms_fvs) ->
     returnRn (HsCase new_expr new_ms src_loc, e_fvs `plusFV` ms_fvs)
 
 rnExpr (HsLet binds expr)
@@ -378,8 +390,8 @@ rnExpr e@(HsDo do_or_lc stmts src_loc)
     rnStmts stmts                      `thenRn` \ ((_, stmts'), fvs) ->
        -- check the statement list ends in an expression
     case last stmts' of {
-       ExprStmt _ _ -> returnRn () ;
-       _            -> addErrRn (doStmtListErr e)
+       ResultStmt _ _ -> returnRn () ;
+       _              -> addErrRn (doStmtListErr e)
     }                                  `thenRn_`
     returnRn (HsDo do_or_lc stmts' src_loc, fvs `plusFV` implicit_fvs)
   where
@@ -575,15 +587,14 @@ rnStmt (ParStmt stmtss) thing_inside
 rnStmt (BindStmt pat expr src_loc) thing_inside
   = pushSrcLocRn src_loc $
     rnExpr expr                                        `thenRn` \ (expr', fv_expr) ->
-    bindLocalsFVRn doc binders                 $ \ new_binders ->
+    bindPatSigTyVars (collectSigTysFromPat pat)        $ \ sig_tyvars ->
+    bindLocalsFVRn doc (collectPatBinders pat) $ \ new_binders ->
     rnPat pat                                  `thenRn` \ (pat', fv_pat) ->
     thing_inside (BindStmt pat' expr' src_loc) `thenRn` \ ((rest_binders, result), fvs) ->
-    -- ZZ is shadowing handled correctly?
     returnRn ((new_binders ++ rest_binders, result),
              fv_expr `plusFV` fvs `plusFV` fv_pat)
   where
-    binders = collectPatBinders pat
-    doc = text "a pattern in do binding" 
+    doc = text "In a pattern in 'do' binding" 
 
 rnStmt (ExprStmt expr src_loc) thing_inside
   = pushSrcLocRn src_loc $
@@ -591,6 +602,12 @@ rnStmt (ExprStmt expr src_loc) thing_inside
     thing_inside (ExprStmt expr' src_loc)      `thenRn` \ (result, fvs) ->
     returnRn (result, fv_expr `plusFV` fvs)
 
+rnStmt (ResultStmt expr src_loc) thing_inside
+  = pushSrcLocRn src_loc $
+    rnExpr expr                                `thenRn` \ (expr', fv_expr) ->
+    thing_inside (ResultStmt expr' src_loc)    `thenRn` \ (result, fvs) ->
+    returnRn (result, fv_expr `plusFV` fvs)
+
 rnStmt (LetStmt binds) thing_inside
   = rnBinds binds                              $ \ binds' ->
     let new_binders = collectHsBinders binds' in
@@ -860,9 +877,7 @@ mkAssertExpr =
      vname = mkSysLocalName uniq SLIT("v")
      expr  = HsLam ignorePredMatch
      loc   = nameSrcLoc vname
-     ignorePredMatch = Match [] [WildPatIn, VarPatIn vname] Nothing 
-                             (GRHSs [GRHS [ExprStmt (HsVar vname) loc] loc]
-                                   EmptyBinds Nothing)
+     ignorePredMatch = mkSimpleMatch [WildPatIn, VarPatIn vname] (HsVar vname) Nothing loc
     in
     returnRn (expr, unitFV name)
   else
@@ -901,7 +916,7 @@ precParseErr op1 op2
 sectionPrecErr op arg_op section
  = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
         nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
-        nest 4 (ptext SLIT("In the section:") <+> quotes (ppr section))]
+        nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
 
 nonStdGuardErr guard
   = hang (ptext