forkProcess(): watch out for ThreadRelocated
[ghc-hetmet.git] / ghc / compiler / rename / RnSource.lhs
index 337b3d2..9150440 100644 (file)
@@ -15,8 +15,9 @@ module RnSource (
 import {-# SOURCE #-} RnExpr( rnLExpr )
 
 import HsSyn
-import RdrName         ( RdrName, isRdrDataCon, elemLocalRdrEnv )
-import RdrHsSyn                ( extractGenericPatTyVars )
+import RdrName         ( RdrName, isRdrDataCon, elemLocalRdrEnv, globalRdrEnvElts,
+                         GlobalRdrElt(..), isLocalGRE )
+import RdrHsSyn                ( extractGenericPatTyVars, extractHsRhoRdrTyVars )
 import RnHsSyn
 import RnTypes         ( rnLHsType, rnLHsTypes, rnHsSigType, rnHsTypeFVs, rnContext )
 import RnBinds         ( rnTopBinds, rnMethodBinds, renameSigs )
@@ -35,11 +36,13 @@ import Class                ( FunDep )
 import Name            ( Name, nameOccName )
 import NameSet
 import NameEnv
+import OccName         ( occEnvElts )
 import Outputable
 import SrcLoc          ( Located(..), unLoc, getLoc, noLoc )
 import DynFlags        ( DynFlag(..) )
 import Maybes          ( seqMaybe )
 import Maybe            ( isNothing )
+import BasicTypes       ( Boxity(..) )
 \end{code}
 
 @rnSourceDecl@ `renames' declarations.
@@ -77,7 +80,8 @@ rnSrcDecls (HsGroup { hs_valds  = val_decls,
 
                -- Deal with top-level fixity decls 
                -- (returns the total new fixity env)
-       fix_env <- rnSrcFixityDecls fix_decls ;
+       fix_env <- rnSrcFixityDeclsEnv fix_decls ;
+        rn_fix_decls <- rnSrcFixityDecls fix_decls ;
        updGblEnv (\gbl -> gbl { tcg_fix_env = fix_env })
                  $ do {
 
@@ -108,7 +112,7 @@ rnSrcDecls (HsGroup { hs_valds  = val_decls,
           rn_group = HsGroup { hs_valds  = rn_val_decls,
                                hs_tyclds = rn_tycl_decls,
                                hs_instds = rn_inst_decls,
-                               hs_fixds  = [],
+                               hs_fixds  = rn_fix_decls,
                                hs_depds  = [],
                                hs_fords  = rn_foreign_decls,
                                hs_defds  = rn_default_decls,
@@ -146,16 +150,27 @@ addTcgDUs tcg_env dus = tcg_env { tcg_dus = tcg_dus tcg_env `plusDU` dus }
 %*********************************************************
 
 \begin{code}
-rnSrcFixityDecls :: [LFixitySig RdrName] -> RnM FixityEnv
+rnSrcFixityDecls :: [LFixitySig RdrName] -> RnM [LFixitySig Name]
 rnSrcFixityDecls fix_decls
+    = do fix_decls <- mapM rnFixityDecl fix_decls
+         return (concat fix_decls)
+
+rnFixityDecl :: LFixitySig RdrName -> RnM [LFixitySig Name]
+rnFixityDecl (L loc (FixitySig (L nameLoc rdr_name) fixity))
+    = do names <- lookupLocalDataTcNames rdr_name
+         return [ L loc (FixitySig (L nameLoc name) fixity)
+                      | name <- names ]
+
+rnSrcFixityDeclsEnv :: [LFixitySig RdrName] -> RnM FixityEnv
+rnSrcFixityDeclsEnv fix_decls
   = getGblEnv                                  `thenM` \ gbl_env ->
-    foldlM rnFixityDecl (tcg_fix_env gbl_env) 
+    foldlM rnFixityDeclEnv (tcg_fix_env gbl_env) 
            fix_decls                                   `thenM` \ fix_env ->
     traceRn (text "fixity env" <+> pprFixEnv fix_env)  `thenM_`
     returnM fix_env
 
-rnFixityDecl :: FixityEnv -> LFixitySig RdrName -> RnM FixityEnv
-rnFixityDecl fix_env (L loc (FixitySig rdr_name fixity))
+rnFixityDeclEnv :: FixityEnv -> LFixitySig RdrName -> RnM FixityEnv
+rnFixityDeclEnv fix_env (L loc (FixitySig rdr_name fixity))
   = setSrcSpan loc $
         -- GHC extension: look up both the tycon and data con 
        -- for con-like things
@@ -315,25 +330,25 @@ extendTyVarEnvForMethodBinds tyvars thing_inside
 %*********************************************************
 
 \begin{code}
-rnHsRuleDecl (HsRule rule_name act vars lhs rhs)
+rnHsRuleDecl (HsRule rule_name act vars lhs fv_lhs rhs fv_rhs)
   = bindPatSigTyVarsFV (collectRuleBndrSigTys vars)    $
 
     bindLocatedLocalsFV doc (map get_var vars)         $ \ ids ->
     mapFvRn rn_var (vars `zip` ids)            `thenM` \ (vars', fv_vars) ->
 
-    rnLExpr lhs                                        `thenM` \ (lhs', fv_lhs) ->
-    rnLExpr rhs                                        `thenM` \ (rhs', fv_rhs) ->
+    rnLExpr lhs                                        `thenM` \ (lhs', fv_lhs') ->
+    rnLExpr rhs                                        `thenM` \ (rhs', fv_rhs') ->
     let
        mb_bad = validRuleLhs ids lhs'
     in
     checkErr (isNothing mb_bad)
             (badRuleLhsErr rule_name lhs' mb_bad)      `thenM_`
     let
-       bad_vars = [var | var <- ids, not (var `elemNameSet` fv_lhs)]
+       bad_vars = [var | var <- ids, not (var `elemNameSet` fv_lhs')]
     in
     mappM (addErr . badRuleVar rule_name) bad_vars     `thenM_`
-    returnM (HsRule rule_name act vars' lhs' rhs',
-            fv_vars `plusFV` fv_lhs `plusFV` fv_rhs)
+    returnM (HsRule rule_name act vars' lhs' fv_lhs' rhs' fv_rhs',
+            fv_vars `plusFV` fv_lhs' `plusFV` fv_rhs')
   where
     doc = text "In the transformation rule" <+> ftext rule_name
   
@@ -445,9 +460,9 @@ rnTyClDecl (TyData {tcdND = new_or_data, tcdCtxt = context, tcdLName = tycon,
                   deriv_fvs) }
 
   | otherwise  -- GADT
-  = ASSERT( null (unLoc context) )
-    do { tycon' <- lookupLocatedTopBndrRn tycon
-       ; tyvars' <- bindTyVarsRn data_doc tyvars 
+  = do { tycon' <- lookupLocatedTopBndrRn tycon
+       ; checkTc (null (unLoc context)) (badGadtStupidTheta tycon)
+       ; tyvars' <- bindTyVarsRn data_doc tyvars 
                                  (\ tyvars' -> return tyvars')
                -- For GADTs, the type variables in the declaration 
                -- do not scope over the constructor signatures
@@ -463,14 +478,13 @@ rnTyClDecl (TyData {tcdND = new_or_data, tcdCtxt = context, tcdLName = tycon,
   where
     is_vanilla = case condecls of      -- Yuk
                     []                    -> True
-                    L _ (ConDecl {}) : _  -> True
+                    L _ (ConDecl { con_res = ResTyH98 }) : _  -> True
                     other                 -> False
 
     data_doc = text "In the data type declaration for" <+> quotes (ppr tycon)
     con_names = map con_names_helper condecls
 
-    con_names_helper (L _ (ConDecl n _ _ _)) = n
-    con_names_helper (L _ (GadtDecl n _)) = n
+    con_names_helper (L _ c) = con_name c
 
     rn_derivs Nothing   = returnM (Nothing, emptyFVs)
     rn_derivs (Just ds) = rnLHsTypes data_doc ds       `thenM` \ ds' -> 
@@ -502,7 +516,7 @@ rnTyClDecl (ClassDecl {tcdCtxt = context, tcdLName = cname,
        -- Check the signatures
        -- First process the class op sigs (op_sigs), then the fixity sigs (non_op_sigs).
     let
-       sig_rdr_names_w_locs   = [op | L _ (Sig op _) <- sigs]
+       sig_rdr_names_w_locs   = [op | L _ (TypeSig op _) <- sigs]
     in
     checkDupNames sig_doc sig_rdr_names_w_locs `thenM_` 
        -- Typechecker is responsible for checking that we only
@@ -542,6 +556,10 @@ rnTyClDecl (ClassDecl {tcdCtxt = context, tcdLName = cname,
     meth_doc = text "In the default-methods for class" <+> ppr cname
     cls_doc  = text "In the declaration for class"     <+> ppr cname
     sig_doc  = text "In the signatures for class"      <+> ppr cname
+
+badGadtStupidTheta tycon
+  = vcat [ptext SLIT("No context is allowed on a GADT-style data declaration"),
+         ptext SLIT("(You can put a context on each contructor, though.)")]
 \end{code}
 
 %*********************************************************
@@ -556,24 +574,40 @@ rnConDecls tycon condecls
   = mappM (wrapLocM rnConDecl) condecls
 
 rnConDecl :: ConDecl RdrName -> RnM (ConDecl Name)
-rnConDecl (ConDecl name tvs cxt details)
-  = addLocM checkConName name          `thenM_` 
-    lookupLocatedTopBndrRn name                `thenM` \ new_name ->
-
-    bindTyVarsRn doc tvs               $ \ new_tyvars ->
-    rnContext doc cxt                  `thenM` \ new_context ->
-    rnConDetails doc details           `thenM` \ new_details -> 
-    returnM (ConDecl new_name new_tyvars new_context new_details)
-  where
-    doc = text "In the definition of data constructor" <+> quotes (ppr name)
+rnConDecl (ConDecl name expl tvs cxt details res_ty)
+  = do { addLocM checkConName name
 
-rnConDecl (GadtDecl name ty) 
-  = addLocM checkConName name          `thenM_` 
-    lookupLocatedTopBndrRn name                `thenM` \ new_name ->
-    rnHsSigType doc ty                  `thenM` \ new_ty ->
-    returnM (GadtDecl new_name new_ty)
+       ; new_name <- lookupLocatedTopBndrRn name
+       ; name_env <- getLocalRdrEnv
+       
+       -- For H98 syntax, the tvs are the existential ones
+       -- For GADT syntax, the tvs are all the quantified tyvars
+       -- Hence the 'filter' in the ResTyH98 case only
+       ; let not_in_scope  = not . (`elemLocalRdrEnv` name_env) . unLoc
+             arg_tys       = hsConArgs details
+             implicit_tvs  = case res_ty of
+                               ResTyH98 -> filter not_in_scope $
+                                               get_rdr_tvs arg_tys
+                               ResTyGADT ty -> get_rdr_tvs (ty : arg_tys)
+             tvs' = case expl of
+                       Explicit -> tvs
+                       Implicit -> userHsTyVarBndrs implicit_tvs
+
+       ; bindTyVarsRn doc tvs' $ \new_tyvars -> do
+       { new_context <- rnContext doc cxt
+        ; new_details <- rnConDetails doc details
+        ; new_res_ty  <- rnConResult doc res_ty
+        ; let rv = ConDecl new_name expl new_tyvars new_context new_details new_res_ty
+        ; traceRn (text "****** - autrijus" <> ppr rv)
+        ; return rv } }
   where
     doc = text "In the definition of data constructor" <+> quotes (ppr name)
+    get_rdr_tvs tys  = extractHsRhoRdrTyVars cxt (noLoc (HsTupleTy Boxed tys))
+
+rnConResult _ ResTyH98 = return ResTyH98
+rnConResult doc (ResTyGADT ty) = do
+    ty' <- rnHsSigType doc ty
+    return $ ResTyGADT ty'
 
 rnConDetails doc (PrefixCon tys)
   = mappM (rnLHsType doc) tys  `thenM` \ new_tys  ->
@@ -641,14 +675,41 @@ rnHsTyvar doc tyvar = lookupOccRn tyvar
 %*                                                     *
 %*********************************************************
 
+Note [Splices]
+~~~~~~~~~~~~~~
+Consider
+       f = ...
+       h = ...$(thing "f")...
+
+The splice can expand into literally anything, so when we do dependency
+analysis we must assume that it might mention 'f'.  So we simply treat
+all locally-defined names as mentioned by any splice.  This is terribly
+brutal, but I don't see what else to do.  For example, it'll mean
+that every locally-defined thing will appear to be used, so no unused-binding
+warnings.  But if we miss the dependency, then we might typecheck 'h' before 'f',
+and that will crash the type checker because 'f' isn't in scope.
+
+Currently, I'm not treating a splice as also mentioning every import,
+which is a bit inconsistent -- but there are a lot of them.  We might
+thereby get some bogus unused-import warnings, but we won't crash the
+type checker.  Not very satisfactory really.
+
 \begin{code}
 rnSplice :: HsSplice RdrName -> RnM (HsSplice Name, FreeVars)
 rnSplice (HsSplice n expr)
-  = checkTH expr "splice"      `thenM_`
-    getSrcSpanM                `thenM` \ loc ->
-    newLocalsRn [L loc n]      `thenM` \ [n'] ->
-    rnLExpr expr               `thenM` \ (expr', fvs) ->
-    returnM (HsSplice n' expr', fvs)
+  = do { checkTH expr "splice"
+       ; loc  <- getSrcSpanM
+       ; [n'] <- newLocalsRn [L loc n]
+       ; (expr', fvs) <- rnLExpr expr
+
+       -- Ugh!  See Note [Splices] above
+       ; lcl_rdr <- getLocalRdrEnv
+       ; gbl_rdr <- getGlobalRdrEnv
+       ; let gbl_names = mkNameSet [gre_name gre | gre <- globalRdrEnvElts gbl_rdr, 
+                                                   isLocalGRE gre]
+             lcl_names = mkNameSet (occEnvElts lcl_rdr)
+
+       ; return (HsSplice n' expr', fvs `plusFV` lcl_names `plusFV` gbl_names) }
 
 #ifdef GHCI 
 checkTH e what = returnM ()    -- OK