[project @ 2003-03-27 08:16:29 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnExpr.lhs
index 36bbc4b..5e18d67 100644 (file)
@@ -28,8 +28,9 @@ import RdrHsSyn
 import RnHsSyn
 import TcRnMonad
 import RnEnv
+import RnNames         ( importsFromLocalDecls )
 import RnTypes         ( rnHsTypeFVs, rnPat, litFVs, rnOverLit, rnPatsAndThen,
-                         dupFieldErr, precParseErr, sectionPrecErr, patSigErr )
+                         dupFieldErr, precParseErr, sectionPrecErr, patSigErr, checkTupSize )
 import CmdLineOpts     ( DynFlag(..), opt_IgnoreAsserts )
 import BasicTypes      ( Fixity(..), FixityDirection(..), IPName(..),
                          defaultFixity, negateFixity, compareFixity )
@@ -41,8 +42,7 @@ import PrelNames      ( hasKey, assertIdKey,
                          replicatePName, mapPName, filterPName,
                          crossPName, zipPName, toPName,
                          enumFromToPName, enumFromThenToPName, assertErrorName,
-                         negateName, qTyConName, monadNames, mfixName )
-import RdrName         ( RdrName )
+                         negateName, monadNames, mfixName )
 import Name            ( Name, nameOccName )
 import NameSet
 import UnicodeUtil     ( stringToUtf8 )
@@ -69,7 +69,7 @@ rnMatch ctxt match@(Match pats maybe_rhs_sig grhss)
   = addSrcLoc (getMatchLoc match)      $
 
        -- Deal with the rhs type signature
