[project @ 2005-03-01 21:40:40 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcTyClsDecls.lhs
index 9516686..cd0e234 100644 (file)
@@ -21,7 +21,7 @@ import HscTypes               ( implicitTyThings )
 import BuildTyCl       ( buildClass, buildAlgTyCon, buildSynTyCon, buildDataCon,
                          mkDataTyConRhs, mkNewTyConRhs )
 import TcRnMonad
-import TcEnv           ( TcTyThing(..), TyThing(..), 
+import TcEnv           ( TyThing(..), 
                          tcLookupLocated, tcLookupLocatedGlobal, 
                          tcExtendGlobalEnv, tcExtendKindEnv,
                          tcExtendRecEnv, tcLookupTyVar )
@@ -29,7 +29,7 @@ import TcTyDecls      ( calcTyConArgVrcs, calcRecFlags, calcClassCycles, calcSynCycle
 import TcClassDcl      ( tcClassSigs, tcAddDeclCtxt )
 import TcHsType                ( kcHsTyVars, kcHsLiftedSigType, kcHsType, 
                          kcHsContext, tcTyVarBndrs, tcHsKindedType, tcHsKindedContext,
-                         kcHsSigType, tcHsBangType, tcLHsConSig )
+                         kcHsSigType, tcHsBangType, tcLHsConSig, tcDataKindSig )
 import TcMType         ( newKindVar, checkValidTheta, checkValidType, checkFreeness, 
                          UserTypeCtxt(..), SourceTyCtxt(..) ) 
 import TcUnify         ( unifyKind )
@@ -39,9 +39,9 @@ import TcType         ( TcKind, ThetaType, TcType, tyVarsOfType,
 import Type            ( splitTyConApp_maybe, pprThetaArrow, pprParendType )
 import Generics                ( validGenericMethodType, canDoGenerics )
 import Class           ( Class, className, classTyCon, DefMeth(..), classBigSig, classTyVars )
-import TyCon           ( TyCon, ArgVrcs, 
+import TyCon           ( TyCon, ArgVrcs, AlgTyConRhs( AbstractTyCon ),
                          tyConDataCons, mkForeignTyCon, isProductTyCon, isRecursiveTyCon,
-                         tyConStupidTheta, getSynTyConDefn, tyConDataCons, isSynTyCon, tyConName )
+                         tyConStupidTheta, getSynTyConDefn, isSynTyCon, tyConName )
 import DataCon         ( DataCon, dataConWrapId, dataConName, dataConSig, 
                          dataConFieldLabels, dataConOrigArgTys, dataConTyCon )
 import Type            ( zipTopTvSubst, substTys )
@@ -328,7 +328,7 @@ kcTyClDeclBody decl thing_inside
        ; thing_inside kinded_tvs }
   where
     result_kind (TyData { tcdKindSig = Just kind }) = kind
-    result_kind other                             = liftedTypeKind
+    result_kind other                              = liftedTypeKind
        -- On GADT-style declarations we allow a kind signature
        --      data T :: *->* where { ... }
 
@@ -366,21 +366,38 @@ tcTyClDecl calc_vrcs calc_isrec decl
 
 tcTyClDecl1 calc_vrcs calc_isrec 
   (TyData {tcdND = new_or_data, tcdCtxt = ctxt, tcdTyVars = tvs,
-          tcdLName = L _ tc_name, tcdCons = cons})
-  = tcTyVarBndrs tvs           $ \ tvs' -> do 
-  { stupid_theta <- tcStupidTheta ctxt cons
+          tcdLName = L _ tc_name, tcdKindSig = mb_ksig, tcdCons = cons})
+  = tcTyVarBndrs tvs   $ \ tvs' -> do 
+  { extra_tvs <- tcDataKindSig mb_ksig
+  ; let final_tvs = tvs' ++ extra_tvs
+  ; stupid_theta <- tcStupidTheta ctxt cons
+
   ; want_generic <- doptM Opt_Generics
+  ; unbox_strict <- doptM Opt_UnboxStrictFields
+  ; gla_exts     <- doptM Opt_GlasgowExts
+  ; is_boot     <- tcIsHsBoot  -- Are we compiling an hs-boot file?
+
+       -- Check that we don't use GADT syntax in H98 world
+  ; checkTc (gla_exts || h98_syntax) (badGadtDecl tc_name)
+
+       -- Check that there's at least one condecl,
+       -- or else we're reading an interface file, or -fglasgow-exts
+  ; checkTc (not (null cons) || gla_exts || is_boot)
+           (emptyConDeclsErr tc_name)
+    
   ; tycon <- fixM (\ tycon -> do 
-       { unbox_strict <- doptM Opt_UnboxStrictFields
-       ; gla_exts <- doptM Opt_GlasgowExts
-       ; checkTc (gla_exts || h98_syntax) (badGadtDecl tc_name)
-
-       ; data_cons <- mappM (addLocM (tcConDecl unbox_strict new_or_data tycon tvs')) cons
-       ; let tc_rhs = case new_or_data of
+       { data_cons <- mappM (addLocM (tcConDecl unbox_strict new_or_data 
+                                                tycon final_tvs)) 
+                            cons
+       ; let tc_rhs 
+               | null cons && is_boot  -- In a hs-boot file, empty cons means
+               = AbstractTyCon         -- "don't know"; hence Abstract
+               | otherwise
+               = case new_or_data of
                        DataType -> mkDataTyConRhs stupid_theta data_cons
                        NewType  -> ASSERT( isSingleton data_cons )
                                    mkNewTyConRhs tycon (head data_cons)
-       ; buildAlgTyCon tc_name tvs' tc_rhs arg_vrcs is_rec
+       ; buildAlgTyCon tc_name final_tvs tc_rhs arg_vrcs is_rec
                        (want_generic && canDoGenerics data_cons)
        })
   ; return (ATyCon tycon)
@@ -447,6 +464,9 @@ tcConDecl unbox_strict DataType tycon tc_tvs        -- Ordinary data types
     ; let 
        is_vanilla = null ex_tvs && null (unLoc ex_ctxt) 
                -- Vanilla iff no ex_tvs and no context
+               -- Must check the context too because of
+               -- implicit params; e.g.
+               --  data T = (?x::Int) => MkT Int
 
        tc_datacon is_infix field_lbls btys
          = do { let { bangs = map getBangStrictness btys }
@@ -461,7 +481,10 @@ tcConDecl unbox_strict DataType tycon tc_tvs       -- Ordinary data types
     ; case details of
        PrefixCon btys     -> tc_datacon False [] btys
        InfixCon bty1 bty2 -> tc_datacon True [] [bty1,bty2]
-       RecCon fields      -> do { checkTc is_vanilla (exRecConErr name)
+       RecCon fields      -> do { checkTc (null ex_tvs) (exRecConErr name)
+               -- It's ok to have an implicit-parameter context
+               -- for the data constructor, provided it binds
+               -- no type variables
                                 ; let { (field_names, btys) = unzip fields }
                                 ; tc_datacon False field_names btys } }
 
@@ -606,7 +629,7 @@ checkValidDataCon tc con
 --     ; checkFreeness tvs ex_theta }
   where
     ctxt = ConArgCtxt (dataConName con) 
-    (tvs, ex_theta, _, _, _) = dataConSig con
+--    (tvs, ex_theta, _, _, _) = dataConSig con
 
 
 -------------------------------
@@ -737,4 +760,8 @@ badDataConTyCon data_con
 badGadtDecl tc_name
   = vcat [ ptext SLIT("Illegal generalised algebraic data declaration for") <+> quotes (ppr tc_name)
         , nest 2 (parens $ ptext SLIT("Use -fglasgow-exts to allow GADTs")) ]
+
+emptyConDeclsErr tycon
+  = sep [quotes (ppr tycon) <+> ptext SLIT("has no constructors"),
+        nest 4 (ptext SLIT("(-fglasgow-exts permits this)"))]
 \end{code}