[project @ 1999-06-28 15:42:33 by simonmar]
[ghc-hetmet.git] / ghc / compiler / rename / ParseIface.y
index 4cf9211..419fa11 100644 (file)
@@ -5,7 +5,6 @@ module ParseIface ( parseIface, IfaceStuff(..) ) where
 
 import HsSyn           -- quite a bit of stuff
 import RdrHsSyn                -- oodles of synonyms
-import HsDecls         ( HsIdInfo(..), HsStrictnessInfo(..) )
 import HsTypes         ( mkHsForAllTy )
 import HsCore
 import Const           ( Literal(..), mkMachInt_safe )
@@ -14,12 +13,12 @@ import BasicTypes   ( Fixity(..), FixityDirection(..),
                        )
 import CostCentre       ( CostCentre(..), IsCafCC(..), IsDupdCC(..) )
 import HsPragmas       ( noDataPragmas, noClassPragmas )
-import Type            ( Kind, mkArrowKind, boxedTypeKind, openTypeKind )
-import IdInfo           ( ArityInfo, exactArity )
+import Type            ( Kind, mkArrowKind, boxedTypeKind, openTypeKind, UsageAnn(..) )
+import IdInfo           ( ArityInfo, exactArity, CprInfo(..) )
 import Lex             
 
 import RnMonad         ( ImportVersion, LocalVersion, ParsedIface(..), WhatsImported(..),
-                         RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..)
+                         RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..), WhetherHasOrphans
                        ) 
 import Bag             ( emptyBag, unitBag, snocBag )
 import FiniteMap       ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
@@ -29,7 +28,7 @@ import OccName          ( mkSysOccFS,
                          tcName, varName, dataName, clsName, tvName,
                          EncodedFS 
                        )
-import Module           ( Module, mkSysModuleFS, IfaceFlavour, hiFile, hiBootFile )                    
+import Module           ( ModuleName, mkSysModuleFS )                  
 import PrelMods         ( mkTupNameStr, mkUbxTupNameStr )
 import PrelInfo         ( mkTupConRdrName, mkUbxTupConRdrName )
 import SrcLoc          ( SrcLoc )
@@ -44,9 +43,9 @@ import Ratio ( (%) )
 }
 
 %name      parseIface
-%tokentype  { IfaceToken }
-%monad     { IfM }{ thenIf }{ returnIf }
-%lexer      { lexIface } { ITeof }
+%tokentype  { Token }
+%monad     { P }{ thenP }{ returnP }
+%lexer      { lexer } { ITeof }
 
 %token
  'case'        { ITcase }                      -- Haskell keywords
@@ -70,17 +69,22 @@ import Ratio ( (%) )
  'then'        { ITthen }
  'type'        { ITtype }
  'where'       { ITwhere }
- 'as'          { ITas }
- 'qualified'   { ITqualified }
- 'hiding'      { IThiding }
-
- '__interface' { ITinterface }                 -- GHC-extension keywords
- '__export'    { ITexport }
- '__instimport'        { ITinstimport }
- '__forall'    { ITforall }
+
+ 'forall'      { ITforall }                    -- GHC extension keywords
+ 'foreign'     { ITforeign }
+ 'export'      { ITexport }
+ 'label'       { ITlabel } 
+ 'dynamic'     { ITdynamic }
+ 'unsafe'      { ITunsafe }
+
+ '__interface' { ITinterface }                 -- interface keywords
+ '__export'    { IT__export }
+ '__forall'    { IT__forall }
+ '__depends'   { ITdepends }
  '__letrec'    { ITletrec }
  '__coerce'    { ITcoerce }
- '__inline'    { ITinline }
+ '__inline_call'{ ITinlineCall }
+ '__inline_me'  { ITinlineMe }
  '__DEFAULT'   { ITdefaultbranch }
  '__bot'       { ITbottom }
  '__integer'   { ITinteger_lit }
@@ -93,11 +97,16 @@ import Ratio ( (%) )
  '__scc'       { ITscc }
  '__sccC'       { ITsccAllCafs }
 
+ '__o'         { ITonce }
+ '__m'         { ITmany }
+
  '__A'         { ITarity }
  '__P'         { ITspecialise }
  '__C'         { ITnocaf }
  '__U'         { ITunfold $$ }
  '__S'         { ITstrict $$ }
