libabigail/include/abg-fwd.h
Dodji Seketeli 8b28d171c3 Canonicalize types either early or late after TU reading
While trying to diff two identical files (abidiff foo.so foo.so) it
appeared that canonicalizing types during e.g, the DWARF reading
process was leading to subtle errors because it's extremely hard to
know when a type is complete.  That is, during the building of a class
type C, a pointer to C can be built before C is complete.  Worse, even
after reading the DIE (from DWARF) of class C, there can be DIE seen
later in the translation unit that modifies type C.  In these late
cases, one needs to wait -- not only until C is fully built, but also
sometimes, after the translation unit is fully built -- to
canonicalize C and then the pointer to C.  This kind of things.

So now there are two possible points in time when canonicalization of
a type can happen.  It can happen early, when the type is built.  This
is the case for basic types and composite types for which all
sub-types are canonicalized already.  It can happen late, right after
we've finished reading the debug info for the current translation
unit.

So this patch fixes the IR traversal and uses that to walk the
translation unit (or even types) after it's built.  It does away with
the first attempt to perform early canonicalizing only.

The patch also handles type canonicalizing while reading xml-abi
format.

	* include/abg-fwd.h (is_class_type)
	(type_has_non_canonicalized_subtype): Declare new functions.
	(is_member_type): Remove the overload that takes a decl_base_sptr.
	It's superfluous.  We just need the one that takes a
	type_base_sptr.
	* include/abg-ir.h (translation_unit::{is_constructed,
	set_is_constructed}): Add new methods.
	(class_decl::has_virtual_member_functions): Likewise.
	(class decl_base): Makes it virtually inherit ir_traversable_base.
	(class type_base): Make this virtually inherit traversable_base
	too.
	(type_base::canonicalize): Renamed enable_canonical_equality
	into this.
	(type_base::traverse): Declare new virtual method.
	(canonicalize): Renamed enable_canonical_equality into this.
	(scope_type_decl::traverse): Declare new virtual method.
	(namespace_decl::get_pretty_representation): Declare new virtual
	method.
	(function_type::traverse): Likewise.
	(class_decl::base_spec::traverse): Likewise.
	(ir_node_visitor::visit): Remove the overloads and replace each of
	them with a pair of ...
	(ir_node_visitor::{visit_begin, visit_end}): ... of these.
	* include/abg-traverse.h (traversable_base::visiting): New
	method.
	(traversable_base::visiting_): New data member.
	(traversable_base::traversable_base): New constructor.
	* src/abg-ir.cc ({scope_decl, type_decl, namespace_decl,
	qualified_type_def, pointer_type_def, reference_type_def,
	array_type_def, enum_type_decl, typedef_decl, var_decl,
	function_decl, function_decl::parameter, class_decl,
	class_decl::member_function_template,
	class_decl::member_class_template, function_tdecl,
	class_tdecl}::traverse): Fix this to properly set the
	traversable_base::visiting_ flag and to reflect the new signatures
	of the ir_node_visitor methods.
	({type_base, scope_type_decl, function_type,
	class_decl::base_spec}::traverse): New method.
	(type_base::get_canonical_type_for): Handle the case of the type
	already having a canonical type.  Properly hash the type using the
	dynamic type hasher.  Look through declaration-only classes to
	consider the definition of the class instead.  Fix logic to have a
	single pointer of return, to ease debugging.
	(canonicalize): Renamed enable_canonical_equality into this.
	(namespace_decl::get_pretty_representation): Define new method.
	(ir_node_visitor::visit): Replace each of these overloads with a
	pair of visit_begin/visit_end ones.
	(translation_unit::priv::is_constructed_): New data member.
	(translation_unit::priv::priv): Initialize it.
	(translation_unit::{is_constructed, set_is_constructed}): Define
	new methods.
	(is_member_type(const decl_base_sptr)): Remove.
	(is_class_type(decl_base *d)): Define new function.
	(class_decl::has_virtual_member_functions): Define new method.
	(equals(const class_decl&, const class_decl&, change_kind*)): If
	the containing translation unit is not constructed yet, do not
	take virtual member functions in account when comparing the
	classes.  This is because when reading from DWARF, there can be
	DIEs that change the number of virtual member functions after the
	DIE of the class.  So one needs to start taking virtual members
	into account only after the translation unit has been constructed.
	(class non_canonicalized_subtype_detector): Define new type.
	(type_has_non_canonicalized_subtype): Define new function.
	* src/abg-corpus.cc (symtab_build_visitor_type::visit): Renamed
	this into symtab_build_visitor_type::visit_end.
	* src/abg-dwarf-reader.cc (die_type_map_type): New typedef.
	(die_class_map_type): This is now a typedef on a map of
	Dwarf_Off/class_decl_sptr.
	(read_context::{die_type_map_, alternate_die_type_map_,
	types_to_canonicalize_, alt_types_to_canonicalize_}): New data
	members.
	(read_context::{associate_die_to_decl,
	associate_die_to_decl_primary}): Make these methods public.
	(read_context::{associate_die_to_type,
	lookup_type_from_die_offset, is_wip_class_die_offset,
	types_to_canonicalize, schedule_type_for_canonicalization}):
	Define new methods.
	(build_type_decl, build_enum_type)
	(build_class_type_and_add_to_ir, build_qualified_type)
	(build_pointer_type_def, build_reference_type, build_array_type)
	(build_typedef_type, build_function_decl): Do not canonicalize
	types here.
	(maybe_canonicalize_type): Define new function.
	(build_ir_node_from_die): Take a new flag that says if the ir node
	is a member type/function or not. Early-canonicalize base types.
	Canonicalize composite types that have only canonicalized
	sub-types.  Schedule the other types for late canonicalizing.  For
	class types, early canonicalize those that are non-member types,
	that are fully constructed and that have only canonicalized
	sub-types.  Adjust to the new signature of build_ir_node_from_die.
	(get_scope_for_die, build_namespace_decl_and_add_to_ir)
	(build_qualified_type, build_pointer_type_def)
	(build_reference_type, build_array_type, build_typedef_type)
	(build_var_decl, build_function_decl): Adjust for the new
	signature of build_ir_node_from_die.
	(build_translation_unit_and_add_to_ir): Likewise.  Perform the
	late canonicalizing of the types that have been scheduled for
	that.
	(build_class_type_and_add_to_ir): Return a class_decl_sptr, not a
	decl_base_sptr.  Adjust for the new signature of
	build_ir_node_from_die.  Early canonicalize member types that are
	created and added to a given class, or schedule them for late
	canonicalizing.
	* src/abg-reader.cc (class read_context::{m_wip_classes_map,
	m_types_to_canonicalize}): New data members.
	(read_context::{clear_types_to_canonicalize,
	clear_wip_classes_map, mark_class_as_wip, unmark_class_as_wip,
	is_wip_class, maybe_canonicalize_type,
	schedule_type_for_late_canonicalizing,
	perform_late_type_canonicalizing}): Add new method definitions.
	(read_context::clear_per_translation_unit_data): Call
	read_context::clear_types_to_canonicalize().
	(read_translation_unit_from_input): Call
	read_context::perform_late_type_canonicalizing() at the end of the
	function.
	(build_function_decl): Fix the function type canonicalizing (per
	translation) that was already in place.  Do the canonicalizing of
	these only when the type is fully built.  Oops.  This was really
	brokend.  Also, when the function type is constructed, consider it
	for type canonicalizing.
	(build_type_decl): Early canonicalize basic types.
	(build_qualified_type_decl, build_pointer_type_def)
	(build_pointer_type_def, build_reference_type_def)
	(build_array_type_def, build_enum_type_decl, build_typedef_decl):
	Handle the canonicalizing for these composite types: either early
	or late.
	(build_class_decl): Likewise.  Also, mark this class a 'being
	built' until it's fully built.  This helps the canonicalizing code
	to know that it should leave a class alone until it's fully built.
	* tests/test-ir-walker.cc (struct name_printing_visitor): Adjust
	to the visitor methods naming change.
	* configure.ac: Generate the tests/runtestcanonicalizetypes.sh
	testing script from tests/runtestcanonicalizetypes.sh.in.
	* tests/runtestcanonicalizetypes.sh.in: Add the template for the
	new runtestcanonicalizetypes.sh script that test for type
	canonicalizing.
	* tests/Makefile.am: Add the new runtestcanonicalizetypes.sh
	regression testing script to the build system.

