[project @ 2003-11-06 17:09:50 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
index a4294e1..68cc7ea 100644 (file)
@@ -1,6 +1,6 @@
 {-                                                             -*-haskell-*-
 -----------------------------------------------------------------------------
-$Id: Parser.y,v 1.122 2003/09/08 11:52:25 simonmar Exp $
+$Id: Parser.y,v 1.129 2003/11/06 17:09:53 simonpj Exp $
 
 Haskell grammar.
 
@@ -14,29 +14,24 @@ module Parser ( parseModule, parseStmt, parseIdentifier, parseIface ) where
 #include "HsVersions.h"
 
 import HsSyn
-import HsTypes         ( mkHsTupCon )
-
 import RdrHsSyn
-import HscTypes                ( ParsedIface(..), IsBootInterface, noDependencies )
+import HscTypes                ( ModIface, IsBootInterface, DeprecTxt )
 import Lexer
 import RdrName
-import PrelNames       ( mAIN_Name, funTyConName, listTyConName, 
-                         parrTyConName, consDataConName )
-import TysWiredIn      ( unitTyCon, unitDataCon, tupleTyCon, 
-                         tupleCon, nilDataCon )
+import TysWiredIn      ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
+                         listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
+import Type            ( funTyCon )
 import ForeignCall     ( Safety(..), CExportSpec(..), 
-                         CCallConv(..), CCallTarget(..), defaultCCallConv,
+                         CCallConv(..), CCallTarget(..), defaultCCallConv
                        )
-import OccName         ( UserFS, varName, tcName, dataName, tcClsName, tvName )
-import TyCon           ( DataConDetails(..) )
+import OccName         ( UserFS, varName, dataName, tcClsName, tvName )
 import DataCon         ( DataCon, dataConName )
-import SrcLoc          ( SrcLoc )
+import SrcLoc          ( SrcLoc, noSrcLoc )
 import Module
-import CmdLineOpts     ( opt_SccProfilingOn, opt_InPackage )
+import CmdLineOpts     ( opt_SccProfilingOn )
 import Type            ( Kind, mkArrowKind, liftedTypeKind )
-import BasicTypes      ( Boxity(..), Fixity(..), FixityDirection(..), 
-                         IPName(..), NewOrData(..), StrictnessMark(..),
-                         Activation(..), FixitySig(..) )
+import BasicTypes      ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
+                         NewOrData(..), Activation(..) )
 import Panic
 
 import GLAEXTS
@@ -127,17 +122,12 @@ Conflicts: 29 shift/reduce, [SDM 19/9/2002]
  'safe'                { T _ _ ITsafe }
  'threadsafe'  { T _ _ ITthreadsafe }
  'unsafe'      { T _ _ ITunsafe }
- 'with'        { T _ _ ITwith }
  'mdo'         { T _ _ ITmdo }
  'stdcall'      { T _ _ ITstdcallconv }
  'ccall'        { T _ _ ITccallconv }
  'dotnet'       { T _ _ ITdotnet }
  'proc'                { T _ _ ITproc }                -- for arrow notation extension
  'rec'         { T _ _ ITrec }         -- for arrow notation extension
- '_ccall_'     { T _ _ (ITccall (False, False, PlayRisky)) }
- '_ccall_GC_'  { T _ _ (ITccall (False, False, PlaySafe False)) }
- '_casm_'      { T _ _ (ITccall (False, True,  PlayRisky)) }
- '_casm_GC_'   { T _ _ (ITccall (False, True,  PlaySafe False)) }
 
  '{-# SPECIALISE'  { T _ _ ITspecialise_prag }
  '{-# SOURCE'     { T _ _ ITsource_prag }
@@ -211,7 +201,6 @@ Conflicts: 29 shift/reduce, [SDM 19/9/2002]
  PRIMINTEGER   { T _ _ (ITprimint    $$) }
  PRIMFLOAT     { T _ _ (ITprimfloat  $$) }
  PRIMDOUBLE    { T _ _ (ITprimdouble $$) }
- CLITLIT       { T _ _ (ITlitlit     $$) }
  
 -- Template Haskell
 '[|'            { T _ _ ITopenExpQuote  }       
@@ -219,11 +208,10 @@ Conflicts: 29 shift/reduce, [SDM 19/9/2002]
 '[t|'           { T _ _ ITopenTypQuote  }      
 '[d|'           { T _ _ ITopenDecQuote  }      
 '|]'            { T _ _ ITcloseQuote    }
-ID_SPLICE       { T _ _ (ITidEscape $$) }     -- $x
+TH_ID_SPLICE    { T _ _ (ITidEscape $$) }     -- $x
 '$('           { T _ _ ITparenEscape   }     -- $( exp )
-REIFY_TYPE     { T _ _ ITreifyType }   
-REIFY_DECL     { T _ _ ITreifyDecl }   
-REIFY_FIXITY   { T _ _ ITreifyFixity }
+TH_VAR_QUOTE   { T _ _ ITvarQuote      }     -- 'x
+TH_TY_QUOTE    { T _ _ ITtyQuote      }      -- ''T
 
 %monad { P } { >>= } { return }
 %lexer { lexer } { T _ _ ITeof }
@@ -272,37 +260,32 @@ cvtopdecls :: { [RdrNameHsDecl] }
 -----------------------------------------------------------------------------
 -- Interfaces (.hi-boot files)
 
-iface   :: { ParsedIface }
-       : 'module' modid 'where' ifacebody
-         {         ParsedIface {
-                       pi_mod     = $2,
-                       pi_pkg     = opt_InPackage,
-                       pi_vers    = 1,                 -- Module version
-                       pi_orphan  = False,
-                       pi_exports = (1,[($2,mkIfaceExports $4)]),
-                       pi_deps    = noDependencies,
-                       pi_usages  = [],
-                       pi_fixity  = [],
-                       pi_insts   = [],
-                       pi_decls   = map (\x -> (1,x)) $4,
-                       pi_rules   = (1,[]),
-                       pi_deprecs = Nothing
-                   }
-          }
-
-ifacebody :: { [RdrNameTyClDecl] }
+iface   :: { ModIface }
+       : 'module' modid 'where' ifacebody  { mkBootIface $2 $4 }
+
+ifacebody :: { [HsDecl RdrName] }
        :  '{'            ifacedecls '}'                { $2 }
        |      vocurly    ifacedecls close              { $2 }
 
-ifacedecls :: { [RdrNameTyClDecl] }
+ifacedecls :: { [HsDecl RdrName] }
        : ifacedecl ';' ifacedecls      { $1 : $3 }
        | ';' ifacedecls                { $2 }
        | ifacedecl                     { [$1] }
        | {- empty -}                   { [] }
 
-ifacedecl :: { RdrNameTyClDecl }
-       : tycl_decl                     { $1 }
-       | srcloc var '::' sigtype       { IfaceSig $2 $4 [] $1 }
+ifacedecl :: { HsDecl RdrName }
+       : var '::' sigtype      
+                { SigD (Sig $1 $3 noSrcLoc) }
+       | 'type' syn_hdr '=' ctype      
+               { let (tc,tvs) = $2 in TyClD (TySynonym tc tvs $4 noSrcLoc) }
+       | new_or_data tycl_hdr
+               { TyClD (mkTyData $1 $2 [] Nothing noSrcLoc) }
+       | 'class' tycl_hdr fds
+               { TyClD (mkClassDecl $2 $3 [] EmptyMonoBinds noSrcLoc) }
+
+new_or_data :: { NewOrData }
+       : 'data'        { DataType }
+       | 'newtype'     { NewType }
 
 -----------------------------------------------------------------------------
 -- The Export List
@@ -399,7 +382,7 @@ topdecl :: { RdrBinding }
        : tycl_decl                     { RdrHsDecl (TyClD $1) }
        | srcloc 'instance' inst_type where
                { let (binds,sigs) = cvMonoBindsAndSigs $4
-                 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
+                 in RdrHsDecl (InstD (InstDecl $3 binds sigs $1)) }
        | srcloc 'default' '(' comma_types0 ')'         { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
        | 'foreign' fdecl                               { RdrHsDecl $2 }
        | '{-# DEPRECATED' deprecations '#-}'           { RdrBindings (reverse $2) }
@@ -415,18 +398,17 @@ tycl_decl :: { RdrNameTyClDecl }
                -- Instead we just say b is out of scope
                { let (tc,tvs) = $3 in TySynonym tc tvs $5 $1 }
 
-
        | srcloc 'data' tycl_hdr constrs deriving
-               { mkTyData DataType $3 (DataCons (reverse $4)) $5 $1 }
+               { mkTyData DataType $3 (reverse $4) $5 $1 }
 
        | srcloc 'newtype' tycl_hdr '=' newconstr deriving
-               { mkTyData NewType $3 (DataCons [$5]) $6 $1 }
+               { mkTyData NewType $3 [$5] $6 $1 }
 
        | srcloc 'class' tycl_hdr fds where
                { let 
                        (binds,sigs) = cvMonoBindsAndSigs $5 
                  in
-                 mkClassDecl $3 $4 sigs (Just binds) $1 }
+                 mkClassDecl $3 $4 sigs binds $1 }
 
 syn_hdr :: { (RdrName, [RdrNameHsTyVar]) }     -- We don't retain the syntax of an infix
                                                -- type synonym declaration. Oh well.
@@ -440,10 +422,8 @@ syn_hdr :: { (RdrName, [RdrNameHsTyVar]) } -- We don't retain the syntax of an i
 --     (Eq a, Ord b) => T a b
 -- Rather a lot of inlining here, else we get reduce/reduce errors
 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
-       : context '=>' type             {% checkTyClHdr $3      >>= \ (tc,tvs) ->
-                                          return ($1, tc, tvs) }
-       | type                          {% checkTyClHdr $1      >>= \ (tc,tvs) ->
-                                          return ([], tc, tvs) }
+       : context '=>' type             {% checkTyClHdr $1 $3 }
+       | type                          {% checkTyClHdr [] $1 }
 
 -----------------------------------------------------------------------------
 -- Nested declarations
@@ -466,8 +446,8 @@ where       :: { [RdrBinding] }     -- Reversed
 
 binds  ::  { RdrNameHsBinds }  -- May have implicit parameters
        : decllist                      { cvBinds $1 }
-       | '{'            dbinds '}'     { IPBinds $2 False{-not with-} }
-       |     vocurly    dbinds close   { IPBinds $2 False{-not with-} }
+       | '{'            dbinds '}'     { IPBinds $2 }
+       |     vocurly    dbinds close   { IPBinds $2 }
 
 wherebinds :: { RdrNameHsBinds }       -- May have implicit parameters
        : 'where' binds                 { $2 }
@@ -692,7 +672,8 @@ sigtypes :: { [RdrNameHsType] }
        | sigtypes ',' sigtype          { $3 : $1 }
 
 sigtype :: { RdrNameHsType }
-       : ctype                         { mkHsForAllTy Nothing [] $1 }
+       : ctype                         { mkImplicitHsForAllTy [] $1 }
+       -- Wrap an Implicit forall if there isn't one there already
 
 sig_vars :: { [RdrName] }
         : sig_vars ',' var             { $3 : $1 }
@@ -703,8 +684,8 @@ sig_vars :: { [RdrName] }
 
 -- A ctype is a for-all type
 ctype  :: { RdrNameHsType }
-       : 'forall' tv_bndrs '.' ctype   { mkHsForAllTy (Just $2) [] $4 }
-       | context '=>' type             { mkHsForAllTy Nothing   $1 $3 }
+       : 'forall' tv_bndrs '.' ctype   { mkExplicitHsForAllTy $2 [] $4 }
+       | context '=>' type             { mkImplicitHsForAllTy   $1 $3 }
        -- A type of form (context => type) is an *implicit* HsForAllTy
        | type                          { $1 }
 
@@ -721,9 +702,9 @@ type :: { RdrNameHsType }
 
 gentype :: { RdrNameHsType }
         : btype                         { $1 }
-        | btype qtyconop gentype        { HsOpTy $1 (HsTyOp $2) $3 }
-        | btype  '`' tyvar '`' gentype  { HsOpTy $1 (HsTyOp $3) $5 }
-       | btype '->' gentype            { HsOpTy $1 HsArrow $3 }
+        | btype qtyconop gentype        { HsOpTy $1 $2 $3 }
+        | btype  '`' tyvar '`' gentype  { HsOpTy $1 $3 $5 }
+       | btype '->' gentype            { HsFunTy $1 $3 }
 
 btype :: { RdrNameHsType }
        : btype atype                   { HsAppTy $1 $2 }
@@ -732,8 +713,8 @@ btype :: { RdrNameHsType }
 atype :: { RdrNameHsType }
        : gtycon                        { HsTyVar $1 }
        | tyvar                         { HsTyVar $1 }
-       | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2:$4) }
-       | '(#' comma_types1 '#)'        { HsTupleTy (mkHsTupCon tcName Unboxed     $2) $2      }
+       | '(' type ',' comma_types1 ')' { HsTupleTy Boxed  ($2:$4) }
+       | '(#' comma_types1 '#)'        { HsTupleTy Unboxed $2     }
        | '[' type ']'                  { HsListTy  $2 }
        | '[:' type ':]'                { HsPArrTy  $2 }
        | '(' ctype ')'                 { HsParTy   $2 }