+ '__R'         { ITrules }
+ '__M'         { ITcprinfo $$ }
 
  '..'          { ITdotdot }                    -- reserved symbols
  '::'          { ITdcolon }
@@ -140,6 +149,7 @@ import Ratio ( (%) )
  STRING                { ITstring   $$ }
  INTEGER       { ITinteger  $$ }
  RATIONAL      { ITrational $$ }
+ CLITLIT       { ITlitlit   $$ }
 
  UNKNOWN       { ITunknown  $$ }
 %%
@@ -153,25 +163,26 @@ iface_stuff :: { IfaceStuff }
 iface_stuff : iface            { let (nm, iff) = $1 in PIface nm iff }
            | type              { PType   $1 }
            | id_info           { PIdInfo $1 }
+           | '__R' rules       { PRules  $2 }
 
 
-iface          :: { (EncodedFS, ParsedIface) }
-iface          : '__interface' mod_fs INTEGER checkVersion 'where'
-                  import_part
-                 instance_import_part
+iface          :: { (ModuleName, ParsedIface) }
+iface          : '__interface' mod_fs INTEGER orphans checkVersion 'where'
                  exports_part
+                  import_part
                  instance_decl_part
                  decls_part
+                 rules_part
                  { ( $2                        -- Module name
-                   , ParsedIface 
-                       (fromInteger $3)        -- Module version
-                       (reverse $6)            -- Usages
-                       (reverse $8)            -- Exports
-                       (reverse $7)            -- Instance import modules
-                       (reverse $10)           -- Decls
-                       (reverse $9)            -- Local instances
-                   )
-                 }
+                   , ParsedIface {
+                       pi_mod = fromInteger $3,        -- Module version
+                       pi_orphan  = $4,
+                       pi_exports = $7,        -- Exports
+                       pi_usages  = $8,        -- Usages
+                       pi_insts   = $9,        -- Local instances
+                       pi_decls   = $10,       -- Decls
+                       pi_rules   = $11        -- Rules 
+                     } ) }
 
 --------------------------------------------------------------------------
 
@@ -180,12 +191,19 @@ import_part :                                               { [] }
            |  import_part import_decl                    { $2 : $1 }
            
 import_decl :: { ImportVersion OccName }
-import_decl : 'import' mod_fs opt_bang INTEGER '::' whats_imported ';'
-                       { (mkSysModuleFS $2 $3, fromInteger $4, $6) }
+import_decl : 'import' mod_fs INTEGER orphans whats_imported ';'
+                       { (mkSysModuleFS $2, fromInteger $3, $4, $5) }
+       -- import Foo 3 :: a 1 b 3 c 7 ;        means import a,b,c from Foo
+       -- import Foo 3 ;                       means import all of Foo
+       -- import Foo 3 ! :: ...stuff... ;      the ! means that Foo contains orphans
+
+orphans                    :: { WhetherHasOrphans }
+orphans                    :                                           { False }
+                   | '!'                                       { True }
 
 whats_imported      :: { WhatsImported OccName }
 whats_imported      :                                           { Everything }
-                    | name_version_pair name_version_pairs      { Specifically ($1:$2) }
+                    | '::' name_version_pairs                  { Specifically $2 }
 
 name_version_pairs  :: { [LocalVersion OccName] }
 name_version_pairs  :                                                  { [] }
@@ -195,21 +213,13 @@ name_version_pair   ::    { LocalVersion OccName }
 name_version_pair   :  var_occ INTEGER                         { ($1, fromInteger $2) }
                     |  tc_occ  INTEGER                          { ($1, fromInteger $2) }
 
-instance_import_part :: { [Module] }
-instance_import_part :                                                 {   []    }
-                     | instance_import_part '__instimport' mod_name ';'
-                                                               { $3 : $1 }
 
 --------------------------------------------------------------------------
 
 exports_part   :: { [ExportItem] }
 exports_part   :                                       { [] }
-               | exports_part '__export' opt_bang mod_fs entities ';'
-                                               { (mkSysModuleFS $4 $3,$5) : $1 }
-
-opt_bang       :: { IfaceFlavour }
-opt_bang       :                                       { hiFile }
-               | '!'                                   { hiBootFile }
+               | exports_part '__export' 
+                 mod_fs entities ';'                   { (mkSysModuleFS $3, $4) : $1 }
 
 entities       :: { [RdrAvailInfo] }
 entities       :                                       { [] }
