<commit_before>%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
%
\section[MkIface]{Print an interface for a module}

\begin{code}
module MkIface (
	startIface, endIface, ifaceDecls
    ) where

#include "HsVersions.h"

import IO		( Handle, hPutStr, openFile, 
			  hClose, hPutStrLn, IOMode(..) )

import HsSyn
import BasicTypes	( Fixity(..), FixityDirection(..), NewOrData(..) )
import RnMonad
import RnEnv		( availName )

import TcInstUtil	( InstInfo(..) )

import CmdLineOpts
import Id		( Id, idType, idInfo, omitIfaceSigForId, isUserExportedId,
			  idSpecialisation
			)
import Var		( isId )
import VarSet
import DataCon		( StrictnessMark(..), dataConSig, dataConFieldLabels, dataConStrictMarks )
import IdInfo		( IdInfo, StrictnessInfo(..), ArityInfo, InlinePragInfo(..), inlinePragInfo,
			  arityInfo, ppArityInfo, arityLowerBound,
			  strictnessInfo, ppStrictnessInfo, isBottomingStrictness,
			  cafInfo, ppCafInfo, specInfo,
			  cprInfo, ppCprInfo, pprInlinePragInfo,
			  occInfo, OccInfo(..),
			  workerExists, workerInfo, ppWorkerInfo, WorkerInfo(..)
			)
import CoreSyn		( CoreExpr, CoreBind, Bind(..), rulesRules, rulesRhsFreeVars )
import CoreFVs		( exprSomeFreeVars, ruleSomeLhsFreeVars, ruleSomeFreeVars )
import CoreUnfold	( okToUnfoldInHiFile, couldBeSmallEnoughToInline )
import Module		( moduleString, pprModule, pprModuleName )
import Name		( isLocallyDefined, isWiredInName, nameRdrName, nameModule,
			  Name, NamedThing(..)
			)
import OccName		( OccName, pprOccName )
import TyCon		( TyCon, getSynTyConDefn, isSynTyCon, isNewTyCon, isAlgTyCon,
			  tyConTheta, tyConTyVars, tyConDataCons
			)
import Class		( Class, classExtraBigSig )
import FieldLabel	( fieldLabelName, fieldLabelType )
import Type		( mkSigmaTy, splitSigmaTy, mkDictTy, tidyTopType,
			  deNoteType, classesToPreds,
			  Type, ThetaType, PredType(..), ClassContext
		        )

import PprType
import PprCore		( pprIfaceUnfolding, pprCoreRule )
import FunDeps		( pprFundeps )
import Rules		( pprProtoCoreRule, ProtoCoreRule(..) )

import Bag		( bagToList, isEmptyBag )
import Maybes		( catMaybes, maybeToBool )
import FiniteMap	( emptyFM, addToFM, addToFM_C, fmToList, FiniteMap )
import UniqFM		( lookupUFM, listToUFM )
import UniqSet		( uniqSetToList )
import Util		( sortLt, mapAccumL )
import Bag
import Outputable
\end{code}

We have a function @startIface@ to open the output file and put
(something like) ``interface Foo'' in it.  It gives back a handle
for subsequent additions to the interface file.

We then have one-function-per-block-of-interface-stuff, e.g.,
@ifaceExportList@ produces the @__exports__@ section; it appends
to the handle provided by @startIface@.

NOTE: ALWAYS remember that ghc-iface.lprl rewrites the interface file,
so you have to keep it in synch with the code below. Otherwise you'll
lose the happiest years of your life, believe me...  -- SUP

\begin{code}
startIface  :: Module -> InterfaceDetails
	    -> IO (Maybe Handle) -- Nothing <=> don't do an interface

ifaceDecls :: Maybe Handle
	   -> [TyCon] -> [Class]
	   -> Bag InstInfo 
	   -> [Id]		-- Ids used at code-gen time; they have better pragma info!
	   -> [CoreBind]	-- In dependency order, later depend on earlier
	   -> [ProtoCoreRule]	-- Rules
	   -> [Deprecation Name]
	   -> IO ()

endIface    :: Maybe Handle -> IO ()
\end{code}

\begin{code}
startIface mod (InterfaceDetails has_orphans import_usages (ExportEnv avails fixities _) _)
  = case opt_ProduceHi of
      Nothing -> return Nothing ; -- not producing any .hi file

      Just fn -> do 
	if_hdl <- openFile fn WriteMode
	hPutStr		if_hdl ("__interface \"" ++ show opt_InPackage ++ "\" " ++ moduleString mod)
	hPutStr		if_hdl (' ' : show (opt_HiVersion :: Int))
	hPutStr		if_hdl (' ' : orphan_indicator)
	hPutStrLn	if_hdl " where"
	ifaceExports	if_hdl avails
	ifaceImports	if_hdl import_usages
	ifaceFixities	if_hdl fixities
	return (Just if_hdl)
  where
    orphan_indicator | has_orphans = " !"
		     | otherwise   = ""

endIface Nothing	= return ()
endIface (Just if_hdl)	= hPutStr if_hdl "\n" >> hClose if_hdl
\end{code}


\begin{code}<commit_msg>[project @ 2000-04-14 09:37:03 by simonmar] Don't bother printing the HiVersion in the __interface line (it's added by ghc-iface later on anyway).<commit_after>%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
%
\section[MkIface]{Print an interface for a module}

