-newImportedGlobalName :: Module -> OccName
- -> IfaceFlavour
- -> RnM s d Name
-newImportedGlobalName mod occ hif
- = -- First check the cache
- getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
- let
- key = (mod,occ)
- prov = NonLocalDef noSrcLoc hif False
- in
- case lookupFM cache key of
-
- -- A hit in the cache!
- -- If it has no provenance at the moment then set its provenance
- -- so that it has the right HiFlag component.
- -- (This is necessary
- -- for known-key things. For example, GHCmain.lhs imports as SOURCE
- -- Main; but Main.main is a known-key thing.)
- -- Don't fiddle with the provenance if it already has one
- Just name -> case getNameProvenance name of
- NoProvenance -> let
- new_name = setNameProvenance name prov
- new_cache = addToFM cache key new_name
- in
- setNameSupplyRn (us, inst_ns, new_cache) `thenRn_`
- returnRn new_name
- other -> returnRn name
-
- Nothing -> -- Miss in the cache!
- -- Build a new original name, and put it in the cache
- let
- (us', us1) = splitUniqSupply us
- uniq = getUnique us1
- name = mkGlobalName uniq mod occ prov
- new_cache = addToFM cache key name
- in
- setNameSupplyRn (us', inst_ns, new_cache) `thenRn_`
- returnRn name
-
-{-
- let
- pprC ((mod,occ),name) = pprModule mod <> text "." <> pprOccName occ <+> text "--->"
- <+> ppr name
- in
- pprTrace "ng" (vcat [text "newGlobalName miss" <+> pprModule mod <+> pprOccName occ,
- brackets (sep (map pprC (fmToList cache))),
- text ""
- ]) $
--}
-
-
-newLocallyDefinedGlobalName :: Module -> OccName
- -> (Name -> ExportFlag) -> SrcLoc
- -> RnM s d Name
-newLocallyDefinedGlobalName mod occ rec_exp_fn loc
- = -- First check the cache
- getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
- let
- key = (mod,occ)
- in
- case lookupFM cache key of
-
- -- A hit in the cache!
- -- Overwrite whatever provenance is in the cache already;
- -- this updates WiredIn things and known-key things,
- -- which are there from the start, to LocalDef.
- Just name -> let
- new_name = setNameProvenance name (LocalDef loc (rec_exp_fn new_name))
- new_cache = addToFM cache key new_name
- in
- setNameSupplyRn (us, inst_ns, new_cache) `thenRn_`
- returnRn new_name
-
- -- Miss in the cache!
- -- Build a new original name, and put it in the cache
- Nothing -> let
- provenance = LocalDef loc (rec_exp_fn new_name)
- (us', us1) = splitUniqSupply us
- uniq = getUnique us1
- new_name = mkGlobalName uniq mod occ provenance
- new_cache = addToFM cache key new_name
- in
- setNameSupplyRn (us', inst_ns, new_cache) `thenRn_`
- returnRn new_name
-
-
--- newDfunName is a variant, specially for dfuns.
--- When renaming derived definitions we are in *interface* mode (because we can trip
--- over original names), but we still want to make the Dfun locally-defined.
--- So we can't use whether or not we're in source mode to decide the locally-defined question.
-newDfunName :: Maybe RdrName -> SrcLoc -> RnMS s Name
-newDfunName Nothing src_loc -- Local instance decls have a "Nothing"
- = getModuleRn `thenRn` \ mod_name ->
- newInstUniq `thenRn` \ inst_uniq ->
- let
- dfun_occ = VarOcc (_PK_ ("$d" ++ show inst_uniq))
- in
- newLocallyDefinedGlobalName mod_name dfun_occ
- (\_ -> Exported) src_loc
-
-newDfunName (Just n) src_loc -- Imported ones have "Just n"
- = getModuleRn `thenRn` \ mod_name ->
- newImportedGlobalName mod_name (rdrNameOcc n) HiFile {- Correct? -}
-
-
-newLocalNames :: [(RdrName,SrcLoc)] -> RnM s d [Name]
-newLocalNames rdr_names
- = getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
- let
- n = length rdr_names
- (us', us1) = splitUniqSupply us
- uniqs = getUniques n us1
- locals = [ mkLocalName uniq (rdrNameOcc rdr_name) loc
- | ((rdr_name,loc), uniq) <- rdr_names `zip` uniqs
- ]
- in
- setNameSupplyRn (us', inst_ns, cache) `thenRn_`
- returnRn locals
-
--- mkUnboundName makes a place-holder Name; it shouldn't be looked at except possibly
--- during compiler debugging.
-mkUnboundName :: RdrName -> Name
-mkUnboundName rdr_name = mkLocalName unboundKey (rdrNameOcc rdr_name) noSrcLoc
-
-isUnboundName :: Name -> Bool
-isUnboundName name = uniqueOf name == unboundKey
-\end{code}
-
-\begin{code}
-bindLocatedLocalsRn :: SDoc -- Documentation string for error message
- -> [(RdrName,SrcLoc)]
- -> ([Name] -> RnMS s a)
- -> RnMS s a
-bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
- = checkDupOrQualNames doc_str rdr_names_w_loc `thenRn_`
-
- getLocalNameEnv `thenRn` \ name_env ->
- (if opt_WarnNameShadowing
- then
- mapRn (check_shadow name_env) rdr_names_w_loc
- else
- returnRn []
- ) `thenRn_`
-
- newLocalNames rdr_names_w_loc `thenRn` \ names ->
- let
- new_name_env = addListToFM name_env (map fst rdr_names_w_loc `zip` names)
- in
- setLocalNameEnv new_name_env (enclosed_scope names)
- where
- check_shadow name_env (rdr_name,loc)
- = case lookupFM name_env rdr_name of
- Nothing -> returnRn ()
- Just name -> pushSrcLocRn loc $
- addWarnRn (shadowedNameWarn rdr_name)
-
-bindLocalsRn doc_str rdr_names enclosed_scope
- = getSrcLocRn `thenRn` \ loc ->
- bindLocatedLocalsRn (text doc_str)
- (rdr_names `zip` repeat loc)
- enclosed_scope
-
-bindTyVarsRn doc_str tyvar_names enclosed_scope
- = getSrcLocRn `thenRn` \ loc ->
- let
- located_tyvars = [(getTyVarName tv, loc) | tv <- tyvar_names]
- in
- bindLocatedLocalsRn doc_str located_tyvars $ \ names ->
- enclosed_scope (zipWith replaceTyVarName tyvar_names names)
-
- -- Works in any variant of the renamer monad
-checkDupOrQualNames, checkDupNames :: SDoc
- -> [(RdrName, SrcLoc)]
- -> RnM s d ()