@@ -255,11 +265,8 @@ csigs1             : csig                          { [$1] }
                | csig ';' csigs1               { $1 : $3 }
 
 csig           :: { RdrNameSig }
-csig           :  src_loc var_name '::' type { ClassOpSig $2 Nothing $4 $1 }
-               |  src_loc var_name '=' '::' type       
-                       { ClassOpSig $2 
-                           (Just (error "Un-filled-in default method"))
-                           $5 $1 }
+csig           :  src_loc var_name '::' type           { mkClassOpSig False $2 $4 $1 }
+               |  src_loc var_name '=' '::' type       { mkClassOpSig True  $2 $5 $1 }
 
 --------------------------------------------------------------------------
 
@@ -272,7 +279,7 @@ inst_decl   :  src_loc 'instance' type '=' var_name ';'
                        { InstDecl $3
                                   EmptyMonoBinds       {- No bindings -}
                                   []                   {- No user pragmas -}
-                                  (Just $5)            {- Dfun id -}
+                                  $5                   {- Dfun id -}
                                   $1
                        }
 
@@ -301,14 +308,44 @@ decl    : src_loc var_name '::' type maybe_idinfo
 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
 maybe_idinfo  : {- empty -}    { \_ -> [] }
              | src_loc PRAGMA  { \x -> 
-                                  case parseIface $2 $1 of
-                                    Succeeded (PIdInfo id_info) -> id_info
-                                    Failed err -> pprPanic "IdInfo parse failed" 
-                                                           (vcat [ppr x, err])
+                                  case parseIface $2
+                                          PState{bol = 0#, atbol = 1#,
+                                                 context = [],
+                                                 glasgow_exts = 1#,
+                                                 loc = $1 } of
+                                    POk _ (PIdInfo id_info) -> id_info
+                                    PFailed err -> 
+                                       pprPanic "IdInfo parse failed" 
+                                           (vcat [ppr x, err])
                                }
 
 -----------------------------------------------------------------------------
 
+rules_part :: { [RdrNameRuleDecl] }
+rules_part : {- empty -}       { [] }
+          | src_loc PRAGMA     { case parseIface $2 
+                                          PState{bol = 0#, atbol = 1#,
+                                                 context = [],
+                                                 glasgow_exts = 1#,
+                                                 loc = $1 }  of
+                                    POk _ (PRules rules) -> rules
+                                    PFailed err -> 
+                                         pprPanic "Rules parse failed" err
+                               }
+
+rules     :: { [RdrNameRuleDecl] }
+          : {- empty -}        { [] }
+          | rule ';' rules     { $1:$3 }
+
+rule      :: { RdrNameRuleDecl }
+rule      : src_loc STRING rule_forall qvar_name 
+            core_args '=' core_expr    { IfaceRuleDecl $4 (UfRuleBody $2 $3 $5 $7) $1 } 
+
+rule_forall    :: { [UfBinder RdrName] }
+rule_forall    : '__forall' '{' core_bndrs '}' { $3 }
+                 
+-----------------------------------------------------------------------------
+
 version                :: { Version }
 version                :  INTEGER                              { fromInteger $1 }
 
@@ -316,7 +353,7 @@ decl_context        :: { RdrNameContext }
 decl_context   :                                       { [] }
                | '{' context_list1 '}' '=>'    { $2 }
 
-----------------------------------------------------------------
+----------------------------------------------------------------------------
 
 constrs                :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
                :                       { [] }
@@ -348,6 +385,7 @@ batypes             :                                       { [] }
 batype         :: { RdrNameBangType }
 batype         :  atype                                { Unbanged $1 }
                |  '!' atype                            { Banged   $2 }
+               |  '!' '!' atype                        { Unpacked $3 }
 
 fields1                :: { [([RdrName], RdrNameBangType)] }
 fields1                : field                                 { [$1] }
@@ -356,6 +394,7 @@ fields1             : field                                 { [$1] }
 field          :: { ([RdrName], RdrNameBangType) }
 field          :  var_names1 '::' type         { ($1, Unbanged $3) }
                |  var_names1 '::' '!' type     { ($1, Banged   $4) }
