<commit_before>%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
\section[RnSource]{Main pass of renamer}

\begin{code}
module RnSource ( 
	rnSrcDecls, checkModDeprec,
	rnTyClDecl, rnIfaceRuleDecl, rnInstDecl, 
	rnBinds, rnBindsAndThen, rnStats,
    ) where

#include "HsVersions.h"

import HsSyn
import RdrName		( RdrName, isRdrDataCon, elemRdrEnv )
import RdrHsSyn		( RdrNameConDecl, RdrNameTyClDecl, 
			  RdrNameDeprecation, RdrNameFixitySig,
			  RdrNameHsBinds,
			  extractGenericPatTyVars
			)
import RnHsSyn
import HsCore
import RnExpr		( rnExpr )
import RnTypes		( rnHsType, rnHsSigType, rnHsTypeFVs, rnContext )

import RnBinds		( rnTopMonoBinds, rnMonoBinds, rnMethodBinds, 
			  rnMonoBindsAndThen, renameSigs, checkSigs )
import RnEnv		( lookupTopBndrRn, lookupOccRn, lookupSysBndr,
			  newLocalsRn, lookupGlobalOccRn,
			  bindLocalsFVRn, bindPatSigTyVars,
			  bindTyVarsRn, extendTyVarEnvFVRn,
			  bindCoreLocalRn, bindCoreLocalsRn, bindLocalNames,
			  checkDupOrQualNames, checkDupNames, mapFvRn,
			  lookupTopSrcBndr_maybe, lookupTopSrcBndr,
			  dataTcOccs, unknownNameErr
			)
import TcRnMonad

import BasicTypes	( FixitySig(..) )
import HscTypes		( ExternalPackageState(..), FixityEnv, 
			  Deprecations(..), plusDeprecs )
import Module		( moduleEnvElts )
import Class		( FunDep, DefMeth (..) )
import TyCon		( DataConDetails(..), visibleDataCons )
import Name		( Name )
import NameSet
import NameEnv
import ErrUtils		( dumpIfSet )
import PrelNames	( newStablePtrName, bindIOName, returnIOName )
import List		( partition )
import Bag		( bagToList )
import Outputable
import SrcLoc		( SrcLoc )
import CmdLineOpts	( DynFlag(..) )
				-- Warn of unused for-all'd tyvars
import Maybes		( maybeToBool, seqMaybe )
import Maybe            ( maybe, catMaybes, isNothing )
\end{code}