Signed-off-by: Dodji Seketeli <dodji@redhat.com>
2015-02-18 21:32:37 +01:00

526 lines
11 KiB
C++

// -*- Mode: C++ -*-
//
// Copyright (C) 2013-2015 Red Hat, Inc.
//
// This file is part of the GNU Application Binary Interface Generic
// Analysis and Instrumentation Library (libabigail). This library is
// free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3, or (at your option) any
// later version.
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this program; see the file COPYING-LGPLV3. If
// not, see <http://www.gnu.org/licenses/>.
/// @file
#ifndef __ABG_IRFWD_H__
#define __ABG_IRFWD_H__
#include <cstddef>
#include <tr1/memory>
#include <list>
#include <vector>
#include <string>
#include <tr1/functional>
#include <typeinfo>
#include <utility> // for std::rel_ops, at least.
#include <ostream>
#include "abg-hash.h"
/// Toplevel namespace for libabigail.
namespace abigail
{
/**
@mainpage libabigail
This is the API documentation of the Application Binary
Interface Generic Analysis and Instrumentation Library, aka,
<em>libabigail</em>.
Check out <a href="http://sourceware.org/libabigail"> the project
homepage</a>!
The current libabigail source code can be browsed at
http://sourceware.org/git/gitweb.cgi?p=libabigail.git
It can be checked out with:
<em>git clone git://sourceware.org/git/libabigail.git</em>
The mailing list to send messages and patches to is
libabigail@sourceware.org.
You can hang out with libabigail developers and users on irc at
irc://irc.oftc.net\#libabigail.
*/
// Inject some types.
using std::tr1::shared_ptr;
using std::tr1::weak_ptr;
using std::string;
using std::vector;
// Pull in relational operators.
using namespace std::rel_ops;
namespace ir
{
// Forward declarations for corpus.
class corpus;
// Forward declarations for ir.
class location;
class location_manager;
class translation_unit;
class class_decl;
class class_tdecl;
class decl_base;
class enum_type_decl;
class function_decl;
class function_tdecl;
class function_type;
class global_scope;
class node_visitor;
class location;
class location_manager;
class method_type;
class namespace_decl;
class parameter;
class pointer_type_def;
class qualified_type_def;
class reference_type_def;
class scope_decl;
class scope_type_decl;
class template_decl;
class template_parameter;
class non_type_tparameter;
class type_tparameter;
class template_tparameter;
class type_composition;
class type_base;
class type_decl;
class typedef_decl;
class var_decl;
class array_type_def;
class subrange_type;
struct type_shared_ptr_equal;
struct traversable_base;
shared_ptr<decl_base>
add_decl_to_scope(shared_ptr<decl_base>, scope_decl*);
shared_ptr<decl_base>
add_decl_to_scope (shared_ptr<decl_base>, shared_ptr<scope_decl>);
const global_scope*
get_global_scope(const decl_base&);
const global_scope*
get_global_scope(const decl_base*);
const global_scope*
get_global_scope(const shared_ptr<decl_base>);
translation_unit*
get_translation_unit(const decl_base&);
translation_unit*
get_translation_unit(const decl_base*);
translation_unit*
get_translation_unit(const shared_ptr<decl_base>);
bool
is_global_scope(const scope_decl&);
bool
is_global_scope(const scope_decl*);
bool
is_global_scope(const shared_ptr<scope_decl>);
bool
is_at_global_scope(const decl_base&);
bool
is_at_global_scope(const shared_ptr<decl_base>);
bool
is_at_class_scope(const shared_ptr<decl_base>);
bool
is_at_class_scope(const decl_base*);
bool
is_at_class_scope(const decl_base&);
bool
is_at_template_scope(const shared_ptr<decl_base>);
bool
is_template_parameter(const shared_ptr<decl_base>);
shared_ptr<function_decl>
is_function_decl(shared_ptr<decl_base>);
bool
is_type(const decl_base&);
shared_ptr<type_base>
is_type(const shared_ptr<decl_base>);
shared_ptr<type_decl>
is_type_decl(const shared_ptr<type_base>);
shared_ptr<typedef_decl>
is_typedef(const shared_ptr<type_base>);
shared_ptr<typedef_decl>
is_typedef(const shared_ptr<decl_base>);
shared_ptr<enum_type_decl>
is_enum_type(const shared_ptr<type_base>&);
shared_ptr<enum_type_decl>
is_enum_type(const shared_ptr<decl_base>&);
bool
is_class_type(decl_base*);
shared_ptr<class_decl>
is_class_type(const shared_ptr<type_base>);
shared_ptr<class_decl>
is_class_type(const shared_ptr<decl_base>);
shared_ptr<pointer_type_def>
is_pointer_type(const shared_ptr<type_base>);
shared_ptr<reference_type_def>
is_reference_type(const shared_ptr<type_base>);
shared_ptr<qualified_type_def>
is_qualified_type(const shared_ptr<type_base>);
shared_ptr<function_type>
is_function_type(const shared_ptr<type_base>);
shared_ptr<method_type>
is_method_type(const shared_ptr<type_base>);
shared_ptr<class_decl>
look_through_decl_only_class(shared_ptr<class_decl>);
shared_ptr<var_decl>
is_var_decl(const shared_ptr<decl_base>);
bool
is_template_parm_composition_type(const shared_ptr<decl_base>);
bool
is_template_decl(const shared_ptr<decl_base>);
bool
is_function_template_pattern(const shared_ptr<decl_base>);
shared_ptr<decl_base>
add_decl_to_scope(shared_ptr<decl_base>, scope_decl*);
shared_ptr<decl_base>
add_decl_to_scope(shared_ptr<decl_base>, shared_ptr<scope_decl>);
shared_ptr<decl_base>
insert_decl_into_scope(shared_ptr<decl_base>,
vector<shared_ptr<decl_base> >::iterator,
scope_decl*);
shared_ptr<decl_base>
insert_decl_into_scope(shared_ptr<decl_base>,
vector<shared_ptr<decl_base> >::iterator,
shared_ptr<scope_decl>);
bool
has_scope(const decl_base&);
bool
has_scope(const shared_ptr<decl_base>);
bool
is_member_decl(const shared_ptr<decl_base>);
bool
is_member_decl(const decl_base*);
bool
is_member_decl(const decl_base&);
bool
is_member_type(const shared_ptr<type_base>);
void
remove_decl_from_scope(shared_ptr<decl_base>);
bool
get_member_is_static(const decl_base&);
bool
get_member_is_static(const decl_base*);
bool
get_member_is_static(const shared_ptr<decl_base>);
void
set_member_is_static(decl_base&, bool);
void
set_member_is_static(shared_ptr<decl_base>, bool);
bool
is_data_member(const var_decl&);
bool
is_data_member(const var_decl*);
bool
is_data_member(const shared_ptr<var_decl>);
shared_ptr<var_decl>
is_data_member(const shared_ptr<decl_base>&);
shared_ptr<array_type_def>
is_array_type(const shared_ptr<type_base> decl);
void
set_data_member_offset(shared_ptr<var_decl>, size_t);
size_t
get_data_member_offset(const var_decl&);
size_t
get_data_member_offset(const shared_ptr<var_decl>);
size_t
get_data_member_offset(const shared_ptr<decl_base>);
void
set_data_member_is_laid_out(shared_ptr<var_decl>, bool);
bool
get_data_member_is_laid_out(const var_decl&);
bool
get_data_member_is_laid_out(const shared_ptr<var_decl>);
bool
is_member_function(const function_decl&);
bool
is_member_function(const function_decl*);
bool
is_member_function(const shared_ptr<function_decl>);
bool
get_member_function_is_ctor(const function_decl&);
bool
get_member_function_is_ctor(const shared_ptr<function_decl>);
void
set_member_function_is_ctor(const function_decl&, bool);
void
set_member_function_is_ctor(const shared_ptr<function_decl>, bool);
bool
get_member_function_is_dtor(const function_decl&);
bool
get_member_function_is_dtor(const shared_ptr<function_decl>);
void
set_member_function_is_dtor(const function_decl&, bool);
void
set_member_function_is_dtor(const shared_ptr<function_decl>, bool);
bool
get_member_function_is_const(const function_decl&);
bool
get_member_function_is_const(const shared_ptr<function_decl>);
void
set_member_function_is_const(const function_decl&, bool);
void
set_member_function_is_const(const shared_ptr<function_decl>, bool);
size_t
get_member_function_vtable_offset(const function_decl&);
size_t
get_member_function_vtable_offset(const shared_ptr<function_decl>);
void
set_member_function_vtable_offset(const function_decl& f,
size_t s);
void
set_member_function_vtable_offset(const shared_ptr<function_decl> f,
size_t s);
bool
get_member_function_is_virtual(const function_decl&);
bool
get_member_function_is_virtual(const shared_ptr<function_decl>);
bool
get_member_function_is_virtual(const function_decl*);
void
set_member_function_is_virtual(const function_decl&, bool);
void
set_member_function_is_virtual(const shared_ptr<function_decl>&, bool);
shared_ptr<type_base>
strip_typedef(const shared_ptr<type_base>);
string
get_type_name(const shared_ptr<type_base>);
string
get_pretty_representation(const decl_base*);
string
get_pretty_representation(const type_base*);
string
get_pretty_representation(const shared_ptr<decl_base>&);
string
get_pretty_representation(const shared_ptr<type_base>&);
const decl_base*
get_type_declaration(const type_base*);
decl_base*
get_type_declaration(type_base*);
shared_ptr<decl_base>
get_type_declaration(const shared_ptr<type_base>);
bool
types_are_compatible(const shared_ptr<type_base>,
const shared_ptr<type_base>);
bool
types_are_compatible(const shared_ptr<decl_base>,
const shared_ptr<decl_base>);
const scope_decl*
get_top_most_scope_under(const decl_base*,
const scope_decl*);
const scope_decl*
get_top_most_scope_under(const shared_ptr<decl_base>,
const scope_decl*);
const scope_decl*
get_top_most_scope_under(const shared_ptr<decl_base>,
const shared_ptr<scope_decl>);
void
fqn_to_components(const std::string&,
std::list<string>&);
const shared_ptr<decl_base>
lookup_type_in_translation_unit(const string&,
const translation_unit&);
const shared_ptr<decl_base>
lookup_type_in_translation_unit(const std::list<string>&,
const translation_unit&);
const shared_ptr<decl_base>
lookup_type_in_scope(const string&,
const shared_ptr<scope_decl>);
const shared_ptr<decl_base>
lookup_type_in_scope(const std::list<string>&,
const shared_ptr<scope_decl>);
const shared_ptr<decl_base>
lookup_var_decl_in_scope(const string&,
const shared_ptr<scope_decl>);
const shared_ptr<decl_base>
lookup_var_decl_in_scope(const std::list<string>&,
const shared_ptr<scope_decl>);
string
demangle_cplus_mangled_name(const string&);
shared_ptr<type_base>
type_or_void(const shared_ptr<type_base>);
bool
type_has_non_canonicalized_subtype(shared_ptr<type_base> t);
} // end namespace ir
using namespace abigail::ir;
void
dump(const shared_ptr<decl_base>, std::ostream&);
void
dump(const shared_ptr<decl_base>);
void
dump(const shared_ptr<type_base>, std::ostream&);
void
dump(const shared_ptr<type_base>);
void
dump(const shared_ptr<var_decl>, std::ostream&);
void
dump(const shared_ptr<var_decl>);
void
dump(const translation_unit&, std::ostream&);
void
dump(const translation_unit&);
void
dump(const shared_ptr<translation_unit>, std::ostream&);
void
dump(const shared_ptr<translation_unit>);
void
dump_decl_location(const decl_base&);
void
dump_decl_location(const decl_base*);
void
dump_decl_location(const shared_ptr<decl_base>&);
} // end namespace abigail
#endif // __ABG_IRFWD_H__