[project @ 2000-02-10 18:39:51 by lewie]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcIfaceSig.lhs
index 6b8328b..6eae048 100644 (file)
@@ -19,7 +19,7 @@ import TcMonoType     ( tcHsType, tcHsTypeKind,
 import TcEnv           ( ValueEnv, tcExtendTyVarEnv, 
                          tcExtendGlobalValEnv, tcSetValueEnv,
                          tcLookupTyConByKey, tcLookupValueMaybe,
-                         explicitLookupValue, badCon, badPrimOp
+                         explicitLookupValue, badCon, badPrimOp, valueEnvIds
                        )
 import TcType          ( TcKind, kindToTcKind )
 
@@ -31,18 +31,18 @@ import CoreSyn
 import CoreUtils       ( coreExprType )
 import CoreUnfold
 import CoreLint                ( lintUnfolding )
-import WwLib           ( mkWrapper )
+import WorkWrap                ( mkWrapper )
 import PrimOp          ( PrimOp(..) )
 
 import Id              ( Id, mkId, mkVanillaId,
-                         isPrimitiveId_maybe, isDataConId_maybe
+                         isDataConId_maybe
                        )
 import IdInfo
 import DataCon         ( dataConSig, dataConArgTys )
-import Type            ( mkSynTy, mkTyVarTys, splitAlgTyConApp )
+import Type            ( mkSynTy, mkTyVarTys, splitAlgTyConApp, unUsgTy )
 import Var             ( IdOrTyVar, mkTyVar, tyVarKind )
 import VarEnv
-import Name            ( Name, NamedThing(..) )
+import Name            ( Name, NamedThing(..), isLocallyDefined )
 import Unique          ( rationalTyConKey )
 import TysWiredIn      ( integerTy, stringTy )
 import Demand          ( wwLazy )
@@ -65,23 +65,23 @@ tcInterfaceSigs :: ValueEnv         -- Envt to use when checking unfoldings
                -> TcM s [Id]
                
 
-tcInterfaceSigs unf_env (SigD (IfaceSig name ty id_infos src_loc) : rest)
-  = tcAddSrcLoc src_loc (
-    tcAddErrCtxt (ifaceSigCtxt name) (
-       tcHsType ty                                             `thenTc` \ sigma_ty ->
-       tcIdInfo unf_env name sigma_ty vanillaIdInfo id_infos   `thenTc` \ id_info ->
+tcInterfaceSigs unf_env decls
+  = listTc [ do_one name ty id_infos src_loc
+          | SigD (IfaceSig name ty id_infos src_loc) <- decls]
+  where
+    in_scope_vars = filter isLocallyDefined (valueEnvIds unf_env)
+
+    do_one name ty id_infos src_loc
+      = tcAddSrcLoc src_loc                            $       
+       tcAddErrCtxt (ifaceSigCtxt name)                $
+       tcHsType ty                                     `thenTc` \ sigma_ty ->
+       tcIdInfo unf_env in_scope_vars name 
+                sigma_ty vanillaIdInfo id_infos        `thenTc` \ id_info ->
        returnTc (mkId name sigma_ty id_info)
-    ))                                         `thenTc` \ sig_id ->
-    tcInterfaceSigs unf_env rest               `thenTc` \ sig_ids ->
-    returnTc (sig_id : sig_ids)
-
-tcInterfaceSigs unf_env (other_decl : rest) = tcInterfaceSigs unf_env rest
-
-tcInterfaceSigs unf_env [] = returnTc []
 \end{code}
 
 \begin{code}
-tcIdInfo unf_env name ty info info_ins
+tcIdInfo unf_env in_scope_vars name ty info info_ins
   = foldlTc tcPrag vanillaIdInfo info_ins
   where
     tcPrag info (HsArity arity) = returnTc (info `setArityInfo`  arity)
@@ -89,17 +89,14 @@ tcIdInfo unf_env name ty info info_ins
     tcPrag info (HsNoCafRefs)   = returnTc (info `setCafInfo`   NoCafRefs)
     tcPrag info (HsCprInfo cpr_info)     = returnTc (info `setCprInfo` cpr_info)
 
-    tcPrag info (HsUnfold inline_prag maybe_expr)
-       = (case maybe_expr of
-               Just expr -> tcPragExpr unf_env name [] expr
-               Nothing   -> returnNF_Tc Nothing
-         )                                     `thenNF_Tc` \ maybe_expr' ->
+    tcPrag info (HsUnfold inline_prag expr)
+       = tcPragExpr unf_env name in_scope_vars expr    `thenNF_Tc` \ maybe_expr' ->
          let
                -- maybe_expr doesn't get looked at if the unfolding
                -- is never inspected; so the typecheck doesn't even happen
                unfold_info = case maybe_expr' of
-                               Nothing    -> NoUnfolding
-                               Just expr' -> mkUnfolding expr' 
+                               Nothing    -> noUnfolding
+                               Just expr' -> mkTopUnfolding expr' 
                info1 = info `setUnfoldingInfo` unfold_info
                info2 = info1 `setInlinePragInfo` inline_prag
          in
@@ -114,7 +111,7 @@ tcIdInfo unf_env name ty info info_ins
 
 \begin{code}
 tcWorkerInfo unf_env ty info worker_name
-  | arity == 0
+  | not (hasArity arity_info)
   = pprPanic "Worker with no arity info" (ppr worker_name)
  
   | otherwise
@@ -122,7 +119,7 @@ tcWorkerInfo unf_env ty info worker_name
     let
        -- Watch out! We can't pull on unf_env too eagerly!
        info' = case explicitLookupValue unf_env worker_name of
-                       Just worker_id -> info `setUnfoldingInfo`  mkUnfolding (wrap_fn worker_id)
+                       Just worker_id -> info `setUnfoldingInfo`  mkTopUnfolding (wrap_fn worker_id)
                                                `setWorkerInfo`     Just worker_id
 
                        Nothing        -> pprTrace "tcWorkerInfo failed:" (ppr worker_name) info
@@ -131,11 +128,12 @@ tcWorkerInfo unf_env ty info worker_name
   where
        -- We are relying here on arity, cpr and strictness info always appearing 
        -- before worker info,  fingers crossed ....
-      arity    = arityLowerBound (arityInfo info)
-      cpr_info = cprInfo info
-      demands  = case strictnessInfo info of
+      arity_info = arityInfo info
+      arity      = arityLowerBound arity_info
+      cpr_info   = cprInfo info
+      demands    = case strictnessInfo info of
                        StrictnessInfo d _ -> d
-                       _                  -> repeat wwLazy     -- Noncommittal
+                       _                  -> take arity (repeat wwLazy)        -- Noncommittal
 \end{code}
 
 For unfoldings we try to do the job lazily, so that we never type check
@@ -203,18 +201,18 @@ tcCoreExpr (UfVar name)
     returnTc (Var id)
 
 tcCoreExpr (UfCon con args) 
-  = tcUfCon con                        `thenTc` \ con' ->
-    mapTc tcCoreExpr args      `thenTc` \ args' ->
-    returnTc (Con con' args')
+  = mapTc tcCoreExpr args      `thenTc` \ args' ->
+    tcUfCon con args'
 
 tcCoreExpr (UfTuple name args) 
-  = tcUfDataCon name           `thenTc` \ con ->
+  =    -- See notes with tcUfCon (UfDataCon ...)
+    tcVar name                 `thenTc` \ con_id ->
     mapTc tcCoreExpr args      `thenTc` \ args' ->
     let
        -- Put the missing type arguments back in
-       con_args = map (Type . coreExprType) args' ++ args'
+       con_args = map (Type . unUsgTy . coreExprType) args' ++ args'
     in
-    returnTc (Con con con_args)
+    returnTc (mkApps (Var con_id) con_args)
 
 tcCoreExpr (UfLam bndr body)
   = tcCoreLamBndr bndr                 $ \ bndr' ->
@@ -254,7 +252,8 @@ tcCoreExpr (UfNote note expr)
   = tcCoreExpr expr            `thenTc` \ expr' ->
     case note of
        UfCoerce to_ty -> tcHsType to_ty        `thenTc` \ to_ty' ->
-                         returnTc (Note (Coerce to_ty' (coreExprType expr')) expr')
+                         returnTc (Note (Coerce (unUsgTy to_ty')
+                                                 (unUsgTy (coreExprType expr'))) expr')
        UfInlineCall   -> returnTc (Note InlineCall expr')
        UfInlineMe     -> returnTc (Note InlineMe   expr')
        UfSCC cc       -> returnTc (Note (SCC cc)   expr')
@@ -263,50 +262,54 @@ tcCoreNote (UfSCC cc)   = returnTc (SCC cc)
 tcCoreNote UfInlineCall = returnTc InlineCall 
 
 
--- rationalTy isn't built in so, we have to construct it
--- (the "ty" part of the incoming literal is simply bottom)
-tcUfCon (UfLitCon (NoRepRational lit _)) 
-  = tcLookupTyConByKey rationalTyConKey        `thenNF_Tc` \ rational_tycon ->
-    let
-       rational_ty  = mkSynTy rational_tycon []
-    in
-    returnTc (Literal (NoRepRational lit rational_ty)) 
-
--- Similarly for integers and strings, except that they are wired in
-tcUfCon (UfLitCon (NoRepInteger lit _)) 
-  = returnTc (Literal (NoRepInteger lit integerTy))
-tcUfCon (UfLitCon (NoRepStr lit _))
-  = returnTc (Literal (NoRepStr lit stringTy))
-
-tcUfCon (UfLitCon other_lit)
-  = returnTc (Literal other_lit)
+----------------------------------
+tcUfCon (UfLitCon lit) args
+  = ASSERT( null args)
+    tcUfLit lit                `thenTc` \ lit ->
+    returnTc (Con (Literal lit) [])
 
 -- The dreaded lit-lits are also similar, except here the type
 -- is read in explicitly rather than being implicit
-tcUfCon (UfLitLitCon lit ty)
-  = tcHsType ty                `thenTc` \ ty' ->
-    returnTc (Literal (MachLitLit lit ty'))
-
-tcUfCon (UfDataCon name) = tcUfDataCon name
-
-tcUfCon (UfPrimOp name)
-  = tcVar name         `thenTc` \ op_id ->
-    case isPrimitiveId_maybe op_id of
-       Just op -> returnTc (PrimOp op)
-       Nothing -> failWithTc (badPrimOp name)
-
-tcUfCon (UfCCallOp str is_dyn casm gc)
-  = case is_dyn of
-       True  -> 
-          tcGetUnique `thenNF_Tc` \ u ->
-         returnTc (PrimOp (CCallOp (Right u) casm gc cCallConv))
-       False -> returnTc (PrimOp (CCallOp (Left str) casm gc cCallConv))
-
-tcUfDataCon name
+tcUfCon (UfLitLitCon lit ty) args
+  = ASSERT( null args )
+    tcHsType ty                `thenTc` \ ty' ->
+    returnTc (Con (Literal (MachLitLit lit ty')) [])
+
+-- Primops are reverse-engineered
+-- into applications of their Ids.  In this way, any
+-- RULES that apply to the Id will work when this thing is unfolded.
+-- It's a bit of a hack, but it works nicely
+-- Can't do it for datacons, because the data con Id doesn't necessarily
+-- have the same type as the data con (existentials)
+
+tcUfCon (UfPrimOp name)  args = tcVar name             `thenTc` \ op_id ->
+                               returnTc (mkApps (Var op_id) args)
+
+tcUfCon (UfDataCon name) args
   = tcVar name         `thenTc` \ con_id ->
     case isDataConId_maybe con_id of
-       Just con -> returnTc (DataCon con)
+       Just con -> returnTc (mkConApp con args)
        Nothing  -> failWithTc (badCon name)
+
+tcUfCon (UfCCallOp str is_dyn casm gc) args
+  | is_dyn    = tcGetUnique `thenNF_Tc` \ u ->
+               returnTc (Con (PrimOp (CCallOp (Right u) casm gc cCallConv)) args)
+  | otherwise = returnTc (Con (PrimOp (CCallOp (Left str) casm gc cCallConv)) args)
+
+----------------------------------
+tcUfLit (NoRepRational lit _)
+  =    -- rationalTy isn't built in so, we have to construct it
+       -- (the "ty" part of the incoming literal is simply bottom)
+    tcLookupTyConByKey rationalTyConKey        `thenNF_Tc` \ rational_tycon ->
+    let
+       rational_ty  = mkSynTy rational_tycon []
+    in
+    returnTc (NoRepRational lit rational_ty)
+
+-- Similarly for integers and strings, except that they are wired in
+tcUfLit (NoRepInteger lit _) = returnTc (NoRepInteger lit integerTy)
+tcUfLit (NoRepStr lit _)     = returnTc (NoRepStr lit stringTy)
+tcUfLit other_lit           = returnTc other_lit
 \end{code}
 
 \begin{code}