PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/compiler/rename/RnEnv.lhs

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

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