@@ -762,7 +743,7 @@ tv_bndrs :: { [RdrNameHsTyVar] }
 
 tv_bndr :: { RdrNameHsTyVar }
        : tyvar                         { UserTyVar $1 }
-       | '(' tyvar '::' kind ')'       { IfaceTyVar $2 $4 }
+       | '(' tyvar '::' kind ')'       { KindedTyVar $2 $4 }
 
 fds :: { [([RdrName], [RdrName])] }
        : {- empty -}                   { [] }
@@ -819,19 +800,19 @@ forall :: { [RdrNameHsTyVar] }
 
 constr_stuff :: { (RdrName, RdrNameConDetails) }
        : btype                         {% mkPrefixCon $1 [] }
-       | btype '!' atype satypes       {% mkPrefixCon $1 (BangType MarkedUserStrict $3 : $4) }
+       | btype strict_mark atype satypes {% mkPrefixCon $1 (BangType $2 $3 : $4) }
        | oqtycon '{' '}'               {% mkRecCon $1 [] }
        | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 }
        | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
 
 satypes        :: { [RdrNameBangType] }
        : atype satypes                 { unbangedType $1 : $2 }
-       | '!' atype satypes             { BangType MarkedUserStrict $2 : $3 }
+       | strict_mark atype satypes     { BangType $1 $2 : $3 }
        | {- empty -}                   { [] }
 
 sbtype :: { RdrNameBangType }
        : btype                         { unbangedType $1 }
