-================ Occurrences =====================
-
-Every time we get an occurrence of a name we put it in one of two lists:
- one for "compulsory" occurrences
- one for "optional" occurrences
-
-The significance of "compulsory" is
- (a) we *must* find the declaration
- (b) in the case of type or class names, the name is part of the
- source level program, and we must slurp in any instance decls
- involving it.
-
-We don't need instance decls "optional" names, because the type inference
-process will never come across them. Optional names are buried inside
-type checked (but not renamed) cross-module unfoldings and such.
-
-The pair of lists is held in a mutable variable in RnDown.
-
-The lists are kept separate so that we can process all the compulsory occurrences
-before any of the optional ones. Why? Because suppose we processed an optional
-"g", and slurped an interface decl of g::T->T. Then we'd rename the type T->T in
-optional mode. But if we later need g compulsorily we'll find that it's already
-been slurped and will do nothing. We could, I suppose, rename it a second time,
-but it seems simpler just to do all the compulsory ones first.
-
-\begin{code}
-addOccurrenceName :: Name -> RnMS s Name -- Same name returned as passed
-addOccurrenceName name (RnDown {rn_loc = loc, rn_occs = occs_var})
- (SDown {rn_mode = mode})
- | isLocallyDefinedName name ||
- not_necessary necessity
- = returnSST name
-
- | otherwise
- = readMutVarSST occs_var `thenSST` \ (comp_occs, opt_occs) ->
- let
- new_occ_pair = case necessity of
- Optional -> (comp_occs, (name,loc):opt_occs)
- Compulsory -> ((name,loc):comp_occs, opt_occs)
- in
- writeMutVarSST occs_var new_occ_pair `thenSST_`
- returnSST name
- where
- necessity = modeToNecessity mode
-
-
-addOccurrenceNames :: [Name] -> RnMS s ()
-addOccurrenceNames names (RnDown {rn_loc = loc, rn_occs = occs_var})
- (SDown {rn_mode = mode})
- | not_necessary necessity
- = returnSST ()
-
- | otherwise
- = readMutVarSST occs_var `thenSST` \ (comp_occs, opt_occs) ->
- let
- new_occ_pair = case necessity of
- Optional -> (comp_occs, non_local_occs ++ opt_occs)
- Compulsory -> (non_local_occs ++ comp_occs, opt_occs)
- in
- writeMutVarSST occs_var new_occ_pair
- where
- non_local_occs = [(name, loc) | name <- names, not (isLocallyDefinedName name)]
- necessity = modeToNecessity mode
-
- -- Never look for optional things if we're
- -- ignoring optional input interface information
-not_necessary Compulsory = False
-not_necessary Optional = opt_IgnoreIfacePragmas
-
-popOccurrenceName :: RnMode -> RnM s d (Maybe Occurrence)
-popOccurrenceName mode (RnDown {rn_occs = occs_var}) l_down
- = readMutVarSST occs_var `thenSST` \ occs ->
- case (mode, occs) of
- -- Find a compulsory occurrence
- (InterfaceMode Compulsory, (comp:comps, opts))
- -> writeMutVarSST occs_var (comps, opts) `thenSST_`
- returnSST (Just comp)
-
- -- Find an optional occurrence
- -- We shouldn't be looking unless we've done all the compulsories
- (InterfaceMode Optional, (comps, opt:opts))
- -> ASSERT2( null comps, ppr comps )
- writeMutVarSST occs_var (comps, opts) `thenSST_`
- returnSST (Just opt)
-
- -- No suitable occurrence
- other -> returnSST Nothing
-
--- discardOccurrencesRn does the enclosed thing with a *fresh* occurrences
--- variable, and discards the list of occurrences thus found. It's useful
--- when loading instance decls and specialisation signatures, when we want to
--- know the names of the things in the types, but we don't want to treat them
--- as occurrences.
-
-discardOccurrencesRn :: RnM s d a -> RnM s d a
-discardOccurrencesRn enclosed_thing rn_down l_down
- = newMutVarSST ([],[]) `thenSST` \ new_occs_var ->
- enclosed_thing (rn_down {rn_occs = new_occs_var}) l_down
-\end{code}
-
-