summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/callgraph/call-graph-visitor.cc
blob: 6ad31c5a6e5232fbde6a30590d1515f3799f5b8c (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 ** \file callgraph/call-graph-visitor.cc
 ** \brief Implementation of callgraph::CallGraphVisitor.
 **/

#include <ast/call-exp.hh>
#include <ast/function-dec.hh>
#include <callgraph/call-graph-visitor.hh>

namespace callgraph
{
  const CallGraph* CallGraphVisitor::create(const ast::Ast& tree)
  {
    // Create a new empty callgraph
    callgraph_ = new CallGraph();

    // Launch visitor.
    tree.accept(*this);

    // Return created callgraph.
    return callgraph_;
  }

  CallGraph* CallGraphVisitor::create(ast::Ast& tree)
  {
    return const_cast<CallGraph*>(create(const_cast<const ast::Ast&>(tree)));
  }

  /*-----------.
  | Visiting.  |
  `-----------*/

  void CallGraphVisitor::operator()(const ast::CallExp& e)
  {
    // FIXME: Some code was deleted here (Link the Caller with the CallExp's declaration).
  }

  void CallGraphVisitor::operator()(const ast::FunctionChunk& e)
  {
    // First define the nodes for each defined function.
    for (ast::FunctionDec* f : e)
      callgraph_->fundec_add(f);
    // Now bind callers and callees.
    super_type::operator()(e);
  }

  void CallGraphVisitor::operator()(const ast::FunctionDec& e)
  {
    // Current function becomes temporarily the caller function.
    ast::FunctionDec* save = caller;
    caller = const_cast<ast::FunctionDec*>(&e);
    super_type::operator()(e);
    caller = save;
  }

} // namespace callgraph