-       | '!' atype                     { BangType MarkedUserStrict $2 }
+       | strict_mark atype             { BangType $1 $2 }
 
 fielddecls :: { [([RdrName],RdrNameBangType)] }
        : fielddecl ',' fielddecls      { $1 : $3 }
@@ -842,7 +823,11 @@ fielddecl :: { ([RdrName],RdrNameBangType) }
 
 stype :: { RdrNameBangType }
        : ctype                         { unbangedType $1 }
-       | '!' atype                     { BangType MarkedUserStrict $2 }
+       | strict_mark atype             { BangType $1 $2 }
+
+strict_mark :: { HsBang }
+       : '!'                           { HsStrict }
+       | '!' '!'                       { HsUnbox }
 
 deriving :: { Maybe RdrNameContext }
        : {- empty -}                   { Nothing }
@@ -910,7 +895,6 @@ sigdecl :: { RdrBinding }
 
 exp   :: { RdrNameHsExpr }
        : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
-       | infixexp 'with' dbinding      { HsLet (IPBinds $3 True{-not a let-}) $1 }
        | fexp srcloc '-<' exp          { HsArrApp $1 $4 placeHolderType HsFirstOrderApp True $2 }
        | fexp srcloc '>-' exp          { HsArrApp $4 $1 placeHolderType HsFirstOrderApp False $2 }
        | fexp srcloc '-<<' exp         { HsArrApp $1 $4 placeHolderType HsHigherOrderApp True $2 }
