[project @ 2002-10-23 14:30:00 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcExpr.lhs
index f6f822b..676a5d2 100644 (file)
@@ -9,16 +9,17 @@ module TcExpr ( tcExpr, tcExpr_id, tcMonoExpr ) where
 #include "HsVersions.h"
 
 #ifdef GHCI    /* Only if bootstrapped */
-import {-# SOURCE #-}  TcSplice( tcSpliceExpr )
-import TcEnv           ( bracketOK, tcMetaTy )
+import {-# SOURCE #-}  TcSplice( tcSpliceExpr, tcBracket )
+import HsSyn           ( HsReify(..), ReifyFlavour(..) )
+import TcType          ( isTauTy )
+import TcEnv           ( bracketOK, tcMetaTy, tcLookupGlobal,
+                         wellStaged, metaLevel )
 import TcSimplify      ( tcSimplifyBracket )
-import PrelNames       ( exprTyConName )
-import HsSyn           ( HsBracket(..) )
+import Name            ( isExternalName )
+import qualified DsMeta
 #endif
 
-import HsSyn           ( HsExpr(..), HsLit(..), ArithSeqInfo(..), 
-                         mkMonoBind, recBindFields
-                       )
+import HsSyn           ( HsExpr(..), HsLit(..), ArithSeqInfo(..), recBindFields )
 import RnHsSyn         ( RenamedHsExpr, RenamedRecordBinds )
 import TcHsSyn         ( TcExpr, TcRecordBinds, hsLitType, mkHsDictApp, mkHsTyApp, mkHsLet )
 import TcRnMonad
@@ -33,8 +34,7 @@ import Inst           ( InstOrigin(..),
                        )
 import TcBinds         ( tcBindsAndThen )
 import TcEnv           ( tcLookupClass, tcLookupGlobal_maybe, tcLookupIdLvl,
-                         tcLookupTyCon, tcLookupDataCon, tcLookupId,
-                         wellStaged, metaLevel
+                         tcLookupTyCon, tcLookupDataCon, tcLookupId
                        )
 import TcMatches       ( tcMatchesCase, tcMatchLambda, tcDoStmts )
 import TcMonoType      ( tcHsSigType, UserTypeCtxt(..) )
@@ -44,7 +44,7 @@ import TcMType                ( tcInstTyVars, tcInstType, newHoleTyVarTy, zapToType,
                          newTyVarTy, newTyVarTys, zonkTcType, readHoleResult )
 import TcType          ( TcType, TcSigmaType, TcRhoType, TyVarDetails(VanillaTv),
                          tcSplitFunTys, tcSplitTyConApp, mkTyVarTys,
-                         isSigmaTy, isTauTy, mkFunTy, mkFunTys,
+                         isSigmaTy, mkFunTy, mkFunTys,
                          mkTyConApp, mkClassPred, tcFunArgTy,
                          tyVarsOfTypes, isLinearPred,
                          liftedTypeKind, openTypeKind, 
@@ -53,10 +53,8 @@ import TcType                ( TcType, TcSigmaType, TcRhoType, TyVarDetails(VanillaTv),
                        )
 import FieldLabel      ( FieldLabel, fieldLabelName, fieldLabelType, fieldLabelTyCon )
 import Id              ( Id, idType, recordSelectorFieldLabel, isRecordSelector, isDataConWrapId_maybe )
-import DataCon         ( dataConFieldLabels, dataConSig, 
-                         dataConStrictMarks
-                       )
-import Name            ( Name, isExternalName )
+import DataCon         ( DataCon, dataConFieldLabels, dataConSig, dataConStrictMarks )
+import Name            ( Name )
 import TyCon           ( TyCon, tyConTyVars, tyConTheta, isAlgTyCon, tyConDataCons )
 import Subst           ( mkTopTyVarSubst, substTheta, substTy )
 import VarSet          ( emptyVarSet, elemVarSet )
@@ -65,7 +63,7 @@ import PrelNames      ( cCallableClassName, cReturnableClassName,
                          enumFromName, enumFromThenName, 
                          enumFromToName, enumFromThenToName,
                          enumFromToPName, enumFromThenToPName,
-                         ioTyConName, liftName
+                         ioTyConName
                        )
 import ListSetOps      ( minusList )
 import CmdLineOpts
@@ -236,11 +234,9 @@ tcMonoExpr in_expr@(OpApp arg1 op fix arg2) res_ty
 \begin{code}
 tcMonoExpr (HsLet binds expr) res_ty
   = tcBindsAndThen
-       combiner
+       HsLet
        binds                   -- Bindings to check
        (tcMonoExpr expr res_ty)
-  where
-    combiner is_rec bind expr = HsLet (mkMonoBind bind [] is_rec) expr
 
 tcMonoExpr in_expr@(HsCase scrut matches src_loc) res_ty
   = addSrcLoc src_loc                  $
@@ -405,16 +401,8 @@ tcMonoExpr expr@(RecordCon con_name rbinds) res_ty
        -- Typecheck the record bindings
     tcRecordBinds tycon ty_args rbinds         `thenM` \ rbinds' ->
     
-    let
-      (missing_s_fields, missing_fields) = missingFields rbinds data_con
-    in
-    checkM (null missing_s_fields)
-       (mappM_ (addErrTc . missingStrictFieldCon con_name) missing_s_fields)
-                                       `thenM_`
-    doptM Opt_WarnMissingFields                `thenM` \ warn ->
-    checkM (not (warn && notNull missing_fields))
-       (mappM_ ((warnTc True) . missingFieldCon con_name) missing_fields)
-                                       `thenM_`
+       -- Check for missing fields
+    checkMissingFields data_con rbinds         `thenM_` 
 
     returnM (RecordConOut data_con con_expr rbinds')
 
@@ -632,10 +620,11 @@ tcMonoExpr (PArrSeqIn _) _
 #ifdef GHCI    /* Only if bootstrapped */
        -- Rename excludes these cases otherwise
 
-tcMonoExpr (HsSplice n expr) res_ty = tcSpliceExpr n expr res_ty
+tcMonoExpr (HsSplice n expr loc) res_ty = addSrcLoc loc (tcSpliceExpr n expr res_ty)
   
-tcMonoExpr (HsBracket (ExpBr expr)) res_ty
-  = getStage                                   `thenM` \ level ->
+tcMonoExpr (HsBracket brack loc) res_ty
+  = addSrcLoc loc                      $
+    getStage                           `thenM` \ level ->
     case bracketOK level of {
        Nothing         -> failWithTc (illegalBracket level) ;
        Just next_level ->
@@ -645,48 +634,30 @@ tcMonoExpr (HsBracket (ExpBr expr)) res_ty
        -- it again when we actually use it.
     newMutVar []                       `thenM` \ pending_splices ->
     getLIEVar                          `thenM` \ lie_var ->
-    newTyVarTy openTypeKind            `thenM` \ any_ty ->
 
     setStage (Brack next_level pending_splices lie_var) (
-       getLIE (tcMonoExpr expr any_ty)
-    )                                          `thenM` \ (expr', lie) ->
-    tcSimplifyBracket lie                      `thenM_`  
+       getLIE (tcBracket brack)
+    )                                  `thenM` \ (meta_ty, lie) ->
+    tcSimplifyBracket lie              `thenM_`  
 
-    tcMetaTy exprTyConName                     `thenM` \ meta_exp_ty ->
-    unifyTauTy res_ty meta_exp_ty              `thenM_`
+    unifyTauTy res_ty meta_ty          `thenM_`
 
        -- Return the original expression, not the type-decorated one
     readMutVar pending_splices         `thenM` \ pendings ->
-    returnM (HsBracketOut (ExpBr expr) pendings)
+    returnM (HsBracketOut brack pendings)
     }
-#endif GHCI
-\end{code}
 
-%************************************************************************
-%*                                                                     *
-\subsection{Implicit Parameter bindings}
-%*                                                                     *
-%************************************************************************
-
-\begin{code}
-tcMonoExpr (HsWith expr binds is_with) res_ty
-  = getLIE (tcMonoExpr expr res_ty)    `thenM` \ (expr', expr_lie) ->
-    mapAndUnzipM tc_ip_bind binds      `thenM` \ (avail_ips, binds') ->
-
-       -- If the binding binds ?x = E, we  must now 
-       -- discharge any ?x constraints in expr_lie
-    tcSimplifyIPs avail_ips expr_lie   `thenM` \ dict_binds ->
-    let
-       expr'' = HsLet (mkMonoBind dict_binds [] Recursive) expr'
-    in
-    returnM (HsWith expr'' binds' is_with)
+tcMonoExpr (HsReify (Reify flavour name)) res_ty
+  = addErrCtxt (ptext SLIT("At the reification of") <+> ppr name)      $
+    tcMetaTy  tycon_name       `thenM` \ reify_ty ->
+    unifyTauTy res_ty reify_ty `thenM_`
+    returnM (HsReify (ReifyOut flavour name))
   where
-    tc_ip_bind (ip, expr)
-      = newTyVarTy openTypeKind                `thenM` \ ty ->
-       getSrcLocM                      `thenM` \ loc ->
-       newIPDict (IPBind ip) ip ty     `thenM` \ (ip', ip_inst) ->
-       tcMonoExpr expr ty              `thenM` \ expr' ->
-       returnM (ip_inst, (ip', expr'))
+    tycon_name = case flavour of
+                  ReifyDecl -> DsMeta.decTyConName
+                  ReifyType -> DsMeta.typTyConName
+                  ReifyFixity -> pprPanic "tcMonoExpr: cant do reifyFixity yet" (ppr name)
+#endif GHCI
 \end{code}
 
 
@@ -822,6 +793,7 @@ tcId name   -- Look up the Id and instantiate its type
   = tcLookupIdLvl name                 `thenM` \ (id, bind_lvl) ->
 
        -- Check for cross-stage lifting
+#ifdef GHCI
     getStage                           `thenM` \ use_stage -> 
     case use_stage of
       Brack use_lvl ps_var lie_var
@@ -845,7 +817,7 @@ tcId name   -- Look up the Id and instantiate its type
                    -- just going to flag an error for now
 
        setLIEVar lie_var       (
-       newMethodFromName orig id_ty liftName   `thenM` \ lift ->
+       newMethodFromName orig id_ty DsMeta.liftName    `thenM` \ lift ->
                -- Put the 'lift' constraint into the right LIE
        
        -- Update the pending splices
@@ -860,7 +832,8 @@ tcId name   -- Look up the Id and instantiate its type
        in
        checkTc (wellStaged bind_lvl use_lvl)
                (badStageErr id bind_lvl use_lvl)       `thenM_`
-
+#endif
+       -- This is the bit that handles the no-Template-Haskell case
        case isDataConWrapId_maybe id of
                Nothing       -> loop (HsVar id) (idType id)
                Just data_con -> inst_data_con id data_con
@@ -872,8 +845,8 @@ tcId name   -- Look up the Id and instantiate its type
        | want_method_inst fun_ty
        = tcInstType VanillaTv fun_ty           `thenM` \ (tyvars, theta, tau) ->
          newMethodWithGivenTy orig fun_id 
-               (mkTyVarTys tyvars) theta tau   `thenM` \ meth ->
-         loop (HsVar (instToId meth)) tau
+               (mkTyVarTys tyvars) theta tau   `thenM` \ meth_id ->
+         loop (HsVar meth_id) tau
 
     loop fun fun_ty 
        | isSigmaTy fun_ty
@@ -981,18 +954,31 @@ badFields rbinds data_con
   where
     field_names = map fieldLabelName (dataConFieldLabels data_con)
 
-missingFields rbinds data_con
-  | null field_labels = ([], [])       -- Not declared as a record;
-                                       -- But C{} is still valid
-  | otherwise  
-  = (missing_strict_fields, other_missing_fields)
+checkMissingFields :: DataCon -> RenamedRecordBinds -> TcM ()
+checkMissingFields data_con rbinds
+  | null field_labels  -- Not declared as a record;
+                       -- But C{} is still valid if no strict fields
+  = if any isMarkedStrict field_strs then
+       -- Illegal if any arg is strict
+       addErrTc (missingStrictFields data_con [])
+    else
+       returnM ()
+                       
+  | otherwise          -- A record
+  = checkM (null missing_s_fields)
+          (addErrTc (missingStrictFields data_con missing_s_fields))   `thenM_`
+
+    doptM Opt_WarnMissingFields                `thenM` \ warn ->
+    checkM (not (warn && notNull missing_ns_fields))
+          (warnTc True (missingFields data_con missing_ns_fields))
+
   where
-    missing_strict_fields
+    missing_s_fields
        = [ fl | (fl, str) <- field_info,
                 isMarkedStrict str,
                 not (fieldLabelName fl `elem` field_names_used)
          ]
-    other_missing_fields
+    missing_ns_fields
        = [ fl | (fl, str) <- field_info,
                 not (isMarkedStrict str),
                 not (fieldLabelName fl `elem` field_names_used)
@@ -1003,7 +989,9 @@ missingFields rbinds data_con
 
     field_info = zipEqual "missingFields"
                          field_labels
-                         (dropList ex_theta (dataConStrictMarks data_con))
+                         field_strs
+
+    field_strs = dropList ex_theta (dataConStrictMarks data_con)
        -- The 'drop' is because dataConStrictMarks
        -- includes the existential dictionaries
     (_, _, _, ex_theta, _, _) = dataConSig data_con
@@ -1122,15 +1110,22 @@ recordConCtxt expr = ptext SLIT("In the record construction:") <+> ppr expr
 notSelector field
   = hsep [quotes (ppr field), ptext SLIT("is not a record selector")]
 
-missingStrictFieldCon :: Name -> FieldLabel -> SDoc
-missingStrictFieldCon con field
-  = hsep [ptext SLIT("Constructor") <+> quotes (ppr con),
-         ptext SLIT("does not have the required strict field"), quotes (ppr field)]
-
-missingFieldCon :: Name -> FieldLabel -> SDoc
-missingFieldCon con field
-  = hsep [ptext SLIT("Field") <+> quotes (ppr field),
-         ptext SLIT("is not initialised")]
+missingStrictFields :: DataCon -> [FieldLabel] -> SDoc
+missingStrictFields con fields
+  = header <> rest
+  where
+    rest | null fields = empty -- Happens for non-record constructors 
+                               -- with strict fields
+        | otherwise   = colon <+> pprWithCommas ppr fields
+
+    header = ptext SLIT("Constructor") <+> quotes (ppr con) <+> 
+            ptext SLIT("does not have the required strict field(s)") 
+         
+
+missingFields :: DataCon -> [FieldLabel] -> SDoc
+missingFields con fields
+  = ptext SLIT("Fields of") <+> quotes (ppr con) <+> ptext SLIT("not initialised:") 
+       <+> pprWithCommas ppr fields
 
 polySpliceErr :: Id -> SDoc
 polySpliceErr id