\begin{code}
module MkIface (
	startIface, endIface, ifaceDecls
    ) where

#include "HsVersions.h"

import IO		( Handle, hPutStr, openFile, 
			  hClose, hPutStrLn, IOMode(..) )

import HsSyn
import BasicTypes	( Fixity(..), FixityDirection(..), NewOrData(..) )
import RnMonad
import RnEnv		( availName )

import TcInstUtil	( InstInfo(..) )

import CmdLineOpts
import Id		( Id, idType, idInfo, omitIfaceSigForId, isUserExportedId,
			  idSpecialisation
			)
import Var		( isId )
import VarSet
import DataCon		( StrictnessMark(..), dataConSig, dataConFieldLabels, dataConStrictMarks )
import IdInfo		( IdInfo, StrictnessInfo(..), ArityInfo, InlinePragInfo(..), inlinePragInfo,
			  arityInfo, ppArityInfo, arityLowerBound,
			  strictnessInfo, ppStrictnessInfo, isBottomingStrictness,
			  cafInfo, ppCafInfo, specInfo,
			  cprInfo, ppCprInfo, pprInlinePragInfo,
			  occInfo, OccInfo(..),
			  workerExists, workerInfo, ppWorkerInfo, WorkerInfo(..)
			)
import CoreSyn		( CoreExpr, CoreBind, Bind(..), rulesRules, rulesRhsFreeVars )
import CoreFVs		( exprSomeFreeVars, ruleSomeLhsFreeVars, ruleSomeFreeVars )
import CoreUnfold	( okToUnfoldInHiFile, couldBeSmallEnoughToInline )
import Module		( moduleString, pprModule, pprModuleName )
import Name		( isLocallyDefined, isWiredInName, nameRdrName, nameModule,
			  Name, NamedThing(..)
			)
import OccName		( OccName, pprOccName )
import TyCon		( TyCon, getSynTyConDefn, isSynTyCon, isNewTyCon, isAlgTyCon,
			  tyConTheta, tyConTyVars, tyConDataCons
			)
import Class		( Class, classExtraBigSig )
import FieldLabel	( fieldLabelName, fieldLabelType )
import Type		( mkSigmaTy, splitSigmaTy, mkDictTy, tidyTopType,
			  deNoteType, classesToPreds,
			  Type, ThetaType, PredType(..), ClassContext
		        )

import PprType
import PprCore		( pprIfaceUnfolding, pprCoreRule )
import FunDeps		( pprFundeps )
import Rules		( pprProtoCoreRule, ProtoCoreRule(..) )

import Bag		( bagToList, isEmptyBag )
import Maybes		( catMaybes, maybeToBool )
import FiniteMap	( emptyFM, addToFM, addToFM_C, fmToList, FiniteMap )
import UniqFM		( lookupUFM, listToUFM )
import UniqSet		( uniqSetToList )
import Util		( sortLt, mapAccumL )
import Bag
import Outputable
\end{code}

We have a function @startIface@ to open the output file and put
(something like) ``interface Foo'' in it.  It gives back a handle
for subsequent additions to the interface file.

We then have one-function-per-block-of-interface-stuff, e.g.,
@ifaceExportList@ produces the @__exports__@ section; it appends
to the handle provided by @startIface@.

NOTE: ALWAYS remember that ghc-iface.lprl rewrites the interface file,
so you have to keep it in synch with the code below. Otherwise you'll
lose the happiest years of your life, believe me...  -- SUP

\begin{code}
startIface  :: Module -> InterfaceDetails
	    -> IO (Maybe Handle) -- Nothing <=> don't do an interface

ifaceDecls :: Maybe Handle
	   -> [TyCon] -> [Class]
	   -> Bag InstInfo 
	   -> [Id]		-- Ids used at code-gen time; they have better pragma info!
	   -> [CoreBind]	-- In dependency order, later depend on earlier
	   -> [ProtoCoreRule]	-- Rules
	   -> [Deprecation Name]
	   -> IO ()

endIface    :: Maybe Handle -> IO ()
\end{code}

\begin{code}
startIface mod (InterfaceDetails has_orphans import_usages (ExportEnv avails fixities _) _)
  = case opt_ProduceHi of
      Nothing -> return Nothing ; -- not producing any .hi file

      Just fn -> do 
	if_hdl <- openFile fn WriteMode
	hPutStr		if_hdl ("__interface \"" ++ show opt_InPackage ++ "\" " ++ moduleString mod)
	hPutStr		if_hdl (' ' : orphan_indicator)
	hPutStrLn	if_hdl " where"
	ifaceExports	if_hdl avails
	ifaceImports	if_hdl import_usages
	ifaceFixities	if_hdl fixities
	return (Just if_hdl)
  where
    orphan_indicator | has_orphans = " !"
		     | otherwise   = ""

endIface Nothing	= return ()
endIface (Just if_hdl)	= hPutStr if_hdl "\n" >> hClose if_hdl
\end{code}


\begin{code}
{'fraction_non_alphanumeric': 0.09819927971188476, 'fraction_numerical': 0.003601440576230492, 'mean_word_length': 4.076782449725777, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '335199', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2944, 'n_words': 818}