@@ -937,11 +921,6 @@ exp10 :: { RdrNameHsExpr }
        | srcloc 'mdo' stmtlist                 {% checkMDo $3  >>= \ stmts ->
                                                   return (mkHsDo MDoExpr stmts $1) }
 
-       | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 PlayRisky False placeHolderType }
-       | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 (PlaySafe False) False placeHolderType }
-       | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 PlayRisky True  placeHolderType }
-       | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 (PlaySafe False) True  placeHolderType }
-
         | scc_annot exp                                { if opt_SccProfilingOn
                                                        then HsSCC $1 $2
                                                        else HsPar $2 }
@@ -952,30 +931,16 @@ exp10 :: { RdrNameHsExpr }
 
         | '{-# CORE' STRING '#-}' exp           { HsCoreAnn $2 $4 }    -- hdaume: core annotation
 
-       | reifyexp                              { HsReify $1 }
        | fexp                                  { $1 }
 
 scc_annot :: { FastString }
        : '_scc_' STRING                        { $2 }
        | '{-# SCC' STRING '#-}'                { $2 }
 
-ccallid :: { FastString }
-       :  VARID                                { $1 }
-       |  CONID                                { $1 }
-
 fexp   :: { RdrNameHsExpr }
-       : fexp aexp                             { (HsApp $1 $2) }
+       : fexp aexp                             { HsApp $1 $2 }
        | aexp                                  { $1 }
 