@rnSourceDecl@ `renames' declarations.
It simultaneously performs dependency analysis and precedence parsing.
It also does the following error checks:
\begin{enumerate}
\item
Checks that tyvars are used properly. This includes checking
for undefined tyvars, and tyvars in contexts that are ambiguous.
(Some of this checking has now been moved to module @TcMonoType@,
since we don't have functional dependency information at this point.)
\item
Checks that all variable occurences are defined.
\item 
Checks the @(..)@ etc constraints in the export list.
\end{enumerate}


\begin{code}
rnSrcDecls :: HsGroup RdrName -> RnM (TcGblEnv, HsGroup Name, FreeVars)

rnSrcDecls (HsGroup { hs_valds  = MonoBind binds sigs _,
		      hs_tyclds = tycl_decls,
		      hs_instds = inst_decls,
		      hs_fixds  = fix_decls,
		      hs_depds  = deprec_decls,
		      hs_fords  = foreign_decls,
		      hs_defds  = default_decls,
		      hs_ruleds = rule_decls,
		      hs_coreds = core_decls })

 = do {		-- Deal with deprecations (returns only the extra deprecations)
	deprecs <- rnSrcDeprecDecls deprec_decls ;
	updGblEnv (\gbl -> gbl { tcg_deprecs = tcg_deprecs gbl `plusDeprecs` deprecs })
		  $ do {

		-- Deal with top-level fixity decls 
		-- (returns the total new fixity env)
	fix_env <- rnSrcFixityDecls fix_decls ;
	updGblEnv (\gbl -> gbl { tcg_fix_env = fix_env })
		  $ do {

	failIfErrsM ;	-- No point in continuing if (say) we have duplicate declarations

		-- Rename other declarations
	(rn_val_decls, src_fvs1)     <- rnTopMonoBinds binds sigs ;
	(rn_inst_decls, src_fvs2)    <- mapFvRn rnSrcInstDecl inst_decls ;
	(rn_tycl_decls, src_fvs3)    <- mapFvRn rnSrcTyClDecl tycl_decls ;
	(rn_rule_decls, src_fvs4)    <- mapFvRn rnHsRuleDecl rule_decls ;
	(rn_foreign_decls, src_fvs5) <- mapFvRn rnHsForeignDecl foreign_decls ;
	(rn_default_decls, src_fvs6) <- mapFvRn rnDefaultDecl default_decls ;
	(rn_core_decls,    src_fvs7) <- mapFvRn rnCoreDecl core_decls ;
	
	let {
	   rn_group = HsGroup { hs_valds  = rn_val_decls,
			    	hs_tyclds = rn_tycl_decls,
			    	hs_instds = rn_inst_decls,
			    	hs_fixds  = [],
			    	hs_depds  = [],
			    	hs_fords  = rn_foreign_decls,
			    	hs_defds  = rn_default_decls,
			    	hs_ruleds = rn_rule_decls,
			    	hs_coreds = rn_core_decls } ;
	   src_fvs = plusFVs [src_fvs1, src_fvs2, src_fvs3, src_fvs4, 
			      src_fvs5, src_fvs6, src_fvs7] } ;

	tcg_env <- getGblEnv ;
	return (tcg_env, rn_group, src_fvs)
    }}}
\end{code}


%*********************************************************
%*						 	 *
	Source-code fixity declarations
%*							 *
%*********************************************************

\begin{code}
rnSrcFixityDecls :: [RdrNameFixitySig] -> TcRn m FixityEnv
rnSrcFixityDecls fix_decls
  = getGblEnv					`thenM` \ gbl_env ->
    foldlM rnFixityDecl (tcg_fix_env gbl_env) 
	    fix_decls				`thenM` \ fix_env ->
    traceRn (text "fixity env" <+> ppr fix_env)	`thenM_`
    returnM fix_env

rnFixityDecl :: FixityEnv -> RdrNameFixitySig -> TcRn m FixityEnv
rnFixityDecl fix_env (FixitySig rdr_name fixity loc)
  =	-- GHC extension: look up both the tycon and data con 
	-- for con-like things
	-- If neither are in scope, report an error; otherwise
	-- add both to the fixity env
     mappM lookupTopSrcBndr_maybe (dataTcOccs rdr_name)	`thenM` \ maybe_ns ->
     case catMaybes maybe_ns of<commit_msg>[project @ 2002-10-18 13:36:17 by simonpj] Add a trace<commit_after>%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
\section[RnSource]{Main pass of renamer}

\begin{code}
module RnSource ( 
	rnSrcDecls, checkModDeprec,
	rnTyClDecl, rnIfaceRuleDecl, rnInstDecl, 
	rnBinds, rnBindsAndThen, rnStats,
    ) where

#include "HsVersions.h"

import HsSyn
import RdrName		( RdrName, isRdrDataCon, elemRdrEnv )
import RdrHsSyn		( RdrNameConDecl, RdrNameTyClDecl, 
			  RdrNameDeprecation, RdrNameFixitySig,
			  RdrNameHsBinds,
			  extractGenericPatTyVars
			)
import RnHsSyn
import HsCore
import RnExpr		( rnExpr )
import RnTypes		( rnHsType, rnHsSigType, rnHsTypeFVs, rnContext )

import RnBinds		( rnTopMonoBinds, rnMonoBinds, rnMethodBinds, 
			  rnMonoBindsAndThen, renameSigs, checkSigs )
import RnEnv		( lookupTopBndrRn, lookupOccRn, lookupSysBndr,
			  newLocalsRn, lookupGlobalOccRn,
			  bindLocalsFVRn, bindPatSigTyVars,
			  bindTyVarsRn, extendTyVarEnvFVRn,
			  bindCoreLocalRn, bindCoreLocalsRn, bindLocalNames,
			  checkDupOrQualNames, checkDupNames, mapFvRn,
			  lookupTopSrcBndr_maybe, lookupTopSrcBndr,
			  dataTcOccs, unknownNameErr
			)
import TcRnMonad

import BasicTypes	( FixitySig(..) )
import HscTypes		( ExternalPackageState(..), FixityEnv, 
			  Deprecations(..), plusDeprecs )
import Module		( moduleEnvElts )
import Class		( FunDep, DefMeth (..) )
import TyCon		( DataConDetails(..), visibleDataCons )
import Name		( Name )
import NameSet
import NameEnv
import ErrUtils		( dumpIfSet )
import PrelNames	( newStablePtrName, bindIOName, returnIOName )
import List		( partition )
import Bag		( bagToList )
import Outputable
import SrcLoc		( SrcLoc )
import CmdLineOpts	( DynFlag(..) )
				-- Warn of unused for-all'd tyvars
import Maybes		( maybeToBool, seqMaybe )
import Maybe            ( maybe, catMaybes, isNothing )
\end{code}

@rnSourceDecl@ `renames' declarations.
It simultaneously performs dependency analysis and precedence parsing.
It also does the following error checks:
\begin{enumerate}
\item
Checks that tyvars are used properly. This includes checking
for undefined tyvars, and tyvars in contexts that are ambiguous.
(Some of this checking has now been moved to module @TcMonoType@,
since we don't have functional dependency information at this point.)
\item
Checks that all variable occurences are defined.
\item 
Checks the @(..)@ etc constraints in the export list.
\end{enumerate}


\begin{code}
rnSrcDecls :: HsGroup RdrName -> RnM (TcGblEnv, HsGroup Name, FreeVars)

rnSrcDecls (HsGroup { hs_valds  = MonoBind binds sigs _,
		      hs_tyclds = tycl_decls,
		      hs_instds = inst_decls,
		      hs_fixds  = fix_decls,
		      hs_depds  = deprec_decls,
		      hs_fords  = foreign_decls,
		      hs_defds  = default_decls,
		      hs_ruleds = rule_decls,
		      hs_coreds = core_decls })

 = do {		-- Deal with deprecations (returns only the extra deprecations)
	deprecs <- rnSrcDeprecDecls deprec_decls ;
	updGblEnv (\gbl -> gbl { tcg_deprecs = tcg_deprecs gbl `plusDeprecs` deprecs })
		  $ do {

		-- Deal with top-level fixity decls 
		-- (returns the total new fixity env)
	fix_env <- rnSrcFixityDecls fix_decls ;
	updGblEnv (\gbl -> gbl { tcg_fix_env = fix_env })
		  $ do {

	failIfErrsM ;	-- No point in continuing if (say) we have duplicate declarations

		-- Rename other declarations
	(rn_val_decls, src_fvs1)     <- rnTopMonoBinds binds sigs ;
	(rn_inst_decls, src_fvs2)    <- mapFvRn rnSrcInstDecl inst_decls ;
	(rn_tycl_decls, src_fvs3)    <- mapFvRn rnSrcTyClDecl tycl_decls ;
	(rn_rule_decls, src_fvs4)    <- mapFvRn rnHsRuleDecl rule_decls ;
	(rn_foreign_decls, src_fvs5) <- mapFvRn rnHsForeignDecl foreign_decls ;
	(rn_default_decls, src_fvs6) <- mapFvRn rnDefaultDecl default_decls ;
	(rn_core_decls,    src_fvs7) <- mapFvRn rnCoreDecl core_decls ;
	
	let {
	   rn_group = HsGroup { hs_valds  = rn_val_decls,
			    	hs_tyclds = rn_tycl_decls,
			    	hs_instds = rn_inst_decls,
			    	hs_fixds  = [],
			    	hs_depds  = [],
			    	hs_fords  = rn_foreign_decls,
			    	hs_defds  = rn_default_decls,
			    	hs_ruleds = rn_rule_decls,
			    	hs_coreds = rn_core_decls } ;
	   src_fvs = plusFVs [src_fvs1, src_fvs2, src_fvs3, src_fvs4, 
			      src_fvs5, src_fvs6, src_fvs7] } ;

	traceRn (text "rnSrcDecls" <+> ppr (nameSetToList src_fvs)) ;
	tcg_env <- getGblEnv ;
	return (tcg_env, rn_group, src_fvs)
    }}}
\end{code}


%*********************************************************
%*						 	 *
	Source-code fixity declarations
%*							 *
%*********************************************************

\begin{code}
rnSrcFixityDecls :: [RdrNameFixitySig] -> TcRn m FixityEnv
rnSrcFixityDecls fix_decls
  = getGblEnv					`thenM` \ gbl_env ->
    foldlM rnFixityDecl (tcg_fix_env gbl_env) 
	    fix_decls				`thenM` \ fix_env ->
    traceRn (text "fixity env" <+> ppr fix_env)	`thenM_`
    returnM fix_env

rnFixityDecl :: FixityEnv -> RdrNameFixitySig -> TcRn m FixityEnv
rnFixityDecl fix_env (FixitySig rdr_name fixity loc)
  =	-- GHC extension: look up both the tycon and data con 
	-- for con-like things
	-- If neither are in scope, report an error; otherwise
	-- add both to the fixity env
     mappM lookupTopSrcBndr_maybe (dataTcOccs rdr_name)	`thenM` \ maybe_ns ->
     case catMaybes maybe_ns of
{'fraction_non_alphanumeric': 0.10403133358807795, 'fraction_numerical': 0.00554069545280856, 'mean_word_length': 3.7716499544211484, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 24, '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': '2208877', 'n_tokens_mistral': 4383, 'n_tokens_neox': 3981, 'n_words': 938}