libabigail
abg-ir.cc
Go to the documentation of this file.
1 // C++ -*-
2 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3 // -*- Mode: C++ -*-
4 
5 // Copyright (C) 2013-2023 Red Hat, Inc.
6 //
7 //Author: Dodji Seketeli
8 
9 /// @file
10 ///
11 /// Definitions for the Internal Representation artifacts of libabigail.
12 
13 #include <cxxabi.h>
14 #include <algorithm>
15 #include <cstdint>
16 #include <functional>
17 #include <iterator>
18 #include <memory>
19 #include <sstream>
20 #include <typeinfo>
21 #include <unordered_map>
22 #include <utility>
23 #include <vector>
24 
25 #include "abg-internal.h"
26 // <headers defining libabigail's API go under here>
27 ABG_BEGIN_EXPORT_DECLARATIONS
28 
29 #include "abg-interned-str.h"
30 #include "abg-ir.h"
31 #include "abg-corpus.h"
32 #include "abg-regex.h"
33 
34 ABG_END_EXPORT_DECLARATIONS
35 // </headers defining libabigail's API>
36 
37 #include "abg-corpus-priv.h"
38 #include "abg-tools-utils.h"
39 #include "abg-comp-filter.h"
40 #include "abg-ir-priv.h"
41 
42 namespace
43 {
44 /// This internal type is a tree walking that is used to set the
45 /// qualified name of a tree of decls and types. It used by the
46 /// function update_qualified_name().
47 class qualified_name_setter : public abigail::ir::ir_node_visitor
48 {
49 
50 public:
51  bool
52  do_update(abigail::ir::decl_base* d);
53 
54  bool
55  visit_begin(abigail::ir::decl_base* d);
56 
57  bool
58  visit_begin(abigail::ir::type_base* d);
59 }; // end class qualified_name_setter
60 
61 }// end anon namespace
62 
63 namespace abigail
64 {
65 
66 // Inject.
67 using std::string;
68 using std::list;
69 using std::vector;
70 using std::unordered_map;
71 using std::dynamic_pointer_cast;
72 using std::static_pointer_cast;
73 
74 /// Convenience typedef for a map of string -> string*.
75 typedef unordered_map<string, string*> pool_map_type;
76 
77 /// The type of the private data structure of type @ref
78 /// intered_string_pool.
79 struct interned_string_pool::priv
80 {
81  pool_map_type map;
82 }; //end struc struct interned_string_pool::priv
83 
84 /// Default constructor.
86  : priv_(new priv)
87 {
88  priv_->map[""] = 0;
89 }
90 
91 /// Test if the interned string pool already contains a string with a
92 /// given value.
93 ///
94 /// @param s the string to test for.
95 ///
96 /// @return true if the pool contains a string with the value @p s.
97 bool
99 {return priv_->map.find(s) != priv_->map.end();}
100 
101 /// Get a pointer to the interned string which has a given value.
102 ///
103 /// @param s the value of the interned string to look for.
104 ///
105 /// @return a pointer to the raw string of characters which has the
106 /// value of @p s. Or null if no string with value @p s was interned.
107 const char*
109 {
110  unordered_map<string, string*>::const_iterator i =
111  priv_->map.find(s);
112  if (i == priv_->map.end())
113  return 0;
114  if (i->second)
115  return i->second->c_str();
116  return "";
117 }
118 
119 /// Create an interned string with a given value.
120 ///
121 /// @param str_value the value of the interned string to create.
122 ///
123 /// @return the new created instance of @ref interned_string created.
125 interned_string_pool::create_string(const std::string& str_value)
126 {
127  string*& result = priv_->map[str_value];
128  if (!result && !str_value.empty())
129  result = new string(str_value);
130  return interned_string(result);
131 }
132 
133 /// Destructor.
135 {
136  for (pool_map_type::iterator i = priv_->map.begin();
137  i != priv_->map.end();
138  ++i)
139  if (i->second)
140  delete i->second;
141 }
142 
143 /// Equality operator.
144 ///
145 /// @param l the instance of std::string on the left-hand-side of the
146 /// equality operator.
147 ///
148 /// @param r the instance of @ref interned_string on the
149 /// right-hand-side of the equality operator.
150 ///
151 /// @return true iff the two string are equal.
152 bool
153 operator==(const std::string& l, const interned_string& r)
154 {return r.operator==(l);}
155 
156 bool
157 operator!=(const std::string& l, const interned_string& r)
158 {return !(l == r);}
159 
160 /// Streaming operator.
161 ///
162 /// Streams an instance of @ref interned_string to an output stream.
163 ///
164 /// @param o the destination output stream.
165 ///
166 /// @param s the instance of @ref interned_string to stream out.
167 ///
168 /// @return the output stream this function just streamed to.
169 std::ostream&
170 operator<<(std::ostream& o, const interned_string& s)
171 {
172  o << static_cast<std::string>(s);
173  return o;
174 }
175 
176 /// Concatenation operator.
177 ///
178 /// Concatenate two instances of @ref interned_string, builds an
179 /// instance of std::string with the resulting string and return it.
180 ///
181 /// @param s1 the first string to consider.
182 ///
183 /// @param s2 the second string to consider.
184 ///
185 /// @return the resuting concatenated string.
186 std::string
187 operator+(const interned_string& s1,const std::string& s2)
188 {return static_cast<std::string>(s1) + s2;}
189 
190 /// Concatenation operator.
191 ///
192 /// Concatenate two instances of @ref interned_string, builds an
193 /// instance of std::string with the resulting string and return it.
194 ///
195 /// @param s1 the first string to consider.
196 ///
197 /// @param s2 the second string to consider.
198 ///
199 /// @return the resuting concatenated string.
200 std::string
201 operator+(const std::string& s1, const interned_string& s2)
202 {return s1 + static_cast<std::string>(s2);}
203 
204 namespace ir
205 {
206 
207 static size_t
208 hash_as_canonical_type_or_constant(const type_base *t);
209 
210 static bool
211 has_generic_anonymous_internal_type_name(const decl_base *d);
212 
213 static interned_string
214 get_generic_anonymous_internal_type_name(const decl_base *d);
215 
216 static string
217 get_internal_integral_type_name(const type_base*);
218 
219 static void
220 update_qualified_name(decl_base * d);
221 
222 static void
223 update_qualified_name(decl_base_sptr d);
224 
225 static interned_string
226 pointer_declaration_name(const type_base* ptr,
227  const string& variable_name,
228  bool qualified, bool internal);
229 
230 static interned_string
231 pointer_declaration_name(const type_base_sptr& ptr,
232  const string& variable_name,
233  bool qualified, bool internal);
234 
235 static interned_string
236 ptr_to_mbr_declaration_name(const ptr_to_mbr_type* ptr,
237  const string& variable_name,
238  bool qualified, bool internal);
239 
240 static interned_string
241 ptr_to_mbr_declaration_name(const ptr_to_mbr_type_sptr& ptr,
242  const string& variable_name,
243  bool qualified, bool internal);
244 
245 static interned_string
246 array_declaration_name(const array_type_def* array,
247  const string& variable_name,
248  bool qualified, bool internal);
249 
250 static interned_string
251 array_declaration_name(const array_type_def_sptr& array,
252  const string& variable_name,
253  bool qualified, bool internal);
254 
255 static void
256 stream_pretty_representation_of_fn_parms(const function_type& fn_type,
257  ostream& o, bool qualified,
258  bool internal);
259 static string
260 add_outer_pointer_to_fn_type_expr(const type_base* pointer_to_fn,
261  const string& input, bool qualified,
262  bool internal);
263 
264 static string
265 add_outer_pointer_to_fn_type_expr(const type_base_sptr& pointer_to_fn,
266  const string& input, bool qualified,
267  bool internal);
268 
269 static string
270 add_outer_pointer_to_array_type_expr(const type_base* pointer_to_ar,
271  const string& input, bool qualified,
272  bool internal);
273 
274 static string
275 add_outer_pointer_to_array_type_expr(const type_base_sptr& pointer_to_ar,
276  const string& input, bool qualified,
277  bool internal);
278 
279 static string
280 add_outer_ptr_to_mbr_type_expr(const ptr_to_mbr_type* p,
281  const string& input, bool qualified,
282  bool internal);
283 
284 static string
285 add_outer_ptr_to_mbr_type_expr(const ptr_to_mbr_type_sptr& p,
286  const string& input, bool qualified,
287  bool internal);
288 
289 static string
290 add_outer_pointer_to_ptr_to_mbr_type_expr(const type_base* p,
291  const string& input,
292  bool qualified, bool internal);
293 
294 void
295 push_composite_type_comparison_operands(const type_base& left,
296  const type_base& right);
297 
298 void
299 pop_composite_type_comparison_operands(const type_base& left,
300  const type_base& right);
301 
302 bool
303 mark_dependant_types_compared_until(const type_base &r);
304 
305 /// Push a pair of operands on the stack of operands of the current
306 /// type comparison, during type canonicalization.
307 ///
308 /// For more information on this, please look at the description of
309 /// the environment::priv::right_type_comp_operands_ data member.
310 ///
311 /// @param left the left-hand-side comparison operand to push.
312 ///
313 /// @param right the right-hand-side comparison operand to push.
314 void
316  const type_base& right)
317 {
318  const environment& env = left.get_environment();
319  env.priv_->push_composite_type_comparison_operands(&left, &right);
320 }
321 
322 /// Pop a pair of operands from the stack of operands to the current
323 /// type comparison.
324 ///
325 /// For more information on this, please look at the description of
326 /// the environment::privright_type_comp_operands_ data member.
327 ///
328 /// @param left the left-hand-side comparison operand we expect to
329 /// pop from the top of the stack. If this doesn't match the
330 /// operand found on the top of the stack, the function aborts.
331 ///
332 /// @param right the right-hand-side comparison operand we expect to
333 /// pop from the bottom of the stack. If this doesn't match the
334 /// operand found on the top of the stack, the function aborts.
335 void
337  const type_base& right)
338 {
339  const environment& env = left.get_environment();
340  env.priv_->pop_composite_type_comparison_operands(&left, &right);
341 }
342 
343 /// In the stack of the current types being compared (as part of type
344 /// canonicalization), mark all the types that comes after a certain
345 /// one as NOT being eligible to the canonical type propagation
346 /// optimization.
347 ///
348 /// For a starter, please read about the @ref
349 /// OnTheFlyCanonicalization, aka, "canonical type propagation
350 /// optimization".
351 ///
352 /// To implement that optimization, we need, among other things to
353 /// maintain stack of the types (and their sub-types) being
354 /// currently compared as part of type canonicalization.
355 ///
356 /// Note that we only consider the type that is the right-hand-side
357 /// operand of the comparison because it's that one that is being
358 /// canonicalized and thus, that is not yet canonicalized.
359 ///
360 /// The reason why a type is deemed NON-eligible to the canonical
361 /// type propagation optimization is that it "depends" on
362 /// recursively present type. Let me explain.
363 ///
364 /// Suppose we have a type T that has sub-types named ST0 and ST1.
365 /// Suppose ST1 itself has a sub-type that is T itself. In this
366 /// case, we say that T is a recursive type, because it has T
367 /// (itself) as one of its sub-types:
368 ///
369 /// T
370 /// +-- ST0
371 /// |
372 /// +-- ST1
373 /// +
374 /// |
375 /// +-- T
376 ///
377 /// ST1 is said to "depend" on T because it has T as a sub-type.
378 /// But because T is recursive, then ST1 is said to depend on a
379 /// recursive type. Notice however that ST0 does not depend on any
380 /// recursive type.
381 ///
382 /// When we are at the point of comparing the sub-type T of ST1
383 /// against its counterpart, the stack of the right-hand-side
384 /// operands of the type canonicalization is going to look like
385 /// this:
386 ///
387 /// | T | ST1 |
388 ///
389 /// We don't add the type T to the stack as we detect that T was
390 /// already in there (recursive cycle).
391 ///
392 /// So, this function will basically mark ST1 as being NON-eligible
393 /// to being the target of canonical type propagation, by marking ST1
394 /// as being dependant on T.
395 ///
396 /// @param right the right-hand-side operand of the type comparison.
397 ///
398 /// @return true iff the operation was successful.
399 bool
401 {
402  const environment& env = r.get_environment();
404  return env.priv_->mark_dependant_types_compared_until(&r);
405  return false;
406 }
407 
408 /// @brief the location of a token represented in its simplest form.
409 /// Instances of this type are to be stored in a sorted vector, so the
410 /// type must have proper relational operators.
411 class expanded_location
412 {
413  string path_;
414  unsigned line_;
415  unsigned column_;
416 
417  expanded_location();
418 
419 public:
420 
421  friend class location_manager;
422 
423  expanded_location(const string& path, unsigned line, unsigned column)
424  : path_(path), line_(line), column_(column)
425  {}
426 
427  bool
428  operator==(const expanded_location& l) const
429  {
430  return (path_ == l.path_
431  && line_ == l.line_
432  && column_ && l.column_);
433  }
434 
435  bool
436  operator<(const expanded_location& l) const
437  {
438  if (path_ < l.path_)
439  return true;
440  else if (path_ > l.path_)
441  return false;
442 
443  if (line_ < l.line_)
444  return true;
445  else if (line_ > l.line_)
446  return false;
447 
448  return column_ < l.column_;
449  }
450 };
451 
452 /// Expand the location into a tripplet path, line and column number.
453 ///
454 /// @param path the output parameter where this function sets the
455 /// expanded path.
456 ///
457 /// @param line the output parameter where this function sets the
458 /// expanded line.
459 ///
460 /// @param column the ouptut parameter where this function sets the
461 /// expanded column.
462 void
463 location::expand(std::string& path, unsigned& line, unsigned& column) const
464 {
465  if (!get_location_manager())
466  {
467  // We don't have a location manager maybe because this location
468  // was just freshly instanciated. We still want to be able to
469  // expand to default values.
470  path = "";
471  line = 0;
472  column = 0;
473  return;
474  }
475  get_location_manager()->expand_location(*this, path, line, column);
476 }
477 
478 
479 /// Expand the location into a string.
480 ///
481 /// @return the string representing the location.
482 string
483 location::expand(void) const
484 {
485  string path, result;
486  unsigned line = 0, column = 0;
487  expand(path, line, column);
488 
489  std::ostringstream o;
490  o << path << ":" << line << ":" << column;
491  return o.str();
492 }
493 
494 struct location_manager::priv
495 {
496  /// This sorted vector contains the expanded locations of the tokens
497  /// coming from a given ABI Corpus. The index of a given expanded
498  /// location in the table gives us an integer that is used to build
499  /// instance of location types.
500  std::vector<expanded_location> locs;
501 };
502 
503 location_manager::location_manager()
504  : priv_(new location_manager::priv)
505 {}
506 
507 location_manager::~location_manager() = default;
508 
509 /// Insert the triplet representing a source locus into our internal
510 /// vector of location triplet. Return an instance of location type,
511 /// built from an integral type that represents the index of the
512 /// source locus triplet into our source locus table.
513 ///
514 /// @param file_path the file path of the source locus
515 /// @param line the line number of the source location
516 /// @param col the column number of the source location
517 location
518 location_manager::create_new_location(const std::string& file_path,
519  size_t line,
520  size_t col)
521 {
522  expanded_location l(file_path, line, col);
523 
524  // Just append the new expanded location to the end of the vector
525  // and return its index. Note that indexes start at 1.
526  priv_->locs.push_back(l);
527  return location(priv_->locs.size(), this);
528 }
529 
530 /// Given an instance of location type, return the triplet
531 /// {path,line,column} that represents the source locus. Note that
532 /// the location must have been previously created from the function
533 /// location_manager::create_new_location, otherwise this function yields
534 /// unexpected results, including possibly a crash.
535 ///
536 /// @param location the instance of location type to expand
537 /// @param path the resulting path of the source locus
538 /// @param line the resulting line of the source locus
539 /// @param column the resulting colum of the source locus
540 void
542  std::string& path,
543  unsigned& line,
544  unsigned& column) const
545 {
546  if (location.value_ == 0)
547  return;
548  expanded_location &l = priv_->locs[location.value_ - 1];
549  path = l.path_;
550  line = l.line_;
551  column = l.column_;
552 }
553 
554 typedef unordered_map<function_type_sptr,
555  bool,
557  type_shared_ptr_equal> fn_type_ptr_map;
558 
559 // <type_maps stuff>
560 
561 struct type_maps::priv
562 {
563  mutable istring_type_base_wptrs_map_type basic_types_;
564  mutable istring_type_base_wptrs_map_type class_types_;
565  mutable istring_type_base_wptrs_map_type union_types_;
566  mutable istring_type_base_wptrs_map_type enum_types_;
567  mutable istring_type_base_wptrs_map_type typedef_types_;
568  mutable istring_type_base_wptrs_map_type qualified_types_;
569  mutable istring_type_base_wptrs_map_type pointer_types_;
570  mutable istring_type_base_wptrs_map_type ptr_to_mbr_types_;
571  mutable istring_type_base_wptrs_map_type reference_types_;
572  mutable istring_type_base_wptrs_map_type array_types_;
573  mutable istring_type_base_wptrs_map_type subrange_types_;
574  mutable istring_type_base_wptrs_map_type function_types_;
575  mutable vector<type_base_wptr> sorted_types_;
576 }; // end struct type_maps::priv
577 
578 type_maps::type_maps()
579  : priv_(new priv)
580 {}
581 
582 type_maps::~type_maps() = default;
583 
584 /// Test if the type_maps is empty.
585 ///
586 /// @return true iff the type_maps is empty.
587 bool
589 {
590  return (basic_types().empty()
591  && class_types().empty()
592  && union_types().empty()
593  && enum_types().empty()
594  && typedef_types().empty()
595  && qualified_types().empty()
596  && pointer_types().empty()
597  && reference_types().empty()
598  && array_types().empty()
599  && subrange_types().empty()
600  && function_types().empty());
601 }
602 
603 /// Getter for the map that associates the name of a basic type to the
604 /// vector instances of type_decl_sptr that represents that type.
607 {return priv_->basic_types_;}
608 
609 /// Getter for the map that associates the name of a basic type to the
610 /// vector of instances of @ref type_decl_sptr that represents that
611 /// type.
614 {return priv_->basic_types_;}
615 
616 /// Getter for the map that associates the name of a class type to the
617 /// vector of instances of @ref class_decl_sptr that represents that
618 /// type.
621 {return priv_->class_types_;}
622 
623 /// Getter for the map that associates the name of a class type to the
624 /// vector of instances of @ref class_decl_sptr that represents that
625 /// type.
628 {return priv_->class_types_;}
629 
630 /// Getter for the map that associates the name of a union type to the
631 /// vector of instances of @ref union_decl_sptr that represents that
632 /// type.
635 {return priv_->union_types_;}
636 
637 /// Getter for the map that associates the name of a union type to the
638 /// vector of instances of @ref union_decl_sptr that represents that
639 /// type.
642 {return priv_->union_types_;}
643 
644 /// Getter for the map that associates the name of an enum type to the
645 /// vector of instances of @ref enum_type_decl_sptr that represents
646 /// that type.
649 {return priv_->enum_types_;}
650 
651 /// Getter for the map that associates the name of an enum type to the
652 /// vector of instances of @ref enum_type_decl_sptr that represents
653 /// that type.
656 {return priv_->enum_types_;}
657 
658 /// Getter for the map that associates the name of a typedef to the
659 /// vector of instances of @ref typedef_decl_sptr that represents tha
660 /// type.
663 {return priv_->typedef_types_;}
664 
665 /// Getter for the map that associates the name of a typedef to the
666 /// vector of instances of @ref typedef_decl_sptr that represents tha
667 /// type.
670 {return priv_->typedef_types_;}
671 
672 /// Getter for the map that associates the name of a qualified type to
673 /// the vector of instances of @ref qualified_type_def_sptr.
676 {return priv_->qualified_types_;}
677 
678 /// Getter for the map that associates the name of a qualified type to
679 /// the vector of instances of @ref qualified_type_def_sptr.
682 {return priv_->qualified_types_;}
683 
684 /// Getter for the map that associates the name of a pointer type to
685 /// the vector of instances of @ref pointer_type_def_sptr that
686 /// represents that type.
689 {return priv_->pointer_types_;}
690 
691 /// Getter for the map that associates the name of a pointer-to-member
692 /// type to the vector of instances of @ref ptr_to_mbr_type_sptr that
693 /// represents that type.
696 {return priv_->ptr_to_mbr_types_;}
697 
698 /// Getter for the map that associates the name of a pointer-to-member
699 /// type to the vector of instances of @ref ptr_to_mbr_type_sptr that
700 /// represents that type.
703 {return priv_->ptr_to_mbr_types_;}
704 
705 /// Getter for the map that associates the name of a pointer type to
706 /// the vector of instances of @ref pointer_type_def_sptr that
707 /// represents that type.
710 {return priv_->pointer_types_;}
711 
712 /// Getter for the map that associates the name of a reference type to
713 /// the vector of instances of @ref reference_type_def_sptr that
714 /// represents that type.
717 {return priv_->reference_types_;}
718 
719 /// Getter for the map that associates the name of a reference type to
720 /// the vector of instances of @ref reference_type_def_sptr that
721 /// represents that type.
724 {return priv_->reference_types_;}
725 
726 /// Getter for the map that associates the name of an array type to
727 /// the vector of instances of @ref array_type_def_sptr that
728 /// represents that type.
731 {return priv_->array_types_;}
732 
733 /// Getter for the map that associates the name of an array type to
734 /// the vector of instances of @ref array_type_def_sptr that
735 /// represents that type.
738 {return priv_->array_types_;}
739 
740 /// Getter for the map that associates the name of a subrange type to
741 /// the vector of instances of @ref array_type_def::subrange_sptr that
742 /// represents that type.
745 {return priv_->subrange_types_;}
746 
747 /// Getter for the map that associates the name of a subrange type to
748 /// the vector of instances of @ref array_type_def::subrange_sptr that
749 /// represents that type.
752 {return priv_->subrange_types_;}
753 
754 /// Getter for the map that associates the name of a function type to
755 /// the vector of instances of @ref function_type_sptr that represents
756 /// that type.
759 {return priv_->function_types_;}
760 
761 /// Getter for the map that associates the name of a function type to
762 /// the vector of instances of @ref function_type_sptr that represents
763 /// that type.
766 {return priv_->function_types_;}
767 
768 /// A comparison functor to compare/sort types based on their pretty
769 /// representations.
770 struct type_name_comp
771 {
772  /// Comparison operator for two instances of @ref type_base.
773  ///
774  /// This compares the two types by lexicographically comparing their
775  /// pretty representation.
776  ///
777  /// @param l the left-most type to compare.
778  ///
779  /// @param r the right-most type to compare.
780  ///
781  /// @return true iff @p l < @p r.
782  bool
783  operator()(type_base *l, type_base *r) const
784  {
785  if (l == 0 && r == 0)
786  return false;
787 
788  string l_repr = get_pretty_representation(l);
789  string r_repr = get_pretty_representation(r);
790  return l_repr < r_repr;
791  }
792 
793  /// Comparison operator for two instances of @ref type_base.
794  ///
795  /// This compares the two types by lexicographically comparing their
796  /// pretty representation.
797  ///
798  /// @param l the left-most type to compare.
799  ///
800  /// @param r the right-most type to compare.
801  ///
802  /// @return true iff @p l < @p r.
803  bool
804  operator()(const type_base_sptr &l, const type_base_sptr &r) const
805  {return operator()(l.get(), r.get());}
806 
807  /// Comparison operator for two instances of @ref type_base.
808  ///
809  /// This compares the two types by lexicographically comparing their
810  /// pretty representation.
811  ///
812  /// @param l the left-most type to compare.
813  ///
814  /// @param r the right-most type to compare.
815  ///
816  /// @return true iff @p l < @p r.
817  bool
818  operator()(const type_base_wptr &l, const type_base_wptr &r) const
819  {return operator()(type_base_sptr(l), type_base_sptr(r));}
820 }; // end struct type_name_comp
821 
822 #ifdef WITH_DEBUG_SELF_COMPARISON
823 
824 /// This is a function called when the ABG_RETURN* macros defined
825 /// below return false.
826 ///
827 /// The purpose of this function is to ease debugging. To know where
828 /// the equality functions first compare non-equal, we can just set a
829 /// breakpoint on this notify_equality_failed function and run the
830 /// equality functions. Because all the equality functions use the
831 /// ABG_RETURN* macros to return their values, this function is always
832 /// called when any of those equality function return false.
833 ///
834 /// @param l the first operand of the equality.
835 ///
836 /// @param r the second operand of the equality.
837 static void
838 notify_equality_failed(const type_or_decl_base &l __attribute__((unused)),
839  const type_or_decl_base &r __attribute__((unused)))
840 {}
841 
842 /// This is a function called when the ABG_RETURN* macros defined
843 /// below return false.
844 ///
845 /// The purpose of this function is to ease debugging. To know where
846 /// the equality functions first compare non-equal, we can just set a
847 /// breakpoint on this notify_equality_failed function and run the
848 /// equality functions. Because all the equality functions use the
849 /// ABG_RETURN* macros to return their values, this function is always
850 /// called when any of those equality function return false.
851 ///
852 /// @param l the first operand of the equality.
853 ///
854 /// @param r the second operand of the equality.
855 static void
856 notify_equality_failed(const type_or_decl_base *l __attribute__((unused)),
857  const type_or_decl_base *r __attribute__((unused)))
858 {}
859 
860 #define ABG_RETURN_EQUAL(l, r) \
861  do \
862  { \
863  if (l != r) \
864  notify_equality_failed(l, r); \
865  return (l == r); \
866  } \
867  while(false)
868 
869 
870 #define ABG_RETURN_FALSE \
871  do \
872  { \
873  notify_equality_failed(l, r); \
874  return false; \
875  } while(false)
876 
877 #define ABG_RETURN(value) \
878  do \
879  { \
880  if (value == false) \
881  notify_equality_failed(l, r); \
882  return value; \
883  } while (false)
884 
885 #else // WITH_DEBUG_SELF_COMPARISON
886 
887 #define ABG_RETURN_FALSE return false
888 #define ABG_RETURN(value) return (value)
889 #define ABG_RETURN_EQUAL(l, r) return ((l) == (r));
890 #endif
891 
892 /// Compare two types by comparing their canonical types if present.
893 ///
894 /// If the canonical types are not present (because the types have not
895 /// yet been canonicalized, for instance) then the types are compared
896 /// structurally.
897 ///
898 /// @param l the first type to take into account in the comparison.
899 ///
900 /// @param r the second type to take into account in the comparison.
901 template<typename T>
902 bool
903 try_canonical_compare(const T *l, const T *r)
904 {
905 #if WITH_DEBUG_TYPE_CANONICALIZATION
906  // We are debugging the canonicalization of a type down the stack.
907  // 'l' is a subtype of a canonical type and 'r' is a subtype of the
908  // type being canonicalized. We are at a point where we can compare
909  // 'l' and 'r' either using canonical comparison (if 'l' and 'r'
910  // have canonical types) or structural comparison.
911  //
912  // Because we are debugging the process of type canonicalization, we
913  // want to compare 'l' and 'r' canonically *AND* structurally. Both
914  // kinds of comparison should yield the same result, otherwise type
915  // canonicalization just failed for the subtype 'r' of the type
916  // being canonicalized.
917  //
918  // In concrete terms, this function is going to be called twice with
919  // the same pair {'l', 'r'} to compare: The first time with
920  // environment::priv_->use_canonical_type_comparison_ set to true,
921  // instructing us to compare them canonically, and the second time
922  // with that boolean set to false, instructing us to compare them
923  // structurally.
924  const environment&env = l->get_environment();
925  if (env.priv_->use_canonical_type_comparison_)
926  {
927  if (const type_base *lc = l->get_naked_canonical_type())
928  if (const type_base *rc = r->get_naked_canonical_type())
929  ABG_RETURN_EQUAL(lc, rc);
930  }
931  return equals(*l, *r, 0);
932 #else
933  if (const type_base *lc = l->get_naked_canonical_type())
934  if (const type_base *rc = r->get_naked_canonical_type())
935  ABG_RETURN_EQUAL(lc, rc);
936  return equals(*l, *r, 0);
937 #endif
938 
939 
940 }
941 
942 /// Detect if a recursive comparison cycle is detected while
943 /// structurally comparing two types (a.k.a member-wise comparison).
944 ///
945 /// @param l the left-hand-side operand of the current comparison.
946 ///
947 /// @param r the right-hand-side operand of the current comparison.
948 ///
949 /// @return true iff a comparison cycle is detected.
950 template<typename T>
951 bool
953 {
954  bool result = l.priv_->comparison_started(l, r);
955  return result ;
956 }
957 
958 /// Detect if a recursive comparison cycle is detected while
959 /// structurally comparing two @ref class_decl types.
960 ///
961 /// @param l the left-hand-side operand of the current comparison.
962 ///
963 /// @param r the right-hand-side operand of the current comparison.
964 ///
965 /// @return true iff a comparison cycle is detected.
966 template<>
967 bool
969 {
970  return is_comparison_cycle_detected(static_cast<const class_or_union&>(l),
971  static_cast<const class_or_union&>(r));
972 }
973 
974 /// This macro is to be used while comparing composite types that
975 /// might recursively refer to themselves. Comparing two such types
976 /// might get us into a cyle.
977 ///
978 /// Practically, if we detect that we are already into comparing 'l'
979 /// and 'r'; then, this is a cycle.
980 //
981 /// To break the cycle, we assume the result of the comparison is true
982 /// for now. Comparing the other sub-types of l & r will tell us later
983 /// if l & r are actually different or not.
984 ///
985 /// In the mean time, returning true from this macro should not be
986 /// used to propagate the canonical type of 'l' onto 'r' as we don't
987 /// know yet if l equals r. All the types that depend on l and r
988 /// can't (and that are in the comparison stack currently) can't have
989 /// their canonical type propagated either. So this macro disallows
990 /// canonical type propagation for those types that depend on a
991 /// recursively defined sub-type for now.
992 ///
993 /// @param l the left-hand-side operand of the comparison.
994 #define RETURN_TRUE_IF_COMPARISON_CYCLE_DETECTED(l, r) \
995  do \
996  { \
997  if (is_comparison_cycle_detected(l, r)) \
998  { \
999  mark_dependant_types_compared_until(r); \
1000  return true; \
1001  } \
1002  } \
1003  while(false)
1004 
1005 
1006 /// Mark a pair of types as being compared.
1007 ///
1008 /// This is helpful to later detect recursive cycles in the comparison
1009 /// stack.
1010 ///
1011 /// @param l the left-hand-side operand of the comparison.
1012 ///
1013 /// @parm r the right-hand-side operand of the comparison.
1014 template<typename T>
1015 void
1017 {
1018  l.priv_->mark_as_being_compared(l, r);
1020 }
1021 
1022 /// Mark a pair of @ref class_decl types as being compared.
1023 ///
1024 /// This is helpful to later detect recursive cycles in the comparison
1025 /// stack.
1026 ///
1027 /// @param l the left-hand-side operand of the comparison.
1028 ///
1029 /// @parm r the right-hand-side operand of the comparison.
1030 template<>
1031 void
1033 {
1034  return mark_types_as_being_compared(static_cast<const class_or_union&>(l),
1035  static_cast<const class_or_union&>(r));
1036 }
1037 
1038 /// Mark a pair of types as being not compared anymore.
1039 ///
1040 /// This is helpful to later detect recursive cycles in the comparison
1041 /// stack.
1042 ///
1043 /// Note that the types must have been passed to
1044 /// mark_types_as_being_compared prior to calling this function.
1045 ///
1046 /// @param l the left-hand-side operand of the comparison.
1047 ///
1048 /// @parm r the right-hand-side operand of the comparison.
1049 template<typename T>
1050 void
1052 {
1053  l.priv_->unmark_as_being_compared(l, r);
1055 }
1056 
1057 /// Mark a pair of @ref class_decl types as being not compared
1058 /// anymore.
1059 ///
1060 /// This is helpful to later detect recursive cycles in the comparison
1061 /// stack.
1062 ///
1063 /// Note that the types must have been passed to
1064 /// mark_types_as_being_compared prior to calling this function.
1065 ///
1066 /// @param l the left-hand-side operand of the comparison.
1067 ///
1068 /// @parm r the right-hand-side operand of the comparison.
1069 template<>
1070 void
1072 {
1073  return unmark_types_as_being_compared(static_cast<const class_or_union&>(l),
1074  static_cast<const class_or_union&>(r));
1075 }
1076 
1077 /// Return the result of the comparison of two (sub) types.
1078 ///
1079 /// The function does the necessary book keeping before returning the
1080 /// result of the comparison of two (sub) types.
1081 ///
1082 /// The book-keeping done is in the following
1083 /// areas:
1084 ///
1085 /// * Management of the Canonical Type Propagation optimization
1086 /// * type comparison cycle detection
1087 ///
1088 /// @param l the left-hand-side operand of the type comparison
1089 ///
1090 /// @param r the right-hand-side operand of the type comparison
1091 ///
1092 /// @param propagate_canonical_type if true, it means the function
1093 /// performs the @ref OnTheFlyCanonicalization, aka, "canonical type
1094 /// propagation optimization".
1095 ///
1096 /// @param value the result of the comparison of @p l and @p r.
1097 ///
1098 /// @return the value @p value.
1099 template<typename T>
1100 bool
1101 return_comparison_result(T& l, T& r, bool value,
1102  bool propagate_canonical_type = true)
1103 {
1104  if (propagate_canonical_type && (value == true))
1105  maybe_propagate_canonical_type(l, r);
1106 
1108 
1109  const environment& env = l.get_environment();
1111  // We are instructed to perform the "canonical type propagation"
1112  // optimization, making 'r' to possibly get the canonical type of
1113  // 'l' if it has one. This mostly means that we are currently
1114  // canonicalizing the type that contain the subtype provided in
1115  // the 'r' argument.
1116  {
1117  if (value == true
1118  && (is_type(&r)->priv_->depends_on_recursive_type()
1119  || env.priv_->is_recursive_type(&r))
1120  && is_type(&r)->priv_->canonical_type_propagated()
1121  && !is_type(&r)->priv_->propagated_canonical_type_confirmed()
1122  && !env.priv_->right_type_comp_operands_.empty())
1123  {
1124  // Track the object 'r' for which the propagated canonical
1125  // type might be re-initialized if the current comparison
1126  // eventually fails.
1127  env.priv_->add_to_types_with_non_confirmed_propagated_ct(is_type(&r));
1128  }
1129  else if (value == true
1130  && env.priv_->right_type_comp_operands_.empty()
1131  && is_type(&r)->priv_->canonical_type_propagated()
1132  && !is_type(&r)->priv_->propagated_canonical_type_confirmed())
1133  {
1134  // The type provided in the 'r' argument is the type that is
1135  // being canonicalized; 'r' is not a mere subtype being
1136  // compared, it's the whole type being canonicalized. And
1137  // its canonicalization has just succeeded.
1138  //
1139  // Let's confirm the canonical type resulting from the
1140  // "canonical type propagation" optimization.
1141  env.priv_->confirm_ct_propagation(&r);
1142  }
1143  else if (value == true
1144  && is_type(&r)->priv_->canonical_type_propagated()
1145  && !is_type(&r)->priv_->propagated_canonical_type_confirmed())
1146  // In any other case, we are not sure if propagated types
1147  // should be confirmed yet. So let's mark them as such.
1148  env.priv_->add_to_types_with_non_confirmed_propagated_ct(is_type(&r));
1149  else if (value == false)
1150  {
1151  // The comparison of the current sub-type failed. So all
1152  // the with non-confirmed propagated types (those in
1153  // env.prix_->types_with_non_confirmed_propagated_ct_)
1154  // should see their tentatively propagated canonical type
1155  // cancelled.
1156  env.priv_->cancel_all_non_confirmed_propagated_canonical_types();
1157  }
1158  }
1159 
1160  // If we reached this point with value == true and the stack of
1161  // types being compared is empty, then it means that the type pair
1162  // that was at the bottom of the stack is now fully compared.
1163  //
1164  // It follows that all types that were target of canonical type
1165  // propagation can now see their tentative canonical type be
1166  // confirmed for real.
1167  if (value == true
1168  && env.priv_->right_type_comp_operands_.empty()
1169  && !env.priv_->types_with_non_confirmed_propagated_ct_.empty())
1170  // So the comparison is completely done and there are some
1171  // types for which their propagated canonical type is sitll
1172  // considered not confirmed. As the comparison did yield true, we
1173  // shall now confirm the propagation for all those types.
1174  env.priv_->confirm_ct_propagation();
1175 
1176 #ifdef WITH_DEBUG_SELF_COMPARISON
1177  if (value == false && env.priv_->right_type_comp_operands_.empty())
1178  {
1179  for (const auto i : env.priv_->types_with_non_confirmed_propagated_ct_)
1180  {
1181  type_base *t = reinterpret_cast<type_base*>(i);
1182  env.priv_->check_abixml_canonical_type_propagation_during_self_comp(t);
1183  }
1184  }
1185 #endif
1186 
1187  ABG_RETURN(value);
1188 }
1189 
1190 #define CACHE_AND_RETURN_COMPARISON_RESULT(value) \
1191  do \
1192  { \
1193  bool res = return_comparison_result(l, r, value); \
1194  l.get_environment().priv_->cache_type_comparison_result(l, r, res); \
1195  return res; \
1196  } while (false)
1197 
1198 /// Cache the result of a comparison between too artifacts (l & r) and
1199 /// return immediately.
1200 ///
1201 /// @param value the value to cache.
1202 #define CACHE_COMPARISON_RESULT_AND_RETURN(value) \
1203  do \
1204  { \
1205  l.get_environment().priv_->cache_type_comparison_result(l, r, value); \
1206  return value; \
1207  } while (false)
1208 
1209 /// Getter of all types types sorted by their pretty representation.
1210 ///
1211 /// @return a sorted vector of all types sorted by their pretty
1212 /// representation.
1213 const vector<type_base_wptr>&
1215 {
1216  if (priv_->sorted_types_.empty())
1217  {
1218  istring_type_base_wptrs_map_type::const_iterator i;
1219  vector<type_base_wptr>::const_iterator j;
1220 
1221  for (i = basic_types().begin(); i != basic_types().end(); ++i)
1222  for (j = i->second.begin(); j != i->second.end(); ++j)
1223  priv_->sorted_types_.push_back(*j);
1224 
1225  for (i = class_types().begin(); i != class_types().end(); ++i)
1226  for (j = i->second.begin(); j != i->second.end(); ++j)
1227  priv_->sorted_types_.push_back(*j);
1228 
1229  for (i = union_types().begin(); i != union_types().end(); ++i)
1230  for (j = i->second.begin(); j != i->second.end(); ++j)
1231  priv_->sorted_types_.push_back(*j);
1232 
1233  for (i = enum_types().begin(); i != enum_types().end(); ++i)
1234  for (j = i->second.begin(); j != i->second.end(); ++j)
1235  priv_->sorted_types_.push_back(*j);
1236 
1237  for (i = typedef_types().begin(); i != typedef_types().end(); ++i)
1238  for (j = i->second.begin(); j != i->second.end(); ++j)
1239  priv_->sorted_types_.push_back(*j);
1240 
1241  type_name_comp comp;
1242  sort(priv_->sorted_types_.begin(), priv_->sorted_types_.end(), comp);
1243  }
1244 
1245  return priv_->sorted_types_;
1246 }
1247 
1248 // </type_maps stuff>
1249 
1250 // <translation_unit stuff>
1251 
1252 /// Constructor of translation_unit.
1253 ///
1254 /// @param env the environment of this translation unit. Please note
1255 /// that the life time of the environment must be greater than the
1256 /// life time of the translation unit because the translation uses
1257 /// resources that are allocated in the environment.
1258 ///
1259 /// @param path the location of the translation unit.
1260 ///
1261 /// @param address_size the size of addresses in the translation unit,
1262 /// in bits.
1263 translation_unit::translation_unit(const environment& env,
1264  const std::string& path,
1265  char address_size)
1266  : priv_(new priv(env))
1267 {
1268  priv_->path_ = path;
1269  priv_->address_size_ = address_size;
1270 }
1271 
1272 /// Getter of the the global scope of the translation unit.
1273 ///
1274 /// @return the global scope of the current translation unit. If
1275 /// there is not global scope allocated yet, this function creates one
1276 /// and returns it.
1277 const scope_decl_sptr&
1279 {
1280  return const_cast<translation_unit*>(this)->get_global_scope();
1281 }
1282 
1283 /// Getter of the the global scope of the translation unit.
1284 ///
1285 /// @return the global scope of the current translation unit. If
1286 /// there is not global scope allocated yet, this function creates one
1287 /// and returns it.
1290 {
1291  if (!priv_->global_scope_)
1292  {
1293  priv_->global_scope_.reset
1294  (new global_scope(const_cast<translation_unit*>(this)));
1295  priv_->global_scope_->set_translation_unit
1296  (const_cast<translation_unit*>(this));
1297  }
1298  return priv_->global_scope_;
1299 }
1300 
1301 /// Getter of the types of the current @ref translation_unit.
1302 ///
1303 /// @return the maps of the types of the translation unit.
1304 const type_maps&
1306 {return priv_->types_;}
1307 
1308 /// Getter of the types of the current @ref translation_unit.
1309 ///
1310 /// @return the maps of the types of the translation unit.
1311 type_maps&
1313 {return priv_->types_;}
1314 
1315 /// Get the vector of function types that are used in the current
1316 /// translation unit.
1317 ///
1318 /// @return the vector of function types that are used in the current
1319 /// translation unit.
1320 const vector<function_type_sptr>&
1322 {return priv_->live_fn_types_;}
1323 
1324 /// Getter of the environment of the current @ref translation_unit.
1325 ///
1326 /// @return the translation unit of the current translation unit.
1327 const environment&
1329 {return priv_->env_;}
1330 
1331 /// Getter of the language of the source code of the translation unit.
1332 ///
1333 /// @return the language of the source code.
1336 {return priv_->language_;}
1337 
1338 /// Setter of the language of the source code of the translation unit.
1339 ///
1340 /// @param l the new language.
1341 void
1343 {priv_->language_ = l;}
1344 
1345 
1346 /// Get the path of the current translation unit.
1347 ///
1348 /// This path is relative to the build directory of the translation
1349 /// unit as returned by translation_unit::get_compilation_dir_path.
1350 ///
1351 /// @return the relative path of the compilation unit associated to
1352 /// the current instance of translation_unit.
1353 //
1354 const std::string&
1356 {return priv_->path_;}
1357 
1358 /// Set the path associated to the current instance of
1359 /// translation_unit.
1360 ///
1361 /// This path is relative to the build directory of the translation
1362 /// unit as returned by translation_unit::get_compilation_dir_path.
1363 ///
1364 /// @param a_path the new relative path to set.
1365 void
1366 translation_unit::set_path(const string& a_path)
1367 {priv_->path_ = a_path;}
1368 
1369 
1370 /// Get the path of the directory that was 'current' when the
1371 /// translation unit was compiled.
1372 ///
1373 /// Note that the path returned by translation_unit::get_path is
1374 /// relative to the path returned by this function.
1375 ///
1376 /// @return the compilation directory for the current translation
1377 /// unit.
1378 const std::string&
1380 {return priv_->comp_dir_path_;}
1381 
1382 /// Set the path of the directory that was 'current' when the
1383 /// translation unit was compiled.
1384 ///
1385 /// Note that the path returned by translation_unit::get_path is
1386 /// relative to the path returned by this function.
1387 ///
1388 /// @param the compilation directory for the current translation unit.
1389 void
1391 {priv_->comp_dir_path_ = d;}
1392 
1393 /// Get the concatenation of the build directory and the relative path
1394 /// of the translation unit.
1395 ///
1396 /// @return the absolute path of the translation unit.
1397 const std::string&
1399 {
1400  if (priv_->abs_path_.empty())
1401  {
1402  string path;
1403  if (!priv_->path_.empty())
1404  {
1405  if (!priv_->comp_dir_path_.empty())
1406  {
1407  path = priv_->comp_dir_path_;
1408  path += "/";
1409  }
1410  path += priv_->path_;
1411  }
1412  priv_->abs_path_ = path;
1413  }
1414 
1415  return priv_->abs_path_;
1416 }
1417 
1418 /// Set the corpus this translation unit is a member of.
1419 ///
1420 /// Note that adding a translation unit to a @ref corpus automatically
1421 /// triggers a call to this member function.
1422 ///
1423 /// @param corpus the corpus.
1424 void
1426 {priv_->corp = c;}
1427 
1428 /// Get the corpus this translation unit is a member of.
1429 ///
1430 /// @return the parent corpus, or nil if this doesn't belong to any
1431 /// corpus yet.
1432 corpus*
1434 {return priv_->corp;}
1435 
1436 /// Get the corpus this translation unit is a member of.
1437 ///
1438 /// @return the parent corpus, or nil if this doesn't belong to any
1439 /// corpus yet.
1440 const corpus*
1442 {return const_cast<translation_unit*>(this)->get_corpus();}
1443 
1444 /// Getter of the location manager for the current translation unit.
1445 ///
1446 /// @return a reference to the location manager for the current
1447 /// translation unit.
1450 {return priv_->loc_mgr_;}
1451 
1452 /// const Getter of the location manager.
1453 ///
1454 /// @return a const reference to the location manager for the current
1455 /// translation unit.
1456 const location_manager&
1458 {return priv_->loc_mgr_;}
1459 
1460 /// Tests whether if the current translation unit contains ABI
1461 /// artifacts or not.
1462 ///
1463 /// @return true iff the current translation unit is empty.
1464 bool
1466 {
1467  if (!priv_->global_scope_)
1468  return true;
1469  return get_global_scope()->is_empty();
1470 }
1471 
1472 /// Getter of the address size in this translation unit.
1473 ///
1474 /// @return the address size, in bits.
1475 char
1477 {return priv_->address_size_;}
1478 
1479 /// Setter of the address size in this translation unit.
1480 ///
1481 /// @param a the new address size in bits.
1482 void
1484 {priv_->address_size_= a;}
1485 
1486 /// Getter of the 'is_constructed" flag. It says if the translation
1487 /// unit is fully constructed or not.
1488 ///
1489 /// This flag is important for cases when comparison might depend on
1490 /// if the translation unit is fully built or not. For instance, when
1491 /// reading types from DWARF, the virtual methods of a class are not
1492 /// necessarily fully constructed until we have reached the end of the
1493 /// translation unit. In that case, before we've reached the end of
1494 /// the translation unit, we might not take virtual functions into
1495 /// account when comparing classes.
1496 ///
1497 /// @return true if the translation unit is constructed.
1498 bool
1500 {return priv_->is_constructed_;}
1501 
1502 /// Setter of the 'is_constructed" flag. It says if the translation
1503 /// unit is fully constructed or not.
1504 ///
1505 /// This flag is important for cases when comparison might depend on
1506 /// if the translation unit is fully built or not. For instance, when
1507 /// reading types from DWARF, the virtual methods of a class are not
1508 /// necessarily fully constructed until we have reached the end of the
1509 /// translation unit. In that case, before we've reached the end of
1510 /// the translation unit, we might not take virtual functions into
1511 /// account when comparing classes.
1512 ///
1513 /// @param f true if the translation unit is constructed.
1514 void
1516 {priv_->is_constructed_ = f;}
1517 
1518 /// Compare the current translation unit against another one.
1519 ///
1520 /// @param other the other tu to compare against.
1521 ///
1522 /// @return true if the two translation units are equal, false
1523 /// otherwise.
1524 bool
1526 {
1527  if (get_address_size() != other.get_address_size())
1528  return false;
1529 
1530  return *get_global_scope() == *other.get_global_scope();
1531 }
1532 
1533 /// Inequality operator.
1534 ///
1535 /// @param o the instance of @ref translation_unit to compare the
1536 /// current instance against.
1537 ///
1538 /// @return true iff the current instance is different from @p o.
1539 bool
1541 {return ! operator==(o);}
1542 
1543 /// Ensure that the life time of a function type is bound to the life
1544 /// time of the current translation unit.
1545 ///
1546 /// @param ftype the function time which life time to bind to the life
1547 /// time of the current instance of @ref translation_unit. That is,
1548 /// it's onlyh when the translation unit is destroyed that the
1549 /// function type can be destroyed to.
1550 void
1552 {
1553  const environment& env = get_environment();
1554 
1555  const_cast<translation_unit*>(this)->priv_->live_fn_types_.push_back(ftype);
1556 
1557  interned_string repr = get_type_name(ftype);
1558  const_cast<translation_unit*>(this)->get_types().function_types()[repr].
1559  push_back(ftype);
1560 
1561  // The function type must be out of the same environment as its
1562  // translation unit.
1563  {
1564  const environment& e = ftype->get_environment();
1565  ABG_ASSERT(&env == &e);
1566  }
1567 
1568  if (const translation_unit* existing_tu = ftype->get_translation_unit())
1569  ABG_ASSERT(existing_tu == this);
1570  else
1571  ftype->set_translation_unit(const_cast<translation_unit*>(this));
1572 
1574 }
1575 
1576 /// This implements the ir_traversable_base::traverse virtual
1577 /// function.
1578 ///
1579 /// @param v the visitor used on the member nodes of the translation
1580 /// unit during the traversal.
1581 ///
1582 /// @return true if the entire type IR tree got traversed, false
1583 /// otherwise.
1584 bool
1586 {return get_global_scope()->traverse(v);}
1587 
1588 translation_unit::~translation_unit()
1589 {}
1590 
1591 /// Converts a translation_unit::language enumerator into a string.
1592 ///
1593 /// @param l the language enumerator to translate.
1594 ///
1595 /// @return the resulting string.
1596 string
1598 {
1599  switch (l)
1600  {
1601  case translation_unit::LANG_UNKNOWN:
1602  return "LANG_UNKNOWN";
1603  case translation_unit::LANG_Cobol74:
1604  return "LANG_Cobol74";
1605  case translation_unit::LANG_Cobol85:
1606  return "LANG_Cobol85";
1607  case translation_unit::LANG_C89:
1608  return "LANG_C89";
1609  case translation_unit::LANG_C99:
1610  return "LANG_C99";
1611  case translation_unit::LANG_C11:
1612  return "LANG_C11";
1613  case translation_unit::LANG_C:
1614  return "LANG_C";
1615  case translation_unit::LANG_C_plus_plus_11:
1616  return "LANG_C_plus_plus_11";
1617  case translation_unit::LANG_C_plus_plus_14:
1618  return "LANG_C_plus_plus_14";
1619  case translation_unit::LANG_C_plus_plus:
1620  return "LANG_C_plus_plus";
1621  case translation_unit::LANG_ObjC:
1622  return "LANG_ObjC";
1623  case translation_unit::LANG_ObjC_plus_plus:
1624  return "LANG_ObjC_plus_plus";
1625  case translation_unit::LANG_Fortran77:
1626  return "LANG_Fortran77";
1627  case translation_unit::LANG_Fortran90:
1628  return "LANG_Fortran90";
1629  case translation_unit::LANG_Fortran95:
1630  return "LANG_Fortran95";
1631  case translation_unit::LANG_Ada83:
1632  return "LANG_Ada83";
1633  case translation_unit::LANG_Ada95:
1634  return "LANG_Ada95";
1635  case translation_unit::LANG_Pascal83:
1636  return "LANG_Pascal83";
1637  case translation_unit::LANG_Modula2:
1638  return "LANG_Modula2";
1639  case translation_unit::LANG_Java:
1640  return "LANG_Java";
1641  case translation_unit::LANG_PLI:
1642  return "LANG_PLI";
1643  case translation_unit::LANG_UPC:
1644  return "LANG_UPC";
1645  case translation_unit::LANG_D:
1646  return "LANG_D";
1647  case translation_unit::LANG_Python:
1648  return "LANG_Python";
1649  case translation_unit::LANG_Go:
1650  return "LANG_Go";
1651  case translation_unit::LANG_Mips_Assembler:
1652  return "LANG_Mips_Assembler";
1653  default:
1654  return "LANG_UNKNOWN";
1655  }
1656 
1657  return "LANG_UNKNOWN";
1658 }
1659 
1660 /// Parse a string representing a language into a
1661 /// translation_unit::language enumerator into a string.
1662 ///
1663 /// @param l the string representing the language.
1664 ///
1665 /// @return the resulting translation_unit::language enumerator.
1668 {
1669  if (l == "LANG_Cobol74")
1670  return translation_unit::LANG_Cobol74;
1671  else if (l == "LANG_Cobol85")
1672  return translation_unit::LANG_Cobol85;
1673  else if (l == "LANG_C89")
1674  return translation_unit::LANG_C89;
1675  else if (l == "LANG_C99")
1676  return translation_unit::LANG_C99;
1677  else if (l == "LANG_C11")
1678  return translation_unit::LANG_C11;
1679  else if (l == "LANG_C")
1680  return translation_unit::LANG_C;
1681  else if (l == "LANG_C_plus_plus_11")
1682  return translation_unit::LANG_C_plus_plus_11;
1683  else if (l == "LANG_C_plus_plus_14")
1684  return translation_unit::LANG_C_plus_plus_14;
1685  else if (l == "LANG_C_plus_plus")
1686  return translation_unit::LANG_C_plus_plus;
1687  else if (l == "LANG_ObjC")
1688  return translation_unit::LANG_ObjC;
1689  else if (l == "LANG_ObjC_plus_plus")
1690  return translation_unit::LANG_ObjC_plus_plus;
1691  else if (l == "LANG_Fortran77")
1692  return translation_unit::LANG_Fortran77;
1693  else if (l == "LANG_Fortran90")
1694  return translation_unit::LANG_Fortran90;
1695  else if (l == "LANG_Fortran95")
1696  return translation_unit::LANG_Fortran95;
1697  else if (l == "LANG_Ada83")
1698  return translation_unit::LANG_Ada83;
1699  else if (l == "LANG_Ada95")
1700  return translation_unit::LANG_Ada95;
1701  else if (l == "LANG_Pascal83")
1702  return translation_unit::LANG_Pascal83;
1703  else if (l == "LANG_Modula2")
1704  return translation_unit::LANG_Modula2;
1705  else if (l == "LANG_Java")
1706  return translation_unit::LANG_Java;
1707  else if (l == "LANG_PLI")
1708  return translation_unit::LANG_PLI;
1709  else if (l == "LANG_UPC")
1710  return translation_unit::LANG_UPC;
1711  else if (l == "LANG_D")
1712  return translation_unit::LANG_D;
1713  else if (l == "LANG_Python")
1714  return translation_unit::LANG_Python;
1715  else if (l == "LANG_Go")
1716  return translation_unit::LANG_Go;
1717  else if (l == "LANG_Mips_Assembler")
1718  return translation_unit::LANG_Mips_Assembler;
1719 
1720  return translation_unit::LANG_UNKNOWN;
1721 }
1722 
1723 /// Test if a language enumerator designates the C language.
1724 ///
1725 /// @param l the language enumerator to consider.
1726 ///
1727 /// @return true iff @p l designates the C language.
1728 bool
1730 {
1731  return (l == translation_unit::LANG_C89
1732  || l == translation_unit::LANG_C99
1733  || l == translation_unit::LANG_C11
1734  || l == translation_unit::LANG_C);
1735 }
1736 
1737 /// Test if a language enumerator designates the C++ language.
1738 ///
1739 /// @param l the language enumerator to consider.
1740 ///
1741 /// @return true iff @p l designates the C++ language.
1742 bool
1744 {
1745  return (l == translation_unit::LANG_C_plus_plus_03
1746  || l == translation_unit::LANG_C_plus_plus_11
1747  || l == translation_unit::LANG_C_plus_plus_14
1748  || l == translation_unit::LANG_C_plus_plus);
1749 }
1750 
1751 /// Test if a language enumerator designates the Java language.
1752 ///
1753 /// @param l the language enumerator to consider.
1754 ///
1755 /// @return true iff @p l designates the Java language.
1756 bool
1758 {return l == translation_unit::LANG_Java;}
1759 
1760 /// Test if a language enumerator designates the Ada language.
1761 ///
1762 /// @param l the language enumerator to consider.
1763 ///
1764 /// @return true iff @p l designates the Ada language.
1765 bool
1767 {
1768  return (l == translation_unit::LANG_Ada83
1769  || l == translation_unit::LANG_Ada95);
1770 }
1771 
1772 /// A deep comparison operator for pointers to translation units.
1773 ///
1774 /// @param l the first translation unit to consider for the comparison.
1775 ///
1776 /// @param r the second translation unit to consider for the comparison.
1777 ///
1778 /// @return true if the two translation units are equal, false otherwise.
1779 bool
1781 {
1782  if (l.get() == r.get())
1783  return true;
1784 
1785  if (!!l != !!r)
1786  return false;
1787 
1788  return *l == *r;
1789 }
1790 
1791 /// A deep inequality operator for pointers to translation units.
1792 ///
1793 /// @param l the first translation unit to consider for the comparison.
1794 ///
1795 /// @param r the second translation unit to consider for the comparison.
1796 ///
1797 /// @return true iff the two translation units are different.
1798 bool
1800 {return !operator==(l, r);}
1801 
1802 // </translation_unit stuff>
1803 
1804 // <elf_symbol stuff>
1805 struct elf_symbol::priv
1806 {
1807  const environment& env_;
1808  size_t index_;
1809  size_t size_;
1810  string name_;
1811  elf_symbol::type type_;
1812  elf_symbol::binding binding_;
1813  elf_symbol::version version_;
1814  elf_symbol::visibility visibility_;
1815  bool is_defined_;
1816  // This flag below says if the symbol is a common elf symbol. In
1817  // relocatable files, a common symbol is a symbol defined in a
1818  // section of kind SHN_COMMON.
1819  //
1820  // Note that a symbol of kind STT_COMMON is also considered a common
1821  // symbol. Here is what the gABI says about STT_COMMON and
1822  // SHN_COMMON:
1823  //
1824  // Symbols with type STT_COMMON label uninitialized common
1825  // blocks. In relocatable objects, these symbols are not
1826  // allocated and must have the special section index SHN_COMMON
1827  // (see below). In shared objects and executables these symbols
1828  // must be allocated to some section in the defining object.
1829  //
1830  // In relocatable objects, symbols with type STT_COMMON are
1831  // treated just as other symbols with index SHN_COMMON. If the
1832  // link-editor allocates space for the SHN_COMMON symbol in an
1833  // output section of the object it is producing, it must
1834  // preserve the type of the output symbol as STT_COMMON.
1835  //
1836  // When the dynamic linker encounters a reference to a symbol
1837  // that resolves to a definition of type STT_COMMON, it may (but
1838  // is not required to) change its symbol resolution rules as
1839  // follows: instead of binding the reference to the first symbol
1840  // found with the given name, the dynamic linker searches for
1841  // the first symbol with that name with type other than
1842  // STT_COMMON. If no such symbol is found, it looks for the
1843  // STT_COMMON definition of that name that has the largest size.
1844  bool is_common_;
1845  bool is_in_ksymtab_;
1848  bool is_suppressed_;
1849  elf_symbol_wptr main_symbol_;
1850  elf_symbol_wptr next_alias_;
1851  elf_symbol_wptr next_common_instance_;
1852  string id_string_;
1853 
1854  priv(const environment& e)
1855  : env_(e),
1856  index_(),
1857  size_(),
1858  type_(elf_symbol::NOTYPE_TYPE),
1859  binding_(elf_symbol::GLOBAL_BINDING),
1860  visibility_(elf_symbol::DEFAULT_VISIBILITY),
1861  is_defined_(false),
1862  is_common_(false),
1863  is_in_ksymtab_(false),
1864  crc_(),
1865  namespace_(),
1866  is_suppressed_(false)
1867  {}
1868 
1869  priv(const environment& e,
1870  size_t i,
1871  size_t s,
1872  const string& n,
1873  elf_symbol::type t,
1875  bool d,
1876  bool c,
1877  const elf_symbol::version& ve,
1879  bool is_in_ksymtab,
1880  const abg_compat::optional<uint32_t>& crc,
1882  bool is_suppressed)
1883  : env_(e),
1884  index_(i),
1885  size_(s),
1886  name_(n),
1887  type_(t),
1888  binding_(b),
1889  version_(ve),
1890  visibility_(vi),
1891  is_defined_(d),
1892  is_common_(c),
1893  is_in_ksymtab_(is_in_ksymtab),
1894  crc_(crc),
1895  namespace_(ns),
1896  is_suppressed_(is_suppressed)
1897  {
1898  if (!is_common_)
1899  is_common_ = type_ == COMMON_TYPE;
1900  }
1901 }; // end struct elf_symbol::priv
1902 
1903 /// Constructor of the @ref elf_symbol type.
1904 ///
1905 /// Note that this constructor is private, so client code cannot use
1906 /// it to create instances of @ref elf_symbol. Rather, client code
1907 /// should use the @ref elf_symbol::create() function to create
1908 /// instances of @ref elf_symbol instead.
1909 ///
1910 /// @param e the environment we are operating from.
1911 ///
1912 /// @param i the index of the symbol in the (ELF) symbol table.
1913 ///
1914 /// @param s the size of the symbol.
1915 ///
1916 /// @param n the name of the symbol.
1917 ///
1918 /// @param t the type of the symbol.
1919 ///
1920 /// @param b the binding of the symbol.
1921 ///
1922 /// @param d true if the symbol is defined, false otherwise.
1923 ///
1924 /// @param c true if the symbol is a common symbol, false otherwise.
1925 ///
1926 /// @param ve the version of the symbol.
1927 ///
1928 /// @param vi the visibility of the symbol.
1929 ///
1930 /// @param crc the CRC (modversions) value of Linux Kernel symbols
1931 ///
1932 /// @param ns the namespace of Linux Kernel symbols, if any
1933 elf_symbol::elf_symbol(const environment& e,
1934  size_t i,
1935  size_t s,
1936  const string& n,
1937  type t,
1938  binding b,
1939  bool d,
1940  bool c,
1941  const version& ve,
1942  visibility vi,
1943  bool is_in_ksymtab,
1944  const abg_compat::optional<uint32_t>& crc,
1946  bool is_suppressed)
1947  : priv_(new priv(e,
1948  i,
1949  s,
1950  n,
1951  t,
1952  b,
1953  d,
1954  c,
1955  ve,
1956  vi,
1957  is_in_ksymtab,
1958  crc,
1959  ns,
1960  is_suppressed))
1961 {}
1962 
1963 /// Factory of instances of @ref elf_symbol.
1964 ///
1965 /// This is the function to use to create instances of @ref elf_symbol.
1966 ///
1967 /// @param e the environment we are operating from.
1968 ///
1969 /// @param i the index of the symbol in the (ELF) symbol table.
1970 ///
1971 /// @param s the size of the symbol.
1972 ///
1973 /// @param n the name of the symbol.
1974 ///
1975 /// @param t the type of the symbol.
1976 ///
1977 /// @param b the binding of the symbol.
1978 ///
1979 /// @param d true if the symbol is defined, false otherwise.
1980 ///
1981 /// @param c true if the symbol is a common symbol.
1982 ///
1983 /// @param ve the version of the symbol.
1984 ///
1985 /// @param vi the visibility of the symbol.
1986 ///
1987 /// @param crc the CRC (modversions) value of Linux Kernel symbols
1988 ///
1989 /// @param ns the namespace of Linux Kernel symbols, if any
1990 ///
1991 /// @return a (smart) pointer to a newly created instance of @ref
1992 /// elf_symbol.
1995  size_t i,
1996  size_t s,
1997  const string& n,
1998  type t,
1999  binding b,
2000  bool d,
2001  bool c,
2002  const version& ve,
2003  visibility vi,
2004  bool is_in_ksymtab,
2005  const abg_compat::optional<uint32_t>& crc,
2007  bool is_suppressed)
2008 {
2009  elf_symbol_sptr sym(new elf_symbol(e, i, s, n, t, b, d, c, ve, vi,
2010  is_in_ksymtab, crc, ns, is_suppressed));
2011  sym->priv_->main_symbol_ = sym;
2012  return sym;
2013 }
2014 
2015 /// Test textual equality between two symbols.
2016 ///
2017 /// Textual equality means that the aliases of the compared symbols
2018 /// are not taken into account. Only the name, type, and version of
2019 /// the symbols are compared.
2020 ///
2021 /// @return true iff the two symbols are textually equal.
2022 static bool
2023 textually_equals(const elf_symbol&l,
2024  const elf_symbol&r)
2025 {
2026  bool equals = (l.get_name() == r.get_name()
2027  && l.get_type() == r.get_type()
2028  && l.is_public() == r.is_public()
2029  && l.is_defined() == r.is_defined()
2030  && l.is_common_symbol() == r.is_common_symbol()
2031  && l.get_version() == r.get_version()
2032  && l.get_crc() == r.get_crc()
2033  && l.get_namespace() == r.get_namespace());
2034 
2035  if (equals && l.is_variable())
2036  // These are variable symbols. Let's compare their symbol size.
2037  // The symbol size in this case is the size taken by the storage
2038  // of the variable. If that size changes, then it's an ABI
2039  // change.
2040  equals = l.get_size() == r.get_size();
2041 
2042  return equals;
2043 }
2044 
2045 /// Getter of the environment used by the current instance of @ref
2046 /// elf_symbol.
2047 ///
2048 /// @return the enviroment used by the current instance of @ref elf_symbol.
2049 const environment&
2051 {return priv_->env_;}
2052 
2053 /// Getter for the index
2054 ///
2055 /// @return the index of the symbol.
2056 size_t
2058 {return priv_->index_;}
2059 
2060 /// Setter for the index.
2061 ///
2062 /// @param s the new index.
2063 void
2065 {priv_->index_ = s;}
2066 
2067 /// Getter for the name of the @ref elf_symbol.
2068 ///
2069 /// @return a reference to the name of the @ref symbol.
2070 const string&
2072 {return priv_->name_;}
2073 
2074 /// Setter for the name of the current intance of @ref elf_symbol.
2075 ///
2076 /// @param n the new name.
2077 void
2078 elf_symbol::set_name(const string& n)
2079 {
2080  priv_->name_ = n;
2081  priv_->id_string_.clear();
2082 }
2083 
2084 /// Getter for the type of the current instance of @ref elf_symbol.
2085 ///
2086 /// @return the type of the elf symbol.
2089 {return priv_->type_;}
2090 
2091 /// Setter for the type of the current instance of @ref elf_symbol.
2092 ///
2093 /// @param t the new symbol type.
2094 void
2096 {priv_->type_ = t;}
2097 
2098 /// Getter of the size of the symbol.
2099 ///
2100 /// @return the size of the symbol, in bytes.
2101 size_t
2103 {return priv_->size_;}
2104 
2105 /// Setter of the size of the symbol.
2106 ///
2107 /// @param size the new size of the symbol, in bytes.
2108 void
2110 {priv_->size_ = size;}
2111 
2112 /// Getter for the binding of the current instance of @ref elf_symbol.
2113 ///
2114 /// @return the binding of the symbol.
2117 {return priv_->binding_;}
2118 
2119 /// Setter for the binding of the current instance of @ref elf_symbol.
2120 ///
2121 /// @param b the new binding.
2122 void
2124 {priv_->binding_ = b;}
2125 
2126 /// Getter for the version of the current instanc of @ref elf_symbol.
2127 ///
2128 /// @return the version of the elf symbol.
2131 {return priv_->version_;}
2132 
2133 /// Setter for the version of the current instance of @ref elf_symbol.
2134 ///
2135 /// @param v the new version of the elf symbol.
2136 void
2138 {
2139  priv_->version_ = v;
2140  priv_->id_string_.clear();
2141 }
2142 
2143 /// Setter of the visibility of the current instance of @ref
2144 /// elf_symbol.
2145 ///
2146 /// @param v the new visibility of the elf symbol.
2147 void
2149 {priv_->visibility_ = v;}
2150 
2151 /// Getter of the visibility of the current instance of @ref
2152 /// elf_symbol.
2153 ///
2154 /// @return the visibility of the elf symbol.
2157 {return priv_->visibility_;}
2158 
2159 /// Test if the current instance of @ref elf_symbol is defined or not.
2160 ///
2161 /// @return true if the current instance of @ref elf_symbol is
2162 /// defined, false otherwise.
2163 bool
2165 {return priv_->is_defined_;}
2166 
2167 /// Sets a flag saying if the current instance of @ref elf_symbol is
2168 /// defined
2169 ///
2170 /// @param b the new value of the flag.
2171 void
2173 {priv_->is_defined_ = d;}
2174 
2175 /// Test if the current instance of @ref elf_symbol is public or not.
2176 ///
2177 /// This tests if the symbol is defined, has default or protected
2178 ///visibility, and either:
2179 /// - has global binding
2180 /// - has weak binding
2181 /// - or has a GNU_UNIQUE binding.
2182 ///
2183 /// return true if the current instance of @ref elf_symbol is public,
2184 /// false otherwise.
2185 bool
2187 {
2188  return (is_defined()
2189  && (get_binding() == GLOBAL_BINDING
2190  || get_binding() == WEAK_BINDING
2191  || get_binding() == GNU_UNIQUE_BINDING)
2192  && (get_visibility() == DEFAULT_VISIBILITY
2193  || get_visibility() == PROTECTED_VISIBILITY));
2194 }
2195 
2196 /// Test if the current instance of @ref elf_symbol is a function
2197 /// symbol or not.
2198 ///
2199 /// @return true if the current instance of @ref elf_symbol is a
2200 /// function symbol, false otherwise.
2201 bool
2203 {return get_type() == FUNC_TYPE || get_type() == GNU_IFUNC_TYPE;}
2204 
2205 /// Test if the current instance of @ref elf_symbol is a variable
2206 /// symbol or not.
2207 ///
2208 /// @return true if the current instance of @ref elf_symbol is a
2209 /// variable symbol, false otherwise.
2210 bool
2212 {
2213  return (get_type() == OBJECT_TYPE
2214  || get_type() == TLS_TYPE
2215  // It appears that undefined variables have NOTYPE type.
2216  || (get_type() == NOTYPE_TYPE
2217  && !is_defined()));
2218 }
2219 
2220 /// Getter of the 'is-in-ksymtab' property.
2221 ///
2222 /// @return true iff the current symbol is in the Linux Kernel
2223 /// specific 'ksymtab' symbol table.
2224 bool
2226 {return priv_->is_in_ksymtab_;}
2227 
2228 /// Setter of the 'is-in-ksymtab' property.
2229 ///
2230 /// @param is_in_ksymtab this is true iff the current symbol is in the
2231 /// Linux Kernel specific 'ksymtab' symbol table.
2232 void
2234 {priv_->is_in_ksymtab_ = is_in_ksymtab;}
2235 
2236 /// Getter of the 'crc' property.
2237 ///
2238 /// @return the CRC (modversions) value for Linux Kernel symbols, if any
2241 {return priv_->crc_;}
2242 
2243 /// Setter of the 'crc' property.
2244 ///
2245 /// @param crc the new CRC (modversions) value for Linux Kernel symbols
2246 void
2248 {priv_->crc_ = crc;}
2249 
2250 /// Getter of the 'namespace' property.
2251 ///
2252 /// @return the namespace for Linux Kernel symbols, if any
2255 {return priv_->namespace_;}
2256 
2257 /// Setter of the 'namespace' property.
2258 ///
2259 /// @param ns the new namespace for Linux Kernel symbols, if any
2260 void
2262 {priv_->namespace_ = ns;}
2263 
2264 /// Getter for the 'is-suppressed' property.
2265 ///
2266 /// @return true iff the current symbol has been suppressed by a
2267 /// suppression specification that was provided in the context that
2268 /// led to the creation of the corpus this ELF symbol belongs to.
2269 bool
2271 {return priv_->is_suppressed_;}
2272 
2273 /// Setter for the 'is-suppressed' property.
2274 ///
2275 /// @param true iff the current symbol has been suppressed by a
2276 /// suppression specification that was provided in the context that
2277 /// led to the creation of the corpus this ELF symbol belongs to.
2278 void
2280 {priv_->is_suppressed_ = is_suppressed;}
2281 
2282 /// @name Elf symbol aliases
2283 ///
2284 /// An alias A for an elf symbol S is a symbol that is defined at the
2285 /// same address as S. S is chained to A through the
2286 /// elf_symbol::get_next_alias() method.
2287 ///
2288 /// When there are several aliases to a symbol, the main symbol is the
2289 /// the first symbol found in the symbol table for a given address.
2290 ///
2291 /// The alias chain is circular. That means if S is the main symbol
2292 /// and A is the alias, S is chained to A and A
2293 /// is chained back to the main symbol S. The last alias in an alias
2294 ///chain is always chained to the main symbol.
2295 ///
2296 /// Thus, when looping over the aliases of an elf_symbol A, detecting
2297 /// an alias that is equal to the main symbol should logically be a
2298 /// loop exit condition.
2299 ///
2300 /// Accessing and adding aliases for instances of elf_symbol is done
2301 /// through the member functions below.
2302 
2303 /// @{
2304 
2305 /// Get the main symbol of an alias chain.
2306 ///
2307 ///@return the main symbol.
2308 const elf_symbol_sptr
2310 {return priv_->main_symbol_.lock();}
2311 
2312 /// Get the main symbol of an alias chain.
2313 ///
2314 ///@return the main symbol.
2317 {return priv_->main_symbol_.lock();}
2318 
2319 /// Tests whether this symbol is the main symbol.
2320 ///
2321 /// @return true iff this symbol is the main symbol.
2322 bool
2324 {return get_main_symbol().get() == this;}
2325 
2326 /// Get the next alias of the current symbol.
2327 ///
2328 ///@return the alias, or NULL if there is no alias.
2331 {return priv_->next_alias_.lock();}
2332 
2333 
2334 /// Check if the current elf_symbol has an alias.
2335 ///
2336 ///@return true iff the current elf_symbol has an alias.
2337 bool
2339 {return bool(get_next_alias());}
2340 
2341 /// Get the number of aliases to this elf symbol
2342 ///
2343 /// @return the number of aliases to this elf symbol.
2344 int
2346 {
2347  int result = 0;
2348 
2349  for (elf_symbol_sptr a = get_next_alias();
2350  a && a.get() != get_main_symbol().get();
2351  a = a->get_next_alias())
2352  ++result;
2353 
2354  return result;
2355 }
2356 
2357 /// Add an alias to the current elf symbol.
2358 ///
2359 /// @param alias the new alias. Note that this elf_symbol should *NOT*
2360 /// have aliases prior to the invocation of this function.
2361 void
2363 {
2364  if (!alias)
2365  return;
2366 
2367  ABG_ASSERT(!alias->has_aliases());
2369 
2370  if (has_aliases())
2371  {
2372  elf_symbol_sptr last_alias;
2373  for (elf_symbol_sptr a = get_next_alias();
2374  a && !a->is_main_symbol();
2375  a = a->get_next_alias())
2376  {
2377  if (a->get_next_alias()->is_main_symbol())
2378  {
2379  ABG_ASSERT(last_alias == 0);
2380  last_alias = a;
2381  }
2382  }
2383  ABG_ASSERT(last_alias);
2384 
2385  last_alias->priv_->next_alias_ = alias;
2386  }
2387  else
2388  priv_->next_alias_ = alias;
2389 
2390  alias->priv_->next_alias_ = get_main_symbol();
2391  alias->priv_->main_symbol_ = get_main_symbol();
2392 }
2393 
2394 /// Update the main symbol for a group of aliased symbols
2395 ///
2396 /// If after the construction of the symbols (in order of discovery), the
2397 /// actual main symbol can be identified (e.g. as the symbol that actually is
2398 /// defined in the code), this method offers a way of updating the main symbol
2399 /// through one of the aliased symbols.
2400 ///
2401 /// For that, locate the new main symbol by name and update all references to
2402 /// the main symbol among the group of aliased symbols.
2403 ///
2404 /// @param name the name of the main symbol
2405 ///
2406 /// @return the new main elf_symbol
2408 elf_symbol::update_main_symbol(const std::string& name)
2409 {
2411  if (!has_aliases() || get_name() == name)
2412  return get_main_symbol();
2413 
2414  // find the new main symbol
2415  elf_symbol_sptr new_main;
2416  // we've already checked this; check the rest of the aliases
2417  for (elf_symbol_sptr a = get_next_alias(); a.get() != this;
2418  a = a->get_next_alias())
2419  if (a->get_name() == name)
2420  {
2421  new_main = a;
2422  break;
2423  }
2424 
2425  if (!new_main)
2426  return get_main_symbol();
2427 
2428  // now update all main symbol references
2429  priv_->main_symbol_ = new_main;
2430  for (elf_symbol_sptr a = get_next_alias(); a.get() != this;
2431  a = a->get_next_alias())
2432  a->priv_->main_symbol_ = new_main;
2433 
2434  return new_main;
2435 }
2436 
2437 /// Return true if the symbol is a common one.
2438 ///
2439 /// @return true iff the symbol is common.
2440 bool
2442 {return priv_->is_common_;}
2443 
2444 /// Return true if this common common symbol has other common instances.
2445 ///
2446 /// A common instance of a given common symbol is another common
2447 /// symbol with the same name. Those exist in relocatable files. The
2448 /// linker normally allocates all the instances into a common block in
2449 /// the final output file.
2450 ///
2451 /// Note that the current object must be a common symbol, otherwise,
2452 /// this function aborts.
2453 ///
2454 /// @return true iff the current common symbol has other common
2455 /// instances.
2456 bool
2458 {
2460  return bool(get_next_common_instance());
2461 }
2462 
2463 /// Get the next common instance of the current common symbol.
2464 ///
2465 /// A common instance of a given common symbol is another common
2466 /// symbol with the same name. Those exist in relocatable files. The
2467 /// linker normally allocates all the instances into a common block in
2468 /// the final output file.
2469 ///
2470 /// @return the next common instance, or nil if there is not any.
2473 {return priv_->next_common_instance_.lock();}
2474 
2475 /// Add a common instance to the current common elf symbol.
2476 ///
2477 /// Note that this symbol must be the main symbol. Being the main
2478 /// symbol means being the first common symbol to appear in the symbol
2479 /// table.
2480 ///
2481 /// @param common the other common instance to add.
2482 void
2484 {
2485  if (!common)
2486  return;
2487 
2488  ABG_ASSERT(!common->has_other_common_instances());
2491 
2493  {
2494  elf_symbol_sptr last_common_instance;
2496  c && (c.get() != get_main_symbol().get());
2497  c = c->get_next_common_instance())
2498  {
2499  if (c->get_next_common_instance().get() == get_main_symbol().get())
2500  {
2501  ABG_ASSERT(last_common_instance == 0);
2502  last_common_instance = c;
2503  }
2504  }
2505  ABG_ASSERT(last_common_instance);
2506 
2507  last_common_instance->priv_->next_common_instance_ = common;
2508  }
2509  else
2510  priv_->next_common_instance_ = common;
2511 
2512  common->priv_->next_common_instance_ = get_main_symbol();
2513  common->priv_->main_symbol_ = get_main_symbol();
2514 }
2515 
2516 /// Get a string that is representative of a given elf_symbol.
2517 ///
2518 /// If the symbol has a version, then the ID string is the
2519 /// concatenation of the name of the symbol, the '@' character, and
2520 /// the version of the symbol. If the version is the default version
2521 /// of the symbol then the '@' character is replaced by a "@@" string.
2522 ///
2523 /// Otherwise, if the symbol does not have any version, this function
2524 /// returns the name of the symbol.
2525 ///
2526 /// @return a the ID string.
2527 const string&
2529 {
2530  if (priv_->id_string_.empty())
2531  {
2532  string s = get_name ();
2533 
2534  if (!get_version().is_empty())
2535  {
2536  if (get_version().is_default())
2537  s += "@@";
2538  else
2539  s += "@";
2540  s += get_version().str();
2541  }
2542  priv_->id_string_ = s;
2543  }
2544 
2545  return priv_->id_string_;
2546 }
2547 
2548 /// From the aliases of the current symbol, lookup one with a given name.
2549 ///
2550 /// @param name the name of symbol alias we are looking for.
2551 ///
2552 /// @return the symbol alias that has the name @p name, or nil if none
2553 /// has been found.
2555 elf_symbol::get_alias_from_name(const string& name) const
2556 {
2557  if (name == get_name())
2558  return elf_symbol_sptr(priv_->main_symbol_);
2559 
2560  for (elf_symbol_sptr a = get_next_alias();
2561  a && a.get() != get_main_symbol().get();
2562  a = a->get_next_alias())
2563  if (a->get_name() == name)
2564  return a;
2565 
2566  return elf_symbol_sptr();
2567 }
2568 
2569 /// In the list of aliases of a given elf symbol, get the alias that
2570 /// equals this current symbol.
2571 ///
2572 /// @param other the elf symbol to get the potential aliases from.
2573 ///
2574 /// @return the alias of @p other that texually equals the current
2575 /// symbol, or nil if no alias textually equals the current symbol.
2578 {
2579  for (elf_symbol_sptr a = other.get_next_alias();
2580  a && a.get() != a->get_main_symbol().get();
2581  a = a->get_next_alias())
2582  if (textually_equals(*this, *a))
2583  return a;
2584  return elf_symbol_sptr();
2585 }
2586 
2587 /// Return a comma separated list of the id of the current symbol as
2588 /// well as the id string of its aliases.
2589 ///
2590 /// @param syms a map of all the symbols of the corpus the current
2591 /// symbol belongs to.
2592 ///
2593 /// @param include_symbol_itself if set to true, then the name of the
2594 /// current symbol is included in the list of alias names that is emitted.
2595 ///
2596 /// @return the string.
2597 string
2599  bool include_symbol_itself) const
2600 {
2601  string result;
2602 
2603  if (include_symbol_itself)
2604  result = get_id_string();
2605 
2606  vector<elf_symbol_sptr> aliases;
2607  compute_aliases_for_elf_symbol(*this, syms, aliases);
2608  if (!aliases.empty() && include_symbol_itself)
2609  result += ", ";
2610 
2611  for (vector<elf_symbol_sptr>::const_iterator i = aliases.begin();
2612  i != aliases.end();
2613  ++i)
2614  {
2615  if (i != aliases.begin())
2616  result += ", ";
2617  result += (*i)->get_id_string();
2618  }
2619  return result;
2620 }
2621 
2622 /// Return a comma separated list of the id of the current symbol as
2623 /// well as the id string of its aliases.
2624 ///
2625 /// @param include_symbol_itself if set to true, then the name of the
2626 /// current symbol is included in the list of alias names that is emitted.
2627 ///
2628 /// @return the string.
2629 string
2630 elf_symbol::get_aliases_id_string(bool include_symbol_itself) const
2631 {
2632  vector<elf_symbol_sptr> aliases;
2633  if (include_symbol_itself)
2634  aliases.push_back(get_main_symbol());
2635 
2636  for (elf_symbol_sptr a = get_next_alias();
2637  a && a.get() != get_main_symbol().get();
2638  a = a->get_next_alias())
2639  aliases.push_back(a);
2640 
2641  string result;
2642  for (vector<elf_symbol_sptr>::const_iterator i = aliases.begin();
2643  i != aliases.end();
2644  ++i)
2645  {
2646  if (i != aliases.begin())
2647  result += ", ";
2648  result += (*i)->get_id_string();
2649  }
2650 
2651  return result;
2652 }
2653 
2654 /// Given the ID of a symbol, get the name and the version of said
2655 /// symbol.
2656 ///
2657 /// @param id the symbol ID to consider.
2658 ///
2659 /// @param name the symbol name extracted from the ID. This is set
2660 /// only if the function returned true.
2661 ///
2662 /// @param ver the symbol version extracted from the ID.
2663 bool
2665  string& name,
2666  string& ver)
2667 {
2668  name.clear(), ver.clear();
2669 
2670  string::size_type i = id.find('@');
2671  if (i == string::npos)
2672  {
2673  name = id;
2674  return true;
2675  }
2676 
2677  name = id.substr(0, i);
2678  ++i;
2679 
2680  if (i >= id.size())
2681  return true;
2682 
2683  string::size_type j = id.find('@', i);
2684  if (j == string::npos)
2685  j = i;
2686  else
2687  ++j;
2688 
2689  if (j >= id.size())
2690  {
2691  ver = "";
2692  return true;
2693  }
2694 
2695  ver = id.substr(j);
2696  return true;
2697 }
2698 
2699 ///@}
2700 
2701 /// Test if two main symbols are textually equal, or, if they have
2702 /// aliases that are textually equal.
2703 ///
2704 /// @param other the symbol to compare against.
2705 ///
2706 /// @return true iff the current instance of elf symbol equals the @p
2707 /// other.
2708 bool
2710 {
2711  bool are_equal = textually_equals(*this, other);
2712  if (!are_equal)
2713  are_equal = bool(get_alias_which_equals(other));
2714  return are_equal;
2715 }
2716 
2717 /// Test if the current symbol aliases another one.
2718 ///
2719 /// @param o the other symbol to test against.
2720 ///
2721 /// @return true iff the current symbol aliases @p o.
2722 bool
2724 {
2725  if (*this == o)
2726  return true;
2727 
2728  if (get_main_symbol() == o.get_main_symbol())
2729  return true;
2730 
2731  for (elf_symbol_sptr a = get_next_alias();
2732  a && !a->is_main_symbol();
2733  a = a->get_next_alias())
2734  {
2735  if (o == *a)
2736  return true;
2737  }
2738  return false;
2739 }
2740 
2741 /// Equality operator for smart pointers to elf_symbol.
2742 ///
2743 /// @param lhs the first elf symbol to consider.
2744 ///
2745 /// @param rhs the second elf symbol to consider.
2746 ///
2747 /// @return true iff @p lhs equals @p rhs.
2748 bool
2750 {
2751  if (!!lhs != !!rhs)
2752  return false;
2753 
2754  if (!lhs)
2755  return true;
2756 
2757  return *lhs == *rhs;
2758 }
2759 
2760 /// Inequality operator for smart pointers to elf_symbol.
2761 ///
2762 /// @param lhs the first elf symbol to consider.
2763 ///
2764 /// @param rhs the second elf symbol to consider.
2765 ///
2766 /// @return true iff @p lhs is different from @p rhs.
2767 bool
2769 {return !operator==(lhs, rhs);}
2770 
2771 /// Test if two symbols alias.
2772 ///
2773 /// @param s1 the first symbol to consider.
2774 ///
2775 /// @param s2 the second symbol to consider.
2776 ///
2777 /// @return true if @p s1 aliases @p s2.
2778 bool
2780 {return s1.does_alias(s2) || s2.does_alias(s1);}
2781 
2782 void
2783 compute_aliases_for_elf_symbol(const elf_symbol& sym,
2784  const string_elf_symbols_map_type& symtab,
2785  vector<elf_symbol_sptr>& aliases)
2786 {
2787 
2788  if (elf_symbol_sptr a = sym.get_next_alias())
2789  for (; a && !a->is_main_symbol(); a = a->get_next_alias())
2790  aliases.push_back(a);
2791  else
2792  for (string_elf_symbols_map_type::const_iterator i = symtab.begin();
2793  i != symtab.end();
2794  ++i)
2795  for (elf_symbols::const_iterator j = i->second.begin();
2796  j != i->second.end();
2797  ++j)
2798  {
2799  if (**j == sym)
2800  for (elf_symbol_sptr s = (*j)->get_next_alias();
2801  s && !s->is_main_symbol();
2802  s = s->get_next_alias())
2803  aliases.push_back(s);
2804  else
2805  for (elf_symbol_sptr s = (*j)->get_next_alias();
2806  s && !s->is_main_symbol();
2807  s = s->get_next_alias())
2808  if (*s == sym)
2809  aliases.push_back(*j);
2810  }
2811 }
2812 
2813 /// Test if two symbols alias.
2814 ///
2815 /// @param s1 the first symbol to consider.
2816 ///
2817 /// @param s2 the second symbol to consider.
2818 ///
2819 /// @return true if @p s1 aliases @p s2.
2820 bool
2822 {
2823  if (!!s1 != !!s2)
2824  return false;
2825  if (s1 == s2)
2826  return true;
2827  return elf_symbols_alias(*s1, *s2);
2828 }
2829 
2830 /// Test if two symbols alias.
2831 ///
2832 /// @param s1 the first symbol to consider.
2833 ///
2834 /// @param s2 the second symbol to consider.
2835 ///
2836 /// @return true if @p s1 aliases @p s2.
2837 bool
2839 {return elf_symbols_alias(s1.get(), s2.get());}
2840 
2841 /// Serialize an instance of @ref symbol_type and stream it to a given
2842 /// output stream.
2843 ///
2844 /// @param o the output stream to serialize the symbole type to.
2845 ///
2846 /// @param t the symbol type to serialize.
2847 std::ostream&
2848 operator<<(std::ostream& o, elf_symbol::type t)
2849 {
2850  string repr;
2851 
2852  switch (t)
2853  {
2854  case elf_symbol::NOTYPE_TYPE:
2855  repr = "unspecified symbol type";
2856  break;
2857  case elf_symbol::OBJECT_TYPE:
2858  repr = "variable symbol type";
2859  break;
2860  case elf_symbol::FUNC_TYPE:
2861  repr = "function symbol type";
2862  break;
2863  case elf_symbol::SECTION_TYPE:
2864  repr = "section symbol type";
2865  break;
2866  case elf_symbol::FILE_TYPE:
2867  repr = "file symbol type";
2868  break;
2869  case elf_symbol::COMMON_TYPE:
2870  repr = "common data object symbol type";
2871  break;
2872  case elf_symbol::TLS_TYPE:
2873  repr = "thread local data object symbol type";
2874  break;
2875  case elf_symbol::GNU_IFUNC_TYPE:
2876  repr = "indirect function symbol type";
2877  break;
2878  default:
2879  {
2880  std::ostringstream s;
2881  s << "unknown symbol type (" << (char)t << ')';
2882  repr = s.str();
2883  }
2884  break;
2885  }
2886 
2887  o << repr;
2888  return o;
2889 }
2890 
2891 /// Serialize an instance of @ref symbol_binding and stream it to a
2892 /// given output stream.
2893 ///
2894 /// @param o the output stream to serialize the symbole type to.
2895 ///
2896 /// @param b the symbol binding to serialize.
2897 std::ostream&
2898 operator<<(std::ostream& o, elf_symbol::binding b)
2899 {
2900  string repr;
2901 
2902  switch (b)
2903  {
2904  case elf_symbol::LOCAL_BINDING:
2905  repr = "local binding";
2906  break;
2907  case elf_symbol::GLOBAL_BINDING:
2908  repr = "global binding";
2909  break;
2910  case elf_symbol::WEAK_BINDING:
2911  repr = "weak binding";
2912  break;
2913  case elf_symbol::GNU_UNIQUE_BINDING:
2914  repr = "GNU unique binding";
2915  break;
2916  default:
2917  {
2918  std::ostringstream s;
2919  s << "unknown binding (" << (unsigned char) b << ")";
2920  repr = s.str();
2921  }
2922  break;
2923  }
2924 
2925  o << repr;
2926  return o;
2927 }
2928 
2929 /// Serialize an instance of @ref elf_symbol::visibility and stream it
2930 /// to a given output stream.
2931 ///
2932 /// @param o the output stream to serialize the symbole type to.
2933 ///
2934 /// @param v the symbol visibility to serialize.
2935 std::ostream&
2936 operator<<(std::ostream& o, elf_symbol::visibility v)
2937 {
2938  string repr;
2939 
2940  switch (v)
2941  {
2942  case elf_symbol::DEFAULT_VISIBILITY:
2943  repr = "default visibility";
2944  break;
2945  case elf_symbol::PROTECTED_VISIBILITY:
2946  repr = "protected visibility";
2947  break;
2948  case elf_symbol::HIDDEN_VISIBILITY:
2949  repr = "hidden visibility";
2950  break;
2951  case elf_symbol::INTERNAL_VISIBILITY:
2952  repr = "internal visibility";
2953  break;
2954  default:
2955  {
2956  std::ostringstream s;
2957  s << "unknown visibility (" << (unsigned char) v << ")";
2958  repr = s.str();
2959  }
2960  break;
2961  }
2962 
2963  o << repr;
2964  return o;
2965 }
2966 
2967 /// Convert a string representing a symbol type into an
2968 /// elf_symbol::type.
2969 ///
2970 ///@param s the string to convert.
2971 ///
2972 ///@param t the resulting elf_symbol::type.
2973 ///
2974 /// @return true iff the conversion completed successfully.
2975 bool
2977 {
2978  if (s == "no-type")
2979  t = elf_symbol::NOTYPE_TYPE;
2980  else if (s == "object-type")
2981  t = elf_symbol::OBJECT_TYPE;
2982  else if (s == "func-type")
2983  t = elf_symbol::FUNC_TYPE;
2984  else if (s == "section-type")
2985  t = elf_symbol::SECTION_TYPE;
2986  else if (s == "file-type")
2987  t = elf_symbol::FILE_TYPE;
2988  else if (s == "common-type")
2989  t = elf_symbol::COMMON_TYPE;
2990  else if (s == "tls-type")
2991  t = elf_symbol::TLS_TYPE;
2992  else if (s == "gnu-ifunc-type")
2993  t = elf_symbol::GNU_IFUNC_TYPE;
2994  else
2995  return false;
2996 
2997  return true;
2998 }
2999 
3000 /// Convert a string representing a an elf symbol binding into an
3001 /// elf_symbol::binding.
3002 ///
3003 /// @param s the string to convert.
3004 ///
3005 /// @param b the resulting elf_symbol::binding.
3006 ///
3007 /// @return true iff the conversion completed successfully.
3008 bool
3010 {
3011  if (s == "local-binding")
3012  b = elf_symbol::LOCAL_BINDING;
3013  else if (s == "global-binding")
3014  b = elf_symbol::GLOBAL_BINDING;
3015  else if (s == "weak-binding")
3016  b = elf_symbol::WEAK_BINDING;
3017  else if (s == "gnu-unique-binding")
3018  b = elf_symbol::GNU_UNIQUE_BINDING;
3019  else
3020  return false;
3021 
3022  return true;
3023 }
3024 
3025 /// Convert a string representing a an elf symbol visibility into an
3026 /// elf_symbol::visibility.
3027 ///
3028 /// @param s the string to convert.
3029 ///
3030 /// @param b the resulting elf_symbol::visibility.
3031 ///
3032 /// @return true iff the conversion completed successfully.
3033 bool
3035 {
3036  if (s == "default-visibility")
3037  v = elf_symbol::DEFAULT_VISIBILITY;
3038  else if (s == "protected-visibility")
3039  v = elf_symbol::PROTECTED_VISIBILITY;
3040  else if (s == "hidden-visibility")
3041  v = elf_symbol::HIDDEN_VISIBILITY;
3042  else if (s == "internal-visibility")
3043  v = elf_symbol::INTERNAL_VISIBILITY;
3044  else
3045  return false;
3046 
3047  return true;
3048 }
3049 
3050 /// Test if the type of an ELF symbol denotes a function symbol.
3051 ///
3052 /// @param t the type of the ELF symbol.
3053 ///
3054 /// @return true iff elf symbol type @p t denotes a function symbol
3055 /// type.
3056 bool
3058 {return t == elf_symbol::FUNC_TYPE;}
3059 
3060 /// Test if the type of an ELF symbol denotes a function symbol.
3061 ///
3062 /// @param t the type of the ELF symbol.
3063 ///
3064 /// @return true iff elf symbol type @p t denotes a function symbol
3065 /// type.
3066 bool
3068 {return t == elf_symbol::OBJECT_TYPE;}
3069 
3070 // <elf_symbol::version stuff>
3071 
3072 struct elf_symbol::version::priv
3073 {
3074  string version_;
3075  bool is_default_;
3076 
3077  priv()
3078  : is_default_(false)
3079  {}
3080 
3081  priv(const string& v,
3082  bool d)
3083  : version_(v),
3084  is_default_(d)
3085  {}
3086 }; // end struct elf_symbol::version::priv
3087 
3088 elf_symbol::version::version()
3089  : priv_(new priv)
3090 {}
3091 
3092 /// @param v the name of the version.
3093 ///
3094 /// @param is_default true if this is a default version.
3095 elf_symbol::version::version(const string& v,
3096  bool is_default)
3097  : priv_(new priv(v, is_default))
3098 {}
3099 
3100 elf_symbol::version::version(const elf_symbol::version& v)
3101  : priv_(new priv(v.str(), v.is_default()))
3102 {
3103 }
3104 
3105 elf_symbol::version::~version() = default;
3106 
3107 /// Cast the version_type into a string that is its name.
3108 ///
3109 /// @return the name of the version.
3110 elf_symbol::version::operator const string&() const
3111 {return priv_->version_;}
3112 
3113 /// Getter for the version name.
3114 ///
3115 /// @return the version name.
3116 const string&
3118 {return priv_->version_;}
3119 
3120 /// Setter for the version name.
3121 ///
3122 /// @param s the version name.
3123 void
3125 {priv_->version_ = s;}
3126 
3127 /// Getter for the 'is_default' property of the version.
3128 ///
3129 /// @return true iff this is a default version.
3130 bool
3132 {return priv_->is_default_;}
3133 
3134 /// Setter for the 'is_default' property of the version.
3135 ///
3136 /// @param f true if this is the default version.
3137 void
3139 {priv_->is_default_ = f;}
3140 
3141 bool
3142 elf_symbol::version::is_empty() const
3143 {return str().empty();}
3144 
3145 /// Compares the current version against another one.
3146 ///
3147 /// @param o the other version to compare the current one to.
3148 ///
3149 /// @return true iff the current version equals @p o.
3150 bool
3152 {return str() == o.str();}
3153 
3154 /// Inequality operator.
3155 ///
3156 /// @param o the version to compare against the current one.
3157 ///
3158 /// @return true iff both versions are different.
3159 bool
3161 {return !operator==(o);}
3162 
3163 /// Assign a version to the current one.
3164 ///
3165 /// @param o the other version to assign to this one.
3166 ///
3167 /// @return a reference to the assigned version.
3170 {
3171  str(o.str());
3172  is_default(o.is_default());
3173  return *this;
3174 }
3175 
3176 // </elf_symbol::version stuff>
3177 
3178 // </elf_symbol stuff>
3179 
3180 // <class dm_context_rel stuff>
3181 struct dm_context_rel::priv
3182 {
3183  bool is_laid_out_;
3184  size_t offset_in_bits_;
3185  var_decl* anonymous_data_member_;
3186 
3187  priv(bool is_static = false)
3188  : is_laid_out_(!is_static),
3189  offset_in_bits_(0),
3190  anonymous_data_member_()
3191  {}
3192 
3193  priv(bool is_laid_out, size_t offset_in_bits)
3194  : is_laid_out_(is_laid_out),
3195  offset_in_bits_(offset_in_bits),
3196  anonymous_data_member_()
3197  {}
3198 }; //end struct dm_context_rel::priv
3199 
3200 dm_context_rel::dm_context_rel()
3201  : context_rel(),
3202  priv_(new priv)
3203 {}
3204 
3205 dm_context_rel::dm_context_rel(scope_decl* s,
3206  bool is_laid_out,
3207  size_t offset_in_bits,
3208  access_specifier a,
3209  bool is_static)
3210  : context_rel(s, a, is_static),
3211  priv_(new priv(is_laid_out, offset_in_bits))
3212 {}
3213 
3214 dm_context_rel::dm_context_rel(scope_decl* s)
3215  : context_rel(s),
3216  priv_(new priv())
3217 {}
3218 
3219 bool
3220 dm_context_rel::get_is_laid_out() const
3221 {return priv_->is_laid_out_;}
3222 
3223 void
3224 dm_context_rel::set_is_laid_out(bool f)
3225 {priv_->is_laid_out_ = f;}
3226 
3227 size_t
3228 dm_context_rel::get_offset_in_bits() const
3229 {return priv_->offset_in_bits_;}
3230 
3231 void
3232 dm_context_rel::set_offset_in_bits(size_t o)
3233 {priv_->offset_in_bits_ = o;}
3234 
3235 bool
3236 dm_context_rel::operator==(const dm_context_rel& o) const
3237 {
3238  if (!context_rel::operator==(o))
3239  return false;
3240 
3241  return (priv_->is_laid_out_ == o.priv_->is_laid_out_
3242  && priv_->offset_in_bits_ == o.priv_->offset_in_bits_);
3243 }
3244 
3245 bool
3246 dm_context_rel::operator!=(const dm_context_rel& o) const
3247 {return !operator==(o);}
3248 
3249 /// Return a non-nil value if this data member context relationship
3250 /// has an anonymous data member. That means, if the data member this
3251 /// relation belongs to is part of an anonymous data member.
3252 ///
3253 /// @return the containing anonymous data member of this data member
3254 /// relationship. Nil if there is none.
3255 const var_decl*
3257 {return priv_->anonymous_data_member_;}
3258 
3259 /// Set the containing anonymous data member of this data member
3260 /// context relationship. That means that the data member this
3261 /// relation belongs to is part of an anonymous data member.
3262 ///
3263 /// @param anon_dm the containing anonymous data member of this data
3264 /// member relationship. Nil if there is none.
3265 void
3267 {priv_->anonymous_data_member_ = anon_dm;}
3268 
3269 dm_context_rel::~dm_context_rel()
3270 {}
3271 // </class dm_context_rel stuff>
3272 
3273 // <environment stuff>
3274 
3275 /// Convenience typedef for a map of interned_string -> bool.
3276 typedef unordered_map<interned_string,
3278 
3279 
3280 /// Default constructor of the @ref environment type.
3282  :priv_(new priv)
3283 {}
3284 
3285 /// Destructor for the @ref environment type.
3287 {}
3288 
3289 /// Getter the map of canonical types.
3290 ///
3291 /// @return the map of canonical types. The key of the map is the
3292 /// hash of the canonical type and its value if the canonical type.
3295 {return priv_->canonical_types_;}
3296 
3297 /// Getter the map of canonical types.
3298 ///
3299 /// @return the map of canonical types. The key of the map is the
3300 /// hash of the canonical type and its value if the canonical type.
3303 {return const_cast<environment*>(this)->get_canonical_types_map();}
3304 
3305 /// Helper to detect if a type is either a reference, a pointer, or a
3306 /// qualified type.
3307 static bool
3308 is_ptr_ref_or_qual_type(const type_base *t)
3309 {
3310  if (is_pointer_type(t)
3311  || is_reference_type(t)
3312  || is_qualified_type(t))
3313  return true;
3314  return false;
3315 }
3316 
3317 /// Compare decls using their locations.
3318 ///
3319 /// @param f the first decl to compare.
3320 ///
3321 /// @param s the second decl to compare.
3322 ///
3323 /// @return true if @p f compares less than @p s.
3324 static bool
3325 compare_using_locations(const decl_base *f,
3326  const decl_base *s)
3327 {
3328  // If a decl has artificial location, then use that one over the
3329  // natural one.
3332 
3333  ABG_ASSERT(fl.get_value() && sl.get_value());
3334  if (fl.get_is_artificial() == sl.get_is_artificial())
3335  {
3336  // The locations of the two artfifacts have the same
3337  // artificial-ness so they can be compared.
3338  string p1, p2;
3339  unsigned l1 = 0, l2 = 0, c1 = 0, c2 = 0;
3340  fl.expand(p1, l1, c1);
3341  sl.expand(p2, l2, c2);
3342  if (p1 != p2)
3343  return p1 < p2;
3344  if (l1 != l2)
3345  return l1 < l2;
3346  if (c1 != c2)
3347  return c1 < c2;
3348  }
3349 
3350  return (get_pretty_representation(f, /*internal=*/false)
3351  < get_pretty_representation(s, /*internal=*/false));
3352 }
3353 
3354 /// A functor to sort decls somewhat topologically. That is, types
3355 /// are sorted in a way that makes the ones that are defined "first"
3356 /// to come first.
3357 ///
3358 /// The topological criteria is a lexicographic sort of the definition
3359 /// location of the type. For types that have no location (or the
3360 /// same location), it's their qualified name that is used for the
3361 /// lexicographic sort.
3362 struct decl_topo_comp
3363 {
3364 
3365  /// The "Less Than" comparison operator of this functor.
3366  ///
3367  /// @param f the first decl to be considered for the comparison.
3368  ///
3369  /// @param s the second decl to be considered for the comparison.
3370  ///
3371  /// @return true iff @p f is less than @p s.
3372  bool
3373  operator()(const decl_base *f,
3374  const decl_base *s)
3375  {
3376  if (!!f != !!s)
3377  return f && !s;
3378 
3379  if (!f)
3380  return false;
3381 
3382  // Unique types that are artificially created in the environment
3383  // don't have locations. They ought to be compared on the basis
3384  // of their pretty representation before we start looking at IR
3385  // nodes' locations down the road.
3387  return (get_pretty_representation(f, /*internal=*/false)
3388  < get_pretty_representation(s, /*internal=*/false));
3389 
3390  // If both decls come from an abixml file, keep the order they
3391  // have from that abixml file.
3392  if ((!f->get_corpus() && !s->get_corpus())
3393  || (f->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN
3394  && s->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN))
3395  return compare_using_locations(f, s);
3396 
3397  // If a decl has artificial location, then use that one over the
3398  // natural one.
3399  location fl = get_artificial_or_natural_location(f);
3400  location sl = get_artificial_or_natural_location(s);
3401 
3402  if (fl.get_value() && sl.get_value())
3403  return compare_using_locations(f, s);
3404  else if (!!fl != !!sl)
3405  // So one of the decls doesn't have location data.
3406  // The first decl is less than the second if it's the one not
3407  // having location data.
3408  return !fl && sl;
3409 
3410  // We reach this point if location data is useless.
3411  if (f->get_is_anonymous()
3412  && s->get_is_anonymous()
3413  && (get_pretty_representation(f, /*internal=*/false)
3414  == get_pretty_representation(s, /*internal=*/false)))
3415  return f->get_name() < s->get_name();
3416 
3417  return (get_pretty_representation(f, /*internal=*/false)
3418  < get_pretty_representation(s, /*internal=*/false));
3419  }
3420 
3421  /// The "Less Than" comparison operator of this functor.
3422  ///
3423  /// @param f the first decl to be considered for the comparison.
3424  ///
3425  /// @param s the second decl to be considered for the comparison.
3426  ///
3427  /// @return true iff @p f is less than @p s.
3428  bool
3429  operator()(const decl_base_sptr &f,
3430  const decl_base_sptr &s)
3431  {return operator()(f.get(), s.get());}
3432 
3433 }; // end struct decl_topo_comp
3434 
3435 /// A functor to sort types somewhat topologically. That is, types
3436 /// are sorted in a way that makes the ones that are defined "first"
3437 /// to come first.
3438 ///
3439 /// The topological criteria is a lexicographic sort of the definition
3440 /// location of the type. For types that have no location, it's their
3441 /// qualified name that is used for the lexicographic sort.
3442 struct type_topo_comp
3443 {
3444  /// Test if a decl has an artificial or natural location.
3445  ///
3446  /// @param d the decl to consider
3447  ///
3448  /// @return true iff @p d has a location.
3449  bool
3450  has_artificial_or_natural_location(const decl_base* d)
3452 
3453  /// Test if a type has an artificial or natural location.
3454  ///
3455  /// @param t the type to consider
3456  ///
3457  /// @return true iff @p t has a location.
3458  bool
3459  has_artificial_or_natural_location(const type_base* t)
3460  {
3461  if (decl_base *d = is_decl(t))
3462  return has_artificial_or_natural_location(d);
3463  return false;
3464  }
3465 
3466  /// The "Less Than" comparison operator of this functor.
3467  ///
3468  /// @param f the first type to be considered for the comparison.
3469  ///
3470  /// @param s the second type to be considered for the comparison.
3471  ///
3472  /// @return true iff @p f is less than @p s.
3473  bool
3474  operator()(const type_base_sptr &f,
3475  const type_base_sptr &s)
3476  {return operator()(f.get(), s.get());}
3477 
3478  /// The "Less Than" comparison operator of this functor.
3479  ///
3480  /// @param f the first type to be considered for the comparison.
3481  ///
3482  /// @param s the second type to be considered for the comparison.
3483  ///
3484  /// @return true iff @p f is less than @p s.
3485  bool
3486  operator()(const type_base *f,
3487  const type_base *s)
3488  {
3489  // If both decls come from an abixml file, keep the order they
3490  // have from that abixml file.
3491  if ((!f->get_corpus() && !s->get_corpus())
3492  || (f->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN
3493  && s->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN))
3494  return compare_using_locations(is_decl(f), is_decl(s));
3495 
3496  bool f_is_ptr_ref_or_qual = is_ptr_ref_or_qual_type(f);
3497  bool s_is_ptr_ref_or_qual = is_ptr_ref_or_qual_type(s);
3498 
3499  if (f_is_ptr_ref_or_qual != s_is_ptr_ref_or_qual)
3500  return !f_is_ptr_ref_or_qual && s_is_ptr_ref_or_qual;
3501 
3502  if (f_is_ptr_ref_or_qual && s_is_ptr_ref_or_qual
3503  && !has_artificial_or_natural_location(f)
3504  && !has_artificial_or_natural_location(s))
3505  {
3506  string s1 = get_pretty_representation(f, /*internal=*/false);
3507  string s2 = get_pretty_representation(s, /*internal=*/false);
3508  if (s1 == s2)
3509  {
3510  if (qualified_type_def * q = is_qualified_type(f))
3511  {
3512  if (q->get_cv_quals() == qualified_type_def::CV_NONE)
3513  if (!is_qualified_type(s))
3514  // We are looking at two types that are the result of
3515  // an optimization that happens during the IR
3516  // construction. Namely, type f is a cv-qualified
3517  // type with no qualifier (no const, no volatile, no
3518  // nothing, we call it an empty-qualified type).
3519  // These are the result of an optimization which
3520  // removes "redundant qualifiers" from some types.
3521  // For instance, consider a "const reference". The
3522  // const there is redundant because a reference is
3523  // always const. So as a result of the optimizaton
3524  // that type is going to be transformed into an
3525  // empty-qualified reference. If we don't make that
3526  // optimization, then we risk having spurious change
3527  // reports down the road. But then, as a consequence
3528  // of that optimization, we need to sort the
3529  // empty-qualified type and its non-qualified variant
3530  // e.g, to ensure stability in the abixml output; both
3531  // types are logically equal, but here, we decide that
3532  // the empty-qualified one is topologically "less
3533  // than" the non-qualified counterpart.
3534  //
3535  // So here, type f is an empty-qualified type and type
3536  // s is its non-qualified variant. We decide that f
3537  // is topologically less than s.
3538  return true;
3539  }
3540  // Now let's peel off the pointer (or reference types) and
3541  // see if the ultimate underlying types have the same
3542  // textual representation; if not, use that as sorting
3543  // criterion.
3544  type_base *peeled_f =
3546  type_base *peeled_s =
3548 
3549  s1 = get_pretty_representation(peeled_f, /*internal=*/false);
3550  s2 = get_pretty_representation(peeled_s, /*internal=*/false);
3551  if (s1 != s2)
3552  return s1 < s2;
3553 
3554  // The underlying type of pointer/reference have the same
3555  // textual representation; let's try to peel of typedefs
3556  // as well and we'll consider sorting the result as decls.
3557  peeled_f = peel_typedef_pointer_or_reference_type(peeled_f, true);
3558  peeled_s = peel_typedef_pointer_or_reference_type(peeled_s, true);
3559 
3560  s1 = get_pretty_representation(peeled_f, false);
3561  s2 = get_pretty_representation(peeled_s, false);
3562  if (s1 != s2)
3563  return s1 < s2;
3564  }
3565  }
3566 
3567  string s1 = get_pretty_representation(f, false);
3568  string s2 = get_pretty_representation(s, false);
3569 
3570  if (s1 != s2)
3571  return s1 < s2;
3572 
3573  if (is_typedef(f) && is_typedef(s))
3574  {
3575  s1 = get_pretty_representation(is_typedef(f)->get_underlying_type(),
3576  false);
3577  s2 = get_pretty_representation(is_typedef(s)->get_underlying_type(),
3578  false);
3579  if (s1 != s2)
3580  return s1 < s2;
3581  }
3582 
3583  type_base *peeled_f = peel_typedef_pointer_or_reference_type(f, true);
3584  type_base *peeled_s = peel_typedef_pointer_or_reference_type(s, true);
3585 
3586  s1 = get_pretty_representation(peeled_f, false);
3587  s2 = get_pretty_representation(peeled_s, false);
3588 
3589  if (s1 != s2)
3590  return s1 < s2;
3591 
3592  decl_base *fd = is_decl(f);
3593  decl_base *sd = is_decl(s);
3594 
3595  if (!!fd != !!sd)
3596  return fd && !sd;
3597 
3598  // If the two types have no decls, how come we could not sort them
3599  // until now? Let's investigate.
3600  ABG_ASSERT(fd);
3601 
3602  // From this point, fd and sd should be non-nil
3603  decl_topo_comp decl_comp;
3604  return decl_comp(fd, sd);
3605  }
3606 }; //end struct type_topo_comp
3607 
3608 /// Sort types in a hopefully stable manner.
3609 ///
3610 /// @param types a set of types with canonical types to sort.
3611 ///
3612 /// @param result the resulting sorted vector.
3613 void
3615  vector<type_base_sptr>& result)
3616 {
3617  for (auto t: types)
3618  result.push_back(t);
3619 
3620  type_topo_comp comp;
3621  std::stable_sort(result.begin(), result.end(), comp);
3622 }
3623 
3624 /// Get the unique @ref type_decl that represents a "void" type for
3625 /// the current environment. This node must be the only one
3626 /// representing a void type in the system.
3627 ///
3628 /// Note that upon first use of this IR node (by the relevant
3629 /// front-end, for instance) it must be added to a scope using e.g,
3630 /// the @ref add_decl_to_scope() function.
3631 ///
3632 /// @return the @ref type_decl that represents a "void" type.
3633 const type_base_sptr&
3635 {
3636  if (!priv_->void_type_)
3637  priv_->void_type_.reset(new type_decl(*this,
3638  intern("void"),
3639  0, 0, location()));
3640  return priv_->void_type_;
3641 }
3642 
3643 /// Getter of the "pointer-to-void" IR node that is shared across the
3644 /// ABI corpus. This node must be the only one representing a void
3645 /// pointer type in the system.
3646 ///
3647 /// Note that upon first use of this IR node (by the relevant
3648 /// front-end, for instance) it must be added to a scope using e.g,
3649 /// the @ref add_decl_to_scope() function.
3650 ///
3651 /// @return the "pointer-to-void" IR node.
3652 const type_base_sptr&
3654 {
3655  if (!priv_->void_pointer_type_)
3656  priv_->void_pointer_type_.reset(new pointer_type_def(get_void_type(),
3657  0, 0, location()));
3658  return priv_->void_pointer_type_;
3659 }
3660 
3661 /// Get a @ref type_decl instance that represents a the type of a
3662 /// variadic function parameter. This node must be the only one
3663 /// representing a variadic parameter type in the system.
3664 ///
3665 /// Note that upon first use of this IR node (by the relevant
3666 /// front-end, for instance) it must be added to a scope using e.g,
3667 /// the @ref add_decl_to_scope() function.
3668 ///
3669 /// @return the Get a @ref type_decl instance that represents a the
3670 /// type of a variadic function parameter.
3671 const type_base_sptr&
3673 {
3674  if (!priv_->variadic_marker_type_)
3675  priv_->variadic_marker_type_.
3677  0, 0, location()));
3678  return priv_->variadic_marker_type_;
3679 }
3680 
3681 /// Getter of the name of the variadic parameter type.
3682 ///
3683 /// @return the name of the variadic parameter type.
3684 string&
3686 {
3687  static string variadic_parameter_type_name = "variadic parameter type";
3688  return variadic_parameter_type_name;
3689 }
3690 
3691 /// Test if the canonicalization of types created out of the current
3692 /// environment is done.
3693 ///
3694 /// @return true iff the canonicalization of types created out of the current
3695 /// environment is done.
3696 bool
3698 {return priv_->canonicalization_is_done_;}
3699 
3700 /// Set a flag saying if the canonicalization of types created out of
3701 /// the current environment is done or not.
3702 ///
3703 /// Note that this function must only be called by internal code of
3704 /// the library that creates ABI artifacts (e.g, read an abi corpus
3705 /// from elf or from our own xml format and creates representations of
3706 /// types out of it) and thus needs to canonicalize types to speed-up
3707 /// further type comparison.
3708 ///
3709 /// @param f the new value of the flag.
3710 void
3712 {priv_->canonicalization_is_done_ = f;}
3713 
3714 /// Getter for the "on-the-fly-canonicalization" flag.
3715 ///
3716 /// @return true iff @ref OnTheFlyCanonicalization
3717 /// "on-the-fly-canonicalization" is to be performed during
3718 /// comparison.
3719 bool
3721 {return priv_->do_on_the_fly_canonicalization_;}
3722 
3723 /// Setter for the "on-the-fly-canonicalization" flag.
3724 ///
3725 /// @param f If this is true then @ref OnTheFlyCanonicalization
3726 /// "on-the-fly-canonicalization" is to be performed during
3727 /// comparison.
3728 void
3730 {priv_->do_on_the_fly_canonicalization_ = f;}
3731 
3732 /// Getter of the "decl-only-class-equals-definition" flag.
3733 ///
3734 /// Usually, a declaration-only class named 'struct foo' compares
3735 /// equal to any class definition named "struct foo'. This is at
3736 /// least true for C++.
3737 ///
3738 /// In C, though, because there can be multiple definitions of 'struct
3739 /// foo' in the binary, a declaration-only "struct foo" might be
3740 /// considered to *NOT* resolve to any of the struct foo defined. In
3741 /// that case, the declaration-only "struct foo" is considered
3742 /// different from the definitions.
3743 ///
3744 /// This flag controls the behaviour of the comparison of an
3745 /// unresolved decl-only class against a definition of the same name.
3746 ///
3747 /// If set to false, the the declaration equals the definition. If
3748 /// set to false, then the decalration is considered different from
3749 /// the declaration.
3750 ///
3751 /// @return the value of the "decl-only-class-equals-definition" flag.
3752 bool
3754 {return priv_->decl_only_class_equals_definition_;}
3755 
3756 /// Setter of the "decl-only-class-equals-definition" flag.
3757 ///
3758 /// Usually, a declaration-only class named 'struct foo' compares
3759 /// equal to any class definition named "struct foo'. This is at
3760 /// least true for C++.
3761 ///
3762 /// In C, though, because there can be multiple definitions of 'struct
3763 /// foo' in the binary, a declaration-only "struct foo" might be
3764 /// considered to *NOT* resolve to any of the struct foo defined. In
3765 /// that case, the declaration-only "struct foo" is considered
3766 /// different from the definitions.
3767 ///
3768 /// This flag controls the behaviour of the comparison of an
3769 /// unresolved decl-only class against a definition of the same name.
3770 ///
3771 /// If set to false, the the declaration equals the definition. If
3772 /// set to false, then the decalration is considered different from
3773 /// the declaration.
3774 ///
3775 /// @param the new value of the "decl-only-class-equals-definition"
3776 /// flag.
3777 void
3779 {priv_->decl_only_class_equals_definition_ = f;}
3780 
3781 /// Test if a given type is a void type as defined in the current
3782 /// environment.
3783 ///
3784 /// @param t the type to consider.
3785 ///
3786 /// @return true iff @p t is a void type as defined in the current
3787 /// environment.
3788 bool
3789 environment::is_void_type(const type_base_sptr& t) const
3790 {
3791  if (!t)
3792  return false;
3793  return is_void_type(t.get());
3794 }
3795 
3796 /// Test if a given type is a void type as defined in the current
3797 /// environment.
3798 ///
3799 /// @param t the type to consider.
3800 ///
3801 /// @return true iff @p t is a void type as defined in the current
3802 /// environment.
3803 bool
3805 {
3806  if (!t)
3807  return false;
3808  return (t == get_void_type().get()
3809  || (is_type_decl(t) && is_type_decl(t)->get_name() == "void"));
3810 }
3811 
3812 /// Test if a given type is the same as the void pointer type of the
3813 /// environment.
3814 ///
3815 /// @param t the IR type to test.
3816 ///
3817 /// @return true iff @p t is the void pointer returned by
3818 /// environment::get_void_pointer_type().
3819 bool
3820 environment::is_void_pointer_type(const type_base_sptr& t) const
3821 {
3822  if (!t)
3823  return false;
3824 
3825  return t.get() == get_void_pointer_type().get();
3826 }
3827 
3828 /// Test if a given type is the same as the void pointer type of the
3829 /// environment.
3830 ///
3831 /// @param t the IR type to test.
3832 ///
3833 /// @return true iff @p t is the void pointer returned by
3834 /// environment::get_void_pointer_type().
3835 bool
3837 {
3838  if (!t)
3839  return false;
3840 
3841  return t == get_void_pointer_type().get();
3842 }
3843 
3844 /// Test if a type is a variadic parameter type as defined in the
3845 /// current environment.
3846 ///
3847 /// @param t the type to consider.
3848 ///
3849 /// @return true iff @p t is a variadic parameter type as defined in
3850 /// the current environment.
3851 bool
3853 {
3854  if (!t)
3855  return false;
3856  return t == get_variadic_parameter_type().get();
3857 }
3858 
3859 /// Test if a type is a variadic parameter type as defined in the
3860 /// current environment.
3861 ///
3862 /// @param t the type to consider.
3863 ///
3864 /// @return true iff @p t is a variadic parameter type as defined in
3865 /// the current environment.
3866 bool
3867 environment::is_variadic_parameter_type(const type_base_sptr& t) const
3868 {return is_variadic_parameter_type(t.get());}
3869 
3870 /// Do intern a string.
3871 ///
3872 /// If a value of this string already exists in the interned string
3873 /// pool of the current environment, then this function returns a new
3874 /// interned_string pointing to that already existing string.
3875 /// Otherwise, a new string is created, stored in the interned string
3876 /// pool and a new interned_string instance is created to point to
3877 /// that new intrerned string, and it's return.
3878 ///
3879 /// @param s the value of the string to intern.
3880 ///
3881 /// @return the interned string.
3883 environment::intern(const string& s) const
3884 {return const_cast<environment*>(this)->priv_->string_pool_.create_string(s);}
3885 
3886 /// Getter of the general configuration object.
3887 ///
3888 /// @return the configuration object.
3889 const config&
3891 {return priv_->config_;}
3892 
3893 /// Getter for a property that says if the user actually did set the
3894 /// analyze_exported_interfaces_only() property. If not, it means
3895 /// the default behaviour prevails.
3896 ///
3897 /// @return tru iff the user did set the
3898 /// analyze_exported_interfaces_only() property.
3899 bool
3901 {return priv_->analyze_exported_interfaces_only_.has_value();}
3902 
3903 /// Setter for the property that controls if we are to restrict the
3904 /// analysis to the types that are only reachable from the exported
3905 /// interfaces only, or if the set of types should be more broad than
3906 /// that. Typically, we'd restrict the analysis to types reachable
3907 /// from exported interfaces only (stricto sensu, that would really be
3908 /// only the types that are part of the ABI of well designed
3909 /// libraries) for performance reasons.
3910 ///
3911 /// @param f the value of the flag.
3912 void
3914 {priv_->analyze_exported_interfaces_only_ = f;}
3915 
3916 /// Getter for the property that controls if we are to restrict the
3917 /// analysis to the types that are only reachable from the exported
3918 /// interfaces only, or if the set of types should be more broad than
3919 /// that. Typically, we'd restrict the analysis to types reachable
3920 /// from exported interfaces only (stricto sensu, that would really be
3921 /// only the types that are part of the ABI of well designed
3922 /// libraries) for performance reasons.
3923 ///
3924 /// @param f the value of the flag.
3925 bool
3927 {return priv_->analyze_exported_interfaces_only_.value_or(false);}
3928 
3929 #ifdef WITH_DEBUG_SELF_COMPARISON
3930 /// Setter of the corpus of the input corpus of the self comparison
3931 /// that takes place when doing "abidw --debug-abidiff <binary>".
3932 ///
3933 /// The first invocation of this function sets the first corpus of the
3934 /// self comparison. The second invocation of this very same function
3935 /// sets the second corpus of the self comparison. That second corpus
3936 /// is supposed to come from the abixml serialization of the first
3937 /// corpus.
3938 ///
3939 /// @param c the corpus of the input binary or the corpus of the
3940 /// abixml serialization of the initial binary input.
3941 void
3942 environment::set_self_comparison_debug_input(const corpus_sptr& c)
3943 {
3944  self_comparison_debug_is_on(true);
3945  if (priv_->first_self_comparison_corpus_.expired())
3946  priv_->first_self_comparison_corpus_ = c;
3947  else if (priv_->second_self_comparison_corpus_.expired()
3948  && c.get() != corpus_sptr(priv_->first_self_comparison_corpus_).get())
3949  priv_->second_self_comparison_corpus_ = c;
3950 }
3951 
3952 /// Getter for the corpora of the input binary and the intermediate
3953 /// abixml of the self comparison that takes place when doing
3954 /// 'abidw --debug-abidiff <binary>'.
3955 ///
3956 /// @param first_corpus output parameter that is set to the corpus of
3957 /// the input corpus.
3958 ///
3959 /// @param second_corpus output parameter that is set to the corpus of
3960 /// the second corpus.
3961 void
3962 environment::get_self_comparison_debug_inputs(corpus_sptr& first_corpus,
3963  corpus_sptr& second_corpus)
3964 {
3965  first_corpus = priv_->first_self_comparison_corpus_.lock();
3966  second_corpus = priv_->second_self_comparison_corpus_.lock();
3967 }
3968 
3969 /// Turn on/off the self comparison debug mode.
3970 ///
3971 /// @param f true iff the self comparison debug mode is turned on.
3972 void
3973 environment::self_comparison_debug_is_on(bool f)
3974 {priv_->self_comparison_debug_on_ = f;}
3975 
3976 /// Test if we are in the process of the 'self-comparison
3977 /// debugging' as triggered by 'abidw --debug-abidiff' command.
3978 ///
3979 /// @return true if self comparison debug is on.
3980 bool
3981 environment::self_comparison_debug_is_on() const
3982 {return priv_->self_comparison_debug_on_;}
3983 #endif
3984 
3985 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
3986 /// Set the "type canonicalization debugging" mode, triggered by using
3987 /// the command: "abidw --debug-tc".
3988 ///
3989 /// @param flag if true then the type canonicalization debugging mode
3990 /// is enabled.
3991 void
3992 environment::debug_type_canonicalization_is_on(bool flag)
3993 {priv_->debug_type_canonicalization_ = flag;}
3994 
3995 /// Getter of the "type canonicalization debugging" mode, triggered by
3996 /// using the command: "abidw --debug-tc".
3997 ///
3998 /// @return true iff the type canonicalization debugging mode is
3999 /// enabled.
4000 bool
4001 environment::debug_type_canonicalization_is_on() const
4002 {return priv_->debug_type_canonicalization_;}
4003 
4004 /// Setter of the "DIE canonicalization debugging" mode, triggered by
4005 /// using the command: "abidw --debug-dc".
4006 ///
4007 /// @param flag true iff the DIE canonicalization debugging mode is
4008 /// enabled.
4009 void
4010 environment::debug_die_canonicalization_is_on(bool flag)
4011 {priv_->debug_die_canonicalization_ = flag;}
4012 
4013 /// Getter of the "DIE canonicalization debugging" mode, triggered by
4014 /// using the command: "abidw --debug-dc".
4015 ///
4016 /// @return true iff the DIE canonicalization debugging mode is
4017 /// enabled.
4018 bool
4019 environment::debug_die_canonicalization_is_on() const
4020 {return priv_->debug_die_canonicalization_;}
4021 #endif // WITH_DEBUG_TYPE_CANONICALIZATION
4022 
4023 /// Get the vector of canonical types which have a given "string
4024 /// representation".
4025 ///
4026 /// @param 'name', the textual representation of the type as returned
4027 /// by type_or_decl_base::get_pretty_representation(/*internal=*/true,
4028 /// /*qualified=*/true)
4029 ///
4030 /// This is useful to for debugging purposes as it's handy to use from
4031 /// inside a debugger like GDB.
4032 ///
4033 /// @return a pointer to the vector of canonical types having the
4034 /// representation @p name, or nullptr if no type with that
4035 /// representation exists.
4036 vector<type_base_sptr>*
4038 {
4039  auto ti = get_canonical_types_map().find(name);
4040  if (ti == get_canonical_types_map().end())
4041  return nullptr;
4042  return &ti->second;
4043 }
4044 
4045 /// Get a given canonical type which has a given "string
4046 /// representation".
4047 ///
4048 /// @param 'name', the textual representation of the type as returned
4049 /// by type_or_decl_base::get_pretty_representation(/*internal=*/true,
4050 /// /*qualified=*/true).
4051 ///
4052 /// @param index, the index of the type in the vector of types that
4053 /// all have the same textual representation @p 'name'. That vector
4054 /// is returned by the function environment::get_canonical_types().
4055 ///
4056 /// @return the canonical type which has the representation @p name,
4057 /// and which is at index @p index in the vector of canonical types
4058 /// having that same textual representation.
4059 type_base*
4060 environment::get_canonical_type(const char* name, unsigned index)
4061 {
4062  vector<type_base_sptr> *types = get_canonical_types(name);
4063  if (!types ||index >= types->size())
4064  return nullptr;
4065  return (*types)[index].get();
4066 }
4067 
4068 #ifdef WITH_DEBUG_SELF_COMPARISON
4069 /// Get the set of abixml type-id and the pointer value of the
4070 /// (canonical) type it's associated to.
4071 ///
4072 /// This is useful for debugging purposes, especially in the context
4073 /// of the use of the command:
4074 /// 'abidw --debug-abidiff <binary>'.
4075 ///
4076 /// @return the set of abixml type-id and the pointer value of the
4077 /// (canonical) type it's associated to.
4078 const unordered_map<string, uintptr_t>&
4079 environment::get_type_id_canonical_type_map() const
4080 {return priv_->get_type_id_canonical_type_map();}
4081 
4082 /// Get the set of abixml type-id and the pointer value of the
4083 /// (canonical) type it's associated to.
4084 ///
4085 /// This is useful for debugging purposes, especially in the context
4086 /// of the use of the command:
4087 /// 'abidw --debug-abidiff <binary>'.
4088 ///
4089 /// @return the set of abixml type-id and the pointer value of the
4090 /// (canonical) type it's associated to.
4091 unordered_map<string, uintptr_t>&
4092 environment::get_type_id_canonical_type_map()
4093 {return priv_->get_type_id_canonical_type_map();}
4094 
4095 /// Getter of the map that associates the values of type pointers to
4096 /// their type-id strings.
4097 ///
4098 /// Note that this map is populated at abixml reading time, (by
4099 /// build_type()) when a given XML element representing a type is
4100 /// read into a corresponding abigail::ir::type_base.
4101 ///
4102 /// This is used only for the purpose of debugging the
4103 /// self-comparison process. That is, when invoking "abidw
4104 /// --debug-abidiff".
4105 ///
4106 /// @return the map that associates the values of type pointers to
4107 /// their type-id strings.
4108 const unordered_map<uintptr_t, string>&
4109 environment::get_pointer_type_id_map() const
4110 {return priv_->get_pointer_type_id_map();}
4111 
4112 /// Getter of the map that associates the values of type pointers to
4113 /// their type-id strings.
4114 ///
4115 /// Note that this map is populated at abixml reading time, (by
4116 /// build_type()) when a given XML element representing a type is
4117 /// read into a corresponding abigail::ir::type_base.
4118 ///
4119 /// This is used only for the purpose of debugging the
4120 /// self-comparison process. That is, when invoking "abidw
4121 /// --debug-abidiff".
4122 ///
4123 /// @return the map that associates the values of type pointers to
4124 /// their type-id strings.
4125 unordered_map<uintptr_t, string>&
4126 environment::get_pointer_type_id_map()
4127 {return priv_->get_pointer_type_id_map();}
4128 
4129 /// Getter of the type-id that corresponds to the value of a pointer
4130 /// to abigail::ir::type_base that was created from the abixml reader.
4131 ///
4132 /// That value is retrieved from the map returned from
4133 /// environment::get_pointer_type_id_map().
4134 ///
4135 /// That map is populated at abixml reading time, (by build_type())
4136 /// when a given XML element representing a type is read into a
4137 /// corresponding abigail::ir::type_base.
4138 ///
4139 /// This is used only for the purpose of debugging the
4140 /// self-comparison process. That is, when invoking "abidw
4141 /// --debug-abidiff".
4142 ///
4143 /// @return the type-id strings that corresponds
4144 string
4145 environment::get_type_id_from_pointer(uintptr_t ptr) const
4146 {return priv_->get_type_id_from_pointer(ptr);}
4147 
4148 /// Getter of the type-id that corresponds to the value of an
4149 /// abigail::ir::type_base that was created from the abixml reader.
4150 ///
4151 /// That value is retrieved from the map returned from
4152 /// environment::get_pointer_type_id_map().
4153 ///
4154 /// That map is populated at abixml reading time, (by build_type())
4155 /// when a given XML element representing a type is read into a
4156 /// corresponding abigail::ir::type_base.
4157 ///
4158 /// This is used only for the purpose of debugging the
4159 /// self-comparison process. That is, when invoking "abidw
4160 /// --debug-abidiff".
4161 ///
4162 /// @return the type-id strings that corresponds
4163 string
4164 environment::get_type_id_from_type(const type_base *t) const
4165 {return priv_->get_type_id_from_type(t);}
4166 
4167 /// Getter of the canonical type of the artifact designated by a
4168 /// type-id.
4169 ///
4170 /// That type-id was generated by the abixml writer at the emitting
4171 /// time of the abixml file. The corresponding canonical type was
4172 /// stored in the map returned by
4173 /// environment::get_type_id_canonical_type_map().
4174 ///
4175 /// This is useful for debugging purposes, especially in the context
4176 /// of the use of the command:
4177 /// 'abidw --debug-abidiff <binary>'.
4178 ///
4179 /// @return the set of abixml type-id and the pointer value of the
4180 /// (canonical) type it's associated to.
4181 uintptr_t
4182 environment::get_canonical_type_from_type_id(const char* type_id) const
4183 {return priv_->get_canonical_type_from_type_id(type_id);}
4184 #endif
4185 
4186 // </environment stuff>
4187 
4188 // <type_or_decl_base stuff>
4189 
4190 /// The private data of @ref type_or_decl_base.
4191 struct type_or_decl_base::priv
4192 {
4193  // This holds the kind of dynamic type of particular instance.
4194  // Yes, this is part of the implementation of a "poor man" runtime
4195  // type identification. We are doing this because profiling shows
4196  // that using dynamic_cast in some places is really to slow and is
4197  // constituting a hotspot. This poor man's implementation made
4198  // things be much faster.
4199  enum type_or_decl_kind kind_;
4200  // This holds the runtime type instance pointer of particular
4201  // instance. In other words, this is the "this pointer" of the
4202  // dynamic type of a particular instance.
4203  void* rtti_;
4204  // This holds a pointer to either the type_base sub-object (if the
4205  // current instance is a type) or the decl_base sub-object (if the
4206  // current instance is a decl). This is used by the is_decl() and
4207  // is_type() functions, which also show up during profiling as
4208  // hotspots, due to their use of dynamic_cast.
4209  void* type_or_decl_ptr_;
4210  bool hashing_started_;
4211  const environment& env_;
4212  translation_unit* translation_unit_;
4213  // The location of an artifact as seen from its input by the
4214  // artifact reader. This might be different from the source
4215  // location advertised by the original emitter of the artifact
4216  // emitter.
4217  location artificial_location_;
4218  // Flags if the current ABI artifact is artificial (i.e, *NOT*
4219  // generated from the initial source code, but rather either
4220  // artificially by the compiler or by libabigail itself).
4221  bool is_artificial_;
4222 
4223  /// Constructor of the type_or_decl_base::priv private type.
4224  ///
4225  /// @param e the environment in which the ABI artifact was created.
4226  ///
4227  /// @param k the identifier of the runtime type of the current
4228  /// instance of ABI artifact.
4229  priv(const environment& e,
4230  enum type_or_decl_kind k = ABSTRACT_TYPE_OR_DECL)
4231  : kind_(k),
4232  rtti_(),
4233  type_or_decl_ptr_(),
4234  hashing_started_(),
4235  env_(e),
4236  translation_unit_(),
4237  is_artificial_()
4238  {}
4239 
4240  enum type_or_decl_kind
4241  kind() const
4242  {return kind_;}
4243 
4244  void
4245  kind (enum type_or_decl_kind k)
4246  {kind_ |= k;}
4247 }; // end struct type_or_decl_base::priv
4248 
4249 /// bitwise "OR" operator for the type_or_decl_base::type_or_decl_kind
4250 /// bitmap type.
4254 {
4255  return static_cast<type_or_decl_base::type_or_decl_kind>
4256  (static_cast<unsigned>(l) | static_cast<unsigned>(r));
4257 }
4258 
4259 /// bitwise "|=" operator for the type_or_decl_base::type_or_decl_kind
4260 /// bitmap type.
4264 {
4265  l = l | r;
4266  return l;
4267 }
4268 
4269 /// bitwise "AND" operator for the
4270 /// type_or_decl_base::type_or_decl_kind bitmap type.
4274 {
4275  return static_cast<type_or_decl_base::type_or_decl_kind>
4276  (static_cast<unsigned>(l) & static_cast<unsigned>(r));
4277 }
4278 
4279 /// bitwise "A&=" operator for the
4280 /// type_or_decl_base::type_or_decl_kind bitmap type.
4284 {
4285  l = l & r;
4286  return l;
4287 }
4288 
4289 /// Constructor of @ref type_or_decl_base.
4290 ///
4291 /// @param the environment the current ABI artifact is constructed
4292 /// from.
4293 ///
4294 /// @param k the runtime identifier bitmap of the type being built.
4295 type_or_decl_base::type_or_decl_base(const environment& e,
4296  enum type_or_decl_kind k)
4297  :priv_(new priv(e, k))
4298 {}
4299 
4300 /// The destructor of the @ref type_or_decl_base type.
4302 {}
4303 
4304 /// Getter of the flag that says if the artefact is artificial.
4305 ///
4306 /// Being artificial means it was not explicitely mentionned in the
4307 /// source code, but was rather artificially created by the compiler
4308 /// or libabigail.
4309 ///
4310 /// @return true iff the declaration is artificial.
4311 bool
4313 {return priv_->is_artificial_;}
4314 
4315 /// Setter of the flag that says if the artefact is artificial.
4316 ///
4317 /// Being artificial means the artefact was not explicitely
4318 /// mentionned in the source code, but was rather artificially created
4319 /// by the compiler or by libabigail.
4320 ///
4321 /// @param f the new value of the flag that says if the artefact is
4322 /// artificial.
4323 void
4325 {priv_->is_artificial_ = f;}
4326 
4327 /// Getter for the "kind" property of @ref type_or_decl_base type.
4328 ///
4329 /// This property holds the identifier bitmap of the runtime type of
4330 /// an ABI artifact.
4331 ///
4332 /// @return the runtime type identifier bitmap of the current ABI
4333 /// artifact.
4336 {return priv_->kind();}
4337 
4338 /// Setter for the "kind" property of @ref type_or_decl_base type.
4339 ///
4340 /// This property holds the identifier bitmap of the runtime type of
4341 /// an ABI artifact.
4342 ///
4343 /// @param the runtime type identifier bitmap of the current ABI
4344 /// artifact.
4345 void
4347 {priv_->kind(k);}
4348 
4349 /// Getter of the pointer to the runtime type sub-object of the
4350 /// current instance.
4351 ///
4352 /// @return the pointer to the runtime type sub-object of the current
4353 /// instance.
4354 const void*
4356 {return priv_->rtti_;}
4357 
4358 /// Getter of the pointer to the runtime type sub-object of the
4359 /// current instance.
4360 ///
4361 /// @return the pointer to the runtime type sub-object of the current
4362 /// instance.
4363 void*
4365 {return priv_->rtti_;}
4366 
4367 /// Setter of the pointer to the runtime type sub-object of the
4368 /// current instance.
4369 ///
4370 /// @param i the new pointer to the runtime type sub-object of the
4371 /// current instance.
4372 void
4374 {
4375  priv_->rtti_ = i;
4376  if (type_base* t = dynamic_cast<type_base*>(this))
4377  priv_->type_or_decl_ptr_ = t;
4378  else if (decl_base *d = dynamic_cast<decl_base*>(this))
4379  priv_->type_or_decl_ptr_ = d;
4380 }
4381 
4382 /// Getter of the pointer to either the type_base sub-object of the
4383 /// current instance if it's a type, or to the decl_base sub-object of
4384 /// the current instance if it's a decl.
4385 ///
4386 /// @return the pointer to either the type_base sub-object of the
4387 /// current instance if it's a type, or to the decl_base sub-object of
4388 /// the current instance if it's a decl.
4389 const void*
4391 {return const_cast<type_or_decl_base*>(this)->type_or_decl_base_pointer();}
4392 
4393 /// Getter of the pointer to either the type_base sub-object of the
4394 /// current instance if it's a type, or to the decl_base sub-object of
4395 /// the current instance if it's a decl.
4396 ///
4397 /// @return the pointer to either the type_base sub-object of the
4398 /// current instance if it's a type, or to the decl_base sub-object of
4399 /// the current instance if it's a decl.
4400 void*
4402 {return priv_->type_or_decl_ptr_;}
4403 
4404 /// Getter for the 'hashing_started' property.
4405 ///
4406 /// @return the 'hashing_started' property.
4407 bool
4409 {return priv_->hashing_started_;}
4410 
4411 /// Setter for the 'hashing_started' property.
4412 ///
4413 /// @param b the value to set the 'hashing_property' to.
4414 void
4416 {priv_->hashing_started_ = b;}
4417 
4418 /// Getter of the environment of the current ABI artifact.
4419 ///
4420 /// @return the environment of the artifact.
4421 const environment&
4423 {return priv_->env_;}
4424 
4425 /// Setter of the artificial location of the artificat.
4426 ///
4427 /// The artificial location is a location that was artificially
4428 /// generated by libabigail, not generated by the original emitter of
4429 /// the ABI meta-data. For instance, when reading an XML element from
4430 /// an abixml file, the artificial location is the source location of
4431 /// the XML element within the file, not the value of the
4432 /// 'location'property that might be carried by the element.
4433 ///
4434 /// Artificial locations might be useful to ensure that abixml emitted
4435 /// by the abixml writer are sorted the same way as the input abixml
4436 /// read by the reader.
4437 ///
4438 /// @param l the new artificial location.
4439 void
4441 {priv_->artificial_location_ = l;}
4442 
4443 /// Getter of the artificial location of the artifact.
4444 ///
4445 /// The artificial location is a location that was artificially
4446 /// generated by libabigail, not generated by the original emitter of
4447 /// the ABI meta-data. For instance, when reading an XML element from
4448 /// an abixml file, the artificial location is the source location of
4449 /// the XML element within the file, not the value of the
4450 /// 'location'property that might be carried by the element.
4451 ///
4452 /// Artificial locations might be useful to ensure that the abixml
4453 /// emitted by the abixml writer is sorted the same way as the input
4454 /// abixml read by the reader.
4455 ///
4456 /// @return the new artificial location.
4457 location&
4459 {return priv_->artificial_location_;}
4460 
4461 /// Test if the current ABI artifact carries an artificial location.
4462 ///
4463 /// @return true iff the current ABI artifact carries an artificial location.
4464 bool
4466 {
4467  return (priv_->artificial_location_
4468  && priv_->artificial_location_.get_is_artificial());
4469 }
4470 
4471 /// Get the @ref corpus this ABI artifact belongs to.
4472 ///
4473 /// @return the corpus this ABI artifact belongs to, or nil if it
4474 /// belongs to none for now.
4475 corpus*
4477 {
4479  if (!tu)
4480  return 0;
4481  return tu->get_corpus();
4482 }
4483 
4484 
4485 /// Get the @ref corpus this ABI artifact belongs to.
4486 ///
4487 /// @return the corpus this ABI artifact belongs to, or nil if it
4488 /// belongs to none for now.
4489 const corpus*
4491 {return const_cast<type_or_decl_base*>(this)->get_corpus();}
4492 
4493 /// Set the @ref translation_unit this ABI artifact belongs to.
4494 ///
4495 /// Note that adding an ABI artifact to a containining on should
4496 /// invoke this member function.
4497 void
4499 {priv_->translation_unit_ = tu;}
4500 
4501 
4502 /// Get the @ref translation_unit this ABI artifact belongs to.
4503 ///
4504 /// @return the translation unit this ABI artifact belongs to, or nil
4505 /// if belongs to none for now.
4508 {return priv_->translation_unit_;}
4509 
4510 /// Get the @ref translation_unit this ABI artifact belongs to.
4511 ///
4512 /// @return the translation unit this ABI artifact belongs to, or nil
4513 /// if belongs to none for now.
4514 const translation_unit*
4516 {return const_cast<type_or_decl_base*>(this)->get_translation_unit();}
4517 
4518 /// Traverse the the ABI artifact.
4519 ///
4520 /// @param v the visitor used to traverse the sub-tree nodes of the
4521 /// artifact.
4522 bool
4524 {return true;}
4525 
4526 /// Non-member equality operator for the @type_or_decl_base type.
4527 ///
4528 /// @param lr the left-hand operand of the equality.
4529 ///
4530 /// @param rr the right-hand operatnr of the equality.
4531 ///
4532 /// @return true iff @p lr equals @p rr.
4533 bool
4535 {
4536  const type_or_decl_base* l = &lr;
4537  const type_or_decl_base* r = &rr;
4538 
4539  const decl_base* dl = dynamic_cast<const decl_base*>(l),
4540  *dr = dynamic_cast<const decl_base*>(r);
4541 
4542  if (!!dl != !!dr)
4543  return false;
4544 
4545  if (dl && dr)
4546  return *dl == *dr;
4547 
4548  const type_base* tl = dynamic_cast<const type_base*>(l),
4549  *tr = dynamic_cast<const type_base*>(r);
4550 
4551  if (!!tl != !!tr)
4552  return false;
4553 
4554  if (tl && tr)
4555  return *tl == *tr;
4556 
4557  return false;
4558 }
4559 
4560 /// Non-member equality operator for the @type_or_decl_base type.
4561 ///
4562 /// @param l the left-hand operand of the equality.
4563 ///
4564 /// @param r the right-hand operatnr of the equality.
4565 ///
4566 /// @return true iff @p l equals @p r.
4567 bool
4569 {
4570  if (!! l != !!r)
4571  return false;
4572 
4573  if (!l)
4574  return true;
4575 
4576  return *r == *l;
4577 }
4578 
4579 /// Non-member inequality operator for the @type_or_decl_base type.
4580 ///
4581 /// @param l the left-hand operand of the equality.
4582 ///
4583 /// @param r the right-hand operator of the equality.
4584 ///
4585 /// @return true iff @p l is different from @p r.
4586 bool
4588 {return !operator==(l, r);}
4589 
4590 // </type_or_decl_base stuff>
4591 
4592 // <Decl definition>
4593 
4594 struct decl_base::priv
4595 {
4596  bool in_pub_sym_tab_;
4597  bool is_anonymous_;
4598  location location_;
4599  context_rel *context_;
4600  interned_string name_;
4601  interned_string qualified_parent_name_;
4602  // This temporary qualified name is the cache used for the qualified
4603  // name before the type associated to this decl (if applicable) is
4604  // canonicalized. Once the type is canonicalized, the cached use is
4605  // the data member qualified_parent_name_ above.
4606  interned_string temporary_qualified_name_;
4607  // This is the fully qualified name of the decl. It contains the
4608  // name of the decl and the qualified name of its scope. So if in
4609  // the parent scopes of the decl, there is one anonymous struct,
4610  // somewhere in the name, there is going to by an
4611  // __anonymous_struct__ string, even if the anonymous struct is not
4612  // the direct containing scope of this decl.
4613  interned_string qualified_name_;
4614  interned_string temporary_internal_qualified_name_;
4615  interned_string internal_qualified_name_;
4616  // Unline qualified_name_, scoped_name_ contains the name of the
4617  // decl and the name of its scope; not the qualified name of the
4618  // scope.
4619  interned_string scoped_name_;
4620  interned_string linkage_name_;
4621  visibility visibility_;
4622  decl_base_sptr declaration_;
4623  decl_base_wptr definition_of_declaration_;
4624  decl_base* naked_definition_of_declaration_;
4625  bool is_declaration_only_;
4626  typedef_decl_sptr naming_typedef_;
4627 
4628  priv()
4629  : in_pub_sym_tab_(false),
4630  is_anonymous_(true),
4631  context_(),
4632  visibility_(VISIBILITY_DEFAULT),
4633  naked_definition_of_declaration_(),
4634  is_declaration_only_(false)
4635  {}
4636 
4637  priv(interned_string name, interned_string linkage_name, visibility vis)
4638  : in_pub_sym_tab_(false),
4639  context_(),
4640  name_(name),
4641  qualified_name_(name),
4642  linkage_name_(linkage_name),
4643  visibility_(vis),
4644  naked_definition_of_declaration_(),
4645  is_declaration_only_(false)
4646  {
4647  is_anonymous_ = name_.empty();
4648  }
4649 
4650  ~priv()
4651  {
4652  delete context_;
4653  }
4654 };// end struct decl_base::priv
4655 
4656 /// Constructor for the @ref decl_base type.
4657 ///
4658 /// @param e the environment the current @ref decl_base is being
4659 /// created in.
4660 ///
4661 /// @param name the name of the declaration.
4662 ///
4663 /// @param locus the location where to find the declaration in the
4664 /// source code.
4665 ///
4666 /// @param linkage_name the linkage name of the declaration.
4667 ///
4668 /// @param vis the visibility of the declaration.
4669 decl_base::decl_base(const environment& e,
4670  const string& name,
4671  const location& locus,
4672  const string& linkage_name,
4673  visibility vis)
4674  : type_or_decl_base(e, ABSTRACT_DECL_BASE),
4675  priv_(new priv(e.intern(name), e.intern(linkage_name), vis))
4676 {
4677  set_location(locus);
4678 }
4679 
4680 /// Constructor.
4681 ///
4682 /// @param e the environment this instance of @ref decl_base is
4683 /// created in.
4684 ///
4685 /// @param name the name of the declaration being constructed.
4686 ///
4687 /// @param locus the source location of the declaration being constructed.
4688 ///
4689 /// @param linkage_name the linkage name of the declaration being
4690 /// constructed.
4691 ///
4692 /// @param vis the visibility of the declaration being constructed.
4693 decl_base::decl_base(const environment& e,
4694  const interned_string& name,
4695  const location& locus,
4696  const interned_string& linkage_name,
4697  visibility vis)
4698  : type_or_decl_base(e, ABSTRACT_DECL_BASE),
4699  priv_(new priv(name, linkage_name, vis))
4700 {
4701  set_location(locus);
4702 }
4703 
4704 /// Constructor for the @ref decl_base type.
4705 ///
4706 ///@param environment the environment this instance of @ref decl_base
4707 /// is being constructed in.
4708 ///
4709 /// @param l the location where to find the declaration in the source
4710 /// code.
4711 decl_base::decl_base(const environment& e, const location& l)
4712  : type_or_decl_base(e, ABSTRACT_DECL_BASE),
4713  priv_(new priv())
4714 {
4715  set_location(l);
4716 }
4717 
4718 /// Getter for the qualified name.
4719 ///
4720 /// Unlike decl_base::get_qualified_name() this doesn't try to update
4721 /// the qualified name.
4722 ///
4723 /// @return the qualified name.
4724 const interned_string&
4726 {return priv_->qualified_name_;}
4727 
4728 /// Clear the qualified name of this decl.
4729 ///
4730 /// This is useful to ensure that the cache for the qualified name of
4731 /// the decl is refreshed right after type canonicalization, for
4732 /// instance.
4733 void
4735 {priv_->qualified_name_.clear();}
4736 
4737 /// Setter for the qualified name.
4738 ///
4739 /// @param n the new qualified name.
4740 void
4742 {priv_->qualified_name_ = n;}
4743 
4744 /// Getter of the temporary qualified name of the current declaration.
4745 ///
4746 /// This temporary qualified name is used as a qualified name cache by
4747 /// the type for which this is the declaration (when applicable)
4748 /// before the type is canonicalized. Once the type is canonicalized,
4749 /// it's the result of decl_base::peek_qualified_name() that becomes
4750 /// the qualified name cached.
4751 ///
4752 /// @return the temporary qualified name.
4753 const interned_string&
4755 {return priv_->temporary_qualified_name_;}
4756 
4757 /// Setter for the temporary qualified name of the current
4758 /// declaration.
4759 ///
4760 ///@param n the new temporary qualified name.
4761 ///
4762 /// This temporary qualified name is used as a qualified name cache by
4763 /// the type for which this is the declaration (when applicable)
4764 /// before the type is canonicalized. Once the type is canonicalized,
4765 /// it's the result of decl_base::peek_qualified_name() that becomes
4766 /// the qualified name cached.
4767 void
4769 {priv_->temporary_qualified_name_ = n;}
4770 
4771 ///Getter for the context relationship.
4772 ///
4773 ///@return the context relationship for the current decl_base.
4774 const context_rel*
4776 {return priv_->context_;}
4777 
4778 ///Getter for the context relationship.
4779 ///
4780 ///@return the context relationship for the current decl_base.
4781 context_rel*
4783 {return priv_->context_;}
4784 
4785 void
4786 decl_base::set_context_rel(context_rel *c)
4787 {priv_->context_ = c;}
4788 
4789 /// Get the hash of a decl. If the hash hasn't been computed yet,
4790 /// compute it ans store its value; otherwise, just return the hash.
4791 ///
4792 /// @return the hash of the decl.
4793 size_t
4795 {
4796  size_t result = 0;
4797 
4798  if (const type_base* t = dynamic_cast<const type_base*>(this))
4799  {
4801  result = hash(t);
4802  }
4803  else
4804  // If we reach this point, it mean we are missing a virtual
4805  // overload for decl_base::get_hash. Add it!
4806  abort();
4807 
4808  return result;
4809 }
4810 
4811 /// Test if the decl is defined in a ELF symbol table as a public
4812 /// symbol.
4813 ///
4814 /// @return true iff the decl is defined in a ELF symbol table as a
4815 /// public symbol.
4816 bool
4818 {return priv_->in_pub_sym_tab_;}
4819 
4820 /// Set the flag saying if this decl is from a symbol that is in
4821 /// a public symbols table, defined as public (global or weak).
4822 ///
4823 /// @param f the new flag value.
4824 void
4826 {priv_->in_pub_sym_tab_ = f;}
4827 
4828 /// Get the location of a given declaration.
4829 ///
4830 /// The location is an abstraction for the tripplet {file path,
4831 /// line, column} that defines where the declaration appeared in the
4832 /// source code.
4833 ///
4834 /// To get the value of the tripplet {file path, line, column} from
4835 /// the @ref location, you need to use the
4836 /// location_manager::expand_location() method.
4837 ///
4838 /// The instance of @ref location_manager that you want is
4839 /// accessible from the instance of @ref translation_unit that the
4840 /// current instance of @ref decl_base belongs to, via a call to
4841 /// translation_unit::get_loc_mgr().
4842 ///
4843 /// @return the location of the current instance of @ref decl_base.
4844 const location&
4846 {return priv_->location_;}
4847 
4848 /// Set the location for a given declaration.
4849 ///
4850 /// The location is an abstraction for the tripplet {file path,
4851 /// line, column} that defines where the declaration appeared in the
4852 /// source code.
4853 ///
4854 /// To create a location from a tripplet {file path, line, column},
4855 /// you need to use the method @ref
4856 /// location_manager::create_new_location().
4857 ///
4858 /// Note that there can be two kinds of location. An artificial
4859 /// location and a non-artificial one. The non-artificial location is
4860 /// the one emitted by the original emitter of the ABI artifact, for
4861 /// instance, if the ABI artifact comes from debug info, then the
4862 /// source location that is present in the debug info represent a
4863 /// non-artificial location. When looking at an abixml file on the
4864 /// other hand, the value of the 'location' attribute of an XML
4865 /// element describing an artifact is the non-artificial location.
4866 /// The artificial location is the location (line number from the
4867 /// beginning of the file) of the XML element within the abixml file.
4868 ///
4869 /// So, if the location that is being set is artificial, note that the
4870 /// type_or_decl_base::has_artificial_location() method of this decl will
4871 /// subsequently return true and that artificial location will have to
4872 /// be retrieved using type_or_decl_base::get_artificial_location().
4873 /// If the location is non-artificial however,
4874 /// type_or_decl_base::has_artificial_location() will subsequently
4875 /// return false and the non-artificial location will have to be
4876 /// retrieved using decl_base::get_location().
4877 ///
4878 /// The instance of @ref location_manager that you want is
4879 /// accessible from the instance of @ref translation_unit that the
4880 /// current instance of @ref decl_base belongs to, via a call to
4881 /// translation_unit::get_loc_mgr().
4882 void
4884 {
4885  if (l.get_is_artificial())
4887  else
4888  priv_->location_ = l;
4889 }
4890 
4891 /// Setter for the name of the decl.
4892 ///
4893 /// @param n the new name to set.
4894 void
4895 decl_base::set_name(const string& n)
4896 {
4897  priv_->name_ = get_environment().intern(n);
4898  priv_->is_anonymous_ = n.empty();
4899 }
4900 
4901 /// Test if the current declaration is anonymous.
4902 ///
4903 /// Being anonymous means that the declaration was created without a
4904 /// name. This can usually happen for enum or struct types.
4905 ///
4906 /// @return true iff the type is anonymous.
4907 bool
4909 {return priv_->is_anonymous_;}
4910 
4911 /// Set the "is_anonymous" flag of the current declaration.
4912 ///
4913 /// Being anonymous means that the declaration was created without a
4914 /// name. This can usually happen for enum or struct types.
4915 ///
4916 /// @param f the new value of the flag.
4917 void
4919 {priv_->is_anonymous_ = f;}
4920 
4921 
4922 /// Get the "has_anonymous_parent" flag of the current declaration.
4923 ///
4924 /// Having an anoymous parent means having a anonymous parent scope
4925 /// (containing type or namespace) which is either direct or indirect.
4926 ///
4927 /// @return true iff the current decl has a direct or indirect scope
4928 /// which is anonymous.
4929 bool
4931 {
4932  scope_decl *scope = get_scope();
4933  if (!scope)
4934  return false;
4935  return scope->get_is_anonymous();
4936 }
4937 
4938 /// @return the logical "OR" of decl_base::get_is_anonymous() and
4939 /// decl_base::get_has_anonymous_parent().
4940 bool
4943 
4944 /// Getter for the naming typedef of the current decl.
4945 ///
4946 /// Consider the C idiom:
4947 ///
4948 /// typedef struct {int member;} foo_type;
4949 ///
4950 /// In that idiom, foo_type is the naming typedef of the anonymous
4951 /// struct that is declared.
4952 ///
4953 /// @return the naming typedef, if any. Otherwise, returns nil.
4956 {return priv_->naming_typedef_;}
4957 
4958 /// Set the naming typedef of the current instance of @ref decl_base.
4959 ///
4960 /// Consider the C idiom:
4961 ///
4962 /// typedef struct {int member;} foo_type;
4963 ///
4964 /// In that idiom, foo_type is the naming typedef of the anonymous
4965 /// struct that is declared.
4966 ///
4967 /// After completion of this function, the decl will not be considered
4968 /// anonymous anymore. It's name is going to be the name of the
4969 /// naming typedef.
4970 ///
4971 /// @param typedef_type the new naming typedef.
4972 void
4974 {
4975  // A naming typedef is usually for an anonymous type.
4977  // Whe the typedef-named decl is saved into abixml, it's
4978  // not anonymous anymore. Its name is the typedef name.
4979  // So when we read it back, we must still be able to
4980  // apply the naming typedef to the decl.
4981  || t->get_name() == get_name());
4982  // Only non canonicalized types can be edited this way.
4983  ABG_ASSERT(is_type(this)
4984  && is_type(this)->get_naked_canonical_type() == nullptr);
4985 
4986  priv_->naming_typedef_ = t;
4987  set_name(t->get_name());
4988  string qualified_name = build_qualified_name(get_scope(), t->get_name());
4989  set_qualified_name(get_environment().intern(qualified_name));
4990  set_is_anonymous(false);
4991  // Now that the qualified type of the decl has changed, let's update
4992  // the qualified names of the member types of this decls.
4993  update_qualified_name(this);
4994 }
4995 
4996 /// Getter for the mangled name.
4997 ///
4998 /// @return the new mangled name.
4999 const interned_string&
5001 {return priv_->linkage_name_;}
5002 
5003 /// Setter for the linkage name.
5004 ///
5005 /// @param m the new linkage name.
5006 void
5008 {
5009  const environment& env = get_environment();
5010  priv_->linkage_name_ = env.intern(m);
5011 }
5012 
5013 /// Getter for the visibility of the decl.
5014 ///
5015 /// @return the new visibility.
5018 {return priv_->visibility_;}
5019 
5020 /// Setter for the visibility of the decl.
5021 ///
5022 /// @param v the new visibility.
5023 void
5025 {priv_->visibility_ = v;}
5026 
5027 /// Return the type containing the current decl, if any.
5028 ///
5029 /// @return the type that contains the current decl, or NULL if there
5030 /// is none.
5031 scope_decl*
5033 {
5034  if (priv_->context_)
5035  return priv_->context_->get_scope();
5036  return 0;
5037 }
5038 
5039 /// Return a copy of the qualified name of the parent of the current
5040 /// decl.
5041 ///
5042 /// @return the newly-built qualified name of the of the current decl.
5043 const interned_string&
5045 {return priv_->qualified_parent_name_;}
5046 
5047 /// Getter for the name of the current decl.
5048 ///
5049 /// @return the name of the current decl.
5050 const interned_string&
5052 {return priv_->name_;}
5053 
5054 /// Compute the qualified name of the decl.
5055 ///
5056 /// @param qn the resulting qualified name.
5057 ///
5058 /// @param internal set to true if the call is intended for an
5059 /// internal use (for technical use inside the library itself), false
5060 /// otherwise. If you don't know what this is for, then set it to
5061 /// false.
5062 void
5064 {qn = get_qualified_name(internal);}
5065 
5066 /// Get the pretty representatin of the current declaration.
5067 ///
5068 ///
5069 /// @param internal set to true if the call is intended to get a
5070 /// representation of the decl (or type) for the purpose of canonical
5071 /// type comparison. This is mainly used in the function
5072 /// type_base::get_canonical_type_for().
5073 ///
5074 /// In other words if the argument for this parameter is true then the
5075 /// call is meant for internal use (for technical use inside the
5076 /// library itself), false otherwise. If you don't know what this is
5077 /// for, then set it to false.
5078 ///
5079 /// @param qualified_name if true, names emitted in the pretty
5080 /// representation are fully qualified.
5081 ///
5082 /// @return the default pretty representation for a decl. This is
5083 /// basically the fully qualified name of the decl optionally prefixed
5084 /// with a meaningful string to add context for the user.
5085 string
5087  bool qualified_name) const
5088 {
5089  if (internal
5090  && get_is_anonymous()
5091  && has_generic_anonymous_internal_type_name(this))
5092  {
5093  // We are looking at an anonymous enum, union or class and we
5094  // want an *internal* pretty representation for it. All
5095  // anonymous types of this kind in the same namespace must have
5096  // the same internal representation for type canonicalization to
5097  // work properly.
5098  //
5099  // OK, in practise, we are certainly looking at an enum because
5100  // classes and unions should have their own overloaded virtual
5101  // member function for this.
5102  string name = get_generic_anonymous_internal_type_name(this);
5103  if (qualified_name && !get_qualified_parent_name().empty())
5104  name = get_qualified_parent_name() + "::" + name;
5105  return name;
5106  }
5107 
5108  if (qualified_name)
5109  return get_qualified_name(internal);
5110  return get_name();
5111 }
5112 
5113 /// Return the qualified name of the decl.
5114 ///
5115 /// This is the fully qualified name of the decl. It's made of the
5116 /// concatenation of the name of the decl with the qualified name of
5117 /// its scope.
5118 ///
5119 /// Note that the value returned by this function is computed by @ref
5120 /// update_qualified_name when the decl is added to its scope.
5121 ///
5122 /// @param internal set to true if the call is intended for an
5123 /// internal use (for technical use inside the library itself), false
5124 /// otherwise. If you don't know what this is for, then set it to
5125 /// false.
5126 ///
5127 /// @return the resulting qualified name.
5128 const interned_string&
5129 decl_base::get_qualified_name(bool /*internal*/) const
5130 {return priv_->qualified_name_;}
5131 
5132 /// Return the scoped name of the decl.
5133 ///
5134 /// This is made of the concatenation of the name of the decl with the
5135 /// name of its scope. It doesn't contain the qualified name of its
5136 /// scope, unlike what is returned by decl_base::get_qualified_name.
5137 ///
5138 /// Note that the value returned by this function is computed by @ref
5139 /// update_qualified_name when the decl is added to its scope.
5140 ///
5141 /// @return the scoped name of the decl.
5142 const interned_string&
5144 {return priv_->scoped_name_;}
5145 
5146 /// If this @ref decl_base is a definition, get its earlier
5147 /// declaration.
5148 ///
5149 /// @return the earlier declaration of the class, if any.
5150 const decl_base_sptr
5152 {return priv_->declaration_;}
5153 
5154 /// set the earlier declaration of this @ref decl_base definition.
5155 ///
5156 /// @param d the earlier declaration to set. Note that it's set only
5157 /// if it's a pure declaration.
5158 void
5159 decl_base::set_earlier_declaration(const decl_base_sptr& d)
5160 {
5161  if (d && d->get_is_declaration_only())
5162  priv_->declaration_ = d;
5163 }
5164 
5165 
5166 /// If this @ref decl_base is declaration-only, get its definition, if
5167 /// any.
5168 ///
5169 /// @return the definition of this decl-only @ref decl_base.
5170 const decl_base_sptr
5172 {return priv_->definition_of_declaration_.lock();}
5173 
5174 /// If this @ref decl_base is declaration-only, get its definition,
5175 /// if any.
5176 ///
5177 /// Note that this function doesn't return a smart pointer, but rather
5178 /// the underlying pointer managed by the smart pointer. So it's as
5179 /// fast as possible. This getter is to be used in code paths that
5180 /// are proven to be performance hot spots; especially, when comparing
5181 /// sensitive types like enums, classes or unions. Those are compared
5182 /// extremely frequently and thus, their access to the definition of
5183 /// declaration must be fast.
5184 ///
5185 /// @return the definition of the declaration.
5186 const decl_base*
5188 {return priv_->naked_definition_of_declaration_;}
5189 
5190 /// Test if a @ref decl_base is a declaration-only decl.
5191 ///
5192 /// @return true iff the current @ref decl_base is declaration-only.
5193 bool
5195 {return priv_->is_declaration_only_;}
5196 
5197 /// Set a flag saying if the @ref enum_type_decl is a declaration-only
5198 /// @ref enum_type_decl.
5199 ///
5200 /// @param f true if the @ref enum_type_decl is a declaration-only
5201 /// @ref enum_type_decl.
5202 void
5204 {
5205  bool update_types_lookup_map = !f && priv_->is_declaration_only_;
5206 
5207  priv_->is_declaration_only_ = f;
5208 
5209  if (update_types_lookup_map)
5210  if (scope_decl* s = get_scope())
5211  {
5212  scope_decl::declarations::iterator i;
5213  if (s->find_iterator_for_member(this, i))
5215  else
5217  }
5218 }
5219 
5222 {
5223  return static_cast<change_kind>(static_cast<unsigned>(l)
5224  | static_cast<unsigned>(r));
5225 }
5226 
5229 {
5230  return static_cast<change_kind>(static_cast<unsigned>(l)
5231  & static_cast<unsigned>(r));
5232 }
5233 
5234 change_kind&
5236 {
5237  l = l | r;
5238  return l;
5239 }
5240 
5241 change_kind&
5243 {
5244  l = l & r;
5245  return l;
5246 }
5247 
5248 /// Compare the properties that belong to the "is-a-member-relation"
5249 /// of a decl.
5250 ///
5251 /// For instance, access specifiers are part of the
5252 /// "is-a-member-relation" of a decl.
5253 ///
5254 /// This comparison however doesn't take decl names into account. So
5255 /// typedefs for instance are decls that we want to compare with this
5256 /// function.
5257 ///
5258 /// This function is a sub-routine of the more general 'equals'
5259 /// overload for instances of decl_base.
5260 ///
5261 /// @param l the left-hand side operand of the comparison.
5262 ///
5263 /// @param r the right-hand side operand of the comparison.
5264 ///
5265 /// @return true iff @p l compare equals, as a member decl, to @p r.
5266 bool
5268  const decl_base& r,
5269  change_kind* k)
5270 {
5271  bool result = true;
5272  if (is_member_decl(l) && is_member_decl(r))
5273  {
5274  context_rel* r1 = const_cast<context_rel*>(l.get_context_rel());
5275  context_rel *r2 = const_cast<context_rel*>(r.get_context_rel());
5276 
5277  access_specifier la = no_access, ra = no_access;
5278  bool member_types_or_functions =
5279  ((is_type(l) && is_type(r))
5280  || (is_function_decl(l) && is_function_decl(r)));
5281 
5282  if (member_types_or_functions)
5283  {
5284  // Access specifiers on member types in DWARF is not
5285  // reliable; in the same DSO, the same struct can be either
5286  // a class or a struct, and the access specifiers of its
5287  // member types are not necessarily given, so they
5288  // effectively can be considered differently, again, in the
5289  // same DSO. So, here, let's avoid considering those!
5290  // during comparison.
5291  la = r1->get_access_specifier();
5292  ra = r2->get_access_specifier();
5293  r1->set_access_specifier(no_access);
5294  r2->set_access_specifier(no_access);
5295  }
5296 
5297  bool rels_are_different = *r1 != *r2;
5298 
5299  if (member_types_or_functions)
5300  {
5301  // restore the access specifiers.
5302  r1->set_access_specifier(la);
5303  r2->set_access_specifier(ra);
5304  }
5305 
5306  if (rels_are_different)
5307  {
5308  result = false;
5309  if (k)
5311  }
5312  }
5313  ABG_RETURN(result);
5314 }
5315 
5316 /// Get the name of a decl for the purpose of comparing two decl
5317 /// names.
5318 ///
5319 /// This is a sub-routine of the 'equal' overload for decl_base.
5320 ///
5321 /// This function takes into account the fact that all anonymous names
5322 /// shall have the same name for the purpose of comparison.
5323 ///
5324 /// For decls that are part of an anonymous scope, only the
5325 /// non-qualified name should be taken into account.
5326 static interned_string
5327 get_decl_name_for_comparison(const decl_base &d)
5328 {
5329  if (has_generic_anonymous_internal_type_name(&d)
5330  && d.get_is_anonymous())
5331  {
5332  // The decl is anonymous. It should have the same name as the
5333  // other anymous types of the same kind.
5334  string r;
5335  r += get_generic_anonymous_internal_type_name(&d);
5336  return d.get_environment().intern(r);
5337  }
5338 
5341  || is_typedef(&d))
5342  ? d.get_name()
5343  : d.get_qualified_name(/*internal=*/true);
5344  return n;
5345 }
5346 
5347 /// Compares two instances of @ref decl_base.
5348 ///
5349 /// If the two intances are different, set a bitfield to give some
5350 /// insight about the kind of differences there are.
5351 ///
5352 /// @param l the first artifact of the comparison.
5353 ///
5354 /// @param r the second artifact of the comparison.
5355 ///
5356 /// @param k a pointer to a bitfield that gives information about the
5357 /// kind of changes there are between @p l and @p r. This one is set
5358 /// iff it's non-null and if the function returns false.
5359 ///
5360 /// Please note that setting k to a non-null value does have a
5361 /// negative performance impact because even if @p l and @p r are not
5362 /// equal, the function keeps up the comparison in order to determine
5363 /// the different kinds of ways in which they are different.
5364 ///
5365 /// @return true if @p l equals @p r, false otherwise.
5366 bool
5367 equals(const decl_base& l, const decl_base& r, change_kind* k)
5368 {
5369  bool result = true;
5370  const interned_string &l_linkage_name = l.get_linkage_name();
5371  const interned_string &r_linkage_name = r.get_linkage_name();
5372  if (!l_linkage_name.empty() && !r_linkage_name.empty())
5373  {
5374  if (l_linkage_name != r_linkage_name)
5375  {
5376  // Linkage names are different. That usually means the two
5377  // decls are different, unless we are looking at two
5378  // function declarations which have two different symbols
5379  // that are aliases of each other.
5380  const function_decl *f1 = is_function_decl(&l),
5381  *f2 = is_function_decl(&r);
5382  if (f1 && f2 && function_decls_alias(*f1, *f2))
5383  ;// The two functions are aliases, so they are not
5384  // different.
5385  else
5386  {
5387  result = false;
5388  if (k)
5390  else
5392  }
5393  }
5394  }
5395 
5396  // This is the qualified name of the decls that we want to compare.
5397  // We want to use the "internal" version of the qualified name as
5398  // that one is stable even for anonymous decls.
5399  interned_string ln = get_decl_name_for_comparison(l);
5400  interned_string rn = get_decl_name_for_comparison(r);
5401 
5402  /// If both of the current decls have an anonymous scope then let's
5403  /// compare their name component by component by properly handling
5404  /// anonymous scopes. That's the slow path.
5405  ///
5406  /// Otherwise, let's just compare their name, the obvious way.
5407  /// That's the fast path because in that case the names are
5408  /// interned_string and comparing them is much faster.
5409  bool decls_are_same = (ln == rn);
5410  if (!decls_are_same
5411  && l.get_is_anonymous()
5412  && !l.get_has_anonymous_parent()
5413  && r.get_is_anonymous()
5414  && !r.get_has_anonymous_parent())
5415  // Both decls are anonymous and their scope are *NOT* anonymous.
5416  // So we consider the decls to have equivalent names (both
5417  // anonymous, remember). We are still in the fast path here.
5418  decls_are_same = true;
5419 
5420  if (!decls_are_same
5422  && r.get_has_anonymous_parent())
5423  // This is the slow path as we are comparing the decl qualified
5424  // names component by component, properly handling anonymous
5425  // scopes.
5426  decls_are_same = tools_utils::decl_names_equal(ln, rn);
5427 
5428  if (!decls_are_same)
5429  {
5430  result = false;
5431  if (k)
5433  else
5435  }
5436 
5437  result &= maybe_compare_as_member_decls(l, r, k);
5438 
5439  ABG_RETURN(result);
5440 }
5441 
5442 /// Return true iff the two decls have the same name.
5443 ///
5444 /// This function doesn't test if the scopes of the the two decls are
5445 /// equal.
5446 ///
5447 /// Note that this virtual function is to be implemented by classes
5448 /// that extend the \p decl_base class.
5449 bool
5451 {return equals(*this, other, 0);}
5452 
5453 /// Inequality operator.
5454 ///
5455 /// @param other to other instance of @ref decl_base to compare the
5456 /// current instance to.
5457 ///
5458 /// @return true iff the current instance of @ref decl_base is
5459 /// different from @p other.
5460 bool
5462 {return !operator==(other);}
5463 
5464 /// Destructor of the @ref decl_base type.
5466 {delete priv_;}
5467 
5468 /// This implements the ir_traversable_base::traverse pure virtual
5469 /// function.
5470 ///
5471 /// @param v the visitor used on the member nodes of the translation
5472 /// unit during the traversal.
5473 ///
5474 /// @return true if the entire IR node tree got traversed, false
5475 /// otherwise.
5476 bool
5478 {
5479  // Do nothing in the base class.
5480  return true;
5481 }
5482 
5483 /// Setter of the scope of the current decl.
5484 ///
5485 /// Note that the decl won't hold a reference on the scope. It's
5486 /// rather the scope that holds a reference on its members.
5487 void
5489 {
5490  if (!priv_->context_)
5491  priv_->context_ = new context_rel(scope);
5492  else
5493  priv_->context_->set_scope(scope);
5494 }
5495 
5496 // </decl_base definition>
5497 
5498 /// Streaming operator for the decl_base::visibility.
5499 ///
5500 /// @param o the output stream to serialize the visibility to.
5501 ///
5502 /// @param v the visibility to serialize.
5503 ///
5504 /// @return the output stream.
5505 std::ostream&
5506 operator<<(std::ostream& o, decl_base::visibility v)
5507 {
5508  string r;
5509  switch (v)
5510  {
5511  case decl_base::VISIBILITY_NONE:
5512  r = "none";
5513  break;
5514  case decl_base::VISIBILITY_DEFAULT:
5515  r = "default";
5516  break;
5517  case decl_base::VISIBILITY_PROTECTED:
5518  r = "protected";
5519  break;
5520  case decl_base::VISIBILITY_HIDDEN:
5521  r = "hidden";
5522  break;
5523  case decl_base::VISIBILITY_INTERNAL:
5524  r = "internal";
5525  break;
5526  }
5527  return o;
5528 }
5529 
5530 /// Streaming operator for decl_base::binding.
5531 ///
5532 /// @param o the output stream to serialize the visibility to.
5533 ///
5534 /// @param b the binding to serialize.
5535 ///
5536 /// @return the output stream.
5537 std::ostream&
5538 operator<<(std::ostream& o, decl_base::binding b)
5539 {
5540  string r;
5541  switch (b)
5542  {
5543  case decl_base::BINDING_NONE:
5544  r = "none";
5545  break;
5546  case decl_base::BINDING_LOCAL:
5547  r = "local";
5548  break;
5549  case decl_base::BINDING_GLOBAL:
5550  r = "global";
5551  break;
5552  case decl_base::BINDING_WEAK:
5553  r = "weak";
5554  break;
5555  }
5556  o << r;
5557  return o;
5558 }
5559 
5560 /// Turn equality of shared_ptr of decl_base into a deep equality;
5561 /// that is, make it compare the pointed to objects, not just the
5562 /// pointers.
5563 ///
5564 /// @param l the shared_ptr of decl_base on left-hand-side of the
5565 /// equality.
5566 ///
5567 /// @param r the shared_ptr of decl_base on right-hand-side of the
5568 /// equality.
5569 ///
5570 /// @return true if the decl_base pointed to by the shared_ptrs are
5571 /// equal, false otherwise.
5572 bool
5573 operator==(const decl_base_sptr& l, const decl_base_sptr& r)
5574 {
5575  if (l.get() == r.get())
5576  return true;
5577  if (!!l != !!r)
5578  return false;
5579 
5580  return *l == *r;
5581 }
5582 
5583 /// Inequality operator of shared_ptr of @ref decl_base.
5584 ///
5585 /// This is a deep equality operator, that is, it compares the
5586 /// pointed-to objects, rather than just the pointers.
5587 ///
5588 /// @param l the left-hand-side operand.
5589 ///
5590 /// @param r the right-hand-side operand.
5591 ///
5592 /// @return true iff @p l is different from @p r.
5593 bool
5594 operator!=(const decl_base_sptr& l, const decl_base_sptr& r)
5595 {return !operator==(l, r);}
5596 
5597 /// Turn equality of shared_ptr of type_base into a deep equality;
5598 /// that is, make it compare the pointed to objects too.
5599 ///
5600 /// @param l the shared_ptr of type_base on left-hand-side of the
5601 /// equality.
5602 ///
5603 /// @param r the shared_ptr of type_base on right-hand-side of the
5604 /// equality.
5605 ///
5606 /// @return true if the type_base pointed to by the shared_ptrs are
5607 /// equal, false otherwise.
5608 bool
5609 operator==(const type_base_sptr& l, const type_base_sptr& r)
5610 {
5611  if (l.get() == r.get())
5612  return true;
5613  if (!!l != !!r)
5614  return false;
5615 
5616  return *l == *r;
5617 }
5618 
5619 /// Turn inequality of shared_ptr of type_base into a deep equality;
5620 /// that is, make it compare the pointed to objects..
5621 ///
5622 /// @param l the shared_ptr of type_base on left-hand-side of the
5623 /// equality.
5624 ///
5625 /// @param r the shared_ptr of type_base on right-hand-side of the
5626 /// equality.
5627 ///
5628 /// @return true iff the type_base pointed to by the shared_ptrs are
5629 /// different.
5630 bool
5631 operator!=(const type_base_sptr& l, const type_base_sptr& r)
5632 {return !operator==(l, r);}
5633 
5634 /// Tests if a declaration has got a scope.
5635 ///
5636 /// @param d the declaration to consider.
5637 ///
5638 /// @return true if the declaration has got a scope, false otherwise.
5639 bool
5641 {return (d.get_scope());}
5642 
5643 /// Tests if a declaration has got a scope.
5644 ///
5645 /// @param d the declaration to consider.
5646 ///
5647 /// @return true if the declaration has got a scope, false otherwise.
5648 bool
5649 has_scope(const decl_base_sptr d)
5650 {return has_scope(*d.get());}
5651 
5652 /// Tests if a declaration is a class member.
5653 ///
5654 /// @param d the declaration to consider.
5655 ///
5656 /// @return true if @p d is a class member, false otherwise.
5657 bool
5658 is_member_decl(const decl_base_sptr d)
5659 {return is_at_class_scope(d) || is_method_decl(d);}
5660 
5661 /// Tests if a declaration is a class member.
5662 ///
5663 /// @param d the declaration to consider.
5664 ///
5665 /// @return true if @p d is a class member, false otherwise.
5666 bool
5668 {return is_at_class_scope(d) || is_method_decl(d);}
5669 
5670 /// Tests if a declaration is a class member.
5671 ///
5672 /// @param d the declaration to consider.
5673 ///
5674 /// @return true if @p d is a class member, false otherwise.
5675 bool
5677 {return is_at_class_scope(d) || is_method_decl(d);}
5678 
5679 /// Test if a declaration is a @ref scope_decl.
5680 ///
5681 /// @param d the declaration to take in account.
5682 ///
5683 /// @return the a pointer to the @ref scope_decl sub-object of @p d,
5684 /// if d is a @ref scope_decl.
5685 scope_decl*
5687 {return dynamic_cast<scope_decl*>(d);}
5688 
5689 /// Test if a declaration is a @ref scope_decl.
5690 ///
5691 /// @param d the declaration to take in account.
5692 ///
5693 /// @return the a pointer to the @ref scope_decl sub-object of @p d,
5694 /// if d is a @ref scope_decl.
5696 is_scope_decl(const decl_base_sptr& d)
5697 {return dynamic_pointer_cast<scope_decl>(d);}
5698 
5699 /// Tests if a type is a class member.
5700 ///
5701 /// @param t the type to consider.
5702 ///
5703 /// @return true if @p t is a class member type, false otherwise.
5704 bool
5705 is_member_type(const type_base_sptr& t)
5706 {
5707  decl_base_sptr d = get_type_declaration(t);
5708  return is_member_decl(d);
5709 }
5710 
5711 /// Test if a type is user-defined.
5712 ///
5713 /// A type is considered user-defined if it's a
5714 /// struct/class/union/enum that is *NOT* artificial.
5715 ///
5716 /// @param t the type to consider.
5717 ///
5718 /// @return true iff the type @p t is user-defined.
5719 bool
5721 {
5722  if (t == 0)
5723  return false;
5724 
5726  decl_base *d = is_decl(t);
5727 
5728  if ((is_class_or_union_type(t) || is_enum_type(t))
5729  && d && !d->get_is_artificial())
5730  return true;
5731 
5732  return false;
5733 }
5734 
5735 /// Test if a type is user-defined.
5736 ///
5737 /// A type is considered user-defined if it's a
5738 /// struct/class/union/enum.
5739 ///
5740 ///
5741 /// @param t the type to consider.
5742 ///
5743 /// @return true iff the type @p t is user-defined.
5744 bool
5745 is_user_defined_type(const type_base_sptr& t)
5746 {return is_user_defined_type(t.get());}
5747 
5748 /// Gets the access specifier for a class member.
5749 ///
5750 /// @param d the declaration of the class member to consider. Note
5751 /// that this must be a class member otherwise the function aborts the
5752 /// current process.
5753 ///
5754 /// @return the access specifier for the class member @p d.
5757 {
5759 
5760  const context_rel* c = d.get_context_rel();
5761  ABG_ASSERT(c);
5762 
5763  return c->get_access_specifier();
5764 }
5765 
5766 /// Gets the access specifier for a class member.
5767 ///
5768 /// @param d the declaration of the class member to consider. Note
5769 /// that this must be a class member otherwise the function aborts the
5770 /// current process.
5771 ///
5772 /// @return the access specifier for the class member @p d.
5774 get_member_access_specifier(const decl_base_sptr& d)
5775 {return get_member_access_specifier(*d);}
5776 
5777 /// Sets the access specifier for a class member.
5778 ///
5779 /// @param d the class member to set the access specifier for. Note
5780 /// that this must be a class member otherwise the function aborts the
5781 /// current process.
5782 ///
5783 /// @param a the new access specifier to set the class member to.
5784 void
5786  access_specifier a)
5787 {
5789 
5790  context_rel* c = d.get_context_rel();
5791  ABG_ASSERT(c);
5792 
5793  c->set_access_specifier(a);
5794 }
5795 
5796 /// Sets the access specifier for a class member.
5797 ///
5798 /// @param d the class member to set the access specifier for. Note
5799 /// that this must be a class member otherwise the function aborts the
5800 /// current process.
5801 ///
5802 /// @param a the new access specifier to set the class member to.
5803 void
5804 set_member_access_specifier(const decl_base_sptr& d,
5805  access_specifier a)
5807 
5808 /// Gets a flag saying if a class member is static or not.
5809 ///
5810 /// @param d the declaration for the class member to consider. Note
5811 /// that this must be a class member otherwise the function aborts the
5812 /// current process.
5813 ///
5814 /// @return true if the class member @p d is static, false otherwise.
5815 bool
5817 {
5819 
5820  const context_rel* c = d.get_context_rel();
5821  ABG_ASSERT(c);
5822 
5823  return c->get_is_static();
5824 }
5825 
5826 /// Gets a flag saying if a class member is static or not.
5827 ///
5828 /// @param d the declaration for the class member to consider. Note
5829 /// that this must be a class member otherwise the function aborts the
5830 /// current process.
5831 ///
5832 /// @return true if the class member @p d is static, false otherwise.
5833 bool
5835 {return get_member_is_static(*d);}
5836 
5837 /// Gets a flag saying if a class member is static or not.
5838 ///
5839 /// @param d the declaration for the class member to consider. Note
5840 /// that this must be a class member otherwise the function aborts the
5841 /// current process.
5842 ///
5843 /// @return true if the class member @p d is static, false otherwise.
5844 bool
5845 get_member_is_static(const decl_base_sptr& d)
5846 {return get_member_is_static(*d);}
5847 
5848 /// Test if a var_decl is a data member.
5849 ///
5850 /// @param v the var_decl to consider.
5851 ///
5852 /// @return true if @p v is data member, false otherwise.
5853 bool
5855 {return is_at_class_scope(v);}
5856 
5857 /// Test if a var_decl is a data member.
5858 ///
5859 /// @param v the var_decl to consider.
5860 ///
5861 /// @return true if @p v is data member, false otherwise.
5862 bool
5864 {return is_data_member(*v);}
5865 
5866 /// Test if a var_decl is a data member.
5867 ///
5868 /// @param v the var_decl to consider.
5869 ///
5870 /// @return true if @p v is data member, false otherwise.
5871 bool
5873 {return is_at_class_scope(d);}
5874 
5875 /// Test if a decl is a data member.
5876 ///
5877 /// @param d the decl to consider.
5878 ///
5879 /// @return a pointer to the data member iff @p d is a data member, or
5880 /// a null pointer.
5882 is_data_member(const decl_base_sptr& d)
5883 {
5884  if (var_decl_sptr v = is_var_decl(d))
5885  {
5886  if (is_data_member(v))
5887  return v;
5888  }
5889  return var_decl_sptr();
5890 }
5891 
5892 /// Test if a decl is a data member.
5893 ///
5894 /// @param d the decl to consider.
5895 ///
5896 /// @return a pointer to the data member iff @p d is a data member, or
5897 /// a null pointer.
5900 {
5901  if (var_decl_sptr v = is_var_decl(d))
5902  {
5903  if (is_data_member(v))
5904  return v;
5905  }
5906  return var_decl_sptr();
5907 }
5908 
5909 /// Test if a decl is a data member.
5910 ///
5911 /// @param d the decl to consider.
5912 ///
5913 /// @return a pointer to the data member iff @p d is a data member, or
5914 /// a null pointer.
5915 var_decl*
5917 {
5918  if (var_decl *v = is_var_decl(d))
5919  if (is_data_member(v))
5920  return v;
5921  return 0;
5922 }
5923 
5924 /// Test if a decl is a data member.
5925 ///
5926 /// @param d the decl to consider.
5927 ///
5928 /// @return a pointer to the data member iff @p d is a data member, or
5929 /// a null pointer.
5930 var_decl*
5932 {
5933  if (var_decl *v = is_var_decl(d))
5934  if (is_data_member(v))
5935  return v;
5936  return 0;
5937 }
5938 
5939 /// Get the first non-anonymous data member of a given anonymous data
5940 /// member.
5941 ///
5942 /// E.g:
5943 ///
5944 /// struct S
5945 /// {
5946 /// union // <-- for this anonymous data member, the function
5947 /// // returns a.
5948 /// {
5949 /// int a;
5950 /// charb;
5951 /// };
5952 /// };
5953 ///
5954 /// @return anon_dm the anonymous data member to consider.
5955 ///
5956 /// @return the first non-anonymous data member of @p anon_dm. If no
5957 /// data member was found then this function returns @p anon_dm.
5958 const var_decl_sptr
5960 {
5961  if (!anon_dm || !is_anonymous_data_member(anon_dm))
5962  return anon_dm;
5963 
5964  class_or_union_sptr klass = anonymous_data_member_to_class_or_union(anon_dm);
5965  var_decl_sptr first = *klass->get_non_static_data_members().begin();
5966 
5967  if (is_anonymous_data_member(first))
5969 
5970  return first;
5971 }
5972 
5973 /// In the context of a given class or union, this function returns
5974 /// the data member that is located after a given data member.
5975 ///
5976 /// @param klass the class or union to consider.
5977 ///
5978 /// @param the data member to consider.
5979 ///
5980 /// @return the data member that is located right after @p
5981 /// data_member.
5982 const var_decl_sptr
5984  const var_decl_sptr &data_member)
5985 {
5986  if (!klass ||!data_member)
5987  return var_decl_sptr();
5988 
5989  for (class_or_union::data_members::const_iterator it =
5990  klass->get_non_static_data_members().begin();
5991  it != klass->get_non_static_data_members().end();
5992  ++it)
5993  if (**it == *data_member)
5994  {
5995  ++it;
5996  if (it != klass->get_non_static_data_members().end())
5998  break;
5999  }
6000 
6001  return var_decl_sptr();
6002 }
6003 
6004 /// In the context of a given class or union, this function returns
6005 /// the data member that is located after a given data member.
6006 ///
6007 /// @param klass the class or union to consider.
6008 ///
6009 /// @param the data member to consider.
6010 ///
6011 /// @return the data member that is located right after @p
6012 /// data_member.
6013 const var_decl_sptr
6014 get_next_data_member(const class_or_union_sptr& klass,
6015  const var_decl_sptr &data_member)
6016 {return get_next_data_member(klass.get(), data_member);}
6017 
6018 /// Get the last data member of a class type.
6019 ///
6020 /// @param klass the class type to consider.
6023 {return klass.get_non_static_data_members().back();}
6024 
6025 /// Get the last data member of a class type.
6026 ///
6027 /// @param klass the class type to consider.
6030 {return get_last_data_member(*klass);}
6031 
6032 /// Get the last data member of a class type.
6033 ///
6034 /// @param klass the class type to consider.
6036 get_last_data_member(const class_or_union_sptr &klass)
6037 {return get_last_data_member(klass.get());}
6038 
6039 /// Collect all the non-anonymous data members of a class or union type.
6040 ///
6041 /// If the class contains any anonymous data member, this function
6042 /// looks through it to collect the non-anonymous data members that it
6043 /// contains. The function also also looks through the base classes
6044 /// of the current type.
6045 ///
6046 /// @param cou the class or union type to consider.
6047 ///
6048 /// @param dms output parameter. This is populated by the function
6049 /// with a map containing the non-anonymous data members that were
6050 /// collected. The key of the map is the name of the data member.
6051 /// This is set iff the function returns true.
6052 ///
6053 /// @return true iff at least one non-anonymous data member was
6054 /// collected.
6055 bool
6058 {
6059  if (!cou)
6060  return false;
6061 
6062  bool result = false;
6063  class_decl* klass = is_class_type(cou);
6064  if (klass)
6065  // First look into base classes for data members.
6066  for (class_decl::base_spec_sptr base : klass->get_base_specifiers())
6067  result |= collect_non_anonymous_data_members(base->get_base_class().get(), dms);
6068 
6069  // Then look into our data members
6070  for (var_decl_sptr member : cou->get_non_static_data_members())
6071  {
6072  if (is_anonymous_data_member(member))
6073  {
6074  class_or_union_sptr cl = anonymous_data_member_to_class_or_union(member);
6075  ABG_ASSERT(cl);
6076  result |= collect_non_anonymous_data_members(cl.get(), dms);
6077  }
6078  else
6079  {
6080  dms[member->get_name()] = member;
6081  result = true;
6082  }
6083  }
6084  return true;
6085 }
6086 
6087 /// Collect all the non-anonymous data members of a class or union type.
6088 ///
6089 /// If the class contains any anonymous data member, this function
6090 /// looks through it to collect the non-anonymous data members that it
6091 /// contains. The function also also looks through the base classes
6092 /// of the current type.
6093 ///
6094 /// @param cou the class or union type to consider.
6095 ///
6096 /// @param dms output parameter. This is populated by the function
6097 /// with a map containing the non-anonymous data members that were
6098 /// collected. The key of the map is the name of the data member.
6099 /// This is set iff the function returns true.
6100 ///
6101 /// @return true iff at least one non-anonymous data member was
6102 /// collected.
6103 bool
6105 {return collect_non_anonymous_data_members(cou.get(), dms);}
6106 
6107 /// Test if a decl is an anonymous data member.
6108 ///
6109 /// @param d the decl to consider.
6110 ///
6111 /// @return true iff @p d is an anonymous data member.
6112 bool
6114 {return is_anonymous_data_member(&d);}
6115 
6116 /// Test if a decl is an anonymous data member.
6117 ///
6118 /// @param d the decl to consider.
6119 ///
6120 /// @return the var_decl representing the data member iff @p d is an
6121 /// anonymous data member.
6122 const var_decl*
6124 {
6125  if (const var_decl* v = is_data_member(d))
6126  {
6127  if (is_anonymous_data_member(v))
6128  return v;
6129  }
6130  return 0;
6131 }
6132 
6133 /// Test if a decl is an anonymous data member.
6134 ///
6135 /// @param d the decl to consider.
6136 ///
6137 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
6138 /// it's an anonymous data member. Otherwise returns a nil pointer.
6139 const var_decl*
6141 {
6142  if (const var_decl* v = is_data_member(d))
6143  {
6144  if (is_anonymous_data_member(v))
6145  return v;
6146  }
6147  return 0;
6148 }
6149 
6150 /// Test if a decl is an anonymous data member.
6151 ///
6152 /// @param d the decl to consider.
6153 ///
6154 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
6155 /// it's an anonymous data member. Otherwise returns a nil pointer.
6158 {
6159  if (var_decl_sptr v = is_data_member(d))
6160  {
6161  if (is_anonymous_data_member(v))
6162  return v;
6163  }
6164  return var_decl_sptr();
6165 }
6166 
6167 /// Test if a decl is an anonymous data member.
6168 ///
6169 /// @param d the decl to consider.
6170 ///
6171 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
6172 /// it's an anonymous data member. Otherwise returns a nil pointer.
6174 is_anonymous_data_member(const decl_base_sptr& d)
6175 {
6176  if (var_decl_sptr v = is_data_member(d))
6177  return is_anonymous_data_member(v);
6178  return var_decl_sptr();
6179 }
6180 
6181 /// Test if a @ref var_decl is an anonymous data member.
6182 ///
6183 /// @param d the @ref var_decl to consider.
6184 ///
6185 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
6186 /// it's an anonymous data member. Otherwise returns a nil pointer.
6189 {
6190  if (is_anonymous_data_member(d.get()))
6191  return d;
6192  return var_decl_sptr();
6193 }
6194 
6195 /// Test if a @ref var_decl is an anonymous data member.
6196 ///
6197 /// @param d the @ref var_decl to consider.
6198 ///
6199 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
6200 /// it's an anonymous data member. Otherwise returns a nil pointer.
6201 const var_decl*
6203 {
6204  if (d && is_anonymous_data_member(*d))
6205  return d;
6206  return 0;
6207 }
6208 
6209 /// Test if a @ref var_decl is an anonymous data member.
6210 ///
6211 /// @param d the @ref var_decl to consider.
6212 ///
6213 /// @return true iff @p d is an anonymous data member.
6214 bool
6216 {
6217  return (is_data_member(d)
6218  && d.get_is_anonymous()
6219  && d.get_name().empty()
6221 }
6222 
6223 /// Test if a @ref var_decl is a data member belonging to an anonymous
6224 /// type.
6225 ///
6226 /// @param d the @ref var_decl to consider.
6227 ///
6228 /// @return true iff @p d is a data member belonging to an anonymous
6229 /// type.
6230 bool
6232 {
6233  if (is_data_member(d))
6234  {
6235  scope_decl* scope = d.get_scope();
6236  if (scope && scope->get_is_anonymous())
6237  return true;
6238  }
6239  return false;
6240 }
6241 
6242 /// Test if a @ref var_decl is a data member belonging to an anonymous
6243 /// type.
6244 ///
6245 /// @param d the @ref var_decl to consider.
6246 ///
6247 /// @return true iff @p d is a data member belonging to an anonymous
6248 /// type.
6249 bool
6252 
6253 /// Test if a @ref var_decl is a data member belonging to an anonymous
6254 /// type.
6255 ///
6256 /// @param d the @ref var_decl to consider.
6257 ///
6258 /// @return true iff @p d is a data member belonging to an anonymous
6259 /// type.
6260 bool
6263 
6264 /// Get the @ref class_or_union type of a given anonymous data member.
6265 ///
6266 /// @param d the anonymous data member to consider.
6267 ///
6268 /// @return the @ref class_or_union type of the anonymous data member
6269 /// @p d.
6272 {
6273  if ((d = is_anonymous_data_member(d)))
6274  return is_class_or_union_type(d->get_type().get());
6275  return 0;
6276 }
6277 
6278 /// Get the @ref class_or_union type of a given anonymous data member.
6279 ///
6280 /// @param d the anonymous data member to consider.
6281 ///
6282 /// @return the @ref class_or_union type of the anonymous data member
6283 /// @p d.
6284 class_or_union_sptr
6286 {
6287  if (is_anonymous_data_member(d))
6288  return is_class_or_union_type(d.get_type());
6289  return class_or_union_sptr();
6290 }
6291 
6292 /// Test if a data member has annonymous type or not.
6293 ///
6294 /// @param d the data member to consider.
6295 ///
6296 /// @return the anonymous class or union type iff @p turns out to have
6297 /// an anonymous type. Otherwise, returns nil.
6298 const class_or_union_sptr
6300 {
6301  if (is_data_member(d))
6302  if (const class_or_union_sptr cou = is_class_or_union_type(d.get_type()))
6303  if (cou->get_is_anonymous())
6304  return cou;
6305 
6306  return class_or_union_sptr();
6307 }
6308 
6309 /// Test if a data member has annonymous type or not.
6310 ///
6311 /// @param d the data member to consider.
6312 ///
6313 /// @return the anonymous class or union type iff @p turns out to have
6314 /// an anonymous type. Otherwise, returns nil.
6315 const class_or_union_sptr
6317 {
6318  if (d)
6319  return data_member_has_anonymous_type(*d);
6320  return class_or_union_sptr();
6321 }
6322 
6323 /// Test if a data member has annonymous type or not.
6324 ///
6325 /// @param d the data member to consider.
6326 ///
6327 /// @return the anonymous class or union type iff @p turns out to have
6328 /// an anonymous type. Otherwise, returns nil.
6329 const class_or_union_sptr
6331 {return data_member_has_anonymous_type(d.get());}
6332 
6333 /// Get the @ref class_or_union type of a given anonymous data member.
6334 ///
6335 /// @param d the anonymous data member to consider.
6336 ///
6337 /// @return the @ref class_or_union type of the anonymous data member
6338 /// @p d.
6339 class_or_union_sptr
6341 {
6343  return is_class_or_union_type(v->get_type());
6344  return class_or_union_sptr();
6345 }
6346 
6347 /// Test if a given anonymous data member exists in a class or union.
6348 ///
6349 /// @param anon_dm the anonymous data member to consider.
6350 ///
6351 /// @param clazz the class to consider.
6352 ///
6353 /// @return true iff @p anon_dm exists in the @clazz.
6354 bool
6356  const class_or_union& clazz)
6357 {
6358  if (!anon_dm.get_is_anonymous()
6359  || !is_class_or_union_type(anon_dm.get_type()))
6360  return false;
6361 
6362  class_or_union_sptr cl = is_class_or_union_type(anon_dm.get_type());
6363  ABG_ASSERT(cl);
6364 
6365  // Look for the presence of each data member of anon_dm in clazz.
6366  //
6367  // If one data member of anon_dm is not present in clazz, then the
6368  // data member anon_dm is considered to not exist in clazz.
6369  for (auto anon_dm_m : cl->get_non_static_data_members())
6370  {
6371  // If the data member anon_dm_m is not an anonymous data member,
6372  // it's easy to look for it.
6373  if (!is_anonymous_data_member(anon_dm_m))
6374  {
6375  if (!clazz.find_data_member(anon_dm_m->get_name()))
6376  return false;
6377  }
6378  // If anon_dm_m is itself an anonymous data member then recurse
6379  else
6380  {
6381  if (!anonymous_data_member_exists_in_class(*anon_dm_m, clazz))
6382  return false;
6383  }
6384  }
6385 
6386  return true;
6387 }
6388 
6389 /// Test if a given decl is anonymous or has a naming typedef.
6390 ///
6391 /// @param d the decl to consider.
6392 ///
6393 /// @return true iff @p d is anonymous or has a naming typedef.
6394 bool
6396 {
6397  if (d.get_is_anonymous() || d.get_naming_typedef())
6398  return true;
6399  return false;
6400 }
6401 
6402 /// Set the offset of a data member into its containing class.
6403 ///
6404 /// @param m the data member to consider.
6405 ///
6406 /// @param o the offset, in bits.
6407 void
6409 {
6411 
6412  dm_context_rel* ctxt_rel =
6413  dynamic_cast<dm_context_rel*>(m->get_context_rel());
6414  ABG_ASSERT(ctxt_rel);
6415 
6416  ctxt_rel->set_offset_in_bits(o);
6417 }
6418 
6419 /// Get the offset of a data member.
6420 ///
6421 /// @param m the data member to consider.
6422 ///
6423 /// @return the offset (in bits) of @p m in its containing class.
6424 uint64_t
6426 {
6428  const dm_context_rel* ctxt_rel =
6429  dynamic_cast<const dm_context_rel*>(m.get_context_rel());
6430  ABG_ASSERT(ctxt_rel);
6431  return ctxt_rel->get_offset_in_bits();
6432 }
6433 
6434 /// Get the offset of a data member.
6435 ///
6436 /// @param m the data member to consider.
6437 ///
6438 /// @return the offset (in bits) of @p m in its containing class.
6439 uint64_t
6441 {return get_data_member_offset(*m);}
6442 
6443 /// Get the offset of a data member.
6444 ///
6445 /// @param m the data member to consider.
6446 ///
6447 /// @return the offset (in bits) of @p m in its containing class.
6448 uint64_t
6449 get_data_member_offset(const decl_base_sptr d)
6450 {return get_data_member_offset(dynamic_pointer_cast<var_decl>(d));}
6451 
6452 /// Get the offset of the non-static data member that comes after a
6453 /// given one.
6454 ///
6455 /// If there is no data member after after the one given to this
6456 /// function (maybe because the given one is the last data member of
6457 /// the class type) then the function return false.
6458 ///
6459 /// @param klass the class to consider.
6460 ///
6461 /// @param dm the data member before the one we want to retrieve.
6462 ///
6463 /// @param offset out parameter. This parameter is set by the
6464 /// function to the offset of the data member that comes right after
6465 /// the data member @p dm, iff the function returns true.
6466 ///
6467 /// @return true iff the data member coming right after @p dm was
6468 /// found.
6469 bool
6471  const var_decl_sptr& dm,
6472  uint64_t& offset)
6473 {
6474  var_decl_sptr next_dm = get_next_data_member(klass, dm);
6475  if (!next_dm)
6476  return false;
6477  offset = get_data_member_offset(next_dm);
6478  return true;
6479 }
6480 
6481 /// Get the offset of the non-static data member that comes after a
6482 /// given one.
6483 ///
6484 /// If there is no data member after after the one given to this
6485 /// function (maybe because the given one is the last data member of
6486 /// the class type) then the function return false.
6487 ///
6488 /// @param klass the class to consider.
6489 ///
6490 /// @param dm the data member before the one we want to retrieve.
6491 ///
6492 /// @param offset out parameter. This parameter is set by the
6493 /// function to the offset of the data member that comes right after
6494 /// the data member @p dm, iff the function returns true.
6495 ///
6496 /// @return true iff the data member coming right after @p dm was
6497 /// found.
6498 bool
6499 get_next_data_member_offset(const class_or_union_sptr& klass,
6500  const var_decl_sptr& dm,
6501  uint64_t& offset)
6502 {return get_next_data_member_offset(klass.get(), dm, offset);}
6503 
6504 /// Get the absolute offset of a data member.
6505 ///
6506 /// If the data member is part of an anonymous data member then this
6507 /// returns the absolute offset -- relative to the beginning of the
6508 /// containing class of the anonymous data member.
6509 ///
6510 /// @param m the data member to consider.
6511 ///
6512 /// @return the aboslute offset of the data member @p m.
6513 uint64_t
6515 {
6517  const dm_context_rel* ctxt_rel =
6518  dynamic_cast<const dm_context_rel*>(m.get_context_rel());
6519  ABG_ASSERT(ctxt_rel);
6520 
6521  const var_decl *containing_anonymous_data_member =
6522  ctxt_rel->get_anonymous_data_member();
6523 
6524  uint64_t containing_anonymous_data_member_offset = 0;
6525  if (containing_anonymous_data_member)
6526  containing_anonymous_data_member_offset =
6527  get_absolute_data_member_offset(*containing_anonymous_data_member);
6528 
6529  return (ctxt_rel->get_offset_in_bits()
6530  +
6531  containing_anonymous_data_member_offset);
6532 }
6533 
6534 /// Get the absolute offset of a data member.
6535 ///
6536 /// If the data member is part of an anonymous data member then this
6537 /// returns the absolute offset -- relative to the beginning of the
6538 /// containing class of the anonymous data member.
6539 ///
6540 /// @param m the data member to consider.
6541 ///
6542 /// @return the aboslute offset of the data member @p m.
6543 uint64_t
6545 {
6546  if (!m)
6547  return 0;
6549 }
6550 
6551 /// Get the size of a given variable.
6552 ///
6553 /// @param v the variable to consider.
6554 ///
6555 /// @return the size of variable @p v.
6556 uint64_t
6558 {
6559  type_base_sptr t = v->get_type();
6560  ABG_ASSERT(t);
6561 
6562  return t->get_size_in_bits();
6563 }
6564 
6565 /// Set a flag saying if a data member is laid out.
6566 ///
6567 /// @param m the data member to consider.
6568 ///
6569 /// @param l true if @p m is to be considered as laid out.
6570 void
6572 {
6574  dm_context_rel* ctxt_rel =
6575  dynamic_cast<dm_context_rel*>(m->get_context_rel());
6576  ctxt_rel->set_is_laid_out(l);
6577 }
6578 
6579 /// Test whether a data member is laid out.
6580 ///
6581 /// @param m the data member to consider.
6582 ///
6583 /// @return true if @p m is laid out, false otherwise.
6584 bool
6586 {
6588  const dm_context_rel* ctxt_rel =
6589  dynamic_cast<const dm_context_rel*>(m.get_context_rel());
6590 
6591  return ctxt_rel->get_is_laid_out();
6592 }
6593 
6594 /// Test whether a data member is laid out.
6595 ///
6596 /// @param m the data member to consider.
6597 ///
6598 /// @return true if @p m is laid out, false otherwise.
6599 bool
6601 {return get_data_member_is_laid_out(*m);}
6602 
6603 /// Test whether a function_decl is a member function.
6604 ///
6605 /// @param f the function_decl to test.
6606 ///
6607 /// @return true if @p f is a member function, false otherwise.
6608 bool
6610 {return is_member_decl(f);}
6611 
6612 /// Test whether a function_decl is a member function.
6613 ///
6614 /// @param f the function_decl to test.
6615 ///
6616 /// @return true if @p f is a member function, false otherwise.
6617 bool
6619 {return is_member_decl(*f);}
6620 
6621 /// Test whether a function_decl is a member function.
6622 ///
6623 /// @param f the function_decl to test.
6624 ///
6625 /// @return true if @p f is a member function, false otherwise.
6626 bool
6628 {return is_member_decl(*f);}
6629 
6630 /// Test whether a member function is a constructor.
6631 ///
6632 /// @param f the member function to test.
6633 ///
6634 /// @return true if @p f is a constructor, false otherwise.
6635 bool
6637 {
6639 
6640  const method_decl* m = is_method_decl(&f);
6641  ABG_ASSERT(m);
6642 
6643  const mem_fn_context_rel* ctxt =
6644  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6645 
6646  return ctxt->is_constructor();
6647 }
6648 
6649 /// Test whether a member function is a constructor.
6650 ///
6651 /// @param f the member function to test.
6652 ///
6653 /// @return true if @p f is a constructor, false otherwise.
6654 bool
6656 {return get_member_function_is_ctor(*f);}
6657 
6658 
6659 /// Setter for the is_ctor property of the member function.
6660 ///
6661 /// @param f the member function to set.
6662 ///
6663 /// @param f the new boolean value of the is_ctor property. Is true
6664 /// if @p f is a constructor, false otherwise.
6665 void
6667 {
6669 
6670  method_decl* m = is_method_decl(&f);
6671  ABG_ASSERT(m);
6672 
6673  mem_fn_context_rel* ctxt =
6674  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6675 
6676  ctxt->is_constructor(c);
6677 }
6678 
6679 /// Setter for the is_ctor property of the member function.
6680 ///
6681 /// @param f the member function to set.
6682 ///
6683 /// @param f the new boolean value of the is_ctor property. Is true
6684 /// if @p f is a constructor, false otherwise.
6685 void
6688 
6689 /// Test whether a member function is a destructor.
6690 ///
6691 /// @param f the function to test.
6692 ///
6693 /// @return true if @p f is a destructor, false otherwise.
6694 bool
6696 {
6698 
6699  const method_decl* m = is_method_decl(&f);
6700  ABG_ASSERT(m);
6701 
6702  const mem_fn_context_rel* ctxt =
6703  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6704 
6705  return ctxt->is_destructor();
6706 }
6707 
6708 /// Test whether a member function is a destructor.
6709 ///
6710 /// @param f the function to test.
6711 ///
6712 /// @return true if @p f is a destructor, false otherwise.
6713 bool
6715 {return get_member_function_is_dtor(*f);}
6716 
6717 /// Set the destructor-ness property of a member function.
6718 ///
6719 /// @param f the function to set.
6720 ///
6721 /// @param d true if @p f is a destructor, false otherwise.
6722 void
6724 {
6726 
6727  method_decl* m = is_method_decl(&f);
6728  ABG_ASSERT(m);
6729 
6730  mem_fn_context_rel* ctxt =
6731  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6732 
6733  ctxt->is_destructor(d);
6734 }
6735 
6736 /// Set the destructor-ness property of a member function.
6737 ///
6738 /// @param f the function to set.
6739 ///
6740 /// @param d true if @p f is a destructor, false otherwise.
6741 void
6744 
6745 /// Test whether a member function is const.
6746 ///
6747 /// @param f the function to test.
6748 ///
6749 /// @return true if @p f is const, false otherwise.
6750 bool
6752 {
6754 
6755  const method_decl* m = is_method_decl(&f);
6756  ABG_ASSERT(m);
6757 
6758  const mem_fn_context_rel* ctxt =
6759  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6760 
6761  return ctxt->is_const();
6762 }
6763 
6764 /// Test whether a member function is const.
6765 ///
6766 /// @param f the function to test.
6767 ///
6768 /// @return true if @p f is const, false otherwise.
6769 bool
6771 {return get_member_function_is_const(*f);}
6772 
6773 /// set the const-ness property of a member function.
6774 ///
6775 /// @param f the function to set.
6776 ///
6777 /// @param is_const the new value of the const-ness property of @p f
6778 void
6780 {
6782 
6783  method_decl* m = is_method_decl(&f);
6784  ABG_ASSERT(m);
6785 
6786  mem_fn_context_rel* ctxt =
6787  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6788 
6789  ctxt->is_const(is_const);
6790 }
6791 
6792 /// set the const-ness property of a member function.
6793 ///
6794 /// @param f the function to set.
6795 ///
6796 /// @param is_const the new value of the const-ness property of @p f
6797 void
6799 {set_member_function_is_const(*f, is_const);}
6800 
6801 /// Test if a virtual member function has a vtable offset set.
6802 ///
6803 /// @param f the virtual member function to consider.
6804 ///
6805 /// @return true iff the virtual member function has its vtable offset
6806 /// set, i.e, if the vtable offset of @p is different from -1.
6807 bool
6809 {return get_member_function_vtable_offset(f) != -1;}
6810 
6811 /// Get the vtable offset of a member function.
6812 ///
6813 /// @param f the member function to consider.
6814 ///
6815 /// @return the vtable offset of @p f. Note that a vtable offset of
6816 /// value -1 means that the member function does *NOT* yet have a
6817 /// vtable offset associated to it.
6818 ssize_t
6820 {
6822 
6823  const method_decl* m =
6824  dynamic_cast<const method_decl*>(&f);
6825  ABG_ASSERT(m);
6826 
6827  const mem_fn_context_rel* ctxt =
6828  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6829 
6830  return ctxt->vtable_offset();
6831 }
6832 
6833 /// Get the vtable offset of a member function.
6834 ///
6835 /// @param f the member function to consider.
6836 ///
6837 /// @return the vtable offset of @p f. Note that a vtable offset of
6838 /// value -1 means that the member function does *NOT* yet have a
6839 /// vtable offset associated to it.
6840 ssize_t
6843 
6844 /// Set the vtable offset of a member function.
6845 ///
6846 /// @param f the member function to consider.
6847 ///
6848 /// @param s the new vtable offset. Please note that a vtable offset
6849 /// of value -1 means that the virtual member function does not (yet)
6850 /// have any vtable offset associated to it.
6851 void
6853 {
6855 
6856  method_decl* m = is_method_decl(&f);
6857  ABG_ASSERT(m);
6858 
6859  mem_fn_context_rel* ctxt =
6860  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6861 
6862  ctxt->vtable_offset(s);
6863 }
6864 
6865 /// Get the vtable offset of a member function.
6866 ///
6867 /// @param f the member function to consider.
6868 ///
6869 /// @param s the new vtable offset. Please note that a vtable offset
6870 /// of value -1 means that the virtual member function does not (yet)
6871 /// have any vtable offset associated to it.
6872 void
6874 {return set_member_function_vtable_offset(*f, s);}
6875 
6876 /// Test if a given member function is virtual.
6877 ///
6878 /// @param mem_fn the member function to consider.
6879 ///
6880 /// @return true iff a @p mem_fn is virtual.
6881 bool
6883 {
6885 
6886  const method_decl* m =
6887  dynamic_cast<const method_decl*>(&f);
6888  ABG_ASSERT(m);
6889 
6890  const mem_fn_context_rel* ctxt =
6891  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6892 
6893  return ctxt->is_virtual();
6894 }
6895 
6896 /// Test if a given member function is virtual.
6897 ///
6898 /// @param mem_fn the member function to consider.
6899 ///
6900 /// @return true iff a @p mem_fn is virtual.
6901 bool
6903 {return mem_fn ? get_member_function_is_virtual(*mem_fn) : false;}
6904 
6905 /// Test if a given member function is virtual.
6906 ///
6907 /// @param mem_fn the member function to consider.
6908 ///
6909 /// @return true iff a @p mem_fn is virtual.
6910 bool
6912 {return mem_fn ? get_member_function_is_virtual(*mem_fn) : false;}
6913 
6914 /// Set the virtual-ness of a member function.
6915 ///
6916 /// @param f the member function to consider.
6917 ///
6918 /// @param is_virtual set to true if the function is virtual.
6919 void
6921 {
6923 
6924  method_decl* m = is_method_decl(&f);
6925  ABG_ASSERT(m);
6926 
6927  mem_fn_context_rel* ctxt =
6928  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6929 
6930  ctxt->is_virtual(is_virtual);
6931 }
6932 
6933 /// Set the virtual-ness of a member function.
6934 ///
6935 /// @param f the member function to consider.
6936 ///
6937 /// @param is_virtual set to true if the function is virtual.
6938 void
6940 {
6941  if (fn)
6942  {
6943  set_member_function_is_virtual(*fn, is_virtual);
6945  (dynamic_pointer_cast<method_decl>(fn));
6946  }
6947 }
6948 
6949 /// Recursively returns the the underlying type of a typedef. The
6950 /// return type should not be a typedef of anything anymore.
6951 ///
6952 ///
6953 /// Also recursively strip typedefs from the sub-types of the type
6954 /// given in arguments.
6955 ///
6956 /// Note that this function builds types in which typedefs are
6957 /// stripped off. Usually, types are held by their scope, so their
6958 /// life time is bound to the life time of their scope. But as this
6959 /// function cannot really insert the built type into it's scope, it
6960 /// must ensure that the newly built type stays live long enough.
6961 ///
6962 /// So, if the newly built type has a canonical type, this function
6963 /// returns the canonical type. Otherwise, this function ensure that
6964 /// the newly built type has a life time that is the same as the life
6965 /// time of the entire libabigail library.
6966 ///
6967 /// @param type the type to strip the typedefs from.
6968 ///
6969 /// @return the resulting type stripped from its typedefs, or just
6970 /// return @p type if it has no typedef in any of its sub-types.
6971 type_base_sptr
6972 strip_typedef(const type_base_sptr type)
6973 {
6974  if (!type)
6975  return type;
6976 
6977  // If type is a class type then do not try to strip typedefs from it.
6978  // And if it has no canonical type (which can mean that it's a
6979  // declaration-only class), then, make sure its live for ever and
6980  // return it.
6981  if (class_decl_sptr cl = is_class_type(type))
6982  {
6983  if (!cl->get_canonical_type())
6984  keep_type_alive(type);
6985  return type;
6986  }
6987 
6988  const environment& env = type->get_environment();
6989  type_base_sptr t = type;
6990 
6991  if (const typedef_decl_sptr ty = is_typedef(t))
6992  t = strip_typedef(type_or_void(ty->get_underlying_type(), env));
6993  else if (const reference_type_def_sptr ty = is_reference_type(t))
6994  {
6995  type_base_sptr p = strip_typedef(type_or_void(ty->get_pointed_to_type(),
6996  env));
6997  ABG_ASSERT(p);
6998  t.reset(new reference_type_def(p,
6999  ty->is_lvalue(),
7000  ty->get_size_in_bits(),
7001  ty->get_alignment_in_bits(),
7002  ty->get_location()));
7003  }
7004  else if (const pointer_type_def_sptr ty = is_pointer_type(t))
7005  {
7006  type_base_sptr p = strip_typedef(type_or_void(ty->get_pointed_to_type(),
7007  env));
7008  ABG_ASSERT(p);
7009  t.reset(new pointer_type_def(p,
7010  ty->get_size_in_bits(),
7011  ty->get_alignment_in_bits(),
7012  ty->get_location()));
7013  }
7014  else if (const qualified_type_def_sptr ty = is_qualified_type(t))
7015  {
7016  type_base_sptr p = strip_typedef(type_or_void(ty->get_underlying_type(),
7017  env));
7018  ABG_ASSERT(p);
7019  t.reset(new qualified_type_def(p,
7020  ty->get_cv_quals(),
7021  ty->get_location()));
7022  }
7023  else if (const array_type_def_sptr ty = is_array_type(t))
7024  {
7025  type_base_sptr p = strip_typedef(ty->get_element_type());
7026  ABG_ASSERT(p);
7027  t.reset(new array_type_def(p, ty->get_subranges(), ty->get_location()));
7028  }
7029  else if (const method_type_sptr ty = is_method_type(t))
7030  {
7032  for (function_decl::parameters::const_iterator i =
7033  ty->get_parameters().begin();
7034  i != ty->get_parameters().end();
7035  ++i)
7036  {
7038  type_base_sptr typ = strip_typedef(p->get_type());
7039  ABG_ASSERT(typ);
7041  (new function_decl::parameter(typ,
7042  p->get_index(),
7043  p->get_name(),
7044  p->get_location(),
7045  p->get_variadic_marker(),
7046  p->get_is_artificial()));
7047  parm.push_back(stripped);
7048  }
7049  type_base_sptr p = strip_typedef(ty->get_return_type());
7050  ABG_ASSERT(!!p == !!ty->get_return_type());
7051  t.reset(new method_type(p, ty->get_class_type(),
7052  parm, ty->get_is_const(),
7053  ty->get_size_in_bits(),
7054  ty->get_alignment_in_bits()));
7055  }
7056  else if (const function_type_sptr ty = is_function_type(t))
7057  {
7059  for (function_decl::parameters::const_iterator i =
7060  ty->get_parameters().begin();
7061  i != ty->get_parameters().end();
7062  ++i)
7063  {
7065  type_base_sptr typ = strip_typedef(p->get_type());
7066  ABG_ASSERT(typ);
7068  (new function_decl::parameter(typ,
7069  p->get_index(),
7070  p->get_name(),
7071  p->get_location(),
7072  p->get_variadic_marker(),
7073  p->get_is_artificial()));
7074  parm.push_back(stripped);
7075  }
7076  type_base_sptr p = strip_typedef(ty->get_return_type());
7077  ABG_ASSERT(!!p == !!ty->get_return_type());
7078  t.reset(new function_type(p, parm,
7079  ty->get_size_in_bits(),
7080  ty->get_alignment_in_bits()));
7081  }
7082 
7083  if (!t->get_translation_unit())
7084  t->set_translation_unit(type->get_translation_unit());
7085 
7086  if (!(type->get_canonical_type() && canonicalize(t)))
7087  keep_type_alive(t);
7088 
7089  return t->get_canonical_type() ? t->get_canonical_type() : t;
7090 }
7091 
7092 /// Strip qualification from a qualified type, when it makes sense.
7093 ///
7094 /// DWARF constructs "const reference". This is redundant because a
7095 /// reference is always const. It also constructs the useless "const
7096 /// void" type. The issue is these redundant types then leak into the
7097 /// IR and make for bad diagnostics.
7098 ///
7099 /// This function thus strips the const qualifier from the type in
7100 /// that case. It might contain code to strip other cases like this
7101 /// in the future.
7102 ///
7103 /// @param t the type to strip const qualification from.
7104 ///
7105 /// @return the stripped type or just return @p t.
7106 decl_base_sptr
7107 strip_useless_const_qualification(const qualified_type_def_sptr t)
7108 {
7109  if (!t)
7110  return t;
7111 
7112  decl_base_sptr result = t;
7113  type_base_sptr u = t->get_underlying_type();
7114  const environment& env = t->get_environment();
7115 
7116  if ((t->get_cv_quals() & qualified_type_def::CV_CONST
7117  && (is_reference_type(u)))
7118  || (t->get_cv_quals() & qualified_type_def::CV_CONST
7119  && env.is_void_type(u))
7120  || t->get_cv_quals() == qualified_type_def::CV_NONE)
7121  // Let's strip the const qualifier because a reference is always
7122  // 'const' and a const void doesn't make sense. They will just
7123  // lead to spurious changes later down the pipeline, that we'll
7124  // have to deal with by doing painful and error-prone editing of
7125  // the diff IR. Dropping that useless and inconsistent artefact
7126  // right here seems to be a good way to go.
7127  result = is_decl(u);
7128 
7129  return result;
7130 }
7131 
7132 /// Merge redundant qualifiers from a tree of qualified types.
7133 ///
7134 /// Suppose a tree of qualified types leads to:
7135 ///
7136 /// const virtual const restrict const int;
7137 ///
7138 /// Suppose the IR tree of qualified types ressembles (with C meaning
7139 /// const, V meaning virtual and R meaning restrict):
7140 ///
7141 /// [C|V]-->[C|R] -->[C] --> [int].
7142 ///
7143 /// This function walks the IR and remove the redundant CV qualifiers
7144 /// so the IR becomes:
7145 ///
7146 /// [C|V] --> [R] --> [] -->[int].
7147 ///
7148 /// Note that the empty qualified type (noted []) represents a
7149 /// qualified type with no qualifier. It's rare, but it can exist.
7150 /// I've put it here just for the sake of example.
7151 ///
7152 /// The resulting IR thus represents the (merged) type:
7153 ///
7154 /// const virtual restrict int.
7155 ///
7156 /// This function is a sub-routine of the overload @ref
7157 /// strip_useless_const_qualification which doesn't return any value.
7158 ///
7159 /// @param t the qualified type to consider.
7160 ///
7161 /// @param redundant_quals the (redundant) qualifiers to be removed
7162 /// from the qualifiers of the underlying types of @p t.
7163 ///
7164 /// @return the underlying type of @p t which might have had its
7165 /// redundant qualifiers removed.
7166 static qualified_type_def_sptr
7167 strip_redundant_quals_from_underyling_types(const qualified_type_def_sptr& t,
7168  qualified_type_def::CV redundant_quals)
7169 {
7170  if (!t)
7171  return t;
7172 
7173  // We must NOT edit canonicalized types.
7174  ABG_ASSERT(!t->get_canonical_type());
7175 
7176  qualified_type_def_sptr underlying_qualified_type =
7177  is_qualified_type(t->get_underlying_type());
7178 
7179  // Let's build 'currated qualifiers' that are the qualifiers of the
7180  // current type from which redundant qualifiers are removed.
7181  qualified_type_def::CV currated_quals = t->get_cv_quals();
7182 
7183  // Remove the redundant qualifiers from these currated qualifiers
7184  currated_quals &= ~redundant_quals;
7185  t->set_cv_quals(currated_quals);
7186 
7187  // The redundant qualifiers, moving forward, is now the union of the
7188  // previous set of redundant qualifiers and the currated qualifiers.
7189  redundant_quals |= currated_quals;
7190 
7191  qualified_type_def_sptr result = t;
7192  if (underlying_qualified_type)
7193  // Now remove the redundant qualifiers from the qualified types
7194  // potentially carried by the underlying type.
7195  result =
7196  strip_redundant_quals_from_underyling_types(underlying_qualified_type,
7197  redundant_quals);
7198 
7199  return result;
7200 }
7201 
7202 /// Merge redundant qualifiers from a tree of qualified types.
7203 ///
7204 /// Suppose a tree of qualified types leads to:
7205 ///
7206 /// const virtual const restrict const int;
7207 ///
7208 /// Suppose the IR tree of qualified types ressembles (with C meaning
7209 /// const, V meaning virtual and R meaning restrict):
7210 ///
7211 /// [C|V]-->[C|R] -->[C] --> [int].
7212 ///
7213 /// This function walks the IR and remove the redundant CV qualifiers
7214 /// so the IR becomes:
7215 ///
7216 /// [C|V] --> [R] --> [] -->[int].
7217 ///
7218 /// Note that the empty qualified type (noted []) represents a
7219 /// qualified type with no qualifier. It's rare, but it can exist.
7220 /// I've put it here just for the sake of example.
7221 ///
7222 /// The resulting IR thus represents the (merged) type:
7223 ///
7224 /// const virtual restrict int.
7225 ///
7226 /// @param t the qualified type to consider. The IR below the
7227 /// argument to this parameter will be edited to remove redundant
7228 /// qualifiers where applicable.
7229 void
7230 strip_redundant_quals_from_underyling_types(const qualified_type_def_sptr& t)
7231 {
7232  if (!t)
7233  return;
7234 
7235  qualified_type_def::CV redundant_quals = qualified_type_def::CV_NONE;
7236  strip_redundant_quals_from_underyling_types(t, redundant_quals);
7237 }
7238 
7239 /// Return the leaf underlying type node of a @ref typedef_decl node.
7240 ///
7241 /// If the underlying type of a @ref typedef_decl node is itself a
7242 /// @ref typedef_decl node, then recursively look at the underlying
7243 /// type nodes to get the first one that is not a a @ref typedef_decl
7244 /// node. This is what a leaf underlying type node means.
7245 ///
7246 /// Otherwise, if the underlying type node of @ref typedef_decl is
7247 /// *NOT* a @ref typedef_decl node, then just return the underlying
7248 /// type node.
7249 ///
7250 /// And if the type node considered is not a @ref typedef_decl node,
7251 /// then just return it.
7252 ///
7253 /// @return the leaf underlying type node of a @p type.
7254 type_base_sptr
7255 peel_typedef_type(const type_base_sptr& type)
7256 {
7257  typedef_decl_sptr t = is_typedef(type);
7258  if (!t)
7259  return type;
7260 
7261  if (is_typedef(t->get_underlying_type()))
7262  return peel_typedef_type(t->get_underlying_type());
7263  return t->get_underlying_type();
7264 }
7265 
7266 /// Return the leaf underlying type node of a @ref typedef_decl node.
7267 ///
7268 /// If the underlying type of a @ref typedef_decl node is itself a
7269 /// @ref typedef_decl node, then recursively look at the underlying
7270 /// type nodes to get the first one that is not a a @ref typedef_decl
7271 /// node. This is what a leaf underlying type node means.
7272 ///
7273 /// Otherwise, if the underlying type node of @ref typedef_decl is
7274 /// *NOT* a @ref typedef_decl node, then just return the underlying
7275 /// type node.
7276 ///
7277 /// And if the type node considered is not a @ref typedef_decl node,
7278 /// then just return it.
7279 ///
7280 /// @return the leaf underlying type node of a @p type.
7281 const type_base*
7283 {
7284  const typedef_decl* t = is_typedef(type);
7285  if (!t)
7286  return type;
7287 
7288  return peel_typedef_type(t->get_underlying_type()).get();
7289 }
7290 
7291 /// Return the leaf pointed-to type node of a @ref pointer_type_def
7292 /// node.
7293 ///
7294 /// If the pointed-to type of a @ref pointer_type_def node is itself a
7295 /// @ref pointer_type_def node, then recursively look at the
7296 /// pointed-to type nodes to get the first one that is not a a @ref
7297 /// pointer_type_def node. This is what a leaf pointed-to type node
7298 /// means.
7299 ///
7300 /// Otherwise, if the pointed-to type node of @ref pointer_type_def is
7301 /// *NOT* a @ref pointer_type_def node, then just return the
7302 /// pointed-to type node.
7303 ///
7304 /// And if the type node considered is not a @ref pointer_type_def
7305 /// node, then just return it.
7306 ///
7307 /// @return the leaf pointed-to type node of a @p type.
7308 type_base_sptr
7309 peel_pointer_type(const type_base_sptr& type)
7310 {
7312  if (!t)
7313  return type;
7314 
7315  if (is_pointer_type(t->get_pointed_to_type()))
7316  return peel_pointer_type(t->get_pointed_to_type());
7317  return t->get_pointed_to_type();
7318 }
7319 
7320 /// Return the leaf pointed-to type node of a @ref pointer_type_def
7321 /// node.
7322 ///
7323 /// If the pointed-to type of a @ref pointer_type_def node is itself a
7324 /// @ref pointer_type_def node, then recursively look at the
7325 /// pointed-to type nodes to get the first one that is not a a @ref
7326 /// pointer_type_def node. This is what a leaf pointed-to type node
7327 /// means.
7328 ///
7329 /// Otherwise, if the pointed-to type node of @ref pointer_type_def is
7330 /// *NOT* a @ref pointer_type_def node, then just return the
7331 /// pointed-to type node.
7332 ///
7333 /// And if the type node considered is not a @ref pointer_type_def
7334 /// node, then just return it.
7335 ///
7336 /// @return the leaf pointed-to type node of a @p type.
7337 const type_base*
7339 {
7340  const pointer_type_def* t = is_pointer_type(type);
7341  if (!t)
7342  return type;
7343 
7344  return peel_pointer_type(t->get_pointed_to_type()).get();
7345 }
7346 
7347 /// Return the leaf pointed-to type node of a @ref reference_type_def
7348 /// node.
7349 ///
7350 /// If the pointed-to type of a @ref reference_type_def node is itself
7351 /// a @ref reference_type_def node, then recursively look at the
7352 /// pointed-to type nodes to get the first one that is not a a @ref
7353 /// reference_type_def node. This is what a leaf pointed-to type node
7354 /// means.
7355 ///
7356 /// Otherwise, if the pointed-to type node of @ref reference_type_def
7357 /// is *NOT* a @ref reference_type_def node, then just return the
7358 /// pointed-to type node.
7359 ///
7360 /// And if the type node considered is not a @ref reference_type_def
7361 /// node, then just return it.
7362 ///
7363 /// @return the leaf pointed-to type node of a @p type.
7364 type_base_sptr
7365 peel_reference_type(const type_base_sptr& type)
7366 {
7368  if (!t)
7369  return type;
7370 
7371  if (is_reference_type(t->get_pointed_to_type()))
7372  return peel_reference_type(t->get_pointed_to_type());
7373  return t->get_pointed_to_type();
7374 }
7375 
7376 /// Return the leaf pointed-to type node of a @ref reference_type_def
7377 /// node.
7378 ///
7379 /// If the pointed-to type of a @ref reference_type_def node is itself
7380 /// a @ref reference_type_def node, then recursively look at the
7381 /// pointed-to type nodes to get the first one that is not a a @ref
7382 /// reference_type_def node. This is what a leaf pointed-to type node
7383 /// means.
7384 ///
7385 /// Otherwise, if the pointed-to type node of @ref reference_type_def
7386 /// is *NOT* a @ref reference_type_def node, then just return the
7387 /// pointed-to type node.
7388 ///
7389 /// And if the type node considered is not a @ref reference_type_def
7390 /// node, then just return it.
7391 ///
7392 /// @return the leaf pointed-to type node of a @p type.
7393 const type_base*
7395 {
7396  const reference_type_def* t = is_reference_type(type);
7397  if (!t)
7398  return type;
7399 
7400  return peel_reference_type(t->get_pointed_to_type()).get();
7401 }
7402 
7403 /// Return the leaf element type of an array.
7404 ///
7405 /// If the element type is itself an array, then recursively return
7406 /// the element type of that array itself.
7407 ///
7408 /// @param type the array type to consider. If this is not an array
7409 /// type, this type is returned by the function.
7410 ///
7411 /// @return the leaf element type of the array @p type, or, if it's
7412 /// not an array type, then just return @p.
7413 const type_base_sptr
7414 peel_array_type(const type_base_sptr& type)
7415 {
7416  const array_type_def_sptr t = is_array_type(type);
7417  if (!t)
7418  return type;
7419 
7420  return peel_array_type(t->get_element_type());
7421 }
7422 
7423 /// Return the leaf element type of an array.
7424 ///
7425 /// If the element type is itself an array, then recursively return
7426 /// the element type of that array itself.
7427 ///
7428 /// @param type the array type to consider. If this is not an array
7429 /// type, this type is returned by the function.
7430 ///
7431 /// @return the leaf element type of the array @p type, or, if it's
7432 /// not an array type, then just return @p.
7433 const type_base*
7435 {
7436  const array_type_def* t = is_array_type(type);
7437  if (!t)
7438  return type;
7439 
7440  return peel_array_type(t->get_element_type()).get();
7441 }
7442 
7443 /// Return the leaf underlying type of a qualified type.
7444 ///
7445 /// If the underlying type is itself a qualified type, then
7446 /// recursively return the first underlying type of that qualified
7447 /// type to return the first underlying type that is not a qualified type.
7448 ///
7449 /// If the underlying type is NOT a qualified type, then just return
7450 /// that underlying type.
7451 ///
7452 /// @param type the qualified type to consider.
7453 ///
7454 /// @return the leaf underlying type.
7455 const type_base*
7457 {
7458  const qualified_type_def* t = is_qualified_type(type);
7459  if (!t)
7460  return type;
7461 
7462  return peel_qualified_type(t->get_underlying_type().get());
7463 }
7464 
7465 /// Return the leaf underlying type of a qualified type.
7466 ///
7467 /// If the underlying type is itself a qualified type, then
7468 /// recursively return the first underlying type of that qualified
7469 /// type to return the first underlying type that is not a qualified type.
7470 ///
7471 /// If the underlying type is NOT a qualified type, then just return
7472 /// that underlying type.
7473 ///
7474 /// @param type the qualified type to consider.
7475 ///
7476 /// @return the leaf underlying type.
7477 const type_base_sptr
7478 peel_qualified_type(const type_base_sptr& type)
7479 {
7480  const qualified_type_def_sptr t = is_qualified_type(type);
7481  if (!t)
7482  return type;
7483 
7484  return peel_qualified_type(t->get_underlying_type());
7485 }
7486 
7487 /// Test if a given qualified type is const.
7488 ///
7489 /// @pram t the qualified type to consider.
7490 ///
7491 /// @return true iff @p t is a const qualified type.
7492 bool
7493 is_const_qualified_type(const qualified_type_def_sptr& t)
7494 {
7495  if (!t)
7496  return false;
7497 
7498  if (t->get_cv_quals() == qualified_type_def::CV_CONST)
7499  return true;
7500 
7501  return false;
7502 }
7503 
7504 /// Test if a given type is const-qualified.
7505 ///
7506 /// @pram t the type to consider.
7507 ///
7508 /// @return true iff @p t is a const qualified type.
7509 bool
7510 is_const_qualified_type(const type_base_sptr& t)
7511 {
7512  qualified_type_def_sptr q = is_qualified_type(t);
7513  if (!q)
7514  return false;
7515  return is_const_qualified_type(q);
7516 }
7517 
7518 /// If a qualified type is const, then return its underlying type.
7519 ///
7520 /// @param q the qualified type to consider.
7521 ///
7522 /// @return the underlying type of @p q if it's a const-qualified
7523 /// type, otherwise, return @p q itself.
7524 type_base_sptr
7525 peel_const_qualified_type(const qualified_type_def_sptr& q)
7526 {
7527  if (!q)
7528  return q;
7529 
7530  if (is_const_qualified_type(q))
7531  return q->get_underlying_type();
7532 
7533  return q;
7534 }
7535 
7536 /// Return the leaf underlying type of a qualified or typedef type.
7537 ///
7538 /// If the underlying type is itself a qualified or typedef type, then
7539 /// recursively return the first underlying type of that qualified or
7540 /// typedef type to return the first underlying type that is not a
7541 /// qualified or typedef type.
7542 ///
7543 /// If the underlying type is NOT a qualified nor a typedef type, then
7544 /// just return that underlying type.
7545 ///
7546 /// @param type the qualified or typedef type to consider.
7547 ///
7548 /// @return the leaf underlying type.
7549 type_base*
7551 {
7552  while (is_typedef(type) || is_qualified_type(type))
7553  {
7554  if (const typedef_decl* t = is_typedef(type))
7555  type = peel_typedef_type(t);
7556 
7557  if (const qualified_type_def* t = is_qualified_type(type))
7558  type = peel_qualified_type(t);
7559  }
7560 
7561  return const_cast<type_base*>(type);
7562 }
7563 
7564 /// Return the leaf underlying type of a qualified or typedef type.
7565 ///
7566 /// If the underlying type is itself a qualified or typedef type, then
7567 /// recursively return the first underlying type of that qualified or
7568 /// typedef type to return the first underlying type that is not a
7569 /// qualified or typedef type.
7570 ///
7571 /// If the underlying type is NOT a qualified nor a typedef type, then
7572 /// just return that underlying type.
7573 ///
7574 /// @param type the qualified or typedef type to consider.
7575 ///
7576 /// @return the leaf underlying type.
7577 type_base_sptr
7578 peel_qualified_or_typedef_type(const type_base_sptr &t)
7579 {
7580  type_base_sptr type = t;
7581  while (is_typedef(type) || is_qualified_type(type))
7582  {
7583  if (typedef_decl_sptr t = is_typedef(type))
7584  type = peel_typedef_type(t);
7585 
7586  if (qualified_type_def_sptr t = is_qualified_type(type))
7587  type = peel_qualified_type(t);
7588  }
7589 
7590  return type;
7591 }
7592 
7593 /// Return the leaf underlying or pointed-to type node of a @ref
7594 /// typedef_decl, @ref pointer_type_def, @ref reference_type_def,
7595 /// or @ref array_type_def node.
7596 ///
7597 /// @param type the type to peel.
7598 ///
7599 /// @return the leaf underlying or pointed-to type node of @p type.
7600 type_base_sptr
7601 peel_typedef_pointer_or_reference_type(const type_base_sptr type)
7602 {
7603  type_base_sptr typ = type;
7604  while (is_typedef(typ)
7605  || is_pointer_type(typ)
7606  || is_reference_type(typ)
7607  || is_array_type(typ))
7608  {
7609  if (typedef_decl_sptr t = is_typedef(typ))
7610  typ = peel_typedef_type(t);
7611 
7613  typ = peel_pointer_type(t);
7614 
7616  typ = peel_reference_type(t);
7617 
7618  if (const array_type_def_sptr t = is_array_type(typ))
7619  typ = peel_array_type(t);
7620  }
7621 
7622  return typ;
7623 }
7624 
7625 /// Return the leaf underlying or pointed-to type node of a @ref
7626 /// typedef_decl, @ref pointer_type_def or @ref reference_type_def
7627 /// node.
7628 ///
7629 /// @param type the type to peel.
7630 ///
7631 /// @return the leaf underlying or pointed-to type node of @p type.
7632 type_base*
7634 {
7635  while (is_typedef(type)
7636  || is_pointer_type(type)
7637  || is_reference_type(type)
7638  || is_array_type(type))
7639  {
7640  if (const typedef_decl* t = is_typedef(type))
7641  type = peel_typedef_type(t);
7642 
7643  if (const pointer_type_def* t = is_pointer_type(type))
7644  type = peel_pointer_type(t);
7645 
7646  if (const reference_type_def* t = is_reference_type(type))
7647  type = peel_reference_type(t);
7648 
7649  if (const array_type_def* t = is_array_type(type))
7650  type = peel_array_type(t);
7651  }
7652 
7653  return const_cast<type_base*>(type);
7654 }
7655 
7656 /// Return the leaf underlying or pointed-to type node of a @ref
7657 /// typedef_decl, @ref pointer_type_def or @ref reference_type_def
7658 /// node.
7659 ///
7660 /// @param type the type to peel.
7661 ///
7662 /// @return the leaf underlying or pointed-to type node of @p type.
7663 type_base*
7665  bool peel_qual_type)
7666 {
7667  while (is_typedef(type)
7668  || is_pointer_type(type)
7669  || is_reference_type(type)
7670  || is_array_type(type)
7671  || (peel_qual_type && is_qualified_type(type)))
7672  {
7673  if (const typedef_decl* t = is_typedef(type))
7674  type = peel_typedef_type(t);
7675 
7676  if (const pointer_type_def* t = is_pointer_type(type))
7677  type = peel_pointer_type(t);
7678 
7679  if (const reference_type_def* t = is_reference_type(type))
7680  type = peel_reference_type(t);
7681 
7682  if (const array_type_def* t = is_array_type(type))
7683  type = peel_array_type(t);
7684 
7685  if (peel_qual_type)
7686  if (const qualified_type_def* t = is_qualified_type(type))
7687  type = peel_qualified_type(t);
7688  }
7689 
7690  return const_cast<type_base*>(type);
7691 }
7692 
7693 /// Return the leaf underlying or pointed-to type node of a, @ref
7694 /// pointer_type_def, @ref reference_type_def or @ref
7695 /// qualified_type_def type node.
7696 ///
7697 /// @param type the type to peel.
7698 ///
7699 /// @param peel_qualified_type if true, also peel qualified types.
7700 ///
7701 /// @return the leaf underlying or pointed-to type node of @p type.
7702 type_base*
7704  bool peel_qual_type)
7705 {
7706  while (is_pointer_type(type)
7707  || is_reference_type(type)
7708  || is_array_type(type)
7709  || (peel_qual_type && is_qualified_type(type)))
7710  {
7711  if (const pointer_type_def* t = is_pointer_type(type))
7712  type = peel_pointer_type(t);
7713 
7714  if (const reference_type_def* t = is_reference_type(type))
7715  type = peel_reference_type(t);
7716 
7717  if (const array_type_def* t = is_array_type(type))
7718  type = peel_array_type(t);
7719 
7720  if (peel_qual_type)
7721  if (const qualified_type_def* t = is_qualified_type(type))
7722  type = peel_qualified_type(t);
7723  }
7724 
7725  return const_cast<type_base*>(type);
7726 }
7727 
7728 /// Clone an array type.
7729 ///
7730 /// Note that the element type of the new array is shared witht the
7731 /// old one.
7732 ///
7733 /// @param array the array type to clone.
7734 ///
7735 /// @return a newly built array type. Note that it needs to be added
7736 /// to a scope (e.g, using add_decl_to_scope) for its lifetime to be
7737 /// bound to the one of that scope. Otherwise, its lifetime is bound
7738 /// to the lifetime of its containing shared pointer.
7741 {
7742  vector<array_type_def::subrange_sptr> subranges;
7743 
7744  for (vector<array_type_def::subrange_sptr>::const_iterator i =
7745  array->get_subranges().begin();
7746  i != array->get_subranges().end();
7747  ++i)
7748  {
7750  (new array_type_def::subrange_type(array->get_environment(),
7751  (*i)->get_name(),
7752  (*i)->get_lower_bound(),
7753  (*i)->get_upper_bound(),
7754  (*i)->get_underlying_type(),
7755  (*i)->get_location(),
7756  (*i)->get_language()));
7757  subrange->is_non_finite((*i)->is_non_finite());
7758  if (scope_decl *scope = (*i)->get_scope())
7759  add_decl_to_scope(subrange, scope);
7760  subranges.push_back(subrange);
7761  }
7762 
7763  array_type_def_sptr result
7764  (new array_type_def(array->get_element_type(),
7765  subranges, array->get_location()));
7766 
7767  return result;
7768 }
7769 
7770 /// Clone a typedef type.
7771 ///
7772 /// Note that the underlying type of the newly constructed typedef is
7773 /// shared with the old one.
7774 ///
7775 /// @param t the typedef to clone.
7776 ///
7777 /// @return the newly constructed typedef. Note that it needs to be
7778 /// added to a scope (e.g, using add_decl_to_scope) for its lifetime
7779 /// to be bound to the one of that scope. Otherwise, its lifetime is
7780 /// bound to the lifetime of its containing shared pointer.
7783 {
7784  if (!t)
7785  return t;
7786 
7787  typedef_decl_sptr result
7788  (new typedef_decl(t->get_name(), t->get_underlying_type(),
7789  t->get_location(), t->get_linkage_name(),
7790  t->get_visibility()));
7791  return result;
7792 }
7793 
7794 /// Clone a qualifiend type.
7795 ///
7796 /// Note that underlying type of the newly constructed qualified type
7797 /// is shared with the old one.
7798 ///
7799 /// @param t the qualified type to clone.
7800 ///
7801 /// @return the newly constructed qualified type. Note that it needs
7802 /// to be added to a scope (e.g, using add_decl_to_scope) for its
7803 /// lifetime to be bound to the one of that scope. Otherwise, its
7804 /// lifetime is bound to the lifetime of its containing shared
7805 /// pointer.
7806 qualified_type_def_sptr
7807 clone_qualified_type(const qualified_type_def_sptr& t)
7808 {
7809  if (!t)
7810  return t;
7811 
7812  qualified_type_def_sptr result
7813  (new qualified_type_def(t->get_underlying_type(),
7814  t->get_cv_quals(), t->get_location()));
7815 
7816  return result;
7817 }
7818 
7819 /// Clone a typedef, an array or a qualified tree.
7820 ///
7821 /// @param type the typedef, array or qualified tree to clone. any
7822 /// order.
7823 ///
7824 /// @return the cloned type, or NULL if @type was neither a typedef,
7825 /// array nor a qualified type.
7826 static type_base_sptr
7827 clone_typedef_array_qualified_type(type_base_sptr type)
7828 {
7829  if (!type)
7830  return type;
7831 
7832  scope_decl* scope = is_decl(type) ? is_decl(type)->get_scope() : 0;
7833  type_base_sptr result;
7834 
7835  if (typedef_decl_sptr t = is_typedef(type))
7836  result = clone_typedef(is_typedef(t));
7837  else if (qualified_type_def_sptr t = is_qualified_type(type))
7838  result = clone_qualified_type(t);
7839  else if (array_type_def_sptr t = is_array_type(type))
7840  result = clone_array(t);
7841  else
7842  return type_base_sptr();
7843 
7844  if (scope)
7845  add_decl_to_scope(is_decl(result), scope);
7846 
7847  return result;
7848 }
7849 
7850 /// Clone a type tree made of an array or a typedef of array.
7851 ///
7852 /// Note that this can be a tree which root node is a typedef an which
7853 /// sub-tree can be any arbitrary combination of typedef, qualified
7854 /// type and arrays.
7855 ///
7856 /// @param t the array or typedef of qualified array to consider.
7857 ///
7858 /// @return a clone of @p t.
7859 type_base_sptr
7860 clone_array_tree(const type_base_sptr t)
7861 {
7863 
7864  scope_decl* scope = is_decl(t)->get_scope();
7865  type_base_sptr result = clone_typedef_array_qualified_type(t);
7866  ABG_ASSERT(is_typedef_of_array(result) || is_array_type(result));
7867 
7868  type_base_sptr subtree;
7869  if (typedef_decl_sptr type = is_typedef(result))
7870  {
7871  type_base_sptr s =
7872  clone_typedef_array_qualified_type(type->get_underlying_type());
7873  if (s)
7874  {
7875  subtree = s;
7876  type->set_underlying_type(subtree);
7877  }
7878  }
7879  else if (array_type_def_sptr type = is_array_type(result))
7880  {
7881  type_base_sptr s =
7882  clone_typedef_array_qualified_type(type->get_element_type());
7883  if (s)
7884  {
7885  subtree = s;
7886  type->set_element_type(subtree);
7887  }
7888  }
7889  add_decl_to_scope(is_decl(subtree), scope);
7890 
7891  for (;;)
7892  {
7893  if (typedef_decl_sptr t = is_typedef(subtree))
7894  {
7895  type_base_sptr s =
7896  clone_typedef_array_qualified_type(t->get_underlying_type());
7897  if (s)
7898  {
7899  scope_decl* scope =
7900  is_decl(t->get_underlying_type())->get_scope();
7901  ABG_ASSERT(scope);
7902  add_decl_to_scope(is_decl(s), scope);
7903  t->set_underlying_type (s);
7904  subtree = s;
7905  }
7906  else
7907  break;
7908  }
7909  else if (qualified_type_def_sptr t = is_qualified_type(subtree))
7910  {
7911  type_base_sptr s =
7912  clone_typedef_array_qualified_type(t->get_underlying_type());
7913  if (s)
7914  {
7915  scope_decl* scope =
7916  is_decl(t->get_underlying_type())->get_scope();
7917  ABG_ASSERT(scope);
7918  add_decl_to_scope(is_decl(s), scope);
7919  t->set_underlying_type(s);
7920  subtree = s;
7921  }
7922  else
7923  break;
7924  }
7925  else if (array_type_def_sptr t = is_array_type(subtree))
7926  {
7927  type_base_sptr e = t->get_element_type();
7928  if (is_typedef(e) || is_qualified_type(e))
7929  {
7930  type_base_sptr s =
7931  clone_typedef_array_qualified_type(e);
7932  if (s)
7933  {
7934  scope_decl* scope = is_decl(e)->get_scope();
7935  ABG_ASSERT(scope);
7936  add_decl_to_scope(is_decl(s), scope);
7937  t->set_element_type(s);
7938  }
7939  else
7940  break;
7941  }
7942  break;
7943  }
7944  else
7945  break;
7946  }
7947  return result;
7948 }
7949 
7950 /// Update the qualified name of a given sub-tree.
7951 ///
7952 /// @param d the sub-tree for which to update the qualified name.
7953 static void
7954 update_qualified_name(decl_base * d)
7955 {
7956  ::qualified_name_setter setter;
7957  d->traverse(setter);
7958 }
7959 
7960 /// Update the qualified name of a given sub-tree.
7961 ///
7962 /// @param d the sub-tree for which to update the qualified name.
7963 static void
7964 update_qualified_name(decl_base_sptr d)
7965 {return update_qualified_name(d.get());}
7966 
7967 // <scope_decl stuff>
7968 
7969 /// Hash a type by returning the pointer value of its canonical type.
7970 ///
7971 /// @param l the type to hash.
7972 ///
7973 /// @return the the pointer value of the canonical type of @p l.
7974 size_t
7975 canonical_type_hash::operator()(const type_base_sptr& l) const
7976 {return operator()(l.get());}
7977 
7978 /// Hash a (canonical) type by returning its pointer value
7979 ///
7980 /// @param l the canonical type to hash.
7981 ///
7982 /// @return the pointer value of the canonical type of @p l.
7983 size_t
7985 {return reinterpret_cast<size_t>(l);}
7986 
7987 struct scope_decl::priv
7988 {
7989  declarations members_;
7990  declarations sorted_members_;
7991  type_base_sptrs_type member_types_;
7992  type_base_sptrs_type sorted_member_types_;
7993  scopes member_scopes_;
7994  canonical_type_sptr_set_type canonical_types_;
7995  type_base_sptrs_type sorted_canonical_types_;
7996 }; // end struct scope_decl::priv
7997 
7998 /// Constructor of the @ref scope_decl type.
7999 ///
8000 /// @param the environment to use for the new instance.
8001 ///
8002 /// @param the name of the scope decl.
8003 ///
8004 /// @param locus the source location where the scope_decl is defined.
8005 ///
8006 /// @param vis the visibility of the declaration.
8007 scope_decl::scope_decl(const environment& env,
8008  const string& name,
8009  const location& locus,
8010  visibility vis)
8011  : type_or_decl_base(env, ABSTRACT_SCOPE_DECL|ABSTRACT_DECL_BASE),
8012  decl_base(env, name, locus, /*mangled_name=*/name, vis),
8013  priv_(new priv)
8014 {}
8015 
8016 /// Constructor of the @ref scope_decl type.
8017 ///
8018 /// @param the environment to use for the new instance.
8019 ///
8020 /// @param l the source location where the scope_decl is defined.
8021 ///
8022 /// @param vis the visibility of the declaration.
8023 scope_decl::scope_decl(const environment& env, location& l)
8024  : type_or_decl_base(env, ABSTRACT_SCOPE_DECL|ABSTRACT_DECL_BASE),
8025  decl_base(env, "", l),
8026  priv_(new priv)
8027 {}
8028 
8029 /// @eturn the set of canonical types of the the current scope.
8032 {return priv_->canonical_types_;}
8033 
8034 /// @eturn the set of canonical types of the the current scope.
8037 {return const_cast<scope_decl*>(this)->get_canonical_types();}
8038 
8039 /// Return a vector of sorted canonical types of the current scope.
8040 ///
8041 /// The types are sorted "almost topologically". That means, they are
8042 /// sorted using the lexicographic order of the string representing
8043 /// the location their definition point. If a type doesn't have a
8044 /// location, then its pretty representation is used.
8045 ///
8046 /// @return a vector of sorted canonical types of the current scope.
8047 const type_base_sptrs_type&
8049 {
8050  if (priv_->sorted_canonical_types_.empty())
8051  {
8052  for (canonical_type_sptr_set_type::const_iterator e =
8053  get_canonical_types().begin();
8054  e != get_canonical_types().end();
8055  ++e)
8056  priv_->sorted_canonical_types_.push_back(*e);
8057 
8058  type_topo_comp comp;
8059  std::stable_sort(priv_->sorted_canonical_types_.begin(),
8060  priv_->sorted_canonical_types_.end(),
8061  comp);
8062  }
8063  return priv_->sorted_canonical_types_;
8064 }
8065 
8066 /// Getter for the member declarations carried by the current @ref
8067 /// scope_decl.
8068 ///
8069 /// @return the member declarations carried by the current @ref
8070 /// scope_decl.
8073 {return priv_->members_;}
8074 
8075 /// Getter for the member declarations carried by the current @ref
8076 /// scope_decl.
8077 ///
8078 /// @return the member declarations carried by the current @ref
8079 /// scope_decl.
8082 {return priv_->members_;}
8083 
8084 /// Getter for the sorted member declarations carried by the current
8085 /// @ref scope_decl.
8086 ///
8087 /// @return the sorted member declarations carried by the current @ref
8088 /// scope_decl. The declarations are sorted topologically.
8091 {
8092  decl_topo_comp comp;
8093  if (priv_->sorted_members_.empty())
8094  {
8095  for (declarations::const_iterator i = get_member_decls().begin();
8096  i != get_member_decls().end();
8097  ++i)
8098  priv_->sorted_members_.push_back(*i);
8099 
8100  std::stable_sort(priv_->sorted_members_.begin(),
8101  priv_->sorted_members_.end(),
8102  comp);
8103  }
8104  return priv_->sorted_members_;
8105 }
8106 
8107 /// Getter for the number of anonymous classes contained in this
8108 /// scope.
8109 ///
8110 /// @return the number of anonymous classes contained in this scope.
8111 size_t
8113 {
8114  int result = 0;
8115  for (declarations::const_iterator it = get_member_decls().begin();
8116  it != get_member_decls().end();
8117  ++it)
8118  if (class_decl_sptr t = is_class_type(*it))
8119  if (t->get_is_anonymous())
8120  ++result;
8121 
8122  return result;
8123 }
8124 
8125 /// Getter for the number of anonymous unions contained in this
8126 /// scope.
8127 ///
8128 /// @return the number of anonymous unions contained in this scope.
8129 size_t
8131 {
8132  int result = 0;
8133  for (declarations::const_iterator it = get_member_decls().begin();
8134  it != get_member_decls().end();
8135  ++it)
8136  if (union_decl_sptr t = is_union_type(*it))
8137  if (t->get_is_anonymous())
8138  ++result;
8139 
8140  return result;
8141 }
8142 
8143 /// Getter for the number of anonymous enums contained in this
8144 /// scope.
8145 ///
8146 /// @return the number of anonymous enums contained in this scope.
8147 size_t
8149 {
8150  int result = 0;
8151  for (declarations::const_iterator it = get_member_decls().begin();
8152  it != get_member_decls().end();
8153  ++it)
8154  if (enum_type_decl_sptr t = is_enum_type(*it))
8155  if (t->get_is_anonymous())
8156  ++result;
8157 
8158  return result;
8159 }
8160 
8161 /// Getter for the scopes carried by the current scope.
8162 ///
8163 /// @return the scopes carried by the current scope.
8166 {return priv_->member_scopes_;}
8167 
8168 /// Getter for the scopes carried by the current scope.
8169 ///
8170 /// @return the scopes carried by the current scope.
8171 const scope_decl::scopes&
8173 {return priv_->member_scopes_;}
8174 
8175 /// Test if the current scope is empty.
8176 ///
8177 /// @return true iff the current scope is empty.
8178 bool
8180 {
8181  return (get_member_decls().empty()
8182  && get_canonical_types().empty());
8183 }
8184 
8185 /// Set the translation unit of a decl
8186 ///
8187 /// It also perform some IR integrity checks.
8188 ///
8189 /// This is a sub-routine of scope_decl::{insert,add}_member_decl.
8190 ///
8191 /// @param decl the decl to set the translation unit for.
8192 ///
8193 /// @param tu the translation unit to set.
8194 static void
8195 maybe_set_translation_unit(const decl_base_sptr& decl,
8196  translation_unit* tu)
8197 {
8198  if (translation_unit* existing_tu = decl->get_translation_unit())
8199  // The decl already belongs to a translation unit.
8200  // Either:
8201  //
8202  // 1/ it's a unique type, in which case we should not add it to
8203  // any translation unique since unique types are "logically"
8204  // supposed to belong to no translation unit in particular, as
8205  // they are unique.
8206  //
8207  // 2/ or the decl was already added to this translation unit.
8208  ABG_ASSERT(tu == existing_tu || is_unique_type(is_type(decl)));
8209  else
8210  decl->set_translation_unit(tu);
8211 }
8212 
8213 /// Add a member decl to this scope. Note that user code should not
8214 /// use this, but rather use add_decl_to_scope.
8215 ///
8216 /// Note that this function updates the qualified name of the member
8217 /// decl that is added. It also sets the scope of the member. Thus,
8218 /// it ABG_ASSERTs that member should not have its scope set, prior to
8219 /// calling this function.
8220 ///
8221 /// @param member the new member decl to add to this scope.
8222 decl_base_sptr
8223 scope_decl::add_member_decl(const decl_base_sptr& member)
8224 {
8225  ABG_ASSERT(!has_scope(member));
8226 
8227  member->set_scope(this);
8228  priv_->members_.push_back(member);
8229  if (is_type(member))
8230  priv_->member_types_.push_back(is_type(member));
8231 
8232  if (scope_decl_sptr m = dynamic_pointer_cast<scope_decl>(member))
8233  priv_->member_scopes_.push_back(m);
8234 
8235  update_qualified_name(member);
8236 
8238  maybe_set_translation_unit(member, tu);
8239 
8241 
8242  return member;
8243 }
8244 
8245 /// Get the member types of this @ref scope_decl.
8246 ///
8247 /// @return a vector of the member types of this ref class_or_union.
8248 const type_base_sptrs_type&
8250 {return priv_->member_types_;}
8251 
8252 /// Find a member type of a given name, inside the current @ref
8253 /// scope_decl.
8254 ///
8255 /// @param name the name of the member type to look for.
8256 ///
8257 /// @return a pointer to the @ref type_base that represents the member
8258 /// type of name @p name, for the current scope.
8259 type_base_sptr
8260 scope_decl::find_member_type(const string& name) const
8261 {
8262  for (auto t : get_member_types())
8263  if (get_type_name(t, /*qualified*/false) == name)
8264  return t;
8265  return type_base_sptr();
8266 }
8267 
8268 /// Insert a member type.
8269 ///
8270 /// @param t the type to insert in the @ref scope_decl type.
8271 ///
8272 /// @param an iterator right before which @p t has to be inserted.
8273 void
8275  declarations::iterator before)
8276 {
8277  decl_base_sptr d = get_type_declaration(t);
8278  ABG_ASSERT(d);
8279  ABG_ASSERT(!has_scope(d));
8280 
8281  priv_->member_types_.push_back(t);
8282  insert_member_decl(d, before);
8283 }
8284 
8285 /// Add a member type to the current instance of class_or_union.
8286 ///
8287 /// @param t the member type to add. It must not have been added to a
8288 /// scope, otherwise this will violate an ABG_ASSERTion.
8289 void
8291 {insert_member_type(t, get_member_decls().end());}
8292 
8293 /// Add a member type to the current instance of class_or_union.
8294 ///
8295 /// @param t the type to be added as a member type to the current
8296 /// instance of class_or_union. An instance of class_or_union::member_type
8297 /// will be created out of @p t and and added to the the class.
8298 ///
8299 /// @param a the access specifier for the member type to be created.
8300 type_base_sptr
8302 {
8303  decl_base_sptr d = get_type_declaration(t);
8304  ABG_ASSERT(d);
8306  add_member_type(t);
8308  return t;
8309 }
8310 
8311 /// Remove a member type from the current @ref class_or_union scope.
8312 ///
8313 /// @param t the type to remove.
8314 void
8316 {
8317  for (auto i = priv_->member_types_.begin();
8318  i != priv_->member_types_.end();
8319  ++i)
8320  {
8321  if (*((*i)) == *t)
8322  {
8323  priv_->member_types_.erase(i);
8324  return;
8325  }
8326  }
8327 }
8328 
8329 /// Get the sorted member types of this @ref scope_decl
8330 ///
8331 /// @return a vector of the sorted member types of this ref
8332 /// class_or_union.
8333 const type_base_sptrs_type&
8335 {
8336  if (priv_->sorted_member_types_.empty())
8337  {
8338  for (auto t : get_member_types())
8339  priv_->sorted_member_types_.push_back(t);
8340 
8341  type_topo_comp comp;
8342  std::stable_sort(priv_->sorted_member_types_.begin(),
8343  priv_->sorted_member_types_.end(),
8344  comp);
8345  }
8346  return priv_->sorted_member_types_;
8347 }
8348 
8349 /// Insert a member decl to this scope, right before an element
8350 /// pointed to by a given iterator. Note that user code should not
8351 /// use this, but rather use insert_decl_into_scope.
8352 ///
8353 /// Note that this function updates the qualified name of the inserted
8354 /// member.
8355 ///
8356 /// @param member the new member decl to add to this scope.
8357 ///
8358 /// @param before an interator pointing to the element before which
8359 /// the new member should be inserted.
8360 decl_base_sptr
8361 scope_decl::insert_member_decl(decl_base_sptr member,
8362  declarations::iterator before)
8363 {
8364  ABG_ASSERT(!member->get_scope());
8365 
8366  member->set_scope(this);
8367  priv_->members_.insert(before, member);
8368 
8369  if (scope_decl_sptr m = dynamic_pointer_cast<scope_decl>(member))
8370  priv_-> member_scopes_.push_back(m);
8371 
8372  update_qualified_name(member);
8373 
8375  maybe_set_translation_unit(member, tu);
8376 
8378 
8379  return member;
8380 }
8381 
8382 /// Remove a declaration from the current scope.
8383 ///
8384 /// @param member the declaration to remove from the scope.
8385 void
8386 scope_decl::remove_member_decl(decl_base_sptr member)
8387 {
8388  for (declarations::iterator i = priv_->members_.begin();
8389  i != priv_->members_.end();
8390  ++i)
8391  {
8392  if (**i == *member)
8393  {
8394  priv_->members_.erase(i);
8395  // Do not access i after this point as it's invalided by the
8396  // erase call.
8397  break;
8398  }
8399  }
8400 
8401  scope_decl_sptr scope = dynamic_pointer_cast<scope_decl>(member);
8402  if (scope)
8403  {
8404  for (scopes::iterator i = priv_->member_scopes_.begin();
8405  i != priv_->member_scopes_.end();
8406  ++i)
8407  {
8408  if (**i == *member)
8409  {
8410  priv_->member_scopes_.erase(i);
8411  break;
8412  }
8413  }
8414  }
8415 
8416  member->set_scope(nullptr);
8417  member->set_translation_unit(nullptr);
8418 }
8419 
8420 /// Return the hash value for the current instance of scope_decl.
8421 ///
8422 /// This method can trigger the computing of the hash value, if need be.
8423 ///
8424 /// @return the hash value.
8425 size_t
8427 {
8428  scope_decl::hash hash_scope;
8429  return hash_scope(this);
8430 }
8431 
8432 /// Compares two instances of @ref scope_decl.
8433 ///
8434 /// If the two intances are different, set a bitfield to give some
8435 /// insight about the kind of differences there are.
8436 ///
8437 /// @param l the first artifact of the comparison.
8438 ///
8439 /// @param r the second artifact of the comparison.
8440 ///
8441 /// @param k a pointer to a bitfield that gives information about the
8442 /// kind of changes there are between @p l and @p r. This one is set
8443 /// iff @p k is non-null and the function returns false.
8444 ///
8445 /// Please note that setting k to a non-null value does have a
8446 /// negative performance impact because even if @p l and @p r are not
8447 /// equal, the function keeps up the comparison in order to determine
8448 /// the different kinds of ways in which they are different.
8449 ///
8450 /// @return true if @p l equals @p r, false otherwise.
8451 bool
8452 equals(const scope_decl& l, const scope_decl& r, change_kind* k)
8453 {
8454  bool result = true;
8455 
8456  if (!l.decl_base::operator==(r))
8457  {
8458  result = false;
8459  if (k)
8461  else
8463  }
8464 
8465  scope_decl::declarations::const_iterator i, j;
8466  for (i = l.get_member_decls().begin(), j = r.get_member_decls().begin();
8467  i != l.get_member_decls().end() && j != r.get_member_decls().end();
8468  ++i, ++j)
8469  {
8470  if (**i != **j)
8471  {
8472  result = false;
8473  if (k)
8474  {
8475  *k |= SUBTYPE_CHANGE_KIND;
8476  break;
8477  }
8478  else
8480  }
8481  }
8482 
8483  if (i != l.get_member_decls().end() || j != r.get_member_decls().end())
8484  {
8485  result = false;
8486  if (k)
8488  else
8490  }
8491 
8492  ABG_RETURN(result);
8493 }
8494 
8495 /// Return true iff both scopes have the same names and have the same
8496 /// member decls.
8497 ///
8498 /// This function doesn't check for equality of the scopes of its
8499 /// arguments.
8500 bool
8502 {
8503  const scope_decl* other = dynamic_cast<const scope_decl*>(&o);
8504  if (!other)
8505  return false;
8506 
8507  return equals(*this, *other, 0);
8508 }
8509 
8510 /// Equality operator for @ref scope_decl_sptr.
8511 ///
8512 /// @param l the left hand side operand of the equality operator.
8513 ///
8514 /// @pram r the right hand side operand of the equalify operator.
8515 ///
8516 /// @return true iff @p l equals @p r.
8517 bool
8519 {
8520  if (!!l != !!r)
8521  return false;
8522  if (l.get() == r.get())
8523  return true;
8524  return *l == *r;
8525 }
8526 
8527 /// Inequality operator for @ref scope_decl_sptr.
8528 ///
8529 /// @param l the left hand side operand of the equality operator.
8530 ///
8531 /// @pram r the right hand side operand of the equalify operator.
8532 ///
8533 /// @return true iff @p l equals @p r.
8534 bool
8536 {return !operator==(l, r);}
8537 
8538 /// Find a member of the current scope and return an iterator on it.
8539 ///
8540 /// @param decl the scope member to find.
8541 ///
8542 /// @param i the iterator to set to the member @p decl. This is set
8543 /// iff the function returns true.
8544 ///
8545 /// @return true if the member decl was found, false otherwise.
8546 bool
8548  declarations::iterator& i)
8549 {
8550  if (!decl)
8551  return false;
8552 
8553  if (get_member_decls().empty())
8554  {
8555  i = get_member_decls().end();
8556  return false;
8557  }
8558 
8559  for (declarations::iterator it = get_member_decls().begin();
8560  it != get_member_decls().end();
8561  ++it)
8562  {
8563  if ((*it).get() == decl)
8564  {
8565  i = it;
8566  return true;
8567  }
8568  }
8569 
8570  return false;
8571 }
8572 
8573 /// Find a member of the current scope and return an iterator on it.
8574 ///
8575 /// @param decl the scope member to find.
8576 ///
8577 /// @param i the iterator to set to the member @p decl. This is set
8578 /// iff the function returns true.
8579 ///
8580 /// @return true if the member decl was found, false otherwise.
8581 bool
8582 scope_decl::find_iterator_for_member(const decl_base_sptr decl,
8583  declarations::iterator& i)
8584 {return find_iterator_for_member(decl.get(), i);}
8585 
8586 /// This implements the ir_traversable_base::traverse pure virtual
8587 /// function.
8588 ///
8589 /// @param v the visitor used on the current instance of scope_decl
8590 /// and on its member nodes.
8591 ///
8592 /// @return true if the traversal of the tree should continue, false
8593 /// otherwise.
8594 bool
8596 {
8597  if (visiting())
8598  return true;
8599 
8600  if (v.visit_begin(this))
8601  {
8602  visiting(true);
8603  for (scope_decl::declarations::const_iterator i =
8604  get_member_decls().begin();
8605  i != get_member_decls ().end();
8606  ++i)
8607  if (!(*i)->traverse(v))
8608  break;
8609  visiting(false);
8610  }
8611  return v.visit_end(this);
8612 }
8613 
8614 scope_decl::~scope_decl()
8615 {}
8616 
8617 /// Appends a declaration to a given scope, if the declaration
8618 /// doesn't already belong to one and if the declaration is not for a
8619 /// type that is supposed to be unique.
8620 ///
8621 /// @param decl the declaration to add to the scope
8622 ///
8623 /// @param scope the scope to append the declaration to
8624 decl_base_sptr
8625 add_decl_to_scope(decl_base_sptr decl, scope_decl* scope)
8626 {
8627  ABG_ASSERT(scope);
8628 
8629  if (scope && decl && !decl->get_scope())
8630  decl = scope->add_member_decl(decl);
8631 
8632  return decl;
8633 }
8634 
8635 /// Appends a declaration to a given scope, if the declaration doesn't
8636 /// already belong to a scope.
8637 ///
8638 /// @param decl the declaration to add append to the scope
8639 ///
8640 /// @param scope the scope to append the decl to
8641 decl_base_sptr
8642 add_decl_to_scope(decl_base_sptr decl, const scope_decl_sptr& scope)
8643 {return add_decl_to_scope(decl, scope.get());}
8644 
8645 /// Remove a given decl from its scope
8646 ///
8647 /// @param decl the decl to remove from its scope.
8648 void
8649 remove_decl_from_scope(decl_base_sptr decl)
8650 {
8651  if (!decl)
8652  return;
8653 
8654  scope_decl* scope = decl->get_scope();
8655  scope->remove_member_decl(decl);
8656 }
8657 
8658 /// Inserts a declaration into a given scope, before a given IR child
8659 /// node of the scope.
8660 ///
8661 /// @param decl the declaration to insert into the scope.
8662 ///
8663 /// @param before an iterator pointing to the child IR node before
8664 /// which to insert the declaration.
8665 ///
8666 /// @param scope the scope into which to insert the declaration.
8667 decl_base_sptr
8668 insert_decl_into_scope(decl_base_sptr decl,
8669  scope_decl::declarations::iterator before,
8670  scope_decl* scope)
8671 {
8672  if (scope && decl && !decl->get_scope())
8673  {
8674  decl_base_sptr d = scope->insert_member_decl(decl, before);
8675  decl = d;
8676  }
8677  return decl;
8678 }
8679 
8680 /// Inserts a declaration into a given scope, before a given IR child
8681 /// node of the scope.
8682 ///
8683 /// @param decl the declaration to insert into the scope.
8684 ///
8685 /// @param before an iterator pointing to the child IR node before
8686 /// which to insert the declaration.
8687 ///
8688 /// @param scope the scope into which to insert the declaration.
8689 decl_base_sptr
8690 insert_decl_into_scope(decl_base_sptr decl,
8691  scope_decl::declarations::iterator before,
8692  scope_decl_sptr scope)
8693 {return insert_decl_into_scope(decl, before, scope.get());}
8694 
8695 /// Constructor of the @ref global_scope type.
8696 ///
8697 /// @param tu the translation unit the scope belongs to.
8698 global_scope::global_scope(translation_unit *tu)
8699  : type_or_decl_base(tu->get_environment(),
8700  GLOBAL_SCOPE_DECL
8701  | ABSTRACT_DECL_BASE
8702  | ABSTRACT_SCOPE_DECL),
8703  decl_base(tu->get_environment(), "", location()),
8704  scope_decl(tu->get_environment(), "", location()),
8705  translation_unit_(tu)
8706 {
8707  runtime_type_instance(this);
8708 }
8709 
8710 /// return the global scope as seen by a given declaration.
8711 ///
8712 /// @param decl the declaration to consider.
8713 ///
8714 /// @return the global scope of the decl, or a null pointer if the
8715 /// decl is not yet added to a translation_unit.
8716 const global_scope*
8718 {
8719  if (const global_scope* s = dynamic_cast<const global_scope*>(&decl))
8720  return s;
8721 
8722  scope_decl* scope = decl.get_scope();
8723  while (scope && !dynamic_cast<global_scope*>(scope))
8724  scope = scope->get_scope();
8725 
8726  return scope ? dynamic_cast<global_scope*> (scope) : 0;
8727 }
8728 
8729 /// return the global scope as seen by a given declaration.
8730 ///
8731 /// @param decl the declaration to consider.
8732 ///
8733 /// @return the global scope of the decl, or a null pointer if the
8734 /// decl is not yet added to a translation_unit.
8735 const global_scope*
8737 {return get_global_scope(*decl);}
8738 
8739 /// Return the global scope as seen by a given declaration.
8740 ///
8741 /// @param decl the declaration to consider.
8742 ///
8743 /// @return the global scope of the decl, or a null pointer if the
8744 /// decl is not yet added to a translation_unit.
8745 const global_scope*
8746 get_global_scope(const shared_ptr<decl_base> decl)
8747 {return get_global_scope(decl.get());}
8748 
8749 /// Return the a scope S containing a given declaration and that is
8750 /// right under a given scope P.
8751 ///
8752 /// Note that @p scope must come before @p decl in topological
8753 /// order.
8754 ///
8755 /// @param decl the decl for which to find a scope.
8756 ///
8757 /// @param scope the scope under which the resulting scope must be.
8758 ///
8759 /// @return the resulting scope.
8760 const scope_decl*
8762  const scope_decl* scope)
8763 {
8764  if (!decl)
8765  return 0;
8766 
8767  if (scope == 0)
8768  return get_global_scope(decl);
8769 
8770  // Handle the case where decl is a scope itself.
8771  const scope_decl* s = dynamic_cast<const scope_decl*>(decl);
8772  if (!s)
8773  s = decl->get_scope();
8774 
8775  if (is_global_scope(s))
8776  return scope;
8777 
8778  // Here, decl is in the scope 'scope', or decl and 'scope' are the
8779  // same. The caller needs to be prepared to deal with this case.
8780  if (s == scope)
8781  return s;
8782 
8783  while (s && !is_global_scope(s) && s->get_scope() != scope)
8784  s = s->get_scope();
8785 
8786  if (!s || is_global_scope(s))
8787  // SCOPE must come before decl in topological order, but I don't
8788  // know how to ensure that ...
8789  return scope;
8790  ABG_ASSERT(s);
8791 
8792  return s;
8793 }
8794 
8795 /// Return the a scope S containing a given declaration and that is
8796 /// right under a given scope P.
8797 ///
8798 /// @param decl the decl for which to find a scope.
8799 ///
8800 /// @param scope the scope under which the resulting scope must be.
8801 ///
8802 /// @return the resulting scope.
8803 const scope_decl*
8804 get_top_most_scope_under(const decl_base_sptr decl,
8805  const scope_decl* scope)
8806 {return get_top_most_scope_under(decl.get(), scope);}
8807 
8808 /// Return the a scope S containing a given declaration and that is
8809 /// right under a given scope P.
8810 ///
8811 /// @param decl the decl for which to find a scope.
8812 ///
8813 /// @param scope the scope under which the resulting scope must be.
8814 ///
8815 /// @return the resulting scope.
8816 const scope_decl*
8817 get_top_most_scope_under(const decl_base_sptr decl,
8818  const scope_decl_sptr scope)
8819 {return get_top_most_scope_under(decl, scope.get());}
8820 
8821 // </scope_decl stuff>
8822 
8823 
8824 /// Get the string representation of a CV qualifier bitmap.
8825 ///
8826 /// @param cv_quals the bitmap of CV qualifiers to consider.
8827 ///
8828 /// @return the string representation.
8829 string
8831 {
8832  string repr;
8833  if (cv_quals & qualified_type_def::CV_RESTRICT)
8834  repr = "restrict";
8835  if (cv_quals & qualified_type_def::CV_CONST)
8836  {
8837  if (!repr.empty())
8838  repr += ' ';
8839  repr += "const";
8840  }
8841  if (cv_quals & qualified_type_def::CV_VOLATILE)
8842  {
8843  if (!repr.empty())
8844  repr += ' ';
8845  repr += "volatile";
8846  }
8847  return repr;
8848 }
8849 
8850 /// Build and return a copy of the name of an ABI artifact that is
8851 /// either a type or a decl.
8852 ///
8853 /// @param tod the ABI artifact to get the name for.
8854 ///
8855 /// @param qualified if yes, return the qualified name of @p tod;
8856 /// otherwise, return the non-qualified name;
8857 ///
8858 /// @return the name of @p tod.
8859 string
8860 get_name(const type_or_decl_base *tod, bool qualified)
8861 {
8862  string result;
8863 
8864  type_or_decl_base* a = const_cast<type_or_decl_base*>(tod);
8865 
8866  if (type_base* t = dynamic_cast<type_base*>(a))
8867  result = get_type_name(t, qualified);
8868  else if (decl_base *d = dynamic_cast<decl_base*>(a))
8869  {
8870  if (qualified)
8871  result = d->get_qualified_name();
8872  else
8873  result = d->get_name();
8874  }
8875  else
8876  // We should never reach this point.
8877  abort();
8878 
8879  return result;
8880 }
8881 
8882 /// Build and return a copy of the name of an ABI artifact that is
8883 /// either a type of a decl.
8884 ///
8885 /// @param tod the ABI artifact to get the name for.
8886 ///
8887 /// @param qualified if yes, return the qualified name of @p tod;
8888 /// otherwise, return the non-qualified name;
8889 ///
8890 /// @return the name of @p tod.
8891 string
8892 get_name(const type_or_decl_base_sptr& tod, bool qualified)
8893 {return get_name(tod.get(), qualified);}
8894 
8895 /// Build and return a qualified name from a name and its scope.
8896 ///
8897 /// The name is supposed to be for an entity that is part of the
8898 /// scope.
8899 ///
8900 /// @param the scope to consider.
8901 ///
8902 /// @param name of the name to consider.
8903 ///
8904 /// @return a copy of the string that represents the qualified name.
8905 string
8906 build_qualified_name(const scope_decl* scope, const string& name)
8907 {
8908  if (name.empty())
8909  return "";
8910 
8911  string qualified_name;
8912  if (scope)
8913  qualified_name = scope->get_qualified_name();
8914 
8915  if (qualified_name.empty())
8916  qualified_name = name;
8917  else
8918  qualified_name = qualified_name + "::" + name;
8919 
8920  return qualified_name;
8921 }
8922 
8923 /// Build and return the qualified name of a type in its scope.
8924 ///
8925 /// @param scope the scope of the type to consider.
8926 ///
8927 /// @param type the type to consider.
8928 string
8929 build_qualified_name(const scope_decl* scope, const type_base_sptr& type)
8930 {return build_qualified_name(scope, get_name((type)));}
8931 
8932 // </scope_decl stuff>
8933 
8934 /// Get the location of the declaration of a given type.
8935 ///
8936 /// @param type the type to consider.
8937 ///
8938 /// @return the location of the declaration of type @p type.
8939 location
8940 get_location(const type_base_sptr& type)
8941 {
8942  if (decl_base_sptr decl = get_type_declaration(type))
8943  return get_location(decl);
8944  return location();
8945 }
8946 
8947 /// Get the location of a given declaration.
8948 ///
8949 /// @param decl the declaration to consider.
8950 ///
8951 /// @return the location of the declaration @p decl.
8952 location
8953 get_location(const decl_base_sptr& decl)
8954 {
8955  location loc = decl->get_location();
8956  if (!loc)
8957  {
8958  if (class_or_union_sptr c = is_class_or_union_type(decl))
8959  if (c->get_is_declaration_only() && c->get_definition_of_declaration())
8960  {
8961  c = is_class_or_union_type(c->get_definition_of_declaration());
8962  loc = c->get_location();
8963  }
8964  }
8965  return loc;
8966 }
8967 
8968 /// Get the scope of a given type.
8969 ///
8970 /// @param t the type to consider.
8971 ///
8972 /// @return the scope of type @p t or 0 if the type has no scope yet.
8973 scope_decl*
8975 {
8976  if (!t)
8977  return 0;
8978 
8980  if (d)
8981  return d->get_scope();
8982  return 0;
8983 }
8984 
8985 /// Get the scope of a given type.
8986 ///
8987 /// @param t the type to consider.
8988 ///
8989 /// @return the scope of type @p t or 0 if the type has no scope yet.
8990 scope_decl*
8991 get_type_scope(const type_base_sptr& t)
8992 {return get_type_scope(t.get());}
8993 
8994 /// Get the name of a given type and return a copy of it.
8995 ///
8996 /// @param t the type to consider.
8997 ///
8998 /// @param qualified if true then return the qualified name of the
8999 /// type.
9000 ///
9001 /// @param internal set to true if the call is intended for an
9002 /// internal use (for technical use inside the library itself), false
9003 /// otherwise. If you don't know what this is for, then set it to
9004 /// false.
9005 ///
9006 /// @return a copy of the type name if the type has a name, or the
9007 /// empty string if it does not.
9009 get_type_name(const type_base_sptr& t, bool qualified, bool internal)
9010 {return get_type_name(t.get(), qualified, internal);}
9011 
9012 /// Return true iff a decl is for a type type that has a generic
9013 /// anonymous internal type name.
9014 ///
9015 /// @param d the decl to considier.
9016 ///
9017 /// @return true iff @p d is for a type type that has a generic
9018 /// anonymous internal type name.
9019 static bool
9020 has_generic_anonymous_internal_type_name(const decl_base *d)
9021 {
9022  return (is_class_or_union_type(d)
9023  || is_enum_type(d)
9024  || is_subrange_type(d));
9025 }
9026 
9027 /// Return the generic internal name of an anonymous type.
9028 ///
9029 /// For internal purposes, we want to define a generic name for all
9030 /// anonymous types of a certain kind. For instance, all anonymous
9031 /// structs will be have a generic name of "__anonymous_struct__", all
9032 /// anonymous unions will have a generic name of
9033 /// "__anonymous_union__", etc.
9034 ///
9035 /// That generic name can be used as a hash to put all anonymous types
9036 /// of a certain kind in the same hash table bucket, for instance.
9037 static interned_string
9038 get_generic_anonymous_internal_type_name(const decl_base *d)
9039 {
9040  ABG_ASSERT(has_generic_anonymous_internal_type_name(d));
9041 
9042  const environment&env = d->get_environment();
9043 
9044  interned_string result;
9045  if (is_class_type(d))
9046  result =
9048  else if (is_union_type(d))
9049  result =
9051  else if (is_enum_type(d))
9052  result =
9054  else if (is_subrange_type(d))
9055  result =
9057  else
9059 
9060  return result;
9061 }
9062 
9063 /// Get the internal name for a given integral type.
9064 ///
9065 /// All integral types that have the modifiers 'short, long or long
9066 /// long' have the same internal name. This is so that they can all
9067 /// have the same canonical type if they are of the same size.
9068 /// Otherwise, 'long int' and 'long long int' would have different
9069 /// canonical types even though they are equivalent from an ABI point
9070 /// of view.
9071 ///
9072 /// @param t the integral type to consider
9073 ///
9074 /// @return the internal name for @p t if it's an integral type, or
9075 /// the empty string if @p t is not an integral type.
9076 static string
9077 get_internal_integral_type_name(const type_base* t)
9078 {
9079  string name;
9080  type_decl *type = is_integral_type(t);
9081 
9082  if (!type)
9083  return name;
9084 
9085  integral_type int_type;
9086  if (parse_integral_type(type->get_name(), int_type))
9087  name = int_type.to_string(/*internal=*/true);
9088 
9089  return name;
9090 }
9091 
9092 /// Get the name of a given type and return a copy of it.
9093 ///
9094 /// @param t the type to consider.
9095 ///
9096 /// @param qualified if true then return the qualified name of the
9097 /// type.
9098 ///
9099 /// @param internal set to true if the call is intended for an
9100 /// internal use (for technical use inside the library itself), false
9101 /// otherwise. If you don't know what this is for, then set it to
9102 /// false.
9103 ///
9104 /// @return a copy of the type name if the type has a name, or the
9105 /// empty string if it does not.
9106 interned_string
9107 get_type_name(const type_base* t, bool qualified, bool internal)
9108 {
9109  const decl_base* d = dynamic_cast<const decl_base*>(t);
9110  if (!d)
9111  {
9112  const function_type* fn_type = is_function_type(t);
9113  ABG_ASSERT(fn_type);
9114  return fn_type->get_cached_name(internal);
9115  }
9116 
9117  const environment&env = d->get_environment();
9118 
9119  // All anonymous types of a given kind get to have the same internal
9120  // name for internal purpose. This to allow them to be compared
9121  // among themselves during type canonicalization.
9122  if (internal)
9123  {
9124  if (d->get_is_anonymous())
9125  {
9126  string r;
9127  r += get_generic_anonymous_internal_type_name(d);
9128  return t->get_environment().intern(r);
9129  }
9130 
9131  if (is_typedef(t))
9132  return d->get_name();
9133 
9134  if (qualified)
9135  return d->get_qualified_name(internal);
9136 
9137  return env.intern(get_internal_integral_type_name(t));
9138  }
9139 
9140  if (d->get_is_anonymous())
9141  {
9142  if (is_class_or_union_type(t) || is_enum_type(t))
9143  return env.intern
9145  /*one_line=*/true,
9146  internal, qualified));
9147  }
9148 
9149  if (qualified)
9150  return d->get_qualified_name(internal);
9151  return d->get_name();
9152 }
9153 
9154 /// Get the name of a given type and return a copy of it.
9155 ///
9156 /// @param t the type to consider.
9157 ///
9158 /// @param qualified if true then return the qualified name of the
9159 /// type.
9160 ///
9161 /// @param internal set to true if the call is intended for an
9162 /// internal use (for technical use inside the library itself), false
9163 /// otherwise. If you don't know what this is for, then set it to
9164 /// false.
9165 ///
9166 /// @return a copy of the type name if the type has a name, or the
9167 /// empty string if it does not.
9169 get_type_name(const type_base& t, bool qualified, bool internal)
9170 {return get_type_name(&t, qualified, internal);}
9171 
9172 /// Get the name of the pointer to a given type.
9173 ///
9174 /// @param pointed_to_type the pointed-to-type to consider.
9175 ///
9176 /// @param qualified this is true if the resulting name should be of a
9177 /// pointer to a *fully-qualified* pointed-to-type.
9178 ///
9179 /// @param internal true if the name is for libabigail-internal
9180 /// purposes.
9181 ///
9182 /// @return the name (string representation) of the pointer.
9185  bool qualified, bool internal)
9186 {
9187  const environment& env = pointed_to_type.get_environment();
9188  string tn = get_type_name(pointed_to_type, qualified, internal);
9189  tn = tn + "*";
9190 
9191  return env.intern(tn);
9192 }
9193 
9194 /// Get the name of the reference to a given type.
9195 ///
9196 /// @param pointed_to_type the pointed-to-type to consider.
9197 ///
9198 /// @param qualified this is true if the resulting name should be of a
9199 /// reference to a *fully-qualified* pointed-to-type.
9200 ///
9201 /// @param internal true if the name is for libabigail-internal
9202 /// purposes.
9203 ///
9204 /// @return the name (string representation) of the reference.
9207  bool lvalue_reference,
9208  bool qualified, bool internal)
9209 {
9210  const environment& env = pointed_to_type.get_environment();
9211 
9212  string name = get_type_name(pointed_to_type, qualified, internal);
9213  if (lvalue_reference)
9214  name = name + "&";
9215  else
9216  name = name + "&&";
9217 
9218  return env.intern(name);
9219 }
9220 
9221 /// Get the name of a qualified type, given the underlying type and
9222 /// its qualifiers.
9223 ///
9224 /// @param underlying_type the underlying type to consider.
9225 ///
9226 /// @param quals the CV qualifiers of the name.
9227 ///
9228 /// @param qualified true if we should consider the fully qualified
9229 /// name of @p underlying_type.
9230 ///
9231 /// @param internal true if the result is to be used for
9232 /// libabigail-internal purposes.
9233 ///
9234 /// @return the name (string representation) of the qualified type.
9236 get_name_of_qualified_type(const type_base_sptr& underlying_type,
9237  qualified_type_def::CV quals,
9238  bool qualified, bool internal)
9239 {
9240  const environment& env = underlying_type->get_environment();
9241 
9242  string quals_repr = get_string_representation_of_cv_quals(quals);
9243  string name = get_type_name(underlying_type, qualified, internal);
9244 
9245  if (quals_repr.empty() && internal)
9246  // We are asked to return the internal name, that might be used
9247  // for type canonicalization. For that canonicalization, we need
9248  // to make a difference between a no-op qualified type which
9249  // underlying type is foo (the qualified type is named "none
9250  // foo"), and the name of foo, which is just "foo".
9251  //
9252  // Please remember that this has to be kept in sync with what is
9253  // done in die_qualified_name, in abg-dwarf-reader.cc. So if you
9254  // change this code here, please change that code there too.
9255  quals_repr = "";
9256 
9257  if (!quals_repr.empty())
9258  {
9259  if (is_pointer_type(peel_qualified_type(underlying_type))
9260  || is_reference_type(peel_qualified_type(underlying_type)))
9261  {
9262  name += " ";
9263  name += quals_repr;
9264  }
9265  else
9266  name = quals_repr + " " + name;
9267  }
9268 
9269  return env.intern(name);
9270 }
9271 
9272 /// Get the name of a given function type and return a copy of it.
9273 ///
9274 /// @param fn_type the function type to consider.
9275 ///
9276 /// @param internal set to true if the call is intended for an
9277 /// internal use (for technical use inside the library itself), false
9278 /// otherwise. If you don't know what this is for, then set it to
9279 /// false.
9280 ///
9281 /// @return a copy of the function type name
9284  bool internal)
9285 {return get_function_type_name(fn_type.get(), internal);}
9286 
9287 /// Get the name of a given function type and return a copy of it.
9288 ///
9289 /// @param fn_type the function type to consider.
9290 ///
9291 /// @param internal set to true if the call is intended for an
9292 /// internal use (for technical use inside the library itself), false
9293 /// otherwise. If you don't know what this is for, then set it to
9294 /// false.
9295 ///
9296 /// @return a copy of the function type name
9299  bool internal)
9300 {
9301  ABG_ASSERT(fn_type);
9302 
9303  if (const method_type* method = is_method_type(fn_type))
9304  return get_method_type_name(method, internal);
9305 
9306  return get_function_type_name(*fn_type, internal);
9307 }
9308 
9309 /// Get the name of a given function type and return a copy of it.
9310 ///
9311 /// @param fn_type the function type to consider.
9312 ///
9313 /// @param internal set to true if the call is intended for an
9314 /// internal use (for technical use inside the library itself), false
9315 /// otherwise. If you don't know what this is for, then set it to
9316 /// false.
9317 ///
9318 /// @return a copy of the function type name
9321  bool internal)
9322 {
9323  std::ostringstream o;
9324  // When the function name is used for internal purposes (e.g, for
9325  // canonicalization), we want its representation to stay the same,
9326  // regardless of typedefs. So let's strip typedefs from the return
9327  // type.
9328  type_base_sptr return_type =
9329  internal
9330  ? peel_typedef_type(fn_type.get_return_type())
9331  : fn_type.get_return_type();
9332  const environment& env = fn_type.get_environment();
9333 
9334  o << get_pretty_representation(return_type, internal) << " ";
9335  stream_pretty_representation_of_fn_parms(fn_type, o,
9336  /*qualified=*/true,
9337  internal);
9338  return env.intern(o.str());
9339 }
9340 
9341 /// Get the ID of a function, or, if the ID can designate several
9342 /// different functions, get its pretty representation.
9343 ///
9344 /// @param fn the function to consider
9345 ///
9346 /// @return the function ID of pretty representation of @p fn.
9349 {
9350  ABG_ASSERT(fn);
9351 
9352  interned_string result = fn->get_environment().intern(fn->get_id());
9353 
9354  if (const corpus *c = fn->get_corpus())
9355  {
9357  c->get_exported_decls_builder();
9358  if (b->fn_id_maps_to_several_fns(fn))
9359  result = fn->get_environment().intern(fn->get_pretty_representation());
9360  }
9361 
9362  return result;
9363 }
9364 
9365 /// Get the name of a given method type and return a copy of it.
9366 ///
9367 /// @param fn_type the function type to consider.
9368 ///
9369 /// @param internal set to true if the call is intended for an
9370 /// internal use (for technical use inside the library itself), false
9371 /// otherwise. If you don't know what this is for, then set it to
9372 /// false.
9373 ///
9374 /// @return a copy of the function type name
9377  bool internal)
9378 {return get_method_type_name(fn_type.get(), internal);}
9379 
9380 /// Get the name of a given method type and return a copy of it.
9381 ///
9382 /// @param fn_type the function type to consider.
9383 ///
9384 /// @param internal set to true if the call is intended for an
9385 /// internal use (for technical use inside the library itself), false
9386 /// otherwise. If you don't know what this is for, then set it to
9387 /// false.
9388 ///
9389 /// @return a copy of the function type name
9392  bool internal)
9393 {
9394  if (fn_type)
9395  return get_method_type_name(*fn_type, internal);
9396 
9397  return interned_string();
9398 }
9399 
9400 /// Get the name of a given method type and return a copy of it.
9401 ///
9402 /// @param fn_type the function type to consider.
9403 ///
9404 /// @param internal set to true if the call is intended for an
9405 /// internal use (for technical use inside the library itself), false
9406 /// otherwise. If you don't know what this is for, then set it to
9407 /// false.
9408 ///
9409 /// @return a copy of the function type name
9412  bool internal)
9413 {
9414  std::ostringstream o;
9415  // When the function name is used for internal purposes (e.g, for
9416  // canonicalization), we want its representation to stay the same,
9417  // regardless of typedefs. So let's strip typedefs from the return
9418  // type.
9419  type_base_sptr return_type =
9420  internal
9421  ? peel_typedef_type(fn_type.get_return_type())
9422  : fn_type.get_return_type();
9423  const environment& env = fn_type.get_environment();
9424 
9425  if (return_type)
9426  o << return_type->get_cached_pretty_representation(internal);
9427  else
9428  // There are still some abixml files out there in which "void"
9429  // can be expressed as an empty type.
9430  o << "void";
9431 
9432  class_or_union_sptr class_type = fn_type.get_class_type();
9433  ABG_ASSERT(class_type);
9434 
9435  o << " (" << class_type->get_qualified_name(internal) << "::*) ";
9436  stream_pretty_representation_of_fn_parms(fn_type, o,
9437  /*qualified=*/true,
9438  internal);
9439 
9440  return env.intern(o.str());
9441 }
9442 
9443 /// Build and return a copy of the pretty representation of an ABI
9444 /// artifact that could be either a type of a decl.
9445 ///
9446 /// param tod the ABI artifact to consider.
9447 ///
9448 /// @param internal set to true if the call is intended for an
9449 /// internal use (for technical use inside the library itself), false
9450 /// otherwise. If you don't know what this is for, then set it to
9451 /// false.
9452 ///
9453 /// @return a copy of the pretty representation of an ABI artifact
9454 /// that could be either a type of a decl.
9455 string
9457 {
9458  string result;
9459 
9460  if (type_base* t = is_type(const_cast<type_or_decl_base*>(tod)))
9461  result = get_pretty_representation(t, internal);
9462  else if (decl_base* d = is_decl(const_cast<type_or_decl_base*>(tod)))
9463  result = get_pretty_representation(d, internal);
9464  else
9465  // We should never reach this point
9466  abort();
9467 
9468  return result;
9469 }
9470 
9471 /// Build and return a copy of the pretty representation of an ABI
9472 /// artifact that could be either a type of a decl.
9473 ///
9474 /// param tod the ABI artifact to consider.
9475 ///
9476 /// @param internal set to true if the call is intended for an
9477 /// internal use (for technical use inside the library itself), false
9478 /// otherwise. If you don't know what this is for, then set it to
9479 /// false.
9480 ///
9481 /// @return a copy of the pretty representation of an ABI artifact
9482 /// that could be either a type of a decl.
9483 string
9485 {return get_pretty_representation(tod.get(), internal);}
9486 
9487 /// Get a copy of the pretty representation of a decl.
9488 ///
9489 /// @param d the decl to consider.
9490 ///
9491 /// @param internal set to true if the call is intended for an
9492 /// internal use (for technical use inside the library itself), false
9493 /// otherwise. If you don't know what this is for, then set it to
9494 /// false.
9495 ///
9496 /// @return the pretty representation of the decl.
9497 string
9498 get_pretty_representation(const decl_base* d, bool internal)
9499 {
9500  if (!d)
9501  return "";
9502  return d->get_pretty_representation(internal);
9503 }
9504 
9505 /// Get a copy of the pretty representation of a type.
9506 ///
9507 /// @param d the type to consider.
9508 ///
9509 /// @param internal set to true if the call is intended for an
9510 /// internal use (for technical use inside the library itself), false
9511 /// otherwise. If you don't know what this is for, then set it to
9512 /// false.
9513 ///
9514 /// @return the pretty representation of the type.
9515 string
9516 get_pretty_representation(const type_base* t, bool internal)
9517 {
9518  if (!t)
9519  return "void";
9520  if (const function_type* fn_type = is_function_type(t))
9521  return get_pretty_representation(fn_type, internal);
9522 
9523  const decl_base* d = get_type_declaration(t);
9524  ABG_ASSERT(d);
9525  return get_pretty_representation(d, internal);
9526 }
9527 
9528 /// Get a copy of the pretty representation of a decl.
9529 ///
9530 /// @param d the decl to consider.
9531 ///
9532 /// @param internal set to true if the call is intended for an
9533 /// internal use (for technical use inside the library itself), false
9534 /// otherwise. If you don't know what this is for, then set it to
9535 /// false.
9536 ///
9537 /// @return the pretty representation of the decl.
9538 string
9539 get_pretty_representation(const decl_base_sptr& d, bool internal)
9540 {return get_pretty_representation(d.get(), internal);}
9541 
9542 /// Get a copy of the pretty representation of a type.
9543 ///
9544 /// @param d the type to consider.
9545 ///
9546 /// @param internal set to true if the call is intended for an
9547 /// internal use (for technical use inside the library itself), false
9548 /// otherwise. If you don't know what this is for, then set it to
9549 /// false.
9550 ///
9551 /// @return the pretty representation of the type.
9552 string
9553 get_pretty_representation(const type_base_sptr& t, bool internal)
9554 {return get_pretty_representation(t.get(), internal);}
9555 
9556 /// Get the pretty representation of a function type.
9557 ///
9558 /// @param fn_type the function type to consider.
9559 ///
9560 /// @param internal set to true if the call is intended for an
9561 /// internal use (for technical use inside the library itself), false
9562 /// otherwise. If you don't know what this is for, then set it to
9563 /// false.
9564 ///
9565 /// @return the string represenation of the function type.
9566 string
9568  bool internal)
9569 {return get_pretty_representation(fn_type.get(), internal);}
9570 
9571 /// Get the pretty representation of a function type.
9572 ///
9573 /// @param fn_type the function type to consider.
9574 ///
9575 /// @param internal set to true if the call is intended for an
9576 /// internal use (for technical use inside the library itself), false
9577 /// otherwise. If you don't know what this is for, then set it to
9578 /// false.
9579 ///
9580 /// @return the string represenation of the function type.
9581 string
9582 get_pretty_representation(const function_type* fn_type, bool internal)
9583 {
9584  if (!fn_type)
9585  return "void";
9586 
9587  if (const method_type* method = is_method_type(fn_type))
9588  return get_pretty_representation(method, internal);
9589 
9590  return get_pretty_representation(*fn_type, internal);
9591 }
9592 
9593 /// Get the pretty representation of a function type.
9594 ///
9595 /// @param fn_type the function type to consider.
9596 ///
9597 /// @param internal set to true if the call is intended for an
9598 /// internal use (for technical use inside the library itself), false
9599 /// otherwise. If you don't know what this is for, then set it to
9600 /// false.
9601 ///
9602 /// @return the string represenation of the function type.
9603 string
9604 get_pretty_representation(const function_type& fn_type, bool internal)
9605 {
9606  std::ostringstream o;
9607  o << "function type " << get_function_type_name(fn_type, internal);
9608  return o.str();
9609 }
9610 
9611 /// Get the pretty representation of a method type.
9612 ///
9613 /// @param method the method type to consider.
9614 ///
9615 /// @param internal set to true if the call is intended for an
9616 /// internal use (for technical use inside the library itself), false
9617 /// otherwise. If you don't know what this is for, then set it to
9618 /// false.
9619 ///
9620 /// @return the string represenation of the method type.
9621 string
9622 get_pretty_representation(const method_type& method, bool internal)
9623 {
9624  std::ostringstream o;
9625  o << "method type " << get_method_type_name(method, internal);
9626  return o.str();
9627 }
9628 
9629 /// Get the pretty representation of a method type.
9630 ///
9631 /// @param method the method type to consider.
9632 ///
9633 /// @param internal set to true if the call is intended for an
9634 /// internal use (for technical use inside the library itself), false
9635 /// otherwise. If you don't know what this is for, then set it to
9636 /// false.
9637 ///
9638 /// @return the string represenation of the method type.
9639 string
9640 get_pretty_representation(const method_type* method, bool internal)
9641 {
9642  if (!method)
9643  return "void";
9644  return get_pretty_representation(*method, internal);
9645 }
9646 
9647 /// Get the pretty representation of a method type.
9648 ///
9649 /// @param method the method type to consider.
9650 ///
9651 /// @param internal set to true if the call is intended for an
9652 /// internal use (for technical use inside the library itself), false
9653 /// otherwise. If you don't know what this is for, then set it to
9654 /// false.
9655 ///
9656 /// @return the string represenation of the method type.
9657 string
9658 get_pretty_representation(const method_type_sptr method, bool internal)
9659 {return get_pretty_representation(method.get(), internal);}
9660 
9661 /// Get the flat representation of an instance of @ref class_or_union
9662 /// type.
9663 ///
9664 /// The flat representation of a given @ref class_or_union type is the
9665 /// actual definition of the type, for instance:
9666 ///
9667 /// struct foo {int a; char b;}
9668 ///
9669 ///@param cou the instance of @ref class_or_union to consider.
9670 ///
9671 ///@param indent the identation spaces to use in the representation.
9672 ///
9673 ///@param one_line if true, then the flat representation stands on one
9674 ///line. Otherwise, it stands on multiple lines.
9675 ///
9676 ///@return the resulting flat representation.
9677 string
9679  const string& indent,
9680  bool one_line,
9681  bool internal,
9682  bool qualified_names)
9683 {
9684  string repr;
9685  string local_indent = " ";
9686 
9687  if (class_decl* clazz = is_class_type(&cou))
9688  {
9689  repr = indent;
9690  if (!internal && clazz->is_struct())
9691  repr += "struct";
9692  else
9693  repr += "class";
9694  }
9695  else if (is_union_type(cou))
9696  repr = indent + "union";
9697  else
9698  return "";
9699 
9700  repr += " ";
9701 
9702  string name = cou.get_qualified_name();
9703 
9704  if (!cou.get_is_anonymous())
9705  repr += name;
9706 
9707  repr += "{";
9708 
9709  if (!one_line)
9710  repr += "\n";
9711 
9712  string real_indent;
9713  const class_or_union::data_members &dmems = cou.get_data_members();
9714  for (class_or_union::data_members::const_iterator dm = dmems.begin();
9715  dm != dmems.end();
9716  ++dm)
9717  {
9718  if (dm != dmems.begin())
9719  {
9720  if (one_line)
9721  real_indent = " ";
9722  else
9723  real_indent = "\n" + indent + local_indent;
9724  }
9725 
9727  repr +=
9730  real_indent, one_line, internal, qualified_names);
9731  else
9732  {
9733  if (one_line)
9734  {
9735  if (dm != dmems.begin())
9736  repr += real_indent;
9737  repr += (*dm)->get_pretty_representation(internal,
9738  qualified_names);
9739  }
9740  else
9741  repr +=
9742  real_indent+ (*dm)->get_pretty_representation(internal,
9743  qualified_names);
9744  }
9745  repr += ";";
9746  }
9747 
9748  if (one_line)
9749  repr += "}";
9750  else
9751  repr += indent + "}";
9752 
9753  return repr;
9754 }
9755 
9756 /// Get the flat representation of an instance of @ref class_or_union
9757 /// type.
9758 ///
9759 /// The flat representation of a given @ref class_or_union type is the
9760 /// actual definition of the type, for instance:
9761 ///
9762 /// struct foo {int a; char b;}
9763 ///
9764 ///@param cou the instance of @ref class_or_union to consider.
9765 ///
9766 ///@param indent the identation spaces to use in the representation.
9767 ///
9768 ///@param one_line if true, then the flat representation stands on one
9769 ///line. Otherwise, it stands on multiple lines.
9770 ///
9771 ///@return the resulting flat representation.
9772 string
9774  const string& indent,
9775  bool one_line,
9776  bool internal,
9777  bool qualified_names)
9778 {
9779  if (cou)
9780  return get_class_or_union_flat_representation(*cou, indent, one_line,
9781  internal, qualified_names);
9782  return "";
9783 }
9784 
9785 /// Get the flat representation of an instance of @ref class_or_union
9786 /// type.
9787 ///
9788 /// The flat representation of a given @ref class_or_union type is the
9789 /// actual definition of the type, for instance:
9790 ///
9791 /// struct foo {int a; char b;}
9792 ///
9793 ///@param cou the instance of @ref class_or_union to consider.
9794 ///
9795 ///@param indent the identation spaces to use in the representation.
9796 ///
9797 ///@param one_line if true, then the flat representation stands on one
9798 ///line. Otherwise, it stands on multiple lines.
9799 ///
9800 ///@return the resulting flat representation.
9801 string
9802 get_class_or_union_flat_representation(const class_or_union_sptr& cou,
9803  const string& indent,
9804  bool one_line,
9805  bool internal,
9806  bool qualified_names)
9807 {return get_class_or_union_flat_representation(cou.get(),
9808  indent,
9809  one_line,
9810  internal,
9811  qualified_names);}
9812 
9813 /// Get the flat representation of an instance of @ref enum_type_decl
9814 /// type.
9815 ///
9816 /// The flat representation of a given @ref enum_type_decl type is the
9817 /// actual definition of the type, for instance:
9818 ///
9819 /// enum {E_0 =0, E_1 = 1}
9820 ///
9821 ///@param enum_type the enum type to consider.
9822 ///
9823 ///@param indent the identation spaces to use in the representation.
9824 ///
9825 ///@param one_line if true, then the flat representation stands on one
9826 ///line. Otherwise, it stands on multiple lines.
9827 ///
9828 ///@param qualified_names use qualified names when applicable.
9829 ///Typically, if this is true, the name of the enum is going to be
9830 ///qualified.
9831 ///
9832 ///@return the resulting flat representation.
9833 string
9835  const string& indent, bool one_line,
9836  bool qualified_names)
9837 {
9838  string repr;
9839  std::ostringstream o;
9840  string local_indent = " ";
9841 
9842  repr = indent + "enum ";
9843 
9844  if (!enum_type.get_is_anonymous())
9845  o << (qualified_names
9846  ? enum_type.get_qualified_name()
9847  : enum_type.get_name()) + " ";
9848 
9849  o << "{";
9850 
9851  if (!one_line)
9852  o << "\n";
9853 
9854  for (const auto &enumerator : enum_type.get_sorted_enumerators())
9855  {
9856  if (!one_line)
9857  o << "\n" + indent;
9858 
9859  o << enumerator.get_name() + "=" << enumerator.get_value() << ", ";
9860  }
9861 
9862  if (!one_line)
9863  o << "\n" + indent << "}";
9864  else
9865  o << "}";
9866 
9867  repr =o.str();
9868 
9869  return repr;
9870 }
9871 
9872 /// Get the flat representation of an instance of @ref enum_type_decl
9873 /// type.
9874 ///
9875 /// The flat representation of a given @ref enum_type_decl type is the
9876 /// actual definition of the type, for instance:
9877 ///
9878 /// enum {E_0 =0, E_1 = 1}
9879 ///
9880 ///@param enum_type the enum type to consider.
9881 ///
9882 ///@param indent the identation spaces to use in the representation.
9883 ///
9884 ///@param one_line if true, then the flat representation stands on one
9885 ///line. Otherwise, it stands on multiple lines.
9886 ///
9887 ///@param qualified_names use qualified names when applicable.
9888 ///Typically, if this is true, the name of the enum is going to be
9889 ///qualified.
9890 ///
9891 ///@return the resulting flat representation.
9892 string
9894  const string& indent, bool one_line,
9895  bool qualified_names)
9896 {
9897  if (!enum_type)
9898  return "";
9899 
9900  return get_enum_flat_representation(*enum_type, indent,
9901  one_line, qualified_names);
9902 }
9903 
9904 /// Get the flat representation of an instance of @ref enum_type_decl
9905 /// type.
9906 ///
9907 /// The flat representation of a given @ref enum_type_decl type is the
9908 /// actual definition of the type, for instance:
9909 ///
9910 /// enum {E_0 =0, E_1 = 1}
9911 ///
9912 ///@param enum_type the enum type to consider.
9913 ///
9914 ///@param indent the identation spaces to use in the representation.
9915 ///
9916 ///@param one_line if true, then the flat representation stands on one
9917 ///line. Otherwise, it stands on multiple lines.
9918 ///
9919 ///@param qualified_names use qualified names when applicable.
9920 ///Typically, if this is true, the name of the enum is going to be
9921 ///qualified.
9922 ///
9923 ///@return the resulting flat representation.
9924 string
9926  const string& indent, bool one_line,
9927  bool qualified_names)
9928 {
9929  return get_enum_flat_representation(enum_type.get(),
9930  indent, one_line,
9931  qualified_names);
9932 }
9933 
9934 /// Get the flat representation of an instance of @ref enum_type_decl
9935 /// type.
9936 ///
9937 /// The flat representation of a given @ref enum_type_decl type is the
9938 /// actual definition of the type, for instance:
9939 ///
9940 /// enum {E_0 =0, E_1 = 1}
9941 ///
9942 ///@param enum_type the enum type to consider.
9943 ///
9944 ///@param indent the identation spaces to use in the representation.
9945 ///
9946 ///@param one_line if true, then the flat representation stands on one
9947 ///line. Otherwise, it stands on multiple lines.
9948 ///
9949 ///@param qualified_names use qualified names when applicable.
9950 ///Typically, if this is true, the name of the enum is going to be
9951 ///qualified.
9952 ///
9953 ///@return the resulting flat representation.
9954 string
9956  const string& indent,
9957  bool one_line,
9958  bool internal,
9959  bool qualified_name)
9960 
9961 {
9962  string repr;
9963  if (const class_or_union* cou = is_class_or_union_type(&coe))
9964  repr = get_class_or_union_flat_representation(cou, indent, one_line,
9965  internal, qualified_name);
9966  else if (const enum_type_decl* enom = is_enum_type(&coe))
9967  repr = get_enum_flat_representation(*enom, indent, one_line, qualified_name);
9968 
9969  return repr;
9970 }
9971 
9972 /// Get the textual representation of a type for debugging purposes.
9973 ///
9974 /// If the type is a class/union, this shows the data members, virtual
9975 /// member functions, size, pointer value of its canonical type, etc.
9976 /// Otherwise, this just shows the name of the artifact as returned by
9977 /// type_or_decl_base:get_pretty_representation().
9978 ///
9979 /// @param artifact the artifact to show a debugging representation of.
9980 ///
9981 /// @return a debugging string representation of @p artifact.
9982 string
9984 {
9985  if (!artifact)
9986  return string("");
9987 
9988  class_or_union * c = is_class_or_union_type(artifact);
9989  if (c)
9990  {
9991  class_decl *clazz = is_class_type(c);
9992  string name = c->get_qualified_name();
9993  std::ostringstream o;
9994  if (clazz)
9995  {
9996  if (clazz->is_struct())
9997  o << "struct ";
9998  else
9999  o << "class ";
10000  }
10001  else if (is_union_type(c))
10002  o << "union ";
10003  o << name;
10004 
10005  if (clazz)
10006  {
10007  if (!clazz->get_base_specifiers().empty())
10008  o << " :" << std::endl;
10009  for (auto &b : clazz->get_base_specifiers())
10010  {
10011  o << " ";
10012  if (b->get_is_virtual())
10013  o << "virtual ";
10014  o << b->get_base_class()->get_qualified_name()
10015  << std::endl;
10016  }
10017  }
10018  o << std::endl
10019  << "{"
10020  << " // size in bits: " << c->get_size_in_bits() << "\n"
10021  << " // is-declaration-only: " << c->get_is_declaration_only() << "\n"
10022  << " // definition point: " << get_natural_or_artificial_location(c).expand() << "\n"
10023  << " // translation unit: " << c->get_translation_unit()->get_absolute_path() << std::endl
10024  << " // @: " << std::hex << is_type(c)
10025  << ", @canonical: " << c->get_canonical_type().get() << std::dec
10026  << "\n\n";
10027 
10028  for (auto m : c->get_data_members())
10029  {
10030  type_base_sptr t = m->get_type();
10032 
10033  o << " "
10034  << m->get_pretty_representation(/*internal=*/false,
10035  /*qualified=*/false)
10036  << ";";
10037 
10038  if (t && t->get_canonical_type())
10039  o << " // uses canonical type '@"
10040  << std::hex << t->get_canonical_type().get() << std::dec;
10041 
10042  o << "'" << std::endl;
10043  }
10044 
10045  if (clazz && clazz->has_vtable())
10046  {
10047  o << " // virtual member functions\n\n";
10048  for (auto f : clazz->get_virtual_mem_fns())
10049  o << " " << f->get_pretty_representation(/*internal=*/false,
10050  /*qualified=*/false)
10051  << ";" << std::endl;
10052  }
10053 
10054  o << "};" << std::endl;
10055 
10056  return o.str();
10057  }
10058  else if (const enum_type_decl* e = is_enum_type(artifact))
10059  {
10060  string name = e->get_qualified_name();
10061  std::ostringstream o;
10062  o << "enum " << name
10063  << " : "
10064  << e->get_underlying_type()->get_pretty_representation(/*internal=*/false,
10065  true)
10066  << "\n"
10067  << "{\n"
10068  << " // size in bits: " << e->get_size_in_bits() << "\n"
10069  << " // is-declaration-only: " << e->get_is_declaration_only() << "\n"
10070  << " // definition point: " << get_natural_or_artificial_location(e).expand() << "\n"
10071  << " // translation unit: "
10072  << e->get_translation_unit()->get_absolute_path() << "\n"
10073  << " // @: " << std::hex << is_type(e)
10074  << ", @canonical: " << e->get_canonical_type().get() << std::dec
10075  << "\n\n";
10076 
10077  for (const auto &enom : e->get_enumerators())
10078  o << " " << enom.get_name() << " = " << enom.get_value() << ",\n";
10079 
10080  o << "};\n";
10081 
10082  return o.str();
10083  }
10084  return artifact->get_pretty_representation(/*internal=*/true,
10085  /*qualified=*/true);
10086 }
10087 
10088 /// Get a given data member, referred to by its name, of a class type.
10089 ///
10090 /// @param clazz the class to consider.
10091 ///
10092 /// @param member_name name of the data member to get.
10093 ///
10094 /// @return the resulting data member or nullptr if none was found.
10096 get_data_member(class_or_union *clazz, const char* member_name)
10097 {
10098  if (!clazz)
10099  return var_decl_sptr();
10100  return clazz->find_data_member(member_name);
10101 }
10102 
10103 /// Get a given data member, referred to by its name, of a class type.
10104 ///
10105 /// @param clazz the class to consider.
10106 ///
10107 /// @param member_name name of the data member to get.
10108 ///
10109 /// @return the resulting data member or nullptr if none was found.
10111 get_data_member(type_base *clazz, const char* member_name)
10112 {return get_data_member(is_class_or_union_type(clazz), member_name);}
10113 
10114 /// Get the non-artificial (natural) location of a decl.
10115 ///
10116 /// If the decl doesn't have a natural location then return its
10117 /// artificial one.
10118 ///
10119 /// @param decl the decl to consider.
10120 ///
10121 /// @return the natural location @p decl if it has one; otherwise,
10122 /// return its artificial one.
10123 const location&
10125 {
10126  ABG_ASSERT(decl);
10127 
10128  if (decl->get_location())
10129  return decl->get_location();
10130  return decl->get_artificial_location();
10131 }
10132 
10133 /// Get the artificial location of a decl.
10134 ///
10135 /// If the decl doesn't have an artificial location then return its
10136 /// natural one.
10137 ///
10138 /// @param decl the decl to consider.
10139 ///
10140 /// @return the artificial location @p decl if it has one; otherwise,
10141 /// return its natural one.
10142 const location&
10144 {
10145  ABG_ASSERT(decl);
10146 
10147  if (decl->has_artificial_location())
10148  return decl->get_artificial_location();
10149  return decl->get_location();
10150 }
10151 
10152 /// Emit a textual representation of an artifact to std error stream
10153 /// for debugging purposes.
10154 ///
10155 /// This is useful to invoke from within a command line debugger like
10156 /// GDB to help make sense of a given ABI artifact.
10157 ///
10158 /// @param artifact the ABI artifact to emit the debugging
10159 /// representation for.
10160 ///
10161 /// @return the artifact @p artifact.
10163 debug(const type_or_decl_base* artifact)
10164 {
10165  std::cerr << get_debug_representation(artifact) << std::endl;
10166  return const_cast<type_or_decl_base*>(artifact);
10167 }
10168 
10169 /// Emit a textual representation of an artifact to std error stream
10170 /// for debugging purposes.
10171 ///
10172 /// This is useful to invoke from within a command line debugger like
10173 /// GDB to help make sense of a given ABI artifact.
10174 ///
10175 /// @param artifact the ABI artifact to emit the debugging
10176 /// representation for.
10177 ///
10178 /// @return the artifact @p artifact.
10179 type_base*
10180 debug(const type_base* artifact)
10181 {
10182  debug(static_cast<const type_or_decl_base*>(artifact));
10183  return const_cast<type_base*>(artifact);
10184 }
10185 
10186 /// Emit a textual representation of an artifact to std error stream
10187 /// for debugging purposes.
10188 ///
10189 /// This is useful to invoke from within a command line debugger like
10190 /// GDB to help make sense of a given ABI artifact.
10191 ///
10192 /// @param artifact the ABI artifact to emit the debugging
10193 /// representation for.
10194 ///
10195 /// @return the artifact @p artifact.
10196 decl_base*
10197 debug(const decl_base* artifact)
10198 {
10199  debug(static_cast<const type_or_decl_base*>(artifact));
10200  return const_cast<decl_base*>(artifact);
10201 }
10202 
10203 /// Test if two ABI artifacts are equal.
10204 ///
10205 /// This can be useful when used from the command line of a debugger
10206 /// like GDB.
10207 ///
10208 /// @param l the first ABI artifact to consider in the comparison.
10209 ///
10210 /// @param r the second ABI artifact to consider in the comparison.
10211 ///
10212 /// @return true iff @p l equals @p r.
10213 bool
10215 {
10216  if (!!l != !!r)
10217  return false;
10218  if (!l && !r)
10219  return true;
10220 
10221  return (*l == *r);
10222 }
10223 
10224 /// Emit a trace of a comparison operand stack.
10225 ///
10226 /// @param vect the operand stack to emit the trace for.
10227 ///
10228 /// @param o the output stream to emit the trace to.
10229 static void
10230 debug_comp_vec(const vector<const type_base*>& vect, std::ostringstream& o)
10231 {
10232  for (auto t : vect)
10233  {
10234  o << "|" << t->get_pretty_representation()
10235  << "@" << std::hex << t << std::dec;
10236  }
10237  if (!vect.empty())
10238  o << "|";
10239 }
10240 
10241 /// Construct a trace of the two comparison operand stacks.
10242 ///
10243 /// @param the environment in which the comparison operand stacks are.
10244 ///
10245 /// @return a string representing the trace.
10246 static string
10247 print_comp_stack(const environment& env)
10248 {
10249  std::ostringstream o;
10250  o << "left-operands: ";
10251  debug_comp_vec(env.priv_->left_type_comp_operands_, o);
10252  o << "\n" << "right-operands: ";
10253  debug_comp_vec(env.priv_->right_type_comp_operands_, o);
10254  o << "\n";
10255  return o.str();
10256 }
10257 
10258 /// Emit a trace of the two comparison operands stack on the standard
10259 /// error stream.
10260 ///
10261 /// @param env the environment the comparison operands stack belong
10262 /// to.
10263 void
10265 {
10266  std::cerr << print_comp_stack(env);
10267  std::cerr << std::endl;
10268 }
10269 
10270 /// By looking at the language of the TU a given ABI artifact belongs
10271 /// to, test if the ONE Definition Rule should apply.
10272 ///
10273 /// To date, it applies to c++, java and ada.
10274 ///
10275 /// @param artifact the ABI artifact to consider.
10276 ///
10277 /// @return true iff the One Definition Rule should apply.
10278 bool
10280 {
10282  artifact.get_translation_unit()->get_language();
10283 
10284  if (is_cplus_plus_language(l)
10285  || is_java_language(l)
10286  || is_ada_language(l))
10287  return true;
10288 
10289  return false;
10290 }
10291 
10292 /// Get the declaration for a given type.
10293 ///
10294 /// @param t the type to consider.
10295 ///
10296 /// @return the declaration for the type to return.
10297 const decl_base*
10299 {return dynamic_cast<const decl_base*>(t);}
10300 
10301 /// Get the declaration for a given type.
10302 ///
10303 /// @param t the type to consider.
10304 ///
10305 /// @return the declaration for the type to return.
10306 decl_base*
10308 {return dynamic_cast<decl_base*>(t);}
10309 
10310 /// Get the declaration for a given type.
10311 ///
10312 /// @param t the type to consider.
10313 ///
10314 /// @return the declaration for the type to return.
10315 decl_base_sptr
10316 get_type_declaration(const type_base_sptr t)
10317 {return dynamic_pointer_cast<decl_base>(t);}
10318 
10319 /// Test if two types are equal modulo a typedef.
10320 ///
10321 /// Type A and B are compatible if
10322 ///
10323 /// - A and B are equal
10324 /// - or if one type is a typedef of the other one.
10325 ///
10326 /// @param type1 the first type to consider.
10327 ///
10328 /// @param type2 the second type to consider.
10329 ///
10330 /// @return true iff @p type1 and @p type2 are compatible.
10331 bool
10332 types_are_compatible(const type_base_sptr type1,
10333  const type_base_sptr type2)
10334 {
10335  if (!type1 || !type2)
10336  return false;
10337 
10338  if (type1 == type2)
10339  return true;
10340 
10341  // Normally we should strip typedefs entirely, but this is
10342  // potentially costly, especially on binaries with huge changesets
10343  // like the Linux Kernel. So we just get the leaf types for now.
10344  //
10345  // Maybe there should be an option by which users accepts to pay the
10346  // CPU usage toll in exchange for finer filtering?
10347 
10348  // type_base_sptr t1 = strip_typedef(type1);
10349  // type_base_sptr t2 = strip_typedef(type2);
10350 
10351  type_base_sptr t1 = peel_typedef_type(type1);
10352  type_base_sptr t2 = peel_typedef_type(type2);
10353 
10354  return t1 == t2;
10355 }
10356 
10357 /// Test if two types are equal modulo a typedef.
10358 ///
10359 /// Type A and B are compatible if
10360 ///
10361 /// - A and B are equal
10362 /// - or if one type is a typedef of the other one.
10363 ///
10364 /// @param type1 the declaration of the first type to consider.
10365 ///
10366 /// @param type2 the declaration of the second type to consider.
10367 ///
10368 /// @return true iff @p type1 and @p type2 are compatible.
10369 bool
10370 types_are_compatible(const decl_base_sptr d1,
10371  const decl_base_sptr d2)
10372 {return types_are_compatible(is_type(d1), is_type(d2));}
10373 
10374 /// Return the translation unit a declaration belongs to.
10375 ///
10376 /// @param decl the declaration to consider.
10377 ///
10378 /// @return the resulting translation unit, or null if the decl is not
10379 /// yet added to a translation unit.
10382 {return const_cast<translation_unit*>(decl.get_translation_unit());}
10383 
10384 /// Return the translation unit a declaration belongs to.
10385 ///
10386 /// @param decl the declaration to consider.
10387 ///
10388 /// @return the resulting translation unit, or null if the decl is not
10389 /// yet added to a translation unit.
10392 {return decl ? get_translation_unit(*decl) : 0;}
10393 
10394 /// Return the translation unit a declaration belongs to.
10395 ///
10396 /// @param decl the declaration to consider.
10397 ///
10398 /// @return the resulting translation unit, or null if the decl is not
10399 /// yet added to a translation unit.
10401 get_translation_unit(const shared_ptr<decl_base> decl)
10402 {return get_translation_unit(decl.get());}
10403 
10404 /// Tests whether if a given scope is the global scope.
10405 ///
10406 /// @param scope the scope to consider.
10407 ///
10408 /// @return true iff the current scope is the global one.
10409 bool
10411 {return !!dynamic_cast<const global_scope*>(&scope);}
10412 
10413 /// Tests whether if a given scope is the global scope.
10414 ///
10415 /// @param scope the scope to consider.
10416 ///
10417 /// @return the @ref global_scope* representing the scope @p scope or
10418 /// 0 if @p scope is not a global scope.
10419 const global_scope*
10421 {return dynamic_cast<const global_scope*>(scope);}
10422 
10423 /// Tests whether if a given scope is the global scope.
10424 ///
10425 /// @param scope the scope to consider.
10426 ///
10427 /// @return true iff the current scope is the global one.
10428 bool
10429 is_global_scope(const shared_ptr<scope_decl>scope)
10430 {return is_global_scope(scope.get());}
10431 
10432 /// Tests whether a given declaration is at global scope.
10433 ///
10434 /// @param decl the decl to consider.
10435 ///
10436 /// @return true iff decl is at global scope.
10437 bool
10439 {return (is_global_scope(decl.get_scope()));}
10440 
10441 /// Tests whether a given declaration is at global scope.
10442 ///
10443 /// @param decl the decl to consider.
10444 ///
10445 /// @return true iff decl is at global scope.
10446 bool
10447 is_at_global_scope(const decl_base_sptr decl)
10448 {return (decl && is_global_scope(decl->get_scope()));}
10449 
10450 /// Tests whether a given declaration is at global scope.
10451 ///
10452 /// @param decl the decl to consider.
10453 ///
10454 /// @return true iff decl is at global scope.
10455 bool
10457 {return is_at_global_scope(*decl);}
10458 
10459 /// Tests whether a given decl is at class scope.
10460 ///
10461 /// @param decl the decl to consider.
10462 ///
10463 /// @return true iff decl is at class scope.
10465 is_at_class_scope(const decl_base_sptr decl)
10466 {return is_at_class_scope(decl.get());}
10467 
10468 /// Tests whether a given decl is at class scope.
10469 ///
10470 /// @param decl the decl to consider.
10471 ///
10472 /// @return true iff decl is at class scope.
10475 {
10476  if (!decl)
10477  return 0;
10478 
10479  return is_at_class_scope(*decl);
10480 }
10481 
10482 /// Tests whether a given decl is at class scope.
10483 ///
10484 /// @param decl the decl to consider.
10485 ///
10486 /// @return true iff decl is at class scope.
10489 {
10490  scope_decl* scope = decl.get_scope();
10491  if (class_or_union* cl = is_class_type(scope))
10492  return cl;
10493  if (class_or_union* cl = is_union_type(scope))
10494  return cl;
10495  return 0;
10496 }
10497 
10498 /// Find a data member inside an anonymous data member.
10499 ///
10500 /// An anonymous data member has a type which is a class or union.
10501 /// This function looks for a data member inside the type of that
10502 /// anonymous data member.
10503 ///
10504 /// @param anon_dm the anonymous data member to consider.
10505 ///
10506 /// @param name the name of the data member to look for.
10509  const string& name)
10510 {
10511  const class_or_union* containing_class_or_union =
10513 
10514  if (!containing_class_or_union)
10515  return var_decl_sptr();
10516 
10517  var_decl_sptr result = containing_class_or_union->find_data_member(name);
10518  return result;
10519 }
10520 
10521 /// Tests whether a given decl is at template scope.
10522 ///
10523 /// Note that only template parameters , types that are compositions,
10524 /// and template patterns (function or class) can be at template scope.
10525 ///
10526 /// @param decl the decl to consider.
10527 ///
10528 /// @return true iff the decl is at template scope.
10529 bool
10530 is_at_template_scope(const shared_ptr<decl_base> decl)
10531 {return (decl && dynamic_cast<template_decl*>(decl->get_scope()));}
10532 
10533 /// Tests whether a decl is a template parameter.
10534 ///
10535 /// @param decl the decl to consider.
10536 ///
10537 /// @return true iff decl is a template parameter.
10538 bool
10539 is_template_parameter(const shared_ptr<decl_base> decl)
10540 {
10541  return (decl && (dynamic_pointer_cast<type_tparameter>(decl)
10542  || dynamic_pointer_cast<non_type_tparameter>(decl)
10543  || dynamic_pointer_cast<template_tparameter>(decl)));
10544 }
10545 
10546 /// Test whether a declaration is a @ref function_decl.
10547 ///
10548 /// @param d the declaration to test for.
10549 ///
10550 /// @return a shared pointer to @ref function_decl if @p d is a @ref
10551 /// function_decl. Otherwise, a nil shared pointer.
10554 {return dynamic_cast<function_decl*>(const_cast<type_or_decl_base*>(d));}
10555 
10556 /// Test whether a declaration is a @ref function_decl.
10557 ///
10558 /// @param d the declaration to test for.
10559 ///
10560 /// @return true if @p d is a function_decl.
10561 bool
10563 {return is_function_decl(&d);}
10564 
10565 /// Test whether a declaration is a @ref function_decl.
10566 ///
10567 /// @param d the declaration to test for.
10568 ///
10569 /// @return a shared pointer to @ref function_decl if @p d is a @ref
10570 /// function_decl. Otherwise, a nil shared pointer.
10573 {return dynamic_pointer_cast<function_decl>(d);}
10574 
10575 /// Test whether a declaration is a @ref function_decl.
10576 ///
10577 /// @param d the declaration to test for.
10578 ///
10579 /// @return a pointer to @ref function_decl if @p d is a @ref
10580 /// function_decl. Otherwise, a nil shared pointer.
10583 {
10584  return dynamic_cast<function_decl::parameter*>
10585  (const_cast<type_or_decl_base*>(tod));
10586 }
10587 
10588 /// Test whether an ABI artifact is a @ref function_decl.
10589 ///
10590 /// @param tod the declaration to test for.
10591 ///
10592 /// @return a pointer to @ref function_decl if @p d is a @ref
10593 /// function_decl. Otherwise, a nil shared pointer.
10596 {return dynamic_pointer_cast<function_decl::parameter>(tod);}
10597 
10598 /// Test if an ABI artifact is a declaration.
10599 ///
10600 /// @param d the artifact to consider.
10601 ///
10602 /// @param return the declaration sub-object of @p d if it's a
10603 /// declaration, or NULL if it is not.
10604 decl_base*
10606 {
10607  if (d && (d->kind() & type_or_decl_base::ABSTRACT_DECL_BASE))
10608  {
10609  if (!(d->kind() & type_or_decl_base::ABSTRACT_TYPE_BASE))
10610  // The artifact is a decl-only (like a function or a
10611  // variable). That is, it's not a type that also has a
10612  // declaration. In this case, we are in the fast path and we
10613  // have a pointer to the decl sub-object handy. Just return
10614  // it ...
10615  return reinterpret_cast<decl_base*>
10616  (const_cast<type_or_decl_base*>(d)->type_or_decl_base_pointer());
10617 
10618  // ... Otherwise, we are in the slow path, which is that the
10619  // artifact is a type which has a declaration. In that case,
10620  // let's use the slow dynamic_cast because we don't have the
10621  // pointer to the decl sub-object handily present.
10622  return dynamic_cast<decl_base*>(const_cast<type_or_decl_base*>(d));
10623  }
10624  return 0;
10625 }
10626 
10627 /// Test if an ABI artifact is a declaration.
10628 ///
10629 /// @param d the artifact to consider.
10630 ///
10631 /// @param return the declaration sub-object of @p d if it's a
10632 /// declaration, or NULL if it is not.
10633 decl_base_sptr
10635 {return dynamic_pointer_cast<decl_base>(d);}
10636 
10637 /// Test if an ABI artifact is a declaration.
10638 ///
10639 /// This is done using a slow path that uses dynamic_cast.
10640 ///
10641 /// @param d the artifact to consider.
10642 ///
10643 /// @param return the declaration sub-object of @p d if it's a
10644 decl_base*
10646 {return dynamic_cast<decl_base*>(const_cast<type_or_decl_base*>(t));}
10647 
10648 /// Test if an ABI artifact is a declaration.
10649 ///
10650 /// This is done using a slow path that uses dynamic_cast.
10651 ///
10652 /// @param d the artifact to consider.
10653 ///
10654 /// @param return the declaration sub-object of @p d if it's a
10655 decl_base_sptr
10657 {return dynamic_pointer_cast<decl_base>(t);}
10658 
10659 /// Test whether a declaration is a type.
10660 ///
10661 /// @param d the IR artefact to test for.
10662 ///
10663 /// @return true if the artifact is a type, false otherwise.
10664 bool
10666 {
10667  if (dynamic_cast<const type_base*>(&tod))
10668  return true;
10669  return false;
10670 }
10671 
10672 /// Test whether a declaration is a type.
10673 ///
10674 /// @param d the IR artefact to test for.
10675 ///
10676 /// @return true if the artifact is a type, false otherwise.
10677 type_base*
10679 {
10680  if (t && (t->kind() & type_or_decl_base::ABSTRACT_TYPE_BASE))
10681  return reinterpret_cast<type_base*>
10682  (const_cast<type_or_decl_base*>(t)->type_or_decl_base_pointer());
10683 
10684  return 0;
10685 }
10686 
10687 /// Test whether a declaration is a type.
10688 ///
10689 /// @param d the IR artefact to test for.
10690 ///
10691 /// @return true if the artifact is a type, false otherwise.
10692 type_base_sptr
10694 {return dynamic_pointer_cast<type_base>(tod);}
10695 
10696 /// Test whether a declaration is a type.
10697 ///
10698 /// @param d the declaration to test for.
10699 ///
10700 /// @return true if the declaration is a type, false otherwise.
10701 
10702 /// Test if a given type is anonymous.
10703 ///
10704 /// Note that this function considers that an anonymous class that is
10705 /// named by a typedef is not anonymous anymore. This is the C idiom:
10706 ///
10707 /// typedef struct {int member;} s_type;
10708 ///
10709 /// The typedef s_type becomes the name of the originally anonymous
10710 /// struct.
10711 ///
10712 /// @param t the type to consider.
10713 ///
10714 /// @return true iff @p t is anonymous.
10715 bool
10717 {
10718  const decl_base* d = get_type_declaration(t);
10719  if (d)
10720  if (d->get_is_anonymous())
10721  {
10722  if (class_or_union *cou = is_class_or_union_type(t))
10723  {
10724  // An anonymous class that is named by a typedef is not
10725  // considered anonymous anymore.
10726  if (!cou->get_naming_typedef())
10727  return true;
10728  }
10729  else
10730  return true;
10731  }
10732  return false;
10733 }
10734 
10735 /// Test if a given type is anonymous.
10736 ///
10737 /// @param t the type to consider.
10738 ///
10739 /// @return true iff @p t is anonymous.
10740 bool
10741 is_anonymous_type(const type_base_sptr& t)
10742 {return is_anonymous_type(t.get());}
10743 
10744 /// Test if a type is a neither a pointer, an array nor a function
10745 /// type.
10746 ///
10747 /// @param t the type to consider.
10748 ///
10749 /// @return true if the @p t is NOT a pointer, an array nor a
10750 /// function.
10751 bool
10752 is_npaf_type(const type_base_sptr& t)
10753 {
10754  if (!(is_pointer_type(t)
10755  || is_array_type(t)
10756  || is_function_type(t)
10757  || is_ptr_to_mbr_type(t)))
10758  return true;
10759  return false;
10760 }
10761 
10762 /// Test whether a type is a type_decl (a builtin type).
10763 ///
10764 /// @return the type_decl* for @t if it's type_decl, otherwise, return
10765 /// nil.
10766 const type_decl*
10768 {return dynamic_cast<const type_decl*>(t);}
10769 
10770 /// Test whether a type is a type_decl (a builtin type).
10771 ///
10772 /// @return the type_decl_sptr for @t if it's type_decl, otherwise,
10773 /// return nil.
10776 {return dynamic_pointer_cast<type_decl>(t);}
10777 
10778 /// Test if a type is an integral type.
10779 ///
10780 /// @param t the type to test.
10781 ///
10782 /// @return the integral type @p t can be converted to, or nil if @p
10783 /// is not an integral type.
10784 type_decl*
10786 {
10787  type_decl *type = const_cast<type_decl*>(is_type_decl(t));
10788  if (!type)
10789  return nullptr;
10790 
10791  integral_type int_type;
10792  if (!parse_integral_type(type->get_name(), int_type))
10793  return nullptr;
10794 
10795  return type;
10796 }
10797 
10798 /// Test if a type is an integral type.
10799 ///
10800 /// @param t the type to test.
10801 ///
10802 /// @return the integral type @p t can be converted to, or nil if @p
10803 /// is not an integral type.
10806 {
10807  const type_decl_sptr type = is_type_decl(t);
10808  if (!type)
10809  return type_decl_sptr();
10810 
10811  integral_type int_type;
10812  if (!parse_integral_type(type->get_name(), int_type))
10813  return type_decl_sptr();
10814 
10815  return type;
10816 }
10817 
10818 /// Test whether a type is a typedef.
10819 ///
10820 /// @param t the type to test for.
10821 ///
10822 /// @return the typedef declaration of the @p t, or NULL if it's not a
10823 /// typedef.
10826 {return dynamic_pointer_cast<typedef_decl>(t);}
10827 
10828 /// Test whether a type is a typedef.
10829 ///
10830 /// @param t the declaration of the type to test for.
10831 ///
10832 /// @return the typedef declaration of the @p t, or NULL if it's not a
10833 /// typedef.
10834 const typedef_decl*
10836 {return dynamic_cast<const typedef_decl*>(t);}
10837 
10838 /// Test whether a type is a typedef.
10839 ///
10840 /// @param t the declaration of the type to test for.
10841 ///
10842 /// @return the typedef declaration of the @p t, or NULL if it's not a
10843 /// typedef.
10844 typedef_decl*
10846 {return dynamic_cast<typedef_decl*>(t);}
10847 
10848 /// Test whether a type is a typedef.
10849 ///
10850 /// @param t the declaration of the type to test for.
10851 ///
10852 /// @return the typedef declaration of the @p t, or NULL if it's not a
10853 /// typedef.
10854 const typedef_decl*
10856 {return dynamic_cast<const typedef_decl*>(t);}
10857 /// Test if a type is an enum. This function looks through typedefs.
10858 ///
10859 /// @parm t the type to consider.
10860 ///
10861 /// @return the enum_decl if @p t is an @ref enum_decl or null
10862 /// otherwise.
10864 is_compatible_with_enum_type(const type_base_sptr& t)
10865 {
10866  if (!t)
10867  return enum_type_decl_sptr();
10868 
10869  // Normally we should strip typedefs entirely, but this is
10870  // potentially costly, especially on binaries with huge changesets
10871  // like the Linux Kernel. So we just get the leaf types for now.
10872  //
10873  // Maybe there should be an option by which users accepts to pay the
10874  // CPU usage toll in exchange for finer filtering?
10875 
10876  // type_base_sptr ty = strip_typedef(t);
10877  type_base_sptr ty = peel_typedef_type(t);;
10878  return is_enum_type(ty);
10879 }
10880 
10881 /// Test if a type is an enum. This function looks through typedefs.
10882 ///
10883 /// @parm t the type to consider.
10884 ///
10885 /// @return the enum_decl if @p t is an @ref enum_decl or null
10886 /// otherwise.
10888 is_compatible_with_enum_type(const decl_base_sptr& t)
10890 
10891 /// Test if a decl is an enum_type_decl
10892 ///
10893 /// @param d the decl to test for.
10894 ///
10895 /// @return the enum_type_decl* if @p d is an enum, nil otherwise.
10896 const enum_type_decl*
10898 {return dynamic_cast<const enum_type_decl*>(d);}
10899 
10900 /// Test if a decl is an enum_type_decl
10901 ///
10902 /// @param d the decl to test for.
10903 ///
10904 /// @return the enum_type_decl_sptr if @p d is an enum, nil otherwise.
10907 {return dynamic_pointer_cast<enum_type_decl>(d);}
10908 
10909 /// Test if a type is a class. This function looks through typedefs.
10910 ///
10911 /// @parm t the type to consider.
10912 ///
10913 /// @return the class_decl if @p t is a class_decl or null otherwise.
10915 is_compatible_with_class_type(const type_base_sptr& t)
10916 {
10917  if (!t)
10918  return class_decl_sptr();
10919 
10920  // Normally we should strip typedefs entirely, but this is
10921  // potentially costly, especially on binaries with huge changesets
10922  // like the Linux Kernel. So we just get the leaf types for now.
10923  //
10924  // Maybe there should be an option by which users accepts to pay the
10925  // CPU usage toll in exchange for finer filtering?
10926 
10927  // type_base_sptr ty = strip_typedef(t);
10928  type_base_sptr ty = peel_typedef_type(t);
10929  return is_class_type(ty);
10930 }
10931 
10932 /// Test if a type is a class. This function looks through typedefs.
10933 ///
10934 /// @parm t the type to consider.
10935 ///
10936 /// @return the class_decl if @p t is a class_decl or null otherwise.
10938 is_compatible_with_class_type(const decl_base_sptr& t)
10940 
10941 /// Test whether a type is a class.
10942 ///
10943 /// @parm t the type to consider.
10944 ///
10945 /// @return true iff @p t is a class_decl.
10946 bool
10948 {return is_class_type(&t);}
10949 
10950 /// Test whether a type is a class.
10951 ///
10952 /// @parm t the type to consider.
10953 ///
10954 /// @return the class_decl if @p t is a class_decl or null otherwise.
10955 class_decl*
10957 {
10958  if (!t)
10959  return 0;
10960 
10961  if (t->kind() & type_or_decl_base::CLASS_TYPE)
10962  return reinterpret_cast<class_decl*>
10963  (const_cast<type_or_decl_base*>(t)->runtime_type_instance());
10964 
10965  return 0;
10966 }
10967 
10968 /// Test whether a type is a class.
10969 ///
10970 /// @parm t the type to consider.
10971 ///
10972 /// @return the class_decl if @p t is a class_decl or null otherwise.
10975 {return dynamic_pointer_cast<class_decl>(d);}
10976 
10977 /// Test if the last data member of a class is an array with
10978 /// non-finite data member.
10979 ///
10980 /// The flexible data member idiom is a well known C idiom:
10981 /// https://en.wikipedia.org/wiki/Flexible_array_member.
10982 ///
10983 /// @param klass the class to consider.
10984 ///
10985 /// @return the data member which type is a flexible array, if any, or
10986 /// nil.
10989 {
10990  var_decl_sptr nil;
10991  const class_or_union::data_members& dms = klass.get_data_members();
10992  if (dms.empty())
10993  return nil;
10994 
10995  if (array_type_def_sptr array = is_array_type(dms.back()->get_type()))
10996  {// The type of the last data member is an array.
10997  if (array->is_non_finite())
10998  // The array has a non-finite size. We are thus looking at a
10999  // flexible array data member. Let's return it.
11000  return dms.back();
11001  }
11002 
11003  return nil;
11004 }
11005 
11006 /// Test if the last data member of a class is an array with
11007 /// non-finite data member.
11008 ///
11009 /// The flexible data member idiom is a well known C idiom:
11010 /// https://en.wikipedia.org/wiki/Flexible_array_member.
11011 ///
11012 /// @param klass the class to consider.
11013 ///
11014 /// @return the data member which type is a flexible array, if any, or
11015 /// nil.
11018 {
11019  if (!klass)
11020  return var_decl_sptr();
11021 
11022  return has_flexible_array_data_member(*klass);
11023 }
11024 
11025 /// Test if the last data member of a class is an array with
11026 /// non-finite data member.
11027 ///
11028 /// The flexible data member idiom is a well known C idiom:
11029 /// https://en.wikipedia.org/wiki/Flexible_array_member.
11030 ///
11031 /// @param klass the class to consider.
11032 ///
11033 /// @return the data member which type is a flexible array, if any, or
11034 /// nil.
11037 {return has_flexible_array_data_member(klass.get());}
11038 
11039 /// Test if the last data member of a class is an array with
11040 /// one element.
11041 ///
11042 /// An array with one element is a way to mimic the flexible data
11043 /// member idiom that was later standardized in C99.
11044 ///
11045 /// To learn more about the flexible data member idiom, please
11046 /// consider reading :
11047 /// https://en.wikipedia.org/wiki/Flexible_array_member.
11048 ///
11049 /// The various ways of representing that idiom pre-standardization
11050 /// are presented in this article:
11051 /// https://developers.redhat.com/articles/2022/09/29/benefits-limitations-flexible-array-members#
11052 ///
11053 /// @param klass the class to consider.
11054 ///
11055 /// @return the data member which type is a fake flexible array, if
11056 /// any, or nil.
11059 {
11060  var_decl_sptr nil;
11061  const class_or_union::data_members& dms = klass.get_data_members();
11062  if (dms.empty())
11063  return nil;
11064 
11065  if (array_type_def_sptr array = is_array_type(dms.back()->get_type()))
11066  {// The type of the last data member is an array.
11067  if (array->get_subranges().size() == 1
11068  && array->get_subranges()[0]->get_length() == 1)
11069  // The array has a size of one. We are thus looking at a
11070  // "fake" flexible array data member. Let's return it.
11071  return dms.back();
11072  }
11073 
11074  return nil;
11075 }
11076 
11077 /// Test if the last data member of a class is an array with
11078 /// one element.
11079 ///
11080 /// An array with one element is a way to mimic the flexible data
11081 /// member idiom that was later standardized in C99.
11082 ///
11083 /// To learn more about the flexible data member idiom, please
11084 /// consider reading :
11085 /// https://en.wikipedia.org/wiki/Flexible_array_member.
11086 ///
11087 /// The various ways of representing that idiom pre-standardization
11088 /// are presented in this article:
11089 /// https://developers.redhat.com/articles/2022/09/29/benefits-limitations-flexible-array-members#
11090 ///
11091 /// @param klass the class to consider.
11092 ///
11093 /// @return the data member which type is a fake flexible array, if
11094 /// any, or nil.
11097 {return has_fake_flexible_array_data_member(*klass);}
11098 
11099 /// Test if the last data member of a class is an array with
11100 /// one element.
11101 ///
11102 /// An array with one element is a way to mimic the flexible data
11103 /// member idiom that was later standardized in C99.
11104 ///
11105 /// To learn more about the flexible data member idiom, please
11106 /// consider reading :
11107 /// https://en.wikipedia.org/wiki/Flexible_array_member.
11108 ///
11109 /// The various ways of representing that idiom pre-standardization
11110 /// are presented in this article:
11111 /// https://developers.redhat.com/articles/2022/09/29/benefits-limitations-flexible-array-members#
11112 ///
11113 /// @param klass the class to consider.
11114 ///
11115 /// @return the data member which type is a fake flexible array, if
11116 /// any, or nil.
11119 {return has_fake_flexible_array_data_member(klass.get());}
11120 
11121 /// Test wheter a type is a declaration-only class.
11122 ///
11123 /// @param t the type to considier.
11124 ///
11125 /// @param look_through_decl_only if true, then look through the
11126 /// decl-only class to see if it actually has a class definition in
11127 /// the same ABI corpus.
11128 ///
11129 /// @return true iff @p t is a declaration-only class.
11130 bool
11133 {
11134  if (class_or_union *klass = is_class_or_union_type(t))
11135  {
11137  klass = look_through_decl_only_class(klass);
11138  return klass->get_is_declaration_only();
11139  }
11140  return false;
11141 }
11142 
11143 /// Test wheter a type is a declaration-only class.
11144 ///
11145 /// @param t the type to considier.
11146 ///
11147 /// @param look_through_decl_only if true, then look through the
11148 /// decl-only class to see if it actually has a class definition in
11149 /// the same ABI corpus.
11150 ///
11151 /// @return true iff @p t is a declaration-only class.
11152 bool
11156 
11157 /// Test wheter a type is a declaration-only class.
11158 ///
11159 /// @param t the type to considier.
11160 ///
11161 /// @param look_through_decl_only if true, then look through the
11162 /// decl-only class to see if it actually has a class definition in
11163 /// the same ABI corpus.
11164 ///
11165 /// @return true iff @p t is a declaration-only class.
11166 bool
11167 is_declaration_only_class_type(const type_base_sptr& t,
11170 
11171 /// Test if a type is a @ref class_or_union.
11172 ///
11173 /// @param t the type to consider.
11174 ///
11175 /// @return the @ref class_or_union is @p is a @ref class_or_union, or
11176 /// nil otherwise.
11179 {return dynamic_cast<class_or_union*>(const_cast<type_or_decl_base*>(t));}
11180 
11181 /// Test if a type is a @ref class_or_union.
11182 ///
11183 /// @param t the type to consider.
11184 ///
11185 /// @return the @ref class_or_union is @p is a @ref class_or_union, or
11186 /// nil otherwise.
11187 shared_ptr<class_or_union>
11188 is_class_or_union_type(const shared_ptr<type_or_decl_base>& t)
11189 {return dynamic_pointer_cast<class_or_union>(t);}
11190 
11191 /// Test if two class or union types are of the same kind.
11192 ///
11193 /// @param first the first type to consider.
11194 ///
11195 /// @param second the second type to consider.
11196 ///
11197 /// @return true iff @p first is of the same kind as @p second.
11198 bool
11200  const class_or_union* second)
11201 {
11202  if ((is_class_type(first) && is_class_type(second))
11203  || (is_union_type(first) && is_union_type(second)))
11204  return true;
11205 
11206  return false;
11207 }
11208 
11209 /// Test if two class or union types are of the same kind.
11210 ///
11211 /// @param first the first type to consider.
11212 ///
11213 /// @param second the second type to consider.
11214 ///
11215 /// @return true iff @p first is of the same kind as @p second.
11216 bool
11217 class_or_union_types_of_same_kind(const class_or_union_sptr& first,
11218  const class_or_union_sptr& second)
11219 {return class_or_union_types_of_same_kind(first.get(), second.get());}
11220 
11221 /// Test if a type is a @ref union_decl.
11222 ///
11223 /// @param t the type to consider.
11224 ///
11225 /// @return true iff @p t is a union_decl.
11226 bool
11228 {return is_union_type(&t);}
11229 
11230 /// Test if a type is a @ref union_decl.
11231 ///
11232 /// @param t the type to consider.
11233 ///
11234 /// @return the @ref union_decl is @p is a @ref union_decl, or nil
11235 /// otherwise.
11236 union_decl*
11238 {return dynamic_cast<union_decl*>(const_cast<type_or_decl_base*>(t));}
11239 
11240 /// Test if a type is a @ref union_decl.
11241 ///
11242 /// @param t the type to consider.
11243 ///
11244 /// @return the @ref union_decl is @p is a @ref union_decl, or nil
11245 /// otherwise.
11246 union_decl_sptr
11247 is_union_type(const shared_ptr<type_or_decl_base>& t)
11248 {return dynamic_pointer_cast<union_decl>(t);}
11249 
11250 /// Test whether a type is a pointer_type_def.
11251 ///
11252 /// @param t the type to test.
11253 ///
11254 /// @param look_through_decl_only if this is true, then look through
11255 /// qualified types to see if the underlying type is a
11256 /// pointer_type_def.
11257 ///
11258 /// @return the @ref pointer_type_def_sptr if @p t is a
11259 /// pointer_type_def, null otherwise.
11260 const pointer_type_def*
11262  bool look_through_qualifiers)
11263 {
11264  if (!t)
11265  return 0;
11266 
11267  const type_base* type = is_type(t);
11268  if (look_through_qualifiers)
11269  type = peel_qualified_type(is_type(t));
11270 
11271  return dynamic_cast<pointer_type_def*>(const_cast<type_base*>(type));
11272 }
11273 
11274 /// Test whether a type is a pointer_type_def.
11275 ///
11276 /// @param t the type to test.
11277 ///
11278 /// @param look_through_decl_only if this is true, then look through
11279 /// qualified types to see if the underlying type is a
11280 /// pointer_type_def.
11281 ///
11282 /// @return the @ref pointer_type_def_sptr if @p t is a
11283 /// pointer_type_def, null otherwise.
11286  bool look_through_qualifiers)
11287 {
11288  type_base_sptr type = is_type(t);
11289  if (look_through_qualifiers)
11290  type = peel_qualified_type(type);
11291  return dynamic_pointer_cast<pointer_type_def>(type);
11292 }
11293 
11294 /// Test if a type is a pointer to function type.
11295 ///
11296 /// @param t the type to consider.
11297 ///
11298 /// @return the @ref pointer_type_def_sptr iff @p t is a pointer to
11299 /// function type.
11301 is_pointer_to_function_type(const type_base_sptr& t)
11302 {
11304  {
11305  if (is_function_type(p->get_pointed_to_type()))
11306  return p;
11307  }
11308  return pointer_type_def_sptr();
11309 }
11310 
11311 /// Test if a type is a pointer to array type.
11312 ///
11313 /// @param t the type to consider.
11314 ///
11315 /// @return the pointer_type_def_sptr iff @p t is a pointer to array
11316 /// type.
11318 is_pointer_to_array_type(const type_base_sptr& t)
11319 {
11321  {
11322  if (is_array_type(p->get_pointed_to_type()))
11323  return p;
11324  }
11325  return pointer_type_def_sptr();
11326 }
11327 
11328 /// Test if we are looking at a pointer to a
11329 /// neither-a-pointer-to-an-array-nor-a-function type.
11330 ///
11331 /// @param t the type to consider.
11332 ///
11333 /// @return the @ref pointer_type_def_sptr type iff @p t is a
11334 /// neither-a-pointer-an-array-nor-a-function type.
11336 is_pointer_to_npaf_type(const type_base_sptr& t)
11337 {
11339  {
11340  if (is_npaf_type(p->get_pointed_to_type()))
11341  return p;
11342  }
11343  return pointer_type_def_sptr();
11344 }
11345 
11346 /// Test if we are looking at a pointer to pointer to member type.
11347 ///
11348 /// @param t the type to consider.
11349 ///
11350 /// @return the @ref pointer_type_def_sptr type iff @p t is a pointer
11351 /// to pointer to member type.
11353 is_pointer_to_ptr_to_mbr_type(const type_base_sptr& t)
11354 {
11356  {
11357  if (is_ptr_to_mbr_type(p->get_pointed_to_type()))
11358  return p;
11359  }
11360  return pointer_type_def_sptr();
11361 }
11362 
11363 /// Test if a type is a typedef, pointer or reference to a decl-only
11364 /// class/union.
11365 ///
11366 /// This looks into qualified types too.
11367 ///
11368 /// @param t the type to consider.
11369 ///
11370 /// @return true iff @p t is a type is a typedef, pointer or reference
11371 /// to a decl-only class/union.
11372 bool
11374 {
11375  const type_base * type =
11376  peel_typedef_pointer_or_reference_type(t, /*peel_qual_type=*/true);
11377 
11379  /*look_through_decl_only=*/true))
11380  return true;
11381 
11382  return false;
11383 }
11384 
11385 /// Test if a type is a typedef of a class or union type, or a typedef
11386 /// of a qualified class or union type.
11387 ///
11388 /// Note that if the type is directly a class or union type, the
11389 /// function returns true as well.
11390 ///
11391 /// @param t the type to consider.
11392 ///
11393 /// @return true iff @p t is a typedef of a class or union type, or a
11394 /// typedef of a qualified class or union type.
11395 bool
11397 {
11398  if (!t)
11399  return false;
11400 
11402  if (is_class_or_union_type(t))
11403  return true;
11404 
11405 return false;
11406 }
11407 
11408 /// Test if a type is a typedef of a class or union type, or a typedef
11409 /// of a qualified class or union type.
11410 ///
11411 /// Note that if the type is directly a class or union type, the
11412 /// function returns true as well.
11413 ///
11414 /// @param t the type to consider.
11415 ///
11416 /// @return true iff @p t is a typedef of a class or union type, or a
11417 /// typedef of a qualified class or union type.
11418 bool
11421 
11422 /// Test whether a type is a reference_type_def.
11423 ///
11424 /// @param t the type to test.
11425 ///
11426 /// @param look_through_decl_only if this is true, then look through
11427 /// qualified types to see if the underlying type is a
11428 /// reference_type_def.
11429 ///
11430 /// @return the @ref reference_type_def_sptr if @p t is a
11431 /// reference_type_def, null otherwise.
11434  bool look_through_qualifiers)
11435 {
11436  const type_base* type = is_type(t);
11437  if (!type)
11438  return nullptr;
11439 
11440  if (look_through_qualifiers)
11441  type = peel_qualified_type(type);
11442  return dynamic_cast<reference_type_def*>(const_cast<type_base*>(type));
11443 }
11444 
11445 /// Test whether a type is a reference_type_def.
11446 ///
11447 /// @param t the type to test.
11448 ///
11449 /// @param look_through_decl_only if this is true, then look through
11450 /// qualified types to see if the underlying type is a
11451 /// reference_type_def.
11452 ///
11453 /// @return the @ref reference_type_def_sptr if @p t is a
11454 /// reference_type_def, null otherwise.
11455 const reference_type_def*
11457  bool look_through_qualifiers)
11458 {
11459  const type_base* type = is_type(t);
11460 
11461  if (look_through_qualifiers)
11462  type = peel_qualified_type(type);
11463  return dynamic_cast<const reference_type_def*>(type);
11464 }
11465 
11466 /// Test whether a type is a reference_type_def.
11467 ///
11468 /// @param t the type to test.
11469 ///
11470 /// @param look_through_decl_only if this is true, then look through
11471 /// qualified types to see if the underlying type is a
11472 /// reference_type_def.
11473 ///
11474 /// @return the @ref reference_type_def_sptr if @p t is a
11475 /// reference_type_def, null otherwise.
11478  bool look_through_qualifiers)
11479 {
11480  type_base_sptr type = is_type(t);
11481  if (look_through_qualifiers)
11482  type = peel_qualified_type(type);
11483  return dynamic_pointer_cast<reference_type_def>(type);
11484 }
11485 
11486 /// Test whether a type is a @ref ptr_to_mbr_type.
11487 ///
11488 /// @param t the type to test.
11489 ///
11490 /// @return the @ref ptr_to_mbr_type* if @p t is a @ref
11491 /// ptr_to_mbr_type type, null otherwise.
11492 const ptr_to_mbr_type*
11494  bool look_through_qualifiers)
11495 {
11496  const type_base* type = is_type(t);
11497  if (look_through_qualifiers)
11498  type = peel_qualified_type(type);
11499  return dynamic_cast<const ptr_to_mbr_type*>(type);
11500 }
11501 
11502 /// Test whether a type is a @ref ptr_to_mbr_type_sptr.
11503 ///
11504 /// @param t the type to test.
11505 ///
11506 /// @param look_through_decl_only if this is true, then look through
11507 /// qualified types to see if the underlying type is a
11508 /// ptr_to_mbr_type..
11509 ///
11510 /// @return the @ref ptr_to_mbr_type_sptr if @p t is a @ref
11511 /// ptr_to_mbr_type type, null otherwise.
11514  bool look_through_qualifiers)
11515 {
11516  type_base_sptr type = is_type(t);
11517  if (look_through_qualifiers)
11518  type = peel_qualified_type(type);
11519  return dynamic_pointer_cast<ptr_to_mbr_type>(type);
11520 }
11521 
11522 /// Test if a type is equivalent to a pointer to void type.
11523 ///
11524 /// Note that this looks trough typedefs or CV qualifiers to look for
11525 /// the void pointer.
11526 ///
11527 /// @param type the type to consider.
11528 ///
11529 /// @return the actual void pointer if @p is eqivalent to a void
11530 /// pointer or NULL if it's not.
11531 const type_base*
11533 {
11534  type = peel_qualified_or_typedef_type(type);
11535 
11536  const pointer_type_def * t = is_pointer_type(type);
11537  if (!t)
11538  return 0;
11539 
11540  // Look through typedefs in the pointed-to type as well.
11541  type_base * ty = t->get_pointed_to_type().get();
11543  if (ty && ty->get_environment().is_void_type(ty))
11544  return ty;
11545 
11546  return 0;
11547 }
11548 
11549 /// Test if a type is equivalent to a pointer to void type.
11550 ///
11551 /// Note that this looks trough typedefs or CV qualifiers to look for
11552 /// the void pointer.
11553 ///
11554 /// @param type the type to consider.
11555 ///
11556 /// @return the actual void pointer if @p is eqivalent to a void
11557 /// pointer or NULL if it's not.
11558 const type_base*
11560 {return is_void_pointer_type_equivalent(&type);}
11561 
11562 /// Test if a type is a pointer to void type.
11563 ///
11564 /// @param type the type to consider.
11565 ///
11566 /// @return the actual void pointer if @p is a void pointer or NULL if
11567 /// it's not.
11568 const type_base*
11570 {
11571  if (!t)
11572  return nullptr;
11573 
11574  if (t->get_environment().get_void_pointer_type().get() == t)
11575  return t;
11576 
11577  const pointer_type_def* ptr = is_pointer_type(t);
11578  if (!ptr)
11579  return nullptr;
11580 
11582  return t;
11583 
11584  return nullptr;
11585 }
11586 
11587 /// Test if a type is a pointer to void type.
11588 ///
11589 /// @param type the type to consider.
11590 ///
11591 /// @return the actual void pointer if @p is a void pointer or NULL if
11592 /// it's not.
11593 const type_base_sptr
11594 is_void_pointer_type(const type_base_sptr& t)
11595 {
11596  type_base_sptr nil;
11597  if (!t)
11598  return nil;
11599 
11600  if (t->get_environment().get_void_pointer_type().get() == t.get())
11601  return t;
11602 
11603  const pointer_type_def* ptr = is_pointer_type(t.get());
11604  if (!ptr)
11605  return nil;
11606 
11607  if (t->get_environment().is_void_type(ptr->get_pointed_to_type()))
11608  return t;
11609 
11610  return nil;
11611 }
11612 
11613 /// Test whether a type is a reference_type_def.
11614 ///
11615 /// @param t the type to test.
11616 ///
11617 /// @return the @ref reference_type_def_sptr if @p t is a
11618 /// reference_type_def, null otherwise.
11621 {return dynamic_cast<qualified_type_def*>(const_cast<type_or_decl_base*>(t));}
11622 
11623 /// Test whether a type is a qualified_type_def.
11624 ///
11625 /// @param t the type to test.
11626 ///
11627 /// @return the @ref qualified_type_def_sptr if @p t is a
11628 /// qualified_type_def, null otherwise.
11629 qualified_type_def_sptr
11631 {return dynamic_pointer_cast<qualified_type_def>(t);}
11632 
11633 /// Test whether a type is a function_type.
11634 ///
11635 /// @param t the type to test.
11636 ///
11637 /// @return the @ref function_type_sptr if @p t is a
11638 /// function_type, null otherwise.
11641 {return dynamic_pointer_cast<function_type>(t);}
11642 
11643 /// Test whether a type is a function_type.
11644 ///
11645 /// @param t the type to test.
11646 ///
11647 /// @return the @ref function_type_sptr if @p t is a
11648 /// function_type, null otherwise.
11651 {return dynamic_cast<function_type*>(t);}
11652 
11653 /// Test whether a type is a function_type.
11654 ///
11655 /// @param t the type to test.
11656 ///
11657 /// @return the @ref function_type_sptr if @p t is a
11658 /// function_type, null otherwise.
11659 const function_type*
11661 {return dynamic_cast<const function_type*>(t);}
11662 
11663 /// Test whether a type is a method_type.
11664 ///
11665 /// @param t the type to test.
11666 ///
11667 /// @return the @ref method_type_sptr if @p t is a
11668 /// method_type, null otherwise.
11671 {return dynamic_pointer_cast<method_type>(t);}
11672 
11673 /// Test whether a type is a method_type.
11674 ///
11675 /// @param t the type to test.
11676 ///
11677 /// @return the @ref method_type_sptr if @p t is a
11678 /// method_type, null otherwise.
11679 const method_type*
11681 {return dynamic_cast<const method_type*>(t);}
11682 
11683 /// Test whether a type is a method_type.
11684 ///
11685 /// @param t the type to test.
11686 ///
11687 /// @return the @ref method_type_sptr if @p t is a
11688 /// method_type, null otherwise.
11689 method_type*
11691 {return dynamic_cast<method_type*>(t);}
11692 
11693 /// If a class (or union) is a decl-only class, get its definition.
11694 /// Otherwise, just return the initial class.
11695 ///
11696 /// @param the_class the class (or union) to consider.
11697 ///
11698 /// @return either the definition of the class, or the class itself.
11701 {return is_class_or_union_type(look_through_decl_only(the_class));}
11702 
11703 /// If a class (or union) is a decl-only class, get its definition.
11704 /// Otherwise, just return the initial class.
11705 ///
11706 /// @param the_class the class (or union) to consider.
11707 ///
11708 /// @return either the definition of the class, or the class itself.
11709 class_or_union_sptr
11711 {return is_class_or_union_type(look_through_decl_only(the_class));}
11712 
11713 /// If a class (or union) is a decl-only class, get its definition.
11714 /// Otherwise, just return the initial class.
11715 ///
11716 /// @param klass the class (or union) to consider.
11717 ///
11718 /// @return either the definition of the class, or the class itself.
11719 class_or_union_sptr
11720 look_through_decl_only_class(class_or_union_sptr klass)
11722 
11723 /// If an enum is a decl-only enum, get its definition.
11724 /// Otherwise, just return the initial enum.
11725 ///
11726 /// @param the_enum the enum to consider.
11727 ///
11728 /// @return either the definition of the enum, or the enum itself.
11731 {return is_enum_type(look_through_decl_only(the_enum));}
11732 
11733 /// If an enum is a decl-only enum, get its definition.
11734 /// Otherwise, just return the initial enum.
11735 ///
11736 /// @param enom the enum to consider.
11737 ///
11738 /// @return either the definition of the enum, or the enum itself.
11741 {return is_enum_type(look_through_decl_only(enom));}
11742 
11743 /// If a decl is decl-only get its definition. Otherwise, just return nil.
11744 ///
11745 /// @param d the decl to consider.
11746 ///
11747 /// @return either the definition of the decl, or nil.
11748 decl_base_sptr
11750 {
11751  decl_base_sptr decl;
11752  if (d.get_is_declaration_only())
11753  decl = d.get_definition_of_declaration();
11754 
11755  if (!decl)
11756  return decl;
11757 
11758  while (decl->get_is_declaration_only()
11759  && decl->get_definition_of_declaration())
11760  decl = decl->get_definition_of_declaration();
11761 
11762  return decl;
11763 }
11764 
11765 /// If a decl is decl-only enum, get its definition. Otherwise, just
11766 /// return the initial decl.
11767 ///
11768 /// @param d the decl to consider.
11769 ///
11770 /// @return either the definition of the enum, or the decl itself.
11771 decl_base*
11773 {
11774  if (!d)
11775  return d;
11776 
11777  decl_base* result = look_through_decl_only(*d).get();
11778  if (!result)
11779  result = d;
11780 
11781  return result;
11782 }
11783 
11784 /// If a decl is decl-only get its definition. Otherwise, just return nil.
11785 ///
11786 /// @param d the decl to consider.
11787 ///
11788 /// @return either the definition of the decl, or nil.
11789 decl_base_sptr
11790 look_through_decl_only(const decl_base_sptr& d)
11791 {
11792  if (!d)
11793  return d;
11794 
11795  decl_base_sptr result = look_through_decl_only(*d);
11796  if (!result)
11797  result = d;
11798 
11799  return result;
11800 }
11801 
11802 /// If a type is is decl-only, then get its definition. Otherwise,
11803 /// just return the initial type.
11804 ///
11805 /// @param d the decl to consider.
11806 ///
11807 /// @return either the definition of the decl, or the initial type.
11808 type_base*
11810 {
11811  decl_base* d = is_decl(t);
11812  if (!d)
11813  return t;
11814  d = look_through_decl_only(d);
11815  return is_type(d);
11816 }
11817 
11818 /// If a type is is decl-only, then get its definition. Otherwise,
11819 /// just return the initial type.
11820 ///
11821 /// @param d the decl to consider.
11822 ///
11823 /// @return either the definition of the decl, or the initial type.
11824 type_base_sptr
11825 look_through_decl_only(const type_base_sptr& t)
11826 {
11827  decl_base_sptr d = is_decl(t);
11828  if (!d)
11829  return t;
11830  d = look_through_decl_only(d);
11831  return is_type(d);
11832 }
11833 
11834 /// Tests if a declaration is a variable declaration.
11835 ///
11836 /// @param decl the decl to test.
11837 ///
11838 /// @return the var_decl_sptr iff decl is a variable declaration; nil
11839 /// otherwise.
11840 var_decl*
11842 {return dynamic_cast<var_decl*>(const_cast<type_or_decl_base*>(tod));}
11843 
11844 /// Tests if a declaration is a variable declaration.
11845 ///
11846 /// @param decl the decl to test.
11847 ///
11848 /// @return the var_decl_sptr iff decl is a variable declaration; nil
11849 /// otherwise.
11852 {return dynamic_pointer_cast<var_decl>(decl);}
11853 
11854 /// Tests if a declaration is a namespace declaration.
11855 ///
11856 /// @param d the decalration to consider.
11857 ///
11858 /// @return the namespace declaration if @p d is a namespace.
11860 is_namespace(const decl_base_sptr& d)
11861 {return dynamic_pointer_cast<namespace_decl>(d);}
11862 
11863 /// Tests if a declaration is a namespace declaration.
11864 ///
11865 /// @param d the decalration to consider.
11866 ///
11867 /// @return the namespace declaration if @p d is a namespace.
11870 {return dynamic_cast<namespace_decl*>(const_cast<decl_base*>(d));}
11871 
11872 /// Tests whether a decl is a template parameter composition type.
11873 ///
11874 /// @param decl the declaration to consider.
11875 ///
11876 /// @return true iff decl is a template parameter composition type.
11877 bool
11878 is_template_parm_composition_type(const shared_ptr<decl_base> decl)
11879 {
11880  return (decl
11881  && is_at_template_scope(decl)
11882  && is_type(decl)
11883  && !is_template_parameter(decl));
11884 }
11885 
11886 /// Test whether a decl is the pattern of a function template.
11887 ///
11888 /// @param decl the decl to consider.
11889 ///
11890 /// @return true iff decl is the pattern of a function template.
11891 bool
11892 is_function_template_pattern(const shared_ptr<decl_base> decl)
11893 {
11894  return (decl
11895  && dynamic_pointer_cast<function_decl>(decl)
11896  && dynamic_cast<template_decl*>(decl->get_scope()));
11897 }
11898 
11899 /// Test if a type is an array_type_def.
11900 ///
11901 /// @param type the type to consider.
11902 ///
11903 /// @return true iff @p type is an array_type_def.
11906  bool look_through_qualifiers)
11907 {
11908  const type_base* t = is_type(type);
11909 
11910  if (look_through_qualifiers)
11911  t = peel_qualified_type(t);
11912  return dynamic_cast<array_type_def*>(const_cast<type_base*>(t));
11913 }
11914 
11915 /// Test if a type is an array_type_def.
11916 ///
11917 /// @param type the type to consider.
11918 ///
11919 /// @return true iff @p type is an array_type_def.
11922  bool look_through_qualifiers)
11923 {
11924  type_base_sptr t = is_type(type);
11925 
11926  if (look_through_qualifiers)
11927  t = peel_qualified_type(t);
11928  return dynamic_pointer_cast<array_type_def>(t);
11929 }
11930 
11931 /// Tests if the element of a given array is a qualified type.
11932 ///
11933 /// @param array the array type to consider.
11934 ///
11935 /// @return the qualified element of the array iff it's a qualified
11936 /// type. Otherwise, return a nil object.
11937 qualified_type_def_sptr
11939 {
11940  if (!array)
11941  return qualified_type_def_sptr();
11942 
11943  return is_qualified_type(array->get_element_type());
11944 }
11945 
11946 /// Test if an array type is an array to a qualified element type.
11947 ///
11948 /// @param type the array type to consider.
11949 ///
11950 /// @return true the array @p type iff it's an array to a qualified
11951 /// element type.
11953 is_array_of_qualified_element(const type_base_sptr& type)
11954 {
11955  if (array_type_def_sptr array = is_array_type(type))
11956  if (is_array_of_qualified_element(array))
11957  return array;
11958 
11959  return array_type_def_sptr();
11960 }
11961 
11962 /// Test if a type is a typedef of an array.
11963 ///
11964 /// Note that the function looks through qualified and typedefs types
11965 /// of the underlying type of the current typedef. In other words, if
11966 /// we are looking at a typedef of a CV-qualified array, or at a
11967 /// typedef of a CV-qualified typedef of an array, this function will
11968 /// still return TRUE.
11969 ///
11970 /// @param t the type to consider.
11971 ///
11972 /// @return true if t is a typedef which underlying type is an array.
11973 /// That array might be either cv-qualified array or a typedef'ed
11974 /// array, or a combination of both.
11976 is_typedef_of_array(const type_base_sptr& t)
11977 {
11978  array_type_def_sptr result;
11979 
11980  if (typedef_decl_sptr typdef = is_typedef(t))
11981  {
11982  type_base_sptr u =
11983  peel_qualified_or_typedef_type(typdef->get_underlying_type());
11984  result = is_array_type(u);
11985  }
11986 
11987  return result;
11988 }
11989 
11990 /// Test if a type is an array_type_def::subrange_type.
11991 ///
11992 /// @param type the type to consider.
11993 ///
11994 /// @return the array_type_def::subrange_type which @p type is a type
11995 /// of, or nil if it's not of that type.
11996 array_type_def::subrange_type*
11998 {
11999  return dynamic_cast<array_type_def::subrange_type*>
12000  (const_cast<type_or_decl_base*>(type));
12001 }
12002 
12003 /// Test if a type is an array_type_def::subrange_type.
12004 ///
12005 /// @param type the type to consider.
12006 ///
12007 /// @return the array_type_def::subrange_type which @p type is a type
12008 /// of, or nil if it's not of that type.
12011 {return dynamic_pointer_cast<array_type_def::subrange_type>(type);}
12012 
12013 /// Tests whether a decl is a template.
12014 ///
12015 /// @param decl the decl to consider.
12016 ///
12017 /// @return true iff decl is a function template, class template, or
12018 /// template template parameter.
12019 bool
12020 is_template_decl(const decl_base_sptr& decl)
12021 {return decl && dynamic_pointer_cast<template_decl>(decl);}
12022 
12023 /// This enum describe the kind of entity to lookup, while using the
12024 /// lookup API.
12026 {
12027  LOOKUP_ENTITY_TYPE,
12028  LOOKUP_ENTITY_VAR,
12029 };
12030 
12031 /// Find the first relevant delimiter (the "::" string) in a fully
12032 /// qualified C++ type name, starting from a given position. The
12033 /// delimiter returned separates a type name from the name of its
12034 /// context.
12035 ///
12036 /// This is supposed to work correctly on names in cases like this:
12037 ///
12038 /// foo<ns1::name1, ns2::name2>
12039 ///
12040 /// In that case when called with with parameter @p begin set to 0, no
12041 /// delimiter is returned, because the type name in this case is:
12042 /// 'foo<ns1::name1, ns2::name2>'.
12043 ///
12044 /// But in this case:
12045 ///
12046 /// foo<p1, bar::name>::some_type
12047 ///
12048 /// The "::" returned is the one right before 'some_type'.
12049 ///
12050 /// @param fqn the fully qualified name of the type to consider.
12051 ///
12052 /// @param begin the position from which to look for the delimiter.
12053 ///
12054 /// @param delim_pos out parameter. Is set to the position of the
12055 /// delimiter iff the function returned true.
12056 ///
12057 /// @return true iff the function found and returned the delimiter.
12058 static bool
12059 find_next_delim_in_cplus_type(const string& fqn,
12060  size_t begin,
12061  size_t& delim_pos)
12062 {
12063  int angle_count = 0;
12064  bool found = false;
12065  size_t i = begin;
12066  for (; i < fqn.size(); ++i)
12067  {
12068  if (fqn[i] == '<')
12069  ++angle_count;
12070  else if (fqn[i] == '>')
12071  --angle_count;
12072  else if (i + 1 < fqn.size()
12073  && !angle_count
12074  && fqn[i] == ':'
12075  && fqn[i+1] == ':')
12076  {
12077  delim_pos = i;
12078  found = true;
12079  break;
12080  }
12081  }
12082  return found;
12083 }
12084 
12085 /// Decompose a fully qualified name into the list of its components.
12086 ///
12087 /// @param fqn the fully qualified name to decompose.
12088 ///
12089 /// @param comps the resulting list of component to fill.
12090 void
12091 fqn_to_components(const string& fqn,
12092  list<string>& comps)
12093 {
12094  string::size_type fqn_size = fqn.size(), comp_begin = 0, comp_end = fqn_size;
12095  do
12096  {
12097  if (!find_next_delim_in_cplus_type(fqn, comp_begin, comp_end))
12098  comp_end = fqn_size;
12099 
12100  string comp = fqn.substr(comp_begin, comp_end - comp_begin);
12101  comps.push_back(comp);
12102 
12103  comp_begin = comp_end + 2;
12104  if (comp_begin >= fqn_size)
12105  break;
12106  } while (true);
12107 }
12108 
12109 /// Turn a set of qualified name components (that name a type) into a
12110 /// qualified name string.
12111 ///
12112 /// @param comps the name components
12113 ///
12114 /// @return the resulting string, which would be the qualified name of
12115 /// a type.
12116 string
12117 components_to_type_name(const list<string>& comps)
12118 {
12119  string result;
12120  for (list<string>::const_iterator c = comps.begin();
12121  c != comps.end();
12122  ++c)
12123  if (c == comps.begin())
12124  result = *c;
12125  else
12126  result += "::" + *c;
12127  return result;
12128 }
12129 
12130 /// This predicate returns true if a given container iterator points
12131 /// to the last element of the container, false otherwise.
12132 ///
12133 /// @tparam T the type of the container of the iterator.
12134 ///
12135 /// @param container the container the iterator points into.
12136 ///
12137 /// @param i the iterator to consider.
12138 ///
12139 /// @return true iff the iterator points to the last element of @p
12140 /// container.
12141 template<typename T>
12142 static bool
12143 iterator_is_last(T& container,
12144  typename T::const_iterator i)
12145 {
12146  typename T::const_iterator next = i;
12147  ++next;
12148  return (next == container.end());
12149 }
12150 
12151 //--------------------------------
12152 // <type and decls lookup stuff>
12153 // ------------------------------
12154 
12155 /// Lookup all the type*s* that have a given fully qualified name.
12156 ///
12157 /// @param type_name the fully qualified name of the type to
12158 /// lookup.
12159 ///
12160 /// @param type_map the map to look into.
12161 ///
12162 /// @return the vector containing the types named @p type_name. If
12163 /// the lookup didn't yield any type, then this function returns nil.
12164 static const type_base_wptrs_type*
12165 lookup_types_in_map(const interned_string& type_name,
12166  const istring_type_base_wptrs_map_type& type_map)
12167 {
12168  istring_type_base_wptrs_map_type::const_iterator i = type_map.find(type_name);
12169  if (i != type_map.end())
12170  return &i->second;
12171  return 0;
12172 }
12173 
12174 /// Lookup a type (with a given name) in a map that associates a type
12175 /// name to a type. If there are several types with a given name,
12176 /// then try to return the first one that is not decl-only.
12177 /// Otherwise, return the last of such types, that is, the last one
12178 /// that got registered.
12179 ///
12180 /// @tparam TypeKind the type of the type this function is supposed to
12181 /// return.
12182 ///
12183 /// @param type_name the name of the type to lookup.
12184 ///
12185 /// @param type_map the map in which to look.
12186 ///
12187 /// @return a shared_ptr to the type found. If no type was found or
12188 /// if the type found was not of type @p TypeKind then the function
12189 /// returns nil.
12190 template <class TypeKind>
12191 static shared_ptr<TypeKind>
12192 lookup_type_in_map(const interned_string& type_name,
12193  const istring_type_base_wptrs_map_type& type_map)
12194 {
12195  istring_type_base_wptrs_map_type::const_iterator i = type_map.find(type_name);
12196  if (i != type_map.end())
12197  {
12198  // Walk the types that have the name "type_name" and return the
12199  // first one that is not declaration-only ...
12200  for (auto j : i->second)
12201  {
12202  type_base_sptr t(j);
12203  decl_base_sptr d = is_decl(t);
12204  if (d && !d->get_is_declaration_only())
12205  return dynamic_pointer_cast<TypeKind>(type_base_sptr(j));
12206  }
12207  // ... or return the last type with the name "type_name" that
12208  // was recorded. It's likely to be declaration-only if we
12209  // reached this point.
12210  return dynamic_pointer_cast<TypeKind>(type_base_sptr(i->second.back()));
12211  }
12212  return shared_ptr<TypeKind>();
12213 }
12214 
12215 /// Lookup a basic type from a translation unit.
12216 ///
12217 /// This is done by looking the type up in the type map that is
12218 /// maintained in the translation unit. So this is as fast as
12219 /// possible.
12220 ///
12221 /// @param type_name the name of the basic type to look for.
12222 ///
12223 /// @param tu the translation unit to look into.
12224 ///
12225 /// @return the basic type found or nil if no basic type was found.
12228 {
12229  return lookup_type_in_map<type_decl>(type_name,
12230  tu.get_types().basic_types());
12231 }
12232 
12233 /// Lookup a basic type from a translation unit.
12234 ///
12235 /// This is done by looking the type up in the type map that is
12236 /// maintained in the translation unit. So this is as fast as
12237 /// possible.
12238 ///
12239 /// @param type_name the name of the basic type to look for.
12240 ///
12241 /// @param tu the translation unit to look into.
12242 ///
12243 /// @return the basic type found or nil if no basic type was found.
12245 lookup_basic_type(const string& type_name, const translation_unit& tu)
12246 {
12247  const environment& env = tu.get_environment();
12248 
12249  interned_string s = env.intern(type_name);
12250  return lookup_basic_type(s, tu);
12251 }
12252 
12253 /// Lookup a class type from a translation unit.
12254 ///
12255 /// This is done by looking the type up in the type map that is
12256 /// maintained in the translation unit. So this is as fast as
12257 /// possible.
12258 ///
12259 /// @param fqn the fully qualified name of the class type node to look
12260 /// up.
12261 ///
12262 /// @param tu the translation unit to perform lookup from.
12263 ///
12264 /// @return the declaration of the class type IR node found, NULL
12265 /// otherwise.
12267 lookup_class_type(const string& fqn, const translation_unit& tu)
12268 {
12269  const environment& env = tu.get_environment();
12270  interned_string s = env.intern(fqn);
12271  return lookup_class_type(s, tu);
12272 }
12273 
12274 /// Lookup a class type from a translation unit.
12275 ///
12276 /// This is done by looking the type up in the type map that is
12277 /// maintained in the translation unit. So this is as fast as
12278 /// possible.
12279 ///
12280 /// @param type_name the name of the class type to look for.
12281 ///
12282 /// @param tu the translation unit to look into.
12283 ///
12284 /// @return the class type found or nil if no class type was found.
12287 {
12288  return lookup_type_in_map<class_decl>(type_name,
12289  tu.get_types().class_types());
12290 }
12291 
12292 /// Lookup a union type from a translation unit.
12293 ///
12294 /// This is done by looking the type up in the type map that is
12295 /// maintained in the translation unit. So this is as fast as
12296 /// possible.
12297 ///
12298 /// @param type_name the name of the union type to look for.
12299 ///
12300 /// @param tu the translation unit to look into.
12301 ///
12302 /// @return the union type found or nil if no union type was found.
12303 union_decl_sptr
12305 {
12306  return lookup_type_in_map<union_decl>(type_name,
12307  tu.get_types().union_types());
12308 }
12309 
12310 /// Lookup a union type from a translation unit.
12311 ///
12312 /// This is done by looking the type up in the type map that is
12313 /// maintained in the translation unit. So this is as fast as
12314 /// possible.
12315 ///
12316 /// @param fqn the fully qualified name of the type to lookup.
12317 ///
12318 /// @param tu the translation unit to look into.
12319 ///
12320 /// @return the union type found or nil if no union type was found.
12321 union_decl_sptr
12322 lookup_union_type(const string& fqn, const translation_unit& tu)
12323 {
12324  const environment& env = tu.get_environment();
12325  interned_string s = env.intern(fqn);
12326  return lookup_union_type(s, tu);
12327 }
12328 
12329 /// Lookup a union type in a given corpus, from its location.
12330 ///
12331 /// @param loc the location of the union type to look for.
12332 ///
12333 /// @param corp the corpus to look it from.
12334 ///
12335 /// @return the resulting union_decl.
12336 union_decl_sptr
12338 {
12341  union_decl_sptr result = lookup_type_in_map<union_decl>(loc, m);
12342 
12343  return result;
12344 }
12345 
12346 /// Lookup a union type in a given corpus, from its location.
12347 ///
12348 /// @param loc the location of the union type to look for.
12349 ///
12350 /// @param corp the corpus to look it from.
12351 ///
12352 /// @return the resulting union_decl.
12353 union_decl_sptr
12354 lookup_union_type_per_location(const string& loc, const corpus& corp)
12355 {
12356  const environment& env = corp.get_environment();
12357  return lookup_union_type_per_location(env.intern(loc), corp);
12358 }
12359 
12360 /// Lookup an enum type from a translation unit.
12361 ///
12362 /// This is done by looking the type up in the type map that is
12363 /// maintained in the translation unit. So this is as fast as
12364 /// possible.
12365 ///
12366 /// @param type_name the name of the enum type to look for.
12367 ///
12368 /// @param tu the translation unit to look into.
12369 ///
12370 /// @return the enum type found or nil if no enum type was found.
12373 {
12374  return lookup_type_in_map<enum_type_decl>(type_name,
12375  tu.get_types().enum_types());
12376 }
12377 
12378 /// Lookup an enum type from a translation unit.
12379 ///
12380 /// This is done by looking the type up in the type map that is
12381 /// maintained in the translation unit. So this is as fast as
12382 /// possible.
12383 ///
12384 /// @param type_name the name of the enum type to look for.
12385 ///
12386 /// @param tu the translation unit to look into.
12387 ///
12388 /// @return the enum type found or nil if no enum type was found.
12390 lookup_enum_type(const string& type_name, const translation_unit& tu)
12391 {
12392  const environment& env = tu.get_environment();
12393  interned_string s = env.intern(type_name);
12394  return lookup_enum_type(s, tu);
12395 }
12396 
12397 /// Lookup a typedef type from a translation unit.
12398 ///
12399 /// This is done by looking the type up in the type map that is
12400 /// maintained in the translation unit. So this is as fast as
12401 /// possible.
12402 ///
12403 /// @param type_name the name of the typedef type to look for.
12404 ///
12405 /// @param tu the translation unit to look into.
12406 ///
12407 /// @return the typedef type found or nil if no typedef type was
12408 /// found.
12411  const translation_unit& tu)
12412 {
12413  return lookup_type_in_map<typedef_decl>(type_name,
12414  tu.get_types().typedef_types());
12415 }
12416 
12417 /// Lookup a typedef type from a translation unit.
12418 ///
12419 /// This is done by looking the type up in the type map that is
12420 /// maintained in the translation unit. So this is as fast as
12421 /// possible.
12422 ///
12423 /// @param type_name the name of the typedef type to look for.
12424 ///
12425 /// @param tu the translation unit to look into.
12426 ///
12427 /// @return the typedef type found or nil if no typedef type was
12428 /// found.
12430 lookup_typedef_type(const string& type_name, const translation_unit& tu)
12431 {
12432  const environment& env = tu.get_environment();
12433  interned_string s = env.intern(type_name);
12434  return lookup_typedef_type(s, tu);
12435 }
12436 
12437 /// Lookup a qualified type from a translation unit.
12438 ///
12439 /// This is done by looking the type up in the type map that is
12440 /// maintained in the translation unit. So this is as fast as
12441 /// possible.
12442 ///
12443 /// @param type_name the name of the qualified type to look for.
12444 ///
12445 /// @param tu the translation unit to look into.
12446 ///
12447 /// @return the qualified type found or nil if no qualified type was
12448 /// found.
12449 qualified_type_def_sptr
12451  const translation_unit& tu)
12452 {
12453  const type_maps& m = tu.get_types();
12454  return lookup_type_in_map<qualified_type_def>(type_name,
12455  m.qualified_types());
12456 }
12457 
12458 /// Lookup a qualified type from a translation unit.
12459 ///
12460 /// This is done by looking the type up in the type map that is
12461 /// maintained in the translation unit. So this is as fast as
12462 /// possible.
12463 ///
12464 /// @param underlying_type the underying type of the qualified type to
12465 /// look up.
12466 ///
12467 /// @param quals the CV-qualifiers of the qualified type to look for.
12468 ///
12469 /// @param tu the translation unit to look into.
12470 ///
12471 /// @return the qualified type found or nil if no qualified type was
12472 /// found.
12473 qualified_type_def_sptr
12474 lookup_qualified_type(const type_base_sptr& underlying_type,
12475  qualified_type_def::CV quals,
12476  const translation_unit& tu)
12477 {
12478  interned_string type_name = get_name_of_qualified_type(underlying_type,
12479  quals);
12480  return lookup_qualified_type(type_name, tu);
12481 }
12482 
12483 /// Lookup a pointer type from a translation unit.
12484 ///
12485 /// This is done by looking the type up in the type map that is
12486 /// maintained in the translation unit. So this is as fast as
12487 /// possible.
12488 ///
12489 /// @param type_name the name of the pointer type to look for.
12490 ///
12491 /// @param tu the translation unit to look into.
12492 ///
12493 /// @return the pointer type found or nil if no pointer type was
12494 /// found.
12497  const translation_unit& tu)
12498 {
12499  const type_maps& m = tu.get_types();
12500  return lookup_type_in_map<pointer_type_def>(type_name,
12501  m.pointer_types());
12502 }
12503 
12504 /// Lookup a pointer type from a translation unit.
12505 ///
12506 /// This is done by looking the type up in the type map that is
12507 /// maintained in the translation unit. So this is as fast as
12508 /// possible.
12509 ///
12510 /// @param type_name the name of the pointer type to look for.
12511 ///
12512 /// @param tu the translation unit to look into.
12513 ///
12514 /// @return the pointer type found or nil if no pointer type was
12515 /// found.
12517 lookup_pointer_type(const string& type_name, const translation_unit& tu)
12518 {
12519  const environment& env = tu.get_environment();
12520  interned_string s = env.intern(type_name);
12521  return lookup_pointer_type(s, tu);
12522 }
12523 
12524 /// Lookup a pointer type from a translation unit.
12525 ///
12526 /// This is done by looking the type up in the type map that is
12527 /// maintained in the translation unit. So this is as fast as
12528 /// possible.
12529 ///
12530 /// @param pointed_to_type the pointed-to-type of the pointer to look for.
12531 ///
12532 /// @param tu the translation unit to look into.
12533 ///
12534 /// @return the pointer type found or nil if no pointer type was
12535 /// found.
12537 lookup_pointer_type(const type_base_sptr& pointed_to_type,
12538  const translation_unit& tu)
12539 {
12540  type_base_sptr t = look_through_decl_only(pointed_to_type);
12542  return lookup_pointer_type(type_name, tu);
12543 }
12544 
12545 /// Lookup a reference type from a translation unit.
12546 ///
12547 /// This is done by looking the type up in the type map that is
12548 /// maintained in the translation unit. So this is as fast as
12549 /// possible.
12550 ///
12551 /// @param type_name the name of the reference type to look for.
12552 ///
12553 /// @param tu the translation unit to look into.
12554 ///
12555 /// @return the reference type found or nil if no reference type was
12556 /// found.
12559  const translation_unit& tu)
12560 {
12561  const type_maps& m = tu.get_types();
12562  return lookup_type_in_map<reference_type_def>(type_name,
12563  m.reference_types());
12564 }
12565 
12566 /// Lookup a reference type from a translation unit.
12567 ///
12568 /// This is done by looking the type up in the type map that is
12569 /// maintained in the translation unit. So this is as fast as
12570 /// possible.
12571 ///
12572 /// @param pointed_to_type the pointed-to-type of the reference to
12573 /// look up.
12574 ///
12575 /// @param tu the translation unit to look into.
12576 ///
12577 /// @return the reference type found or nil if no reference type was
12578 /// found.
12580 lookup_reference_type(const type_base_sptr& pointed_to_type,
12581  bool lvalue_reference,
12582  const translation_unit& tu)
12583 {
12584  interned_string type_name =
12586  lvalue_reference);
12587  return lookup_reference_type(type_name, tu);
12588 }
12589 
12590 /// Lookup an array type from a translation unit.
12591 ///
12592 /// This is done by looking the type up in the type map that is
12593 /// maintained in the translation unit. So this is as fast as
12594 /// possible.
12595 ///
12596 /// @param type_name the name of the array type to look for.
12597 ///
12598 /// @param tu the translation unit to look into.
12599 ///
12600 /// @return the array type found or nil if no array type was found.
12603  const translation_unit& tu)
12604 {
12605  const type_maps& m = tu.get_types();
12606  return lookup_type_in_map<array_type_def>(type_name,
12607  m.array_types());
12608 }
12609 
12610 /// Lookup a function type from a translation unit.
12611 ///
12612 /// This is done by looking the type up in the type map that is
12613 /// maintained in the translation unit. So this is as fast as
12614 /// possible.
12615 ///
12616 /// @param type_name the name of the type to lookup.
12617 ///
12618 /// @param tu the translation unit to look into.
12619 ///
12620 /// @return the function type found, or NULL of none was found.
12623  const translation_unit& tu)
12624 {
12625  const type_maps& m = tu.get_types();
12626  return lookup_type_in_map<function_type>(type_name,
12627  m.function_types());
12628 }
12629 
12630 /// Lookup a function type from a translation unit.
12631 ///
12632 /// This walks all the function types held by the translation unit and
12633 /// compare their sub-type *names*. If the names match then return
12634 /// the function type found in the translation unit.
12635 ///
12636 /// @param t the function type to look for.
12637 ///
12638 /// @param tu the translation unit to look into.
12639 ///
12640 /// @return the function type found, or NULL of none was found.
12643  const translation_unit& tu)
12644 {
12645  interned_string type_name = get_type_name(t);
12646  return lookup_function_type(type_name, tu);
12647 }
12648 
12649 /// Lookup a function type from a translation unit.
12650 ///
12651 /// This is done by looking the type up in the type map that is
12652 /// maintained in the translation unit. So this is as fast as
12653 /// possible.
12654 ///
12655 /// @param t the function type to look for.
12656 ///
12657 /// @param tu the translation unit to look into.
12658 ///
12659 /// @return the function type found, or NULL of none was found.
12662  const translation_unit& tu)
12663 {return lookup_function_type(*t, tu);}
12664 
12665 /// Lookup a type in a translation unit.
12666 ///
12667 /// @param fqn the fully qualified name of the type to lookup.
12668 ///
12669 /// @param tu the translation unit to consider.
12670 ///
12671 /// @return the declaration of the type if found, NULL otherwise.
12672 const type_base_sptr
12674  const translation_unit& tu)
12675 {
12676  type_base_sptr result;
12677  ((result = lookup_typedef_type(fqn, tu))
12678  || (result = lookup_class_type(fqn, tu))
12679  || (result = lookup_union_type(fqn, tu))
12680  || (result = lookup_enum_type(fqn, tu))
12681  || (result = lookup_qualified_type(fqn, tu))
12682  || (result = lookup_pointer_type(fqn, tu))
12683  || (result = lookup_reference_type(fqn, tu))
12684  || (result = lookup_array_type(fqn, tu))
12685  || (result = lookup_function_type(fqn, tu))
12686  || (result = lookup_basic_type(fqn, tu)));
12687 
12688  return result;
12689 }
12690 
12691 /// Lookup a type in a translation unit, starting from the global
12692 /// namespace.
12693 ///
12694 /// @param fqn the fully qualified name of the type to lookup.
12695 ///
12696 /// @param tu the translation unit to consider.
12697 ///
12698 /// @return the declaration of the type if found, NULL otherwise.
12699 type_base_sptr
12700 lookup_type(const string& fqn, const translation_unit& tu)
12701 {
12702  const environment&env = tu.get_environment();
12703  interned_string ifqn = env.intern(fqn);
12704  return lookup_type(ifqn, tu);
12705 }
12706 
12707 /// Lookup a type from a translation unit.
12708 ///
12709 /// @param fqn the components of the fully qualified name of the node
12710 /// to look up.
12711 ///
12712 /// @param tu the translation unit to perform lookup from.
12713 ///
12714 /// @return the declaration of the IR node found, NULL otherwise.
12715 const type_base_sptr
12716 lookup_type(const type_base_sptr type,
12717  const translation_unit& tu)
12718 {
12719  interned_string type_name = get_type_name(type);
12720  return lookup_type(type_name, tu);
12721 }
12722 
12723 /// Lookup a type in a scope.
12724 ///
12725 /// This is really slow as it walks the member types of the scope in
12726 /// sequence to find the type with a given name.
12727 ///
12728 /// If possible, users should prefer looking up types from the
12729 /// enclosing translation unit or even ABI corpus because both the
12730 /// translation unit and the corpus have a map of type, indexed by
12731 /// their name. Looking up a type from those maps is thus much
12732 /// faster.
12733 ///
12734 /// @param fqn the fully qualified name of the type to lookup.
12735 ///
12736 /// @param skope the scope to look into.
12737 ///
12738 /// @return the declaration of the type if found, NULL otherwise.
12739 const type_base_sptr
12740 lookup_type_in_scope(const string& fqn,
12741  const scope_decl_sptr& skope)
12742 {
12743  list<string> comps;
12744  fqn_to_components(fqn, comps);
12745  return lookup_type_in_scope(comps, skope);
12746 }
12747 
12748 /// Lookup a @ref var_decl in a scope.
12749 ///
12750 /// @param fqn the fuly qualified name of the @var_decl to lookup.
12751 ///
12752 /// @param skope the scope to look into.
12753 ///
12754 /// @return the declaration of the @ref var_decl if found, NULL
12755 /// otherwise.
12756 const decl_base_sptr
12757 lookup_var_decl_in_scope(const string& fqn,
12758  const scope_decl_sptr& skope)
12759 {
12760  list<string> comps;
12761  fqn_to_components(fqn, comps);
12762  return lookup_var_decl_in_scope(comps, skope);
12763 }
12764 
12765 /// A generic function (template) to get the name of a node, whatever
12766 /// node it is. This has to be specialized for the kind of node we
12767 /// want.
12768 ///
12769 /// Note that a node is a member of a scope.
12770 ///
12771 /// @tparam NodeKind the kind of node to consider.
12772 ///
12773 /// @param node the node to get the name from.
12774 ///
12775 /// @return the name of the node.
12776 template<typename NodeKind>
12777 static const interned_string&
12778 get_node_name(shared_ptr<NodeKind> node);
12779 
12780 /// Gets the name of a class_decl node.
12781 ///
12782 /// @param node the decl_base node to get the name from.
12783 ///
12784 /// @return the name of the node.
12785 template<>
12786 const interned_string&
12788 {return node->get_name();}
12789 
12790 /// Gets the name of a type_base node.
12791 ///
12792 /// @param node the type_base node to get the name from.
12793 ///
12794 /// @return the name of the node.
12795 template<>
12796 const interned_string&
12797 get_node_name(type_base_sptr node)
12798 {return get_type_declaration(node)->get_name();}
12799 
12800 /// Gets the name of a var_decl node.
12801 ///
12802 /// @param node the var_decl node to get the name from.
12803 ///
12804 /// @return the name of the node.
12805 template<>
12806 const interned_string&
12808 {return node->get_name();}
12809 
12810 /// Generic function to get the declaration of a given node, whatever
12811 /// it is. There has to be specializations for the kind of the nodes
12812 /// we want to support.
12813 ///
12814 /// @tparam NodeKind the type of the node we are looking at.
12815 ///
12816 /// @return the declaration.
12817 template<typename NodeKind>
12818 static decl_base_sptr
12819 convert_node_to_decl(shared_ptr<NodeKind> node);
12820 
12821 /// Lookup a node in a given scope.
12822 ///
12823 /// @tparam the type of the node to lookup.
12824 ///
12825 /// @param fqn the components of the fully qualified name of the node
12826 /// to lookup.
12827 ///
12828 /// @param skope the scope to look into.
12829 ///
12830 /// @return the declaration of the looked up node, or NULL if it
12831 /// wasn't found.
12832 template<typename NodeKind>
12833 static const type_or_decl_base_sptr
12834 lookup_node_in_scope(const list<string>& fqn,
12835  const scope_decl_sptr& skope)
12836 {
12837  type_or_decl_base_sptr resulting_decl;
12838  shared_ptr<NodeKind> node;
12839  bool it_is_last = false;
12840  scope_decl_sptr cur_scope = skope, new_scope, scope;
12841 
12842  for (list<string>::const_iterator c = fqn.begin(); c != fqn.end(); ++c)
12843  {
12844  new_scope.reset();
12845  it_is_last = iterator_is_last(fqn, c);
12846  for (scope_decl::declarations::const_iterator m =
12847  cur_scope->get_member_decls().begin();
12848  m != cur_scope->get_member_decls().end();
12849  ++m)
12850  {
12851  if (!it_is_last)
12852  {
12853  // looking for a scope
12854  scope = dynamic_pointer_cast<scope_decl>(*m);
12855  if (scope && scope->get_name() == *c)
12856  {
12857  new_scope = scope;
12858  break;
12859  }
12860  }
12861  else
12862  {
12863  //looking for a final type.
12864  node = dynamic_pointer_cast<NodeKind>(*m);
12865  if (node && get_node_name(node) == *c)
12866  {
12867  if (class_decl_sptr cl =
12868  dynamic_pointer_cast<class_decl>(node))
12869  if (cl->get_is_declaration_only()
12870  && !cl->get_definition_of_declaration())
12871  continue;
12872  resulting_decl = node;
12873  break;
12874  }
12875  }
12876  }
12877  if (!new_scope && !resulting_decl)
12878  return decl_base_sptr();
12879  cur_scope = new_scope;
12880  }
12881  ABG_ASSERT(resulting_decl);
12882  return resulting_decl;
12883 }
12884 
12885 /// lookup a type in a scope.
12886 ///
12887 ///
12888 /// This is really slow as it walks the member types of the scope in
12889 /// sequence to find the type with a given name.
12890 ///
12891 /// If possible, users should prefer looking up types from the
12892 /// enclosing translation unit or even ABI corpus because both the
12893 /// translation unit and the corpus have a map of type, indexed by
12894 /// their name. Looking up a type from those maps is thus much
12895 /// faster.
12896 ///
12897 /// @param comps the components of the fully qualified name of the
12898 /// type to lookup.
12899 ///
12900 /// @param skope the scope to look into.
12901 ///
12902 /// @return the declaration of the type found.
12903 const type_base_sptr
12904 lookup_type_in_scope(const list<string>& comps,
12905  const scope_decl_sptr& scope)
12906 {return is_type(lookup_node_in_scope<type_base>(comps, scope));}
12907 
12908 /// lookup a type in a scope.
12909 ///
12910 /// This is really slow as it walks the member types of the scope in
12911 /// sequence to find the type with a given name.
12912 ///
12913 /// If possible, users should prefer looking up types from the
12914 /// enclosing translation unit or even ABI corpus because both the
12915 /// translation unit and the corpus have a map of type, indexed by
12916 /// their name. Looking up a type from those maps is thus much
12917 /// faster.
12918 ///
12919 /// @param type the type to look for.
12920 ///
12921 /// @param access_path a vector of scopes the path of scopes to follow
12922 /// before reaching the scope into which to look for @p type. Note
12923 /// that the deepest scope (the one immediately containing @p type) is
12924 /// at index 0 of this vector, and the top-most scope is the last
12925 /// element of the vector.
12926 ///
12927 /// @param scope the top-most scope into which to look for @p type.
12928 ///
12929 /// @return the scope found in @p scope, or NULL if it wasn't found.
12930 static const type_base_sptr
12931 lookup_type_in_scope(const type_base& type,
12932  const vector<scope_decl*>& access_path,
12933  const scope_decl* scope)
12934 {
12935  vector<scope_decl*> a = access_path;
12936  type_base_sptr result;
12937 
12938  scope_decl* first_scope = 0;
12939  if (!a.empty())
12940  {
12941  first_scope = a.back();
12942  ABG_ASSERT(first_scope->get_name() == scope->get_name());
12943  a.pop_back();
12944  }
12945 
12946  if (a.empty())
12947  {
12948  interned_string n = get_type_name(type, false);
12949  for (scope_decl::declarations::const_iterator i =
12950  scope->get_member_decls().begin();
12951  i != scope->get_member_decls().end();
12952  ++i)
12953  if (is_type(*i) && (*i)->get_name() == n)
12954  {
12955  result = is_type(*i);
12956  break;
12957  }
12958  }
12959  else
12960  {
12961  first_scope = a.back();
12962  interned_string scope_name, cur_scope_name = first_scope->get_name();
12963  for (scope_decl::scopes::const_iterator i =
12964  scope->get_member_scopes().begin();
12965  i != scope->get_member_scopes().end();
12966  ++i)
12967  {
12968  scope_name = (*i)->get_name();
12969  if (scope_name == cur_scope_name)
12970  {
12971  result = lookup_type_in_scope(type, a, (*i).get());
12972  break;
12973  }
12974  }
12975  }
12976  return result;
12977 }
12978 
12979 /// lookup a type in a scope.
12980 ///
12981 /// This is really slow as it walks the member types of the scope in
12982 /// sequence to find the type with a given name.
12983 ///
12984 /// If possible, users should prefer looking up types from the
12985 /// enclosing translation unit or even ABI corpus because both the
12986 /// translation unit and the corpus have a map of type, indexed by
12987 /// their name. Looking up a type from those maps is thus much
12988 /// faster.
12989 ///
12990 /// @param type the type to look for.
12991 ///
12992 /// @param scope the top-most scope into which to look for @p type.
12993 ///
12994 /// @return the scope found in @p scope, or NULL if it wasn't found.
12995 static const type_base_sptr
12996 lookup_type_in_scope(const type_base_sptr type,
12997  const scope_decl* scope)
12998 {
12999  if (!type || is_function_type(type))
13000  return type_base_sptr();
13001 
13002  decl_base_sptr type_decl = get_type_declaration(type);
13003  ABG_ASSERT(type_decl);
13004  vector<scope_decl*> access_path;
13005  for (scope_decl* s = type_decl->get_scope(); s != 0; s = s->get_scope())
13006  {
13007  access_path.push_back(s);
13008  if (is_global_scope(s))
13009  break;
13010  }
13011  return lookup_type_in_scope(*type, access_path, scope);
13012 }
13013 
13014 /// Lookup a type from a translation unit by walking the scopes of the
13015 /// translation unit in sequence and looking into them.
13016 ///
13017 /// This is really slow as it walks the member types of the scopes in
13018 /// sequence to find the type with a given name.
13019 ///
13020 /// If possible, users should prefer looking up types from the
13021 /// translation unit or even ABI corpus in a more direct way, by using
13022 /// the lookup_type() functins.
13023 ///
13024 ///
13025 /// This is because both the translation unit and the corpus have a
13026 /// map of types, indexed by their name. Looking up a type from those
13027 /// maps is thus much faster. @param fqn the components of the fully
13028 /// qualified name of the node to look up.
13029 ///
13030 /// @param tu the translation unit to perform lookup from.
13031 ///
13032 /// @return the declaration of the IR node found, NULL otherwise.
13033 const type_base_sptr
13034 lookup_type_through_scopes(const type_base_sptr type,
13035  const translation_unit& tu)
13036 {
13037  if (function_type_sptr fn_type = is_function_type(type))
13038  return lookup_function_type(fn_type, tu);
13039  return lookup_type_in_scope(type, tu.get_global_scope().get());
13040 }
13041 
13042 /// lookup a var_decl in a scope.
13043 ///
13044 /// @param comps the components of the fully qualified name of the
13045 /// var_decl to lookup.
13046 ///
13047 /// @param skope the scope to look into.
13048 const decl_base_sptr
13049 lookup_var_decl_in_scope(const std::list<string>& comps,
13050  const scope_decl_sptr& skope)
13051 {return is_var_decl(lookup_node_in_scope<var_decl>(comps, skope));}
13052 
13053 /// Lookup an IR node from a translation unit.
13054 ///
13055 /// @tparam NodeKind the type of the IR node to lookup from the
13056 /// translation unit.
13057 ///
13058 /// @param fqn the components of the fully qualified name of the node
13059 /// to look up.
13060 ///
13061 /// @param tu the translation unit to perform lookup from.
13062 ///
13063 /// @return the declaration of the IR node found, NULL otherwise.
13064 template<typename NodeKind>
13065 static const type_or_decl_base_sptr
13066 lookup_node_in_translation_unit(const list<string>& fqn,
13067  const translation_unit& tu)
13068 {return lookup_node_in_scope<NodeKind>(fqn, tu.get_global_scope());}
13069 
13070 /// Lookup a type from a translation unit by walking its scopes in
13071 /// sequence and by looking into them.
13072 ///
13073 /// This is much slower than using the lookup_type() function.
13074 ///
13075 /// @param fqn the components of the fully qualified name of the node
13076 /// to look up.
13077 ///
13078 /// @param tu the translation unit to perform lookup from.
13079 ///
13080 /// @return the declaration of the IR node found, NULL otherwise.
13081 type_base_sptr
13082 lookup_type_through_scopes(const list<string>& fqn,
13083  const translation_unit& tu)
13084 {return is_type(lookup_node_in_translation_unit<type_base>(fqn, tu));}
13085 
13086 
13087 /// Lookup a class type from a translation unit by walking its scopes
13088 /// in sequence and by looking into them.
13089 ///
13090 /// This is much slower than using the lookup_class_type() function
13091 /// because it walks all the scopes of the translation unit in
13092 /// sequence and lookup the types to find one that has a given name.
13093 ///
13094 /// @param fqn the components of the fully qualified name of the class
13095 /// type node to look up.
13096 ///
13097 /// @param tu the translation unit to perform lookup from.
13098 ///
13099 /// @return the declaration of the class type IR node found, NULL
13100 /// otherwise.
13102 lookup_class_type_through_scopes(const list<string>& fqn,
13103  const translation_unit& tu)
13104 {return is_class_type(lookup_node_in_translation_unit<class_decl>(fqn, tu));}
13105 
13106 /// Lookup a basic type from all the translation units of a given
13107 /// corpus.
13108 ///
13109 /// @param fqn the components of the fully qualified name of the basic
13110 /// type node to look up.
13111 ///
13112 /// @param tu the translation unit to perform lookup from.
13113 ///
13114 /// @return the declaration of the basic type IR node found, NULL
13115 /// otherwise.
13116 static type_decl_sptr
13117 lookup_basic_type_through_translation_units(const interned_string& type_name,
13118  const corpus& abi_corpus)
13119 {
13120  type_decl_sptr result;
13121 
13122  for (translation_units::const_iterator tu =
13123  abi_corpus.get_translation_units().begin();
13124  tu != abi_corpus.get_translation_units().end();
13125  ++tu)
13126  if ((result = lookup_basic_type(type_name, **tu)))
13127  break;
13128 
13129  return result;
13130 }
13131 
13132 /// Lookup a union type from all the translation units of a given
13133 /// corpus.
13134 ///
13135 /// @param fqn the components of the fully qualified name of the union
13136 /// type node to look up.
13137 ///
13138 /// @param tu the translation unit to perform lookup from.
13139 ///
13140 /// @return the declaration of the union type IR node found, NULL
13141 /// otherwise.
13142 static union_decl_sptr
13143 lookup_union_type_through_translation_units(const interned_string& type_name,
13144  const corpus & abi_corpus)
13145 {
13146  union_decl_sptr result;
13147 
13148  for (translation_units::const_iterator tu =
13149  abi_corpus.get_translation_units().begin();
13150  tu != abi_corpus.get_translation_units().end();
13151  ++tu)
13152  if ((result = lookup_union_type(type_name, **tu)))
13153  break;
13154 
13155  return result;
13156 }
13157 
13158 /// Lookup an enum type from all the translation units of a given
13159 /// corpus.
13160 ///
13161 /// @param fqn the components of the fully qualified name of the enum
13162 /// type node to look up.
13163 ///
13164 /// @param tu the translation unit to perform lookup from.
13165 ///
13166 /// @return the declaration of the enum type IR node found, NULL
13167 /// otherwise.
13168 static enum_type_decl_sptr
13169 lookup_enum_type_through_translation_units(const interned_string& type_name,
13170  const corpus & abi_corpus)
13171 {
13172  enum_type_decl_sptr result;
13173 
13174  for (translation_units::const_iterator tu =
13175  abi_corpus.get_translation_units().begin();
13176  tu != abi_corpus.get_translation_units().end();
13177  ++tu)
13178  if ((result = lookup_enum_type(type_name, **tu)))
13179  break;
13180 
13181  return result;
13182 }
13183 
13184 /// Lookup a typedef type definition in all the translation units of a
13185 /// given ABI corpus.
13186 ///
13187 /// @param @param qn the fully qualified name of the typedef type to lookup.
13188 ///
13189 /// @param abi_corpus the ABI corpus which to look the type up in.
13190 ///
13191 /// @return the type definition if any was found, or a NULL pointer.
13192 static typedef_decl_sptr
13193 lookup_typedef_type_through_translation_units(const interned_string& type_name,
13194  const corpus & abi_corpus)
13195 {
13196  typedef_decl_sptr result;
13197 
13198  for (translation_units::const_iterator tu =
13199  abi_corpus.get_translation_units().begin();
13200  tu != abi_corpus.get_translation_units().end();
13201  ++tu)
13202  if ((result = lookup_typedef_type(type_name, **tu)))
13203  break;
13204 
13205  return result;
13206 }
13207 
13208 /// Lookup a qualified type definition in all the translation units of a
13209 /// given ABI corpus.
13210 ///
13211 /// @param @param qn the fully qualified name of the qualified type to
13212 /// lookup.
13213 ///
13214 /// @param abi_corpus the ABI corpus which to look the type up in.
13215 ///
13216 /// @return the type definition if any was found, or a NULL pointer.
13217 static qualified_type_def_sptr
13218 lookup_qualified_type_through_translation_units(const interned_string& t_name,
13219  const corpus & abi_corpus)
13220 {
13221  qualified_type_def_sptr result;
13222 
13223  for (translation_units::const_iterator tu =
13224  abi_corpus.get_translation_units().begin();
13225  tu != abi_corpus.get_translation_units().end();
13226  ++tu)
13227  if ((result = lookup_qualified_type(t_name, **tu)))
13228  break;
13229 
13230  return result;
13231 }
13232 
13233 /// Lookup a pointer type definition in all the translation units of a
13234 /// given ABI corpus.
13235 ///
13236 /// @param @param qn the fully qualified name of the pointer type to
13237 /// lookup.
13238 ///
13239 /// @param abi_corpus the ABI corpus which to look the type up in.
13240 ///
13241 /// @return the type definition if any was found, or a NULL pointer.
13242 static pointer_type_def_sptr
13243 lookup_pointer_type_through_translation_units(const interned_string& type_name,
13244  const corpus & abi_corpus)
13245 {
13246  pointer_type_def_sptr result;
13247 
13248  for (translation_units::const_iterator tu =
13249  abi_corpus.get_translation_units().begin();
13250  tu != abi_corpus.get_translation_units().end();
13251  ++tu)
13252  if ((result = lookup_pointer_type(type_name, **tu)))
13253  break;
13254 
13255  return result;
13256 }
13257 
13258 /// Lookup a reference type definition in all the translation units of a
13259 /// given ABI corpus.
13260 ///
13261 /// @param @param qn the fully qualified name of the reference type to
13262 /// lookup.
13263 ///
13264 /// @param abi_corpus the ABI corpus which to look the type up in.
13265 ///
13266 /// @return the type definition if any was found, or a NULL pointer.
13268 lookup_reference_type_through_translation_units(const interned_string& t_name,
13269  const corpus & abi_corpus)
13270 {
13271  reference_type_def_sptr result;
13272 
13273  for (translation_units::const_iterator tu =
13274  abi_corpus.get_translation_units().begin();
13275  tu != abi_corpus.get_translation_units().end();
13276  ++tu)
13277  if ((result = lookup_reference_type(t_name, **tu)))
13278  break;
13279 
13280  return result;
13281 }
13282 
13283 /// Lookup a array type definition in all the translation units of a
13284 /// given ABI corpus.
13285 ///
13286 /// @param @param qn the fully qualified name of the array type to
13287 /// lookup.
13288 ///
13289 /// @param abi_corpus the ABI corpus which to look the type up in.
13290 ///
13291 /// @return the type definition if any was found, or a NULL pointer.
13292 static array_type_def_sptr
13293 lookup_array_type_through_translation_units(const interned_string& type_name,
13294  const corpus & abi_corpus)
13295 {
13296  array_type_def_sptr result;
13297 
13298  for (translation_units::const_iterator tu =
13299  abi_corpus.get_translation_units().begin();
13300  tu != abi_corpus.get_translation_units().end();
13301  ++tu)
13302  if ((result = lookup_array_type(type_name, **tu)))
13303  break;
13304 
13305  return result;
13306 }
13307 
13308 /// Lookup a function type definition in all the translation units of
13309 /// a given ABI corpus.
13310 ///
13311 /// @param @param qn the fully qualified name of the function type to
13312 /// lookup.
13313 ///
13314 /// @param abi_corpus the ABI corpus which to look the type up in.
13315 ///
13316 /// @return the type definition if any was found, or a NULL pointer.
13317 static function_type_sptr
13318 lookup_function_type_through_translation_units(const interned_string& type_name,
13319  const corpus & abi_corpus)
13320 {
13321  function_type_sptr result;
13322 
13323  for (translation_units::const_iterator tu =
13324  abi_corpus.get_translation_units().begin();
13325  tu != abi_corpus.get_translation_units().end();
13326  ++tu)
13327  if ((result = lookup_function_type(type_name, **tu)))
13328  break;
13329 
13330  return result;
13331 }
13332 
13333 /// Lookup a type definition in all the translation units of a given
13334 /// ABI corpus.
13335 ///
13336 /// @param @param qn the fully qualified name of the type to lookup.
13337 ///
13338 /// @param abi_corpus the ABI corpus which to look the type up in.
13339 ///
13340 /// @return the type definition if any was found, or a NULL pointer.
13341 type_base_sptr
13343  const corpus& abi_corpus)
13344 {
13345  type_base_sptr result;
13346 
13347  for (translation_units::const_iterator tu =
13348  abi_corpus.get_translation_units().begin();
13349  tu != abi_corpus.get_translation_units().end();
13350  ++tu)
13351  if ((result = lookup_type(qn, **tu)))
13352  break;
13353 
13354  return result;
13355 }
13356 
13357 /// Lookup a type from a given translation unit present in a give corpus.
13358 ///
13359 /// @param type_name the name of the type to look for.
13360 ///
13361 /// @parm tu_path the path of the translation unit to consider.
13362 ///
13363 /// @param corp the corpus to consider.
13364 ///
13365 /// @return the resulting type, if any.
13366 type_base_sptr
13367 lookup_type_from_translation_unit(const string& type_name,
13368  const string& tu_path,
13369  const corpus& corp)
13370 {
13371  string_tu_map_type::const_iterator i = corp.priv_->path_tu_map.find(tu_path);
13372  if (i == corp.priv_->path_tu_map.end())
13373  return type_base_sptr();
13374 
13375  translation_unit_sptr tu = i->second;
13376  ABG_ASSERT(tu);
13377 
13378  type_base_sptr t = lookup_type(type_name, *tu);
13379  return t;
13380 }
13381 
13382 /// Look into an ABI corpus for a function type.
13383 ///
13384 /// @param fn_type the function type to be looked for in the ABI
13385 /// corpus.
13386 ///
13387 /// @param corpus the ABI corpus into which to look for the function
13388 /// type.
13389 ///
13390 /// @return the function type found in the corpus.
13393  const corpus& corpus)
13394 {
13395  ABG_ASSERT(fn_t);
13396 
13397  function_type_sptr result;
13398 
13399  if ((result = lookup_function_type(fn_t, corpus)))
13400  return result;
13401 
13402  for (translation_units::const_iterator i =
13403  corpus.get_translation_units().begin();
13404  i != corpus.get_translation_units().end();
13405  ++i)
13407  **i)))
13408  return result;
13409 
13410  return result;
13411 }
13412 
13413 /// Look into a given corpus to find a type which has the same
13414 /// qualified name as a giventype.
13415 ///
13416 /// If the per-corpus type map is non-empty (because the corpus allows
13417 /// the One Definition Rule) then the type islooked up in that
13418 /// per-corpus type map. Otherwise, the type is looked-up in each
13419 /// translation unit.
13420 ///
13421 /// @param t the type which has the same qualified name as the type we
13422 /// are looking for.
13423 ///
13424 /// @param corp the ABI corpus to look into for the type.
13426 lookup_basic_type(const type_decl& t, const corpus& corp)
13427 {return lookup_basic_type(t.get_name(), corp);}
13428 
13429 /// Look into a given corpus to find a basic type which has a given
13430 /// qualified name.
13431 ///
13432 /// If the per-corpus type map is non-empty (because the corpus allows
13433 /// the One Definition Rule) then the type islooked up in that
13434 /// per-corpus type map. Otherwise, the type is looked-up in each
13435 /// translation unit.
13436 ///
13437 /// @param qualified_name the qualified name of the basic type to look
13438 /// for.
13439 ///
13440 /// @param corp the corpus to look into.
13442 lookup_basic_type(const interned_string &qualified_name, const corpus& corp)
13443 {
13445  type_decl_sptr result;
13446 
13447  if (!m.empty())
13448  result = lookup_type_in_map<type_decl>(qualified_name, m);
13449  else
13450  result = lookup_basic_type_through_translation_units(qualified_name, corp);
13451 
13452  return result;
13453 }
13454 
13455 /// Lookup a @ref type_decl type from a given corpus, by its location.
13456 ///
13457 /// @param loc the location to consider.
13458 ///
13459 /// @param corp the corpus to consider.
13460 ///
13461 /// @return the resulting basic type, if any.
13464  const corpus &corp)
13465 {
13468  type_decl_sptr result;
13469 
13470  result = lookup_type_in_map<type_decl>(loc, m);
13471 
13472  return result;
13473 }
13474 
13475 /// Lookup a @ref type_decl type from a given corpus, by its location.
13476 ///
13477 /// @param loc the location to consider.
13478 ///
13479 /// @param corp the corpus to consider.
13480 ///
13481 /// @return the resulting basic type, if any.
13483 lookup_basic_type_per_location(const string &loc, const corpus &corp)
13484 {
13485  const environment& env = corp.get_environment();
13486  return lookup_basic_type_per_location(env.intern(loc), corp);
13487 }
13488 
13489 /// Look into a given corpus to find a basic type which has a given
13490 /// qualified name.
13491 ///
13492 /// If the per-corpus type map is non-empty (because the corpus allows
13493 /// the One Definition Rule) then the type islooked up in that
13494 /// per-corpus type map. Otherwise, the type is looked-up in each
13495 /// translation unit.
13496 ///
13497 /// @param qualified_name the qualified name of the basic type to look
13498 /// for.
13499 ///
13500 /// @param corp the corpus to look into.
13502 lookup_basic_type(const string& qualified_name, const corpus& corp)
13503 {
13504  return lookup_basic_type(corp.get_environment().intern(qualified_name),
13505  corp);
13506 }
13507 
13508 /// Look into a given corpus to find a class type which has the same
13509 /// qualified name as a given type.
13510 ///
13511 /// If the per-corpus type map is non-empty (because the corpus allows
13512 /// the One Definition Rule) then the type islooked up in that
13513 /// per-corpus type map. Otherwise, the type is looked-up in each
13514 /// translation unit.
13515 ///
13516 /// @param t the class decl type which has the same qualified name as
13517 /// the type we are looking for.
13518 ///
13519 /// @param corp the corpus to look into.
13521 lookup_class_type(const class_decl& t, const corpus& corp)
13522 {
13524  return lookup_class_type(s, corp);
13525 }
13526 
13527 /// Look into a given corpus to find a class type which has a given
13528 /// qualified name.
13529 ///
13530 /// If the per-corpus type map is non-empty (because the corpus allows
13531 /// the One Definition Rule) then the type islooked up in that
13532 /// per-corpus type map. Otherwise, the type is looked-up in each
13533 /// translation unit.
13534 ///
13535 /// @param qualified_name the qualified name of the type to look for.
13536 ///
13537 /// @param corp the corpus to look into.
13539 lookup_class_type(const string& qualified_name, const corpus& corp)
13540 {
13541  interned_string s = corp.get_environment().intern(qualified_name);
13542  return lookup_class_type(s, corp);
13543 }
13544 
13545 /// Look into a given corpus to find a class type which has a given
13546 /// qualified name.
13547 ///
13548 /// If the per-corpus type map is non-empty (because the corpus allows
13549 /// the One Definition Rule) then the type islooked up in that
13550 /// per-corpus type map. Otherwise, the type is looked-up in each
13551 /// translation unit.
13552 ///
13553 /// @param qualified_name the qualified name of the type to look for.
13554 ///
13555 /// @param corp the corpus to look into.
13557 lookup_class_type(const interned_string& qualified_name, const corpus& corp)
13558 {
13560 
13561  class_decl_sptr result = lookup_type_in_map<class_decl>(qualified_name, m);
13562 
13563  return result;
13564 }
13565 
13566 /// Look into a given corpus to find the class type*s* that have a
13567 /// given qualified name.
13568 ///
13569 /// @param qualified_name the qualified name of the type to look for.
13570 ///
13571 /// @param corp the corpus to look into.
13572 ///
13573 /// @return the vector of class types named @p qualified_name.
13574 const type_base_wptrs_type *
13575 lookup_class_types(const interned_string& qualified_name, const corpus& corp)
13576 {
13578 
13579  return lookup_types_in_map(qualified_name, m);
13580 }
13581 
13582 /// Look into a given corpus to find the class type*s* that have a
13583 /// given qualified name and that are declaration-only.
13584 ///
13585 /// @param qualified_name the qualified name of the type to look for.
13586 ///
13587 /// @param corp the corpus to look into.
13588 ///
13589 /// @param result the vector of decl-only class types named @p
13590 /// qualified_name. This is populated iff the function returns true.
13591 ///
13592 /// @return true iff @p result was populated with the decl-only
13593 /// classes named @p qualified_name.
13594 bool
13596  const corpus& corp,
13597  type_base_wptrs_type& result)
13598 {
13600 
13601  const type_base_wptrs_type *v = lookup_types_in_map(qualified_name, m);
13602  if (!v)
13603  return false;
13604 
13605  for (auto type : *v)
13606  {
13607  type_base_sptr t(type);
13609  if (c->get_is_declaration_only()
13610  && !c->get_definition_of_declaration())
13611  result.push_back(type);
13612  }
13613 
13614  return !result.empty();
13615 }
13616 
13617 /// Look into a given corpus to find the union type*s* that have a
13618 /// given qualified name.
13619 ///
13620 /// @param qualified_name the qualified name of the type to look for.
13621 ///
13622 /// @param corp the corpus to look into.
13623 ///
13624 /// @return the vector of union types named @p qualified_name.
13625 const type_base_wptrs_type *
13626 lookup_union_types(const interned_string& qualified_name, const corpus& corp)
13627 {
13629 
13630  return lookup_types_in_map(qualified_name, m);
13631 }
13632 
13633 /// Look into a given corpus to find the class type*s* that have a
13634 /// given qualified name.
13635 ///
13636 /// @param qualified_name the qualified name of the type to look for.
13637 ///
13638 /// @param corp the corpus to look into.
13639 ///
13640 /// @return the vector of class types which name is @p qualified_name.
13641 const type_base_wptrs_type*
13642 lookup_class_types(const string& qualified_name, const corpus& corp)
13643 {
13644  interned_string s = corp.get_environment().intern(qualified_name);
13645  return lookup_class_types(s, corp);
13646 }
13647 
13648 /// Look into a given corpus to find the union types that have a given
13649 /// qualified name.
13650 ///
13651 /// @param qualified_name the qualified name of the type to look for.
13652 ///
13653 /// @param corp the corpus to look into.
13654 ///
13655 /// @return the vector of union types which name is @p qualified_name.
13656 const type_base_wptrs_type *
13657 lookup_union_types(const string& qualified_name, const corpus& corp)
13658 {
13659  interned_string s = corp.get_environment().intern(qualified_name);
13660  return lookup_union_types(s, corp);
13661 }
13662 
13663 /// Look up a @ref class_decl from a given corpus by its location.
13664 ///
13665 /// @param loc the location to consider.
13666 ///
13667 /// @param corp the corpus to consider.
13668 ///
13669 /// @return the resulting class decl, if any.
13672  const corpus& corp)
13673 {
13676  class_decl_sptr result = lookup_type_in_map<class_decl>(loc, m);
13677 
13678  return result;
13679 }
13680 
13681 /// Look up a @ref class_decl from a given corpus by its location.
13682 ///
13683 /// @param loc the location to consider.
13684 ///
13685 /// @param corp the corpus to consider.
13686 ///
13687 /// @return the resulting class decl, if any.
13689 lookup_class_type_per_location(const string &loc, const corpus &corp)
13690 {
13691  const environment& env = corp.get_environment();
13692  return lookup_class_type_per_location(env.intern(loc), corp);
13693 }
13694 
13695 /// Look into a given corpus to find a union type which has a given
13696 /// qualified name.
13697 ///
13698 /// If the per-corpus type map is non-empty (because the corpus allows
13699 /// the One Definition Rule) then the type islooked up in that
13700 /// per-corpus type map. Otherwise, the type is looked-up in each
13701 /// translation unit.
13702 ///
13703 /// @param qualified_name the qualified name of the type to look for.
13704 ///
13705 /// @param corp the corpus to look into.
13706 union_decl_sptr
13707 lookup_union_type(const interned_string& type_name, const corpus& corp)
13708 {
13710 
13711  union_decl_sptr result = lookup_type_in_map<union_decl>(type_name, m);
13712  if (!result)
13713  result = lookup_union_type_through_translation_units(type_name, corp);
13714 
13715  return result;
13716 }
13717 
13718 /// Look into a given corpus to find a union type which has a given
13719 /// qualified name.
13720 ///
13721 /// If the per-corpus type map is non-empty (because the corpus allows
13722 /// the One Definition Rule) then the type islooked up in that
13723 /// per-corpus type map. Otherwise, the type is looked-up in each
13724 /// translation unit.
13725 ///
13726 /// @param qualified_name the qualified name of the type to look for.
13727 ///
13728 /// @param corp the corpus to look into.
13729 union_decl_sptr
13730 lookup_union_type(const string& type_name, const corpus& corp)
13731 {
13732  interned_string s = corp.get_environment().intern(type_name);
13733  return lookup_union_type(s, corp);
13734 }
13735 
13736 /// Look into a given corpus to find an enum type which has the same
13737 /// qualified name as a given enum type.
13738 ///
13739 /// If the per-corpus type map is non-empty (because the corpus allows
13740 /// the One Definition Rule) then the type islooked up in that
13741 /// per-corpus type map. Otherwise, the type is looked-up in each
13742 /// translation unit.
13743 ///
13744 /// @param t the enum type which has the same qualified name as the
13745 /// type we are looking for.
13746 ///
13747 /// @param corp the corpus to look into.
13750 {
13752  return lookup_enum_type(s, corp);
13753 }
13754 
13755 /// Look into a given corpus to find an enum type which has a given
13756 /// qualified name.
13757 ///
13758 /// If the per-corpus type map is non-empty (because the corpus allows
13759 /// the One Definition Rule) then the type islooked up in that
13760 /// per-corpus type map. Otherwise, the type is looked-up in each
13761 /// translation unit.
13762 ///
13763 /// @param qualified_name the qualified name of the enum type to look
13764 /// for.
13765 ///
13766 /// @param corp the corpus to look into.
13768 lookup_enum_type(const string& qualified_name, const corpus& corp)
13769 {
13770  interned_string s = corp.get_environment().intern(qualified_name);
13771  return lookup_enum_type(s, corp);
13772 }
13773 
13774 /// Look into a given corpus to find an enum type which has a given
13775 /// qualified name.
13776 ///
13777 /// If the per-corpus type map is non-empty (because the corpus allows
13778 /// the One Definition Rule) then the type islooked up in that
13779 /// per-corpus type map. Otherwise, the type is looked-up in each
13780 /// translation unit.
13781 ///
13782 /// @param qualified_name the qualified name of the enum type to look
13783 /// for.
13784 ///
13785 /// @param corp the corpus to look into.
13787 lookup_enum_type(const interned_string& qualified_name, const corpus& corp)
13788 {
13790 
13791  enum_type_decl_sptr result =
13792  lookup_type_in_map<enum_type_decl>(qualified_name, m);
13793  if (!result)
13794  result = lookup_enum_type_through_translation_units(qualified_name, corp);
13795 
13796  return result;
13797 }
13798 
13799 /// Look into a given corpus to find the enum type*s* that have a
13800 /// given qualified name.
13801 ///
13802 /// @param qualified_name the qualified name of the type to look for.
13803 ///
13804 /// @param corp the corpus to look into.
13805 ///
13806 /// @return the vector of enum types that which name is @p qualified_name.
13807 const type_base_wptrs_type *
13808 lookup_enum_types(const interned_string& qualified_name, const corpus& corp)
13809 {
13811 
13812  return lookup_types_in_map(qualified_name, m);
13813 }
13814 
13815 /// Look into a given corpus to find the enum type*s* that have a
13816 /// given qualified name.
13817 ///
13818 /// @param qualified_name the qualified name of the type to look for.
13819 ///
13820 /// @param corp the corpus to look into.
13821 ///
13822 /// @return the vector of enum types that which name is @p qualified_name.
13823 const type_base_wptrs_type*
13824 lookup_enum_types(const string& qualified_name, const corpus& corp)
13825 {
13826  interned_string s = corp.get_environment().intern(qualified_name);
13827  return lookup_enum_types(s, corp);
13828 }
13829 
13830 /// Look up an @ref enum_type_decl from a given corpus, by its location.
13831 ///
13832 /// @param loc the location to consider.
13833 ///
13834 /// @param corp the corpus to look the type from.
13835 ///
13836 /// @return the resulting enum type, if any.
13839 {
13842  enum_type_decl_sptr result = lookup_type_in_map<enum_type_decl>(loc, m);
13843 
13844  return result;
13845 }
13846 
13847 /// Look up an @ref enum_type_decl from a given corpus, by its location.
13848 ///
13849 /// @param loc the location to consider.
13850 ///
13851 /// @param corp the corpus to look the type from.
13852 ///
13853 /// @return the resulting enum type, if any.
13855 lookup_enum_type_per_location(const string &loc, const corpus &corp)
13856 {
13857  const environment& env = corp.get_environment();
13858  return lookup_enum_type_per_location(env.intern(loc), corp);
13859 }
13860 
13861 /// Look into a given corpus to find a typedef type which has the
13862 /// same qualified name as a given typedef type.
13863 ///
13864 /// If the per-corpus type map is non-empty (because the corpus allows
13865 /// the One Definition Rule) then the type islooked up in that
13866 /// per-corpus type map. Otherwise, the type is looked-up in each
13867 /// translation unit.
13868 ///
13869 /// @param t the typedef type which has the same qualified name as the
13870 /// typedef type we are looking for.
13871 ///
13872 /// @param corp the corpus to look into.
13875 {
13877  return lookup_typedef_type(s, corp);
13878 }
13879 
13880 /// Look into a given corpus to find a typedef type which has the
13881 /// same qualified name as a given typedef type.
13882 ///
13883 /// If the per-corpus type map is non-empty (because the corpus allows
13884 /// the One Definition Rule) then the type islooked up in that
13885 /// per-corpus type map. Otherwise, the type is looked-up in each
13886 /// translation unit.
13887 ///
13888 /// @param t the typedef type which has the same qualified name as the
13889 /// typedef type we are looking for.
13890 ///
13891 /// @param corp the corpus to look into.
13893 lookup_typedef_type(const string& qualified_name, const corpus& corp)
13894 {
13895  interned_string s = corp.get_environment().intern(qualified_name);
13896  return lookup_typedef_type(s, corp);
13897 }
13898 
13899 /// Look into a given corpus to find a typedef type which has a
13900 /// given qualified name.
13901 ///
13902 /// If the per-corpus type map is non-empty (because the corpus allows
13903 /// the One Definition Rule) then the type islooked up in that
13904 /// per-corpus type map. Otherwise, the type is looked-up in each
13905 /// translation unit.
13906 ///
13907 /// @param qualified_name the qualified name of the typedef type to
13908 /// look for.
13909 ///
13910 /// @param corp the corpus to look into.
13912 lookup_typedef_type(const interned_string& qualified_name, const corpus& corp)
13913 {
13915 
13916  typedef_decl_sptr result =
13917  lookup_type_in_map<typedef_decl>(qualified_name, m);
13918  if (!result)
13919  result = lookup_typedef_type_through_translation_units(qualified_name,
13920  corp);
13921 
13922  return result;
13923 }
13924 
13925 /// Lookup a @ref typedef_decl from a corpus, by its location.
13926 ///
13927 /// @param loc the location to consider.
13928 ///
13929 /// @param corp the corpus to consider.
13930 ///
13931 /// @return the typedef_decl found, if any.
13934 {
13937  typedef_decl_sptr result = lookup_type_in_map<typedef_decl>(loc, m);
13938 
13939  return result;
13940 }
13941 
13942 /// Lookup a @ref typedef_decl from a corpus, by its location.
13943 ///
13944 /// @param loc the location to consider.
13945 ///
13946 /// @param corp the corpus to consider.
13947 ///
13948 /// @return the typedef_decl found, if any.
13950 lookup_typedef_type_per_location(const string &loc, const corpus &corp)
13951 {
13952  const environment& env = corp.get_environment();
13953  return lookup_typedef_type_per_location(env.intern(loc), corp);
13954 }
13955 
13956 /// Look into a corpus to find a class, union or typedef type which
13957 /// has a given qualified name.
13958 ///
13959 /// If the per-corpus type map is non-empty (because the corpus allows
13960 /// the One Definition Rule) then the type islooked up in that
13961 /// per-corpus type map. Otherwise, the type is looked-up in each
13962 /// translation unit.
13963 ///
13964 /// @param qualified_name the name of the type to find.
13965 ///
13966 /// @param corp the corpus to look into.
13967 ///
13968 /// @return the typedef or class type found.
13969 type_base_sptr
13970 lookup_class_or_typedef_type(const string& qualified_name, const corpus& corp)
13971 {
13972  type_base_sptr result = lookup_class_type(qualified_name, corp);
13973  if (!result)
13974  result = lookup_union_type(qualified_name, corp);
13975 
13976  if (!result)
13977  result = lookup_typedef_type(qualified_name, corp);
13978  return result;
13979 }
13980 
13981 /// Look into a corpus to find a class, typedef or enum type which has
13982 /// a given qualified name.
13983 ///
13984 /// If the per-corpus type map is non-empty (because the corpus allows
13985 /// the One Definition Rule) then the type islooked up in that
13986 /// per-corpus type map. Otherwise, the type is looked-up in each
13987 /// translation unit.
13988 ///
13989 /// @param qualified_name the qualified name of the type to look for.
13990 ///
13991 /// @param corp the corpus to look into.
13992 ///
13993 /// @return the typedef, class or enum type found.
13994 type_base_sptr
13995 lookup_class_typedef_or_enum_type(const string& qualified_name,
13996  const corpus& corp)
13997 {
13998  type_base_sptr result = lookup_class_or_typedef_type(qualified_name, corp);
13999  if (!result)
14000  result = lookup_enum_type(qualified_name, corp);
14001 
14002  return result;
14003 }
14004 
14005 /// Look into a given corpus to find a qualified type which has the
14006 /// same qualified name as a given type.
14007 ///
14008 /// @param t the type which has the same qualified name as the
14009 /// qualified type we are looking for.
14010 ///
14011 /// @param corp the corpus to look into.
14012 ///
14013 /// @return the qualified type found.
14014 qualified_type_def_sptr
14016 {
14018  return lookup_qualified_type(s, corp);
14019 }
14020 
14021 /// Look into a given corpus to find a qualified type which has a
14022 /// given qualified name.
14023 ///
14024 /// @param qualified_name the qualified name of the type to look for.
14025 ///
14026 /// @param corp the corpus to look into.
14027 ///
14028 /// @return the type found.
14029 qualified_type_def_sptr
14030 lookup_qualified_type(const interned_string& qualified_name, const corpus& corp)
14031 {
14033  corp.get_types().qualified_types();
14034 
14035  qualified_type_def_sptr result =
14036  lookup_type_in_map<qualified_type_def>(qualified_name, m);
14037 
14038  if (!result)
14039  result = lookup_qualified_type_through_translation_units(qualified_name,
14040  corp);
14041 
14042  return result;
14043 }
14044 
14045 /// Look into a given corpus to find a pointer type which has the same
14046 /// qualified name as a given pointer type.
14047 ///
14048 /// @param t the pointer type which has the same qualified name as the
14049 /// type we are looking for.
14050 ///
14051 /// @param corp the corpus to look into.
14052 ///
14053 /// @return the pointer type found.
14056 {
14058  return lookup_pointer_type(s, corp);
14059 }
14060 
14061 /// Look into a given corpus to find a pointer type which has a given
14062 /// qualified name.
14063 ///
14064 /// If the per-corpus type map is non-empty (because the corpus allows
14065 /// the One Definition Rule) then the type islooked up in that
14066 /// per-corpus type map. Otherwise, the type is looked-up in each
14067 /// translation unit.
14068 ///
14069 /// @param qualified_name the qualified name of the pointer type to
14070 /// look for.
14071 ///
14072 /// @param corp the corpus to look into.
14073 ///
14074 /// @return the pointer type found.
14076 lookup_pointer_type(const interned_string& qualified_name, const corpus& corp)
14077 {
14079 
14080  pointer_type_def_sptr result =
14081  lookup_type_in_map<pointer_type_def>(qualified_name, m);
14082  if (!result)
14083  result = lookup_pointer_type_through_translation_units(qualified_name,
14084  corp);
14085 
14086  return result;
14087 }
14088 
14089 /// Look into a given corpus to find a reference type which has the
14090 /// same qualified name as a given reference type.
14091 ///
14092 /// If the per-corpus type map is non-empty (because the corpus allows
14093 /// the One Definition Rule) then the type islooked up in that
14094 /// per-corpus type map. Otherwise, the type is looked-up in each
14095 /// translation unit.
14096 ///
14097 /// @param t the reference type which has the same qualified name as
14098 /// the reference type we are looking for.
14099 ///
14100 /// @param corp the corpus to look into.
14101 ///
14102 /// @return the reference type found.
14105 {
14107  return lookup_reference_type(s, corp);
14108 }
14109 
14110 /// Look into a given corpus to find a reference type which has a
14111 /// given qualified name.
14112 ///
14113 /// If the per-corpus type map is non-empty (because the corpus allows
14114 /// the One Definition Rule) then the type islooked up in that
14115 /// per-corpus type map. Otherwise, the type is looked-up in each
14116 /// translation unit.
14117 ///
14118 /// @param qualified_name the qualified name of the reference type to
14119 /// look for.
14120 ///
14121 /// @param corp the corpus to look into.
14122 ///
14123 /// @return the reference type found.
14125 lookup_reference_type(const interned_string& qualified_name, const corpus& corp)
14126 {
14128  corp.get_types().reference_types();
14129 
14130  reference_type_def_sptr result =
14131  lookup_type_in_map<reference_type_def>(qualified_name, m);
14132  if (!result)
14133  result = lookup_reference_type_through_translation_units(qualified_name,
14134  corp);
14135 
14136  return result;
14137 }
14138 
14139 /// Look into a given corpus to find an array type which has a given
14140 /// qualified name.
14141 ///
14142 /// If the per-corpus type map is non-empty (because the corpus allows
14143 /// the One Definition Rule) then the type islooked up in that
14144 /// per-corpus type map. Otherwise, the type is looked-up in each
14145 /// translation unit.
14146 ///
14147 /// @param qualified_name the qualified name of the array type to look
14148 /// for.
14149 ///
14150 /// @param corp the corpus to look into.
14151 ///
14152 /// @return the array type found.
14155 {
14157  return lookup_array_type(s, corp);
14158 }
14159 
14160 /// Look into a given corpus to find an array type which has the same
14161 /// qualified name as a given array type.
14162 ///
14163 /// If the per-corpus type map is non-empty (because the corpus allows
14164 /// the One Definition Rule) then the type islooked up in that
14165 /// per-corpus type map. Otherwise, the type is looked-up in each
14166 /// translation unit.
14167 ///
14168 /// @param t the type which has the same qualified name as the type we
14169 /// are looking for.
14170 ///
14171 /// @param corp the corpus to look into.
14172 ///
14173 /// @return the type found.
14175 lookup_array_type(const interned_string& qualified_name, const corpus& corp)
14176 {
14178 
14179  array_type_def_sptr result =
14180  lookup_type_in_map<array_type_def>(qualified_name, m);
14181  if (!result)
14182  result = lookup_array_type_through_translation_units(qualified_name, corp);
14183 
14184  return result;
14185 }
14186 
14187 /// Look into a given corpus to find a function type which has the same
14188 /// qualified name as a given function type.
14189 ///
14190 /// If the per-corpus type map is non-empty (because the corpus allows
14191 /// the One Definition Rule) then the type islooked up in that
14192 /// per-corpus type map. Otherwise, the type is looked-up in each
14193 /// translation unit.
14194 ///
14195 /// @param t the function type which has the same qualified name as
14196 /// the function type we are looking for.
14197 ///
14198 /// @param corp the corpus to look into.
14199 ///
14200 /// @return the function type found.
14203 {
14204  interned_string type_name = get_type_name(t);
14205  return lookup_function_type(type_name, corp);
14206 }
14207 
14208 /// Look into a given corpus to find a function type which has the same
14209 /// qualified name as a given function type.
14210 ///
14211 /// If the per-corpus type map is non-empty (because the corpus allows
14212 /// the One Definition Rule) then the type islooked up in that
14213 /// per-corpus type map. Otherwise, the type is looked-up in each
14214 /// translation unit.
14215 ///
14216 /// @param t the function type which has the same qualified name as
14217 /// the function type we are looking for.
14218 ///
14219 /// @param corp the corpus to look into.
14220 ///
14221 /// @return the function type found.
14224  const corpus& corpus)
14225 {
14226  if (fn_t)
14227  return lookup_function_type(*fn_t, corpus);
14228  return function_type_sptr();
14229 }
14230 
14231 /// Look into a given corpus to find a function type which has a given
14232 /// qualified name.
14233 ///
14234 /// If the per-corpus type map is non-empty (because the corpus allows
14235 /// the One Definition Rule) then the type islooked up in that
14236 /// per-corpus type map. Otherwise, the type is looked-up in each
14237 /// translation unit.
14238 ///
14239 /// @param qualified_name the qualified name of the function type to
14240 /// look for.
14241 ///
14242 /// @param corp the corpus to look into.
14243 ///
14244 /// @return the function type found.
14246 lookup_function_type(const interned_string& qualified_name, const corpus& corp)
14247 {
14249 
14250  function_type_sptr result =
14251  lookup_type_in_map<function_type>(qualified_name, m);
14252  if (!result)
14253  result = lookup_function_type_through_translation_units(qualified_name,
14254  corp);
14255 
14256  return result;
14257 }
14258 
14259 /// Look into a given corpus to find a type which has a given
14260 /// qualified name.
14261 ///
14262 /// If the per-corpus type map is non-empty (because the corpus allows
14263 /// the One Definition Rule) then the type islooked up in that
14264 /// per-corpus type map. Otherwise, the type is looked-up in each
14265 /// translation unit.
14266 ///
14267 /// @param qualified_name the qualified name of the function type to
14268 /// look for.
14269 ///
14270 /// @param corp the corpus to look into.
14271 ///
14272 /// @return the function type found.
14273 type_base_sptr
14274 lookup_type(const interned_string& n, const corpus& corp)
14275 {
14276  type_base_sptr result;
14277 
14278  ((result = lookup_basic_type(n, corp))
14279  || (result = lookup_class_type(n, corp))
14280  || (result = lookup_union_type(n, corp))
14281  || (result = lookup_enum_type(n, corp))
14282  || (result = lookup_typedef_type(n, corp))
14283  || (result = lookup_qualified_type(n, corp))
14284  || (result = lookup_pointer_type(n, corp))
14285  || (result = lookup_reference_type(n, corp))
14286  || (result = lookup_array_type(n, corp))
14287  || (result= lookup_function_type(n, corp)));
14288 
14289  return result;
14290 }
14291 
14292 /// Lookup a type from a corpus, by its location.
14293 ///
14294 /// @param loc the location to consider.
14295 ///
14296 /// @param corp the corpus to look the type from.
14297 ///
14298 /// @return the resulting type, if any found.
14299 type_base_sptr
14301 {
14302  // TODO: finish this.
14303 
14304  //TODO: when we fully support types indexed by their location, this
14305  //function should return a vector of types because at each location,
14306  //there can be several types that are defined (yay, C and C++,
14307  //*sigh*).
14308 
14309  type_base_sptr result;
14310  ((result = lookup_basic_type_per_location(loc, corp))
14311  || (result = lookup_class_type_per_location(loc, corp))
14312  || (result = lookup_union_type_per_location(loc, corp))
14313  || (result = lookup_enum_type_per_location(loc, corp))
14314  || (result = lookup_typedef_type_per_location(loc, corp)));
14315 
14316  return result;
14317 }
14318 
14319 /// Look into a given corpus to find a type
14320 ///
14321 /// If the per-corpus type map is non-empty (because the corpus allows
14322 /// the One Definition Rule) then the type islooked up in that
14323 /// per-corpus type map. Otherwise, the type is looked-up in each
14324 /// translation unit.
14325 ///
14326 /// @param qualified_name the qualified name of the function type to
14327 /// look for.
14328 ///
14329 /// @param corp the corpus to look into.
14330 ///
14331 /// @return the function type found.
14332 type_base_sptr
14333 lookup_type(const type_base&t, const corpus& corp)
14334 {
14336  return lookup_type(n, corp);
14337 }
14338 
14339 /// Look into a given corpus to find a type
14340 ///
14341 /// If the per-corpus type map is non-empty (because the corpus allows
14342 /// the One Definition Rule) then the type islooked up in that
14343 /// per-corpus type map. Otherwise, the type is looked-up in each
14344 /// translation unit.
14345 ///
14346 /// @param qualified_name the qualified name of the function type to
14347 /// look for.
14348 ///
14349 /// @param corp the corpus to look into.
14350 ///
14351 /// @return the function type found.
14352 type_base_sptr
14353 lookup_type(const type_base_sptr&t, const corpus& corp)
14354 {
14355  if (t)
14356  return lookup_type(*t, corp);
14357  return type_base_sptr();
14358 }
14359 
14360 /// Update the map that associates a fully qualified name of a given
14361 /// type to that type.
14362 ///
14363 ///
14364 /// @param type the type we are considering.
14365 ///
14366 /// @param types_map the map to update. It's a map that assciates a
14367 /// fully qualified name of a type to the type itself.
14368 ///
14369 /// @param use_type_name_as_key if true, use the name of the type as
14370 /// the key to look it up later. If false, then use the location of
14371 /// the type as a key to look it up later.
14372 ///
14373 /// @return true iff the type was added to the map.
14374 template<typename TypeKind>
14375 bool
14376 maybe_update_types_lookup_map(const shared_ptr<TypeKind>& type,
14378  bool use_type_name_as_key = true)
14379 {
14380  interned_string s;
14381 
14382  if (use_type_name_as_key)
14383  s = get_type_name(type);
14384  else if (location l = type->get_location())
14385  {
14386  string str = l.expand();
14387  s = type->get_environment().intern(str);
14388  }
14389 
14390  istring_type_base_wptrs_map_type::iterator i = types_map.find(s);
14391  bool result = false;
14392 
14393  if (i == types_map.end())
14394  {
14395  types_map[s].push_back(type);
14396  result = true;
14397  }
14398  else
14399  i->second.push_back(type);
14400 
14401  return result;
14402 }
14403 
14404 /// This is the specialization for type @ref class_decl of the
14405 /// function template:
14406 ///
14407 /// maybe_update_types_lookup_map<T>(scope_decl*,
14408 /// const shared_ptr<T>&,
14409 /// istring_type_base_wptrs_map_type&)
14410 ///
14411 /// @param class_type the type to consider.
14412 ///
14413 /// @param types_map the type map to update.
14414 ///
14415 /// @return true iff the type was added to the map.
14416 template<>
14417 bool
14420  bool use_type_name_as_key)
14421 {
14422  class_decl_sptr type = class_type;
14423 
14424  bool update_qname_map = true;
14425  if (type->get_is_declaration_only())
14426  {
14427  // Let's try to look through decl-only classes to get their
14428  // definition. But if the class doesn't have a definition then
14429  // we'll keep it.
14430  if (class_decl_sptr def =
14431  is_class_type(class_type->get_definition_of_declaration()))
14432  type = def;
14433  }
14434 
14435  if (!update_qname_map)
14436  return false;
14437 
14438  interned_string s;
14439  if (use_type_name_as_key)
14440  {
14441  string qname = type->get_qualified_name();
14442  s = type->get_environment().intern(qname);
14443  }
14444  else if (location l = type->get_location())
14445  {
14446  string str = l.expand();
14447  s = type->get_environment().intern(str);
14448  }
14449 
14450  bool result = false;
14451  istring_type_base_wptrs_map_type::iterator i = map.find(s);
14452  if (i == map.end())
14453  {
14454  map[s].push_back(type);
14455  result = true;
14456  }
14457  else
14458  i->second.push_back(type);
14459 
14460  return result;
14461 }
14462 
14463 /// This is the specialization for type @ref function_type of the
14464 /// function template:
14465 ///
14466 /// maybe_update_types_lookup_map<T>(scope_decl*,
14467 /// const shared_ptr<T>&,
14468 /// istring_type_base_wptrs_map_type&)
14469 ///
14470 /// @param scope the scope of the type to consider.
14471 ///
14472 /// @param class_type the type to consider.
14473 ///
14474 /// @param types_map the type map to update.
14475 ///
14476 /// @return true iff the type was added to the map.
14477 template<>
14478 bool
14480 (const function_type_sptr& type,
14482  bool /*use_type_name_as_key*/)
14483 {
14484  bool result = false;
14485  interned_string s = get_type_name(type);
14486  istring_type_base_wptrs_map_type::iterator i = types_map.find(s);
14487  if (i == types_map.end())
14488  {
14489  types_map[s].push_back(type);
14490  result = true;
14491  }
14492  else
14493  i->second.push_back(type);
14494 
14495  return result;
14496 }
14497 
14498 /// Update the map that associates the fully qualified name of a basic
14499 /// type with the type itself.
14500 ///
14501 /// The per-translation unit type map is updated if no type with this
14502 /// name was already existing in that map.
14503 ///
14504 /// If no type with this name did already exist in the per-corpus type
14505 /// map, then that per-corpus type map is updated. Otherwise, that
14506 /// type is erased from that per-corpus map.
14507 ///
14508 /// @param basic_type the basic type to consider.
14509 void
14511 {
14512  if (translation_unit *tu = basic_type->get_translation_unit())
14513  maybe_update_types_lookup_map<type_decl>
14514  (basic_type, tu->get_types().basic_types());
14515 
14516  if (corpus *type_corpus = basic_type->get_corpus())
14517  {
14518  maybe_update_types_lookup_map<type_decl>
14519  (basic_type,
14520  type_corpus->priv_->get_types().basic_types());
14521 
14522  maybe_update_types_lookup_map<type_decl>
14523  (basic_type,
14524  type_corpus->get_type_per_loc_map().basic_types(),
14525  /*use_type_name_as_key*/false);
14526 
14527  if (corpus *group = type_corpus->get_group())
14528  {
14529  maybe_update_types_lookup_map<type_decl>
14530  (basic_type,
14531  group->priv_->get_types().basic_types());
14532 
14533  maybe_update_types_lookup_map<type_decl>
14534  (basic_type,
14535  group->get_type_per_loc_map().basic_types(),
14536  /*use_type_name_as_key*/false);
14537  }
14538  }
14539 
14540 }
14541 
14542 /// Update the map that associates the fully qualified name of a class
14543 /// type with the type itself.
14544 ///
14545 /// The per-translation unit type map is updated if no type with this
14546 /// name was already existing in that map.
14547 ///
14548 /// If no type with this name did already exist in the per-corpus type
14549 /// map, then that per-corpus type map is updated. Otherwise, that
14550 /// type is erased from that per-corpus map.
14551 ///
14552 /// @param class_type the class type to consider.
14553 void
14555 {
14556  if (translation_unit *tu = class_type->get_translation_unit())
14558  (class_type, tu->get_types().class_types());
14559 
14560  if (corpus *type_corpus = class_type->get_corpus())
14561  {
14563  (class_type,
14564  type_corpus->priv_->get_types().class_types());
14565 
14567  (class_type,
14568  type_corpus->get_type_per_loc_map().class_types(),
14569  /*use_type_name_as_key*/false);
14570 
14571  if (corpus *group = type_corpus->get_group())
14572  {
14574  (class_type,
14575  group->priv_->get_types().class_types());
14576 
14578  (class_type,
14579  group->get_type_per_loc_map().class_types(),
14580  /*use_type_name_as_key*/false);
14581  }
14582  }
14583 }
14584 
14585 /// Update the map that associates the fully qualified name of a union
14586 /// type with the type itself.
14587 ///
14588 /// The per-translation unit type map is updated if no type with this
14589 /// name was already existing in that map.
14590 ///
14591 /// If no type with this name did already exist in the per-corpus type
14592 /// map, then that per-corpus type map is updated. Otherwise, that
14593 /// type is erased from that per-corpus map.
14594 ///
14595 /// @param union_type the union type to consider.
14596 void
14597 maybe_update_types_lookup_map(const union_decl_sptr& union_type)
14598 {
14599  if (translation_unit *tu = union_type->get_translation_unit())
14600  maybe_update_types_lookup_map<union_decl>
14601  (union_type, tu->get_types().union_types());
14602 
14603  if (corpus *type_corpus = union_type->get_corpus())
14604  {
14605  maybe_update_types_lookup_map<union_decl>
14606  (union_type,
14607  type_corpus->priv_->get_types().union_types());
14608 
14609  maybe_update_types_lookup_map<union_decl>
14610  (union_type,
14611  type_corpus->get_type_per_loc_map().union_types(),
14612  /*use_type_name_as_key*/false);
14613 
14614  if (corpus *group = type_corpus->get_group())
14615  {
14616  maybe_update_types_lookup_map<union_decl>
14617  (union_type,
14618  group->priv_->get_types().union_types());
14619 
14620  maybe_update_types_lookup_map<union_decl>
14621  (union_type,
14622  group->get_type_per_loc_map().union_types(),
14623  /*use_type_name_as_key*/false);
14624  }
14625  }
14626 }
14627 
14628 /// Update the map that associates the fully qualified name of an enum
14629 /// type with the type itself.
14630 ///
14631 /// The per-translation unit type map is updated if no type with this
14632 /// name was already existing in that map.
14633 ///
14634 /// If no type with this name did already exist in the per-corpus type
14635 /// map, then that per-corpus type map is updated. Otherwise, that
14636 /// type is erased from that per-corpus map.
14637 ///
14638 /// @param enum_type the type to consider.
14639 void
14641 {
14642  if (translation_unit *tu = enum_type->get_translation_unit())
14643  maybe_update_types_lookup_map<enum_type_decl>
14644  (enum_type, tu->get_types().enum_types());
14645 
14646  if (corpus *type_corpus = enum_type->get_corpus())
14647  {
14648  maybe_update_types_lookup_map<enum_type_decl>
14649  (enum_type,
14650  type_corpus->priv_->get_types().enum_types());
14651 
14652  maybe_update_types_lookup_map<enum_type_decl>
14653  (enum_type,
14654  type_corpus->get_type_per_loc_map().enum_types(),
14655  /*use_type_name_as_key*/false);
14656 
14657  if (corpus *group = type_corpus->get_group())
14658  {
14659  maybe_update_types_lookup_map<enum_type_decl>
14660  (enum_type,
14661  group->priv_->get_types().enum_types());
14662 
14663  maybe_update_types_lookup_map<enum_type_decl>
14664  (enum_type,
14665  group->get_type_per_loc_map().enum_types(),
14666  /*use_type_name_as_key*/false);
14667  }
14668  }
14669 
14670 }
14671 
14672 /// Update the map that associates the fully qualified name of a
14673 /// typedef type with the type itself.
14674 ///
14675 /// The per-translation unit type map is updated if no type with this
14676 /// name was already existing in that map.
14677 ///
14678 /// If no type with this name did already exist in the per-corpus type
14679 /// map, then that per-corpus type map is updated. Otherwise, that
14680 /// type is erased from that per-corpus map.
14681 ///
14682 /// @param typedef_type the type to consider.
14683 void
14685 {
14686  if (translation_unit *tu = typedef_type->get_translation_unit())
14687  maybe_update_types_lookup_map<typedef_decl>
14688  (typedef_type, tu->get_types().typedef_types());
14689 
14690  if (corpus *type_corpus = typedef_type->get_corpus())
14691  {
14692  maybe_update_types_lookup_map<typedef_decl>
14693  (typedef_type,
14694  type_corpus->priv_->get_types().typedef_types());
14695 
14696  maybe_update_types_lookup_map<typedef_decl>
14697  (typedef_type,
14698  type_corpus->get_type_per_loc_map().typedef_types(),
14699  /*use_type_name_as_key*/false);
14700 
14701  if (corpus *group = type_corpus->get_group())
14702  {
14703  maybe_update_types_lookup_map<typedef_decl>
14704  (typedef_type,
14705  group->priv_->get_types().typedef_types());
14706 
14707  maybe_update_types_lookup_map<typedef_decl>
14708  (typedef_type,
14709  group->get_type_per_loc_map().typedef_types(),
14710  /*use_type_name_as_key*/false);
14711  }
14712  }
14713 }
14714 
14715 /// Update the map that associates the fully qualified name of a
14716 /// qualified type with the type itself.
14717 ///
14718 /// The per-translation unit type map is updated if no type with this
14719 /// name was already existing in that map.
14720 ///
14721 /// If no type with this name did already exist in the per-corpus type
14722 /// map, then that per-corpus type map is updated. Otherwise, that
14723 /// type is erased from that per-corpus map.
14724 ///
14725 /// @param qualified_type the type to consider.
14726 void
14727 maybe_update_types_lookup_map(const qualified_type_def_sptr& qualified_type)
14728 {
14729  if (translation_unit *tu = qualified_type->get_translation_unit())
14730  maybe_update_types_lookup_map<qualified_type_def>
14731  (qualified_type, tu->get_types().qualified_types());
14732 
14733  if (corpus *type_corpus = qualified_type->get_corpus())
14734  {
14735  maybe_update_types_lookup_map<qualified_type_def>
14736  (qualified_type,
14737  type_corpus->priv_->get_types().qualified_types());
14738 
14739  if (corpus *group = type_corpus->get_group())
14740  {
14741  maybe_update_types_lookup_map<qualified_type_def>
14742  (qualified_type,
14743  group->priv_->get_types().qualified_types());
14744  }
14745  }
14746 }
14747 
14748 /// Update the map that associates the fully qualified name of a
14749 /// pointer type with the type itself.
14750 ///
14751 /// The per-translation unit type map is updated if no type with this
14752 /// name was already existing in that map.
14753 ///
14754 /// If no type with this name did already exist in the per-corpus type
14755 /// map, then that per-corpus type map is updated. Otherwise, that
14756 /// type is erased from that per-corpus map.
14757 ///
14758 /// @param pointer_type the type to consider.
14759 void
14761 {
14762  if (translation_unit *tu = pointer_type->get_translation_unit())
14763  maybe_update_types_lookup_map<pointer_type_def>
14764  (pointer_type, tu->get_types().pointer_types());
14765 
14766  if (corpus *type_corpus = pointer_type->get_corpus())
14767  {
14768  maybe_update_types_lookup_map<pointer_type_def>
14769  (pointer_type,
14770  type_corpus->priv_->get_types().pointer_types());
14771 
14772  if (corpus *group = type_corpus->get_group())
14773  {
14774  maybe_update_types_lookup_map<pointer_type_def>
14775  (pointer_type,
14776  group->priv_->get_types().pointer_types());
14777  }
14778  }
14779 }
14780 
14781 /// Update the map that associates the fully qualified name of a
14782 /// pointer-to-member type with the type itself.
14783 ///
14784 /// The per-translation unit type map is updated if no type with this
14785 /// name was already existing in that map.
14786 ///
14787 /// If no type with this name did already exist in the per-corpus type
14788 /// map, then that per-corpus type map is updated. Otherwise, that
14789 /// type is erased from that per-corpus map.
14790 ///
14791 /// @param ptr_to_mbr_type the type to consider.
14792 void
14794 {
14795  if (translation_unit *tu = ptr_to_member->get_translation_unit())
14796  maybe_update_types_lookup_map<ptr_to_mbr_type>
14797  (ptr_to_member, tu->get_types().ptr_to_mbr_types());
14798 
14799  if (corpus *type_corpus = ptr_to_member->get_corpus())
14800  {
14801  maybe_update_types_lookup_map<ptr_to_mbr_type>
14802  (ptr_to_member,
14803  type_corpus->priv_->get_types().ptr_to_mbr_types());
14804 
14805  if (corpus *group = type_corpus->get_group())
14806  {
14807  maybe_update_types_lookup_map<ptr_to_mbr_type>
14808  (ptr_to_member,
14809  group->priv_->get_types().ptr_to_mbr_types());
14810  }
14811  }
14812 }
14813 
14814 /// Update the map that associates the fully qualified name of a
14815 /// reference type with the type itself.
14816 ///
14817 /// The per-translation unit type map is updated if no type with this
14818 /// name was already existing in that map.
14819 ///
14820 /// If no type with this name did already exist in the per-corpus type
14821 /// map, then that per-corpus type map is updated. Otherwise, that
14822 /// type is erased from that per-corpus map.
14823 ///
14824 /// @param reference_type the type to consider.
14825 void
14827 {
14828  if (translation_unit *tu = reference_type->get_translation_unit())
14829  maybe_update_types_lookup_map<reference_type_def>
14830  (reference_type, tu->get_types().reference_types());
14831 
14832  if (corpus *type_corpus = reference_type->get_corpus())
14833  {
14834  maybe_update_types_lookup_map<reference_type_def>
14835  (reference_type,
14836  type_corpus->priv_->get_types().reference_types());
14837 
14838  if (corpus *group = type_corpus->get_group())
14839  {
14840  maybe_update_types_lookup_map<reference_type_def>
14841  (reference_type,
14842  group->priv_->get_types().reference_types());
14843  }
14844  }
14845 }
14846 
14847 /// Update the map that associates the fully qualified name of a type
14848 /// with the type itself.
14849 ///
14850 /// The per-translation unit type map is updated if no type with this
14851 /// name was already existing in that map.
14852 ///
14853 /// If no type with this name did already exist in the per-corpus type
14854 /// map, then that per-corpus type map is updated. Otherwise, that
14855 /// type is erased from that per-corpus map.
14856 ///
14857 /// @param array_type the type to consider.
14858 void
14860 {
14861  if (translation_unit *tu = array_type->get_translation_unit())
14862  maybe_update_types_lookup_map<array_type_def>
14863  (array_type, tu->get_types().array_types());
14864 
14865  if (corpus *type_corpus = array_type->get_corpus())
14866  {
14867  maybe_update_types_lookup_map<array_type_def>
14868  (array_type,
14869  type_corpus->priv_->get_types().array_types());
14870 
14871  maybe_update_types_lookup_map<array_type_def>
14872  (array_type,
14873  type_corpus->get_type_per_loc_map().array_types(),
14874  /*use_type_name_as_key*/false);
14875 
14876  if (corpus *group = type_corpus->get_group())
14877  {
14878  maybe_update_types_lookup_map<array_type_def>
14879  (array_type,
14880  group->priv_->get_types().array_types());
14881 
14882  maybe_update_types_lookup_map<array_type_def>
14883  (array_type,
14884  group->get_type_per_loc_map().array_types(),
14885  /*use_type_name_as_key*/false);
14886  }
14887  }
14888 }
14889 
14890 /// Update the map that associates the fully qualified name of a type
14891 /// with the type itself.
14892 ///
14893 /// The per-translation unit type map is updated if no type with this
14894 /// name was already existing in that map.
14895 ///
14896 /// If no type with this name did already exist in the per-corpus type
14897 /// map, then that per-corpus type map is updated. Otherwise, that
14898 /// type is erased from that per-corpus map.
14899 ///
14900 /// @param subrange_type the type to consider.
14901 void
14903 (const array_type_def::subrange_sptr& subrange_type)
14904 {
14905  if (translation_unit *tu = subrange_type->get_translation_unit())
14906  maybe_update_types_lookup_map<array_type_def::subrange_type>
14907  (subrange_type, tu->get_types().subrange_types());
14908 
14909  if (corpus *type_corpus = subrange_type->get_corpus())
14910  {
14911  maybe_update_types_lookup_map<array_type_def::subrange_type>
14912  (subrange_type,
14913  type_corpus->priv_->get_types().subrange_types());
14914 
14915  maybe_update_types_lookup_map<array_type_def::subrange_type>
14916  (subrange_type,
14917  type_corpus->get_type_per_loc_map().subrange_types(),
14918  /*use_type_name_as_key*/false);
14919 
14920  if (corpus *group = subrange_type->get_corpus())
14921  {
14922  maybe_update_types_lookup_map<array_type_def::subrange_type>
14923  (subrange_type,
14924  group->priv_->get_types().subrange_types());
14925 
14926  maybe_update_types_lookup_map<array_type_def::subrange_type>
14927  (subrange_type,
14928  group->get_type_per_loc_map().subrange_types(),
14929  /*use_type_name_as_key*/false);
14930  }
14931  }
14932 }
14933 
14934 /// Update the map that associates the fully qualified name of a
14935 /// function type with the type itself.
14936 ///
14937 /// The per-translation unit type map is updated if no type with this
14938 /// name was already existing in that map.
14939 ///
14940 /// If no type with this name did already exist in the per-corpus type
14941 /// map, then that per-corpus type map is updated. Otherwise, that
14942 /// type is erased from that per-corpus map.
14943 ///
14944 /// @param scope the scope of the function type.
14945 /// @param fn_type the type to consider.
14946 void
14948 {
14949  if (translation_unit *tu = fn_type->get_translation_unit())
14951  (fn_type, tu->get_types().function_types());
14952 
14953  if (corpus *type_corpus = fn_type->get_corpus())
14954  {
14956  (fn_type,
14957  type_corpus->priv_->get_types().function_types());
14958 
14959  if (corpus *group = fn_type->get_corpus())
14960  {
14962  (fn_type,
14963  group->priv_->get_types().function_types());
14964  }
14965  }
14966 }
14967 
14968 /// Update the map that associates the fully qualified name of a type
14969 /// declaration with the type itself.
14970 ///
14971 /// The per-translation unit type map is updated if no type with this
14972 /// name was already existing in that map.
14973 ///
14974 /// If no type with this name did already exist in the per-corpus type
14975 /// map, then that per-corpus type map is updated. Otherwise, that
14976 /// type is erased from that per-corpus map.
14977 ///
14978 /// @param decl the declaration of the type to consider.
14979 void
14980 maybe_update_types_lookup_map(const decl_base_sptr& decl)
14981 {
14982  if (!is_type(decl))
14983  return;
14984 
14985  if (type_decl_sptr basic_type = is_type_decl(decl))
14986  maybe_update_types_lookup_map(basic_type);
14987  else if (class_decl_sptr class_type = is_class_type(decl))
14988  maybe_update_types_lookup_map(class_type);
14989  else if (union_decl_sptr union_type = is_union_type(decl))
14990  maybe_update_types_lookup_map(union_type);
14991  else if (enum_type_decl_sptr enum_type = is_enum_type(decl))
14992  maybe_update_types_lookup_map(enum_type);
14993  else if (typedef_decl_sptr typedef_type = is_typedef(decl))
14994  maybe_update_types_lookup_map(typedef_type);
14995  else if (qualified_type_def_sptr qualified_type = is_qualified_type(decl))
14996  maybe_update_types_lookup_map(qualified_type);
14997  else if (pointer_type_def_sptr pointer_type = is_pointer_type(decl))
14998  maybe_update_types_lookup_map(pointer_type);
14999  else if (ptr_to_mbr_type_sptr ptr_to_member = is_ptr_to_mbr_type(decl))
15000  maybe_update_types_lookup_map(ptr_to_member);
15001  else if (reference_type_def_sptr reference_type = is_reference_type(decl))
15002  maybe_update_types_lookup_map(reference_type);
15003  else if (array_type_def_sptr array_type = is_array_type(decl))
15004  maybe_update_types_lookup_map(array_type);
15005  else if (array_type_def::subrange_sptr subrange_type = is_subrange_type(decl))
15006  maybe_update_types_lookup_map(subrange_type);
15007  else if (function_type_sptr fn_type = is_function_type(decl))
15009  else
15011 }
15012 
15013 /// Update the map that associates the fully qualified name of a type
15014 /// with the type itself.
15015 ///
15016 /// The per-translation unit type map is updated if no type with this
15017 /// name was already existing in that map.
15018 ///
15019 /// If no type with this name did already exist in the per-corpus type
15020 /// map, then that per-corpus type map is updated. Otherwise, that
15021 /// type is erased from that per-corpus map.
15022 ///
15023 /// @param type the type to consider.
15024 void
15025 maybe_update_types_lookup_map(const type_base_sptr& type)
15026 {
15027  if (decl_base_sptr decl = get_type_declaration(type))
15029  else if (function_type_sptr fn_type = is_function_type(type))
15031  else
15033 }
15034 
15035 //--------------------------------
15036 // </type and decls lookup stuff>
15037 // ------------------------------
15038 
15039 /// In a translation unit, lookup a given type or synthesize it if
15040 /// it's a qualified type.
15041 ///
15042 /// So this function first looks the type up in the translation unit.
15043 /// If it's found, then OK, it's returned. Otherwise, if it's a
15044 /// qualified, reference or pointer or function type (a composite
15045 /// type), lookup the underlying type, synthesize the type we want
15046 /// from it and return it.
15047 ///
15048 /// If the underlying types is not not found, then give up and return
15049 /// nil.
15050 ///
15051 /// @return the type that was found or the synthesized type.
15052 type_base_sptr
15053 synthesize_type_from_translation_unit(const type_base_sptr& type,
15054  translation_unit& tu)
15055 {
15056  type_base_sptr result;
15057 
15058  result = lookup_type(type, tu);
15059 
15060  if (!result)
15061  {
15062  if (qualified_type_def_sptr qual = is_qualified_type(type))
15063  {
15064  type_base_sptr underlying_type =
15065  synthesize_type_from_translation_unit(qual->get_underlying_type(),
15066  tu);
15067  if (underlying_type)
15068  {
15069  result.reset(new qualified_type_def(underlying_type,
15070  qual->get_cv_quals(),
15071  qual->get_location()));
15072  }
15073  }
15074  else if (pointer_type_def_sptr p = is_pointer_type(type))
15075  {
15076  type_base_sptr pointed_to_type =
15077  synthesize_type_from_translation_unit(p->get_pointed_to_type(),
15078  tu);
15079  if (pointed_to_type)
15080  {
15081  result.reset(new pointer_type_def(pointed_to_type,
15082  p->get_size_in_bits(),
15083  p->get_alignment_in_bits(),
15084  p->get_location()));
15085  }
15086  }
15087  else if (reference_type_def_sptr r = is_reference_type(type))
15088  {
15089  type_base_sptr pointed_to_type =
15090  synthesize_type_from_translation_unit(r->get_pointed_to_type(), tu);
15091  if (pointed_to_type)
15092  {
15093  result.reset(new reference_type_def(pointed_to_type,
15094  r->is_lvalue(),
15095  r->get_size_in_bits(),
15096  r->get_alignment_in_bits(),
15097  r->get_location()));
15098  }
15099  }
15100  else if (function_type_sptr f = is_function_type(type))
15102 
15103  if (result)
15104  {
15106  canonicalize(result);
15107  }
15108  }
15109 
15110  if (result)
15111  tu.priv_->synthesized_types_.push_back(result);
15112 
15113  return result;
15114 }
15115 
15116 /// In a translation unit, lookup the sub-types that make up a given
15117 /// function type and if the sub-types are all found, synthesize and
15118 /// return a function_type with them.
15119 ///
15120 /// This function is like lookup_function_type_in_translation_unit()
15121 /// execept that it constructs the function type from the sub-types
15122 /// found in the translation, rather than just looking for the
15123 /// function types held by the translation unit. This can be useful
15124 /// if the translation unit doesnt hold the function type we are
15125 /// looking for (i.e, lookup_function_type_in_translation_unit()
15126 /// returned NULL) but we still want to see if the sub-types of the
15127 /// function types are present in the translation unit.
15128 ///
15129 /// @param fn_type the function type to consider.
15130 ///
15131 /// @param tu the translation unit to look into.
15132 ///
15133 /// @return the resulting synthesized function type if all its
15134 /// sub-types have been found, NULL otherwise.
15137  translation_unit& tu)
15138 {
15140 
15141  const environment& env = tu.get_environment();
15142 
15143  type_base_sptr return_type = fn_type.get_return_type();
15144  type_base_sptr result_return_type;
15145  if (!return_type || env.is_void_type(return_type))
15146  result_return_type = env.get_void_type();
15147  else
15148  result_return_type = synthesize_type_from_translation_unit(return_type, tu);
15149  if (!result_return_type)
15150  return nil;
15151 
15153  type_base_sptr parm_type;
15155  for (function_type::parameters::const_iterator i =
15156  fn_type.get_parameters().begin();
15157  i != fn_type.get_parameters().end();
15158  ++i)
15159  {
15160  type_base_sptr t = (*i)->get_type();
15161  parm_type = synthesize_type_from_translation_unit(t, tu);
15162  if (!parm_type)
15163  return nil;
15164  parm.reset(new function_decl::parameter(parm_type,
15165  (*i)->get_index(),
15166  (*i)->get_name(),
15167  (*i)->get_location(),
15168  (*i)->get_variadic_marker(),
15169  (*i)->get_is_artificial()));
15170  parms.push_back(parm);
15171  }
15172 
15173  class_or_union_sptr class_type;
15174  const method_type* method = is_method_type(&fn_type);
15175  if (method)
15176  {
15177  class_type = is_class_or_union_type
15179  ABG_ASSERT(class_type);
15180  }
15181 
15182  function_type_sptr result_fn_type;
15183 
15184  if (class_type)
15185  result_fn_type.reset(new method_type(result_return_type,
15186  class_type,
15187  parms,
15188  method->get_is_const(),
15189  fn_type.get_size_in_bits(),
15190  fn_type.get_alignment_in_bits()));
15191  else
15192  result_fn_type.reset(new function_type(result_return_type,
15193  parms,
15194  fn_type.get_size_in_bits(),
15195  fn_type.get_alignment_in_bits()));
15196 
15197  tu.priv_->synthesized_types_.push_back(result_fn_type);
15198  tu.bind_function_type_life_time(result_fn_type);
15199 
15200  canonicalize(result_fn_type);
15201  return result_fn_type;
15202 }
15203 
15204 /// Demangle a C++ mangled name and return the resulting string
15205 ///
15206 /// @param mangled_name the C++ mangled name to demangle.
15207 ///
15208 /// @return the resulting mangled name.
15209 string
15210 demangle_cplus_mangled_name(const string& mangled_name)
15211 {
15212  if (mangled_name.empty())
15213  return "";
15214 
15215  size_t l = 0;
15216  int status = 0;
15217  char * str = abi::__cxa_demangle(mangled_name.c_str(),
15218  NULL, &l, &status);
15219  string demangled_name = mangled_name;
15220  if (str)
15221  {
15222  ABG_ASSERT(status == 0);
15223  demangled_name = str;
15224  free(str);
15225  str = 0;
15226  }
15227  return demangled_name;
15228 }
15229 
15230 /// Return either the type given in parameter if it's non-null, or the
15231 /// void type.
15232 ///
15233 /// @param t the type to consider.
15234 ///
15235 /// @param env the environment to use. If NULL, just abort the
15236 /// process.
15237 ///
15238 /// @return either @p t if it is non-null, or the void type.
15239 type_base_sptr
15240 type_or_void(const type_base_sptr t, const environment& env)
15241 {
15242  type_base_sptr r;
15243 
15244  if (t)
15245  r = t;
15246  else
15247  r = type_base_sptr(env.get_void_type());
15248 
15249  return r;
15250 }
15251 
15252 global_scope::~global_scope()
15253 {
15254 }
15255 
15256 static bool
15257 maybe_propagate_canonical_type(const type_base& lhs_type,
15258  const type_base& rhs_type);
15259 
15260 /// Test if two types are eligible to the "Linux Kernel Fast Type
15261 /// Comparison Optimization", a.k.a LKFTCO.
15262 ///
15263 /// Two types T1 and T2 (who are presumably of the same name and kind)
15264 /// are eligible to the LKFTCO if they fulfill the following criteria/
15265 ///
15266 /// 1/ T1 and T2 come from the same Linux Kernel Corpus and they are
15267 /// either class, union or enums.
15268 ///
15269 /// 2/ They are defined in the same translation unit.
15270 ///
15271 /// @param t1 the first type to consider.
15272 ///
15273 /// @param t2 the second type to consider.
15274 ///
15275 /// @return true iff t1 and t2 are eligible to the LKFTCO.
15276 static bool
15277 types_defined_same_linux_kernel_corpus_public(const type_base& t1,
15278  const type_base& t2)
15279 {
15280  const corpus *t1_corpus = t1.get_corpus(), *t2_corpus = t2.get_corpus();
15281  string t1_file_path, t2_file_path;
15282 
15283  /// If the t1 (and t2) are classes/unions/enums from the same linux
15284  /// kernel corpus, let's move on. Otherwise bail out.
15285  if (!(t1_corpus && t2_corpus
15286  && t1_corpus == t2_corpus
15287  && (t1_corpus->get_origin() & corpus::LINUX_KERNEL_BINARY_ORIGIN)
15288  && (is_class_or_union_type(&t1)
15289  || is_enum_type(&t1))))
15290  return false;
15291 
15292  class_or_union *c1 = 0, *c2 = 0;
15293  c1 = is_class_or_union_type(&t1);
15294  c2 = is_class_or_union_type(&t2);
15295 
15296  // Two anonymous class types with no naming typedefs cannot be
15297  // eligible to this optimization.
15298  if ((c1 && c1->get_is_anonymous() && !c1->get_naming_typedef())
15299  || (c2 && c2->get_is_anonymous() && !c2->get_naming_typedef()))
15300  return false;
15301 
15302  // Two anonymous classes with naming typedefs should have the same
15303  // typedef name.
15304  if (c1
15305  && c2
15306  && c1->get_is_anonymous() && c1->get_naming_typedef()
15307  && c2->get_is_anonymous() && c2->get_naming_typedef())
15308  if (c1->get_naming_typedef()->get_name()
15309  != c2->get_naming_typedef()->get_name())
15310  return false;
15311 
15312  // Two anonymous enum types cannot be eligible to this optimization.
15313  if (const enum_type_decl *e1 = is_enum_type(&t1))
15314  if (const enum_type_decl *e2 = is_enum_type(&t2))
15315  if (e1->get_is_anonymous() || e2->get_is_anonymous())
15316  return false;
15317 
15318  // Look through declaration-only types. That is, get the associated
15319  // definition type.
15322 
15323  if (c1 && c2)
15324  {
15325  if (c1->get_is_declaration_only() != c2->get_is_declaration_only())
15326  {
15327  if (c1->get_environment().decl_only_class_equals_definition())
15328  // At least one of classes/union is declaration-only.
15329  // Because we are in a context in which a declaration-only
15330  // class/union is equal to all definitions of that
15331  // class/union, we can assume that the two types are
15332  // equal.
15333  return true;
15334  }
15335  }
15336 
15337  if (t1.get_size_in_bits() != t2.get_size_in_bits())
15338  return false;
15339 
15340  // Look at the file names of the locations of t1 and t2. If they
15341  // are equal, then t1 and t2 are defined in the same file.
15342  {
15343  location l;
15344 
15345  if (c1)
15346  l = c1->get_location();
15347  else
15348  l = dynamic_cast<const decl_base&>(t1).get_location();
15349 
15350  unsigned line = 0, col = 0;
15351  if (l)
15352  l.expand(t1_file_path, line, col);
15353  if (c2)
15354  l = c2->get_location();
15355  else
15356  l = dynamic_cast<const decl_base&>(t2).get_location();
15357  if (l)
15358  l.expand(t2_file_path, line, col);
15359  }
15360 
15361  if (t1_file_path.empty() || t2_file_path.empty())
15362  return false;
15363 
15364  if (t1_file_path == t2_file_path)
15365  return true;
15366 
15367  return false;
15368 }
15369 
15370 
15371 /// Compare a type T against a canonical type.
15372 ///
15373 /// This function is called during the canonicalization process of the
15374 /// type T. T is called the "candidate type" because it's in the
15375 /// process of being canonicalized. Meaning, it's going to be
15376 /// compared to a canonical type C. If T equals C, then the canonical
15377 /// type of T is C.
15378 ///
15379 /// The purpose of this function is to allow the debugging of the
15380 /// canonicalization of T, if that debugging is activated by
15381 /// configuring the libabigail package with
15382 /// --enable-debug-type-canonicalization and by running "abidw
15383 /// --debug-tc". In that case, T is going to be compared to C twice:
15384 /// once with canonical equality and once with structural equality.
15385 /// The two comparisons must be equal. Otherwise, the
15386 /// canonicalization process is said to be faulty and this function
15387 /// aborts.
15388 ///
15389 /// This is a sub-routine of type_base::get_canonical_type_for.
15390 ///
15391 /// @param canonical_type the canonical type to compare the candidate
15392 /// type against.
15393 ///
15394 /// @param candidate_type the candidate type to compare against the
15395 /// canonical type.
15396 ///
15397 /// @return true iff @p canonical_type equals @p candidate_type.
15398 ///
15399 static bool
15400 compare_types_during_canonicalization(const type_base& canonical_type,
15401  const type_base& candidate_type)
15402 {
15403 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
15404  const environment& env = canonical_type.get_environment();
15405  if (env.debug_type_canonicalization_is_on())
15406  {
15407  bool canonical_equality = false, structural_equality = false;
15408  env.priv_->use_canonical_type_comparison_ = false;
15409  structural_equality = canonical_type == candidate_type;
15410  env.priv_->use_canonical_type_comparison_ = true;
15411  canonical_equality = canonical_type == candidate_type;
15412  if (canonical_equality != structural_equality)
15413  {
15414  std::cerr << "structural & canonical equality different for type: "
15415  << canonical_type.get_pretty_representation(true, true)
15416  << std::endl;
15418  }
15419  return structural_equality;
15420  }
15421 #endif //end WITH_DEBUG_TYPE_CANONICALIZATION
15422  return canonical_type == candidate_type;
15423 }
15424 
15425 /// Compare a canonical type against a candidate canonical type.
15426 ///
15427 /// This is ultimately a sub-routine of the
15428 /// type_base::get_canonical_type_for().
15429 ///
15430 /// The goal of this function is to ease debugging because it can be
15431 /// called from within type_base::get_canonical_type_for() from the
15432 /// prompt of the debugger (with some breakpoint appropriately set) to
15433 /// debug the comparison that happens during type canonicalization,
15434 /// between a candidate type being canonicalized, and an existing
15435 /// canonical type that is registered in the system, in as returned by
15436 /// environment::get_canonical_types()
15437 ///
15438 /// @param canonical_type the canonical type to consider.
15439 ///
15440 /// @param candidate_type the candidate type that is being
15441 /// canonicalized, and thus compared to @p canonical_type.
15442 ///
15443 /// @return true iff @p canonical_type compares equal to @p
15444 /// candidate_type.
15445 static bool
15446 compare_canonical_type_against_candidate(const type_base& canonical_type,
15447  const type_base& candidate_type)
15448 {
15449  environment& env = const_cast<environment&>(canonical_type.get_environment());
15450 
15451  // Before the "*it == it" comparison below is done, let's
15452  // perform on-the-fly-canonicalization. For C types, let's
15453  // consider that an unresolved struct declaration 'struct S'
15454  // is different from a definition 'struct S'. This is
15455  // because normally, at this point all the declarations of
15456  // struct S that are compatible with the definition of
15457  // struct S have already been resolved to that definition,
15458  // during the DWARF parsing. The remaining unresolved
15459  // declaration are thus considered different. With this
15460  // setup we can properly handle cases of two *different*
15461  // struct S being defined in the same binary (in different
15462  // translation units), and a third struct S being only
15463  // declared as an opaque type in a third translation unit of
15464  // its own, with no definition in there. In that case, the
15465  // declaration-only struct S should be left alone and not
15466  // resolved to any of the two definitions of struct S.
15467  bool saved_decl_only_class_equals_definition =
15468  env.decl_only_class_equals_definition();
15469  env.do_on_the_fly_canonicalization(true);
15470  // Compare types by considering that decl-only classes don't
15471  // equal their definition.
15472  env.decl_only_class_equals_definition(false);
15473  env.priv_->allow_type_comparison_results_caching(true);
15474  bool equal = (types_defined_same_linux_kernel_corpus_public(canonical_type,
15475  candidate_type)
15476  || compare_types_during_canonicalization(canonical_type,
15477  candidate_type));
15478  // Restore the state of the on-the-fly-canonicalization and
15479  // the decl-only-class-being-equal-to-a-matching-definition
15480  // flags.
15481  env.priv_->clear_type_comparison_results_cache();
15482  env.priv_->allow_type_comparison_results_caching(false);
15483  env.do_on_the_fly_canonicalization(false);
15484  env.decl_only_class_equals_definition
15485  (saved_decl_only_class_equals_definition);
15486  return equal;
15487 }
15488 
15489 /// Compare a canonical type against a candidate canonical type.
15490 ///
15491 /// This is ultimately a sub-routine of the
15492 /// type_base::get_canonical_type_for().
15493 ///
15494 /// The goal of this function is to ease debugging because it can be
15495 /// called from within type_base::get_canonical_type_for() from the
15496 /// prompt of the debugger (with some breakpoint appropriately set) to
15497 /// debug the comparison that happens during type canonicalization,
15498 /// between a candidate type being canonicalized, and an existing
15499 /// canonical type that is registered in the system, in as returned by
15500 /// environment::get_canonical_types()
15501 ///
15502 /// @param canonical_type the canonical type to consider.
15503 ///
15504 /// @param candidate_type the candidate type that is being
15505 /// canonicalized, and thus compared to @p canonical_type.
15506 ///
15507 /// @return true iff @p canonical_type compares equal to @p
15508 /// candidate_type.
15509 static bool
15510 compare_canonical_type_against_candidate(const type_base* canonical_type,
15511  const type_base* candidate_type)
15512 {
15513  return compare_canonical_type_against_candidate(*canonical_type,
15514  *candidate_type);
15515 }
15516 
15517 /// Compare a canonical type against a candidate canonical type.
15518 ///
15519 /// This is ultimately a sub-routine of the
15520 /// type_base::get_canonical_type_for().
15521 ///
15522 /// The goal of this function is to ease debugging because it can be
15523 /// called from within type_base::get_canonical_type_for() from the
15524 /// prompt of the debugger (with some breakpoint appropriately set) to
15525 /// debug the comparison that happens during type canonicalization,
15526 /// between a candidate type being canonicalized, and an existing
15527 /// canonical type that is registered in the system, in as returned by
15528 /// environment::get_canonical_types()
15529 ///
15530 /// @param canonical_type the canonical type to consider.
15531 ///
15532 /// @param candidate_type the candidate type that is being
15533 /// canonicalized, and thus compared to @p canonical_type.
15534 ///
15535 /// @return true iff @p canonical_type compares equal to @p
15536 /// candidate_type.
15537 static bool
15538 compare_canonical_type_against_candidate(const type_base_sptr& canonical_type,
15539  const type_base_sptr& candidate_type)
15540 {
15541  return compare_canonical_type_against_candidate(canonical_type.get(),
15542  candidate_type.get());
15543 }
15544 
15545 /// Compute the canonical type for a given instance of @ref type_base.
15546 ///
15547 /// Consider two types T and T'. The canonical type of T, denoted
15548 /// C(T) is a type such as T == T' if and only if C(T) == C(T'). Said
15549 /// otherwise, to compare two types, one just needs to compare their
15550 /// canonical types using pointer equality. That makes type
15551 /// comparison faster than the structural comparison performed by the
15552 /// abigail::ir::equals() overloads.
15553 ///
15554 /// If there is not yet any canonical type for @p t, then @p t is its
15555 /// own canonical type. Otherwise, this function returns the
15556 /// canonical type of @p t which is the canonical type that has the
15557 /// same hash value as @p t and that structurally equals @p t. Note
15558 /// that after invoking this function, the life time of the returned
15559 /// canonical time is then equals to the life time of the current
15560 /// process.
15561 ///
15562 /// @param t a smart pointer to instance of @ref type_base we want to
15563 /// compute a canonical type for.
15564 ///
15565 /// @return the canonical type for the current instance of @ref
15566 /// type_base.
15567 type_base_sptr
15568 type_base::get_canonical_type_for(type_base_sptr t)
15569 {
15570  if (!t)
15571  return t;
15572 
15573  environment& env = const_cast<environment&>(t->get_environment());
15574 
15576  // This type should not be canonicalized!
15577  return type_base_sptr();
15578 
15579  if (is_decl(t))
15581 
15582  // Look through decl-only types (classes, unions and enums)
15583  bool decl_only_class_equals_definition =
15584  (odr_is_relevant(*t) || env.decl_only_class_equals_definition());
15585 
15586  class_or_union_sptr class_or_union = is_class_or_union_type(t);
15587 
15588  // In the context of types from C++ or languages where we assume the
15589  // "One Definition Rule", we assume that a declaration-only
15590  // non-anonymous class equals all fully defined classes of the same
15591  // name.
15592  //
15593  // Otherwise, all classes, including declaration-only classes are
15594  // canonicalized and only canonical comparison is going to be used
15595  // in the system.
15596  if (decl_only_class_equals_definition)
15597  if (class_or_union)
15598  if (class_or_union->get_is_declaration_only())
15599  return type_base_sptr();
15600 
15601  class_decl_sptr is_class = is_class_type(t);
15602  if (t->get_canonical_type())
15603  return t->get_canonical_type();
15604 
15605  // For classes and union, ensure that an anonymous class doesn't
15606  // have a linkage name. If it does in the future, then me must be
15607  // mindful that the linkage name respects the type identity
15608  // constraints which states that "if two linkage names are different
15609  // then the two types are different".
15610  ABG_ASSERT(!class_or_union
15611  || !class_or_union->get_is_anonymous()
15612  || class_or_union->get_linkage_name().empty());
15613 
15614  // We want the pretty representation of the type, but for an
15615  // internal use, not for a user-facing purpose.
15616  //
15617  // If two classe types Foo are declared, one as a class and the
15618  // other as a struct, but are otherwise equivalent, we want their
15619  // pretty representation to be the same. Hence the 'internal'
15620  // argument of ir::get_pretty_representation() is set to true here.
15621  // So in this case, the pretty representation of Foo is going to be
15622  // "class Foo", regardless of its struct-ness. This also applies to
15623  // composite types which would have "class Foo" as a sub-type.
15624  string repr = t->get_cached_pretty_representation(/*internal=*/true);
15625 
15626  // If 't' already has a canonical type 'inside' its corpus
15627  // (t_corpus), then this variable is going to contain that canonical
15628  // type.
15629  type_base_sptr canonical_type_present_in_corpus;
15631  env.get_canonical_types_map();
15632 
15633  type_base_sptr result;
15634  environment::canonical_types_map_type::iterator i = types.find(repr);
15635  if (i == types.end())
15636  {
15637  vector<type_base_sptr> v;
15638  v.push_back(t);
15639  types[repr] = v;
15640  result = t;
15641  }
15642  else
15643  {
15644  vector<type_base_sptr> &v = i->second;
15645  // Let's compare 't' structurally (i.e, compare its sub-types
15646  // recursively) against the canonical types of the system. If it
15647  // equals a given canonical type C, then it means C is the
15648  // canonical type of 't'. Otherwise, if 't' is different from
15649  // all the canonical types of the system, then it means 't' is a
15650  // canonical type itself.
15651  for (vector<type_base_sptr>::const_reverse_iterator it = v.rbegin();
15652  it != v.rend();
15653  ++it)
15654  {
15655  bool equal = compare_canonical_type_against_candidate(*it, t);
15656  if (equal)
15657  {
15658  result = *it;
15659  break;
15660  }
15661  }
15662 #ifdef WITH_DEBUG_SELF_COMPARISON
15663  if (env.self_comparison_debug_is_on())
15664  {
15665  // So we are debugging the canonicalization process,
15666  // possibly via the use of 'abidw --debug-abidiff <binary>'.
15667  corpus_sptr corp1, corp2;
15668  env.get_self_comparison_debug_inputs(corp1, corp2);
15669  if (corp1 && corp2 && t->get_corpus() == corp2.get())
15670  {
15671  // If 't' comes from the second corpus, then it *must*
15672  // be equal to its matching canonical type coming from
15673  // the first corpus because the second corpus is the
15674  // abixml representation of the first corpus. In other
15675  // words, all types coming from the second corpus must
15676  // have canonical types coming from the first corpus.
15677  if (result)
15678  {
15679  if (!env.priv_->
15680  check_canonical_type_from_abixml_during_self_comp(t,
15681  result))
15682  {
15683  // The canonical type of the type re-read from abixml
15684  // type doesn't match the canonical type that was
15685  // initially serialized down.
15686  uintptr_t should_have_canonical_type = 0;
15687  string type_id = env.get_type_id_from_type(t.get());
15688  if (type_id.empty())
15689  type_id = "type-id-<not-found>";
15690  else
15691  should_have_canonical_type =
15692  env.get_canonical_type_from_type_id(type_id.c_str());
15693  std::cerr << "error: wrong canonical type for '"
15694  << repr
15695  << "' / type: @"
15696  << std::hex
15697  << t.get()
15698  << "/ canon: @"
15699  << result.get()
15700  << ", type-id: '"
15701  << type_id
15702  << "'. Should have had canonical type: "
15703  << std::hex
15704  << should_have_canonical_type
15705  << std::endl;
15706  }
15707  }
15708  else //!result
15709  {
15710  uintptr_t ptr_val = reinterpret_cast<uintptr_t>(t.get());
15711  string type_id = env.get_type_id_from_pointer(ptr_val);
15712  if (type_id.empty())
15713  type_id = "type-id-<not-found>";
15714  // We are in the case where 't' is different from all
15715  // the canonical types of the same name that come from
15716  // the first corpus.
15717  //
15718  // If 't' indeed comes from the second corpus then this
15719  // clearly is a canonicalization failure.
15720  //
15721  // There was a problem either during the serialization
15722  // of 't' into abixml, or during the de-serialization
15723  // from abixml into abigail::ir. Further debugging is
15724  // needed to determine what that root cause problem is.
15725  //
15726  // Note that the first canonicalization problem of this
15727  // kind must be fixed before looking at the subsequent
15728  // ones, because the later might well just be
15729  // consequences of the former.
15730  std::cerr << "error: wrong induced canonical type for '"
15731  << repr
15732  << "' from second corpus"
15733  << ", ptr: " << std::hex << t.get()
15734  << " type-id: " << type_id
15735  << std::endl;
15736  }
15737  }
15738  }
15739 #endif //WITH_DEBUG_SELF_COMPARISON
15740 
15741  if (!result)
15742  {
15743  v.push_back(t);
15744  result = t;
15745  }
15746  }
15747 
15748  return result;
15749 }
15750 
15751 /// This method is invoked automatically right after the current
15752 /// instance of @ref class_decl has been canonicalized.
15753 void
15755 {}
15756 
15757 /// This is a subroutine of the canonicalize() function.
15758 ///
15759 /// When the canonical type C of type T has just been computed, there
15760 /// can be cases where T has member functions that C doesn't have.
15761 ///
15762 /// This is possible because non virtual member functions are not
15763 /// taken in account when comparing two types.
15764 ///
15765 /// In that case, this function updates C so that it contains the
15766 /// member functions.
15767 ///
15768 /// There can also be cases where C has a method M which is not linked
15769 /// to any underlying symbol, whereas in T, M is to link to an
15770 /// underlying symbol. In that case, this function updates M in C so
15771 /// that it's linked to the same underlying symbol as for M in T.
15772 static void
15773 maybe_adjust_canonical_type(const type_base_sptr& canonical,
15774  const type_base_sptr& type)
15775 {
15776  if (type->get_naked_canonical_type())
15777  return;
15778 
15779  class_decl_sptr canonical_class = is_class_type(canonical);
15780 
15781  if (class_decl_sptr cl = is_class_type(type))
15782  {
15783  if (canonical_class
15784  && canonical_class.get() != cl.get())
15785  {
15786  // Set symbols of member functions that might be missing
15787  // theirs.
15788  for (class_decl::member_functions::const_iterator i =
15789  cl->get_member_functions().begin();
15790  i != cl->get_member_functions().end();
15791  ++i)
15792  if ((*i)->get_symbol())
15793  {
15794  if (method_decl *m = canonical_class->
15795  find_member_function((*i)->get_linkage_name()))
15796  {
15797  elf_symbol_sptr s1 = (*i)->get_symbol();
15798  if (s1 && !m->get_symbol())
15799  // Method 'm' in the canonical type is not
15800  // linked to the underlying symbol of '*i'.
15801  // Let's link it now. have th
15802  m->set_symbol(s1);
15803  }
15804  else
15805  // There is a member function defined and publicly
15806  // exported in the other class, and the canonical
15807  // class doesn't have that member function. Let's
15808  // copy that member function to the canonical class
15809  // then.
15810  {
15811  method_decl_sptr method =
15812  copy_member_function (canonical_class, *i);
15813  canonicalize(method->get_type());
15814  }
15815  }
15816  }
15817  }
15818 
15819  // Make sure the virtual member functions with exported symbols are
15820  // all added to the set of exported functions of the corpus.
15821 
15822  // If we are looking at a non-canonicalized class (for instance, a
15823  // decl-only class that has virtual member functoins), let's pretend
15824  // it does have a canonical class so that we can perform the
15825  // necessary virtual member function adjustments
15826  if (class_decl_sptr cl = is_class_type(type))
15827  if (is_non_canonicalized_type(cl))
15828  {
15829  ABG_ASSERT(!canonical_class);
15830  canonical_class = cl;
15831  }
15832 
15833  if (canonical_class)
15834  {
15835  if (auto abi_corpus = canonical_class->get_corpus())
15836  {
15837  for (auto& fn : canonical_class->get_member_functions())
15838  {
15839  if (elf_symbol_sptr sym = fn->get_symbol())
15840  {
15841  if (sym->is_defined() && sym->is_public())
15842  {
15843  fn->set_is_in_public_symbol_table(true);
15844  auto b = abi_corpus->get_exported_decls_builder();
15845  b->maybe_add_fn_to_exported_fns(fn.get());
15846  }
15847  else if (!sym->is_defined())
15848  abi_corpus->get_undefined_functions().insert(fn.get());
15849  }
15850  }
15851  }
15852  }
15853 
15854  // If an artificial function type equals a non-artfificial one in
15855  // the system, then the canonical type of both should be deemed
15856  // non-artificial. This is important because only non-artificial
15857  // canonical function types are emitted out into abixml, so if don't
15858  // do this we risk missing to emit some function types.
15859  if (is_function_type(type))
15860  if (type->get_is_artificial() != canonical->get_is_artificial())
15861  canonical->set_is_artificial(false);
15862 }
15863 
15864 /// Compute the canonical type of a given type.
15865 ///
15866 /// It means that after invoking this function, comparing the intance
15867 /// instance @ref type_base and another one (on which
15868 /// type_base::enable_canonical_equality() would have been invoked as
15869 /// well) is performed by just comparing the pointer values of the
15870 /// canonical types of both types. That equality comparison is
15871 /// supposedly faster than structural comparison of the types.
15872 ///
15873 /// @param t a smart pointer to the instance of @ref type_base for
15874 /// which to compute the canonical type. After this call,
15875 /// t->get_canonical_type() will return the newly computed canonical
15876 /// type.
15877 ///
15878 /// @return the canonical type computed for @p t.
15879 type_base_sptr
15880 canonicalize(type_base_sptr t)
15881 {
15882  if (!t)
15883  return t;
15884 
15885  if (t->get_canonical_type())
15886  return t->get_canonical_type();
15887 
15888  if (t->get_environment().priv_->do_log())
15889  std::cerr << "Canonicalization of type '"
15890  << t->get_pretty_representation(true, true)
15891  << "/@#" << std::hex << t.get() << ": ";
15892 
15893  tools_utils::timer tmr;
15894 
15895  if (t->get_environment().priv_->do_log())
15896  tmr.start();
15897  type_base_sptr canonical = type_base::get_canonical_type_for(t);
15898 
15899  if (t->get_environment().priv_->do_log())
15900  tmr.stop();
15901 
15902  if (t->get_environment().priv_->do_log())
15903  std::cerr << tmr << "\n";
15904 
15905  maybe_adjust_canonical_type(canonical, t);
15906 
15907  t->priv_->canonical_type = canonical;
15908  t->priv_->naked_canonical_type = canonical.get();
15909 
15910  // So this type is now canonicalized.
15911  //
15912  // It means that:
15913  //
15914  // 1/ Either the canonical type was not propagated during the
15915  // comparison of another type that was being canonicalized
15916  //
15917  // 2/ Or the canonical type has been propagated during the
15918  // comparison of another type that was being canonicalized and
15919  // that propagated canonical type has been confirmed, because
15920  // it was depending on a recursive type which comparison
15921  // succeeded.
15922  ABG_ASSERT(!t->priv_->canonical_type_propagated()
15923  || t->priv_->propagated_canonical_type_confirmed());
15924 
15925  if (class_decl_sptr cl = is_class_type(t))
15926  if (type_base_sptr d = is_type(cl->get_earlier_declaration()))
15927  if ((canonical = d->get_canonical_type()))
15928  {
15929  d->priv_->canonical_type = canonical;
15930  d->priv_->naked_canonical_type = canonical.get();
15931  }
15932 
15933  if (canonical)
15934  {
15935  if (decl_base_sptr d = is_decl_slow(canonical))
15936  {
15937  scope_decl *scope = d->get_scope();
15938  // Add the canonical type to the set of canonical types
15939  // belonging to its scope.
15940  if (scope)
15941  {
15942  if (is_type(scope))
15943  // The scope in question is itself a type (e.g, a class
15944  // or union). Let's call that type ST. We want to add
15945  // 'canonical' to the set of canonical types belonging
15946  // to ST.
15947  if (type_base_sptr c = is_type(scope)->get_canonical_type())
15948  // We want to add 'canonical' to set of canonical
15949  // types belonging to the canonical type of ST. That
15950  // way, just looking at the canonical type of ST is
15951  // enough to get the types that belong to the scope of
15952  // the class of equivalence of ST.
15953  scope = is_scope_decl(is_decl(c)).get();
15954  scope->get_canonical_types().insert(canonical);
15955  }
15956  // else, if the type doesn't have a scope, it's not meant to be
15957  // emitted. This can be the case for the result of the
15958  // function strip_typedef, for instance.
15959  }
15960 
15961 #ifdef WITH_DEBUG_CT_PROPAGATION
15962  // Update the book-keeping of the set of the types which
15963  // propagated canonical type has been cleared.
15964  //
15965  // If this type 't' which has just been canonicalized was
15966  // previously in the set of types which propagated canonical
15967  // type has been cleared, then remove it from that set because
15968  // its canonical type is now computed and definitely set.
15969  const environment& env = t->get_environment();
15970  env.priv_->erase_type_with_cleared_propagated_canonical_type(t.get());
15971 #endif
15972  }
15973 
15974  t->on_canonical_type_set();
15975  return canonical;
15976 }
15977 
15978 /// Set the definition of this declaration-only @ref decl_base.
15979 ///
15980 /// @param d the new definition to set.
15981 void
15983 {
15985  priv_->definition_of_declaration_ = d;
15986  if (type_base *t = is_type(this))
15987  if (type_base_sptr canonical_type = is_type(d)->get_canonical_type())
15988  t->priv_->canonical_type = canonical_type;
15989 
15990  priv_->naked_definition_of_declaration_ = const_cast<decl_base*>(d.get());
15991 }
15992 
15993 /// The constructor of @ref type_base.
15994 ///
15995 /// @param s the size of the type, in bits.
15996 ///
15997 /// @param a the alignment of the type, in bits.
15998 type_base::type_base(const environment& e, size_t s, size_t a)
15999  : type_or_decl_base(e, ABSTRACT_TYPE_BASE|ABSTRACT_TYPE_BASE),
16000  priv_(new priv(s, a))
16001 {}
16002 
16003 /// Getter of the canonical type of the current instance of @ref
16004 /// type_base.
16005 ///
16006 /// @return a smart pointer to the canonical type of the current
16007 /// intance of @ref type_base, or an empty smart pointer if the
16008 /// current instance of @ref type_base doesn't have any canonical
16009 /// type.
16010 type_base_sptr
16012 {return priv_->canonical_type.lock();}
16013 
16014 /// Getter of the canonical type pointer.
16015 ///
16016 /// Note that this function doesn't return a smart pointer, but rather
16017 /// the underlying pointer managed by the smart pointer. So it's as
16018 /// fast as possible. This getter is to be used in code paths that
16019 /// are proven to be performance hot spots; especially, when comparing
16020 /// sensitive types like class, function, pointers and reference
16021 /// types. Those are compared extremely frequently and thus, their
16022 /// accessing the canonical type must be fast.
16023 ///
16024 /// @return the canonical type pointer, not managed by a smart
16025 /// pointer.
16026 type_base*
16028 {return priv_->naked_canonical_type;}
16029 
16030 /// Get the pretty representation of the current type.
16031 ///
16032 /// The pretty representation is retrieved from a cache. If the cache
16033 /// is empty, this function computes the pretty representation, put it
16034 /// in the cache and returns it.
16035 ///
16036 /// Note that if the type is *NOT* canonicalized, the pretty
16037 /// representation is never cached.
16038 ///
16039 /// @param internal if true, then the pretty representation is to be
16040 /// used for purpuses that are internal to the libabigail library
16041 /// itself. If you don't know what this means, then you probably
16042 /// should set this parameter to "false".
16043 const interned_string&
16045 {
16046  if (internal)
16047  {
16048  if (!get_naked_canonical_type() || priv_->internal_cached_repr_.empty())
16049  {
16050  string r = ir::get_pretty_representation(this, internal);
16051  priv_->internal_cached_repr_ = get_environment().intern(r);
16052  }
16053  return priv_->internal_cached_repr_;
16054  }
16055 
16056  if (!get_naked_canonical_type() || priv_->cached_repr_.empty())
16057  {
16058  string r = ir::get_pretty_representation(this, internal);
16059  priv_->cached_repr_ = get_environment().intern(r);
16060  }
16061 
16062  return priv_->cached_repr_;
16063 }
16064 
16065 /// Compares two instances of @ref type_base.
16066 ///
16067 /// If the two intances are different, set a bitfield to give some
16068 /// insight about the kind of differences there are.
16069 ///
16070 /// @param l the first artifact of the comparison.
16071 ///
16072 /// @param r the second artifact of the comparison.
16073 ///
16074 /// @param k a pointer to a bitfield that gives information about the
16075 /// kind of changes there are between @p l and @p r. This one is set
16076 /// iff @p is non-null and if the function returns false.
16077 ///
16078 /// Please note that setting k to a non-null value does have a
16079 /// negative performance impact because even if @p l and @p r are not
16080 /// equal, the function keeps up the comparison in order to determine
16081 /// the different kinds of ways in which they are different.
16082 ///
16083 /// @return true if @p l equals @p r, false otherwise.
16084 bool
16085 equals(const type_base& l, const type_base& r, change_kind* k)
16086 {
16087  bool result = (l.get_size_in_bits() == r.get_size_in_bits()
16089  if (!result)
16090  if (k)
16091  *k |= LOCAL_TYPE_CHANGE_KIND;
16092  ABG_RETURN(result);
16093 }
16094 
16095 /// Return true iff both type declarations are equal.
16096 ///
16097 /// Note that this doesn't test if the scopes of both types are equal.
16098 bool
16100 {return equals(*this, other, 0);}
16101 
16102 /// Inequality operator.
16103 ///
16104 ///@param other the instance of @ref type_base to compare the current
16105 /// instance against.
16106 ///
16107 /// @return true iff the current instance is different from @p other.
16108 bool
16110 {return !operator==(other);}
16111 
16112 /// Setter for the size of the type.
16113 ///
16114 /// @param s the new size -- in bits.
16115 void
16117 {priv_->size_in_bits = s;}
16118 
16119 /// Getter for the size of the type.
16120 ///
16121 /// @return the size in bits of the type.
16122 size_t
16124 {return priv_->size_in_bits;}
16125 
16126 /// Setter for the alignment of the type.
16127 ///
16128 /// @param a the new alignment -- in bits.
16129 void
16131 {priv_->alignment_in_bits = a;}
16132 
16133 /// Getter for the alignment of the type.
16134 ///
16135 /// @return the alignment of the type in bits.
16136 size_t
16138 {return priv_->alignment_in_bits;}
16139 
16140 /// Default implementation of traversal for types. This function does
16141 /// nothing. It must be implemented by every single new type that is
16142 /// written.
16143 ///
16144 /// Please look at e.g, class_decl::traverse() for an example of how
16145 /// to implement this.
16146 ///
16147 /// @param v the visitor used to visit the type.
16148 bool
16150 {
16151  if (v.type_node_has_been_visited(this))
16152  return true;
16153 
16154  v.visit_begin(this);
16155  bool result = v.visit_end(this);
16156  v.mark_type_node_as_visited(this);
16157 
16158  return result;
16159 }
16160 
16161 type_base::~type_base()
16162 {delete priv_;}
16163 
16164 // </type_base definitions>
16165 
16166 // <integral_type definitions>
16167 
16168 /// Bitwise OR operator for integral_type::modifiers_type.
16169 ///
16170 /// @param l the left-hand side operand.
16171 ///
16172 /// @param r the right-hand side operand.
16173 ///
16174 /// @return the result of the bitwise OR.
16177 {
16178  return static_cast<integral_type::modifiers_type>(static_cast<unsigned>(l)
16179  |
16180  static_cast<unsigned>(r));
16181 }
16182 
16183 /// Bitwise AND operator for integral_type::modifiers_type.
16184 ///
16185 /// @param l the left-hand side operand.
16186 ///
16187 /// @param r the right-hand side operand.
16188 ///
16189 /// @return the result of the bitwise AND.
16192 {
16193  return static_cast<integral_type::modifiers_type>(static_cast<unsigned>(l)
16194  &
16195  static_cast<unsigned>(r));
16196 }
16197 
16198 /// Bitwise one's complement operator for integral_type::modifiers_type.
16199 ///
16200 /// @param l the left-hand side operand.
16201 ///
16202 /// @param r the right-hand side operand.
16203 ///
16204 /// @return the result of the bitwise one's complement operator.
16207 {
16208  return static_cast<integral_type::modifiers_type>(~static_cast<unsigned>(l));
16209 }
16210 
16211 /// Bitwise |= operator for integral_type::modifiers_type.
16212 ///
16213 /// @param l the left-hand side operand.
16214 ///
16215 /// @param r the right-hand side operand.
16216 ///
16217 /// @return the result of the bitwise |=.
16220 {
16221  l = l | r;
16222  return l;
16223 }
16224 
16225 /// Bitwise &= operator for integral_type::modifiers_type.
16226 ///
16227 /// @param l the left-hand side operand.
16228 ///
16229 /// @param r the right-hand side operand.
16230 ///
16231 /// @return the result of the bitwise &=.
16234 {
16235  l = l & r;
16236  return l;
16237 }
16238 
16239 /// Parse a word containing one integral type modifier.
16240 ///
16241 /// A word is considered to be a string of characters that doesn't
16242 /// contain any white space.
16243 ///
16244 /// @param word the word to parse. It is considered to be a string of
16245 /// characters that doesn't contain any white space.
16246 ///
16247 /// @param modifiers out parameter. It's set by this function to the
16248 /// parsed modifier iff the function returned true.
16249 ///
16250 /// @return true iff @word was successfully parsed.
16251 static bool
16252 parse_integral_type_modifier(const string& word,
16253  integral_type::modifiers_type &modifiers)
16254 {
16255  if (word == "signed")
16256  modifiers |= integral_type::SIGNED_MODIFIER;
16257  else if (word == "unsigned")
16258  modifiers |= integral_type::UNSIGNED_MODIFIER;
16259  else if (word == "short")
16260  modifiers |= integral_type::SHORT_MODIFIER;
16261  else if (word == "long")
16262  modifiers |= integral_type::LONG_MODIFIER;
16263  else if (word == "long long")
16264  modifiers |= integral_type::LONG_LONG_MODIFIER;
16265  else
16266  return false;
16267 
16268  return true;
16269 }
16270 
16271 /// Parse a base type of an integral type from a string.
16272 ///
16273 /// @param type_name the type name to parse.
16274 ///
16275 /// @param base out parameter. This is set to the resulting base type
16276 /// parsed, iff the function returned true.
16277 ///
16278 /// @return true iff the function could successfully parse the base
16279 /// type.
16280 static bool
16281 parse_base_integral_type(const string& type_name,
16283 {
16284  if (type_name == "int")
16286  else if (type_name == "char")
16288  else if (type_name == "bool" || type_name == "_Bool")
16290  else if (type_name == "double")
16292  else if (type_name =="float")
16294  else if (type_name == "char16_t")
16296  else if (type_name == "char32_t")
16298  else if (type_name == "wchar_t")
16300  else
16301  return false;
16302 
16303  return true;
16304 }
16305 
16306 /// Parse an integral type from a string.
16307 ///
16308 /// @param type_name the string containing the integral type to parse.
16309 ///
16310 /// @param base out parameter. Is set by this function to the base
16311 /// type of the integral type, iff the function returned true.
16312 ///
16313 /// @param modifiers out parameter If set by this function to the
16314 /// modifier of the integral type, iff the function returned true.
16315 ///
16316 /// @return true iff the function could parse an integral type from @p
16317 /// type_name.
16318 static bool
16319 parse_integral_type(const string& type_name,
16321  integral_type::modifiers_type& modifiers)
16322 {
16323  string input = type_name;
16324  string::size_type len = input.length();
16325  string::size_type cur_pos = 0, prev_pos = 0;
16326  string cur_word, prev_word;
16327  bool ok = false;
16328 
16329  while (cur_pos < len)
16330  {
16331  if (cur_pos < len && isspace(input[cur_pos]))
16332  do
16333  ++cur_pos;
16334  while (cur_pos < len && isspace(input[cur_pos]));
16335 
16336  prev_pos = cur_pos;
16337  cur_pos = input.find(' ', prev_pos);
16338  prev_word = cur_word;
16339  cur_word = input.substr(prev_pos, cur_pos - prev_pos);
16340 
16341  if (cur_pos < len
16342  && cur_word == "long"
16343  && prev_word != "long")
16344  {
16345  if (cur_pos < len && isspace(input[cur_pos]))
16346  do
16347  ++cur_pos;
16348  while (cur_pos < len && isspace(input[cur_pos]));
16349  prev_pos = cur_pos;
16350 
16351  cur_pos = input.find(' ', prev_pos);
16352  string saved_prev_word = prev_word;
16353  prev_word = cur_word;
16354  cur_word = input.substr(prev_pos, cur_pos - prev_pos);
16355  if (cur_word == "long")
16356  cur_word = "long long";
16357  else
16358  {
16359  cur_pos = prev_pos;
16360  cur_word = prev_word;
16361  prev_word = saved_prev_word;
16362  }
16363  }
16364 
16365  if (!parse_integral_type_modifier(cur_word, modifiers))
16366  {
16367  if (!parse_base_integral_type(cur_word, base))
16368  return false;
16369  else
16370  ok = true;
16371  }
16372  else
16373  ok = true;
16374  }
16375 
16376  return ok;
16377 }
16378 
16379 /// Parse an integral type from a string.
16380 ///
16381 /// @param str the string containing the integral type to parse.
16382 ///
16383 ///@param type the resulting @ref integral_type. Is set to the result
16384 ///of the parse, iff the function returns true.
16385 ///
16386 /// @return true iff the function could parse an integral type from @p
16387 /// str.
16388 bool
16389 parse_integral_type(const string& str, integral_type& type)
16390 {
16392  integral_type::modifiers_type modifiers = integral_type::NO_MODIFIER;
16393 
16394  if (!parse_integral_type(str, base_type, modifiers))
16395  return false;
16396 
16397  // So this is an integral type.
16398  integral_type int_type(base_type, modifiers);
16399  type = int_type;
16400  return true;
16401 }
16402 
16403 /// Default constructor of the @ref integral_type.
16405  : base_(INT_BASE_TYPE),
16406  modifiers_(NO_MODIFIER)
16407 {}
16408 
16409 /// Constructor of the @ref integral_type.
16410 ///
16411 /// @param b the base type of the integral type.
16412 ///
16413 /// @param m the modifiers of the integral type.
16415  : base_(b), modifiers_(m)
16416 {}
16417 
16418 /// Constructor of the @ref integral_type.
16419 ///
16420 /// @param the name of the integral type to parse to initialize the
16421 /// current instance of @ref integral_type.
16422 integral_type::integral_type(const string& type_name)
16423  : base_(INT_BASE_TYPE),
16424  modifiers_(NO_MODIFIER)
16425 {
16426  bool could_parse = parse_integral_type(type_name, base_, modifiers_);
16427  ABG_ASSERT(could_parse);
16428 }
16429 
16430 /// Getter of the base type of the @ref integral_type.
16431 ///
16432 /// @return the base type of the @ref integral_type.
16435 {return base_;}
16436 
16437 /// Getter of the modifiers bitmap of the @ref integral_type.
16438 ///
16439 /// @return the modifiers bitmap of the @ref integral_type.
16442 {return modifiers_;}
16443 
16444 /// Setter of the modifiers bitmap of the @ref integral_type.
16445 ///
16446 /// @param m the new modifiers.
16447 void
16449 {modifiers_ = m;}
16450 
16451 /// Equality operator for the @ref integral_type.
16452 ///
16453 /// @param other the other integral type to compare against.
16454 ///
16455 /// @return true iff @p other equals the current instance of @ref
16456 /// integral_type.
16457 bool
16459 {return base_ == other.base_ && modifiers_ == other.modifiers_;}
16460 
16461 /// Return the string representation of the current instance of @ref
16462 /// integral_type.
16463 ///
16464 /// @param internal if true the string representation is to be used
16465 /// for internal purposes. In general, it means it's for type
16466 /// canonicalization purposes.
16467 ///
16468 /// @return the string representation of the current instance of @ref
16469 /// integral_type.
16470 string
16471 integral_type::to_string(bool internal) const
16472 {
16473  string result;
16474 
16475  // Look at modifiers ...
16476  if (modifiers_ & SIGNED_MODIFIER)
16477  result += "signed ";
16478  if (modifiers_ & UNSIGNED_MODIFIER)
16479  result += "unsigned ";
16480  if (!internal)
16481  {
16482  // For canonicalization purposes, we won't emit the "short, long, or
16483  // long long" modifiers. This is because on some platforms, "long
16484  // int" and "long long int" might have the same size. In those
16485  // cases, we want the two types to be equivalent if they have the
16486  // same size. If they don't have the same internal string
16487  // representation, they'd automatically have different canonical
16488  // types and thus be canonically different.
16489  if (modifiers_ & SHORT_MODIFIER)
16490  result += "short ";
16491  if (modifiers_ & LONG_MODIFIER)
16492  result += "long ";
16493  if (modifiers_ & LONG_LONG_MODIFIER)
16494  result += "long long ";
16495  }
16496 
16497  // ... and look at base types.
16498  if (base_ == INT_BASE_TYPE)
16499  result += "int";
16500  else if (base_ == CHAR_BASE_TYPE)
16501  result += "char";
16502  else if (base_ == BOOL_BASE_TYPE)
16503  result += "bool";
16504  else if (base_ == DOUBLE_BASE_TYPE)
16505  result += "double";
16506  else if (base_ == FLOAT_BASE_TYPE)
16507  result += "float";
16508  else if (base_ == CHAR16_T_BASE_TYPE)
16509  result += "char16_t";
16510  else if (base_ == CHAR32_T_BASE_TYPE)
16511  result += "char32_t";
16512  else if (base_ == WCHAR_T_BASE_TYPE)
16513  result += "wchar_t";
16514 
16515  return result;
16516 }
16517 
16518 /// Convert the current instance of @ref integral_type into its string
16519 /// representation.
16520 ///
16521 /// @return the string representation of the current instance of @ref
16522 /// integral_type.
16523 integral_type::operator string() const
16524 {return to_string();}
16525 
16526 // </integral_type definitions>
16527 
16528 //<type_decl definitions>
16529 
16530 /// Constructor.
16531 ///
16532 /// @param env the environment we are operating from.
16533 ///
16534 /// @param name the name of the type declaration.
16535 ///
16536 /// @param size_in_bits the size of the current type_decl, in bits.
16537 ///
16538 /// @param alignment_in_bits the alignment of the current typ, in
16539 /// bits.
16540 ///
16541 /// @param locus the source location of the current type declaration.
16542 ///
16543 /// @param linkage_name the linkage_name of the current type declaration.
16544 ///
16545 /// @param vis the visibility of the type declaration.
16546 type_decl::type_decl(const environment& env,
16547  const string& name,
16548  size_t size_in_bits,
16549  size_t alignment_in_bits,
16550  const location& locus,
16551  const string& linkage_name,
16552  visibility vis)
16553 
16554  : type_or_decl_base(env,
16555  BASIC_TYPE
16556  | ABSTRACT_TYPE_BASE
16557  | ABSTRACT_DECL_BASE),
16558  decl_base(env, name, locus, linkage_name, vis),
16559  type_base(env, size_in_bits, alignment_in_bits)
16560 {
16561  runtime_type_instance(this);
16562 
16564  integral_type::modifiers_type modifiers = integral_type::NO_MODIFIER;
16565  integral_type int_type(base_type, modifiers);
16566  if (parse_integral_type(name, int_type))
16567  {
16568  // Convert the integral_type into its canonical string
16569  // representation.
16570  string integral_type_name = int_type;
16571 
16572  // Set the name of this type_decl to the canonical string
16573  // representation above
16574  set_name(integral_type_name);
16576 
16577  if (!get_linkage_name().empty())
16578  set_linkage_name(integral_type_name);
16579  }
16580 }
16581 
16582 /// Compares two instances of @ref type_decl.
16583 ///
16584 /// If the two intances are different, set a bitfield to give some
16585 /// insight about the kind of differences there are.
16586 ///
16587 /// @param l the first artifact of the comparison.
16588 ///
16589 /// @param r the second artifact of the comparison.
16590 ///
16591 /// @param k a pointer to a bitfield that gives information about the
16592 /// kind of changes there are between @p l and @p r. This one is set
16593 /// iff @p k is non-null and the function returns false.
16594 ///
16595 /// Please note that setting k to a non-null value does have a
16596 /// negative performance impact because even if @p l and @p r are not
16597 /// equal, the function keeps up the comparison in order to determine
16598 /// the different kinds of ways in which they are different.
16599 ///
16600 /// @return true if @p l equals @p r, false otherwise.
16601 bool
16602 equals(const type_decl& l, const type_decl& r, change_kind* k)
16603 {
16604  bool result = false;
16605 
16606  // Consider the types as decls to compare their decls-related
16607  // properties.
16608  result = equals(static_cast<const decl_base&>(l),
16609  static_cast<const decl_base&>(r),
16610  k);
16611  if (!k && !result)
16613 
16614  // Now consider the types a "types' to compare their size-related
16615  // properties.
16616  result &= equals(static_cast<const type_base&>(l),
16617  static_cast<const type_base&>(r),
16618  k);
16619  ABG_RETURN(result);
16620 }
16621 
16622 /// Return true if both types equals.
16623 ///
16624 /// This operator re-uses the overload that takes a decl_base.
16625 ///
16626 /// Note that this does not check the scopes of any of the types.
16627 ///
16628 /// @param o the other type_decl to check agains.
16629 bool
16631 {
16632  const decl_base* other = dynamic_cast<const decl_base*>(&o);
16633  if (!other)
16634  return false;
16635  return *this == *other;
16636 }
16637 
16638 /// Return true if both types equals.
16639 ///
16640 /// Note that this does not check the scopes of any of the types.
16641 ///
16642 /// @param o the other type_decl to check against.
16643 bool
16645 {
16646  const type_decl* other = dynamic_cast<const type_decl*>(&o);
16647  if (!other)
16648  return false;
16649  return try_canonical_compare(this, other);
16650 }
16651 
16652 /// Return true if both types equals.
16653 ///
16654 /// Note that this does not check the scopes of any of the types.
16655 ///
16656 /// @param o the other type_decl to check against.
16657 ///
16658 /// @return true iff the current isntance equals @p o
16659 bool
16661 {
16662  const decl_base& other = o;
16663  return *this == other;
16664 }
16665 
16666 /// Return true if both types equals.
16667 ///
16668 /// Note that this does not check the scopes of any of the types.
16669 ///
16670 /// @param o the other type_decl to check against.
16671 ///
16672 /// @return true iff the current isntance equals @p o
16673 bool
16675 {return !operator==(o);}
16676 
16677 /// Return true if both types equals.
16678 ///
16679 /// Note that this does not check the scopes of any of the types.
16680 ///
16681 /// @param o the other type_decl to check against.
16682 ///
16683 /// @return true iff the current isntance equals @p o
16684 bool
16686 {return !operator==(o);}
16687 
16688 /// Inequality operator.
16689 ///
16690 /// @param o the other type to compare against.
16691 ///
16692 /// @return true iff the current instance is different from @p o.
16693 bool
16695 {return !operator==(o);}
16696 
16697 /// Equality operator for @ref type_decl_sptr.
16698 ///
16699 /// @param l the first operand to compare.
16700 ///
16701 /// @param r the second operand to compare.
16702 ///
16703 /// @return true iff @p l equals @p r.
16704 bool
16706 {
16707  if (!!l != !!r)
16708  return false;
16709  if (l.get() == r.get())
16710  return true;
16711  return *l == *r;
16712 }
16713 
16714 /// Inequality operator for @ref type_decl_sptr.
16715 ///
16716 /// @param l the first operand to compare.
16717 ///
16718 /// @param r the second operand to compare.
16719 ///
16720 /// @return true iff @p l is different from @p r.
16721 bool
16723 {return !operator==(l, r);}
16724 
16725 /// Implementation for the virtual qualified name builder for @ref
16726 /// type_decl.
16727 ///
16728 /// @param qualified_name the output parameter to hold the resulting
16729 /// qualified name.
16730 ///
16731 /// @param internal set to true if the call is intended for an
16732 /// internal use (for technical use inside the library itself), false
16733 /// otherwise. If you don't know what this is for, then set it to
16734 /// false.
16735 void
16737  bool internal) const
16738 {qualified_name = get_qualified_name(internal);}
16739 
16740 /// Implementation for the virtual qualified name builder for @ref
16741 /// type_decl.
16742 ///
16743 /// @param qualified_name the output parameter to hold the resulting
16744 /// qualified name.
16745 ///
16746 /// @param internal set to true if the call is intended for an
16747 /// internal use (for technical use inside the library itself), false
16748 /// otherwise. If you don't know what this is for, then set it to
16749 /// false.
16750 const interned_string&
16751 type_decl::get_qualified_name(bool internal) const
16752 {
16753  const environment& env = get_environment();
16754 
16755 
16756  if (internal)
16757  if (is_integral_type(this))
16758  {
16760  {
16761  if (decl_base::priv_->internal_qualified_name_.empty())
16762  decl_base::priv_->internal_qualified_name_ =
16763  env.intern(get_internal_integral_type_name(this));
16764  return decl_base::priv_->internal_qualified_name_;
16765  }
16766  else
16767  {
16768  decl_base::priv_->temporary_internal_qualified_name_ =
16769  env.intern(get_internal_integral_type_name(this));
16770  return decl_base::priv_->temporary_internal_qualified_name_;
16771  }
16772  }
16773 
16774  return decl_base::get_qualified_name(/*internal=*/false);
16775 }
16776 
16777 /// Get the pretty representation of the current instance of @ref
16778 /// type_decl.
16779 ///
16780 /// @param internal set to true if the call is intended to get a
16781 /// representation of the decl (or type) for the purpose of canonical
16782 /// type comparison. This is mainly used in the function
16783 /// type_base::get_canonical_type_for().
16784 ///
16785 /// In other words if the argument for this parameter is true then the
16786 /// call is meant for internal use (for technical use inside the
16787 /// library itself), false otherwise. If you don't know what this is
16788 /// for, then set it to false.
16789 ///
16790 /// @param qualified_name if true, names emitted in the pretty
16791 /// representation are fully qualified.
16792 ///
16793 /// @return the pretty representatin of the @ref type_decl.
16794 string
16796  bool qualified_name) const
16797 {
16798  if (internal)
16799  if (is_integral_type(this))
16800  return get_internal_integral_type_name(this);
16801 
16802  if (qualified_name)
16803  return get_qualified_name(internal);
16804  return get_name();
16805 }
16806 
16807 /// This implements the ir_traversable_base::traverse pure virtual
16808 /// function.
16809 ///
16810 /// @param v the visitor used on the current instance.
16811 ///
16812 /// @return true if the entire IR node tree got traversed, false
16813 /// otherwise.
16814 bool
16816 {
16817  if (v.type_node_has_been_visited(this))
16818  return true;
16819 
16820  v.visit_begin(this);
16821  bool result = v.visit_end(this);
16822  v.mark_type_node_as_visited(this);
16823 
16824  return result;
16825 }
16826 
16827 type_decl::~type_decl()
16828 {}
16829 //</type_decl definitions>
16830 
16831 // <scope_type_decl definitions>
16832 
16833 /// Constructor.
16834 ///
16835 /// @param env the environment we are operating from.
16836 ///
16837 /// @param name the name of the type.
16838 ///
16839 /// @param size_in_bits the size of the type, in bits.
16840 ///
16841 /// @param alignment_in_bits the alignment of the type, in bits.
16842 ///
16843 /// @param locus the source location where the type is defined.
16844 ///
16845 /// @param vis the visibility of the type.
16846 scope_type_decl::scope_type_decl(const environment& env,
16847  const string& name,
16848  size_t size_in_bits,
16849  size_t alignment_in_bits,
16850  const location& locus,
16851  visibility vis)
16852  : type_or_decl_base(env,
16853  ABSTRACT_SCOPE_TYPE_DECL
16854  | ABSTRACT_TYPE_BASE
16855  | ABSTRACT_DECL_BASE),
16856  decl_base(env, name, locus, "", vis),
16857  type_base(env, size_in_bits, alignment_in_bits),
16858  scope_decl(env, name, locus)
16859 {}
16860 
16861 /// Compares two instances of @ref scope_type_decl.
16862 ///
16863 /// If the two intances are different, set a bitfield to give some
16864 /// insight about the kind of differences there are.
16865 ///
16866 /// @param l the first artifact of the comparison.
16867 ///
16868 /// @param r the second artifact of the comparison.
16869 ///
16870 /// @param k a pointer to a bitfield that gives information about the
16871 /// kind of changes there are between @p l and @p r. This one is set
16872 /// iff @p k is non-null and the function returns false.
16873 ///
16874 /// Please note that setting k to a non-null value does have a
16875 /// negative performance impact because even if @p l and @p r are not
16876 /// equal, the function keeps up the comparison in order to determine
16877 /// the different kinds of ways in which they are different.
16878 ///
16879 /// @return true if @p l equals @p r, false otherwise.
16880 bool
16882 {
16883  bool result = equals(static_cast<const scope_decl&>(l),
16884  static_cast<const scope_decl&>(r),
16885  k);
16886 
16887  if (!k && !result)
16889 
16890  result &= equals(static_cast<const type_base&>(l),
16891  static_cast<const type_base&>(r),
16892  k);
16893 
16894  ABG_RETURN(result);
16895 }
16896 
16897 /// Equality operator between two scope_type_decl.
16898 ///
16899 /// Note that this function does not consider the scope of the scope
16900 /// types themselves.
16901 ///
16902 /// @return true iff both scope types are equal.
16903 bool
16905 {
16906  const scope_type_decl* other = dynamic_cast<const scope_type_decl*>(&o);
16907  if (!other)
16908  return false;
16909  return try_canonical_compare(this, other);
16910 }
16911 
16912 /// Equality operator between two scope_type_decl.
16913 ///
16914 /// This re-uses the equality operator that takes a decl_base.
16915 ///
16916 /// @param o the other scope_type_decl to compare against.
16917 ///
16918 /// @return true iff both scope types are equal.
16919 bool
16921 {
16922  const decl_base* other = dynamic_cast<const decl_base*>(&o);
16923  if (!other)
16924  return false;
16925 
16926  return *this == *other;
16927 }
16928 
16929 /// Traverses an instance of @ref scope_type_decl, visiting all the
16930 /// sub-types and decls that it might contain.
16931 ///
16932 /// @param v the visitor that is used to visit every IR sub-node of
16933 /// the current node.
16934 ///
16935 /// @return true if either
16936 /// - all the children nodes of the current IR node were traversed
16937 /// and the calling code should keep going with the traversing.
16938 /// - or the current IR node is already being traversed.
16939 /// Otherwise, returning false means that the calling code should not
16940 /// keep traversing the tree.
16941 bool
16943 {
16944  if (visiting())
16945  return true;
16946 
16947  if (v.type_node_has_been_visited(this))
16948  return true;
16949 
16950  if (v.visit_begin(this))
16951  {
16952  visiting(true);
16953  for (scope_decl::declarations::const_iterator i =
16954  get_member_decls().begin();
16955  i != get_member_decls ().end();
16956  ++i)
16957  if (!(*i)->traverse(v))
16958  break;
16959  visiting(false);
16960  }
16961 
16962  bool result = v.visit_end(this);
16963  v.mark_type_node_as_visited(this);
16964 
16965  return result;
16966 }
16967 
16968 scope_type_decl::~scope_type_decl()
16969 {}
16970 // </scope_type_decl definitions>
16971 
16972 // <namespace_decl>
16973 
16974 /// Constructor.
16975 ///
16976 /// @param the environment we are operatin from.
16977 ///
16978 /// @param name the name of the namespace.
16979 ///
16980 /// @param locus the source location where the namespace is defined.
16981 ///
16982 /// @param vis the visibility of the namespace.
16984  const string& name,
16985  const location& locus,
16986  visibility vis)
16987  // We need to call the constructor of decl_base directly here
16988  // because it is virtually inherited by scope_decl. Note that we
16989  // just implicitely call the default constructor for scope_decl
16990  // here, as what we really want is to initialize the decl_base
16991  // subobject. Wow, virtual inheritance is useful, but setting it
16992  // up is ugly.
16993  : type_or_decl_base(env,
16994  NAMESPACE_DECL
16995  | ABSTRACT_DECL_BASE
16996  | ABSTRACT_SCOPE_DECL),
16997  decl_base(env, name, locus, "", vis),
16998  scope_decl(env, name, locus)
16999 {
17000  runtime_type_instance(this);
17001 }
17002 
17003 /// Build and return a copy of the pretty representation of the
17004 /// namespace.
17005 ///
17006 /// @param internal set to true if the call is intended to get a
17007 /// representation of the decl (or type) for the purpose of canonical
17008 /// type comparison. This is mainly used in the function
17009 /// type_base::get_canonical_type_for().
17010 ///
17011 /// In other words if the argument for this parameter is true then the
17012 /// call is meant for internal use (for technical use inside the
17013 /// library itself), false otherwise. If you don't know what this is
17014 /// for, then set it to false.
17015 ///
17016 /// @param qualified_name if true, names emitted in the pretty
17017 /// representation are fully qualified.
17018 ///
17019 /// @return a copy of the pretty representation of the namespace.
17020 string
17022  bool qualified_name) const
17023 {
17024  string r =
17025  "namespace " + scope_decl::get_pretty_representation(internal,
17026  qualified_name);
17027  return r;
17028 }
17029 
17030 /// Return true iff both namespaces and their members are equal.
17031 ///
17032 /// Note that this function does not check if the scope of these
17033 /// namespaces are equal.
17034 bool
17036 {
17037  const namespace_decl* other = dynamic_cast<const namespace_decl*>(&o);
17038  if (!other)
17039  return false;
17040  return scope_decl::operator==(*other);
17041 }
17042 
17043 /// Test if the current namespace_decl is empty or contains empty
17044 /// namespaces itself.
17045 ///
17046 /// @return true iff the current namespace_decl is empty or contains
17047 /// empty itself.
17048 bool
17050 {
17051  if (is_empty())
17052  return true;
17053 
17054  for (declarations::const_iterator i = get_member_decls().begin();
17055  i != get_member_decls().end();
17056  ++i)
17057  {
17058  if (!is_namespace(*i))
17059  return false;
17060 
17062  ABG_ASSERT(ns);
17063 
17064  if (!ns->is_empty_or_has_empty_sub_namespaces())
17065  return false;
17066  }
17067 
17068  return true;
17069 }
17070 
17071 /// This implements the ir_traversable_base::traverse pure virtual
17072 /// function.
17073 ///
17074 /// @param v the visitor used on the current instance and on its
17075 /// member nodes.
17076 ///
17077 /// @return true if the entire IR node tree got traversed, false
17078 /// otherwise.
17079 bool
17081 {
17082  if (visiting())
17083  return true;
17084 
17085  if (v.visit_begin(this))
17086  {
17087  visiting(true);
17088  scope_decl::declarations::const_iterator i;
17089  for (i = get_member_decls().begin();
17090  i != get_member_decls ().end();
17091  ++i)
17092  {
17094  dynamic_pointer_cast<ir_traversable_base>(*i);
17095  if (t)
17096  if (!t->traverse (v))
17097  break;
17098  }
17099  visiting(false);
17100  }
17101  return v.visit_end(this);
17102 }
17103 
17104 namespace_decl::~namespace_decl()
17105 {
17106 }
17107 
17108 // </namespace_decl>
17109 
17110 // <qualified_type_def>
17111 
17112 /// Type of the private data of qualified_type_def.
17113 class qualified_type_def::priv
17114 {
17115  friend class qualified_type_def;
17116 
17117  qualified_type_def::CV cv_quals_;
17118  // Before the type is canonicalized, this is used as a temporary
17119  // internal name.
17120  interned_string temporary_internal_name_;
17121  // Once the type is canonicalized, this is used as the internal
17122  // name.
17123  interned_string internal_name_;
17124  weak_ptr<type_base> underlying_type_;
17125 
17126  priv()
17127  : cv_quals_(CV_NONE)
17128  {}
17129 
17130  priv(qualified_type_def::CV quals,
17131  type_base_sptr t)
17132  : cv_quals_(quals),
17133  underlying_type_(t)
17134  {}
17135 
17136  priv(qualified_type_def::CV quals)
17137  : cv_quals_(quals)
17138  {}
17139 };// end class qualified_type_def::priv
17140 
17141 /// Build the name of the current instance of qualified type.
17142 ///
17143 /// @param fully_qualified if true, build a fully qualified name.
17144 ///
17145 /// @param internal set to true if the call is intended for an
17146 /// internal use (for technical use inside the library itself), false
17147 /// otherwise. If you don't know what this is for, then set it to
17148 /// false.
17149 ///
17150 /// @return a copy of the newly-built name.
17151 string
17152 qualified_type_def::build_name(bool fully_qualified, bool internal) const
17153 {
17154  type_base_sptr t = get_underlying_type();
17155  if (!t)
17156  // The qualified type might temporarily have no underlying type,
17157  // especially during the construction of the type, while the
17158  // underlying type is not yet constructed. In that case, let's do
17159  // like if the underlying type is the 'void' type.
17161 
17163  fully_qualified,
17164  internal);
17165 }
17166 
17167 /// This function is automatically invoked whenever an instance of
17168 /// this type is canonicalized.
17169 ///
17170 /// It's an overload of the virtual type_base::on_canonical_type_set.
17171 ///
17172 /// We put here what is thus meant to be executed only at the point of
17173 /// type canonicalization.
17174 void
17177 
17178 /// Constructor of the qualified_type_def
17179 ///
17180 /// @param type the underlying type
17181 ///
17182 /// @param quals a bitfield representing the const/volatile qualifiers
17183 ///
17184 /// @param locus the location of the qualified type definition
17185 qualified_type_def::qualified_type_def(type_base_sptr type,
17186  CV quals,
17187  const location& locus)
17188  : type_or_decl_base(type->get_environment(),
17189  QUALIFIED_TYPE
17190  | ABSTRACT_TYPE_BASE
17191  | ABSTRACT_DECL_BASE),
17192  type_base(type->get_environment(), type->get_size_in_bits(),
17193  type->get_alignment_in_bits()),
17194  decl_base(type->get_environment(), "", locus, "",
17195  dynamic_pointer_cast<decl_base>(type)->get_visibility()),
17196  priv_(new priv(quals, type))
17197 {
17198  runtime_type_instance(this);
17199  interned_string name = type->get_environment().intern(build_name(false));
17200  set_name(name);
17201 }
17202 
17203 /// Constructor of the qualified_type_def
17204 ///
17205 /// @param env the environment of the type.
17206 ///
17207 /// @param quals a bitfield representing the const/volatile qualifiers
17208 ///
17209 /// @param locus the location of the qualified type definition
17210 qualified_type_def::qualified_type_def(const environment& env,
17211  CV quals,
17212  const location& locus)
17213  : type_or_decl_base(env,
17214  QUALIFIED_TYPE
17215  | ABSTRACT_TYPE_BASE
17216  | ABSTRACT_DECL_BASE),
17217  type_base(env, /*size_in_bits=*/0,
17218  /*alignment_in_bits=*/0),
17219  decl_base(env, "", locus, ""),
17220  priv_(new priv(quals))
17221 {
17222  runtime_type_instance(this);
17223  // We don't yet have an underlying type. So for naming purpose,
17224  // let's temporarily pretend the underlying type is 'void'.
17225  interned_string name = env.intern("void");
17226  set_name(name);
17227 }
17228 
17229 /// Get the size of the qualified type def.
17230 ///
17231 /// This is an overload for type_base::get_size_in_bits().
17232 ///
17233 /// @return the size of the qualified type.
17234 size_t
17236 {
17237  size_t s = 0;
17238  if (type_base_sptr ut = get_underlying_type())
17239  {
17240  // We do have the underlying type properly set, so let's make
17241  // the size of the qualified type match the size of its
17242  // underlying type.
17243  s = ut->get_size_in_bits();
17244  if (s != type_base::get_size_in_bits())
17245  const_cast<qualified_type_def*>(this)->set_size_in_bits(s);
17246  }
17247  return type_base::get_size_in_bits();
17248 }
17249 
17250 /// Compares two instances of @ref qualified_type_def.
17251 ///
17252 /// If the two intances are different, set a bitfield to give some
17253 /// insight about the kind of differences there are.
17254 ///
17255 /// @param l the first artifact of the comparison.
17256 ///
17257 /// @param r the second artifact of the comparison.
17258 ///
17259 /// @param k a pointer to a bitfield that gives information about the
17260 /// kind of changes there are between @p l and @p r. This one is set
17261 /// iff @p k is non-null and the function returns false.
17262 ///
17263 /// Please note that setting k to a non-null value does have a
17264 /// negative performance impact because even if @p l and @p r are not
17265 /// equal, the function keeps up the comparison in order to determine
17266 /// the different kinds of ways in which they are different.
17267 ///
17268 /// @return true if @p l equals @p r, false otherwise.
17269 bool
17271 {
17272  bool result = true;
17273  if (l.get_cv_quals() != r.get_cv_quals())
17274  {
17275  result = false;
17276  if (k)
17278  else
17280  }
17281 
17283  {
17284  result = false;
17285  if (k)
17286  {
17288  r.get_underlying_type().get()))
17289  // Underlying type changes in which the structure of the
17290  // type changed are considered local changes to the
17291  // qualified type.
17292  *k |= LOCAL_TYPE_CHANGE_KIND;
17293  else
17294  *k |= SUBTYPE_CHANGE_KIND;
17295  }
17296  else
17297  // okay strictly speaking this is not necessary, but I am
17298  // putting it here to maintenance; that is, so that adding
17299  // subsequent clauses needed to compare two qualified types
17300  // later still works.
17302  }
17303 
17304  ABG_RETURN(result);
17305 }
17306 
17307 /// Equality operator for qualified types.
17308 ///
17309 /// Note that this function does not check for equality of the scopes.
17310 ///
17311 ///@param o the other qualified type to compare against.
17312 ///
17313 /// @return true iff both qualified types are equal.
17314 bool
17316 {
17317  const qualified_type_def* other =
17318  dynamic_cast<const qualified_type_def*>(&o);
17319  if (!other)
17320  return false;
17321  return try_canonical_compare(this, other);
17322 }
17323 
17324 /// Equality operator for qualified types.
17325 ///
17326 /// Note that this function does not check for equality of the scopes.
17327 /// Also, this re-uses the equality operator above that takes a
17328 /// decl_base.
17329 ///
17330 ///@param o the other qualified type to compare against.
17331 ///
17332 /// @return true iff both qualified types are equal.
17333 bool
17335 {
17336  const decl_base* other = dynamic_cast<const decl_base*>(&o);
17337  if (!other)
17338  return false;
17339  return *this == *other;
17340 }
17341 
17342 /// Equality operator for qualified types.
17343 ///
17344 /// Note that this function does not check for equality of the scopes.
17345 /// Also, this re-uses the equality operator above that takes a
17346 /// decl_base.
17347 ///
17348 ///@param o the other qualified type to compare against.
17349 ///
17350 /// @return true iff both qualified types are equal.
17351 bool
17353 {
17354  const decl_base* other = dynamic_cast<const decl_base*>(&o);
17355  if (!other)
17356  return false;
17357  return *this == *other;
17358 }
17359 
17360 /// Implementation for the virtual qualified name builder for @ref
17361 /// qualified_type_def.
17362 ///
17363 /// @param qualified_name the output parameter to hold the resulting
17364 /// qualified name.
17365 ///
17366 /// @param internal set to true if the call is intended for an
17367 /// internal use (for technical use inside the library itself), false
17368 /// otherwise. If you don't know what this is for, then set it to
17369 /// false.
17370 void
17372  bool internal) const
17373 {qualified_name = get_qualified_name(internal);}
17374 
17375 /// Implementation of the virtual qualified name builder/getter.
17376 ///
17377 /// @param internal set to true if the call is intended for an
17378 /// internal use (for technical use inside the library itself), false
17379 /// otherwise. If you don't know what this is for, then set it to
17380 /// false.
17381 ///
17382 /// @return the resulting qualified name.
17383 const interned_string&
17385 {
17386  const environment& env = get_environment();
17387 
17388 
17389  if (!get_canonical_type())
17390  {
17391  // The type hasn't been canonicalized yet. We want to return a
17392  // temporary name that is not cached because the structure of
17393  // this type (and so its name) can change until its
17394  // canonicalized.
17395  if (internal)
17396  {
17397  // We are asked to return a temporary *internal* name.
17398  // Lets compute it and return a reference to where it's
17399  // stored.
17400  if (priv_->temporary_internal_name_.empty())
17401  priv_->temporary_internal_name_ =
17402  env.intern(build_name(true, /*internal=*/true));
17403  return priv_->temporary_internal_name_;
17404  }
17405  else
17406  {
17407  // We are asked to return a temporary non-internal name.
17409  (env.intern(build_name(true, /*internal=*/false)));
17411  }
17412  }
17413  else
17414  {
17415  // The type has already been canonicalized. We want to return
17416  // the definitive name and cache it.
17417  if (internal)
17418  {
17419  if (priv_->internal_name_.empty())
17420  priv_->internal_name_ =
17421  env.intern(build_name(/*qualified=*/true,
17422  /*internal=*/true));
17423  return priv_->internal_name_;
17424  }
17425  else
17426  {
17427  if (peek_qualified_name().empty())
17429  (env.intern(build_name(/*qualified=*/true,
17430  /*internal=*/false)));
17431  return peek_qualified_name();
17432  }
17433  }
17434 }
17435 
17436 /// This implements the ir_traversable_base::traverse pure virtual
17437 /// function.
17438 ///
17439 /// @param v the visitor used on the current instance.
17440 ///
17441 /// @return true if the entire IR node tree got traversed, false
17442 /// otherwise.
17443 bool
17445 {
17446  if (v.type_node_has_been_visited(this))
17447  return true;
17448 
17449  if (visiting())
17450  return true;
17451 
17452  if (v.visit_begin(this))
17453  {
17454  visiting(true);
17455  if (type_base_sptr t = get_underlying_type())
17456  t->traverse(v);
17457  visiting(false);
17458  }
17459  bool result = v.visit_end(this);
17460  v.mark_type_node_as_visited(this);
17461  return result;
17462 }
17463 
17464 qualified_type_def::~qualified_type_def()
17465 {
17466 }
17467 
17468 /// Getter of the const/volatile qualifier bit field
17471 {return priv_->cv_quals_;}
17472 
17473 /// Setter of the const/value qualifiers bit field
17474 void
17476 {priv_->cv_quals_ = cv_quals;}
17477 
17478 /// Compute and return the string prefix or suffix representing the
17479 /// qualifiers hold by the current instance of @ref
17480 /// qualified_type_def.
17481 ///
17482 /// @return the newly-built cv string.
17483 string
17485 {return get_string_representation_of_cv_quals(priv_->cv_quals_);}
17486 
17487 /// Getter of the underlying type
17488 type_base_sptr
17490 {return priv_->underlying_type_.lock();}
17491 
17492 /// Setter of the underlying type.
17493 ///
17494 /// @param t the new underlying type.
17495 void
17497 {
17498  ABG_ASSERT(t);
17499  priv_->underlying_type_ = t;
17500  // Now we need to update other properties that depend on the new underlying type.
17501  set_size_in_bits(t->get_size_in_bits());
17502  set_alignment_in_bits(t->get_alignment_in_bits());
17504  set_name(name);
17505  if (scope_decl* s = get_scope())
17506  {
17507  // Now that the name has been updated, we need to update the
17508  // lookup maps accordingly.
17509  scope_decl::declarations::iterator i;
17510  if (s->find_iterator_for_member(this, i))
17512  else
17514  }
17515 }
17516 
17517 /// Non-member equality operator for @ref qualified_type_def
17518 ///
17519 /// @param l the left-hand side of the equality operator
17520 ///
17521 /// @param r the right-hand side of the equality operator
17522 ///
17523 /// @return true iff @p l and @p r equals.
17524 bool
17525 operator==(const qualified_type_def_sptr& l, const qualified_type_def_sptr& r)
17526 {
17527  if (l.get() == r.get())
17528  return true;
17529  if (!!l != !!r)
17530  return false;
17531 
17532  return *l == *r;
17533 }
17534 
17535 /// Non-member inequality operator for @ref qualified_type_def
17536 ///
17537 /// @param l the left-hand side of the equality operator
17538 ///
17539 /// @param r the right-hand side of the equality operator
17540 ///
17541 /// @return true iff @p l and @p r equals.
17542 bool
17543 operator!=(const qualified_type_def_sptr& l, const qualified_type_def_sptr& r)
17544 {return ! operator==(l, r);}
17545 
17546 /// Overloaded bitwise OR operator for cv qualifiers.
17549 {
17550  return static_cast<qualified_type_def::CV>
17551  (static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
17552 }
17553 
17554 /// Overloaded bitwise |= operator for cv qualifiers.
17557 {
17558  l = l | r;
17559  return l;
17560 }
17561 
17562 /// Overloaded bitwise &= operator for cv qualifiers.
17565 {
17566  l = l & r;
17567  return l;
17568 }
17569 
17570 /// Overloaded bitwise AND operator for CV qualifiers.
17573 {
17574  return static_cast<qualified_type_def::CV>
17575  (static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
17576 }
17577 
17578 /// Overloaded bitwise inverting operator for CV qualifiers.
17581 {return static_cast<qualified_type_def::CV>(~static_cast<unsigned>(q));}
17582 
17583 /// Streaming operator for qualified_type_decl::CV
17584 ///
17585 /// @param o the output stream to serialize the cv qualifier to.
17586 ///
17587 /// @param cv the cv qualifier to serialize.
17588 ///
17589 /// @return the output stream used.
17590 std::ostream&
17591 operator<<(std::ostream& o, qualified_type_def::CV cv)
17592 {
17593  string str;
17594 
17595  switch (cv)
17596  {
17597  case qualified_type_def::CV_NONE:
17598  str = "none";
17599  break;
17600  case qualified_type_def::CV_CONST:
17601  str = "const";
17602  break;
17603  case qualified_type_def::CV_VOLATILE:
17604  str = "volatile";
17605  break;
17606  case qualified_type_def::CV_RESTRICT:
17607  str = "restrict";
17608  break;
17609  }
17610 
17611  o << str;
17612  return o;
17613 }
17614 
17615 // </qualified_type_def>
17616 
17617 //<pointer_type_def definitions>
17618 
17619 /// Private data structure of the @ref pointer_type_def.
17620 struct pointer_type_def::priv
17621 {
17622  type_base_wptr pointed_to_type_;
17623  type_base* naked_pointed_to_type_;
17624  interned_string internal_qualified_name_;
17625  interned_string temp_internal_qualified_name_;
17626 
17627  priv(const type_base_sptr& t)
17628  : pointed_to_type_(type_or_void(t, t->get_environment())),
17629  naked_pointed_to_type_(t.get())
17630  {}
17631 
17632  priv()
17633  : naked_pointed_to_type_()
17634  {}
17635 }; //end struct pointer_type_def
17636 
17637 /// This function is automatically invoked whenever an instance of
17638 /// this type is canonicalized.
17639 ///
17640 /// It's an overload of the virtual type_base::on_canonical_type_set.
17641 ///
17642 /// We put here what is thus meant to be executed only at the point of
17643 /// type canonicalization.
17644 void
17647 
17648 
17649 ///Constructor of @ref pointer_type_def.
17650 ///
17651 /// @param pointed_to the pointed-to type.
17652 ///
17653 /// @param size_in_bits the size of the type, in bits.
17654 ///
17655 /// @param align_in_bits the alignment of the type, in bits.
17656 ///
17657 /// @param locus the source location where the type was defined.
17658 pointer_type_def::pointer_type_def(const type_base_sptr& pointed_to,
17659  size_t size_in_bits,
17660  size_t align_in_bits,
17661  const location& locus)
17662  : type_or_decl_base(pointed_to->get_environment(),
17663  POINTER_TYPE
17664  | ABSTRACT_TYPE_BASE
17665  | ABSTRACT_DECL_BASE),
17666  type_base(pointed_to->get_environment(), size_in_bits, align_in_bits),
17667  decl_base(pointed_to->get_environment(), "", locus, ""),
17668  priv_(new priv(pointed_to))
17669 {
17670  runtime_type_instance(this);
17671  try
17672  {
17673  ABG_ASSERT(pointed_to);
17674  const environment& env = pointed_to->get_environment();
17675  decl_base_sptr pto = dynamic_pointer_cast<decl_base>(pointed_to);
17676  string name = (pto ? pto->get_name() : string("void")) + "*";
17677  set_name(env.intern(name));
17678  if (pto)
17679  set_visibility(pto->get_visibility());
17680  }
17681  catch (...)
17682  {}
17683 }
17684 
17685 ///Constructor of @ref pointer_type_def.
17686 ///
17687 /// @param env the environment of the type.
17688 ///
17689 /// @param size_in_bits the size of the type, in bits.
17690 ///
17691 /// @param align_in_bits the alignment of the type, in bits.
17692 ///
17693 /// @param locus the source location where the type was defined.
17694 pointer_type_def::pointer_type_def(const environment& env, size_t size_in_bits,
17695  size_t alignment_in_bits,
17696  const location& locus)
17697  : type_or_decl_base(env,
17698  POINTER_TYPE
17699  | ABSTRACT_TYPE_BASE
17700  | ABSTRACT_DECL_BASE),
17701  type_base(env, size_in_bits, alignment_in_bits),
17702  decl_base(env, "", locus, ""),
17703  priv_(new priv())
17704 {
17705  runtime_type_instance(this);
17706  string name = string("void") + "*";
17707  set_name(env.intern(name));
17708 }
17709 
17710 /// Set the pointed-to type of the pointer.
17711 ///
17712 /// @param t the new pointed-to type.
17713 void
17715 {
17716  ABG_ASSERT(t);
17717  priv_->pointed_to_type_ = t;
17718  priv_->naked_pointed_to_type_ = t.get();
17719 
17720  try
17721  {
17722  const environment& env = t->get_environment();
17723  decl_base_sptr pto = dynamic_pointer_cast<decl_base>(t);
17724  string name = (pto ? pto->get_name() : string("void")) + "*";
17725  set_name(env.intern(name));
17726  if (pto)
17727  set_visibility(pto->get_visibility());
17728  }
17729  catch (...)
17730  {}
17731 }
17732 
17733 /// Compares two instances of @ref pointer_type_def.
17734 ///
17735 /// If the two intances are different, set a bitfield to give some
17736 /// insight about the kind of differences there are.
17737 ///
17738 /// @param l the first artifact of the comparison.
17739 ///
17740 /// @param r the second artifact of the comparison.
17741 ///
17742 /// @param k a pointer to a bitfield that gives information about the
17743 /// kind of changes there are between @p l and @p r. This one is set
17744 /// iff @p k is non-null and the function returns false.
17745 ///
17746 /// Please note that setting k to a non-null value does have a
17747 /// negative performance impact because even if @p l and @p r are not
17748 /// equal, the function keeps up the comparison in order to determine
17749 /// the different kinds of ways in which they are different.
17750 ///
17751 /// @return true if @p l equals @p r, false otherwise.
17752 bool
17754 {
17755  // In C and C++ languages, a pointer to void equals all other
17756  // pointers.
17757  if (l.get_translation_unit()
17758  && r.get_translation_unit()
17763  return true;
17764 
17765  bool result = l.get_pointed_to_type() == r.get_pointed_to_type();
17766  if (!result)
17767  if (k)
17768  {
17769  if (!types_have_similar_structure(&l, &r))
17770  // pointed-to type changes in which the structure of the
17771  // type changed are considered local changes to the pointer
17772  // type.
17773  *k |= LOCAL_TYPE_CHANGE_KIND;
17774  *k |= SUBTYPE_CHANGE_KIND;
17775  }
17776 
17777  ABG_RETURN(result);
17778 }
17779 
17780 /// Return true iff both instances of pointer_type_def are equal.
17781 ///
17782 /// Note that this function does not check for the scopes of the this
17783 /// types.
17784 bool
17786 {
17787  const pointer_type_def* other = is_pointer_type(&o);
17788  if (!other)
17789  return false;
17790  return try_canonical_compare(this, other);
17791 }
17792 
17793 /// Return true iff both instances of pointer_type_def are equal.
17794 ///
17795 /// Note that this function does not check for the scopes of the
17796 /// types.
17797 ///
17798 /// @param other the other type to compare against.
17799 ///
17800 /// @return true iff @p other equals the current instance.
17801 bool
17803 {
17804  const decl_base* o = is_decl(&other);
17805  if (!o)
17806  return false;
17807  return *this == *o;
17808 }
17809 
17810 /// Return true iff both instances of pointer_type_def are equal.
17811 ///
17812 /// Note that this function does not check for the scopes of the
17813 /// types.
17814 ///
17815 /// @param other the other type to compare against.
17816 ///
17817 /// @return true iff @p other equals the current instance.
17818 bool
17820 {
17821  const decl_base& o = other;
17822  return *this == o;
17823 }
17824 
17825 /// Getter of the pointed-to type.
17826 ///
17827 /// @return the pointed-to type.
17828 const type_base_sptr
17830 {return priv_->pointed_to_type_.lock();}
17831 
17832 /// Getter of a naked pointer to the pointed-to type.
17833 ///
17834 /// @return a naked pointed to the pointed-to type.
17835 type_base*
17837 {return priv_->naked_pointed_to_type_;}
17838 
17839 /// Build and return the qualified name of the current instance of
17840 /// @ref pointer_type_def.
17841 ///
17842 /// @param qn output parameter. The resulting qualified name.
17843 ///
17844 /// @param internal set to true if the call is intended for an
17845 /// internal use (for technical use inside the library itself), false
17846 /// otherwise. If you don't know what this is for, then set it to
17847 /// false.
17848 void
17850 {qn = get_qualified_name(internal);}
17851 
17852 /// Build, cache and return the qualified name of the current instance
17853 /// of @ref pointer_type_def. Subsequent invocations of this function
17854 /// return the cached value.
17855 ///
17856 /// Note that this function should work even if the underlying type is
17857 /// momentarily empty.
17858 ///
17859 /// @param internal set to true if the call is intended for an
17860 /// internal use (for technical use inside the library itself), false
17861 /// otherwise. If you don't know what this is for, then set it to
17862 /// false.
17863 ///
17864 /// @return the resulting qualified name.
17865 const interned_string&
17867 {
17868  type_base* pointed_to_type = get_naked_pointed_to_type();
17869  pointed_to_type = look_through_decl_only(pointed_to_type);
17870 
17871  if (internal)
17872  {
17873  if (get_canonical_type())
17874  {
17875  if (priv_->internal_qualified_name_.empty())
17876  if (pointed_to_type)
17877  priv_->internal_qualified_name_ =
17878  pointer_declaration_name(this,
17879  /*variable_name=*/"",
17880  /*qualified_name=*/
17881  is_typedef(pointed_to_type)
17882  ? false
17883  : true,
17884  /*internal=*/true);
17885  return priv_->internal_qualified_name_;
17886  }
17887  else
17888  {
17889  // As the type hasn't yet been canonicalized, its structure
17890  // (and so its name) can change. So let's invalidate the
17891  // cache where we store its name at each invocation of this
17892  // function.
17893  if (pointed_to_type)
17894  if (priv_->temp_internal_qualified_name_.empty())
17895  priv_->temp_internal_qualified_name_ =
17896  pointer_declaration_name(this,
17897  /*variable_name=*/"",
17898  /*qualified_name=*/
17899  is_typedef(pointed_to_type)
17900  ? false
17901  : true,
17902  /*internal=*/true);
17903  return priv_->temp_internal_qualified_name_;
17904  }
17905  }
17906  else
17907  {
17909  {
17910  if (decl_base::peek_qualified_name().empty())
17912  (pointer_declaration_name(this,
17913  /*variable_name=*/"",
17914  /*qualified_name=*/true,
17915  /*internal=*/false));
17917  }
17918  else
17919  {
17920  // As the type hasn't yet been canonicalized, its structure
17921  // (and so its name) can change. So let's invalidate the
17922  // cache where we store its name at each invocation of this
17923  // function.
17924  if (pointed_to_type)
17926  (pointer_declaration_name(this,
17927  /*variable_name=*/"",
17928  /*qualified_name=*/true,
17929  /*internal=*/false));
17931  }
17932  }
17933 }
17934 
17935 /// This implements the ir_traversable_base::traverse pure virtual
17936 /// function.
17937 ///
17938 /// @param v the visitor used on the current instance.
17939 ///
17940 /// @return true if the entire IR node tree got traversed, false
17941 /// otherwise.
17942 bool
17944 {
17945  if (v.type_node_has_been_visited(this))
17946  return true;
17947 
17948  if (visiting())
17949  return true;
17950 
17951  if (v.visit_begin(this))
17952  {
17953  visiting(true);
17954  if (type_base_sptr t = get_pointed_to_type())
17955  t->traverse(v);
17956  visiting(false);
17957  }
17958 
17959  bool result = v.visit_end(this);
17960  v.mark_type_node_as_visited(this);
17961  return result;
17962 }
17963 
17964 pointer_type_def::~pointer_type_def()
17965 {}
17966 
17967 /// Turn equality of shared_ptr of @ref pointer_type_def into a deep
17968 /// equality; that is, make it compare the pointed to objects too.
17969 ///
17970 /// @param l the shared_ptr of @ref pointer_type_def on left-hand-side
17971 /// of the equality.
17972 ///
17973 /// @param r the shared_ptr of @ref pointer_type_def on
17974 /// right-hand-side of the equality.
17975 ///
17976 /// @return true if the @ref pointer_type_def pointed to by the
17977 /// shared_ptrs are equal, false otherwise.
17978 bool
17980 {
17981  if (l.get() == r.get())
17982  return true;
17983  if (!!l != !!r)
17984  return false;
17985 
17986  return *l == *r;
17987 }
17988 
17989 /// Turn inequality of shared_ptr of @ref pointer_type_def into a deep
17990 /// equality; that is, make it compare the pointed to objects too.
17991 ///
17992 /// @param l the shared_ptr of @ref pointer_type_def on left-hand-side
17993 /// of the equality.
17994 ///
17995 /// @param r the shared_ptr of @ref pointer_type_def on
17996 /// right-hand-side of the equality.
17997 ///
17998 /// @return true iff the @ref pointer_type_def pointed to by the
17999 /// shared_ptrs are different.
18000 bool
18002 {return !operator==(l, r);}
18003 
18004 // </pointer_type_def definitions>
18005 
18006 // <reference_type_def definitions>
18007 
18008 /// Private data structure of the @ref reference_type_def type.
18009 struct reference_type_def::priv
18010 {
18011 
18012  type_base_wptr pointed_to_type_;
18013  bool is_lvalue_;
18014  interned_string internal_qualified_name_;
18015  interned_string temp_internal_qualified_name_;
18016 
18017  priv(const type_base_sptr& t, bool is_lvalue)
18018  : pointed_to_type_(type_or_void(t, t->get_environment())),
18019  is_lvalue_(is_lvalue)
18020  {}
18021 
18022  priv(bool is_lvalue)
18023  : is_lvalue_(is_lvalue)
18024  {}
18025 
18026  priv() = delete;
18027 };
18028 
18029 /// This function is automatically invoked whenever an instance of
18030 /// this type is canonicalized.
18031 ///
18032 /// It's an overload of the virtual type_base::on_canonical_type_set.
18033 ///
18034 /// We put here what is thus meant to be executed only at the point of
18035 /// type canonicalization.
18036 void
18039 
18040 /// Constructor of the reference_type_def type.
18041 ///
18042 /// @param pointed_to the pointed to type.
18043 ///
18044 /// @param lvalue wether the reference is an lvalue reference. If
18045 /// false, the reference is an rvalue one.
18046 ///
18047 /// @param size_in_bits the size of the type, in bits.
18048 ///
18049 /// @param align_in_bits the alignment of the type, in bits.
18050 ///
18051 /// @param locus the source location of the type.
18052 reference_type_def::reference_type_def(const type_base_sptr pointed_to,
18053  bool lvalue,
18054  size_t size_in_bits,
18055  size_t align_in_bits,
18056  const location& locus)
18057  : type_or_decl_base(pointed_to->get_environment(),
18058  REFERENCE_TYPE
18059  | ABSTRACT_TYPE_BASE
18060  | ABSTRACT_DECL_BASE),
18061  type_base(pointed_to->get_environment(), size_in_bits, align_in_bits),
18062  decl_base(pointed_to->get_environment(), "", locus, ""),
18063  priv_(new priv(pointed_to, lvalue))
18064 {
18065  runtime_type_instance(this);
18066 
18067  try
18068  {
18069  decl_base_sptr pto = dynamic_pointer_cast<decl_base>(pointed_to);
18070  string name;
18071  if (pto)
18072  {
18073  set_visibility(pto->get_visibility());
18074  name = string(pto->get_name()) + "&";
18075  }
18076  else
18077  name = string(get_type_name(is_function_type(pointed_to),
18078  /*qualified_name=*/true)) + "&";
18079 
18080  if (!is_lvalue())
18081  name += "&";
18082  const environment& env = pointed_to->get_environment();
18083  set_name(env.intern(name));
18084  }
18085  catch (...)
18086  {}
18087 }
18088 
18089 /// Constructor of the reference_type_def type.
18090 ///
18091 /// This one creates a type that has no pointed-to type, temporarily.
18092 /// This is useful for cases where the underlying type is not yet
18093 /// available. It can be set later using
18094 /// reference_type_def::set_pointed_to_type().
18095 ///
18096 /// @param env the environment of the type.
18097 ///
18098 /// @param lvalue wether the reference is an lvalue reference. If
18099 /// false, the reference is an rvalue one.
18100 ///
18101 /// @param size_in_bits the size of the type, in bits.
18102 ///
18103 /// @param align_in_bits the alignment of the type, in bits.
18104 ///
18105 /// @param locus the source location of the type.
18106 reference_type_def::reference_type_def(const environment& env, bool lvalue,
18107  size_t size_in_bits,
18108  size_t alignment_in_bits,
18109  const location& locus)
18110  : type_or_decl_base(env,
18111  REFERENCE_TYPE
18112  | ABSTRACT_TYPE_BASE
18113  | ABSTRACT_DECL_BASE),
18114  type_base(env, size_in_bits, alignment_in_bits),
18115  decl_base(env, "", locus, ""),
18116  priv_(new priv(lvalue))
18117 {
18118  runtime_type_instance(this);
18119  string name = "void&";
18120  if (!is_lvalue())
18121  name += "&";
18122 
18123  set_name(env.intern(name));
18124  priv_->pointed_to_type_ = type_base_wptr(env.get_void_type());
18125 }
18126 
18127 /// Setter of the pointed_to type of the current reference type.
18128 ///
18129 /// @param pointed_to the new pointed to type.
18130 void
18131 reference_type_def::set_pointed_to_type(type_base_sptr& pointed_to_type)
18132 {
18133  ABG_ASSERT(pointed_to_type);
18134  priv_->pointed_to_type_ = pointed_to_type;
18135 
18136  decl_base_sptr pto;
18137  try
18138  {pto = dynamic_pointer_cast<decl_base>(pointed_to_type);}
18139  catch (...)
18140  {}
18141 
18142  if (pto)
18143  {
18144  set_visibility(pto->get_visibility());
18145  string name = string(pto->get_name()) + "&";
18146  if (!is_lvalue())
18147  name += "&";
18148  const environment& env = pto->get_environment();
18149  set_name(env.intern(name));
18150  }
18151 }
18152 
18153 /// Compares two instances of @ref reference_type_def.
18154 ///
18155 /// If the two intances are different, set a bitfield to give some
18156 /// insight about the kind of differences there are.
18157 ///
18158 /// @param l the first artifact of the comparison.
18159 ///
18160 /// @param r the second artifact of the comparison.
18161 ///
18162 /// @param k a pointer to a bitfield that gives information about the
18163 /// kind of changes there are between @p l and @p r. This one is set
18164 /// iff @p k is non-null and the function returns false.
18165 ///
18166 /// Please note that setting k to a non-null value does have a
18167 /// negative performance impact because even if @p l and @p r are not
18168 /// equal, the function keeps up the comparison in order to determine
18169 /// the different kinds of ways in which they are different.
18170 ///
18171 /// @return true if @p l equals @p r, false otherwise.
18172 bool
18174 {
18175  if (l.is_lvalue() != r.is_lvalue())
18176  {
18177  if (k)
18178  *k |= LOCAL_TYPE_CHANGE_KIND;
18180  }
18181 
18182  // Compare the pointed-to-types modulo the typedefs they might have
18183  bool result = (l.get_pointed_to_type() == r.get_pointed_to_type());
18184  if (!result)
18185  if (k)
18186  {
18187  if (!types_have_similar_structure(&l, &r))
18188  *k |= LOCAL_TYPE_CHANGE_KIND;
18189  *k |= SUBTYPE_CHANGE_KIND;
18190  }
18191  ABG_RETURN(result);
18192 }
18193 
18194 /// Equality operator of the @ref reference_type_def type.
18195 ///
18196 /// @param o the other instance of @ref reference_type_def to compare
18197 /// against.
18198 ///
18199 /// @return true iff the two instances are equal.
18200 bool
18202 {
18203  const reference_type_def* other =
18204  dynamic_cast<const reference_type_def*>(&o);
18205  if (!other)
18206  return false;
18207  return try_canonical_compare(this, other);
18208 }
18209 
18210 /// Equality operator of the @ref reference_type_def type.
18211 ///
18212 /// @param o the other instance of @ref reference_type_def to compare
18213 /// against.
18214 ///
18215 /// @return true iff the two instances are equal.
18216 bool
18218 {
18219  const decl_base* other = dynamic_cast<const decl_base*>(&o);
18220  if (!other)
18221  return false;
18222  return *this == *other;
18223 }
18224 
18225 /// Equality operator of the @ref reference_type_def type.
18226 ///
18227 /// @param o the other instance of @ref reference_type_def to compare
18228 /// against.
18229 ///
18230 /// @return true iff the two instances are equal.
18231 bool
18233 {
18234  const decl_base* other = dynamic_cast<const decl_base*>(&o);
18235  if (!other)
18236  return false;
18237  return *this == *other;
18238 }
18239 
18240 type_base_sptr
18241 reference_type_def::get_pointed_to_type() const
18242 {return priv_->pointed_to_type_.lock();}
18243 
18244 bool
18245 reference_type_def::is_lvalue() const
18246 {return priv_->is_lvalue_;}
18247 
18248 /// Build and return the qualified name of the current instance of the
18249 /// @ref reference_type_def.
18250 ///
18251 /// @param qn output parameter. Is set to the newly-built qualified
18252 /// name of the current instance of @ref reference_type_def.
18253 ///
18254 /// @param internal set to true if the call is intended for an
18255 /// internal use (for technical use inside the library itself), false
18256 /// otherwise. If you don't know what this is for, then set it to
18257 /// false.
18258 void
18260 {qn = get_qualified_name(internal);}
18261 
18262 /// Build, cache and return the qualified name of the current instance
18263 /// of the @ref reference_type_def. Subsequent invocations of this
18264 /// function return the cached value.
18265 ///
18266 /// @param internal set to true if the call is intended for an
18267 /// internal use (for technical use inside the library itself), false
18268 /// otherwise. If you don't know what this is for, then set it to
18269 /// false.
18270 ///
18271 /// @return the newly-built qualified name of the current instance of
18272 /// @ref reference_type_def.
18273 const interned_string&
18275 {
18276  type_base_sptr pointed_to_type = get_pointed_to_type();
18277  pointed_to_type = look_through_decl_only(pointed_to_type);
18278 
18279  if (internal)
18280  {
18281  if (get_canonical_type())
18282  {
18283  if (priv_->internal_qualified_name_.empty())
18284  if (pointed_to_type)
18285  priv_->internal_qualified_name_ =
18286  get_name_of_reference_to_type(*pointed_to_type,
18287  is_lvalue(),
18288  /*qualified_name=*/
18289  is_typedef(pointed_to_type)
18290  ? false
18291  : true,
18292  /*internal=*/true);
18293  return priv_->internal_qualified_name_;
18294  }
18295  else
18296  {
18297  // As the type hasn't yet been canonicalized, its structure
18298  // (and so its name) can change. So let's invalidate the
18299  // cache where we store its name at each invocation of this
18300  // function.
18301  if (pointed_to_type)
18302  if (priv_->temp_internal_qualified_name_.empty())
18303  priv_->temp_internal_qualified_name_ =
18304  get_name_of_reference_to_type(*pointed_to_type,
18305  is_lvalue(),
18306  /*qualified_name=*/
18307  is_typedef(pointed_to_type)
18308  ? false
18309  : true,
18310  /*internal=*/true);
18311  return priv_->temp_internal_qualified_name_;
18312  }
18313  }
18314  else
18315  {
18317  {
18319  (get_name_of_reference_to_type(*pointed_to_type,
18320  is_lvalue(),
18321  /*qualified_name=*/true,
18322  /*internal=*/false));
18324  }
18325  else
18326  {
18327  // As the type hasn't yet been canonicalized, its structure
18328  // (and so its name) can change. So let's invalidate the
18329  // cache where we store its name at each invocation of this
18330  // function.
18331  if (pointed_to_type)
18333  (get_name_of_reference_to_type(*pointed_to_type,
18334  is_lvalue(),
18335  /*qualified_name=*/true,
18336  /*internal=*/false));
18338  }
18339  }
18340 }
18341 
18342 /// Get the pretty representation of the current instance of @ref
18343 /// reference_type_def.
18344 ///
18345 /// @param internal set to true if the call is intended to get a
18346 /// representation of the decl (or type) for the purpose of canonical
18347 /// type comparison. This is mainly used in the function
18348 /// type_base::get_canonical_type_for().
18349 ///
18350 /// In other words if the argument for this parameter is true then the
18351 /// call is meant for internal use (for technical use inside the
18352 /// library itself), false otherwise. If you don't know what this is
18353 /// for, then set it to false.
18354 ///
18355 /// @param qualified_name if true, names emitted in the pretty
18356 /// representation are fully qualified.
18357 ///
18358 /// @return the pretty representatin of the @ref reference_type_def.
18359 string
18361  bool qualified_name) const
18362 {
18363  string result =
18365  (get_pointed_to_type()),
18366  is_lvalue(),
18367  qualified_name,
18368  internal);
18369 
18370  return result;
18371 }
18372 
18373 /// This implements the ir_traversable_base::traverse pure virtual
18374 /// function.
18375 ///
18376 /// @param v the visitor used on the current instance.
18377 ///
18378 /// @return true if the entire IR node tree got traversed, false
18379 /// otherwise.
18380 bool
18382 {
18383  if (v.type_node_has_been_visited(this))
18384  return true;
18385 
18386  if (visiting())
18387  return true;
18388 
18389  if (v.visit_begin(this))
18390  {
18391  visiting(true);
18392  if (type_base_sptr t = get_pointed_to_type())
18393  t->traverse(v);
18394  visiting(false);
18395  }
18396 
18397  bool result = v.visit_end(this);
18398  v.mark_type_node_as_visited(this);
18399  return result;
18400 }
18401 
18402 reference_type_def::~reference_type_def()
18403 {}
18404 
18405 /// Turn equality of shared_ptr of @ref reference_type_def into a deep
18406 /// equality; that is, make it compare the pointed to objects too.
18407 ///
18408 /// @param l the shared_ptr of @ref reference_type_def on left-hand-side
18409 /// of the equality.
18410 ///
18411 /// @param r the shared_ptr of @ref reference_type_def on
18412 /// right-hand-side of the equality.
18413 ///
18414 /// @return true if the @ref reference_type_def pointed to by the
18415 /// shared_ptrs are equal, false otherwise.
18416 bool
18418 {
18419  if (l.get() == r.get())
18420  return true;
18421  if (!!l != !!r)
18422  return false;
18423 
18424  return *l == *r;
18425 }
18426 
18427 /// Turn inequality of shared_ptr of @ref reference_type_def into a deep
18428 /// equality; that is, make it compare the pointed to objects too.
18429 ///
18430 /// @param l the shared_ptr of @ref reference_type_def on left-hand-side
18431 /// of the equality.
18432 ///
18433 /// @param r the shared_ptr of @ref reference_type_def on
18434 /// right-hand-side of the equality.
18435 ///
18436 /// @return true iff the @ref reference_type_def pointed to by the
18437 /// shared_ptrs are different.
18438 bool
18440 {return !operator==(l, r);}
18441 
18442 // </reference_type_def definitions>
18443 
18444 // <ptr_to_mbr_type definitions>
18445 
18446 /// The private data type of @ref ptr_to_mbr_type.
18447 struct ptr_to_mbr_type::priv
18448 {
18449  // The type of the data member this pointer-to-member-type
18450  // designates.
18451  type_base_sptr dm_type_;
18452  // The class (or typedef to potentially qualified class) containing
18453  // the data member this pointer-to-member-type designates.
18454  type_base_sptr containing_type_;
18455  interned_string internal_qualified_name_;
18456  interned_string temp_internal_qualified_name_;
18457 
18458  priv()
18459  {}
18460 
18461  priv(const type_base_sptr& dm_type, const type_base_sptr& containing_type)
18462  : dm_type_(dm_type),
18463  containing_type_(containing_type)
18464  {}
18465 };// end struct ptr_to_mbr_type::priv
18466 
18467 /// A constructor for a @ref ptr_to_mbr_type type.
18468 ///
18469 /// @param env the environment to construct the @ref ptr_to_mbr_type in.
18470 ///
18471 /// @param member_type the member type of the of the @ref
18472 /// ptr_to_mbr_type to construct.
18473 ///
18474 /// @param containing_type the containing type of the @ref
18475 /// ptr_to_mbr_type to construct.
18476 ///
18477 /// @param size_in_bits the size (in bits) of the resulting type.
18478 ///
18479 /// @param alignment_in_bits the alignment (in bits) of the resulting
18480 /// type.
18481 ///
18482 /// @param locus the source location of the definition of the
18483 /// resulting type.
18484 ptr_to_mbr_type::ptr_to_mbr_type(const environment& env,
18485  const type_base_sptr& member_type,
18486  const type_base_sptr& containing_type,
18487  size_t size_in_bits,
18488  size_t alignment_in_bits,
18489  const location& locus)
18490  : type_or_decl_base(env,
18491  POINTER_TO_MEMBER_TYPE
18492  | ABSTRACT_TYPE_BASE
18493  | ABSTRACT_DECL_BASE),
18494  type_base(env, size_in_bits, alignment_in_bits),
18495  decl_base(env, "", locus, ""),
18496  priv_(new priv(member_type, containing_type))
18497 {
18498  runtime_type_instance(this);
18499  ABG_ASSERT(member_type);
18500  ABG_ASSERT(containing_type);
18501  interned_string name = ptr_to_mbr_declaration_name(this, "",
18502  /*qualified=*/true,
18503  /*internal=*/false);
18504  set_name(name);
18505 }
18506 
18507 /// Getter of the member type of the current @ref ptr_to_mbr_type.
18508 ///
18509 /// @return the type of the member referred to by the current
18510 /// @ptr_to_mbr_type.
18511 const type_base_sptr&
18513 {return priv_->dm_type_;}
18514 
18515 /// Getter of the type containing the member pointed-to by the current
18516 /// @ref ptr_to_mbr_type.
18517 ///
18518 /// @return the type containing the member pointed-to by the current
18519 /// @ref ptr_to_mbr_type.
18520 const type_base_sptr&
18522 {return priv_->containing_type_;}
18523 
18524 /// Equality operator for the current @ref ptr_to_mbr_type.
18525 ///
18526 ///@param o the other instance of @ref ptr_to_mbr_type to compare the
18527 ///current instance to.
18528 ///
18529 /// @return true iff the current @ref ptr_to_mbr_type equals @p o.
18530 bool
18532 {
18533  const ptr_to_mbr_type* other =
18534  dynamic_cast<const ptr_to_mbr_type*>(&o);
18535  if (!other)
18536  return false;
18537  return try_canonical_compare(this, other);
18538 }
18539 
18540 /// Equality operator for the current @ref ptr_to_mbr_type.
18541 ///
18542 ///@param o the other instance of @ref ptr_to_mbr_type to compare the
18543 ///current instance to.
18544 ///
18545 /// @return true iff the current @ref ptr_to_mbr_type equals @p o.
18546 bool
18548 {
18549  const decl_base* other = dynamic_cast<const decl_base*>(&o);
18550  if (!other)
18551  return false;
18552  return *this == *other;
18553 }
18554 
18555 /// Equality operator for the current @ref ptr_to_mbr_type.
18556 ///
18557 ///@param o the other instance of @ref ptr_to_mbr_type to compare the
18558 ///current instance to.
18559 ///
18560 /// @return true iff the current @ref ptr_to_mbr_type equals @p o.
18561 bool
18563 {
18564  const decl_base* other = dynamic_cast<const decl_base*>(&o);
18565  if (!other)
18566  return false;
18567  return *this == *other;
18568 }
18569 
18570 /// Get the qualified name for the current @ref ptr_to_mbr_type.
18571 ///
18572 /// @param qualified_name out parameter. This is set to the name of
18573 /// the current @ref ptr_to_mbr_type.
18574 ///
18575 /// @param internal if this is true, then the qualified name is for
18576 /// the purpose of type canoicalization.
18577 void
18579  bool internal) const
18580 {qualified_name = get_qualified_name(internal);}
18581 
18582 /// Get the qualified name for the current @ref ptr_to_mbr_type.
18583 ///
18584 /// @param internal if this is true, then the qualified name is for
18585 /// the purpose of type canoicalization.
18586 ///
18587 /// @return the qualified name for the current @ref ptr_to_mbr_type.
18588 const interned_string&
18590 {
18591  type_base_sptr member_type = get_member_type();
18592  type_base_sptr containing_type = get_containing_type();
18593 
18594  if (internal)
18595  {
18596  if (get_canonical_type())
18597  {
18598  if (priv_->internal_qualified_name_.empty())
18599  priv_->internal_qualified_name_ =
18600  ptr_to_mbr_declaration_name(this, "",
18601  /*qualified=*/true,
18602  internal);
18603  return priv_->internal_qualified_name_;
18604  }
18605  else
18606  {
18607  priv_->temp_internal_qualified_name_ =
18608  ptr_to_mbr_declaration_name(this, "", /*qualified=*/true, internal);
18609  return priv_->temp_internal_qualified_name_;
18610  }
18611  }
18612  else
18613  {
18615  (ptr_to_mbr_declaration_name(this, "", /*qualified=*/true,
18616  /*internal=*/false));
18618  }
18619 }
18620 
18621 /// This implements the ir_traversable_base::traverse pure virtual
18622 /// function for @ref ptr_to_mbr_type.
18623 ///
18624 /// @param v the visitor used on the current instance.
18625 ///
18626 /// @return true if the entire IR node tree got traversed, false
18627 /// otherwise.
18628 bool
18630 {
18631  if (v.type_node_has_been_visited(this))
18632  return true;
18633 
18634  if (visiting())
18635  return true;
18636 
18637  if (v.visit_begin(this))
18638  {
18639  visiting(true);
18640  if (type_base_sptr t = get_member_type())
18641  t->traverse(v);
18642 
18643  if (type_base_sptr t = get_containing_type())
18644  t->traverse(v);
18645  visiting(false);
18646  }
18647 
18648  bool result = v.visit_end(this);
18649  v.mark_type_node_as_visited(this);
18650  return result;
18651 }
18652 
18653 /// Desctructor for @ref ptr_to_mbr_type.
18655 {}
18656 
18657 
18658 /// Compares two instances of @ref ptr_to_mbr_type.
18659 ///
18660 /// If the two intances are different, set a bitfield to give some
18661 /// insight about the kind of differences there are.
18662 ///
18663 /// @param l the first artifact of the comparison.
18664 ///
18665 /// @param r the second artifact of the comparison.
18666 ///
18667 /// @param k a pointer to a bitfield that gives information about the
18668 /// kind of changes there are between @p l and @p r. This one is set
18669 /// iff @p k is non-null and the function returns false.
18670 ///
18671 /// Please note that setting k to a non-null value does have a
18672 /// negative performance impact because even if @p l and @p r are not
18673 /// equal, the function keeps up the comparison in order to determine
18674 /// the different kinds of ways in which they are different.
18675 ///
18676 /// @return true if @p l equals @p r, false otherwise.
18677 bool
18679 {
18680  bool result = true;
18681 
18682  if (!(l.decl_base::operator==(r)))
18683  {
18684  result = false;
18685  if (k)
18686  *k |= LOCAL_TYPE_CHANGE_KIND;
18687  else
18688  result = false;
18689  }
18690 
18691  if (l.get_member_type() != r.get_member_type())
18692  {
18693  if (k)
18694  {
18695  if (!types_have_similar_structure(&l, &r))
18696  *k |= LOCAL_TYPE_CHANGE_KIND;
18697  *k |= SUBTYPE_CHANGE_KIND;
18698  }
18699  result = false;
18700  }
18701 
18703  {
18704  if (k)
18705  {
18706  if (!types_have_similar_structure(&l, &r))
18707  *k |= LOCAL_TYPE_CHANGE_KIND;
18708  *k |= SUBTYPE_CHANGE_KIND;
18709  }
18710  result = false;
18711  }
18712 
18713  ABG_RETURN(result);
18714 }
18715 
18716 // </ptr_to_mbr_type definitions>
18717 
18718 // <array_type_def definitions>
18719 
18720 // <array_type_def::subrange_type>
18721 array_type_def::subrange_type::~subrange_type() = default;
18722 
18723 // <array_type_def::subrante_type::bound_value>
18724 
18725 /// Default constructor of the @ref
18726 /// array_type_def::subrange_type::bound_value class.
18727 ///
18728 /// Constructs an unsigned bound_value of value zero.
18730  : s_(UNSIGNED_SIGNEDNESS)
18731 {
18732  v_.unsigned_ = 0;
18733 }
18734 
18735 /// Initialize an unsigned bound_value with a given value.
18736 ///
18737 /// @param v the initial bound value.
18739  : s_(UNSIGNED_SIGNEDNESS)
18740 {
18741  v_.unsigned_ = v;
18742 }
18743 
18744 /// Initialize a signed bound_value with a given value.
18745 ///
18746 /// @param v the initial bound value.
18748  : s_(SIGNED_SIGNEDNESS)
18749 {
18750  v_.signed_ = v;
18751 }
18752 
18753 /// Getter of the signedness (unsigned VS signed) of the bound value.
18754 ///
18755 /// @return the signedness of the bound value.
18756 enum array_type_def::subrange_type::bound_value::signedness
18758 {return s_;}
18759 
18760 /// Setter of the signedness (unsigned VS signed) of the bound value.
18761 ///
18762 /// @param s the new signedness of the bound value.
18763 void
18765 { s_ = s;}
18766 
18767 /// Getter of the bound value as a signed value.
18768 ///
18769 /// @return the bound value as signed.
18770 int64_t
18772 {return v_.signed_;
18773 }
18774 
18775 /// Getter of the bound value as an unsigned value.
18776 ///
18777 /// @return the bound value as unsigned.
18778 uint64_t
18780 {return v_.unsigned_;}
18781 
18782 /// Setter of the bound value as unsigned.
18783 ///
18784 /// @param v the new unsigned value.
18785 void
18787 {
18788  s_ = UNSIGNED_SIGNEDNESS;
18789  v_.unsigned_ = v;
18790 }
18791 
18792 /// Setter of the bound value as signed.
18793 ///
18794 /// @param v the new signed value.
18795 void
18797 {
18798  s_ = SIGNED_SIGNEDNESS;
18799  v_.signed_ = v;
18800 }
18801 
18802 /// Equality operator of the bound value.
18803 ///
18804 /// @param v the other bound value to compare with.
18805 ///
18806 /// @return true iff the current bound value equals @p v.
18807 bool
18809 {
18810  return s_ == v.s_ && v_.unsigned_ == v.v_.unsigned_;
18811 }
18812 
18813 // </array_type_def::subrante_type::bound_value>
18814 
18815 struct array_type_def::subrange_type::priv
18816 {
18817  bound_value lower_bound_;
18818  bound_value upper_bound_;
18819  type_base_wptr underlying_type_;
18820  translation_unit::language language_;
18821  bool infinite_;
18822 
18823  priv(bound_value ub,
18824  translation_unit::language l = translation_unit::LANG_C11)
18825  : upper_bound_(ub), language_(l), infinite_(false)
18826  {}
18827 
18828  priv(bound_value lb, bound_value ub,
18829  translation_unit::language l = translation_unit::LANG_C11)
18830  : lower_bound_(lb), upper_bound_(ub),
18831  language_(l), infinite_(false)
18832  {}
18833 
18834  priv(bound_value lb, bound_value ub, const type_base_sptr &u,
18835  translation_unit::language l = translation_unit::LANG_C11)
18836  : lower_bound_(lb), upper_bound_(ub), underlying_type_(u),
18837  language_(l), infinite_(false)
18838  {}
18839 };
18840 
18841 /// Constructor of an array_type_def::subrange_type type.
18842 ///
18843 /// @param env the environment this type was created from.
18844 ///
18845 /// @param name the name of the subrange type.
18846 ///
18847 /// @param lower_bound the lower bound of the array. This is
18848 /// generally zero (at least for C and C++).
18849 ///
18850 /// @param upper_bound the upper bound of the array.
18851 ///
18852 /// @param underlying_type the underlying type of the subrange type.
18853 ///
18854 /// @param loc the source location where the type is defined.
18855 array_type_def::subrange_type::subrange_type(const environment& env,
18856  const string& name,
18857  bound_value lower_bound,
18858  bound_value upper_bound,
18859  const type_base_sptr& utype,
18860  const location& loc,
18862  : type_or_decl_base(env, SUBRANGE_TYPE | ABSTRACT_TYPE_BASE | ABSTRACT_DECL_BASE),
18863  type_base(env,
18864  upper_bound.get_unsigned_value()
18865  - lower_bound.get_unsigned_value(),
18866  0),
18867  decl_base(env, name, loc, ""),
18868  priv_(new priv(lower_bound, upper_bound, utype, l))
18869 {
18870  runtime_type_instance(this);
18871 }
18872 
18873 /// Constructor of the array_type_def::subrange_type type.
18874 ///
18875 /// @param env the environment this type is being created in.
18876 ///
18877 /// @param name the name of the subrange type.
18878 ///
18879 /// @param lower_bound the lower bound of the array. This is
18880 /// generally zero (at least for C and C++).
18881 ///
18882 /// @param upper_bound the upper bound of the array.
18883 ///
18884 /// @param loc the source location where the type is defined.
18885 ///
18886 /// @param l the language that generated this subrange.
18887 array_type_def::subrange_type::subrange_type(const environment& env,
18888  const string& name,
18889  bound_value lower_bound,
18890  bound_value upper_bound,
18891  const location& loc,
18893  : type_or_decl_base(env, SUBRANGE_TYPE | ABSTRACT_TYPE_BASE | ABSTRACT_DECL_BASE),
18894  type_base(env,
18895  upper_bound.get_unsigned_value()
18896  - lower_bound.get_unsigned_value(), 0),
18897  decl_base(env, name, loc, ""),
18898  priv_(new priv(lower_bound, upper_bound, l))
18899 {
18900  runtime_type_instance(this);
18901 }
18902 
18903 /// Constructor of the array_type_def::subrange_type type.
18904 ///
18905 /// @param env the environment this type is being created from.
18906 ///
18907 /// @param name of the name of type.
18908 ///
18909 /// @param upper_bound the upper bound of the array. The lower bound
18910 /// is considered to be zero.
18911 ///
18912 /// @param loc the source location of the type.
18913 ///
18914 /// @param the language that generated this type.
18915 array_type_def::subrange_type::subrange_type(const environment& env,
18916  const string& name,
18917  bound_value upper_bound,
18918  const location& loc,
18920  : type_or_decl_base(env, SUBRANGE_TYPE | ABSTRACT_TYPE_BASE | ABSTRACT_DECL_BASE),
18921  type_base(env, upper_bound.get_unsigned_value(), 0),
18922  decl_base(env, name, loc, ""),
18923  priv_(new priv(upper_bound, l))
18924 {
18925  runtime_type_instance(this);
18926 }
18927 
18928 /// Getter of the underlying type of the subrange, that is, the type
18929 /// that defines the range.
18930 ///
18931 /// @return the underlying type.
18932 type_base_sptr
18934 {return priv_->underlying_type_.lock();}
18935 
18936 /// Setter of the underlying type of the subrange, that is, the type
18937 /// that defines the range.
18938 ///
18939 /// @param u the new underlying type.
18940 void
18942 {
18943  ABG_ASSERT(priv_->underlying_type_.expired());
18944  priv_->underlying_type_ = u;
18945 }
18946 
18947 /// Getter of the upper bound of the subrange type.
18948 ///
18949 /// @return the upper bound of the subrange type.
18950 int64_t
18952 {return priv_->upper_bound_.get_signed_value();}
18953 
18954 /// Getter of the lower bound of the subrange type.
18955 ///
18956 /// @return the lower bound of the subrange type.
18957 int64_t
18959 {return priv_->lower_bound_.get_signed_value();}
18960 
18961 /// Setter of the upper bound of the subrange type.
18962 ///
18963 /// @param ub the new value of the upper bound.
18964 void
18966 {priv_->upper_bound_ = ub;}
18967 
18968 /// Setter of the lower bound.
18969 ///
18970 /// @param lb the new value of the lower bound.
18971 void
18973 {priv_->lower_bound_ = lb;}
18974 
18975 /// Getter of the length of the subrange type.
18976 ///
18977 /// Note that a length of zero means the array has an infinite (or
18978 /// rather a non-known) size.
18979 ///
18980 /// @return the length of the subrange type.
18981 uint64_t
18983 {
18984  if (is_non_finite())
18985  return 0;
18986 
18987  // A subrange can have an upper bound that is lower than its lower
18988  // bound. This is possible in Ada for instance. In that case, the
18989  // length of the subrange is considered to be zero.
18990  if (get_upper_bound() >= get_lower_bound())
18991  return get_upper_bound() - get_lower_bound() + 1;
18992  return 0;
18993 }
18994 
18995 /// Test if the length of the subrange type is infinite.
18996 ///
18997 /// @return true iff the length of the subrange type is infinite.
18998 bool
19000 {return priv_->infinite_;}
19001 
19002 /// Set the infinite-ness status of the subrange type.
19003 ///
19004 /// @param f true iff the length of the subrange type should be set to
19005 /// being infinite.
19006 void
19008 {priv_->infinite_ = f;}
19009 
19010 /// Getter of the language that generated this type.
19011 ///
19012 /// @return the language of this type.
19015 {return priv_->language_;}
19016 
19017 /// Return a string representation of the sub range.
19018 ///
19019 /// @return the string representation of the sub range.
19020 string
19022 {
19023  std::ostringstream o;
19024 
19026  {
19027  type_base_sptr underlying_type = get_underlying_type();
19028  if (underlying_type)
19029  o << ir::get_pretty_representation(underlying_type, false) << " ";
19030  o << "range "<< get_lower_bound() << " .. " << get_upper_bound();
19031  }
19032  else if (is_non_finite())
19033  o << "[]";
19034  else
19035  o << "[" << get_length() << "]";
19036 
19037  return o.str();
19038 }
19039 
19040 /// Return a string representation of a vector of subranges
19041 ///
19042 /// @return the string representation of a vector of sub ranges.
19043 string
19045 {
19046  if (v.empty())
19047  return "[]";
19048 
19049  string r;
19050  for (vector<subrange_sptr>::const_iterator i = v.begin();
19051  i != v.end();
19052  ++i)
19053  r += (*i)->as_string();
19054 
19055  return r;
19056 }
19057 
19058 /// Compares two isntances of @ref array_type_def::subrange_type.
19059 ///
19060 /// If the two intances are different, set a bitfield to give some
19061 /// insight about the kind of differences there are.
19062 ///
19063 /// @param l the first artifact of the comparison.
19064 ///
19065 /// @param r the second artifact of the comparison.
19066 ///
19067 /// @param k a pointer to a bitfield that gives information about the
19068 /// kind of changes there are between @p l and @p r. This one is set
19069 /// iff @p k is non-null and the function returns false.
19070 ///
19071 /// Please note that setting k to a non-null value does have a
19072 /// negative performance impact because even if @p l and @p r are not
19073 /// equal, the function keeps up the comparison in order to determine
19074 /// the different kinds of ways in which they are different.
19075 ///
19076 /// @return true if @p l equals @p r, false otherwise.
19077 bool
19080  change_kind* k)
19081 {
19082  bool result = true;
19083 
19084  if (l.get_lower_bound() != r.get_lower_bound()
19085  || l.get_upper_bound() != r.get_upper_bound()
19086  || l.get_name() != r.get_name())
19087  {
19088  result = false;
19089  if (k)
19090  *k |= LOCAL_TYPE_CHANGE_KIND;
19091  else
19092  ABG_RETURN(result);
19093  }
19094 
19095  ABG_RETURN(result);
19096 }
19097 
19098 /// Equality operator.
19099 ///
19100 /// @param o the other subrange to test against.
19101 ///
19102 /// @return true iff @p o equals the current instance of
19103 /// array_type_def::subrange_type.
19104 bool
19106 {
19107  const subrange_type* other =
19108  dynamic_cast<const subrange_type*>(&o);
19109  if (!other)
19110  return false;
19111  return try_canonical_compare(this, other);
19112 }
19113 
19114 /// Equality operator.
19115 ///
19116 /// @param o the other subrange to test against.
19117 ///
19118 /// @return true iff @p o equals the current instance of
19119 /// array_type_def::subrange_type.
19120 bool
19122 {
19123  const decl_base* other = dynamic_cast<const decl_base*>(&o);
19124  if (!other)
19125  return false;
19126  return *this == *other;
19127 }
19128 
19129 /// Equality operator.
19130 ///
19131 /// @param o the other subrange to test against.
19132 ///
19133 /// @return true iff @p o equals the current instance of
19134 /// array_type_def::subrange_type.
19135 bool
19137 {
19138  const type_base &t = o;
19139  return operator==(t);
19140 }
19141 
19142 /// Equality operator.
19143 ///
19144 /// @param o the other subrange to test against.
19145 ///
19146 /// @return true iff @p o equals the current instance of
19147 /// array_type_def::subrange_type.
19148 bool
19150 {return !operator==(o);}
19151 
19152 /// Equality operator.
19153 ///
19154 /// @param o the other subrange to test against.
19155 ///
19156 /// @return true iff @p o equals the current instance of
19157 /// array_type_def::subrange_type.
19158 bool
19160 {return !operator==(o);}
19161 
19162 /// Inequality operator.
19163 ///
19164 /// @param o the other subrange to test against.
19165 ///
19166 /// @return true iff @p o is different from the current instance of
19167 /// array_type_def::subrange_type.
19168 bool
19170 {return !operator==(o);}
19171 
19172 /// Build a pretty representation for an
19173 /// array_type_def::subrange_type.
19174 ///
19175 /// @param internal set to true if the call is intended to get a
19176 /// representation of the decl (or type) for the purpose of canonical
19177 /// type comparison. This is mainly used in the function
19178 /// type_base::get_canonical_type_for().
19179 ///
19180 /// In other words if the argument for this parameter is true then the
19181 /// call is meant for internal use (for technical use inside the
19182 /// library itself), false otherwise. If you don't know what this is
19183 /// for, then set it to false.
19184 ///
19185 /// @return a copy of the pretty representation of the current
19186 /// instance of typedef_decl.
19187 string
19189 {
19190  string name = get_name();
19191  string repr;
19192 
19193  if (name.empty())
19194  repr += "<anonymous range>";
19195  else
19196  repr += "<range " + get_name() + ">";
19197  repr += as_string();
19198 
19199  return repr;
19200 }
19201 
19202 /// This implements the ir_traversable_base::traverse pure virtual
19203 /// function.
19204 ///
19205 /// @param v the visitor used on the current instance.
19206 ///
19207 /// @return true if the entire IR node tree got traversed, false
19208 /// otherwise.
19209 bool
19211 {
19212  if (v.type_node_has_been_visited(this))
19213  return true;
19214 
19215  if (v.visit_begin(this))
19216  {
19217  visiting(true);
19218  if (type_base_sptr u = get_underlying_type())
19219  u->traverse(v);
19220  visiting(false);
19221  }
19222 
19223  bool result = v.visit_end(this);
19224  v.mark_type_node_as_visited(this);
19225  return result;
19226 }
19227 
19228 // </array_type_def::subrange_type>
19229 
19230 struct array_type_def::priv
19231 {
19232  type_base_wptr element_type_;
19233  subranges_type subranges_;
19234  interned_string temp_internal_qualified_name_;
19235  interned_string internal_qualified_name_;
19236 
19237  priv(type_base_sptr t)
19238  : element_type_(t)
19239  {}
19240 
19241  priv(type_base_sptr t, subranges_type subs)
19242  : element_type_(t), subranges_(subs)
19243  {}
19244 
19245  priv()
19246  {}
19247 };
19248 
19249 /// Constructor for the type array_type_def
19250 ///
19251 /// Note how the constructor expects a vector of subrange
19252 /// objects. Parsing of the array information always entails
19253 /// parsing the subrange info as well, thus the class subrange_type
19254 /// is defined inside class array_type_def and also parsed
19255 /// simultaneously.
19256 ///
19257 /// @param e_type the type of the elements contained in the array
19258 ///
19259 /// @param subs a vector of the array's subranges(dimensions)
19260 ///
19261 /// @param locus the source location of the array type definition.
19262 array_type_def::array_type_def(const type_base_sptr e_type,
19263  const std::vector<subrange_sptr>& subs,
19264  const location& locus)
19265  : type_or_decl_base(e_type->get_environment(),
19266  ARRAY_TYPE
19267  | ABSTRACT_TYPE_BASE
19268  | ABSTRACT_DECL_BASE),
19269  type_base(e_type->get_environment(), 0, e_type->get_alignment_in_bits()),
19270  decl_base(e_type->get_environment(), locus),
19271  priv_(new priv(e_type))
19272 {
19273  runtime_type_instance(this);
19274  append_subranges(subs);
19275 }
19276 
19277 /// Constructor for the type array_type_def
19278 ///
19279 /// This constructor builds a temporary array that has no element type
19280 /// associated. Later when the element type is available, it be set
19281 /// with the array_type_def::set_element_type() member function.
19282 ///
19283 /// Note how the constructor expects a vector of subrange
19284 /// objects. Parsing of the array information always entails
19285 /// parsing the subrange info as well, thus the class subrange_type
19286 /// is defined inside class array_type_def and also parsed
19287 /// simultaneously.
19288 ///
19289 /// @param env the environment of the array type.
19290 ///
19291 /// @param subs a vector of the array's subranges(dimensions)
19292 ///
19293 /// @param locus the source location of the array type definition.
19294 array_type_def::array_type_def(const environment& env,
19295  const std::vector<subrange_sptr>& subs,
19296  const location& locus)
19297  : type_or_decl_base(env,
19298  ARRAY_TYPE
19299  | ABSTRACT_TYPE_BASE
19300  | ABSTRACT_DECL_BASE),
19301  type_base(env, 0, 0),
19302  decl_base(env, locus),
19303  priv_(new priv)
19304 {
19305  runtime_type_instance(this);
19306  append_subranges(subs);
19307 }
19308 
19309 /// Update the size of the array.
19310 ///
19311 /// This function computes the size of the array and sets it using
19312 /// type_base::set_size_in_bits().
19313 void
19314 array_type_def::update_size()
19315 {
19316  type_base_sptr e = priv_->element_type_.lock();
19317  if (e)
19318  {
19319  size_t s = e->get_size_in_bits();
19320  if (s)
19321  {
19322  for (const auto &sub : get_subranges())
19323  s *= sub->get_length();
19324  set_size_in_bits(s);
19325  }
19326  set_alignment_in_bits(e->get_alignment_in_bits());
19327  }
19328 }
19329 
19330 string
19331 array_type_def::get_subrange_representation() const
19332 {
19334  return r;
19335 }
19336 
19337 /// Get the pretty representation of the current instance of @ref
19338 /// array_type_def.
19339 ///
19340 /// @param internal set to true if the call is intended to get a
19341 /// representation of the decl (or type) for the purpose of canonical
19342 /// type comparison. This is mainly used in the function
19343 /// type_base::get_canonical_type_for().
19344 ///
19345 /// In other words if the argument for this parameter is true then the
19346 /// call is meant for internal use (for technical use inside the
19347 /// library itself), false otherwise. If you don't know what this is
19348 /// for, then set it to false.
19349 /// @param internal set to true if the call is intended for an
19350 /// internal use (for technical use inside the library itself), false
19351 /// otherwise. If you don't know what this is for, then set it to
19352 /// false.
19353 ///
19354 /// @return the pretty representation of the ABI artifact.
19355 string
19357  bool qualified_name) const
19358 {
19359  return array_declaration_name(this, /*variable_name=*/"",
19360  qualified_name, internal);
19361 }
19362 
19363 /// Compares two instances of @ref array_type_def.
19364 ///
19365 /// If the two intances are different, set a bitfield to give some
19366 /// insight about the kind of differences there are.
19367 ///
19368 /// @param l the first artifact of the comparison.
19369 ///
19370 /// @param r the second artifact of the comparison.
19371 ///
19372 /// @param k a pointer to a bitfield that gives information about the
19373 /// kind of changes there are between @p l and @p r. This one is set
19374 /// iff @p k is non-null and the function returns false.
19375 ///
19376 /// Please note that setting k to a non-null value does have a
19377 /// negative performance impact because even if @p l and @p r are not
19378 /// equal, the function keeps up the comparison in order to determine
19379 /// the different kinds of ways in which they are different.
19380 ///
19381 /// @return true if @p l equals @p r, false otherwise.
19382 bool
19384 {
19385  std::vector<array_type_def::subrange_sptr > this_subs = l.get_subranges();
19386  std::vector<array_type_def::subrange_sptr > other_subs = r.get_subranges();
19387 
19388  bool result = true;
19389  if (this_subs.size() != other_subs.size())
19390  {
19391  result = false;
19392  if (k)
19393  *k |= LOCAL_TYPE_CHANGE_KIND;
19394  else
19396  }
19397 
19398  std::vector<array_type_def::subrange_sptr >::const_iterator i,j;
19399  for (i = this_subs.begin(), j = other_subs.begin();
19400  i != this_subs.end() && j != other_subs.end();
19401  ++i, ++j)
19402  if (**i != **j)
19403  {
19404  result = false;
19405  if (k)
19406  {
19407  *k |= LOCAL_TYPE_CHANGE_KIND;
19408  break;
19409  }
19410  else
19412  }
19413 
19414  // Compare the element types modulo the typedefs they might have
19415  if (l.get_element_type() != r.get_element_type())
19416  {
19417  result = false;
19418  if (k)
19419  *k |= SUBTYPE_CHANGE_KIND;
19420  else
19422  }
19423 
19424  ABG_RETURN(result);
19425 }
19426 
19427 /// Test if two variables are equals modulo CV qualifiers.
19428 ///
19429 /// @param l the first array of the comparison.
19430 ///
19431 /// @param r the second array of the comparison.
19432 ///
19433 /// @return true iff @p l equals @p r or, if they are different, the
19434 /// difference between the too is just a matter of CV qualifiers.
19435 bool
19437 {
19438  if (l == r)
19439  return true;
19440 
19441  if (!l || !r)
19443 
19446 
19447  std::vector<array_type_def::subrange_sptr > this_subs = l->get_subranges();
19448  std::vector<array_type_def::subrange_sptr > other_subs = r->get_subranges();
19449 
19450  if (this_subs.size() != other_subs.size())
19452 
19453  std::vector<array_type_def::subrange_sptr >::const_iterator i,j;
19454  for (i = this_subs.begin(), j = other_subs.begin();
19455  i != this_subs.end() && j != other_subs.end();
19456  ++i, ++j)
19457  if (**i != **j)
19459 
19460  type_base *first_element_type =
19462  type_base *second_element_type =
19464 
19465  if (*first_element_type != *second_element_type)
19467 
19468  return true;
19469 }
19470 
19471 /// Get the language of the array.
19472 ///
19473 /// @return the language of the array.
19476 {
19477  const std::vector<subrange_sptr>& subranges =
19478  get_subranges();
19479 
19480  if (subranges.empty())
19481  return translation_unit::LANG_C11;
19482  return subranges.front()->get_language();
19483 }
19484 
19485 bool
19487 {
19488  const array_type_def* other =
19489  dynamic_cast<const array_type_def*>(&o);
19490  if (!other)
19491  return false;
19492  return try_canonical_compare(this, other);
19493 }
19494 
19495 bool
19497 {
19498  const decl_base* other = dynamic_cast<const decl_base*>(&o);
19499  if (!other)
19500  return false;
19501  return *this == *other;
19502 }
19503 
19504 /// Getter of the type of an array element.
19505 ///
19506 /// @return the type of an array element.
19507 const type_base_sptr
19509 {return priv_->element_type_.lock();}
19510 
19511 /// Setter of the type of array element.
19512 ///
19513 /// Beware that after using this function, one might want to
19514 /// re-compute the canonical type of the array, if one has already
19515 /// been computed.
19516 ///
19517 /// The intended use of this method is to permit in-place adjustment
19518 /// of the element type's qualifiers. In particular, the size of the
19519 /// element type should not be changed.
19520 ///
19521 /// @param element_type the new element type to set.
19522 void
19523 array_type_def::set_element_type(const type_base_sptr& element_type)
19524 {
19525  priv_->element_type_ = element_type;
19526  update_size();
19528 }
19529 
19530 /// Append subranges from the vector @param subs to the current
19531 /// vector of subranges.
19532 void
19533 array_type_def::append_subranges(const std::vector<subrange_sptr>& subs)
19534 {
19535 
19536  for (const auto &sub : subs)
19537  priv_->subranges_.push_back(sub);
19538 
19539  update_size();
19541 }
19542 
19543 /// @return true if one of the sub-ranges of the array is infinite, or
19544 /// if the array has no sub-range at all, also meaning that the size
19545 /// of the array is infinite.
19546 bool
19548 {
19549  if (priv_->subranges_.empty())
19550  return true;
19551 
19552  for (std::vector<shared_ptr<subrange_type> >::const_iterator i =
19553  priv_->subranges_.begin();
19554  i != priv_->subranges_.end();
19555  ++i)
19556  if ((*i)->is_non_finite())
19557  return true;
19558 
19559  return false;
19560 }
19561 
19562 int
19563 array_type_def::get_dimension_count() const
19564 {return priv_->subranges_.size();}
19565 
19566 /// Build and return the qualified name of the current instance of the
19567 /// @ref array_type_def.
19568 ///
19569 /// @param qn output parameter. Is set to the newly-built qualified
19570 /// name of the current instance of @ref array_type_def.
19571 ///
19572 /// @param internal set to true if the call is intended for an
19573 /// internal use (for technical use inside the library itself), false
19574 /// otherwise. If you don't know what this is for, then set it to
19575 /// false.
19576 void
19578 {qn = get_qualified_name(internal);}
19579 
19580 /// Compute the qualified name of the array.
19581 ///
19582 /// @param internal set to true if the call is intended for an
19583 /// internal use (for technical use inside the library itself), false
19584 /// otherwise. If you don't know what this is for, then set it to
19585 /// false.
19586 ///
19587 /// @return the resulting qualified name.
19588 const interned_string&
19590 {
19591  if (internal)
19592  {
19593  if (get_canonical_type())
19594  {
19595  if (priv_->internal_qualified_name_.empty())
19596  priv_->internal_qualified_name_ =
19597  array_declaration_name(this, /*variable_name=*/"",
19598  /*qualified=*/false,
19599  /*internal=*/true);
19600  return priv_->internal_qualified_name_;
19601  }
19602  else
19603  {
19604  priv_->temp_internal_qualified_name_ =
19605  array_declaration_name(this, /*variable_name=*/"",
19606  /*qualified*/false, /*internal*/true);
19607  return priv_->temp_internal_qualified_name_;
19608  }
19609  }
19610  else
19611  {
19612  if (get_canonical_type())
19613  {
19614  if (decl_base::peek_qualified_name().empty())
19615  set_qualified_name(array_declaration_name(this,
19616  /*variable_name=*/"",
19617  /*qualified=*/false,
19618  /*internal=*/false));
19620  }
19621  else
19622  {
19624  (array_declaration_name(this, /*variable_name=*/"",
19625  /*qualified=*/false,
19626  /*internal=*/false));
19628  }
19629  }
19630 }
19631 
19632 /// This implements the ir_traversable_base::traverse pure virtual
19633 /// function.
19634 ///
19635 /// @param v the visitor used on the current instance.
19636 ///
19637 /// @return true if the entire IR node tree got traversed, false
19638 /// otherwise.
19639 bool
19641 {
19642  if (v.type_node_has_been_visited(this))
19643  return true;
19644 
19645  if (visiting())
19646  return true;
19647 
19648  if (v.visit_begin(this))
19649  {
19650  visiting(true);
19651  if (type_base_sptr t = get_element_type())
19652  t->traverse(v);
19653  visiting(false);
19654  }
19655 
19656  bool result = v.visit_end(this);
19657  v.mark_type_node_as_visited(this);
19658  return result;
19659 }
19660 
19661 const location&
19662 array_type_def::get_location() const
19663 {return decl_base::get_location();}
19664 
19665 /// Get the array's subranges
19666 const std::vector<array_type_def::subrange_sptr>&
19668 {return priv_->subranges_;}
19669 
19670 array_type_def::~array_type_def()
19671 {}
19672 
19673 // </array_type_def definitions>
19674 
19675 // <enum_type_decl definitions>
19676 
19677 class enum_type_decl::priv
19678 {
19679  type_base_sptr underlying_type_;
19680  enumerators enumerators_;
19681  mutable enumerators sorted_enumerators_;
19682 
19683  friend class enum_type_decl;
19684 
19685  priv();
19686 
19687 public:
19688  priv(type_base_sptr underlying_type,
19690  : underlying_type_(underlying_type),
19691  enumerators_(enumerators)
19692  {}
19693 }; // end class enum_type_decl::priv
19694 
19695 /// Constructor.
19696 ///
19697 /// @param name the name of the type declaration.
19698 ///
19699 /// @param locus the source location where the type was defined.
19700 ///
19701 /// @param underlying_type the underlying type of the enum.
19702 ///
19703 /// @param enums the enumerators of this enum type.
19704 ///
19705 /// @param linkage_name the linkage name of the enum.
19706 ///
19707 /// @param vis the visibility of the enum type.
19708 enum_type_decl::enum_type_decl(const string& name,
19709  const location& locus,
19710  type_base_sptr underlying_type,
19711  enumerators& enums,
19712  const string& linkage_name,
19713  visibility vis)
19714  : type_or_decl_base(underlying_type->get_environment(),
19715  ENUM_TYPE
19716  | ABSTRACT_TYPE_BASE
19717  | ABSTRACT_DECL_BASE),
19718  type_base(underlying_type->get_environment(),
19719  underlying_type->get_size_in_bits(),
19720  underlying_type->get_alignment_in_bits()),
19721  decl_base(underlying_type->get_environment(),
19722  name, locus, linkage_name, vis),
19723  priv_(new priv(underlying_type, enums))
19724 {
19725  runtime_type_instance(this);
19726  for (enumerators::iterator e = get_enumerators().begin();
19727  e != get_enumerators().end();
19728  ++e)
19729  e->set_enum_type(this);
19730 }
19731 
19732 /// Return the underlying type of the enum.
19733 type_base_sptr
19735 {return priv_->underlying_type_;}
19736 
19737 /// @return the list of enumerators of the enum.
19740 {return priv_->enumerators_;}
19741 
19742 /// @return the list of enumerators of the enum.
19745 {return priv_->enumerators_;}
19746 
19747 /// Get the lexicographically sorted vector of enumerators.
19748 ///
19749 /// @return the lexicographically sorted vector of enumerators.
19752 {
19753  if (priv_->sorted_enumerators_.empty())
19754  {
19755  for (auto e = get_enumerators().rbegin();
19756  e != get_enumerators().rend();
19757  ++e)
19758  priv_->sorted_enumerators_.push_back(*e);
19759 
19760  std::sort(priv_->sorted_enumerators_.begin(),
19761  priv_->sorted_enumerators_.end(),
19762  [](const enum_type_decl::enumerator& l,
19763  const enum_type_decl::enumerator& r)
19764  {
19765  if (l.get_name() == r.get_name())
19766  return l.get_value() < r.get_value();
19767  return (l.get_name() < r.get_name());
19768  });
19769  }
19770 
19771  return priv_->sorted_enumerators_;
19772 }
19773 
19774 /// Get the pretty representation of the current instance of @ref
19775 /// enum_type_decl.
19776 ///
19777 /// @param internal set to true if the call is intended to get a
19778 /// representation of the decl (or type) for the purpose of canonical
19779 /// type comparison. This is mainly used in the function
19780 /// type_base::get_canonical_type_for().
19781 ///
19782 /// In other words if the argument for this parameter is true then the
19783 /// call is meant for internal use (for technical use inside the
19784 /// library itself), false otherwise. If you don't know what this is
19785 /// for, then set it to false.
19786 ///
19787 /// @param qualified_name if true, names emitted in the pretty
19788 /// representation are fully qualified.
19789 ///
19790 /// @return the pretty representation of the enum type.
19791 string
19793  bool qualified_name) const
19794 {
19795  string r = "enum ";
19796 
19797  if (internal && get_is_anonymous())
19798  r += get_type_name(this, qualified_name, /*internal=*/true);
19799  else if (get_is_anonymous())
19800  r += get_enum_flat_representation(*this, "",
19801  /*one_line=*/true,
19802  qualified_name);
19803  else
19805  qualified_name);
19806  return r;
19807 }
19808 
19809 /// This implements the ir_traversable_base::traverse pure virtual
19810 /// function.
19811 ///
19812 /// @param v the visitor used on the current instance.
19813 ///
19814 /// @return true if the entire IR node tree got traversed, false
19815 /// otherwise.
19816 bool
19818 {
19819  if (v.type_node_has_been_visited(this))
19820  return true;
19821 
19822  if (visiting())
19823  return true;
19824 
19825  if (v.visit_begin(this))
19826  {
19827  visiting(true);
19828  if (type_base_sptr t = get_underlying_type())
19829  t->traverse(v);
19830  visiting(false);
19831  }
19832 
19833  bool result = v.visit_end(this);
19834  v.mark_type_node_as_visited(this);
19835  return result;
19836 }
19837 
19838 /// Destructor for the enum type declaration.
19840 {}
19841 
19842 /// Test if two enums differ, but not by a name change.
19843 ///
19844 /// @param l the first enum to consider.
19845 ///
19846 /// @param r the second enum to consider.
19847 ///
19848 /// @return true iff @p l differs from @p r by anything but a name
19849 /// change.
19850 bool
19852  const enum_type_decl& r,
19853  change_kind* k)
19854 {
19855  bool result = false;
19856  if (*l.get_underlying_type() != *r.get_underlying_type())
19857  {
19858  result = true;
19859  if (k)
19860  *k |= SUBTYPE_CHANGE_KIND;
19861  else
19862  return true;
19863  }
19864 
19865  enum_type_decl::enumerators::const_iterator i, j;
19866  for (i = l.get_enumerators().begin(), j = r.get_enumerators().begin();
19867  i != l.get_enumerators().end() && j != r.get_enumerators().end();
19868  ++i, ++j)
19869  if (*i != *j)
19870  {
19871  result = true;
19872  if (k)
19873  {
19874  *k |= LOCAL_TYPE_CHANGE_KIND;
19875  break;
19876  }
19877  else
19878  return true;
19879  }
19880 
19881  if (i != l.get_enumerators().end() || j != r.get_enumerators().end())
19882  {
19883  result = true;
19884  if (k)
19885  *k |= LOCAL_TYPE_CHANGE_KIND;
19886  else
19887  return true;
19888  }
19889 
19890  enum_type_decl &local_r = const_cast<enum_type_decl&>(r);
19893  string n_l = l.get_name();
19894  string n_r = r.get_name();
19895  local_r.set_qualified_name(qn_l);
19896  local_r.set_name(n_l);
19897 
19898  if (!(l.decl_base::operator==(r) && l.type_base::operator==(r)))
19899  {
19900  result = true;
19901  if (k)
19902  {
19903  if (!l.decl_base::operator==(r))
19905  if (!l.type_base::operator==(r))
19906  *k |= LOCAL_TYPE_CHANGE_KIND;
19907  }
19908  else
19909  {
19910  local_r.set_name(n_r);
19911  local_r.set_qualified_name(qn_r);
19912  return true;
19913  }
19914  }
19915  local_r.set_qualified_name(qn_r);
19916  local_r.set_name(n_r);
19917 
19918  return result;
19919 }
19920 
19921 /// Test if a given enumerator is found present in an enum.
19922 ///
19923 /// This is a subroutine of the equals function for enums.
19924 ///
19925 /// @param enr the enumerator to consider.
19926 ///
19927 /// @param enom the enum to consider.
19928 ///
19929 /// @return true iff the enumerator @p enr is present in the enum @p
19930 /// enom.
19931 bool
19933  const enum_type_decl &enom)
19934 {
19935  for (const auto &e : enom.get_enumerators())
19936  if (e == enr)
19937  return true;
19938  return false;
19939 }
19940 
19941 /// Check if two enumerators values are equal.
19942 ///
19943 /// This function doesn't check if the names of the enumerators are
19944 /// equal or not.
19945 ///
19946 /// @param enr the first enumerator to consider.
19947 ///
19948 /// @param enl the second enumerator to consider.
19949 ///
19950 /// @return true iff @p enr has the same value as @p enl.
19951 static bool
19952 enumerators_values_are_equal(const enum_type_decl::enumerator &enr,
19953  const enum_type_decl::enumerator &enl)
19954 {return enr.get_value() == enl.get_value();}
19955 
19956 /// Detect if a given enumerator value is present in an enum.
19957 ///
19958 /// This function looks inside the enumerators of a given enum and
19959 /// detect if the enum contains at least one enumerator or a given
19960 /// value. The function also detects if the enumerator value we are
19961 /// looking at is present in the enum with a different name. An
19962 /// enumerator with the same value but with a different name is named
19963 /// a "redundant enumerator". The function returns the set of
19964 /// enumerators that are redundant with the value we are looking at.
19965 ///
19966 /// @param enr the enumerator to consider.
19967 ///
19968 /// @param enom the enum to consider.
19969 ///
19970 /// @param redundant_enrs if the function returns true, then this
19971 /// vector is filled with enumerators that are redundant with the
19972 /// value of @p enr.
19973 ///
19974 /// @return true iff the function detects that @p enom contains
19975 /// enumerators with the same value as @p enr.
19976 static bool
19977 is_enumerator_value_present_in_enum(const enum_type_decl::enumerator &enr,
19978  const enum_type_decl &enom,
19979  vector<enum_type_decl::enumerator>& redundant_enrs)
19980 {
19981  bool found = false;
19982  for (const auto &e : enom.get_enumerators())
19983  if (enumerators_values_are_equal(e, enr))
19984  {
19985  found = true;
19986  if (e != enr)
19987  redundant_enrs.push_back(e);
19988  }
19989 
19990  return found;
19991 }
19992 
19993 /// Check if an enumerator value is redundant in a given enum.
19994 ///
19995 /// Given an enumerator value, this function detects if an enum
19996 /// contains at least one enumerator with the the same value but with
19997 /// a different name.
19998 ///
19999 /// @param enr the enumerator to consider.
20000 ///
20001 /// @param enom the enum to consider.
20002 ///
20003 /// @return true iff @p enr is a redundant enumerator in enom.
20004 static bool
20005 is_enumerator_value_redundant(const enum_type_decl::enumerator &enr,
20006  const enum_type_decl &enom)
20007 {
20008  vector<enum_type_decl::enumerator> redundant_enrs;
20009  if (is_enumerator_value_present_in_enum(enr, enom, redundant_enrs))
20010  {
20011  if (!redundant_enrs.empty())
20012  return true;
20013  }
20014  return false;
20015 }
20016 
20017 /// Compares two instances of @ref enum_type_decl.
20018 ///
20019 /// If the two intances are different, set a bitfield to give some
20020 /// insight about the kind of differences there are.
20021 ///
20022 /// @param l the first artifact of the comparison.
20023 ///
20024 /// @param r the second artifact of the comparison.
20025 ///
20026 /// @param k a pointer to a bitfield that gives information about the
20027 /// kind of changes there are between @p l and @p r. This one is set
20028 /// iff @p k is non-null and the function returns false.
20029 ///
20030 /// Please note that setting k to a non-null value does have a
20031 /// negative performance impact because even if @p l and @p r are not
20032 /// equal, the function keeps up the comparison in order to determine
20033 /// the different kinds of ways in which they are different.
20034 ///
20035 /// @return true if @p l equals @p r, false otherwise.
20036 bool
20038 {
20039  bool result = true;
20040 
20041  //
20042  // Look through decl-only-enum.
20043  //
20044 
20045  const enum_type_decl *def1 =
20048  : &l;
20049 
20050  const enum_type_decl *def2 =
20053  : &r;
20054 
20055  if (!!def1 != !!def2)
20056  {
20057  // One enum is decl-only while the other is not.
20058  // So the two enums are different.
20059  result = false;
20060  if (k)
20061  *k |= SUBTYPE_CHANGE_KIND;
20062  else
20064  }
20065 
20066  //
20067  // At this point, both enums have the same state of decl-only-ness.
20068  // So we can compare oranges to oranges.
20069  //
20070 
20071  if (!def1)
20072  def1 = &l;
20073  if (!def2)
20074  def2 = &r;
20075 
20076  if (def1->get_underlying_type() != def2->get_underlying_type())
20077  {
20078  result = false;
20079  if (k)
20080  *k |= SUBTYPE_CHANGE_KIND;
20081  else
20083  }
20084 
20085  if (!(def1->decl_base::operator==(*def2)
20086  && def1->type_base::operator==(*def2)))
20087  {
20088  result = false;
20089  if (k)
20090  {
20091  if (!def1->decl_base::operator==(*def2))
20093  if (!def1->type_base::operator==(*def2))
20094  *k |= LOCAL_TYPE_CHANGE_KIND;
20095  }
20096  else
20098  }
20099 
20100  // Now compare the enumerators. Note that the order of declaration
20101  // of enumerators should not matter in the comparison.
20102  //
20103  // Also if an enumerator value is redundant, that shouldn't impact
20104  // the comparison.
20105  //
20106  // In that case, note that the two enums below are considered equal:
20107  //
20108  // enum foo
20109  // {
20110  // e0 = 0;
20111  // e1 = 1;
20112  // e2 = 2;
20113  // };
20114  //
20115  // enum foo
20116  // {
20117  // e0 = 0;
20118  // e1 = 1;
20119  // e2 = 2;
20120  // e_added = 1; // <-- this value is redundant with the value
20121  // // of the enumerator e1.
20122  // };
20123  //
20124  // Note however that in the case below, the enums are different.
20125  //
20126  // enum foo
20127  // {
20128  // e0 = 0;
20129  // e1 = 1;
20130  // };
20131  //
20132  // enum foo
20133  // {
20134  // e0 = 0;
20135  // e2 = 1; // <-- this enum value is present in the first version
20136  // // of foo, but is not redundant with any enumerator
20137  // // in the second version of of enum foo.
20138  // };
20139  //
20140  // These two enums are considered equal.
20141 
20142  for(const auto &e : def1->get_enumerators())
20143  if (!is_enumerator_present_in_enum(e, *def2)
20144  && (!is_enumerator_value_redundant(e, *def2)
20145  || !is_enumerator_value_redundant(e, *def1)))
20146  {
20147  result = false;
20148  if (k)
20149  {
20150  *k |= LOCAL_TYPE_CHANGE_KIND;
20151  break;
20152  }
20153  else
20155  }
20156 
20157  for(const auto &e : def2->get_enumerators())
20158  if (!is_enumerator_present_in_enum(e, *def1)
20159  && (!is_enumerator_value_redundant(e, *def1)
20160  || !is_enumerator_value_redundant(e, *def2)))
20161  {
20162  result = false;
20163  if (k)
20164  {
20165  *k |= LOCAL_TYPE_CHANGE_KIND;
20166  break;
20167  }
20168  else
20170  }
20171 
20172  ABG_RETURN(result);
20173 }
20174 
20175 /// Equality operator.
20176 ///
20177 /// @param o the other enum to test against.
20178 ///
20179 /// @return true iff @p o equals the current instance of enum type
20180 /// decl.
20181 bool
20183 {
20184  const enum_type_decl* op = dynamic_cast<const enum_type_decl*>(&o);
20185  if (!op)
20186  return false;
20187  return try_canonical_compare(this, op);
20188 }
20189 
20190 /// Equality operator.
20191 ///
20192 /// @param o the other enum to test against.
20193 ///
20194 /// @return true iff @p o is equals the current instance of enum type
20195 /// decl.
20196 bool
20198 {
20199  const decl_base* other = dynamic_cast<const decl_base*>(&o);
20200  if (!other)
20201  return false;
20202  return *this == *other;
20203 }
20204 
20205 /// Equality operator for @ref enum_type_decl_sptr.
20206 ///
20207 /// @param l the first operand to compare.
20208 ///
20209 /// @param r the second operand to compare.
20210 ///
20211 /// @return true iff @p l equals @p r.
20212 bool
20214 {
20215  if (!!l != !!r)
20216  return false;
20217  if (l.get() == r.get())
20218  return true;
20219  decl_base_sptr o = r;
20220  return *l == *o;
20221 }
20222 
20223 /// Inequality operator for @ref enum_type_decl_sptr.
20224 ///
20225 /// @param l the first operand to compare.
20226 ///
20227 /// @param r the second operand to compare.
20228 ///
20229 /// @return true iff @p l equals @p r.
20230 bool
20232 {return !operator==(l, r);}
20233 
20234 /// The type of the private data of an @ref
20235 /// enum_type_decl::enumerator.
20236 class enum_type_decl::enumerator::priv
20237 {
20238  string name_;
20239  int64_t value_;
20240  string qualified_name_;
20241  enum_type_decl* enum_type_;
20242 
20243  friend class enum_type_decl::enumerator;
20244 
20245 public:
20246 
20247  priv()
20248  : enum_type_()
20249  {}
20250 
20251  priv(const string& name,
20252  int64_t value,
20253  enum_type_decl* e = 0)
20254  : name_(name),
20255  value_(value),
20256  enum_type_(e)
20257  {}
20258 }; // end class enum_type_def::enumerator::priv
20259 
20260 /// Default constructor of the @ref enum_type_decl::enumerator type.
20262  : priv_(new priv)
20263 {}
20264 
20265 enum_type_decl::enumerator::~enumerator() = default;
20266 
20267 /// Constructor of the @ref enum_type_decl::enumerator type.
20268 ///
20269 /// @param env the environment we are operating from.
20270 ///
20271 /// @param name the name of the enumerator.
20272 ///
20273 /// @param value the value of the enumerator.
20275  int64_t value)
20276  : priv_(new priv(name, value))
20277 {}
20278 
20279 /// Copy constructor of the @ref enum_type_decl::enumerator type.
20280 ///
20281 /// @param other enumerator to copy.
20283  : priv_(new priv(other.get_name(),
20284  other.get_value(),
20285  other.get_enum_type()))
20286 {}
20287 
20288 /// Assignment operator of the @ref enum_type_decl::enumerator type.
20289 ///
20290 /// @param o
20293 {
20294  priv_->name_ = o.get_name();
20295  priv_->value_ = o.get_value();
20296  priv_->enum_type_ = o.get_enum_type();
20297  return *this;
20298 }
20299 
20300 /// Equality operator
20301 ///
20302 /// @param other the enumerator to compare to the current
20303 /// instance of enum_type_decl::enumerator.
20304 ///
20305 /// @return true if @p other equals the current instance of
20306 /// enum_type_decl::enumerator.
20307 bool
20309 {
20310  bool names_equal = true;
20311  names_equal = (get_name() == other.get_name());
20312  return names_equal && (get_value() == other.get_value());
20313 }
20314 
20315 /// Inequality operator.
20316 ///
20317 /// @param other the other instance to compare against.
20318 ///
20319 /// @return true iff @p other is different from the current instance.
20320 bool
20322 {return !operator==(other);}
20323 
20324 /// Getter for the name of the current instance of
20325 /// enum_type_decl::enumerator.
20326 ///
20327 /// @return a reference to the name of the current instance of
20328 /// enum_type_decl::enumerator.
20329 const string&
20331 {return priv_->name_;}
20332 
20333 /// Getter for the qualified name of the current instance of
20334 /// enum_type_decl::enumerator. The first invocation of the method
20335 /// builds the qualified name, caches it and return a reference to the
20336 /// cached qualified name. Subsequent invocations just return the
20337 /// cached value.
20338 ///
20339 /// @param internal set to true if the call is intended for an
20340 /// internal use (for technical use inside the library itself), false
20341 /// otherwise. If you don't know what this is for, then set it to
20342 /// false.
20343 ///
20344 /// @return the qualified name of the current instance of
20345 /// enum_type_decl::enumerator.
20346 const string&
20348 {
20349  if (priv_->qualified_name_.empty())
20350  {
20351  priv_->qualified_name_ =
20352  get_enum_type()->get_qualified_name(internal)
20353  + "::"
20354  + get_name();
20355  }
20356  return priv_->qualified_name_;
20357 }
20358 
20359 /// Setter for the name of @ref enum_type_decl::enumerator.
20360 ///
20361 /// @param n the new name.
20362 void
20364 {priv_->name_ = n;}
20365 
20366 /// Getter for the value of @ref enum_type_decl::enumerator.
20367 ///
20368 /// @return the value of the current instance of
20369 /// enum_type_decl::enumerator.
20370 int64_t
20372 {return priv_->value_;}
20373 
20374 /// Setter for the value of @ref enum_type_decl::enumerator.
20375 ///
20376 /// @param v the new value of the enum_type_decl::enumerator.
20377 void
20379 {priv_->value_= v;}
20380 
20381 /// Getter for the enum type that this enumerator is for.
20382 ///
20383 /// @return the enum type that this enumerator is for.
20386 {return priv_->enum_type_;}
20387 
20388 /// Setter for the enum type that this enumerator is for.
20389 ///
20390 /// @param e the new enum type.
20391 void
20393 {priv_->enum_type_ = e;}
20394 // </enum_type_decl definitions>
20395 
20396 // <typedef_decl definitions>
20397 
20398 /// Private data structure of the @ref typedef_decl.
20399 struct typedef_decl::priv
20400 {
20401  type_base_wptr underlying_type_;
20402 
20403  priv(const type_base_sptr& t)
20404  : underlying_type_(t)
20405  {}
20406 }; // end struct typedef_decl::priv
20407 
20408 /// Constructor of the typedef_decl type.
20409 ///
20410 /// @param name the name of the typedef.
20411 ///
20412 /// @param underlying_type the underlying type of the typedef.
20413 ///
20414 /// @param locus the source location of the typedef declaration.
20415 ///
20416 /// @param linkage_name the mangled name of the typedef.
20417 ///
20418 /// @param vis the visibility of the typedef type.
20419 typedef_decl::typedef_decl(const string& name,
20420  const type_base_sptr underlying_type,
20421  const location& locus,
20422  const string& linkage_name,
20423  visibility vis)
20424  : type_or_decl_base(underlying_type->get_environment(),
20425  TYPEDEF_TYPE
20426  | ABSTRACT_TYPE_BASE
20427  | ABSTRACT_DECL_BASE),
20428  type_base(underlying_type->get_environment(),
20429  underlying_type->get_size_in_bits(),
20430  underlying_type->get_alignment_in_bits()),
20431  decl_base(underlying_type->get_environment(),
20432  name, locus, linkage_name, vis),
20433  priv_(new priv(underlying_type))
20434 {
20435  runtime_type_instance(this);
20436 }
20437 
20438 /// Constructor of the typedef_decl type.
20439 ///
20440 /// @param name the name of the typedef.
20441 ///
20442 /// @param env the environment of the current typedef.
20443 ///
20444 /// @param locus the source location of the typedef declaration.
20445 ///
20446 /// @param mangled_name the mangled name of the typedef.
20447 ///
20448 /// @param vis the visibility of the typedef type.
20449 typedef_decl::typedef_decl(const string& name,
20450  const environment& env,
20451  const location& locus,
20452  const string& mangled_name,
20453  visibility vis)
20454  : type_or_decl_base(env,
20455  TYPEDEF_TYPE
20456  | ABSTRACT_TYPE_BASE
20457  | ABSTRACT_DECL_BASE),
20458  type_base(env, /*size_in_bits=*/0,
20459  /*alignment_in_bits=*/0),
20460  decl_base(env, name, locus, mangled_name, vis),
20461  priv_(new priv(nullptr))
20462 {
20463  runtime_type_instance(this);
20464 }
20465 
20466 /// Return the size of the typedef.
20467 ///
20468 /// This function looks at the size of the underlying type and ensures
20469 /// that it's the same as the size of the typedef.
20470 ///
20471 /// @return the size of the typedef.
20472 size_t
20474 {
20475  if (!get_underlying_type())
20476  return 0;
20477  size_t s = get_underlying_type()->get_size_in_bits();
20478  if (s != type_base::get_size_in_bits())
20479  const_cast<typedef_decl*>(this)->set_size_in_bits(s);
20480  return type_base::get_size_in_bits();
20481 }
20482 
20483 /// Return the alignment of the typedef.
20484 ///
20485 /// This function looks at the alignment of the underlying type and
20486 /// ensures that it's the same as the alignment of the typedef.
20487 ///
20488 /// @return the size of the typedef.
20489 size_t
20491 {
20492  if (!get_underlying_type())
20493  return 0;
20494  size_t s = get_underlying_type()->get_alignment_in_bits();
20496  const_cast<typedef_decl*>(this)->set_alignment_in_bits(s);
20498 }
20499 
20500 /// Compares two instances of @ref typedef_decl.
20501 ///
20502 /// If the two intances are different, set a bitfield to give some
20503 /// insight about the kind of differences there are.
20504 ///
20505 /// @param l the first artifact of the comparison.
20506 ///
20507 /// @param r the second artifact of the comparison.
20508 ///
20509 /// @param k a pointer to a bitfield that gives information about the
20510 /// kind of changes there are between @p l and @p r. This one is set
20511 /// iff @p k is non-null and the function returns false.
20512 ///
20513 /// Please note that setting k to a non-null value does have a
20514 /// negative performance impact because even if @p l and @p r are not
20515 /// equal, the function keeps up the comparison in order to determine
20516 /// the different kinds of ways in which they are different.
20517 ///
20518 /// @return true if @p l equals @p r, false otherwise.
20519 bool
20521 {
20522  bool result = true;
20523 
20524  // No need to go further if the types have different names or
20525  // different size / alignment.
20526  if (!(l.decl_base::operator==(r)))
20527  {
20528  result = false;
20529  if (k)
20530  *k |= LOCAL_TYPE_CHANGE_KIND;
20531  else
20533  }
20534 
20535  if (*l.get_underlying_type() != *r.get_underlying_type())
20536  {
20537  // Changes to the underlying type of a typedef are considered
20538  // local, a bit like for pointers.
20539  result = false;
20540  if (k)
20541  *k |= LOCAL_TYPE_CHANGE_KIND;
20542  else
20544  }
20545 
20546  ABG_RETURN(result);
20547 }
20548 
20549 /// Equality operator
20550 ///
20551 /// @param o the other typedef_decl to test against.
20552 bool
20554 {
20555  const typedef_decl* other = dynamic_cast<const typedef_decl*>(&o);
20556  if (!other)
20557  return false;
20558  return try_canonical_compare(this, other);
20559 }
20560 
20561 /// Equality operator
20562 ///
20563 /// @param o the other typedef_decl to test against.
20564 ///
20565 /// @return true if the current instance of @ref typedef_decl equals
20566 /// @p o.
20567 bool
20569 {
20570  const decl_base* other = dynamic_cast<const decl_base*>(&o);
20571  if (!other)
20572  return false;
20573  return *this == *other;
20574 }
20575 
20576 /// Build a pretty representation for a typedef_decl.
20577 ///
20578 /// @param internal set to true if the call is intended to get a
20579 /// representation of the decl (or type) for the purpose of canonical
20580 /// type comparison. This is mainly used in the function
20581 /// type_base::get_canonical_type_for().
20582 ///
20583 /// In other words if the argument for this parameter is true then the
20584 /// call is meant for internal use (for technical use inside the
20585 /// library itself), false otherwise. If you don't know what this is
20586 /// for, then set it to false.
20587 
20588 /// @param qualified_name if true, names emitted in the pretty
20589 /// representation are fully qualified.
20590 ///
20591 /// @return a copy of the pretty representation of the current
20592 /// instance of typedef_decl.
20593 string
20595  bool qualified_name) const
20596 {
20597 
20598  string result = "typedef ";
20599  if (internal)
20600  result += get_name();
20601  else
20602  {
20603  if (qualified_name)
20604  result += get_qualified_name(internal);
20605  else
20606  result += get_name();
20607  }
20608 
20609  return result;
20610 }
20611 
20612 /// Getter of the underlying type of the typedef.
20613 ///
20614 /// @return the underlying_type.
20615 type_base_sptr
20617 {return priv_->underlying_type_.lock();}
20618 
20619 /// Setter ofthe underlying type of the typedef.
20620 ///
20621 /// @param t the new underlying type of the typedef.
20622 void
20623 typedef_decl::set_underlying_type(const type_base_sptr& t)
20624 {
20625  priv_->underlying_type_ = t;
20626  set_size_in_bits(t->get_size_in_bits());
20627  set_alignment_in_bits(t->get_alignment_in_bits());
20628 }
20629 
20630 /// Implementation of the virtual "get_qualified_name" method.
20631 ///
20632 /// @param qualified_name the resuling qualified name of the typedef type.
20633 ///
20634 /// @param internal if true, then it means the qualified name is for
20635 /// "internal" purposes, meaning mainly for type canonicalization
20636 /// purposes.
20637 void
20639  bool internal) const
20640 {qualified_name = get_qualified_name(internal);}
20641 
20642 /// Implementation of the virtual "get_qualified_name" method.
20643 ///
20644 /// @param internal if true, then it means the qualified name is for
20645 /// "internal" purposes, meaning mainly for type canonicalization
20646 /// purposes.
20647 ///
20648 /// @return the qualified name.
20649 const interned_string&
20651 {
20652  // Note that the qualified name has been already set by
20653  // qualified_name_setter::do_update, which is invoked by
20654  // update_qualified_name. The latter is itself invoked whenever the
20655  // typedef is added to its scope, in scope_decl::add_member_decl.
20656  if (internal)
20657  return decl_base::priv_->internal_qualified_name_;
20658  else
20659  return decl_base::priv_->qualified_name_;
20660 }
20661 
20662 /// This implements the ir_traversable_base::traverse pure virtual
20663 /// function.
20664 ///
20665 /// @param v the visitor used on the current instance.
20666 ///
20667 /// @return true if the entire IR node tree got traversed, false
20668 /// otherwise.
20669 bool
20671 {
20672  if (v.type_node_has_been_visited(this))
20673  return true;
20674 
20675  if (visiting())
20676  return true;
20677 
20678  if (v.visit_begin(this))
20679  {
20680  visiting(true);
20681  if (type_base_sptr t = get_underlying_type())
20682  t->traverse(v);
20683  visiting(false);
20684  }
20685 
20686  bool result = v.visit_end(this);
20687  v.mark_type_node_as_visited(this);
20688  return result;
20689 }
20690 
20691 typedef_decl::~typedef_decl()
20692 {}
20693 // </typedef_decl definitions>
20694 
20695 // <var_decl definitions>
20696 
20697 struct var_decl::priv
20698 {
20699  type_base_wptr type_;
20700  type_base* naked_type_;
20701  decl_base::binding binding_;
20702  elf_symbol_sptr symbol_;
20703  interned_string id_;
20704 
20705  priv()
20706  : naked_type_(),
20707  binding_(decl_base::BINDING_GLOBAL)
20708  {}
20709 
20710  priv(type_base_sptr t,
20712  : type_(t),
20713  naked_type_(t.get()),
20714  binding_(b)
20715  {}
20716 
20717  /// Setter of the type of the variable.
20718  ///
20719  /// @param t the new variable type.
20720  void
20721  set_type(type_base_sptr t)
20722  {
20723  type_ = t;
20724  naked_type_ = t.get();
20725  }
20726 }; // end struct var_decl::priv
20727 
20728 /// Constructor of the @ref var_decl type.
20729 ///
20730 /// @param name the name of the variable declaration
20731 ///
20732 /// @param type the type of the variable declaration
20733 ///
20734 /// @param locus the source location where the variable was defined.
20735 ///
20736 /// @param linkage_name the linkage name of the variable.
20737 ///
20738 /// @param vis the visibility of of the variable.
20739 ///
20740 /// @param bind the binding kind of the variable.
20741 var_decl::var_decl(const string& name,
20742  type_base_sptr type,
20743  const location& locus,
20744  const string& linkage_name,
20745  visibility vis,
20746  binding bind)
20747  : type_or_decl_base(type->get_environment(),
20748  VAR_DECL | ABSTRACT_DECL_BASE),
20749  decl_base(type->get_environment(), name, locus, linkage_name, vis),
20750  priv_(new priv(type, bind))
20751 {
20752  runtime_type_instance(this);
20753 }
20754 
20755 /// Getter of the type of the variable.
20756 ///
20757 /// @return the type of the variable.
20758 const type_base_sptr
20760 {return priv_->type_.lock();}
20761 
20762 /// Setter of the type of the variable.
20763 ///
20764 /// @param the new type of the variable.
20765 void
20766 var_decl::set_type(type_base_sptr& t)
20767 {priv_->set_type(t);}
20768 
20769 /// Getter of the type of the variable.
20770 ///
20771 /// This getter returns a bare pointer, as opposed to a smart pointer.
20772 /// It's to be used on performance sensitive code paths identified by
20773 /// careful profiling.
20774 ///
20775 /// @return the type of the variable, as a bare pointer.
20776 const type_base*
20778 {return priv_->naked_type_;}
20779 
20780 /// Getter of the binding of the variable.
20781 ///
20782 /// @return the biding of the variable.
20785 {return priv_->binding_;}
20786 
20787 /// Setter of the binding of the variable.
20788 ///
20789 /// @param b the new binding value.
20790 void
20792 {priv_->binding_ = b;}
20793 
20794 /// Sets the underlying ELF symbol for the current variable.
20795 ///
20796 /// And underlyin$g ELF symbol for the current variable might exist
20797 /// only if the corpus that this variable originates from was
20798 /// constructed from an ELF binary file.
20799 ///
20800 /// Note that comparing two variables that have underlying ELF symbols
20801 /// involves comparing their underlying elf symbols. The decl name
20802 /// for the variable thus becomes irrelevant in the comparison.
20803 ///
20804 /// @param sym the new ELF symbol for this variable decl.
20805 void
20807 {
20808  priv_->symbol_ = sym;
20809  // The variable id cache that depends on the symbol must be
20810  // invalidated because the symbol changed.
20811  priv_->id_ = get_environment().intern("");
20812 }
20813 
20814 /// Gets the the underlying ELF symbol for the current variable,
20815 /// that was set using var_decl::set_symbol(). Please read the
20816 /// documentation for that member function for more information about
20817 /// "underlying ELF symbols".
20818 ///
20819 /// @return sym the underlying ELF symbol for this variable decl, if
20820 /// one exists.
20821 const elf_symbol_sptr&
20823 {return priv_->symbol_;}
20824 
20825 /// Create a new var_decl that is a clone of the current one.
20826 ///
20827 /// @return the cloned var_decl.
20830 {
20832  get_type(),
20833  get_location(),
20834  get_linkage_name(),
20835  get_visibility(),
20836  get_binding()));
20837 
20838  v->set_symbol(get_symbol());
20839 
20840  if (is_member_decl(*this))
20841  {
20845  get_member_is_static(*this),
20846  get_data_member_offset(*this));
20847  }
20848  else
20850 
20851  return v;
20852 }
20853 /// Setter of the scope of the current var_decl.
20854 ///
20855 /// Note that the decl won't hold a reference on the scope. It's
20856 /// rather the scope that holds a reference on its members.
20857 ///
20858 /// @param scope the new scope.
20859 void
20860 var_decl::set_scope(scope_decl* scope)
20861 {
20862  if (!get_context_rel())
20863  set_context_rel(new dm_context_rel(scope));
20864  else
20865  get_context_rel()->set_scope(scope);
20866 }
20867 
20868 /// Compares two instances of @ref var_decl without taking their type
20869 /// into account.
20870 ///
20871 /// If the two intances are different modulo their type, set a
20872 /// bitfield to give some insight about the kind of differences there
20873 /// are.
20874 ///
20875 /// @param l the first artifact of the comparison.
20876 ///
20877 /// @param r the second artifact of the comparison.
20878 ///
20879 /// @param k a pointer to a bitfield that gives information about the
20880 /// kind of changes there are between @p l and @p r. This one is set
20881 /// iff @p k is non-null and the function returns false.
20882 ///
20883 /// Please note that setting k to a non-null value does have a
20884 /// negative performance impact because even if @p l and @p r are not
20885 /// equal, the function keeps up the comparison in order to determine
20886 /// the different kinds of ways in which they are different.
20887 ///
20888 /// @return true if @p l equals @p r, false otherwise.
20889 bool
20891 {
20892  bool result = true;
20893 
20894  // If there are underlying elf symbols for these variables,
20895  // compare them. And then compare the other parts.
20896  const elf_symbol_sptr &s0 = l.get_symbol(), &s1 = r.get_symbol();
20897  if (!!s0 != !!s1)
20898  {
20899  result = false;
20900  if (k)
20902  else
20904  }
20905  else if (s0 && s0 != s1)
20906  {
20907  result = false;
20908  if (k)
20910  else
20912  }
20913  bool symbols_are_equal = (s0 && s1 && result);
20914 
20915  if (symbols_are_equal)
20916  {
20917  // The variables have underlying elf symbols that are equal, so
20918  // now, let's compare the decl_base part of the variables w/o
20919  // considering their decl names.
20920  const environment& env = l.get_environment();
20921  const interned_string n1 = l.get_qualified_name(), n2 = r.get_qualified_name();
20922  const_cast<var_decl&>(l).set_qualified_name(env.intern(""));
20923  const_cast<var_decl&>(r).set_qualified_name(env.intern(""));
20924  bool decl_bases_different = !l.decl_base::operator==(r);
20925  const_cast<var_decl&>(l).set_qualified_name(n1);
20926  const_cast<var_decl&>(r).set_qualified_name(n2);
20927 
20928  if (decl_bases_different)
20929  {
20930  result = false;
20931  if (k)
20933  else
20935  }
20936  }
20937  else
20938  if (!l.decl_base::operator==(r))
20939  {
20940  result = false;
20941  if (k)
20943  else
20945  }
20946 
20947  const dm_context_rel* c0 =
20948  dynamic_cast<const dm_context_rel*>(l.get_context_rel());
20949  const dm_context_rel* c1 =
20950  dynamic_cast<const dm_context_rel*>(r.get_context_rel());
20951  ABG_ASSERT(c0 && c1);
20952 
20953  if (*c0 != *c1)
20954  {
20955  result = false;
20956  if (k)
20958  else
20960  }
20961 
20962  ABG_RETURN(result);
20963 }
20964 
20965 /// Compares two instances of @ref var_decl.
20966 ///
20967 /// If the two intances are different, set a bitfield to give some
20968 /// insight about the kind of differences there are.
20969 ///
20970 /// @param l the first artifact of the comparison.
20971 ///
20972 /// @param r the second artifact of the comparison.
20973 ///
20974 /// @param k a pointer to a bitfield that gives information about the
20975 /// kind of changes there are between @p l and @p r. This one is set
20976 /// iff @p k is non-null and the function returns false.
20977 ///
20978 /// Please note that setting k to a non-null value does have a
20979 /// negative performance impact because even if @p l and @p r are not
20980 /// equal, the function keeps up the comparison in order to determine
20981 /// the different kinds of ways in which they are different.
20982 ///
20983 /// @return true if @p l equals @p r, false otherwise.
20984 bool
20985 equals(const var_decl& l, const var_decl& r, change_kind* k)
20986 {
20987  bool result = true;
20988 
20989  // First test types of variables. This should be fast because in
20990  // the general case, most types should be canonicalized.
20991  if (*l.get_naked_type() != *r.get_naked_type())
20992  {
20993  result = false;
20994  if (k)
20995  {
20997  r.get_naked_type()))
20998  *k |= (LOCAL_TYPE_CHANGE_KIND);
20999  else
21000  *k |= SUBTYPE_CHANGE_KIND;
21001  }
21002  else
21004  }
21005 
21006  result &= var_equals_modulo_types(l, r, k);
21007 
21008  ABG_RETURN(result);
21009 }
21010 
21011 /// Comparison operator of @ref var_decl.
21012 ///
21013 /// @param o the instance of @ref var_decl to compare against.
21014 ///
21015 /// @return true iff the current instance of @ref var_decl equals @p o.
21016 bool
21018 {
21019  const var_decl* other = dynamic_cast<const var_decl*>(&o);
21020  if (!other)
21021  return false;
21022 
21023  return equals(*this, *other, 0);
21024 }
21025 
21026 /// Return an ID that tries to uniquely identify the variable inside a
21027 /// program or a library.
21028 ///
21029 /// So if the variable has an underlying elf symbol, the ID is the
21030 /// concatenation of the symbol name and its version. Otherwise, the
21031 /// ID is the linkage name if its non-null. Otherwise, it's the
21032 /// pretty representation of the variable.
21033 ///
21034 /// @return the ID.
21037 {
21038  if (priv_->id_.empty())
21039  {
21040  string repr = get_name();
21041  string sym_str;
21042  if (elf_symbol_sptr s = get_symbol())
21043  sym_str = s->get_id_string();
21044  else if (!get_linkage_name().empty())
21045  sym_str = get_linkage_name();
21046 
21047  const environment& env = get_type()->get_environment();
21048  priv_->id_ = env.intern(repr);
21049  if (!sym_str.empty())
21050  priv_->id_ = env.intern(priv_->id_ + "{" + sym_str + "}");
21051  }
21052  return priv_->id_;
21053 }
21054 
21055 /// Return the hash value for the current instance.
21056 ///
21057 /// @return the hash value.
21058 size_t
21060 {
21061  var_decl::hash hash_var;
21062  return hash_var(this);
21063 }
21064 
21065 /// Get the qualified name of a given variable or data member.
21066 ///
21067 ///
21068 /// Note that if the current instance of @ref var_decl is an anonymous
21069 /// data member, then the qualified name is actually the flat
21070 /// representation (the definition) of the type of the anonymous data
21071 /// member. We chose the flat representation because otherwise, the
21072 /// name of an *anonymous* data member is empty, by construction, e.g:
21073 ///
21074 /// struct foo {
21075 /// int a;
21076 /// union {
21077 /// char b;
21078 /// char c;
21079 /// }; // <---- this data member is anonymous.
21080 /// int d;
21081 /// }
21082 ///
21083 /// The string returned for the anonymous member here is going to be:
21084 ///
21085 /// "union {char b; char c}"
21086 ///
21087 /// @param internal if true then this is for a purpose to the library,
21088 /// otherwise, it's for being displayed to users.
21089 ///
21090 /// @return the resulting qualified name.
21091 const interned_string&
21092 var_decl::get_qualified_name(bool internal) const
21093 {
21094  if (is_anonymous_data_member(this)
21095  && decl_base::get_qualified_name().empty())
21096  {
21097  // Display the anonymous data member in a way that makes sense.
21098  string r = get_pretty_representation(internal);
21099  set_qualified_name(get_environment().intern(r));
21100  }
21101 
21102  return decl_base::get_qualified_name(internal);
21103 }
21104 
21105 /// Build and return the pretty representation of this variable.
21106 ///
21107 /// @param internal set to true if the call is intended to get a
21108 /// representation of the decl (or type) for the purpose of canonical
21109 /// type comparison. This is mainly used in the function
21110 /// type_base::get_canonical_type_for().
21111 ///
21112 /// In other words if the argument for this parameter is true then the
21113 /// call is meant for internal use (for technical use inside the
21114 /// library itself), false otherwise. If you don't know what this is
21115 /// for, then set it to false.
21116 ///
21117 /// @param qualified_name if true, names emitted in the pretty
21118 /// representation are fully qualified.
21119 ///
21120 /// @return a copy of the pretty representation of this variable.
21121 string
21122 var_decl::get_pretty_representation(bool internal, bool qualified_name) const
21123 {
21124  string result;
21125 
21126  if (is_member_decl(this) && get_member_is_static(this))
21127  result = "static ";
21128 
21129  // Detect if the current instance of var_decl is a member of
21130  // an anonymous class or union.
21131  bool member_of_anonymous_class = false;
21132  if (class_or_union* scope = is_at_class_scope(this))
21133  if (scope->get_is_anonymous())
21134  member_of_anonymous_class = true;
21135 
21136  type_base_sptr type = get_type();
21137  if (is_array_type(type, /*look_through_qualifiers=*/true)
21138  || is_pointer_type(type, /*look_through_qualifiers=*/true)
21139  || is_reference_type(type, /*look_through_qualifiers=*/true)
21140  || is_ptr_to_mbr_type(type, /*look_through_qualifiers=*/true))
21141  {
21142  string name;
21143  if (member_of_anonymous_class || !qualified_name)
21144  name = get_name();
21145  else
21146  name = get_qualified_name(internal);
21147 
21148  if (qualified_type_def_sptr q = is_qualified_type(type))
21149  {
21150  string quals_repr =
21151  get_string_representation_of_cv_quals(q->get_cv_quals());
21152  if (!quals_repr.empty())
21153  name = quals_repr + " " + name;
21154  type = peel_qualified_type(type);
21155  }
21156 
21157  name = string(" ") + name;
21158  if (array_type_def_sptr t = is_array_type(type))
21159  result += array_declaration_name(t, name, qualified_name, internal);
21160  else if (pointer_type_def_sptr t = is_pointer_type(type))
21161  result += pointer_declaration_name(t, name, qualified_name, internal);
21162  else if (reference_type_def_sptr t = is_reference_type(type))
21163  result += pointer_declaration_name(t, name, qualified_name, internal);
21164  else if (ptr_to_mbr_type_sptr t = is_ptr_to_mbr_type(type))
21165  result += ptr_to_mbr_declaration_name(t, name,
21166  qualified_name,
21167  internal);
21168  }
21169  else
21170  {
21171  if (/*The current var_decl is to be used as an anonymous data
21172  member. */
21173  get_name().empty())
21174  {
21175  // Display the anonymous data member in a way that
21176  // makes sense.
21177  result +=
21180  "", /*one_line=*/true, internal);
21181  }
21182  else if (data_member_has_anonymous_type(this))
21183  {
21186  "", /*one_line=*/true, internal);
21187  result += " ";
21188  if (!internal
21189  && (member_of_anonymous_class || !qualified_name))
21190  // It doesn't make sense to name the member of an
21191  // anonymous class or union like:
21192  // "__anonymous__::data_member_name". So let's just use
21193  // its non-qualified name.
21194  result += get_name();
21195  else
21196  result += get_qualified_name(internal);
21197  }
21198  else
21199  {
21200  result +=
21202  + " ";
21203 
21204  if (!internal
21205  && (member_of_anonymous_class || !qualified_name))
21206  // It doesn't make sense to name the member of an
21207  // anonymous class or union like:
21208  // "__anonymous__::data_member_name". So let's just use
21209  // its non-qualified name.
21210  result += get_name();
21211  else
21212  result += get_qualified_name(internal);
21213  }
21214  }
21215  return result;
21216 }
21217 
21218 /// Get a name that is valid even for an anonymous data member.
21219 ///
21220 /// If the current @ref var_decl is an anonymous data member, then
21221 /// return its pretty representation. As of now, that pretty
21222 /// representation is actually its flat representation as returned by
21223 /// get_class_or_union_flat_representation().
21224 ///
21225 /// Otherwise, just return the name of the current @ref var_decl.
21226 ///
21227 /// @param qualified if true, return the qualified name. This doesn't
21228 /// have an effet if the current @ref var_decl represents an anonymous
21229 /// data member.
21230 string
21232 {
21233  string name;
21234  if (is_anonymous_data_member(this))
21235  // This function is used in the comparison engine to determine
21236  // which anonymous data member was deleted. So it's not involved
21237  // in type comparison or canonicalization. We don't want to use
21238  // the 'internal' version of the pretty presentation.
21239  name = get_pretty_representation(/*internal=*/false, qualified);
21240  else
21241  name = get_name();
21242 
21243  return name;
21244 }
21245 
21246 /// This implements the ir_traversable_base::traverse pure virtual
21247 /// function.
21248 ///
21249 /// @param v the visitor used on the current instance.
21250 ///
21251 /// @return true if the entire IR node tree got traversed, false
21252 /// otherwise.
21253 bool
21255 {
21256  if (visiting())
21257  return true;
21258 
21259  if (v.visit_begin(this))
21260  {
21261  visiting(true);
21262  if (type_base_sptr t = get_type())
21263  t->traverse(v);
21264  visiting(false);
21265  }
21266  return v.visit_end(this);
21267 }
21268 
21269 var_decl::~var_decl()
21270 {}
21271 
21272 // </var_decl definitions>
21273 
21274 /// This function is automatically invoked whenever an instance of
21275 /// this type is canonicalized.
21276 ///
21277 /// It's an overload of the virtual type_base::on_canonical_type_set.
21278 ///
21279 /// We put here what is thus meant to be executed only at the point of
21280 /// type canonicalization.
21281 void
21283 {
21284  priv_->cached_name_.clear();
21285  priv_->internal_cached_name_.clear();
21286 }
21287 
21288 /// The most straightforward constructor for the function_type class.
21289 ///
21290 /// @param return_type the return type of the function type.
21291 ///
21292 /// @param parms the list of parameters of the function type.
21293 /// Stricto sensu, we just need a list of types; we are using a list
21294 /// of parameters (where each parameter also carries the name of the
21295 /// parameter and its source location) to try and provide better
21296 /// diagnostics whenever it makes sense. If it appears that this
21297 /// wasts too many resources, we can fall back to taking just a
21298 /// vector of types here.
21299 ///
21300 /// @param size_in_bits the size of this type, in bits.
21301 ///
21302 /// @param alignment_in_bits the alignment of this type, in bits.
21303 ///
21304 /// @param size_in_bits the size of this type.
21305 function_type::function_type(type_base_sptr return_type,
21306  const parameters& parms,
21307  size_t size_in_bits,
21308  size_t alignment_in_bits)
21309  : type_or_decl_base(return_type->get_environment(),
21310  FUNCTION_TYPE | ABSTRACT_TYPE_BASE),
21311  type_base(return_type->get_environment(), size_in_bits, alignment_in_bits),
21312  priv_(new priv(parms, return_type))
21313 {
21314  runtime_type_instance(this);
21315 
21316  for (parameters::size_type i = 0, j = 1;
21317  i < priv_->parms_.size();
21318  ++i, ++j)
21319  {
21320  if (i == 0 && priv_->parms_[i]->get_is_artificial())
21321  // If the first parameter is artificial, then it certainly
21322  // means that this is a member function, and the first
21323  // parameter is the implicit this pointer. In that case, set
21324  // the index of that implicit parameter to zero. Otherwise,
21325  // the index of the first parameter starts at one.
21326  j = 0;
21327  priv_->parms_[i]->set_index(j);
21328  }
21329 }
21330 
21331 /// A constructor for a function_type that takes no parameters.
21332 ///
21333 /// @param return_type the return type of this function_type.
21334 ///
21335 /// @param size_in_bits the size of this type, in bits.
21336 ///
21337 /// @param alignment_in_bits the alignment of this type, in bits.
21338 function_type::function_type(type_base_sptr return_type,
21339  size_t size_in_bits, size_t alignment_in_bits)
21340  : type_or_decl_base(return_type->get_environment(),
21341  FUNCTION_TYPE | ABSTRACT_TYPE_BASE),
21342  type_base(return_type->get_environment(), size_in_bits, alignment_in_bits),
21343  priv_(new priv(return_type))
21344 {
21345  runtime_type_instance(this);
21346 }
21347 
21348 /// A constructor for a function_type that takes no parameter and
21349 /// that has no return_type yet. These missing parts can (and must)
21350 /// be added later.
21351 ///
21352 /// @param env the environment we are operating from.
21353 ///
21354 /// @param size_in_bits the size of this type, in bits.
21355 ///
21356 /// @param alignment_in_bits the alignment of this type, in bits.
21357 function_type::function_type(const environment& env,
21358  size_t size_in_bits,
21359  size_t alignment_in_bits)
21360  : type_or_decl_base(env, FUNCTION_TYPE | ABSTRACT_TYPE_BASE),
21361  type_base(env, size_in_bits, alignment_in_bits),
21362  priv_(new priv)
21363 {
21364  runtime_type_instance(this);
21365 }
21366 
21367 /// Getter for the return type of the current instance of @ref
21368 /// function_type.
21369 ///
21370 /// @return the return type.
21371 type_base_sptr
21373 {return priv_->return_type_.lock();}
21374 
21375 /// Setter of the return type of the current instance of @ref
21376 /// function_type.
21377 ///
21378 /// @param t the new return type to set.
21379 void
21381 {priv_->return_type_ = t;}
21382 
21383 /// Getter for the set of parameters of the current intance of @ref
21384 /// function_type.
21385 ///
21386 /// @return the parameters of the current instance of @ref
21387 /// function_type.
21390 {return priv_->parms_;}
21391 
21392 /// Get the Ith parameter of the vector of parameters of the current
21393 /// instance of @ref function_type.
21394 ///
21395 /// Note that the first parameter is at index 0. That parameter is
21396 /// the first parameter that comes after the possible implicit "this"
21397 /// parameter, when the current instance @ref function_type is for a
21398 /// member function. Otherwise, if the current instance of @ref
21399 /// function_type is for a non-member function, the parameter at index
21400 /// 0 is the first parameter of the function.
21401 ///
21402 ///
21403 /// @param i the index of the parameter to return. If i is greater
21404 /// than the index of the last parameter, then this function returns
21405 /// an empty parameter (smart) pointer.
21406 ///
21407 /// @return the @p i th parameter that is not implicit.
21410 {
21411  parameter_sptr result;
21412  if (dynamic_cast<const method_type*>(this))
21413  {
21414  if (i + 1 < get_parameters().size())
21415  result = get_parameters()[i + 1];
21416  }
21417  else
21418  {
21419  if (i < get_parameters().size())
21420  result = get_parameters()[i];
21421  }
21422  return result;
21423 }
21424 
21425 /// Setter for the parameters of the current instance of @ref
21426 /// function_type.
21427 ///
21428 /// @param p the new vector of parameters to set.
21429 void
21431 {
21432  priv_->parms_ = p;
21433  for (parameters::size_type i = 0, j = 1;
21434  i < priv_->parms_.size();
21435  ++i, ++j)
21436  {
21437  if (i == 0 && priv_->parms_[i]->get_is_artificial())
21438  // If the first parameter is artificial, then it certainly
21439  // means that this is a member function, and the first
21440  // parameter is the implicit this pointer. In that case, set
21441  // the index of that implicit parameter to zero. Otherwise,
21442  // the index of the first parameter starts at one.
21443  j = 0;
21444  priv_->parms_[i]->set_index(j);
21445  }
21446 }
21447 
21448 /// Append a new parameter to the vector of parameters of the current
21449 /// instance of @ref function_type.
21450 ///
21451 /// @param parm the parameter to append.
21452 void
21454 {
21455  parm->set_index(priv_->parms_.size());
21456  priv_->parms_.push_back(parm);
21457 }
21458 
21459 /// Test if the current instance of @ref function_type is for a
21460 /// variadic function.
21461 ///
21462 /// A variadic function is a function that takes a variable number of
21463 /// arguments.
21464 ///
21465 /// @return true iff the current instance of @ref function_type is for
21466 /// a variadic function.
21467 bool
21469 {
21470  return (!priv_->parms_.empty()
21471  && priv_->parms_.back()->get_variadic_marker());
21472 }
21473 
21474 /// Compare two function types.
21475 ///
21476 /// In case these function types are actually method types, this
21477 /// function avoids comparing two parameters (of the function types)
21478 /// if the types of the parameters are actually the types of the
21479 /// classes of the method types. This prevents infinite recursion
21480 /// during the comparison of two classes that are structurally
21481 /// identical.
21482 ///
21483 /// This is a subroutine of the equality operator of function_type.
21484 ///
21485 /// @param lhs the first function type to consider
21486 ///
21487 /// @param rhs the second function type to consider
21488 ///
21489 /// @param k a pointer to a bitfield set by the function to give
21490 /// information about the kind of changes carried by @p lhs and @p
21491 /// rhs. It is set iff @p k is non-null and the function returns
21492 /// false.
21493 ///
21494 /// Please note that setting k to a non-null value does have a
21495 /// negative performance impact because even if @p l and @p r are not
21496 /// equal, the function keeps up the comparison in order to determine
21497 /// the different kinds of ways in which they are different.
21498 ///
21499 ///@return true if lhs == rhs, false otherwise.
21500 bool
21502 {
21503 #define RETURN(value) CACHE_AND_RETURN_COMPARISON_RESULT(value)
21504 
21506 
21507  {
21508  // First of all, let's see if these two function types haven't
21509  // already been compared. If so, and if the result of the
21510  // comparison has been cached, let's just re-use it, rather than
21511  // comparing them all over again.
21512  bool cached_result = false;
21513  if (l.get_environment().priv_->is_type_comparison_cached(l, r,
21514  cached_result))
21515  ABG_RETURN(cached_result);
21516  }
21517 
21519 
21520  bool result = true;
21521 
21522  if (!l.type_base::operator==(r))
21523  {
21524  result = false;
21525  if (k)
21526  *k |= LOCAL_TYPE_CHANGE_KIND;
21527  else
21528  RETURN(result);
21529  }
21530 
21531  class_or_union* l_class = 0, *r_class = 0;
21532  if (const method_type* m = dynamic_cast<const method_type*>(&l))
21533  l_class = m->get_class_type().get();
21534 
21535  if (const method_type* m = dynamic_cast<const method_type*>(&r))
21536  r_class = m->get_class_type().get();
21537 
21538  // Compare the names of the class of the method
21539 
21540  if (!!l_class != !!r_class)
21541  {
21542  result = false;
21543  if (k)
21544  *k |= LOCAL_TYPE_CHANGE_KIND;
21545  else
21546  RETURN(result);
21547  }
21548  else if (l_class
21549  && (l_class->get_qualified_name()
21550  != r_class->get_qualified_name()))
21551  {
21552  result = false;
21553  if (k)
21554  *k |= LOCAL_TYPE_CHANGE_KIND;
21555  else
21556  RETURN(result);
21557  }
21558 
21559  // Then compare the return type; Beware if it's t's a class type
21560  // that is the same as the method class name; we can recurse for
21561  // ever in that case.
21562 
21563  decl_base* l_return_type_decl =
21565  decl_base* r_return_type_decl =
21567  bool compare_result_types = true;
21568  string l_rt_name = l_return_type_decl
21569  ? l_return_type_decl->get_qualified_name()
21570  : string();
21571  string r_rt_name = r_return_type_decl
21572  ? r_return_type_decl->get_qualified_name()
21573  : string();
21574 
21575  if ((l_class && (l_class->get_qualified_name() == l_rt_name))
21576  ||
21577  (r_class && (r_class->get_qualified_name() == r_rt_name)))
21578  compare_result_types = false;
21579 
21580  if (compare_result_types)
21581  {
21582  // Let's not consider typedefs when comparing return types to
21583  // avoid spurious changes.
21584  //
21585  // TODO: We should also do this for parameter types, or rather,
21586  // we should teach the equality operators in the IR, at some
21587  // point, to peel typedefs off.
21589  !=
21591  {
21592  result = false;
21593  if (k)
21594  {
21596  r.get_return_type()))
21597  *k |= LOCAL_TYPE_CHANGE_KIND;
21598  else
21599  *k |= SUBTYPE_CHANGE_KIND;
21600  }
21601  else
21602  RETURN(result);
21603  }
21604  }
21605  else
21606  if (l_rt_name != r_rt_name)
21607  {
21608  result = false;
21609  if (k)
21610  *k |= SUBTYPE_CHANGE_KIND;
21611  else
21612  RETURN(result);
21613  }
21614 
21615  vector<shared_ptr<function_decl::parameter> >::const_iterator i,j;
21616  for (i = l.get_first_parm(), j = r.get_first_parm();
21617  i != l.get_parameters().end() && j != r.get_parameters().end();
21618  ++i, ++j)
21619  {
21620  if (**i != **j)
21621  {
21622  result = false;
21623  if (k)
21624  {
21625  if (!types_have_similar_structure((*i)->get_type(),
21626  (*j)->get_type()))
21627  *k |= LOCAL_TYPE_CHANGE_KIND;
21628  else
21629  *k |= SUBTYPE_CHANGE_KIND;
21630  }
21631  else
21632  RETURN(result);
21633  }
21634  }
21635 
21636  if ((i != l.get_parameters().end()
21637  || j != r.get_parameters().end()))
21638  {
21639  result = false;
21640  if (k)
21642  else
21643  RETURN(result);
21644  }
21645 
21646  RETURN(result);
21647 #undef RETURN
21648 }
21649 
21650 /// Get the first parameter of the function.
21651 ///
21652 /// If the function is a non-static member function, the parameter
21653 /// returned is the first one following the implicit 'this' parameter.
21654 ///
21655 /// @return the first non implicit parameter of the function.
21656 function_type::parameters::const_iterator
21658 {
21659  if (get_parameters().empty())
21660  return get_parameters().end();
21661 
21662  bool is_method = dynamic_cast<const method_type*>(this);
21663 
21664  parameters::const_iterator i = get_parameters().begin();
21665 
21666  if (is_method)
21667  ++i;
21668 
21669  return i;
21670 }
21671 
21672 /// Get the first parameter of the function.
21673 ///
21674 /// Note that if the function is a non-static member function, the
21675 /// parameter returned is the implicit 'this' parameter.
21676 ///
21677 /// @return the first parameter of the function.
21678 function_type::parameters::const_iterator
21680 {return get_parameters().begin();}
21681 
21682 /// Get the name of the current @ref function_type.
21683 ///
21684 /// The name is retrieved from a cache. If the cache is empty, this
21685 /// function computes the name of the type, stores it in the cache and
21686 /// returns it. Subsequent invocation of the function are going to
21687 /// just hit the cache.
21688 ///
21689 /// Note that if the type is *NOT* canonicalized then function type
21690 /// name is never cached.
21691 ///
21692 /// @param internal if true then it means the function type name is
21693 /// going to be used for purposes that are internal to libabigail
21694 /// itself. If you don't know what this is then you probably should
21695 /// set this parameter to 'false'.
21696 ///
21697 /// @return the name of the function type.
21698 const interned_string&
21700 {
21701  if (internal)
21702  {
21704  {
21705  if (priv_->internal_cached_name_.empty())
21706  priv_->internal_cached_name_ =
21707  get_function_type_name(this, /*internal=*/true);
21708  return priv_->internal_cached_name_;
21709  }
21710  else
21711  {
21712  if (priv_->temp_internal_cached_name_.empty())
21713  priv_->temp_internal_cached_name_ =
21714  get_function_type_name(this, /*internal=*/true);
21715  return priv_->temp_internal_cached_name_;
21716  }
21717  }
21718  else
21719  {
21721  {
21722  if (priv_->cached_name_.empty())
21723  priv_->cached_name_ =
21724  get_function_type_name(this, /*internal=*/false);
21725  return priv_->cached_name_;
21726  }
21727  else
21728  {
21729  priv_->cached_name_ =
21730  get_function_type_name(this, /*internal=*/false);
21731  return priv_->cached_name_;
21732  }
21733  }
21734 }
21735 
21736 /// Equality operator for function_type.
21737 ///
21738 /// @param o the other function_type to compare against.
21739 ///
21740 /// @return true iff the two function_type are equal.
21741 bool
21743 {
21744  const function_type* o = dynamic_cast<const function_type*>(&other);
21745  if (!o)
21746  return false;
21747  return try_canonical_compare(this, o);
21748 }
21749 
21750 /// Return a copy of the pretty representation of the current @ref
21751 /// function_type.
21752 ///
21753 /// @param internal set to true if the call is intended to get a
21754 /// representation of the decl (or type) for the purpose of canonical
21755 /// type comparison. This is mainly used in the function
21756 /// type_base::get_canonical_type_for().
21757 ///
21758 /// In other words if the argument for this parameter is true then the
21759 /// call is meant for internal use (for technical use inside the
21760 /// library itself), false otherwise. If you don't know what this is
21761 /// for, then set it to false.
21762 ///
21763 /// @return a copy of the pretty representation of the current @ref
21764 /// function_type.
21765 string
21767  bool /*qualified_name*/) const
21768 {return ir::get_pretty_representation(this, internal);}
21769 
21770 /// Traverses an instance of @ref function_type, visiting all the
21771 /// sub-types and decls that it might contain.
21772 ///
21773 /// @param v the visitor that is used to visit every IR sub-node of
21774 /// the current node.
21775 ///
21776 /// @return true if either
21777 /// - all the children nodes of the current IR node were traversed
21778 /// and the calling code should keep going with the traversing.
21779 /// - or the current IR node is already being traversed.
21780 /// Otherwise, returning false means that the calling code should not
21781 /// keep traversing the tree.
21782 bool
21784 {
21785  // TODO: should we allow the walker to avoid visiting function type
21786  // twice? I think that if we do, then ir_node_visitor needs an
21787  // option to specifically disallow this feature for function types.
21788 
21789  if (visiting())
21790  return true;
21791 
21792  if (v.visit_begin(this))
21793  {
21794  visiting(true);
21795  bool keep_going = true;
21796 
21797  if (type_base_sptr t = get_return_type())
21798  {
21799  if (!t->traverse(v))
21800  keep_going = false;
21801  }
21802 
21803  if (keep_going)
21804  for (parameters::const_iterator i = get_parameters().begin();
21805  i != get_parameters().end();
21806  ++i)
21807  if (type_base_sptr parm_type = (*i)->get_type())
21808  if (!parm_type->traverse(v))
21809  break;
21810 
21811  visiting(false);
21812  }
21813  return v.visit_end(this);
21814 }
21815 
21816 function_type::~function_type()
21817 {}
21818 // </function_type>
21819 
21820 // <method_type>
21821 
21822 struct method_type::priv
21823 {
21824  class_or_union_wptr class_type_;
21825  bool is_const;
21826 
21827  priv()
21828  : is_const()
21829  {}
21830 }; // end struct method_type::priv
21831 
21832 /// Constructor for instances of method_type.
21833 ///
21834 /// Instances of method_decl must be of type method_type.
21835 ///
21836 /// @param return_type the type of the return value of the method.
21837 ///
21838 /// @param class_type the base type of the method type. That is, the
21839 /// type of the class the method belongs to.
21840 ///
21841 /// @param p the vector of the parameters of the method.
21842 ///
21843 /// @param is_const whether this method type is for a const method.
21844 /// Note that const-ness is a property of the method *type* and of the
21845 /// relationship between a method *declaration* and its scope.
21846 ///
21847 /// @param size_in_bits the size of an instance of method_type,
21848 /// expressed in bits.
21849 ///
21850 /// @param alignment_in_bits the alignment of an instance of
21851 /// method_type, expressed in bits.
21852 method_type::method_type (type_base_sptr return_type,
21853  class_or_union_sptr class_type,
21854  const std::vector<function_decl::parameter_sptr>& p,
21855  bool is_const,
21856  size_t size_in_bits,
21857  size_t alignment_in_bits)
21858  : type_or_decl_base(class_type->get_environment(),
21859  METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
21860  type_base(class_type->get_environment(), size_in_bits, alignment_in_bits),
21861  function_type(return_type, p, size_in_bits, alignment_in_bits),
21862  priv_(new priv)
21863 {
21864  runtime_type_instance(this);
21865  set_class_type(class_type);
21866  set_is_const(is_const);
21867 }
21868 
21869 /// Constructor of instances of method_type.
21870 ///
21871 ///Instances of method_decl must be of type method_type.
21872 ///
21873 /// @param return_type the type of the return value of the method.
21874 ///
21875 /// @param class_type the type of the class the method belongs to.
21876 /// The actual (dynamic) type of class_type must be a pointer
21877 /// class_type. We are setting it to pointer to type_base here to
21878 /// help client code that is compiled without rtti and thus cannot
21879 /// perform dynamic casts.
21880 ///
21881 /// @param p the vector of the parameters of the method type.
21882 ///
21883 /// @param is_const whether this method type is for a const method.
21884 /// Note that const-ness is a property of the method *type* and of the
21885 /// relationship between a method *declaration* and its scope.
21886 ///
21887 /// @param size_in_bits the size of an instance of method_type,
21888 /// expressed in bits.
21889 ///
21890 /// @param alignment_in_bits the alignment of an instance of
21891 /// method_type, expressed in bits.
21892 method_type::method_type(type_base_sptr return_type,
21893  type_base_sptr class_type,
21894  const std::vector<function_decl::parameter_sptr>& p,
21895  bool is_const,
21896  size_t size_in_bits,
21897  size_t alignment_in_bits)
21898  : type_or_decl_base(class_type->get_environment(),
21899  METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
21900  type_base(class_type->get_environment(), size_in_bits, alignment_in_bits),
21901  function_type(return_type, p, size_in_bits, alignment_in_bits),
21902  priv_(new priv)
21903 {
21904  runtime_type_instance(this);
21905  set_class_type(is_class_type(class_type));
21906  set_is_const(is_const);
21907 }
21908 
21909 /// Constructor of the qualified_type_def
21910 ///
21911 /// @param env the environment we are operating from.
21912 ///
21913 /// @param size_in_bits the size of the type, expressed in bits.
21914 ///
21915 /// @param alignment_in_bits the alignment of the type, expressed in bits
21916 method_type::method_type(const environment& env,
21917  size_t size_in_bits,
21918  size_t alignment_in_bits)
21919  : type_or_decl_base(env, METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
21920  type_base(env, size_in_bits, alignment_in_bits),
21921  function_type(env, size_in_bits, alignment_in_bits),
21922  priv_(new priv)
21923 {
21924  runtime_type_instance(this);
21925 }
21926 
21927 /// Constructor of instances of method_type.
21928 ///
21929 /// When constructed with this constructor, and instane of method_type
21930 /// must set a return type using method_type::set_return_type
21931 ///
21932 /// @param class_typ the base type of the method type. That is, the
21933 /// type of the class (or union) the method belongs to.
21934 ///
21935 /// @param size_in_bits the size of an instance of method_type,
21936 /// expressed in bits.
21937 ///
21938 /// @param alignment_in_bits the alignment of an instance of
21939 /// method_type, expressed in bits.
21940 method_type::method_type(class_or_union_sptr class_type,
21941  bool is_const,
21942  size_t size_in_bits,
21943  size_t alignment_in_bits)
21944  : type_or_decl_base(class_type->get_environment(),
21945  METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
21946  type_base(class_type->get_environment(), size_in_bits, alignment_in_bits),
21947  function_type(class_type->get_environment(),
21948  size_in_bits,
21949  alignment_in_bits),
21950  priv_(new priv)
21951 {
21952  runtime_type_instance(this);
21953  set_class_type(class_type);
21954  set_is_const(is_const);
21955 }
21956 
21957 /// Get the class type this method belongs to.
21958 ///
21959 /// @return the class type.
21960 class_or_union_sptr
21962 {return class_or_union_sptr(priv_->class_type_);}
21963 
21964 /// Sets the class type of the current instance of method_type.
21965 ///
21966 /// The class type is the type of the class the method belongs to.
21967 ///
21968 /// @param t the new class type to set.
21969 void
21970 method_type::set_class_type(const class_or_union_sptr& t)
21971 {
21972  if (!t)
21973  return;
21974 
21975  priv_->class_type_ = t;
21976 }
21977 
21978 /// Return a copy of the pretty representation of the current @ref
21979 /// method_type.
21980 ///
21981 /// @param internal set to true if the call is intended to get a
21982 /// representation of the decl (or type) for the purpose of canonical
21983 /// type comparison. This is mainly used in the function
21984 /// type_base::get_canonical_type_for().
21985 ///
21986 /// In other words if the argument for this parameter is true then the
21987 /// call is meant for internal use (for technical use inside the
21988 /// library itself), false otherwise. If you don't know what this is
21989 /// for, then set it to false.
21990 ///
21991 /// @return a copy of the pretty representation of the current @ref
21992 /// method_type.
21993 string
21995  bool /*qualified_name*/) const
21996 {return ir::get_pretty_representation(*this, internal);}
21997 
21998 /// Setter of the "is-const" property of @ref method_type.
21999 ///
22000 /// @param the new value of the "is-const" property.
22001 void
22003 {priv_->is_const = f;}
22004 
22005 /// Getter of the "is-const" property of @ref method_type.
22006 ///
22007 /// @return true iff the "is-const" property was set.
22008 bool
22010 {return priv_->is_const;}
22011 
22012 /// The destructor of method_type
22014 {}
22015 
22016 // </method_type>
22017 
22018 // <function_decl definitions>
22019 
22020 struct function_decl::priv
22021 {
22022  bool declared_inline_;
22023  decl_base::binding binding_;
22024  function_type_wptr type_;
22025  function_type* naked_type_;
22026  elf_symbol_sptr symbol_;
22027  interned_string id_;
22028 
22029  priv()
22030  : declared_inline_(false),
22031  binding_(decl_base::BINDING_GLOBAL),
22032  naked_type_()
22033  {}
22034 
22035  priv(function_type_sptr t,
22036  bool declared_inline,
22038  : declared_inline_(declared_inline),
22039  binding_(binding),
22040  type_(t),
22041  naked_type_(t.get())
22042  {}
22043 
22044  priv(function_type_sptr t,
22045  bool declared_inline,
22047  elf_symbol_sptr s)
22048  : declared_inline_(declared_inline),
22049  binding_(binding),
22050  type_(t),
22051  naked_type_(t.get()),
22052  symbol_(s)
22053  {}
22054 }; // end sruct function_decl::priv
22055 
22056 /// Constructor of the @ref function_decl.
22057 ///
22058 /// @param name the name of the function.
22059 ///
22060 /// @param function_type the type of the function.
22061 ///
22062 /// @param declared_inline wether the function is declared inline.
22063 ///
22064 /// @param locus the source location of the function.
22065 ///
22066 /// @param mangled_name the linkage name of the function.
22067 ///
22068 /// @param vis the visibility of the function.
22069 ///
22070 /// @param bind the binding of the function.
22071 function_decl::function_decl(const string& name,
22073  bool declared_inline,
22074  const location& locus,
22075  const string& mangled_name,
22076  visibility vis,
22077  binding bind)
22078  : type_or_decl_base(function_type->get_environment(),
22079  FUNCTION_DECL | ABSTRACT_DECL_BASE),
22080  decl_base(function_type->get_environment(), name, locus, mangled_name, vis),
22081  priv_(new priv(function_type, declared_inline, bind))
22082 {
22083  runtime_type_instance(this);
22084 }
22085 
22086 /// Constructor of the function_decl type.
22087 ///
22088 /// This flavour of constructor is for when the pointer to the
22089 /// instance of function_type that the client code has is presented as
22090 /// a pointer to type_base. In that case, this constructor saves the
22091 /// client code from doing a dynamic_cast to get the function_type
22092 /// pointer.
22093 ///
22094 /// @param name the name of the function declaration.
22095 ///
22096 /// @param fn_type the type of the function declaration. The dynamic
22097 /// type of this parameter should be 'pointer to function_type'
22098 ///
22099 /// @param declared_inline whether this function was declared inline
22100 ///
22101 /// @param locus the source location of the function declaration.
22102 ///
22103 /// @param linkage_name the mangled name of the function declaration.
22104 ///
22105 /// @param vis the visibility of the function declaration.
22106 ///
22107 /// @param bind the kind of the binding of the function
22108 /// declaration.
22109 function_decl::function_decl(const string& name,
22110  type_base_sptr fn_type,
22111  bool declared_inline,
22112  const location& locus,
22113  const string& linkage_name,
22114  visibility vis,
22115  binding bind)
22116  : type_or_decl_base(fn_type->get_environment(),
22117  FUNCTION_DECL | ABSTRACT_DECL_BASE),
22118  decl_base(fn_type->get_environment(), name, locus, linkage_name, vis),
22119  priv_(new priv(dynamic_pointer_cast<function_type>(fn_type),
22120  declared_inline,
22121  bind))
22122 {
22123  runtime_type_instance(this);
22124 }
22125 
22126 /// Get the pretty representation of the current instance of @ref function_decl.
22127 ///
22128 /// @param internal set to true if the call is intended to get a
22129 /// representation of the decl (or type) for the purpose of canonical
22130 /// type comparison. This is mainly used in the function
22131 /// type_base::get_canonical_type_for().
22132 ///
22133 /// In other words if the argument for this parameter is true then the
22134 /// call is meant for internal use (for technical use inside the
22135 /// library itself), false otherwise. If you don't know what this is
22136 /// for, then set it to false.
22137 ///
22138 /// @return the pretty representation for a function.
22139 string
22141  bool qualified_name) const
22142 {
22143  const method_decl* mem_fn =
22144  dynamic_cast<const method_decl*>(this);
22145 
22146  string fn_prefix = mem_fn ? "method ": "function ";
22147  string result;
22148 
22149  if (mem_fn
22150  && is_member_function(mem_fn)
22151  && get_member_function_is_virtual(mem_fn))
22152  fn_prefix += "virtual ";
22153 
22154  decl_base_sptr return_type;
22155  if ((mem_fn
22156  && is_member_function(mem_fn)
22157  && (get_member_function_is_dtor(*mem_fn)
22158  || get_member_function_is_ctor(*mem_fn))))
22159  /*cdtors do not have return types. */;
22160  else
22161  return_type = mem_fn
22162  ? get_type_declaration(mem_fn->get_type()->get_return_type())
22164 
22165  result = get_pretty_representation_of_declarator(internal);
22166  if (return_type)
22167  {
22168  if (is_npaf_type(is_type(return_type))
22169  || !(is_pointer_to_function_type(is_type(return_type))
22170  || is_pointer_to_array_type(is_type(return_type))))
22171  result = get_type_name(is_type(return_type).get(), qualified_name,
22172  internal) + " " + result;
22173  else if (pointer_type_def_sptr p =
22174  is_pointer_to_function_type(is_type(return_type)))
22175  result = add_outer_pointer_to_fn_type_expr(p, result,
22176  /*qualified=*/true,
22177  internal);
22178  else if(pointer_type_def_sptr p =
22179  is_pointer_to_array_type(is_type(return_type)))
22180  result = add_outer_pointer_to_array_type_expr(p, result,
22181  qualified_name,
22182  internal);
22183  else
22185  }
22186 
22187  return fn_prefix + result;
22188 }
22189 
22190 /// Compute and return the pretty representation for the part of the
22191 /// function declaration that starts at the declarator. That is, the
22192 /// return type and the other specifiers of the beginning of the
22193 /// function's declaration ar omitted.
22194 ///
22195 /// @param internal set to true if the call is intended to get a
22196 /// representation of the decl (or type) for the purpose of canonical
22197 /// type comparison. This is mainly used in the function
22198 /// type_base::get_canonical_type_for().
22199 ///
22200 /// In other words if the argument for this parameter is true then the
22201 /// call is meant for internal use (for technical use inside the
22202 /// library itself), false otherwise. If you don't know what this is
22203 /// for, then set it to false.
22204 ///
22205 /// @return the pretty representation for the part of the function
22206 /// declaration that starts at the declarator.
22207 string
22209 {
22210  const method_decl* mem_fn =
22211  dynamic_cast<const method_decl*>(this);
22212 
22213  string result;
22214 
22215  if (mem_fn)
22216  {
22217  result += mem_fn->get_type()->get_class_type()->get_qualified_name()
22218  + "::" + mem_fn->get_name();
22219  }
22220  else
22221  result += get_qualified_name();
22222 
22223  std::ostringstream fn_parms;
22224  stream_pretty_representation_of_fn_parms(*get_type(),
22225  fn_parms,
22226  /*qualified=*/true,
22227  internal);
22228  result += fn_parms.str();
22229 
22230  if (mem_fn
22231  &&((is_member_function(mem_fn) && get_member_function_is_const(*mem_fn))
22232  || is_method_type(mem_fn->get_type())->get_is_const()))
22233  result += " const";
22234 
22235  return result;
22236 }
22237 
22238 /// Getter for the first non-implicit parameter of a function decl.
22239 ///
22240 /// If the function is a non-static member function, the parameter
22241 /// returned is the first one following the implicit 'this' parameter.
22242 ///
22243 /// @return the first non implicit parm.
22244 function_decl::parameters::const_iterator
22246 {
22247  if (get_parameters().empty())
22248  return get_parameters().end();
22249 
22250  bool is_method = dynamic_cast<const method_decl*>(this);
22251 
22252  parameters::const_iterator i = get_parameters().begin();
22253  if (is_method)
22254  ++i;
22255 
22256  return i;
22257 }
22258 
22259 /// Return the type of the current instance of @ref function_decl.
22260 ///
22261 /// It's either a function_type or method_type.
22262 /// @return the type of the current instance of @ref function_decl.
22263 const shared_ptr<function_type>
22265 {return priv_->type_.lock();}
22266 
22267 /// Fast getter of the type of the current instance of @ref function_decl.
22268 ///
22269 /// Note that this function returns the underlying pointer managed by
22270 /// the smart pointer returned by function_decl::get_type(). It's
22271 /// faster than function_decl::get_type(). This getter is to be used
22272 /// in code paths that are proven to be performance hot spots;
22273 /// especially (for instance) when comparing function types. Those
22274 /// are compared extremely frequently when libabigail is used to
22275 /// handle huge binaries with a lot of functions.
22276 ///
22277 /// @return the type of the current instance of @ref function_decl.
22278 const function_type*
22280 {return priv_->naked_type_;}
22281 
22282 void
22283 function_decl::set_type(const function_type_sptr& fn_type)
22284 {
22285  priv_->type_ = fn_type;
22286  priv_->naked_type_ = fn_type.get();
22287 }
22288 
22289 /// This sets the underlying ELF symbol for the current function decl.
22290 ///
22291 /// And underlyin$g ELF symbol for the current function decl might
22292 /// exist only if the corpus that this function decl originates from
22293 /// was constructed from an ELF binary file.
22294 ///
22295 /// Note that comparing two function decls that have underlying ELF
22296 /// symbols involves comparing their underlying elf symbols. The decl
22297 /// name for the function thus becomes irrelevant in the comparison.
22298 ///
22299 /// @param sym the new ELF symbol for this function decl.
22300 void
22302 {
22303  priv_->symbol_ = sym;
22304  // The function id cache that depends on the symbol must be
22305  // invalidated because the symbol changed.
22306  priv_->id_ = get_environment().intern("");
22307 }
22308 
22309 /// Gets the the underlying ELF symbol for the current variable,
22310 /// that was set using function_decl::set_symbol(). Please read the
22311 /// documentation for that member function for more information about
22312 /// "underlying ELF symbols".
22313 ///
22314 /// @return sym the underlying ELF symbol for this function decl, if
22315 /// one exists.
22316 const elf_symbol_sptr&
22318 {return priv_->symbol_;}
22319 
22320 /// Test if the function was declared inline.
22321 ///
22322 /// @return true iff the function was declared inline.
22323 bool
22325 {return priv_->declared_inline_;}
22326 
22327 /// Set the property of the function being declared inline.
22328 ///
22329 /// @param value true iff the function was declared inline.
22330 void
22332 {priv_->declared_inline_ = value;}
22333 
22335 function_decl::get_binding() const
22336 {return priv_->binding_;}
22337 
22338 /// @return the return type of the current instance of function_decl.
22339 const shared_ptr<type_base>
22341 {return get_type()->get_return_type();}
22342 
22343 /// @return the parameters of the function.
22344 const std::vector<shared_ptr<function_decl::parameter> >&
22346 {return get_type()->get_parameters();}
22347 
22348 /// Append a parameter to the type of this function.
22349 ///
22350 /// @param parm the parameter to append.
22351 void
22352 function_decl::append_parameter(shared_ptr<parameter> parm)
22353 {get_type()->append_parameter(parm);}
22354 
22355 /// Append a vector of parameters to the type of this function.
22356 ///
22357 /// @param parms the vector of parameters to append.
22358 void
22359 function_decl::append_parameters(std::vector<shared_ptr<parameter> >& parms)
22360 {
22361  for (std::vector<shared_ptr<parameter> >::const_iterator i = parms.begin();
22362  i != parms.end();
22363  ++i)
22364  get_type()->append_parameter(*i);
22365 }
22366 
22367 /// Create a new instance of function_decl that is a clone of the
22368 /// current one.
22369 ///
22370 /// @return the new clone.
22373 {
22375  if (is_member_function(*this))
22376  {
22377  method_decl_sptr
22378  m(new method_decl(get_name(),
22379  get_type(),
22381  get_location(),
22382  get_linkage_name(),
22383  get_visibility(),
22384  get_binding()));
22386  ABG_ASSERT(scope);
22390  get_member_is_static(*this),
22394  f = m;
22395  }
22396  else
22397  {
22398  f.reset(new function_decl(get_name(),
22399  get_type(),
22401  get_location(),
22402  get_linkage_name(),
22403  get_visibility(),
22404  get_binding()));
22406  }
22407  f->set_symbol(get_symbol());
22408 
22409  return f;
22410 }
22411 
22412 /// Compares two instances of @ref function_decl.
22413 ///
22414 /// If the two intances are different, set a bitfield to give some
22415 /// insight about the kind of differences there are.
22416 ///
22417 /// @param l the first artifact of the comparison.
22418 ///
22419 /// @param r the second artifact of the comparison.
22420 ///
22421 /// @param k a pointer to a bitfield that gives information about the
22422 /// kind of changes there are between @p l and @p r. This one is set
22423 /// iff @p k is non-null and the function returns false.
22424 ///
22425 /// Please note that setting k to a non-null value does have a
22426 /// negative performance impact because even if @p l and @p r are not
22427 /// equal, the function keeps up the comparison in order to determine
22428 /// the different kinds of ways in which they are different.
22429 ///
22430 /// @return true if @p l equals @p r, false otherwise.
22431 bool
22433 {
22434  bool result = true;
22435 
22436  // Compare function types
22437  const type_base* t0 = l.get_naked_type(), *t1 = r.get_naked_type();
22438  if (t0 == t1 || *t0 == *t1)
22439  ; // the types are equal, let's move on to compare the other
22440  // properties of the functions.
22441  else
22442  {
22443  result = false;
22444  if (k)
22445  {
22446  if (!types_have_similar_structure(t0, t1))
22447  *k |= LOCAL_TYPE_CHANGE_KIND;
22448  else
22449  *k |= SUBTYPE_CHANGE_KIND;
22450  }
22451  else
22453  }
22454 
22455  const elf_symbol_sptr &s0 = l.get_symbol(), &s1 = r.get_symbol();
22456  if (!!s0 != !!s1)
22457  {
22458  result = false;
22459  if (k)
22461  else
22463  }
22464  else if (s0 && s0 != s1)
22465  {
22466  if (!elf_symbols_alias(s0, s1))
22467  {
22468  result = false;
22469  if (k)
22471  else
22473  }
22474  }
22475  bool symbols_are_equal = (s0 && s1 && result);
22476 
22477  if (symbols_are_equal)
22478  {
22479  // The functions have underlying elf symbols that are equal,
22480  // so now, let's compare the decl_base part of the functions
22481  // w/o considering their decl names.
22482  interned_string n1 = l.get_name(), n2 = r.get_name();
22483  interned_string ln1 = l.get_linkage_name(), ln2 = r.get_linkage_name();
22484  const_cast<function_decl&>(l).set_name("");
22485  const_cast<function_decl&>(l).set_linkage_name("");
22486  const_cast<function_decl&>(r).set_name("");
22487  const_cast<function_decl&>(r).set_linkage_name("");
22488 
22489  bool decl_bases_different = !l.decl_base::operator==(r);
22490 
22491  const_cast<function_decl&>(l).set_name(n1);
22492  const_cast<function_decl&>(l).set_linkage_name(ln1);
22493  const_cast<function_decl&>(r).set_name(n2);
22494  const_cast<function_decl&>(r).set_linkage_name(ln2);
22495 
22496  if (decl_bases_different)
22497  {
22498  result = false;
22499  if (k)
22501  else
22503  }
22504  }
22505  else
22506  if (!l.decl_base::operator==(r))
22507  {
22508  result = false;
22509  if (k)
22511  else
22513  }
22514 
22515  // Compare the remaining properties. Note that we don't take into
22516  // account the fact that the function was declared inline or not as
22517  // that doesn't have any impact on the final ABI.
22518  if (l.get_binding() != r.get_binding())
22519  {
22520  result = false;
22521  if (k)
22523  else
22525  }
22526 
22528  {
22529  result = false;
22530  if (k)
22532  else
22534  }
22535 
22537  {
22538  if (!((get_member_function_is_ctor(l)
22542  && (get_member_is_static(l)
22543  == get_member_is_static(r))
22550  {
22551  result = false;
22552  if (k)
22554  else
22556  }
22557  }
22558 
22559  ABG_RETURN(result);
22560 }
22561 
22562 /// Comparison operator for @ref function_decl.
22563 ///
22564 /// @param other the other instance of @ref function_decl to compare
22565 /// against.
22566 ///
22567 /// @return true iff the current instance of @ref function_decl equals
22568 /// @p other.
22569 bool
22571 {
22572  const function_decl* o = dynamic_cast<const function_decl*>(&other);
22573  if (!o)
22574  return false;
22575  return equals(*this, *o, 0);
22576 }
22577 
22578 /// Return true iff the function takes a variable number of
22579 /// parameters.
22580 ///
22581 /// @return true if the function taks a variable number
22582 /// of parameters.
22583 bool
22585 {
22586  return (!get_parameters().empty()
22587  && get_parameters().back()->get_variadic_marker());
22588 }
22589 
22590 /// The virtual implementation of 'get_hash' for a function_decl.
22591 ///
22592 /// This allows decl_base::get_hash to work for function_decls.
22593 ///
22594 /// @return the hash value for function decl.
22595 size_t
22597 {
22598  function_decl::hash hash_fn;
22599  return hash_fn(*this);
22600 }
22601 
22602 /// Return an ID that tries to uniquely identify the function inside a
22603 /// program or a library.
22604 ///
22605 /// So if the function has an underlying elf symbol, the ID is the
22606 /// concatenation of the symbol name and its version. Otherwise, the
22607 /// ID is the linkage name if its non-null. Otherwise, it's the
22608 /// pretty representation of the function.
22609 ///
22610 /// @return the ID.
22613 {
22614  if (priv_->id_.empty())
22615  {
22616  const environment& env = get_type()->get_environment();
22617  if (elf_symbol_sptr s = get_symbol())
22618  {
22619  string virtual_member_suffix;
22620  if (is_member_function(this))
22621  {
22622  method_decl* m = is_method_decl(this);
22623  ABG_ASSERT(m);
22625  {
22627  (m->get_type()->get_class_type(),
22628  /*look_through_decl_only=*/true))
22629  virtual_member_suffix += "/o";
22630  }
22631  }
22632  if (s->has_aliases())
22633  // The symbol has several aliases, so let's use a scheme
22634  // that allows all aliased functions to have different
22635  // IDs.
22636  priv_->id_ = env.intern(get_name() + "/" + s->get_id_string());
22637  else
22638  // Let's use the full symbol name with its version as ID.
22639  priv_->id_ = env.intern(s->get_id_string());
22640 
22641  if (!virtual_member_suffix.empty())
22642  priv_->id_ = env.intern(priv_->id_ + virtual_member_suffix);
22643  }
22644  else if (!get_linkage_name().empty())
22645  priv_->id_= env.intern(get_linkage_name());
22646  else
22647  priv_->id_ = env.intern(get_pretty_representation());
22648  }
22649  return priv_->id_;
22650 }
22651 
22652 /// Test if two function declarations are aliases.
22653 ///
22654 /// Two functions declarations are aliases if their symbols are
22655 /// aliases, in the ELF sense.
22656 ///
22657 /// @param f1 the first function to consider.
22658 ///
22659 /// @param f2 the second function to consider.
22660 ///
22661 /// @return true iff @p f1 is an alias of @p f2
22662 bool
22664 {
22665  elf_symbol_sptr s1 = f1.get_symbol(), s2 = f2.get_symbol();
22666 
22667  if (!s1 || !s2)
22668  return false;
22669 
22670  return elf_symbols_alias(s1, s2);
22671 }
22672 
22673 /// This implements the ir_traversable_base::traverse pure virtual
22674 /// function.
22675 ///
22676 /// @param v the visitor used on the current instance.
22677 ///
22678 /// @return true if the entire IR node tree got traversed, false
22679 /// otherwise.
22680 bool
22682 {
22683  if (visiting())
22684  return true;
22685 
22686  if (v.visit_begin(this))
22687  {
22688  visiting(true);
22689  if (type_base_sptr t = get_type())
22690  t->traverse(v);
22691  visiting(false);
22692  }
22693  return v.visit_end(this);
22694 }
22695 
22696 /// Destructor of the @ref function_decl type.
22698 {delete priv_;}
22699 
22700 /// A deep comparison operator for a shared pointer to @ref function_decl
22701 ///
22702 /// This function compares to shared pointers to @ref function_decl by
22703 /// looking at the pointed-to instances of @ref function_dec
22704 /// comparing them too. If the two pointed-to objects are equal then
22705 /// this function returns true.
22706 ///
22707 /// @param l the left-hand side argument of the equality operator.
22708 ///
22709 /// @param r the right-hand side argument of the equality operator.
22710 ///
22711 /// @return true iff @p l equals @p r.
22712 bool
22714 {
22715  if (l.get() == r.get())
22716  return true;
22717  if (!!l != !!r)
22718  return false;
22719 
22720  return *l == *r;
22721 }
22722 
22723 /// A deep inequality operator for smart pointers to functions.
22724 ///
22725 /// @param l the left-hand side argument of the inequality operator.
22726 ///
22727 /// @pram r the right-hand side argument of the inequality operator.
22728 ///
22729 /// @return true iff @p is not equal to @p r.
22730 bool
22732 {return !operator==(l, r);}
22733 
22734 // <function_decl definitions>
22735 
22736 // <function_decl::parameter definitions>
22737 
22738 struct function_decl::parameter::priv
22739 {
22740  type_base_wptr type_;
22741  unsigned index_;
22742  bool variadic_marker_;
22743 
22744  priv()
22745  : index_(),
22746  variadic_marker_()
22747  {}
22748 
22749  priv(type_base_sptr type,
22750  unsigned index,
22751  bool variadic_marker)
22752  : type_(type),
22753  index_(index),
22754  variadic_marker_(variadic_marker)
22755  {}
22756 };// end struct function_decl::parameter::priv
22757 
22758 function_decl::parameter::parameter(const type_base_sptr type,
22759  unsigned index,
22760  const string& name,
22761  const location& loc,
22762  bool is_variadic)
22763  : type_or_decl_base(type->get_environment(),
22764  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
22765  decl_base(type->get_environment(), name, loc),
22766  priv_(new priv(type, index, is_variadic))
22767 {
22768  runtime_type_instance(this);
22769 }
22770 
22771 function_decl::parameter::parameter(const type_base_sptr type,
22772  unsigned index,
22773  const string& name,
22774  const location& loc,
22775  bool is_variadic,
22776  bool is_artificial)
22777  : type_or_decl_base(type->get_environment(),
22778  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
22779  decl_base(type->get_environment(), name, loc),
22780  priv_(new priv(type, index, is_variadic))
22781 {
22782  runtime_type_instance(this);
22783  set_is_artificial(is_artificial);
22784 }
22785 
22786 function_decl::parameter::parameter(const type_base_sptr type,
22787  const string& name,
22788  const location& loc,
22789  bool is_variadic,
22790  bool is_artificial)
22791  : type_or_decl_base(type->get_environment(),
22792  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
22793  decl_base(type->get_environment(), name, loc),
22794  priv_(new priv(type, 0, is_variadic))
22795 {
22796  runtime_type_instance(this);
22797  set_is_artificial(is_artificial);
22798 }
22799 
22800 function_decl::parameter::parameter(const type_base_sptr type,
22801  unsigned index,
22802  bool variad)
22803  : type_or_decl_base(type->get_environment(),
22804  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
22805  decl_base(type->get_environment(), "", location()),
22806  priv_(new priv(type, index, variad))
22807 {
22808  runtime_type_instance(this);
22809 }
22810 
22811 function_decl::parameter::~parameter() = default;
22812 
22813 const type_base_sptr
22814 function_decl::parameter::get_type()const
22815 {return priv_->type_.lock();}
22816 
22817 /// @return a copy of the type name of the parameter.
22818 interned_string
22820 {
22821  const environment& env = get_environment();
22822 
22823  type_base_sptr t = get_type();
22824  string str;
22825  if (get_variadic_marker() || env.is_variadic_parameter_type(t))
22826  str = "...";
22827  else
22828  {
22829  ABG_ASSERT(t);
22830  str = abigail::ir::get_type_name(t);
22831  }
22832  return env.intern(str);
22833 }
22834 
22835 /// @return a copy of the pretty representation of the type of the
22836 /// parameter.
22837 const string
22839 {
22840  type_base_sptr t = get_type();
22841  string str;
22842  if (get_variadic_marker()
22843  || get_environment().is_variadic_parameter_type(t))
22844  str = "...";
22845  else
22846  {
22847  ABG_ASSERT(t);
22849  }
22850  return str;
22851 }
22852 
22853 /// Get a name uniquely identifying the parameter in the function.
22854 ///
22855 ///@return the unique parm name id.
22858 {
22859  const environment& env = get_environment();
22860 
22861 
22862  std::ostringstream o;
22863  o << "parameter-" << get_index();
22864 
22865  return env.intern(o.str());
22866 }
22867 
22868 unsigned
22869 function_decl::parameter::get_index() const
22870 {return priv_->index_;}
22871 
22872 void
22873 function_decl::parameter::set_index(unsigned i)
22874 {priv_->index_ = i;}
22875 
22876 
22877 bool
22878 function_decl::parameter::get_variadic_marker() const
22879 {return priv_->variadic_marker_;}
22880 
22881 /// Compares two instances of @ref function_decl::parameter.
22882 ///
22883 /// If the two intances are different, set a bitfield to give some
22884 /// insight about the kind of differences there are.
22885 ///
22886 /// @param l the first artifact of the comparison.
22887 ///
22888 /// @param r the second artifact of the comparison.
22889 ///
22890 /// @param k a pointer to a bitfield that gives information about the
22891 /// kind of changes there are between @p l and @p r. This one is set
22892 /// iff @p k is non-null and the function returns false.
22893 ///
22894 /// Please note that setting k to a non-null value does have a
22895 /// negative performance impact because even if @p l and @p r are not
22896 /// equal, the function keeps up the comparison in order to determine
22897 /// the different kinds of ways in which they are different.
22898 ///
22899 /// @return true if @p l equals @p r, false otherwise.
22900 bool
22902  const function_decl::parameter& r,
22903  change_kind* k)
22904 {
22905  bool result = true;
22906 
22907  if ((l.get_variadic_marker() != r.get_variadic_marker())
22908  || (l.get_index() != r.get_index())
22909  || (!!l.get_type() != !!r.get_type()))
22910  {
22911  result = false;
22912  if (k)
22913  {
22914  if (l.get_index() != r.get_index())
22916  if (l.get_variadic_marker() != r.get_variadic_marker()
22917  || !!l.get_type() != !!r.get_type())
22918  *k |= LOCAL_TYPE_CHANGE_KIND;
22919  }
22920  else
22922  }
22923 
22924  type_base_sptr l_type = peel_typedef_type(l.get_type());
22925  type_base_sptr r_type = peel_typedef_type(r.get_type());
22926  if (l_type != r_type)
22927  {
22928  result = false;
22929  if (k)
22930  {
22931  if (!types_have_similar_structure(l_type, r_type))
22932  *k |= LOCAL_TYPE_CHANGE_KIND;
22933  else
22934  *k |= SUBTYPE_CHANGE_KIND;
22935  }
22936  else
22938  }
22939 
22940  ABG_RETURN(result);
22941 }
22942 
22943 bool
22944 function_decl::parameter::operator==(const parameter& o) const
22945 {return equals(*this, o, 0);}
22946 
22947 bool
22948 function_decl::parameter::operator==(const decl_base& o) const
22949 {
22950  const function_decl::parameter* p =
22951  dynamic_cast<const function_decl::parameter*>(&o);
22952  if (!p)
22953  return false;
22954  return function_decl::parameter::operator==(*p);
22955 }
22956 
22957 /// Non-member equality operator for @ref function_decl::parameter.
22958 ///
22959 /// @param l the left-hand side of the equality operator
22960 ///
22961 /// @param r the right-hand side of the equality operator
22962 ///
22963 /// @return true iff @p l and @p r equals.
22964 bool
22967 {
22968  if (!!l != !!r)
22969  return false;
22970  if (!l)
22971  return true;
22972  return *l == *r;
22973 }
22974 
22975 /// Non-member inequality operator for @ref function_decl::parameter.
22976 ///
22977 /// @param l the left-hand side of the equality operator
22978 ///
22979 /// @param r the right-hand side of the equality operator
22980 ///
22981 /// @return true iff @p l and @p r different.
22982 bool
22985 {return !operator==(l, r);}
22986 
22987 /// Traverse the diff sub-tree under the current instance
22988 /// function_decl.
22989 ///
22990 /// @param v the visitor to invoke on each diff node of the sub-tree.
22991 ///
22992 /// @return true if the traversing has to keep going on, false
22993 /// otherwise.
22994 bool
22996 {
22997  if (visiting())
22998  return true;
22999 
23000  if (v.visit_begin(this))
23001  {
23002  visiting(true);
23003  if (type_base_sptr t = get_type())
23004  t->traverse(v);
23005  visiting(false);
23006  }
23007  return v.visit_end(this);
23008 }
23009 
23010 /// Get the hash of a decl. If the hash hasn't been computed yet,
23011 /// compute it ans store its value; otherwise, just return the hash.
23012 ///
23013 /// @return the hash of the decl.
23014 size_t
23016 {
23017  function_decl::parameter::hash hash_fn_parm;
23018  return hash_fn_parm(this);
23019 }
23020 
23021 /// Compute the qualified name of the parameter.
23022 ///
23023 /// @param internal set to true if the call is intended for an
23024 /// internal use (for technical use inside the library itself), false
23025 /// otherwise. If you don't know what this is for, then set it to
23026 /// false.
23027 ///
23028 /// @param qn the resulting qualified name.
23029 void
23031  bool /*internal*/) const
23032 {qualified_name = get_name();}
23033 
23034 /// Compute and return a copy of the pretty representation of the
23035 /// current function parameter.
23036 ///
23037 /// @param internal set to true if the call is intended to get a
23038 /// representation of the decl (or type) for the purpose of canonical
23039 /// type comparison. This is mainly used in the function
23040 /// type_base::get_canonical_type_for().
23041 ///
23042 /// In other words if the argument for this parameter is true then the
23043 /// call is meant for internal use (for technical use inside the
23044 /// library itself), false otherwise. If you don't know what this is
23045 /// for, then set it to false.
23046 ///
23047 /// @return a copy of the textual representation of the current
23048 /// function parameter.
23049 string
23051  bool qualified_name) const
23052 {
23053  const environment& env = get_environment();
23054 
23055  string type_repr;
23056  type_base_sptr t = get_type();
23057  if (!t)
23058  type_repr = "void";
23059  else if (env.is_variadic_parameter_type(t))
23060  type_repr = "...";
23061  else
23062  type_repr = ir::get_type_name(t, qualified_name, internal);
23063 
23064  string result = type_repr;
23065  string parm_name = get_name_id();
23066 
23067  if (!parm_name.empty())
23068  result += " " + parm_name;
23069 
23070  return result;
23071 }
23072 
23073 // </function_decl::parameter definitions>
23074 
23075 // <class_or_union definitions>
23076 
23077 /// A Constructor for instances of @ref class_or_union
23078 ///
23079 /// @param env the environment we are operating from.
23080 ///
23081 /// @param name the identifier of the class.
23082 ///
23083 /// @param size_in_bits the size of an instance of @ref
23084 /// class_or_union, expressed in bits
23085 ///
23086 /// @param align_in_bits the alignment of an instance of @ref class_or_union,
23087 /// expressed in bits.
23088 ///
23089 /// @param locus the source location of declaration point this class.
23090 ///
23091 /// @param vis the visibility of instances of @ref class_or_union.
23092 ///
23093 /// @param mem_types the vector of member types of this instance of
23094 /// @ref class_or_union.
23095 ///
23096 /// @param data_members the vector of data members of this instance of
23097 /// @ref class_or_union.
23098 ///
23099 /// @param member_fns the vector of member functions of this instance
23100 /// of @ref class_or_union.
23101 class_or_union::class_or_union(const environment& env, const string& name,
23102  size_t size_in_bits, size_t align_in_bits,
23103  const location& locus, visibility vis,
23104  member_types& mem_types,
23106  member_functions& member_fns)
23107  : type_or_decl_base(env,
23108  ABSTRACT_TYPE_BASE
23109  | ABSTRACT_DECL_BASE
23110  | ABSTRACT_SCOPE_TYPE_DECL
23111  | ABSTRACT_SCOPE_DECL),
23112  decl_base(env, name, locus, name, vis),
23113  type_base(env, size_in_bits, align_in_bits),
23114  scope_type_decl(env, name, size_in_bits, align_in_bits, locus, vis),
23115  priv_(new priv(data_members, member_fns))
23116 {
23117  for (member_types::iterator i = mem_types.begin();
23118  i != mem_types.end();
23119  ++i)
23120  if (!has_scope(get_type_declaration(*i)))
23122 
23123  for (data_members::iterator i = data_members.begin();
23124  i != data_members.end();
23125  ++i)
23126  if (!has_scope(*i))
23127  add_decl_to_scope(*i, this);
23128 
23129  for (member_functions::iterator i = member_fns.begin();
23130  i != member_fns.end();
23131  ++i)
23132  if (!has_scope(static_pointer_cast<decl_base>(*i)))
23133  add_decl_to_scope(*i, this);
23134 }
23135 
23136 /// A constructor for instances of @ref class_or_union.
23137 ///
23138 /// @param env the environment we are operating from.
23139 ///
23140 /// @param name the name of the class.
23141 ///
23142 /// @param size_in_bits the size of an instance of @ref
23143 /// class_or_union, expressed in bits
23144 ///
23145 /// @param align_in_bits the alignment of an instance of @ref class_or_union,
23146 /// expressed in bits.
23147 ///
23148 /// @param locus the source location of declaration point this class.
23149 ///
23150 /// @param vis the visibility of instances of @ref class_or_union.
23151 class_or_union::class_or_union(const environment& env, const string& name,
23152  size_t size_in_bits, size_t align_in_bits,
23153  const location& locus, visibility vis)
23154  : type_or_decl_base(env,
23155  ABSTRACT_TYPE_BASE
23156  | ABSTRACT_DECL_BASE
23157  | ABSTRACT_SCOPE_TYPE_DECL
23158  | ABSTRACT_SCOPE_DECL),
23159  decl_base(env, name, locus, name, vis),
23160  type_base(env, size_in_bits, align_in_bits),
23161  scope_type_decl(env, name, size_in_bits, align_in_bits, locus, vis),
23162  priv_(new priv)
23163 {}
23164 
23165 /// Constructor of the @ref class_or_union type.
23166 ///
23167 /// @param env the @ref environment we are operating from.
23168 ///
23169 /// @param name the name of the @ref class_or_union.
23170 ///
23171 /// @param is_declaration_only a boolean saying whether the instance
23172 /// represents a declaration only, or not.
23173 class_or_union::class_or_union(const environment& env, const string& name,
23174  bool is_declaration_only)
23175  : type_or_decl_base(env,
23176  ABSTRACT_TYPE_BASE
23177  | ABSTRACT_DECL_BASE
23178  | ABSTRACT_SCOPE_TYPE_DECL
23179  | ABSTRACT_SCOPE_DECL),
23180  decl_base(env, name, location(), name),
23181  type_base(env, 0, 0),
23182  scope_type_decl(env, name, 0, 0, location()),
23183  priv_(new priv)
23184 {
23185  set_is_declaration_only(is_declaration_only);
23186 }
23187 
23188 /// This implements the ir_traversable_base::traverse pure virtual
23189 /// function.
23190 ///
23191 /// @param v the visitor used on the member nodes of the translation
23192 /// unit during the traversal.
23193 ///
23194 /// @return true if the entire IR node tree got traversed, false
23195 /// otherwise.
23196 bool
23198 {
23199  if (v.type_node_has_been_visited(this))
23200  return true;
23201 
23202  if (visiting())
23203  return true;
23204 
23205  if (v.visit_begin(this))
23206  {
23207  visiting(true);
23208  bool stop = false;
23209 
23210  if (!stop)
23211  for (data_members::const_iterator i = get_data_members().begin();
23212  i != get_data_members().end();
23213  ++i)
23214  if (!(*i)->traverse(v))
23215  {
23216  stop = true;
23217  break;
23218  }
23219 
23220  if (!stop)
23221  for (member_functions::const_iterator i= get_member_functions().begin();
23222  i != get_member_functions().end();
23223  ++i)
23224  if (!(*i)->traverse(v))
23225  {
23226  stop = true;
23227  break;
23228  }
23229 
23230  if (!stop)
23231  for (member_types::const_iterator i = get_member_types().begin();
23232  i != get_member_types().end();
23233  ++i)
23234  if (!(*i)->traverse(v))
23235  {
23236  stop = true;
23237  break;
23238  }
23239 
23240  if (!stop)
23241  for (member_function_templates::const_iterator i =
23243  i != get_member_function_templates().end();
23244  ++i)
23245  if (!(*i)->traverse(v))
23246  {
23247  stop = true;
23248  break;
23249  }
23250 
23251  if (!stop)
23252  for (member_class_templates::const_iterator i =
23253  get_member_class_templates().begin();
23254  i != get_member_class_templates().end();
23255  ++i)
23256  if (!(*i)->traverse(v))
23257  {
23258  stop = true;
23259  break;
23260  }
23261  visiting(false);
23262  }
23263 
23264  bool result = v.visit_end(this);
23265  v.mark_type_node_as_visited(this);
23266  return result;
23267 }
23268 
23269 /// Destrcutor of the @ref class_or_union type.
23271 {delete priv_;}
23272 
23273 /// Add a member declaration to the current instance of class_or_union.
23274 /// The member declaration can be either a member type, data member,
23275 /// member function, or member template.
23276 ///
23277 /// @param d the member declaration to add.
23278 decl_base_sptr
23279 class_or_union::add_member_decl(const decl_base_sptr& d)
23280 {return insert_member_decl(d);}
23281 
23282 /// Remove a given decl from the current @ref class_or_union scope.
23283 ///
23284 /// Note that only type declarations are supported by this method for
23285 /// now. Support for the other kinds of declaration is left as an
23286 /// exercise for the interested reader of the code.
23287 ///
23288 /// @param decl the declaration to remove from this @ref
23289 /// class_or_union scope.
23290 void
23292 {
23293  type_base_sptr t = is_type(decl);
23294 
23295  // For now we want to support just removing types from classes. For
23296  // other kinds of IR node, we need more work.
23297  ABG_ASSERT(t);
23298 
23299  remove_member_type(t);
23300 }
23301 
23302 /// Fixup the members of the type of an anonymous data member.
23303 ///
23304 /// Walk all data members of (the type of) a given anonymous data
23305 /// member and set a particular property of the relationship between
23306 /// each data member and its containing type.
23307 ///
23308 /// That property records the fact that the data member belongs to the
23309 /// anonymous data member we consider.
23310 ///
23311 /// In the future, if there are other properties of this relationship
23312 /// to set in this manner, they ought to be added here.
23313 ///
23314 /// @param anon_dm the anonymous data member to consider.
23315 void
23317 {
23318  class_or_union * anon_dm_type =
23320  if (!anon_dm_type)
23321  return;
23322 
23323  for (class_or_union::data_members::const_iterator it =
23324  anon_dm_type->get_non_static_data_members().begin();
23325  it != anon_dm_type->get_non_static_data_members().end();
23326  ++it)
23327  {
23328  dm_context_rel *rel =
23329  dynamic_cast<dm_context_rel*>((*it)->get_context_rel());
23330  ABG_ASSERT(rel);
23331  rel->set_anonymous_data_member(anon_dm.get());
23332  }
23333 }
23334 
23335 /// Getter of the alignment of the @ref class_or_union type.
23336 ///
23337 /// If this @ref class_or_union is a declaration of a definition that
23338 /// is elsewhere, then the size of the definition is returned.
23339 ///
23340 /// @return the alignment of the @ref class_or_union type.
23341 size_t
23343 {
23345  return is_class_or_union_type
23347 
23349 }
23350 
23351 /// Setter of the alignment of the class type.
23352 ///
23353 /// If this class is a declaration of a definition that is elsewhere,
23354 /// then the new alignment is set to the definition.
23355 ///
23356 /// @param s the new alignment.
23357 void
23359 {
23363  else
23365 }
23366 
23367 /// Setter of the size of the @ref class_or_union type.
23368 ///
23369 /// If this @ref class_or_union is a declaration of a definition that
23370 /// is elsewhere, then the new size is set to the definition.
23371 ///
23372 /// @param s the new size.
23373 void
23375 {
23379  else
23381 }
23382 
23383 /// Getter of the size of the @ref class_or_union type.
23384 ///
23385 /// If this @ref class_or_union is a declaration of a definition that
23386 /// is elsewhere, then the size of the definition is returned.
23387 ///
23388 /// @return the size of the @ref class_or_union type.
23389 size_t
23391 {
23393  return is_class_or_union_type
23395 
23396  return type_base::get_size_in_bits();
23397 }
23398 
23399 /// Get the number of anonymous member classes contained in this
23400 /// class.
23401 ///
23402 /// @return the number of anonymous member classes contained in this
23403 /// class.
23404 size_t
23406 {
23407  int result = 0;
23408  for (member_types::const_iterator it = get_member_types().begin();
23409  it != get_member_types().end();
23410  ++it)
23411  if (class_decl_sptr t = is_class_type(*it))
23412  if (t->get_is_anonymous())
23413  ++result;
23414 
23415  return result;
23416 }
23417 
23418 /// Get the number of anonymous member unions contained in this class.
23419 ///
23420 /// @return the number of anonymous member unions contained in this
23421 /// class.
23422 size_t
23424 {
23425  int result = 0;
23426  for (member_types::const_iterator it = get_member_types().begin();
23427  it != get_member_types().end();
23428  ++it)
23429  if (union_decl_sptr t = is_union_type(*it))
23430  if (t->get_is_anonymous())
23431  ++result;
23432 
23433  return result;
23434 }
23435 
23436 /// Get the number of anonymous member enums contained in this class.
23437 ///
23438 /// @return the number of anonymous member enums contained in this
23439 /// class.
23440 size_t
23442 {
23443  int result = 0;
23444  for (member_types::const_iterator it = get_member_types().begin();
23445  it != get_member_types().end();
23446  ++it)
23447  if (enum_type_decl_sptr t = is_enum_type(*it))
23448  if (t->get_is_anonymous())
23449  ++result;
23450 
23451  return result;
23452 }
23453 
23454 /// Add a data member to the current instance of class_or_union.
23455 ///
23456 /// @param v a var_decl to add as a data member. A proper
23457 /// class_or_union::data_member is created from @p v and added to the
23458 /// class_or_union. This var_decl should not have been already added
23459 /// to a scope.
23460 ///
23461 /// @param access the access specifier for the data member.
23462 ///
23463 /// @param is_laid_out whether the data member was laid out. That is,
23464 /// if its offset has been computed. In the pattern of a class
23465 /// template for instance, this would be set to false.
23466 ///
23467 /// @param is_static whether the data memer is static.
23468 ///
23469 /// @param offset_in_bits if @p is_laid_out is true, this is the
23470 /// offset of the data member, expressed (oh, surprise) in bits.
23471 void
23473  bool is_laid_out, bool is_static,
23474  size_t offset_in_bits)
23475 {
23476  ABG_ASSERT(!has_scope(v));
23477 
23478  priv_->data_members_.push_back(v);
23480  set_data_member_is_laid_out(v, is_laid_out);
23481  set_data_member_offset(v, offset_in_bits);
23482  set_member_access_specifier(v, access);
23483  set_member_is_static(v, is_static);
23484 
23485  if (!is_static)
23486  {
23487  // If this is a non-static variable, add it to the set of
23488  // non-static variables, if it's not already in there.
23489  bool is_already_in = false;
23490  for (data_members::const_iterator i =
23491  priv_->non_static_data_members_.begin();
23492  i != priv_->non_static_data_members_.end();
23493  ++i)
23494  if (*i == v)
23495  {
23496  is_already_in = true;
23497  break;
23498  }
23499  if (!is_already_in)
23500  priv_->non_static_data_members_.push_back(v);
23501  }
23502 
23503  // If v is an anonymous data member, then fixup its data members.
23504  // For now, the only thing the fixup does is to make the data
23505  // members of the anonymous data member be aware of their containing
23506  // anonymous data member. That is helpful to compute the absolute
23507  // bit offset of each of the members of the anonymous data member.
23509 }
23510 
23511 /// Get the data members of this @ref class_or_union.
23512 ///
23513 /// @return a vector of the data members of this @ref class_or_union.
23516 {return priv_->data_members_;}
23517 
23518 /// Find a data member of a given name in the current @ref class_or_union.
23519 ///
23520 /// @param name the name of the data member to find in the current
23521 /// @ref class_or_union.
23522 ///
23523 /// @return a pointer to the @ref var_decl that represents the data
23524 /// member to find inside the current @ref class_or_union.
23525 const var_decl_sptr
23526 class_or_union::find_data_member(const string& name) const
23527 {
23528  for (data_members::const_iterator i = get_data_members().begin();
23529  i != get_data_members().end();
23530  ++i)
23531  if ((*i)->get_name() == name)
23532  return *i;
23533 
23534  // We haven't found a data member with the name 'name'. Let's look
23535  // closer again, this time in our anonymous data members.
23536  for (data_members::const_iterator i = get_data_members().begin();
23537  i != get_data_members().end();
23538  ++i)
23539  if (is_anonymous_data_member(*i))
23540  {
23541  class_or_union_sptr type = is_class_or_union_type((*i)->get_type());
23542  ABG_ASSERT(type);
23543  if (var_decl_sptr data_member = type->find_data_member(name))
23544  return data_member;
23545  }
23546 
23547  return var_decl_sptr();
23548 }
23549 
23550 /// Find an anonymous data member in the class.
23551 ///
23552 /// @param v the anonymous data member to find.
23553 ///
23554 /// @return the anonymous data member found, or nil if none was found.
23555 const var_decl_sptr
23557 {
23558  if (!v->get_name().empty())
23559  return var_decl_sptr();
23560 
23561  for (data_members::const_iterator it = get_non_static_data_members().begin();
23562  it != get_non_static_data_members().end();
23563  ++it)
23564  {
23565  if (is_anonymous_data_member(*it))
23566  if ((*it)->get_pretty_representation(/*internal=*/false, true)
23567  == v->get_pretty_representation(/*internal=*/false, true))
23568  return *it;
23569  }
23570 
23571  return var_decl_sptr();
23572 }
23573 
23574 /// Find a given data member.
23575 ///
23576 /// This function takes a @ref var_decl as an argument. If it has a
23577 /// non-empty name, then it tries to find a data member which has the
23578 /// same name as the argument.
23579 ///
23580 /// If it has an empty name, then the @ref var_decl is considered as
23581 /// an anonymous data member. In that case, this function tries to
23582 /// find an anonymous data member which type equals that of the @ref
23583 /// var_decl argument.
23584 ///
23585 /// @param v this carries either the name of the data member we need
23586 /// to look for, or the type of the anonymous data member we are
23587 /// looking for.
23588 const var_decl_sptr
23590 {
23591  if (!v)
23592  return var_decl_sptr();
23593 
23594  if (v->get_name().empty())
23595  return find_anonymous_data_member(v);
23596 
23597  return find_data_member(v->get_name());
23598 }
23599 
23600 
23601 /// Get the non-static data memebers of this @ref class_or_union.
23602 ///
23603 /// @return a vector of the non-static data members of this @ref
23604 /// class_or_union.
23607 {return priv_->non_static_data_members_;}
23608 
23609 /// Add a member function.
23610 ///
23611 /// @param f the new member function to add.
23612 ///
23613 /// @param a the access specifier to use for the new member function.
23614 ///
23615 /// @param is_static whether the new member function is static.
23616 ///
23617 /// @param is_ctor whether the new member function is a constructor.
23618 ///
23619 /// @param is_dtor whether the new member function is a destructor.
23620 ///
23621 /// @param is_const whether the new member function is const.
23622 void
23624  access_specifier a,
23625  bool is_static, bool is_ctor,
23626  bool is_dtor, bool is_const)
23627 {
23628  ABG_ASSERT(!has_scope(f));
23629 
23631 
23632  set_member_function_is_ctor(f, is_ctor);
23633  set_member_function_is_dtor(f, is_dtor);
23635  set_member_is_static(f, is_static);
23636  set_member_function_is_const(f, is_const);
23637 
23638  priv_->member_functions_.push_back(f);
23639 
23640  // Update the map of linkage name -> member functions. It's useful,
23641  // so that class_or_union::find_member_function() can function.
23642  if (!f->get_linkage_name().empty())
23643  priv_->mem_fns_map_[f->get_linkage_name()] = f;
23644 }
23645 
23646 /// Get the member functions of this @ref class_or_union.
23647 ///
23648 /// @return a vector of the member functions of this @ref
23649 /// class_or_union.
23652 {return priv_->member_functions_;}
23653 
23654 /// Find a method, using its linkage name as a key.
23655 ///
23656 /// @param linkage_name the linkage name of the method to find.
23657 ///
23658 /// @return the method found, or nil if none was found.
23659 const method_decl*
23660 class_or_union::find_member_function(const string& linkage_name) const
23661 {
23662  return const_cast<class_or_union*>(this)->find_member_function(linkage_name);
23663 }
23664 
23665 /// Find a method, using its linkage name as a key.
23666 ///
23667 /// @param linkage_name the linkage name of the method to find.
23668 ///
23669 /// @return the method found, or nil if none was found.
23670 method_decl*
23671 class_or_union::find_member_function(const string& linkage_name)
23672 {
23673  string_mem_fn_sptr_map_type::const_iterator i =
23674  priv_->mem_fns_map_.find(linkage_name);
23675  if (i == priv_->mem_fns_map_.end())
23676  return 0;
23677  return i->second.get();
23678 }
23679 
23680 /// Find a method, using its linkage name as a key.
23681 ///
23682 /// @param linkage_name the linkage name of the method to find.
23683 ///
23684 /// @return the method found, or nil if none was found.
23685 method_decl_sptr
23687 {
23688  string_mem_fn_sptr_map_type::const_iterator i =
23689  priv_->mem_fns_map_.find(linkage_name);
23690  if (i == priv_->mem_fns_map_.end())
23691  return 0;
23692  return i->second;
23693 }
23694 
23695 /// Find a method (member function) using its signature (pretty
23696 /// representation) as a key.
23697 ///
23698 /// @param s the signature of the method.
23699 ///
23700 /// @return the method found, or nil if none was found.
23701 const method_decl*
23703 {
23704  return const_cast<class_or_union*>(this)->find_member_function_from_signature(s);
23705 }
23706 
23707 /// Find a method (member function) using its signature (pretty
23708 /// representation) as a key.
23709 ///
23710 /// @param s the signature of the method.
23711 ///
23712 /// @return the method found, or nil if none was found.
23713 method_decl*
23715 {
23716  string_mem_fn_ptr_map_type::const_iterator i =
23717  priv_->signature_2_mem_fn_map_.find(s);
23718  if (i == priv_->signature_2_mem_fn_map_.end())
23719  return 0;
23720  return i->second;
23721 }
23722 
23723 /// Get the member function templates of this class.
23724 ///
23725 /// @return a vector of the member function templates of this class.
23726 const member_function_templates&
23728 {return priv_->member_function_templates_;}
23729 
23730 /// Get the member class templates of this class.
23731 ///
23732 /// @return a vector of the member class templates of this class.
23733 const member_class_templates&
23735 {return priv_->member_class_templates_;}
23736 
23737 /// Append a member function template to the @ref class_or_union.
23738 ///
23739 /// @param m the member function template to append.
23740 void
23741 class_or_union::add_member_function_template(member_function_template_sptr m)
23742 {
23743  decl_base* c = m->as_function_tdecl()->get_scope();
23744  /// TODO: use our own ABG_ASSERTion facility that adds a meaningful
23745  /// error message or something like a structured error.
23746  priv_->member_function_templates_.push_back(m);
23747  if (!c)
23748  scope_decl::add_member_decl(m->as_function_tdecl());
23749 }
23750 
23751 /// Append a member class template to the @ref class_or_union.
23752 ///
23753 /// @param m the member function template to append.
23754 void
23755 class_or_union::add_member_class_template(member_class_template_sptr m)
23756 {
23757  decl_base* c = m->as_class_tdecl()->get_scope();
23758  /// TODO: use our own ABG_ASSERTion facility that adds a meaningful
23759  /// error message or something like a structured error.
23760  m->set_scope(this);
23761  priv_->member_class_templates_.push_back(m);
23762  if (!c)
23763  scope_decl::add_member_decl(m->as_class_tdecl());
23764 }
23765 
23766 ///@return true iff the current instance has no member.
23767 bool
23769 {
23770  return (get_member_types().empty()
23771  && priv_->data_members_.empty()
23772  && priv_->member_functions_.empty()
23773  && priv_->member_function_templates_.empty()
23774  && priv_->member_class_templates_.empty());
23775 }
23776 
23777 /// Insert a data member to this @ref class_or_union type.
23778 ///
23779 /// @param d the data member to insert.
23780 ///
23781 /// @return the decl @p that got inserted.
23782 decl_base_sptr
23784 {
23785  if (var_decl_sptr v = dynamic_pointer_cast<var_decl>(d))
23786  {
23787  add_data_member(v, public_access,
23788  /*is_laid_out=*/false,
23789  /*is_static=*/true,
23790  /*offset_in_bits=*/0);
23791  d = v;
23792  }
23793  else if (method_decl_sptr f = dynamic_pointer_cast<method_decl>(d))
23794  add_member_function(f, public_access,
23795  /*is_static=*/false,
23796  /*is_ctor=*/false,
23797  /*is_dtor=*/false,
23798  /*is_const=*/false);
23799  else if (member_function_template_sptr f =
23800  dynamic_pointer_cast<member_function_template>(d))
23802  else if (member_class_template_sptr c =
23803  dynamic_pointer_cast<member_class_template>(d))
23805  else
23807 
23808  return d;
23809 }
23810 
23811 /// Equality operator.
23812 ///
23813 /// @param other the other @ref class_or_union to compare against.
23814 ///
23815 /// @return true iff @p other equals the current @ref class_or_union.
23816 bool
23818 {
23819  const class_or_union* op = dynamic_cast<const class_or_union*>(&other);
23820  if (!op)
23821  return false;
23822 
23823  // If this is a decl-only type (and thus with no canonical type),
23824  // use the canonical type of the definition, if any.
23825  const class_or_union *l = 0;
23827  l = dynamic_cast<const class_or_union*>(get_naked_definition_of_declaration());
23828  if (l == 0)
23829  l = this;
23830 
23831  // Likewise for the other class.
23832  const class_or_union *r = 0;
23833  if (op->get_is_declaration_only())
23834  r = dynamic_cast<const class_or_union*>(op->get_naked_definition_of_declaration());
23835  if (r == 0)
23836  r = op;
23837 
23838  return try_canonical_compare(l, r);
23839 }
23840 
23841 /// Equality operator.
23842 ///
23843 /// @param other the other @ref class_or_union to compare against.
23844 ///
23845 /// @return true iff @p other equals the current @ref class_or_union.
23846 bool
23848 {
23849  const decl_base* o = dynamic_cast<const decl_base*>(&other);
23850  if (!o)
23851  return false;
23852  return *this == *o;
23853 }
23854 
23855 /// Equality operator.
23856 ///
23857 /// @param other the other @ref class_or_union to compare against.
23858 ///
23859 /// @return true iff @p other equals the current @ref class_or_union.
23860 bool
23862 {
23863  const decl_base& o = other;
23864  return class_or_union::operator==(o);
23865 }
23866 
23867 /// Compares two instances of @ref class_or_union.
23868 ///
23869 /// If the two intances are different, set a bitfield to give some
23870 /// insight about the kind of differences there are.
23871 ///
23872 /// @param l the first artifact of the comparison.
23873 ///
23874 /// @param r the second artifact of the comparison.
23875 ///
23876 /// @param k a pointer to a bitfield that gives information about the
23877 /// kind of changes there are between @p l and @p r. This one is set
23878 /// iff it's non-null and if the function returns false.
23879 ///
23880 /// Please note that setting k to a non-null value does have a
23881 /// negative performance impact because even if @p l and @p r are not
23882 /// equal, the function keeps up the comparison in order to determine
23883 /// the different kinds of ways in which they are different.
23884 ///
23885 /// @return true if @p l equals @p r, false otherwise.
23886 bool
23888 {
23889  // if one of the classes is declaration-only, look through it to
23890  // get its definition.
23891  bool l_is_decl_only = l.get_is_declaration_only();
23892  bool r_is_decl_only = r.get_is_declaration_only();
23893  if (l_is_decl_only || r_is_decl_only)
23894  {
23895  const class_or_union* def1 = l_is_decl_only
23897  : &l;
23898 
23899  const class_or_union* def2 = r_is_decl_only
23901  : &r;
23902 
23903  if (!def1 || !def2)
23904  {
23905  if (!l.get_is_anonymous()
23906  && !r.get_is_anonymous()
23907  && l_is_decl_only && r_is_decl_only
23909  // The two decl-only classes differ from their size. A
23910  // true decl-only class should not have a size property to
23911  // begin with. This comes from a DWARF oddity and can
23912  // results in a false positive, so let's not consider that
23913  // change.
23914  return true;
23915 
23917  || ((odr_is_relevant(l) && !def1)
23918  || (odr_is_relevant(r) && !def2)))
23921  {
23922  const interned_string& q1 = l.get_scoped_name();
23923  const interned_string& q2 = r.get_scoped_name();
23924  if (q1 == q2)
23925  // Not using RETURN(true) here, because that causes
23926  // performance issues. We don't need to do
23927  // l.priv_->unmark_as_being_compared({l,r}) here because
23928  // we haven't marked l or r as being compared yet, and
23929  // doing so has a peformance cost that shows up on
23930  // performance profiles for *big* libraries.
23931  return true;
23932  else
23933  {
23934  if (k)
23935  *k |= LOCAL_TYPE_CHANGE_KIND;
23936  // Not using RETURN(true) here, because that causes
23937  // performance issues. We don't need to do
23938  // l.priv_->unmark_as_being_compared({l,r}) here because
23939  // we haven't marked l or r as being compared yet, and
23940  // doing so has a peformance cost that shows up on
23941  // performance profiles for *big* libraries.
23943  }
23944  }
23945  else // A decl-only class is considered different from a
23946  // class definition of the same name.
23947  {
23948  if (!!def1 != !!def2)
23949  {
23950  if (k)
23951  *k |= LOCAL_TYPE_CHANGE_KIND;
23953  }
23954 
23955  // both definitions are empty
23956  if (!(l.decl_base::operator==(r)
23957  && l.type_base::operator==(r)))
23958  {
23959  if (k)
23960  *k |= LOCAL_TYPE_CHANGE_KIND;
23962  }
23963 
23964  return true;
23965  }
23966  }
23967 
23968  bool val = *def1 == *def2;
23969  if (!val)
23970  if (k)
23971  *k |= LOCAL_TYPE_CHANGE_KIND;
23972  ABG_RETURN(val);
23973  }
23974 
23975  // No need to go further if the classes have different names or
23976  // different size / alignment.
23977  if (!(l.decl_base::operator==(r) && l.type_base::operator==(r)))
23978  {
23979  if (k)
23980  *k |= LOCAL_TYPE_CHANGE_KIND;
23982  }
23983 
23984  if (types_defined_same_linux_kernel_corpus_public(l, r))
23985  return true;
23986 
23987  //TODO: Maybe remove this (cycle detection and canonical type
23988  //propagation handling) from here and have it only in the equal
23989  //overload for class_decl and union_decl because this one ( the
23990  //equal overload for class_or_union) is just a sub-routine of these
23991  //two above.
23992 #define RETURN(value) \
23993  return return_comparison_result(l, r, value, \
23994  /*propagate_canonical_type=*/false);
23995 
23997 
23999 
24000  bool result = true;
24001 
24002  //compare data_members
24003  {
24004  if (l.get_non_static_data_members().size()
24005  != r.get_non_static_data_members().size())
24006  {
24007  result = false;
24008  if (k)
24009  *k |= LOCAL_TYPE_CHANGE_KIND;
24010  else
24011  RETURN(result);
24012  }
24013 
24014  for (class_or_union::data_members::const_iterator
24015  d0 = l.get_non_static_data_members().begin(),
24016  d1 = r.get_non_static_data_members().begin();
24017  (d0 != l.get_non_static_data_members().end()
24018  && d1 != r.get_non_static_data_members().end());
24019  ++d0, ++d1)
24020  if (**d0 != **d1)
24021  {
24022  result = false;
24023  if (k)
24024  {
24025  // Report any representation change as being local.
24026  if (!types_have_similar_structure((*d0)->get_type(),
24027  (*d1)->get_type())
24028  || (*d0)->get_type() == (*d1)->get_type())
24029  *k |= LOCAL_TYPE_CHANGE_KIND;
24030  else
24031  *k |= SUBTYPE_CHANGE_KIND;
24032  }
24033  else
24034  RETURN(result);
24035  }
24036  }
24037 
24038  // Do not compare member functions. DWARF does not necessarily
24039  // all the member functions, be they virtual or not, in all
24040  // translation units. So we cannot have a clear view of them, per
24041  // class
24042 
24043  // compare member function templates
24044  {
24045  if (l.get_member_function_templates().size()
24046  != r.get_member_function_templates().size())
24047  {
24048  result = false;
24049  if (k)
24051  else
24052  RETURN(result);
24053  }
24054 
24055  for (member_function_templates::const_iterator
24056  fn_tmpl_it0 = l.get_member_function_templates().begin(),
24057  fn_tmpl_it1 = r.get_member_function_templates().begin();
24058  fn_tmpl_it0 != l.get_member_function_templates().end()
24059  && fn_tmpl_it1 != r.get_member_function_templates().end();
24060  ++fn_tmpl_it0, ++fn_tmpl_it1)
24061  if (**fn_tmpl_it0 != **fn_tmpl_it1)
24062  {
24063  result = false;
24064  if (k)
24065  {
24067  break;
24068  }
24069  else
24070  RETURN(result);
24071  }
24072  }
24073 
24074  // compare member class templates
24075  {
24076  if (l.get_member_class_templates().size()
24077  != r.get_member_class_templates().size())
24078  {
24079  result = false;
24080  if (k)
24082  else
24083  RETURN(result);
24084  }
24085 
24086  for (member_class_templates::const_iterator
24087  cl_tmpl_it0 = l.get_member_class_templates().begin(),
24088  cl_tmpl_it1 = r.get_member_class_templates().begin();
24089  cl_tmpl_it0 != l.get_member_class_templates().end()
24090  && cl_tmpl_it1 != r.get_member_class_templates().end();
24091  ++cl_tmpl_it0, ++cl_tmpl_it1)
24092  if (**cl_tmpl_it0 != **cl_tmpl_it1)
24093  {
24094  result = false;
24095  if (k)
24096  {
24098  break;
24099  }
24100  else
24101  RETURN(result);
24102  }
24103  }
24104 
24105  RETURN(result);
24106 #undef RETURN
24107 }
24108 
24109 
24110 /// Copy a method of a @ref class_or_union into a new @ref
24111 /// class_or_union.
24112 ///
24113 /// @param t the @ref class_or_union into which the method is to be copied.
24114 ///
24115 /// @param method the method to copy into @p t.
24116 ///
24117 /// @return the resulting newly copied method.
24118 method_decl_sptr
24119 copy_member_function(const class_or_union_sptr& t,
24120  const method_decl_sptr& method)
24121 {return copy_member_function(t, method.get());}
24122 
24123 
24124 /// Copy a method of a @ref class_or_union into a new @ref
24125 /// class_or_union.
24126 ///
24127 /// @param t the @ref class_or_union into which the method is to be copied.
24128 ///
24129 /// @param method the method to copy into @p t.
24130 ///
24131 /// @return the resulting newly copied method.
24132 method_decl_sptr
24133 copy_member_function(const class_or_union_sptr& t, const method_decl* method)
24134 {
24135  ABG_ASSERT(t);
24136  ABG_ASSERT(method);
24137 
24138  method_type_sptr old_type = method->get_type();
24139  ABG_ASSERT(old_type);
24140  method_type_sptr new_type(new method_type(old_type->get_return_type(),
24141  t,
24142  old_type->get_parameters(),
24143  old_type->get_is_const(),
24144  old_type->get_size_in_bits(),
24145  old_type->get_alignment_in_bits()));
24146  t->get_translation_unit()->bind_function_type_life_time(new_type);
24147 
24148  method_decl_sptr
24149  new_method(new method_decl(method->get_name(),
24150  new_type,
24151  method->is_declared_inline(),
24152  method->get_location(),
24153  method->get_linkage_name(),
24154  method->get_visibility(),
24155  method->get_binding()));
24156  new_method->set_symbol(method->get_symbol());
24157 
24158  if (class_decl_sptr class_type = is_class_type(t))
24159  class_type->add_member_function(new_method,
24160  get_member_access_specifier(*method),
24163  get_member_is_static(*method),
24164  get_member_function_is_ctor(*method),
24165  get_member_function_is_dtor(*method),
24166  get_member_function_is_const(*method));
24167  else
24168  t->add_member_function(new_method,
24169  get_member_access_specifier(*method),
24170  get_member_is_static(*method),
24171  get_member_function_is_ctor(*method),
24172  get_member_function_is_dtor(*method),
24173  get_member_function_is_const(*method));
24174  return new_method;
24175 }
24176 
24177 // </class_or_union definitions>
24178 
24179 /// @defgroup OnTheFlyCanonicalization On-the-fly Canonicalization
24180 /// @{
24181 ///
24182 /// This optimization is also known as "canonical type propagation".
24183 ///
24184 /// During the canonicalization of a type T (which doesn't yet have a
24185 /// canonical type), T is compared structurally (member-wise) against
24186 /// a type C which already has a canonical type. The comparison
24187 /// expression is C == T.
24188 ///
24189 /// During that structural comparison, if a subtype of C (which also
24190 /// already has a canonical type) is structurally compared to a
24191 /// subtype of T (which doesn't yet have a canonical type) and if they
24192 /// are equal, then we can deduce that the canonical type of the
24193 /// subtype of C is the canonical type of the subtype of C.
24194 ///
24195 /// Thus, we can canonicalize the sub-type of the T, during the
24196 /// canonicalization of T itself. That canonicalization of the
24197 /// sub-type of T is what we call the "on-the-fly canonicalization".
24198 /// It's on the fly because it happens during a comparison -- which
24199 /// itself happens during the canonicalization of T.
24200 ///
24201 /// For now this on-the-fly canonicalization only happens when
24202 /// comparing @ref class_decl and @ref function_type.
24203 ///
24204 /// Note however that there is a case when a type is *NOT* eligible to
24205 /// this canonical type propagation optimization.
24206 ///
24207 /// The reason why a type is deemed NON-eligible to the canonical type
24208 /// propagation optimization is that it "depends" on recursively
24209 /// present type. Let me explain.
24210 ///
24211 /// Suppose we have a type T that has sub-types named ST0 and ST1.
24212 /// Suppose ST1 itself has a sub-type that is T itself. In this case,
24213 /// we say that T is a recursive type, because it has T (itself) as
24214 /// one of its sub-types:
24215 ///
24216 /// <PRE>
24217 /// T
24218 /// +-- ST0
24219 /// |
24220 /// +-- ST1
24221 /// | +
24222 /// | |
24223 /// | +-- T
24224 /// |
24225 /// +-- ST2
24226 /// </PRE>
24227 ///
24228 /// ST1 is said to "depend" on T because it has T as a sub-type. But
24229 /// because T is recursive, then ST1 is said to depend on a recursive
24230 /// type. Notice however that ST0 does not depend on any recursive
24231 /// type.
24232 ///
24233 /// Now suppose we are comparing T to a type T' that has the same
24234 /// structure with sub-types ST0', ST1' and ST2'. During the
24235 /// comparison of ST1 against ST1', their sub-type T is compared
24236 /// against T'. Because T (resp. T') is a recursive type that is
24237 /// already being compared, the comparison of T against T' (as a
24238 /// subtypes of ST1 and ST1') returns true, meaning they are
24239 /// considered equal. This is done so that we don't enter an infinite
24240 /// recursion.
24241 ///
24242 /// That means ST1 is also deemed equal to ST1'. If we are in the
24243 /// course of the canonicalization of T' and thus if T (as well as as
24244 /// all of its sub-types) is already canonicalized, then the canonical
24245 /// type propagation optimization will make us propagate the canonical
24246 /// type of ST1 onto ST1'. So the canonical type of ST1' will be
24247 /// equal to the canonical type of ST1 as a result of that
24248 /// optmization.
24249 ///
24250 /// But then, later down the road, when ST2 is compared against ST2',
24251 /// let's suppose that we find out that they are different. Meaning
24252 /// that ST2 != ST2'. This means that T != T', i.e, the
24253 /// canonicalization of T' failed for now. But most importantly, it
24254 /// means that the propagation of the canonical type of ST1 to ST1'
24255 /// must now be invalidated. Meaning, ST1' must now be considered as
24256 /// not having any canonical type.
24257 ///
24258 /// In other words, during type canonicalization, if ST1' depends on a
24259 /// recursive type T', its propagated canonical type must be
24260 /// invalidated (set to nullptr) if T' appears to be different from T,
24261 /// a.k.a, the canonicalization of T' temporarily failed.
24262 ///
24263 /// This means that any sub-type that depends on recursive types and
24264 /// that has been the target of the canonical type propagation
24265 /// optimization must be tracked. If the dependant recursive type
24266 /// fails its canonicalization, then the sub-type being compared must
24267 /// have its propagated canonical type cleared. In other words, its
24268 /// propagated canonical type must be cancelled.
24269 ///
24270 /// @}
24271 
24272 
24273 /// If on-the-fly canonicalization is turned on, then this function
24274 /// sets the canonical type of its second parameter to the canonical
24275 /// type of the first parameter.
24276 ///
24277 /// @param lhs_type the type which canonical type to propagate.
24278 ///
24279 /// @param rhs_type the type which canonical type to set.
24280 static bool
24281 maybe_propagate_canonical_type(const type_base& lhs_type,
24282  const type_base& rhs_type)
24283 {
24284  const environment& env = lhs_type.get_environment();
24285 #if WITH_DEBUG_TYPE_CANONICALIZATION
24286  if (!env.priv_->use_canonical_type_comparison_)
24287  return false;
24288 #endif
24289 
24291  if (type_base_sptr canonical_type = lhs_type.get_canonical_type())
24292  if (!rhs_type.get_canonical_type())
24293  if (env.priv_->propagate_ct(lhs_type, rhs_type))
24294  return true;
24295  return false;
24296 }
24297 
24298 // <class_decl definitions>
24299 
24300 static void
24301 sort_virtual_member_functions(class_decl::member_functions& mem_fns);
24302 
24303 /// The private data for the class_decl type.
24304 struct class_decl::priv
24305 {
24306  base_specs bases_;
24307  unordered_map<string, base_spec_sptr> bases_map_;
24308  member_functions virtual_mem_fns_;
24309  virtual_mem_fn_map_type virtual_mem_fns_map_;
24310  bool is_struct_;
24311 
24312  priv()
24313  : is_struct_(false)
24314  {}
24315 
24316  priv(bool is_struct, class_decl::base_specs& bases)
24317  : bases_(bases),
24318  is_struct_(is_struct)
24319  {
24320  }
24321 
24322  priv(bool is_struct)
24323  : is_struct_(is_struct)
24324  {}
24325 };// end struct class_decl::priv
24326 
24327 /// A Constructor for instances of \ref class_decl
24328 ///
24329 /// @param env the environment we are operating from.
24330 ///
24331 /// @param name the identifier of the class.
24332 ///
24333 /// @param size_in_bits the size of an instance of class_decl, expressed
24334 /// in bits
24335 ///
24336 /// @param align_in_bits the alignment of an instance of class_decl,
24337 /// expressed in bits.
24338 ///
24339 /// @param locus the source location of declaration point this class.
24340 ///
24341 /// @param vis the visibility of instances of class_decl.
24342 ///
24343 /// @param bases the vector of base classes for this instance of class_decl.
24344 ///
24345 /// @param mbrs the vector of member types of this instance of
24346 /// class_decl.
24347 ///
24348 /// @param data_mbrs the vector of data members of this instance of
24349 /// class_decl.
24350 ///
24351 /// @param mbr_fns the vector of member functions of this instance of
24352 /// class_decl.
24353 class_decl::class_decl(const environment& env, const string& name,
24354  size_t size_in_bits, size_t align_in_bits,
24355  bool is_struct, const location& locus,
24356  visibility vis, base_specs& bases,
24357  member_types& mbr_types,
24358  data_members& data_mbrs,
24359  member_functions& mbr_fns)
24360  : type_or_decl_base(env,
24361  CLASS_TYPE
24362  | ABSTRACT_TYPE_BASE
24363  | ABSTRACT_DECL_BASE
24364  | ABSTRACT_SCOPE_TYPE_DECL
24365  | ABSTRACT_SCOPE_DECL),
24366  decl_base(env, name, locus, name, vis),
24367  type_base(env, size_in_bits, align_in_bits),
24368  class_or_union(env, name, size_in_bits, align_in_bits,
24369  locus, vis, mbr_types, data_mbrs, mbr_fns),
24370  priv_(new priv(is_struct, bases))
24371 {
24372  runtime_type_instance(this);
24373 }
24374 
24375 /// A Constructor for instances of @ref class_decl
24376 ///
24377 /// @param env the environment we are operating from.
24378 ///
24379 /// @param name the identifier of the class.
24380 ///
24381 /// @param size_in_bits the size of an instance of class_decl, expressed
24382 /// in bits
24383 ///
24384 /// @param align_in_bits the alignment of an instance of class_decl,
24385 /// expressed in bits.
24386 ///
24387 /// @param locus the source location of declaration point this class.
24388 ///
24389 /// @param vis the visibility of instances of class_decl.
24390 ///
24391 /// @param bases the vector of base classes for this instance of class_decl.
24392 ///
24393 /// @param mbrs the vector of member types of this instance of
24394 /// class_decl.
24395 ///
24396 /// @param data_mbrs the vector of data members of this instance of
24397 /// class_decl.
24398 ///
24399 /// @param mbr_fns the vector of member functions of this instance of
24400 /// class_decl.
24401 ///
24402 /// @param is_anonymous whether the newly created instance is
24403 /// anonymous.
24404 class_decl::class_decl(const environment& env, const string& name,
24405  size_t size_in_bits, size_t align_in_bits,
24406  bool is_struct, const location& locus,
24407  visibility vis, base_specs& bases,
24408  member_types& mbr_types, data_members& data_mbrs,
24409  member_functions& mbr_fns, bool is_anonymous)
24410  : type_or_decl_base(env,
24411  CLASS_TYPE
24412  | ABSTRACT_TYPE_BASE
24413  | ABSTRACT_DECL_BASE
24414  | ABSTRACT_SCOPE_TYPE_DECL
24415  | ABSTRACT_SCOPE_DECL),
24416  decl_base(env, name, locus,
24417  // If the class is anonymous then by default it won't
24418  // have a linkage name. Also, the anonymous class does
24419  // have an internal-only unique name that is generally
24420  // not taken into account when comparing classes; such a
24421  // unique internal-only name, when used as a linkage
24422  // name might introduce spurious comparison false
24423  // negatives.
24424  /*linkage_name=*/is_anonymous ? string() : name,
24425  vis),
24426  type_base(env, size_in_bits, align_in_bits),
24427  class_or_union(env, name, size_in_bits, align_in_bits,
24428  locus, vis, mbr_types, data_mbrs, mbr_fns),
24429  priv_(new priv(is_struct, bases))
24430 {
24431  runtime_type_instance(this);
24432  set_is_anonymous(is_anonymous);
24433 }
24434 
24435 /// A constructor for instances of class_decl.
24436 ///
24437 /// @param env the environment we are operating from.
24438 ///
24439 /// @param name the name of the class.
24440 ///
24441 /// @param size_in_bits the size of an instance of class_decl, expressed
24442 /// in bits
24443 ///
24444 /// @param align_in_bits the alignment of an instance of class_decl,
24445 /// expressed in bits.
24446 ///
24447 /// @param locus the source location of declaration point this class.
24448 ///
24449 /// @param vis the visibility of instances of class_decl.
24450 class_decl::class_decl(const environment& env, const string& name,
24451  size_t size_in_bits, size_t align_in_bits,
24452  bool is_struct, const location& locus,
24453  visibility vis)
24454  : type_or_decl_base(env,
24455  CLASS_TYPE
24456  | ABSTRACT_TYPE_BASE
24457  | ABSTRACT_DECL_BASE
24458  | ABSTRACT_SCOPE_TYPE_DECL
24459  | ABSTRACT_SCOPE_DECL),
24460  decl_base(env, name, locus, name, vis),
24461  type_base(env, size_in_bits, align_in_bits),
24462  class_or_union(env, name, size_in_bits, align_in_bits,
24463  locus, vis),
24464  priv_(new priv(is_struct))
24465 {
24466  runtime_type_instance(this);
24467 }
24468 
24469 /// A constructor for instances of @ref class_decl.
24470 ///
24471 /// @param env the environment we are operating from.
24472 ///
24473 /// @param name the name of the class.
24474 ///
24475 /// @param size_in_bits the size of an instance of class_decl, expressed
24476 /// in bits
24477 ///
24478 /// @param align_in_bits the alignment of an instance of class_decl,
24479 /// expressed in bits.
24480 ///
24481 /// @param locus the source location of declaration point this class.
24482 ///
24483 /// @param vis the visibility of instances of class_decl.
24484 ///
24485 /// @param is_anonymous whether the newly created instance is
24486 /// anonymous.
24487 class_decl:: class_decl(const environment& env, const string& name,
24488  size_t size_in_bits, size_t align_in_bits,
24489  bool is_struct, const location& locus,
24490  visibility vis, bool is_anonymous)
24491  : type_or_decl_base(env,
24492  CLASS_TYPE
24493  | ABSTRACT_TYPE_BASE
24494  | ABSTRACT_DECL_BASE
24495  | ABSTRACT_SCOPE_TYPE_DECL
24496  | ABSTRACT_SCOPE_DECL),
24497  decl_base(env, name, locus,
24498  // If the class is anonymous then by default it won't
24499  // have a linkage name. Also, the anonymous class does
24500  // have an internal-only unique name that is generally
24501  // not taken into account when comparing classes; such a
24502  // unique internal-only name, when used as a linkage
24503  // name might introduce spurious comparison false
24504  // negatives.
24505  /*linkage_name=*/ is_anonymous ? string() : name,
24506  vis),
24507  type_base(env, size_in_bits, align_in_bits),
24508  class_or_union(env, name, size_in_bits, align_in_bits,
24509  locus, vis),
24510  priv_(new priv(is_struct))
24511 {
24512  runtime_type_instance(this);
24513  set_is_anonymous(is_anonymous);
24514 }
24515 
24516 /// A constuctor for instances of class_decl that represent a
24517 /// declaration without definition.
24518 ///
24519 /// @param env the environment we are operating from.
24520 ///
24521 /// @param name the name of the class.
24522 ///
24523 /// @param is_declaration_only a boolean saying whether the instance
24524 /// represents a declaration only, or not.
24525 class_decl::class_decl(const environment& env, const string& name,
24526  bool is_struct, bool is_declaration_only)
24527  : type_or_decl_base(env,
24528  CLASS_TYPE
24529  | ABSTRACT_TYPE_BASE
24530  | ABSTRACT_DECL_BASE
24531  | ABSTRACT_SCOPE_TYPE_DECL
24532  | ABSTRACT_SCOPE_DECL),
24533  decl_base(env, name, location(), name),
24534  type_base(env, 0, 0),
24535  class_or_union(env, name, is_declaration_only),
24536  priv_(new priv(is_struct))
24537 {
24538  runtime_type_instance(this);
24539 }
24540 
24541 /// This method is invoked automatically right after the current
24542 /// instance of @ref class_decl has been canonicalized.
24543 ///
24544 /// Currently, the only thing it does is to sort the virtual member
24545 /// functions vector.
24546 void
24548 {
24550 
24551  for (class_decl::virtual_mem_fn_map_type::iterator i =
24552  priv_->virtual_mem_fns_map_.begin();
24553  i != priv_->virtual_mem_fns_map_.end();
24554  ++i)
24555  sort_virtual_member_functions(i->second);
24556 }
24557 
24558 /// Set the "is-struct" flag of the class.
24559 ///
24560 /// @param f the new value of the flag.
24561 void
24563 {priv_->is_struct_ = f;}
24564 
24565 /// Test if the class is a struct.
24566 ///
24567 /// @return true iff the class is a struct.
24568 bool
24570 {return priv_->is_struct_;}
24571 
24572 /// Add a base specifier to this class.
24573 ///
24574 /// @param b the new base specifier.
24575 void
24577 {
24578  priv_->bases_.push_back(b);
24579  priv_->bases_map_[b->get_base_class()->get_qualified_name()] = b;
24580 }
24581 
24582 /// Get the base specifiers for this class.
24583 ///
24584 /// @return a vector of the base specifiers.
24587 {return priv_->bases_;}
24588 
24589 /// Find a base class of a given qualified name for the current class.
24590 ///
24591 /// @param qualified_name the qualified name of the base class to look for.
24592 ///
24593 /// @return a pointer to the @ref class_decl that represents the base
24594 /// class of name @p qualified_name, if found.
24596 class_decl::find_base_class(const string& qualified_name) const
24597 {
24598  unordered_map<string, base_spec_sptr>::iterator i =
24599  priv_->bases_map_.find(qualified_name);
24600 
24601  if (i != priv_->bases_map_.end())
24602  return i->second->get_base_class();
24603 
24604  return class_decl_sptr();
24605 }
24606 
24607 /// Get the virtual member functions of this class.
24608 ///
24609 /// @param return a vector of the virtual member functions of this
24610 /// class.
24613 {return priv_->virtual_mem_fns_;}
24614 
24615 /// Get the map that associates a virtual table offset to the virtual
24616 /// member functions with that virtual table offset.
24617 ///
24618 /// Usually, there should be a 1:1 mapping between a given vtable
24619 /// offset and virtual member functions of that vtable offset. But
24620 /// because of some implementation details, there can be several C++
24621 /// destructor functions that are *generated* by compilers, for a
24622 /// given destructor that is defined in the source code. If the
24623 /// destructor is virtual then those generated functions have some
24624 /// DWARF attributes in common with the constructor that the user
24625 /// actually defined in its source code. Among those attributes are
24626 /// the vtable offset of the destructor.
24627 ///
24628 /// @return the map that associates a virtual table offset to the
24629 /// virtual member functions with that virtual table offset.
24632 {return priv_->virtual_mem_fns_map_;}
24633 
24634 /// Sort the virtual member functions by their virtual index.
24635 void
24637 {sort_virtual_member_functions(priv_->virtual_mem_fns_);}
24638 
24639 /// Getter of the pretty representation of the current instance of
24640 /// @ref class_decl.
24641 ///
24642 /// @param internal set to true if the call is intended to get a
24643 /// representation of the decl (or type) for the purpose of canonical
24644 /// type comparison. This is mainly used in the function
24645 /// type_base::get_canonical_type_for().
24646 ///
24647 /// In other words if the argument for this parameter is true then the
24648 /// call is meant for internal use (for technical use inside the
24649 /// library itself), false otherwise. If you don't know what this is
24650 /// for, then set it to false.
24651 ///
24652 /// @param qualified_name if true, names emitted in the pretty
24653 /// representation are fully qualified.
24654 ///
24655 /// @return the pretty representaion for a class_decl.
24656 string
24658  bool qualified_name) const
24659 {
24660  string cl = "class ";
24661  if (!internal && is_struct())
24662  cl = "struct ";
24663 
24664  // When computing the pretty representation for internal purposes,
24665  // if an anonymous class is named by a typedef, then consider that
24666  // it has a name, which is the typedef name.
24667  if (get_is_anonymous())
24668  {
24669  if (internal && !get_name().empty())
24670  return cl + get_type_name(this, qualified_name, /*internal=*/true);
24672  /*one_line=*/true,
24673  internal);
24674 
24675  }
24676 
24677  string result = cl;
24678  if (qualified_name)
24679  result += get_qualified_name(internal);
24680  else
24681  result += get_name();
24682 
24683  return result;
24684 }
24685 
24686 decl_base_sptr
24687 class_decl::insert_member_decl(decl_base_sptr d)
24688 {
24689  if (method_decl_sptr f = dynamic_pointer_cast<method_decl>(d))
24690  add_member_function(f, public_access,
24691  /*is_virtual=*/false,
24692  /*vtable_offset=*/0,
24693  /*is_static=*/false,
24694  /*is_ctor=*/false,
24695  /*is_dtor=*/false,
24696  /*is_const=*/false);
24697  else
24699 
24700  return d;
24701 }
24702 
24703 /// The private data structure of class_decl::base_spec.
24704 struct class_decl::base_spec::priv
24705 {
24706  class_decl_wptr base_class_;
24707  long offset_in_bits_;
24708  bool is_virtual_;
24709 
24710  priv(const class_decl_sptr& cl,
24711  long offset_in_bits,
24712  bool is_virtual)
24713  : base_class_(cl),
24714  offset_in_bits_(offset_in_bits),
24715  is_virtual_(is_virtual)
24716  {}
24717 };
24718 
24719 /// Constructor for base_spec instances.
24720 ///
24721 /// @param base the base class to consider
24722 ///
24723 /// @param a the access specifier of the base class.
24724 ///
24725 /// @param offset_in_bits if positive or null, represents the offset
24726 /// of the base in the layout of its containing type.. If negative,
24727 /// means that the current base is not laid out in its containing type.
24728 ///
24729 /// @param is_virtual if true, means that the current base class is
24730 /// virtual in it's containing type.
24731 class_decl::base_spec::base_spec(const class_decl_sptr& base,
24732  access_specifier a,
24733  long offset_in_bits,
24734  bool is_virtual)
24735  : type_or_decl_base(base->get_environment(),
24736  ABSTRACT_DECL_BASE),
24737  decl_base(base->get_environment(), base->get_name(), base->get_location(),
24738  base->get_linkage_name(), base->get_visibility()),
24739  member_base(a),
24740  priv_(new priv(base, offset_in_bits, is_virtual))
24741 {
24742  runtime_type_instance(this);
24743  set_qualified_name(base->get_qualified_name());
24744 }
24745 
24746 /// Get the base class referred to by the current base class
24747 /// specifier.
24748 ///
24749 /// @return the base class.
24752 {return priv_->base_class_.lock();}
24753 
24754 /// Getter of the "is-virtual" proprerty of the base class specifier.
24755 ///
24756 /// @return true iff this specifies a virtual base class.
24757 bool
24759 {return priv_->is_virtual_;}
24760 
24761 /// Getter of the offset of the base.
24762 ///
24763 /// @return the offset of the base.
24764 long
24766 {return priv_->offset_in_bits_;}
24767 
24768 /// Calculate the hash value for a class_decl::base_spec.
24769 ///
24770 /// @return the hash value.
24771 size_t
24773 {
24774  base_spec::hash h;
24775  return h(*this);
24776 }
24777 
24778 /// Traverses an instance of @ref class_decl::base_spec, visiting all
24779 /// the sub-types and decls that it might contain.
24780 ///
24781 /// @param v the visitor that is used to visit every IR sub-node of
24782 /// the current node.
24783 ///
24784 /// @return true if either
24785 /// - all the children nodes of the current IR node were traversed
24786 /// and the calling code should keep going with the traversing.
24787 /// - or the current IR node is already being traversed.
24788 /// Otherwise, returning false means that the calling code should not
24789 /// keep traversing the tree.
24790 bool
24792 {
24793  if (visiting())
24794  return true;
24795 
24796  if (v.visit_begin(this))
24797  {
24798  visiting(true);
24799  get_base_class()->traverse(v);
24800  visiting(false);
24801  }
24802 
24803  return v.visit_end(this);
24804 }
24805 
24806 /// Constructor for base_spec instances.
24807 ///
24808 /// Note that this constructor is for clients that don't support RTTI
24809 /// and that have a base class of type_base, but of dynamic type
24810 /// class_decl.
24811 ///
24812 /// @param base the base class to consider. Must be a pointer to an
24813 /// instance of class_decl
24814 ///
24815 /// @param a the access specifier of the base class.
24816 ///
24817 /// @param offset_in_bits if positive or null, represents the offset
24818 /// of the base in the layout of its containing type.. If negative,
24819 /// means that the current base is not laid out in its containing type.
24820 ///
24821 /// @param is_virtual if true, means that the current base class is
24822 /// virtual in it's containing type.
24823 class_decl::base_spec::base_spec(const type_base_sptr& base,
24824  access_specifier a,
24825  long offset_in_bits,
24826  bool is_virtual)
24828  ABSTRACT_DECL_BASE),
24833  member_base(a),
24834  priv_(new priv(dynamic_pointer_cast<class_decl>(base),
24835  offset_in_bits,
24836  is_virtual))
24837 {
24838  runtime_type_instance(this);
24839 }
24840 
24841 class_decl::base_spec::~base_spec() = default;
24842 
24843 /// Compares two instances of @ref class_decl::base_spec.
24844 ///
24845 /// If the two intances are different, set a bitfield to give some
24846 /// insight about the kind of differences there are.
24847 ///
24848 /// @param l the first artifact of the comparison.
24849 ///
24850 /// @param r the second artifact of the comparison.
24851 ///
24852 /// @param k a pointer to a bitfield that gives information about the
24853 /// kind of changes there are between @p l and @p r. This one is set
24854 /// iff @p k is non-null and the function returns false.
24855 ///
24856 /// Please note that setting k to a non-null value does have a
24857 /// negative performance impact because even if @p l and @p r are not
24858 /// equal, the function keeps up the comparison in order to determine
24859 /// the different kinds of ways in which they are different.
24860 ///
24861 /// @return true if @p l equals @p r, false otherwise.
24862 bool
24864  const class_decl::base_spec& r,
24865  change_kind* k)
24866 {
24867  if (!l.member_base::operator==(r))
24868  {
24869  if (k)
24870  *k |= LOCAL_TYPE_CHANGE_KIND;
24872  }
24873 
24874  ABG_RETURN((*l.get_base_class() == *r.get_base_class()));
24875 }
24876 
24877 /// Comparison operator for @ref class_decl::base_spec.
24878 ///
24879 /// @param other the instance of @ref class_decl::base_spec to compare
24880 /// against.
24881 ///
24882 /// @return true if the current instance of @ref class_decl::base_spec
24883 /// equals @p other.
24884 bool
24886 {
24887  const class_decl::base_spec* o =
24888  dynamic_cast<const class_decl::base_spec*>(&other);
24889 
24890  if (!o)
24891  return false;
24892 
24893  return equals(*this, *o, 0);
24894 }
24895 
24896 /// Comparison operator for @ref class_decl::base_spec.
24897 ///
24898 /// @param other the instance of @ref class_decl::base_spec to compare
24899 /// against.
24900 ///
24901 /// @return true if the current instance of @ref class_decl::base_spec
24902 /// equals @p other.
24903 bool
24905 {
24906  const class_decl::base_spec* o =
24907  dynamic_cast<const class_decl::base_spec*>(&other);
24908  if (!o)
24909  return false;
24910 
24911  return operator==(static_cast<const decl_base&>(*o));
24912 }
24913 
24914 mem_fn_context_rel::~mem_fn_context_rel()
24915 {
24916 }
24917 
24918 /// A constructor for instances of method_decl.
24919 ///
24920 /// @param name the name of the method.
24921 ///
24922 /// @param type the type of the method.
24923 ///
24924 /// @param declared_inline whether the method was
24925 /// declared inline or not.
24926 ///
24927 /// @param locus the source location of the method.
24928 ///
24929 /// @param linkage_name the mangled name of the method.
24930 ///
24931 /// @param vis the visibility of the method.
24932 ///
24933 /// @param bind the binding of the method.
24934 method_decl::method_decl(const string& name,
24935  method_type_sptr type,
24936  bool declared_inline,
24937  const location& locus,
24938  const string& linkage_name,
24939  visibility vis,
24940  binding bind)
24942  METHOD_DECL
24943  | ABSTRACT_DECL_BASE
24944  |FUNCTION_DECL),
24945  decl_base(type->get_environment(), name, locus, linkage_name, vis),
24946  function_decl(name, static_pointer_cast<function_type>(type),
24947  declared_inline, locus, linkage_name, vis, bind)
24948 {
24949  runtime_type_instance(this);
24950  set_context_rel(new mem_fn_context_rel(0));
24951  set_member_function_is_const(*this, type->get_is_const());
24952 }
24953 
24954 /// A constructor for instances of method_decl.
24955 ///
24956 /// @param name the name of the method.
24957 ///
24958 /// @param type the type of the method. Must be an instance of
24959 /// method_type.
24960 ///
24961 /// @param declared_inline whether the method was
24962 /// declared inline or not.
24963 ///
24964 /// @param locus the source location of the method.
24965 ///
24966 /// @param linkage_name the mangled name of the method.
24967 ///
24968 /// @param vis the visibility of the method.
24969 ///
24970 /// @param bind the binding of the method.
24971 method_decl::method_decl(const string& name,
24972  function_type_sptr type,
24973  bool declared_inline,
24974  const location& locus,
24975  const string& linkage_name,
24976  visibility vis,
24977  binding bind)
24978  : type_or_decl_base(type->get_environment(),
24979  METHOD_DECL
24980  | ABSTRACT_DECL_BASE
24981  | FUNCTION_DECL),
24982  decl_base(type->get_environment(), name, locus, linkage_name, vis),
24983  function_decl(name, static_pointer_cast<function_type>
24984  (dynamic_pointer_cast<method_type>(type)),
24985  declared_inline, locus, linkage_name, vis, bind)
24986 {
24987  runtime_type_instance(this);
24988  set_context_rel(new mem_fn_context_rel(0));
24989 }
24990 
24991 /// A constructor for instances of method_decl.
24992 ///
24993 /// @param name the name of the method.
24994 ///
24995 /// @param type the type of the method. Must be an instance of
24996 /// method_type.
24997 ///
24998 /// @param declared_inline whether the method was
24999 /// declared inline or not.
25000 ///
25001 /// @param locus the source location of the method.
25002 ///
25003 /// @param linkage_name the mangled name of the method.
25004 ///
25005 /// @param vis the visibility of the method.
25006 ///
25007 /// @param bind the binding of the method.
25008 method_decl::method_decl(const string& name,
25009  type_base_sptr type,
25010  bool declared_inline,
25011  const location& locus,
25012  const string& linkage_name,
25013  visibility vis,
25014  binding bind)
25015  : type_or_decl_base(type->get_environment(),
25016  METHOD_DECL
25017  | ABSTRACT_DECL_BASE
25018  | FUNCTION_DECL),
25019  decl_base(type->get_environment(), name, locus, linkage_name, vis),
25020  function_decl(name, static_pointer_cast<function_type>
25021  (dynamic_pointer_cast<method_type>(type)),
25022  declared_inline, locus, linkage_name, vis, bind)
25023 {
25024  runtime_type_instance(this);
25025  set_context_rel(new mem_fn_context_rel(0));
25026 }
25027 
25028 /// Set the linkage name of the method.
25029 ///
25030 /// @param l the new linkage name of the method.
25031 void
25033 {
25034  string old_lname = get_linkage_name();
25036  // Update the linkage_name -> member function map of the containing
25037  // class declaration.
25038  if (!l.empty())
25039  {
25040  method_type_sptr t = get_type();
25041  class_or_union_sptr cl = t->get_class_type();
25042  method_decl_sptr m(this, sptr_utils::noop_deleter());
25043  cl->priv_->mem_fns_map_[l] = m;
25044  if (!old_lname.empty() && l != old_lname)
25045  {
25046  if (method_decl_sptr m = cl->find_member_function_sptr(old_lname))
25047  {
25048  ABG_ASSERT(m.get() == this);
25049  cl->priv_->mem_fns_map_.erase(old_lname);
25050  }
25051  }
25052  }
25053 }
25054 
25055 method_decl::~method_decl()
25056 {}
25057 
25058 const method_type_sptr
25060 {
25061  method_type_sptr result;
25063  result = dynamic_pointer_cast<method_type>(function_decl::get_type());
25064  return result;
25065 }
25066 
25067 /// Set the containing class of a method_decl.
25068 ///
25069 /// @param scope the new containing class_decl.
25070 void
25071 method_decl::set_scope(scope_decl* scope)
25072 {
25073  if (!get_context_rel())
25074  set_context_rel(new mem_fn_context_rel(scope));
25075  else
25076  get_context_rel()->set_scope(scope);
25077 }
25078 
25079 /// Equality operator for @ref method_decl_sptr.
25080 ///
25081 /// This is a deep equality operator, as it compares the @ref
25082 /// method_decl that is pointed-to by the smart pointer.
25083 ///
25084 /// @param l the left-hand side argument of the equality operator.
25085 ///
25086 /// @param r the righ-hand side argument of the equality operator.
25087 ///
25088 /// @return true iff @p l equals @p r.
25089 bool
25090 operator==(const method_decl_sptr& l, const method_decl_sptr& r)
25091 {
25092  if (l.get() == r.get())
25093  return true;
25094  if (!!l != !!r)
25095  return false;
25096 
25097  return *l == *r;
25098 }
25099 
25100 /// Inequality operator for @ref method_decl_sptr.
25101 ///
25102 /// This is a deep equality operator, as it compares the @ref
25103 /// method_decl that is pointed-to by the smart pointer.
25104 ///
25105 /// @param l the left-hand side argument of the equality operator.
25106 ///
25107 /// @param r the righ-hand side argument of the equality operator.
25108 ///
25109 /// @return true iff @p l differs from @p r.
25110 bool
25111 operator!=(const method_decl_sptr& l, const method_decl_sptr& r)
25112 {return !operator==(l, r);}
25113 
25114 /// Test if a function_decl is actually a method_decl.
25115 ///
25116 ///@param d the @ref function_decl to consider.
25117 ///
25118 /// @return the method_decl sub-object of @p d if inherits
25119 /// a method_decl type.
25120 method_decl*
25122 {
25123  return dynamic_cast<method_decl*>
25124  (const_cast<type_or_decl_base*>(d));
25125 }
25126 
25127 /// Test if a function_decl is actually a method_decl.
25128 ///
25129 ///@param d the @ref function_decl to consider.
25130 ///
25131 /// @return the method_decl sub-object of @p d if inherits
25132 /// a method_decl type.
25133 method_decl*
25135 {return is_method_decl(&d);}
25136 
25137 /// Test if a function_decl is actually a method_decl.
25138 ///
25139 ///@param d the @ref function_decl to consider.
25140 ///
25141 /// @return the method_decl sub-object of @p d if inherits
25142 /// a method_decl type.
25143 method_decl_sptr
25145 {return dynamic_pointer_cast<method_decl>(d);}
25146 
25147 /// A "less than" functor to sort a vector of instances of
25148 /// method_decl that are virtual.
25149 struct virtual_member_function_less_than
25150 {
25151  /// The less than operator. First, it sorts the methods by their
25152  /// vtable index. If they have the same vtable index, it sorts them
25153  /// by the name of their ELF symbol. If they don't have elf
25154  /// symbols, it sorts them by considering their pretty
25155  /// representation.
25156  ///
25157  /// Note that this method expects virtual methods.
25158  ///
25159  /// @param f the first method to consider.
25160  ///
25161  /// @param s the second method to consider.
25162  ///
25163  /// @return true if method @p is less than method @s.
25164  bool
25165  operator()(const method_decl& f,
25166  const method_decl& s)
25167  {
25170 
25171  ssize_t f_offset = get_member_function_vtable_offset(f);
25172  ssize_t s_offset = get_member_function_vtable_offset(s);
25173  if (f_offset != s_offset) return f_offset < s_offset;
25174 
25175  string fn, sn;
25176  // Try the linkage names (important for destructors).
25177  fn = f.get_linkage_name();
25178  sn = s.get_linkage_name();
25179  if (fn != sn) return fn < sn;
25180 
25181  // If the functions have symbols, then compare their symbol-id
25182  // string.
25183  elf_symbol_sptr f_sym = f.get_symbol();
25184  elf_symbol_sptr s_sym = s.get_symbol();
25185  if ((!f_sym) != (!s_sym)) return !f_sym;
25186  if (f_sym && s_sym)
25187  {
25188  fn = f_sym->get_id_string();
25189  sn = s_sym->get_id_string();
25190  if (fn != sn) return fn < sn;
25191  }
25192 
25193  // None of the functions have symbols or linkage names that
25194  // distinguish them, so compare their pretty representation.
25195  fn = f.get_pretty_representation();
25196  sn = s.get_pretty_representation();
25197  if (fn != sn) return fn < sn;
25198 
25199  /// If it's just the file paths that are different then sort them
25200  /// too.
25201  string fn_filepath, sn_filepath;
25202  unsigned line = 0, column = 0;
25203  location fn_loc = f.get_location(), sn_loc = s.get_location();
25204  if (fn_loc)
25205  fn_loc.expand(fn_filepath, line, column);
25206  if (sn_loc)
25207  sn_loc.expand(sn_filepath, line, column);
25208  return fn_filepath < sn_filepath;
25209  }
25210 
25211  /// The less than operator. First, it sorts the methods by their
25212  /// vtable index. If they have the same vtable index, it sorts them
25213  /// by the name of their ELF symbol. If they don't have elf
25214  /// symbols, it sorts them by considering their pretty
25215  /// representation.
25216  ///
25217  /// Note that this method expects to take virtual methods.
25218  ///
25219  /// @param f the first method to consider.
25220  ///
25221  /// @param s the second method to consider.
25222  bool
25223  operator()(const method_decl_sptr f,
25224  const method_decl_sptr s)
25225  {return operator()(*f, *s);}
25226 }; // end struct virtual_member_function_less_than
25227 
25228 /// Sort a vector of instances of virtual member functions.
25229 ///
25230 /// @param mem_fns the vector of member functions to sort.
25231 static void
25232 sort_virtual_member_functions(class_decl::member_functions& mem_fns)
25233 {
25234  virtual_member_function_less_than lt;
25235  std::stable_sort(mem_fns.begin(), mem_fns.end(), lt);
25236 }
25237 
25238 /// Add a member function to the current instance of @ref class_or_union.
25239 ///
25240 /// @param f a method_decl to add to the current class. This function
25241 /// should not have been already added to a scope.
25242 ///
25243 /// @param access the access specifier for the member function to add.
25244 ///
25245 /// @param is_virtual if this is true then it means the function @p f
25246 /// is a virtual function. That also means that the current instance
25247 /// of @ref class_or_union is actually an instance of @ref class_decl.
25248 ///
25249 /// @param vtable_offset the offset of the member function in the
25250 /// virtual table. This parameter is taken into account only if @p
25251 /// is_virtual is true.
25252 ///
25253 /// @param is_static whether the member function is static.
25254 ///
25255 /// @param is_ctor whether the member function is a constructor.
25256 ///
25257 /// @param is_dtor whether the member function is a destructor.
25258 ///
25259 /// @param is_const whether the member function is const.
25260 void
25262  access_specifier a,
25263  bool is_virtual,
25264  size_t vtable_offset,
25265  bool is_static, bool is_ctor,
25266  bool is_dtor, bool is_const)
25267 {
25268  add_member_function(f, a, is_static, is_ctor,
25269  is_dtor, is_const);
25270 
25271  if (class_decl* klass = is_class_type(this))
25272  {
25273  set_member_function_is_virtual(f, is_virtual);
25274  if (is_virtual)
25275  {
25276  set_member_function_vtable_offset(f, vtable_offset);
25277  sort_virtual_member_functions(klass->priv_->virtual_mem_fns_);
25278  }
25279  }
25280 }
25281 
25282 /// When a virtual member function has seen its virtualness set by
25283 /// set_member_function_is_virtual(), this function ensures that the
25284 /// member function is added to the specific vectors and maps of
25285 /// virtual member function of its class.
25286 ///
25287 /// @param method the method to fixup.
25288 void
25289 fixup_virtual_member_function(method_decl_sptr method)
25290 {
25291  if (!method || !get_member_function_is_virtual(method))
25292  return;
25293 
25294  class_decl_sptr klass = is_class_type(method->get_type()->get_class_type());
25295 
25296  class_decl::member_functions::const_iterator m;
25297  for (m = klass->priv_->virtual_mem_fns_.begin();
25298  m != klass->priv_->virtual_mem_fns_.end();
25299  ++m)
25300  if (m->get() == method.get()
25301  || (*m)->get_linkage_name() == method->get_linkage_name())
25302  break;
25303  if (m == klass->priv_->virtual_mem_fns_.end())
25304  klass->priv_->virtual_mem_fns_.push_back(method);
25305 
25306  // Build or udpate the map that associates a vtable offset to the
25307  // number of virtual member functions that "point" to it.
25308  ssize_t voffset = get_member_function_vtable_offset(method);
25309  if (voffset == -1)
25310  return;
25311 
25312  class_decl::virtual_mem_fn_map_type::iterator i =
25313  klass->priv_->virtual_mem_fns_map_.find(voffset);
25314  if (i == klass->priv_->virtual_mem_fns_map_.end())
25315  {
25316  class_decl::member_functions virtual_mem_fns_at_voffset;
25317  virtual_mem_fns_at_voffset.push_back(method);
25318  klass->priv_->virtual_mem_fns_map_[voffset] = virtual_mem_fns_at_voffset;
25319  }
25320  else
25321  {
25322  for (m = i->second.begin() ; m != i->second.end(); ++m)
25323  if (m->get() == method.get()
25324  || (*m)->get_linkage_name() == method->get_linkage_name())
25325  break;
25326  if (m == i->second.end())
25327  i->second.push_back(method);
25328  }
25329 }
25330 
25331 /// Return true iff the class has no entity in its scope.
25332 bool
25334 {return priv_->bases_.empty() && has_no_member();}
25335 
25336 /// Test if the current instance of @ref class_decl has virtual member
25337 /// functions.
25338 ///
25339 /// @return true iff the current instance of @ref class_decl has
25340 /// virtual member functions.
25341 bool
25343 {return !get_virtual_mem_fns().empty();}
25344 
25345 /// Test if the current instance of @ref class_decl has at least one
25346 /// virtual base.
25347 ///
25348 /// @return true iff the current instance of @ref class_decl has a
25349 /// virtual member function.
25350 bool
25352 {
25353  for (base_specs::const_iterator b = get_base_specifiers().begin();
25354  b != get_base_specifiers().end();
25355  ++b)
25356  if ((*b)->get_is_virtual()
25357  || (*b)->get_base_class()->has_virtual_bases())
25358  return true;
25359 
25360  return false;
25361 }
25362 
25363 /// Test if the current instance has a vtable.
25364 ///
25365 /// This is only valid for a C++ program.
25366 ///
25367 /// Basically this function checks if the class has either virtual
25368 /// functions, or virtual bases.
25369 bool
25371 {
25373  || has_virtual_bases())
25374  return true;
25375  return false;
25376 }
25377 
25378 /// Get the highest vtable offset of all the virtual methods of the
25379 /// class.
25380 ///
25381 /// @return the highest vtable offset of all the virtual methods of
25382 /// the class.
25383 ssize_t
25385 {
25386  ssize_t offset = -1;
25387  for (class_decl::virtual_mem_fn_map_type::const_iterator e =
25388  get_virtual_mem_fns_map().begin();
25389  e != get_virtual_mem_fns_map().end();
25390  ++e)
25391  if (e->first > offset)
25392  offset = e->first;
25393 
25394  return offset;
25395 }
25396 
25397 /// Return the hash value for the current instance.
25398 ///
25399 /// @return the hash value.
25400 size_t
25402 {
25403  class_decl::hash hash_class;
25404  return hash_class(this);
25405 }
25406 
25407 /// Test if two methods are equal without taking their symbol or
25408 /// linkage name into account.
25409 ///
25410 /// @param f the first method.
25411 ///
25412 /// @param s the second method.
25413 ///
25414 /// @return true iff @p f equals @p s without taking their linkage
25415 /// name or symbol into account.
25416 static bool
25417 methods_equal_modulo_elf_symbol(const method_decl_sptr& f,
25418  const method_decl_sptr& s)
25419 {
25420  method_decl_sptr first = f, second = s;
25421  elf_symbol_sptr saved_first_elf_symbol =
25422  first->get_symbol();
25423  elf_symbol_sptr saved_second_elf_symbol =
25424  second->get_symbol();
25425  interned_string saved_first_linkage_name =
25426  first->get_linkage_name();
25427  interned_string saved_second_linkage_name =
25428  second->get_linkage_name();
25429 
25430  first->set_symbol(elf_symbol_sptr());
25431  first->set_linkage_name("");
25432  second->set_symbol(elf_symbol_sptr());
25433  second->set_linkage_name("");
25434 
25435  bool equal = *first == *second;
25436 
25437  first->set_symbol(saved_first_elf_symbol);
25438  first->set_linkage_name(saved_first_linkage_name);
25439  second->set_symbol(saved_second_elf_symbol);
25440  second->set_linkage_name(saved_second_linkage_name);
25441 
25442  return equal;
25443 }
25444 
25445 /// Test if a given method is equivalent to at least of other method
25446 /// that is in a vector of methods.
25447 ///
25448 /// Note that "equivalent" here means being equal without taking the
25449 /// linkage name or the symbol of the methods into account.
25450 ///
25451 /// This is a sub-routine of the 'equals' function that compares @ref
25452 /// class_decl.
25453 ///
25454 /// @param method the method to compare.
25455 ///
25456 /// @param fns the vector of functions to compare @p method against.
25457 ///
25458 /// @return true iff @p is equivalent to at least one method in @p
25459 /// fns.
25460 static bool
25461 method_matches_at_least_one_in_vector(const method_decl_sptr& method,
25462  const class_decl::member_functions& fns)
25463 {
25464  for (class_decl::member_functions::const_iterator i = fns.begin();
25465  i != fns.end();
25466  ++i)
25467  // Note that the comparison must be done in this order: method ==
25468  // *i This is to keep the consistency of the comparison. It's
25469  // important especially when doing type canonicalization. The
25470  // already canonicalize type is the left operand, and the type
25471  // being canonicalized is the right operand. This comes from the
25472  // code in type_base::get_canonical_type_for().
25473  if (methods_equal_modulo_elf_symbol(method, *i))
25474  return true;
25475 
25476  return false;
25477 }
25478 
25479 /// Cancel the canonical type that was propagated.
25480 ///
25481 /// If we are in the process of comparing a type for the purpose of
25482 /// canonicalization, and if that type has been the target of the
25483 /// canonical type propagation optimization, then clear the propagated
25484 /// canonical type. See @ref OnTheFlyCanonicalization for more about
25485 /// the canonical type optimization
25486 ///
25487 /// @param t the type to consider.
25488 static bool
25489 maybe_cancel_propagated_canonical_type(const class_or_union& t)
25490 {
25491  const environment& env = t.get_environment();
25492  if (env.do_on_the_fly_canonicalization())
25493  if (is_type(&t)->priv_->canonical_type_propagated())
25494  {
25495  is_type(&t)->priv_->clear_propagated_canonical_type();
25496  env.priv_->remove_from_types_with_non_confirmed_propagated_ct(&t);
25497  return true;
25498  }
25499  return false;
25500 }
25501 
25502 /// Compares two instances of @ref class_decl.
25503 ///
25504 /// If the two intances are different, set a bitfield to give some
25505 /// insight about the kind of differences there are.
25506 ///
25507 /// @param l the first artifact of the comparison.
25508 ///
25509 /// @param r the second artifact of the comparison.
25510 ///
25511 /// @param k a pointer to a bitfield that gives information about the
25512 /// kind of changes there are between @p l and @p r. This one is set
25513 /// iff @p k is non-null and the function returns false.
25514 ///
25515 /// Please note that setting k to a non-null value does have a
25516 /// negative performance impact because even if @p l and @p r are not
25517 /// equal, the function keeps up the comparison in order to determine
25518 /// the different kinds of ways in which they are different.
25519 ///
25520 /// @return true if @p l equals @p r, false otherwise.
25521 bool
25522 equals(const class_decl& l, const class_decl& r, change_kind* k)
25523 {
25524  {
25525  // First of all, let's see if these two types haven't already been
25526  // compared. If so, and if the result of the comparison has been
25527  // cached, let's just re-use it, rather than comparing them all
25528  // over again.
25529  bool result = false;
25530  if (l.get_environment().priv_->is_type_comparison_cached(l, r, result))
25531  ABG_RETURN(result);
25532  }
25533 
25534  // if one of the classes is declaration-only then we take a fast
25535  // path here.
25537  ABG_RETURN(equals(static_cast<const class_or_union&>(l),
25538  static_cast<const class_or_union&>(r),
25539  k));
25540 
25541  bool had_canonical_type = !!r.get_naked_canonical_type();
25542  bool result = true;
25543  if (!equals(static_cast<const class_or_union&>(l),
25544  static_cast<const class_or_union&>(r),
25545  k))
25546  {
25547  result = false;
25548  if (!k)
25549  ABG_RETURN(result);
25550  }
25551 
25552  // If comparing the class_or_union 'part' of the type led to
25553  // canonical type propagation, then cancel that because it's too
25554  // early to do that at this point. We still need to compare bases
25555  // virtual members.
25556  if (!had_canonical_type)
25557  maybe_cancel_propagated_canonical_type(r);
25558 
25560 
25562 
25563 #define RETURN(value) CACHE_AND_RETURN_COMPARISON_RESULT(value)
25564 
25565  // Compare bases.
25566  if (l.get_base_specifiers().size() != r.get_base_specifiers().size())
25567  {
25568  result = false;
25569  if (k)
25570  *k |= LOCAL_TYPE_CHANGE_KIND;
25571  else
25572  RETURN(result);
25573  }
25574 
25575  for (class_decl::base_specs::const_iterator
25576  b0 = l.get_base_specifiers().begin(),
25577  b1 = r.get_base_specifiers().begin();
25578  (b0 != l.get_base_specifiers().end()
25579  && b1 != r.get_base_specifiers().end());
25580  ++b0, ++b1)
25581  if (*b0 != *b1)
25582  {
25583  result = false;
25584  if (k)
25585  {
25586  if (!types_have_similar_structure((*b0)->get_base_class().get(),
25587  (*b1)->get_base_class().get()))
25588  *k |= LOCAL_TYPE_CHANGE_KIND;
25589  else
25590  *k |= SUBTYPE_CHANGE_KIND;
25591  break;
25592  }
25593  RETURN(result);
25594  }
25595 
25596  // Compare virtual member functions
25597 
25598  // We look at the map that associates a given vtable offset to a
25599  // vector of virtual member functions that point to that offset.
25600  //
25601  // This is because there are cases where several functions can
25602  // point to the same virtual table offset.
25603  //
25604  // This is usually the case for virtual destructors. Even though
25605  // there can be only one virtual destructor declared in source
25606  // code, there are actually potentially up to three generated
25607  // functions for that destructor. Some of these generated
25608  // functions can be clones of other functions that are among those
25609  // generated ones. In any cases, they all have the same
25610  // properties, including the vtable offset property.
25611 
25612  // So, there should be the same number of different vtable
25613  // offsets, the size of two maps must be equals.
25614  if (l.get_virtual_mem_fns_map().size()
25615  != r.get_virtual_mem_fns_map().size())
25616  {
25617  result = false;
25618  if (k)
25620  else
25621  RETURN(result);
25622  }
25623 
25624  // Then, each virtual member function of a given vtable offset in
25625  // the first class type, must match an equivalent virtual member
25626  // function of a the same vtable offset in the second class type.
25627  //
25628  // By "match", I mean that the two virtual member function should
25629  // be equal if we don't take into account their symbol name or
25630  // their linkage name. This is because two destructor functions
25631  // clones (for instance) might have different linkage name, but
25632  // are still equivalent if their other properties are the same.
25633  for (class_decl::virtual_mem_fn_map_type::const_iterator first_v_fn_entry =
25634  l.get_virtual_mem_fns_map().begin();
25635  first_v_fn_entry != l.get_virtual_mem_fns_map().end();
25636  ++first_v_fn_entry)
25637  {
25638  unsigned voffset = first_v_fn_entry->first;
25639  const class_decl::member_functions& first_vfns =
25640  first_v_fn_entry->second;
25641 
25642  const class_decl::virtual_mem_fn_map_type::const_iterator
25643  second_v_fn_entry = r.get_virtual_mem_fns_map().find(voffset);
25644 
25645  if (second_v_fn_entry == r.get_virtual_mem_fns_map().end())
25646  {
25647  result = false;
25648  if (k)
25650  RETURN(result);
25651  }
25652 
25653  const class_decl::member_functions& second_vfns =
25654  second_v_fn_entry->second;
25655 
25656  bool matches = false;
25657  for (class_decl::member_functions::const_iterator i =
25658  first_vfns.begin();
25659  i != first_vfns.end();
25660  ++i)
25661  if (method_matches_at_least_one_in_vector(*i, second_vfns))
25662  {
25663  matches = true;
25664  break;
25665  }
25666 
25667  if (!matches)
25668  {
25669  result = false;
25670  if (k)
25671  *k |= SUBTYPE_CHANGE_KIND;
25672  else
25673  RETURN(result);
25674  }
25675  }
25676 
25677  RETURN(result);
25678 #undef RETURN
25679 }
25680 
25681 /// Copy a method of a class into a new class.
25682 ///
25683 /// @param klass the class into which the method is to be copied.
25684 ///
25685 /// @param method the method to copy into @p klass.
25686 ///
25687 /// @return the resulting newly copied method.
25688 method_decl_sptr
25689 copy_member_function(const class_decl_sptr& clazz, const method_decl_sptr& f)
25690 {return copy_member_function(static_pointer_cast<class_or_union>(clazz), f);}
25691 
25692 /// Copy a method of a class into a new class.
25693 ///
25694 /// @param klass the class into which the method is to be copied.
25695 ///
25696 /// @param method the method to copy into @p klass.
25697 ///
25698 /// @return the resulting newly copied method.
25699 method_decl_sptr
25701 {return copy_member_function(static_pointer_cast<class_or_union>(clazz), f);}
25702 
25703 /// Comparison operator for @ref class_decl.
25704 ///
25705 /// @param other the instance of @ref class_decl to compare against.
25706 ///
25707 /// @return true iff the current instance of @ref class_decl equals @p
25708 /// other.
25709 bool
25711 {
25712  const class_decl* op = is_class_type(&other);
25713  if (!op)
25714  return false;
25715 
25716  // If this is a decl-only type (and thus with no canonical type),
25717  // use the canonical type of the definition, if any.
25718  const class_decl *l = 0;
25720  l = dynamic_cast<const class_decl*>(get_naked_definition_of_declaration());
25721  if (l == 0)
25722  l = this;
25723 
25724  ABG_ASSERT(l);
25725 
25726  // Likewise for the other type.
25727  const class_decl *r = 0;
25728  if (op->get_is_declaration_only())
25729  r = dynamic_cast<const class_decl*>(op->get_naked_definition_of_declaration());
25730  if (r == 0)
25731  r = op;
25732 
25733  ABG_ASSERT(r);
25734 
25735  return try_canonical_compare(l, r);
25736 }
25737 
25738 /// Equality operator for class_decl.
25739 ///
25740 /// Re-uses the equality operator that takes a decl_base.
25741 ///
25742 /// @param other the other class_decl to compare against.
25743 ///
25744 /// @return true iff the current instance equals the other one.
25745 bool
25747 {
25748  const decl_base* o = is_decl(&other);
25749  if (!o)
25750  return false;
25751  return *this == *o;
25752 }
25753 
25754 /// Equality operator for class_decl.
25755 ///
25756 /// Re-uses the equality operator that takes a decl_base.
25757 ///
25758 /// @param other the other class_decl to compare against.
25759 ///
25760 /// @return true iff the current instance equals the other one.
25761 bool
25763 {
25764  const decl_base& o = other;
25765  return *this == o;
25766 }
25767 
25768 /// Comparison operator for @ref class_decl.
25769 ///
25770 /// @param other the instance of @ref class_decl to compare against.
25771 ///
25772 /// @return true iff the current instance of @ref class_decl equals @p
25773 /// other.
25774 bool
25776 {
25777  const decl_base& o = other;
25778  return *this == o;
25779 }
25780 
25781 /// Turn equality of shared_ptr of class_decl into a deep equality;
25782 /// that is, make it compare the pointed to objects too.
25783 ///
25784 /// @param l the shared_ptr of class_decl on left-hand-side of the
25785 /// equality.
25786 ///
25787 /// @param r the shared_ptr of class_decl on right-hand-side of the
25788 /// equality.
25789 ///
25790 /// @return true if the class_decl pointed to by the shared_ptrs are
25791 /// equal, false otherwise.
25792 bool
25794 {
25795  if (l.get() == r.get())
25796  return true;
25797  if (!!l != !!r)
25798  return false;
25799 
25800  return *l == *r;
25801 }
25802 
25803 /// Turn inequality of shared_ptr of class_decl into a deep equality;
25804 /// that is, make it compare the pointed to objects too.
25805 ///
25806 /// @param l the shared_ptr of class_decl on left-hand-side of the
25807 /// equality.
25808 ///
25809 /// @param r the shared_ptr of class_decl on right-hand-side of the
25810 /// equality.
25811 ///
25812 /// @return true if the class_decl pointed to by the shared_ptrs are
25813 /// different, false otherwise.
25814 bool
25816 {return !operator==(l, r);}
25817 
25818 /// Turn equality of shared_ptr of class_or_union into a deep
25819 /// equality; that is, make it compare the pointed to objects too.
25820 ///
25821 /// @param l the left-hand-side operand of the operator
25822 ///
25823 /// @param r the right-hand-side operand of the operator.
25824 ///
25825 /// @return true iff @p l equals @p r.
25826 bool
25827 operator==(const class_or_union_sptr& l, const class_or_union_sptr& r)
25828 {
25829  if (l.get() == r.get())
25830  return true;
25831  if (!!l != !!r)
25832  return false;
25833 
25834  return *l == *r;
25835 }
25836 
25837 /// Turn inequality of shared_ptr of class_or_union into a deep
25838 /// equality; that is, make it compare the pointed to objects too.
25839 ///
25840 /// @param l the left-hand-side operand of the operator
25841 ///
25842 /// @param r the right-hand-side operand of the operator.
25843 ///
25844 /// @return true iff @p l is different from @p r.
25845 bool
25846 operator!=(const class_or_union_sptr& l, const class_or_union_sptr& r)
25847 {return !operator==(l, r);}
25848 
25849 /// This implements the ir_traversable_base::traverse pure virtual
25850 /// function.
25851 ///
25852 /// @param v the visitor used on the current instance and on its
25853 /// members.
25854 ///
25855 /// @return true if the entire IR node tree got traversed, false
25856 /// otherwise.
25857 bool
25859 {
25860  if (v.type_node_has_been_visited(this))
25861  return true;
25862 
25863  if (visiting())
25864  return true;
25865 
25866  if (v.visit_begin(this))
25867  {
25868  visiting(true);
25869  bool stop = false;
25870 
25871  for (base_specs::const_iterator i = get_base_specifiers().begin();
25872  i != get_base_specifiers().end();
25873  ++i)
25874  {
25875  if (!(*i)->traverse(v))
25876  {
25877  stop = true;
25878  break;
25879  }
25880  }
25881 
25882  if (!stop)
25883  for (data_members::const_iterator i = get_data_members().begin();
25884  i != get_data_members().end();
25885  ++i)
25886  if (!(*i)->traverse(v))
25887  {
25888  stop = true;
25889  break;
25890  }
25891 
25892  if (!stop)
25893  for (member_functions::const_iterator i= get_member_functions().begin();
25894  i != get_member_functions().end();
25895  ++i)
25896  if (!(*i)->traverse(v))
25897  {
25898  stop = true;
25899  break;
25900  }
25901 
25902  if (!stop)
25903  for (member_types::const_iterator i = get_member_types().begin();
25904  i != get_member_types().end();
25905  ++i)
25906  if (!(*i)->traverse(v))
25907  {
25908  stop = true;
25909  break;
25910  }
25911 
25912  if (!stop)
25913  for (member_function_templates::const_iterator i =
25915  i != get_member_function_templates().end();
25916  ++i)
25917  if (!(*i)->traverse(v))
25918  {
25919  stop = true;
25920  break;
25921  }
25922 
25923  if (!stop)
25924  for (member_class_templates::const_iterator i =
25925  get_member_class_templates().begin();
25926  i != get_member_class_templates().end();
25927  ++i)
25928  if (!(*i)->traverse(v))
25929  {
25930  stop = true;
25931  break;
25932  }
25933  visiting(false);
25934  }
25935 
25936  bool result = v.visit_end(this);
25937  v.mark_type_node_as_visited(this);
25938  return result;
25939 }
25940 
25941 /// Destructor of the @ref class_decl type.
25943 {delete priv_;}
25944 
25945 context_rel::~context_rel()
25946 {}
25947 
25948 bool
25949 member_base::operator==(const member_base& o) const
25950 {
25952  && get_is_static() == o.get_is_static());
25953 }
25954 
25955 /// Equality operator for smart pointers to @ref
25956 /// class_decl::base_specs.
25957 ///
25958 /// This compares the pointed-to objects.
25959 ///
25960 /// @param l the first instance to consider.
25961 ///
25962 /// @param r the second instance to consider.
25963 ///
25964 /// @return true iff @p l equals @p r.
25965 bool
25967  const class_decl::base_spec_sptr& r)
25968 {
25969  if (l.get() == r.get())
25970  return true;
25971  if (!!l != !!r)
25972  return false;
25973 
25974  return *l == static_cast<const decl_base&>(*r);
25975 }
25976 
25977 /// Inequality operator for smart pointers to @ref
25978 /// class_decl::base_specs.
25979 ///
25980 /// This compares the pointed-to objects.
25981 ///
25982 /// @param l the first instance to consider.
25983 ///
25984 /// @param r the second instance to consider.
25985 ///
25986 /// @return true iff @p l is different from @p r.
25987 bool
25989  const class_decl::base_spec_sptr& r)
25990 {return !operator==(l, r);}
25991 
25992 /// Test if an ABI artifact is a class base specifier.
25993 ///
25994 /// @param tod the ABI artifact to consider.
25995 ///
25996 /// @return a pointer to the @ref class_decl::base_spec sub-object of
25997 /// @p tod iff it's a class base specifier.
26000 {
26001  return dynamic_cast<class_decl::base_spec*>
26002  (const_cast<type_or_decl_base*>(tod));
26003 }
26004 
26005 /// Test if an ABI artifact is a class base specifier.
26006 ///
26007 /// @param tod the ABI artifact to consider.
26008 ///
26009 /// @return a pointer to the @ref class_decl::base_spec sub-object of
26010 /// @p tod iff it's a class base specifier.
26013 {return dynamic_pointer_cast<class_decl::base_spec>(tod);}
26014 
26015 bool
26016 member_function_template::operator==(const member_base& other) const
26017 {
26018  try
26019  {
26020  const member_function_template& o =
26021  dynamic_cast<const member_function_template&>(other);
26022 
26023  if (!(is_constructor() == o.is_constructor()
26024  && is_const() == o.is_const()
26025  && member_base::operator==(o)))
26026  return false;
26027 
26028  if (function_tdecl_sptr ftdecl = as_function_tdecl())
26029  {
26030  function_tdecl_sptr other_ftdecl = o.as_function_tdecl();
26031  if (other_ftdecl)
26032  return ftdecl->function_tdecl::operator==(*other_ftdecl);
26033  }
26034  }
26035  catch(...)
26036  {}
26037  return false;
26038 }
26039 
26040 /// Equality operator for smart pointers to @ref
26041 /// member_function_template. This is compares the
26042 /// pointed-to instances.
26043 ///
26044 /// @param l the first instance to consider.
26045 ///
26046 /// @param r the second instance to consider.
26047 ///
26048 /// @return true iff @p l equals @p r.
26049 bool
26050 operator==(const member_function_template_sptr& l,
26051  const member_function_template_sptr& r)
26052 {
26053  if (l.get() == r.get())
26054  return true;
26055  if (!!l != !!r)
26056  return false;
26057 
26058  return *l == *r;
26059 }
26060 
26061 /// Inequality operator for smart pointers to @ref
26062 /// member_function_template. This is compares the pointed-to
26063 /// instances.
26064 ///
26065 /// @param l the first instance to consider.
26066 ///
26067 /// @param r the second instance to consider.
26068 ///
26069 /// @return true iff @p l equals @p r.
26070 bool
26071 operator!=(const member_function_template_sptr& l,
26072  const member_function_template_sptr& r)
26073 {return !operator==(l, r);}
26074 
26075 /// This implements the ir_traversable_base::traverse pure virtual
26076 /// function.
26077 ///
26078 /// @param v the visitor used on the current instance and on its
26079 /// underlying function template.
26080 ///
26081 /// @return true if the entire IR node tree got traversed, false
26082 /// otherwise.
26083 bool
26085 {
26086  if (visiting())
26087  return true;
26088 
26089  if (v.visit_begin(this))
26090  {
26091  visiting(true);
26092  if (function_tdecl_sptr f = as_function_tdecl())
26093  f->traverse(v);
26094  visiting(false);
26095  }
26096  return v.visit_end(this);
26097 }
26098 
26099 /// Equality operator of the the @ref member_class_template class.
26100 ///
26101 /// @param other the other @ref member_class_template to compare against.
26102 ///
26103 /// @return true iff the current instance equals @p other.
26104 bool
26106 {
26107  try
26108  {
26109  const member_class_template& o =
26110  dynamic_cast<const member_class_template&>(other);
26111 
26112  if (!member_base::operator==(o))
26113  return false;
26114 
26115  return as_class_tdecl()->class_tdecl::operator==(o);
26116  }
26117  catch(...)
26118  {return false;}
26119 }
26120 
26121 /// Equality operator of the the @ref member_class_template class.
26122 ///
26123 /// @param other the other @ref member_class_template to compare against.
26124 ///
26125 /// @return true iff the current instance equals @p other.
26126 bool
26128 {
26129  if (!decl_base::operator==(other))
26130  return false;
26131  return as_class_tdecl()->class_tdecl::operator==(other);
26132 }
26133 
26134 /// Comparison operator for the @ref member_class_template
26135 /// type.
26136 ///
26137 /// @param other the other instance of @ref
26138 /// member_class_template to compare against.
26139 ///
26140 /// @return true iff the two instances are equal.
26141 bool
26143 {
26144  const decl_base* o = dynamic_cast<const decl_base*>(&other);
26145  return *this == *o;
26146 }
26147 
26148 /// Comparison operator for the @ref member_class_template
26149 /// type.
26150 ///
26151 /// @param l the first argument of the operator.
26152 ///
26153 /// @param r the second argument of the operator.
26154 ///
26155 /// @return true iff the two instances are equal.
26156 bool
26157 operator==(const member_class_template_sptr& l,
26158  const member_class_template_sptr& r)
26159 {
26160  if (l.get() == r.get())
26161  return true;
26162  if (!!l != !!r)
26163  return false;
26164 
26165  return *l == *r;
26166 }
26167 
26168 /// Inequality operator for the @ref member_class_template
26169 /// type.
26170 ///
26171 /// @param l the first argument of the operator.
26172 ///
26173 /// @param r the second argument of the operator.
26174 ///
26175 /// @return true iff the two instances are equal.
26176 bool
26177 operator!=(const member_class_template_sptr& l,
26178  const member_class_template_sptr& r)
26179 {return !operator==(l, r);}
26180 
26181 /// This implements the ir_traversable_base::traverse pure virtual
26182 /// function.
26183 ///
26184 /// @param v the visitor used on the current instance and on the class
26185 /// pattern of the template.
26186 ///
26187 /// @return true if the entire IR node tree got traversed, false
26188 /// otherwise.
26189 bool
26191 {
26192  if (visiting())
26193  return true;
26194 
26195  if (v.visit_begin(this))
26196  {
26197  visiting(true);
26198  if (class_tdecl_sptr t = as_class_tdecl())
26199  t->traverse(v);
26200  visiting(false);
26201  }
26202  return v.visit_end(this);
26203 }
26204 
26205 /// Streaming operator for class_decl::access_specifier.
26206 ///
26207 /// @param o the output stream to serialize the access specifier to.
26208 ///
26209 /// @param a the access specifier to serialize.
26210 ///
26211 /// @return the output stream.
26212 std::ostream&
26213 operator<<(std::ostream& o, access_specifier a)
26214 {
26215  string r;
26216 
26217  switch (a)
26218  {
26219  case no_access:
26220  r = "none";
26221  break;
26222  case private_access:
26223  r = "private";
26224  break;
26225  case protected_access:
26226  r = "protected";
26227  break;
26228  case public_access:
26229  r= "public";
26230  break;
26231  };
26232  o << r;
26233  return o;
26234 }
26235 
26236 /// Sets the static-ness property of a class member.
26237 ///
26238 /// @param d the class member to set the static-ness property for.
26239 /// Note that this must be a class member otherwise the function
26240 /// aborts the current process.
26241 ///
26242 /// @param s this must be true if the member is to be static, false
26243 /// otherwise.
26244 void
26246 {
26248 
26249  context_rel* c = d.get_context_rel();
26250  ABG_ASSERT(c);
26251 
26252  c->set_is_static(s);
26253 
26254  scope_decl* scope = d.get_scope();
26255 
26256  if (class_or_union* cl = is_class_or_union_type(scope))
26257  {
26258  if (var_decl* v = is_var_decl(&d))
26259  {
26260  if (s)
26261  // remove from the non-static data members
26262  for (class_decl::data_members::iterator i =
26263  cl->priv_->non_static_data_members_.begin();
26264  i != cl->priv_->non_static_data_members_.end();
26265  ++i)
26266  {
26267  if ((*i)->get_name() == v->get_name())
26268  {
26269  cl->priv_->non_static_data_members_.erase(i);
26270  break;
26271  }
26272  }
26273  else
26274  {
26275  bool is_already_in_non_static_data_members = false;
26276  for (class_or_union::data_members::iterator i =
26277  cl->priv_->non_static_data_members_.begin();
26278  i != cl->priv_->non_static_data_members_.end();
26279  ++i)
26280  {
26281  if ((*i)->get_name() == v->get_name())
26282  {
26283  is_already_in_non_static_data_members = true;
26284  break;
26285  }
26286  }
26287  if (!is_already_in_non_static_data_members)
26288  {
26289  var_decl_sptr var;
26290  // add to non-static data members.
26291  for (class_or_union::data_members::const_iterator i =
26292  cl->priv_->data_members_.begin();
26293  i != cl->priv_->data_members_.end();
26294  ++i)
26295  {
26296  if ((*i)->get_name() == v->get_name())
26297  {
26298  var = *i;
26299  break;
26300  }
26301  }
26302  ABG_ASSERT(var);
26303  cl->priv_->non_static_data_members_.push_back(var);
26304  }
26305  }
26306  }
26307  }
26308 }
26309 
26310 /// Sets the static-ness property of a class member.
26311 ///
26312 /// @param d the class member to set the static-ness property for.
26313 /// Note that this must be a class member otherwise the function
26314 /// aborts the current process.
26315 ///
26316 /// @param s this must be true if the member is to be static, false
26317 /// otherwise.
26318 void
26319 set_member_is_static(const decl_base_sptr& d, bool s)
26320 {set_member_is_static(*d, s);}
26321 
26322 // </class_decl>
26323 
26324 // <union_decl>
26325 
26326 /// Constructor for the @ref union_decl type.
26327 ///
26328 /// @param env the @ref environment we are operating from.
26329 ///
26330 /// @param name the name of the union type.
26331 ///
26332 /// @param size_in_bits the size of the union, in bits.
26333 ///
26334 /// @param locus the location of the type.
26335 ///
26336 /// @param vis the visibility of instances of @ref union_decl.
26337 ///
26338 /// @param mbr_types the member types of the union.
26339 ///
26340 /// @param data_mbrs the data members of the union.
26341 ///
26342 /// @param member_fns the member functions of the union.
26343 union_decl::union_decl(const environment& env, const string& name,
26344  size_t size_in_bits, const location& locus,
26345  visibility vis, member_types& mbr_types,
26346  data_members& data_mbrs, member_functions& member_fns)
26347  : type_or_decl_base(env,
26348  UNION_TYPE
26349  | ABSTRACT_TYPE_BASE
26350  | ABSTRACT_DECL_BASE),
26351  decl_base(env, name, locus, name, vis),
26352  type_base(env, size_in_bits, 0),
26353  class_or_union(env, name, size_in_bits, 0,
26354  locus, vis, mbr_types, data_mbrs, member_fns)
26355 {
26356  runtime_type_instance(this);
26357 }
26358 
26359 /// Constructor for the @ref union_decl type.
26360 ///
26361 /// @param env the @ref environment we are operating from.
26362 ///
26363 /// @param name the name of the union type.
26364 ///
26365 /// @param size_in_bits the size of the union, in bits.
26366 ///
26367 /// @param locus the location of the type.
26368 ///
26369 /// @param vis the visibility of instances of @ref union_decl.
26370 ///
26371 /// @param mbr_types the member types of the union.
26372 ///
26373 /// @param data_mbrs the data members of the union.
26374 ///
26375 /// @param member_fns the member functions of the union.
26376 ///
26377 /// @param is_anonymous whether the newly created instance is
26378 /// anonymous.
26379 union_decl::union_decl(const environment& env, const string& name,
26380  size_t size_in_bits, const location& locus,
26381  visibility vis, member_types& mbr_types,
26382  data_members& data_mbrs, member_functions& member_fns,
26383  bool is_anonymous)
26384  : type_or_decl_base(env,
26385  UNION_TYPE
26386  | ABSTRACT_TYPE_BASE
26387  | ABSTRACT_DECL_BASE),
26388  decl_base(env, name, locus,
26389  // If the class is anonymous then by default it won't
26390  // have a linkage name. Also, the anonymous class does
26391  // have an internal-only unique name that is generally
26392  // not taken into account when comparing classes; such a
26393  // unique internal-only name, when used as a linkage
26394  // name might introduce spurious comparison false
26395  // negatives.
26396  /*linkage_name=*/is_anonymous ? string() : name,
26397  vis),
26398  type_base(env, size_in_bits, 0),
26399  class_or_union(env, name, size_in_bits, 0,
26400  locus, vis, mbr_types, data_mbrs, member_fns)
26401 {
26402  runtime_type_instance(this);
26403  set_is_anonymous(is_anonymous);
26404 }
26405 
26406 /// Constructor for the @ref union_decl type.
26407 ///
26408 /// @param env the @ref environment we are operating from.
26409 ///
26410 /// @param name the name of the union type.
26411 ///
26412 /// @param size_in_bits the size of the union, in bits.
26413 ///
26414 /// @param locus the location of the type.
26415 ///
26416 /// @param vis the visibility of instances of @ref union_decl.
26417 union_decl::union_decl(const environment& env, const string& name,
26418  size_t size_in_bits, const location& locus,
26419  visibility vis)
26420  : type_or_decl_base(env,
26421  UNION_TYPE
26422  | ABSTRACT_TYPE_BASE
26423  | ABSTRACT_DECL_BASE
26424  | ABSTRACT_SCOPE_TYPE_DECL
26425  | ABSTRACT_SCOPE_DECL),
26426  decl_base(env, name, locus, name, vis),
26427  type_base(env, size_in_bits, 0),
26428  class_or_union(env, name, size_in_bits,
26429  0, locus, vis)
26430 {
26431  runtime_type_instance(this);
26432 }
26433 
26434 /// Constructor for the @ref union_decl type.
26435 ///
26436 /// @param env the @ref environment we are operating from.
26437 ///
26438 /// @param name the name of the union type.
26439 ///
26440 /// @param size_in_bits the size of the union, in bits.
26441 ///
26442 /// @param locus the location of the type.
26443 ///
26444 /// @param vis the visibility of instances of @ref union_decl.
26445 ///
26446 /// @param is_anonymous whether the newly created instance is
26447 /// anonymous.
26448 union_decl::union_decl(const environment& env, const string& name,
26449  size_t size_in_bits, const location& locus,
26450  visibility vis, bool is_anonymous)
26451  : type_or_decl_base(env,
26452  UNION_TYPE
26453  | ABSTRACT_TYPE_BASE
26454  | ABSTRACT_DECL_BASE
26455  | ABSTRACT_SCOPE_TYPE_DECL
26456  | ABSTRACT_SCOPE_DECL),
26457  decl_base(env, name, locus,
26458  // If the class is anonymous then by default it won't
26459  // have a linkage name. Also, the anonymous class does
26460  // have an internal-only unique name that is generally
26461  // not taken into account when comparing classes; such a
26462  // unique internal-only name, when used as a linkage
26463  // name might introduce spurious comparison false
26464  // negatives.
26465  /*linkage_name=*/is_anonymous ? string() : name,
26466  vis),
26467  type_base(env, size_in_bits, 0),
26468  class_or_union(env, name, size_in_bits,
26469  0, locus, vis)
26470 {
26471  runtime_type_instance(this);
26472  set_is_anonymous(is_anonymous);
26473 }
26474 
26475 /// Constructor for the @ref union_decl type.
26476 ///
26477 /// @param env the @ref environment we are operating from.
26478 ///
26479 /// @param name the name of the union type.
26480 ///
26481 /// @param is_declaration_only a boolean saying whether the instance
26482 /// represents a declaration only, or not.
26483 union_decl::union_decl(const environment& env,
26484  const string& name,
26485  bool is_declaration_only)
26486  : type_or_decl_base(env,
26487  UNION_TYPE
26488  | ABSTRACT_TYPE_BASE
26489  | ABSTRACT_DECL_BASE
26490  | ABSTRACT_SCOPE_TYPE_DECL
26491  | ABSTRACT_SCOPE_DECL),
26492  decl_base(env, name, location(), name),
26493  type_base(env, 0, 0),
26494  class_or_union(env, name, is_declaration_only)
26495 {
26496  runtime_type_instance(this);
26497 }
26498 
26499 /// Getter of the pretty representation of the current instance of
26500 /// @ref union_decl.
26501 ///
26502 /// @param internal set to true if the call is intended to get a
26503 /// representation of the decl (or type) for the purpose of canonical
26504 /// type comparison. This is mainly used in the function
26505 /// type_base::get_canonical_type_for().
26506 ///
26507 /// In other words if the argument for this parameter is true then the
26508 /// call is meant for internal use (for technical use inside the
26509 /// library itself), false otherwise. If you don't know what this is
26510 /// for, then set it to false.
26511 ///
26512 /// @param qualified_name if true, names emitted in the pretty
26513 /// representation are fully qualified.
26514 ///
26515 /// @return the pretty representaion for a union_decl.
26516 string
26518  bool qualified_name) const
26519 {
26520  string repr;
26521  if (get_is_anonymous())
26522  {
26523  if (internal && !get_name().empty())
26524  repr = string("union ") +
26525  get_type_name(this, qualified_name, /*internal=*/true);
26526  else
26528  /*one_line=*/true,
26529  internal);
26530  }
26531  else
26532  {
26533  repr = "union ";
26534  if (qualified_name)
26535  repr += get_qualified_name(internal);
26536  else
26537  repr += get_name();
26538  }
26539 
26540  return repr;
26541 }
26542 
26543 /// Comparison operator for @ref union_decl.
26544 ///
26545 /// @param other the instance of @ref union_decl to compare against.
26546 ///
26547 /// @return true iff the current instance of @ref union_decl equals @p
26548 /// other.
26549 bool
26551 {
26552  const union_decl* op = dynamic_cast<const union_decl*>(&other);
26553  if (!op)
26554  return false;
26555  return try_canonical_compare(this, op);
26556 }
26557 
26558 /// Equality operator for union_decl.
26559 ///
26560 /// Re-uses the equality operator that takes a decl_base.
26561 ///
26562 /// @param other the other union_decl to compare against.
26563 ///
26564 /// @return true iff the current instance equals the other one.
26565 bool
26567 {
26568  const decl_base *o = dynamic_cast<const decl_base*>(&other);
26569  if (!o)
26570  return false;
26571  return *this == *o;
26572 }
26573 
26574 /// Equality operator for union_decl.
26575 ///
26576 /// Re-uses the equality operator that takes a decl_base.
26577 ///
26578 /// @param other the other union_decl to compare against.
26579 ///
26580 /// @return true iff the current instance equals the other one.
26581 bool
26583 {
26584  const decl_base *o = dynamic_cast<const decl_base*>(&other);
26585  return *this == *o;
26586 }
26587 
26588 /// Comparison operator for @ref union_decl.
26589 ///
26590 /// @param other the instance of @ref union_decl to compare against.
26591 ///
26592 /// @return true iff the current instance of @ref union_decl equals @p
26593 /// other.
26594 bool
26596 {
26597  const decl_base& o = other;
26598  return *this == o;
26599 }
26600 
26601 /// This implements the ir_traversable_base::traverse pure virtual
26602 /// function.
26603 ///
26604 /// @param v the visitor used on the current instance and on its
26605 /// members.
26606 ///
26607 /// @return true if the entire IR node tree got traversed, false
26608 /// otherwise.
26609 bool
26611 {
26612  if (v.type_node_has_been_visited(this))
26613  return true;
26614 
26615  if (visiting())
26616  return true;
26617 
26618  if (v.visit_begin(this))
26619  {
26620  visiting(true);
26621  bool stop = false;
26622 
26623  if (!stop)
26624  for (data_members::const_iterator i = get_data_members().begin();
26625  i != get_data_members().end();
26626  ++i)
26627  if (!(*i)->traverse(v))
26628  {
26629  stop = true;
26630  break;
26631  }
26632 
26633  if (!stop)
26634  for (member_functions::const_iterator i= get_member_functions().begin();
26635  i != get_member_functions().end();
26636  ++i)
26637  if (!(*i)->traverse(v))
26638  {
26639  stop = true;
26640  break;
26641  }
26642 
26643  if (!stop)
26644  for (member_types::const_iterator i = get_member_types().begin();
26645  i != get_member_types().end();
26646  ++i)
26647  if (!(*i)->traverse(v))
26648  {
26649  stop = true;
26650  break;
26651  }
26652 
26653  if (!stop)
26654  for (member_function_templates::const_iterator i =
26656  i != get_member_function_templates().end();
26657  ++i)
26658  if (!(*i)->traverse(v))
26659  {
26660  stop = true;
26661  break;
26662  }
26663 
26664  if (!stop)
26665  for (member_class_templates::const_iterator i =
26666  get_member_class_templates().begin();
26667  i != get_member_class_templates().end();
26668  ++i)
26669  if (!(*i)->traverse(v))
26670  {
26671  stop = true;
26672  break;
26673  }
26674  visiting(false);
26675  }
26676 
26677  bool result = v.visit_end(this);
26678  v.mark_type_node_as_visited(this);
26679  return result;
26680 }
26681 
26682 /// Destructor of the @ref union_decl type.
26684 {}
26685 
26686 /// Compares two instances of @ref union_decl.
26687 ///
26688 /// If the two intances are different, set a bitfield to give some
26689 /// insight about the kind of differences there are.
26690 ///
26691 /// @param l the first artifact of the comparison.
26692 ///
26693 /// @param r the second artifact of the comparison.
26694 ///
26695 /// @param k a pointer to a bitfield that gives information about the
26696 /// kind of changes there are between @p l and @p r. This one is set
26697 /// iff @p k is non-null and the function returns false.
26698 ///
26699 /// Please note that setting k to a non-null value does have a
26700 /// negative performance impact because even if @p l and @p r are not
26701 /// equal, the function keeps up the comparison in order to determine
26702 /// the different kinds of ways in which they are different.
26703 ///
26704 /// @return true if @p l equals @p r, false otherwise.
26705 bool
26706 equals(const union_decl& l, const union_decl& r, change_kind* k)
26707 {
26708 
26710 
26711  {
26712  // First of all, let's see if these two types haven't already been
26713  // compared. If so, and if the result of the comparison has been
26714  // cached, let's just re-use it, rather than comparing them all
26715  // over again.
26716  bool result = false;
26717  if (l.get_environment().priv_->is_type_comparison_cached(l, r, result))
26718  ABG_RETURN(result);
26719  }
26720 
26721  bool result = equals(static_cast<const class_or_union&>(l),
26722  static_cast<const class_or_union&>(r),
26723  k);
26724 
26726 }
26727 
26728 /// Copy a method of a @ref union_decl into a new @ref
26729 /// union_decl.
26730 ///
26731 /// @param t the @ref union_decl into which the method is to be copied.
26732 ///
26733 /// @param method the method to copy into @p t.
26734 ///
26735 /// @return the resulting newly copied method.
26736 method_decl_sptr
26737 copy_member_function(const union_decl_sptr& union_type,
26738  const method_decl_sptr& f)
26739 {return copy_member_function(union_type, f.get());}
26740 
26741 /// Copy a method of a @ref union_decl into a new @ref
26742 /// union_decl.
26743 ///
26744 /// @param t the @ref union_decl into which the method is to be copied.
26745 ///
26746 /// @param method the method to copy into @p t.
26747 ///
26748 /// @return the resulting newly copied method.
26749 method_decl_sptr
26750 copy_member_function(const union_decl_sptr& union_type,
26751  const method_decl* f)
26752 {
26753  const class_or_union_sptr t = union_type;
26754  return copy_member_function(t, f);
26755 }
26756 
26757 /// Turn equality of shared_ptr of union_decl into a deep equality;
26758 /// that is, make it compare the pointed to objects too.
26759 ///
26760 /// @param l the left-hand-side operand of the operator
26761 ///
26762 /// @param r the right-hand-side operand of the operator.
26763 ///
26764 /// @return true iff @p l equals @p r.
26765 bool
26766 operator==(const union_decl_sptr& l, const union_decl_sptr& r)
26767 {
26768  if (l.get() == r.get())
26769  return true;
26770  if (!!l != !!r)
26771  return false;
26772 
26773  return *l == *r;
26774 }
26775 
26776 /// Turn inequality of shared_ptr of union_decl into a deep equality;
26777 /// that is, make it compare the pointed to objects too.
26778 ///
26779 /// @param l the left-hand-side operand of the operator
26780 ///
26781 /// @param r the right-hand-side operand of the operator.
26782 ///
26783 /// @return true iff @p l is different from @p r.
26784 bool
26785 operator!=(const union_decl_sptr& l, const union_decl_sptr& r)
26786 {return !operator==(l, r);}
26787 // </union_decl>
26788 
26789 // <template_decl stuff>
26790 
26791 /// Data type of the private data of the @template_decl type.
26792 class template_decl::priv
26793 {
26794  friend class template_decl;
26795 
26796  std::list<template_parameter_sptr> parms_;
26797 public:
26798 
26799  priv()
26800  {}
26801 }; // end class template_decl::priv
26802 
26803 /// Add a new template parameter to the current instance of @ref
26804 /// template_decl.
26805 ///
26806 /// @param p the new template parameter to add.
26807 void
26809 {priv_->parms_.push_back(p);}
26810 
26811 /// Get the list of template parameters of the current instance of
26812 /// @ref template_decl.
26813 ///
26814 /// @return the list of template parameters.
26815 const std::list<template_parameter_sptr>&
26817 {return priv_->parms_;}
26818 
26819 /// Constructor.
26820 ///
26821 /// @param env the environment we are operating from.
26822 ///
26823 /// @param name the name of the template decl.
26824 ///
26825 /// @param locus the source location where the template declaration is
26826 /// defined.
26827 ///
26828 /// @param vis the visibility of the template declaration.
26829 template_decl::template_decl(const environment& env,
26830  const string& name,
26831  const location& locus,
26832  visibility vis)
26833  : type_or_decl_base(env, TEMPLATE_DECL | ABSTRACT_DECL_BASE),
26834  decl_base(env, name, locus, /*mangled_name=*/"", vis),
26835  priv_(new priv)
26836 {
26837  runtime_type_instance(this);
26838 }
26839 
26840 /// Destructor.
26842 {}
26843 
26844 /// Equality operator.
26845 ///
26846 /// @param o the other instance to compare against.
26847 ///
26848 /// @return true iff @p equals the current instance.
26849 bool
26851 {
26852  const template_decl* other = dynamic_cast<const template_decl*>(&o);
26853  if (!other)
26854  return false;
26855  return *this == *other;
26856 }
26857 
26858 /// Equality operator.
26859 ///
26860 /// @param o the other instance to compare against.
26861 ///
26862 /// @return true iff @p equals the current instance.
26863 bool
26865 {
26866  try
26867  {
26868  list<shared_ptr<template_parameter> >::const_iterator t0, t1;
26869  for (t0 = get_template_parameters().begin(),
26870  t1 = o.get_template_parameters().begin();
26871  (t0 != get_template_parameters().end()
26872  && t1 != o.get_template_parameters().end());
26873  ++t0, ++t1)
26874  {
26875  if (**t0 != **t1)
26876  return false;
26877  }
26878 
26879  if (t0 != get_template_parameters().end()
26880  || t1 != o.get_template_parameters().end())
26881  return false;
26882 
26883  return true;
26884  }
26885  catch(...)
26886  {return false;}
26887 }
26888 
26889 // </template_decl stuff>
26890 
26891 //<template_parameter>
26892 
26893 /// The type of the private data of the @ref template_parameter type.
26894 class template_parameter::priv
26895 {
26896  friend class template_parameter;
26897 
26898  unsigned index_;
26899  template_decl_wptr template_decl_;
26900  mutable bool hashing_started_;
26901  mutable bool comparison_started_;
26902 
26903  priv();
26904 
26905 public:
26906 
26907  priv(unsigned index, template_decl_sptr enclosing_template_decl)
26908  : index_(index),
26909  template_decl_(enclosing_template_decl),
26910  hashing_started_(),
26911  comparison_started_()
26912  {}
26913 }; // end class template_parameter::priv
26914 
26915 template_parameter::template_parameter(unsigned index,
26916  template_decl_sptr enclosing_template)
26917  : priv_(new priv(index, enclosing_template))
26918  {}
26919 
26920 unsigned
26921 template_parameter::get_index() const
26922 {return priv_->index_;}
26923 
26924 const template_decl_sptr
26925 template_parameter::get_enclosing_template_decl() const
26926 {return priv_->template_decl_.lock();}
26927 
26928 bool
26929 template_parameter::get_hashing_has_started() const
26930 {return priv_->hashing_started_;}
26931 
26932 void
26933 template_parameter::set_hashing_has_started(bool f) const
26934 {priv_->hashing_started_ = f;}
26935 
26936 bool
26937 template_parameter::operator==(const template_parameter& o) const
26938 {
26939  if (get_index() != o.get_index())
26940  return false;
26941 
26942  if (priv_->comparison_started_)
26943  return true;
26944 
26945  bool result = false;
26946 
26947  // Avoid inifite loops due to the fact that comparison the enclosing
26948  // template decl might lead to comparing this very same template
26949  // parameter with another one ...
26950  priv_->comparison_started_ = true;
26951 
26952  if (!!get_enclosing_template_decl() != !!o.get_enclosing_template_decl())
26953  ;
26954  else if (get_enclosing_template_decl()
26955  && (*get_enclosing_template_decl()
26956  != *o.get_enclosing_template_decl()))
26957  ;
26958  else
26959  result = true;
26960 
26961  priv_->comparison_started_ = false;
26962 
26963  return result;
26964 }
26965 
26966 /// Inequality operator.
26967 ///
26968 /// @param other the other instance to compare against.
26969 ///
26970 /// @return true iff the other instance is different from the current
26971 /// one.
26972 bool
26974 {return !operator==(other);}
26975 
26976 /// Destructor.
26978 {}
26979 
26980 /// The type of the private data of the @ref type_tparameter type.
26981 class type_tparameter::priv
26982 {
26983  friend class type_tparameter;
26984 }; // end class type_tparameter::priv
26985 
26986 /// Constructor of the @ref type_tparameter type.
26987 ///
26988 /// @param index the index the type template parameter.
26989 ///
26990 /// @param enclosing_tdecl the enclosing template declaration.
26991 ///
26992 /// @param name the name of the template parameter.
26993 ///
26994 /// @param locus the location of the declaration of this type template
26995 /// parameter.
26996 type_tparameter::type_tparameter(unsigned index,
26997  template_decl_sptr enclosing_tdecl,
26998  const string& name,
26999  const location& locus)
27000  : type_or_decl_base(enclosing_tdecl->get_environment(),
27001  ABSTRACT_DECL_BASE
27002  | ABSTRACT_TYPE_BASE
27003  | BASIC_TYPE),
27004  decl_base(enclosing_tdecl->get_environment(), name, locus),
27005  type_base(enclosing_tdecl->get_environment(), 0, 0),
27006  type_decl(enclosing_tdecl->get_environment(), name, 0, 0, locus),
27007  template_parameter(index, enclosing_tdecl),
27008  priv_(new priv)
27009 {
27010  runtime_type_instance(this);
27011 }
27012 
27013 /// Equality operator.
27014 ///
27015 /// @param other the other template type parameter to compare against.
27016 ///
27017 /// @return true iff @p other equals the current instance.
27018 bool
27020 {
27021  if (!type_decl::operator==(other))
27022  return false;
27023 
27024  try
27025  {
27026  const type_tparameter& o = dynamic_cast<const type_tparameter&>(other);
27028  }
27029  catch (...)
27030  {return false;}
27031 }
27032 
27033 /// Equality operator.
27034 ///
27035 /// @param other the other template type parameter to compare against.
27036 ///
27037 /// @return true iff @p other equals the current instance.
27038 bool
27040 {
27041  if (!type_decl::operator==(other))
27042  return false;
27043 
27044  try
27045  {
27046  const type_tparameter& o = dynamic_cast<const type_tparameter&>(other);
27048  }
27049  catch (...)
27050  {return false;}
27051 }
27052 
27053 /// Equality operator.
27054 ///
27055 /// @param other the other template type parameter to compare against.
27056 ///
27057 /// @return true iff @p other equals the current instance.
27058 bool
27060 {
27061  if (!decl_base::operator==(other))
27062  return false;
27063 
27064  try
27065  {
27066  const type_tparameter& o = dynamic_cast<const type_tparameter&>(other);
27068  }
27069  catch (...)
27070  {return false;}
27071 }
27072 
27073 /// Equality operator.
27074 ///
27075 /// @param other the other template type parameter to compare against.
27076 ///
27077 /// @return true iff @p other equals the current instance.
27078 bool
27080 {
27081  try
27082  {
27083  const type_base& o = dynamic_cast<const type_base&>(other);
27084  return *this == o;
27085  }
27086  catch(...)
27087  {return false;}
27088 }
27089 
27090 /// Equality operator.
27091 ///
27092 /// @param other the other template type parameter to compare against.
27093 ///
27094 /// @return true iff @p other equals the current instance.
27095 bool
27097 {return *this == static_cast<const type_base&>(other);}
27098 
27099 type_tparameter::~type_tparameter()
27100 {}
27101 
27102 /// The type of the private data of the @ref non_type_tparameter type.
27103 class non_type_tparameter::priv
27104 {
27105  friend class non_type_tparameter;
27106 
27107  type_base_wptr type_;
27108 
27109  priv();
27110 
27111 public:
27112 
27113  priv(type_base_sptr type)
27114  : type_(type)
27115  {}
27116 }; // end class non_type_tparameter::priv
27117 
27118 /// The constructor for the @ref non_type_tparameter type.
27119 ///
27120 /// @param index the index of the template parameter.
27121 ///
27122 /// @param enclosing_tdecl the enclosing template declaration that
27123 /// holds this parameter parameter.
27124 ///
27125 /// @param name the name of the template parameter.
27126 ///
27127 /// @param type the type of the template parameter.
27128 ///
27129 /// @param locus the location of the declaration of this template
27130 /// parameter.
27131 non_type_tparameter::non_type_tparameter(unsigned index,
27132  template_decl_sptr enclosing_tdecl,
27133  const string& name,
27134  type_base_sptr type,
27135  const location& locus)
27136  : type_or_decl_base(type->get_environment(), ABSTRACT_DECL_BASE),
27137  decl_base(type->get_environment(), name, locus, ""),
27138  template_parameter(index, enclosing_tdecl),
27139  priv_(new priv(type))
27140 {
27141  runtime_type_instance(this);
27142 }
27143 
27144 /// Getter for the type of the template parameter.
27145 ///
27146 /// @return the type of the template parameter.
27147 const type_base_sptr
27149 {return priv_->type_.lock();}
27150 
27151 /// Get the hash value of the current instance.
27152 ///
27153 /// @return the hash value.
27154 size_t
27156 {
27157  non_type_tparameter::hash hash_tparm;
27158  return hash_tparm(this);
27159 }
27160 
27161 bool
27163 {
27164  if (!decl_base::operator==(other))
27165  return false;
27166 
27167  try
27168  {
27169  const non_type_tparameter& o =
27170  dynamic_cast<const non_type_tparameter&>(other);
27171  return (template_parameter::operator==(o)
27172  && get_type() == o.get_type());
27173  }
27174  catch(...)
27175  {return false;}
27176 }
27177 
27178 bool
27180 {
27181  try
27182  {
27183  const decl_base& o = dynamic_cast<const decl_base&>(other);
27184  return *this == o;
27185  }
27186  catch(...)
27187  {return false;}
27188 }
27189 
27190 non_type_tparameter::~non_type_tparameter()
27191 {}
27192 
27193 // <template_tparameter stuff>
27194 
27195 /// Type of the private data of the @ref template_tparameter type.
27196 class template_tparameter::priv
27197 {
27198 }; //end class template_tparameter::priv
27199 
27200 /// Constructor for the @ref template_tparameter.
27201 ///
27202 /// @param index the index of the template parameter.
27203 ///
27204 /// @param enclosing_tdecl the enclosing template declaration.
27205 ///
27206 /// @param name the name of the template parameter.
27207 ///
27208 /// @param locus the location of the declaration of the template
27209 /// parameter.
27210 template_tparameter::template_tparameter(unsigned index,
27211  template_decl_sptr enclosing_tdecl,
27212  const string& name,
27213  const location& locus)
27214  : type_or_decl_base(enclosing_tdecl->get_environment(),
27215  ABSTRACT_DECL_BASE
27216  | ABSTRACT_TYPE_BASE
27217  | BASIC_TYPE),
27218  decl_base(enclosing_tdecl->get_environment(), name, locus),
27219  type_base(enclosing_tdecl->get_environment(), 0, 0),
27220  type_decl(enclosing_tdecl->get_environment(), name,
27221  0, 0, locus, name, VISIBILITY_DEFAULT),
27222  type_tparameter(index, enclosing_tdecl, name, locus),
27223  template_decl(enclosing_tdecl->get_environment(), name, locus),
27224  priv_(new priv)
27225 {
27226  runtime_type_instance(this);
27227 }
27228 
27229 /// Equality operator.
27230 ///
27231 /// @param other the other template parameter to compare against.
27232 ///
27233 /// @return true iff @p other equals the current instance.
27234 bool
27236 {
27237  try
27238  {
27239  const template_tparameter& o =
27240  dynamic_cast<const template_tparameter&>(other);
27241  return (type_tparameter::operator==(o)
27243  }
27244  catch(...)
27245  {return false;}
27246 }
27247 
27248 /// Equality operator.
27249 ///
27250 /// @param other the other template parameter to compare against.
27251 ///
27252 /// @return true iff @p other equals the current instance.
27253 bool
27255 {
27256  try
27257  {
27258  const template_tparameter& o =
27259  dynamic_cast<const template_tparameter&>(other);
27260  return (type_tparameter::operator==(o)
27262  }
27263  catch(...)
27264  {return false;}
27265 }
27266 
27267 bool
27269 {
27270  try
27271  {
27272  const template_tparameter& other =
27273  dynamic_cast<const template_tparameter&>(o);
27274  return *this == static_cast<const type_base&>(other);
27275  }
27276  catch(...)
27277  {return false;}
27278 }
27279 
27280 bool
27282 {
27283  try
27284  {
27285  const template_tparameter& other =
27286  dynamic_cast<const template_tparameter&>(o);
27287  return type_base::operator==(other);
27288  }
27289  catch(...)
27290  {return false;}
27291 }
27292 
27293 template_tparameter::~template_tparameter()
27294 {}
27295 
27296 // </template_tparameter stuff>
27297 
27298 // <type_composition stuff>
27299 
27300 /// The type of the private data of the @ref type_composition type.
27301 class type_composition::priv
27302 {
27303  friend class type_composition;
27304 
27305  type_base_wptr type_;
27306 
27307  // Forbid this.
27308  priv();
27309 
27310 public:
27311 
27312  priv(type_base_wptr type)
27313  : type_(type)
27314  {}
27315 }; //end class type_composition::priv
27316 
27317 /// Constructor for the @ref type_composition type.
27318 ///
27319 /// @param index the index of the template type composition.
27320 ///
27321 /// @param tdecl the enclosing template parameter that owns the
27322 /// composition.
27323 ///
27324 /// @param t the resulting type.
27325 type_composition::type_composition(unsigned index,
27326  template_decl_sptr tdecl,
27327  type_base_sptr t)
27328  : type_or_decl_base(tdecl->get_environment(),
27329  ABSTRACT_DECL_BASE),
27330  decl_base(tdecl->get_environment(), "", location()),
27331  template_parameter(index, tdecl),
27332  priv_(new priv(t))
27333 {
27334  runtime_type_instance(this);
27335 }
27336 
27337 /// Getter for the resulting composed type.
27338 ///
27339 /// @return the composed type.
27340 const type_base_sptr
27342 {return priv_->type_.lock();}
27343 
27344 /// Setter for the resulting composed type.
27345 ///
27346 /// @param t the composed type.
27347 void
27349 {priv_->type_ = t;}
27350 
27351 /// Get the hash value for the current instance.
27352 ///
27353 /// @return the hash value.
27354 size_t
27356 {
27357  type_composition::hash hash_type_composition;
27358  return hash_type_composition(this);
27359 }
27360 
27361 type_composition::~type_composition()
27362 {}
27363 
27364 // </type_composition stuff>
27365 
27366 //</template_parameter stuff>
27367 
27368 // <function_template>
27369 
27370 class function_tdecl::priv
27371 {
27372  friend class function_tdecl;
27373 
27374  function_decl_sptr pattern_;
27375  binding binding_;
27376 
27377  priv();
27378 
27379 public:
27380 
27381  priv(function_decl_sptr pattern, binding bind)
27382  : pattern_(pattern), binding_(bind)
27383  {}
27384 
27385  priv(binding bind)
27386  : binding_(bind)
27387  {}
27388 }; // end class function_tdecl::priv
27389 
27390 /// Constructor for a function template declaration.
27391 ///
27392 /// @param env the environment we are operating from.
27393 ///
27394 /// @param locus the location of the declaration.
27395 ///
27396 /// @param vis the visibility of the declaration. This is the
27397 /// visibility the functions instantiated from this template are going
27398 /// to have.
27399 ///
27400 /// @param bind the binding of the declaration. This is the binding
27401 /// the functions instantiated from this template are going to have.
27402 function_tdecl::function_tdecl(const environment& env,
27403  const location& locus,
27404  visibility vis,
27405  binding bind)
27406  : type_or_decl_base(env,
27407  ABSTRACT_DECL_BASE
27408  | TEMPLATE_DECL
27409  | ABSTRACT_SCOPE_DECL),
27410  decl_base(env, "", locus, "", vis),
27411  template_decl(env, "", locus, vis),
27412  scope_decl(env, "", locus),
27413  priv_(new priv(bind))
27414 {
27415  runtime_type_instance(this);
27416 }
27417 
27418 /// Constructor for a function template declaration.
27419 ///
27420 /// @param pattern the pattern of the template.
27421 ///
27422 /// @param locus the location of the declaration.
27423 ///
27424 /// @param vis the visibility of the declaration. This is the
27425 /// visibility the functions instantiated from this template are going
27426 /// to have.
27427 ///
27428 /// @param bind the binding of the declaration. This is the binding
27429 /// the functions instantiated from this template are going to have.
27430 function_tdecl::function_tdecl(function_decl_sptr pattern,
27431  const location& locus,
27432  visibility vis,
27433  binding bind)
27434  : type_or_decl_base(pattern->get_environment(),
27435  ABSTRACT_DECL_BASE
27436  | TEMPLATE_DECL
27437  | ABSTRACT_SCOPE_DECL),
27438  decl_base(pattern->get_environment(), pattern->get_name(), locus,
27439  pattern->get_name(), vis),
27440  template_decl(pattern->get_environment(), pattern->get_name(), locus, vis),
27441  scope_decl(pattern->get_environment(), pattern->get_name(), locus),
27442  priv_(new priv(pattern, bind))
27443 {
27444  runtime_type_instance(this);
27445 }
27446 
27447 /// Set a new pattern to the function template.
27448 ///
27449 /// @param p the new pattern.
27450 void
27452 {
27453  priv_->pattern_ = p;
27454  add_decl_to_scope(p, this);
27455  set_name(p->get_name());
27456 }
27457 
27458 /// Get the pattern of the function template.
27459 ///
27460 /// @return the pattern.
27463 {return priv_->pattern_;}
27464 
27465 /// Get the binding of the function template.
27466 ///
27467 /// @return the binding
27470 {return priv_->binding_;}
27471 
27472 /// Comparison operator for the @ref function_tdecl type.
27473 ///
27474 /// @param other the other instance of @ref function_tdecl to compare against.
27475 ///
27476 /// @return true iff the two instance are equal.
27477 bool
27479 {
27480  const function_tdecl* o = dynamic_cast<const function_tdecl*>(&other);
27481  if (o)
27482  return *this == *o;
27483  return false;
27484 }
27485 
27486 /// Comparison operator for the @ref function_tdecl type.
27487 ///
27488 /// @param other the other instance of @ref function_tdecl to compare against.
27489 ///
27490 /// @return true iff the two instance are equal.
27491 bool
27493 {
27494  const function_tdecl* o = dynamic_cast<const function_tdecl*>(&other);
27495  if (o)
27496  return *this == *o;
27497  return false;
27498 }
27499 
27500 /// Comparison operator for the @ref function_tdecl type.
27501 ///
27502 /// @param o the other instance of @ref function_tdecl to compare against.
27503 ///
27504 /// @return true iff the two instance are equal.
27505 bool
27507 {
27508  if (!(get_binding() == o.get_binding()
27509  && template_decl::operator==(o)
27510  && scope_decl::operator==(o)
27511  && !!get_pattern() == !!o.get_pattern()))
27512  return false;
27513 
27514  if (get_pattern())
27515  return (*get_pattern() == *o.get_pattern());
27516 
27517  return true;
27518 }
27519 
27520 /// This implements the ir_traversable_base::traverse pure virtual
27521 /// function.
27522 ///
27523 /// @param v the visitor used on the current instance and on the
27524 /// function pattern of the template.
27525 ///
27526 /// @return true if the entire IR node tree got traversed, false
27527 /// otherwise.
27528 bool
27530 {
27531  if (visiting())
27532  return true;
27533 
27534  if (!v.visit_begin(this))
27535  {
27536  visiting(true);
27537  if (get_pattern())
27538  get_pattern()->traverse(v);
27539  visiting(false);
27540  }
27541  return v.visit_end(this);
27542 }
27543 
27544 function_tdecl::~function_tdecl()
27545 {}
27546 
27547 // </function_template>
27548 
27549 // <class template>
27550 
27551 /// Type of the private data of the the @ref class_tdecl type.
27552 class class_tdecl::priv
27553 {
27554  friend class class_tdecl;
27555  class_decl_sptr pattern_;
27556 
27557 public:
27558 
27559  priv()
27560  {}
27561 
27562  priv(class_decl_sptr pattern)
27563  : pattern_(pattern)
27564  {}
27565 }; // end class class_tdecl::priv
27566 
27567 /// Constructor for the @ref class_tdecl type.
27568 ///
27569 /// @param env the environment we are operating from.
27570 ///
27571 /// @param locus the location of the declaration of the class_tdecl
27572 /// type.
27573 ///
27574 /// @param vis the visibility of the instance of class instantiated
27575 /// from this template.
27576 class_tdecl::class_tdecl(const environment& env,
27577  const location& locus,
27578  visibility vis)
27579  : type_or_decl_base(env,
27580  ABSTRACT_DECL_BASE
27581  | TEMPLATE_DECL
27582  | ABSTRACT_SCOPE_DECL),
27583  decl_base(env, "", locus, "", vis),
27584  template_decl(env, "", locus, vis),
27585  scope_decl(env, "", locus),
27586  priv_(new priv)
27587 {
27588  runtime_type_instance(this);
27589 }
27590 
27591 /// Constructor for the @ref class_tdecl type.
27592 ///
27593 /// @param pattern The details of the class template. This must NOT be a
27594 /// null pointer. If you really this to be null, please use the
27595 /// constructor above instead.
27596 ///
27597 /// @param locus the source location of the declaration of the type.
27598 ///
27599 /// @param vis the visibility of the instances of class instantiated
27600 /// from this template.
27601 class_tdecl::class_tdecl(class_decl_sptr pattern,
27602  const location& locus,
27603  visibility vis)
27604  : type_or_decl_base(pattern->get_environment(),
27605  ABSTRACT_DECL_BASE
27606  | TEMPLATE_DECL
27607  | ABSTRACT_SCOPE_DECL),
27608  decl_base(pattern->get_environment(), pattern->get_name(),
27609  locus, pattern->get_name(), vis),
27610  template_decl(pattern->get_environment(), pattern->get_name(), locus, vis),
27611  scope_decl(pattern->get_environment(), pattern->get_name(), locus),
27612  priv_(new priv(pattern))
27613 {
27614  runtime_type_instance(this);
27615 }
27616 
27617 /// Setter of the pattern of the template.
27618 ///
27619 /// @param p the new template.
27620 void
27622 {
27623  priv_->pattern_ = p;
27624  add_decl_to_scope(p, this);
27625  set_name(p->get_name());
27626 }
27627 
27628 /// Getter of the pattern of the template.
27629 ///
27630 /// @return p the new template.
27633 {return priv_->pattern_;}
27634 
27635 bool
27637 {
27638  try
27639  {
27640  const class_tdecl& o = dynamic_cast<const class_tdecl&>(other);
27641 
27642  if (!(template_decl::operator==(o)
27643  && scope_decl::operator==(o)
27644  && !!get_pattern() == !!o.get_pattern()))
27645  return false;
27646 
27647  if (!get_pattern() || !o.get_pattern())
27648  return true;
27649 
27650  return get_pattern()->decl_base::operator==(*o.get_pattern());
27651  }
27652  catch(...) {}
27653  return false;
27654 }
27655 
27656 bool
27658 {
27659  try
27660  {
27661  const class_tdecl& o = dynamic_cast<const class_tdecl&>(other);
27662  return *this == static_cast<const decl_base&>(o);
27663  }
27664  catch(...)
27665  {return false;}
27666 }
27667 
27668 bool
27669 class_tdecl::operator==(const class_tdecl& o) const
27670 {return *this == static_cast<const decl_base&>(o);}
27671 
27672 /// This implements the ir_traversable_base::traverse pure virtual
27673 /// function.
27674 ///
27675 /// @param v the visitor used on the current instance and on the class
27676 /// pattern of the template.
27677 ///
27678 /// @return true if the entire IR node tree got traversed, false
27679 /// otherwise.
27680 bool
27682 {
27683  if (visiting())
27684  return true;
27685 
27686  if (v.visit_begin(this))
27687  {
27688  visiting(true);
27689  if (class_decl_sptr pattern = get_pattern())
27690  pattern->traverse(v);
27691  visiting(false);
27692  }
27693  return v.visit_end(this);
27694 }
27695 
27696 class_tdecl::~class_tdecl()
27697 {}
27698 
27699 /// This visitor checks if a given type as non-canonicalized sub
27700 /// types.
27701 class non_canonicalized_subtype_detector : public ir::ir_node_visitor
27702 {
27703  type_base* type_;
27704  type_base* has_non_canonical_type_;
27705 
27706 private:
27707  non_canonicalized_subtype_detector();
27708 
27709 public:
27710  non_canonicalized_subtype_detector(type_base* type)
27711  : type_(type),
27712  has_non_canonical_type_()
27713  {}
27714 
27715  /// Return true if the visitor detected that there is a
27716  /// non-canonicalized sub-type.
27717  ///
27718  /// @return true if the visitor detected that there is a
27719  /// non-canonicalized sub-type.
27720  type_base*
27721  has_non_canonical_type() const
27722  {return has_non_canonical_type_;}
27723 
27724  /// The intent of this visitor handler is to avoid looking into
27725  /// sub-types of member functions of the type we are traversing.
27726  bool
27727  visit_begin(function_decl* f)
27728  {
27729  // Do not look at sub-types of non-virtual member functions.
27730  if (is_member_function(f)
27732  return false;
27733  return true;
27734  }
27735 
27736  /// When visiting a sub-type, if it's *NOT* been canonicalized, set
27737  /// the 'has_non_canonical_type' flag. And in any case, when
27738  /// visiting a sub-type, do not visit its children nodes. So this
27739  /// function only goes to the level below the level of the top-most
27740  /// type.
27741  ///
27742  /// @return true if we are at the same level as the top-most type,
27743  /// otherwise return false.
27744  bool
27745  visit_begin(type_base* t)
27746  {
27747  if (t != type_)
27748  {
27749  if (!t->get_canonical_type())
27750  // We are looking a sub-type of 'type_' which has no
27751  // canonical type. So tada! we found one! Get out right
27752  // now with the trophy.
27753  has_non_canonical_type_ = t;
27754 
27755  return false;
27756  }
27757  return true;
27758  }
27759 
27760  /// When we are done visiting a sub-type, if it's been flagged as
27761  /// been non-canonicalized, then stop the traversing.
27762  ///
27763  /// Otherwise, keep going.
27764  ///
27765  /// @return false iff the sub-type that has been visited is
27766  /// non-canonicalized.
27767  bool
27768  visit_end(type_base* )
27769  {
27770  if (has_non_canonical_type_)
27771  return false;
27772  return true;
27773  }
27774 }; //end class non_canonicalized_subtype_detector
27775 
27776 /// Test if a type has sub-types that are non-canonicalized.
27777 ///
27778 /// @param t the type which sub-types to consider.
27779 ///
27780 /// @return true if a type has sub-types that are non-canonicalized.
27781 type_base*
27783 {
27784  if (!t)
27785  return 0;
27786 
27787  non_canonicalized_subtype_detector v(t.get());
27788  t->traverse(v);
27789  return v.has_non_canonical_type();
27790 }
27791 
27792 /// Tests if the change of a given type effectively comes from just
27793 /// its sub-types. That is, if the type has changed but its type name
27794 /// hasn't changed, then the change of the type mostly likely is a
27795 /// sub-type change.
27796 ///
27797 /// @param t_v1 the first version of the type.
27798 ///
27799 /// @param t_v2 the second version of the type.
27800 ///
27801 /// @return true iff the type changed and the change is about its
27802 /// sub-types.
27803 bool
27804 type_has_sub_type_changes(const type_base_sptr t_v1,
27805  const type_base_sptr t_v2)
27806 {
27807  type_base_sptr t1 = strip_typedef(t_v1);
27808  type_base_sptr t2 = strip_typedef(t_v2);
27809 
27810  string repr1 = get_pretty_representation(t1, /*internal=*/false),
27811  repr2 = get_pretty_representation(t2, /*internal=*/false);
27812  return (t1 != t2 && repr1 == repr2);
27813 }
27814 
27815 /// Make sure that the life time of a given (smart pointer to a) type
27816 /// is the same as the life time of the libabigail library.
27817 ///
27818 /// @param t the type to consider.
27819 void
27820 keep_type_alive(type_base_sptr t)
27821 {
27822  const environment& env = t->get_environment();
27823  env.priv_->extra_live_types_.push_back(t);
27824 }
27825 
27826 /// Hash an ABI artifact that is either a type or a decl.
27827 ///
27828 /// This function intends to provides the fastest possible hashing for
27829 /// types and decls, while being completely correct.
27830 ///
27831 /// Note that if the artifact is a type and if it has a canonical
27832 /// type, the hash value is going to be the pointer value of the
27833 /// canonical type. Otherwise, this function computes a hash value
27834 /// for the type by recursively walking the type members. This last
27835 /// code path is possibly *very* slow and should only be used when
27836 /// only handful of types are going to be hashed.
27837 ///
27838 /// If the artifact is a decl, then a combination of the hash of its
27839 /// type and the hash of the other properties of the decl is computed.
27840 ///
27841 /// @param tod the type or decl to hash.
27842 ///
27843 /// @return the resulting hash value.
27844 size_t
27846 {
27847  size_t result = 0;
27848 
27849  if (tod == 0)
27850  ;
27851  else if (const type_base* t = is_type(tod))
27852  result = hash_type(t);
27853  else if (const decl_base* d = is_decl(tod))
27854  {
27855  if (var_decl* v = is_var_decl(d))
27856  {
27857  ABG_ASSERT(v->get_type());
27858  size_t h = hash_type_or_decl(v->get_type());
27859  string repr = v->get_pretty_representation(/*internal=*/true);
27860  std::hash<string> hash_string;
27861  h = hashing::combine_hashes(h, hash_string(repr));
27862  result = h;
27863  }
27864  else if (function_decl* f = is_function_decl(d))
27865  {
27866  ABG_ASSERT(f->get_type());
27867  size_t h = hash_type_or_decl(f->get_type());
27868  string repr = f->get_pretty_representation(/*internal=*/true);
27869  std::hash<string> hash_string;
27870  h = hashing::combine_hashes(h, hash_string(repr));
27871  result = h;
27872  }
27874  {
27875  type_base_sptr parm_type = p->get_type();
27876  ABG_ASSERT(parm_type);
27877  std::hash<bool> hash_bool;
27878  std::hash<unsigned> hash_unsigned;
27879  size_t h = hash_type_or_decl(parm_type);
27880  h = hashing::combine_hashes(h, hash_unsigned(p->get_index()));
27881  h = hashing::combine_hashes(h, hash_bool(p->get_variadic_marker()));
27882  result = h;
27883  }
27884  else if (class_decl::base_spec *bs = is_class_base_spec(d))
27885  {
27886  member_base::hash hash_member;
27887  std::hash<size_t> hash_size;
27888  std::hash<bool> hash_bool;
27889  type_base_sptr type = bs->get_base_class();
27890  size_t h = hash_type_or_decl(type);
27891  h = hashing::combine_hashes(h, hash_member(*bs));
27892  h = hashing::combine_hashes(h, hash_size(bs->get_offset_in_bits()));
27893  h = hashing::combine_hashes(h, hash_bool(bs->get_is_virtual()));
27894  result = h;
27895  }
27896  else
27897  // This is a *really* *SLOW* path. If it shows up in a
27898  // performance profile, I bet it'd be a good idea to try to
27899  // avoid it altogether.
27900  result = d->get_hash();
27901  }
27902  else
27903  // We should never get here.
27904  abort();
27905  return result;
27906 }
27907 
27908 /// Hash an ABI artifact that is either a type.
27909 ///
27910 /// This function intends to provides the fastest possible hashing for
27911 /// types while being completely correct.
27912 ///
27913 /// Note that if the type artifact has a canonical type, the hash
27914 /// value is going to be the pointer value of the canonical type.
27915 /// Otherwise, this function computes a hash value for the type by
27916 /// recursively walking the type members. This last code path is
27917 /// possibly *very* slow and should only be used when only handful of
27918 /// types are going to be hashed.
27919 ///
27920 /// @param t the type or decl to hash.
27921 ///
27922 /// @return the resulting hash value.
27923 size_t
27925 {return hash_as_canonical_type_or_constant(t);}
27926 
27927 /// Hash an ABI artifact that is either a type of a decl.
27928 ///
27929 /// @param tod the ABI artifact to hash.
27930 ///
27931 /// @return the hash value of the ABI artifact.
27932 size_t
27934 {return hash_type_or_decl(tod.get());}
27935 
27936 /// Test if a given type is allowed to be non canonicalized
27937 ///
27938 /// This is a subroutine of hash_as_canonical_type_or_constant.
27939 ///
27940 /// For now, the only types allowed to be non canonicalized in the
27941 /// system are (typedefs & pointers to) decl-only class/union, the
27942 /// void type and variadic parameter types.
27943 ///
27944 /// @return true iff @p t is a one of the only types allowed to be
27945 /// non-canonicalized in the system.
27946 bool
27948 {
27949  if (!t)
27950  return true;
27951 
27952  return (// The IR nodes for the types below are unique across the
27953  // entire ABI corpus. Thus, no need to canonicalize them.
27954  // Maybe we could say otherwise and canonicalize them once
27955  // for all so that they can be removed from here.
27956  is_unique_type(t)
27957 
27958  // An IR node for the types below can be equal to several
27959  // other types (i.e, a decl-only type t equals a fully
27960  // defined type of the same name in ODR-supported
27961  // languages). Hence, they can't be given a canonical type.
27962  //
27963  // TODO: Maybe add a mode that would detect ODR violations
27964  // that would make a decl-only type co-exists with several
27965  // different definitions of the type in the ABI corpus.
27968  /*look_through_decl_only=*/true)
27970 
27971 }
27972 
27973 /// Test if a type is unique in the entire environment.
27974 ///
27975 /// Examples of unique types are void, void* and variadic parameter
27976 /// types.
27977 ///
27978 /// @param t the type to test for.
27979 ///
27980 /// @return true iff the type @p t is unique in the entire
27981 /// environment.
27982 bool
27983 is_unique_type(const type_base_sptr& t)
27984 {return is_unique_type(t.get());}
27985 
27986 /// Test if a type is unique in the entire environment.
27987 ///
27988 /// Examples of unique types are void, void* and variadic parameter
27989 /// types.
27990 ///
27991 /// @param t the type to test for.
27992 ///
27993 /// @return true iff the type @p t is unique in the entire
27994 /// environment.
27995 bool
27997 {
27998  if (!t)
27999  return false;
28000 
28001  const environment& env = t->get_environment();
28002  return (env.is_void_type(t)
28003  || env.is_void_pointer_type(t)
28004  || env.is_variadic_parameter_type(t));
28005 }
28006 
28007 /// For a given type, return its exemplar type.
28008 ///
28009 /// For a given type, its exemplar type is either its canonical type
28010 /// or the canonical type of the definition type of a given
28011 /// declaration-only type. If the neither of those two types exist,
28012 /// then the exemplar type is the given type itself.
28013 ///
28014 /// @param type the input to consider.
28015 ///
28016 /// @return the exemplar type.
28017 type_base*
28019 {
28020  if (decl_base * decl = is_decl(type))
28021  {
28022  // Make sure we get the real definition of a decl-only type.
28023  decl = look_through_decl_only(decl);
28024  type = is_type(decl);
28025  ABG_ASSERT(type);
28026  }
28027  type_base *exemplar = type->get_naked_canonical_type();
28028  if (!exemplar)
28029  {
28030  // The type has no canonical type. Let's be sure that it's one
28031  // of those rare types that are allowed to be non canonicalized
28032  // in the system.
28033  exemplar = const_cast<type_base*>(type);
28035  }
28036  return exemplar;
28037 }
28038 
28039 /// Test if a given type is allowed to be non canonicalized
28040 ///
28041 /// This is a subroutine of hash_as_canonical_type_or_constant.
28042 ///
28043 /// For now, the only types allowed to be non canonicalized in the
28044 /// system are decl-only class/union and the void type.
28045 ///
28046 /// @return true iff @p t is a one of the only types allowed to be
28047 /// non-canonicalized in the system.
28048 bool
28049 is_non_canonicalized_type(const type_base_sptr& t)
28050 {return is_non_canonicalized_type(t.get());}
28051 
28052 /// Hash a type by either returning the pointer value of its canonical
28053 /// type or by returning a constant if the type doesn't have a
28054 /// canonical type.
28055 ///
28056 /// This is a subroutine of hash_type.
28057 ///
28058 /// @param t the type to consider.
28059 ///
28060 /// @return the hash value.
28061 static size_t
28062 hash_as_canonical_type_or_constant(const type_base *t)
28063 {
28064  type_base *canonical_type = 0;
28065 
28066  if (t)
28067  canonical_type = t->get_naked_canonical_type();
28068 
28069  if (!canonical_type)
28070  {
28071  // If the type doesn't have a canonical type, maybe it's because
28072  // it's a declaration-only type? If that's the case, let's try
28073  // to get the canonical type of the definition of this
28074  // declaration.
28075  decl_base *decl = is_decl(t);
28076  if (decl
28077  && decl->get_is_declaration_only()
28079  {
28080  type_base *definition =
28082  ABG_ASSERT(definition);
28083  canonical_type = definition->get_naked_canonical_type();
28084  }
28085  }
28086 
28087  if (canonical_type)
28088  return reinterpret_cast<size_t>(canonical_type);
28089 
28090  // If we reached this point, it means we are seeing a
28091  // non-canonicalized type. It must be a decl-only class or a void
28092  // type, otherwise it means that for some weird reason, the type
28093  // hasn't been canonicalized. It should be!
28095 
28096  return 0xDEADBABE;
28097 }
28098 
28099 /// Test if the pretty representation of a given @ref function_decl is
28100 /// lexicographically less then the pretty representation of another
28101 /// @ref function_decl.
28102 ///
28103 /// @param f the first @ref function_decl to consider for comparison.
28104 ///
28105 /// @param s the second @ref function_decl to consider for comparison.
28106 ///
28107 /// @return true iff the pretty representation of @p f is
28108 /// lexicographically less than the pretty representation of @p s.
28109 bool
28111 {
28114 
28115  if (fr != sr)
28116  return fr < sr;
28117 
28118  fr = f.get_pretty_representation(/*internal=*/true),
28119  sr = s.get_pretty_representation(/*internal=*/true);
28120 
28121  if (fr != sr)
28122  return fr < sr;
28123 
28124  if (f.get_symbol())
28125  fr = f.get_symbol()->get_id_string();
28126  else if (!f.get_linkage_name().empty())
28127  fr = f.get_linkage_name();
28128 
28129  if (s.get_symbol())
28130  sr = s.get_symbol()->get_id_string();
28131  else if (!s.get_linkage_name().empty())
28132  sr = s.get_linkage_name();
28133 
28134  return fr < sr;
28135 }
28136 
28137 /// Test if two types have similar structures, even though they are
28138 /// (or can be) different.
28139 ///
28140 /// const and volatile qualifiers are completely ignored.
28141 ///
28142 /// typedef are resolved to their definitions; their names are ignored.
28143 ///
28144 /// Two indirect types (pointers or references) have similar structure
28145 /// if their underlying types are of the same kind and have the same
28146 /// name. In the indirect types case, the size of the underlying type
28147 /// does not matter.
28148 ///
28149 /// Two direct types (i.e, non indirect) have a similar structure if
28150 /// they have the same kind, name and size. Two class types have
28151 /// similar structure if they have the same name, size, and if the
28152 /// types of their data members have similar types.
28153 ///
28154 /// @param first the first type to consider.
28155 ///
28156 /// @param second the second type to consider.
28157 ///
28158 /// @param indirect_type whether to do an indirect comparison
28159 ///
28160 /// @return true iff @p first and @p second have similar structures.
28161 bool
28162 types_have_similar_structure(const type_base_sptr& first,
28163  const type_base_sptr& second,
28164  bool indirect_type)
28165 {return types_have_similar_structure(first.get(), second.get(), indirect_type);}
28166 
28167 /// Test if two types have similar structures, even though they are
28168 /// (or can be) different.
28169 ///
28170 /// const and volatile qualifiers are completely ignored.
28171 ///
28172 /// typedef are resolved to their definitions; their names are ignored.
28173 ///
28174 /// Two indirect types (pointers, references or arrays) have similar
28175 /// structure if their underlying types are of the same kind and have
28176 /// the same name. In the indirect types case, the size of the
28177 /// underlying type does not matter.
28178 ///
28179 /// Two direct types (i.e, non indirect) have a similar structure if
28180 /// they have the same kind, name and size. Two class types have
28181 /// similar structure if they have the same name, size, and if the
28182 /// types of their data members have similar types.
28183 ///
28184 /// @param first the first type to consider.
28185 ///
28186 /// @param second the second type to consider.
28187 ///
28188 /// @param indirect_type if true, then consider @p first and @p
28189 /// second as being underlying types of indirect types. Meaning that
28190 /// their size does not matter.
28191 ///
28192 /// @return true iff @p first and @p second have similar structures.
28193 bool
28195  const type_base* second,
28196  bool indirect_type)
28197 {
28198  if (!!first != !!second)
28199  return false;
28200 
28201  if (!first)
28202  return false;
28203 
28204  // Treat typedefs purely as type aliases and ignore CV-qualifiers.
28205  first = peel_qualified_or_typedef_type(first);
28206  second = peel_qualified_or_typedef_type(second);
28207 
28208  // Eliminate all but N of the N^2 comparison cases. This also guarantees the
28209  // various ty2 below cannot be null.
28210  if (typeid(*first) != typeid(*second))
28211  return false;
28212 
28213  // Peel off matching pointers.
28214  if (const pointer_type_def* ty1 = is_pointer_type(first))
28215  {
28216  const pointer_type_def* ty2 = is_pointer_type(second);
28217  return types_have_similar_structure(ty1->get_pointed_to_type(),
28218  ty2->get_pointed_to_type(),
28219  /*indirect_type=*/true);
28220  }
28221 
28222  // Peel off matching references.
28223  if (const reference_type_def* ty1 = is_reference_type(first))
28224  {
28225  const reference_type_def* ty2 = is_reference_type(second);
28226  if (ty1->is_lvalue() != ty2->is_lvalue())
28227  return false;
28228  return types_have_similar_structure(ty1->get_pointed_to_type(),
28229  ty2->get_pointed_to_type(),
28230  /*indirect_type=*/true);
28231  }
28232 
28233  // Peel off matching pointer-to-member types.
28234  if (const ptr_to_mbr_type* ty1 = is_ptr_to_mbr_type(first))
28235  {
28236  const ptr_to_mbr_type* ty2 = is_ptr_to_mbr_type(second);
28237  return (types_have_similar_structure(ty1->get_member_type(),
28238  ty2->get_member_type(),
28239  /*indirect_type=*/true)
28240  && types_have_similar_structure(ty1->get_containing_type(),
28241  ty2->get_containing_type(),
28242  /*indirect_type=*/true));
28243  }
28244 
28245  if (const type_decl* ty1 = is_type_decl(first))
28246  {
28247  const type_decl* ty2 = is_type_decl(second);
28248  if (!indirect_type)
28249  if (ty1->get_size_in_bits() != ty2->get_size_in_bits())
28250  return false;
28251 
28252  return ty1->get_name() == ty2->get_name();
28253  }
28254 
28255  if (const enum_type_decl* ty1 = is_enum_type(first))
28256  {
28257  const enum_type_decl* ty2 = is_enum_type(second);
28258  if (!indirect_type)
28259  if (ty1->get_size_in_bits() != ty2->get_size_in_bits())
28260  return false;
28261 
28262  return (get_name(ty1->get_underlying_type())
28263  == get_name(ty2->get_underlying_type()));
28264  }
28265 
28266  if (const class_decl* ty1 = is_class_type(first))
28267  {
28268  const class_decl* ty2 = is_class_type(second);
28269  if (!ty1->get_is_anonymous() && !ty2->get_is_anonymous()
28270  && ty1->get_name() != ty2->get_name())
28271  return false;
28272 
28273  if (!indirect_type)
28274  {
28275  if ((ty1->get_size_in_bits() != ty2->get_size_in_bits())
28276  || (ty1->get_non_static_data_members().size()
28277  != ty2->get_non_static_data_members().size()))
28278  return false;
28279 
28280  for (class_or_union::data_members::const_iterator
28281  i = ty1->get_non_static_data_members().begin(),
28282  j = ty2->get_non_static_data_members().begin();
28283  (i != ty1->get_non_static_data_members().end()
28284  && j != ty2->get_non_static_data_members().end());
28285  ++i, ++j)
28286  {
28287  var_decl_sptr dm1 = *i;
28288  var_decl_sptr dm2 = *j;
28289  if (!types_have_similar_structure(dm1->get_type().get(),
28290  dm2->get_type().get(),
28291  indirect_type))
28292  return false;
28293  }
28294  }
28295 
28296  return true;
28297  }
28298 
28299  if (const union_decl* ty1 = is_union_type(first))
28300  {
28301  const union_decl* ty2 = is_union_type(second);
28302  if (!ty1->get_is_anonymous() && !ty2->get_is_anonymous()
28303  && ty1->get_name() != ty2->get_name())
28304  return false;
28305 
28306  if (!indirect_type)
28307  return ty1->get_size_in_bits() == ty2->get_size_in_bits();
28308 
28309  return true;
28310  }
28311 
28312  if (const array_type_def* ty1 = is_array_type(first))
28313  {
28314  const array_type_def* ty2 = is_array_type(second);
28315  // TODO: Handle int[5][2] vs int[2][5] better.
28316  if (!indirect_type)
28317  {
28318  if (ty1->get_size_in_bits() != ty2->get_size_in_bits()
28319  || ty1->get_dimension_count() != ty2->get_dimension_count())
28320  return false;
28321  }
28322 
28323  if (!types_have_similar_structure(ty1->get_element_type(),
28324  ty2->get_element_type(),
28325  /*indirect_type=*/true))
28326  return false;
28327 
28328  return true;
28329  }
28330 
28331  if (const array_type_def::subrange_type *ty1 = is_subrange_type(first))
28332  {
28333  const array_type_def::subrange_type *ty2 = is_subrange_type(second);
28334  if (ty1->get_upper_bound() != ty2->get_upper_bound()
28335  || ty1->get_lower_bound() != ty2->get_lower_bound()
28336  || ty1->get_language() != ty2->get_language()
28337  || !types_have_similar_structure(ty1->get_underlying_type(),
28338  ty2->get_underlying_type(),
28339  indirect_type))
28340  return false;
28341 
28342  return true;
28343  }
28344 
28345  if (const function_type* ty1 = is_function_type(first))
28346  {
28347  const function_type* ty2 = is_function_type(second);
28348  if (!types_have_similar_structure(ty1->get_return_type(),
28349  ty2->get_return_type(),
28350  indirect_type))
28351  return false;
28352 
28353  if (ty1->get_parameters().size() != ty2->get_parameters().size())
28354  return false;
28355 
28356  for (function_type::parameters::const_iterator
28357  i = ty1->get_parameters().begin(),
28358  j = ty2->get_parameters().begin();
28359  (i != ty1->get_parameters().end()
28360  && j != ty2->get_parameters().end());
28361  ++i, ++j)
28362  if (!types_have_similar_structure((*i)->get_type(),
28363  (*j)->get_type(),
28364  indirect_type))
28365  return false;
28366 
28367  return true;
28368  }
28369 
28370  // All kinds of type should have been handled at this point.
28372 
28373  return false;
28374 }
28375 
28376 /// Look for a data member of a given class, struct or union type and
28377 /// return it.
28378 ///
28379 /// The data member is designated by its name.
28380 ///
28381 /// @param type the class, struct or union type to consider.
28382 ///
28383 /// @param dm_name the name of the data member to lookup.
28384 ///
28385 /// @return the data member iff it was found in @type or NULL if no
28386 /// data member with that name was found.
28387 const var_decl*
28389  const char* dm_name)
28390 
28391 {
28393  if (!cou)
28394  return 0;
28395 
28396  return cou->find_data_member(dm_name).get();
28397 }
28398 
28399 /// Look for a data member of a given class, struct or union type and
28400 /// return it.
28401 ///
28402 /// The data member is designated by its name.
28403 ///
28404 /// @param type the class, struct or union type to consider.
28405 ///
28406 /// @param dm the data member to lookup.
28407 ///
28408 /// @return the data member iff it was found in @type or NULL if no
28409 /// data member with that name was found.
28410 const var_decl_sptr
28411 lookup_data_member(const type_base_sptr& type, const var_decl_sptr& dm)
28412 {
28413  class_or_union_sptr cou = is_class_or_union_type(type);
28414  if (!cou)
28415  return var_decl_sptr();
28416 
28417  return cou->find_data_member(dm);
28418 }
28419 
28420 /// Get the function parameter designated by its index.
28421 ///
28422 /// Note that the first function parameter has index 0.
28423 ///
28424 /// @param fun the function to consider.
28425 ///
28426 /// @param parm_index the index of the function parameter to get.
28427 ///
28428 /// @return the function parameter designated by its index, of NULL if
28429 /// no function parameter with that index was found.
28432  unsigned parm_index)
28433 {
28434  function_decl* fn = is_function_decl(fun);
28435  if (!fn)
28436  return 0;
28437 
28438  const function_decl::parameters &parms = fn->get_type()->get_parameters();
28439  if (parms.size() <= parm_index)
28440  return 0;
28441 
28442  return parms[parm_index].get();
28443 }
28444 
28445 /// Build the internal name of the underlying type of an enum.
28446 ///
28447 /// @param base_name the (unqualified) name of the enum the underlying
28448 /// type is destined to.
28449 ///
28450 /// @param is_anonymous true if the underlying type of the enum is to
28451 /// be anonymous.
28452 string
28454  bool is_anonymous,
28455  uint64_t size)
28456 {
28457  std::ostringstream o;
28458 
28459  if (is_anonymous)
28460  o << "unnamed-enum";
28461  else
28462  o << "enum-" << base_name;
28463 
28464  o << "-underlying-type-" << size;
28465 
28466  return o.str();
28467 }
28468 
28469 /// Find the first data member of a class or union which name matches
28470 /// a regular expression.
28471 ///
28472 /// @param t the class or union to consider.
28473 ///
28474 /// @param r the regular expression to consider.
28475 ///
28476 /// @return the data member matched by @p r or nil if none was found.
28479  const regex::regex_t_sptr& r)
28480 {
28481  for (auto data_member : t.get_data_members())
28482  {
28483  if (regex::match(r, data_member->get_name()))
28484  return data_member;
28485  }
28486 
28487  return var_decl_sptr();
28488 }
28489 
28490 /// Find the last data member of a class or union which name matches
28491 /// a regular expression.
28492 ///
28493 /// @param t the class or union to consider.
28494 ///
28495 /// @param r the regular expression to consider.
28496 ///
28497 /// @return the data member matched by @p r or nil if none was found.
28500  const regex::regex_t_sptr& regex)
28501 {
28502  auto d = t.get_data_members().rbegin();
28503  auto e = t.get_data_members().rend();
28504  for (; d != e; ++d)
28505  {
28506  if (regex::match(regex, (*d)->get_name()))
28507  return *d;
28508  }
28509 
28510  return var_decl_sptr();
28511 }
28512 
28513 /// Emit the pretty representation of the parameters of a function
28514 /// type.
28515 ///
28516 /// @param fn_type the function type to consider.
28517 ///
28518 /// @param o the output stream to emit the pretty representation to.
28519 ///
28520 /// @param qualified if true, emit fully qualified names.
28521 ///
28522 /// @param internal if true, then the result is to be used for the
28523 /// purpose of type canonicalization.
28524 static void
28525 stream_pretty_representation_of_fn_parms(const function_type& fn_type,
28526  ostream& o, bool qualified,
28527  bool internal)
28528 {
28529  o << "(";
28530  if (fn_type.get_parameters().empty())
28531  o << "void";
28532  else
28533  {
28534  type_base_sptr type;
28535  auto end = fn_type.get_parameters().end();
28536  auto first_parm = fn_type.get_first_non_implicit_parm();
28538  const environment& env = fn_type.get_environment();
28539  for (auto i = fn_type.get_first_non_implicit_parm(); i != end; ++i)
28540  {
28541  if (i != first_parm)
28542  o << ", ";
28543  parm = *i;
28544  type = parm->get_type();
28545  if (env.is_variadic_parameter_type(type))
28546  o << "...";
28547  else
28548  {
28549  if (internal)
28550  type = peel_typedef_type(type);
28551  o << get_type_name(type, qualified, internal);
28552  }
28553  }
28554  }
28555  o << ")";
28556 }
28557 
28558 /// When constructing the name of a pointer to function type, add the
28559 /// return type to the left of the existing type identifier, and the
28560 /// parameters declarator to the right.
28561 ///
28562 /// This function considers the name of the type as an expression.
28563 ///
28564 /// The resulting type expr is going to be made of three parts:
28565 /// left_expr inner_expr right_expr.
28566 ///
28567 /// Suppose we want to build the type expression representing:
28568 ///
28569 /// "an array of pointer to function taking a char parameter and
28570 /// returning an int".
28571 ///
28572 /// It's going to look like:
28573 ///
28574 /// int(*a[])(char);
28575 ///
28576 /// Suppose the caller of this function started to emit the inner
28577 /// "a[]" part of the expression already. It thus calls this
28578 /// function with that input "a[]" part. We consider that "a[]" as
28579 /// the "type identifier".
28580 ///
28581 /// So the inner_expr is going to be "(*a[])".
28582 ///
28583 /// The left_expr part is "int". The right_expr part is "(char)".
28584 ///
28585 /// In other words, this function adds the left_expr and right_expr to
28586 /// the inner_expr. left_expr and right_expr are called "outer
28587 /// pointer to function type expression".
28588 ///
28589 /// This is a sub-routine of @ref pointer_declaration_name() and @ref
28590 /// array_declaration_name()
28591 ///
28592 /// @param p the pointer to function type to consider.
28593 ///
28594 /// @param input the type-id to use as the inner expression of the
28595 /// overall pointer-to-function type expression
28596 ///
28597 /// @param qualified if true then use qualified names in the resulting
28598 /// type name.
28599 ///
28600 /// @param internal if true then the resulting type name is going to
28601 /// be used for type canonicalization purposes.
28602 ///
28603 /// @return the name of the pointer to function type.
28604 static string
28605 add_outer_pointer_to_fn_type_expr(const type_base* p,
28606  const string& input,
28607  bool qualified, bool internal)
28608 {
28609  if (!p)
28610  return "";
28611 
28612  function_type_sptr pointed_to_fn;
28613  string star_or_ref;
28614 
28615  if (const pointer_type_def* ptr = is_pointer_type(p))
28616  {
28617  pointed_to_fn = is_function_type(ptr->get_pointed_to_type());
28618  star_or_ref= "*";
28619  }
28620  else if (const reference_type_def* ref = is_reference_type(p))
28621  {
28622  star_or_ref = "&";
28623  pointed_to_fn = is_function_type(ref->get_pointed_to_type());
28624  }
28625 
28626  if (!pointed_to_fn)
28627  return "";
28628 
28629  std::ostringstream left, right, inner;
28630 
28631  inner << "(" << star_or_ref << input << ")";
28632 
28633  type_base_sptr type;
28634  stream_pretty_representation_of_fn_parms(*pointed_to_fn, right,
28635  qualified, internal);
28636  type_base_sptr return_type =
28637  internal
28638  ? peel_typedef_type(pointed_to_fn->get_return_type())
28639  : pointed_to_fn->get_return_type();
28640 
28641  string result;
28642 
28643  if (is_npaf_type(return_type)
28644  || !(is_pointer_to_function_type(return_type)
28645  || is_pointer_to_array_type(return_type)))
28646  {
28647  if (return_type)
28648  left << get_type_name(return_type, qualified, internal);
28649  result = left.str() + " " + inner.str() + right.str();
28650  }
28651  else if (pointer_type_def_sptr p = is_pointer_to_function_type(return_type))
28652  {
28653  string inner_string = inner.str() + right.str();
28654  result = add_outer_pointer_to_fn_type_expr(p, inner_string,
28655  qualified, internal);
28656  }
28657  else if (pointer_type_def_sptr p = is_pointer_to_array_type(return_type))
28658  {
28659  string inner_string = inner.str() + right.str();
28660  result = add_outer_pointer_to_array_type_expr(p, inner_string,
28661  qualified, internal);
28662  }
28663  else
28665 
28666  return result;
28667 }
28668 
28669 /// When constructing the name of a pointer to function type, add the
28670 /// return type to the left of the existing type identifier, and the
28671 /// parameters declarator to the right.
28672 ///
28673 /// This function considers the name of the type as an expression.
28674 ///
28675 /// The resulting type expr is going to be made of three parts:
28676 /// left_expr inner_expr right_expr.
28677 ///
28678 /// Suppose we want to build the type expression representing:
28679 ///
28680 /// "an array of pointer to function taking a char parameter and
28681 /// returning an int".
28682 ///
28683 /// It's going to look like:
28684 ///
28685 /// int(*a[])(char);
28686 ///
28687 /// Suppose the caller of this function started to emit the inner
28688 /// "a[]" part of the expression already. It thus calls this
28689 /// function with that input "a[]" part. We consider that "a[]" as
28690 /// the "type identifier".
28691 ///
28692 /// So the inner_expr is going to be "(*a[])".
28693 ///
28694 /// The left_expr part is "int". The right_expr part is "(char)".
28695 ///
28696 /// In other words, this function adds the left_expr and right_expr to
28697 /// the inner_expr. left_expr and right_expr are called "outer
28698 /// pointer to function type expression".
28699 ///
28700 /// This is a sub-routine of @ref pointer_declaration_name() and @ref
28701 /// array_declaration_name()
28702 ///
28703 /// @param p the pointer to function type to consider.
28704 ///
28705 /// @param input the type-id to use as the inner expression of the
28706 /// overall pointer-to-function type expression
28707 ///
28708 /// @param qualified if true then use qualified names in the resulting
28709 /// type name.
28710 ///
28711 /// @param internal if true then the resulting type name is going to
28712 /// be used for type canonicalization purposes.
28713 ///
28714 /// @return the name of the pointer to function type.
28715 static string
28716 add_outer_pointer_to_fn_type_expr(const type_base_sptr& p,
28717  const string& input,
28718  bool qualified, bool internal)
28719 {return add_outer_pointer_to_fn_type_expr(p.get(), input, qualified, internal);}
28720 
28721 /// When constructing the name of a pointer to array type, add the
28722 /// array element type type to the left of the existing type
28723 /// identifier, and the array declarator part to the right.
28724 ///
28725 /// This function considers the name of the type as an expression.
28726 ///
28727 /// The resulting type expr is going to be made of three parts:
28728 /// left_expr inner_expr right_expr.
28729 ///
28730 /// Suppose we want to build the type expression representing:
28731 ///
28732 /// "a pointer to an array of int".
28733 ///
28734 /// It's going to look like:
28735 ///
28736 /// int(*foo)[];
28737 ///
28738 /// Suppose the caller of this function started to emit the inner
28739 /// "foo" part of the expression already. It thus calls this function
28740 /// with that input "foo" part. We consider that "foo" as the "type
28741 /// identifier".
28742 ///
28743 /// So we are passed an input string that is "foo" and it's going to
28744 /// be turned into the inner_expr part, which is going to be "(*foo)".
28745 ///
28746 /// The left_expr part is "int". The right_expr part is "[]".
28747 ///
28748 /// In other words, this function adds the left_expr and right_expr to
28749 /// the inner_expr. left_expr and right_expr are called "outer
28750 /// pointer to array type expression".
28751 ///
28752 /// The model of this function was taken from the article "Reading C
28753 /// type declaration", from Steve Friedl at
28754 /// http://unixwiz.net/techtips/reading-cdecl.html.
28755 ///
28756 /// This is a sub-routine of @ref pointer_declaration_name() and @ref
28757 /// array_declaration_name()
28758 ///
28759 /// @param p the pointer to array type to consider.
28760 ///
28761 /// @param input the type-id to start from as the inner part of the
28762 /// final type name.
28763 ///
28764 /// @param qualified if true then use qualified names in the resulting
28765 /// type name.
28766 ///
28767 /// @param internal if true then the resulting type name is going to
28768 /// be used for type canonicalization purposes.
28769 ///
28770 /// @return the name of the pointer to array type.
28771 static string
28772 add_outer_pointer_to_array_type_expr(const type_base* p,
28773  const string& input, bool qualified,
28774  bool internal)
28775 {
28776  if (!p)
28777  return "";
28778 
28779  string star_or_ref;
28780  type_base_sptr pointed_to_type;
28781 
28782  if (const pointer_type_def *ptr = is_pointer_type(p))
28783  {
28784  pointed_to_type = ptr->get_pointed_to_type();
28785  star_or_ref = "*";
28786  }
28787  else if (const reference_type_def *ref = is_reference_type(p))
28788  {
28789  pointed_to_type = ref->get_pointed_to_type();
28790  star_or_ref = "&";
28791  }
28792 
28793  array_type_def_sptr array = is_array_type(pointed_to_type);
28794  if (!array)
28795  return "";
28796 
28797  std::ostringstream left, right, inner;
28798  inner << "(" << star_or_ref << input << ")";
28799  right << array->get_subrange_representation();
28800  string result;
28801 
28802  type_base_sptr array_element_type = array->get_element_type();
28803 
28804  if (is_npaf_type(array_element_type)
28805  || !(is_pointer_to_function_type(array_element_type)
28806  || is_pointer_to_array_type(array_element_type)))
28807  {
28808  left << get_type_name(array_element_type, qualified, internal);
28809  result = left.str() + inner.str() + right.str();
28810  }
28811  else if (pointer_type_def_sptr p =
28812  is_pointer_to_function_type(array_element_type))
28813  {
28814  string r = inner.str() + right.str();
28815  result = add_outer_pointer_to_fn_type_expr(p, r, qualified, internal);
28816  }
28817  else if (pointer_type_def_sptr p =
28818  is_pointer_to_array_type(array_element_type))
28819  {
28820  string inner_string = inner.str() + right.str();
28821  result = add_outer_pointer_to_array_type_expr(p, inner_string,
28822  qualified, internal);
28823  }
28824  else
28826 
28827  return result;
28828 }
28829 
28830 /// When constructing the name of a pointer to array type, add the
28831 /// array element type type to the left of the existing type
28832 /// identifier, and the array declarator part to the right.
28833 ///
28834 /// This function considers the name of the type as an expression.
28835 ///
28836 /// The resulting type expr is going to be made of three parts:
28837 /// left_expr inner_expr right_expr.
28838 ///
28839 /// Suppose we want to build the type expression representing:
28840 ///
28841 /// "a pointer to an array of int".
28842 ///
28843 /// It's going to look like:
28844 ///
28845 /// int(*foo)[];
28846 ///
28847 /// Suppose the caller of this function started to emit the inner
28848 /// "foo" part of the expression already. It thus calls this function
28849 /// with that input "foo" part. We consider that "foo" as the "type
28850 /// identifier".
28851 ///
28852 /// So we are passed an input string that is "foo" and it's going to
28853 /// be turned into the inner_expr part, which is going to be "(*foo)".
28854 ///
28855 /// The left_expr part is "int". The right_expr part is "[]".
28856 ///
28857 /// In other words, this function adds the left_expr and right_expr to
28858 /// the inner_expr. left_expr and right_expr are called "outer
28859 /// pointer to array type expression".
28860 ///
28861 /// The model of this function was taken from the article "Reading C
28862 /// type declaration", from Steve Friedl at
28863 /// http://unixwiz.net/techtips/reading-cdecl.html.
28864 ///
28865 /// This is a sub-routine of @ref pointer_declaration_name() and @ref
28866 /// array_declaration_name()
28867 ///
28868 /// @param p the pointer to array type to consider.
28869 ///
28870 /// @param input the type-id to start from as the inner part of the
28871 /// final type name.
28872 ///
28873 /// @param qualified if true then use qualified names in the resulting
28874 /// type name.
28875 ///
28876 /// @param internal if true then the resulting type name is going to
28877 /// be used for type canonicalization purposes.
28878 ///
28879 /// @return the name of the pointer to array type.
28880 static string
28881 add_outer_pointer_to_array_type_expr(const type_base_sptr& pointer_to_ar,
28882  const string& input, bool qualified,
28883  bool internal)
28884 {return add_outer_pointer_to_array_type_expr(pointer_to_ar.get(),
28885  input, qualified, internal);}
28886 
28887 /// When constructing the name of a pointer to mebmer type, add the
28888 /// return type to the left of the existing type identifier, and the
28889 /// parameters declarator to the right.
28890 ///
28891 /// This function considers the name of the type as an expression.
28892 ///
28893 /// The resulting type expr is going to be made of three parts:
28894 /// left_expr inner_expr right_expr.
28895 ///
28896 /// Suppose we want to build the type expression representing:
28897 ///
28898 /// "an array of pointer to member function (of a containing struct
28899 /// X) taking a char parameter and returning an int".
28900 ///
28901 /// It's going to look like:
28902 ///
28903 /// int (X::* a[])(char);
28904 ///
28905 /// Suppose the caller of this function started to emit the inner
28906 /// "a[]" part of the expression already. It thus calls this
28907 /// function with that input "a[]" part. We consider that "a[]" as
28908 /// the "type identifier".
28909 ///
28910 /// So the inner_expr is going to be "(X::* a[])".
28911 ///
28912 /// The left_expr part is "int". The right_expr part is "(char)".
28913 ///
28914 /// In other words, this function adds the left_expr and right_expr to
28915 /// the inner_expr. left_expr and right_expr are called "outer
28916 /// pointer to member type expression".
28917 ///
28918 /// This is a sub-routine of @ref ptr_to_mbr_declaration_name().
28919 ///
28920 /// @param p the pointer to member type to consider.
28921 ///
28922 /// @param input the type-id to use as the inner expression of the
28923 /// overall pointer-to-member type expression
28924 ///
28925 /// @param qualified if true then use qualified names in the resulting
28926 /// type name.
28927 ///
28928 /// @param internal if true then the resulting type name is going to
28929 /// be used for type canonicalization purposes.
28930 ///
28931 /// @return the name of the pointer to member type.
28932 static string
28933 add_outer_ptr_to_mbr_type_expr(const ptr_to_mbr_type* p,
28934  const string& input, bool qualified,
28935  bool internal)
28936 {
28937  if (!p)
28938  return "";
28939 
28940  std::ostringstream left, right, inner;
28941  string containing_type_name = get_type_name(p->get_containing_type(),
28942  qualified, internal);
28943  type_base_sptr mbr_type = p->get_member_type();
28944  string result;
28945  if (function_type_sptr fn_type = is_function_type(mbr_type))
28946  {
28947  inner << "(" << containing_type_name << "::*" << input << ")";
28948  stream_pretty_representation_of_fn_parms(*fn_type, right,
28949  qualified, internal);
28950  type_base_sptr return_type = fn_type->get_return_type();
28951  if (is_npaf_type(return_type)
28952  || !(is_pointer_to_function_type(return_type)
28953  || is_pointer_to_array_type(return_type)
28954  || is_pointer_to_ptr_to_mbr_type(return_type)
28955  || is_ptr_to_mbr_type(return_type)))
28956  {
28957  left << get_type_name(return_type, qualified, internal) << " ";;
28958  result = left.str() + inner.str() + right.str();
28959  }
28960  else if (pointer_type_def_sptr p = is_pointer_type(return_type))
28961  {
28962  string inner_str = inner.str() + right.str();
28963  result = pointer_declaration_name(p, inner_str, qualified, internal);
28964  }
28965  else if (ptr_to_mbr_type_sptr p = is_ptr_to_mbr_type(return_type))
28966  {
28967  string inner_str = inner.str() + right.str();
28968  result = add_outer_ptr_to_mbr_type_expr(p, inner_str,
28969  qualified, internal);
28970  }
28971  else
28973  }
28974  else if (ptr_to_mbr_type_sptr ptr_mbr_type = is_ptr_to_mbr_type(mbr_type))
28975  {
28976  inner << "(" << containing_type_name << "::*" << input << ")";
28977  stream_pretty_representation_of_fn_parms(*fn_type, right,
28978  qualified, internal);
28979  string inner_str = inner.str() + right.str();
28980  result = add_outer_ptr_to_mbr_type_expr(ptr_mbr_type, inner_str,
28981  qualified, internal);
28982  }
28983  else
28984  {
28985  left << get_type_name(p->get_member_type(), qualified, internal) << " ";
28986  inner << containing_type_name << "::*" << input;
28987  result = left.str()+ inner.str();
28988  }
28989 
28990  return result;
28991 }
28992 
28993 /// When constructing the name of a pointer to mebmer type, add the
28994 /// return type to the left of the existing type identifier, and the
28995 /// parameters declarator to the right.
28996 ///
28997 /// This function considers the name of the type as an expression.
28998 ///
28999 /// The resulting type expr is going to be made of three parts:
29000 /// left_expr inner_expr right_expr.
29001 ///
29002 /// Suppose we want to build the type expression representing:
29003 ///
29004 /// "an array of pointer to member function (of a containing struct
29005 /// X) taking a char parameter and returning an int".
29006 ///
29007 /// It's going to look like:
29008 ///
29009 /// int (X::* a[])(char);
29010 ///
29011 /// Suppose the caller of this function started to emit the inner
29012 /// "a[]" part of the expression already. It thus calls this
29013 /// function with that input "a[]" part. We consider that "a[]" as
29014 /// the "type identifier".
29015 ///
29016 /// So the inner_expr is going to be "(X::* a[])".
29017 ///
29018 /// The left_expr part is "int". The right_expr part is "(char)".
29019 ///
29020 /// In other words, this function adds the left_expr and right_expr to
29021 /// the inner_expr. left_expr and right_expr are called "outer
29022 /// pointer to member type expression".
29023 ///
29024 /// This is a sub-routine of @ref ptr_to_mbr_declaration_name().
29025 ///
29026 /// @param p the pointer to member type to consider.
29027 ///
29028 /// @param input the type-id to use as the inner expression of the
29029 /// overall pointer-to-member type expression
29030 ///
29031 /// @param qualified if true then use qualified names in the resulting
29032 /// type name.
29033 ///
29034 /// @param internal if true then the resulting type name is going to
29035 /// be used for type canonicalization purposes.
29036 ///
29037 /// @return the name of the pointer to member type.
29038 static string
29039 add_outer_ptr_to_mbr_type_expr(const ptr_to_mbr_type_sptr& p,
29040  const string& input, bool qualified,
29041  bool internal)
29042 {return add_outer_ptr_to_mbr_type_expr(p.get(), input, qualified, internal);}
29043 
29044 /// This adds the outer parts of a pointer to a pointer-to-member
29045 /// expression.
29046 ///
29047 /// Please read the comments of @ref add_outer_ptr_to_mbr_type_expr to
29048 /// learn more about this function, which is similar.
29049 ///
29050 /// This is a sub-routine of @ref pointer_declaration_name().
29051 ///
29052 /// @param a pointer (or reference) to a pointer-to-member type.
29053 ///
29054 /// @param input the inner type-id to add the outer parts to.
29055 ///
29056 /// @param qualified if true then use qualified names in the resulting
29057 /// type name.
29058 ///
29059 /// @param internal if true then the resulting type name is going to
29060 /// be used for type canonicalization purposes.
29061 static string
29062 add_outer_pointer_to_ptr_to_mbr_type_expr(const type_base* p,
29063  const string& input, bool qualified,
29064  bool internal)
29065 {
29066  if (!p)
29067  return "";
29068 
29069  string star_or_ref;
29070  type_base_sptr pointed_to_type;
29071 
29072  if (const pointer_type_def* ptr = is_pointer_type(p))
29073  {
29074  pointed_to_type = ptr->get_pointed_to_type();
29075  star_or_ref = "*";
29076  }
29077  else if (const reference_type_def* ref = is_reference_type(p))
29078  {
29079  pointed_to_type= ref->get_pointed_to_type();
29080  star_or_ref = "&";
29081  }
29082 
29083  if (!pointed_to_type)
29084  return "";
29085 
29086  ptr_to_mbr_type_sptr pointed_to_ptr_to_mbr =
29087  is_ptr_to_mbr_type(pointed_to_type);
29088  if (!pointed_to_ptr_to_mbr)
29089  return "";
29090 
29091  std::ostringstream inner;
29092  inner << star_or_ref << input;
29093  string result = add_outer_ptr_to_mbr_type_expr(pointed_to_ptr_to_mbr,
29094  inner.str(),
29095  qualified, internal);
29096  return result;
29097 }
29098 
29099 /// Emit the name of a pointer declaration.
29100 ///
29101 /// @param the pointer to consider.
29102 ///
29103 /// @param idname the name of the variable that has @p as a type or
29104 /// the id of the type. If it's empty then the resulting name is
29105 /// going to be the abstract name of the type.
29106 ///
29107 /// @param qualified if true then the type name is going to be
29108 /// fully qualified.
29109 ///
29110 /// @param internal if true then the type name is going to be used for
29111 /// type canonicalization purposes.
29112 static interned_string
29113 pointer_declaration_name(const type_base* ptr,
29114  const string& idname,
29115  bool qualified, bool internal)
29116 {
29117  if (!ptr)
29118  return interned_string();
29119 
29120  type_base_sptr pointed_to_type;
29121  string star_or_ref;
29122  if (const pointer_type_def* p = is_pointer_type(ptr))
29123  {
29124  pointed_to_type = p->get_pointed_to_type();
29125  star_or_ref = "*";
29126  }
29127  else if (const reference_type_def* p = is_reference_type(ptr))
29128  {
29129  pointed_to_type = p->get_pointed_to_type();
29130  star_or_ref = "&";
29131  }
29132 
29133  if (!pointed_to_type)
29134  return interned_string();
29135 
29136  string result;
29137  if (is_npaf_type(pointed_to_type)
29138  || !(is_function_type(pointed_to_type)
29139  || is_array_type(pointed_to_type)
29140  || is_ptr_to_mbr_type(pointed_to_type)))
29141  {
29142  result = get_type_name(pointed_to_type,
29143  qualified,
29144  internal)
29145  + star_or_ref;
29146 
29147  if (!idname.empty())
29148  result += idname;
29149  }
29150  else
29151  {
29152  // derived type
29153  if (is_function_type(pointed_to_type))
29154  result = add_outer_pointer_to_fn_type_expr(ptr, idname,
29155  qualified, internal);
29156  else if (is_array_type(pointed_to_type))
29157  result = add_outer_pointer_to_array_type_expr(ptr, idname,
29158  qualified, internal);
29159  else if (is_ptr_to_mbr_type(pointed_to_type))
29160  result = add_outer_pointer_to_ptr_to_mbr_type_expr(ptr, idname,
29161  qualified, internal);
29162  else
29164  }
29165  return ptr->get_environment().intern(result);
29166 }
29167 
29168 
29169 /// Emit the name of a pointer declaration.
29170 ///
29171 /// @param the pointer to consider.
29172 ///
29173 /// @param the name of the variable that has @p as a type. If it's
29174 /// empty then the resulting name is going to be the abstract name of
29175 /// the type.
29176 ///
29177 /// @param qualified if true then the type name is going to be
29178 /// fully qualified.
29179 ///
29180 /// @param internal if true then the type name is going to be used for
29181 /// type canonicalization purposes.
29182 static interned_string
29183 pointer_declaration_name(const type_base_sptr& ptr,
29184  const string& variable_name,
29185  bool qualified, bool internal)
29186 {return pointer_declaration_name(ptr.get(), variable_name,
29187  qualified, internal);}
29188 
29189 /// Emit the name of a array declaration.
29190 ///
29191 /// @param the array to consider.
29192 ///
29193 /// @param the name of the variable that has @p as a type. If it's
29194 /// empty then the resulting name is going to be the abstract name of
29195 /// the type.
29196 ///
29197 /// @param qualified if true then the type name is going to be
29198 /// fully qualified.
29199 ///
29200 /// @param internal if true then the type name is going to be used for
29201 /// type canonicalization purposes.
29202 static interned_string
29203 array_declaration_name(const array_type_def* array,
29204  const string& variable_name,
29205  bool qualified, bool internal)
29206 {
29207  if (!array)
29208  return interned_string();
29209 
29210  type_base_sptr e_type = array->get_element_type();
29211  string e_type_repr =
29212  (e_type
29213  ? get_type_name(e_type, qualified, internal)
29214  : string("void"));
29215 
29216  string result;
29217  if (is_ada_language(array->get_language()))
29218  {
29219  std::ostringstream o;
29220  if (!variable_name.empty())
29221  o << variable_name << " is ";
29222  o << "array ("
29223  << array->get_subrange_representation()
29224  << ") of " << e_type_repr;
29225  result = o.str();
29226  }
29227  else
29228  {
29229  if (is_npaf_type(e_type)
29230  || !(is_pointer_to_function_type(e_type)
29231  || is_pointer_to_array_type(e_type)
29233  || is_ptr_to_mbr_type(e_type)))
29234  {
29235  result = e_type_repr;
29236  if (!variable_name.empty())
29237  result += variable_name;
29238  result += array->get_subrange_representation();
29239  }
29240  else if (pointer_type_def_sptr p = is_pointer_type(e_type))
29241  {
29242  string s = variable_name + array->get_subrange_representation();
29243  result = pointer_declaration_name(p, s, qualified, internal);
29244  }
29245  else if (ptr_to_mbr_type_sptr p = is_ptr_to_mbr_type(e_type))
29246  {
29247  string s = variable_name + array->get_subrange_representation();
29248  result = ptr_to_mbr_declaration_name(p, s, qualified, internal);
29249  }
29250  else
29252  }
29253  return array->get_environment().intern(result);
29254 }
29255 
29256 /// Emit the name of a array declaration.
29257 ///
29258 /// @param the array to consider.
29259 ///
29260 /// @param the name of the variable that has @p as a type. If it's
29261 /// empty then the resulting name is going to be the abstract name of
29262 /// the type.
29263 ///
29264 /// @param qualified if true then the type name is going to be
29265 /// fully qualified.
29266 ///
29267 /// @param internal if true then the type name is going to be used for
29268 /// type canonicalization purposes.
29269 static interned_string
29270 array_declaration_name(const array_type_def_sptr& array,
29271  const string& variable_name,
29272  bool qualified, bool internal)
29273 {return array_declaration_name(array.get(), variable_name,
29274  qualified, internal);}
29275 
29276 /// Emit the name of a pointer-to-member declaration.
29277 ///
29278 /// @param ptr the pointer-to-member to consider.
29279 ///
29280 /// @param variable_name the name of the variable that has @p as a
29281 /// type. If it's empty then the resulting name is going to be the
29282 /// abstract name of the type.
29283 ///
29284 /// @param qualified if true then the type name is going to be
29285 /// fully qualified.
29286 ///
29287 /// @param internal if true then the type name is going to be used for
29288 /// type canonicalization purposes.
29289 static interned_string
29290 ptr_to_mbr_declaration_name(const ptr_to_mbr_type* ptr,
29291  const string& variable_name,
29292  bool qualified, bool internal)
29293 {
29294  if (!ptr)
29295  return interned_string();
29296 
29297  string input = variable_name;
29298  string result = add_outer_ptr_to_mbr_type_expr(ptr, input,
29299  qualified, internal);
29300  return ptr->get_environment().intern(result);
29301 }
29302 
29303 /// Emit the name of a pointer-to-member declaration.
29304 ///
29305 /// @param ptr the pointer-to-member to consider.
29306 ///
29307 /// @param variable_name the name of the variable that has @p as a
29308 /// type. If it's empty then the resulting name is going to be the
29309 /// abstract name of the type.
29310 ///
29311 /// @param qualified if true then the type name is going to be
29312 /// fully qualified.
29313 ///
29314 /// @param internal if true then the type name is going to be used for
29315 /// type canonicalization purposes.
29316 static interned_string
29317 ptr_to_mbr_declaration_name(const ptr_to_mbr_type_sptr& ptr,
29318  const string& variable_name,
29319  bool qualified, bool internal)
29320 {
29321  return ptr_to_mbr_declaration_name(ptr.get(), variable_name,
29322  qualified, internal);
29323 }
29324 
29325 bool
29327 {return true;}
29328 
29329 // <ir_node_visitor stuff>
29330 
29331 /// The private data structure of the ir_node_visitor type.
29332 struct ir_node_visitor::priv
29333 {
29334  pointer_set visited_ir_nodes;
29336 
29337  priv()
29339  {}
29340 }; // end struct ir_node_visitory::priv
29341 
29342 /// Default Constructor of the ir_node_visitor type.
29344  : priv_(new priv)
29345 {}
29346 
29347 ir_node_visitor::~ir_node_visitor() = default;
29348 
29349 /// Set if the walker using this visitor is allowed to re-visit a type
29350 /// node that was previously visited or not.
29351 ///
29352 /// @param f if true, then the walker using this visitor is allowed to
29353 /// re-visit a type node that was previously visited.
29354 void
29356 {priv_->allow_visiting_already_visited_type_node = f;}
29357 
29358 /// Get if the walker using this visitor is allowed to re-visit a type
29359 /// node that was previously visited or not.
29360 ///
29361 /// @return true iff the walker using this visitor is allowed to
29362 /// re-visit a type node that was previously visited.
29363 bool
29365 {return priv_->allow_visiting_already_visited_type_node;}
29366 
29367 /// Mark a given type node as having been visited.
29368 ///
29369 /// Note that for this function to work, the type node must have been
29370 /// canonicalized. Otherwise the process is aborted.
29371 ///
29372 /// @param p the type to mark as having been visited.
29373 void
29375 {
29377  return;
29378 
29379  if (p == 0 || type_node_has_been_visited(p))
29380  return;
29381 
29382  type_base* canonical_type = p->get_naked_canonical_type();
29383  ABG_ASSERT(canonical_type);
29384 
29385  size_t canonical_ptr_value = reinterpret_cast<size_t>(canonical_type);
29386  priv_->visited_ir_nodes.insert(canonical_ptr_value);
29387 }
29388 
29389 /// Un-mark all visited type nodes.
29390 ///
29391 /// That is, no type node is going to be considered as having been
29392 /// visited anymore.
29393 ///
29394 /// In other words, after invoking this funciton,
29395 /// ir_node_visitor::type_node_has_been_visited() is going to return
29396 /// false on all type nodes.
29397 void
29399 {priv_->visited_ir_nodes.clear();}
29400 
29401 /// Test if a given type node has been marked as visited.
29402 ///
29403 /// @param p the type node to consider.
29404 ///
29405 /// @return true iff the type node @p p has been marked as visited by
29406 /// the function ir_node_visitor::mark_type_node_as_visited.
29407 bool
29409 {
29411  return false;
29412 
29413  if (p == 0)
29414  return false;
29415 
29416  type_base *canonical_type = p->get_naked_canonical_type();
29417  ABG_ASSERT(canonical_type);
29418 
29419  size_t ptr_value = reinterpret_cast<size_t>(canonical_type);
29420  pointer_set::iterator it = priv_->visited_ir_nodes.find(ptr_value);
29421  if (it == priv_->visited_ir_nodes.end())
29422  return false;
29423 
29424  return true;
29425 }
29426 
29427 bool
29428 ir_node_visitor::visit_begin(decl_base*)
29429 {return true;}
29430 
29431 bool
29432 ir_node_visitor::visit_end(decl_base*)
29433 {return true;}
29434 
29435 bool
29436 ir_node_visitor::visit_begin(scope_decl*)
29437 {return true;}
29438 
29439 bool
29440 ir_node_visitor::visit_end(scope_decl*)
29441 {return true;}
29442 
29443 bool
29444 ir_node_visitor::visit_begin(type_base*)
29445 {return true;}
29446 
29447 bool
29448 ir_node_visitor::visit_end(type_base*)
29449 {return true;}
29450 
29451 bool
29452 ir_node_visitor::visit_begin(scope_type_decl* t)
29453 {return visit_begin(static_cast<type_base*>(t));}
29454 
29455 bool
29456 ir_node_visitor::visit_end(scope_type_decl* t)
29457 {return visit_end(static_cast<type_base*>(t));}
29458 
29459 bool
29460 ir_node_visitor::visit_begin(type_decl* t)
29461 {return visit_begin(static_cast<type_base*>(t));}
29462 
29463 bool
29464 ir_node_visitor::visit_end(type_decl* t)
29465 {return visit_end(static_cast<type_base*>(t));}
29466 
29467 bool
29468 ir_node_visitor::visit_begin(namespace_decl* d)
29469 {return visit_begin(static_cast<decl_base*>(d));}
29470 
29471 bool
29472 ir_node_visitor::visit_end(namespace_decl* d)
29473 {return visit_end(static_cast<decl_base*>(d));}
29474 
29475 bool
29476 ir_node_visitor::visit_begin(qualified_type_def* t)
29477 {return visit_begin(static_cast<type_base*>(t));}
29478 
29479 bool
29480 ir_node_visitor::visit_end(qualified_type_def* t)
29481 {return visit_end(static_cast<type_base*>(t));}
29482 
29483 bool
29484 ir_node_visitor::visit_begin(pointer_type_def* t)
29485 {return visit_begin(static_cast<type_base*>(t));}
29486 
29487 bool
29488 ir_node_visitor::visit_end(pointer_type_def* t)
29489 {return visit_end(static_cast<type_base*>(t));}
29490 
29491 bool
29492 ir_node_visitor::visit_begin(reference_type_def* t)
29493 {return visit_begin(static_cast<type_base*>(t));}
29494 
29495 bool
29496 ir_node_visitor::visit_end(reference_type_def* t)
29497 {return visit_end(static_cast<type_base*>(t));}
29498 
29499 bool
29500 ir_node_visitor::visit_begin(ptr_to_mbr_type* t)
29501 {return visit_begin(static_cast<type_base*>(t));}
29502 
29503 bool
29504 ir_node_visitor::visit_end(ptr_to_mbr_type* t)
29505 {return visit_end(static_cast<type_base*>(t));}
29506 
29507 bool
29508 ir_node_visitor::visit_begin(array_type_def* t)
29509 {return visit_begin(static_cast<type_base*>(t));}
29510 
29511 bool
29512 ir_node_visitor::visit_end(array_type_def* t)
29513 {return visit_end(static_cast<type_base*>(t));}
29514 
29515 bool
29516 ir_node_visitor::visit_begin(array_type_def::subrange_type* t)
29517 {return visit_begin(static_cast<type_base*>(t));}
29518 
29519 bool
29520 ir_node_visitor::visit_end(array_type_def::subrange_type* t)
29521 {return visit_end(static_cast<type_base*>(t));}
29522 
29523 bool
29524 ir_node_visitor::visit_begin(enum_type_decl* t)
29525 {return visit_begin(static_cast<type_base*>(t));}
29526 
29527 bool
29528 ir_node_visitor::visit_end(enum_type_decl* t)
29529 {return visit_end(static_cast<type_base*>(t));}
29530 
29531 bool
29532 ir_node_visitor::visit_begin(typedef_decl* t)
29533 {return visit_begin(static_cast<type_base*>(t));}
29534 
29535 bool
29536 ir_node_visitor::visit_end(typedef_decl* t)
29537 {return visit_end(static_cast<type_base*>(t));}
29538 
29539 bool
29540 ir_node_visitor::visit_begin(function_type* t)
29541 {return visit_begin(static_cast<type_base*>(t));}
29542 
29543 bool
29544 ir_node_visitor::visit_end(function_type* t)
29545 {return visit_end(static_cast<type_base*>(t));}
29546 
29547 bool
29548 ir_node_visitor::visit_begin(var_decl* d)
29549 {return visit_begin(static_cast<decl_base*>(d));}
29550 
29551 bool
29552 ir_node_visitor::visit_end(var_decl* d)
29553 {return visit_end(static_cast<decl_base*>(d));}
29554 
29555 bool
29556 ir_node_visitor::visit_begin(function_decl* d)
29557 {return visit_begin(static_cast<decl_base*>(d));}
29558 
29559 bool
29560 ir_node_visitor::visit_end(function_decl* d)
29561 {return visit_end(static_cast<decl_base*>(d));}
29562 
29563 bool
29564 ir_node_visitor::visit_begin(function_decl::parameter* d)
29565 {return visit_begin(static_cast<decl_base*>(d));}
29566 
29567 bool
29568 ir_node_visitor::visit_end(function_decl::parameter* d)
29569 {return visit_end(static_cast<decl_base*>(d));}
29570 
29571 bool
29572 ir_node_visitor::visit_begin(function_tdecl* d)
29573 {return visit_begin(static_cast<decl_base*>(d));}
29574 
29575 bool
29576 ir_node_visitor::visit_end(function_tdecl* d)
29577 {return visit_end(static_cast<decl_base*>(d));}
29578 
29579 bool
29580 ir_node_visitor::visit_begin(class_tdecl* d)
29581 {return visit_begin(static_cast<decl_base*>(d));}
29582 
29583 bool
29584 ir_node_visitor::visit_end(class_tdecl* d)
29585 {return visit_end(static_cast<decl_base*>(d));}
29586 
29587 bool
29588 ir_node_visitor::visit_begin(class_or_union* t)
29589 {return visit_begin(static_cast<type_base*>(t));}
29590 
29591 bool
29592 ir_node_visitor::visit_end(class_or_union* t)
29593 {return visit_end(static_cast<type_base*>(t));}
29594 
29595 bool
29596 ir_node_visitor::visit_begin(class_decl* t)
29597 {return visit_begin(static_cast<type_base*>(t));}
29598 
29599 bool
29600 ir_node_visitor::visit_end(class_decl* t)
29601 {return visit_end(static_cast<type_base*>(t));}
29602 
29603 bool
29604 ir_node_visitor::visit_begin(union_decl* t)
29605 {return visit_begin(static_cast<type_base*>(t));}
29606 
29607 bool
29608 ir_node_visitor::visit_end(union_decl* t)
29609 {return visit_end(static_cast<type_base*>(t));}
29610 
29611 bool
29612 ir_node_visitor::visit_begin(class_decl::base_spec* d)
29613 {return visit_begin(static_cast<decl_base*>(d));}
29614 
29615 bool
29616 ir_node_visitor::visit_end(class_decl::base_spec* d)
29617 {return visit_end(static_cast<decl_base*>(d));}
29618 
29619 bool
29620 ir_node_visitor::visit_begin(member_function_template* d)
29621 {return visit_begin(static_cast<decl_base*>(d));}
29622 
29623 bool
29624 ir_node_visitor::visit_end(member_function_template* d)
29625 {return visit_end(static_cast<decl_base*>(d));}
29626 
29627 bool
29628 ir_node_visitor::visit_begin(member_class_template* d)
29629 {return visit_begin(static_cast<decl_base*>(d));}
29630 
29631 bool
29632 ir_node_visitor::visit_end(member_class_template* d)
29633 {return visit_end(static_cast<decl_base*>(d));}
29634 
29635 // </ir_node_visitor stuff>
29636 
29637 // <debugging facilities>
29638 
29639 /// Generate a different string at each invocation.
29640 ///
29641 /// @return the resulting string.
29642 static string
29643 get_next_string()
29644 {
29645  static __thread size_t counter;
29646  ++counter;
29647  std::ostringstream o;
29648  o << counter;
29649  return o.str();
29650 }
29651 
29652 /// Convenience typedef for a hash map of pointer to function_decl and
29653 /// string.
29654 typedef unordered_map<const function_decl*, string,
29655  function_decl::hash,
29657 
29658 /// Return a string associated to a given function. Two functions
29659 /// that compare equal would yield the same string, as far as this
29660 /// routine is concerned. And two functions that are different would
29661 /// yield different strings.
29662 ///
29663 /// This is used to debug core diffing issues on functions. The
29664 /// sequence of strings can be given to the 'testdiff2' program that
29665 /// is in the tests/ directory of the source tree, to reproduce core
29666 /// diffing issues on string and thus ease the debugging.
29667 ///
29668 /// @param fn the function to generate a string for.
29669 ///
29670 /// @param m the function_decl* <-> string map to be used by this
29671 /// function to generate strings associated to a function.
29672 ///
29673 /// @return the resulting string.
29674 static const string&
29675 fn_to_str(const function_decl* fn,
29677 {
29678  fns_to_str_map_type::const_iterator i = m.find(fn);
29679  if (i != m.end())
29680  return i->second;
29681  string s = get_next_string();
29682  return m[fn]= s;
29683 }
29684 
29685 /// Generate a sequence of string that matches a given sequence of
29686 /// function. In the resulting sequence, each function is "uniquely
29687 /// representated" by a string. For instance, if the same function "foo"
29688 /// appears at indexes 1 and 3, then the same string 'schmurf' (okay,
29689 /// we don't care about the actual string) would appear at index 1 and 3.
29690 ///
29691 /// @param begin the beginning of the sequence of functions to consider.
29692 ///
29693 /// @param end the end of the sequence of functions. This points to
29694 /// one-passed-the-end of the actual sequence.
29695 ///
29696 /// @param m the function_decl* <-> string map to be used by this
29697 /// function to generate strings associated to a function.
29698 ///
29699 /// @param o the output stream where to emit the generated list of
29700 /// strings to.
29701 static void
29702 fns_to_str(vector<function_decl*>::const_iterator begin,
29703  vector<function_decl*>::const_iterator end,
29705  std::ostream& o)
29706 {
29707  vector<function_decl*>::const_iterator i;
29708  for (i = begin; i != end; ++i)
29709  o << "'" << fn_to_str(*i, m) << "' ";
29710 }
29711 
29712 /// For each sequence of functions given in argument, generate a
29713 /// sequence of string that matches a given sequence of function. In
29714 /// the resulting sequence, each function is "uniquely representated"
29715 /// by a string. For instance, if the same function "foo" appears at
29716 /// indexes 1 and 3, then the same string 'schmurf' (okay, we don't
29717 /// care about the actual string) would appear at index 1 and 3.
29718 ///
29719 /// @param a_begin the beginning of the sequence of functions to consider.
29720 ///
29721 /// @param a_end the end of the sequence of functions. This points to
29722 /// one-passed-the-end of the actual sequence.
29723 ///
29724 /// @param b_begin the beginning of the second sequence of functions
29725 /// to consider.
29726 ///
29727 /// @param b_end the end of the second sequence of functions.
29728 ///
29729 /// @param m the function_decl* <-> string map to be used by this
29730 /// function to generate strings associated to a function.
29731 ///
29732 /// @param o the output stream where to emit the generated list of
29733 /// strings to.
29734 static void
29735 fns_to_str(vector<function_decl*>::const_iterator a_begin,
29736  vector<function_decl*>::const_iterator a_end,
29737  vector<function_decl*>::const_iterator b_begin,
29738  vector<function_decl*>::const_iterator b_end,
29740  std::ostream& o)
29741 {
29742  fns_to_str(a_begin, a_end, m, o);
29743  o << "->|<- ";
29744  fns_to_str(b_begin, b_end, m, o);
29745  o << "\n";
29746 }
29747 
29748 /// For each sequence of functions given in argument, generate a
29749 /// sequence of string that matches a given sequence of function. In
29750 /// the resulting sequence, each function is "uniquely representated"
29751 /// by a string. For instance, if the same function "foo" appears at
29752 /// indexes 1 and 3, then the same string 'schmurf' (okay, we don't
29753 /// care about the actual string) would appear at index 1 and 3.
29754 ///
29755 /// @param a_begin the beginning of the sequence of functions to consider.
29756 ///
29757 /// @param a_end the end of the sequence of functions. This points to
29758 /// one-passed-the-end of the actual sequence.
29759 ///
29760 /// @param b_begin the beginning of the second sequence of functions
29761 /// to consider.
29762 ///
29763 /// @param b_end the end of the second sequence of functions.
29764 ///
29765 /// @param o the output stream where to emit the generated list of
29766 /// strings to.
29767 void
29768 fns_to_str(vector<function_decl*>::const_iterator a_begin,
29769  vector<function_decl*>::const_iterator a_end,
29770  vector<function_decl*>::const_iterator b_begin,
29771  vector<function_decl*>::const_iterator b_end,
29772  std::ostream& o)
29773 {
29775  fns_to_str(a_begin, a_end, b_begin, b_end, m, o);
29776 }
29777 
29778 // </debugging facilities>
29779 
29780 // </class template>
29781 
29782 }// end namespace ir
29783 }//end namespace abigail
29784 
29785 namespace
29786 {
29787 
29788 /// Update the qualified parent name, qualified name and scoped name
29789 /// of a tree decl node.
29790 ///
29791 /// @return true if the tree walking should continue, false otherwise.
29792 ///
29793 /// @param d the tree node to take in account.
29794 bool
29795 qualified_name_setter::do_update(abigail::ir::decl_base* d)
29796 {
29797  std::string parent_qualified_name;
29798  abigail::ir::scope_decl* parent = d->get_scope();
29799  if (parent)
29800  d->priv_->qualified_parent_name_ = parent->get_qualified_name();
29801  else
29802  d->priv_->qualified_parent_name_ = abigail::interned_string();
29803 
29804  const abigail::ir::environment& env = d->get_environment();
29805 
29806  if (!d->priv_->qualified_parent_name_.empty())
29807  {
29808  if (d->get_name().empty())
29809  d->priv_->qualified_name_ = abigail::interned_string();
29810  else
29811  {
29812  d->priv_->qualified_name_ =
29813  env.intern(d->priv_->qualified_parent_name_ + "::" + d->get_name());
29814  d->priv_->internal_qualified_name_ = env.intern(d->get_name());
29815  }
29816  }
29817 
29818  if (d->priv_->scoped_name_.empty())
29819  {
29820  if (parent
29821  && !parent->get_is_anonymous()
29822  && !parent->get_name().empty())
29823  d->priv_->scoped_name_ =
29824  env.intern(parent->get_name() + "::" + d->get_name());
29825  else
29826  d->priv_->scoped_name_ =
29827  env.intern(d->get_name());
29828  }
29829 
29830  if (!is_scope_decl(d))
29831  return false;
29832 
29833  return true;
29834 }
29835 
29836 /// This is called when we start visiting a decl node, during the
29837 /// udpate of the qualified name of a given sub-tree.
29838 ///
29839 /// @param d the decl node we are visiting.
29840 ///
29841 /// @return true iff the traversal should keep going.
29842 bool
29843 qualified_name_setter::visit_begin(abigail::ir::decl_base* d)
29844 {return do_update(d);}
29845 
29846 /// This is called when we start visiting a type node, during the
29847 /// udpate of the qualified name of a given sub-tree.
29848 ///
29849 /// @param d the decl node we are visiting.
29850 ///
29851 /// @return true iff the traversal should keep going.
29852 bool
29853 qualified_name_setter::visit_begin(abigail::ir::type_base* t)
29854 {
29856  return do_update(d);
29857  return false;
29858 }
29859 }// end anonymous namespace.
This header declares filters for the diff trees resulting from comparing ABI Corpora.
The private data and functions of the abigail::ir::corpus type.
#define ABG_RETURN_FALSE
A macro used to return the "false" boolean from DIE comparison routines.
#define ABG_RETURN(value)
A macro used to return from DIE comparison routines.
#define ABG_ASSERT(cond)
This is a wrapper around the 'assert' glibc call. It allows for its argument to have side effects,...
Definition: abg-fwd.h:1714
Declaration of types pertaining to the interned string pool used throughout Libabigail,...
This contains the private implementation of the suppression engine of libabigail.
#define CACHE_COMPARISON_RESULT_AND_RETURN(value)
Cache the result of a comparison between too artifacts (l & r) and return immediately.
Definition: abg-ir.cc:1202
#define RETURN_TRUE_IF_COMPARISON_CYCLE_DETECTED(l, r)
This macro is to be used while comparing composite types that might recursively refer to themselves....
Definition: abg-ir.cc:994
Types of the main internal representation of libabigail.
Wrappers around regex types and functions.
#define ABG_ASSERT_NOT_REACHED
A macro that expands to aborting the program when executed.
This type abstracts the configuration information of the library.
Definition: abg-config.h:18
bool has_string(const char *s) const
Test if the interned string pool already contains a string with a given value.
Definition: abg-ir.cc:98
const char * get_string(const char *s) const
Get a pointer to the interned string which has a given value.
Definition: abg-ir.cc:108
interned_string create_string(const std::string &)
Create an interned string with a given value.
Definition: abg-ir.cc:125
interned_string_pool()
Default constructor.
Definition: abg-ir.cc:85
~interned_string_pool()
Destructor.
Definition: abg-ir.cc:134
The abstraction of an interned string.
bool empty() const
Test if the current instance of interned_string is empty.
This class is to hold the value of the bound of a subrange. The value can be either signed or unsigne...
Definition: abg-ir.h:2560
void set_signed(int64_t v)
Setter of the bound value as signed.
Definition: abg-ir.cc:18796
void set_signedness(enum signedness s)
Setter of the signedness (unsigned VS signed) of the bound value.
Definition: abg-ir.cc:18764
enum signedness get_signedness() const
Getter of the signedness (unsigned VS signed) of the bound value.
Definition: abg-ir.cc:18757
int64_t get_signed_value() const
Getter of the bound value as a signed value.
Definition: abg-ir.cc:18771
bool operator==(const bound_value &) const
Equality operator of the bound value.
Definition: abg-ir.cc:18808
uint64_t get_unsigned_value()
Getter of the bound value as an unsigned value.
Definition: abg-ir.cc:18779
bound_value()
Default constructor of the array_type_def::subrange_type::bound_value class.
Definition: abg-ir.cc:18729
void set_unsigned(uint64_t v)
Setter of the bound value as unsigned.
Definition: abg-ir.cc:18786
Abstraction for an array range type, like in Ada, or just for an array dimension like in C or C++.
Definition: abg-ir.h:2545
void set_lower_bound(int64_t lb)
Setter of the lower bound.
Definition: abg-ir.cc:18972
bool is_non_finite() const
Test if the length of the subrange type is infinite.
Definition: abg-ir.cc:18999
void set_upper_bound(int64_t ub)
Setter of the upper bound of the subrange type.
Definition: abg-ir.cc:18965
void set_underlying_type(const type_base_sptr &)
Setter of the underlying type of the subrange, that is, the type that defines the range.
Definition: abg-ir.cc:18941
string as_string() const
Return a string representation of the sub range.
Definition: abg-ir.cc:19021
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:19210
bool operator!=(const decl_base &o) const
Equality operator.
Definition: abg-ir.cc:19149
int64_t get_upper_bound() const
Getter of the upper bound of the subrange type.
Definition: abg-ir.cc:18951
type_base_sptr get_underlying_type() const
Getter of the underlying type of the subrange, that is, the type that defines the range.
Definition: abg-ir.cc:18933
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:19105
int64_t get_lower_bound() const
Getter of the lower bound of the subrange type.
Definition: abg-ir.cc:18958
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Build a pretty representation for an array_type_def::subrange_type.
Definition: abg-ir.cc:19188
static string vector_as_string(const vector< subrange_sptr > &)
Return a string representation of a vector of subranges.
Definition: abg-ir.cc:19044
uint64_t get_length() const
Getter of the length of the subrange type.
Definition: abg-ir.cc:18982
translation_unit::language get_language() const
Getter of the language that generated this type.
Definition: abg-ir.cc:19014
The abstraction of an array type.
Definition: abg-ir.h:2519
virtual bool is_non_finite() const
Definition: abg-ir.cc:19547
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Build and return the qualified name of the current instance of the array_type_def.
Definition: abg-ir.cc:19577
const type_base_sptr get_element_type() const
Getter of the type of an array element.
Definition: abg-ir.cc:19508
void set_element_type(const type_base_sptr &element_type)
Setter of the type of array element.
Definition: abg-ir.cc:19523
shared_ptr< subrange_type > subrange_sptr
Convenience typedef for a shared pointer on a function_decl::subrange.
Definition: abg-ir.h:2533
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:19640
const std::vector< subrange_sptr > & get_subranges() const
Get the array's subranges.
Definition: abg-ir.cc:19667
virtual bool operator==(const decl_base &) const
Return true iff the two decls have the same name.
Definition: abg-ir.cc:19486
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition: abg-ir.h:2540
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Get the pretty representation of the current instance of array_type_def.
Definition: abg-ir.cc:19356
translation_unit::language get_language() const
Get the language of the array.
Definition: abg-ir.cc:19475
virtual void append_subranges(const std::vector< subrange_sptr > &subs)
Append subranges from the vector.
Definition: abg-ir.cc:19533
Abstraction of a base specifier in a class declaration.
Definition: abg-ir.h:4428
class_decl_sptr get_base_class() const
Get the base class referred to by the current base class specifier.
Definition: abg-ir.cc:24751
bool get_is_virtual() const
Getter of the "is-virtual" proprerty of the base class specifier.
Definition: abg-ir.cc:24758
long get_offset_in_bits() const
Getter of the offset of the base.
Definition: abg-ir.cc:24765
virtual bool traverse(ir_node_visitor &)
Traverses an instance of class_decl::base_spec, visiting all the sub-types and decls that it might co...
Definition: abg-ir.cc:24791
virtual bool operator==(const decl_base &) const
Comparison operator for class_decl::base_spec.
Definition: abg-ir.cc:24885
virtual size_t get_hash() const
Calculate the hash value for a class_decl::base_spec.
Definition: abg-ir.cc:24772
Abstracts a class declaration.
Definition: abg-ir.h:4231
void is_struct(bool f)
Set the "is-struct" flag of the class.
Definition: abg-ir.cc:24562
bool has_virtual_member_functions() const
Test if the current instance of class_decl has virtual member functions.
Definition: abg-ir.cc:25342
const virtual_mem_fn_map_type & get_virtual_mem_fns_map() const
Get the map that associates a virtual table offset to the virtual member functions with that virtual ...
Definition: abg-ir.cc:24631
bool is_struct() const
Test if the class is a struct.
Definition: abg-ir.cc:24569
const base_specs & get_base_specifiers() const
Get the base specifiers for this class.
Definition: abg-ir.cc:24586
virtual ~class_decl()
Destructor of the class_decl type.
Definition: abg-ir.cc:25942
virtual void on_canonical_type_set()
This method is invoked automatically right after the current instance of class_decl has been canonica...
Definition: abg-ir.cc:24547
bool has_vtable() const
Test if the current instance has a vtable.
Definition: abg-ir.cc:25370
ssize_t get_biggest_vtable_offset() const
Get the highest vtable offset of all the virtual methods of the class.
Definition: abg-ir.cc:25384
bool has_virtual_bases() const
Test if the current instance of class_decl has at least one virtual base.
Definition: abg-ir.cc:25351
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:25858
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition: abg-ir.h:4245
void add_base_specifier(shared_ptr< base_spec > b)
Add a base specifier to this class.
Definition: abg-ir.cc:24576
const member_functions & get_virtual_mem_fns() const
Get the virtual member functions of this class.
Definition: abg-ir.cc:24612
void sort_virtual_mem_fns()
Sort the virtual member functions by their virtual index.
Definition: abg-ir.cc:24636
friend bool equals(const class_decl &, const class_decl &, change_kind *)
Compares two instances of class_decl.
Definition: abg-ir.cc:25522
virtual bool operator==(const decl_base &) const
Comparison operator for class_decl.
Definition: abg-ir.cc:25710
bool has_no_base_nor_member() const
Return true iff the class has no entity in its scope.
Definition: abg-ir.cc:25333
virtual size_t get_hash() const
Return the hash value for the current instance.
Definition: abg-ir.cc:25401
class_decl_sptr find_base_class(const string &) const
Find a base class of a given qualified name for the current class.
Definition: abg-ir.cc:24596
vector< base_spec_sptr > base_specs
Convenience typedef.
Definition: abg-ir.h:4250
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Getter of the pretty representation of the current instance of class_decl.
Definition: abg-ir.cc:24657
The base type of class_decl and union_decl.
Definition: abg-ir.h:4029
virtual size_t get_num_anonymous_member_classes() const
Get the number of anonymous member classes contained in this class.
Definition: abg-ir.cc:23405
void add_member_function(method_decl_sptr f, access_specifier a, bool is_static, bool is_ctor, bool is_dtor, bool is_const)
Add a member function.
Definition: abg-ir.cc:23623
const var_decl_sptr find_anonymous_data_member(const var_decl_sptr &) const
Find an anonymous data member in the class.
Definition: abg-ir.cc:23556
const member_functions & get_member_functions() const
Get the member functions of this class_or_union.
Definition: abg-ir.cc:23651
virtual void remove_member_decl(decl_base_sptr)
Remove a given decl from the current class_or_union scope.
Definition: abg-ir.cc:23291
const member_function_templates & get_member_function_templates() const
Get the member function templates of this class.
Definition: abg-ir.cc:23727
virtual size_t get_size_in_bits() const
Getter of the size of the class_or_union type.
Definition: abg-ir.cc:23390
virtual size_t get_num_anonymous_member_unions() const
Get the number of anonymous member unions contained in this class.
Definition: abg-ir.cc:23423
void add_member_function_template(member_function_template_sptr)
Append a member function template to the class_or_union.
Definition: abg-ir.cc:23741
unordered_map< ssize_t, member_functions > virtual_mem_fn_map_type
Convenience typedef.
Definition: abg-ir.h:4061
vector< method_decl_sptr > member_functions
Convenience typedef.
Definition: abg-ir.h:4060
const data_members & get_data_members() const
Get the data members of this class_or_union.
Definition: abg-ir.cc:23515
void add_data_member(var_decl_sptr v, access_specifier a, bool is_laid_out, bool is_static, size_t offset_in_bits)
Add a data member to the current instance of class_or_union.
Definition: abg-ir.cc:23472
const method_decl * find_member_function_from_signature(const string &s) const
Find a method (member function) using its signature (pretty representation) as a key.
Definition: abg-ir.cc:23702
method_decl_sptr find_member_function_sptr(const string &mangled_name)
Find a method, using its linkage name as a key.
Definition: abg-ir.cc:23686
virtual void set_size_in_bits(size_t)
Setter of the size of the class_or_union type.
Definition: abg-ir.cc:23374
decl_base_sptr insert_member_decl(decl_base_sptr member)
Insert a data member to this class_or_union type.
Definition: abg-ir.cc:23783
virtual decl_base_sptr add_member_decl(const decl_base_sptr &)
Add a member declaration to the current instance of class_or_union. The member declaration can be eit...
Definition: abg-ir.cc:23279
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:23197
void add_member_class_template(member_class_template_sptr m)
Append a member class template to the class_or_union.
Definition: abg-ir.cc:23755
const data_members & get_non_static_data_members() const
Get the non-static data memebers of this class_or_union.
Definition: abg-ir.cc:23606
const method_decl * find_member_function(const string &mangled_name) const
Find a method, using its linkage name as a key.
Definition: abg-ir.cc:23660
void maybe_fixup_members_of_anon_data_member(var_decl_sptr &anon_dm)
Fixup the members of the type of an anonymous data member.
Definition: abg-ir.cc:23316
vector< var_decl_sptr > data_members
Convenience typedef.
Definition: abg-ir.h:4059
virtual ~class_or_union()
Destrcutor of the class_or_union type.
Definition: abg-ir.cc:23270
bool has_no_member() const
Definition: abg-ir.cc:23768
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:23817
friend void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:26245
virtual size_t get_alignment_in_bits() const
Getter of the alignment of the class_or_union type.
Definition: abg-ir.cc:23342
const member_class_templates & get_member_class_templates() const
Get the member class templates of this class.
Definition: abg-ir.cc:23734
virtual void set_alignment_in_bits(size_t)
Setter of the alignment of the class type.
Definition: abg-ir.cc:23358
vector< type_base_sptr > member_types
Convenience typedef.
Definition: abg-ir.h:4054
virtual size_t get_num_anonymous_member_enums() const
Get the number of anonymous member enums contained in this class.
Definition: abg-ir.cc:23441
const var_decl_sptr find_data_member(const string &) const
Find a data member of a given name in the current class_or_union.
Definition: abg-ir.cc:23526
Abstract a class template.
Definition: abg-ir.h:3832
shared_ptr< class_decl > get_pattern() const
Getter of the pattern of the template.
Definition: abg-ir.cc:27632
void set_pattern(class_decl_sptr p)
Setter of the pattern of the template.
Definition: abg-ir.cc:27621
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:27681
virtual bool operator==(const decl_base &) const
Return true iff both scopes have the same names and have the same member decls.
Definition: abg-ir.cc:27636
The abstraction of the relationship between an entity and its containing scope (its context)....
Definition: abg-ir.h:1247
This is the abstraction of a set of translation units (themselves seen as bundles of unitary abi arte...
Definition: abg-corpus.h:25
shared_ptr< exported_decls_builder > exported_decls_builder_sptr
Convenience typedef for shared_ptr<exported_decls_builder>.
Definition: abg-corpus.h:42
const translation_units & get_translation_units() const
Return the list of translation units of the current corpus.
Definition: abg-corpus.cc:743
type_maps & get_types()
Get the maps that associate a name to a certain kind of type.
Definition: abg-corpus.cc:775
type_maps & get_type_per_loc_map()
Get the maps that associate a location string to a certain kind of type.
Definition: abg-corpus.cc:884
const corpus_group * get_group() const
Getter of the group this corpus is a member of.
Definition: abg-corpus.cc:892
const environment & get_environment() const
Getter of the enviroment of the corpus.
Definition: abg-corpus.cc:698
The base type of all declarations.
Definition: abg-ir.h:1538
void set_definition_of_declaration(const decl_base_sptr &)
Set the definition of this declaration-only decl_base.
Definition: abg-ir.cc:15982
void set_is_declaration_only(bool f)
Set a flag saying if the enum_type_decl is a declaration-only enum_type_decl.
Definition: abg-ir.cc:5203
virtual bool operator!=(const decl_base &) const
Inequality operator.
Definition: abg-ir.cc:5461
scope_decl * get_scope() const
Return the type containing the current decl, if any.
Definition: abg-ir.cc:5032
void set_qualified_name(const interned_string &) const
Setter for the qualified name.
Definition: abg-ir.cc:4741
void set_is_in_public_symbol_table(bool)
Set the flag saying if this decl is from a symbol that is in a public symbols table,...
Definition: abg-ir.cc:4825
friend bool get_member_is_static(const decl_base &d)
Gets a flag saying if a class member is static or not.
Definition: abg-ir.cc:5816
const decl_base_sptr get_earlier_declaration() const
If this decl_base is a definition, get its earlier declaration.
Definition: abg-ir.cc:5151
virtual void set_linkage_name(const string &m)
Setter for the linkage name.
Definition: abg-ir.cc:5007
const decl_base * get_naked_definition_of_declaration() const
If this decl_base is declaration-only, get its definition, if any.
Definition: abg-ir.cc:5187
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Compute the qualified name of the decl.
Definition: abg-ir.cc:5063
void clear_qualified_name()
Clear the qualified name of this decl.
Definition: abg-ir.cc:4734
void set_name(const string &n)
Setter for the name of the decl.
Definition: abg-ir.cc:4895
const location & get_location() const
Get the location of a given declaration.
Definition: abg-ir.cc:4845
binding
ELF binding.
Definition: abg-ir.h:1589
typedef_decl_sptr get_naming_typedef() const
Getter for the naming typedef of the current decl.
Definition: abg-ir.cc:4955
const interned_string & get_name() const
Getter for the name of the current decl.
Definition: abg-ir.cc:5051
virtual void set_scope(scope_decl *)
Setter of the scope of the current decl.
Definition: abg-ir.cc:5488
const interned_string & peek_qualified_name() const
Getter for the qualified name.
Definition: abg-ir.cc:4725
const context_rel * get_context_rel() const
Getter for the context relationship.
Definition: abg-ir.cc:4775
friend void set_member_function_is_virtual(function_decl &, bool)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6920
bool get_is_anonymous() const
Test if the current declaration is anonymous.
Definition: abg-ir.cc:4908
friend decl_base_sptr add_decl_to_scope(decl_base_sptr decl, scope_decl *scpe)
Appends a declaration to a given scope, if the declaration doesn't already belong to one and if the d...
Definition: abg-ir.cc:8625
virtual const interned_string & get_scoped_name() const
Return the scoped name of the decl.
Definition: abg-ir.cc:5143
const decl_base_sptr get_definition_of_declaration() const
If this decl_base is declaration-only, get its definition, if any.
Definition: abg-ir.cc:5171
friend void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5785
void set_naming_typedef(const typedef_decl_sptr &)
Set the naming typedef of the current instance of decl_base.
Definition: abg-ir.cc:4973
void set_location(const location &l)
Set the location for a given declaration.
Definition: abg-ir.cc:4883
void set_is_anonymous(bool)
Set the "is_anonymous" flag of the current declaration.
Definition: abg-ir.cc:4918
void set_visibility(visibility v)
Setter for the visibility of the decl.
Definition: abg-ir.cc:5024
void set_temporary_qualified_name(const interned_string &) const
Setter for the temporary qualified name of the current declaration.
Definition: abg-ir.cc:4768
visibility get_visibility() const
Getter for the visibility of the decl.
Definition: abg-ir.cc:5017
visibility
ELF visibility.
Definition: abg-ir.h:1579
bool get_is_declaration_only() const
Test if a decl_base is a declaration-only decl.
Definition: abg-ir.cc:5194
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:5477
void set_earlier_declaration(const decl_base_sptr &)
set the earlier declaration of this decl_base definition.
Definition: abg-ir.cc:5159
const interned_string & get_linkage_name() const
Getter for the mangled name.
Definition: abg-ir.cc:5000
friend enum access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5756
friend bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6882
virtual ~decl_base()
Destructor of the decl_base type.
Definition: abg-ir.cc:5465
virtual bool operator==(const decl_base &) const
Return true iff the two decls have the same name.
Definition: abg-ir.cc:5450
const interned_string & get_qualified_parent_name() const
Return a copy of the qualified name of the parent of the current decl.
Definition: abg-ir.cc:5044
bool get_is_anonymous_or_has_anonymous_parent() const
Definition: abg-ir.cc:4941
bool get_has_anonymous_parent() const
Get the "has_anonymous_parent" flag of the current declaration.
Definition: abg-ir.cc:4930
bool get_is_in_public_symbol_table() const
Test if the decl is defined in a ELF symbol table as a public symbol.
Definition: abg-ir.cc:4817
friend bool equals(const decl_base &, const decl_base &, change_kind *)
Compares two instances of decl_base.
Definition: abg-ir.cc:5367
virtual size_t get_hash() const
Get the hash of a decl. If the hash hasn't been computed yet, compute it ans store its value; otherwi...
Definition: abg-ir.cc:4794
const interned_string & peek_temporary_qualified_name() const
Getter of the temporary qualified name of the current declaration.
Definition: abg-ir.cc:4754
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Get the pretty representatin of the current declaration.
Definition: abg-ir.cc:5086
The abstraction for a data member context relationship. This relates a data member to its parent clas...
Definition: abg-ir.h:2954
const var_decl * get_anonymous_data_member() const
Return a non-nil value if this data member context relationship has an anonymous data member....
Definition: abg-ir.cc:3256
void set_anonymous_data_member(var_decl *)
Set the containing anonymous data member of this data member context relationship....
Definition: abg-ir.cc:3266
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1194
version & operator=(const version &o)
Assign a version to the current one.
Definition: abg-ir.cc:3169
bool operator==(const version &o) const
Compares the current version against another one.
Definition: abg-ir.cc:3151
bool is_default() const
Getter for the 'is_default' property of the version.
Definition: abg-ir.cc:3131
const string & str() const
Getter for the version name.
Definition: abg-ir.cc:3117
bool operator!=(const version &o) const
Inequality operator.
Definition: abg-ir.cc:3160
Abstraction of an elf symbol.
Definition: abg-ir.h:923
const abg_compat::optional< std::string > & get_namespace() const
Getter of the 'namespace' property.
Definition: abg-ir.cc:2254
elf_symbol_sptr get_alias_which_equals(const elf_symbol &other) const
In the list of aliases of a given elf symbol, get the alias that equals this current symbol.
Definition: abg-ir.cc:2577
elf_symbol_sptr get_next_common_instance() const
Get the next common instance of the current common symbol.
Definition: abg-ir.cc:2472
type get_type() const
Getter for the type of the current instance of elf_symbol.
Definition: abg-ir.cc:2088
const elf_symbol_sptr get_main_symbol() const
Get the main symbol of an alias chain.
Definition: abg-ir.cc:2309
void set_is_in_ksymtab(bool is_in_ksymtab)
Setter of the 'is-in-ksymtab' property.
Definition: abg-ir.cc:2233
bool has_aliases() const
Check if the current elf_symbol has an alias.
Definition: abg-ir.cc:2338
void set_name(const string &n)
Setter for the name of the current intance of elf_symbol.
Definition: abg-ir.cc:2078
bool is_suppressed() const
Getter for the 'is-suppressed' property.
Definition: abg-ir.cc:2270
binding
The binding of a symbol.
Definition: abg-ir.h:940
int get_number_of_aliases() const
Get the number of aliases to this elf symbol.
Definition: abg-ir.cc:2345
string get_aliases_id_string(const string_elf_symbols_map_type &symtab, bool include_symbol_itself=true) const
Return a comma separated list of the id of the current symbol as well as the id string of its aliases...
Definition: abg-ir.cc:2598
void set_binding(binding b)
Setter for the binding of the current instance of elf_symbol.
Definition: abg-ir.cc:2123
void add_common_instance(const elf_symbol_sptr &)
Add a common instance to the current common elf symbol.
Definition: abg-ir.cc:2483
void add_alias(const elf_symbol_sptr &)
Add an alias to the current elf symbol.
Definition: abg-ir.cc:2362
void set_is_suppressed(bool is_suppressed)
Setter for the 'is-suppressed' property.
Definition: abg-ir.cc:2279
bool is_variable() const
Test if the current instance of elf_symbol is a variable symbol or not.
Definition: abg-ir.cc:2211
elf_symbol_sptr update_main_symbol(const std::string &)
Update the main symbol for a group of aliased symbols.
Definition: abg-ir.cc:2408
void set_size(size_t)
Setter of the size of the symbol.
Definition: abg-ir.cc:2109
const string & get_name() const
Getter for the name of the elf_symbol.
Definition: abg-ir.cc:2071
binding get_binding() const
Getter for the binding of the current instance of elf_symbol.
Definition: abg-ir.cc:2116
static bool get_name_and_version_from_id(const string &id, string &name, string &ver)
Given the ID of a symbol, get the name and the version of said symbol.
Definition: abg-ir.cc:2664
bool is_function() const
Test if the current instance of elf_symbol is a function symbol or not.
Definition: abg-ir.cc:2202
type
The type of a symbol.
Definition: abg-ir.h:927
void set_version(const version &v)
Setter for the version of the current instance of elf_symbol.
Definition: abg-ir.cc:2137
const abg_compat::optional< uint32_t > & get_crc() const
Getter of the 'crc' property.
Definition: abg-ir.cc:2240
void set_visibility(visibility v)
Setter of the visibility of the current instance of elf_symbol.
Definition: abg-ir.cc:2148
bool does_alias(const elf_symbol &) const
Test if the current symbol aliases another one.
Definition: abg-ir.cc:2723
bool is_main_symbol() const
Tests whether this symbol is the main symbol.
Definition: abg-ir.cc:2323
void set_crc(const abg_compat::optional< uint32_t > &crc)
Setter of the 'crc' property.
Definition: abg-ir.cc:2247
static elf_symbol_sptr create(const environment &e, size_t i, size_t s, const string &n, type t, binding b, bool d, bool c, const version &ve, visibility vi, bool is_in_ksymtab=false, const abg_compat::optional< uint32_t > &crc={}, const abg_compat::optional< std::string > &ns={}, bool is_suppressed=false)
Factory of instances of elf_symbol.
Definition: abg-ir.cc:1994
visibility
The visibility of the symbol.
Definition: abg-ir.h:949
version & get_version() const
Getter for the version of the current instanc of elf_symbol.
Definition: abg-ir.cc:2130
bool is_common_symbol() const
Return true if the symbol is a common one.
Definition: abg-ir.cc:2441
void set_index(size_t)
Setter for the index.
Definition: abg-ir.cc:2064
visibility get_visibility() const
Getter of the visibility of the current instance of elf_symbol.
Definition: abg-ir.cc:2156
bool has_other_common_instances() const
Return true if this common common symbol has other common instances.
Definition: abg-ir.cc:2457
size_t get_index() const
Getter for the index.
Definition: abg-ir.cc:2057
const string & get_id_string() const
Get a string that is representative of a given elf_symbol.
Definition: abg-ir.cc:2528
elf_symbol_sptr get_alias_from_name(const string &name) const
From the aliases of the current symbol, lookup one with a given name.
Definition: abg-ir.cc:2555
const environment & get_environment() const
Getter of the environment used by the current instance of elf_symbol.
Definition: abg-ir.cc:2050
void set_type(type t)
Setter for the type of the current instance of elf_symbol.
Definition: abg-ir.cc:2095
bool is_public() const
Test if the current instance of elf_symbol is public or not.
Definition: abg-ir.cc:2186
bool is_in_ksymtab() const
Getter of the 'is-in-ksymtab' property.
Definition: abg-ir.cc:2225
size_t get_size() const
Getter of the size of the symbol.
Definition: abg-ir.cc:2102
bool is_defined() const
Test if the current instance of elf_symbol is defined or not.
Definition: abg-ir.cc:2164
void set_namespace(const abg_compat::optional< std::string > &ns)
Setter of the 'namespace' property.
Definition: abg-ir.cc:2261
elf_symbol_sptr get_next_alias() const
Get the next alias of the current symbol.
Definition: abg-ir.cc:2330
bool operator==(const elf_symbol &) const
Test if two main symbols are textually equal, or, if they have aliases that are textually equal.
Definition: abg-ir.cc:2709
The abstraction of an enumerator.
Definition: abg-ir.h:2835
enumerator()
Default constructor of the enum_type_decl::enumerator type.
Definition: abg-ir.cc:20261
bool operator!=(const enumerator &other) const
Inequality operator.
Definition: abg-ir.cc:20321
void set_name(const string &n)
Setter for the name of enum_type_decl::enumerator.
Definition: abg-ir.cc:20363
enum_type_decl * get_enum_type() const
Getter for the enum type that this enumerator is for.
Definition: abg-ir.cc:20385
const string & get_name() const
Getter for the name of the current instance of enum_type_decl::enumerator.
Definition: abg-ir.cc:20330
void set_enum_type(enum_type_decl *)
Setter for the enum type that this enumerator is for.
Definition: abg-ir.cc:20392
void set_value(int64_t v)
Setter for the value of enum_type_decl::enumerator.
Definition: abg-ir.cc:20378
const string & get_qualified_name(bool internal=false) const
Getter for the qualified name of the current instance of enum_type_decl::enumerator....
Definition: abg-ir.cc:20347
int64_t get_value() const
Getter for the value of enum_type_decl::enumerator.
Definition: abg-ir.cc:20371
bool operator==(const enumerator &other) const
Equality operator.
Definition: abg-ir.cc:20308
enumerator & operator=(const enumerator &)
Assignment operator of the enum_type_decl::enumerator type.
Definition: abg-ir.cc:20292
Abstracts a declaration for an enum type.
Definition: abg-ir.h:2750
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition: abg-ir.h:2763
virtual ~enum_type_decl()
Destructor for the enum type declaration.
Definition: abg-ir.cc:19839
const enumerators & get_enumerators() const
Definition: abg-ir.cc:19739
const enumerators & get_sorted_enumerators() const
Get the lexicographically sorted vector of enumerators.
Definition: abg-ir.cc:19751
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:19817
type_base_sptr get_underlying_type() const
Return the underlying type of the enum.
Definition: abg-ir.cc:19734
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:20182
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Get the pretty representation of the current instance of enum_type_decl.
Definition: abg-ir.cc:19792
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition: abg-ir.h:140
bool decl_only_class_equals_definition() const
Getter of the "decl-only-class-equals-definition" flag.
Definition: abg-ir.cc:3753
bool is_void_pointer_type(const type_base_sptr &) const
Test if a given type is the same as the void pointer type of the environment.
Definition: abg-ir.cc:3820
std::unordered_map< string, std::vector< type_base_sptr > > canonical_types_map_type
A convenience typedef for a map of canonical types. The key is the pretty representation string of a ...
Definition: abg-ir.h:150
bool user_set_analyze_exported_interfaces_only() const
Getter for a property that says if the user actually did set the analyze_exported_interfaces_only() p...
Definition: abg-ir.cc:3900
const type_base_sptr & get_void_type() const
Get the unique type_decl that represents a "void" type for the current environment....
Definition: abg-ir.cc:3634
bool is_variadic_parameter_type(const type_base *) const
Test if a type is a variadic parameter type as defined in the current environment.
Definition: abg-ir.cc:3852
static string & get_variadic_parameter_type_name()
Getter of the name of the variadic parameter type.
Definition: abg-ir.cc:3685
const type_base_sptr & get_void_pointer_type() const
Getter of the "pointer-to-void" IR node that is shared across the ABI corpus. This node must be the o...
Definition: abg-ir.cc:3653
const config & get_config() const
Getter of the general configuration object.
Definition: abg-ir.cc:3890
environment()
Default constructor of the environment type.
Definition: abg-ir.cc:3281
vector< type_base_sptr > * get_canonical_types(const char *name)
Get the vector of canonical types which have a given "string representation".
Definition: abg-ir.cc:4037
bool canonicalization_is_done() const
Test if the canonicalization of types created out of the current environment is done.
Definition: abg-ir.cc:3697
type_base * get_canonical_type(const char *name, unsigned index)
Get a given canonical type which has a given "string representation".
Definition: abg-ir.cc:4060
const type_base_sptr & get_variadic_parameter_type() const
Get a type_decl instance that represents a the type of a variadic function parameter....
Definition: abg-ir.cc:3672
bool is_void_type(const type_base_sptr &) const
Test if a given type is a void type as defined in the current environment.
Definition: abg-ir.cc:3789
virtual ~environment()
Destructor for the environment type.
Definition: abg-ir.cc:3286
interned_string intern(const string &) const
Do intern a string.
Definition: abg-ir.cc:3883
bool do_on_the_fly_canonicalization() const
Getter for the "on-the-fly-canonicalization" flag.
Definition: abg-ir.cc:3720
bool analyze_exported_interfaces_only() const
Getter for the property that controls if we are to restrict the analysis to the types that are only r...
Definition: abg-ir.cc:3926
canonical_types_map_type & get_canonical_types_map()
Getter the map of canonical types.
Definition: abg-ir.cc:3294
Abstraction of a function parameter.
Definition: abg-ir.h:3286
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Compute the qualified name of the parameter.
Definition: abg-ir.cc:23030
interned_string get_type_name() const
Definition: abg-ir.cc:22819
interned_string get_name_id() const
Get a name uniquely identifying the parameter in the function.
Definition: abg-ir.cc:22857
const string get_type_pretty_representation() const
Definition: abg-ir.cc:22838
virtual bool traverse(ir_node_visitor &v)
Traverse the diff sub-tree under the current instance function_decl.
Definition: abg-ir.cc:22995
virtual size_t get_hash() const
Get the hash of a decl. If the hash hasn't been computed yet, compute it ans store its value; otherwi...
Definition: abg-ir.cc:23015
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Compute and return a copy of the pretty representation of the current function parameter.
Definition: abg-ir.cc:23050
Abstraction for a function declaration.
Definition: abg-ir.h:3111
shared_ptr< parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition: abg-ir.h:3131
string get_pretty_representation_of_declarator(bool internal=false) const
Compute and return the pretty representation for the part of the function declaration that starts at ...
Definition: abg-ir.cc:22208
const function_type * get_naked_type() const
Fast getter of the type of the current instance of function_decl.
Definition: abg-ir.cc:22279
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:22681
void append_parameters(std::vector< parameter_sptr > &parms)
Append a vector of parameters to the type of this function.
Definition: abg-ir.cc:22359
bool is_variadic() const
Return true iff the function takes a variable number of parameters.
Definition: abg-ir.cc:22584
parameters::const_iterator get_first_non_implicit_parm() const
Getter for the first non-implicit parameter of a function decl.
Definition: abg-ir.cc:22245
const function_type_sptr get_type() const
Return the type of the current instance of function_decl.
Definition: abg-ir.cc:22264
function_decl(const string &name, function_type_sptr function_type, bool declared_inline, const location &locus, const string &mangled_name, visibility vis, binding bind)
Constructor of the function_decl.
Definition: abg-ir.cc:22071
const type_base_sptr get_return_type() const
Definition: abg-ir.cc:22340
function_decl_sptr clone() const
Create a new instance of function_decl that is a clone of the current one.
Definition: abg-ir.cc:22372
const std::vector< parameter_sptr > & get_parameters() const
Definition: abg-ir.cc:22345
void append_parameter(parameter_sptr parm)
Append a parameter to the type of this function.
Definition: abg-ir.cc:22352
void set_symbol(const elf_symbol_sptr &sym)
This sets the underlying ELF symbol for the current function decl.
Definition: abg-ir.cc:22301
virtual ~function_decl()
Destructor of the function_decl type.
Definition: abg-ir.cc:22697
const elf_symbol_sptr & get_symbol() const
Gets the the underlying ELF symbol for the current variable, that was set using function_decl::set_sy...
Definition: abg-ir.cc:22317
virtual bool operator==(const decl_base &o) const
Comparison operator for function_decl.
Definition: abg-ir.cc:22570
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3138
virtual size_t get_hash() const
The virtual implementation of 'get_hash' for a function_decl.
Definition: abg-ir.cc:22596
bool is_declared_inline() const
Test if the function was declared inline.
Definition: abg-ir.cc:22324
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Get the pretty representation of the current instance of function_decl.
Definition: abg-ir.cc:22140
interned_string get_id() const
Return an ID that tries to uniquely identify the function inside a program or a library.
Definition: abg-ir.cc:22612
Abstract a function template declaration.
Definition: abg-ir.h:3783
binding get_binding() const
Get the binding of the function template.
Definition: abg-ir.cc:27469
void set_pattern(shared_ptr< function_decl > p)
Set a new pattern to the function template.
Definition: abg-ir.cc:27451
shared_ptr< function_decl > get_pattern() const
Get the pattern of the function template.
Definition: abg-ir.cc:27462
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:27529
virtual bool operator==(const decl_base &) const
Comparison operator for the function_tdecl type.
Definition: abg-ir.cc:27478
Abstraction of a function type.
Definition: abg-ir.h:3390
shared_ptr< function_decl::parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition: abg-ir.h:3396
virtual bool traverse(ir_node_visitor &)
Traverses an instance of function_type, visiting all the sub-types and decls that it might contain.
Definition: abg-ir.cc:21783
bool is_variadic() const
Test if the current instance of function_type is for a variadic function.
Definition: abg-ir.cc:21468
parameters::const_iterator get_first_parm() const
Get the first parameter of the function.
Definition: abg-ir.cc:21679
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:21282
virtual bool operator==(const type_base &) const
Equality operator for function_type.
Definition: abg-ir.cc:21742
void append_parameter(parameter_sptr parm)
Append a new parameter to the vector of parameters of the current instance of function_type.
Definition: abg-ir.cc:21453
void set_parameters(const parameters &p)
Setter for the parameters of the current instance of function_type.
Definition: abg-ir.cc:21430
const interned_string & get_cached_name(bool internal=false) const
Get the name of the current function_type.
Definition: abg-ir.cc:21699
const parameter_sptr get_parm_at_index_from_first_non_implicit_parm(size_t) const
Get the Ith parameter of the vector of parameters of the current instance of function_type.
Definition: abg-ir.cc:21409
type_base_sptr get_return_type() const
Getter for the return type of the current instance of function_type.
Definition: abg-ir.cc:21372
void set_return_type(type_base_sptr t)
Setter of the return type of the current instance of function_type.
Definition: abg-ir.cc:21380
parameters::const_iterator get_first_non_implicit_parm() const
Get the first parameter of the function.
Definition: abg-ir.cc:21657
const parameters & get_parameters() const
Getter for the set of parameters of the current intance of function_type.
Definition: abg-ir.cc:21389
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3402
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Return a copy of the pretty representation of the current function_type.
Definition: abg-ir.cc:21766
This abstracts the global scope of a given translation unit.
Definition: abg-ir.h:1952
The internal representation of an integral type.
Definition: abg-ir-priv.h:46
void set_modifiers(modifiers_type)
Setter of the modifiers bitmap of the integral_type.
Definition: abg-ir.cc:16448
string to_string(bool internal=false) const
Return the string representation of the current instance of integral_type.
Definition: abg-ir.cc:16471
base_type get_base_type() const
Getter of the base type of the integral_type.
Definition: abg-ir.cc:16434
modifiers_type
The modifiers of the base types above. Several modifiers can be combined for a given base type....
Definition: abg-ir-priv.h:80
@ LONG_LONG_MODIFIER
The "long long" modifier.
Definition: abg-ir-priv.h:91
@ LONG_MODIFIER
The "long" modifier.
Definition: abg-ir-priv.h:89
@ SIGNED_MODIFIER
The "signed" modifier.
Definition: abg-ir-priv.h:83
@ UNSIGNED_MODIFIER
The "unsigned" modier.
Definition: abg-ir-priv.h:85
@ SHORT_MODIFIER
The "short" modifier.
Definition: abg-ir-priv.h:87
bool operator==(const integral_type &) const
Equality operator for the integral_type.
Definition: abg-ir.cc:16458
base_type
The possible base types of integral types. We might have forgotten many of these, so do not hesitate ...
Definition: abg-ir-priv.h:54
@ WCHAR_T_BASE_TYPE
The "wchar_t" base type.
Definition: abg-ir-priv.h:70
@ CHAR32_T_BASE_TYPE
The "char32_t" base type.
Definition: abg-ir-priv.h:68
@ FLOAT_BASE_TYPE
The "float" base type.
Definition: abg-ir-priv.h:64
@ BOOL_BASE_TYPE
The "bool" base type in C++ or "_Bool" in C11.
Definition: abg-ir-priv.h:60
@ CHAR_BASE_TYPE
The "char" base type.
Definition: abg-ir-priv.h:58
@ CHAR16_T_BASE_TYPE
The "char16_t base type.
Definition: abg-ir-priv.h:66
@ INT_BASE_TYPE
The "int" base type.
Definition: abg-ir-priv.h:56
@ DOUBLE_BASE_TYPE
The "double" base type.
Definition: abg-ir-priv.h:62
modifiers_type get_modifiers() const
Getter of the modifiers bitmap of the integral_type.
Definition: abg-ir.cc:16441
integral_type()
Default constructor of the integral_type.
Definition: abg-ir.cc:16404
The base class for the visitor type hierarchy used for traversing a translation unit.
Definition: abg-ir.h:4955
bool allow_visiting_already_visited_type_node() const
Get if the walker using this visitor is allowed to re-visit a type node that was previously visited o...
Definition: abg-ir.cc:29364
bool type_node_has_been_visited(type_base *) const
Test if a given type node has been marked as visited.
Definition: abg-ir.cc:29408
void forget_visited_type_nodes()
Un-mark all visited type nodes.
Definition: abg-ir.cc:29398
ir_node_visitor()
Default Constructor of the ir_node_visitor type.
Definition: abg-ir.cc:29343
void mark_type_node_as_visited(type_base *)
Mark a given type node as having been visited.
Definition: abg-ir.cc:29374
The entry point to manage locations.
Definition: abg-ir.h:441
location create_new_location(const std::string &fle, size_t lne, size_t col)
Insert the triplet representing a source locus into our internal vector of location triplet....
Definition: abg-ir.cc:518
void expand_location(const location &location, std::string &path, unsigned &line, unsigned &column) const
Given an instance of location type, return the triplet {path,line,column} that represents the source ...
Definition: abg-ir.cc:541
The source location of a token.
Definition: abg-ir.h:299
bool get_is_artificial() const
Test if the location is artificial.
Definition: abg-ir.h:340
unsigned get_value() const
Get the value of the location.
Definition: abg-ir.h:387
string expand(void) const
Expand the location into a string.
Definition: abg-ir.cc:483
void expand(std::string &path, unsigned &line, unsigned &column) const
Expand the current location into a tripplet file path, line and column number.
Definition: abg-ir.cc:463
Abstraction of a member function context relationship. This relates a member function to its parent c...
Definition: abg-ir.h:4556
bool is_constructor() const
Getter for the 'is-constructor' property.
Definition: abg-ir.h:4634
bool is_const() const
Getter for the 'is-const' property.
Definition: abg-ir.h:4669
size_t vtable_offset() const
Getter for the vtable offset property.
Definition: abg-ir.h:4614
bool is_destructor() const
Getter for the 'is-destructor' property.
Definition: abg-ir.h:4651
The base class for member types, data members and member functions. Its purpose is mainly to carry th...
Definition: abg-ir.h:3878
access_specifier get_access_specifier() const
Getter for the access specifier of this member.
Definition: abg-ir.h:3899
bool get_is_static() const
Definition: abg-ir.h:3911
Abstracts a member class template template.
Definition: abg-ir.h:4761
virtual bool operator==(const member_base &o) const
Equality operator of the the member_class_template class.
Definition: abg-ir.cc:26105
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:26190
Abstract a member function template.
Definition: abg-ir.h:4706
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:26084
Abstraction of the declaration of a method.
Definition: abg-ir.h:3927
friend void set_member_function_is_const(function_decl &, bool)
set the const-ness property of a member function.
Definition: abg-ir.cc:6779
virtual void set_linkage_name(const string &)
Set the linkage name of the method.
Definition: abg-ir.cc:25032
const method_type_sptr get_type() const
Definition: abg-ir.cc:25059
Abstracts the type of a class member function.
Definition: abg-ir.h:3486
void set_class_type(const class_or_union_sptr &t)
Sets the class type of the current instance of method_type.
Definition: abg-ir.cc:21970
void set_is_const(bool)
Setter of the "is-const" property of method_type.
Definition: abg-ir.cc:22002
virtual ~method_type()
The destructor of method_type.
Definition: abg-ir.cc:22013
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Return a copy of the pretty representation of the current method_type.
Definition: abg-ir.cc:21994
class_or_union_sptr get_class_type() const
Get the class type this method belongs to.
Definition: abg-ir.cc:21961
bool get_is_const() const
Getter of the "is-const" property of method_type.
Definition: abg-ir.cc:22009
The abstraction of a namespace declaration.
Definition: abg-ir.h:2197
bool is_empty_or_has_empty_sub_namespaces() const
Test if the current namespace_decl is empty or contains empty namespaces itself.
Definition: abg-ir.cc:17049
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:17080
namespace_decl(const environment &env, const string &name, const location &locus, visibility vis=VISIBILITY_DEFAULT)
Constructor.
Definition: abg-ir.cc:16983
virtual bool operator==(const decl_base &) const
Return true iff both namespaces and their members are equal.
Definition: abg-ir.cc:17035
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Build and return a copy of the pretty representation of the namespace.
Definition: abg-ir.cc:17021
Abstracts non type template parameters.
Definition: abg-ir.h:3660
const type_base_sptr get_type() const
Getter for the type of the template parameter.
Definition: abg-ir.cc:27148
virtual bool operator==(const decl_base &) const
Return true iff the two decls have the same name.
Definition: abg-ir.cc:27162
virtual size_t get_hash() const
Get the hash value of the current instance.
Definition: abg-ir.cc:27155
The abstraction of a pointer type.
Definition: abg-ir.h:2337
void set_pointed_to_type(const type_base_sptr &)
Set the pointed-to type of the pointer.
Definition: abg-ir.cc:17714
virtual void get_qualified_name(interned_string &, bool internal=false) const
Build and return the qualified name of the current instance of pointer_type_def.
Definition: abg-ir.cc:17849
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:17645
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:17943
virtual bool operator==(const decl_base &) const
Return true iff both instances of pointer_type_def are equal.
Definition: abg-ir.cc:17785
const type_base_sptr get_pointed_to_type() const
Getter of the pointed-to type.
Definition: abg-ir.cc:17829
type_base * get_naked_pointed_to_type() const
Getter of a naked pointer to the pointed-to type.
Definition: abg-ir.cc:17836
The abstraction of a pointer-to-member type.
Definition: abg-ir.h:2466
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Get the qualified name for the current ptr_to_mbr_type.
Definition: abg-ir.cc:18578
const type_base_sptr & get_containing_type() const
Getter of the type containing the member pointed-to by the current ptr_to_mbr_type.
Definition: abg-ir.cc:18521
bool operator==(const ptr_to_mbr_type &) const
Equality operator for the current ptr_to_mbr_type.
Definition: abg-ir.cc:18562
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function for ptr_to_mbr_type.
Definition: abg-ir.cc:18629
const type_base_sptr & get_member_type() const
Getter of the member type of the current ptr_to_mbr_type.
Definition: abg-ir.cc:18512
virtual ~ptr_to_mbr_type()
Desctructor for ptr_to_mbr_type.
Definition: abg-ir.cc:18654
The abstraction of a qualified type.
Definition: abg-ir.h:2226
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Implementation for the virtual qualified name builder for qualified_type_def.
Definition: abg-ir.cc:17371
void set_underlying_type(const type_base_sptr &)
Setter of the underlying type.
Definition: abg-ir.cc:17496
virtual size_t get_size_in_bits() const
Get the size of the qualified type def.
Definition: abg-ir.cc:17235
string get_cv_quals_string_prefix() const
Compute and return the string prefix or suffix representing the qualifiers hold by the current instan...
Definition: abg-ir.cc:17484
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition: abg-ir.h:2245
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:17175
void set_cv_quals(CV cv_quals)
Setter of the const/value qualifiers bit field.
Definition: abg-ir.cc:17475
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:17444
CV get_cv_quals() const
Getter of the const/volatile qualifier bit field.
Definition: abg-ir.cc:17470
type_base_sptr get_underlying_type() const
Getter of the underlying type.
Definition: abg-ir.cc:17489
virtual bool operator==(const decl_base &) const
Equality operator for qualified types.
Definition: abg-ir.cc:17315
string build_name(bool, bool internal=false) const
Build the name of the current instance of qualified type.
Definition: abg-ir.cc:17152
Abstracts a reference type.
Definition: abg-ir.h:2400
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Build and return the qualified name of the current instance of the reference_type_def.
Definition: abg-ir.cc:18259
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:18037
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:18381
void set_pointed_to_type(type_base_sptr &pointed_to_type)
Setter of the pointed_to type of the current reference type.
Definition: abg-ir.cc:18131
virtual bool operator==(const decl_base &) const
Equality operator of the reference_type_def type.
Definition: abg-ir.cc:18201
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Get the pretty representation of the current instance of reference_type_def.
Definition: abg-ir.cc:18360
A declaration that introduces a scope.
Definition: abg-ir.h:1809
virtual size_t get_num_anonymous_member_classes() const
Getter for the number of anonymous classes contained in this scope.
Definition: abg-ir.cc:8112
void remove_member_type(type_base_sptr t)
Remove a member type from the current class_or_union scope.
Definition: abg-ir.cc:8315
void insert_member_type(type_base_sptr t, declarations::iterator before)
Insert a member type.
Definition: abg-ir.cc:8274
void add_member_type(type_base_sptr t)
Add a member type to the current instance of class_or_union.
Definition: abg-ir.cc:8290
virtual size_t get_num_anonymous_member_unions() const
Getter for the number of anonymous unions contained in this scope.
Definition: abg-ir.cc:8130
scopes & get_member_scopes()
Getter for the scopes carried by the current scope.
Definition: abg-ir.cc:8165
std::vector< scope_decl_sptr > scopes
Convenience typedef for a vector of scope_decl_sptr.
Definition: abg-ir.h:1820
bool is_empty() const
Test if the current scope is empty.
Definition: abg-ir.cc:8179
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:8595
const type_base_sptrs_type & get_member_types() const
Get the member types of this scope_decl.
Definition: abg-ir.cc:8249
decl_base_sptr insert_member_decl(decl_base_sptr member, declarations::iterator before)
Insert a member decl to this scope, right before an element pointed to by a given iterator....
Definition: abg-ir.cc:8361
std::vector< decl_base_sptr > declarations
Convenience typedef for a vector of decl_base_sptr.
Definition: abg-ir.h:1816
const type_base_sptrs_type & get_sorted_canonical_types() const
Return a vector of sorted canonical types of the current scope.
Definition: abg-ir.cc:8048
bool find_iterator_for_member(const decl_base *, declarations::iterator &)
Find a member of the current scope and return an iterator on it.
Definition: abg-ir.cc:8547
virtual void remove_member_decl(decl_base_sptr member)
Remove a declaration from the current scope.
Definition: abg-ir.cc:8386
virtual decl_base_sptr add_member_decl(const decl_base_sptr &member)
Add a member decl to this scope. Note that user code should not use this, but rather use add_decl_to_...
Definition: abg-ir.cc:8223
type_base_sptr find_member_type(const string &name) const
Find a member type of a given name, inside the current scope_decl.
Definition: abg-ir.cc:8260
const declarations & get_member_decls() const
Getter for the member declarations carried by the current scope_decl.
Definition: abg-ir.cc:8072
const canonical_type_sptr_set_type & get_canonical_types() const
@eturn the set of canonical types of the the current scope.
Definition: abg-ir.cc:8036
const type_base_sptrs_type & get_sorted_member_types() const
Get the sorted member types of this scope_decl.
Definition: abg-ir.cc:8334
friend decl_base_sptr add_decl_to_scope(decl_base_sptr decl, scope_decl *scope)
Appends a declaration to a given scope, if the declaration doesn't already belong to one and if the d...
Definition: abg-ir.cc:8625
virtual bool operator==(const decl_base &) const
Return true iff both scopes have the same names and have the same member decls.
Definition: abg-ir.cc:8501
const declarations & get_sorted_member_decls() const
Getter for the sorted member declarations carried by the current scope_decl.
Definition: abg-ir.cc:8090
virtual size_t get_num_anonymous_member_enums() const
Getter for the number of anonymous enums contained in this scope.
Definition: abg-ir.cc:8148
virtual size_t get_hash() const
Return the hash value for the current instance of scope_decl.
Definition: abg-ir.cc:8426
A type that introduces a scope.
Definition: abg-ir.h:2171
virtual bool traverse(ir_node_visitor &)
Traverses an instance of scope_type_decl, visiting all the sub-types and decls that it might contain.
Definition: abg-ir.cc:16942
virtual bool operator==(const decl_base &) const
Equality operator between two scope_type_decl.
Definition: abg-ir.cc:16904
The base class of templates.
Definition: abg-ir.h:3542
const std::list< template_parameter_sptr > & get_template_parameters() const
Get the list of template parameters of the current instance of template_decl.
Definition: abg-ir.cc:26816
virtual ~template_decl()
Destructor.
Definition: abg-ir.cc:26841
void add_template_parameter(const template_parameter_sptr p)
Add a new template parameter to the current instance of template_decl.
Definition: abg-ir.cc:26808
virtual bool operator==(const decl_base &o) const
Equality operator.
Definition: abg-ir.cc:26850
Base class for a template parameter. Client code should use the more specialized type_template_parame...
Definition: abg-ir.h:3577
virtual ~template_parameter()
Destructor.
Definition: abg-ir.cc:26977
bool operator!=(const template_parameter &) const
Inequality operator.
Definition: abg-ir.cc:26973
Abstracts a template template parameter.
Definition: abg-ir.h:3707
virtual bool operator==(const type_base &) const
Equality operator.
Definition: abg-ir.cc:27235
This is the abstraction of the set of relevant artefacts (types, variable declarations,...
Definition: abg-ir.h:686
void set_address_size(char)
Setter of the address size in this translation unit.
Definition: abg-ir.cc:1483
const std::string & get_absolute_path() const
Get the concatenation of the build directory and the relative path of the translation unit.
Definition: abg-ir.cc:1398
void set_is_constructed(bool)
Setter of the 'is_constructed" flag. It says if the translation unit is fully constructed or not.
Definition: abg-ir.cc:1515
bool operator==(const translation_unit &) const
Compare the current translation unit against another one.
Definition: abg-ir.cc:1525
const corpus * get_corpus() const
Get the corpus this translation unit is a member of.
Definition: abg-ir.cc:1441
char get_address_size() const
Getter of the address size in this translation unit.
Definition: abg-ir.cc:1476
const std::string & get_compilation_dir_path() const
Get the path of the directory that was 'current' when the translation unit was compiled.
Definition: abg-ir.cc:1379
void set_corpus(corpus *)
Set the corpus this translation unit is a member of.
Definition: abg-ir.cc:1425
void set_language(language l)
Setter of the language of the source code of the translation unit.
Definition: abg-ir.cc:1342
void bind_function_type_life_time(function_type_sptr) const
Ensure that the life time of a function type is bound to the life time of the current translation uni...
Definition: abg-ir.cc:1551
const scope_decl_sptr & get_global_scope() const
Getter of the the global scope of the translation unit.
Definition: abg-ir.cc:1278
bool is_empty() const
Tests whether if the current translation unit contains ABI artifacts or not.
Definition: abg-ir.cc:1465
bool is_constructed() const
Getter of the 'is_constructed" flag. It says if the translation unit is fully constructed or not.
Definition: abg-ir.cc:1499
const std::string & get_path() const
Get the path of the current translation unit.
Definition: abg-ir.cc:1355
void set_compilation_dir_path(const std::string &)
Set the path of the directory that was 'current' when the translation unit was compiled.
Definition: abg-ir.cc:1390
location_manager & get_loc_mgr()
Getter of the location manager for the current translation unit.
Definition: abg-ir.cc:1449
void set_path(const string &)
Set the path associated to the current instance of translation_unit.
Definition: abg-ir.cc:1366
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse virtual function.
Definition: abg-ir.cc:1585
language
The language of the translation unit.
Definition: abg-ir.h:699
bool operator!=(const translation_unit &) const
Inequality operator.
Definition: abg-ir.cc:1540
const vector< function_type_sptr > & get_live_fn_types() const
Get the vector of function types that are used in the current translation unit.
Definition: abg-ir.cc:1321
const environment & get_environment() const
Getter of the environment of the current translation_unit.
Definition: abg-ir.cc:1328
const type_maps & get_types() const
Getter of the types of the current translation_unit.
Definition: abg-ir.cc:1305
language get_language() const
Getter of the language of the source code of the translation unit.
Definition: abg-ir.cc:1335
bool visiting() const
This should returns false before and after the node has been visiting. During the visiting of the nod...
Definition: abg-traverse.cc:48
An abstraction helper for type declarations.
Definition: abg-ir.h:1973
const interned_string & get_cached_pretty_representation(bool internal=false) const
Get the pretty representation of the current type.
Definition: abg-ir.cc:16044
type_base * get_naked_canonical_type() const
Getter of the canonical type pointer.
Definition: abg-ir.cc:16027
virtual size_t get_size_in_bits() const
Getter for the size of the type.
Definition: abg-ir.cc:16123
virtual bool traverse(ir_node_visitor &)
Default implementation of traversal for types. This function does nothing. It must be implemented by ...
Definition: abg-ir.cc:16149
virtual void on_canonical_type_set()
This method is invoked automatically right after the current instance of class_decl has been canonica...
Definition: abg-ir.cc:15754
virtual void set_size_in_bits(size_t)
Setter for the size of the type.
Definition: abg-ir.cc:16116
virtual bool operator!=(const type_base &) const
Inequality operator.
Definition: abg-ir.cc:16109
virtual bool operator==(const type_base &) const
Return true iff both type declarations are equal.
Definition: abg-ir.cc:16099
virtual size_t get_alignment_in_bits() const
Getter for the alignment of the type.
Definition: abg-ir.cc:16137
virtual void set_alignment_in_bits(size_t)
Setter for the alignment of the type.
Definition: abg-ir.cc:16130
type_base_sptr get_canonical_type() const
Getter of the canonical type of the current instance of type_base.
Definition: abg-ir.cc:16011
This abstracts a composition of types based on template type parameters. The result of the compositio...
Definition: abg-ir.h:3745
const type_base_sptr get_composed_type() const
Getter for the resulting composed type.
Definition: abg-ir.cc:27341
void set_composed_type(type_base_sptr t)
Setter for the resulting composed type.
Definition: abg-ir.cc:27348
virtual size_t get_hash() const
Get the hash value for the current instance.
Definition: abg-ir.cc:27355
A basic type declaration that introduces no scope.
Definition: abg-ir.h:2108
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Implementation for the virtual qualified name builder for type_decl.
Definition: abg-ir.cc:16736
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:16815
virtual bool operator!=(const type_base &) const
Return true if both types equals.
Definition: abg-ir.cc:16674
virtual bool operator==(const type_base &) const
Return true if both types equals.
Definition: abg-ir.cc:16630
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Get the pretty representation of the current instance of type_decl.
Definition: abg-ir.cc:16795
This is a type that aggregates maps of all the kinds of types that are supported by libabigail.
Definition: abg-ir.h:593
istring_type_base_wptrs_map_type & typedef_types()
Getter for the map that associates the name of a typedef to the vector of instances of typedef_decl_s...
Definition: abg-ir.cc:662
istring_type_base_wptrs_map_type & function_types()
Getter for the map that associates the name of a function type to the vector of instances of function...
Definition: abg-ir.cc:765
istring_type_base_wptrs_map_type & reference_types()
Getter for the map that associates the name of a reference type to the vector of instances of referen...
Definition: abg-ir.cc:716
const istring_type_base_wptrs_map_type & class_types() const
Getter for the map that associates the name of a class type to the vector of instances of class_decl_...
Definition: abg-ir.cc:620
istring_type_base_wptrs_map_type & union_types()
Getter for the map that associates the name of a union type to the vector of instances of union_decl_...
Definition: abg-ir.cc:634
istring_type_base_wptrs_map_type & array_types()
Getter for the map that associates the name of an array type to the vector of instances of array_type...
Definition: abg-ir.cc:730
istring_type_base_wptrs_map_type & enum_types()
Getter for the map that associates the name of an enum type to the vector of instances of enum_type_d...
Definition: abg-ir.cc:648
bool empty() const
Test if the type_maps is empty.
Definition: abg-ir.cc:588
istring_type_base_wptrs_map_type & qualified_types()
Getter for the map that associates the name of a qualified type to the vector of instances of qualifi...
Definition: abg-ir.cc:675
istring_type_base_wptrs_map_type & ptr_to_mbr_types()
Getter for the map that associates the name of a pointer-to-member type to the vector of instances of...
Definition: abg-ir.cc:695
const vector< type_base_wptr > & get_types_sorted_by_name() const
Getter of all types types sorted by their pretty representation.
Definition: abg-ir.cc:1214
istring_type_base_wptrs_map_type & pointer_types()
Getter for the map that associates the name of a pointer type to the vector of instances of pointer_t...
Definition: abg-ir.cc:688
const istring_type_base_wptrs_map_type & basic_types() const
Getter for the map that associates the name of a basic type to the vector instances of type_decl_sptr...
Definition: abg-ir.cc:606
const istring_type_base_wptrs_map_type & subrange_types() const
Getter for the map that associates the name of a subrange type to the vector of instances of array_ty...
Definition: abg-ir.cc:751
The base class of both types and declarations.
Definition: abg-ir.h:1368
void set_translation_unit(translation_unit *)
Set the translation_unit this ABI artifact belongs to.
Definition: abg-ir.cc:4498
bool get_is_artificial() const
Getter of the flag that says if the artefact is artificial.
Definition: abg-ir.cc:4312
virtual ~type_or_decl_base()
The destructor of the type_or_decl_base type.
Definition: abg-ir.cc:4301
location & get_artificial_location() const
Getter of the artificial location of the artifact.
Definition: abg-ir.cc:4458
bool has_artificial_location() const
Test if the current ABI artifact carries an artificial location.
Definition: abg-ir.cc:4465
friend type_base * is_type(const type_or_decl_base *)
Test whether a declaration is a type.
Definition: abg-ir.cc:10678
const corpus * get_corpus() const
Get the corpus this ABI artifact belongs to.
Definition: abg-ir.cc:4490
friend class_decl * is_class_type(const type_or_decl_base *)
Test whether a type is a class.
Definition: abg-ir.cc:10956
enum type_or_decl_kind kind() const
Getter for the "kind" property of type_or_decl_base type.
Definition: abg-ir.cc:4335
void set_is_artificial(bool)
Setter of the flag that says if the artefact is artificial.
Definition: abg-ir.cc:4324
virtual bool traverse(ir_node_visitor &)
Traverse the the ABI artifact.
Definition: abg-ir.cc:4523
const void * runtime_type_instance() const
Getter of the pointer to the runtime type sub-object of the current instance.
Definition: abg-ir.cc:4355
const void * type_or_decl_base_pointer() const
Getter of the pointer to either the type_base sub-object of the current instance if it's a type,...
Definition: abg-ir.cc:4390
bool hashing_started() const
Getter for the 'hashing_started' property.
Definition: abg-ir.cc:4408
void set_artificial_location(const location &)
Setter of the artificial location of the artificat.
Definition: abg-ir.cc:4440
type_or_decl_kind
This is a bitmap type which instance is meant to contain the runtime type of a given ABI artifact....
Definition: abg-ir.h:1382
const environment & get_environment() const
Getter of the environment of the current ABI artifact.
Definition: abg-ir.cc:4422
friend decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10605
const translation_unit * get_translation_unit() const
Get the translation_unit this ABI artifact belongs to.
Definition: abg-ir.cc:4515
Abstracts a type template parameter.
Definition: abg-ir.h:3623
virtual bool operator==(const type_base &) const
Equality operator.
Definition: abg-ir.cc:27019
The abstraction of a typedef declaration.
Definition: abg-ir.h:2889
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Implementation of the virtual "get_qualified_name" method.
Definition: abg-ir.cc:20638
void set_underlying_type(const type_base_sptr &)
Setter ofthe underlying type of the typedef.
Definition: abg-ir.cc:20623
virtual size_t get_size_in_bits() const
Return the size of the typedef.
Definition: abg-ir.cc:20473
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:20670
type_base_sptr get_underlying_type() const
Getter of the underlying type of the typedef.
Definition: abg-ir.cc:20616
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:20553
virtual size_t get_alignment_in_bits() const
Return the alignment of the typedef.
Definition: abg-ir.cc:20490
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Build a pretty representation for a typedef_decl.
Definition: abg-ir.cc:20594
Abstracts a union type declaration.
Definition: abg-ir.h:4486
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:26610
virtual bool operator==(const decl_base &) const
Comparison operator for union_decl.
Definition: abg-ir.cc:26550
virtual ~union_decl()
Destructor of the union_decl type.
Definition: abg-ir.cc:26683
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Getter of the pretty representation of the current instance of union_decl.
Definition: abg-ir.cc:26517
Abstracts a variable declaration.
Definition: abg-ir.h:3008
binding get_binding() const
Getter of the binding of the variable.
Definition: abg-ir.cc:20784
void set_type(type_base_sptr &)
Setter of the type of the variable.
Definition: abg-ir.cc:20766
void set_binding(binding b)
Setter of the binding of the variable.
Definition: abg-ir.cc:20791
friend uint64_t get_data_member_offset(const var_decl_sptr m)
Get the offset of a data member.
Definition: abg-ir.cc:6440
var_decl_sptr clone() const
Create a new var_decl that is a clone of the current one.
Definition: abg-ir.cc:20829
virtual const interned_string & get_qualified_name(bool internal=false) const
Get the qualified name of a given variable or data member.
Definition: abg-ir.cc:21092
const type_base * get_naked_type() const
Getter of the type of the variable.
Definition: abg-ir.cc:20777
friend bool get_data_member_is_laid_out(const var_decl &m)
Test whether a data member is laid out.
Definition: abg-ir.cc:6585
const type_base_sptr get_type() const
Getter of the type of the variable.
Definition: abg-ir.cc:20759
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:21254
string get_anon_dm_reliable_name(bool qualified=true) const
Get a name that is valid even for an anonymous data member.
Definition: abg-ir.cc:21231
void set_symbol(const elf_symbol_sptr &sym)
Sets the underlying ELF symbol for the current variable.
Definition: abg-ir.cc:20806
const elf_symbol_sptr & get_symbol() const
Gets the the underlying ELF symbol for the current variable, that was set using var_decl::set_symbol(...
Definition: abg-ir.cc:20822
virtual bool operator==(const decl_base &) const
Comparison operator of var_decl.
Definition: abg-ir.cc:21017
virtual size_t get_hash() const
Return the hash value for the current instance.
Definition: abg-ir.cc:21059
virtual string get_pretty_representation(bool internal=false, bool qualified_name=true) const
Build and return the pretty representation of this variable.
Definition: abg-ir.cc:21122
interned_string get_id() const
Return an ID that tries to uniquely identify the variable inside a program or a library.
Definition: abg-ir.cc:21036
A type used to time various part of the libabigail system.
bool stop()
Stop the timer.
bool start()
Start the timer.
bool is_decl_only_class_with_size_change(const class_or_union &first, const class_or_union &second)
Test if two classes that are decl-only (have the decl-only flag and carry no data members) but are di...
const type_base * is_void_pointer_type_equivalent(const type_base &type)
Test if a type is equivalent to a pointer to void type.
Definition: abg-ir.cc:11559
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition: abg-fwd.h:233
bool is_declaration_only_class_or_union_type(const type_base_sptr &t, bool look_through_decl_only)
Test wheter a type is a declaration-only class.
Definition: abg-ir.cc:11153
bool enum_has_non_name_change(const enum_type_decl &l, const enum_type_decl &r, change_kind *k)
Test if two enums differ, but not by a name change.
Definition: abg-ir.cc:19851
const type_base_wptrs_type * lookup_class_types(const string &qualified_name, const corpus &corp)
Look into a given corpus to find the class type*s* that have a given qualified name.
Definition: abg-ir.cc:13642
const type_base_sptr lookup_type_in_scope(const string &fqn, const scope_decl_sptr &skope)
Lookup a type in a scope.
Definition: abg-ir.cc:12740
bool is_non_canonicalized_type(const type_base *t)
Test if a given type is allowed to be non canonicalized.
Definition: abg-ir.cc:27947
bool get_member_function_is_dtor(const function_decl &f)
Test whether a member function is a destructor.
Definition: abg-ir.cc:6695
const type_base * peel_qualified_type(const type_base *type)
Return the leaf underlying type of a qualified type.
Definition: abg-ir.cc:7456
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition: abg-fwd.h:219
translation_unit * get_translation_unit(const shared_ptr< decl_base > decl)
Return the translation unit a declaration belongs to.
Definition: abg-ir.cc:10401
size_t hash_type(const type_base *t)
Hash an ABI artifact that is either a type.
Definition: abg-ir.cc:27924
var_decl_sptr get_last_data_member(const class_or_union &klass)
Get the last data member of a class type.
Definition: abg-ir.cc:6022
bool is_anonymous_or_typedef_named(const decl_base &d)
Test if a given decl is anonymous or has a naming typedef.
Definition: abg-ir.cc:6395
bool is_typedef_of_maybe_qualified_class_or_union_type(const type_base_sptr &t)
Test if a type is a typedef of a class or union type, or a typedef of a qualified class or union type...
Definition: abg-ir.cc:11419
decl_base_sptr add_decl_to_scope(decl_base_sptr decl, const scope_decl_sptr &scope)
Appends a declaration to a given scope, if the declaration doesn't already belong to a scope.
Definition: abg-ir.cc:8642
function_decl_sptr is_function_decl(const type_or_decl_base_sptr &d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10572
class_decl_sptr is_class_type(const type_or_decl_base_sptr &d)
Test whether a type is a class.
Definition: abg-ir.cc:10974
bool get_member_is_static(const decl_base &d)
Gets a flag saying if a class member is static or not.
Definition: abg-ir.cc:5816
pointer_type_def_sptr is_pointer_to_npaf_type(const type_base_sptr &t)
Test if we are looking at a pointer to a neither-a-pointer-to-an-array-nor-a-function type.
Definition: abg-ir.cc:11336
bool debug_equals(const type_or_decl_base *l, const type_or_decl_base *r)
Test if two ABI artifacts are equal.
Definition: abg-ir.cc:10214
decl_base * debug(const decl_base *artifact)
Emit a textual representation of an artifact to std error stream for debugging purposes.
Definition: abg-ir.cc:10197
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition: abg-fwd.h:267
access_specifier
Access specifier for class members.
Definition: abg-ir.h:879
pointer_type_def_sptr lookup_pointer_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find a pointer type which has a given qualified name.
Definition: abg-ir.cc:14076
class_decl_sptr lookup_class_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find a class type which has a given qualified name.
Definition: abg-ir.cc:13557
interned_string get_type_name(const type_base &t, bool qualified, bool internal)
Get the name of a given type and return a copy of it.
Definition: abg-ir.cc:9169
void maybe_update_types_lookup_map(const function_type_sptr &fn_type)
Update the map that associates the fully qualified name of a function type with the type itself.
Definition: abg-ir.cc:14947
bool function_decls_alias(const function_decl &f1, const function_decl &f2)
Test if two function declarations are aliases.
Definition: abg-ir.cc:22663
pointer_type_def_sptr is_pointer_to_array_type(const type_base_sptr &t)
Test if a type is a pointer to array type.
Definition: abg-ir.cc:11318
shared_ptr< class_tdecl > class_tdecl_sptr
Convenience typedef for a shared pointer on a class_tdecl.
Definition: abg-fwd.h:287
weak_ptr< function_type > function_type_wptr
Convenience typedef for a weak pointer on a function_type.
Definition: abg-fwd.h:217
type_base_sptr lookup_class_or_typedef_type(const string &qualified_name, const corpus &corp)
Look into a corpus to find a class, union or typedef type which has a given qualified name.
Definition: abg-ir.cc:13970
void set_member_function_is_virtual(function_decl &f, bool is_virtual)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6920
ssize_t get_member_function_vtable_offset(const function_decl &f)
Get the vtable offset of a member function.
Definition: abg-ir.cc:6819
reference_type_def_sptr lookup_reference_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find a reference type which has a given qualified name.
Definition: abg-ir.cc:14125
type_decl_sptr lookup_basic_type_per_location(const string &loc, const corpus &corp)
Lookup a type_decl type from a given corpus, by its location.
Definition: abg-ir.cc:13483
ptr_to_mbr_type_sptr is_ptr_to_mbr_type(const type_or_decl_base_sptr &t, bool look_through_qualifiers)
Test whether a type is a ptr_to_mbr_type_sptr.
Definition: abg-ir.cc:11513
class_decl_sptr is_compatible_with_class_type(const decl_base_sptr &t)
Test if a type is a class. This function looks through typedefs.
Definition: abg-ir.cc:10938
corpus::origin operator|=(corpus::origin &l, corpus::origin r)
Bitwise |= operator for the corpus::origin type.
Definition: abg-corpus.cc:1770
vector< type_base_wptr > type_base_wptrs_type
A convenience typedef for a vector of type_base_wptr.
Definition: abg-fwd.h:143
const type_base_sptr is_void_pointer_type(const type_base_sptr &t)
Test if a type is a pointer to void type.
Definition: abg-ir.cc:11594
bool operator==(const union_decl_sptr &l, const union_decl_sptr &r)
Turn equality of shared_ptr of union_decl into a deep equality; that is, make it compare the pointed ...
Definition: abg-ir.cc:26766
void fixup_virtual_member_function(method_decl_sptr method)
When a virtual member function has seen its virtualness set by set_member_function_is_virtual(),...
Definition: abg-ir.cc:25289
void pop_composite_type_comparison_operands(const type_base &left, const type_base &right)
Pop a pair of operands from the stack of operands to the current type comparison.
Definition: abg-ir.cc:336
bool equals_modulo_cv_qualifier(const array_type_def *l, const array_type_def *r)
Test if two variables are equals modulo CV qualifiers.
Definition: abg-ir.cc:19436
const type_base_wptrs_type * lookup_enum_types(const string &qualified_name, const corpus &corp)
Look into a given corpus to find the enum type*s* that have a given qualified name.
Definition: abg-ir.cc:13824
qualified_type_def_sptr clone_qualified_type(const qualified_type_def_sptr &t)
Clone a qualifiend type.
Definition: abg-ir.cc:7807
string get_class_or_union_flat_representation(const class_or_union_sptr &cou, const string &indent, bool one_line, bool internal, bool qualified_names)
Get the flat representation of an instance of class_or_union type.
Definition: abg-ir.cc:9802
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10665
scope_decl * is_scope_decl(decl_base *d)
Test if a declaration is a scope_decl.
Definition: abg-ir.cc:5686
union_decl_sptr lookup_union_type(const string &type_name, const corpus &corp)
Look into a given corpus to find a union type which has a given qualified name.
Definition: abg-ir.cc:13730
bool is_anonymous_data_member(const decl_base &d)
Test if a decl is an anonymous data member.
Definition: abg-ir.cc:6113
bool is_anonymous_data_member(const var_decl &d)
Test if a var_decl is an anonymous data member.
Definition: abg-ir.cc:6215
string translation_unit_language_to_string(translation_unit::language l)
Converts a translation_unit::language enumerator into a string.
Definition: abg-ir.cc:1597
weak_ptr< type_base > type_base_wptr
Convenience typedef for a weak pointer on a type_base.
Definition: abg-fwd.h:129
type_decl_sptr is_integral_type(const type_or_decl_base_sptr &t)
Test if a type is an integral type.
Definition: abg-ir.cc:10805
type_base_sptr peel_reference_type(const type_base_sptr &type)
Return the leaf pointed-to type node of a reference_type_def node.
Definition: abg-ir.cc:7365
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5640
scope_decl * get_type_scope(const type_base_sptr &t)
Get the scope of a given type.
Definition: abg-ir.cc:8991
integral_type::modifiers_type operator~(integral_type::modifiers_type l)
Bitwise one's complement operator for integral_type::modifiers_type.
Definition: abg-ir.cc:16206
array_type_def::subrange_type * is_subrange_type(const type_or_decl_base *type)
Test if a type is an array_type_def::subrange_type.
Definition: abg-ir.cc:11997
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:886
typedef_decl_sptr lookup_typedef_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find a typedef type which has a given qualified name.
Definition: abg-ir.cc:13912
type_base_sptr lookup_type_from_translation_unit(const string &type_name, const string &tu_path, const corpus &corp)
Lookup a type from a given translation unit present in a give corpus.
Definition: abg-ir.cc:13367
var_decl_sptr get_data_member(type_base *clazz, const char *member_name)
Get a given data member, referred to by its name, of a class type.
Definition: abg-ir.cc:10111
type_base * get_exemplar_type(const type_base *type)
For a given type, return its exemplar type.
Definition: abg-ir.cc:28018
location get_location(const decl_base_sptr &decl)
Get the location of a given declaration.
Definition: abg-ir.cc:8953
const typedef_decl * is_typedef(const type_or_decl_base *t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10855
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8649
type_base_sptr peel_qualified_or_typedef_type(const type_base_sptr &t)
Return the leaf underlying type of a qualified or typedef type.
Definition: abg-ir.cc:7578
bool odr_is_relevant(const type_or_decl_base &artifact)
By looking at the language of the TU a given ABI artifact belongs to, test if the ONE Definition Rule...
Definition: abg-ir.cc:10279
array_type_def_sptr clone_array(const array_type_def_sptr &array)
Clone an array type.
Definition: abg-ir.cc:7740
enum_type_decl_sptr lookup_enum_type_per_location(const string &loc, const corpus &corp)
Look up an enum_type_decl from a given corpus, by its location.
Definition: abg-ir.cc:13855
change_kind
A bitfield that gives callers of abigail::ir::equals() some insight about how different two internal ...
Definition: abg-ir.h:1323
@ LOCAL_TYPE_CHANGE_KIND
This means that a given IR artifact has a local type change.
Definition: abg-ir.h:1327
@ SUBTYPE_CHANGE_KIND
This means that a given IR artifact has changes in some of its sub-types, with respect to the other a...
Definition: abg-ir.h:1343
@ LOCAL_NON_TYPE_CHANGE_KIND
This means that a given IR artifact has a local non-type change. That is a change that is carried by ...
Definition: abg-ir.h:1332
var_decl_sptr find_last_data_member_matching_regexp(const class_or_union &t, const regex::regex_t_sptr &regex)
Find the last data member of a class or union which name matches a regular expression.
Definition: abg-ir.cc:28499
const ptr_to_mbr_type * is_ptr_to_mbr_type(const type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a ptr_to_mbr_type.
Definition: abg-ir.cc:11493
const var_decl_sptr get_first_non_anonymous_data_member(const var_decl_sptr anon_dm)
Get the first non-anonymous data member of a given anonymous data member.
Definition: abg-ir.cc:5959
bool is_user_defined_type(const type_base *t)
Test if a type is user-defined.
Definition: abg-ir.cc:5720
bool operator==(const translation_unit_sptr &l, const translation_unit_sptr &r)
A deep comparison operator for pointers to translation units.
Definition: abg-ir.cc:1780
class_or_union_sptr anonymous_data_member_to_class_or_union(const var_decl_sptr &d)
Get the class_or_union type of a given anonymous data member.
Definition: abg-ir.cc:6340
weak_ptr< class_decl > class_decl_wptr
Convenience typedef for a weak pointer on a class_decl.
Definition: abg-fwd.h:203
const interned_string & get_node_name(var_decl_sptr node)
Gets the name of a var_decl node.
Definition: abg-ir.cc:12807
vector< type_base_sptr > type_base_sptrs_type
Helper typedef for a vector of shared pointer to a type_base.
Definition: abg-ir.h:119
void debug_comp_stack(const environment &env)
Emit a trace of the two comparison operands stack on the standard error stream.
Definition: abg-ir.cc:10264
bool collect_non_anonymous_data_members(const class_or_union *cou, string_decl_base_sptr_map &dms)
Collect all the non-anonymous data members of a class or union type.
Definition: abg-ir.cc:6056
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition: abg-ir.cc:10947
type_base_sptr synthesize_type_from_translation_unit(const type_base_sptr &type, translation_unit &tu)
In a translation unit, lookup a given type or synthesize it if it's a qualified type.
Definition: abg-ir.cc:15053
bool maybe_update_types_lookup_map< function_type >(const function_type_sptr &type, istring_type_base_wptrs_map_type &types_map, bool)
This is the specialization for type function_type of the function template:
Definition: abg-ir.cc:14480
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:242
method_type * is_method_type(type_or_decl_base *t)
Test whether a type is a method_type.
Definition: abg-ir.cc:11690
function_type_sptr lookup_or_synthesize_fn_type(const function_type_sptr &fn_t, const corpus &corpus)
Look into an ABI corpus for a function type.
Definition: abg-ir.cc:13392
qualified_type_def_sptr lookup_qualified_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find a qualified type which has a given qualified name.
Definition: abg-ir.cc:14030
bool is_declaration_only_class_or_union_type(const type_base *t, bool look_through_decl_only)
Test wheter a type is a declaration-only class.
Definition: abg-ir.cc:11131
bool is_global_scope(const shared_ptr< scope_decl >scope)
Tests whether if a given scope is the global scope.
Definition: abg-ir.cc:10429
type_base_sptr lookup_type(const type_base_sptr &t, const corpus &corp)
Look into a given corpus to find a type.
Definition: abg-ir.cc:14353
string get_pretty_representation(const type_or_decl_base *tod, bool internal)
Build and return a copy of the pretty representation of an ABI artifact that could be either a type o...
Definition: abg-ir.cc:9456
class_decl::base_spec * is_class_base_spec(const type_or_decl_base *tod)
Test if an ABI artifact is a class base specifier.
Definition: abg-ir.cc:25999
type_base_sptr lookup_class_typedef_or_enum_type(const string &qualified_name, const corpus &corp)
Look into a corpus to find a class, typedef or enum type which has a given qualified name.
Definition: abg-ir.cc:13995
void mark_types_as_being_compared(T &l, T &r)
Mark a pair of types as being compared.
Definition: abg-ir.cc:1016
const type_base_sptr peel_qualified_type(const type_base_sptr &type)
Return the leaf underlying type of a qualified type.
Definition: abg-ir.cc:7478
type_base_sptr peel_const_qualified_type(const qualified_type_def_sptr &q)
If a qualified type is const, then return its underlying type.
Definition: abg-ir.cc:7525
bool is_at_template_scope(const shared_ptr< decl_base > decl)
Tests whether a given decl is at template scope.
Definition: abg-ir.cc:10530
function_type_sptr lookup_function_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find a function type which has a given qualified name.
Definition: abg-ir.cc:14246
const class_or_union_sptr data_member_has_anonymous_type(const var_decl &d)
Test if a data member has annonymous type or not.
Definition: abg-ir.cc:6299
type_decl * is_integral_type(const type_or_decl_base *t)
Test if a type is an integral type.
Definition: abg-ir.cc:10785
bool anonymous_data_member_exists_in_class(const var_decl &anon_dm, const class_or_union &clazz)
Test if a given anonymous data member exists in a class or union.
Definition: abg-ir.cc:6355
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6723
const function_type * is_function_type(const type_or_decl_base *t)
Test whether a type is a function_type.
Definition: abg-ir.cc:11660
const type_base_sptr peel_array_type(const type_base_sptr &type)
Return the leaf element type of an array.
Definition: abg-ir.cc:7414
bool maybe_update_types_lookup_map< class_decl >(const class_decl_sptr &class_type, istring_type_base_wptrs_map_type &map, bool use_type_name_as_key)
This is the specialization for type class_decl of the function template:
Definition: abg-ir.cc:14418
bool types_have_similar_structure(const type_base_sptr &first, const type_base_sptr &second, bool indirect_type)
Test if two types have similar structures, even though they are (or can be) different.
Definition: abg-ir.cc:28162
shared_ptr< template_parameter > template_parameter_sptr
Convenience typedef for shared pointer to template parameter.
Definition: abg-fwd.h:312
enum_type_decl_sptr look_through_decl_only_enum(enum_type_decl_sptr enom)
If an enum is a decl-only enum, get its definition. Otherwise, just return the initial enum.
Definition: abg-ir.cc:11740
class_or_union * is_class_or_union_type(const type_or_decl_base *t)
Test if a type is a class_or_union.
Definition: abg-ir.cc:11178
type_base_sptr look_through_decl_only(const type_base_sptr &t)
If a type is is decl-only, then get its definition. Otherwise, just return the initial type.
Definition: abg-ir.cc:11825
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition: abg-fwd.h:191
type_base_sptr peel_typedef_pointer_or_reference_type(const type_base_sptr type)
Return the leaf underlying or pointed-to type node of a typedef_decl, pointer_type_def,...
Definition: abg-ir.cc:7601
void set_member_function_is_const(function_decl &f, bool is_const)
set the const-ness property of a member function.
Definition: abg-ir.cc:6779
string get_name(const type_or_decl_base_sptr &tod, bool qualified)
Build and return a copy of the name of an ABI artifact that is either a type of a decl.
Definition: abg-ir.cc:8892
bool is_anonymous_type(const type_base_sptr &t)
Test if a given type is anonymous.
Definition: abg-ir.cc:10741
decl_base_sptr strip_useless_const_qualification(const qualified_type_def_sptr t)
Strip qualification from a qualified type, when it makes sense.
Definition: abg-ir.cc:7107
bool string_to_elf_symbol_type(const string &s, elf_symbol::type &t)
Convert a string representing a symbol type into an elf_symbol::type.
Definition: abg-ir.cc:2976
namespace_decl_sptr is_namespace(const decl_base_sptr &d)
Tests if a declaration is a namespace declaration.
Definition: abg-ir.cc:11860
array_type_def_sptr is_array_of_qualified_element(const type_base_sptr &type)
Test if an array type is an array to a qualified element type.
Definition: abg-ir.cc:11953
const var_decl_sptr get_next_data_member(const class_or_union *klass, const var_decl_sptr &data_member)
In the context of a given class or union, this function returns the data member that is located after...
Definition: abg-ir.cc:5983
void set_member_function_is_ctor(function_decl &f, bool c)
Setter for the is_ctor property of the member function.
Definition: abg-ir.cc:6666
const type_decl * is_type_decl(const type_or_decl_base *t)
Test whether a type is a type_decl (a builtin type).
Definition: abg-ir.cc:10767
decl_base * is_decl_slow(const type_or_decl_base *t)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10645
decl_base_sptr look_through_decl_only(const decl_base &d)
If a decl is decl-only get its definition. Otherwise, just return nil.
Definition: abg-ir.cc:11749
function_type_sptr is_function_type(const type_or_decl_base_sptr &t)
Test whether a type is a function_type.
Definition: abg-ir.cc:11640
scope_decl_sptr is_scope_decl(const decl_base_sptr &d)
Test if a declaration is a scope_decl.
Definition: abg-ir.cc:5696
string get_name(const type_or_decl_base *tod, bool qualified)
Build and return a copy of the name of an ABI artifact that is either a type or a decl.
Definition: abg-ir.cc:8860
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5785
bool is_template_parameter(const shared_ptr< decl_base > decl)
Tests whether a decl is a template parameter.
Definition: abg-ir.cc:10539
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10825
type_base_sptr is_type(const type_or_decl_base_sptr &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10693
uint64_t get_var_size_in_bits(const var_decl_sptr &v)
Get the size of a given variable.
Definition: abg-ir.cc:6557
string get_debug_representation(const type_or_decl_base *artifact)
Get the textual representation of a type for debugging purposes.
Definition: abg-ir.cc:9983
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition: abg-fwd.h:209
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition: abg-fwd.h:165
function_type_sptr synthesize_function_type_from_translation_unit(const function_type &fn_type, translation_unit &tu)
In a translation unit, lookup the sub-types that make up a given function type and if the sub-types a...
Definition: abg-ir.cc:15136
std::ostream & operator<<(std::ostream &o, elf_symbol::type t)
Serialize an instance of symbol_type and stream it to a given output stream.
Definition: abg-ir.cc:2848
type_base_sptr peel_pointer_type(const type_base_sptr &type)
Return the leaf pointed-to type node of a pointer_type_def node.
Definition: abg-ir.cc:7309
bool var_equals_modulo_types(const var_decl &l, const var_decl &r, change_kind *k)
Compares two instances of var_decl without taking their type into account.
Definition: abg-ir.cc:20890
type_base_sptr lookup_type_through_translation_units(const string &qn, const corpus &abi_corpus)
Lookup a type definition in all the translation units of a given ABI corpus.
Definition: abg-ir.cc:13342
void sort_types(const canonical_type_sptr_set_type &types, vector< type_base_sptr > &result)
Sort types in a hopefully stable manner.
Definition: abg-ir.cc:3614
type_base * peel_pointer_or_reference_type(const type_base *type, bool peel_qual_type)
Return the leaf underlying or pointed-to type node of a, pointer_type_def, reference_type_def or qual...
Definition: abg-ir.cc:7703
decl_base_sptr is_decl_slow(const type_or_decl_base_sptr &t)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10656
reference_type_def * is_reference_type(type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:11433
corpus::origin operator|(corpus::origin l, corpus::origin r)
Bitwise | operator for the corpus::origin type.
Definition: abg-corpus.cc:1756
bool elf_symbol_is_function(elf_symbol::type t)
Test if the type of an ELF symbol denotes a function symbol.
Definition: abg-ir.cc:3057
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition: abg-ir.cc:1743
bool try_canonical_compare(const T *l, const T *r)
Compare two types by comparing their canonical types if present.
Definition: abg-ir.cc:903
enum_type_decl_sptr is_compatible_with_enum_type(const decl_base_sptr &t)
Test if a type is an enum. This function looks through typedefs.
Definition: abg-ir.cc:10888
function_decl::parameter_sptr is_function_parameter(const type_or_decl_base_sptr tod)
Test whether an ABI artifact is a function_decl.
Definition: abg-ir.cc:10595
class_or_union_sptr look_through_decl_only_class(class_or_union_sptr klass)
If a class (or union) is a decl-only class, get its definition. Otherwise, just return the initial cl...
Definition: abg-ir.cc:11720
bool lookup_decl_only_class_types(const interned_string &qualified_name, const corpus &corp, type_base_wptrs_type &result)
Look into a given corpus to find the class type*s* that have a given qualified name and that are decl...
Definition: abg-ir.cc:13595
bool member_function_has_vtable_offset(const function_decl &f)
Test if a virtual member function has a vtable offset set.
Definition: abg-ir.cc:6808
bool parse_integral_type(const string &type_name, integral_type &type)
Parse an integral type from a string.
Definition: abg-ir.cc:16389
const type_base * peel_typedef_type(const type_base *type)
Return the leaf underlying type node of a typedef_decl node.
Definition: abg-ir.cc:7282
unordered_map< interned_string, bool, hash_interned_string > interned_string_bool_map_type
Convenience typedef for a map of interned_string -> bool.
Definition: abg-ir.cc:3277
type_base_sptr lookup_type_through_scopes(const list< string > &fqn, const translation_unit &tu)
Lookup a type from a translation unit by walking its scopes in sequence and by looking into them.
Definition: abg-ir.cc:13082
const enum_type_decl * is_enum_type(const type_or_decl_base *d)
Test if a decl is an enum_type_decl.
Definition: abg-ir.cc:10897
const global_scope * get_global_scope(const shared_ptr< decl_base > decl)
Return the global scope as seen by a given declaration.
Definition: abg-ir.cc:8746
unordered_map< interned_string, type_base_wptrs_type, hash_interned_string > istring_type_base_wptrs_map_type
A convenience typedef for a map which key is an interned_string and which value is a vector of type_b...
Definition: abg-fwd.h:149
shared_ptr< class_or_union > is_class_or_union_type(const shared_ptr< type_or_decl_base > &t)
Test if a type is a class_or_union.
Definition: abg-ir.cc:11188
bool function_decl_is_less_than(const function_decl &f, const function_decl &s)
Test if the pretty representation of a given function_decl is lexicographically less then the pretty ...
Definition: abg-ir.cc:28110
bool elf_symbols_alias(const elf_symbol &s1, const elf_symbol &s2)
Test if two symbols alias.
Definition: abg-ir.cc:2779
var_decl_sptr find_data_member_from_anonymous_data_member(const var_decl_sptr &anon_dm, const string &name)
Find a data member inside an anonymous data member.
Definition: abg-ir.cc:10508
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:254
shared_ptr< ptr_to_mbr_type > ptr_to_mbr_type_sptr
Convenience typedef for a shared pointer to a ptr_to_mbr_type.
Definition: abg-fwd.h:238
const location & get_natural_or_artificial_location(const decl_base *decl)
Get the non-artificial (natural) location of a decl.
Definition: abg-ir.cc:10124
string build_qualified_name(const scope_decl *scope, const type_base_sptr &type)
Build and return the qualified name of a type in its scope.
Definition: abg-ir.cc:8929
size_t hash_type_or_decl(const type_or_decl_base *tod)
Hash an ABI artifact that is either a type or a decl.
Definition: abg-ir.cc:27845
array_type_def_sptr is_array_type(const type_or_decl_base_sptr &type, bool look_through_qualifiers)
Test if a type is an array_type_def.
Definition: abg-ir.cc:11921
corpus::origin operator&(corpus::origin l, corpus::origin r)
Bitwise & operator for the corpus::origin type.
Definition: abg-corpus.cc:1784
unordered_map< const function_decl *, string, function_decl::hash, function_decl::ptr_equal > fns_to_str_map_type
Convenience typedef for a hash map of pointer to function_decl and string.
Definition: abg-ir.cc:29656
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition: abg-fwd.h:262
class_decl_sptr lookup_class_type_per_location(const string &loc, const corpus &corp)
Look up a class_decl from a given corpus by its location.
Definition: abg-ir.cc:13689
bool string_to_elf_symbol_binding(const string &s, elf_symbol::binding &b)
Convert a string representing a an elf symbol binding into an elf_symbol::binding.
Definition: abg-ir.cc:3009
shared_ptr< type_or_decl_base > type_or_decl_base_sptr
A convenience typedef for a shared_ptr to type_or_decl_base.
Definition: abg-fwd.h:119
shared_ptr< translation_unit > translation_unit_sptr
Convenience typedef for a shared pointer on a translation_unit type.
Definition: abg-fwd.h:134
bool is_data_member_of_anonymous_class_or_union(const var_decl &d)
Test if a var_decl is a data member belonging to an anonymous type.
Definition: abg-ir.cc:6231
namespace_decl * is_namespace(const decl_base *d)
Tests if a declaration is a namespace declaration.
Definition: abg-ir.cc:11869
type_base * peel_typedef_pointer_or_reference_type(const type_base *type, bool peel_qual_type)
Return the leaf underlying or pointed-to type node of a typedef_decl, pointer_type_def or reference_t...
Definition: abg-ir.cc:7664
const type_base * is_void_pointer_type_equivalent(const type_base *type)
Test if a type is equivalent to a pointer to void type.
Definition: abg-ir.cc:11532
lookup_entity_kind
This enum describe the kind of entity to lookup, while using the lookup API.
Definition: abg-ir.cc:12026
type_base * type_has_non_canonicalized_subtype(type_base_sptr t)
Test if a type has sub-types that are non-canonicalized.
Definition: abg-ir.cc:27782
unordered_map< string, decl_base_sptr > string_decl_base_sptr_map
Convenience typedef for a map which key is a string and which value is a decl_base_sptr.
Definition: abg-fwd.h:158
bool is_function_template_pattern(const shared_ptr< decl_base > decl)
Test whether a decl is the pattern of a function template.
Definition: abg-ir.cc:11892
void push_composite_type_comparison_operands(const type_base &left, const type_base &right)
Push a pair of operands on the stack of operands of the current type comparison, during type canonica...
Definition: abg-ir.cc:315
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition: abg-ir.cc:1757
function_decl::parameter * is_function_parameter(const type_or_decl_base *tod)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10582
type_decl_sptr lookup_basic_type(const string &qualified_name, const corpus &corp)
Look into a given corpus to find a basic type which has a given qualified name.
Definition: abg-ir.cc:13502
class_or_union * is_at_class_scope(const decl_base &decl)
Tests whether a given decl is at class scope.
Definition: abg-ir.cc:10488
bool equals(const decl_base &l, const decl_base &r, change_kind *k)
Compares two instances of decl_base.
Definition: abg-ir.cc:5367
bool get_data_member_is_laid_out(const var_decl &m)
Test whether a data member is laid out.
Definition: abg-ir.cc:6585
bool is_template_parm_composition_type(const shared_ptr< decl_base > decl)
Tests whether a decl is a template parameter composition type.
Definition: abg-ir.cc:11878
const decl_base_sptr lookup_var_decl_in_scope(const std::list< string > &comps, const scope_decl_sptr &skope)
lookup a var_decl in a scope.
Definition: abg-ir.cc:13049
bool string_to_elf_symbol_visibility(const string &s, elf_symbol::visibility &v)
Convert a string representing a an elf symbol visibility into an elf_symbol::visibility.
Definition: abg-ir.cc:3034
union_decl_sptr lookup_union_type_per_location(const string &loc, const corpus &corp)
Lookup a union type in a given corpus, from its location.
Definition: abg-ir.cc:12354
type_base_sptr canonicalize(type_base_sptr t)
Compute the canonical type of a given type.
Definition: abg-ir.cc:15880
weak_ptr< elf_symbol > elf_symbol_wptr
A convenience typedef for a weak pointer to elf_symbol.
Definition: abg-ir.h:891
bool get_member_function_is_const(const function_decl &f)
Test whether a member function is const.
Definition: abg-ir.cc:6751
interned_string get_name_of_reference_to_type(const type_base &pointed_to_type, bool lvalue_reference, bool qualified, bool internal)
Get the name of the reference to a given type.
Definition: abg-ir.cc:9206
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition: abg-fwd.h:224
void maybe_update_types_lookup_map(const type_decl_sptr &basic_type)
Update the map that associates the fully qualified name of a basic type with the type itself.
Definition: abg-ir.cc:14510
bool is_enumerator_present_in_enum(const enum_type_decl::enumerator &enr, const enum_type_decl &enom)
Test if a given enumerator is found present in an enum.
Definition: abg-ir.cc:19932
interned_string get_method_type_name(const method_type &fn_type, bool internal)
Get the name of a given method type and return a copy of it.
Definition: abg-ir.cc:9411
bool is_const_qualified_type(const qualified_type_def_sptr &t)
Test if a given qualified type is const.
Definition: abg-ir.cc:7493
translation_unit::language string_to_translation_unit_language(const string &l)
Parse a string representing a language into a translation_unit::language enumerator into a string.
Definition: abg-ir.cc:1667
enum_type_decl_sptr is_enum_type(const type_or_decl_base_sptr &d)
Test if a decl is an enum_type_decl.
Definition: abg-ir.cc:10906
var_decl_sptr has_fake_flexible_array_data_member(const class_decl_sptr &klass)
Test if the last data member of a class is an array with one element.
Definition: abg-ir.cc:11118
type_base_sptr lookup_type_per_location(const interned_string &loc, const corpus &corp)
Lookup a type from a corpus, by its location.
Definition: abg-ir.cc:14300
bool get_next_data_member_offset(const class_or_union *klass, const var_decl_sptr &dm, uint64_t &offset)
Get the offset of the non-static data member that comes after a given one.
Definition: abg-ir.cc:6470
uint64_t get_absolute_data_member_offset(const var_decl &m)
Get the absolute offset of a data member.
Definition: abg-ir.cc:6514
shared_ptr< ir_traversable_base > ir_traversable_base_sptr
Convenience typedef for a shared pointer to ir_traversable_base.
Definition: abg-fwd.h:106
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition: abg-ir.cc:6609
const function_decl::parameter * get_function_parameter(const decl_base *fun, unsigned parm_index)
Get the function parameter designated by its index.
Definition: abg-ir.cc:28431
var_decl * is_var_decl(const type_or_decl_base *tod)
Tests if a declaration is a variable declaration.
Definition: abg-ir.cc:11841
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1729
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10605
void keep_type_alive(type_base_sptr t)
Make sure that the life time of a given (smart pointer to a) type is the same as the life time of the...
Definition: abg-ir.cc:27820
method_decl * is_method_decl(const type_or_decl_base *d)
Test if a function_decl is actually a method_decl.
Definition: abg-ir.cc:25121
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5705
string get_class_or_union_flat_representation(const class_or_union &cou, const string &indent, bool one_line, bool internal, bool qualified_names)
Get the flat representation of an instance of class_or_union type.
Definition: abg-ir.cc:9678
array_type_def::subrange_sptr is_subrange_type(const type_or_decl_base_sptr &type)
Test if a type is an array_type_def::subrange_type.
Definition: abg-ir.cc:12010
decl_base_sptr add_decl_to_scope(decl_base_sptr decl, scope_decl *scope)
Appends a declaration to a given scope, if the declaration doesn't already belong to one and if the d...
Definition: abg-ir.cc:8625
string build_internal_underlying_enum_type_name(const string &base_name, bool is_anonymous, uint64_t size)
Build the internal name of the underlying type of an enum.
Definition: abg-ir.cc:28453
bool is_npaf_type(const type_base_sptr &t)
Test if a type is a neither a pointer, an array nor a function type.
Definition: abg-ir.cc:10752
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5756
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition: abg-fwd.h:173
void set_data_member_offset(var_decl_sptr m, uint64_t o)
Set the offset of a data member into its containing class.
Definition: abg-ir.cc:6408
type_base_sptr strip_typedef(const type_base_sptr type)
Recursively returns the the underlying type of a typedef. The return type should not be a typedef of ...
Definition: abg-ir.cc:6972
uint64_t get_data_member_offset(const var_decl &m)
Get the offset of a data member.
Definition: abg-ir.cc:6425
unordered_set< uintptr_t > pointer_set
A convenience typedef for an unordered set of pointer values.
Definition: abg-ir.h:100
type_decl_sptr is_type_decl(const type_or_decl_base_sptr &t)
Test whether a type is a type_decl (a builtin type).
Definition: abg-ir.cc:10775
class_or_union * is_at_class_scope(const decl_base_sptr decl)
Tests whether a given decl is at class scope.
Definition: abg-ir.cc:10465
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6882
const pointer_type_def * is_pointer_type(const type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:11261
string get_class_or_enum_flat_representation(const type_base &coe, const string &indent, bool one_line, bool internal, bool qualified_name)
Get the flat representation of an instance of enum_type_decl type.
Definition: abg-ir.cc:9955
bool types_are_compatible(const decl_base_sptr d1, const decl_base_sptr d2)
Test if two types are equal modulo a typedef.
Definition: abg-ir.cc:10370
class_or_union * anonymous_data_member_to_class_or_union(const var_decl *d)
Get the class_or_union type of a given anonymous data member.
Definition: abg-ir.cc:6271
location get_location(const type_base_sptr &type)
Get the location of the declaration of a given type.
Definition: abg-ir.cc:8940
pointer_type_def_sptr is_pointer_to_function_type(const type_base_sptr &t)
Test if a type is a pointer to function type.
Definition: abg-ir.cc:11301
weak_ptr< decl_base > decl_base_wptr
Convenience typedef for a weak pointer to a decl_base.
Definition: abg-fwd.h:182
decl_base_sptr insert_decl_into_scope(decl_base_sptr decl, scope_decl::declarations::iterator before, scope_decl *scope)
Inserts a declaration into a given scope, before a given IR child node of the scope.
Definition: abg-ir.cc:8668
interned_string get_function_type_name(const function_type_sptr &fn_type, bool internal)
Get the name of a given function type and return a copy of it.
Definition: abg-ir.cc:9283
bool return_comparison_result(T &l, T &r, bool value, bool propagate_canonical_type=true)
Return the result of the comparison of two (sub) types.
Definition: abg-ir.cc:1101
class_or_union * look_through_decl_only_class(class_or_union *the_class)
If a class (or union) is a decl-only class, get its definition. Otherwise, just return the initial cl...
Definition: abg-ir.cc:11700
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:11227
const location & get_artificial_or_natural_location(const decl_base *decl)
Get the artificial location of a decl.
Definition: abg-ir.cc:10143
typedef_decl_sptr lookup_typedef_type_per_location(const string &loc, const corpus &corp)
Lookup a typedef_decl from a corpus, by its location.
Definition: abg-ir.cc:13950
type_base_sptr peel_typedef_type(const type_base_sptr &type)
Return the leaf underlying type node of a typedef_decl node.
Definition: abg-ir.cc:7255
interned_string get_name_of_qualified_type(const type_base_sptr &underlying_type, qualified_type_def::CV quals, bool qualified, bool internal)
Get the name of a qualified type, given the underlying type and its qualifiers.
Definition: abg-ir.cc:9236
shared_ptr< template_decl > template_decl_sptr
Convenience typedef for a shared pointer to template_decl.
Definition: abg-fwd.h:304
array_type_def_sptr is_typedef_of_array(const type_base_sptr &t)
Test if a type is a typedef of an array.
Definition: abg-ir.cc:11976
string get_string_representation_of_cv_quals(const qualified_type_def::CV cv_quals)
Get the string representation of a CV qualifier bitmap.
Definition: abg-ir.cc:8830
void set_data_member_is_laid_out(var_decl_sptr m, bool l)
Set a flag saying if a data member is laid out.
Definition: abg-ir.cc:6571
pointer_type_def_sptr is_pointer_to_ptr_to_mbr_type(const type_base_sptr &t)
Test if we are looking at a pointer to pointer to member type.
Definition: abg-ir.cc:11353
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5854
var_decl_sptr find_first_data_member_matching_regexp(const class_or_union &t, const regex::regex_t_sptr &r)
Find the first data member of a class or union which name matches a regular expression.
Definition: abg-ir.cc:28478
union_decl_sptr is_union_type(const shared_ptr< type_or_decl_base > &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:11247
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:10298
bool is_at_global_scope(const decl_base *decl)
Tests whether a given declaration is at global scope.
Definition: abg-ir.cc:10456
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:26245
array_type_def * is_array_type(const type_or_decl_base *type, bool look_through_qualifiers)
Test if a type is an array_type_def.
Definition: abg-ir.cc:11905
weak_ptr< template_decl > template_decl_wptr
Convenience typedef for a weak pointer to template_decl.
Definition: abg-fwd.h:310
const var_decl * lookup_data_member(const type_base *type, const char *dm_name)
Look for a data member of a given class, struct or union type and return it.
Definition: abg-ir.cc:28388
interned_string get_function_id_or_pretty_representation(const function_decl *fn)
Get the ID of a function, or, if the ID can designate several different functions,...
Definition: abg-ir.cc:9348
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition: abg-fwd.h:160
bool is_comparison_cycle_detected(T &l, T &r)
Detect if a recursive comparison cycle is detected while structurally comparing two types (a....
Definition: abg-ir.cc:952
typedef_decl_sptr clone_typedef(const typedef_decl_sptr &t)
Clone a typedef type.
Definition: abg-ir.cc:7782
string get_pretty_representation(const method_type_sptr method, bool internal)
Get the pretty representation of a method type.
Definition: abg-ir.cc:9658
string get_enum_flat_representation(const enum_type_decl_sptr &enum_type, const string &indent, bool one_line, bool qualified_names)
Get the flat representation of an instance of enum_type_decl type.
Definition: abg-ir.cc:9925
void unmark_types_as_being_compared(T &l, T &r)
Mark a pair of types as being not compared anymore.
Definition: abg-ir.cc:1051
unordered_set< type_base_sptr, canonical_type_hash > canonical_type_sptr_set_type
Helper typedef for an unordered set of type_base_sptr which uses pointer value to tell its members ap...
Definition: abg-ir.h:113
array_type_def_sptr lookup_array_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find an array type which has the same qualified name as a given array typ...
Definition: abg-ir.cc:14175
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition: abg-fwd.h:282
var_decl_sptr is_var_decl(const type_or_decl_base_sptr &decl)
Tests if a declaration is a variable declaration.
Definition: abg-ir.cc:11851
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition: abg-ir.cc:1766
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:15210
bool is_unique_type(const type_base_sptr &t)
Test if a type is unique in the entire environment.
Definition: abg-ir.cc:27983
string components_to_type_name(const list< string > &comps)
Turn a set of qualified name components (that name a type) into a qualified name string.
Definition: abg-ir.cc:12117
interned_string get_type_name(const type_base_sptr &t, bool qualified, bool internal)
Get the name of a given type and return a copy of it.
Definition: abg-ir.cc:9009
bool mark_dependant_types_compared_until(const type_base &r)
In the stack of the current types being compared (as part of type canonicalization),...
Definition: abg-ir.cc:400
corpus::origin operator&=(corpus::origin &l, corpus::origin r)
Bitwise &= operator for the corpus::origin type.
Definition: abg-corpus.cc:1798
type_base_sptr clone_array_tree(const type_base_sptr t)
Clone a type tree made of an array or a typedef of array.
Definition: abg-ir.cc:7860
string get_enum_flat_representation(const enum_type_decl &enum_type, const string &indent, bool one_line, bool qualified_names)
Get the flat representation of an instance of enum_type_decl type.
Definition: abg-ir.cc:9834
interned_string get_function_type_name(const function_type &fn_type, bool internal)
Get the name of a given function type and return a copy of it.
Definition: abg-ir.cc:9320
decl_base_sptr is_decl(const type_or_decl_base_sptr &d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10634
bool operator!=(const translation_unit_sptr &l, const translation_unit_sptr &r)
A deep inequality operator for pointers to translation units.
Definition: abg-ir.cc:1799
interned_string get_name_of_pointer_to_type(const type_base &pointed_to_type, bool qualified, bool internal)
Get the name of the pointer to a given type.
Definition: abg-ir.cc:9184
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10553
bool is_template_decl(const decl_base_sptr &decl)
Tests whether a decl is a template.
Definition: abg-ir.cc:12020
bool elf_symbol_is_variable(elf_symbol::type t)
Test if the type of an ELF symbol denotes a function symbol.
Definition: abg-ir.cc:3067
bool type_has_sub_type_changes(const type_base_sptr t_v1, const type_base_sptr t_v2)
Tests if the change of a given type effectively comes from just its sub-types. That is,...
Definition: abg-ir.cc:27804
var_decl_sptr has_flexible_array_data_member(const class_decl_sptr &klass)
Test if the last data member of a class is an array with non-finite data member.
Definition: abg-ir.cc:11036
method_type_sptr is_method_type(const type_or_decl_base_sptr &t)
Test whether a type is a method_type.
Definition: abg-ir.cc:11670
qualified_type_def * is_qualified_type(const type_or_decl_base *t)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:11620
bool is_typedef_ptr_or_ref_to_decl_only_class_or_union_type(const type_base *t)
Test if a type is a typedef, pointer or reference to a decl-only class/union.
Definition: abg-ir.cc:11373
const scope_decl * get_top_most_scope_under(const decl_base_sptr decl, const scope_decl_sptr scope)
Return the a scope S containing a given declaration and that is right under a given scope P.
Definition: abg-ir.cc:8817
std::unordered_map< string, elf_symbols > string_elf_symbols_map_type
Convenience typedef for a map which key is a string and which value is a vector of elf_symbol.
Definition: abg-ir.h:909
string build_qualified_name(const scope_decl *scope, const string &name)
Build and return a qualified name from a name and its scope.
Definition: abg-ir.cc:8906
void set_member_function_vtable_offset(function_decl &f, ssize_t s)
Set the vtable offset of a member function.
Definition: abg-ir.cc:6852
method_decl_sptr copy_member_function(const class_or_union_sptr &t, const method_decl_sptr &method)
Copy a method of a class_or_union into a new class_or_union.
Definition: abg-ir.cc:24119
decl_base_sptr get_type_declaration(const type_base_sptr t)
Get the declaration for a given type.
Definition: abg-ir.cc:10316
shared_ptr< function_tdecl > function_tdecl_sptr
Convenience typedef for a shared pointer on a function_tdecl.
Definition: abg-fwd.h:292
type_base * peel_qualified_or_typedef_type(const type_base *type)
Return the leaf underlying type of a qualified or typedef type.
Definition: abg-ir.cc:7550
type_base_sptr type_or_void(const type_base_sptr t, const environment &env)
Return either the type given in parameter if it's non-null, or the void type.
Definition: abg-ir.cc:15240
pointer_type_def_sptr is_pointer_type(const type_or_decl_base_sptr &t, bool look_through_qualifiers)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:11285
const type_base_wptrs_type * lookup_union_types(const string &qualified_name, const corpus &corp)
Look into a given corpus to find the union types that have a given qualified name.
Definition: abg-ir.cc:13657
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5658
bool maybe_compare_as_member_decls(const decl_base &l, const decl_base &r, change_kind *k)
Compare the properties that belong to the "is-a-member-relation" of a decl.
Definition: abg-ir.cc:5267
qualified_type_def_sptr is_qualified_type(const type_or_decl_base_sptr &t)
Test whether a type is a qualified_type_def.
Definition: abg-ir.cc:11630
bool class_or_union_types_of_same_kind(const class_or_union_sptr &first, const class_or_union_sptr &second)
Test if two class or union types are of the same kind.
Definition: abg-ir.cc:11217
class_decl_sptr lookup_class_type_through_scopes(const list< string > &fqn, const translation_unit &tu)
Lookup a class type from a translation unit by walking its scopes in sequence and by looking into the...
Definition: abg-ir.cc:13102
reference_type_def_sptr is_reference_type(const type_or_decl_base_sptr &t, bool look_through_qualifiers)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:11477
void fqn_to_components(const string &fqn, list< string > &comps)
Decompose a fully qualified name into the list of its components.
Definition: abg-ir.cc:12091
bool get_member_function_is_ctor(const function_decl &f)
Test whether a member function is a constructor.
Definition: abg-ir.cc:6636
bool is_declaration_only_class_type(const type_base_sptr &t, bool look_through_decl_only)
Test wheter a type is a declaration-only class.
Definition: abg-ir.cc:11167
enum_type_decl_sptr lookup_enum_type(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find an enum type which has a given qualified name.
Definition: abg-ir.cc:13787
bool match(const regex_t_sptr &r, const std::string &str)
See if a string matches a regex.
Definition: abg-regex.cc:127
std::shared_ptr< regex_t > regex_t_sptr
A convenience typedef for a shared pointer of regex_t.
Definition: abg-fwd.h:88
bool base_name(string const &path, string &file_name)
Return the file name part of a file part.
const char * get_anonymous_subrange_internal_name_prefix()
Getter of the prefix for the name of anonymous range.
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
bool decl_names_equal(const string &l, const string &r)
Compare two fully qualified decl names by taking into account that they might have compontents that a...
Toplevel namespace for libabigail.
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition: abg-ir.cc:187
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition: abg-ir.cc:153
std::ostream & operator<<(std::ostream &o, const interned_string &s)
Streaming operator.
Definition: abg-ir.cc:170
unordered_map< string, string * > pool_map_type
Convenience typedef for a map of string -> string*.
Definition: abg-ir.cc:75
A functor to hash instances of interned_string.
size_t operator()(const type_base_sptr &l) const
Hash a type by returning the pointer value of its canonical type.
Definition: abg-ir.cc:7975
The hashing functor for class_decl::base_spec.
Definition: abg-ir.h:4888
Hasher for the class_decl type.
Definition: abg-ir.h:4389
The private data of the environment type.
Definition: abg-ir-priv.h:389
A hashing functor fo instances and pointers of function_decl.
Definition: abg-ir.h:4854
A hashing functor for a function_decl::parameter.
Definition: abg-ir.h:3368
Equality functor for instances of function_decl.
Definition: abg-ir.h:4864
The hashing functor for function_type.
Definition: abg-ir.h:3473
The type of the private data of the function_type type.
Definition: abg-ir-priv.h:1538
virtual bool traverse(ir_node_visitor &v)
Traverse a given IR node and its children, calling an visitor on each node.
Definition: abg-ir.cc:29326
The hashing functor for member_base.
Definition: abg-ir.h:4895
Hasher for the non_type_tparameter type.
Definition: abg-ir.h:3695
Hasher for the scope_decl type.
Definition: abg-ir.h:1938
Private type to hold private members of translation_unit.
Definition: abg-ir-priv.h:143
Definition of the private data of type_base.
Definition: abg-ir-priv.h:179
Hasher for the type_composition type.
Definition: abg-ir.h:3772
A predicate for deep equality of instances of shared_ptr<type_base>
Definition: abg-ir.h:2086
A hashing functor for instances and pointers of var_decl.
Definition: abg-ir.h:4823
A deleter for shared pointers that ... doesn't delete the object managed by the shared pointer.