+               |  var_names1 '::' '!' '!' type { ($1, Unpacked $5) }
 --------------------------------------------------------------------------
 
 type           :: { RdrNameHsType }
@@ -385,6 +424,8 @@ types2              :  type ',' type                        { [$1,$3] }
 btype          :: { RdrNameHsType }
 btype          :  atype                                { $1 }
                |  btype atype                          { MonoTyApp $1 $2 }
+                |  '__o' atype                         { MonoUsgTy UsOnce $2 }
+                |  '__m' atype                         { MonoUsgTy UsMany $2 }
 
 atype          :: { RdrNameHsType }
 atype          :  qtc_name                             { MonoTyVar $1 }
@@ -406,19 +447,23 @@ atypes            :                                       { [] }
 mod_fs         :: { EncodedFS }
                :  CONID                { $1 }
 
-mod_name       :: { Module }
-               :  mod_fs               { mkSysModuleFS $1 hiFile }
+mod_name       :: { ModuleName }
+               :  mod_fs               { mkSysModuleFS $1 }
 
 
 ---------------------------------------------------
 var_fs         :: { EncodedFS }
                : VARID                 { $1 }
                | VARSYM                { $1 }
-               | '-'                   { SLIT("-") }
                | '!'                   { SLIT("!") }
-
-
-qvar_fs                :: { (EncodedFS, EncodedFS, IfaceFlavour) }
+               | 'forall'              { SLIT("forall") }
+               | 'foreign'             { SLIT("foreign") }
+               | 'export'              { SLIT("export") }
+               | 'label'               { SLIT("label") }
+               | 'dynamic'             { SLIT("dynamic") }
+               | 'unsafe'              { SLIT("unsafe") }
+
+qvar_fs                :: { (EncodedFS, EncodedFS) }
                :  QVARID               { $1 }
                |  QVARSYM              { $1 }
 
@@ -449,7 +494,7 @@ data_fs             :: { EncodedFS }
                :  CONID                { $1 }
                |  CONSYM               { $1 }
 
-qdata_fs       :: { (EncodedFS, EncodedFS, IfaceFlavour) }
+qdata_fs       :: { (EncodedFS, EncodedFS) }
                 :  QCONID              { $1 }
                 |  QCONSYM             { $1 }
 
@@ -523,31 +568,33 @@ akind             :: { Kind }
 --------------------------------------------------------------------------
 
 id_info                :: { [HsIdInfo RdrName] }
-id_info                :                               { [] }
+               :                               { [] }
                | id_info_item id_info          { $1 : $2 }
+                | strict_info id_info          { $1 ++ $2 }
 
 id_info_item   :: { HsIdInfo RdrName }
-id_info_item   : '__A' arity_info              { HsArity $2 }
-               | strict_info                   { HsStrictness $1 }
+               : '__A' arity_info              { HsArity $2 }
                | '__U' core_expr               { HsUnfold $1 (Just $2) }
                 | '__U'                        { HsUnfold $1 Nothing }
-                | '__P' spec_tvs
-                     atypes '=' core_expr       { HsSpecialise $2 $3 $5 }
                | '__C'                         { HsNoCafRefs }
 
+strict_info     :: { [HsIdInfo RdrName] }
+               : cpr worker                    { ($1:$2) }
+               | strict worker                 { ($1:$2) }
+               | cpr strict worker             { ($1:$2:$3) }
 
-spec_tvs       :: { [HsTyVar RdrName] }
-spec_tvs       : '[' tv_bndrs ']'              { $2 }
-       
+cpr            :: { HsIdInfo RdrName }
+               : '__M'                         { HsCprInfo $1 }
 
-arity_info     :: { ArityInfo }
-arity_info     : INTEGER                       { exactArity (fromInteger $1) }
+strict         :: { HsIdInfo RdrName }
+               : '__S'                         { HsStrictness (HsStrictnessInfo $1) }
 
-strict_info    :: { HsStrictnessInfo RdrName }
-strict_info    : '__S' qvar_name '{' qdata_names '}'   
-                                       { HsStrictnessInfo $1 (Just ($2,$4)) }
-               | '__S' qvar_name       { HsStrictnessInfo $1 (Just ($2,[])) }
-               | '__S'                 { HsStrictnessInfo $1 Nothing }
+worker         :: { [HsIdInfo RdrName] }
+               : qvar_name                     { [HsWorker $1] }
+               | {- nothing -}                 { [] }
+
+arity_info     :: { ArityInfo }
+               : INTEGER                       { exactArity (fromInteger $1) }
 
 -------------------------------------------------------
 core_expr      :: { UfExpr RdrName }
