1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-----------------------------------------------------------------------------
-- Compute for each function the functions which call it.
--
-- Michael Hanus, April 2005
-----------------------------------------------------------------------------

module CurryBrowseAnalysis.CalledByAnalysis(calledBy) where

import FlatCurry.Types
import List

-- Computes the list of all functions that calls some function.
-- Argument: a list of function declarations
-- Result: a list of pairs of qualified functions names and the corresponding
--         caller functions
calledBy :: [FuncDecl] -> [(QName,[QName])]
calledBy funs = map cfFun funs
 where
   cfFun (Func fname _ _ _ _) = (fname, concatMap (getCalls fname) funs)

getCalls :: QName -> FuncDecl -> [QName]
getCalls _ (Func _ _ _ _ (External _)) = []
getCalls name1 (Func name2 _ _ _ (Rule _ e)) | isCalled name1 e = [name2]
                                             | otherwise        = []

isCalled :: QName -> Expr -> Bool
isCalled _ (Var _) = False
isCalled _ (Lit _) = False
isCalled name (Comb _ c es) | c==name   = True
                            | otherwise = any (isCalled name) es
isCalled name (Free _ e)   = isCalled name e
isCalled name (Let bs e) = any (isCalled name) (e : map snd bs)
isCalled name (Or e1 e2)     = (isCalled name e1) || (isCalled name e2)
isCalled name (Case _ _ ces) = any isCalledCase ces
  where isCalledCase (Branch _ e) = isCalled name e
isCalled name (Typed e _) = isCalled name e

-- end of CalledByAnalysis