Add ir::{lookup_data_member, get_function_parameter}
authorDodji Seketeli <dodji@redhat.com>
Thu, 21 Mar 2019 17:17:31 +0000 (18:17 +0100)
committerDodji Seketeli <dodji@redhat.com>
Thu, 21 Mar 2019 17:17:31 +0000 (18:17 +0100)
While looking at something else, I figured it's useful, for debugging
purposes, to be able to lookup a given data member of a union/class by
name, as well as a function parameter by index.

This patch adds both.

* include/abg-ir.h (lookup_data_member, get_function_parameter):
Declare new functions.
* src/abg-ir.cc (lookup_data_member, get_function_parameter):
Define them.

Signed-off-by: Dodji Seketeli <dodji@redhat.com>
include/abg-ir.h
src/abg-ir.cc

index f35cde6..be53b8d 100644 (file)
@@ -4149,6 +4149,14 @@ is_method_decl(const type_or_decl_base&);
 method_decl_sptr
 is_method_decl(const type_or_decl_base_sptr&);
 
+const var_decl*
+lookup_data_member(const type_base* type,
+                  const char* dm_name);
+
+const function_decl::parameter*
+get_function_parameter(const decl_base* fun,
+                      unsigned parm_num);
+
 /// Abstract a member function template.
 class member_function_template : public member_base, public virtual decl_base
 {
index af6fc81..0dd180c 100644 (file)
@@ -21200,6 +21200,62 @@ types_have_similar_structure(const type_base* first, const type_base* second)
   return false;
 }
 
+/// Look for a data member of a given class, struct or union type and
+/// return it.
+///
+/// The data member is designated by its name.
+///
+/// @param type the class, struct or union type to consider.
+///
+/// @param dm_name the name of the data member to lookup.
+///
+/// @return the data member iff it was found in @type or NULL if no
+/// data member with that name was found.
+const var_decl*
+lookup_data_member(const type_base* type,
+                  const char* dm_name)
+
+{
+  class_or_union *cou = is_class_or_union_type(type);
+  if (!cou)
+    return 0;
+
+  for (class_or_union::data_members::const_iterator i =
+        cou->get_data_members().begin();
+       i != cou->get_data_members().end();
+       ++i)
+    {
+      if ((*i)->get_name() == dm_name)
+       return i->get();
+    }
+  return 0;
+}
+
+/// Get the function parameter designated by its index.
+///
+/// Note that the first function parameter has index 0.
+///
+/// @param fun the function to consider.
+///
+/// @param parm_index the index of the function parameter to get.
+///
+/// @return the function parameter designated by its index, of NULL if
+/// no function parameter with that index was found.
+const function_decl::parameter*
+get_function_parameter(const decl_base* fun,
+                      unsigned parm_index)
+{
+  function_decl* fn = is_function_decl(fun);
+  if (!fn)
+    return 0;
+
+  const function_decl::parameters &parms = fn->get_type()->get_parameters();
+  if (parms.size() <= parm_index)
+    return 0;
+
+  return parms[parm_index].get();
+}
+
 bool
 ir_traversable_base::traverse(ir_node_visitor&)
 {return true;}