@@ -563,7 +610,8 @@ core_expr   : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
                | con_or_primop '{' core_args '}'       { UfCon $1 $3 }
                 | '__litlit' STRING atype               { UfCon (UfLitLitCon $2 $3) [] }
 
-                | '__inline' core_expr                  { UfNote UfInlineCall $2 }
+                | '__inline_me' core_expr               { UfNote UfInlineMe $2 }
+                | '__inline_call' core_expr             { UfNote UfInlineCall $2 }
                 | '__coerce' atype core_expr            { UfNote (UfCoerce $2) $3 }
                | scc core_expr                         { UfNote (UfSCC $1) $2  }
                | fexpr                                 { $1 }
@@ -641,23 +689,31 @@ comma_var_names1 : var_name                                       { [$1] }
                 | var_name ',' comma_var_names1                { $1 : $3 }
 
 core_lit       :: { Literal }
-core_lit       : INTEGER                       { mkMachInt_safe $1 }
+core_lit       : integer                       { mkMachInt_safe $1 }
                | CHAR                          { MachChar $1 }
                | STRING                        { MachStr $1 }
                | '__string' STRING             { NoRepStr $2 (panic "NoRepStr type") }
-               | RATIONAL                      { MachDouble $1 }
-               | '__float' RATIONAL            { MachFloat $2 }
+               | rational                      { MachDouble $1 }
+               | '__float' rational            { MachFloat $2 }
 
-               | '__integer' INTEGER           { NoRepInteger  $2 (panic "NoRepInteger type") 
+               | '__integer' integer           { NoRepInteger  $2 (panic "NoRepInteger type") 
                                                        -- The type checker will add the types
                                                }
 
-               | '__rational' INTEGER INTEGER  { NoRepRational ($2 % $3) 
+               | '__rational' integer integer  { NoRepRational ($2 % $3) 
                                                   (panic "NoRepRational type")
                                                        -- The type checker will add the type
                                                }
 
-               | '__addr' INTEGER              { MachAddr $2 }
+               | '__addr' integer              { MachAddr $2 }
+
+integer                :: { Integer }
+               : INTEGER                       { $1 }
+               | '-' INTEGER                   { (-$2) }
+
+rational       :: { Rational }
+               : RATIONAL                      { $1 }
+               | '-' RATIONAL                  { (-$2) }
 
 core_bndr       :: { UfBinder RdrName }
 core_bndr       : core_val_bndr                                 { $1 }
@@ -676,13 +732,14 @@ core_tv_bndr      :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
 
 ccall_string   :: { FAST_STRING }
                : STRING                                        { $1 }
+               | CLITLIT                                       { $1 }
                | VARID                                         { $1 }
                | CONID                                         { $1 }
 
 ------------------------------------------------------------------------
 scc     :: { CostCentre }
         :  '__sccC' '{' mod_name STRING '}'                      { AllCafsCC $3 $4 }
-        |  '__scc' '(' cc_name mod_name STRING cc_dup cc_caf '}'
+        |  '__scc' '{' cc_name mod_name STRING cc_dup cc_caf '}'
                              { NormalCC { cc_name = $3, cc_mod = $4, cc_grp = $5,
                                           cc_is_dupd = $6, cc_is_caf = $7 } }
 
@@ -701,7 +758,7 @@ cc_caf  :: { IsCafCC }
 -------------------------------------------------------------------
 
 src_loc :: { SrcLoc }
-src_loc :                              {% getSrcLocIf }
+src_loc :                              {% getSrcLocP }
 
 checkVersion :: { () }
           : {-empty-}                  {% checkVersion Nothing }
@@ -711,10 +768,13 @@ checkVersion :: { () }
 
 --                     Haskell code 
 {
+happyError :: P a
+happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
 
 data IfaceStuff = PIface       EncodedFS{-.hi module name-} ParsedIface
                | PIdInfo       [HsIdInfo RdrName]
                | PType         RdrNameHsType
+               | PRules        [RdrNameRuleDecl]
 
 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc
 }