PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/compiler/rename/RnEnv.lhs

https://bitbucket.org/carter/ghc
Haskell | 1724 lines | 1232 code | 235 blank | 257 comment | 68 complexity | f9374463a5822abf7d9699edac4c2f7c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. %
  2. % (c) The GRASP/AQUA Project, Glasgow University, 1992-2006
  3. %
  4. \section[RnEnv]{Environment manipulation for the renamer monad}
  5. \begin{code}
  6. module RnEnv (
  7. newTopSrcBinder,
  8. lookupLocatedTopBndrRn, lookupTopBndrRn,
  9. lookupLocatedOccRn, lookupOccRn,
  10. lookupLocalOccRn_maybe,
  11. lookupTypeOccRn, lookupKindOccRn,
  12. lookupGlobalOccRn, lookupGlobalOccRn_maybe,
  13. HsSigCtxt(..), lookupLocalTcNames, lookupSigOccRn,
  14. lookupFixityRn, lookupTyFixityRn,
  15. lookupInstDeclBndr, lookupSubBndrOcc, lookupFamInstName,
  16. greRdrName,
  17. lookupSubBndrGREs, lookupConstructorFields,
  18. lookupSyntaxName, lookupSyntaxNames, lookupIfThenElse,
  19. lookupGreRn, lookupGreLocalRn, lookupGreRn_maybe,
  20. getLookupOccRn, addUsedRdrNames,
  21. newLocalBndrRn, newLocalBndrsRn,
  22. bindLocalName, bindLocalNames, bindLocalNamesFV,
  23. MiniFixityEnv, emptyFsEnv, extendFsEnv, lookupFsEnv,
  24. addLocalFixities,
  25. bindLocatedLocalsFV, bindLocatedLocalsRn,
  26. extendTyVarEnvFVRn,
  27. checkDupRdrNames, checkShadowedRdrNames,
  28. checkDupNames, checkDupAndShadowedNames, checkTupSize,
  29. addFvRn, mapFvRn, mapMaybeFvRn, mapFvRnCPS,
  30. warnUnusedMatches,
  31. warnUnusedTopBinds, warnUnusedLocalBinds,
  32. dataTcOccs, unknownNameErr, kindSigErr, perhapsForallMsg,
  33. HsDocContext(..), docOfHsDocContext
  34. ) where
  35. #include "HsVersions.h"
  36. import LoadIface ( loadInterfaceForName, loadSrcInterface )
  37. import IfaceEnv
  38. import HsSyn
  39. import RdrName
  40. import HscTypes
  41. import TcEnv ( tcLookupDataCon, tcLookupField, isBrackStage )
  42. import TcRnMonad
  43. import Id ( isRecordSelector )
  44. import Name
  45. import NameSet
  46. import NameEnv
  47. import Avail
  48. import Module ( ModuleName, moduleName )
  49. import UniqFM
  50. import DataCon ( dataConFieldLabels, dataConTyCon )
  51. import TyCon ( isTupleTyCon, tyConArity )
  52. import PrelNames ( mkUnboundName, rOOT_MAIN, forall_tv_RDR )
  53. import ErrUtils ( MsgDoc )
  54. import SrcLoc
  55. import Outputable
  56. import Util
  57. import Maybes
  58. import ListSetOps ( removeDups )
  59. import DynFlags
  60. import FastString
  61. import Control.Monad
  62. import Data.List
  63. import qualified Data.Set as Set
  64. import Constants ( mAX_TUPLE_SIZE )
  65. \end{code}
  66. \begin{code}
  67. -- XXX
  68. thenM :: Monad a => a b -> (b -> a c) -> a c
  69. thenM = (>>=)
  70. \end{code}
  71. %*********************************************************
  72. %* *
  73. Source-code binders
  74. %* *
  75. %*********************************************************
  76. \begin{code}
  77. newTopSrcBinder :: Located RdrName -> RnM Name
  78. newTopSrcBinder (L loc rdr_name)
  79. | Just name <- isExact_maybe rdr_name
  80. = -- This is here to catch
  81. -- (a) Exact-name binders created by Template Haskell
  82. -- (b) The PrelBase defn of (say) [] and similar, for which
  83. -- the parser reads the special syntax and returns an Exact RdrName
  84. -- We are at a binding site for the name, so check first that it
  85. -- the current module is the correct one; otherwise GHC can get
  86. -- very confused indeed. This test rejects code like
  87. -- data T = (,) Int Int
  88. -- unless we are in GHC.Tup
  89. if isExternalName name then
  90. do { this_mod <- getModule
  91. ; unless (this_mod == nameModule name)
  92. (addErrAt loc (badOrigBinding rdr_name))
  93. ; return name }
  94. else -- See Note [Binders in Template Haskell] in Convert.hs
  95. do { let occ = nameOccName name
  96. ; occ `seq` return () -- c.f. seq in newGlobalBinder
  97. ; this_mod <- getModule
  98. ; updNameCache $ \ ns ->
  99. let name' = mkExternalName (nameUnique name) this_mod occ loc
  100. ns' = ns { nsNames = extendNameCache (nsNames ns) this_mod occ name' }
  101. in (ns', name') }
  102. | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
  103. = do { this_mod <- getModule
  104. ; unless (rdr_mod == this_mod || rdr_mod == rOOT_MAIN)
  105. (addErrAt loc (badOrigBinding rdr_name))
  106. -- When reading External Core we get Orig names as binders,
  107. -- but they should agree with the module gotten from the monad
  108. --
  109. -- We can get built-in syntax showing up here too, sadly. If you type
  110. -- data T = (,,,)
  111. -- the constructor is parsed as a type, and then RdrHsSyn.tyConToDataCon
  112. -- uses setRdrNameSpace to make it into a data constructors. At that point
  113. -- the nice Exact name for the TyCon gets swizzled to an Orig name.
  114. -- Hence the badOrigBinding error message.
  115. --
  116. -- Except for the ":Main.main = ..." definition inserted into
  117. -- the Main module; ugh!
  118. -- Because of this latter case, we call newGlobalBinder with a module from
  119. -- the RdrName, not from the environment. In principle, it'd be fine to
  120. -- have an arbitrary mixture of external core definitions in a single module,
  121. -- (apart from module-initialisation issues, perhaps).
  122. ; newGlobalBinder rdr_mod rdr_occ loc }
  123. --TODO, should pass the whole span
  124. | otherwise
  125. = do { unless (not (isQual rdr_name))
  126. (addErrAt loc (badQualBndrErr rdr_name))
  127. -- Binders should not be qualified; if they are, and with a different
  128. -- module name, we we get a confusing "M.T is not in scope" error later
  129. ; stage <- getStage
  130. ; if isBrackStage stage then
  131. -- We are inside a TH bracket, so make an *Internal* name
  132. -- See Note [Top-level Names in Template Haskell decl quotes] in RnNames
  133. do { uniq <- newUnique
  134. ; return (mkInternalName uniq (rdrNameOcc rdr_name) loc) }
  135. else
  136. -- Normal case
  137. do { this_mod <- getModule
  138. ; newGlobalBinder this_mod (rdrNameOcc rdr_name) loc } }
  139. \end{code}
  140. %*********************************************************
  141. %* *
  142. Source code occurrences
  143. %* *
  144. %*********************************************************
  145. Looking up a name in the RnEnv.
  146. Note [Type and class operator definitions]
  147. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. We want to reject all of these unless we have -XTypeOperators (Trac #3265)
  149. data a :*: b = ...
  150. class a :*: b where ...
  151. data (:*:) a b = ....
  152. class (:*:) a b where ...
  153. The latter two mean that we are not just looking for a
  154. *syntactically-infix* declaration, but one that uses an operator
  155. OccName. We use OccName.isSymOcc to detect that case, which isn't
  156. terribly efficient, but there seems to be no better way.
  157. \begin{code}
  158. lookupTopBndrRn :: RdrName -> RnM Name
  159. lookupTopBndrRn n = do nopt <- lookupTopBndrRn_maybe n
  160. case nopt of
  161. Just n' -> return n'
  162. Nothing -> do traceRn $ text "lookupTopBndrRn"
  163. unboundName WL_LocalTop n
  164. lookupLocatedTopBndrRn :: Located RdrName -> RnM (Located Name)
  165. lookupLocatedTopBndrRn = wrapLocM lookupTopBndrRn
  166. lookupTopBndrRn_maybe :: RdrName -> RnM (Maybe Name)
  167. -- Look up a top-level source-code binder. We may be looking up an unqualified 'f',
  168. -- and there may be several imported 'f's too, which must not confuse us.
  169. -- For example, this is OK:
  170. -- import Foo( f )
  171. -- infix 9 f -- The 'f' here does not need to be qualified
  172. -- f x = x -- Nor here, of course
  173. -- So we have to filter out the non-local ones.
  174. --
  175. -- A separate function (importsFromLocalDecls) reports duplicate top level
  176. -- decls, so here it's safe just to choose an arbitrary one.
  177. --
  178. -- There should never be a qualified name in a binding position in Haskell,
  179. -- but there can be if we have read in an external-Core file.
  180. -- The Haskell parser checks for the illegal qualified name in Haskell
  181. -- source files, so we don't need to do so here.
  182. lookupTopBndrRn_maybe rdr_name
  183. | Just name <- isExact_maybe rdr_name
  184. = do { name' <- lookupExactOcc name; return (Just name') }
  185. | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
  186. -- This deals with the case of derived bindings, where
  187. -- we don't bother to call newTopSrcBinder first
  188. -- We assume there is no "parent" name
  189. = do { loc <- getSrcSpanM
  190. ; n <- newGlobalBinder rdr_mod rdr_occ loc
  191. ; return (Just n)}
  192. | otherwise
  193. = do { -- Check for operators in type or class declarations
  194. -- See Note [Type and class operator definitions]
  195. let occ = rdrNameOcc rdr_name
  196. ; when (isTcOcc occ && isSymOcc occ)
  197. (do { op_ok <- xoptM Opt_TypeOperators
  198. ; unless op_ok (addErr (opDeclErr rdr_name)) })
  199. ; mb_gre <- lookupGreLocalRn rdr_name
  200. ; case mb_gre of
  201. Nothing -> return Nothing
  202. Just gre -> return (Just $ gre_name gre) }
  203. -----------------------------------------------
  204. lookupExactOcc :: Name -> RnM Name
  205. -- See Note [Looking up Exact RdrNames]
  206. lookupExactOcc name
  207. | Just thing <- wiredInNameTyThing_maybe name
  208. , Just tycon <- case thing of
  209. ATyCon tc -> Just tc
  210. ADataCon dc -> Just (dataConTyCon dc)
  211. _ -> Nothing
  212. , isTupleTyCon tycon
  213. = do { checkTupSize (tyConArity tycon)
  214. ; return name }
  215. | isExternalName name
  216. = return name
  217. | otherwise
  218. = do { env <- getGlobalRdrEnv
  219. ; let -- See Note [Splicing Exact names]
  220. main_occ = nameOccName name
  221. demoted_occs = case demoteOccName main_occ of
  222. Just occ -> [occ]
  223. Nothing -> []
  224. gres = [ gre | occ <- main_occ : demoted_occs
  225. , gre <- lookupGlobalRdrEnv env occ
  226. , gre_name gre == name ]
  227. ; case gres of
  228. [] -> -- See Note [Splicing Exact names]
  229. do { lcl_env <- getLocalRdrEnv
  230. ; unless (name `inLocalRdrEnvScope` lcl_env)
  231. (addErr exact_nm_err)
  232. ; return name }
  233. [gre] -> return (gre_name gre)
  234. _ -> pprPanic "lookupExactOcc" (ppr name $$ ppr gres) }
  235. where
  236. exact_nm_err = hang (ptext (sLit "The exact Name") <+> quotes (ppr name) <+> ptext (sLit "is not in scope"))
  237. 2 (vcat [ ptext (sLit "Probable cause: you used a unique Template Haskell name (NameU), ")
  238. , ptext (sLit "perhaps via newName, but did not bind it")
  239. , ptext (sLit "If that's it, then -ddump-splices might be useful") ])
  240. -----------------------------------------------
  241. lookupInstDeclBndr :: Name -> SDoc -> RdrName -> RnM Name
  242. -- This is called on the method name on the left-hand side of an
  243. -- instance declaration binding. eg. instance Functor T where
  244. -- fmap = ...
  245. -- ^^^^ called on this
  246. -- Regardless of how many unqualified fmaps are in scope, we want
  247. -- the one that comes from the Functor class.
  248. --
  249. -- Furthermore, note that we take no account of whether the
  250. -- name is only in scope qualified. I.e. even if method op is
  251. -- in scope as M.op, we still allow plain 'op' on the LHS of
  252. -- an instance decl
  253. --
  254. -- The "what" parameter says "method" or "associated type",
  255. -- depending on what we are looking up
  256. lookupInstDeclBndr cls what rdr
  257. = do { when (isQual rdr)
  258. (addErr (badQualBndrErr rdr))
  259. -- In an instance decl you aren't allowed
  260. -- to use a qualified name for the method
  261. -- (Although it'd make perfect sense.)
  262. ; lookupSubBndrOcc False -- False => we don't give deprecated
  263. -- warnings when a deprecated class
  264. -- method is defined. We only warn
  265. -- when it's used
  266. (ParentIs cls) doc rdr }
  267. where
  268. doc = what <+> ptext (sLit "of class") <+> quotes (ppr cls)
  269. -----------------------------------------------
  270. lookupFamInstName :: Maybe Name -> Located RdrName -> RnM (Located Name)
  271. -- Used for TyData and TySynonym family instances only,
  272. -- See Note [Family instance binders]
  273. lookupFamInstName (Just cls) tc_rdr -- Associated type; c.f RnBinds.rnMethodBind
  274. = wrapLocM (lookupInstDeclBndr cls (ptext (sLit "associated type"))) tc_rdr
  275. lookupFamInstName Nothing tc_rdr -- Family instance; tc_rdr is an *occurrence*
  276. = lookupLocatedOccRn tc_rdr
  277. -----------------------------------------------
  278. lookupConstructorFields :: Name -> RnM [Name]
  279. -- Look up the fields of a given constructor
  280. -- * For constructors from this module, use the record field env,
  281. -- which is itself gathered from the (as yet un-typechecked)
  282. -- data type decls
  283. --
  284. -- * For constructors from imported modules, use the *type* environment
  285. -- since imported modles are already compiled, the info is conveniently
  286. -- right there
  287. lookupConstructorFields con_name
  288. = do { this_mod <- getModule
  289. ; if nameIsLocalOrFrom this_mod con_name then
  290. do { RecFields field_env _ <- getRecFieldEnv
  291. ; return (lookupNameEnv field_env con_name `orElse` []) }
  292. else
  293. do { con <- tcLookupDataCon con_name
  294. ; return (dataConFieldLabels con) } }
  295. -----------------------------------------------
  296. -- Used for record construction and pattern matching
  297. -- When the -XDisambiguateRecordFields flag is on, take account of the
  298. -- constructor name to disambiguate which field to use; it's just the
  299. -- same as for instance decls
  300. --
  301. -- NB: Consider this:
  302. -- module Foo where { data R = R { fld :: Int } }
  303. -- module Odd where { import Foo; fld x = x { fld = 3 } }
  304. -- Arguably this should work, because the reference to 'fld' is
  305. -- unambiguous because there is only one field id 'fld' in scope.
  306. -- But currently it's rejected.
  307. lookupSubBndrOcc :: Bool
  308. -> Parent -- NoParent => just look it up as usual
  309. -- ParentIs p => use p to disambiguate
  310. -> SDoc -> RdrName
  311. -> RnM Name
  312. lookupSubBndrOcc warnIfDeprec parent doc rdr_name
  313. | Just n <- isExact_maybe rdr_name -- This happens in derived code
  314. = lookupExactOcc n
  315. | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
  316. = lookupOrig rdr_mod rdr_occ
  317. | otherwise -- Find all the things the rdr-name maps to
  318. = do { -- and pick the one with the right parent namep
  319. env <- getGlobalRdrEnv
  320. ; case lookupSubBndrGREs env parent rdr_name of
  321. -- NB: lookupGlobalRdrEnv, not lookupGRE_RdrName!
  322. -- The latter does pickGREs, but we want to allow 'x'
  323. -- even if only 'M.x' is in scope
  324. [gre] -> do { addUsedRdrName warnIfDeprec gre (used_rdr_name gre)
  325. -- Add a usage; this is an *occurrence* site
  326. ; return (gre_name gre) }
  327. [] -> do { addErr (unknownSubordinateErr doc rdr_name)
  328. ; return (mkUnboundName rdr_name) }
  329. gres -> do { addNameClashErrRn rdr_name gres
  330. ; return (gre_name (head gres)) } }
  331. where
  332. -- Note [Usage for sub-bndrs]
  333. used_rdr_name gre
  334. | isQual rdr_name = rdr_name
  335. | otherwise = greRdrName gre
  336. greRdrName :: GlobalRdrElt -> RdrName
  337. greRdrName gre
  338. = case gre_prov gre of
  339. LocalDef -> unqual_rdr
  340. Imported is -> used_rdr_name_from_is is
  341. where
  342. occ = nameOccName (gre_name gre)
  343. unqual_rdr = mkRdrUnqual occ
  344. used_rdr_name_from_is imp_specs -- rdr_name is unqualified
  345. | not (all (is_qual . is_decl) imp_specs)
  346. = unqual_rdr -- An unqualified import is available
  347. | otherwise
  348. = -- Only qualified imports available, so make up
  349. -- a suitable qualifed name from the first imp_spec
  350. ASSERT( not (null imp_specs) )
  351. mkRdrQual (is_as (is_decl (head imp_specs))) occ
  352. lookupSubBndrGREs :: GlobalRdrEnv -> Parent -> RdrName -> [GlobalRdrElt]
  353. -- If Parent = NoParent, just do a normal lookup
  354. -- If Parent = Parent p then find all GREs that
  355. -- (a) have parent p
  356. -- (b) for Unqual, are in scope qualified or unqualified
  357. -- for Qual, are in scope with that qualification
  358. lookupSubBndrGREs env parent rdr_name
  359. = case parent of
  360. NoParent -> pickGREs rdr_name gres
  361. ParentIs p
  362. | isUnqual rdr_name -> filter (parent_is p) gres
  363. | otherwise -> filter (parent_is p) (pickGREs rdr_name gres)
  364. where
  365. gres = lookupGlobalRdrEnv env (rdrNameOcc rdr_name)
  366. parent_is p (GRE { gre_par = ParentIs p' }) = p == p'
  367. parent_is _ _ = False
  368. \end{code}
  369. Note [Family instance binders]
  370. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  371. Consider
  372. data family F a
  373. data instance F T = X1 | X2
  374. The 'data instance' decl has an *occurrence* of F (and T), and *binds*
  375. X1 and X2. (This is unlike a normal data type declaration which would
  376. bind F too.) So we want an AvailTC F [X1,X2].
  377. Now consider a similar pair:
  378. class C a where
  379. data G a
  380. instance C S where
  381. data G S = Y1 | Y2
  382. The 'data G S' *binds* Y1 and Y2, and has an *occurrence* of G.
  383. But there is a small complication: in an instance decl, we don't use
  384. qualified names on the LHS; instead we use the class to disambiguate.
  385. Thus:
  386. module M where
  387. import Blib( G )
  388. class C a where
  389. data G a
  390. instance C S where
  391. data G S = Y1 | Y2
  392. Even though there are two G's in scope (M.G and Blib.G), the occurence
  393. of 'G' in the 'instance C S' decl is unambiguous, becuase C has only
  394. one associated type called G. This is exactly what happens for methods,
  395. and it is only consistent to do the same thing for types. That's the
  396. role of the function lookupTcdName; the (Maybe Name) give the class of
  397. the encloseing instance decl, if any.
  398. Note [Looking up Exact RdrNames]
  399. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  400. Exact RdrNames are generated by Template Haskell. See Note [Binders
  401. in Template Haskell] in Convert.
  402. For data types and classes have Exact system Names in the binding
  403. positions for constructors, TyCons etc. For example
  404. [d| data T = MkT Int |]
  405. when we splice in and Convert to HsSyn RdrName, we'll get
  406. data (Exact (system Name "T")) = (Exact (system Name "MkT")) ...
  407. These System names are generated by Convert.thRdrName
  408. But, constructors and the like need External Names, not System Names!
  409. So we do the following
  410. * In RnEnv.newGlobalBinder we spot Exact RdrNames that wrap a
  411. non-External Name, and make an External name for it. This is
  412. the name that goes in the GlobalRdrEnv
  413. * When looking up an occurrence of an Exact name, done in
  414. RnEnv.lookupExactOcc, we find the Name with the right unique in the
  415. GlobalRdrEnv, and use the one from the envt -- it will be an
  416. External Name in the case of the data type/constructor above.
  417. * Exact names are also use for purely local binders generated
  418. by TH, such as \x_33. x_33
  419. Both binder and occurrence are Exact RdrNames. The occurrence
  420. gets looked up in the LocalRdrEnv by RnEnv.lookupOccRn, and
  421. misses, because lookupLocalRdrEnv always returns Nothing for
  422. an Exact Name. Now we fall through to lookupExactOcc, which
  423. will find the Name is not in the GlobalRdrEnv, so we just use
  424. the Exact supplied Name.
  425. Note [Splicing Exact names]
  426. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  427. Consider the splice $(do { x <- newName "x"; return (VarE x) })
  428. This will generate a (HsExpr RdrName) term that mentions the
  429. Exact RdrName "x_56" (or whatever), but does not bind it. So
  430. when looking such Exact names we want to check that it's in scope,
  431. otherwise the type checker will get confused. To do this we need to
  432. keep track of all the Names in scope, and the LocalRdrEnv does just that;
  433. we consult it with RdrName.inLocalRdrEnvScope.
  434. There is another wrinkle. With TH and -XDataKinds, consider
  435. $( [d| data Nat = Zero
  436. data T = MkT (Proxy 'Zero) |] )
  437. After splicing, but before renaming we get this:
  438. data Nat_77{tc} = Zero_78{d}
  439. data T_79{tc} = MkT_80{d} (Proxy 'Zero_78{tc}) |] )
  440. THe occurrence of 'Zero in the data type for T has the right unique,
  441. but it has a TcClsName name-space in its OccName. (This is set by
  442. the ctxt_ns argument of Convert.thRdrName.) When we check that is
  443. in scope in the GlobalRdrEnv, we need to look up the DataName namespace
  444. too. (An alternative would be to make the GlobalRdrEnv also have
  445. a Name -> GRE mapping.)
  446. Note [Usage for sub-bndrs]
  447. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  448. If you have this
  449. import qualified M( C( f ) )
  450. instance M.C T where
  451. f x = x
  452. then is the qualified import M.f used? Obviously yes.
  453. But the RdrName used in the instance decl is unqualified. In effect,
  454. we fill in the qualification by looking for f's whose class is M.C
  455. But when adding to the UsedRdrNames we must make that qualification
  456. explicit (saying "used M.f"), otherwise we get "Redundant import of M.f".
  457. So we make up a suitable (fake) RdrName. But be careful
  458. import qualifed M
  459. import M( C(f) )
  460. instance C T where
  461. f x = x
  462. Here we want to record a use of 'f', not of 'M.f', otherwise
  463. we'll miss the fact that the qualified import is redundant.
  464. --------------------------------------------------
  465. -- Occurrences
  466. --------------------------------------------------
  467. \begin{code}
  468. getLookupOccRn :: RnM (Name -> Maybe Name)
  469. getLookupOccRn
  470. = getLocalRdrEnv `thenM` \ local_env ->
  471. return (lookupLocalRdrOcc local_env . nameOccName)
  472. lookupLocatedOccRn :: Located RdrName -> RnM (Located Name)
  473. lookupLocatedOccRn = wrapLocM lookupOccRn
  474. lookupLocalOccRn_maybe :: RdrName -> RnM (Maybe Name)
  475. -- Just look in the local environment
  476. lookupLocalOccRn_maybe rdr_name
  477. = do { local_env <- getLocalRdrEnv
  478. ; return (lookupLocalRdrEnv local_env rdr_name) }
  479. -- lookupOccRn looks up an occurrence of a RdrName
  480. lookupOccRn :: RdrName -> RnM Name
  481. lookupOccRn rdr_name = do
  482. opt_name <- lookupOccRn_maybe rdr_name
  483. maybe (unboundName WL_Any rdr_name) return opt_name
  484. lookupKindOccRn :: RdrName -> RnM Name
  485. -- Looking up a name occurring in a kind
  486. lookupKindOccRn rdr_name
  487. = do { mb_name <- lookupOccRn_maybe rdr_name
  488. ; case mb_name of
  489. Just name -> return name
  490. Nothing -> unboundName WL_Any rdr_name }
  491. -- lookupPromotedOccRn looks up an optionally promoted RdrName.
  492. lookupTypeOccRn :: RdrName -> RnM Name
  493. -- see Note [Demotion]
  494. lookupTypeOccRn rdr_name
  495. = do { mb_name <- lookupOccRn_maybe rdr_name
  496. ; case mb_name of {
  497. Just name -> return name ;
  498. Nothing -> lookup_demoted rdr_name } }
  499. lookup_demoted :: RdrName -> RnM Name
  500. lookup_demoted rdr_name
  501. | Just demoted_rdr <- demoteRdrName rdr_name
  502. -- Maybe it's the name of a *data* constructor
  503. = do { data_kinds <- xoptM Opt_DataKinds
  504. ; mb_demoted_name <- lookupOccRn_maybe demoted_rdr
  505. ; case mb_demoted_name of
  506. Nothing -> unboundName WL_Any rdr_name
  507. Just demoted_name
  508. | data_kinds -> return demoted_name
  509. | otherwise -> unboundNameX WL_Any rdr_name suggest_dk }
  510. | otherwise
  511. = unboundName WL_Any rdr_name
  512. where
  513. suggest_dk = ptext (sLit "A data constructor of that name is in scope; did you mean -XDataKinds?")
  514. \end{code}
  515. Note [Demotion]
  516. ~~~~~~~~~~~~~~~
  517. When the user writes:
  518. data Nat = Zero | Succ Nat
  519. foo :: f Zero -> Int
  520. 'Zero' in the type signature of 'foo' is parsed as:
  521. HsTyVar ("Zero", TcClsName)
  522. When the renamer hits this occurence of 'Zero' it's going to realise
  523. that it's not in scope. But because it is renaming a type, it knows
  524. that 'Zero' might be a promoted data constructor, so it will demote
  525. its namespace to DataName and do a second lookup.
  526. The final result (after the renamer) will be:
  527. HsTyVar ("Zero", DataName)
  528. \begin{code}
  529. -- lookupOccRn looks up an occurrence of a RdrName
  530. lookupOccRn_maybe :: RdrName -> RnM (Maybe Name)
  531. lookupOccRn_maybe rdr_name
  532. = do { local_env <- getLocalRdrEnv
  533. ; case lookupLocalRdrEnv local_env rdr_name of {
  534. Just name -> return (Just name) ;
  535. Nothing -> do
  536. { mb_name <- lookupGlobalOccRn_maybe rdr_name
  537. ; case mb_name of {
  538. Just name -> return (Just name) ;
  539. Nothing -> do
  540. { -- We allow qualified names on the command line to refer to
  541. -- *any* name exported by any module in scope, just as if there
  542. -- was an "import qualified M" declaration for every module.
  543. -- But we DONT allow it under Safe Haskell as we need to check
  544. -- imports. We can and should instead check the qualified import
  545. -- but at the moment this requires some refactoring so leave as a TODO
  546. ; dflags <- getDynFlags
  547. ; let allow_qual = dopt Opt_ImplicitImportQualified dflags &&
  548. not (safeDirectImpsReq dflags)
  549. ; is_ghci <- getIsGHCi
  550. -- This test is not expensive,
  551. -- and only happens for failed lookups
  552. ; if isQual rdr_name && allow_qual && is_ghci
  553. then lookupQualifiedName rdr_name
  554. else do { traceRn (text "lookupOccRn" <+> ppr rdr_name)
  555. ; return Nothing } } } } } }
  556. lookupGlobalOccRn :: RdrName -> RnM Name
  557. -- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
  558. -- environment. Adds an error message if the RdrName is not in scope.
  559. lookupGlobalOccRn rdr_name
  560. = do { mb_name <- lookupGlobalOccRn_maybe rdr_name
  561. ; case mb_name of
  562. Just n -> return n
  563. Nothing -> do { traceRn (text "lookupGlobalOccRn" <+> ppr rdr_name)
  564. ; unboundName WL_Global rdr_name } }
  565. lookupGlobalOccRn_maybe :: RdrName -> RnM (Maybe Name)
  566. -- No filter function; does not report an error on failure
  567. lookupGlobalOccRn_maybe rdr_name
  568. | Just n <- isExact_maybe rdr_name -- This happens in derived code
  569. = do { n' <- lookupExactOcc n; return (Just n') }
  570. | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
  571. = do { n <- lookupOrig rdr_mod rdr_occ
  572. ; return (Just n) }
  573. | otherwise
  574. = do { mb_gre <- lookupGreRn_maybe rdr_name
  575. ; case mb_gre of
  576. Nothing -> return Nothing
  577. Just gre -> return (Just (gre_name gre)) }
  578. --------------------------------------------------
  579. -- Lookup in the Global RdrEnv of the module
  580. --------------------------------------------------
  581. lookupGreRn_maybe :: RdrName -> RnM (Maybe GlobalRdrElt)
  582. -- Just look up the RdrName in the GlobalRdrEnv
  583. lookupGreRn_maybe rdr_name
  584. = lookupGreRn_help rdr_name (lookupGRE_RdrName rdr_name)
  585. lookupGreRn :: RdrName -> RnM GlobalRdrElt
  586. -- If not found, add error message, and return a fake GRE
  587. lookupGreRn rdr_name
  588. = do { mb_gre <- lookupGreRn_maybe rdr_name
  589. ; case mb_gre of {
  590. Just gre -> return gre ;
  591. Nothing -> do
  592. { traceRn (text "lookupGreRn" <+> ppr rdr_name)
  593. ; name <- unboundName WL_Global rdr_name
  594. ; return (GRE { gre_name = name, gre_par = NoParent,
  595. gre_prov = LocalDef }) }}}
  596. lookupGreLocalRn :: RdrName -> RnM (Maybe GlobalRdrElt)
  597. -- Similar, but restricted to locally-defined things
  598. lookupGreLocalRn rdr_name
  599. = lookupGreRn_help rdr_name lookup_fn
  600. where
  601. lookup_fn env = filter isLocalGRE (lookupGRE_RdrName rdr_name env)
  602. lookupGreRn_help :: RdrName -- Only used in error message
  603. -> (GlobalRdrEnv -> [GlobalRdrElt]) -- Lookup function
  604. -> RnM (Maybe GlobalRdrElt)
  605. -- Checks for exactly one match; reports deprecations
  606. -- Returns Nothing, without error, if too few
  607. lookupGreRn_help rdr_name lookup
  608. = do { env <- getGlobalRdrEnv
  609. ; case lookup env of
  610. [] -> return Nothing
  611. [gre] -> do { addUsedRdrName True gre rdr_name
  612. ; return (Just gre) }
  613. gres -> do { addNameClashErrRn rdr_name gres
  614. ; return (Just (head gres)) } }
  615. \end{code}
  616. %*********************************************************
  617. %* *
  618. Deprecations
  619. %* *
  620. %*********************************************************
  621. Note [Handling of deprecations]
  622. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  623. * We report deprecations at each *occurrence* of the deprecated thing
  624. (see Trac #5867)
  625. * We do not report deprectations for locally-definded names. For a
  626. start, we may be exporting a deprecated thing. Also we may use a
  627. deprecated thing in the defn of another deprecated things. We may
  628. even use a deprecated thing in the defn of a non-deprecated thing,
  629. when changing a module's interface.
  630. * addUsedRdrNames: we do not report deprecations for sub-binders:
  631. - the ".." completion for records
  632. - the ".." in an export item 'T(..)'
  633. - the things exported by a module export 'module M'
  634. \begin{code}
  635. addUsedRdrName :: Bool -> GlobalRdrElt -> RdrName -> RnM ()
  636. -- Record usage of imported RdrNames
  637. addUsedRdrName warnIfDeprec gre rdr
  638. | isLocalGRE gre = return () -- No call to warnIfDeprecated
  639. -- See Note [Handling of deprecations]
  640. | otherwise = do { env <- getGblEnv
  641. ; when warnIfDeprec $ warnIfDeprecated gre
  642. ; updMutVar (tcg_used_rdrnames env)
  643. (\s -> Set.insert rdr s) }
  644. addUsedRdrNames :: [RdrName] -> RnM ()
  645. -- Record used sub-binders
  646. -- We don't check for imported-ness here, because it's inconvenient
  647. -- and not stritly necessary.
  648. -- NB: no call to warnIfDeprecated; see Note [Handling of deprecations]
  649. addUsedRdrNames rdrs
  650. = do { env <- getGblEnv
  651. ; updMutVar (tcg_used_rdrnames env)
  652. (\s -> foldr Set.insert s rdrs) }
  653. warnIfDeprecated :: GlobalRdrElt -> RnM ()
  654. warnIfDeprecated gre@(GRE { gre_name = name, gre_prov = Imported (imp_spec : _) })
  655. = do { dflags <- getDynFlags
  656. ; when (wopt Opt_WarnWarningsDeprecations dflags) $
  657. do { iface <- loadInterfaceForName doc name
  658. ; case lookupImpDeprec iface gre of
  659. Just txt -> addWarn (mk_msg txt)
  660. Nothing -> return () } }
  661. where
  662. mk_msg txt = sep [ sep [ ptext (sLit "In the use of")
  663. <+> pprNonVarNameSpace (occNameSpace (nameOccName name))
  664. <+> quotes (ppr name)
  665. , parens imp_msg <> colon ]
  666. , ppr txt ]
  667. name_mod = ASSERT2( isExternalName name, ppr name ) nameModule name
  668. imp_mod = importSpecModule imp_spec
  669. imp_msg = ptext (sLit "imported from") <+> ppr imp_mod <> extra
  670. extra | imp_mod == moduleName name_mod = empty
  671. | otherwise = ptext (sLit ", but defined in") <+> ppr name_mod
  672. doc = ptext (sLit "The name") <+> quotes (ppr name) <+> ptext (sLit "is mentioned explicitly")
  673. warnIfDeprecated _ = return () -- No deprecations for things defined locally
  674. lookupImpDeprec :: ModIface -> GlobalRdrElt -> Maybe WarningTxt
  675. lookupImpDeprec iface gre
  676. = mi_warn_fn iface (gre_name gre) `mplus` -- Bleat if the thing,
  677. case gre_par gre of -- or its parent, is warn'd
  678. ParentIs p -> mi_warn_fn iface p
  679. NoParent -> Nothing
  680. \end{code}
  681. Note [Used names with interface not loaded]
  682. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  683. It's (just) possible to to find a used
  684. Name whose interface hasn't been loaded:
  685. a) It might be a WiredInName; in that case we may not load
  686. its interface (although we could).
  687. b) It might be GHC.Real.fromRational, or GHC.Num.fromInteger
  688. These are seen as "used" by the renamer (if -XRebindableSyntax)
  689. is on), but the typechecker may discard their uses
  690. if in fact the in-scope fromRational is GHC.Read.fromRational,
  691. (see tcPat.tcOverloadedLit), and the typechecker sees that the type
  692. is fixed, say, to GHC.Base.Float (see Inst.lookupSimpleInst).
  693. In that obscure case it won't force the interface in.
  694. In both cases we simply don't permit deprecations;
  695. this is, after all, wired-in stuff.
  696. %*********************************************************
  697. %* *
  698. GHCi support
  699. %* *
  700. %*********************************************************
  701. \begin{code}
  702. -- A qualified name on the command line can refer to any module at all: we
  703. -- try to load the interface if we don't already have it.
  704. lookupQualifiedName :: RdrName -> RnM (Maybe Name)
  705. lookupQualifiedName rdr_name
  706. | Just (mod,occ) <- isQual_maybe rdr_name
  707. -- Note: we want to behave as we would for a source file import here,
  708. -- and respect hiddenness of modules/packages, hence loadSrcInterface.
  709. = loadSrcInterface doc mod False Nothing `thenM` \ iface ->
  710. case [ name
  711. | avail <- mi_exports iface,
  712. name <- availNames avail,
  713. nameOccName name == occ ] of
  714. (n:ns) -> ASSERT (null ns) return (Just n)
  715. _ -> do { traceRn (text "lookupQualified" <+> ppr rdr_name)
  716. ; return Nothing }
  717. | otherwise
  718. = pprPanic "RnEnv.lookupQualifiedName" (ppr rdr_name)
  719. where
  720. doc = ptext (sLit "Need to find") <+> ppr rdr_name
  721. \end{code}
  722. Note [Looking up signature names]
  723. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  724. lookupSigOccRn is used for type signatures and pragmas
  725. Is this valid?
  726. module A
  727. import M( f )
  728. f :: Int -> Int
  729. f x = x
  730. It's clear that the 'f' in the signature must refer to A.f
  731. The Haskell98 report does not stipulate this, but it will!
  732. So we must treat the 'f' in the signature in the same way
  733. as the binding occurrence of 'f', using lookupBndrRn
  734. However, consider this case:
  735. import M( f )
  736. f :: Int -> Int
  737. g x = x
  738. We don't want to say 'f' is out of scope; instead, we want to
  739. return the imported 'f', so that later on the reanamer will
  740. correctly report "misplaced type sig".
  741. Note [Signatures for top level things]
  742. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  743. data HsSigCtxt = ... | TopSigCtxt NameSet Bool | ....
  744. * The NameSet says what is bound in this group of bindings.
  745. We can't use isLocalGRE from the GlobalRdrEnv, because of this:
  746. f x = x
  747. $( ...some TH splice... )
  748. f :: Int -> Int
  749. When we encounter the signature for 'f', the binding for 'f'
  750. will be in the GlobalRdrEnv, and will be a LocalDef. Yet the
  751. signature is mis-placed
  752. * The Bool says whether the signature is ok for a class method
  753. or record selector. Consider
  754. infix 3 `f` -- Yes, ok
  755. f :: C a => a -> a -- No, not ok
  756. class C a where
  757. f :: a -> a
  758. \begin{code}
  759. data HsSigCtxt
  760. = TopSigCtxt NameSet Bool -- At top level, binding these names
  761. -- See Note [Signatures for top level things]
  762. -- Bool <=> ok to give sig for
  763. -- class method or record selctor
  764. | LocalBindCtxt NameSet -- In a local binding, binding these names
  765. | ClsDeclCtxt Name -- Class decl for this class
  766. | InstDeclCtxt Name -- Intsance decl for this class
  767. | HsBootCtxt -- Top level of a hs-boot file
  768. lookupSigOccRn :: HsSigCtxt
  769. -> Sig RdrName
  770. -> Located RdrName -> RnM (Located Name)
  771. lookupSigOccRn ctxt sig
  772. = wrapLocM $ \ rdr_name ->
  773. do { mb_name <- lookupBindGroupOcc ctxt (hsSigDoc sig) rdr_name
  774. ; case mb_name of
  775. Left err -> do { addErr err; return (mkUnboundName rdr_name) }
  776. Right name -> return name }
  777. lookupBindGroupOcc :: HsSigCtxt
  778. -> SDoc
  779. -> RdrName -> RnM (Either MsgDoc Name)
  780. -- Looks up the RdrName, expecting it to resolve to one of the
  781. -- bound names passed in. If not, return an appropriate error message
  782. --
  783. -- See Note [Looking up signature names]
  784. lookupBindGroupOcc ctxt what rdr_name
  785. | Just n <- isExact_maybe rdr_name
  786. = do { n' <- lookupExactOcc n
  787. ; return (Right n') } -- Maybe we should check the side conditions
  788. -- but it's a pain, and Exact things only show
  789. -- up when you know what you are doing
  790. | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
  791. = do { n' <- lookupOrig rdr_mod rdr_occ
  792. ; return (Right n') }
  793. | otherwise
  794. = case ctxt of
  795. HsBootCtxt -> lookup_top (const True) True
  796. TopSigCtxt ns meth_ok -> lookup_top (`elemNameSet` ns) meth_ok
  797. LocalBindCtxt ns -> lookup_group ns
  798. ClsDeclCtxt cls -> lookup_cls_op cls
  799. InstDeclCtxt cls -> lookup_cls_op cls
  800. where
  801. lookup_cls_op cls
  802. = do { env <- getGlobalRdrEnv
  803. ; let gres = lookupSubBndrGREs env (ParentIs cls) rdr_name
  804. ; case gres of
  805. [] -> return (Left (unknownSubordinateErr doc rdr_name))
  806. (gre:_) -> return (Right (gre_name gre)) }
  807. -- If there is more than one local GRE for the
  808. -- same OccName 'f', that will be reported separately
  809. -- as a duplicate top-level binding for 'f'
  810. where
  811. doc = ptext (sLit "method of class") <+> quotes (ppr cls)
  812. lookup_top keep_me meth_ok
  813. = do { env <- getGlobalRdrEnv
  814. ; let all_gres = lookupGlobalRdrEnv env (rdrNameOcc rdr_name)
  815. ; case filter (keep_me . gre_name) all_gres of
  816. [] | null all_gres -> bale_out_with empty
  817. | otherwise -> bale_out_with local_msg
  818. (gre:_)
  819. | ParentIs {} <- gre_par gre
  820. , not meth_ok
  821. -> bale_out_with sub_msg
  822. | otherwise
  823. -> return (Right (gre_name gre)) }
  824. lookup_group bound_names -- Look in the local envt (not top level)
  825. = do { local_env <- getLocalRdrEnv
  826. ; case lookupLocalRdrEnv local_env rdr_name of
  827. Just n
  828. | n `elemNameSet` bound_names -> return (Right n)
  829. | otherwise -> bale_out_with local_msg
  830. Nothing -> bale_out_with empty }
  831. bale_out_with msg
  832. = return (Left (sep [ ptext (sLit "The") <+> what
  833. <+> ptext (sLit "for") <+> quotes (ppr rdr_name)
  834. , nest 2 $ ptext (sLit "lacks an accompanying binding")]
  835. $$ nest 2 msg))
  836. local_msg = parens $ ptext (sLit "The") <+> what <+> ptext (sLit "must be given where")
  837. <+> quotes (ppr rdr_name) <+> ptext (sLit "is declared")
  838. sub_msg = parens $ ptext (sLit "You cannot give a") <+> what
  839. <+> ptext (sLit "for a record selector or class method")
  840. ---------------
  841. lookupLocalTcNames :: HsSigCtxt -> SDoc -> RdrName -> RnM [Name]
  842. -- GHC extension: look up both the tycon and data con or variable.
  843. -- Used for top-level fixity signatures and deprecations.
  844. -- Complain if neither is in scope.
  845. -- See Note [Fixity signature lookup]
  846. lookupLocalTcNames ctxt what rdr_name
  847. = do { mb_gres <- mapM lookup (dataTcOccs rdr_name)
  848. ; let (errs, names) = splitEithers mb_gres
  849. ; when (null names) $ addErr (head errs) -- Bleat about one only
  850. ; return names }
  851. where
  852. lookup = lookupBindGroupOcc ctxt what
  853. dataTcOccs :: RdrName -> [RdrName]
  854. -- Return both the given name and the same name promoted to the TcClsName
  855. -- namespace. This is useful when we aren't sure which we are looking at.
  856. dataTcOccs rdr_name
  857. | Just n <- isExact_maybe rdr_name
  858. , not (isBuiltInSyntax n) -- See Note [dataTcOccs and Exact Names]
  859. = [rdr_name]
  860. | isDataOcc occ || isVarOcc occ
  861. = [rdr_name, rdr_name_tc]
  862. | otherwise
  863. = [rdr_name]
  864. where
  865. occ = rdrNameOcc rdr_name
  866. rdr_name_tc = setRdrNameSpace rdr_name tcName
  867. \end{code}
  868. Note [dataTcOccs and Exact Names]
  869. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  870. Exact RdrNames can occur in code generated by Template Haskell, and generally
  871. those references are, well, exact, so it's wrong to return the TyClsName too.
  872. But there is an awkward exception for built-in syntax. Example in GHCi
  873. :info []
  874. This parses as the Exact RdrName for nilDataCon, but we also want
  875. the list type constructor.
  876. Note that setRdrNameSpace on an Exact name requires the Name to be External,
  877. which it always is for built in syntax.
  878. %*********************************************************
  879. %* *
  880. Fixities
  881. %* *
  882. %*********************************************************
  883. Note [Fixity signature lookup]
  884. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  885. A fixity declaration like
  886. infixr 2 ?
  887. can refer to a value-level operator, e.g.:
  888. (?) :: String -> String -> String
  889. or a type-level operator, like:
  890. data (?) a b = A a | B b
  891. so we extend the lookup of the reader name '?' to the TcClsName namespace, as
  892. well as the original namespace.
  893. The extended lookup is also used in other places, like resolution of
  894. deprecation declarations, and lookup of names in GHCi.
  895. \begin{code}
  896. --------------------------------
  897. type FastStringEnv a = UniqFM a -- Keyed by FastString
  898. emptyFsEnv :: FastStringEnv a
  899. lookupFsEnv :: FastStringEnv a -> FastString -> Maybe a
  900. extendFsEnv :: FastStringEnv a -> FastString -> a -> FastStringEnv a
  901. emptyFsEnv = emptyUFM
  902. lookupFsEnv = lookupUFM
  903. extendFsEnv = addToUFM
  904. --------------------------------
  905. type MiniFixityEnv = FastStringEnv (Located Fixity)
  906. -- Mini fixity env for the names we're about
  907. -- to bind, in a single binding group
  908. --
  909. -- It is keyed by the *FastString*, not the *OccName*, because
  910. -- the single fixity decl infix 3 T
  911. -- affects both the data constructor T and the type constrctor T
  912. --
  913. -- We keep the location so that if we find
  914. -- a duplicate, we can report it sensibly
  915. --------------------------------
  916. -- Used for nested fixity decls to bind names along with their fixities.
  917. -- the fixities are given as a UFM from an OccName's FastString to a fixity decl
  918. addLocalFixities :: MiniFixityEnv -> [Name] -> RnM a -> RnM a
  919. addLocalFixities mini_fix_env names thing_inside
  920. = extendFixityEnv (mapCatMaybes find_fixity names) thing_inside
  921. where
  922. find_fixity name
  923. = case lookupFsEnv mini_fix_env (occNameFS occ) of
  924. Just (L _ fix) -> Just (name, FixItem occ fix)
  925. Nothing -> Nothing
  926. where
  927. occ = nameOccName name
  928. \end{code}
  929. --------------------------------
  930. lookupFixity is a bit strange.
  931. * Nested local fixity decls are put in the local fixity env, which we
  932. find with getFixtyEnv
  933. * Imported fixities are found in the HIT or PIT
  934. * Top-level fixity decls in this module may be for Names that are
  935. either Global (constructors, class operations)
  936. or Local/Exported (everything else)
  937. (See notes with RnNames.getLocalDeclBinders for why we have this split.)
  938. We put them all in the local fixity environment
  939. \begin{code}
  940. lookupFixityRn :: Name -> RnM Fixity
  941. lookupFixityRn name
  942. = getModule `thenM` \ this_mod ->
  943. if nameIsLocalOrFrom this_mod name
  944. then do -- It's defined in this module
  945. local_fix_env <- getFixityEnv
  946. traceRn (text "lookupFixityRn: looking up name in local environment:" <+>
  947. vcat [ppr name, ppr local_fix_env])
  948. return $ lookupFixity local_fix_env name
  949. else -- It's imported
  950. -- For imported names, we have to get their fixities by doing a
  951. -- loadInterfaceForName, and consulting the Ifaces that comes back
  952. -- from that, because the interface file for the Name might not
  953. -- have been loaded yet. Why not? Suppose you import module A,
  954. -- which exports a function 'f', thus;
  955. -- module CurrentModule where
  956. -- import A( f )
  957. -- module A( f ) where
  958. -- import B( f )
  959. -- Then B isn't loaded right away (after all, it's possible that
  960. -- nothing from B will be used). When we come across a use of
  961. -- 'f', we need to know its fixity, and it's then, and only
  962. -- then, that we load B.hi. That is what's happening here.
  963. --
  964. -- loadInterfaceForName will find B.hi even if B is a hidden module,
  965. -- and that's what we want.
  966. loadInterfaceForName doc name `thenM` \ iface -> do {
  967. traceRn (text "lookupFixityRn: looking up name in iface cache and found:" <+>
  968. vcat [ppr name, ppr $ mi_fix_fn iface (nameOccName name)]);
  969. return (mi_fix_fn iface (nameOccName name))
  970. }
  971. where
  972. doc = ptext (sLit "Checking fixity for") <+> ppr name
  973. ---------------
  974. lookupTyFixityRn :: Located Name -> RnM Fixity
  975. lookupTyFixityRn (L _ n) = lookupFixityRn n
  976. \end{code}
  977. %************************************************************************
  978. %* *
  979. Rebindable names
  980. Dealing with rebindable syntax is driven by the
  981. Opt_RebindableSyntax dynamic flag.
  982. In "deriving" code we don't want to use rebindable syntax
  983. so we switch off the flag locally
  984. %* *
  985. %************************************************************************
  986. Haskell 98 says that when you say "3" you get the "fromInteger" from the
  987. Standard Prelude, regardless of what is in scope. However, to experiment
  988. with having a language that is less coupled to the standard prelude, we're
  989. trying a non-standard extension that instead gives you whatever "Prelude.fromInteger"
  990. happens to be in scope. Then you can
  991. import Prelude ()
  992. import MyPrelude as Prelude
  993. to get the desired effect.
  994. At the moment this just happens for
  995. * fromInteger, fromRational on literals (in expressions and patterns)
  996. * negate (in expressions)
  997. * minus (arising from n+k patterns)
  998. * "do" notation
  999. We store the relevant Name in the HsSyn tree, in
  1000. * HsIntegral/HsFractional/HsIsString
  1001. * NegApp
  1002. * NPlusKPat
  1003. * HsDo
  1004. respectively. Initially, we just store the "standard" name (PrelNames.fromIntegralName,
  1005. fromRationalName etc), but the renamer changes this to the appropriate user
  1006. name if Opt_NoImplicitPrelude is on. That is what lookupSyntaxName does.
  1007. We treat the orignal (standard) names as free-vars too, because the type checker
  1008. checks the type of the user thing against the type of the standard thing.
  1009. \begin{code}
  1010. lookupIfThenElse :: RnM (Maybe (SyntaxExpr Name), FreeVars)
  1011. -- Different to lookupSyntaxName because in the non-rebindable
  1012. -- case we desugar directly rather than calling an existing function
  1013. -- Hence the (Maybe (SyntaxExpr Name)) return type
  1014. lookupIfThenElse
  1015. = do { rebind <- xoptM Opt_RebindableSyntax
  1016. ; if not rebind
  1017. then return (Nothing, emptyFVs)
  1018. else do { ite <- lookupOccRn (mkVarUnqual (fsLit "ifThenElse"))
  1019. ; return (Just (HsVar ite), unitFV ite) } }
  1020. lookupSyntaxName :: Name -- The standard name
  1021. -> RnM (SyntaxExpr Name, FreeVars) -- Possibly a non-standard name
  1022. lookupSyntaxName std_name
  1023. = do { rebindable_on <- xoptM Opt_RebindableSyntax
  1024. ; if not rebindable_on then
  1025. return (HsVar std_name, emptyFVs)
  1026. else
  1027. -- Get the similarly named thing from the local environment
  1028. do { usr_name <- lookupOccRn (mkRdrUnqual (nameOccName std_name))
  1029. ; return (HsVar usr_name, unitFV usr_name) } }
  1030. lookupSyntaxNames :: [Name] -- Standard names
  1031. -> RnM ([HsExpr Name], FreeVars) -- See comments with HsExpr.ReboundNames
  1032. lookupSyntaxNames std_names
  1033. = do { rebindable_on <- xoptM Opt_RebindableSyntax
  1034. ; if not rebindable_on then
  1035. return (map HsVar std_names, emptyFVs)
  1036. else
  1037. do { usr_names <- mapM (lookupOccRn . mkRdrUnqual . nameOccName) std_names
  1038. ; return (map HsVar usr_names, mkFVs usr_names) } }
  1039. \end{code}
  1040. %*********************************************************
  1041. %* *
  1042. \subsection{Binding}
  1043. %* *
  1044. %*********************************************************
  1045. \begin{code}
  1046. newLocalBndrRn :: Located RdrName -> RnM Name
  1047. -- Used for non-top-level binders. These should
  1048. -- never be qualified.
  1049. newLocalBndrRn (L loc rdr_name)
  1050. | Just name <- isExact_maybe rdr_name
  1051. = return name -- This happens in code generated by Template Haskell
  1052. -- See Note [Binders in Template Haskell] in Convert.lhs
  1053. | otherwise
  1054. = do { unless (isUnqual rdr_name)
  1055. (addErrAt loc (badQualBndrErr rdr_name))
  1056. ; uniq <- newUnique
  1057. ; return (mkInternalName uniq (rdrNameOcc rdr_name) loc) }
  1058. newLocalBndrsRn :: [Located RdrName] -> RnM [Name]
  1059. newLocalBndrsRn = mapM newLocalBndrRn
  1060. ---------------------
  1061. bindLocatedLocalsRn :: [Located RdrName]
  1062. -> ([Name] -> RnM a)
  1063. -> RnM a
  1064. bindLocatedLocalsRn rdr_names_w_loc enclosed_scope
  1065. = do { checkDupRdrNames rdr_names_w_loc
  1066. ; checkShadowedRdrNames rdr_names_w_loc
  1067. -- Make fresh Names and extend the environment
  1068. ; names <- newLocalBndrsRn rdr_names_w_loc
  1069. ; bindLocalNames names (enclosed_scope names) }
  1070. bindLocalNames :: [Name] -> RnM a -> RnM a
  1071. bin

Large files files are truncated, but you can click here to view the full file