-    bindPatSigTyVars rhs_sig_tys       $ 
+    bindPatSigTyVarsFV rhs_sig_tys     $ 
     doptM Opt_GlasgowExts              `thenM` \ opt_GlasgowExts ->
     (case maybe_rhs_sig of
        Nothing -> returnM (Nothing, emptyFVs)
@@ -84,7 +84,7 @@ rnMatch ctxt match@(Match pats maybe_rhs_sig grhss)
     rnGRHSs ctxt grhss         `thenM` \ (grhss', grhss_fvs) ->
 
     returnM (Match pats' maybe_rhs_sig' grhss', grhss_fvs `plusFV` ty_fvs)
-       -- The bindPatSigTyVars and rnPatsAndThen will remove the bound FVs
+       -- The bindPatSigTyVarsFV and rnPatsAndThen will remove the bound FVs
   where
      rhs_sig_tys =  case maybe_rhs_sig of
                        Nothing -> []
@@ -224,19 +224,26 @@ rnExpr (HsPar e)
     returnM (HsPar e', fvs_e)
 
 -- Template Haskell extensions
-rnExpr (HsBracket br_body)
-  = checkGHCI (thErr "bracket")                `thenM_`
-    rnBracket br_body                  `thenM` \ (body', fvs_e) ->
-    returnM (HsBracket body', fvs_e `addOneFV` qTyConName)
-       -- We use the Q tycon as a proxy to haul in all the smart
-       -- constructors; see the hack in RnIfaces
-
-rnExpr (HsSplice n e)
-  = checkGHCI (thErr "splice")         `thenM_`
-    getSrcLocM                         `thenM` \ loc -> 
-    newLocalsRn [(n,loc)]              `thenM` \ [n'] ->
-    rnExpr e                           `thenM` \ (e', fvs_e) ->
-    returnM (HsSplice n' e', fvs_e)    
+-- Don't ifdef-GHCI them because we want to fail gracefully
+-- (not with an rnExpr crash) in a stage-1 compiler.
+rnExpr e@(HsBracket br_body loc)
+  = addSrcLoc loc              $
+    checkTH e "bracket"                `thenM_`
+    rnBracket br_body          `thenM` \ (body', fvs_e) ->
+    returnM (HsBracket body' loc, fvs_e `plusFV` thProxyName)
+
+rnExpr e@(HsSplice n splice loc)
+  = addSrcLoc loc              $
+    checkTH e "splice"         `thenM_`
+    newLocalsRn [(n,loc)]      `thenM` \ [n'] ->
+    rnExpr splice              `thenM` \ (splice', fvs_e) ->
+    returnM (HsSplice n' splice' loc, fvs_e `plusFV` thProxyName)
+
+rnExpr e@(HsReify (Reify flavour name))
+  = checkTH e "reify"          `thenM_`
+    lookupGlobalOccRn name     `thenM` \ name' ->
+       -- For now, we can only reify top-level things
+    returnM (HsReify (Reify flavour name'), unitFV name' `plusFV` thProxyName)
 
 rnExpr section@(SectionL expr op)
   = rnExpr expr                                        `thenM` \ (expr', fvs_expr) ->
@@ -258,6 +265,10 @@ rnExpr (HsCCall fun args may_gc is_casm _)
                                       cReturnableClassName, 
                                       ioDataConName])
 
+rnExpr (HsCoreAnn ann expr)
+  = rnExpr expr `thenM` \ (expr', fvs_expr) ->
+    returnM (HsCoreAnn ann expr', fvs_expr)
+
 rnExpr (HsSCC lbl expr)
   = rnExpr expr                `thenM` \ (expr', fvs_expr) ->
     returnM (HsSCC lbl expr', fvs_expr)
@@ -273,12 +284,6 @@ rnExpr (HsLet binds expr)
     rnExpr expr                         `thenM` \ (expr',fvExpr) ->
     returnM (HsLet binds' expr', fvExpr)
 
-rnExpr (HsWith expr binds is_with)
-  = warnIf is_with withWarning `thenM_`
-    rnExpr expr                        `thenM` \ (expr',fvExpr) ->
-    rnIPBinds binds            `thenM` \ (binds',fvBinds) ->
-    returnM (HsWith expr' binds' is_with, fvExpr `plusFV` fvBinds)
-
 rnExpr e@(HsDo do_or_lc stmts _ _ src_loc)
   = addSrcLoc src_loc $
     rnStmts do_or_lc stmts             `thenM` \ (stmts', fvs) ->
@@ -314,11 +319,13 @@ rnExpr (ExplicitPArr _ exps)
     returnM  (ExplicitPArr placeHolderType exps', 
               fvs `addOneFV` toPName `addOneFV` parrTyCon_name)
 
-rnExpr (ExplicitTuple exps boxity)
-  = rnExprs exps                               `thenM` \ (exps', fvs) ->
+rnExpr e@(ExplicitTuple exps boxity)
+  = checkTupSize tup_size                      `thenM_`
+    rnExprs exps                               `thenM` \ (exps', fvs) ->
     returnM (ExplicitTuple exps' boxity, fvs `addOneFV` tycon_name)
   where
-    tycon_name = tupleTyCon_name boxity (length exps)
+    tup_size   = length exps
+    tycon_name = tupleTyCon_name boxity tup_size
 
 rnExpr (RecordCon con_id rbinds)
   = lookupOccRn con_id                         `thenM` \ conname ->
@@ -429,22 +436,6 @@ rnRbinds str rbinds
 
 %************************************************************************
 %*                                                                     *
-\subsubsection{@rnIPBinds@s: in implicit parameter bindings}           *
-%*                                                                     *
-%************************************************************************
-
-\begin{code}
-rnIPBinds [] = returnM ([], emptyFVs)
-rnIPBinds ((n, expr) : binds)
-  = newIPName n                        `thenM` \ name ->
-    rnExpr expr                        `thenM` \ (expr',fvExpr) ->
-    rnIPBinds binds            `thenM` \ (binds',fvBinds) ->
-    returnM ((name, expr') : binds', fvExpr `plusFV` fvBinds)
-
-\end{code}
-
-%************************************************************************
-%*                                                                     *
        Template Haskell brackets
 %*                                                                     *
 %************************************************************************
@@ -458,10 +449,16 @@ rnBracket (TypBr t) = rnHsTypeFVs doc t   `thenM` \ (t', fvs) ->
                      returnM (TypBr t', fvs)
                    where
                      doc = ptext SLIT("In a Template-Haskell quoted type")
-rnBracket (DecBr ds) = rnSrcDecls ds   `thenM` \ (tcg_env, ds', fvs) ->
-                       -- Discard the tcg_env; it contains the extended global RdrEnv
-                       -- because there is no scope that these decls cover (yet!)
-                      returnM (DecBr ds', fvs)
+rnBracket (DecBr group) 
+  = importsFromLocalDecls group `thenM` \ (rdr_env, avails) ->
+       -- Discard avails (not useful here)
+
+    updGblEnv (\gbl -> gbl { tcg_rdr_env = rdr_env `plusGlobalRdrEnv` tcg_rdr_env gbl }) $
+
+    rnSrcDecls group   `thenM` \ (tcg_env, group', dus) ->
+       -- Discard the tcg_env; it contains only extra info about fixity
+
+    returnM (DecBr group', duUses dus `minusNameSet` duDefs dus)
 \end{code}
 
 %************************************************************************
@@ -507,12 +504,20 @@ rnNormalStmts ctxt (BindStmt pat expr src_loc : stmts)
                                        -- the rnPatsAndThen, but it does not matter
 
 rnNormalStmts ctxt (LetStmt binds : stmts)
-  = rnBindsAndThen binds               $ \ binds' ->
-    rnNormalStmts ctxt stmts           `thenM` \ (stmts', fvs) ->
-    returnM (LetStmt binds' : stmts', fvs)
+  = checkErr (ok ctxt binds) (badIpBinds binds)        `thenM_`
+    rnBindsAndThen binds                       ( \ binds' ->
+    rnNormalStmts ctxt stmts                   `thenM` \ (stmts', fvs) ->
+    returnM (LetStmt binds' : stmts', fvs))
+  where
+       -- We do not allow implicit-parameter bindings in a parallel
+       -- list comprehension.  I'm not sure what it might mean.
+    ok (ParStmtCtxt _) (IPBinds _ _) = False   
+    ok _              _             = True
 
 rnNormalStmts ctxt (ParStmt stmtss : stmts)
-  = mapFvRn (rnNormalStmts ctxt) stmtss        `thenM` \ (stmtss', fv_stmtss) ->
+  = doptM Opt_GlasgowExts              `thenM` \ opt_GlasgowExts ->
+    checkM opt_GlasgowExts parStmtErr  `thenM_`
+    mapFvRn (rnNormalStmts (ParStmtCtxt ctxt)) stmtss  `thenM` \ (stmtss', fv_stmtss) ->
     let
        bndrss = map collectStmtsBinders stmtss'
     in
@@ -546,8 +551,6 @@ rnNormalStmts ctxt stmts = pprPanic "rnNormalStmts" (ppr stmts)
 %************************************************************************
 
 \begin{code}
-type Defs    = NameSet
-type Uses    = NameSet -- Same as FreeVars really
 type FwdRefs = NameSet
 type Segment = (Defs,
                Uses,           -- May include defs
@@ -617,9 +620,9 @@ rn_mdo_stmt (BindStmt pat expr src_loc)
             [BindStmt pat' expr' src_loc])
 
 rn_mdo_stmt (LetStmt binds)
-  = rnBinds binds              `thenM` \ (binds', fv_binds) ->
-    returnM (mkNameSet (collectHsBinders binds'), 
-            fv_binds, emptyNameSet, [LetStmt binds'])
+  = rnBinds binds              `thenM` \ (binds', du_binds) ->
+    returnM (duDefs du_binds, duUses du_binds, 
+            emptyNameSet, [LetStmt binds'])
 
 rn_mdo_stmt stmt@(ParStmt _)   -- Syntactically illegal in mdo
   = pprPanic "rn_mdo_stmt" (ppr stmt)
@@ -713,7 +716,7 @@ segsToStmts ((defs, uses, fwds, ss) : segs)
   where
     (later_stmts, later_uses) = segsToStmts segs
     new_stmt | non_rec  = head ss
-            | otherwise = RecStmt rec_names ss
+            | otherwise = RecStmt rec_names ss []
             where
               non_rec   = isSingleton ss && isEmptyNameSet fwds
               rec_names = nameSetToList (fwds `plusFV` (defs `intersectNameSet` later_uses))
@@ -911,14 +914,18 @@ doStmtListErr do_or_lc e
                        MDoExpr -> "mdo"
                        other   -> "do"
 
-thErr what
-  = ptext SLIT("Template Haskell") <+> text what <+>  
-    ptext SLIT("illegal in a stage-1 compiler") 
+#ifdef GHCI 
+checkTH e what = returnM ()    -- OK
+#else
+checkTH e what         -- Raise an error in a stage-1 compiler
+  = addErr (vcat [ptext SLIT("Template Haskell") <+> text what <+>  
+                 ptext SLIT("illegal in a stage-1 compiler"),
+                 nest 2 (ppr e)])
+#endif   
 
+parStmtErr = addErr (ptext SLIT("Illegal parallel list comprehension: use -fglagow-exts"))
 
-withWarning
-  = sep [quotes (ptext SLIT("with")),
-        ptext SLIT("is deprecated, use"),
-        quotes (ptext SLIT("let")),
-        ptext SLIT("instead")]
+badIpBinds binds
+  = hang (ptext SLIT("Implicit-parameter bindings illegal in a parallel list comprehension:")) 4
+        (ppr binds)
 \end{code}