[project @ 2005-08-11 08:22:11 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnSource.lhs
index e173907..337b3d2 100644 (file)
@@ -7,43 +7,39 @@
 module RnSource ( 
        rnSrcDecls, addTcgDUs, 
        rnTyClDecls, checkModDeprec,
-       rnBindGroups, rnBindGroupsAndThen, rnSplice
+       rnSplice, checkTH
     ) where
 
 #include "HsVersions.h"
 
+import {-# SOURCE #-} RnExpr( rnLExpr )
+
 import HsSyn
-import RdrName         ( RdrName, isRdrDataCon, rdrNameOcc, elemLocalRdrEnv )
+import RdrName         ( RdrName, isRdrDataCon, elemLocalRdrEnv )
 import RdrHsSyn                ( extractGenericPatTyVars )
 import RnHsSyn
-import RnExpr          ( rnLExpr, checkTH )
 import RnTypes         ( rnLHsType, rnLHsTypes, rnHsSigType, rnHsTypeFVs, rnContext )
-import RnBinds         ( rnTopBinds, rnBinds, rnMethodBinds, 
-                         rnBindsAndThen, renameSigs, checkSigs )
-import RnEnv           ( lookupTopBndrRn, lookupTopFixSigNames,
+import RnBinds         ( rnTopBinds, rnMethodBinds, renameSigs )
+import RnEnv           ( lookupLocalDataTcNames,
                          lookupLocatedTopBndrRn, lookupLocatedOccRn,
                          lookupOccRn, newLocalsRn, 
                          bindLocatedLocalsFV, bindPatSigTyVarsFV,
                          bindTyVarsRn, extendTyVarEnvFVRn,
-                         bindLocalNames, newIPNameRn,
-                         checkDupNames, mapFvRn,
-                         unknownNameErr
+                         bindLocalNames, checkDupNames, mapFvRn
                        )
 import TcRnMonad
 
-import BasicTypes      ( TopLevelFlag(..)  )
 import HscTypes                ( FixityEnv, FixItem(..),
                          Deprecations, Deprecs(..), DeprecTxt, plusDeprecs )
 import Class           ( FunDep )
-import Name            ( Name )
+import Name            ( Name, nameOccName )
 import NameSet
 import NameEnv
 import Outputable
-import SrcLoc          ( Located(..), unLoc, getLoc )
-import CmdLineOpts     ( DynFlag(..) )
-                               -- Warn of unused for-all'd tyvars
+import SrcLoc          ( Located(..), unLoc, getLoc, noLoc )
+import DynFlags        ( DynFlag(..) )
 import Maybes          ( seqMaybe )
-import Maybe            ( catMaybes, isNothing )
+import Maybe            ( isNothing )
 \end{code}
 
 @rnSourceDecl@ `renames' declarations.
@@ -65,7 +61,7 @@ Checks the @(..)@ etc constraints in the export list.
 \begin{code}
 rnSrcDecls :: HsGroup RdrName -> RnM (TcGblEnv, HsGroup Name)
 
-rnSrcDecls (HsGroup { hs_valds  = [HsBindGroup binds sigs _],
+rnSrcDecls (HsGroup { hs_valds  = val_decls,
                      hs_tyclds = tycl_decls,
                      hs_instds = inst_decls,
                      hs_fixds  = fix_decls,
@@ -87,7 +83,7 @@ rnSrcDecls (HsGroup { hs_valds  = [HsBindGroup binds sigs _],
 
                -- Rename other declarations
        traceRn (text "Start rnmono") ;
-       (rn_val_decls, bind_dus) <- rnTopBinds binds sigs ;
+       (rn_val_decls, bind_dus) <- rnTopBinds val_decls ;
        traceRn (text "finish rnmono" <+> ppr rn_val_decls) ;
 
                -- You might think that we could build proper def/use information
@@ -121,9 +117,14 @@ rnSrcDecls (HsGroup { hs_valds  = [HsBindGroup binds sigs _],
           other_fvs = plusFVs [src_fvs1, src_fvs2, src_fvs3, 
                                src_fvs4, src_fvs5] ;
           src_dus = bind_dus `plusDU` usesOnly other_fvs 
+               -- Note: src_dus will contain *uses* for locally-defined types
+               -- and classes, but no *defs* for them.  (Because rnTyClDecl 
+               -- returns only the uses.)  This is a little 
+               -- surprising but it doesn't actually matter at all.
        } ;
 
        traceRn (text "finish rnSrc" <+> ppr rn_group) ;
+       traceRn (text "finish Dus" <+> ppr src_dus ) ;
        tcg_env <- getGblEnv ;
        return (tcg_env `addTcgDUs` src_dus, rn_group)
     }}}
@@ -155,17 +156,13 @@ rnSrcFixityDecls fix_decls
 
 rnFixityDecl :: FixityEnv -> LFixitySig RdrName -> RnM FixityEnv
 rnFixityDecl fix_env (L loc (FixitySig rdr_name fixity))
-  = addSrcSpan loc $
+  = setSrcSpan loc $
         -- GHC extension: look up both the tycon and data con 
        -- for con-like things
        -- If neither are in scope, report an error; otherwise
        -- add both to the fixity env
-     addLocM lookupTopFixSigNames rdr_name     `thenM` \ names ->
-     if null names then
-         addLocErr rdr_name unknownNameErr     `thenM_`
-         returnM fix_env
-     else
-         foldlM add fix_env names
+     addLocM lookupLocalDataTcNames rdr_name   `thenM` \ names ->
+     foldlM add fix_env names
   where
     add fix_env name
       = case lookupNameEnv fix_env name of
@@ -174,8 +171,7 @@ rnFixityDecl fix_env (L loc (FixitySig rdr_name fixity))
                     returnM fix_env
          Nothing -> returnM (extendNameEnv fix_env name fix_item)
       where
-       fix_item = FixItem (rdrNameOcc (unLoc rdr_name)) fixity
-                        (getLoc rdr_name)
+       fix_item = FixItem (nameOccName name) fixity (getLoc rdr_name)
 
 pprFixEnv :: FixityEnv -> SDoc
 pprFixEnv env 
@@ -205,12 +201,12 @@ rnSrcDeprecDecls []
   = returnM NoDeprecs
 
 rnSrcDeprecDecls decls
-  = mappM (addLocM rn_deprec) decls    `thenM` \ pairs ->
-    returnM (DeprecSome (mkNameEnv (catMaybes pairs)))
+  = mappM (addLocM rn_deprec) decls    `thenM` \ pairs_s ->
+    returnM (DeprecSome (mkNameEnv (concat pairs_s)))
  where
    rn_deprec (Deprecation rdr_name txt)
-     = lookupTopBndrRn rdr_name        `thenM` \ name ->
-       returnM (Just (name, (rdrNameOcc rdr_name, txt)))
+     = lookupLocalDataTcNames rdr_name `thenM` \ names ->
+       returnM [(name, (nameOccName name, txt)) | name <- names]
 
 checkModDeprec :: Maybe DeprecTxt -> Deprecations
 -- Check for a module deprecation; done once at top level
@@ -234,63 +230,6 @@ rnDefaultDecl (DefaultDecl tys)
 
 %*********************************************************
 %*                                                     *
-               Bindings
-%*                                                     *
-%*********************************************************
-
-These chaps are here, rather than in TcBinds, so that there
-is just one hi-boot file (for RnSource).  rnSrcDecls is part
-of the loop too, and it must be defined in this module.
-
-\begin{code}
-rnBindGroups :: [HsBindGroup RdrName] -> RnM ([HsBindGroup Name], DefUses)
--- This version assumes that the binders are already in scope
--- It's used only in 'mdo'
-rnBindGroups []
-   = returnM ([], emptyDUs)
-rnBindGroups [HsBindGroup bind sigs _]
-   = rnBinds NotTopLevel bind sigs
-rnBindGroups b@[HsIPBinds bind]
-   = do addErr (badIpBinds b)  
-       returnM ([], emptyDUs)
-rnBindGroups _
-   = panic "rnBindGroups"
-
-rnBindGroupsAndThen 
-  :: [HsBindGroup RdrName]
-  -> ([HsBindGroup Name] -> RnM (result, FreeVars))
-  -> RnM (result, FreeVars)
--- This version (a) assumes that the binding vars are not already in scope
---             (b) removes the binders from the free vars of the thing inside
--- The parser doesn't produce ThenBinds
-rnBindGroupsAndThen [] thing_inside
-  = thing_inside []
-rnBindGroupsAndThen [HsBindGroup bind sigs _] thing_inside
-  = rnBindsAndThen bind sigs $ \ groups -> thing_inside groups
-rnBindGroupsAndThen [HsIPBinds binds] thing_inside
-  = rnIPBinds binds                    `thenM` \ (binds',fv_binds) ->
-    thing_inside [HsIPBinds binds']    `thenM` \ (thing, fvs_thing) ->
-    returnM (thing, fvs_thing `plusFV` fv_binds)
-
-rnIPBinds [] = returnM ([], emptyFVs)
-rnIPBinds (bind : binds)
-  = wrapLocFstM rnIPBind bind  `thenM` \ (bind', fvBind) ->
-    rnIPBinds binds            `thenM` \ (binds',fvBinds) ->
-    returnM (bind' : binds', fvBind `plusFV` fvBinds)
-
-rnIPBind (IPBind n expr)
-  = newIPNameRn  n             `thenM` \ name ->
-    rnLExpr expr               `thenM` \ (expr',fvExpr) ->
-    return (IPBind name expr', fvExpr)
-
-badIpBinds binds
-  = hang (ptext SLIT("Implicit-parameter bindings illegal in 'mdo':")) 4
-        (ppr binds)
-\end{code}
-
-
-%*********************************************************
-%*                                                     *
 \subsection{Foreign declarations}
 %*                                                     *
 %*********************************************************
@@ -347,9 +286,9 @@ rnSrcInstDecl (InstDecl inst_ty mbinds uprags)
        -- But the (unqualified) method names are in scope
     let 
        binders = collectHsBindBinders mbinds'
+       ok_sig  = okInstDclSig (mkNameSet binders)
     in
-    bindLocalNames binders (renameSigs uprags)                 `thenM` \ uprags' ->
-    checkSigs (okInstDclSig (mkNameSet binders)) uprags'       `thenM_`
+    bindLocalNames binders (renameSigs ok_sig uprags)  `thenM` \ uprags' ->
 
     returnM (InstDecl inst_ty' mbinds' uprags',
             meth_fvs `plusFV` hsSigsFVs uprags'
@@ -486,24 +425,52 @@ rnTyClDecl (ForeignType {tcdLName = name, tcdFoType = fo_type, tcdExtName = ext_
             emptyFVs)
 
 rnTyClDecl (TyData {tcdND = new_or_data, tcdCtxt = context, tcdLName = tycon,
-                      tcdTyVars = tyvars, tcdCons = condecls, 
-                      tcdDerivs = derivs})
-  = lookupLocatedTopBndrRn tycon               `thenM` \ tycon' ->
+                   tcdTyVars = tyvars, tcdCons = condecls, 
+                   tcdKindSig = sig, tcdDerivs = derivs})
+  | is_vanilla -- Normal Haskell data type decl
+  = ASSERT( isNothing sig )    -- In normal H98 form, kind signature on the 
+                               -- data type is syntactically illegal
     bindTyVarsRn data_doc tyvars               $ \ tyvars' ->
-    rnContext data_doc context                         `thenM` \ context' ->
-    rn_derivs derivs                           `thenM` \ (derivs', deriv_fvs) ->
-    checkDupNames data_doc con_names   `thenM_`
-    rnConDecls (unLoc tycon') condecls `thenM` \ condecls' ->
-    returnM (TyData {tcdND = new_or_data, tcdCtxt = context', tcdLName = tycon',
-                    tcdTyVars = tyvars', tcdCons = condecls', 
-                    tcdDerivs = derivs'}, 
-            delFVs (map hsLTyVarName tyvars')  $
-            extractHsCtxtTyNames context'      `plusFV`
-            plusFVs (map conDeclFVs condecls') `plusFV`
-            deriv_fvs)
+    do { tycon' <- lookupLocatedTopBndrRn tycon
+       ; context' <- rnContext data_doc context
+       ; (derivs', deriv_fvs) <- rn_derivs derivs
+       ; checkDupNames data_doc con_names
+       ; condecls' <- rnConDecls (unLoc tycon') condecls
+       ; returnM (TyData {tcdND = new_or_data, tcdCtxt = context', tcdLName = tycon',
+                          tcdTyVars = tyvars', tcdKindSig = Nothing, tcdCons = condecls', 
+                          tcdDerivs = derivs'}, 
+                  delFVs (map hsLTyVarName tyvars')    $
+                  extractHsCtxtTyNames context'        `plusFV`
+                  plusFVs (map conDeclFVs condecls') `plusFV`
+                  deriv_fvs) }
+
+  | otherwise  -- GADT
+  = ASSERT( null (unLoc context) )
+    do { tycon' <- lookupLocatedTopBndrRn tycon
+       ; tyvars' <- bindTyVarsRn data_doc tyvars 
+                                 (\ tyvars' -> return tyvars')
+               -- For GADTs, the type variables in the declaration 
+               -- do not scope over the constructor signatures
+               --      data T a where { T1 :: forall b. b-> b }
+       ; (derivs', deriv_fvs) <- rn_derivs derivs
+       ; checkDupNames data_doc con_names
+       ; condecls' <- rnConDecls (unLoc tycon') condecls
+       ; returnM (TyData {tcdND = new_or_data, tcdCtxt = noLoc [], tcdLName = tycon',
+                          tcdTyVars = tyvars', tcdCons = condecls', tcdKindSig = sig,
+                          tcdDerivs = derivs'}, 
+                  plusFVs (map conDeclFVs condecls') `plusFV` deriv_fvs) }
+
   where
+    is_vanilla = case condecls of      -- Yuk
+                    []                    -> True
+                    L _ (ConDecl {}) : _  -> True
+                    other                 -> False
+
     data_doc = text "In the data type declaration for" <+> quotes (ppr tycon)
-    con_names = [ n | L _ (ConDecl n _ _ _) <- condecls ]
+    con_names = map con_names_helper condecls
+
+    con_names_helper (L _ (ConDecl n _ _ _)) = n
+    con_names_helper (L _ (GadtDecl n _)) = n
 
     rn_derivs Nothing   = returnM (Nothing, emptyFVs)
     rn_derivs (Just ds) = rnLHsTypes data_doc ds       `thenM` \ ds' -> 
@@ -528,7 +495,7 @@ rnTyClDecl (ClassDecl {tcdCtxt = context, tcdLName = cname,
     bindTyVarsRn cls_doc tyvars                        ( \ tyvars' ->
        rnContext cls_doc context       `thenM` \ context' ->
        rnFds cls_doc fds               `thenM` \ fds' ->
-       renameSigs sigs                 `thenM` \ sigs' ->
+       renameSigs okClsDclSig sigs     `thenM` \ sigs' ->
        returnM   (tyvars', context', fds', sigs')
     )  `thenM` \ (tyvars', context', fds', sigs') ->
 
@@ -538,7 +505,6 @@ rnTyClDecl (ClassDecl {tcdCtxt = context, tcdLName = cname,
        sig_rdr_names_w_locs   = [op | L _ (Sig op _) <- sigs]
     in
     checkDupNames sig_doc sig_rdr_names_w_locs `thenM_` 
-    checkSigs okClsDclSig sigs'                                `thenM_`
        -- Typechecker is responsible for checking that we only
        -- give default-method bindings for things in this class.
        -- The renamer *could* check this for class decls, but can't
@@ -587,14 +553,7 @@ rnTyClDecl (ClassDecl {tcdCtxt = context, tcdLName = cname,
 \begin{code}
 rnConDecls :: Name -> [LConDecl RdrName] -> RnM [LConDecl Name]
 rnConDecls tycon condecls
-  =    -- Check that there's at least one condecl,
-       -- or else we're reading an interface file, or -fglasgow-exts
-    (if null condecls then
-       doptM Opt_GlasgowExts   `thenM` \ glaExts ->
-       checkErr glaExts (emptyConDeclsErr tycon)
-     else returnM ()
-    )                                          `thenM_` 
-    mappM (wrapLocM rnConDecl) condecls
+  = mappM (wrapLocM rnConDecl) condecls
 
 rnConDecl :: ConDecl RdrName -> RnM (ConDecl Name)
 rnConDecl (ConDecl name tvs cxt details)
@@ -608,13 +567,21 @@ rnConDecl (ConDecl name tvs cxt details)
   where
     doc = text "In the definition of data constructor" <+> quotes (ppr 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)
+  where
+    doc = text "In the definition of data constructor" <+> quotes (ppr name)
+
 rnConDetails doc (PrefixCon tys)
-  = mappM (rnLBangTy doc) tys  `thenM` \ new_tys  ->
+  = mappM (rnLHsType doc) tys  `thenM` \ new_tys  ->
     returnM (PrefixCon new_tys)
 
 rnConDetails doc (InfixCon ty1 ty2)
-  = rnLBangTy doc ty1                  `thenM` \ new_ty1 ->
-    rnLBangTy doc ty2                  `thenM` \ new_ty2 ->
+  = rnLHsType doc ty1                  `thenM` \ new_ty1 ->
+    rnLHsType doc ty2                  `thenM` \ new_ty2 ->
     returnM (InfixCon new_ty1 new_ty2)
 
 rnConDetails doc (RecCon fields)
@@ -626,15 +593,9 @@ rnConDetails doc (RecCon fields)
 
 rnField doc (name, ty)
   = lookupLocatedTopBndrRn name        `thenM` \ new_name ->
-    rnLBangTy doc ty           `thenM` \ new_ty ->
+    rnLHsType doc ty           `thenM` \ new_ty ->
     returnM (new_name, new_ty) 
 
-rnLBangTy doc = wrapLocM (rnBangTy doc)
-
-rnBangTy doc (BangType s ty)
-  = rnLHsType doc ty           `thenM` \ new_ty ->
-    returnM (BangType s new_ty)
-
 -- This data decl will parse OK
 --     data T = a Int
 -- treating "a" as the constructor.
@@ -649,10 +610,6 @@ checkConName name = checkErr (isRdrDataCon name) (badDataCon name)
 
 badDataCon name
    = hsep [ptext SLIT("Illegal data constructor name"), quotes (ppr name)]
-
-emptyConDeclsErr tycon
-  = sep [quotes (ppr tycon) <+> ptext SLIT("has no constructors"),
-        nest 4 (ptext SLIT("(-fglasgow-exts permits this)"))]
 \end{code}
 
 
@@ -692,4 +649,13 @@ rnSplice (HsSplice n expr)
     newLocalsRn [L loc n]      `thenM` \ [n'] ->
     rnLExpr expr               `thenM` \ (expr', fvs) ->
     returnM (HsSplice n' expr', fvs)
-\end{code}
\ No newline at end of file
+
+#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   
+\end{code}