@@ -18,6 +18,10 @@ import Prelude ()
1818import Prelude.Compat
1919
2020import Data.Maybe (mapMaybe)
21+import Data.List (mapAccumL)
22+import Data.Foldable (fold)
23+import qualified Data.Set as S
24+
2125import Control.Monad
2226import Control.Arrow ((***), (+++), second)
2327
@@ -25,6 +29,7 @@ import Language.PureScript.AST.Binders
2529import Language.PureScript.AST.Declarations
2630import Language.PureScript.Types
2731import Language.PureScript.Traversals
32+import Language.PureScript.Names
2833
2934everywhereOnValues :: (Declaration -> Declaration) ->
3035 (Expr -> Expr) ->
@@ -389,6 +394,108 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
389394 j' s (DoNotationLet ds) = DoNotationLet <$> traverse (f'' s) ds
390395 j' s (PositionedDoNotationElement pos com e1) = PositionedDoNotationElement pos com <$> j'' s e1
391396
397+everythingWithScope ::
398+ (Monoid r) =>
399+ (S.Set Ident -> Declaration -> r) ->
400+ (S.Set Ident -> Expr -> r) ->
401+ (S.Set Ident -> Binder -> r) ->
402+ (S.Set Ident -> CaseAlternative -> r) ->
403+ (S.Set Ident -> DoNotationElement -> r) ->
404+ S.Set Ident -> Declaration -> r
405+everythingWithScope f g h i j = f''
406+ where
407+ -- Avoid importing Data.Monoid and getting shadowed names above
408+ (<>) = mappend
409+
410+ f'' s a = f s a <> f' s a
411+
412+ f' s (DataBindingGroupDeclaration ds) =
413+ let s' = S.union s (S.fromList (mapMaybe getDeclIdent ds))
414+ in foldMap (f'' s') ds
415+ f' s (ValueDeclaration name _ bs (Right val)) =
416+ let s' = S.insert name s
417+ in foldMap (h'' s') bs <> g'' s' val
418+ f' s (ValueDeclaration name _ bs (Left gs)) =
419+ let s' = S.insert name s
420+ s'' = S.union s' (S.fromList (concatMap binderNames bs))
421+ in foldMap (h'' s') bs <> foldMap (\(grd, val) -> g'' s'' grd <> g'' s'' val) gs
422+ f' s (BindingGroupDeclaration ds) =
423+ let s' = S.union s (S.fromList (map (\(name, _, _) -> name) ds))
424+ in foldMap (\(_, _, val) -> g'' s' val) ds
425+ f' s (TypeClassDeclaration _ _ _ ds) = foldMap (f'' s) ds
426+ f' s (TypeInstanceDeclaration _ _ _ _ (ExplicitInstance ds)) = foldMap (f'' s) ds
427+ f' s (PositionedDeclaration _ _ d) = f'' s d
428+ f' _ _ = mempty
429+
430+ g'' s a = g s a <> g' s a
431+
432+ g' s (UnaryMinus v1) = g'' s v1
433+ g' s (BinaryNoParens op v1 v2) = g' s op <> g' s v1 <> g' s v2
434+ g' s (Parens v1) = g'' s v1
435+ g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
436+ g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
437+ g' s (ArrayLiteral vs) = foldMap (g'' s) vs
438+ g' s (ObjectLiteral vs) = foldMap (g'' s . snd) vs
439+ g' s (ObjectConstructor vs) = foldMap (g'' s) (mapMaybe snd vs)
440+ g' s (TypeClassDictionaryConstructorApp _ v1) = g'' s v1
441+ g' s (Accessor _ v1) = g'' s v1
442+ g' s (ObjectUpdate obj vs) = g'' s obj <> foldMap (g'' s . snd) vs
443+ g' s (ObjectUpdater obj vs) = foldMap (g'' s) obj <> foldMap (g'' s) (mapMaybe snd vs)
444+ g' s (Abs (Left name) v1) =
445+ let s' = S.insert name s
446+ in g'' s' v1
447+ g' s (Abs (Right b) v1) =
448+ let s' = S.union (S.fromList (binderNames b)) s
449+ in g'' s' v1
450+ g' s (App v1 v2) = g'' s v1 <> g'' s v2
451+ g' s (IfThenElse v1 v2 v3) = g'' s v1 <> g'' s v2 <> g'' s v3
452+ g' s (Case vs alts) = foldMap (g'' s) vs <> foldMap (i'' s) alts
453+ g' s (TypedValue _ v1 _) = g'' s v1
454+ g' s (Let ds v1) =
455+ let s' = S.union s (S.fromList (mapMaybe getDeclIdent ds))
456+ in foldMap (f'' s') ds <> g'' s' v1
457+ g' s (Do es) = fold . snd . mapAccumL j'' s $ es
458+ g' s (PositionedValue _ _ v1) = g'' s v1
459+ g' _ _ = mempty
460+
461+ h'' s a = h s a <> h' s a
462+
463+ h' s (ConstructorBinder _ bs) = foldMap (h'' s) bs
464+ h' s (ObjectBinder bs) = foldMap (h'' s . snd) bs
465+ h' s (ArrayBinder bs) = foldMap (h'' s) bs
466+ h' s (NamedBinder name b1) =
467+ let s' = S.insert name s
468+ in h'' s' b1
469+ h' s (PositionedBinder _ _ b1) = h'' s b1
470+ h' s (TypedBinder _ b1) = h'' s b1
471+ h' _ _ = mempty
472+
473+ i'' s a = i s a <> i' s a
474+
475+ i' s (CaseAlternative bs (Right val)) =
476+ let s' = S.union s (S.fromList (concatMap binderNames bs))
477+ in foldMap (h'' s) bs <> g'' s' val
478+ i' s (CaseAlternative bs (Left gs)) =
479+ let s' = S.union s (S.fromList (concatMap binderNames bs))
480+ in foldMap (h'' s) bs <> foldMap (\(grd, val) -> g'' s' grd <> g'' s' val) gs
481+
482+ j'' s a = let (s', r) = j' s a in (s', j s a <> r)
483+
484+ j' s (DoNotationValue v) = (s, g'' s v)
485+ j' s (DoNotationBind b v) =
486+ let s' = S.union (S.fromList (binderNames b)) s
487+ in (s', h'' s b <> g'' s' v)
488+ j' s (DoNotationLet ds) =
489+ let s' = S.union s (S.fromList (mapMaybe getDeclIdent ds))
490+ in (s', foldMap (f'' s') ds)
491+ j' s (PositionedDoNotationElement _ _ e1) = j'' s e1
492+
493+ getDeclIdent :: Declaration -> Maybe Ident
494+ getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
495+ getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
496+ getDeclIdent (TypeDeclaration ident _) = Just ident
497+ getDeclIdent _ = Nothing
498+
392499accumTypes :: (Monoid r) => (Type -> r) -> (Declaration -> r, Expr -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
393500accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (const mempty) (const mempty)
394501 where