module Language.Haskell.Refact.Utils.TypeUtils
(
inScopeInfo, isInScopeAndUnqualified, isInScopeAndUnqualifiedGhc, inScopeNames
, isExported, isExplicitlyExported, modIsExported
, equivalentNameInNewMod
, hsQualifier
,isVarId,isConId,isOperator,isTopLevelPN,isLocalPN,isNonLibraryName
,isQualifiedPN, isFunOrPatName, isTypeSig, isTypeSigDecl
,isFunBindP,isFunBindR,isPatBindP,isPatBindR,isSimplePatBind,isSimplePatDecl
,isComplexPatBind,isComplexPatDecl,isFunOrPatBindP,isFunOrPatBindR
, findEntity'
, findIdForName
, getTypeForName
,definesTypeSigRdr
,sameBindRdr
,UsedByRhs(..)
, isMainModule
, getModule
,defineLoc, useLoc, locToExp
, locToRdrName
,getName
,addDecl, addItemsToImport, addItemsToExport, addHiding
,addParamsToDecls, addParamsToSigs, addActualParamsToRhs, addImportDecl, duplicateDecl
,rmDecl, rmTypeSig, rmTypeSigs
, rmQualifier, qualifyToplevelName, renamePN, HowToQual(..), autoRenameLocalVar
, expToNameRdr
,nameToString
,patToNameRdr
, pNtoPat
, usedWithoutQualR
, divideDecls
, mkRdrName,mkQualifiedRdrName,mkNewGhcName,mkNewName,mkNewToplevelName
, registerRdrName
, causeNameClashInExports
, declsSybTransform
, rdrNameFromName
) where
import Control.Monad.State
import Data.Char
import Data.Foldable
import Data.List
import Data.Maybe
import Exception
import Language.Haskell.Refact.Utils.ExactPrint
import Language.Haskell.Refact.Utils.GhcUtils
import Language.Haskell.Refact.Utils.GhcVersionSpecific
import Language.Haskell.Refact.Utils.LocUtils
import Language.Haskell.Refact.Utils.Monad
import Language.Haskell.Refact.Utils.MonadFunctions
import Language.Haskell.Refact.Utils.TypeSyn
import Language.Haskell.Refact.Utils.Types
import Language.Haskell.Refact.Utils.Variables
import Language.Haskell.GHC.ExactPrint
import Language.Haskell.GHC.ExactPrint.Types
import Language.Haskell.GHC.ExactPrint.Utils
import qualified FastString as GHC
import qualified GHC as GHC
import qualified Module as GHC
import qualified Name as GHC
import qualified Outputable as GHC
import qualified RdrName as GHC
import qualified TyCon as GHC
#if __GLASGOW_HASKELL__ <= 710
import qualified TypeRep as GHC
#else
import qualified TyCoRep as GHC
import qualified BasicTypes as GHC
#endif
import qualified Unique as GHC
import qualified Var as GHC
import qualified Var as Var
import qualified Data.Generics as SYB
import qualified GHC.SYB.Utils as SYB
import qualified Data.Map as Map
import Data.Generics.Strafunski.StrategyLib.StrategyLib hiding (liftIO,MonadPlus,mzero)
inScopeInfo :: InScopes
->[(String, GHC.NameSpace, GHC.ModuleName, Maybe GHC.ModuleName)]
inScopeInfo names = nub $ map getEntInfo $ names
where
getEntInfo name
=(showGhc name,
GHC.occNameSpace $ GHC.nameOccName name,
GHC.moduleName $ GHC.nameModule name,
getQualMaybe $ GHC.nameRdrName name)
getQualMaybe rdrName = case rdrName of
GHC.Qual modName _occName -> Just modName
_ -> Nothing
isInScopeAndUnqualified::String
->InScopes
->Bool
isInScopeAndUnqualified n names
= isJust $ find (\ (x, _,_, qual) -> x == n && isNothing qual ) $ inScopeInfo names
isInScopeAndUnqualifiedGhc ::
String
-> (Maybe GHC.Name)
-> RefactGhc Bool
isInScopeAndUnqualifiedGhc n maybeExising = do
names <- ghandle handler (GHC.parseName n)
logm $ "isInScopeAndUnqualifiedGhc:(n,(maybeExising,names))=" ++ (show n) ++ ":" ++ (showGhc (maybeExising,names))
ctx <- GHC.getContext
logm $ "isInScopeAndUnqualifiedGhc:ctx=" ++ (showGhc ctx)
let nameList = case maybeExising of
Nothing -> names
Just n' -> filter (\x -> (showGhcQual x) /= (showGhcQual n')) names
logm $ "isInScopeAndUnqualifiedGhc:(n,nameList)=" ++ (show n) ++ ":" ++ (showGhc nameList)
return $ nameList /= []
where
handler:: SomeException -> RefactGhc [GHC.Name]
handler e = do
logm $ "isInScopeAndUnqualifiedGhc.handler e=" ++ (show e)
return []
inScopeNames :: String
-> RefactGhc [GHC.Name]
inScopeNames n = do
names <- ghandle handler (GHC.parseName n)
logm $ "inScopeNames:(n,names)=" ++ (show n) ++ ":" ++ (showGhc names)
return $ names
where
handler:: SomeException -> RefactGhc [GHC.Name]
handler e = do
logm $ "inScopeNames.handler e=" ++ (show e)
return []
equivalentNameInNewMod :: GHC.Name -> RefactGhc [GHC.Name]
equivalentNameInNewMod old = do
let eqModules (GHC.Module pk1 mn1) (GHC.Module pk2 mn2) = mn1 == mn2 && pk1 == pk2
gnames <- GHC.getNamesInScope
logm $ "equivalentNameInNewMod:gnames=" ++ showGhcQual (map (\n -> (GHC.nameModule n,n)) gnames)
let clientModule = GHC.nameModule old
logm $ "equivalentNameInNewMod:(old,clientModule)=" ++ showGhcQual (old,clientModule)
let clientInscopes = filter (\n -> clientModule == GHC.nameModule n) gnames
let clientInscopes = filter (\n -> eqModules clientModule (GHC.nameModule n)) gnames
logm $ "equivalentNameInNewMod:clientInscopes=" ++ showGhcQual clientInscopes
let newNames = filter (\n -> showGhcQual n == showGhcQual old) clientInscopes
return newNames
hsQualifier :: GHC.Name
-> RefactGhc [GHC.ModuleName]
hsQualifier pname = do
names <- inScopeNames (showGhc pname)
let mods = map (GHC.moduleName . GHC.nameModule) names
return mods
mkQualifiedRdrName :: GHC.ModuleName -> String -> GHC.RdrName
mkQualifiedRdrName mn s = GHC.mkRdrQual mn (GHC.mkVarOcc s)
mkRdrName :: String -> GHC.RdrName
mkRdrName s = GHC.mkVarUnqual (GHC.mkFastString s)
registerRdrName :: GHC.Located GHC.RdrName -> RefactGhc ()
registerRdrName (GHC.L l rn) = do
case GHC.isQual_maybe rn of
Nothing -> do
n <- mkNewGhcName Nothing (showGhc rn)
addToNameMap l n
Just (mn,oc) -> do
#if __GLASGOW_HASKELL__ <= 710
n <- mkNewGhcName (Just (GHC.Module (GHC.stringToPackageKey "HaRe") mn)) (showGhc oc)
#else
n <- mkNewGhcName (Just (GHC.Module (GHC.stringToUnitId "HaRe") mn)) (showGhc oc)
#endif
addToNameMap l n
mkNewGhcName :: Maybe GHC.Module -> String -> RefactGhc GHC.Name
mkNewGhcName maybeMod name = do
s <- get
u <- gets rsUniqState
put s { rsUniqState = (u+1) }
return (mkNewGhcNamePure 'H' (u + 1) maybeMod name)
mkNewToplevelName :: GHC.Module -> String -> GHC.SrcSpan -> RefactGhc GHC.Name
mkNewToplevelName modid name defLoc = do
s <- get
u <- gets rsUniqState
put s { rsUniqState = (u+1) }
let un = GHC.mkUnique 'H' (u+1)
n = GHC.mkExternalName un modid (GHC.mkVarOcc name) defLoc
return n
mkNewName::String
->[String]
->Int
->String
mkNewName oldName fds suffix
=let newName=if suffix==0 then oldName
else oldName++"_"++ show suffix
in if elem newName fds
then mkNewName oldName fds (suffix+1)
else newName
modIsExported:: GHC.ModuleName
-> GHC.RenamedSource
-> Bool
modIsExported modName (_g,_emps,mexps,_mdocs)
= let
modExported (GHC.L _ (GHC.IEModuleContents (GHC.L _ name))) = name == modName
modExported _ = False
moduleExports = filter modExported $ fromMaybe [] mexps
in if isNothing mexps
then True
else (nonEmptyList moduleExports)
isExported :: GHC.Name -> RefactGhc Bool
isExported n = do
typechecked <- getTypecheckedModule
let modInfo = GHC.tm_checked_module_info typechecked
return $ GHC.modInfoIsExportedName modInfo n
isExplicitlyExported:: NameMap
-> GHC.Name
-> GHC.ParsedSource
-> Bool
isExplicitlyExported nm pn (GHC.L _ p)
= findNameInRdr nm pn (GHC.hsmodExports p)
causeNameClashInExports:: NameMap
-> GHC.Name
-> GHC.Name
-> GHC.ModuleName
-> GHC.ParsedSource
-> Bool
causeNameClashInExports nm pn newName modName parsed@(GHC.L _ p)
= let exps = GHC.unLoc $ fromMaybe (GHC.noLoc []) (GHC.hsmodExports p)
varExps = concatMap nameFromExport $ filter isImpVar exps
nameFromExport (GHC.L _ (GHC.IEVar x)) = [rdrName2NamePure nm x]
nameFromExport _ = []
withoutQual n = showGhc $ GHC.localiseName n
modNames=nub (concatMap (\x -> if withoutQual x== withoutQual newName
then [GHC.moduleName $ GHC.nameModule x]
else []) varExps)
res = (isExplicitlyExported nm pn parsed) &&
( any modIsUnQualifedImported modNames
|| elem modName modNames)
in res
where
isImpVar (GHC.L _ x) = case x of
GHC.IEVar _ -> True
_ -> False
modIsUnQualifedImported modName'
=let
in isJust $ find (\(GHC.L _ (GHC.ImportDecl _ (GHC.L _ modName1) _qualify _source _safe isQualified _isImplicit _as _h))
-> modName1 == modName' && (not isQualified)) (GHC.hsmodImports p)
usedWithoutQualR :: (SYB.Data t) => GHC.Name -> t -> Bool
usedWithoutQualR name t = isJust $ SYB.something (inName) t
where
inName :: (SYB.Typeable a) => a -> Maybe Bool
inName = nameSybQuery checkName
checkName ((GHC.L _ pn)::GHC.Located GHC.RdrName)
| ((GHC.rdrNameOcc pn) == (GHC.nameOccName name)) &&
GHC.isUnqual pn = Just True
checkName _ = Nothing
getModule :: RefactGhc GHC.Module
getModule = do
typechecked <- getTypecheckedModule
return $ GHC.ms_mod $ GHC.pm_mod_summary $ GHC.tm_parsed_module typechecked
isVarId :: String -> Bool
isVarId mid = isId mid && isSmall (ghead "isVarId" mid)
where isSmall c=isLower c || c=='_'
isConId :: String -> Bool
isConId mid = isId mid && isUpper (ghead "isConId" mid)
isOperator :: String -> Bool
isOperator mid = mid /= [] && isOpSym (ghead "isOperator" mid) &&
isLegalOpTail (tail mid) && not (isReservedOp mid)
where
isOpSym mid' = elem mid' opSymbols
where opSymbols = ['!', '#', '$', '%', '&', '*', '+','.','/','<','=','>','?','@','\'','^','|','-','~']
isLegalOpTail tail' = all isLegal tail'
where isLegal c = isOpSym c || c==':'
isReservedOp mid' = elem mid' reservedOps
where reservedOps = ["..", ":","::","=","\"", "|","<-","@","~","=>"]
isId::String->Bool
isId mid = mid/=[] && isLegalIdTail (tail mid) && not (isReservedId mid)
where
isLegalIdTail tail' = all isLegal tail'
where isLegal c=isSmall c|| isUpper c || isDigit c || c=='\''
isReservedId mid' = elem mid' reservedIds
where reservedIds=["case", "class", "data", "default", "deriving","do","else" ,"if",
"import", "in", "infix","infixl","infixr","instance","let","module",
"newtype", "of","then","type","where","_"]
isSmall c=isLower c || c=='_'
isTopLevelPN::GHC.Name -> RefactGhc Bool
isTopLevelPN n = do
typechecked <- getTypecheckedModule
let maybeNames = GHC.modInfoTopLevelScope $ GHC.tm_checked_module_info typechecked
let names = fromMaybe [] maybeNames
return $ n `elem` names
isLocalPN::GHC.Name -> Bool
isLocalPN = GHC.isInternalName
isNonLibraryName :: GHC.Name -> Bool
isNonLibraryName n = case (GHC.nameSrcSpan n) of
GHC.UnhelpfulSpan _ -> False
_ -> True
isFunOrPatName :: (SYB.Data t) => NameMap -> GHC.Name -> t -> Bool
isFunOrPatName nm pn
= isJust . SYB.something (Nothing `SYB.mkQ` worker `SYB.extQ` workerDecl)
where
worker (decl::GHC.LHsBind GHC.RdrName)
| definesRdr nm pn decl = Just True
worker _ = Nothing
workerDecl (GHC.L l (GHC.ValD decl)::GHC.LHsDecl GHC.RdrName)
| definesRdr nm pn (GHC.L l decl) = Just True
workerDecl _ = Nothing
isQualifiedPN :: GHC.Name -> RefactGhc Bool
isQualifiedPN name = return $ GHC.isQual $ GHC.nameRdrName name
isTypeSig :: GHC.LSig a -> Bool
isTypeSig (GHC.L _ (GHC.TypeSig{})) = True
isTypeSig _ = False
isTypeSigDecl :: GHC.LHsDecl a -> Bool
isTypeSigDecl (GHC.L _ (GHC.SigD (GHC.TypeSig{}))) = True
isTypeSigDecl _ = False
isFunBindP :: GHC.LHsDecl GHC.RdrName -> Bool
isFunBindP (GHC.L _ (GHC.ValD (GHC.FunBind{}))) = True
isFunBindP _ =False
isFunBindR::GHC.LHsBind t -> Bool
isFunBindR (GHC.L _l (GHC.FunBind{})) = True
isFunBindR _ =False
isPatBindP :: GHC.LHsDecl GHC.RdrName -> Bool
isPatBindP (GHC.L _ (GHC.ValD (GHC.PatBind _ _ _ _ _))) = True
isPatBindP _=False
isPatBindR :: GHC.LHsBind t -> Bool
isPatBindR (GHC.L _ (GHC.PatBind _ _ _ _ _)) = True
isPatBindR _=False
isSimplePatDecl :: GHC.LHsDecl GHC.RdrName -> Bool
isSimplePatDecl decl = case decl of
(GHC.L _l (GHC.ValD (GHC.PatBind p _rhs _ty _fvs _))) -> hsNamessRdr p /= []
_ -> False
isSimplePatBind :: (GHC.DataId t) => GHC.LHsBind t-> Bool
isSimplePatBind decl = case decl of
(GHC.L _l (GHC.PatBind p _rhs _ty _fvs _)) -> hsNamessRdr p /= []
_ -> False
isComplexPatDecl::GHC.LHsDecl name -> Bool
isComplexPatDecl (GHC.L l (GHC.ValD decl)) = isComplexPatBind (GHC.L l decl)
isComplexPatDecl _ = False
isComplexPatBind::GHC.LHsBind name -> Bool
isComplexPatBind decl
= case decl of
(GHC.L _l (GHC.PatBind (GHC.L _ (GHC.VarPat _)) _rhs _ty _fvs _)) -> True
_ -> False
isFunOrPatBindP :: HsDeclP -> Bool
isFunOrPatBindP decl = isFunBindP decl || isPatBindP decl
isFunOrPatBindR :: GHC.LHsBind t -> Bool
isFunOrPatBindR decl = isFunBindR decl || isPatBindR decl
findEntity':: (SYB.Data a, SYB.Data b)
=> a -> b -> Maybe (SimpPos,SimpPos)
findEntity' a b = res
where
res = SYB.somethingStaged SYB.Parser Nothing worker b
worker :: (SYB.Data c)
=> c -> Maybe (SimpPos,SimpPos)
worker x = if SYB.typeOf a == SYB.typeOf x
then Just (getStartEndLoc x)
else Nothing
sameBindRdr :: NameMap -> GHC.LHsDecl GHC.RdrName -> GHC.LHsDecl GHC.RdrName -> Bool
sameBindRdr nm b1 b2 = (definedNamesRdr nm b1) == (definedNamesRdr nm b2)
class (SYB.Data t) => UsedByRhs t where
usedByRhsRdr :: NameMap -> t -> [GHC.Name] -> Bool
instance UsedByRhs (GHC.HsModule GHC.RdrName) where
usedByRhsRdr _ _parsed _pns = False
instance (UsedByRhs a) => UsedByRhs (GHC.Located a) where
usedByRhsRdr nm (GHC.L _ d) pns = usedByRhsRdr nm d pns
instance (UsedByRhs a) => UsedByRhs (Maybe a) where
usedByRhsRdr _ Nothing _ = False
usedByRhsRdr nm (Just a) pns = usedByRhsRdr nm a pns
instance UsedByRhs [GHC.LIE GHC.RdrName] where
usedByRhsRdr nm ds pns = or $ map (\d -> usedByRhsRdr nm d pns) ds
instance UsedByRhs (GHC.IE GHC.RdrName) where
usedByRhsRdr _ _ _ = False
instance UsedByRhs [GHC.LHsDecl GHC.RdrName] where
usedByRhsRdr nm ds pns = or $ map (\d -> usedByRhsRdr nm d pns) ds
instance UsedByRhs (GHC.HsDecl GHC.RdrName) where
usedByRhsRdr nm de pns =
case de of
GHC.TyClD d -> f d
GHC.InstD d -> f d
GHC.DerivD d -> f d
GHC.ValD d -> f d
GHC.SigD d -> f d
GHC.DefD d -> f d
GHC.ForD d -> f d
GHC.WarningD d -> f d
GHC.AnnD d -> f d
GHC.RuleD d -> f d
GHC.VectD d -> f d
GHC.SpliceD d -> f d
GHC.DocD d -> f d
GHC.RoleAnnotD d -> f d
#if __GLASGOW_HASKELL__ < 711
GHC.QuasiQuoteD d -> f d
#endif
where
f d' = usedByRhsRdr nm d' pns
instance UsedByRhs (GHC.TyClDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.InstDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.DerivDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.ForeignDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.WarnDecls GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.AnnDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.RoleAnnotDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
#if __GLASGOW_HASKELL__ <= 710
instance UsedByRhs (GHC.HsQuasiQuote GHC.RdrName) where
usedByRhsRdr = assert False undefined
#endif
instance UsedByRhs (GHC.DefaultDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.SpliceDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.VectDecl GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.RuleDecls GHC.RdrName) where
usedByRhsRdr = assert False undefined
instance UsedByRhs GHC.DocDecl where
usedByRhsRdr = assert False undefined
instance UsedByRhs (GHC.Sig GHC.RdrName) where
usedByRhsRdr _ _ _ = False
instance UsedByRhs (GHC.Match GHC.RdrName (GHC.LHsExpr GHC.RdrName)) where
usedByRhsRdr nm (GHC.Match _ _ _ (GHC.GRHSs rhs _)) pns
= findNamesRdr nm pns rhs
instance UsedByRhs (GHC.HsBind GHC.RdrName) where
#if __GLASGOW_HASKELL__ <= 710
usedByRhsRdr nm (GHC.FunBind _ _ matches _ _ _) pns = findNamesRdr nm pns matches
#else
usedByRhsRdr nm (GHC.FunBind _ matches _ _ _) pns = findNamesRdr nm pns matches
#endif
usedByRhsRdr nm (GHC.PatBind _ rhs _ _ _) pns = findNamesRdr nm pns rhs
usedByRhsRdr nm (GHC.PatSynBind (GHC.PSB _ _ _ rhs _)) pns = findNamesRdr nm pns rhs
usedByRhsRdr nm (GHC.VarBind _ rhs _) pns = findNamesRdr nm pns rhs
usedByRhsRdr _nm (GHC.AbsBinds _ _ _ _ _) _pns = False
#if __GLASGOW_HASKELL__ > 710
usedByRhsRdr _nm (GHC.AbsBindsSig _ _ _ _ _ _) _pns = False
#endif
instance UsedByRhs (GHC.HsExpr GHC.RdrName) where
usedByRhsRdr nm (GHC.HsLet _lb e) pns = findNamesRdr nm pns e
usedByRhsRdr _ e _pns = error $ "undefined usedByRhsRdr:" ++ (showGhc e)
instance UsedByRhs (GHC.Stmt GHC.RdrName (GHC.LHsExpr GHC.RdrName)) where
usedByRhsRdr nm (GHC.LetStmt lb) pns = findNamesRdr nm pns lb
usedByRhsRdr _ s _pns = error $ "undefined usedByRhsRdr:" ++ (showGhc s)
getName::(SYB.Data t)=> String
-> t
-> Maybe GHC.Name
getName str t
= res
where
res = SYB.somethingStaged SYB.Renamer Nothing
(Nothing `SYB.mkQ` worker
#if __GLASGOW_HASKELL__ <= 710
`SYB.extQ` workerBind
`SYB.extQ` workerExpr
#endif
) t
worker ((GHC.L _ n) :: (GHC.Located GHC.Name))
| showGhcQual n == str = Just n
worker _ = Nothing
#if __GLASGOW_HASKELL__ <= 710
workerBind (GHC.L _ (GHC.VarPat name) :: (GHC.Located (GHC.Pat GHC.Name)))
| showGhcQual name == str = Just name
workerBind _ = Nothing
workerExpr ((GHC.L _ (GHC.HsVar name)) :: (GHC.Located (GHC.HsExpr GHC.Name)))
| showGhcQual name == str = Just name
workerExpr _ = Nothing
#endif
addImportDecl ::
GHC.ParsedSource
-> GHC.ModuleName
#if __GLASGOW_HASKELL__ <= 710
-> Maybe GHC.FastString
#else
-> Maybe GHC.StringLiteral
#endif
-> Bool -> Bool -> Bool
-> Maybe String
-> Bool
-> [GHC.RdrName]
-> RefactGhc GHC.ParsedSource
addImportDecl (GHC.L l p) modName pkgQual source safe qualify alias hide idNames
= do
let imp = GHC.hsmodImports p
impDecl <- mkImpDecl
newSpan <- liftT uniqueSrcSpanT
let newImp = GHC.L newSpan impDecl
liftT $ addSimpleAnnT newImp (DP (1,0)) [((G GHC.AnnImport),DP (0,0))]
return (GHC.L l p { GHC.hsmodImports = (imp++[newImp])})
where
alias' = case alias of
Just stringName -> Just $ GHC.mkModuleName stringName
_ -> Nothing
mkImpDecl = do
newSpan1 <- liftT uniqueSrcSpanT
newSpan2 <- liftT uniqueSrcSpanT
newEnts <- mkNewEntList idNames
let lNewEnts = GHC.L newSpan2 newEnts
liftT $ addSimpleAnnT lNewEnts (DP (0,1)) [((G GHC.AnnHiding),DP (0,0)),((G GHC.AnnOpenP),DP (0,1)),((G GHC.AnnCloseP),DP (0,0))]
let lmodname = GHC.L newSpan1 modName
liftT $ addSimpleAnnT lmodname (DP (0,1)) [((G GHC.AnnVal),DP (0,0))]
return $ GHC.ImportDecl
{ GHC.ideclSourceSrc = Nothing
, GHC.ideclName = lmodname
, GHC.ideclPkgQual = pkgQual
, GHC.ideclSource = source
, GHC.ideclSafe = safe
, GHC.ideclQualified = qualify
, GHC.ideclImplicit = False
, GHC.ideclAs = alias'
, GHC.ideclHiding =
(if idNames == [] && hide == False then
Nothing
else
(Just (hide, lNewEnts)))
}
addDecl:: (SYB.Data t,SYB.Typeable t)
=> t
-> Maybe GHC.Name
-> ([GHC.LHsDecl GHC.RdrName], Maybe Anns)
-> RefactGhc t
addDecl parent pn (declSig, mDeclAnns) = do
logm $ "addDecl:declSig=" ++ showGhc declSig
case mDeclAnns of
Nothing -> return ()
Just declAnns ->
liftT $ modifyAnnsT (mergeAnns declAnns)
case pn of
Just pn' -> appendDecl parent pn' declSig
Nothing -> addLocalDecl parent declSig
where
setDeclSpacing newDeclSig n c = do
mapM_ (\d -> setPrecedingLinesDeclT d 1 0) newDeclSig
setPrecedingLinesT (ghead "addDecl" newDeclSig) n c
appendDecl :: (SYB.Data t)
=> t
-> GHC.Name
-> [GHC.LHsDecl GHC.RdrName]
-> RefactGhc t
appendDecl parent1 pn' newDeclSig = do
hasDeclsSybTransform workerHsDecls workerBind parent1
where
workerHsDecls :: forall t. HasDecls t => t -> RefactGhc t
workerHsDecls parent' = do
liftT $ setDeclSpacing newDeclSig 2 0
nameMap <- getRefactNameMap
decls <- liftT $ hsDecls parent'
let
(before,after) = break (definesDeclRdr nameMap pn') decls
let (decls1,decls2) = case after of
[] -> (before,[])
_ -> (before ++ [ghead "appendDecl14" after],
gtail "appendDecl15" after)
unless (null decls1 || null decls2) $ do liftT $ balanceComments (last decls1) (head decls2)
liftT $ replaceDecls parent' (decls1++newDeclSig++decls2)
workerBind :: (GHC.LHsBind GHC.RdrName -> RefactGhc (GHC.LHsBind GHC.RdrName))
workerBind = assert False undefined
addLocalDecl :: (SYB.Typeable t,SYB.Data t)
=> t -> [GHC.LHsDecl GHC.RdrName]
-> RefactGhc t
addLocalDecl parent' newDeclSig = do
logm $ "addLocalDecl entered"
hasDeclsSybTransform workerHasDecls workerBind parent'
where
workerDecls :: [GHC.LHsDecl GHC.RdrName] -> RefactGhc [GHC.LHsDecl GHC.RdrName]
workerDecls decls = do
logm $ "workerDecls entered"
case decls of
[] -> liftT $ setDeclSpacing newDeclSig 2 0
ds -> do
DP (r,c) <- liftT (getEntryDPT (head ds))
liftT $ setDeclSpacing newDeclSig r c
liftT $ setPrecedingLinesT (head ds) 2 0
return (newDeclSig++decls)
workerHasDecls :: (HasDecls t) => t -> RefactGhc t
workerHasDecls p = do
logm $ "workerHasDecls entered"
decls <- liftT (hsDecls p)
decls' <- workerDecls decls
r <- liftT $ replaceDecls p decls'
return r
workerBind :: GHC.LHsBind GHC.RdrName -> RefactGhc (GHC.LHsBind GHC.RdrName)
workerBind b = do
logm $ "workerBind entered"
case b of
#if __GLASGOW_HASKELL__ <= 710
GHC.L l (GHC.FunBind n i (GHC.MG [match] a ptt o) co fvs t) -> do
#else
GHC.L l (GHC.FunBind n (GHC.MG (GHC.L lm [match]) a ptt o) co fvs t) -> do
#endif
match' <- workerHasDecls match
#if __GLASGOW_HASKELL__ <= 710
return (GHC.L l (GHC.FunBind n i (GHC.MG [match'] a ptt o) co fvs t))
#else
return (GHC.L l (GHC.FunBind n (GHC.MG (GHC.L lm [match']) a ptt o) co fvs t))
#endif
#if __GLASGOW_HASKELL__ <= 710
GHC.L _ (GHC.FunBind _ _ (GHC.MG _matches _ _ _) _ _ _) -> do
#else
GHC.L _ (GHC.FunBind _ (GHC.MG _matches _ _ _) _ _ _) -> do
#endif
error "addDecl:Cannot add a local decl to a FunBind with multiple matches"
p@(GHC.L _ (GHC.PatBind _pat _rhs _ty _fvs _t)) -> do
logm $ "workerBind.PatBind entered"
decls <- liftT (hsDeclsPatBind p)
decls' <- workerDecls decls
r <- liftT $ replaceDeclsPatBind p decls'
return r
x -> error $ "addLocalDecl.workerBind:not processing:" ++ SYB.showData SYB.Parser 0 x
rdrNameFromName :: Bool -> GHC.Name -> RefactGhc GHC.RdrName
rdrNameFromName useQual newName = do
mname <- case (GHC.nameModule_maybe newName) of
Just (GHC.Module _ mn) -> return mn
Nothing -> do
GHC.Module _ mn <- getRefactModule
return mn
if useQual
then return $ GHC.mkRdrQual mname (GHC.nameOccName newName)
else return $ GHC.mkRdrUnqual (GHC.nameOccName newName)
addHiding::
GHC.ModuleName
-> GHC.ParsedSource
-> [GHC.RdrName]
-> RefactGhc GHC.ParsedSource
addHiding mn p ns = do
logm $ "addHiding called for (module,names):" ++ showGhc (mn,ns)
p' <- addItemsToImport' mn p (Left ns) Hide
putRefactParsed p' emptyAnns
return p'
mkNewEntList :: [GHC.RdrName] -> RefactGhc [GHC.LIE GHC.RdrName]
mkNewEntList idNames = do
case idNames of
[] -> return []
_ -> do
newEntsInit <- mapM (mkNewEnt True) (init idNames)
newEntsLast <- mkNewEnt False (last idNames)
return (newEntsInit ++ [newEntsLast])
mkNewEnt :: Bool -> GHC.RdrName -> RefactGhc (GHC.LIE GHC.RdrName)
mkNewEnt addCommaAnn pn = do
newSpan <- liftT uniqueSrcSpanT
let lpn = GHC.L newSpan pn
if addCommaAnn
then liftT $ addSimpleAnnT lpn (DP (0,0)) [((G GHC.AnnVal),DP (0,0)),((G GHC.AnnComma),DP (0,0))]
else liftT $ addSimpleAnnT lpn (DP (0,0)) [((G GHC.AnnVal),DP (0,0))]
return (GHC.L newSpan (GHC.IEVar lpn))
data ImportType = Hide
| Import
addItemsToImport ::
GHC.ModuleName
-> Maybe GHC.Name
-> Either [GHC.RdrName] [GHC.LIE GHC.RdrName]
-> GHC.ParsedSource
-> RefactGhc GHC.ParsedSource
addItemsToImport mn mc ns r = addItemsToImport' mn r ns Import
addItemsToImport'::
GHC.ModuleName
-> GHC.ParsedSource
-> Either [GHC.RdrName] [GHC.LIE GHC.RdrName]
-> ImportType
-> RefactGhc GHC.ParsedSource
addItemsToImport' serverModName (GHC.L l p) pns impType = do
let imps = GHC.hsmodImports p
imps' <- mapM inImport imps
return $ GHC.L l p {GHC.hsmodImports = imps'}
where
isHide = case impType of
Hide -> True
Import -> False
inImport :: GHC.LImportDecl GHC.RdrName -> RefactGhc (GHC.LImportDecl GHC.RdrName)
inImport imp@(GHC.L _ (GHC.ImportDecl _st (GHC.L _ modName) _qualify _source _safe isQualified _isImplicit _as h))
| serverModName == modName && not isQualified
= case h of
Nothing -> insertEnts imp [] True
Just (_isHide, (GHC.L _le ents)) -> insertEnts imp ents False
inImport x = return x
insertEnts ::
GHC.LImportDecl GHC.RdrName
-> [GHC.LIE GHC.RdrName]
-> Bool
-> RefactGhc ( GHC.LImportDecl GHC.RdrName )
insertEnts imp ents isNew = do
logm $ "addItemsToImport':insertEnts:(imp,ents,isNew):" ++ showGhc (imp,ents,isNew)
if isNew && not isHide then return imp
else do
logm $ "addItemsToImport':insertEnts:doing stuff"
newSpan <- liftT uniqueSrcSpanT
newEnts <- case pns of
Left pns' -> mkNewEntList pns'
Right pns' -> return pns'
let lNewEnts = GHC.L newSpan (ents++newEnts)
logm $ "addImportDecl.mkImpDecl:adding anns for:" ++ showGhc lNewEnts
if isHide
then
liftT $ addSimpleAnnT lNewEnts (DP (0,1)) [((G GHC.AnnHiding),DP (0,0)),((G GHC.AnnOpenP),DP (0,1)),((G GHC.AnnCloseP),DP (0,0))]
else
liftT $ addSimpleAnnT lNewEnts (DP (0,1)) [((G GHC.AnnOpenP),DP (0,0)),((G GHC.AnnCloseP),DP (0,0))]
when (not (null ents)) $ do liftT (addTrailingCommaT (last ents))
return (replaceHiding imp (Just (isHide, lNewEnts)))
replaceHiding (GHC.L l1 (GHC.ImportDecl st mn q src safe isQ isImp as _h)) h1 =
(GHC.L l1 (GHC.ImportDecl st mn q src safe isQ isImp as h1))
addParamsToSigs :: [GHC.Name] -> GHC.LSig GHC.RdrName -> RefactGhc (GHC.LSig GHC.RdrName)
addParamsToSigs [] ms = return ms
#if __GLASGOW_HASKELL__ <= 710
addParamsToSigs newParams (GHC.L l (GHC.TypeSig lns ltyp pns)) = do
#else
addParamsToSigs newParams (GHC.L l (GHC.TypeSig lns (GHC.HsIB ivs (GHC.HsWC wcs mwc ltyp)))) = do
#endif
logm $ "addParamsToSigs:newParams=" ++ showGhc newParams
mts <- mapM getTypeForName newParams
let ts = catMaybes mts
logm $ "addParamsToSigs:ts=" ++ showGhc ts
logDataWithAnns "addParamsToSigs:ts=" ts
let newStr = ":: " ++ (intercalate " -> " $ map printSigComponent ts) ++ " -> "
logm $ "addParamsToSigs:newStr=[" ++ newStr ++ "]"
typ' <- liftT $ foldlM addOneType ltyp (reverse ts)
sigOk <- isNewSignatureOk ts
logm $ "addParamsToSigs:(sigOk,newStr)=" ++ show (sigOk,newStr)
if sigOk
#if __GLASGOW_HASKELL__ <= 710
then return (GHC.L l (GHC.TypeSig lns typ' pns))
#else
then return (GHC.L l (GHC.TypeSig lns (GHC.HsIB ivs (GHC.HsWC wcs mwc typ'))))
#endif
else error $ "\nNew type signature may fail type checking: " ++ newStr ++ "\n"
where
addOneType :: GHC.LHsType GHC.RdrName -> GHC.Type -> Transform (GHC.LHsType GHC.RdrName)
addOneType et t = do
hst <- typeToLHsType t
ss1 <- uniqueSrcSpanT
#if __GLASGOW_HASKELL__ <= 710
hst1 <- case t of
(GHC.FunTy _ _) -> do
ss <- uniqueSrcSpanT
let t1 = GHC.L ss (GHC.HsParTy hst)
setEntryDPT hst (DP (0,0))
addSimpleAnnT t1 (DP (0,0)) [((G GHC.AnnOpenP),DP (0,1)),((G GHC.AnnCloseP),DP (0,0))]
return t1
_ -> return hst
let typ = GHC.L ss1 (GHC.HsFunTy hst1 et)
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnRarrow),DP (0,1))]
#else
hst1 <- case t of
(GHC.ForAllTy (GHC.Anon _) _) -> do
ss <- uniqueSrcSpanT
let t1 = GHC.L ss (GHC.HsParTy hst)
setEntryDPT hst (DP (0,0))
addSimpleAnnT t1 (DP (0,0)) [((G GHC.AnnOpenP),DP (0,1)),((G GHC.AnnCloseP),DP (0,0))]
return t1
_ -> return hst
let typ = GHC.L ss1 (GHC.HsFunTy hst1 et)
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnRarrow),DP (0,1))]
#endif
return typ
printSigComponent :: GHC.Type -> String
printSigComponent x = ppType x
addParamsToSigs np ls = error $ "addParamsToSigs: no match for:" ++ showGhc (np,ls)
isNewSignatureOk :: [GHC.Type] -> RefactGhc Bool
isNewSignatureOk types = do
logm $ "isNewSignatureOk:types=" ++ SYB.showData SYB.Parser 0 types
let
r = SYB.everythingStaged SYB.TypeChecker (++) []
([] `SYB.mkQ` usesForAll) types
#if __GLASGOW_HASKELL__ <= 710
usesForAll (GHC.ForAllTy _ _) = [1::Int]
#else
usesForAll (GHC.ForAllTy (GHC.Named _ _) _) = [1::Int]
#endif
usesForAll _ = []
return $ emptyList r
typeToLHsType :: GHC.Type -> Transform (GHC.LHsType GHC.RdrName)
typeToLHsType (GHC.TyVarTy v) = do
ss <- uniqueSrcSpanT
#if __GLASGOW_HASKELL__ <= 710
let typ = GHC.L ss (GHC.HsTyVar (GHC.nameRdrName $ Var.varName v))
#else
let typ = GHC.L ss (GHC.HsTyVar (GHC.L ss (GHC.nameRdrName $ Var.varName v)))
#endif
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnVal),DP (0,0))]
return typ
typeToLHsType (GHC.AppTy t1 t2) = do
t1' <- typeToLHsType t1
t2' <- typeToLHsType t2
ss <- uniqueSrcSpanT
return $ GHC.L ss (GHC.HsAppTy t1' t2')
typeToLHsType t@(GHC.TyConApp _tc _ts) = tyConAppToHsType t
#if __GLASGOW_HASKELL__ <= 710
typeToLHsType (GHC.FunTy t1 t2) = do
t1' <- typeToLHsType t1
t2' <- typeToLHsType t2
ss <- uniqueSrcSpanT
let typ = GHC.L ss (GHC.HsFunTy t1' t2')
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnRarrow),DP (0,1))]
return typ
#else
typeToLHsType (GHC.ForAllTy (GHC.Anon t1) t2) = do
t1' <- typeToLHsType t1
t2' <- typeToLHsType t2
ss <- uniqueSrcSpanT
let typ = GHC.L ss (GHC.HsFunTy t1' t2')
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnRarrow),DP (0,1))]
return typ
#endif
typeToLHsType (GHC.ForAllTy _v t) = do
t' <- typeToLHsType t
ss1 <- uniqueSrcSpanT
#if __GLASGOW_HASKELL__ <= 710
ss2 <- uniqueSrcSpanT
return $ GHC.L ss1 (GHC.HsForAllTy GHC.Explicit Nothing (GHC.HsQTvs [] []) (GHC.L ss2 []) t')
#else
return $ GHC.L ss1 (GHC.HsForAllTy [] t')
#endif
typeToLHsType (GHC.LitTy (GHC.NumTyLit i)) = do
ss <- uniqueSrcSpanT
let typ = GHC.L ss (GHC.HsTyLit (GHC.HsNumTy (show i) i)) :: GHC.LHsType GHC.RdrName
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnVal),DP (0,0))]
return typ
typeToLHsType (GHC.LitTy (GHC.StrTyLit s)) = do
ss <- uniqueSrcSpanT
let typ = GHC.L ss (GHC.HsTyLit (GHC.HsStrTy "" s)) :: GHC.LHsType GHC.RdrName
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnVal),DP (0,0))]
return typ
tyConAppToHsType :: GHC.Type -> Transform (GHC.LHsType GHC.RdrName)
tyConAppToHsType (GHC.TyConApp tc _ts) = r (show $ GHC.tyConName tc)
where
r str = do
ss <- uniqueSrcSpanT
let typ = GHC.L ss (GHC.HsTyLit (GHC.HsStrTy str $ GHC.mkFastString str)) :: GHC.LHsType GHC.RdrName
addSimpleAnnT typ (DP (0,0)) [((G GHC.AnnVal),DP (0,1))]
return typ
addParamsToDecls::
[GHC.LHsDecl GHC.RdrName]
-> GHC.Name
-> [GHC.RdrName]
-> RefactGhc [GHC.LHsDecl GHC.RdrName]
addParamsToDecls decls pn paramPNames = do
logm $ "addParamsToDecls (pn,paramPNames)=" ++ (showGhc (pn,paramPNames))
nameMap <- getRefactNameMap
if (paramPNames /= [])
then mapM (addParamToDecl nameMap) decls
else return decls
where
addParamToDecl :: NameMap -> GHC.LHsDecl GHC.RdrName -> RefactGhc (GHC.LHsDecl GHC.RdrName)
#if __GLASGOW_HASKELL__ <= 710
addParamToDecl nameMap (GHC.L l1 (GHC.ValD (GHC.FunBind lp@(GHC.L l2 pname) i (GHC.MG matches a ptt o) co fvs t)))
#else
addParamToDecl nameMap (GHC.L l1 (GHC.ValD (GHC.FunBind lp@(GHC.L l2 pname) (GHC.MG (GHC.L lm matches) a ptt o) co fvs t)))
#endif
| eqRdrNamePure nameMap lp pn
= do
matches' <- mapM addParamtoMatch matches
#if __GLASGOW_HASKELL__ <= 710
return (GHC.L l1 (GHC.ValD (GHC.FunBind (GHC.L l2 pname) i (GHC.MG matches' a ptt o) co fvs t)))
#else
return (GHC.L l1 (GHC.ValD (GHC.FunBind (GHC.L l2 pname) (GHC.MG (GHC.L lm matches') a ptt o) co fvs t)))
#endif
where
addParamtoMatch (GHC.L l (GHC.Match fn1 pats mtyp rhs))
= do
rhs' <- addActualParamsToRhs pn paramPNames rhs
pats' <- liftT $ mapM addParam paramPNames
return (GHC.L l (GHC.Match fn1 (pats'++pats) mtyp rhs'))
addParamToDecl _nameMap x@(GHC.L _l1 (GHC.ValD (GHC.PatBind _pat@(GHC.L _l2 (GHC.VarPat _p)) _rhs _ty _fvs _t)))
= return x
addParamToDecl _ x = return x
addParam n = do
newSpan <- uniqueSrcSpanT
#if __GLASGOW_HASKELL__ <= 710
let vn = (GHC.L newSpan (GHC.VarPat n))
#else
let vn = (GHC.L newSpan (GHC.VarPat (GHC.L newSpan n)))
#endif
addSimpleAnnT vn (DP (0,1)) [((G GHC.AnnVal),DP (0,0))]
return vn
addItemsToExport ::
GHC.ParsedSource
-> Maybe GHC.Name
-> Bool
-> Either [GHC.RdrName] [GHC.LIE GHC.RdrName]
-> RefactGhc GHC.ParsedSource
addItemsToExport modu _ _ (Left []) = return modu
addItemsToExport modu _ _ (Right []) = return modu
addItemsToExport modu@(GHC.L l (GHC.HsModule modName exps imps ds deps hs)) (Just pn) _ ids
= case exps of
Just (GHC.L le ents) -> do
logm $ "addItemsToExport:pn=" ++ showGhc pn
nm <- getRefactNameMap
let (e1,e2) = break (findLRdrName nm pn) ents
if e2 /= []
then do
es <- case ids of
Left is' -> mkNewEntList is'
Right es' -> return es'
let e = (ghead "addVarItemInExport" e2)
lNewEnts = GHC.L le (e1++(e:es)++tail e2)
liftT (addTrailingCommaT e)
return (GHC.L l (GHC.HsModule modName (Just lNewEnts) imps ds deps hs))
else return modu
Nothing -> return modu
addItemsToExport (GHC.L l (GHC.HsModule _ (Just ents) _ _ _ _)) Nothing createExp ids
= assert False undefined
addItemsToExport modu@(GHC.L l (GHC.HsModule modName Nothing _ _ _ _)) Nothing createExp ids
= assert False undefined
addActualParamsToRhs :: (SYB.Data t) =>
GHC.Name -> [GHC.RdrName] -> t -> RefactGhc t
addActualParamsToRhs pn paramPNames rhs = do
logm $ "addActualParamsToRhs:entered:(pn,paramPNames)=" ++ showGhc (pn,paramPNames)
nameMap <- getRefactNameMap
let
worker :: (GHC.LHsExpr GHC.RdrName) -> RefactGhc (GHC.LHsExpr GHC.RdrName)
#if __GLASGOW_HASKELL__ <= 710
worker oldExp@(GHC.L l2 (GHC.HsVar pname))
#else
worker oldExp@(GHC.L l2 (GHC.HsVar (GHC.L _ pname)))
#endif
| eqRdrNamePure nameMap (GHC.L l2 pname) pn
= do
logDataWithAnns "addActualParamsToRhs:oldExp=" oldExp
newExp' <- foldlM addParamToExp oldExp paramPNames
edp <- liftT $ getEntryDPT oldExp
liftT $ setEntryDPT oldExp (DP (0,0))
l2' <- liftT $ uniqueSrcSpanT
let newExp = (GHC.L l2' (GHC.HsPar newExp'))
liftT $ addSimpleAnnT newExp (DP (0,0)) [(G GHC.AnnOpenP,DP (0,0)),(G GHC.AnnCloseP,DP (0,0))]
liftT $ setEntryDPT newExp edp
return newExp
worker x = return x
addParamToExp :: (GHC.LHsExpr GHC.RdrName) -> GHC.RdrName -> RefactGhc (GHC.LHsExpr GHC.RdrName)
addParamToExp expr param = do
ss1 <- liftT $ uniqueSrcSpanT
ss2 <- liftT $ uniqueSrcSpanT
logm $ "addActualParamsToRhs.addParamsToExp:(ss1,ss2):" ++ showGhc (ss1,ss2)
registerRdrName (GHC.L ss2 param)
#if __GLASGOW_HASKELL__ <= 710
let var = GHC.L ss2 (GHC.HsVar param)
#else
let var = GHC.L ss2 (GHC.HsVar (GHC.L ss2 param))
#endif
liftT $ addSimpleAnnT var (DP (0,0)) [(G GHC.AnnVal,DP (0,1))]
let expr' = GHC.L ss1 (GHC.HsApp expr var)
liftT $ addSimpleAnnT expr' (DP (0,0)) []
return expr'
r <- applyTP (full_buTP (idTP `adhocTP` worker)) rhs
return r
duplicateDecl ::
[GHC.LHsDecl GHC.RdrName]
->GHC.Name
->GHC.Name
->RefactGhc [GHC.LHsDecl GHC.RdrName]
duplicateDecl decls n newFunName
= do
logm $ "duplicateDecl entered:(decls,n,newFunName)=" ++ showGhc (decls,n,newFunName)
nm <- getRefactNameMap
let
declsToDup = definingDeclsRdrNames nm [n] decls True False
funBinding = filter isFunOrPatBindP declsToDup
typeSig = map wrapSig $ definingSigsRdrNames nm [n] decls
funBinding'' <- renamePN n newFunName PreserveQualify funBinding
typeSig'' <- renamePN n newFunName PreserveQualify typeSig
logm $ "duplicateDecl:funBinding''=" ++ showGhc funBinding''
funBinding3 <- mapM (\f@(GHC.L _ fb) -> do
newSpan <- liftT uniqueSrcSpanT
let fb' = GHC.L newSpan fb
liftT $ modifyAnnsT (copyAnn f fb')
return fb'
) (typeSig'' ++ funBinding'')
when (not $ null funBinding3) $ do
liftT $ setEntryDPT (head funBinding3) (DP (2,0))
liftT $ mapM_ (\d -> setEntryDPT d (DP (1,0))) (tail funBinding3)
let (decls1,decls2) = break (definesDeclRdr nm n) decls
(declsToDup',declsRest) = break (not . definesDeclRdr nm n) decls2
return $ decls1 ++ declsToDup' ++ funBinding3 ++ declsRest
divideDecls :: SYB.Data t =>
[t] -> GHC.Located GHC.Name -> RefactGhc ([t], [t], [t])
divideDecls ds (GHC.L _ pnt) = do
nm <- getRefactNameMap
let (before,after) = break (\x -> findNameInRdr nm pnt x) ds
return $ if (not $ emptyList after)
then (before, [ghead "divideDecls" after], gtail "divideDecls" after)
else (ds,[],[])
rmDecl:: (SYB.Data t)
=> GHC.Name
-> Bool
-> t
-> RefactGhc
(t,
GHC.LHsDecl GHC.RdrName,
Maybe (GHC.LSig GHC.RdrName))
rmDecl pn incSig t = do
setStateStorage StorageNone
t' <- everywhereMStaged' SYB.Parser (SYB.mkM inModule
`SYB.extM` inLet
`SYB.extM` inMatch
) t
storage <- getStateStorage
let decl' = case storage of
StorageDeclRdr decl -> decl
x -> error $ "rmDecl: unexpected value in StateStorage:" ++ (show x)
setStateStorage StorageNone
(t'',sig') <- if incSig
then rmTypeSig pn t'
else return (t', Nothing)
return (t'',decl',sig')
where
inModule (p :: GHC.ParsedSource)
= doRmDeclList p
inMatch x@(((GHC.L _ (GHC.Match _ _ _ (GHC.GRHSs _ _localDecls)))):: (GHC.LMatch GHC.RdrName (GHC.LHsExpr GHC.RdrName)))
= doRmDeclList x
inLet :: GHC.LHsExpr GHC.RdrName -> RefactGhc (GHC.LHsExpr GHC.RdrName)
inLet letExpr@(GHC.L _ (GHC.HsLet _localDecls expr))
= do
isDone <- getDone
if isDone
then return letExpr
else do
nameMap <- getRefactNameMap
decls <- liftT $ hsDecls letExpr
let (decls1,decls2) = break (definesDeclRdr nameMap pn) decls
if not $ emptyList decls2
then do
let decl = ghead "rmDecl" decls2
setStateStorage (StorageDeclRdr decl)
case length decls of
1 -> do
return expr
_ -> do
decls' <- doRmDecl decls1 decls2
letExpr' <- liftT $ replaceDecls letExpr decls'
return letExpr'
else do
return letExpr
inLet x = return x
doRmDeclList parent
= do
isDone <- getDone
if isDone
then return parent
else do
nameMap <- getRefactNameMap
decls <- liftT $ hsDecls parent
let (decls1,decls2) = break (definesDeclRdr nameMap pn) decls
if not (null decls2)
then do
let decl = ghead "doRmDeclList" decls2
setStateStorage (StorageDeclRdr decl)
decls' <- doRmDecl decls1 decls2
parent' <- liftT $ replaceDecls parent decls'
return parent'
else do
return parent
getDone = do
s <- getStateStorage
case s of
StorageNone -> return False
_ -> return True
declsSybTransform :: (SYB.Typeable a)
=> (forall b. HasDecls b => b -> RefactGhc b)
-> a -> RefactGhc a
declsSybTransform transform = mt
where
mt = SYB.mkM inMatch
`SYB.extM` inPatDecl
`SYB.extM` inModule
`SYB.extM` inHsLet
inModule :: GHC.ParsedSource -> RefactGhc GHC.ParsedSource
inModule (modu :: GHC.ParsedSource)
= transform modu
inMatch :: GHC.LMatch GHC.RdrName (GHC.LHsExpr GHC.RdrName) -> RefactGhc (GHC.LMatch GHC.RdrName (GHC.LHsExpr GHC.RdrName))
inMatch x@(GHC.L _ (GHC.Match _ _ _ (GHC.GRHSs _ _localDecls)))
= transform x
inPatDecl ::GHC.LHsDecl GHC.RdrName -> RefactGhc (GHC.LHsDecl GHC.RdrName)
inPatDecl (GHC.L _ (GHC.ValD (GHC.PatBind _ _ _ _ _)))
= error $ "declsSybTransform:need to reimplement PatBind case"
inPatDecl x = return x
inHsLet :: GHC.LHsExpr GHC.RdrName -> RefactGhc (GHC.LHsExpr GHC.RdrName)
inHsLet x@(GHC.L _ (GHC.HsLet{}))
= transform x
inHsLet x = return x
doRmDecl :: [GHC.LHsDecl GHC.RdrName] -> [GHC.LHsDecl GHC.RdrName] -> RefactGhc [GHC.LHsDecl GHC.RdrName]
doRmDecl decls1 decls2
= do
let decls2' = gtail "doRmDecl 1" decls2
declToRemove = head decls2
unless (null decls1) $ do liftT $ balanceComments (last decls1) declToRemove
unless (null decls2') $ do liftT $ balanceComments declToRemove (head decls2')
when (not (null decls2') && null decls1) $ do liftT $ transferEntryDPT declToRemove (head decls2')
when (not (null decls2') && not (null decls1) && not (isTypeSigDecl (last decls1)))
$ do liftT $ transferEntryDPT declToRemove (head decls2')
return $ (decls1 ++ decls2')
rmTypeSigs :: (SYB.Data t) =>
[GHC.Name]
-> t
-> RefactGhc (t,[GHC.LSig GHC.RdrName])
rmTypeSigs pns t = do
(t',demotedSigsMaybe) <- foldM (\(tee,ds) n -> do { (tee',d) <- rmTypeSig n tee; return (tee', ds++[d])}) (t,[]) pns
return (t',catMaybes demotedSigsMaybe)
rmTypeSig :: (SYB.Data t) =>
GHC.Name
-> t
-> RefactGhc (t,Maybe (GHC.LSig GHC.RdrName))
rmTypeSig pn t
= do
setStateStorage StorageNone
t' <- SYB.everywhereMStaged SYB.Renamer (SYB.mkM inMatch `SYB.extM` inPatDecl `SYB.extM` inModule) t
storage <- getStateStorage
let sig' = case storage of
StorageSigRdr sig -> Just sig
StorageNone -> Nothing
x -> error $ "rmTypeSig: unexpected value in StateStorage:" ++ (show x)
return (t',sig')
where
inModule :: GHC.ParsedSource -> RefactGhc GHC.ParsedSource
inModule (modu :: GHC.ParsedSource)
= doRmTypeSig modu
inMatch :: GHC.LMatch GHC.RdrName (GHC.LHsExpr GHC.RdrName)
-> RefactGhc (GHC.LMatch GHC.RdrName (GHC.LHsExpr GHC.RdrName))
inMatch x@(GHC.L _ (GHC.Match _ _ _ (GHC.GRHSs _ _localDecls)))
= doRmTypeSig x
inPatDecl ::GHC.LHsDecl GHC.RdrName -> RefactGhc (GHC.LHsDecl GHC.RdrName)
inPatDecl x@(GHC.L _ (GHC.ValD (GHC.PatBind _ _ _ _ _))) = do
decls <- liftT $ hsDeclsPatBindD x
decls' <- doRmTypeSigDecls decls
liftT $ replaceDeclsPatBindD x decls'
inPatDecl x = return x
doRmTypeSig :: (HasDecls t) => t -> RefactGhc t
doRmTypeSig parent = do
decls <- liftT $ hsDecls parent
decls' <- doRmTypeSigDecls decls
liftT $ replaceDecls parent decls'
doRmTypeSigDecls :: [GHC.LHsDecl GHC.RdrName] -> RefactGhc [GHC.LHsDecl GHC.RdrName]
doRmTypeSigDecls decls = do
isDone <- getDone
if isDone
then return decls
else do
nameMap <- getRefactNameMap
let (decls1,decls2)= break (definesSigDRdr nameMap pn) decls
if not $ null decls2
then do
#if __GLASGOW_HASKELL__ <= 710
let sig@(GHC.L sspan (GHC.SigD (GHC.TypeSig names typ p))) = ghead "rmTypeSig" decls2
#else
let sig@(GHC.L sspan (GHC.SigD (GHC.TypeSig names typ))) = ghead "rmTypeSig" decls2
#endif
if length names > 1
then do
let newNames = filter (\rn -> rdrName2NamePure nameMap rn /= pn) names
#if __GLASGOW_HASKELL__ <= 710
newSig = GHC.L sspan (GHC.SigD (GHC.TypeSig newNames typ p))
#else
newSig = GHC.L sspan (GHC.SigD (GHC.TypeSig newNames typ))
#endif
liftT $ removeTrailingCommaT (glast "doRmTypeSig" newNames)
let pnt = ghead "rmTypeSig" (filter (\rn -> rdrName2NamePure nameMap rn == pn) names)
liftT $ removeTrailingCommaT pnt
newSpan <- liftT uniqueSrcSpanT
#if __GLASGOW_HASKELL__ <= 710
let oldSig = (GHC.L newSpan (GHC.TypeSig [pnt] typ p))
#else
let oldSig = (GHC.L newSpan (GHC.TypeSig [pnt] typ))
#endif
liftT $ modifyAnnsT (copyAnn sig oldSig)
setStateStorage (StorageSigRdr oldSig)
return (decls1++[newSig]++gtail "doRmTypeSig" decls2)
else do
let [oldSig] = decl2Sig sig
setStateStorage (StorageSigRdr oldSig)
decls' <- doRmDecl decls1 decls2
return decls'
else do
return decls
getDone = do
s <- getStateStorage
case s of
StorageNone -> return False
_ -> return True
rmQualifier:: (SYB.Data t)
=>[GHC.Name]
->t
->RefactGhc t
rmQualifier pns t = do
nm <- getRefactNameMap
SYB.everywhereM (nameSybTransform (rename nm)) t
where
rename nm (ln@(GHC.L l pn)::GHC.Located GHC.RdrName)
| elem (rdrName2NamePure nm ln) pns
= do
case pn of
GHC.Qual _ n -> return (GHC.L l (GHC.Unqual n))
_ -> return ln
rename _ x = return x
qualifyToplevelName :: GHC.Name -> RefactGhc ()
qualifyToplevelName n = do
parsed <- getRefactParsed
parsed' <- renamePN n n Qualify parsed
putRefactParsed parsed' emptyAnns
return ()
data HowToQual = Qualify | NoQualify | PreserveQualify
deriving (Show,Eq)
instance GHC.Outputable HowToQual where
ppr x = GHC.text (show x)
renamePN::(SYB.Data t)
=> GHC.Name
-> GHC.Name
-> HowToQual
-> t
-> RefactGhc t
renamePN oldPN newName useQual t = do
newNameQual <- rdrNameFromName True newName
newNameUnqual <- rdrNameFromName False newName
let
cond :: NameMap -> GHC.Located GHC.RdrName -> Bool
cond nm (GHC.L ln _) =
case Map.lookup ln nm of
Nothing -> False
Just n -> GHC.nameUnique n == GHC.nameUnique oldPN || GHC.nameUnique n == GHC.nameUnique newName
newNameCalc :: HowToQual -> GHC.RdrName -> GHC.RdrName
newNameCalc uq old = newNameCalc' uq (GHC.isQual_maybe old)
where
newNameCalc' :: HowToQual -> (Maybe (GHC.ModuleName,GHC.OccName)) -> GHC.RdrName
newNameCalc' Qualify (Just (mn,_)) = GHC.Qual mn (GHC.occName newName)
newNameCalc' PreserveQualify (Just (mn,_)) = GHC.Qual mn (GHC.occName newName)
newNameCalc' NoQualify (Just (_n,_)) = GHC.Unqual (GHC.occName newName)
newNameCalc' uq' _ = if uq' == Qualify then newNameQual else newNameUnqual
makeNewName :: GHC.Located GHC.RdrName -> GHC.RdrName -> RefactGhc (GHC.Located GHC.RdrName)
makeNewName old newRdr = do
ss' <- liftT $ uniqueSrcSpanT
let new = (GHC.L ss' newRdr)
liftT $ modifyAnnsT (copyAnn old new)
addToNameMap ss' newName
return new
renameLRdr :: HowToQual -> GHC.Located GHC.RdrName -> RefactGhc (GHC.Located GHC.RdrName)
renameLRdr useQual' old@(GHC.L _ n) = do
nm <- getRefactNameMap
if cond nm old
then do
logDataWithAnns "renamePN:rename old :" old
let nn = newNameCalc useQual' n
new <- makeNewName old nn
logDataWithAnns "renamePN:rename new :" new
logDataWithAnns "renamePN:rename old2 :" old
return new
else return old
renameVar :: HowToQual -> GHC.LHsExpr GHC.RdrName -> RefactGhc (GHC.LHsExpr GHC.RdrName)
#if __GLASGOW_HASKELL__ <= 710
renameVar useQual' x@(GHC.L l (GHC.HsVar n)) = do
#else
renameVar useQual' x@(GHC.L l (GHC.HsVar (GHC.L _ n))) = do
#endif
nm <- getRefactNameMap
if cond nm (GHC.L l n)
then do
let nn = newNameCalc useQual' n
#if __GLASGOW_HASKELL__ <= 710
ss' <- liftT $ uniqueSrcSpanT
let (GHC.L l' _) = (GHC.L ss' nn)
liftT $ modifyAnnsT (copyAnn x (GHC.L ss' (GHC.HsVar nn)))
addToNameMap ss' newName
return (GHC.L l' (GHC.HsVar nn))
#else
new <- makeNewName (GHC.L l n) nn
return (GHC.L l (GHC.HsVar new))
#endif
else return x
renameVar _ x = return x
renameTyVar :: HowToQual -> (GHC.Located (GHC.HsType GHC.RdrName)) -> RefactGhc (GHC.Located (GHC.HsType GHC.RdrName))
#if __GLASGOW_HASKELL__ <= 710
renameTyVar useQual' x@(GHC.L l (GHC.HsTyVar n)) = do
#else
renameTyVar useQual' x@(GHC.L l (GHC.HsTyVar (GHC.L _ n))) = do
#endif
nm <- getRefactNameMap
if cond nm (GHC.L l n)
then do
logm $ "renamePN:renameTyVar at :" ++ (showGhc l)
let nn = newNameCalc useQual' n
#if __GLASGOW_HASKELL__ <= 710
ss' <- liftT $ uniqueSrcSpanT
let (GHC.L l' _) = (GHC.L ss' nn)
liftT $ modifyAnnsT (copyAnn x (GHC.L ss' (GHC.HsTyVar nn)))
addToNameMap ss' newName
return (GHC.L l' (GHC.HsTyVar nn))
#else
new <- makeNewName (GHC.L l n) nn
return (GHC.L l (GHC.HsTyVar new))
#endif
else return x
renameTyVar _ x = return x
renameHsTyVarBndr :: HowToQual -> GHC.LHsTyVarBndr GHC.RdrName -> RefactGhc (GHC.LHsTyVarBndr GHC.RdrName)
#if __GLASGOW_HASKELL__ <= 710
renameHsTyVarBndr useQual' x@(GHC.L l (GHC.UserTyVar n)) = do
#else
renameHsTyVarBndr useQual' x@(GHC.L l (GHC.UserTyVar (GHC.L _ n))) = do
#endif
nm <- getRefactNameMap
if cond nm (GHC.L l n)
then do
logm $ "renamePN:renameHsTyVarBndr at :" ++ (showGhc l)
let nn = newNameCalc useQual' n
#if __GLASGOW_HASKELL__ <= 710
addToNameMap l newName
return (GHC.L l (GHC.UserTyVar nn))
#else
new <- makeNewName (GHC.L l n) nn
return (GHC.L l (GHC.UserTyVar new))
#endif
else return x
renameHsTyVarBndr _ x = return x
renameLIE :: HowToQual -> (GHC.LIE GHC.RdrName) -> RefactGhc (GHC.LIE GHC.RdrName)
renameLIE useQual' x@(GHC.L l (GHC.IEVar old@(GHC.L ln n))) = do
nm <- getRefactNameMap
if cond nm (GHC.L ln n)
then do
let nn = newNameCalc useQual' n
new <- makeNewName old nn
return (GHC.L l (GHC.IEVar new))
else return x
renameLIE useQual' x@(GHC.L l (GHC.IEThingAbs old@(GHC.L _ln n))) = do
nm <- getRefactNameMap
if cond nm (GHC.L l n)
then do
let nn = newNameCalc useQual' n
new <- makeNewName old nn
return (GHC.L l (GHC.IEThingAbs new))
else return x
renameLIE useQual' x@(GHC.L l (GHC.IEThingAll old@(GHC.L ln n))) = do
nm <- getRefactNameMap
if cond nm (GHC.L ln n)
then do
let nn = newNameCalc useQual' n
new <- makeNewName old nn
return (GHC.L l (GHC.IEThingAll new))
else return x
#if __GLASGOW_HASKELL__ <= 710
renameLIE useQual' (GHC.L l (GHC.IEThingWith old@(GHC.L ln n) ns))
#else
renameLIE useQual' (GHC.L l (GHC.IEThingWith old@(GHC.L ln n) wc ns fls))
#endif
= do
nm <- getRefactNameMap
old' <- if (cond nm (GHC.L ln n))
then do
logm $ "renamePN:renameLIE.IEThingWith at :" ++ (showGhc l)
let nn = newNameCalc useQual' n
new <- makeNewName old nn
return new
else return old
ns' <- if (any (\(GHC.L lnn nn) -> cond nm (GHC.L lnn nn)) ns)
then renameTransform useQual' ns
else return ns
#if __GLASGOW_HASKELL__ <= 710
return (GHC.L l (GHC.IEThingWith old' ns'))
#else
return (GHC.L l (GHC.IEThingWith old' wc ns' fls))
#endif
renameLIE _ x = do
return x
renameLPat :: HowToQual -> (GHC.LPat GHC.RdrName) -> RefactGhc (GHC.LPat GHC.RdrName)
#if __GLASGOW_HASKELL__ <= 710
renameLPat useQual' x@(GHC.L l (GHC.VarPat n)) = do
#else
renameLPat useQual' x@(GHC.L l (GHC.VarPat (GHC.L _ n))) = do
#endif
nm <- getRefactNameMap
if cond nm (GHC.L l n)
then do
logm $ "renamePNworker:renameLPat at :" ++ (showGhc l)
let nn = newNameCalc useQual' n
#if __GLASGOW_HASKELL__ <= 710
ss' <- liftT $ uniqueSrcSpanT
let (GHC.L l' _) = (GHC.L ss' nn)
liftT $ modifyAnnsT (copyAnn x (GHC.L ss' (GHC.VarPat nn)))
addToNameMap ss' newName
return (GHC.L l' (GHC.VarPat nn))
#else
new <- makeNewName (GHC.L l n) nn
return (GHC.L l (GHC.VarPat new))
#endif
else return x
renameLPat _ x = return x
renameMatch :: HowToQual -> GHC.Match GHC.RdrName (GHC.LHsExpr GHC.RdrName)
-> RefactGhc (GHC.Match GHC.RdrName (GHC.LHsExpr GHC.RdrName))
renameMatch _useQual (GHC.Match mln pats ty grhss) = do
logm $ "renamePN.renameMatch entered:"
pats' <- renameTransform _useQual pats
ty' <- renameTransform _useQual ty
grhss' <- renameTransform _useQual grhss
mln' <- case mln of
#if __GLASGOW_HASKELL__ <= 710
Just (old@(GHC.L lmn mn),f) -> do
nm <- getRefactNameMap
if cond nm (GHC.L lmn mn)
then do
new <- makeNewName old newNameUnqual
return (Just (new,f))
else return mln
Nothing -> return mln
#else
GHC.FunBindMatch old f -> do
nm <- getRefactNameMap
if cond nm old
then do
new <- makeNewName old newNameUnqual
return (GHC.FunBindMatch new f)
else return mln
GHC.NonFunBindMatch -> return mln
#endif
return (GHC.Match mln' pats' ty' grhss')
renameImportDecl :: HowToQual -> GHC.ImportDecl GHC.RdrName -> RefactGhc (GHC.ImportDecl GHC.RdrName)
renameImportDecl _useQual (GHC.ImportDecl src mn mq isrc isafe iq ii ma (Just (ij,GHC.L ll ies))) = do
ies' <- mapM (renameLIE PreserveQualify) ies
logm $ "renamePN'.renameImportDecl:(ies,ies')=" ++ showGhc (ies,ies')
return (GHC.ImportDecl src mn mq isrc isafe iq ii ma (Just (ij,GHC.L ll ies')))
renameImportDecl _ x = return x
renameTypeSig :: HowToQual -> (GHC.Sig GHC.RdrName) -> RefactGhc (GHC.Sig GHC.RdrName)
#if __GLASGOW_HASKELL__ <= 710
renameTypeSig _useQual (GHC.TypeSig ns typ p)
#else
renameTypeSig _useQual (GHC.TypeSig ns typ)
#endif
= do
logm $ "renamePN:renameTypeSig"
ns' <- mapM (renameLRdr NoQualify) ns
typ' <- renameTransform _useQual typ
logm $ "renamePN:renameTypeSig done"
#if __GLASGOW_HASKELL__ <= 710
return (GHC.TypeSig ns' typ' p)
#else
return (GHC.TypeSig ns' typ')
#endif
#if __GLASGOW_HASKELL__ > 710
renameTypeSig _useQual (GHC.ClassOpSig f ns typ)
= do
ns' <- mapM (renameLRdr NoQualify) ns
typ' <- renameTransform _useQual typ
return (GHC.ClassOpSig f ns' typ')
#endif
renameTypeSig _ x = return x
everywhereMSkip :: Monad m => SYB.GenericM m -> SYB.GenericM m
everywhereMSkip f x
| (const False `SYB.extQ` typeSig) x = f x
| (const False `SYB.extQ` match) x = f x
| (const False `SYB.extQ` importDecl) x = f x
| otherwise = do x' <- f x
SYB.gmapM (everywhereMSkip f) x'
where
typeSig = const True :: GHC.Sig GHC.RdrName -> Bool
match = const True :: GHC.Match GHC.RdrName (GHC.LHsExpr GHC.RdrName) -> Bool
importDecl = const True :: GHC.ImportDecl GHC.RdrName -> Bool
renameTransform useQual' t' =
(everywhereMSkip (
SYB.mkM (renameVar useQual')
`SYB.extM` (renameLRdr useQual')
`SYB.extM` (renameTyVar useQual')
`SYB.extM` (renameHsTyVarBndr useQual')
`SYB.extM` (renameLIE useQual')
`SYB.extM` (renameLPat useQual')
`SYB.extM` (renameTypeSig useQual')
`SYB.extM` (renameImportDecl useQual')
`SYB.extM` (renameMatch useQual')
) t')
t' <- renameTransform useQual t
return t'
autoRenameLocalVar:: (SYB.Data t)
=> GHC.Name
-> t
-> RefactGhc t
autoRenameLocalVar pn t = do
logm $ "autoRenameLocalVar: (pn)=" ++ (showGhc (pn))
nm <- getRefactNameMap
decls <- liftT $ hsDeclsGeneric t
if isDeclaredInRdr nm pn decls
then do t' <- worker t
return t'
else do return t
where
worker :: (SYB.Data t) => t -> RefactGhc t
worker tt
=do (f,d) <- hsFDNamesFromInsideRdr tt
ds <- hsVisibleNamesRdr pn tt
let newNameStr = mkNewName (nameToString pn) (nub (f `union` d `union` ds)) 1
newName <- mkNewGhcName Nothing newNameStr
renamePN pn newName PreserveQualify tt
isMainModule :: GHC.Module -> Bool
#if __GLASGOW_HASKELL__ <= 710
isMainModule modu = GHC.modulePackageKey modu == GHC.mainPackageKey
#else
isMainModule modu = GHC.moduleUnitId modu == GHC.mainUnitId
#endif
defineLoc :: GHC.Located GHC.Name -> GHC.SrcLoc
defineLoc (GHC.L _ name) = GHC.nameSrcLoc name
useLoc:: (GHC.Located GHC.Name) -> GHC.SrcLoc
useLoc (GHC.L l _) = GHC.srcSpanStart l
findIdForName :: GHC.Name -> RefactGhc (Maybe GHC.Id)
findIdForName n = do
tm <- getTypecheckedModule
let t = GHC.tm_typechecked_source tm
let r = SYB.somethingStaged SYB.Parser Nothing (Nothing `SYB.mkQ` worker) t
worker (i::GHC.Id)
| (GHC.nameUnique n) == (GHC.varUnique i) = Just i
worker _ = Nothing
return r
getTypeForName :: GHC.Name -> RefactGhc (Maybe GHC.Type)
getTypeForName n = do
mId <- findIdForName n
case mId of
Nothing -> return Nothing
Just i -> return $ Just (GHC.varType i)
locToExp:: (SYB.Data t,SYB.Typeable n) =>
SimpPos
-> SimpPos
-> t
-> Maybe (GHC.LHsExpr n)
locToExp beginPos endPos t = res
where
res = SYB.somethingStaged SYB.Parser Nothing (Nothing `SYB.mkQ` expr) t
expr :: GHC.LHsExpr n -> Maybe (GHC.LHsExpr n)
expr e
|inScope e = Just e
expr _ = Nothing
inScope :: GHC.Located e -> Bool
inScope (GHC.L l _) =
let
(startLoc,endLoc) = case l of
(GHC.RealSrcSpan ss) ->
((GHC.srcSpanStartLine ss,GHC.srcSpanStartCol ss),
(GHC.srcSpanEndLine ss,GHC.srcSpanEndCol ss))
(GHC.UnhelpfulSpan _) -> ((0,0),(0,0))
in
(startLoc>=beginPos) && (startLoc<= endPos) && (endLoc>= beginPos) && (endLoc<=endPos)
expToNameRdr :: NameMap -> GHC.LHsExpr GHC.RdrName -> Maybe GHC.Name
#if __GLASGOW_HASKELL__ <= 710
expToNameRdr nm (GHC.L l (GHC.HsVar pnt)) = Just (rdrName2NamePure nm (GHC.L l pnt))
#else
expToNameRdr nm (GHC.L _ (GHC.HsVar pnt)) = Just (rdrName2NamePure nm pnt)
#endif
expToNameRdr nm (GHC.L _ (GHC.HsPar e)) = expToNameRdr nm e
expToNameRdr _ _ = Nothing
nameToString :: GHC.Name -> String
nameToString name = showGhcQual name
patToNameRdr :: NameMap -> GHC.LPat GHC.RdrName -> Maybe GHC.Name
#if __GLASGOW_HASKELL__ <= 710
patToNameRdr nm (GHC.L l (GHC.VarPat n)) = Just (rdrName2NamePure nm (GHC.L l n))
#else
patToNameRdr nm (GHC.L _ (GHC.VarPat n)) = Just (rdrName2NamePure nm n)
#endif
patToNameRdr _ _ = Nothing
pNtoPat :: name -> GHC.Pat name
#if __GLASGOW_HASKELL__ <= 710
pNtoPat pname = GHC.VarPat pname
#else
pNtoPat pname = GHC.VarPat (GHC.noLoc pname)
#endif