LRDE Tiger Compiler  1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cloner.hxx
Go to the documentation of this file.
1 
6 #ifndef ASTCLONE_CLONER_HXX
7 # define ASTCLONE_CLONER_HXX
8 
9 # include <ast/libast.hh>
10 # include <astclone/cloner.hh>
11 
12 namespace astclone
13 {
14  using namespace ast;
15 
16  template <typename T>
17  T*
18  Cloner::recurse(const T& t)
19  {
20  t.accept(*this);
21  T* res = dynamic_cast<T*>(result_);
22  assertion(res);
23  return res;
24  }
25 
26  template <typename T>
27  T*
28  Cloner::recurse(const T* const t)
29  {
30  T* res = nullptr;
31  if (t)
32  {
33  t->accept(*this);
34  res = dynamic_cast<T*>(result_);
35  assertion(res);
36  }
37  return res;
38  }
39 
40  template <typename CollectionType>
41  CollectionType*
42  Cloner::recurse_collection(const CollectionType& c)
43  {
44  /* FIXME: Warning, the memory allocated here is not always
45  reclaimed. Check `tc --clone/--desugar' with Valgrind.
46  Reported by Ing1 2015 students. */
47  auto res = new CollectionType;
48 
49  typedef typename CollectionType::value_type elt_type;
50  for (const elt_type& e : c)
51  {
52  e->accept(*this);
53  auto elt = dynamic_cast<elt_type>(result_);
54  assertion(elt);
55  res->push_back(elt);
56  }
57 
58  return res;
59  }
60 
61  template <typename DecsType>
62  void
63  Cloner::decs_visit(const DecsType& e)
64  {
65  const Location& location = e.location_get();
66 
67  // Shorthand.
68  typedef DecsType decs_type;
69  // The type of the list contained by this node.
70  typedef typename decs_type::Ds elt_type;
71  // The cloned list of declarations.
72  auto decs = new elt_type;
73 
74  for (const typename elt_type::value_type& i : e.decs_get())
75  {
76  i->accept(*this);
77  auto dec = dynamic_cast <typename elt_type::value_type>(result_);
78  assertion(dec);
79  decs->push_back(dec);
80  }
81  // The cloned Decs.
82  result_ = new decs_type(location, decs);
83  }
84 
85 } // namespace astclone
86 
87 #endif // !ASTCLONE_CLONER_HXX