-reifyexp :: { HsReify RdrName }
-       : REIFY_DECL gtycon                     { Reify ReifyDecl $2 }
-       | REIFY_DECL qvar                       { Reify ReifyDecl $2 }
-       | REIFY_TYPE qcname                     { Reify ReifyType $2 }
-       | REIFY_FIXITY qcname                   { Reify ReifyFixity $2 }
-
-aexps0         :: { [RdrNameHsExpr] }
-       : aexps                                 { reverse $1 }
-
 aexps  :: { [RdrNameHsExpr] }
        : aexps aexp                            { $2 : $1 }
        | {- empty -}                           { [] }
@@ -992,7 +957,9 @@ aexp1       :: { RdrNameHsExpr }
 -- Here was the syntax for type applications that I was planning
 -- but there are difficulties (e.g. what order for type args)
 -- so it's not enabled yet.
-       | qcname '{|' gentype '|}'          { (HsApp (HsVar $1) (HsType $3)) }
+-- But this case *is* used for the left hand side of a generic definition,
+-- which is parsed as an expression before being munged into a pattern
+       | qcname '{|' gentype '|}'      { (HsApp (HsVar $1) (HsType $3)) }
 
 aexp2  :: { RdrNameHsExpr }
        : ipvar                         { HsIPVar $1 }
@@ -1010,8 +977,12 @@ aexp2     :: { RdrNameHsExpr }
        | '_'                           { EWildPat }
        
        -- MetaHaskell Extension
-       | srcloc ID_SPLICE              { mkHsSplice (HsVar (mkUnqual varName $2)) $1 }  -- $x
+       | srcloc TH_ID_SPLICE           { mkHsSplice (HsVar (mkUnqual varName $2)) $1 }  -- $x
        | srcloc '$(' exp ')'           { mkHsSplice $3 $1 }                             -- $( exp )
+       | srcloc TH_VAR_QUOTE qvar      { HsBracket (VarBr $3) $1 }
+       | srcloc TH_VAR_QUOTE qcon      { HsBracket (VarBr $3) $1 }
+       | srcloc TH_TY_QUOTE tyvar      { HsBracket (VarBr $3) $1 }
+       | srcloc TH_TY_QUOTE gtycon     { HsBracket (VarBr $3) $1 }
        | srcloc '[|' exp '|]'          { HsBracket (ExpBr $3) $1 }                       
        | srcloc '[t|' ctype '|]'       { HsBracket (TypBr $3) $1 }                       
        | srcloc '[p|' infixexp '|]'    {% checkPattern $1 $3 >>= \p ->
@@ -1189,10 +1160,6 @@ fbind    :: { (RdrName, RdrNameHsExpr) }
 -----------------------------------------------------------------------------
 -- Implicit Parameter Bindings
 
-dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
-       : '{' dbinds '}'                { $2 }
-       | vocurly dbinds close          { $2 }
-
 dbinds         :: { [(IPName RdrName, RdrNameHsExpr)] }
        : dbinds ';' dbind              { $3 : $1 }
        | dbinds ';'                    { $1 }
@@ -1275,9 +1242,9 @@ gtycon    :: { RdrName }  -- A "general" qualified tycon
        : oqtycon                       { $1 }
        | '(' ')'                       { getRdrName unitTyCon }
        | '(' commas ')'                { getRdrName (tupleTyCon Boxed $2) }
-       | '(' '->' ')'                  { nameRdrName funTyConName }
-       | '[' ']'                       { nameRdrName listTyConName }
-       | '[:' ':]'                     { nameRdrName parrTyConName }
+       | '(' '->' ')'                  { getRdrName funTyCon }
+       | '[' ']'                       { listTyCon_RDR }
+       | '[:' ':]'                     { parrTyCon_RDR }
 
 oqtycon :: { RdrName } -- An "ordinary" qualified tycon
        : qtycon                        { $1 }
@@ -1406,8 +1373,7 @@ consym :: { RdrName }
        : CONSYM                { mkUnqual dataName $1 }
 
        -- ':' means only list cons
-       | ':'                   { nameRdrName consDataConName }
-                               -- NB: SrcName because we are reading source
+       | ':'                   { consDataCon_RDR }
 
 
 -----------------------------------------------------------------------------
@@ -1421,7 +1387,6 @@ literal :: { HsLit }
        | PRIMSTRING            { HsStringPrim $1 }
        | PRIMFLOAT             { HsFloatPrim  $1 }
        | PRIMDOUBLE            { HsDoublePrim $1 }
-       | CLITLIT               { HsLitLit     $1 placeHolderType }
 
 srcloc :: { SrcLoc }   :       {% getSrcLoc }