libabigail
abg-ir.cc
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*- mode:
2 // C++ -*-
3 //
4 // Copyright (C) 2013-2023 Red Hat, Inc.
5 //
6 //Author: Dodji Seketeli
7 
8 /// @file
9 ///
10 /// Definitions for the Internal Representation artifacts of libabigail.
11 
12 #include <cxxabi.h>
13 #include <algorithm>
14 #include <cstdint>
15 #include <functional>
16 #include <iterator>
17 #include <memory>
18 #include <sstream>
19 #include <typeinfo>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23 
24 #include "abg-internal.h"
25 // <headers defining libabigail's API go under here>
26 ABG_BEGIN_EXPORT_DECLARATIONS
27 
28 #include "abg-interned-str.h"
29 #include "abg-ir.h"
30 #include "abg-corpus.h"
31 #include "abg-regex.h"
32 
33 ABG_END_EXPORT_DECLARATIONS
34 // </headers defining libabigail's API>
35 
36 #include "abg-corpus-priv.h"
37 #include "abg-tools-utils.h"
38 #include "abg-comp-filter.h"
39 #include "abg-ir-priv.h"
40 
41 namespace
42 {
43 /// This internal type is a tree walking that is used to set the
44 /// qualified name of a tree of decls and types. It used by the
45 /// function update_qualified_name().
46 class qualified_name_setter : public abigail::ir::ir_node_visitor
47 {
48 
49 public:
50  bool
51  do_update(abigail::ir::decl_base* d);
52 
53  bool
54  visit_begin(abigail::ir::decl_base* d);
55 
56  bool
57  visit_begin(abigail::ir::type_base* d);
58 }; // end class qualified_name_setter
59 
60 }// end anon namespace
61 
62 namespace abigail
63 {
64 
65 // Inject.
66 using std::string;
67 using std::list;
68 using std::vector;
69 using std::unordered_map;
70 using std::dynamic_pointer_cast;
71 using std::static_pointer_cast;
72 
73 /// Convenience typedef for a map of string -> string*.
74 typedef unordered_map<string, string*> pool_map_type;
75 
76 /// The type of the private data structure of type @ref
77 /// intered_string_pool.
78 struct interned_string_pool::priv
79 {
80  pool_map_type map;
81 }; //end struc struct interned_string_pool::priv
82 
83 /// Default constructor.
85  : priv_(new priv)
86 {
87  priv_->map[""] = 0;
88 }
89 
90 /// Test if the interned string pool already contains a string with a
91 /// given value.
92 ///
93 /// @param s the string to test for.
94 ///
95 /// @return true if the pool contains a string with the value @p s.
96 bool
98 {return priv_->map.find(s) != priv_->map.end();}
99 
100 /// Get a pointer to the interned string which has a given value.
101 ///
102 /// @param s the value of the interned string to look for.
103 ///
104 /// @return a pointer to the raw string of characters which has the
105 /// value of @p s. Or null if no string with value @p s was interned.
106 const char*
108 {
109  unordered_map<string, string*>::const_iterator i =
110  priv_->map.find(s);
111  if (i == priv_->map.end())
112  return 0;
113  if (i->second)
114  return i->second->c_str();
115  return "";
116 }
117 
118 /// Create an interned string with a given value.
119 ///
120 /// @param str_value the value of the interned string to create.
121 ///
122 /// @return the new created instance of @ref interned_string created.
124 interned_string_pool::create_string(const std::string& str_value)
125 {
126  string*& result = priv_->map[str_value];
127  if (!result && !str_value.empty())
128  result = new string(str_value);
129  return interned_string(result);
130 }
131 
132 /// Destructor.
134 {
135  for (pool_map_type::iterator i = priv_->map.begin();
136  i != priv_->map.end();
137  ++i)
138  if (i->second)
139  delete i->second;
140 }
141 
142 /// Equality operator.
143 ///
144 /// @param l the instance of std::string on the left-hand-side of the
145 /// equality operator.
146 ///
147 /// @param r the instance of @ref interned_string on the
148 /// right-hand-side of the equality operator.
149 ///
150 /// @return true iff the two string are equal.
151 bool
152 operator==(const std::string& l, const interned_string& r)
153 {return r.operator==(l);}
154 
155 bool
156 operator!=(const std::string& l, const interned_string& r)
157 {return !(l == r);}
158 
159 /// Streaming operator.
160 ///
161 /// Streams an instance of @ref interned_string to an output stream.
162 ///
163 /// @param o the destination output stream.
164 ///
165 /// @param s the instance of @ref interned_string to stream out.
166 ///
167 /// @return the output stream this function just streamed to.
168 std::ostream&
169 operator<<(std::ostream& o, const interned_string& s)
170 {
171  o << static_cast<std::string>(s);
172  return o;
173 }
174 
175 /// Concatenation operator.
176 ///
177 /// Concatenate two instances of @ref interned_string, builds an
178 /// instance of std::string with the resulting string and return it.
179 ///
180 /// @param s1 the first string to consider.
181 ///
182 /// @param s2 the second string to consider.
183 ///
184 /// @return the resuting concatenated string.
185 std::string
186 operator+(const interned_string& s1,const std::string& s2)
187 {return static_cast<std::string>(s1) + s2;}
188 
189 /// Concatenation operator.
190 ///
191 /// Concatenate two instances of @ref interned_string, builds an
192 /// instance of std::string with the resulting string and return it.
193 ///
194 /// @param s1 the first string to consider.
195 ///
196 /// @param s2 the second string to consider.
197 ///
198 /// @return the resuting concatenated string.
199 std::string
200 operator+(const std::string& s1, const interned_string& s2)
201 {return s1 + static_cast<std::string>(s2);}
202 
203 namespace ir
204 {
205 
206 static size_t
207 hash_as_canonical_type_or_constant(const type_base *t);
208 
209 static bool
210 has_generic_anonymous_internal_type_name(const decl_base *d);
211 
212 static interned_string
213 get_generic_anonymous_internal_type_name(const decl_base *d);
214 
215 static string
216 get_internal_integral_type_name(const type_base*);
217 
218 static void
219 update_qualified_name(decl_base * d);
220 
221 static void
222 update_qualified_name(decl_base_sptr d);
223 
224 void
225 push_composite_type_comparison_operands(const type_base& left,
226  const type_base& right);
227 
228 void
229 pop_composite_type_comparison_operands(const type_base& left,
230  const type_base& right);
231 
232 bool
233 mark_dependant_types_compared_until(const type_base &r);
234 
235 /// Push a pair of operands on the stack of operands of the current
236 /// type comparison, during type canonicalization.
237 ///
238 /// For more information on this, please look at the description of
239 /// the environment::priv::right_type_comp_operands_ data member.
240 ///
241 /// @param left the left-hand-side comparison operand to push.
242 ///
243 /// @param right the right-hand-side comparison operand to push.
244 void
246  const type_base& right)
247 {
248  const environment& env = left.get_environment();
249  env.priv_->push_composite_type_comparison_operands(&left, &right);
250 }
251 
252 /// Pop a pair of operands from the stack of operands to the current
253 /// type comparison.
254 ///
255 /// For more information on this, please look at the description of
256 /// the environment::privright_type_comp_operands_ data member.
257 ///
258 /// @param left the left-hand-side comparison operand we expect to
259 /// pop from the top of the stack. If this doesn't match the
260 /// operand found on the top of the stack, the function aborts.
261 ///
262 /// @param right the right-hand-side comparison operand we expect to
263 /// pop from the bottom of the stack. If this doesn't match the
264 /// operand found on the top of the stack, the function aborts.
265 void
267  const type_base& right)
268 {
269  const environment& env = left.get_environment();
270  env.priv_->pop_composite_type_comparison_operands(&left, &right);
271 }
272 
273 /// In the stack of the current types being compared (as part of type
274 /// canonicalization), mark all the types that comes after a certain
275 /// one as NOT being eligible to the canonical type propagation
276 /// optimization.
277 ///
278 /// For a starter, please read about the @ref
279 /// OnTheFlyCanonicalization, aka, "canonical type propagation
280 /// optimization".
281 ///
282 /// To implement that optimization, we need, among other things to
283 /// maintain stack of the types (and their sub-types) being
284 /// currently compared as part of type canonicalization.
285 ///
286 /// Note that we only consider the type that is the right-hand-side
287 /// operand of the comparison because it's that one that is being
288 /// canonicalized and thus, that is not yet canonicalized.
289 ///
290 /// The reason why a type is deemed NON-eligible to the canonical
291 /// type propagation optimization is that it "depends" on
292 /// recursively present type. Let me explain.
293 ///
294 /// Suppose we have a type T that has sub-types named ST0 and ST1.
295 /// Suppose ST1 itself has a sub-type that is T itself. In this
296 /// case, we say that T is a recursive type, because it has T
297 /// (itself) as one of its sub-types:
298 ///
299 /// T
300 /// +-- ST0
301 /// |
302 /// +-- ST1
303 /// +
304 /// |
305 /// +-- T
306 ///
307 /// ST1 is said to "depend" on T because it has T as a sub-type.
308 /// But because T is recursive, then ST1 is said to depend on a
309 /// recursive type. Notice however that ST0 does not depend on any
310 /// recursive type.
311 ///
312 /// When we are at the point of comparing the sub-type T of ST1
313 /// against its counterpart, the stack of the right-hand-side
314 /// operands of the type canonicalization is going to look like
315 /// this:
316 ///
317 /// | T | ST1 |
318 ///
319 /// We don't add the type T to the stack as we detect that T was
320 /// already in there (recursive cycle).
321 ///
322 /// So, this function will basically mark ST1 as being NON-eligible
323 /// to being the target of canonical type propagation, by marking ST1
324 /// as being dependant on T.
325 ///
326 /// @param right the right-hand-side operand of the type comparison.
327 ///
328 /// @return true iff the operation was successful.
329 bool
331 {
332  const environment& env = r.get_environment();
334  return env.priv_->mark_dependant_types_compared_until(&r);
335  return false;
336 }
337 
338 /// @brief the location of a token represented in its simplest form.
339 /// Instances of this type are to be stored in a sorted vector, so the
340 /// type must have proper relational operators.
341 class expanded_location
342 {
343  string path_;
344  unsigned line_;
345  unsigned column_;
346 
347  expanded_location();
348 
349 public:
350 
351  friend class location_manager;
352 
353  expanded_location(const string& path, unsigned line, unsigned column)
354  : path_(path), line_(line), column_(column)
355  {}
356 
357  bool
358  operator==(const expanded_location& l) const
359  {
360  return (path_ == l.path_
361  && line_ == l.line_
362  && column_ && l.column_);
363  }
364 
365  bool
366  operator<(const expanded_location& l) const
367  {
368  if (path_ < l.path_)
369  return true;
370  else if (path_ > l.path_)
371  return false;
372 
373  if (line_ < l.line_)
374  return true;
375  else if (line_ > l.line_)
376  return false;
377 
378  return column_ < l.column_;
379  }
380 };
381 
382 /// Expand the location into a tripplet path, line and column number.
383 ///
384 /// @param path the output parameter where this function sets the
385 /// expanded path.
386 ///
387 /// @param line the output parameter where this function sets the
388 /// expanded line.
389 ///
390 /// @param column the ouptut parameter where this function sets the
391 /// expanded column.
392 void
393 location::expand(std::string& path, unsigned& line, unsigned& column) const
394 {
395  if (!get_location_manager())
396  {
397  // We don't have a location manager maybe because this location
398  // was just freshly instanciated. We still want to be able to
399  // expand to default values.
400  path = "";
401  line = 0;
402  column = 0;
403  return;
404  }
405  get_location_manager()->expand_location(*this, path, line, column);
406 }
407 
408 
409 /// Expand the location into a string.
410 ///
411 /// @return the string representing the location.
412 string
413 location::expand(void) const
414 {
415  string path, result;
416  unsigned line = 0, column = 0;
417  expand(path, line, column);
418 
419  std::ostringstream o;
420  o << path << ":" << line << ":" << column;
421  return o.str();
422 }
423 
424 struct location_manager::priv
425 {
426  /// This sorted vector contains the expanded locations of the tokens
427  /// coming from a given ABI Corpus. The index of a given expanded
428  /// location in the table gives us an integer that is used to build
429  /// instance of location types.
430  std::vector<expanded_location> locs;
431 };
432 
433 location_manager::location_manager()
434  : priv_(new location_manager::priv)
435 {}
436 
437 location_manager::~location_manager() = default;
438 
439 /// Insert the triplet representing a source locus into our internal
440 /// vector of location triplet. Return an instance of location type,
441 /// built from an integral type that represents the index of the
442 /// source locus triplet into our source locus table.
443 ///
444 /// @param file_path the file path of the source locus
445 /// @param line the line number of the source location
446 /// @param col the column number of the source location
447 location
448 location_manager::create_new_location(const std::string& file_path,
449  size_t line,
450  size_t col)
451 {
452  expanded_location l(file_path, line, col);
453 
454  // Just append the new expanded location to the end of the vector
455  // and return its index. Note that indexes start at 1.
456  priv_->locs.push_back(l);
457  return location(priv_->locs.size(), this);
458 }
459 
460 /// Given an instance of location type, return the triplet
461 /// {path,line,column} that represents the source locus. Note that
462 /// the location must have been previously created from the function
463 /// location_manager::create_new_location, otherwise this function yields
464 /// unexpected results, including possibly a crash.
465 ///
466 /// @param location the instance of location type to expand
467 /// @param path the resulting path of the source locus
468 /// @param line the resulting line of the source locus
469 /// @param column the resulting colum of the source locus
470 void
472  std::string& path,
473  unsigned& line,
474  unsigned& column) const
475 {
476  if (location.value_ == 0)
477  return;
478  expanded_location &l = priv_->locs[location.value_ - 1];
479  path = l.path_;
480  line = l.line_;
481  column = l.column_;
482 }
483 
484 typedef unordered_map<function_type_sptr,
485  bool,
487  type_shared_ptr_equal> fn_type_ptr_map;
488 
489 // <type_maps stuff>
490 
491 struct type_maps::priv
492 {
493  mutable istring_type_base_wptrs_map_type basic_types_;
494  mutable istring_type_base_wptrs_map_type class_types_;
495  mutable istring_type_base_wptrs_map_type union_types_;
496  mutable istring_type_base_wptrs_map_type enum_types_;
497  mutable istring_type_base_wptrs_map_type typedef_types_;
498  mutable istring_type_base_wptrs_map_type qualified_types_;
499  mutable istring_type_base_wptrs_map_type pointer_types_;
500  mutable istring_type_base_wptrs_map_type reference_types_;
501  mutable istring_type_base_wptrs_map_type array_types_;
502  mutable istring_type_base_wptrs_map_type subrange_types_;
503  mutable istring_type_base_wptrs_map_type function_types_;
504  mutable vector<type_base_wptr> sorted_types_;
505 }; // end struct type_maps::priv
506 
507 type_maps::type_maps()
508  : priv_(new priv)
509 {}
510 
511 type_maps::~type_maps() = default;
512 
513 /// Test if the type_maps is empty.
514 ///
515 /// @return true iff the type_maps is empty.
516 bool
518 {
519  return (basic_types().empty()
520  && class_types().empty()
521  && union_types().empty()
522  && enum_types().empty()
523  && typedef_types().empty()
524  && qualified_types().empty()
525  && pointer_types().empty()
526  && reference_types().empty()
527  && array_types().empty()
528  && subrange_types().empty()
529  && function_types().empty());
530 }
531 
532 /// Getter for the map that associates the name of a basic type to the
533 /// vector instances of type_decl_sptr that represents that type.
536 {return priv_->basic_types_;}
537 
538 /// Getter for the map that associates the name of a basic type to the
539 /// vector of instances of @ref type_decl_sptr that represents that
540 /// type.
543 {return priv_->basic_types_;}
544 
545 /// Getter for the map that associates the name of a class type to the
546 /// vector of instances of @ref class_decl_sptr that represents that
547 /// type.
550 {return priv_->class_types_;}
551 
552 /// Getter for the map that associates the name of a class type to the
553 /// vector of instances of @ref class_decl_sptr that represents that
554 /// type.
557 {return priv_->class_types_;}
558 
559 /// Getter for the map that associates the name of a union type to the
560 /// vector of instances of @ref union_decl_sptr that represents that
561 /// type.
564 {return priv_->union_types_;}
565 
566 /// Getter for the map that associates the name of a union type to the
567 /// vector of instances of @ref union_decl_sptr that represents that
568 /// type.
571 {return priv_->union_types_;}
572 
573 /// Getter for the map that associates the name of an enum type to the
574 /// vector of instances of @ref enum_type_decl_sptr that represents
575 /// that type.
578 {return priv_->enum_types_;}
579 
580 /// Getter for the map that associates the name of an enum type to the
581 /// vector of instances of @ref enum_type_decl_sptr that represents
582 /// that type.
585 {return priv_->enum_types_;}
586 
587 /// Getter for the map that associates the name of a typedef to the
588 /// vector of instances of @ref typedef_decl_sptr that represents tha
589 /// type.
592 {return priv_->typedef_types_;}
593 
594 /// Getter for the map that associates the name of a typedef to the
595 /// vector of instances of @ref typedef_decl_sptr that represents tha
596 /// type.
599 {return priv_->typedef_types_;}
600 
601 /// Getter for the map that associates the name of a qualified type to
602 /// the vector of instances of @ref qualified_type_def_sptr.
605 {return priv_->qualified_types_;}
606 
607 /// Getter for the map that associates the name of a qualified type to
608 /// the vector of instances of @ref qualified_type_def_sptr.
611 {return priv_->qualified_types_;}
612 
613 /// Getter for the map that associates the name of a pointer type to
614 /// the vector of instances of @ref pointer_type_def_sptr that
615 /// represents that type.
618 {return priv_->pointer_types_;}
619 
620 /// Getter for the map that associates the name of a pointer type to
621 /// the vector of instances of @ref pointer_type_def_sptr that
622 /// represents that type.
625 {return priv_->pointer_types_;}
626 
627 /// Getter for the map that associates the name of a reference type to
628 /// the vector of instances of @ref reference_type_def_sptr that
629 /// represents that type.
632 {return priv_->reference_types_;}
633 
634 /// Getter for the map that associates the name of a reference type to
635 /// the vector of instances of @ref reference_type_def_sptr that
636 /// represents that type.
639 {return priv_->reference_types_;}
640 
641 /// Getter for the map that associates the name of an array type to
642 /// the vector of instances of @ref array_type_def_sptr that
643 /// represents that type.
646 {return priv_->array_types_;}
647 
648 /// Getter for the map that associates the name of an array type to
649 /// the vector of instances of @ref array_type_def_sptr that
650 /// represents that type.
653 {return priv_->array_types_;}
654 
655 /// Getter for the map that associates the name of a subrange type to
656 /// the vector of instances of @ref array_type_def::subrange_sptr that
657 /// represents that type.
660 {return priv_->subrange_types_;}
661 
662 /// Getter for the map that associates the name of a subrange type to
663 /// the vector of instances of @ref array_type_def::subrange_sptr that
664 /// represents that type.
667 {return priv_->subrange_types_;}
668 
669 /// Getter for the map that associates the name of a function type to
670 /// the vector of instances of @ref function_type_sptr that represents
671 /// that type.
674 {return priv_->function_types_;}
675 
676 /// Getter for the map that associates the name of a function type to
677 /// the vector of instances of @ref function_type_sptr that represents
678 /// that type.
681 {return priv_->function_types_;}
682 
683 /// A comparison functor to compare/sort types based on their pretty
684 /// representations.
685 struct type_name_comp
686 {
687  /// Comparison operator for two instances of @ref type_base.
688  ///
689  /// This compares the two types by lexicographically comparing their
690  /// pretty representation.
691  ///
692  /// @param l the left-most type to compare.
693  ///
694  /// @param r the right-most type to compare.
695  ///
696  /// @return true iff @p l < @p r.
697  bool
698  operator()(type_base *l, type_base *r) const
699  {
700  if (l == 0 && r == 0)
701  return false;
702 
703  string l_repr = get_pretty_representation(l);
704  string r_repr = get_pretty_representation(r);
705  return l_repr < r_repr;
706  }
707 
708  /// Comparison operator for two instances of @ref type_base.
709  ///
710  /// This compares the two types by lexicographically comparing their
711  /// pretty representation.
712  ///
713  /// @param l the left-most type to compare.
714  ///
715  /// @param r the right-most type to compare.
716  ///
717  /// @return true iff @p l < @p r.
718  bool
719  operator()(const type_base_sptr &l, const type_base_sptr &r) const
720  {return operator()(l.get(), r.get());}
721 
722  /// Comparison operator for two instances of @ref type_base.
723  ///
724  /// This compares the two types by lexicographically comparing their
725  /// pretty representation.
726  ///
727  /// @param l the left-most type to compare.
728  ///
729  /// @param r the right-most type to compare.
730  ///
731  /// @return true iff @p l < @p r.
732  bool
733  operator()(const type_base_wptr &l, const type_base_wptr &r) const
734  {return operator()(type_base_sptr(l), type_base_sptr(r));}
735 }; // end struct type_name_comp
736 
737 #ifdef WITH_DEBUG_SELF_COMPARISON
738 
739 /// This is a function called when the ABG_RETURN* macros defined
740 /// below return false.
741 ///
742 /// The purpose of this function is to ease debugging. To know where
743 /// the equality functions first compare non-equal, we can just set a
744 /// breakpoint on this notify_equality_failed function and run the
745 /// equality functions. Because all the equality functions use the
746 /// ABG_RETURN* macros to return their values, this function is always
747 /// called when any of those equality function return false.
748 ///
749 /// @param l the first operand of the equality.
750 ///
751 /// @param r the second operand of the equality.
752 static void
753 notify_equality_failed(const type_or_decl_base &l __attribute__((unused)),
754  const type_or_decl_base &r __attribute__((unused)))
755 {}
756 
757 /// This is a function called when the ABG_RETURN* macros defined
758 /// below return false.
759 ///
760 /// The purpose of this function is to ease debugging. To know where
761 /// the equality functions first compare non-equal, we can just set a
762 /// breakpoint on this notify_equality_failed function and run the
763 /// equality functions. Because all the equality functions use the
764 /// ABG_RETURN* macros to return their values, this function is always
765 /// called when any of those equality function return false.
766 ///
767 /// @param l the first operand of the equality.
768 ///
769 /// @param r the second operand of the equality.
770 static void
771 notify_equality_failed(const type_or_decl_base *l __attribute__((unused)),
772  const type_or_decl_base *r __attribute__((unused)))
773 {}
774 
775 #define ABG_RETURN_EQUAL(l, r) \
776  do \
777  { \
778  if (l != r) \
779  notify_equality_failed(l, r); \
780  return (l == r); \
781  } \
782  while(false)
783 
784 
785 #define ABG_RETURN_FALSE \
786  do \
787  { \
788  notify_equality_failed(l, r); \
789  return false; \
790  } while(false)
791 
792 #define ABG_RETURN(value) \
793  do \
794  { \
795  if (value == false) \
796  notify_equality_failed(l, r); \
797  return value; \
798  } while (false)
799 
800 #else // WITH_DEBUG_SELF_COMPARISON
801 
802 #define ABG_RETURN_FALSE return false
803 #define ABG_RETURN(value) return (value)
804 #define ABG_RETURN_EQUAL(l, r) return ((l) == (r));
805 #endif
806 
807 /// Compare two types by comparing their canonical types if present.
808 ///
809 /// If the canonical types are not present (because the types have not
810 /// yet been canonicalized, for instance) then the types are compared
811 /// structurally.
812 ///
813 /// @param l the first type to take into account in the comparison.
814 ///
815 /// @param r the second type to take into account in the comparison.
816 template<typename T>
817 bool
818 try_canonical_compare(const T *l, const T *r)
819 {
820 #if WITH_DEBUG_TYPE_CANONICALIZATION
821  // We are debugging the canonicalization of a type down the stack.
822  // 'l' is a subtype of a canonical type and 'r' is a subtype of the
823  // type being canonicalized. We are at a point where we can compare
824  // 'l' and 'r' either using canonical comparison (if 'l' and 'r'
825  // have canonical types) or structural comparison.
826  //
827  // Because we are debugging the process of type canonicalization, we
828  // want to compare 'l' and 'r' canonically *AND* structurally. Both
829  // kinds of comparison should yield the same result, otherwise type
830  // canonicalization just failed for the subtype 'r' of the type
831  // being canonicalized.
832  //
833  // In concrete terms, this function is going to be called twice with
834  // the same pair {'l', 'r'} to compare: The first time with
835  // environment::priv_->use_canonical_type_comparison_ set to true,
836  // instructing us to compare them canonically, and the second time
837  // with that boolean set to false, instructing us to compare them
838  // structurally.
839  const environment&env = l->get_environment();
840  if (env.priv_->use_canonical_type_comparison_)
841  {
842  if (const type_base *lc = l->get_naked_canonical_type())
843  if (const type_base *rc = r->get_naked_canonical_type())
844  ABG_RETURN_EQUAL(lc, rc);
845  }
846  return equals(*l, *r, 0);
847 #else
848  if (const type_base *lc = l->get_naked_canonical_type())
849  if (const type_base *rc = r->get_naked_canonical_type())
850  ABG_RETURN_EQUAL(lc, rc);
851  return equals(*l, *r, 0);
852 #endif
853 
854 
855 }
856 
857 /// Detect if a recursive comparison cycle is detected while
858 /// structurally comparing two types (a.k.a member-wise comparison).
859 ///
860 /// @param l the left-hand-side operand of the current comparison.
861 ///
862 /// @param r the right-hand-side operand of the current comparison.
863 ///
864 /// @return true iff a comparison cycle is detected.
865 template<typename T>
866 bool
868 {
869  bool result = l.priv_->comparison_started(l, r);
870  return result ;
871 }
872 
873 /// Detect if a recursive comparison cycle is detected while
874 /// structurally comparing two @ref class_decl types.
875 ///
876 /// @param l the left-hand-side operand of the current comparison.
877 ///
878 /// @param r the right-hand-side operand of the current comparison.
879 ///
880 /// @return true iff a comparison cycle is detected.
881 template<>
882 bool
884 {
885  return is_comparison_cycle_detected(static_cast<const class_or_union&>(l),
886  static_cast<const class_or_union&>(r));
887 }
888 
889 /// This macro is to be used while comparing composite types that
890 /// might recursively refer to themselves. Comparing two such types
891 /// might get us into a cyle.
892 ///
893 /// Practically, if we detect that we are already into comparing 'l'
894 /// and 'r'; then, this is a cycle.
895 //
896 /// To break the cycle, we assume the result of the comparison is true
897 /// for now. Comparing the other sub-types of l & r will tell us later
898 /// if l & r are actually different or not.
899 ///
900 /// In the mean time, returning true from this macro should not be
901 /// used to propagate the canonical type of 'l' onto 'r' as we don't
902 /// know yet if l equals r. All the types that depend on l and r
903 /// can't (and that are in the comparison stack currently) can't have
904 /// their canonical type propagated either. So this macro disallows
905 /// canonical type propagation for those types that depend on a
906 /// recursively defined sub-type for now.
907 ///
908 /// @param l the left-hand-side operand of the comparison.
909 #define RETURN_TRUE_IF_COMPARISON_CYCLE_DETECTED(l, r) \
910  do \
911  { \
912  if (is_comparison_cycle_detected(l, r)) \
913  { \
914  mark_dependant_types_compared_until(r); \
915  return true; \
916  } \
917  } \
918  while(false)
919 
920 
921 /// Mark a pair of types as being compared.
922 ///
923 /// This is helpful to later detect recursive cycles in the comparison
924 /// stack.
925 ///
926 /// @param l the left-hand-side operand of the comparison.
927 ///
928 /// @parm r the right-hand-side operand of the comparison.
929 template<typename T>
930 void
932 {
933  l.priv_->mark_as_being_compared(l, r);
935 }
936 
937 /// Mark a pair of @ref class_decl types as being compared.
938 ///
939 /// This is helpful to later detect recursive cycles in the comparison
940 /// stack.
941 ///
942 /// @param l the left-hand-side operand of the comparison.
943 ///
944 /// @parm r the right-hand-side operand of the comparison.
945 template<>
946 void
948 {
949  return mark_types_as_being_compared(static_cast<const class_or_union&>(l),
950  static_cast<const class_or_union&>(r));
951 }
952 
953 /// Mark a pair of types as being not compared anymore.
954 ///
955 /// This is helpful to later detect recursive cycles in the comparison
956 /// stack.
957 ///
958 /// Note that the types must have been passed to
959 /// mark_types_as_being_compared prior to calling this function.
960 ///
961 /// @param l the left-hand-side operand of the comparison.
962 ///
963 /// @parm r the right-hand-side operand of the comparison.
964 template<typename T>
965 void
967 {
968  l.priv_->unmark_as_being_compared(l, r);
970 }
971 
972 /// Mark a pair of @ref class_decl types as being not compared
973 /// anymore.
974 ///
975 /// This is helpful to later detect recursive cycles in the comparison
976 /// stack.
977 ///
978 /// Note that the types must have been passed to
979 /// mark_types_as_being_compared prior to calling this function.
980 ///
981 /// @param l the left-hand-side operand of the comparison.
982 ///
983 /// @parm r the right-hand-side operand of the comparison.
984 template<>
985 void
987 {
988  return unmark_types_as_being_compared(static_cast<const class_or_union&>(l),
989  static_cast<const class_or_union&>(r));
990 }
991 
992 /// Return the result of the comparison of two (sub) types.
993 ///
994 /// The function does the necessary book keeping before returning the
995 /// result of the comparison of two (sub) types.
996 ///
997 /// The book-keeping done is in the following
998 /// areas:
999 ///
1000 /// * Management of the Canonical Type Propagation optimization
1001 /// * type comparison cycle detection
1002 ///
1003 /// @param l the left-hand-side operand of the type comparison
1004 ///
1005 /// @param r the right-hand-side operand of the type comparison
1006 ///
1007 /// @param propagate_canonical_type if true, it means the function
1008 /// performs the @ref OnTheFlyCanonicalization, aka, "canonical type
1009 /// propagation optimization".
1010 ///
1011 /// @param value the result of the comparison of @p l and @p r.
1012 ///
1013 /// @return the value @p value.
1014 template<typename T>
1015 bool
1016 return_comparison_result(T& l, T& r, bool value,
1017  bool propagate_canonical_type = true)
1018 {
1019  if (propagate_canonical_type && (value == true))
1020  maybe_propagate_canonical_type(l, r);
1021 
1023 
1024  const environment& env = l.get_environment();
1026  // We are instructed to perform the "canonical type propagation"
1027  // optimization, making 'r' to possibly get the canonical type of
1028  // 'l' if it has one. This mostly means that we are currently
1029  // canonicalizing the type that contain the subtype provided in
1030  // the 'r' argument.
1031  {
1032  if (value == true
1033  && (is_type(&r)->priv_->depends_on_recursive_type()
1034  || env.priv_->is_recursive_type(&r))
1035  && is_type(&r)->priv_->canonical_type_propagated()
1036  && !is_type(&r)->priv_->propagated_canonical_type_confirmed()
1037  && !env.priv_->right_type_comp_operands_.empty())
1038  {
1039  // Track the object 'r' for which the propagated canonical
1040  // type might be re-initialized if the current comparison
1041  // eventually fails.
1042  env.priv_->add_to_types_with_non_confirmed_propagated_ct(is_type(&r));
1043  }
1044  else if (value == true && env.priv_->right_type_comp_operands_.empty())
1045  {
1046  // The type provided in the 'r' argument is the type that is
1047  // being canonicalized; 'r' is not a mere subtype being
1048  // compared, it's the whole type being canonicalized. And
1049  // its canonicalization has just succeeded.
1050  //
1051  // Let's confirm the canonical type resulting from the
1052  // "canonical type propagation" optimization.
1053  env.priv_->confirm_ct_propagation(&r);
1054  }
1055  else if (value == true)
1056  // In any other case, we are not sure if propagated types
1057  // should be confirmed yet. So let's mark them as such.
1058  env.priv_->add_to_types_with_non_confirmed_propagated_ct(is_type(&r));
1059  else if (value == false)
1060  {
1061  // The comparison of the current sub-type failed. So all
1062  // the with non-confirmed propagated types (those in
1063  // env.prix_->types_with_non_confirmed_propagated_ct_)
1064  // should see their tentatively propagated canonical type
1065  // cancelled.
1066  env.priv_->cancel_all_non_confirmed_propagated_canonical_types();
1067  }
1068  }
1069 
1070  // If we reached this point with value == true and the stack of
1071  // types being compared is empty, then it means that the type pair
1072  // that was at the bottom of the stack is now fully compared.
1073  //
1074  // It follows that all types that were target of canonical type
1075  // propagation can now see their tentative canonical type be
1076  // confirmed for real.
1077  if (value == true
1078  && env.priv_->right_type_comp_operands_.empty()
1079  && !env.priv_->types_with_non_confirmed_propagated_ct_.empty())
1080  // So the comparison is completely done and there are some
1081  // types for which their propagated canonical type is sitll
1082  // considered not confirmed. As the comparison did yield true, we
1083  // shall now confirm the propagation for all those types.
1084  env.priv_->confirm_ct_propagation();
1085 
1086 #ifdef WITH_DEBUG_SELF_COMPARISON
1087  if (value == false && env.priv_->right_type_comp_operands_.empty())
1088  {
1089  for (const auto i : env.priv_->types_with_non_confirmed_propagated_ct_)
1090  {
1091  type_base *t = reinterpret_cast<type_base*>(i);
1092  env.priv_->check_abixml_canonical_type_propagation_during_self_comp(t);
1093  }
1094  }
1095 #endif
1096 
1097  ABG_RETURN(value);
1098 }
1099 
1100 #define CACHE_AND_RETURN_COMPARISON_RESULT(value) \
1101  do \
1102  { \
1103  bool res = return_comparison_result(l, r, value); \
1104  l.get_environment().priv_->cache_type_comparison_result(l, r, res); \
1105  return res; \
1106  } while (false)
1107 
1108 /// Cache the result of a comparison between too artifacts (l & r) and
1109 /// return immediately.
1110 ///
1111 /// @param value the value to cache.
1112 #define CACHE_COMPARISON_RESULT_AND_RETURN(value) \
1113  do \
1114  { \
1115  l.get_environment().priv_->cache_type_comparison_result(l, r, value); \
1116  return value; \
1117  } while (false)
1118 
1119 /// Getter of all types types sorted by their pretty representation.
1120 ///
1121 /// @return a sorted vector of all types sorted by their pretty
1122 /// representation.
1123 const vector<type_base_wptr>&
1125 {
1126  if (priv_->sorted_types_.empty())
1127  {
1128  istring_type_base_wptrs_map_type::const_iterator i;
1129  vector<type_base_wptr>::const_iterator j;
1130 
1131  for (i = basic_types().begin(); i != basic_types().end(); ++i)
1132  for (j = i->second.begin(); j != i->second.end(); ++j)
1133  priv_->sorted_types_.push_back(*j);
1134 
1135  for (i = class_types().begin(); i != class_types().end(); ++i)
1136  for (j = i->second.begin(); j != i->second.end(); ++j)
1137  priv_->sorted_types_.push_back(*j);
1138 
1139  for (i = union_types().begin(); i != union_types().end(); ++i)
1140  for (j = i->second.begin(); j != i->second.end(); ++j)
1141  priv_->sorted_types_.push_back(*j);
1142 
1143  for (i = enum_types().begin(); i != enum_types().end(); ++i)
1144  for (j = i->second.begin(); j != i->second.end(); ++j)
1145  priv_->sorted_types_.push_back(*j);
1146 
1147  for (i = typedef_types().begin(); i != typedef_types().end(); ++i)
1148  for (j = i->second.begin(); j != i->second.end(); ++j)
1149  priv_->sorted_types_.push_back(*j);
1150 
1151  type_name_comp comp;
1152  sort(priv_->sorted_types_.begin(), priv_->sorted_types_.end(), comp);
1153  }
1154 
1155  return priv_->sorted_types_;
1156 }
1157 
1158 // </type_maps stuff>
1159 
1160 // <translation_unit stuff>
1161 
1162 /// Constructor of translation_unit.
1163 ///
1164 /// @param env the environment of this translation unit. Please note
1165 /// that the life time of the environment must be greater than the
1166 /// life time of the translation unit because the translation uses
1167 /// resources that are allocated in the environment.
1168 ///
1169 /// @param path the location of the translation unit.
1170 ///
1171 /// @param address_size the size of addresses in the translation unit,
1172 /// in bits.
1173 translation_unit::translation_unit(const environment& env,
1174  const std::string& path,
1175  char address_size)
1176  : priv_(new priv(env))
1177 {
1178  priv_->path_ = path;
1179  priv_->address_size_ = address_size;
1180 }
1181 
1182 /// Getter of the the global scope of the translation unit.
1183 ///
1184 /// @return the global scope of the current translation unit. If
1185 /// there is not global scope allocated yet, this function creates one
1186 /// and returns it.
1187 const scope_decl_sptr&
1189 {
1190  return const_cast<translation_unit*>(this)->get_global_scope();
1191 }
1192 
1193 /// Getter of the the global scope of the translation unit.
1194 ///
1195 /// @return the global scope of the current translation unit. If
1196 /// there is not global scope allocated yet, this function creates one
1197 /// and returns it.
1200 {
1201  if (!priv_->global_scope_)
1202  {
1203  priv_->global_scope_.reset
1204  (new global_scope(const_cast<translation_unit*>(this)));
1205  priv_->global_scope_->set_translation_unit
1206  (const_cast<translation_unit*>(this));
1207  }
1208  return priv_->global_scope_;
1209 }
1210 
1211 /// Getter of the types of the current @ref translation_unit.
1212 ///
1213 /// @return the maps of the types of the translation unit.
1214 const type_maps&
1216 {return priv_->types_;}
1217 
1218 /// Getter of the types of the current @ref translation_unit.
1219 ///
1220 /// @return the maps of the types of the translation unit.
1221 type_maps&
1223 {return priv_->types_;}
1224 
1225 /// Get the vector of function types that are used in the current
1226 /// translation unit.
1227 ///
1228 /// @return the vector of function types that are used in the current
1229 /// translation unit.
1230 const vector<function_type_sptr>&
1232 {return priv_->live_fn_types_;}
1233 
1234 /// Getter of the environment of the current @ref translation_unit.
1235 ///
1236 /// @return the translation unit of the current translation unit.
1237 const environment&
1239 {return priv_->env_;}
1240 
1241 /// Getter of the language of the source code of the translation unit.
1242 ///
1243 /// @return the language of the source code.
1246 {return priv_->language_;}
1247 
1248 /// Setter of the language of the source code of the translation unit.
1249 ///
1250 /// @param l the new language.
1251 void
1253 {priv_->language_ = l;}
1254 
1255 
1256 /// Get the path of the current translation unit.
1257 ///
1258 /// This path is relative to the build directory of the translation
1259 /// unit as returned by translation_unit::get_compilation_dir_path.
1260 ///
1261 /// @return the relative path of the compilation unit associated to
1262 /// the current instance of translation_unit.
1263 //
1264 const std::string&
1266 {return priv_->path_;}
1267 
1268 /// Set the path associated to the current instance of
1269 /// translation_unit.
1270 ///
1271 /// This path is relative to the build directory of the translation
1272 /// unit as returned by translation_unit::get_compilation_dir_path.
1273 ///
1274 /// @param a_path the new relative path to set.
1275 void
1276 translation_unit::set_path(const string& a_path)
1277 {priv_->path_ = a_path;}
1278 
1279 
1280 /// Get the path of the directory that was 'current' when the
1281 /// translation unit was compiled.
1282 ///
1283 /// Note that the path returned by translation_unit::get_path is
1284 /// relative to the path returned by this function.
1285 ///
1286 /// @return the compilation directory for the current translation
1287 /// unit.
1288 const std::string&
1290 {return priv_->comp_dir_path_;}
1291 
1292 /// Set the path of the directory that was 'current' when the
1293 /// translation unit was compiled.
1294 ///
1295 /// Note that the path returned by translation_unit::get_path is
1296 /// relative to the path returned by this function.
1297 ///
1298 /// @param the compilation directory for the current translation unit.
1299 void
1301 {priv_->comp_dir_path_ = d;}
1302 
1303 /// Get the concatenation of the build directory and the relative path
1304 /// of the translation unit.
1305 ///
1306 /// @return the absolute path of the translation unit.
1307 const std::string&
1309 {
1310  if (priv_->abs_path_.empty())
1311  {
1312  string path;
1313  if (!priv_->path_.empty())
1314  {
1315  if (!priv_->comp_dir_path_.empty())
1316  {
1317  path = priv_->comp_dir_path_;
1318  path += "/";
1319  }
1320  path += priv_->path_;
1321  }
1322  priv_->abs_path_ = path;
1323  }
1324 
1325  return priv_->abs_path_;
1326 }
1327 
1328 /// Set the corpus this translation unit is a member of.
1329 ///
1330 /// Note that adding a translation unit to a @ref corpus automatically
1331 /// triggers a call to this member function.
1332 ///
1333 /// @param corpus the corpus.
1334 void
1336 {priv_->corp = c;}
1337 
1338 /// Get the corpus this translation unit is a member of.
1339 ///
1340 /// @return the parent corpus, or nil if this doesn't belong to any
1341 /// corpus yet.
1342 corpus*
1344 {return priv_->corp;}
1345 
1346 /// Get the corpus this translation unit is a member of.
1347 ///
1348 /// @return the parent corpus, or nil if this doesn't belong to any
1349 /// corpus yet.
1350 const corpus*
1352 {return const_cast<translation_unit*>(this)->get_corpus();}
1353 
1354 /// Getter of the location manager for the current translation unit.
1355 ///
1356 /// @return a reference to the location manager for the current
1357 /// translation unit.
1360 {return priv_->loc_mgr_;}
1361 
1362 /// const Getter of the location manager.
1363 ///
1364 /// @return a const reference to the location manager for the current
1365 /// translation unit.
1366 const location_manager&
1368 {return priv_->loc_mgr_;}
1369 
1370 /// Tests whether if the current translation unit contains ABI
1371 /// artifacts or not.
1372 ///
1373 /// @return true iff the current translation unit is empty.
1374 bool
1376 {
1377  if (!priv_->global_scope_)
1378  return true;
1379  return get_global_scope()->is_empty();
1380 }
1381 
1382 /// Getter of the address size in this translation unit.
1383 ///
1384 /// @return the address size, in bits.
1385 char
1387 {return priv_->address_size_;}
1388 
1389 /// Setter of the address size in this translation unit.
1390 ///
1391 /// @param a the new address size in bits.
1392 void
1394 {priv_->address_size_= a;}
1395 
1396 /// Getter of the 'is_constructed" flag. It says if the translation
1397 /// unit is fully constructed or not.
1398 ///
1399 /// This flag is important for cases when comparison might depend on
1400 /// if the translation unit is fully built or not. For instance, when
1401 /// reading types from DWARF, the virtual methods of a class are not
1402 /// necessarily fully constructed until we have reached the end of the
1403 /// translation unit. In that case, before we've reached the end of
1404 /// the translation unit, we might not take virtual functions into
1405 /// account when comparing classes.
1406 ///
1407 /// @return true if the translation unit is constructed.
1408 bool
1410 {return priv_->is_constructed_;}
1411 
1412 /// Setter of the 'is_constructed" flag. It says if the translation
1413 /// unit is fully constructed or not.
1414 ///
1415 /// This flag is important for cases when comparison might depend on
1416 /// if the translation unit is fully built or not. For instance, when
1417 /// reading types from DWARF, the virtual methods of a class are not
1418 /// necessarily fully constructed until we have reached the end of the
1419 /// translation unit. In that case, before we've reached the end of
1420 /// the translation unit, we might not take virtual functions into
1421 /// account when comparing classes.
1422 ///
1423 /// @param f true if the translation unit is constructed.
1424 void
1426 {priv_->is_constructed_ = f;}
1427 
1428 /// Compare the current translation unit against another one.
1429 ///
1430 /// @param other the other tu to compare against.
1431 ///
1432 /// @return true if the two translation units are equal, false
1433 /// otherwise.
1434 bool
1436 {
1437  if (get_address_size() != other.get_address_size())
1438  return false;
1439 
1440  return *get_global_scope() == *other.get_global_scope();
1441 }
1442 
1443 /// Inequality operator.
1444 ///
1445 /// @param o the instance of @ref translation_unit to compare the
1446 /// current instance against.
1447 ///
1448 /// @return true iff the current instance is different from @p o.
1449 bool
1451 {return ! operator==(o);}
1452 
1453 /// Ensure that the life time of a function type is bound to the life
1454 /// time of the current translation unit.
1455 ///
1456 /// @param ftype the function time which life time to bind to the life
1457 /// time of the current instance of @ref translation_unit. That is,
1458 /// it's onlyh when the translation unit is destroyed that the
1459 /// function type can be destroyed to.
1460 void
1462 {
1463  const environment& env = get_environment();
1464 
1465  const_cast<translation_unit*>(this)->priv_->live_fn_types_.push_back(ftype);
1466 
1467  interned_string repr = get_type_name(ftype);
1468  const_cast<translation_unit*>(this)->get_types().function_types()[repr].
1469  push_back(ftype);
1470 
1471  // The function type must be out of the same environment as its
1472  // translation unit.
1473  {
1474  const environment& e = ftype->get_environment();
1475  ABG_ASSERT(&env == &e);
1476  }
1477 
1478  if (const translation_unit* existing_tu = ftype->get_translation_unit())
1479  ABG_ASSERT(existing_tu == this);
1480  else
1481  ftype->set_translation_unit(const_cast<translation_unit*>(this));
1482 }
1483 
1484 /// This implements the ir_traversable_base::traverse virtual
1485 /// function.
1486 ///
1487 /// @param v the visitor used on the member nodes of the translation
1488 /// unit during the traversal.
1489 ///
1490 /// @return true if the entire type IR tree got traversed, false
1491 /// otherwise.
1492 bool
1494 {return get_global_scope()->traverse(v);}
1495 
1496 translation_unit::~translation_unit()
1497 {}
1498 
1499 /// Converts a translation_unit::language enumerator into a string.
1500 ///
1501 /// @param l the language enumerator to translate.
1502 ///
1503 /// @return the resulting string.
1504 string
1506 {
1507  switch (l)
1508  {
1509  case translation_unit::LANG_UNKNOWN:
1510  return "LANG_UNKNOWN";
1511  case translation_unit::LANG_Cobol74:
1512  return "LANG_Cobol74";
1513  case translation_unit::LANG_Cobol85:
1514  return "LANG_Cobol85";
1515  case translation_unit::LANG_C89:
1516  return "LANG_C89";
1517  case translation_unit::LANG_C99:
1518  return "LANG_C99";
1519  case translation_unit::LANG_C11:
1520  return "LANG_C11";
1521  case translation_unit::LANG_C:
1522  return "LANG_C";
1523  case translation_unit::LANG_C_plus_plus_11:
1524  return "LANG_C_plus_plus_11";
1525  case translation_unit::LANG_C_plus_plus_14:
1526  return "LANG_C_plus_plus_14";
1527  case translation_unit::LANG_C_plus_plus:
1528  return "LANG_C_plus_plus";
1529  case translation_unit::LANG_ObjC:
1530  return "LANG_ObjC";
1531  case translation_unit::LANG_ObjC_plus_plus:
1532  return "LANG_ObjC_plus_plus";
1533  case translation_unit::LANG_Fortran77:
1534  return "LANG_Fortran77";
1535  case translation_unit::LANG_Fortran90:
1536  return "LANG_Fortran90";
1537  case translation_unit::LANG_Fortran95:
1538  return "LANG_Fortran95";
1539  case translation_unit::LANG_Ada83:
1540  return "LANG_Ada83";
1541  case translation_unit::LANG_Ada95:
1542  return "LANG_Ada95";
1543  case translation_unit::LANG_Pascal83:
1544  return "LANG_Pascal83";
1545  case translation_unit::LANG_Modula2:
1546  return "LANG_Modula2";
1547  case translation_unit::LANG_Java:
1548  return "LANG_Java";
1549  case translation_unit::LANG_PLI:
1550  return "LANG_PLI";
1551  case translation_unit::LANG_UPC:
1552  return "LANG_UPC";
1553  case translation_unit::LANG_D:
1554  return "LANG_D";
1555  case translation_unit::LANG_Python:
1556  return "LANG_Python";
1557  case translation_unit::LANG_Go:
1558  return "LANG_Go";
1559  case translation_unit::LANG_Mips_Assembler:
1560  return "LANG_Mips_Assembler";
1561  default:
1562  return "LANG_UNKNOWN";
1563  }
1564 
1565  return "LANG_UNKNOWN";
1566 }
1567 
1568 /// Parse a string representing a language into a
1569 /// translation_unit::language enumerator into a string.
1570 ///
1571 /// @param l the string representing the language.
1572 ///
1573 /// @return the resulting translation_unit::language enumerator.
1576 {
1577  if (l == "LANG_Cobol74")
1578  return translation_unit::LANG_Cobol74;
1579  else if (l == "LANG_Cobol85")
1580  return translation_unit::LANG_Cobol85;
1581  else if (l == "LANG_C89")
1582  return translation_unit::LANG_C89;
1583  else if (l == "LANG_C99")
1584  return translation_unit::LANG_C99;
1585  else if (l == "LANG_C11")
1586  return translation_unit::LANG_C11;
1587  else if (l == "LANG_C")
1588  return translation_unit::LANG_C;
1589  else if (l == "LANG_C_plus_plus_11")
1590  return translation_unit::LANG_C_plus_plus_11;
1591  else if (l == "LANG_C_plus_plus_14")
1592  return translation_unit::LANG_C_plus_plus_14;
1593  else if (l == "LANG_C_plus_plus")
1594  return translation_unit::LANG_C_plus_plus;
1595  else if (l == "LANG_ObjC")
1596  return translation_unit::LANG_ObjC;
1597  else if (l == "LANG_ObjC_plus_plus")
1598  return translation_unit::LANG_ObjC_plus_plus;
1599  else if (l == "LANG_Fortran77")
1600  return translation_unit::LANG_Fortran77;
1601  else if (l == "LANG_Fortran90")
1602  return translation_unit::LANG_Fortran90;
1603  else if (l == "LANG_Fortran95")
1604  return translation_unit::LANG_Fortran95;
1605  else if (l == "LANG_Ada83")
1606  return translation_unit::LANG_Ada83;
1607  else if (l == "LANG_Ada95")
1608  return translation_unit::LANG_Ada95;
1609  else if (l == "LANG_Pascal83")
1610  return translation_unit::LANG_Pascal83;
1611  else if (l == "LANG_Modula2")
1612  return translation_unit::LANG_Modula2;
1613  else if (l == "LANG_Java")
1614  return translation_unit::LANG_Java;
1615  else if (l == "LANG_PLI")
1616  return translation_unit::LANG_PLI;
1617  else if (l == "LANG_UPC")
1618  return translation_unit::LANG_UPC;
1619  else if (l == "LANG_D")
1620  return translation_unit::LANG_D;
1621  else if (l == "LANG_Python")
1622  return translation_unit::LANG_Python;
1623  else if (l == "LANG_Go")
1624  return translation_unit::LANG_Go;
1625  else if (l == "LANG_Mips_Assembler")
1626  return translation_unit::LANG_Mips_Assembler;
1627 
1628  return translation_unit::LANG_UNKNOWN;
1629 }
1630 
1631 /// Test if a language enumerator designates the C language.
1632 ///
1633 /// @param l the language enumerator to consider.
1634 ///
1635 /// @return true iff @p l designates the C language.
1636 bool
1638 {
1639  return (l == translation_unit::LANG_C89
1640  || l == translation_unit::LANG_C99
1641  || l == translation_unit::LANG_C11
1642  || l == translation_unit::LANG_C);
1643 }
1644 
1645 /// Test if a language enumerator designates the C++ language.
1646 ///
1647 /// @param l the language enumerator to consider.
1648 ///
1649 /// @return true iff @p l designates the C++ language.
1650 bool
1652 {
1653  return (l == translation_unit::LANG_C_plus_plus_03
1654  || l == translation_unit::LANG_C_plus_plus_11
1655  || l == translation_unit::LANG_C_plus_plus_14
1656  || l == translation_unit::LANG_C_plus_plus);
1657 }
1658 
1659 /// Test if a language enumerator designates the Java language.
1660 ///
1661 /// @param l the language enumerator to consider.
1662 ///
1663 /// @return true iff @p l designates the Java language.
1664 bool
1666 {return l == translation_unit::LANG_Java;}
1667 
1668 /// Test if a language enumerator designates the Ada language.
1669 ///
1670 /// @param l the language enumerator to consider.
1671 ///
1672 /// @return true iff @p l designates the Ada language.
1673 bool
1675 {
1676  return (l == translation_unit::LANG_Ada83
1677  || l == translation_unit::LANG_Ada95);
1678 }
1679 
1680 /// A deep comparison operator for pointers to translation units.
1681 ///
1682 /// @param l the first translation unit to consider for the comparison.
1683 ///
1684 /// @param r the second translation unit to consider for the comparison.
1685 ///
1686 /// @return true if the two translation units are equal, false otherwise.
1687 bool
1689 {
1690  if (l.get() == r.get())
1691  return true;
1692 
1693  if (!!l != !!r)
1694  return false;
1695 
1696  return *l == *r;
1697 }
1698 
1699 /// A deep inequality operator for pointers to translation units.
1700 ///
1701 /// @param l the first translation unit to consider for the comparison.
1702 ///
1703 /// @param r the second translation unit to consider for the comparison.
1704 ///
1705 /// @return true iff the two translation units are different.
1706 bool
1708 {return !operator==(l, r);}
1709 
1710 // </translation_unit stuff>
1711 
1712 // <elf_symbol stuff>
1713 struct elf_symbol::priv
1714 {
1715  const environment& env_;
1716  size_t index_;
1717  size_t size_;
1718  string name_;
1719  elf_symbol::type type_;
1720  elf_symbol::binding binding_;
1721  elf_symbol::version version_;
1722  elf_symbol::visibility visibility_;
1723  bool is_defined_;
1724  // This flag below says if the symbol is a common elf symbol. In
1725  // relocatable files, a common symbol is a symbol defined in a
1726  // section of kind SHN_COMMON.
1727  //
1728  // Note that a symbol of kind STT_COMMON is also considered a common
1729  // symbol. Here is what the gABI says about STT_COMMON and
1730  // SHN_COMMON:
1731  //
1732  // Symbols with type STT_COMMON label uninitialized common
1733  // blocks. In relocatable objects, these symbols are not
1734  // allocated and must have the special section index SHN_COMMON
1735  // (see below). In shared objects and executables these symbols
1736  // must be allocated to some section in the defining object.
1737  //
1738  // In relocatable objects, symbols with type STT_COMMON are
1739  // treated just as other symbols with index SHN_COMMON. If the
1740  // link-editor allocates space for the SHN_COMMON symbol in an
1741  // output section of the object it is producing, it must
1742  // preserve the type of the output symbol as STT_COMMON.
1743  //
1744  // When the dynamic linker encounters a reference to a symbol
1745  // that resolves to a definition of type STT_COMMON, it may (but
1746  // is not required to) change its symbol resolution rules as
1747  // follows: instead of binding the reference to the first symbol
1748  // found with the given name, the dynamic linker searches for
1749  // the first symbol with that name with type other than
1750  // STT_COMMON. If no such symbol is found, it looks for the
1751  // STT_COMMON definition of that name that has the largest size.
1752  bool is_common_;
1753  bool is_in_ksymtab_;
1756  bool is_suppressed_;
1757  elf_symbol_wptr main_symbol_;
1758  elf_symbol_wptr next_alias_;
1759  elf_symbol_wptr next_common_instance_;
1760  string id_string_;
1761 
1762  priv(const environment& e)
1763  : env_(e),
1764  index_(),
1765  size_(),
1766  type_(elf_symbol::NOTYPE_TYPE),
1767  binding_(elf_symbol::GLOBAL_BINDING),
1768  visibility_(elf_symbol::DEFAULT_VISIBILITY),
1769  is_defined_(false),
1770  is_common_(false),
1771  is_in_ksymtab_(false),
1772  crc_(),
1773  namespace_(),
1774  is_suppressed_(false)
1775  {}
1776 
1777  priv(const environment& e,
1778  size_t i,
1779  size_t s,
1780  const string& n,
1781  elf_symbol::type t,
1783  bool d,
1784  bool c,
1785  const elf_symbol::version& ve,
1787  bool is_in_ksymtab,
1788  const abg_compat::optional<uint32_t>& crc,
1790  bool is_suppressed)
1791  : env_(e),
1792  index_(i),
1793  size_(s),
1794  name_(n),
1795  type_(t),
1796  binding_(b),
1797  version_(ve),
1798  visibility_(vi),
1799  is_defined_(d),
1800  is_common_(c),
1801  is_in_ksymtab_(is_in_ksymtab),
1802  crc_(crc),
1803  namespace_(ns),
1804  is_suppressed_(is_suppressed)
1805  {
1806  if (!is_common_)
1807  is_common_ = type_ == COMMON_TYPE;
1808  }
1809 }; // end struct elf_symbol::priv
1810 
1811 /// Constructor of the @ref elf_symbol type.
1812 ///
1813 /// Note that this constructor is private, so client code cannot use
1814 /// it to create instances of @ref elf_symbol. Rather, client code
1815 /// should use the @ref elf_symbol::create() function to create
1816 /// instances of @ref elf_symbol instead.
1817 ///
1818 /// @param e the environment we are operating from.
1819 ///
1820 /// @param i the index of the symbol in the (ELF) symbol table.
1821 ///
1822 /// @param s the size of the symbol.
1823 ///
1824 /// @param n the name of the symbol.
1825 ///
1826 /// @param t the type of the symbol.
1827 ///
1828 /// @param b the binding of the symbol.
1829 ///
1830 /// @param d true if the symbol is defined, false otherwise.
1831 ///
1832 /// @param c true if the symbol is a common symbol, false otherwise.
1833 ///
1834 /// @param ve the version of the symbol.
1835 ///
1836 /// @param vi the visibility of the symbol.
1837 ///
1838 /// @param crc the CRC (modversions) value of Linux Kernel symbols
1839 ///
1840 /// @param ns the namespace of Linux Kernel symbols, if any
1841 elf_symbol::elf_symbol(const environment& e,
1842  size_t i,
1843  size_t s,
1844  const string& n,
1845  type t,
1846  binding b,
1847  bool d,
1848  bool c,
1849  const version& ve,
1850  visibility vi,
1851  bool is_in_ksymtab,
1852  const abg_compat::optional<uint32_t>& crc,
1854  bool is_suppressed)
1855  : priv_(new priv(e,
1856  i,
1857  s,
1858  n,
1859  t,
1860  b,
1861  d,
1862  c,
1863  ve,
1864  vi,
1865  is_in_ksymtab,
1866  crc,
1867  ns,
1868  is_suppressed))
1869 {}
1870 
1871 /// Factory of instances of @ref elf_symbol.
1872 ///
1873 /// This is the function to use to create instances of @ref elf_symbol.
1874 ///
1875 /// @param e the environment we are operating from.
1876 ///
1877 /// @param i the index of the symbol in the (ELF) symbol table.
1878 ///
1879 /// @param s the size of the symbol.
1880 ///
1881 /// @param n the name of the symbol.
1882 ///
1883 /// @param t the type of the symbol.
1884 ///
1885 /// @param b the binding of the symbol.
1886 ///
1887 /// @param d true if the symbol is defined, false otherwise.
1888 ///
1889 /// @param c true if the symbol is a common symbol.
1890 ///
1891 /// @param ve the version of the symbol.
1892 ///
1893 /// @param vi the visibility of the symbol.
1894 ///
1895 /// @param crc the CRC (modversions) value of Linux Kernel symbols
1896 ///
1897 /// @param ns the namespace of Linux Kernel symbols, if any
1898 ///
1899 /// @return a (smart) pointer to a newly created instance of @ref
1900 /// elf_symbol.
1903  size_t i,
1904  size_t s,
1905  const string& n,
1906  type t,
1907  binding b,
1908  bool d,
1909  bool c,
1910  const version& ve,
1911  visibility vi,
1912  bool is_in_ksymtab,
1913  const abg_compat::optional<uint32_t>& crc,
1915  bool is_suppressed)
1916 {
1917  elf_symbol_sptr sym(new elf_symbol(e, i, s, n, t, b, d, c, ve, vi,
1918  is_in_ksymtab, crc, ns, is_suppressed));
1919  sym->priv_->main_symbol_ = sym;
1920  return sym;
1921 }
1922 
1923 /// Test textual equality between two symbols.
1924 ///
1925 /// Textual equality means that the aliases of the compared symbols
1926 /// are not taken into account. Only the name, type, and version of
1927 /// the symbols are compared.
1928 ///
1929 /// @return true iff the two symbols are textually equal.
1930 static bool
1931 textually_equals(const elf_symbol&l,
1932  const elf_symbol&r)
1933 {
1934  bool equals = (l.get_name() == r.get_name()
1935  && l.get_type() == r.get_type()
1936  && l.is_public() == r.is_public()
1937  && l.is_defined() == r.is_defined()
1938  && l.is_common_symbol() == r.is_common_symbol()
1939  && l.get_version() == r.get_version()
1940  && l.get_crc() == r.get_crc()
1941  && l.get_namespace() == r.get_namespace());
1942 
1943  if (equals && l.is_variable())
1944  // These are variable symbols. Let's compare their symbol size.
1945  // The symbol size in this case is the size taken by the storage
1946  // of the variable. If that size changes, then it's an ABI
1947  // change.
1948  equals = l.get_size() == r.get_size();
1949 
1950  return equals;
1951 }
1952 
1953 /// Getter of the environment used by the current instance of @ref
1954 /// elf_symbol.
1955 ///
1956 /// @return the enviroment used by the current instance of @ref elf_symbol.
1957 const environment&
1959 {return priv_->env_;}
1960 
1961 /// Getter for the index
1962 ///
1963 /// @return the index of the symbol.
1964 size_t
1966 {return priv_->index_;}
1967 
1968 /// Setter for the index.
1969 ///
1970 /// @param s the new index.
1971 void
1973 {priv_->index_ = s;}
1974 
1975 /// Getter for the name of the @ref elf_symbol.
1976 ///
1977 /// @return a reference to the name of the @ref symbol.
1978 const string&
1980 {return priv_->name_;}
1981 
1982 /// Setter for the name of the current intance of @ref elf_symbol.
1983 ///
1984 /// @param n the new name.
1985 void
1986 elf_symbol::set_name(const string& n)
1987 {
1988  priv_->name_ = n;
1989  priv_->id_string_.clear();
1990 }
1991 
1992 /// Getter for the type of the current instance of @ref elf_symbol.
1993 ///
1994 /// @return the type of the elf symbol.
1997 {return priv_->type_;}
1998 
1999 /// Setter for the type of the current instance of @ref elf_symbol.
2000 ///
2001 /// @param t the new symbol type.
2002 void
2004 {priv_->type_ = t;}
2005 
2006 /// Getter of the size of the symbol.
2007 ///
2008 /// @return the size of the symbol, in bytes.
2009 size_t
2011 {return priv_->size_;}
2012 
2013 /// Setter of the size of the symbol.
2014 ///
2015 /// @param size the new size of the symbol, in bytes.
2016 void
2018 {priv_->size_ = size;}
2019 
2020 /// Getter for the binding of the current instance of @ref elf_symbol.
2021 ///
2022 /// @return the binding of the symbol.
2025 {return priv_->binding_;}
2026 
2027 /// Setter for the binding of the current instance of @ref elf_symbol.
2028 ///
2029 /// @param b the new binding.
2030 void
2032 {priv_->binding_ = b;}
2033 
2034 /// Getter for the version of the current instanc of @ref elf_symbol.
2035 ///
2036 /// @return the version of the elf symbol.
2039 {return priv_->version_;}
2040 
2041 /// Setter for the version of the current instance of @ref elf_symbol.
2042 ///
2043 /// @param v the new version of the elf symbol.
2044 void
2046 {
2047  priv_->version_ = v;
2048  priv_->id_string_.clear();
2049 }
2050 
2051 /// Setter of the visibility of the current instance of @ref
2052 /// elf_symbol.
2053 ///
2054 /// @param v the new visibility of the elf symbol.
2055 void
2057 {priv_->visibility_ = v;}
2058 
2059 /// Getter of the visibility of the current instance of @ref
2060 /// elf_symbol.
2061 ///
2062 /// @return the visibility of the elf symbol.
2065 {return priv_->visibility_;}
2066 
2067 /// Test if the current instance of @ref elf_symbol is defined or not.
2068 ///
2069 /// @return true if the current instance of @ref elf_symbol is
2070 /// defined, false otherwise.
2071 bool
2073 {return priv_->is_defined_;}
2074 
2075 /// Sets a flag saying if the current instance of @ref elf_symbol is
2076 /// defined
2077 ///
2078 /// @param b the new value of the flag.
2079 void
2081 {priv_->is_defined_ = d;}
2082 
2083 /// Test if the current instance of @ref elf_symbol is public or not.
2084 ///
2085 /// This tests if the symbol is defined, has default or protected
2086 ///visibility, and either:
2087 /// - has global binding
2088 /// - has weak binding
2089 /// - or has a GNU_UNIQUE binding.
2090 ///
2091 /// return true if the current instance of @ref elf_symbol is public,
2092 /// false otherwise.
2093 bool
2095 {
2096  return (is_defined()
2097  && (get_binding() == GLOBAL_BINDING
2098  || get_binding() == WEAK_BINDING
2099  || get_binding() == GNU_UNIQUE_BINDING)
2100  && (get_visibility() == DEFAULT_VISIBILITY
2101  || get_visibility() == PROTECTED_VISIBILITY));
2102 }
2103 
2104 /// Test if the current instance of @ref elf_symbol is a function
2105 /// symbol or not.
2106 ///
2107 /// @return true if the current instance of @ref elf_symbol is a
2108 /// function symbol, false otherwise.
2109 bool
2111 {return get_type() == FUNC_TYPE || get_type() == GNU_IFUNC_TYPE;}
2112 
2113 /// Test if the current instance of @ref elf_symbol is a variable
2114 /// symbol or not.
2115 ///
2116 /// @return true if the current instance of @ref elf_symbol is a
2117 /// variable symbol, false otherwise.
2118 bool
2120 {return get_type() == OBJECT_TYPE || get_type() == TLS_TYPE;}
2121 
2122 /// Getter of the 'is-in-ksymtab' property.
2123 ///
2124 /// @return true iff the current symbol is in the Linux Kernel
2125 /// specific 'ksymtab' symbol table.
2126 bool
2128 {return priv_->is_in_ksymtab_;}
2129 
2130 /// Setter of the 'is-in-ksymtab' property.
2131 ///
2132 /// @param is_in_ksymtab this is true iff the current symbol is in the
2133 /// Linux Kernel specific 'ksymtab' symbol table.
2134 void
2136 {priv_->is_in_ksymtab_ = is_in_ksymtab;}
2137 
2138 /// Getter of the 'crc' property.
2139 ///
2140 /// @return the CRC (modversions) value for Linux Kernel symbols, if any
2143 {return priv_->crc_;}
2144 
2145 /// Setter of the 'crc' property.
2146 ///
2147 /// @param crc the new CRC (modversions) value for Linux Kernel symbols
2148 void
2150 {priv_->crc_ = crc;}
2151 
2152 /// Getter of the 'namespace' property.
2153 ///
2154 /// @return the namespace for Linux Kernel symbols, if any
2157 {return priv_->namespace_;}
2158 
2159 /// Setter of the 'namespace' property.
2160 ///
2161 /// @param ns the new namespace for Linux Kernel symbols, if any
2162 void
2164 {priv_->namespace_ = ns;}
2165 
2166 /// Getter for the 'is-suppressed' property.
2167 ///
2168 /// @return true iff the current symbol has been suppressed by a
2169 /// suppression specification that was provided in the context that
2170 /// led to the creation of the corpus this ELF symbol belongs to.
2171 bool
2173 {return priv_->is_suppressed_;}
2174 
2175 /// Setter for the 'is-suppressed' property.
2176 ///
2177 /// @param true iff the current symbol has been suppressed by a
2178 /// suppression specification that was provided in the context that
2179 /// led to the creation of the corpus this ELF symbol belongs to.
2180 void
2182 {priv_->is_suppressed_ = is_suppressed;}
2183 
2184 /// @name Elf symbol aliases
2185 ///
2186 /// An alias A for an elf symbol S is a symbol that is defined at the
2187 /// same address as S. S is chained to A through the
2188 /// elf_symbol::get_next_alias() method.
2189 ///
2190 /// When there are several aliases to a symbol, the main symbol is the
2191 /// the first symbol found in the symbol table for a given address.
2192 ///
2193 /// The alias chain is circular. That means if S is the main symbol
2194 /// and A is the alias, S is chained to A and A
2195 /// is chained back to the main symbol S. The last alias in an alias
2196 ///chain is always chained to the main symbol.
2197 ///
2198 /// Thus, when looping over the aliases of an elf_symbol A, detecting
2199 /// an alias that is equal to the main symbol should logically be a
2200 /// loop exit condition.
2201 ///
2202 /// Accessing and adding aliases for instances of elf_symbol is done
2203 /// through the member functions below.
2204 
2205 /// @{
2206 
2207 /// Get the main symbol of an alias chain.
2208 ///
2209 ///@return the main symbol.
2210 const elf_symbol_sptr
2212 {return priv_->main_symbol_.lock();}
2213 
2214 /// Get the main symbol of an alias chain.
2215 ///
2216 ///@return the main symbol.
2219 {return priv_->main_symbol_.lock();}
2220 
2221 /// Tests whether this symbol is the main symbol.
2222 ///
2223 /// @return true iff this symbol is the main symbol.
2224 bool
2226 {return get_main_symbol().get() == this;}
2227 
2228 /// Get the next alias of the current symbol.
2229 ///
2230 ///@return the alias, or NULL if there is no alias.
2233 {return priv_->next_alias_.lock();}
2234 
2235 
2236 /// Check if the current elf_symbol has an alias.
2237 ///
2238 ///@return true iff the current elf_symbol has an alias.
2239 bool
2241 {return bool(get_next_alias());}
2242 
2243 /// Get the number of aliases to this elf symbol
2244 ///
2245 /// @return the number of aliases to this elf symbol.
2246 int
2248 {
2249  int result = 0;
2250 
2251  for (elf_symbol_sptr a = get_next_alias();
2252  a && a.get() != get_main_symbol().get();
2253  a = a->get_next_alias())
2254  ++result;
2255 
2256  return result;
2257 }
2258 
2259 /// Add an alias to the current elf symbol.
2260 ///
2261 /// @param alias the new alias. Note that this elf_symbol should *NOT*
2262 /// have aliases prior to the invocation of this function.
2263 void
2265 {
2266  if (!alias)
2267  return;
2268 
2269  ABG_ASSERT(!alias->has_aliases());
2271 
2272  if (has_aliases())
2273  {
2274  elf_symbol_sptr last_alias;
2275  for (elf_symbol_sptr a = get_next_alias();
2276  a && !a->is_main_symbol();
2277  a = a->get_next_alias())
2278  {
2279  if (a->get_next_alias()->is_main_symbol())
2280  {
2281  ABG_ASSERT(last_alias == 0);
2282  last_alias = a;
2283  }
2284  }
2285  ABG_ASSERT(last_alias);
2286 
2287  last_alias->priv_->next_alias_ = alias;
2288  }
2289  else
2290  priv_->next_alias_ = alias;
2291 
2292  alias->priv_->next_alias_ = get_main_symbol();
2293  alias->priv_->main_symbol_ = get_main_symbol();
2294 }
2295 
2296 /// Update the main symbol for a group of aliased symbols
2297 ///
2298 /// If after the construction of the symbols (in order of discovery), the
2299 /// actual main symbol can be identified (e.g. as the symbol that actually is
2300 /// defined in the code), this method offers a way of updating the main symbol
2301 /// through one of the aliased symbols.
2302 ///
2303 /// For that, locate the new main symbol by name and update all references to
2304 /// the main symbol among the group of aliased symbols.
2305 ///
2306 /// @param name the name of the main symbol
2307 ///
2308 /// @return the new main elf_symbol
2310 elf_symbol::update_main_symbol(const std::string& name)
2311 {
2313  if (!has_aliases() || get_name() == name)
2314  return get_main_symbol();
2315 
2316  // find the new main symbol
2317  elf_symbol_sptr new_main;
2318  // we've already checked this; check the rest of the aliases
2319  for (elf_symbol_sptr a = get_next_alias(); a.get() != this;
2320  a = a->get_next_alias())
2321  if (a->get_name() == name)
2322  {
2323  new_main = a;
2324  break;
2325  }
2326 
2327  if (!new_main)
2328  return get_main_symbol();
2329 
2330  // now update all main symbol references
2331  priv_->main_symbol_ = new_main;
2332  for (elf_symbol_sptr a = get_next_alias(); a.get() != this;
2333  a = a->get_next_alias())
2334  a->priv_->main_symbol_ = new_main;
2335 
2336  return new_main;
2337 }
2338 
2339 /// Return true if the symbol is a common one.
2340 ///
2341 /// @return true iff the symbol is common.
2342 bool
2344 {return priv_->is_common_;}
2345 
2346 /// Return true if this common common symbol has other common instances.
2347 ///
2348 /// A common instance of a given common symbol is another common
2349 /// symbol with the same name. Those exist in relocatable files. The
2350 /// linker normally allocates all the instances into a common block in
2351 /// the final output file.
2352 ///
2353 /// Note that the current object must be a common symbol, otherwise,
2354 /// this function aborts.
2355 ///
2356 /// @return true iff the current common symbol has other common
2357 /// instances.
2358 bool
2360 {
2362  return bool(get_next_common_instance());
2363 }
2364 
2365 /// Get the next common instance of the current common symbol.
2366 ///
2367 /// A common instance of a given common symbol is another common
2368 /// symbol with the same name. Those exist in relocatable files. The
2369 /// linker normally allocates all the instances into a common block in
2370 /// the final output file.
2371 ///
2372 /// @return the next common instance, or nil if there is not any.
2375 {return priv_->next_common_instance_.lock();}
2376 
2377 /// Add a common instance to the current common elf symbol.
2378 ///
2379 /// Note that this symbol must be the main symbol. Being the main
2380 /// symbol means being the first common symbol to appear in the symbol
2381 /// table.
2382 ///
2383 /// @param common the other common instance to add.
2384 void
2386 {
2387  if (!common)
2388  return;
2389 
2390  ABG_ASSERT(!common->has_other_common_instances());
2393 
2395  {
2396  elf_symbol_sptr last_common_instance;
2398  c && (c.get() != get_main_symbol().get());
2399  c = c->get_next_common_instance())
2400  {
2401  if (c->get_next_common_instance().get() == get_main_symbol().get())
2402  {
2403  ABG_ASSERT(last_common_instance == 0);
2404  last_common_instance = c;
2405  }
2406  }
2407  ABG_ASSERT(last_common_instance);
2408 
2409  last_common_instance->priv_->next_common_instance_ = common;
2410  }
2411  else
2412  priv_->next_common_instance_ = common;
2413 
2414  common->priv_->next_common_instance_ = get_main_symbol();
2415  common->priv_->main_symbol_ = get_main_symbol();
2416 }
2417 
2418 /// Get a string that is representative of a given elf_symbol.
2419 ///
2420 /// If the symbol has a version, then the ID string is the
2421 /// concatenation of the name of the symbol, the '@' character, and
2422 /// the version of the symbol. If the version is the default version
2423 /// of the symbol then the '@' character is replaced by a "@@" string.
2424 ///
2425 /// Otherwise, if the symbol does not have any version, this function
2426 /// returns the name of the symbol.
2427 ///
2428 /// @return a the ID string.
2429 const string&
2431 {
2432  if (priv_->id_string_.empty())
2433  {
2434  string s = get_name ();
2435 
2436  if (!get_version().is_empty())
2437  {
2438  if (get_version().is_default())
2439  s += "@@";
2440  else
2441  s += "@";
2442  s += get_version().str();
2443  }
2444  priv_->id_string_ = s;
2445  }
2446 
2447  return priv_->id_string_;
2448 }
2449 
2450 /// From the aliases of the current symbol, lookup one with a given name.
2451 ///
2452 /// @param name the name of symbol alias we are looking for.
2453 ///
2454 /// @return the symbol alias that has the name @p name, or nil if none
2455 /// has been found.
2457 elf_symbol::get_alias_from_name(const string& name) const
2458 {
2459  if (name == get_name())
2460  return elf_symbol_sptr(priv_->main_symbol_);
2461 
2462  for (elf_symbol_sptr a = get_next_alias();
2463  a && a.get() != get_main_symbol().get();
2464  a = a->get_next_alias())
2465  if (a->get_name() == name)
2466  return a;
2467 
2468  return elf_symbol_sptr();
2469 }
2470 
2471 /// In the list of aliases of a given elf symbol, get the alias that
2472 /// equals this current symbol.
2473 ///
2474 /// @param other the elf symbol to get the potential aliases from.
2475 ///
2476 /// @return the alias of @p other that texually equals the current
2477 /// symbol, or nil if no alias textually equals the current symbol.
2480 {
2481  for (elf_symbol_sptr a = other.get_next_alias();
2482  a && a.get() != a->get_main_symbol().get();
2483  a = a->get_next_alias())
2484  if (textually_equals(*this, *a))
2485  return a;
2486  return elf_symbol_sptr();
2487 }
2488 
2489 /// Return a comma separated list of the id of the current symbol as
2490 /// well as the id string of its aliases.
2491 ///
2492 /// @param syms a map of all the symbols of the corpus the current
2493 /// symbol belongs to.
2494 ///
2495 /// @param include_symbol_itself if set to true, then the name of the
2496 /// current symbol is included in the list of alias names that is emitted.
2497 ///
2498 /// @return the string.
2499 string
2501  bool include_symbol_itself) const
2502 {
2503  string result;
2504 
2505  if (include_symbol_itself)
2506  result = get_id_string();
2507 
2508  vector<elf_symbol_sptr> aliases;
2509  compute_aliases_for_elf_symbol(*this, syms, aliases);
2510  if (!aliases.empty() && include_symbol_itself)
2511  result += ", ";
2512 
2513  for (vector<elf_symbol_sptr>::const_iterator i = aliases.begin();
2514  i != aliases.end();
2515  ++i)
2516  {
2517  if (i != aliases.begin())
2518  result += ", ";
2519  result += (*i)->get_id_string();
2520  }
2521  return result;
2522 }
2523 
2524 /// Return a comma separated list of the id of the current symbol as
2525 /// well as the id string of its aliases.
2526 ///
2527 /// @param include_symbol_itself if set to true, then the name of the
2528 /// current symbol is included in the list of alias names that is emitted.
2529 ///
2530 /// @return the string.
2531 string
2532 elf_symbol::get_aliases_id_string(bool include_symbol_itself) const
2533 {
2534  vector<elf_symbol_sptr> aliases;
2535  if (include_symbol_itself)
2536  aliases.push_back(get_main_symbol());
2537 
2538  for (elf_symbol_sptr a = get_next_alias();
2539  a && a.get() != get_main_symbol().get();
2540  a = a->get_next_alias())
2541  aliases.push_back(a);
2542 
2543  string result;
2544  for (vector<elf_symbol_sptr>::const_iterator i = aliases.begin();
2545  i != aliases.end();
2546  ++i)
2547  {
2548  if (i != aliases.begin())
2549  result += ", ";
2550  result += (*i)->get_id_string();
2551  }
2552 
2553  return result;
2554 }
2555 
2556 /// Given the ID of a symbol, get the name and the version of said
2557 /// symbol.
2558 ///
2559 /// @param id the symbol ID to consider.
2560 ///
2561 /// @param name the symbol name extracted from the ID. This is set
2562 /// only if the function returned true.
2563 ///
2564 /// @param ver the symbol version extracted from the ID.
2565 bool
2567  string& name,
2568  string& ver)
2569 {
2570  name.clear(), ver.clear();
2571 
2572  string::size_type i = id.find('@');
2573  if (i == string::npos)
2574  {
2575  name = id;
2576  return true;
2577  }
2578 
2579  name = id.substr(0, i);
2580  ++i;
2581 
2582  if (i >= id.size())
2583  return true;
2584 
2585  string::size_type j = id.find('@', i);
2586  if (j == string::npos)
2587  j = i;
2588  else
2589  ++j;
2590 
2591  if (j >= id.size())
2592  {
2593  ver = "";
2594  return true;
2595  }
2596 
2597  ver = id.substr(j);
2598  return true;
2599 }
2600 
2601 ///@}
2602 
2603 /// Test if two main symbols are textually equal, or, if they have
2604 /// aliases that are textually equal.
2605 ///
2606 /// @param other the symbol to compare against.
2607 ///
2608 /// @return true iff the current instance of elf symbol equals the @p
2609 /// other.
2610 bool
2612 {
2613  bool are_equal = textually_equals(*this, other);
2614  if (!are_equal)
2615  are_equal = bool(get_alias_which_equals(other));
2616  return are_equal;
2617 }
2618 
2619 /// Test if the current symbol aliases another one.
2620 ///
2621 /// @param o the other symbol to test against.
2622 ///
2623 /// @return true iff the current symbol aliases @p o.
2624 bool
2626 {
2627  if (*this == o)
2628  return true;
2629 
2630  if (get_main_symbol() == o.get_main_symbol())
2631  return true;
2632 
2633  for (elf_symbol_sptr a = get_next_alias();
2634  a && !a->is_main_symbol();
2635  a = a->get_next_alias())
2636  {
2637  if (o == *a)
2638  return true;
2639  }
2640  return false;
2641 }
2642 
2643 /// Equality operator for smart pointers to elf_symbol.
2644 ///
2645 /// @param lhs the first elf symbol to consider.
2646 ///
2647 /// @param rhs the second elf symbol to consider.
2648 ///
2649 /// @return true iff @p lhs equals @p rhs.
2650 bool
2652 {
2653  if (!!lhs != !!rhs)
2654  return false;
2655 
2656  if (!lhs)
2657  return true;
2658 
2659  return *lhs == *rhs;
2660 }
2661 
2662 /// Inequality operator for smart pointers to elf_symbol.
2663 ///
2664 /// @param lhs the first elf symbol to consider.
2665 ///
2666 /// @param rhs the second elf symbol to consider.
2667 ///
2668 /// @return true iff @p lhs is different from @p rhs.
2669 bool
2671 {return !operator==(lhs, rhs);}
2672 
2673 /// Test if two symbols alias.
2674 ///
2675 /// @param s1 the first symbol to consider.
2676 ///
2677 /// @param s2 the second symbol to consider.
2678 ///
2679 /// @return true if @p s1 aliases @p s2.
2680 bool
2682 {return s1.does_alias(s2) || s2.does_alias(s1);}
2683 
2684 void
2685 compute_aliases_for_elf_symbol(const elf_symbol& sym,
2686  const string_elf_symbols_map_type& symtab,
2687  vector<elf_symbol_sptr>& aliases)
2688 {
2689 
2690  if (elf_symbol_sptr a = sym.get_next_alias())
2691  for (; a && !a->is_main_symbol(); a = a->get_next_alias())
2692  aliases.push_back(a);
2693  else
2694  for (string_elf_symbols_map_type::const_iterator i = symtab.begin();
2695  i != symtab.end();
2696  ++i)
2697  for (elf_symbols::const_iterator j = i->second.begin();
2698  j != i->second.end();
2699  ++j)
2700  {
2701  if (**j == sym)
2702  for (elf_symbol_sptr s = (*j)->get_next_alias();
2703  s && !s->is_main_symbol();
2704  s = s->get_next_alias())
2705  aliases.push_back(s);
2706  else
2707  for (elf_symbol_sptr s = (*j)->get_next_alias();
2708  s && !s->is_main_symbol();
2709  s = s->get_next_alias())
2710  if (*s == sym)
2711  aliases.push_back(*j);
2712  }
2713 }
2714 
2715 /// Test if two symbols alias.
2716 ///
2717 /// @param s1 the first symbol to consider.
2718 ///
2719 /// @param s2 the second symbol to consider.
2720 ///
2721 /// @return true if @p s1 aliases @p s2.
2722 bool
2724 {
2725  if (!!s1 != !!s2)
2726  return false;
2727  if (s1 == s2)
2728  return true;
2729  return elf_symbols_alias(*s1, *s2);
2730 }
2731 
2732 /// Test if two symbols alias.
2733 ///
2734 /// @param s1 the first symbol to consider.
2735 ///
2736 /// @param s2 the second symbol to consider.
2737 ///
2738 /// @return true if @p s1 aliases @p s2.
2739 bool
2741 {return elf_symbols_alias(s1.get(), s2.get());}
2742 
2743 /// Serialize an instance of @ref symbol_type and stream it to a given
2744 /// output stream.
2745 ///
2746 /// @param o the output stream to serialize the symbole type to.
2747 ///
2748 /// @param t the symbol type to serialize.
2749 std::ostream&
2750 operator<<(std::ostream& o, elf_symbol::type t)
2751 {
2752  string repr;
2753 
2754  switch (t)
2755  {
2756  case elf_symbol::NOTYPE_TYPE:
2757  repr = "unspecified symbol type";
2758  break;
2759  case elf_symbol::OBJECT_TYPE:
2760  repr = "variable symbol type";
2761  break;
2762  case elf_symbol::FUNC_TYPE:
2763  repr = "function symbol type";
2764  break;
2765  case elf_symbol::SECTION_TYPE:
2766  repr = "section symbol type";
2767  break;
2768  case elf_symbol::FILE_TYPE:
2769  repr = "file symbol type";
2770  break;
2771  case elf_symbol::COMMON_TYPE:
2772  repr = "common data object symbol type";
2773  break;
2774  case elf_symbol::TLS_TYPE:
2775  repr = "thread local data object symbol type";
2776  break;
2777  case elf_symbol::GNU_IFUNC_TYPE:
2778  repr = "indirect function symbol type";
2779  break;
2780  default:
2781  {
2782  std::ostringstream s;
2783  s << "unknown symbol type (" << (char)t << ')';
2784  repr = s.str();
2785  }
2786  break;
2787  }
2788 
2789  o << repr;
2790  return o;
2791 }
2792 
2793 /// Serialize an instance of @ref symbol_binding and stream it to a
2794 /// given output stream.
2795 ///
2796 /// @param o the output stream to serialize the symbole type to.
2797 ///
2798 /// @param b the symbol binding to serialize.
2799 std::ostream&
2800 operator<<(std::ostream& o, elf_symbol::binding b)
2801 {
2802  string repr;
2803 
2804  switch (b)
2805  {
2806  case elf_symbol::LOCAL_BINDING:
2807  repr = "local binding";
2808  break;
2809  case elf_symbol::GLOBAL_BINDING:
2810  repr = "global binding";
2811  break;
2812  case elf_symbol::WEAK_BINDING:
2813  repr = "weak binding";
2814  break;
2815  case elf_symbol::GNU_UNIQUE_BINDING:
2816  repr = "GNU unique binding";
2817  break;
2818  default:
2819  {
2820  std::ostringstream s;
2821  s << "unknown binding (" << (unsigned char) b << ")";
2822  repr = s.str();
2823  }
2824  break;
2825  }
2826 
2827  o << repr;
2828  return o;
2829 }
2830 
2831 /// Serialize an instance of @ref elf_symbol::visibility and stream it
2832 /// to a given output stream.
2833 ///
2834 /// @param o the output stream to serialize the symbole type to.
2835 ///
2836 /// @param v the symbol visibility to serialize.
2837 std::ostream&
2838 operator<<(std::ostream& o, elf_symbol::visibility v)
2839 {
2840  string repr;
2841 
2842  switch (v)
2843  {
2844  case elf_symbol::DEFAULT_VISIBILITY:
2845  repr = "default visibility";
2846  break;
2847  case elf_symbol::PROTECTED_VISIBILITY:
2848  repr = "protected visibility";
2849  break;
2850  case elf_symbol::HIDDEN_VISIBILITY:
2851  repr = "hidden visibility";
2852  break;
2853  case elf_symbol::INTERNAL_VISIBILITY:
2854  repr = "internal visibility";
2855  break;
2856  default:
2857  {
2858  std::ostringstream s;
2859  s << "unknown visibility (" << (unsigned char) v << ")";
2860  repr = s.str();
2861  }
2862  break;
2863  }
2864 
2865  o << repr;
2866  return o;
2867 }
2868 
2869 /// Convert a string representing a symbol type into an
2870 /// elf_symbol::type.
2871 ///
2872 ///@param s the string to convert.
2873 ///
2874 ///@param t the resulting elf_symbol::type.
2875 ///
2876 /// @return true iff the conversion completed successfully.
2877 bool
2879 {
2880  if (s == "no-type")
2881  t = elf_symbol::NOTYPE_TYPE;
2882  else if (s == "object-type")
2883  t = elf_symbol::OBJECT_TYPE;
2884  else if (s == "func-type")
2885  t = elf_symbol::FUNC_TYPE;
2886  else if (s == "section-type")
2887  t = elf_symbol::SECTION_TYPE;
2888  else if (s == "file-type")
2889  t = elf_symbol::FILE_TYPE;
2890  else if (s == "common-type")
2891  t = elf_symbol::COMMON_TYPE;
2892  else if (s == "tls-type")
2893  t = elf_symbol::TLS_TYPE;
2894  else if (s == "gnu-ifunc-type")
2895  t = elf_symbol::GNU_IFUNC_TYPE;
2896  else
2897  return false;
2898 
2899  return true;
2900 }
2901 
2902 /// Convert a string representing a an elf symbol binding into an
2903 /// elf_symbol::binding.
2904 ///
2905 /// @param s the string to convert.
2906 ///
2907 /// @param b the resulting elf_symbol::binding.
2908 ///
2909 /// @return true iff the conversion completed successfully.
2910 bool
2912 {
2913  if (s == "local-binding")
2914  b = elf_symbol::LOCAL_BINDING;
2915  else if (s == "global-binding")
2916  b = elf_symbol::GLOBAL_BINDING;
2917  else if (s == "weak-binding")
2918  b = elf_symbol::WEAK_BINDING;
2919  else if (s == "gnu-unique-binding")
2920  b = elf_symbol::GNU_UNIQUE_BINDING;
2921  else
2922  return false;
2923 
2924  return true;
2925 }
2926 
2927 /// Convert a string representing a an elf symbol visibility into an
2928 /// elf_symbol::visibility.
2929 ///
2930 /// @param s the string to convert.
2931 ///
2932 /// @param b the resulting elf_symbol::visibility.
2933 ///
2934 /// @return true iff the conversion completed successfully.
2935 bool
2937 {
2938  if (s == "default-visibility")
2939  v = elf_symbol::DEFAULT_VISIBILITY;
2940  else if (s == "protected-visibility")
2941  v = elf_symbol::PROTECTED_VISIBILITY;
2942  else if (s == "hidden-visibility")
2943  v = elf_symbol::HIDDEN_VISIBILITY;
2944  else if (s == "internal-visibility")
2945  v = elf_symbol::INTERNAL_VISIBILITY;
2946  else
2947  return false;
2948 
2949  return true;
2950 }
2951 
2952 /// Test if the type of an ELF symbol denotes a function symbol.
2953 ///
2954 /// @param t the type of the ELF symbol.
2955 ///
2956 /// @return true iff elf symbol type @p t denotes a function symbol
2957 /// type.
2958 bool
2960 {return t == elf_symbol::FUNC_TYPE;}
2961 
2962 /// Test if the type of an ELF symbol denotes a function symbol.
2963 ///
2964 /// @param t the type of the ELF symbol.
2965 ///
2966 /// @return true iff elf symbol type @p t denotes a function symbol
2967 /// type.
2968 bool
2970 {return t == elf_symbol::OBJECT_TYPE;}
2971 
2972 // <elf_symbol::version stuff>
2973 
2974 struct elf_symbol::version::priv
2975 {
2976  string version_;
2977  bool is_default_;
2978 
2979  priv()
2980  : is_default_(false)
2981  {}
2982 
2983  priv(const string& v,
2984  bool d)
2985  : version_(v),
2986  is_default_(d)
2987  {}
2988 }; // end struct elf_symbol::version::priv
2989 
2990 elf_symbol::version::version()
2991  : priv_(new priv)
2992 {}
2993 
2994 /// @param v the name of the version.
2995 ///
2996 /// @param is_default true if this is a default version.
2997 elf_symbol::version::version(const string& v,
2998  bool is_default)
2999  : priv_(new priv(v, is_default))
3000 {}
3001 
3002 elf_symbol::version::version(const elf_symbol::version& v)
3003  : priv_(new priv(v.str(), v.is_default()))
3004 {
3005 }
3006 
3007 elf_symbol::version::~version() = default;
3008 
3009 /// Cast the version_type into a string that is its name.
3010 ///
3011 /// @return the name of the version.
3012 elf_symbol::version::operator const string&() const
3013 {return priv_->version_;}
3014 
3015 /// Getter for the version name.
3016 ///
3017 /// @return the version name.
3018 const string&
3020 {return priv_->version_;}
3021 
3022 /// Setter for the version name.
3023 ///
3024 /// @param s the version name.
3025 void
3027 {priv_->version_ = s;}
3028 
3029 /// Getter for the 'is_default' property of the version.
3030 ///
3031 /// @return true iff this is a default version.
3032 bool
3034 {return priv_->is_default_;}
3035 
3036 /// Setter for the 'is_default' property of the version.
3037 ///
3038 /// @param f true if this is the default version.
3039 void
3041 {priv_->is_default_ = f;}
3042 
3043 bool
3044 elf_symbol::version::is_empty() const
3045 {return str().empty();}
3046 
3047 /// Compares the current version against another one.
3048 ///
3049 /// @param o the other version to compare the current one to.
3050 ///
3051 /// @return true iff the current version equals @p o.
3052 bool
3054 {return str() == o.str();}
3055 
3056 /// Inequality operator.
3057 ///
3058 /// @param o the version to compare against the current one.
3059 ///
3060 /// @return true iff both versions are different.
3061 bool
3063 {return !operator==(o);}
3064 
3065 /// Assign a version to the current one.
3066 ///
3067 /// @param o the other version to assign to this one.
3068 ///
3069 /// @return a reference to the assigned version.
3072 {
3073  str(o.str());
3074  is_default(o.is_default());
3075  return *this;
3076 }
3077 
3078 // </elf_symbol::version stuff>
3079 
3080 // </elf_symbol stuff>
3081 
3082 // <class dm_context_rel stuff>
3083 struct dm_context_rel::priv
3084 {
3085  bool is_laid_out_;
3086  size_t offset_in_bits_;
3087  var_decl* anonymous_data_member_;
3088 
3089  priv(bool is_static = false)
3090  : is_laid_out_(!is_static),
3091  offset_in_bits_(0),
3092  anonymous_data_member_()
3093  {}
3094 
3095  priv(bool is_laid_out, size_t offset_in_bits)
3096  : is_laid_out_(is_laid_out),
3097  offset_in_bits_(offset_in_bits),
3098  anonymous_data_member_()
3099  {}
3100 }; //end struct dm_context_rel::priv
3101 
3102 dm_context_rel::dm_context_rel()
3103  : context_rel(),
3104  priv_(new priv)
3105 {}
3106 
3107 dm_context_rel::dm_context_rel(scope_decl* s,
3108  bool is_laid_out,
3109  size_t offset_in_bits,
3110  access_specifier a,
3111  bool is_static)
3112  : context_rel(s, a, is_static),
3113  priv_(new priv(is_laid_out, offset_in_bits))
3114 {}
3115 
3116 dm_context_rel::dm_context_rel(scope_decl* s)
3117  : context_rel(s),
3118  priv_(new priv())
3119 {}
3120 
3121 bool
3122 dm_context_rel::get_is_laid_out() const
3123 {return priv_->is_laid_out_;}
3124 
3125 void
3126 dm_context_rel::set_is_laid_out(bool f)
3127 {priv_->is_laid_out_ = f;}
3128 
3129 size_t
3130 dm_context_rel::get_offset_in_bits() const
3131 {return priv_->offset_in_bits_;}
3132 
3133 void
3134 dm_context_rel::set_offset_in_bits(size_t o)
3135 {priv_->offset_in_bits_ = o;}
3136 
3137 bool
3138 dm_context_rel::operator==(const dm_context_rel& o) const
3139 {
3140  if (!context_rel::operator==(o))
3141  return false;
3142 
3143  return (priv_->is_laid_out_ == o.priv_->is_laid_out_
3144  && priv_->offset_in_bits_ == o.priv_->offset_in_bits_);
3145 }
3146 
3147 bool
3148 dm_context_rel::operator!=(const dm_context_rel& o) const
3149 {return !operator==(o);}
3150 
3151 /// Return a non-nil value if this data member context relationship
3152 /// has an anonymous data member. That means, if the data member this
3153 /// relation belongs to is part of an anonymous data member.
3154 ///
3155 /// @return the containing anonymous data member of this data member
3156 /// relationship. Nil if there is none.
3157 const var_decl*
3159 {return priv_->anonymous_data_member_;}
3160 
3161 /// Set the containing anonymous data member of this data member
3162 /// context relationship. That means that the data member this
3163 /// relation belongs to is part of an anonymous data member.
3164 ///
3165 /// @param anon_dm the containing anonymous data member of this data
3166 /// member relationship. Nil if there is none.
3167 void
3169 {priv_->anonymous_data_member_ = anon_dm;}
3170 
3171 dm_context_rel::~dm_context_rel()
3172 {}
3173 // </class dm_context_rel stuff>
3174 
3175 // <environment stuff>
3176 
3177 /// Convenience typedef for a map of interned_string -> bool.
3178 typedef unordered_map<interned_string,
3180 
3181 
3182 /// Default constructor of the @ref environment type.
3184  :priv_(new priv)
3185 {}
3186 
3187 /// Destructor for the @ref environment type.
3189 {}
3190 
3191 /// Getter the map of canonical types.
3192 ///
3193 /// @return the map of canonical types. The key of the map is the
3194 /// hash of the canonical type and its value if the canonical type.
3197 {return priv_->canonical_types_;}
3198 
3199 /// Getter the map of canonical types.
3200 ///
3201 /// @return the map of canonical types. The key of the map is the
3202 /// hash of the canonical type and its value if the canonical type.
3205 {return const_cast<environment*>(this)->get_canonical_types_map();}
3206 
3207 /// Helper to detect if a type is either a reference, a pointer, or a
3208 /// qualified type.
3209 static bool
3210 is_ptr_ref_or_qual_type(const type_base *t)
3211 {
3212  if (is_pointer_type(t)
3213  || is_reference_type(t)
3214  || is_qualified_type(t))
3215  return true;
3216  return false;
3217 }
3218 
3219 /// Compare decls using their locations.
3220 ///
3221 /// @param f the first decl to compare.
3222 ///
3223 /// @param s the second decl to compare.
3224 ///
3225 /// @return true if @p f compares less than @p s.
3226 static bool
3227 compare_using_locations(const decl_base *f,
3228  const decl_base *s)
3229 {
3230  // If a decl has artificial location, then use that one over the
3231  // natural one.
3234 
3235  ABG_ASSERT(fl.get_value() && sl.get_value());
3236  if (fl.get_is_artificial() == sl.get_is_artificial())
3237  {
3238  // The locations of the two artfifacts have the same
3239  // artificial-ness so they can be compared.
3240  string p1, p2;
3241  unsigned l1 = 0, l2 = 0, c1 = 0, c2 = 0;
3242  fl.expand(p1, l1, c1);
3243  sl.expand(p2, l2, c2);
3244  if (p1 != p2)
3245  return p1 < p2;
3246  if (l1 != l2)
3247  return l1 < l2;
3248  if (c1 != c2)
3249  return c1 < c2;
3250  }
3251 
3252  return (get_pretty_representation(f, /*internal=*/false)
3253  < get_pretty_representation(s, /*internal=*/false));
3254 }
3255 
3256 /// A functor to sort decls somewhat topologically. That is, types
3257 /// are sorted in a way that makes the ones that are defined "first"
3258 /// to come first.
3259 ///
3260 /// The topological criteria is a lexicographic sort of the definition
3261 /// location of the type. For types that have no location (or the
3262 /// same location), it's their qualified name that is used for the
3263 /// lexicographic sort.
3264 struct decl_topo_comp
3265 {
3266 
3267  /// The "Less Than" comparison operator of this functor.
3268  ///
3269  /// @param f the first decl to be considered for the comparison.
3270  ///
3271  /// @param s the second decl to be considered for the comparison.
3272  ///
3273  /// @return true iff @p f is less than @p s.
3274  bool
3275  operator()(const decl_base *f,
3276  const decl_base *s)
3277  {
3278  if (!!f != !!s)
3279  return f && !s;
3280 
3281  if (!f)
3282  return false;
3283 
3284  // If both decls come from an abixml file, keep the order they
3285  // have from that abixml file.
3286  if ((!f->get_corpus() && !s->get_corpus())
3287  || (f->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN
3288  && s->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN))
3289  return compare_using_locations(f, s);
3290 
3291  // If a decl has artificial location, then use that one over the
3292  // natural one.
3293  location fl = get_artificial_or_natural_location(f);
3294  location sl = get_artificial_or_natural_location(s);
3295 
3296  if (fl.get_value() && sl.get_value())
3297  return compare_using_locations(f, s);
3298  else if (!!fl != !!sl)
3299  // So one of the decls doesn't have location data.
3300  // The first decl is less than the second if it's the one not
3301  // having location data.
3302  return !fl && sl;
3303 
3304  // We reach this point if location data is useless.
3305  if (f->get_is_anonymous()
3306  && s->get_is_anonymous()
3307  && (get_pretty_representation(f, /*internal=*/false)
3308  == get_pretty_representation(s, /*internal=*/false)))
3309  return f->get_name() < s->get_name();
3310 
3311  return (get_pretty_representation(f, /*internal=*/false)
3312  < get_pretty_representation(s, /*internal=*/false));
3313  }
3314 
3315  /// The "Less Than" comparison operator of this functor.
3316  ///
3317  /// @param f the first decl to be considered for the comparison.
3318  ///
3319  /// @param s the second decl to be considered for the comparison.
3320  ///
3321  /// @return true iff @p f is less than @p s.
3322  bool
3323  operator()(const decl_base_sptr &f,
3324  const decl_base_sptr &s)
3325  {return operator()(f.get(), s.get());}
3326 
3327 }; // end struct decl_topo_comp
3328 
3329 /// A functor to sort types somewhat topologically. That is, types
3330 /// are sorted in a way that makes the ones that are defined "first"
3331 /// to come first.
3332 ///
3333 /// The topological criteria is a lexicographic sort of the definition
3334 /// location of the type. For types that have no location, it's their
3335 /// qualified name that is used for the lexicographic sort.
3336 struct type_topo_comp
3337 {
3338  /// Test if a decl has an artificial or natural location.
3339  ///
3340  /// @param d the decl to consider
3341  ///
3342  /// @return true iff @p d has a location.
3343  bool
3344  has_artificial_or_natural_location(const decl_base* d)
3346 
3347  /// Test if a type has an artificial or natural location.
3348  ///
3349  /// @param t the type to consider
3350  ///
3351  /// @return true iff @p t has a location.
3352  bool
3353  has_artificial_or_natural_location(const type_base* t)
3354  {
3355  if (decl_base *d = is_decl(t))
3356  return has_artificial_or_natural_location(d);
3357  return false;
3358  }
3359 
3360  /// The "Less Than" comparison operator of this functor.
3361  ///
3362  /// @param f the first type to be considered for the comparison.
3363  ///
3364  /// @param s the second type to be considered for the comparison.
3365  ///
3366  /// @return true iff @p f is less than @p s.
3367  bool
3368  operator()(const type_base_sptr &f,
3369  const type_base_sptr &s)
3370  {return operator()(f.get(), s.get());}
3371 
3372  /// The "Less Than" comparison operator of this functor.
3373  ///
3374  /// @param f the first type to be considered for the comparison.
3375  ///
3376  /// @param s the second type to be considered for the comparison.
3377  ///
3378  /// @return true iff @p f is less than @p s.
3379  bool
3380  operator()(const type_base *f,
3381  const type_base *s)
3382  {
3383  // If both decls come from an abixml file, keep the order they
3384  // have from that abixml file.
3385  if ((!f->get_corpus() && !s->get_corpus())
3386  || (f->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN
3387  && s->get_corpus()->get_origin() == corpus::NATIVE_XML_ORIGIN))
3388  return compare_using_locations(is_decl(f), is_decl(s));
3389 
3390  bool f_is_ptr_ref_or_qual = is_ptr_ref_or_qual_type(f);
3391  bool s_is_ptr_ref_or_qual = is_ptr_ref_or_qual_type(s);
3392 
3393  if (f_is_ptr_ref_or_qual != s_is_ptr_ref_or_qual)
3394  return !f_is_ptr_ref_or_qual && s_is_ptr_ref_or_qual;
3395 
3396  if (f_is_ptr_ref_or_qual && s_is_ptr_ref_or_qual
3397  && !has_artificial_or_natural_location(f)
3398  && !has_artificial_or_natural_location(s))
3399  {
3400  string s1 = get_pretty_representation(f, /*internal=*/false);
3401  string s2 = get_pretty_representation(s, /*internal=*/false);
3402  if (s1 == s2)
3403  {
3404  if (qualified_type_def * q = is_qualified_type(f))
3405  {
3406  if (q->get_cv_quals() == qualified_type_def::CV_NONE)
3407  if (!is_qualified_type(s))
3408  // We are looking at two types that are the result of
3409  // an optimization that happens during the IR
3410  // construction. Namely, type f is a cv-qualified
3411  // type with no qualifier (no const, no volatile, no
3412  // nothing, we call it an empty-qualified type).
3413  // These are the result of an optimization which
3414  // removes "redundant qualifiers" from some types.
3415  // For instance, consider a "const reference". The
3416  // const there is redundant because a reference is
3417  // always const. So as a result of the optimizaton
3418  // that type is going to be transformed into an
3419  // empty-qualified reference. If we don't make that
3420  // optimization, then we risk having spurious change
3421  // reports down the road. But then, as a consequence
3422  // of that optimization, we need to sort the
3423  // empty-qualified type and its non-qualified variant
3424  // e.g, to ensure stability in the abixml output; both
3425  // types are logically equal, but here, we decide that
3426  // the empty-qualified one is topologically "less
3427  // than" the non-qualified counterpart.
3428  //
3429  // So here, type f is an empty-qualified type and type
3430  // s is its non-qualified variant. We decide that f
3431  // is topologically less than s.
3432  return true;
3433  }
3434  // Now let's peel off the pointer (or reference types) and
3435  // see if the ultimate underlying types have the same
3436  // textual representation; if not, use that as sorting
3437  // criterion.
3438  type_base *peeled_f =
3440  type_base *peeled_s =
3442 
3443  s1 = get_pretty_representation(peeled_f, /*internal=*/false);
3444  s2 = get_pretty_representation(peeled_s, /*internal=*/false);
3445  if (s1 != s2)
3446  return s1 < s2;
3447 
3448  // The underlying type of pointer/reference have the same
3449  // textual representation; let's try to peel of typedefs
3450  // as well and we'll consider sorting the result as decls.
3451  peeled_f = peel_typedef_pointer_or_reference_type(peeled_f, true);
3452  peeled_s = peel_typedef_pointer_or_reference_type(peeled_s, true);
3453 
3454  s1 = get_pretty_representation(peeled_f, false);
3455  s2 = get_pretty_representation(peeled_s, false);
3456  if (s1 != s2)
3457  return s1 < s2;
3458  }
3459  }
3460 
3461  string s1 = get_pretty_representation(f, false);
3462  string s2 = get_pretty_representation(s, false);
3463 
3464  if (s1 != s2)
3465  return s1 < s2;
3466 
3467  if (is_typedef(f) && is_typedef(s))
3468  {
3469  s1 = get_pretty_representation(is_typedef(f)->get_underlying_type(),
3470  false);
3471  s2 = get_pretty_representation(is_typedef(s)->get_underlying_type(),
3472  false);
3473  if (s1 != s2)
3474  return s1 < s2;
3475  }
3476 
3477  type_base *peeled_f = peel_typedef_pointer_or_reference_type(f, true);
3478  type_base *peeled_s = peel_typedef_pointer_or_reference_type(s, true);
3479 
3480  s1 = get_pretty_representation(peeled_f, false);
3481  s2 = get_pretty_representation(peeled_s, false);
3482 
3483  if (s1 != s2)
3484  return s1 < s2;
3485 
3486  decl_base *fd = is_decl(f);
3487  decl_base *sd = is_decl(s);
3488 
3489  if (!!fd != !!sd)
3490  return fd && !sd;
3491 
3492  // If the two types have no decls, how come we could not sort them
3493  // until now? Let's investigate.
3494  ABG_ASSERT(fd);
3495 
3496  // From this point, fd and sd should be non-nil
3497  decl_topo_comp decl_comp;
3498  return decl_comp(fd, sd);
3499  }
3500 }; //end struct type_topo_comp
3501 
3502 /// Sort types in a hopefully stable manner.
3503 ///
3504 /// @param types a set of types with canonical types to sort.
3505 ///
3506 /// @param result the resulting sorted vector.
3507 void
3509  vector<type_base_sptr>& result)
3510 {
3511  for (auto t: types)
3512  result.push_back(t);
3513 
3514  type_topo_comp comp;
3515  std::stable_sort(result.begin(), result.end(), comp);
3516 }
3517 
3518 /// Get a @ref type_decl that represents a "void" type for the current
3519 /// environment.
3520 ///
3521 /// @return the @ref type_decl that represents a "void" type.
3522 const type_base_sptr&
3524 {
3525  if (!priv_->void_type_)
3526  priv_->void_type_.reset(new type_decl(*this,
3527  intern("void"),
3528  0, 0, location()));
3529  return priv_->void_type_;
3530 }
3531 
3532 /// Get a @ref type_decl instance that represents a the type of a
3533 /// variadic function parameter.
3534 ///
3535 /// @return the Get a @ref type_decl instance that represents a the
3536 /// type of a variadic function parameter.
3537 const type_base_sptr&
3539 {
3540  if (!priv_->variadic_marker_type_)
3541  priv_->variadic_marker_type_.
3543  0, 0, location()));
3544  return priv_->variadic_marker_type_;
3545 }
3546 
3547 /// Getter of the name of the variadic parameter type.
3548 ///
3549 /// @return the name of the variadic parameter type.
3550 string&
3552 {
3553  static string variadic_parameter_type_name = "variadic parameter type";
3554  return variadic_parameter_type_name;
3555 }
3556 
3557 /// Test if the canonicalization of types created out of the current
3558 /// environment is done.
3559 ///
3560 /// @return true iff the canonicalization of types created out of the current
3561 /// environment is done.
3562 bool
3564 {return priv_->canonicalization_is_done_;}
3565 
3566 /// Set a flag saying if the canonicalization of types created out of
3567 /// the current environment is done or not.
3568 ///
3569 /// Note that this function must only be called by internal code of
3570 /// the library that creates ABI artifacts (e.g, read an abi corpus
3571 /// from elf or from our own xml format and creates representations of
3572 /// types out of it) and thus needs to canonicalize types to speed-up
3573 /// further type comparison.
3574 ///
3575 /// @param f the new value of the flag.
3576 void
3578 {priv_->canonicalization_is_done_ = f;}
3579 
3580 /// Getter for the "on-the-fly-canonicalization" flag.
3581 ///
3582 /// @return true iff @ref OnTheFlyCanonicalization
3583 /// "on-the-fly-canonicalization" is to be performed during
3584 /// comparison.
3585 bool
3587 {return priv_->do_on_the_fly_canonicalization_;}
3588 
3589 /// Setter for the "on-the-fly-canonicalization" flag.
3590 ///
3591 /// @param f If this is true then @ref OnTheFlyCanonicalization
3592 /// "on-the-fly-canonicalization" is to be performed during
3593 /// comparison.
3594 void
3596 {priv_->do_on_the_fly_canonicalization_ = f;}
3597 
3598 /// Getter of the "decl-only-class-equals-definition" flag.
3599 ///
3600 /// Usually, a declaration-only class named 'struct foo' compares
3601 /// equal to any class definition named "struct foo'. This is at
3602 /// least true for C++.
3603 ///
3604 /// In C, though, because there can be multiple definitions of 'struct
3605 /// foo' in the binary, a declaration-only "struct foo" might be
3606 /// considered to *NOT* resolve to any of the struct foo defined. In
3607 /// that case, the declaration-only "struct foo" is considered
3608 /// different from the definitions.
3609 ///
3610 /// This flag controls the behaviour of the comparison of an
3611 /// unresolved decl-only class against a definition of the same name.
3612 ///
3613 /// If set to false, the the declaration equals the definition. If
3614 /// set to false, then the decalration is considered different from
3615 /// the declaration.
3616 ///
3617 /// @return the value of the "decl-only-class-equals-definition" flag.
3618 bool
3620 {return priv_->decl_only_class_equals_definition_;}
3621 
3622 /// Setter of the "decl-only-class-equals-definition" flag.
3623 ///
3624 /// Usually, a declaration-only class named 'struct foo' compares
3625 /// equal to any class definition named "struct foo'. This is at
3626 /// least true for C++.
3627 ///
3628 /// In C, though, because there can be multiple definitions of 'struct
3629 /// foo' in the binary, a declaration-only "struct foo" might be
3630 /// considered to *NOT* resolve to any of the struct foo defined. In
3631 /// that case, the declaration-only "struct foo" is considered
3632 /// different from the definitions.
3633 ///
3634 /// This flag controls the behaviour of the comparison of an
3635 /// unresolved decl-only class against a definition of the same name.
3636 ///
3637 /// If set to false, the the declaration equals the definition. If
3638 /// set to false, then the decalration is considered different from
3639 /// the declaration.
3640 ///
3641 /// @param the new value of the "decl-only-class-equals-definition"
3642 /// flag.
3643 void
3645 {priv_->decl_only_class_equals_definition_ = f;}
3646 
3647 /// Test if a given type is a void type as defined in the current
3648 /// environment.
3649 ///
3650 /// @param t the type to consider.
3651 ///
3652 /// @return true iff @p t is a void type as defined in the current
3653 /// environment.
3654 bool
3655 environment::is_void_type(const type_base_sptr& t) const
3656 {
3657  if (!t)
3658  return false;
3659  return t.get() == get_void_type().get();
3660 }
3661 
3662 /// Test if a given type is a void type as defined in the current
3663 /// environment.
3664 ///
3665 /// @param t the type to consider.
3666 ///
3667 /// @return true iff @p t is a void type as defined in the current
3668 /// environment.
3669 bool
3671 {
3672  if (!t)
3673  return false;
3674  return t == get_void_type().get();
3675 }
3676 
3677 /// Test if a type is a variadic parameter type as defined in the
3678 /// current environment.
3679 ///
3680 /// @param t the type to consider.
3681 ///
3682 /// @return true iff @p t is a variadic parameter type as defined in
3683 /// the current environment.
3684 bool
3686 {
3687  if (!t)
3688  return false;
3689  return t == get_variadic_parameter_type().get();
3690 }
3691 
3692 /// Test if a type is a variadic parameter type as defined in the
3693 /// current environment.
3694 ///
3695 /// @param t the type to consider.
3696 ///
3697 /// @return true iff @p t is a variadic parameter type as defined in
3698 /// the current environment.
3699 bool
3700 environment::is_variadic_parameter_type(const type_base_sptr& t) const
3701 {return is_variadic_parameter_type(t.get());}
3702 
3703 /// Do intern a string.
3704 ///
3705 /// If a value of this string already exists in the interned string
3706 /// pool of the current environment, then this function returns a new
3707 /// interned_string pointing to that already existing string.
3708 /// Otherwise, a new string is created, stored in the interned string
3709 /// pool and a new interned_string instance is created to point to
3710 /// that new intrerned string, and it's return.
3711 ///
3712 /// @param s the value of the string to intern.
3713 ///
3714 /// @return the interned string.
3716 environment::intern(const string& s) const
3717 {return const_cast<environment*>(this)->priv_->string_pool_.create_string(s);}
3718 
3719 /// Getter of the general configuration object.
3720 ///
3721 /// @return the configuration object.
3722 const config&
3724 {return priv_->config_;}
3725 
3726 /// Getter for a property that says if the user actually did set the
3727 /// analyze_exported_interfaces_only() property. If not, it means
3728 /// the default behaviour prevails.
3729 ///
3730 /// @return tru iff the user did set the
3731 /// analyze_exported_interfaces_only() property.
3732 bool
3734 {return priv_->analyze_exported_interfaces_only_.has_value();}
3735 
3736 /// Setter for the property that controls if we are to restrict the
3737 /// analysis to the types that are only reachable from the exported
3738 /// interfaces only, or if the set of types should be more broad than
3739 /// that. Typically, we'd restrict the analysis to types reachable
3740 /// from exported interfaces only (stricto sensu, that would really be
3741 /// only the types that are part of the ABI of well designed
3742 /// libraries) for performance reasons.
3743 ///
3744 /// @param f the value of the flag.
3745 void
3747 {priv_->analyze_exported_interfaces_only_ = f;}
3748 
3749 /// Getter for the property that controls if we are to restrict the
3750 /// analysis to the types that are only reachable from the exported
3751 /// interfaces only, or if the set of types should be more broad than
3752 /// that. Typically, we'd restrict the analysis to types reachable
3753 /// from exported interfaces only (stricto sensu, that would really be
3754 /// only the types that are part of the ABI of well designed
3755 /// libraries) for performance reasons.
3756 ///
3757 /// @param f the value of the flag.
3758 bool
3760 {return priv_->analyze_exported_interfaces_only_.value_or(false);}
3761 
3762 #ifdef WITH_DEBUG_SELF_COMPARISON
3763 /// Setter of the corpus of the input corpus of the self comparison
3764 /// that takes place when doing "abidw --debug-abidiff <binary>".
3765 ///
3766 /// The first invocation of this function sets the first corpus of the
3767 /// self comparison. The second invocation of this very same function
3768 /// sets the second corpus of the self comparison. That second corpus
3769 /// is supposed to come from the abixml serialization of the first
3770 /// corpus.
3771 ///
3772 /// @param c the corpus of the input binary or the corpus of the
3773 /// abixml serialization of the initial binary input.
3774 void
3775 environment::set_self_comparison_debug_input(const corpus_sptr& c)
3776 {
3777  self_comparison_debug_is_on(true);
3778  if (priv_->first_self_comparison_corpus_.expired())
3779  priv_->first_self_comparison_corpus_ = c;
3780  else if (priv_->second_self_comparison_corpus_.expired()
3781  && c.get() != corpus_sptr(priv_->first_self_comparison_corpus_).get())
3782  priv_->second_self_comparison_corpus_ = c;
3783 }
3784 
3785 /// Getter for the corpora of the input binary and the intermediate
3786 /// abixml of the self comparison that takes place when doing
3787 /// 'abidw --debug-abidiff <binary>'.
3788 ///
3789 /// @param first_corpus output parameter that is set to the corpus of
3790 /// the input corpus.
3791 ///
3792 /// @param second_corpus output parameter that is set to the corpus of
3793 /// the second corpus.
3794 void
3795 environment::get_self_comparison_debug_inputs(corpus_sptr& first_corpus,
3796  corpus_sptr& second_corpus)
3797 {
3798  first_corpus = priv_->first_self_comparison_corpus_.lock();
3799  second_corpus = priv_->second_self_comparison_corpus_.lock();
3800 }
3801 
3802 /// Turn on/off the self comparison debug mode.
3803 ///
3804 /// @param f true iff the self comparison debug mode is turned on.
3805 void
3806 environment::self_comparison_debug_is_on(bool f)
3807 {priv_->self_comparison_debug_on_ = f;}
3808 
3809 /// Test if we are in the process of the 'self-comparison
3810 /// debugging' as triggered by 'abidw --debug-abidiff' command.
3811 ///
3812 /// @return true if self comparison debug is on.
3813 bool
3814 environment::self_comparison_debug_is_on() const
3815 {return priv_->self_comparison_debug_on_;}
3816 #endif
3817 
3818 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
3819 /// Set the "type canonicalization debugging" mode, triggered by using
3820 /// the command: "abidw --debug-tc".
3821 ///
3822 /// @param flag if true then the type canonicalization debugging mode
3823 /// is enabled.
3824 void
3825 environment::debug_type_canonicalization_is_on(bool flag)
3826 {priv_->debug_type_canonicalization_ = flag;}
3827 
3828 /// Getter of the "type canonicalization debugging" mode, triggered by
3829 /// using the command: "abidw --debug-tc".
3830 ///
3831 /// @return true iff the type canonicalization debugging mode is
3832 /// enabled.
3833 bool
3834 environment::debug_type_canonicalization_is_on() const
3835 {return priv_->debug_type_canonicalization_;}
3836 
3837 /// Setter of the "DIE canonicalization debugging" mode, triggered by
3838 /// using the command: "abidw --debug-dc".
3839 ///
3840 /// @param flag true iff the DIE canonicalization debugging mode is
3841 /// enabled.
3842 void
3843 environment::debug_die_canonicalization_is_on(bool flag)
3844 {priv_->debug_die_canonicalization_ = flag;}
3845 
3846 /// Getter of the "DIE canonicalization debugging" mode, triggered by
3847 /// using the command: "abidw --debug-dc".
3848 ///
3849 /// @return true iff the DIE canonicalization debugging mode is
3850 /// enabled.
3851 bool
3852 environment::debug_die_canonicalization_is_on() const
3853 {return priv_->debug_die_canonicalization_;}
3854 #endif // WITH_DEBUG_TYPE_CANONICALIZATION
3855 
3856 /// Get the vector of canonical types which have a given "string
3857 /// representation".
3858 ///
3859 /// @param 'name', the textual representation of the type as returned
3860 /// by type_or_decl_base::get_pretty_representation(/*internal=*/true,
3861 /// /*qualified=*/true)
3862 ///
3863 /// This is useful to for debugging purposes as it's handy to use from
3864 /// inside a debugger like GDB.
3865 ///
3866 /// @return a pointer to the vector of canonical types having the
3867 /// representation @p name, or nullptr if no type with that
3868 /// representation exists.
3869 vector<type_base_sptr>*
3871 {
3872  auto ti = get_canonical_types_map().find(name);
3873  if (ti == get_canonical_types_map().end())
3874  return nullptr;
3875  return &ti->second;
3876 }
3877 
3878 /// Get a given canonical type which has a given "string
3879 /// representation".
3880 ///
3881 /// @param 'name', the textual representation of the type as returned
3882 /// by type_or_decl_base::get_pretty_representation(/*internal=*/true,
3883 /// /*qualified=*/true).
3884 ///
3885 /// @param index, the index of the type in the vector of types that
3886 /// all have the same textual representation @p 'name'. That vector
3887 /// is returned by the function environment::get_canonical_types().
3888 ///
3889 /// @return the canonical type which has the representation @p name,
3890 /// and which is at index @p index in the vector of canonical types
3891 /// having that same textual representation.
3892 type_base*
3893 environment::get_canonical_type(const char* name, unsigned index)
3894 {
3895  vector<type_base_sptr> *types = get_canonical_types(name);
3896  if (!types ||index >= types->size())
3897  return nullptr;
3898  return (*types)[index].get();
3899 }
3900 
3901 #ifdef WITH_DEBUG_SELF_COMPARISON
3902 /// Get the set of abixml type-id and the pointer value of the
3903 /// (canonical) type it's associated to.
3904 ///
3905 /// This is useful for debugging purposes, especially in the context
3906 /// of the use of the command:
3907 /// 'abidw --debug-abidiff <binary>'.
3908 ///
3909 /// @return the set of abixml type-id and the pointer value of the
3910 /// (canonical) type it's associated to.
3911 const unordered_map<string, uintptr_t>&
3912 environment::get_type_id_canonical_type_map() const
3913 {return priv_->get_type_id_canonical_type_map();}
3914 
3915 /// Get the set of abixml type-id and the pointer value of the
3916 /// (canonical) type it's associated to.
3917 ///
3918 /// This is useful for debugging purposes, especially in the context
3919 /// of the use of the command:
3920 /// 'abidw --debug-abidiff <binary>'.
3921 ///
3922 /// @return the set of abixml type-id and the pointer value of the
3923 /// (canonical) type it's associated to.
3924 unordered_map<string, uintptr_t>&
3925 environment::get_type_id_canonical_type_map()
3926 {return priv_->get_type_id_canonical_type_map();}
3927 
3928 /// Getter of the map that associates the values of type pointers to
3929 /// their type-id strings.
3930 ///
3931 /// Note that this map is populated at abixml reading time, (by
3932 /// build_type()) when a given XML element representing a type is
3933 /// read into a corresponding abigail::ir::type_base.
3934 ///
3935 /// This is used only for the purpose of debugging the
3936 /// self-comparison process. That is, when invoking "abidw
3937 /// --debug-abidiff".
3938 ///
3939 /// @return the map that associates the values of type pointers to
3940 /// their type-id strings.
3941 const unordered_map<uintptr_t, string>&
3942 environment::get_pointer_type_id_map() const
3943 {return priv_->get_pointer_type_id_map();}
3944 
3945 /// Getter of the map that associates the values of type pointers to
3946 /// their type-id strings.
3947 ///
3948 /// Note that this map is populated at abixml reading time, (by
3949 /// build_type()) when a given XML element representing a type is
3950 /// read into a corresponding abigail::ir::type_base.
3951 ///
3952 /// This is used only for the purpose of debugging the
3953 /// self-comparison process. That is, when invoking "abidw
3954 /// --debug-abidiff".
3955 ///
3956 /// @return the map that associates the values of type pointers to
3957 /// their type-id strings.
3958 unordered_map<uintptr_t, string>&
3959 environment::get_pointer_type_id_map()
3960 {return priv_->get_pointer_type_id_map();}
3961 
3962 /// Getter of the type-id that corresponds to the value of a pointer
3963 /// to abigail::ir::type_base that was created from the abixml reader.
3964 ///
3965 /// That value is retrieved from the map returned from
3966 /// environment::get_pointer_type_id_map().
3967 ///
3968 /// That map is populated at abixml reading time, (by build_type())
3969 /// when a given XML element representing a type is read into a
3970 /// corresponding abigail::ir::type_base.
3971 ///
3972 /// This is used only for the purpose of debugging the
3973 /// self-comparison process. That is, when invoking "abidw
3974 /// --debug-abidiff".
3975 ///
3976 /// @return the type-id strings that corresponds
3977 string
3978 environment::get_type_id_from_pointer(uintptr_t ptr) const
3979 {return priv_->get_type_id_from_pointer(ptr);}
3980 
3981 /// Getter of the type-id that corresponds to the value of an
3982 /// abigail::ir::type_base that was created from the abixml reader.
3983 ///
3984 /// That value is retrieved from the map returned from
3985 /// environment::get_pointer_type_id_map().
3986 ///
3987 /// That map is populated at abixml reading time, (by build_type())
3988 /// when a given XML element representing a type is read into a
3989 /// corresponding abigail::ir::type_base.
3990 ///
3991 /// This is used only for the purpose of debugging the
3992 /// self-comparison process. That is, when invoking "abidw
3993 /// --debug-abidiff".
3994 ///
3995 /// @return the type-id strings that corresponds
3996 string
3997 environment::get_type_id_from_type(const type_base *t) const
3998 {return priv_->get_type_id_from_type(t);}
3999 
4000 /// Getter of the canonical type of the artifact designated by a
4001 /// type-id.
4002 ///
4003 /// That type-id was generated by the abixml writer at the emitting
4004 /// time of the abixml file. The corresponding canonical type was
4005 /// stored in the map returned by
4006 /// environment::get_type_id_canonical_type_map().
4007 ///
4008 /// This is useful for debugging purposes, especially in the context
4009 /// of the use of the command:
4010 /// 'abidw --debug-abidiff <binary>'.
4011 ///
4012 /// @return the set of abixml type-id and the pointer value of the
4013 /// (canonical) type it's associated to.
4014 uintptr_t
4015 environment::get_canonical_type_from_type_id(const char* type_id) const
4016 {return priv_->get_canonical_type_from_type_id(type_id);}
4017 #endif
4018 
4019 // </environment stuff>
4020 
4021 // <type_or_decl_base stuff>
4022 
4023 /// The private data of @ref type_or_decl_base.
4024 struct type_or_decl_base::priv
4025 {
4026  // This holds the kind of dynamic type of particular instance.
4027  // Yes, this is part of the implementation of a "poor man" runtime
4028  // type identification. We are doing this because profiling shows
4029  // that using dynamic_cast in some places is really to slow and is
4030  // constituting a hotspot. This poor man's implementation made
4031  // things be much faster.
4032  enum type_or_decl_kind kind_;
4033  // This holds the runtime type instance pointer of particular
4034  // instance. In other words, this is the "this pointer" of the
4035  // dynamic type of a particular instance.
4036  void* rtti_;
4037  // This holds a pointer to either the type_base sub-object (if the
4038  // current instance is a type) or the decl_base sub-object (if the
4039  // current instance is a decl). This is used by the is_decl() and
4040  // is_type() functions, which also show up during profiling as
4041  // hotspots, due to their use of dynamic_cast.
4042  void* type_or_decl_ptr_;
4043  bool hashing_started_;
4044  const environment& env_;
4045  translation_unit* translation_unit_;
4046  // The location of an artifact as seen from its input by the
4047  // artifact reader. This might be different from the source
4048  // location advertised by the original emitter of the artifact
4049  // emitter.
4050  location artificial_location_;
4051  // Flags if the current ABI artifact is artificial (i.e, *NOT*
4052  // generated from the initial source code, but rather either
4053  // artificially by the compiler or by libabigail itself).
4054  bool is_artificial_;
4055 
4056  /// Constructor of the type_or_decl_base::priv private type.
4057  ///
4058  /// @param e the environment in which the ABI artifact was created.
4059  ///
4060  /// @param k the identifier of the runtime type of the current
4061  /// instance of ABI artifact.
4062  priv(const environment& e,
4063  enum type_or_decl_kind k = ABSTRACT_TYPE_OR_DECL)
4064  : kind_(k),
4065  rtti_(),
4066  type_or_decl_ptr_(),
4067  hashing_started_(),
4068  env_(e),
4069  translation_unit_(),
4070  is_artificial_()
4071  {}
4072 
4073  enum type_or_decl_kind
4074  kind() const
4075  {return kind_;}
4076 
4077  void
4078  kind (enum type_or_decl_kind k)
4079  {kind_ |= k;}
4080 }; // end struct type_or_decl_base::priv
4081 
4082 /// bitwise "OR" operator for the type_or_decl_base::type_or_decl_kind
4083 /// bitmap type.
4087 {
4088  return static_cast<type_or_decl_base::type_or_decl_kind>
4089  (static_cast<unsigned>(l) | static_cast<unsigned>(r));
4090 }
4091 
4092 /// bitwise "|=" operator for the type_or_decl_base::type_or_decl_kind
4093 /// bitmap type.
4097 {
4098  l = l | r;
4099  return l;
4100 }
4101 
4102 /// bitwise "AND" operator for the
4103 /// type_or_decl_base::type_or_decl_kind bitmap type.
4107 {
4108  return static_cast<type_or_decl_base::type_or_decl_kind>
4109  (static_cast<unsigned>(l) & static_cast<unsigned>(r));
4110 }
4111 
4112 /// bitwise "A&=" operator for the
4113 /// type_or_decl_base::type_or_decl_kind bitmap type.
4117 {
4118  l = l & r;
4119  return l;
4120 }
4121 
4122 /// Constructor of @ref type_or_decl_base.
4123 ///
4124 /// @param the environment the current ABI artifact is constructed
4125 /// from.
4126 ///
4127 /// @param k the runtime identifier bitmap of the type being built.
4128 type_or_decl_base::type_or_decl_base(const environment& e,
4129  enum type_or_decl_kind k)
4130  :priv_(new priv(e, k))
4131 {}
4132 
4133 /// The destructor of the @ref type_or_decl_base type.
4135 {}
4136 
4137 /// Getter of the flag that says if the artefact is artificial.
4138 ///
4139 /// Being artificial means it was not explicitely mentionned in the
4140 /// source code, but was rather artificially created by the compiler
4141 /// or libabigail.
4142 ///
4143 /// @return true iff the declaration is artificial.
4144 bool
4146 {return priv_->is_artificial_;}
4147 
4148 /// Setter of the flag that says if the artefact is artificial.
4149 ///
4150 /// Being artificial means the artefact was not explicitely
4151 /// mentionned in the source code, but was rather artificially created
4152 /// by the compiler or by libabigail.
4153 ///
4154 /// @param f the new value of the flag that says if the artefact is
4155 /// artificial.
4156 void
4158 {priv_->is_artificial_ = f;}
4159 
4160 /// Getter for the "kind" property of @ref type_or_decl_base type.
4161 ///
4162 /// This property holds the identifier bitmap of the runtime type of
4163 /// an ABI artifact.
4164 ///
4165 /// @return the runtime type identifier bitmap of the current ABI
4166 /// artifact.
4169 {return priv_->kind();}
4170 
4171 /// Setter for the "kind" property of @ref type_or_decl_base type.
4172 ///
4173 /// This property holds the identifier bitmap of the runtime type of
4174 /// an ABI artifact.
4175 ///
4176 /// @param the runtime type identifier bitmap of the current ABI
4177 /// artifact.
4178 void
4180 {priv_->kind(k);}
4181 
4182 /// Getter of the pointer to the runtime type sub-object of the
4183 /// current instance.
4184 ///
4185 /// @return the pointer to the runtime type sub-object of the current
4186 /// instance.
4187 const void*
4189 {return priv_->rtti_;}
4190 
4191 /// Getter of the pointer to the runtime type sub-object of the
4192 /// current instance.
4193 ///
4194 /// @return the pointer to the runtime type sub-object of the current
4195 /// instance.
4196 void*
4198 {return priv_->rtti_;}
4199 
4200 /// Setter of the pointer to the runtime type sub-object of the
4201 /// current instance.
4202 ///
4203 /// @param i the new pointer to the runtime type sub-object of the
4204 /// current instance.
4205 void
4207 {
4208  priv_->rtti_ = i;
4209  if (type_base* t = dynamic_cast<type_base*>(this))
4210  priv_->type_or_decl_ptr_ = t;
4211  else if (decl_base *d = dynamic_cast<decl_base*>(this))
4212  priv_->type_or_decl_ptr_ = d;
4213 }
4214 
4215 /// Getter of the pointer to either the type_base sub-object of the
4216 /// current instance if it's a type, or to the decl_base sub-object of
4217 /// the current instance if it's a decl.
4218 ///
4219 /// @return the pointer to either the type_base sub-object of the
4220 /// current instance if it's a type, or to the decl_base sub-object of
4221 /// the current instance if it's a decl.
4222 const void*
4224 {return const_cast<type_or_decl_base*>(this)->type_or_decl_base_pointer();}
4225 
4226 /// Getter of the pointer to either the type_base sub-object of the
4227 /// current instance if it's a type, or to the decl_base sub-object of
4228 /// the current instance if it's a decl.
4229 ///
4230 /// @return the pointer to either the type_base sub-object of the
4231 /// current instance if it's a type, or to the decl_base sub-object of
4232 /// the current instance if it's a decl.
4233 void*
4235 {return priv_->type_or_decl_ptr_;}
4236 
4237 /// Getter for the 'hashing_started' property.
4238 ///
4239 /// @return the 'hashing_started' property.
4240 bool
4242 {return priv_->hashing_started_;}
4243 
4244 /// Setter for the 'hashing_started' property.
4245 ///
4246 /// @param b the value to set the 'hashing_property' to.
4247 void
4249 {priv_->hashing_started_ = b;}
4250 
4251 /// Getter of the environment of the current ABI artifact.
4252 ///
4253 /// @return the environment of the artifact.
4254 const environment&
4256 {return priv_->env_;}
4257 
4258 /// Setter of the artificial location of the artificat.
4259 ///
4260 /// The artificial location is a location that was artificially
4261 /// generated by libabigail, not generated by the original emitter of
4262 /// the ABI meta-data. For instance, when reading an XML element from
4263 /// an abixml file, the artificial location is the source location of
4264 /// the XML element within the file, not the value of the
4265 /// 'location'property that might be carried by the element.
4266 ///
4267 /// Artificial locations might be useful to ensure that abixml emitted
4268 /// by the abixml writer are sorted the same way as the input abixml
4269 /// read by the reader.
4270 ///
4271 /// @param l the new artificial location.
4272 void
4274 {priv_->artificial_location_ = l;}
4275 
4276 /// Getter of the artificial location of the artifact.
4277 ///
4278 /// The artificial location is a location that was artificially
4279 /// generated by libabigail, not generated by the original emitter of
4280 /// the ABI meta-data. For instance, when reading an XML element from
4281 /// an abixml file, the artificial location is the source location of
4282 /// the XML element within the file, not the value of the
4283 /// 'location'property that might be carried by the element.
4284 ///
4285 /// Artificial locations might be useful to ensure that the abixml
4286 /// emitted by the abixml writer is sorted the same way as the input
4287 /// abixml read by the reader.
4288 ///
4289 /// @return the new artificial location.
4290 location&
4292 {return priv_->artificial_location_;}
4293 
4294 /// Test if the current ABI artifact carries an artificial location.
4295 ///
4296 /// @return true iff the current ABI artifact carries an artificial location.
4297 bool
4299 {
4300  return (priv_->artificial_location_
4301  && priv_->artificial_location_.get_is_artificial());
4302 }
4303 
4304 /// Get the @ref corpus this ABI artifact belongs to.
4305 ///
4306 /// @return the corpus this ABI artifact belongs to, or nil if it
4307 /// belongs to none for now.
4308 corpus*
4310 {
4312  if (!tu)
4313  return 0;
4314  return tu->get_corpus();
4315 }
4316 
4317 
4318 /// Get the @ref corpus this ABI artifact belongs to.
4319 ///
4320 /// @return the corpus this ABI artifact belongs to, or nil if it
4321 /// belongs to none for now.
4322 const corpus*
4324 {return const_cast<type_or_decl_base*>(this)->get_corpus();}
4325 
4326 /// Set the @ref translation_unit this ABI artifact belongs to.
4327 ///
4328 /// Note that adding an ABI artifact to a containining on should
4329 /// invoke this member function.
4330 void
4332 {priv_->translation_unit_ = tu;}
4333 
4334 
4335 /// Get the @ref translation_unit this ABI artifact belongs to.
4336 ///
4337 /// @return the translation unit this ABI artifact belongs to, or nil
4338 /// if belongs to none for now.
4341 {return priv_->translation_unit_;}
4342 
4343 /// Get the @ref translation_unit this ABI artifact belongs to.
4344 ///
4345 /// @return the translation unit this ABI artifact belongs to, or nil
4346 /// if belongs to none for now.
4347 const translation_unit*
4349 {return const_cast<type_or_decl_base*>(this)->get_translation_unit();}
4350 
4351 /// Traverse the the ABI artifact.
4352 ///
4353 /// @param v the visitor used to traverse the sub-tree nodes of the
4354 /// artifact.
4355 bool
4357 {return true;}
4358 
4359 /// Non-member equality operator for the @type_or_decl_base type.
4360 ///
4361 /// @param lr the left-hand operand of the equality.
4362 ///
4363 /// @param rr the right-hand operatnr of the equality.
4364 ///
4365 /// @return true iff @p lr equals @p rr.
4366 bool
4368 {
4369  const type_or_decl_base* l = &lr;
4370  const type_or_decl_base* r = &rr;
4371 
4372  const decl_base* dl = dynamic_cast<const decl_base*>(l),
4373  *dr = dynamic_cast<const decl_base*>(r);
4374 
4375  if (!!dl != !!dr)
4376  return false;
4377 
4378  if (dl && dr)
4379  return *dl == *dr;
4380 
4381  const type_base* tl = dynamic_cast<const type_base*>(l),
4382  *tr = dynamic_cast<const type_base*>(r);
4383 
4384  if (!!tl != !!tr)
4385  return false;
4386 
4387  if (tl && tr)
4388  return *tl == *tr;
4389 
4390  return false;
4391 }
4392 
4393 /// Non-member equality operator for the @type_or_decl_base type.
4394 ///
4395 /// @param l the left-hand operand of the equality.
4396 ///
4397 /// @param r the right-hand operatnr of the equality.
4398 ///
4399 /// @return true iff @p l equals @p r.
4400 bool
4402 {
4403  if (!! l != !!r)
4404  return false;
4405 
4406  if (!l)
4407  return true;
4408 
4409  return *r == *l;
4410 }
4411 
4412 /// Non-member inequality operator for the @type_or_decl_base type.
4413 ///
4414 /// @param l the left-hand operand of the equality.
4415 ///
4416 /// @param r the right-hand operator of the equality.
4417 ///
4418 /// @return true iff @p l is different from @p r.
4419 bool
4421 {return !operator==(l, r);}
4422 
4423 // </type_or_decl_base stuff>
4424 
4425 // <Decl definition>
4426 
4427 struct decl_base::priv
4428 {
4429  bool in_pub_sym_tab_;
4430  bool is_anonymous_;
4431  location location_;
4432  context_rel *context_;
4433  interned_string name_;
4434  interned_string qualified_parent_name_;
4435  // This temporary qualified name is the cache used for the qualified
4436  // name before the type associated to this decl (if applicable) is
4437  // canonicalized. Once the type is canonicalized, the cached use is
4438  // the data member qualified_parent_name_ above.
4439  interned_string temporary_qualified_name_;
4440  // This is the fully qualified name of the decl. It contains the
4441  // name of the decl and the qualified name of its scope. So if in
4442  // the parent scopes of the decl, there is one anonymous struct,
4443  // somewhere in the name, there is going to by an
4444  // __anonymous_struct__ string, even if the anonymous struct is not
4445  // the direct containing scope of this decl.
4446  interned_string qualified_name_;
4447  interned_string temporary_internal_qualified_name_;
4448  interned_string internal_qualified_name_;
4449  // Unline qualified_name_, scoped_name_ contains the name of the
4450  // decl and the name of its scope; not the qualified name of the
4451  // scope.
4452  interned_string scoped_name_;
4453  interned_string linkage_name_;
4454  visibility visibility_;
4455  decl_base_sptr declaration_;
4456  decl_base_wptr definition_of_declaration_;
4457  decl_base* naked_definition_of_declaration_;
4458  bool is_declaration_only_;
4459  typedef_decl_sptr naming_typedef_;
4460 
4461  priv()
4462  : in_pub_sym_tab_(false),
4463  is_anonymous_(true),
4464  context_(),
4465  visibility_(VISIBILITY_DEFAULT),
4466  naked_definition_of_declaration_(),
4467  is_declaration_only_(false)
4468  {}
4469 
4470  priv(interned_string name, interned_string linkage_name, visibility vis)
4471  : in_pub_sym_tab_(false),
4472  context_(),
4473  name_(name),
4474  qualified_name_(name),
4475  linkage_name_(linkage_name),
4476  visibility_(vis),
4477  naked_definition_of_declaration_(),
4478  is_declaration_only_(false)
4479  {
4480  is_anonymous_ = name_.empty();
4481  }
4482 
4483  ~priv()
4484  {
4485  delete context_;
4486  }
4487 };// end struct decl_base::priv
4488 
4489 /// Constructor for the @ref decl_base type.
4490 ///
4491 /// @param e the environment the current @ref decl_base is being
4492 /// created in.
4493 ///
4494 /// @param name the name of the declaration.
4495 ///
4496 /// @param locus the location where to find the declaration in the
4497 /// source code.
4498 ///
4499 /// @param linkage_name the linkage name of the declaration.
4500 ///
4501 /// @param vis the visibility of the declaration.
4502 decl_base::decl_base(const environment& e,
4503  const string& name,
4504  const location& locus,
4505  const string& linkage_name,
4506  visibility vis)
4507  : type_or_decl_base(e, ABSTRACT_DECL_BASE),
4508  priv_(new priv(e.intern(name), e.intern(linkage_name), vis))
4509 {
4510  set_location(locus);
4511 }
4512 
4513 /// Constructor.
4514 ///
4515 /// @param e the environment this instance of @ref decl_base is
4516 /// created in.
4517 ///
4518 /// @param name the name of the declaration being constructed.
4519 ///
4520 /// @param locus the source location of the declaration being constructed.
4521 ///
4522 /// @param linkage_name the linkage name of the declaration being
4523 /// constructed.
4524 ///
4525 /// @param vis the visibility of the declaration being constructed.
4526 decl_base::decl_base(const environment& e,
4527  const interned_string& name,
4528  const location& locus,
4529  const interned_string& linkage_name,
4530  visibility vis)
4531  : type_or_decl_base(e, ABSTRACT_DECL_BASE),
4532  priv_(new priv(name, linkage_name, vis))
4533 {
4534  set_location(locus);
4535 }
4536 
4537 /// Constructor for the @ref decl_base type.
4538 ///
4539 ///@param environment the environment this instance of @ref decl_base
4540 /// is being constructed in.
4541 ///
4542 /// @param l the location where to find the declaration in the source
4543 /// code.
4544 decl_base::decl_base(const environment& e, const location& l)
4545  : type_or_decl_base(e, ABSTRACT_DECL_BASE),
4546  priv_(new priv())
4547 {
4548  set_location(l);
4549 }
4550 
4551 /// Getter for the qualified name.
4552 ///
4553 /// Unlike decl_base::get_qualified_name() this doesn't try to update
4554 /// the qualified name.
4555 ///
4556 /// @return the qualified name.
4557 const interned_string&
4559 {return priv_->qualified_name_;}
4560 
4561 /// Clear the qualified name of this decl.
4562 ///
4563 /// This is useful to ensure that the cache for the qualified name of
4564 /// the decl is refreshed right after type canonicalization, for
4565 /// instance.
4566 void
4568 {priv_->qualified_name_.clear();}
4569 
4570 /// Setter for the qualified name.
4571 ///
4572 /// @param n the new qualified name.
4573 void
4575 {priv_->qualified_name_ = n;}
4576 
4577 /// Getter of the temporary qualified name of the current declaration.
4578 ///
4579 /// This temporary qualified name is used as a qualified name cache by
4580 /// the type for which this is the declaration (when applicable)
4581 /// before the type is canonicalized. Once the type is canonicalized,
4582 /// it's the result of decl_base::peek_qualified_name() that becomes
4583 /// the qualified name cached.
4584 ///
4585 /// @return the temporary qualified name.
4586 const interned_string&
4588 {return priv_->temporary_qualified_name_;}
4589 
4590 /// Setter for the temporary qualified name of the current
4591 /// declaration.
4592 ///
4593 ///@param n the new temporary qualified name.
4594 ///
4595 /// This temporary qualified name is used as a qualified name cache by
4596 /// the type for which this is the declaration (when applicable)
4597 /// before the type is canonicalized. Once the type is canonicalized,
4598 /// it's the result of decl_base::peek_qualified_name() that becomes
4599 /// the qualified name cached.
4600 void
4602 {priv_->temporary_qualified_name_ = n;}
4603 
4604 ///Getter for the context relationship.
4605 ///
4606 ///@return the context relationship for the current decl_base.
4607 const context_rel*
4609 {return priv_->context_;}
4610 
4611 ///Getter for the context relationship.
4612 ///
4613 ///@return the context relationship for the current decl_base.
4614 context_rel*
4616 {return priv_->context_;}
4617 
4618 void
4619 decl_base::set_context_rel(context_rel *c)
4620 {priv_->context_ = c;}
4621 
4622 /// Get the hash of a decl. If the hash hasn't been computed yet,
4623 /// compute it ans store its value; otherwise, just return the hash.
4624 ///
4625 /// @return the hash of the decl.
4626 size_t
4628 {
4629  size_t result = 0;
4630 
4631  if (const type_base* t = dynamic_cast<const type_base*>(this))
4632  {
4634  result = hash(t);
4635  }
4636  else
4637  // If we reach this point, it mean we are missing a virtual
4638  // overload for decl_base::get_hash. Add it!
4639  abort();
4640 
4641  return result;
4642 }
4643 
4644 /// Test if the decl is defined in a ELF symbol table as a public
4645 /// symbol.
4646 ///
4647 /// @return true iff the decl is defined in a ELF symbol table as a
4648 /// public symbol.
4649 bool
4651 {return priv_->in_pub_sym_tab_;}
4652 
4653 /// Set the flag saying if this decl is from a symbol that is in
4654 /// a public symbols table, defined as public (global or weak).
4655 ///
4656 /// @param f the new flag value.
4657 void
4659 {priv_->in_pub_sym_tab_ = f;}
4660 
4661 /// Get the location of a given declaration.
4662 ///
4663 /// The location is an abstraction for the tripplet {file path,
4664 /// line, column} that defines where the declaration appeared in the
4665 /// source code.
4666 ///
4667 /// To get the value of the tripplet {file path, line, column} from
4668 /// the @ref location, you need to use the
4669 /// location_manager::expand_location() method.
4670 ///
4671 /// The instance of @ref location_manager that you want is
4672 /// accessible from the instance of @ref translation_unit that the
4673 /// current instance of @ref decl_base belongs to, via a call to
4674 /// translation_unit::get_loc_mgr().
4675 ///
4676 /// @return the location of the current instance of @ref decl_base.
4677 const location&
4679 {return priv_->location_;}
4680 
4681 /// Set the location for a given declaration.
4682 ///
4683 /// The location is an abstraction for the tripplet {file path,
4684 /// line, column} that defines where the declaration appeared in the
4685 /// source code.
4686 ///
4687 /// To create a location from a tripplet {file path, line, column},
4688 /// you need to use the method @ref
4689 /// location_manager::create_new_location().
4690 ///
4691 /// Note that there can be two kinds of location. An artificial
4692 /// location and a non-artificial one. The non-artificial location is
4693 /// the one emitted by the original emitter of the ABI artifact, for
4694 /// instance, if the ABI artifact comes from debug info, then the
4695 /// source location that is present in the debug info represent a
4696 /// non-artificial location. When looking at an abixml file on the
4697 /// other hand, the value of the 'location' attribute of an XML
4698 /// element describing an artifact is the non-artificial location.
4699 /// The artificial location is the location (line number from the
4700 /// beginning of the file) of the XML element within the abixml file.
4701 ///
4702 /// So, if the location that is being set is artificial, note that the
4703 /// type_or_decl_base::has_artificial_location() method of this decl will
4704 /// subsequently return true and that artificial location will have to
4705 /// be retrieved using type_or_decl_base::get_artificial_location().
4706 /// If the location is non-artificial however,
4707 /// type_or_decl_base::has_artificial_location() will subsequently
4708 /// return false and the non-artificial location will have to be
4709 /// retrieved using decl_base::get_location().
4710 ///
4711 /// The instance of @ref location_manager that you want is
4712 /// accessible from the instance of @ref translation_unit that the
4713 /// current instance of @ref decl_base belongs to, via a call to
4714 /// translation_unit::get_loc_mgr().
4715 void
4717 {
4718  if (l.get_is_artificial())
4720  else
4721  priv_->location_ = l;
4722 }
4723 
4724 /// Setter for the name of the decl.
4725 ///
4726 /// @param n the new name to set.
4727 void
4728 decl_base::set_name(const string& n)
4729 {
4730  priv_->name_ = get_environment().intern(n);
4731  priv_->is_anonymous_ = n.empty();
4732 }
4733 
4734 /// Test if the current declaration is anonymous.
4735 ///
4736 /// Being anonymous means that the declaration was created without a
4737 /// name. This can usually happen for enum or struct types.
4738 ///
4739 /// @return true iff the type is anonymous.
4740 bool
4742 {return priv_->is_anonymous_;}
4743 
4744 /// Set the "is_anonymous" flag of the current declaration.
4745 ///
4746 /// Being anonymous means that the declaration was created without a
4747 /// name. This can usually happen for enum or struct types.
4748 ///
4749 /// @param f the new value of the flag.
4750 void
4752 {priv_->is_anonymous_ = f;}
4753 
4754 
4755 /// Get the "has_anonymous_parent" flag of the current declaration.
4756 ///
4757 /// Having an anoymous parent means having a anonymous parent scope
4758 /// (containing type or namespace) which is either direct or indirect.
4759 ///
4760 /// @return true iff the current decl has a direct or indirect scope
4761 /// which is anonymous.
4762 bool
4764 {
4765  scope_decl *scope = get_scope();
4766  if (!scope)
4767  return false;
4768  return scope->get_is_anonymous();
4769 }
4770 
4771 /// @return the logical "OR" of decl_base::get_is_anonymous() and
4772 /// decl_base::get_has_anonymous_parent().
4773 bool
4776 
4777 /// Getter for the naming typedef of the current decl.
4778 ///
4779 /// Consider the C idiom:
4780 ///
4781 /// typedef struct {int member;} foo_type;
4782 ///
4783 /// In that idiom, foo_type is the naming typedef of the anonymous
4784 /// struct that is declared.
4785 ///
4786 /// @return the naming typedef, if any. Otherwise, returns nil.
4789 {return priv_->naming_typedef_;}
4790 
4791 /// Set the naming typedef of the current instance of @ref decl_base.
4792 ///
4793 /// Consider the C idiom:
4794 ///
4795 /// typedef struct {int member;} foo_type;
4796 ///
4797 /// In that idiom, foo_type is the naming typedef of the anonymous
4798 /// struct that is declared.
4799 ///
4800 /// After completion of this function, the decl will not be considered
4801 /// anonymous anymore. It's name is going to be the name of the
4802 /// naming typedef.
4803 ///
4804 /// @param typedef_type the new naming typedef.
4805 void
4807 {
4808  // A naming typedef is usually for an anonymous type.
4810  // Whe the typedef-named decl is saved into abixml, it's
4811  // not anonymous anymore. Its name is the typedef name.
4812  // So when we read it back, we must still be able to
4813  // apply the naming typedef to the decl.
4814  || t->get_name() == get_name());
4815  // Only non canonicalized types can be edited this way.
4816  ABG_ASSERT(is_type(this)
4817  && is_type(this)->get_naked_canonical_type() == nullptr);
4818 
4819  priv_->naming_typedef_ = t;
4820  set_name(t->get_name());
4821  string qualified_name = build_qualified_name(get_scope(), t->get_name());
4822  set_qualified_name(get_environment().intern(qualified_name));
4823  set_is_anonymous(false);
4824  // Now that the qualified type of the decl has changed, let's update
4825  // the qualified names of the member types of this decls.
4826  update_qualified_name(this);
4827 }
4828 
4829 /// Getter for the mangled name.
4830 ///
4831 /// @return the new mangled name.
4832 const interned_string&
4834 {return priv_->linkage_name_;}
4835 
4836 /// Setter for the linkage name.
4837 ///
4838 /// @param m the new linkage name.
4839 void
4841 {
4842  const environment& env = get_environment();
4843  priv_->linkage_name_ = env.intern(m);
4844 }
4845 
4846 /// Getter for the visibility of the decl.
4847 ///
4848 /// @return the new visibility.
4851 {return priv_->visibility_;}
4852 
4853 /// Setter for the visibility of the decl.
4854 ///
4855 /// @param v the new visibility.
4856 void
4858 {priv_->visibility_ = v;}
4859 
4860 /// Return the type containing the current decl, if any.
4861 ///
4862 /// @return the type that contains the current decl, or NULL if there
4863 /// is none.
4864 scope_decl*
4866 {
4867  if (priv_->context_)
4868  return priv_->context_->get_scope();
4869  return 0;
4870 }
4871 
4872 /// Return a copy of the qualified name of the parent of the current
4873 /// decl.
4874 ///
4875 /// @return the newly-built qualified name of the of the current decl.
4876 const interned_string&
4878 {return priv_->qualified_parent_name_;}
4879 
4880 /// Getter for the name of the current decl.
4881 ///
4882 /// @return the name of the current decl.
4883 const interned_string&
4885 {return priv_->name_;}
4886 
4887 /// Compute the qualified name of the decl.
4888 ///
4889 /// @param qn the resulting qualified name.
4890 ///
4891 /// @param internal set to true if the call is intended for an
4892 /// internal use (for technical use inside the library itself), false
4893 /// otherwise. If you don't know what this is for, then set it to
4894 /// false.
4895 void
4897 {qn = get_qualified_name(internal);}
4898 
4899 /// Get the pretty representatin of the current declaration.
4900 ///
4901 ///
4902 /// @param internal set to true if the call is intended to get a
4903 /// representation of the decl (or type) for the purpose of canonical
4904 /// type comparison. This is mainly used in the function
4905 /// type_base::get_canonical_type_for().
4906 ///
4907 /// In other words if the argument for this parameter is true then the
4908 /// call is meant for internal use (for technical use inside the
4909 /// library itself), false otherwise. If you don't know what this is
4910 /// for, then set it to false.
4911 ///
4912 /// @param qualified_name if true, names emitted in the pretty
4913 /// representation are fully qualified.
4914 ///
4915 /// @return the default pretty representation for a decl. This is
4916 /// basically the fully qualified name of the decl optionally prefixed
4917 /// with a meaningful string to add context for the user.
4918 string
4920  bool qualified_name) const
4921 {
4922  if (internal
4923  && get_is_anonymous()
4924  && has_generic_anonymous_internal_type_name(this))
4925  {
4926  // We are looking at an anonymous enum, union or class and we
4927  // want an *internal* pretty representation for it. All
4928  // anonymous types of this kind in the same namespace must have
4929  // the same internal representation for type canonicalization to
4930  // work properly.
4931  //
4932  // OK, in practise, we are certainly looking at an enum because
4933  // classes and unions should have their own overloaded virtual
4934  // member function for this.
4935  string name = get_generic_anonymous_internal_type_name(this);
4936  if (qualified_name && !get_qualified_parent_name().empty())
4937  name = get_qualified_parent_name() + "::" + name;
4938  return name;
4939  }
4940 
4941  if (qualified_name)
4942  return get_qualified_name(internal);
4943  return get_name();
4944 }
4945 
4946 /// Return the qualified name of the decl.
4947 ///
4948 /// This is the fully qualified name of the decl. It's made of the
4949 /// concatenation of the name of the decl with the qualified name of
4950 /// its scope.
4951 ///
4952 /// Note that the value returned by this function is computed by @ref
4953 /// update_qualified_name when the decl is added to its scope.
4954 ///
4955 /// @param internal set to true if the call is intended for an
4956 /// internal use (for technical use inside the library itself), false
4957 /// otherwise. If you don't know what this is for, then set it to
4958 /// false.
4959 ///
4960 /// @return the resulting qualified name.
4961 const interned_string&
4962 decl_base::get_qualified_name(bool /*internal*/) const
4963 {return priv_->qualified_name_;}
4964 
4965 /// Return the scoped name of the decl.
4966 ///
4967 /// This is made of the concatenation of the name of the decl with the
4968 /// name of its scope. It doesn't contain the qualified name of its
4969 /// scope, unlike what is returned by decl_base::get_qualified_name.
4970 ///
4971 /// Note that the value returned by this function is computed by @ref
4972 /// update_qualified_name when the decl is added to its scope.
4973 ///
4974 /// @return the scoped name of the decl.
4975 const interned_string&
4977 {return priv_->scoped_name_;}
4978 
4979 /// If this @ref decl_base is a definition, get its earlier
4980 /// declaration.
4981 ///
4982 /// @return the earlier declaration of the class, if any.
4983 const decl_base_sptr
4985 {return priv_->declaration_;}
4986 
4987 /// set the earlier declaration of this @ref decl_base definition.
4988 ///
4989 /// @param d the earlier declaration to set. Note that it's set only
4990 /// if it's a pure declaration.
4991 void
4992 decl_base::set_earlier_declaration(const decl_base_sptr& d)
4993 {
4994  if (d && d->get_is_declaration_only())
4995  priv_->declaration_ = d;
4996 }
4997 
4998 
4999 /// If this @ref decl_base is declaration-only, get its definition, if
5000 /// any.
5001 ///
5002 /// @return the definition of this decl-only @ref decl_base.
5003 const decl_base_sptr
5005 {return priv_->definition_of_declaration_.lock();}
5006 
5007 /// If this @ref decl_base is declaration-only, get its definition,
5008 /// if any.
5009 ///
5010 /// Note that this function doesn't return a smart pointer, but rather
5011 /// the underlying pointer managed by the smart pointer. So it's as
5012 /// fast as possible. This getter is to be used in code paths that
5013 /// are proven to be performance hot spots; especially, when comparing
5014 /// sensitive types like enums, classes or unions. Those are compared
5015 /// extremely frequently and thus, their access to the definition of
5016 /// declaration must be fast.
5017 ///
5018 /// @return the definition of the declaration.
5019 const decl_base*
5021 {return priv_->naked_definition_of_declaration_;}
5022 
5023 /// Test if a @ref decl_base is a declaration-only decl.
5024 ///
5025 /// @return true iff the current @ref decl_base is declaration-only.
5026 bool
5028 {return priv_->is_declaration_only_;}
5029 
5030 /// Set a flag saying if the @ref enum_type_decl is a declaration-only
5031 /// @ref enum_type_decl.
5032 ///
5033 /// @param f true if the @ref enum_type_decl is a declaration-only
5034 /// @ref enum_type_decl.
5035 void
5037 {
5038  bool update_types_lookup_map = !f && priv_->is_declaration_only_;
5039 
5040  priv_->is_declaration_only_ = f;
5041 
5042  if (update_types_lookup_map)
5043  if (scope_decl* s = get_scope())
5044  {
5045  scope_decl::declarations::iterator i;
5046  if (s->find_iterator_for_member(this, i))
5048  else
5050  }
5051 }
5052 
5055 {
5056  return static_cast<change_kind>(static_cast<unsigned>(l)
5057  | static_cast<unsigned>(r));
5058 }
5059 
5062 {
5063  return static_cast<change_kind>(static_cast<unsigned>(l)
5064  & static_cast<unsigned>(r));
5065 }
5066 
5067 change_kind&
5069 {
5070  l = l | r;
5071  return l;
5072 }
5073 
5074 change_kind&
5076 {
5077  l = l & r;
5078  return l;
5079 }
5080 
5081 /// Compare the properties that belong to the "is-a-member-relation"
5082 /// of a decl.
5083 ///
5084 /// For instance, access specifiers are part of the
5085 /// "is-a-member-relation" of a decl.
5086 ///
5087 /// This comparison however doesn't take decl names into account. So
5088 /// typedefs for instance are decls that we want to compare with this
5089 /// function.
5090 ///
5091 /// This function is a sub-routine of the more general 'equals'
5092 /// overload for instances of decl_base.
5093 ///
5094 /// @param l the left-hand side operand of the comparison.
5095 ///
5096 /// @param r the right-hand side operand of the comparison.
5097 ///
5098 /// @return true iff @p l compare equals, as a member decl, to @p r.
5099 bool
5101  const decl_base& r,
5102  change_kind* k)
5103 {
5104  bool result = true;
5105  if (is_member_decl(l) && is_member_decl(r))
5106  {
5107  context_rel* r1 = const_cast<context_rel*>(l.get_context_rel());
5108  context_rel *r2 = const_cast<context_rel*>(r.get_context_rel());
5109 
5110  access_specifier la = no_access, ra = no_access;
5111  bool member_types_or_functions =
5112  ((is_type(l) && is_type(r))
5113  || (is_function_decl(l) && is_function_decl(r)));
5114 
5115  if (member_types_or_functions)
5116  {
5117  // Access specifiers on member types in DWARF is not
5118  // reliable; in the same DSO, the same struct can be either
5119  // a class or a struct, and the access specifiers of its
5120  // member types are not necessarily given, so they
5121  // effectively can be considered differently, again, in the
5122  // same DSO. So, here, let's avoid considering those!
5123  // during comparison.
5124  la = r1->get_access_specifier();
5125  ra = r2->get_access_specifier();
5126  r1->set_access_specifier(no_access);
5127  r2->set_access_specifier(no_access);
5128  }
5129 
5130  bool rels_are_different = *r1 != *r2;
5131 
5132  if (member_types_or_functions)
5133  {
5134  // restore the access specifiers.
5135  r1->set_access_specifier(la);
5136  r2->set_access_specifier(ra);
5137  }
5138 
5139  if (rels_are_different)
5140  {
5141  result = false;
5142  if (k)
5144  }
5145  }
5146  ABG_RETURN(result);
5147 }
5148 
5149 /// Get the name of a decl for the purpose of comparing two decl
5150 /// names.
5151 ///
5152 /// This is a sub-routine of the 'equal' overload for decl_base.
5153 ///
5154 /// This function takes into account the fact that all anonymous names
5155 /// shall have the same name for the purpose of comparison.
5156 ///
5157 /// For decls that are part of an anonymous scope, only the
5158 /// non-qualified name should be taken into account.
5159 static interned_string
5160 get_decl_name_for_comparison(const decl_base &d)
5161 {
5162  if (has_generic_anonymous_internal_type_name(&d)
5163  && d.get_is_anonymous())
5164  {
5165  // The decl is anonymous. It should have the same name ass the
5166  // other anymous types of the same kind.
5167  string r;
5168  r += get_generic_anonymous_internal_type_name(&d);
5169  return d.get_environment().intern(r);
5170  }
5171 
5174  ? d.get_name()
5175  : d.get_qualified_name(/*internal=*/true);
5176  return n;
5177 }
5178 
5179 /// Compares two instances of @ref decl_base.
5180 ///
5181 /// If the two intances are different, set a bitfield to give some
5182 /// insight about the kind of differences there are.
5183 ///
5184 /// @param l the first artifact of the comparison.
5185 ///
5186 /// @param r the second artifact of the comparison.
5187 ///
5188 /// @param k a pointer to a bitfield that gives information about the
5189 /// kind of changes there are between @p l and @p r. This one is set
5190 /// iff it's non-null and if the function returns false.
5191 ///
5192 /// Please note that setting k to a non-null value does have a
5193 /// negative performance impact because even if @p l and @p r are not
5194 /// equal, the function keeps up the comparison in order to determine
5195 /// the different kinds of ways in which they are different.
5196 ///
5197 /// @return true if @p l equals @p r, false otherwise.
5198 bool
5199 equals(const decl_base& l, const decl_base& r, change_kind* k)
5200 {
5201  bool result = true;
5202  const interned_string &l_linkage_name = l.get_linkage_name();
5203  const interned_string &r_linkage_name = r.get_linkage_name();
5204  if (!l_linkage_name.empty() && !r_linkage_name.empty())
5205  {
5206  if (l_linkage_name != r_linkage_name)
5207  {
5208  // Linkage names are different. That usually means the two
5209  // decls are different, unless we are looking at two
5210  // function declarations which have two different symbols
5211  // that are aliases of each other.
5212  const function_decl *f1 = is_function_decl(&l),
5213  *f2 = is_function_decl(&r);
5214  if (f1 && f2 && function_decls_alias(*f1, *f2))
5215  ;// The two functions are aliases, so they are not
5216  // different.
5217  else
5218  {
5219  result = false;
5220  if (k)
5222  else
5224  }
5225  }
5226  }
5227 
5228  // This is the qualified name of the decls that we want to compare.
5229  // We want to use the "internal" version of the qualified name as
5230  // that one is stable even for anonymous decls.
5231  interned_string ln = get_decl_name_for_comparison(l);
5232  interned_string rn = get_decl_name_for_comparison(r);
5233 
5234  /// If both of the current decls have an anonymous scope then let's
5235  /// compare their name component by component by properly handling
5236  /// anonymous scopes. That's the slow path.
5237  ///
5238  /// Otherwise, let's just compare their name, the obvious way.
5239  /// That's the fast path because in that case the names are
5240  /// interned_string and comparing them is much faster.
5241  bool decls_are_same = (ln == rn);
5242  if (!decls_are_same
5243  && l.get_is_anonymous()
5244  && !l.get_has_anonymous_parent()
5245  && r.get_is_anonymous()
5246  && !r.get_has_anonymous_parent())
5247  // Both decls are anonymous and their scope are *NOT* anonymous.
5248  // So we consider the decls to have equivalent names (both
5249  // anonymous, remember). We are still in the fast path here.
5250  decls_are_same = true;
5251 
5252  if (!decls_are_same
5254  && r.get_has_anonymous_parent())
5255  // This is the slow path as we are comparing the decl qualified
5256  // names component by component, properly handling anonymous
5257  // scopes.
5258  decls_are_same = tools_utils::decl_names_equal(ln, rn);
5259 
5260  if (!decls_are_same)
5261  {
5262  result = false;
5263  if (k)
5265  else
5267  }
5268 
5269  result &= maybe_compare_as_member_decls(l, r, k);
5270 
5271  ABG_RETURN(result);
5272 }
5273 
5274 /// Return true iff the two decls have the same name.
5275 ///
5276 /// This function doesn't test if the scopes of the the two decls are
5277 /// equal.
5278 ///
5279 /// Note that this virtual function is to be implemented by classes
5280 /// that extend the \p decl_base class.
5281 bool
5283 {return equals(*this, other, 0);}
5284 
5285 /// Inequality operator.
5286 ///
5287 /// @param other to other instance of @ref decl_base to compare the
5288 /// current instance to.
5289 ///
5290 /// @return true iff the current instance of @ref decl_base is
5291 /// different from @p other.
5292 bool
5294 {return !operator==(other);}
5295 
5296 /// Destructor of the @ref decl_base type.
5298 {delete priv_;}
5299 
5300 /// This implements the ir_traversable_base::traverse pure virtual
5301 /// function.
5302 ///
5303 /// @param v the visitor used on the member nodes of the translation
5304 /// unit during the traversal.
5305 ///
5306 /// @return true if the entire IR node tree got traversed, false
5307 /// otherwise.
5308 bool
5310 {
5311  // Do nothing in the base class.
5312  return true;
5313 }
5314 
5315 /// Setter of the scope of the current decl.
5316 ///
5317 /// Note that the decl won't hold a reference on the scope. It's
5318 /// rather the scope that holds a reference on its members.
5319 void
5321 {
5322  if (!priv_->context_)
5323  priv_->context_ = new context_rel(scope);
5324  else
5325  priv_->context_->set_scope(scope);
5326 }
5327 
5328 // </decl_base definition>
5329 
5330 /// Streaming operator for the decl_base::visibility.
5331 ///
5332 /// @param o the output stream to serialize the visibility to.
5333 ///
5334 /// @param v the visibility to serialize.
5335 ///
5336 /// @return the output stream.
5337 std::ostream&
5338 operator<<(std::ostream& o, decl_base::visibility v)
5339 {
5340  string r;
5341  switch (v)
5342  {
5343  case decl_base::VISIBILITY_NONE:
5344  r = "none";
5345  break;
5346  case decl_base::VISIBILITY_DEFAULT:
5347  r = "default";
5348  break;
5349  case decl_base::VISIBILITY_PROTECTED:
5350  r = "protected";
5351  break;
5352  case decl_base::VISIBILITY_HIDDEN:
5353  r = "hidden";
5354  break;
5355  case decl_base::VISIBILITY_INTERNAL:
5356  r = "internal";
5357  break;
5358  }
5359  return o;
5360 }
5361 
5362 /// Streaming operator for decl_base::binding.
5363 ///
5364 /// @param o the output stream to serialize the visibility to.
5365 ///
5366 /// @param b the binding to serialize.
5367 ///
5368 /// @return the output stream.
5369 std::ostream&
5370 operator<<(std::ostream& o, decl_base::binding b)
5371 {
5372  string r;
5373  switch (b)
5374  {
5375  case decl_base::BINDING_NONE:
5376  r = "none";
5377  break;
5378  case decl_base::BINDING_LOCAL:
5379  r = "local";
5380  break;
5381  case decl_base::BINDING_GLOBAL:
5382  r = "global";
5383  break;
5384  case decl_base::BINDING_WEAK:
5385  r = "weak";
5386  break;
5387  }
5388  o << r;
5389  return o;
5390 }
5391 
5392 /// Turn equality of shared_ptr of decl_base into a deep equality;
5393 /// that is, make it compare the pointed to objects, not just the
5394 /// pointers.
5395 ///
5396 /// @param l the shared_ptr of decl_base on left-hand-side of the
5397 /// equality.
5398 ///
5399 /// @param r the shared_ptr of decl_base on right-hand-side of the
5400 /// equality.
5401 ///
5402 /// @return true if the decl_base pointed to by the shared_ptrs are
5403 /// equal, false otherwise.
5404 bool
5405 operator==(const decl_base_sptr& l, const decl_base_sptr& r)
5406 {
5407  if (l.get() == r.get())
5408  return true;
5409  if (!!l != !!r)
5410  return false;
5411 
5412  return *l == *r;
5413 }
5414 
5415 /// Inequality operator of shared_ptr of @ref decl_base.
5416 ///
5417 /// This is a deep equality operator, that is, it compares the
5418 /// pointed-to objects, rather than just the pointers.
5419 ///
5420 /// @param l the left-hand-side operand.
5421 ///
5422 /// @param r the right-hand-side operand.
5423 ///
5424 /// @return true iff @p l is different from @p r.
5425 bool
5426 operator!=(const decl_base_sptr& l, const decl_base_sptr& r)
5427 {return !operator==(l, r);}
5428 
5429 /// Turn equality of shared_ptr of type_base into a deep equality;
5430 /// that is, make it compare the pointed to objects too.
5431 ///
5432 /// @param l the shared_ptr of type_base on left-hand-side of the
5433 /// equality.
5434 ///
5435 /// @param r the shared_ptr of type_base on right-hand-side of the
5436 /// equality.
5437 ///
5438 /// @return true if the type_base pointed to by the shared_ptrs are
5439 /// equal, false otherwise.
5440 bool
5441 operator==(const type_base_sptr& l, const type_base_sptr& r)
5442 {
5443  if (l.get() == r.get())
5444  return true;
5445  if (!!l != !!r)
5446  return false;
5447 
5448  return *l == *r;
5449 }
5450 
5451 /// Turn inequality of shared_ptr of type_base into a deep equality;
5452 /// that is, make it compare the pointed to objects..
5453 ///
5454 /// @param l the shared_ptr of type_base on left-hand-side of the
5455 /// equality.
5456 ///
5457 /// @param r the shared_ptr of type_base on right-hand-side of the
5458 /// equality.
5459 ///
5460 /// @return true iff the type_base pointed to by the shared_ptrs are
5461 /// different.
5462 bool
5463 operator!=(const type_base_sptr& l, const type_base_sptr& r)
5464 {return !operator==(l, r);}
5465 
5466 /// Tests if a declaration has got a scope.
5467 ///
5468 /// @param d the declaration to consider.
5469 ///
5470 /// @return true if the declaration has got a scope, false otherwise.
5471 bool
5473 {return (d.get_scope());}
5474 
5475 /// Tests if a declaration has got a scope.
5476 ///
5477 /// @param d the declaration to consider.
5478 ///
5479 /// @return true if the declaration has got a scope, false otherwise.
5480 bool
5481 has_scope(const decl_base_sptr d)
5482 {return has_scope(*d.get());}
5483 
5484 /// Tests if a declaration is a class member.
5485 ///
5486 /// @param d the declaration to consider.
5487 ///
5488 /// @return true if @p d is a class member, false otherwise.
5489 bool
5490 is_member_decl(const decl_base_sptr d)
5491 {return is_at_class_scope(d) || is_method_decl(d);}
5492 
5493 /// Tests if a declaration is a class member.
5494 ///
5495 /// @param d the declaration to consider.
5496 ///
5497 /// @return true if @p d is a class member, false otherwise.
5498 bool
5500 {return is_at_class_scope(d) || is_method_decl(d);}
5501 
5502 /// Tests if a declaration is a class member.
5503 ///
5504 /// @param d the declaration to consider.
5505 ///
5506 /// @return true if @p d is a class member, false otherwise.
5507 bool
5509 {return is_at_class_scope(d) || is_method_decl(d);}
5510 
5511 /// Test if a declaration is a @ref scope_decl.
5512 ///
5513 /// @param d the declaration to take in account.
5514 ///
5515 /// @return the a pointer to the @ref scope_decl sub-object of @p d,
5516 /// if d is a @ref scope_decl.
5517 scope_decl*
5519 {return dynamic_cast<scope_decl*>(d);}
5520 
5521 /// Test if a declaration is a @ref scope_decl.
5522 ///
5523 /// @param d the declaration to take in account.
5524 ///
5525 /// @return the a pointer to the @ref scope_decl sub-object of @p d,
5526 /// if d is a @ref scope_decl.
5528 is_scope_decl(const decl_base_sptr& d)
5529 {return dynamic_pointer_cast<scope_decl>(d);}
5530 
5531 /// Tests if a type is a class member.
5532 ///
5533 /// @param t the type to consider.
5534 ///
5535 /// @return true if @p t is a class member type, false otherwise.
5536 bool
5537 is_member_type(const type_base_sptr& t)
5538 {
5539  decl_base_sptr d = get_type_declaration(t);
5540  return is_member_decl(d);
5541 }
5542 
5543 /// Test if a type is user-defined.
5544 ///
5545 /// A type is considered user-defined if it's a
5546 /// struct/class/union/enum that is *NOT* artificial.
5547 ///
5548 /// @param t the type to consider.
5549 ///
5550 /// @return true iff the type @p t is user-defined.
5551 bool
5553 {
5554  if (t == 0)
5555  return false;
5556 
5558  decl_base *d = is_decl(t);
5559 
5560  if ((is_class_or_union_type(t) || is_enum_type(t))
5561  && d && !d->get_is_artificial())
5562  return true;
5563 
5564  return false;
5565 }
5566 
5567 /// Test if a type is user-defined.
5568 ///
5569 /// A type is considered user-defined if it's a
5570 /// struct/class/union/enum.
5571 ///
5572 ///
5573 /// @param t the type to consider.
5574 ///
5575 /// @return true iff the type @p t is user-defined.
5576 bool
5577 is_user_defined_type(const type_base_sptr& t)
5578 {return is_user_defined_type(t.get());}
5579 
5580 /// Gets the access specifier for a class member.
5581 ///
5582 /// @param d the declaration of the class member to consider. Note
5583 /// that this must be a class member otherwise the function aborts the
5584 /// current process.
5585 ///
5586 /// @return the access specifier for the class member @p d.
5589 {
5591 
5592  const context_rel* c = d.get_context_rel();
5593  ABG_ASSERT(c);
5594 
5595  return c->get_access_specifier();
5596 }
5597 
5598 /// Gets the access specifier for a class member.
5599 ///
5600 /// @param d the declaration of the class member to consider. Note
5601 /// that this must be a class member otherwise the function aborts the
5602 /// current process.
5603 ///
5604 /// @return the access specifier for the class member @p d.
5606 get_member_access_specifier(const decl_base_sptr& d)
5607 {return get_member_access_specifier(*d);}
5608 
5609 /// Sets the access specifier for a class member.
5610 ///
5611 /// @param d the class member to set the access specifier for. Note
5612 /// that this must be a class member otherwise the function aborts the
5613 /// current process.
5614 ///
5615 /// @param a the new access specifier to set the class member to.
5616 void
5618  access_specifier a)
5619 {
5621 
5622  context_rel* c = d.get_context_rel();
5623  ABG_ASSERT(c);
5624 
5625  c->set_access_specifier(a);
5626 }
5627 
5628 /// Sets the access specifier for a class member.
5629 ///
5630 /// @param d the class member to set the access specifier for. Note
5631 /// that this must be a class member otherwise the function aborts the
5632 /// current process.
5633 ///
5634 /// @param a the new access specifier to set the class member to.
5635 void
5636 set_member_access_specifier(const decl_base_sptr& d,
5637  access_specifier a)
5639 
5640 /// Gets a flag saying if a class member is static or not.
5641 ///
5642 /// @param d the declaration for the class member to consider. Note
5643 /// that this must be a class member otherwise the function aborts the
5644 /// current process.
5645 ///
5646 /// @return true if the class member @p d is static, false otherwise.
5647 bool
5649 {
5651 
5652  const context_rel* c = d.get_context_rel();
5653  ABG_ASSERT(c);
5654 
5655  return c->get_is_static();
5656 }
5657 
5658 /// Gets a flag saying if a class member is static or not.
5659 ///
5660 /// @param d the declaration for the class member to consider. Note
5661 /// that this must be a class member otherwise the function aborts the
5662 /// current process.
5663 ///
5664 /// @return true if the class member @p d is static, false otherwise.
5665 bool
5667 {return get_member_is_static(*d);}
5668 
5669 /// Gets a flag saying if a class member is static or not.
5670 ///
5671 /// @param d the declaration for the class member to consider. Note
5672 /// that this must be a class member otherwise the function aborts the
5673 /// current process.
5674 ///
5675 /// @return true if the class member @p d is static, false otherwise.
5676 bool
5677 get_member_is_static(const decl_base_sptr& d)
5678 {return get_member_is_static(*d);}
5679 
5680 /// Test if a var_decl is a data member.
5681 ///
5682 /// @param v the var_decl to consider.
5683 ///
5684 /// @return true if @p v is data member, false otherwise.
5685 bool
5687 {return is_at_class_scope(v);}
5688 
5689 /// Test if a var_decl is a data member.
5690 ///
5691 /// @param v the var_decl to consider.
5692 ///
5693 /// @return true if @p v is data member, false otherwise.
5694 bool
5696 {return is_data_member(*v);}
5697 
5698 /// Test if a var_decl is a data member.
5699 ///
5700 /// @param v the var_decl to consider.
5701 ///
5702 /// @return true if @p v is data member, false otherwise.
5703 bool
5705 {return is_at_class_scope(d);}
5706 
5707 /// Test if a decl is a data member.
5708 ///
5709 /// @param d the decl to consider.
5710 ///
5711 /// @return a pointer to the data member iff @p d is a data member, or
5712 /// a null pointer.
5714 is_data_member(const decl_base_sptr& d)
5715 {
5716  if (var_decl_sptr v = is_var_decl(d))
5717  {
5718  if (is_data_member(v))
5719  return v;
5720  }
5721  return var_decl_sptr();
5722 }
5723 
5724 /// Test if a decl is a data member.
5725 ///
5726 /// @param d the decl to consider.
5727 ///
5728 /// @return a pointer to the data member iff @p d is a data member, or
5729 /// a null pointer.
5732 {
5733  if (var_decl_sptr v = is_var_decl(d))
5734  {
5735  if (is_data_member(v))
5736  return v;
5737  }
5738  return var_decl_sptr();
5739 }
5740 
5741 /// Test if a decl is a data member.
5742 ///
5743 /// @param d the decl to consider.
5744 ///
5745 /// @return a pointer to the data member iff @p d is a data member, or
5746 /// a null pointer.
5747 var_decl*
5749 {
5750  if (var_decl *v = is_var_decl(d))
5751  if (is_data_member(v))
5752  return v;
5753  return 0;
5754 }
5755 
5756 /// Test if a decl is a data member.
5757 ///
5758 /// @param d the decl to consider.
5759 ///
5760 /// @return a pointer to the data member iff @p d is a data member, or
5761 /// a null pointer.
5762 var_decl*
5764 {
5765  if (var_decl *v = is_var_decl(d))
5766  if (is_data_member(v))
5767  return v;
5768  return 0;
5769 }
5770 
5771 /// Get the first non-anonymous data member of a given anonymous data
5772 /// member.
5773 ///
5774 /// E.g:
5775 ///
5776 /// struct S
5777 /// {
5778 /// union // <-- for this anonymous data member, the function
5779 /// // returns a.
5780 /// {
5781 /// int a;
5782 /// charb;
5783 /// };
5784 /// };
5785 ///
5786 /// @return anon_dm the anonymous data member to consider.
5787 ///
5788 /// @return the first non-anonymous data member of @p anon_dm. If no
5789 /// data member was found then this function returns @p anon_dm.
5790 const var_decl_sptr
5792 {
5793  if (!anon_dm || !is_anonymous_data_member(anon_dm))
5794  return anon_dm;
5795 
5796  class_or_union_sptr klass = anonymous_data_member_to_class_or_union(anon_dm);
5797  var_decl_sptr first = *klass->get_non_static_data_members().begin();
5798 
5799  if (is_anonymous_data_member(first))
5801 
5802  return first;
5803 }
5804 
5805 /// In the context of a given class or union, this function returns
5806 /// the data member that is located after a given data member.
5807 ///
5808 /// @param klass the class or union to consider.
5809 ///
5810 /// @param the data member to consider.
5811 ///
5812 /// @return the data member that is located right after @p
5813 /// data_member.
5814 const var_decl_sptr
5816  const var_decl_sptr &data_member)
5817 {
5818  if (!klass ||!data_member)
5819  return var_decl_sptr();
5820 
5821  for (class_or_union::data_members::const_iterator it =
5822  klass->get_non_static_data_members().begin();
5823  it != klass->get_non_static_data_members().end();
5824  ++it)
5825  if (**it == *data_member)
5826  {
5827  ++it;
5828  if (it != klass->get_non_static_data_members().end())
5830  break;
5831  }
5832 
5833  return var_decl_sptr();
5834 }
5835 
5836 /// In the context of a given class or union, this function returns
5837 /// the data member that is located after a given data member.
5838 ///
5839 /// @param klass the class or union to consider.
5840 ///
5841 /// @param the data member to consider.
5842 ///
5843 /// @return the data member that is located right after @p
5844 /// data_member.
5845 const var_decl_sptr
5846 get_next_data_member(const class_or_union_sptr& klass,
5847  const var_decl_sptr &data_member)
5848 {return get_next_data_member(klass.get(), data_member);}
5849 
5850 /// Get the last data member of a class type.
5851 ///
5852 /// @param klass the class type to consider.
5855 {return klass.get_non_static_data_members().back();}
5856 
5857 /// Get the last data member of a class type.
5858 ///
5859 /// @param klass the class type to consider.
5862 {return get_last_data_member(*klass);}
5863 
5864 /// Get the last data member of a class type.
5865 ///
5866 /// @param klass the class type to consider.
5868 get_last_data_member(const class_or_union_sptr &klass)
5869 {return get_last_data_member(klass.get());}
5870 
5871 /// Test if a decl is an anonymous data member.
5872 ///
5873 /// @param d the decl to consider.
5874 ///
5875 /// @return true iff @p d is an anonymous data member.
5876 bool
5878 {return is_anonymous_data_member(&d);}
5879 
5880 /// Test if a decl is an anonymous data member.
5881 ///
5882 /// @param d the decl to consider.
5883 ///
5884 /// @return the var_decl representing the data member iff @p d is an
5885 /// anonymous data member.
5886 const var_decl*
5888 {
5889  if (const var_decl* v = is_data_member(d))
5890  {
5891  if (is_anonymous_data_member(v))
5892  return v;
5893  }
5894  return 0;
5895 }
5896 
5897 /// Test if a decl is an anonymous data member.
5898 ///
5899 /// @param d the decl to consider.
5900 ///
5901 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
5902 /// it's an anonymous data member. Otherwise returns a nil pointer.
5903 const var_decl*
5905 {
5906  if (const var_decl* v = is_data_member(d))
5907  {
5908  if (is_anonymous_data_member(v))
5909  return v;
5910  }
5911  return 0;
5912 }
5913 
5914 /// Test if a decl is an anonymous data member.
5915 ///
5916 /// @param d the decl to consider.
5917 ///
5918 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
5919 /// it's an anonymous data member. Otherwise returns a nil pointer.
5922 {
5923  if (var_decl_sptr v = is_data_member(d))
5924  {
5925  if (is_anonymous_data_member(v))
5926  return v;
5927  }
5928  return var_decl_sptr();
5929 }
5930 
5931 /// Test if a decl is an anonymous data member.
5932 ///
5933 /// @param d the decl to consider.
5934 ///
5935 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
5936 /// it's an anonymous data member. Otherwise returns a nil pointer.
5938 is_anonymous_data_member(const decl_base_sptr& d)
5939 {
5940  if (var_decl_sptr v = is_data_member(d))
5941  return is_anonymous_data_member(v);
5942  return var_decl_sptr();
5943 }
5944 
5945 /// Test if a @ref var_decl is an anonymous data member.
5946 ///
5947 /// @param d the @ref var_decl to consider.
5948 ///
5949 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
5950 /// it's an anonymous data member. Otherwise returns a nil pointer.
5953 {
5954  if (is_anonymous_data_member(d.get()))
5955  return d;
5956  return var_decl_sptr();
5957 }
5958 
5959 /// Test if a @ref var_decl is an anonymous data member.
5960 ///
5961 /// @param d the @ref var_decl to consider.
5962 ///
5963 /// @return a non-nil pointer to the @ref var_decl denoted by @p d if
5964 /// it's an anonymous data member. Otherwise returns a nil pointer.
5965 const var_decl*
5967 {
5968  if (d && is_anonymous_data_member(*d))
5969  return d;
5970  return 0;
5971 }
5972 
5973 /// Test if a @ref var_decl is an anonymous data member.
5974 ///
5975 /// @param d the @ref var_decl to consider.
5976 ///
5977 /// @return true iff @p d is an anonymous data member.
5978 bool
5980 {
5981  return (is_data_member(d)
5982  && d.get_is_anonymous()
5983  && d.get_name().empty()
5985 }
5986 
5987 /// Get the @ref class_or_union type of a given anonymous data member.
5988 ///
5989 /// @param d the anonymous data member to consider.
5990 ///
5991 /// @return the @ref class_or_union type of the anonymous data member
5992 /// @p d.
5995 {
5996  if ((d = is_anonymous_data_member(d)))
5997  return is_class_or_union_type(d->get_type().get());
5998  return 0;
5999 }
6000 
6001 /// Get the @ref class_or_union type of a given anonymous data member.
6002 ///
6003 /// @param d the anonymous data member to consider.
6004 ///
6005 /// @return the @ref class_or_union type of the anonymous data member
6006 /// @p d.
6007 class_or_union_sptr
6009 {
6010  if (is_anonymous_data_member(d))
6011  return is_class_or_union_type(d.get_type());
6012  return class_or_union_sptr();
6013 }
6014 
6015 /// Test if a data member has annonymous type or not.
6016 ///
6017 /// @param d the data member to consider.
6018 ///
6019 /// @return the anonymous class or union type iff @p turns out to have
6020 /// an anonymous type. Otherwise, returns nil.
6021 const class_or_union_sptr
6023 {
6024  if (is_data_member(d))
6025  if (const class_or_union_sptr cou = is_class_or_union_type(d.get_type()))
6026  if (cou->get_is_anonymous())
6027  return cou;
6028 
6029  return class_or_union_sptr();
6030 }
6031 
6032 /// Test if a data member has annonymous type or not.
6033 ///
6034 /// @param d the data member to consider.
6035 ///
6036 /// @return the anonymous class or union type iff @p turns out to have
6037 /// an anonymous type. Otherwise, returns nil.
6038 const class_or_union_sptr
6040 {
6041  if (d)
6042  return data_member_has_anonymous_type(*d);
6043  return class_or_union_sptr();
6044 }
6045 
6046 /// Test if a data member has annonymous type or not.
6047 ///
6048 /// @param d the data member to consider.
6049 ///
6050 /// @return the anonymous class or union type iff @p turns out to have
6051 /// an anonymous type. Otherwise, returns nil.
6052 const class_or_union_sptr
6054 {return data_member_has_anonymous_type(d.get());}
6055 
6056 /// Get the @ref class_or_union type of a given anonymous data member.
6057 ///
6058 /// @param d the anonymous data member to consider.
6059 ///
6060 /// @return the @ref class_or_union type of the anonymous data member
6061 /// @p d.
6062 class_or_union_sptr
6064 {
6066  return is_class_or_union_type(v->get_type());
6067  return class_or_union_sptr();
6068 }
6069 
6070 /// Test if a given anonymous data member exists in a class or union.
6071 ///
6072 /// @param anon_dm the anonymous data member to consider.
6073 ///
6074 /// @param clazz the class to consider.
6075 ///
6076 /// @return true iff @p anon_dm exists in the @clazz.
6077 bool
6079  const class_or_union& clazz)
6080 {
6081  if (!anon_dm.get_is_anonymous()
6082  || !is_class_or_union_type(anon_dm.get_type()))
6083  return false;
6084 
6085  class_or_union_sptr cl = is_class_or_union_type(anon_dm.get_type());
6086  ABG_ASSERT(cl);
6087 
6088  // Look for the presence of each data member of anon_dm in clazz.
6089  //
6090  // If one data member of anon_dm is not present in clazz, then the
6091  // data member anon_dm is considered to not exist in clazz.
6092  for (auto anon_dm_m : cl->get_non_static_data_members())
6093  {
6094  // If the data member anon_dm_m is not an anonymous data member,
6095  // it's easy to look for it.
6096  if (!is_anonymous_data_member(anon_dm_m))
6097  {
6098  if (!clazz.find_data_member(anon_dm_m->get_name()))
6099  return false;
6100  }
6101  // If anon_dm_m is itself an anonymous data member then recurse
6102  else
6103  {
6104  if (!anonymous_data_member_exists_in_class(*anon_dm_m, clazz))
6105  return false;
6106  }
6107  }
6108 
6109  return true;
6110 }
6111 
6112 /// Test if the scope of a given decl is anonymous or anonymous with a
6113 /// naming typedef.
6114 ///
6115 /// @param d the decl consider.
6116 ///
6117 /// @return true iff the scope of @p d is anonymous or anonymous with
6118 /// a naming typedef.
6119 bool
6121 {
6122  if (d.get_has_anonymous_parent()
6123  || (d.get_scope() && d.get_scope()->get_naming_typedef()))
6124  return true;
6125  return false;
6126 }
6127 
6128 /// Test if a given decl is anonymous or has a naming typedef.
6129 ///
6130 /// @param d the decl to consider.
6131 ///
6132 /// @return true iff @p d is anonymous or has a naming typedef.
6133 bool
6135 {
6136  if (d.get_is_anonymous() || d.get_naming_typedef())
6137  return true;
6138  return false;
6139 }
6140 
6141 /// Set the offset of a data member into its containing class.
6142 ///
6143 /// @param m the data member to consider.
6144 ///
6145 /// @param o the offset, in bits.
6146 void
6148 {
6150 
6151  dm_context_rel* ctxt_rel =
6152  dynamic_cast<dm_context_rel*>(m->get_context_rel());
6153  ABG_ASSERT(ctxt_rel);
6154 
6155  ctxt_rel->set_offset_in_bits(o);
6156 }
6157 
6158 /// Get the offset of a data member.
6159 ///
6160 /// @param m the data member to consider.
6161 ///
6162 /// @return the offset (in bits) of @p m in its containing class.
6163 uint64_t
6165 {
6167  const dm_context_rel* ctxt_rel =
6168  dynamic_cast<const dm_context_rel*>(m.get_context_rel());
6169  ABG_ASSERT(ctxt_rel);
6170  return ctxt_rel->get_offset_in_bits();
6171 }
6172 
6173 /// Get the offset of a data member.
6174 ///
6175 /// @param m the data member to consider.
6176 ///
6177 /// @return the offset (in bits) of @p m in its containing class.
6178 uint64_t
6180 {return get_data_member_offset(*m);}
6181 
6182 /// Get the offset of a data member.
6183 ///
6184 /// @param m the data member to consider.
6185 ///
6186 /// @return the offset (in bits) of @p m in its containing class.
6187 uint64_t
6188 get_data_member_offset(const decl_base_sptr d)
6189 {return get_data_member_offset(dynamic_pointer_cast<var_decl>(d));}
6190 
6191 /// Get the offset of the non-static data member that comes after a
6192 /// given one.
6193 ///
6194 /// If there is no data member after after the one given to this
6195 /// function (maybe because the given one is the last data member of
6196 /// the class type) then the function return false.
6197 ///
6198 /// @param klass the class to consider.
6199 ///
6200 /// @param dm the data member before the one we want to retrieve.
6201 ///
6202 /// @param offset out parameter. This parameter is set by the
6203 /// function to the offset of the data member that comes right after
6204 /// the data member @p dm, iff the function returns true.
6205 ///
6206 /// @return true iff the data member coming right after @p dm was
6207 /// found.
6208 bool
6210  const var_decl_sptr& dm,
6211  uint64_t& offset)
6212 {
6213  var_decl_sptr next_dm = get_next_data_member(klass, dm);
6214  if (!next_dm)
6215  return false;
6216  offset = get_data_member_offset(next_dm);
6217  return true;
6218 }
6219 
6220 /// Get the offset of the non-static data member that comes after a
6221 /// given one.
6222 ///
6223 /// If there is no data member after after the one given to this
6224 /// function (maybe because the given one is the last data member of
6225 /// the class type) then the function return false.
6226 ///
6227 /// @param klass the class to consider.
6228 ///
6229 /// @param dm the data member before the one we want to retrieve.
6230 ///
6231 /// @param offset out parameter. This parameter is set by the
6232 /// function to the offset of the data member that comes right after
6233 /// the data member @p dm, iff the function returns true.
6234 ///
6235 /// @return true iff the data member coming right after @p dm was
6236 /// found.
6237 bool
6238 get_next_data_member_offset(const class_or_union_sptr& klass,
6239  const var_decl_sptr& dm,
6240  uint64_t& offset)
6241 {return get_next_data_member_offset(klass.get(), dm, offset);}
6242 
6243 /// Get the absolute offset of a data member.
6244 ///
6245 /// If the data member is part of an anonymous data member then this
6246 /// returns the absolute offset -- relative to the beginning of the
6247 /// containing class of the anonymous data member.
6248 ///
6249 /// @param m the data member to consider.
6250 ///
6251 /// @return the aboslute offset of the data member @p m.
6252 uint64_t
6254 {
6256  const dm_context_rel* ctxt_rel =
6257  dynamic_cast<const dm_context_rel*>(m.get_context_rel());
6258  ABG_ASSERT(ctxt_rel);
6259 
6260  const var_decl *containing_anonymous_data_member =
6261  ctxt_rel->get_anonymous_data_member();
6262 
6263  uint64_t containing_anonymous_data_member_offset = 0;
6264  if (containing_anonymous_data_member)
6265  containing_anonymous_data_member_offset =
6266  get_absolute_data_member_offset(*containing_anonymous_data_member);
6267 
6268  return (ctxt_rel->get_offset_in_bits()
6269  +
6270  containing_anonymous_data_member_offset);
6271 }
6272 
6273 /// Get the absolute offset of a data member.
6274 ///
6275 /// If the data member is part of an anonymous data member then this
6276 /// returns the absolute offset -- relative to the beginning of the
6277 /// containing class of the anonymous data member.
6278 ///
6279 /// @param m the data member to consider.
6280 ///
6281 /// @return the aboslute offset of the data member @p m.
6282 uint64_t
6284 {
6285  if (!m)
6286  return 0;
6288 }
6289 
6290 /// Get the size of a given variable.
6291 ///
6292 /// @param v the variable to consider.
6293 ///
6294 /// @return the size of variable @p v.
6295 uint64_t
6297 {
6298  type_base_sptr t = v->get_type();
6299  ABG_ASSERT(t);
6300 
6301  return t->get_size_in_bits();
6302 }
6303 
6304 /// Set a flag saying if a data member is laid out.
6305 ///
6306 /// @param m the data member to consider.
6307 ///
6308 /// @param l true if @p m is to be considered as laid out.
6309 void
6311 {
6313  dm_context_rel* ctxt_rel =
6314  dynamic_cast<dm_context_rel*>(m->get_context_rel());
6315  ctxt_rel->set_is_laid_out(l);
6316 }
6317 
6318 /// Test whether a data member is laid out.
6319 ///
6320 /// @param m the data member to consider.
6321 ///
6322 /// @return true if @p m is laid out, false otherwise.
6323 bool
6325 {
6327  const dm_context_rel* ctxt_rel =
6328  dynamic_cast<const dm_context_rel*>(m.get_context_rel());
6329 
6330  return ctxt_rel->get_is_laid_out();
6331 }
6332 
6333 /// Test whether a data member is laid out.
6334 ///
6335 /// @param m the data member to consider.
6336 ///
6337 /// @return true if @p m is laid out, false otherwise.
6338 bool
6340 {return get_data_member_is_laid_out(*m);}
6341 
6342 /// Test whether a function_decl is a member function.
6343 ///
6344 /// @param f the function_decl to test.
6345 ///
6346 /// @return true if @p f is a member function, false otherwise.
6347 bool
6349 {return is_member_decl(f);}
6350 
6351 /// Test whether a function_decl is a member function.
6352 ///
6353 /// @param f the function_decl to test.
6354 ///
6355 /// @return true if @p f is a member function, false otherwise.
6356 bool
6358 {return is_member_decl(*f);}
6359 
6360 /// Test whether a function_decl is a member function.
6361 ///
6362 /// @param f the function_decl to test.
6363 ///
6364 /// @return true if @p f is a member function, false otherwise.
6365 bool
6367 {return is_member_decl(*f);}
6368 
6369 /// Test whether a member function is a constructor.
6370 ///
6371 /// @param f the member function to test.
6372 ///
6373 /// @return true if @p f is a constructor, false otherwise.
6374 bool
6376 {
6378 
6379  const method_decl* m = is_method_decl(&f);
6380  ABG_ASSERT(m);
6381 
6382  const mem_fn_context_rel* ctxt =
6383  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6384 
6385  return ctxt->is_constructor();
6386 }
6387 
6388 /// Test whether a member function is a constructor.
6389 ///
6390 /// @param f the member function to test.
6391 ///
6392 /// @return true if @p f is a constructor, false otherwise.
6393 bool
6395 {return get_member_function_is_ctor(*f);}
6396 
6397 
6398 /// Setter for the is_ctor property of the member function.
6399 ///
6400 /// @param f the member function to set.
6401 ///
6402 /// @param f the new boolean value of the is_ctor property. Is true
6403 /// if @p f is a constructor, false otherwise.
6404 void
6406 {
6408 
6409  method_decl* m = is_method_decl(&f);
6410  ABG_ASSERT(m);
6411 
6412  mem_fn_context_rel* ctxt =
6413  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6414 
6415  ctxt->is_constructor(c);
6416 }
6417 
6418 /// Setter for the is_ctor property of the member function.
6419 ///
6420 /// @param f the member function to set.
6421 ///
6422 /// @param f the new boolean value of the is_ctor property. Is true
6423 /// if @p f is a constructor, false otherwise.
6424 void
6427 
6428 /// Test whether a member function is a destructor.
6429 ///
6430 /// @param f the function to test.
6431 ///
6432 /// @return true if @p f is a destructor, false otherwise.
6433 bool
6435 {
6437 
6438  const method_decl* m = is_method_decl(&f);
6439  ABG_ASSERT(m);
6440 
6441  const mem_fn_context_rel* ctxt =
6442  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6443 
6444  return ctxt->is_destructor();
6445 }
6446 
6447 /// Test whether a member function is a destructor.
6448 ///
6449 /// @param f the function to test.
6450 ///
6451 /// @return true if @p f is a destructor, false otherwise.
6452 bool
6454 {return get_member_function_is_dtor(*f);}
6455 
6456 /// Set the destructor-ness property of a member function.
6457 ///
6458 /// @param f the function to set.
6459 ///
6460 /// @param d true if @p f is a destructor, false otherwise.
6461 void
6463 {
6465 
6466  method_decl* m = is_method_decl(&f);
6467  ABG_ASSERT(m);
6468 
6469  mem_fn_context_rel* ctxt =
6470  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6471 
6472  ctxt->is_destructor(d);
6473 }
6474 
6475 /// Set the destructor-ness property of a member function.
6476 ///
6477 /// @param f the function to set.
6478 ///
6479 /// @param d true if @p f is a destructor, false otherwise.
6480 void
6483 
6484 /// Test whether a member function is const.
6485 ///
6486 /// @param f the function to test.
6487 ///
6488 /// @return true if @p f is const, false otherwise.
6489 bool
6491 {
6493 
6494  const method_decl* m = is_method_decl(&f);
6495  ABG_ASSERT(m);
6496 
6497  const mem_fn_context_rel* ctxt =
6498  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6499 
6500  return ctxt->is_const();
6501 }
6502 
6503 /// Test whether a member function is const.
6504 ///
6505 /// @param f the function to test.
6506 ///
6507 /// @return true if @p f is const, false otherwise.
6508 bool
6510 {return get_member_function_is_const(*f);}
6511 
6512 /// set the const-ness property of a member function.
6513 ///
6514 /// @param f the function to set.
6515 ///
6516 /// @param is_const the new value of the const-ness property of @p f
6517 void
6519 {
6521 
6522  method_decl* m = is_method_decl(&f);
6523  ABG_ASSERT(m);
6524 
6525  mem_fn_context_rel* ctxt =
6526  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6527 
6528  ctxt->is_const(is_const);
6529 }
6530 
6531 /// set the const-ness property of a member function.
6532 ///
6533 /// @param f the function to set.
6534 ///
6535 /// @param is_const the new value of the const-ness property of @p f
6536 void
6538 {set_member_function_is_const(*f, is_const);}
6539 
6540 /// Test if a virtual member function has a vtable offset set.
6541 ///
6542 /// @param f the virtual member function to consider.
6543 ///
6544 /// @return true iff the virtual member function has its vtable offset
6545 /// set, i.e, if the vtable offset of @p is different from -1.
6546 bool
6548 {return get_member_function_vtable_offset(f) != -1;}
6549 
6550 /// Get the vtable offset of a member function.
6551 ///
6552 /// @param f the member function to consider.
6553 ///
6554 /// @return the vtable offset of @p f. Note that a vtable offset of
6555 /// value -1 means that the member function does *NOT* yet have a
6556 /// vtable offset associated to it.
6557 ssize_t
6559 {
6561 
6562  const method_decl* m =
6563  dynamic_cast<const method_decl*>(&f);
6564  ABG_ASSERT(m);
6565 
6566  const mem_fn_context_rel* ctxt =
6567  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6568 
6569  return ctxt->vtable_offset();
6570 }
6571 
6572 /// Get the vtable offset of a member function.
6573 ///
6574 /// @param f the member function to consider.
6575 ///
6576 /// @return the vtable offset of @p f. Note that a vtable offset of
6577 /// value -1 means that the member function does *NOT* yet have a
6578 /// vtable offset associated to it.
6579 ssize_t
6582 
6583 /// Set the vtable offset of a member function.
6584 ///
6585 /// @param f the member function to consider.
6586 ///
6587 /// @param s the new vtable offset. Please note that a vtable offset
6588 /// of value -1 means that the virtual member function does not (yet)
6589 /// have any vtable offset associated to it.
6590 void
6592 {
6594 
6595  method_decl* m = is_method_decl(&f);
6596  ABG_ASSERT(m);
6597 
6598  mem_fn_context_rel* ctxt =
6599  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6600 
6601  ctxt->vtable_offset(s);
6602 }
6603 
6604 /// Get the vtable offset of a member function.
6605 ///
6606 /// @param f the member function to consider.
6607 ///
6608 /// @param s the new vtable offset. Please note that a vtable offset
6609 /// of value -1 means that the virtual member function does not (yet)
6610 /// have any vtable offset associated to it.
6611 void
6613 {return set_member_function_vtable_offset(*f, s);}
6614 
6615 /// Test if a given member function is virtual.
6616 ///
6617 /// @param mem_fn the member function to consider.
6618 ///
6619 /// @return true iff a @p mem_fn is virtual.
6620 bool
6622 {
6624 
6625  const method_decl* m =
6626  dynamic_cast<const method_decl*>(&f);
6627  ABG_ASSERT(m);
6628 
6629  const mem_fn_context_rel* ctxt =
6630  dynamic_cast<const mem_fn_context_rel*>(m->get_context_rel());
6631 
6632  return ctxt->is_virtual();
6633 }
6634 
6635 /// Test if a given member function is virtual.
6636 ///
6637 /// @param mem_fn the member function to consider.
6638 ///
6639 /// @return true iff a @p mem_fn is virtual.
6640 bool
6642 {return mem_fn ? get_member_function_is_virtual(*mem_fn) : false;}
6643 
6644 /// Test if a given member function is virtual.
6645 ///
6646 /// @param mem_fn the member function to consider.
6647 ///
6648 /// @return true iff a @p mem_fn is virtual.
6649 bool
6651 {return mem_fn ? get_member_function_is_virtual(*mem_fn) : false;}
6652 
6653 /// Set the virtual-ness of a member function.
6654 ///
6655 /// @param f the member function to consider.
6656 ///
6657 /// @param is_virtual set to true if the function is virtual.
6658 void
6660 {
6662 
6663  method_decl* m = is_method_decl(&f);
6664  ABG_ASSERT(m);
6665 
6666  mem_fn_context_rel* ctxt =
6667  dynamic_cast<mem_fn_context_rel*>(m->get_context_rel());
6668 
6669  ctxt->is_virtual(is_virtual);
6670 }
6671 
6672 /// Set the virtual-ness of a member function.
6673 ///
6674 /// @param f the member function to consider.
6675 ///
6676 /// @param is_virtual set to true if the function is virtual.
6677 void
6679 {
6680  if (fn)
6681  {
6682  set_member_function_is_virtual(*fn, is_virtual);
6684  (dynamic_pointer_cast<method_decl>(fn));
6685  }
6686 }
6687 
6688 /// Recursively returns the the underlying type of a typedef. The
6689 /// return type should not be a typedef of anything anymore.
6690 ///
6691 ///
6692 /// Also recursively strip typedefs from the sub-types of the type
6693 /// given in arguments.
6694 ///
6695 /// Note that this function builds types in which typedefs are
6696 /// stripped off. Usually, types are held by their scope, so their
6697 /// life time is bound to the life time of their scope. But as this
6698 /// function cannot really insert the built type into it's scope, it
6699 /// must ensure that the newly built type stays live long enough.
6700 ///
6701 /// So, if the newly built type has a canonical type, this function
6702 /// returns the canonical type. Otherwise, this function ensure that
6703 /// the newly built type has a life time that is the same as the life
6704 /// time of the entire libabigail library.
6705 ///
6706 /// @param type the type to strip the typedefs from.
6707 ///
6708 /// @return the resulting type stripped from its typedefs, or just
6709 /// return @p type if it has no typedef in any of its sub-types.
6710 type_base_sptr
6711 strip_typedef(const type_base_sptr type)
6712 {
6713  if (!type)
6714  return type;
6715 
6716  // If type is a class type then do not try to strip typedefs from it.
6717  // And if it has no canonical type (which can mean that it's a
6718  // declaration-only class), then, make sure its live for ever and
6719  // return it.
6720  if (class_decl_sptr cl = is_class_type(type))
6721  {
6722  if (!cl->get_canonical_type())
6723  keep_type_alive(type);
6724  return type;
6725  }
6726 
6727  const environment& env = type->get_environment();
6728  type_base_sptr t = type;
6729 
6730  if (const typedef_decl_sptr ty = is_typedef(t))
6731  t = strip_typedef(type_or_void(ty->get_underlying_type(), env));
6732  else if (const reference_type_def_sptr ty = is_reference_type(t))
6733  {
6734  type_base_sptr p = strip_typedef(type_or_void(ty->get_pointed_to_type(),
6735  env));
6736  ABG_ASSERT(p);
6737  t.reset(new reference_type_def(p,
6738  ty->is_lvalue(),
6739  ty->get_size_in_bits(),
6740  ty->get_alignment_in_bits(),
6741  ty->get_location()));
6742  }
6743  else if (const pointer_type_def_sptr ty = is_pointer_type(t))
6744  {
6745  type_base_sptr p = strip_typedef(type_or_void(ty->get_pointed_to_type(),
6746  env));
6747  ABG_ASSERT(p);
6748  t.reset(new pointer_type_def(p,
6749  ty->get_size_in_bits(),
6750  ty->get_alignment_in_bits(),
6751  ty->get_location()));
6752  }
6753  else if (const qualified_type_def_sptr ty = is_qualified_type(t))
6754  {
6755  type_base_sptr p = strip_typedef(type_or_void(ty->get_underlying_type(),
6756  env));
6757  ABG_ASSERT(p);
6758  t.reset(new qualified_type_def(p,
6759  ty->get_cv_quals(),
6760  ty->get_location()));
6761  }
6762  else if (const array_type_def_sptr ty = is_array_type(t))
6763  {
6764  type_base_sptr p = strip_typedef(ty->get_element_type());
6765  ABG_ASSERT(p);
6766  t.reset(new array_type_def(p, ty->get_subranges(), ty->get_location()));
6767  }
6768  else if (const method_type_sptr ty = is_method_type(t))
6769  {
6771  for (function_decl::parameters::const_iterator i =
6772  ty->get_parameters().begin();
6773  i != ty->get_parameters().end();
6774  ++i)
6775  {
6777  type_base_sptr typ = strip_typedef(p->get_type());
6778  ABG_ASSERT(typ);
6780  (new function_decl::parameter(typ,
6781  p->get_index(),
6782  p->get_name(),
6783  p->get_location(),
6784  p->get_variadic_marker(),
6785  p->get_is_artificial()));
6786  parm.push_back(stripped);
6787  }
6788  type_base_sptr p = strip_typedef(ty->get_return_type());
6789  ABG_ASSERT(!!p == !!ty->get_return_type());
6790  t.reset(new method_type(p, ty->get_class_type(),
6791  parm, ty->get_is_const(),
6792  ty->get_size_in_bits(),
6793  ty->get_alignment_in_bits()));
6794  }
6795  else if (const function_type_sptr ty = is_function_type(t))
6796  {
6798  for (function_decl::parameters::const_iterator i =
6799  ty->get_parameters().begin();
6800  i != ty->get_parameters().end();
6801  ++i)
6802  {
6804  type_base_sptr typ = strip_typedef(p->get_type());
6805  ABG_ASSERT(typ);
6807  (new function_decl::parameter(typ,
6808  p->get_index(),
6809  p->get_name(),
6810  p->get_location(),
6811  p->get_variadic_marker(),
6812  p->get_is_artificial()));
6813  parm.push_back(stripped);
6814  }
6815  type_base_sptr p = strip_typedef(ty->get_return_type());
6816  ABG_ASSERT(!!p == !!ty->get_return_type());
6817  t.reset(new function_type(p, parm,
6818  ty->get_size_in_bits(),
6819  ty->get_alignment_in_bits()));
6820  }
6821 
6822  if (!t->get_translation_unit())
6823  t->set_translation_unit(type->get_translation_unit());
6824 
6825  if (!(type->get_canonical_type() && canonicalize(t)))
6826  keep_type_alive(t);
6827 
6828  return t->get_canonical_type() ? t->get_canonical_type() : t;
6829 }
6830 
6831 /// Strip qualification from a qualified type, when it makes sense.
6832 ///
6833 /// DWARF constructs "const reference". This is redundant because a
6834 /// reference is always const. It also constructs the useless "const
6835 /// void" type. The issue is these redundant types then leak into the
6836 /// IR and make for bad diagnostics.
6837 ///
6838 /// This function thus strips the const qualifier from the type in
6839 /// that case. It might contain code to strip other cases like this
6840 /// in the future.
6841 ///
6842 /// @param t the type to strip const qualification from.
6843 ///
6844 /// @return the stripped type or just return @p t.
6845 decl_base_sptr
6846 strip_useless_const_qualification(const qualified_type_def_sptr t)
6847 {
6848  if (!t)
6849  return t;
6850 
6851  decl_base_sptr result = t;
6852  type_base_sptr u = t->get_underlying_type();
6853  const environment& env = t->get_environment();
6854 
6855  if ((t->get_cv_quals() & qualified_type_def::CV_CONST
6856  && (is_reference_type(u)))
6857  || (t->get_cv_quals() & qualified_type_def::CV_CONST
6858  && env.is_void_type(u))
6859  || t->get_cv_quals() == qualified_type_def::CV_NONE)
6860  // Let's strip the const qualifier because a reference is always
6861  // 'const' and a const void doesn't make sense. They will just
6862  // lead to spurious changes later down the pipeline, that we'll
6863  // have to deal with by doing painful and error-prone editing of
6864  // the diff IR. Dropping that useless and inconsistent artefact
6865  // right here seems to be a good way to go.
6866  result = is_decl(u);
6867 
6868  return result;
6869 }
6870 
6871 /// Merge redundant qualifiers from a tree of qualified types.
6872 ///
6873 /// Suppose a tree of qualified types leads to:
6874 ///
6875 /// const virtual const restrict const int;
6876 ///
6877 /// Suppose the IR tree of qualified types ressembles (with C meaning
6878 /// const, V meaning virtual and R meaning restrict):
6879 ///
6880 /// [C|V]-->[C|R] -->[C] --> [int].
6881 ///
6882 /// This function walks the IR and remove the redundant CV qualifiers
6883 /// so the IR becomes:
6884 ///
6885 /// [C|V] --> [R] --> [] -->[int].
6886 ///
6887 /// Note that the empty qualified type (noted []) represents a
6888 /// qualified type with no qualifier. It's rare, but it can exist.
6889 /// I've put it here just for the sake of example.
6890 ///
6891 /// The resulting IR thus represents the (merged) type:
6892 ///
6893 /// const virtual restrict int.
6894 ///
6895 /// This function is a sub-routine of the overload @ref
6896 /// strip_useless_const_qualification which doesn't return any value.
6897 ///
6898 /// @param t the qualified type to consider.
6899 ///
6900 /// @param redundant_quals the (redundant) qualifiers to be removed
6901 /// from the qualifiers of the underlying types of @p t.
6902 ///
6903 /// @return the underlying type of @p t which might have had its
6904 /// redundant qualifiers removed.
6905 static qualified_type_def_sptr
6906 strip_redundant_quals_from_underyling_types(const qualified_type_def_sptr& t,
6907  qualified_type_def::CV redundant_quals)
6908 {
6909  if (!t)
6910  return t;
6911 
6912  // We must NOT edit canonicalized types.
6913  ABG_ASSERT(!t->get_canonical_type());
6914 
6915  qualified_type_def_sptr underlying_qualified_type =
6916  is_qualified_type(t->get_underlying_type());
6917 
6918  // Let's build 'currated qualifiers' that are the qualifiers of the
6919  // current type from which redundant qualifiers are removed.
6920  qualified_type_def::CV currated_quals = t->get_cv_quals();
6921 
6922  // Remove the redundant qualifiers from these currated qualifiers
6923  currated_quals &= ~redundant_quals;
6924  t->set_cv_quals(currated_quals);
6925 
6926  // The redundant qualifiers, moving forward, is now the union of the
6927  // previous set of redundant qualifiers and the currated qualifiers.
6928  redundant_quals |= currated_quals;
6929 
6930  qualified_type_def_sptr result = t;
6931  if (underlying_qualified_type)
6932  // Now remove the redundant qualifiers from the qualified types
6933  // potentially carried by the underlying type.
6934  result =
6935  strip_redundant_quals_from_underyling_types(underlying_qualified_type,
6936  redundant_quals);
6937 
6938  return result;
6939 }
6940 
6941 /// Merge redundant qualifiers from a tree of qualified types.
6942 ///
6943 /// Suppose a tree of qualified types leads to:
6944 ///
6945 /// const virtual const restrict const int;
6946 ///
6947 /// Suppose the IR tree of qualified types ressembles (with C meaning
6948 /// const, V meaning virtual and R meaning restrict):
6949 ///
6950 /// [C|V]-->[C|R] -->[C] --> [int].
6951 ///
6952 /// This function walks the IR and remove the redundant CV qualifiers
6953 /// so the IR becomes:
6954 ///
6955 /// [C|V] --> [R] --> [] -->[int].
6956 ///
6957 /// Note that the empty qualified type (noted []) represents a
6958 /// qualified type with no qualifier. It's rare, but it can exist.
6959 /// I've put it here just for the sake of example.
6960 ///
6961 /// The resulting IR thus represents the (merged) type:
6962 ///
6963 /// const virtual restrict int.
6964 ///
6965 /// @param t the qualified type to consider. The IR below the
6966 /// argument to this parameter will be edited to remove redundant
6967 /// qualifiers where applicable.
6968 void
6969 strip_redundant_quals_from_underyling_types(const qualified_type_def_sptr& t)
6970 {
6971  if (!t)
6972  return;
6973 
6974  qualified_type_def::CV redundant_quals = qualified_type_def::CV_NONE;
6975  strip_redundant_quals_from_underyling_types(t, redundant_quals);
6976 }
6977 
6978 /// Return the leaf underlying type node of a @ref typedef_decl node.
6979 ///
6980 /// If the underlying type of a @ref typedef_decl node is itself a
6981 /// @ref typedef_decl node, then recursively look at the underlying
6982 /// type nodes to get the first one that is not a a @ref typedef_decl
6983 /// node. This is what a leaf underlying type node means.
6984 ///
6985 /// Otherwise, if the underlying type node of @ref typedef_decl is
6986 /// *NOT* a @ref typedef_decl node, then just return the underlying
6987 /// type node.
6988 ///
6989 /// And if the type node considered is not a @ref typedef_decl node,
6990 /// then just return it.
6991 ///
6992 /// @return the leaf underlying type node of a @p type.
6993 type_base_sptr
6994 peel_typedef_type(const type_base_sptr& type)
6995 {
6996  typedef_decl_sptr t = is_typedef(type);
6997  if (!t)
6998  return type;
6999 
7000  if (is_typedef(t->get_underlying_type()))
7001  return peel_typedef_type(t->get_underlying_type());
7002  return t->get_underlying_type();
7003 }
7004 
7005 /// Return the leaf underlying type node of a @ref typedef_decl node.
7006 ///
7007 /// If the underlying type of a @ref typedef_decl node is itself a
7008 /// @ref typedef_decl node, then recursively look at the underlying
7009 /// type nodes to get the first one that is not a a @ref typedef_decl
7010 /// node. This is what a leaf underlying type node means.
7011 ///
7012 /// Otherwise, if the underlying type node of @ref typedef_decl is
7013 /// *NOT* a @ref typedef_decl node, then just return the underlying
7014 /// type node.
7015 ///
7016 /// And if the type node considered is not a @ref typedef_decl node,
7017 /// then just return it.
7018 ///
7019 /// @return the leaf underlying type node of a @p type.
7020 const type_base*
7022 {
7023  const typedef_decl* t = is_typedef(type);
7024  if (!t)
7025  return type;
7026 
7027  return peel_typedef_type(t->get_underlying_type()).get();
7028 }
7029 
7030 /// Return the leaf pointed-to type node of a @ref pointer_type_def
7031 /// node.
7032 ///
7033 /// If the pointed-to type of a @ref pointer_type_def node is itself a
7034 /// @ref pointer_type_def node, then recursively look at the
7035 /// pointed-to type nodes to get the first one that is not a a @ref
7036 /// pointer_type_def node. This is what a leaf pointed-to type node
7037 /// means.
7038 ///
7039 /// Otherwise, if the pointed-to type node of @ref pointer_type_def is
7040 /// *NOT* a @ref pointer_type_def node, then just return the
7041 /// pointed-to type node.
7042 ///
7043 /// And if the type node considered is not a @ref pointer_type_def
7044 /// node, then just return it.
7045 ///
7046 /// @return the leaf pointed-to type node of a @p type.
7047 type_base_sptr
7048 peel_pointer_type(const type_base_sptr& type)
7049 {
7051  if (!t)
7052  return type;
7053 
7054  if (is_pointer_type(t->get_pointed_to_type()))
7055  return peel_pointer_type(t->get_pointed_to_type());
7056  return t->get_pointed_to_type();
7057 }
7058 
7059 /// Return the leaf pointed-to type node of a @ref pointer_type_def
7060 /// node.
7061 ///
7062 /// If the pointed-to type of a @ref pointer_type_def node is itself a
7063 /// @ref pointer_type_def node, then recursively look at the
7064 /// pointed-to type nodes to get the first one that is not a a @ref
7065 /// pointer_type_def node. This is what a leaf pointed-to type node
7066 /// means.
7067 ///
7068 /// Otherwise, if the pointed-to type node of @ref pointer_type_def is
7069 /// *NOT* a @ref pointer_type_def node, then just return the
7070 /// pointed-to type node.
7071 ///
7072 /// And if the type node considered is not a @ref pointer_type_def
7073 /// node, then just return it.
7074 ///
7075 /// @return the leaf pointed-to type node of a @p type.
7076 const type_base*
7078 {
7079  const pointer_type_def* t = is_pointer_type(type);
7080  if (!t)
7081  return type;
7082 
7083  return peel_pointer_type(t->get_pointed_to_type()).get();
7084 }
7085 
7086 /// Return the leaf pointed-to type node of a @ref reference_type_def
7087 /// node.
7088 ///
7089 /// If the pointed-to type of a @ref reference_type_def node is itself
7090 /// a @ref reference_type_def node, then recursively look at the
7091 /// pointed-to type nodes to get the first one that is not a a @ref
7092 /// reference_type_def node. This is what a leaf pointed-to type node
7093 /// means.
7094 ///
7095 /// Otherwise, if the pointed-to type node of @ref reference_type_def
7096 /// is *NOT* a @ref reference_type_def node, then just return the
7097 /// pointed-to type node.
7098 ///
7099 /// And if the type node considered is not a @ref reference_type_def
7100 /// node, then just return it.
7101 ///
7102 /// @return the leaf pointed-to type node of a @p type.
7103 type_base_sptr
7104 peel_reference_type(const type_base_sptr& type)
7105 {
7107  if (!t)
7108  return type;
7109 
7110  if (is_reference_type(t->get_pointed_to_type()))
7111  return peel_reference_type(t->get_pointed_to_type());
7112  return t->get_pointed_to_type();
7113 }
7114 
7115 /// Return the leaf pointed-to type node of a @ref reference_type_def
7116 /// node.
7117 ///
7118 /// If the pointed-to type of a @ref reference_type_def node is itself
7119 /// a @ref reference_type_def node, then recursively look at the
7120 /// pointed-to type nodes to get the first one that is not a a @ref
7121 /// reference_type_def node. This is what a leaf pointed-to type node
7122 /// means.
7123 ///
7124 /// Otherwise, if the pointed-to type node of @ref reference_type_def
7125 /// is *NOT* a @ref reference_type_def node, then just return the
7126 /// pointed-to type node.
7127 ///
7128 /// And if the type node considered is not a @ref reference_type_def
7129 /// node, then just return it.
7130 ///
7131 /// @return the leaf pointed-to type node of a @p type.
7132 const type_base*
7134 {
7135  const reference_type_def* t = is_reference_type(type);
7136  if (!t)
7137  return type;
7138 
7139  return peel_reference_type(t->get_pointed_to_type()).get();
7140 }
7141 
7142 /// Return the leaf element type of an array.
7143 ///
7144 /// If the element type is itself an array, then recursively return
7145 /// the element type of that array itself.
7146 ///
7147 /// @param type the array type to consider. If this is not an array
7148 /// type, this type is returned by the function.
7149 ///
7150 /// @return the leaf element type of the array @p type, or, if it's
7151 /// not an array type, then just return @p.
7152 const type_base_sptr
7153 peel_array_type(const type_base_sptr& type)
7154 {
7155  const array_type_def_sptr t = is_array_type(type);
7156  if (!t)
7157  return type;
7158 
7159  return peel_array_type(t->get_element_type());
7160 }
7161 
7162 /// Return the leaf element type of an array.
7163 ///
7164 /// If the element type is itself an array, then recursively return
7165 /// the element type of that array itself.
7166 ///
7167 /// @param type the array type to consider. If this is not an array
7168 /// type, this type is returned by the function.
7169 ///
7170 /// @return the leaf element type of the array @p type, or, if it's
7171 /// not an array type, then just return @p.
7172 const type_base*
7174 {
7175  const array_type_def* t = is_array_type(type);
7176  if (!t)
7177  return type;
7178 
7179  return peel_array_type(t->get_element_type()).get();
7180 }
7181 
7182 /// Return the leaf underlying type of a qualified type.
7183 ///
7184 /// If the underlying type is itself a qualified type, then
7185 /// recursively return the first underlying type of that qualified
7186 /// type to return the first underlying type that is not a qualified type.
7187 ///
7188 /// If the underlying type is NOT a qualified type, then just return
7189 /// that underlying type.
7190 ///
7191 /// @param type the qualified type to consider.
7192 ///
7193 /// @return the leaf underlying type.
7194 const type_base*
7196 {
7197  const qualified_type_def* t = is_qualified_type(type);
7198  if (!t)
7199  return type;
7200 
7201  return peel_qualified_type(t->get_underlying_type().get());
7202 }
7203 
7204 /// Return the leaf underlying type of a qualified type.
7205 ///
7206 /// If the underlying type is itself a qualified type, then
7207 /// recursively return the first underlying type of that qualified
7208 /// type to return the first underlying type that is not a qualified type.
7209 ///
7210 /// If the underlying type is NOT a qualified type, then just return
7211 /// that underlying type.
7212 ///
7213 /// @param type the qualified type to consider.
7214 ///
7215 /// @return the leaf underlying type.
7216 const type_base_sptr
7217 peel_qualified_type(const type_base_sptr& type)
7218 {
7219  const qualified_type_def_sptr t = is_qualified_type(type);
7220  if (!t)
7221  return type;
7222 
7223  return peel_qualified_type(t->get_underlying_type());
7224 }
7225 
7226 /// Return the leaf underlying type of a qualified or typedef type.
7227 ///
7228 /// If the underlying type is itself a qualified or typedef type, then
7229 /// recursively return the first underlying type of that qualified or
7230 /// typedef type to return the first underlying type that is not a
7231 /// qualified or typedef type.
7232 ///
7233 /// If the underlying type is NOT a qualified nor a typedef type, then
7234 /// just return that underlying type.
7235 ///
7236 /// @param type the qualified or typedef type to consider.
7237 ///
7238 /// @return the leaf underlying type.
7239 type_base*
7241 {
7242  while (is_typedef(type) || is_qualified_type(type))
7243  {
7244  if (const typedef_decl* t = is_typedef(type))
7245  type = peel_typedef_type(t);
7246 
7247  if (const qualified_type_def* t = is_qualified_type(type))
7248  type = peel_qualified_type(t);
7249  }
7250 
7251  return const_cast<type_base*>(type);
7252 }
7253 
7254 /// Return the leaf underlying type of a qualified or typedef type.
7255 ///
7256 /// If the underlying type is itself a qualified or typedef type, then
7257 /// recursively return the first underlying type of that qualified or
7258 /// typedef type to return the first underlying type that is not a
7259 /// qualified or typedef type.
7260 ///
7261 /// If the underlying type is NOT a qualified nor a typedef type, then
7262 /// just return that underlying type.
7263 ///
7264 /// @param type the qualified or typedef type to consider.
7265 ///
7266 /// @return the leaf underlying type.
7267 type_base_sptr
7268 peel_qualified_or_typedef_type(const type_base_sptr &t)
7269 {
7270  type_base_sptr type = t;
7271  while (is_typedef(type) || is_qualified_type(type))
7272  {
7273  if (typedef_decl_sptr t = is_typedef(type))
7274  type = peel_typedef_type(t);
7275 
7276  if (qualified_type_def_sptr t = is_qualified_type(type))
7277  type = peel_qualified_type(t);
7278  }
7279 
7280  return type;
7281 }
7282 
7283 /// Return the leaf underlying or pointed-to type node of a @ref
7284 /// typedef_decl, @ref pointer_type_def, @ref reference_type_def,
7285 /// or @ref array_type_def node.
7286 ///
7287 /// @param type the type to peel.
7288 ///
7289 /// @return the leaf underlying or pointed-to type node of @p type.
7290 type_base_sptr
7291 peel_typedef_pointer_or_reference_type(const type_base_sptr type)
7292 {
7293  type_base_sptr typ = type;
7294  while (is_typedef(typ)
7295  || is_pointer_type(typ)
7296  || is_reference_type(typ)
7297  || is_array_type(typ))
7298  {
7299  if (typedef_decl_sptr t = is_typedef(typ))
7300  typ = peel_typedef_type(t);
7301 
7303  typ = peel_pointer_type(t);
7304 
7306  typ = peel_reference_type(t);
7307 
7308  if (const array_type_def_sptr t = is_array_type(typ))
7309  typ = peel_array_type(t);
7310  }
7311 
7312  return typ;
7313 }
7314 
7315 /// Return the leaf underlying or pointed-to type node of a @ref
7316 /// typedef_decl, @ref pointer_type_def or @ref reference_type_def
7317 /// node.
7318 ///
7319 /// @param type the type to peel.
7320 ///
7321 /// @return the leaf underlying or pointed-to type node of @p type.
7322 type_base*
7324 {
7325  while (is_typedef(type)
7326  || is_pointer_type(type)
7327  || is_reference_type(type)
7328  || is_array_type(type))
7329  {
7330  if (const typedef_decl* t = is_typedef(type))
7331  type = peel_typedef_type(t);
7332 
7333  if (const pointer_type_def* t = is_pointer_type(type))
7334  type = peel_pointer_type(t);
7335 
7336  if (const reference_type_def* t = is_reference_type(type))
7337  type = peel_reference_type(t);
7338 
7339  if (const array_type_def* t = is_array_type(type))
7340  type = peel_array_type(t);
7341  }
7342 
7343  return const_cast<type_base*>(type);
7344 }
7345 
7346 /// Return the leaf underlying or pointed-to type node of a @ref
7347 /// typedef_decl, @ref pointer_type_def or @ref reference_type_def
7348 /// node.
7349 ///
7350 /// @param type the type to peel.
7351 ///
7352 /// @return the leaf underlying or pointed-to type node of @p type.
7353 type_base*
7355  bool peel_qual_type)
7356 {
7357  while (is_typedef(type)
7358  || is_pointer_type(type)
7359  || is_reference_type(type)
7360  || is_array_type(type)
7361  || (peel_qual_type && is_qualified_type(type)))
7362  {
7363  if (const typedef_decl* t = is_typedef(type))
7364  type = peel_typedef_type(t);
7365 
7366  if (const pointer_type_def* t = is_pointer_type(type))
7367  type = peel_pointer_type(t);
7368 
7369  if (const reference_type_def* t = is_reference_type(type))
7370  type = peel_reference_type(t);
7371 
7372  if (const array_type_def* t = is_array_type(type))
7373  type = peel_array_type(t);
7374 
7375  if (peel_qual_type)
7376  if (const qualified_type_def* t = is_qualified_type(type))
7377  type = peel_qualified_type(t);
7378  }
7379 
7380  return const_cast<type_base*>(type);
7381 }
7382 
7383 /// Return the leaf underlying or pointed-to type node of a, @ref
7384 /// pointer_type_def, @ref reference_type_def or @ref
7385 /// qualified_type_def type node.
7386 ///
7387 /// @param type the type to peel.
7388 ///
7389 /// @param peel_qualified_type if true, also peel qualified types.
7390 ///
7391 /// @return the leaf underlying or pointed-to type node of @p type.
7392 type_base*
7394  bool peel_qual_type)
7395 {
7396  while (is_pointer_type(type)
7397  || is_reference_type(type)
7398  || is_array_type(type)
7399  || (peel_qual_type && is_qualified_type(type)))
7400  {
7401  if (const pointer_type_def* t = is_pointer_type(type))
7402  type = peel_pointer_type(t);
7403 
7404  if (const reference_type_def* t = is_reference_type(type))
7405  type = peel_reference_type(t);
7406 
7407  if (const array_type_def* t = is_array_type(type))
7408  type = peel_array_type(t);
7409 
7410  if (peel_qual_type)
7411  if (const qualified_type_def* t = is_qualified_type(type))
7412  type = peel_qualified_type(t);
7413  }
7414 
7415  return const_cast<type_base*>(type);
7416 }
7417 
7418 /// Clone an array type.
7419 ///
7420 /// Note that the element type of the new array is shared witht the
7421 /// old one.
7422 ///
7423 /// @param array the array type to clone.
7424 ///
7425 /// @return a newly built array type. Note that it needs to be added
7426 /// to a scope (e.g, using add_decl_to_scope) for its lifetime to be
7427 /// bound to the one of that scope. Otherwise, its lifetime is bound
7428 /// to the lifetime of its containing shared pointer.
7431 {
7432  vector<array_type_def::subrange_sptr> subranges;
7433 
7434  for (vector<array_type_def::subrange_sptr>::const_iterator i =
7435  array->get_subranges().begin();
7436  i != array->get_subranges().end();
7437  ++i)
7438  {
7440  (new array_type_def::subrange_type(array->get_environment(),
7441  (*i)->get_name(),
7442  (*i)->get_lower_bound(),
7443  (*i)->get_upper_bound(),
7444  (*i)->get_underlying_type(),
7445  (*i)->get_location(),
7446  (*i)->get_language()));
7447  subrange->is_infinite((*i)->is_infinite());
7448  if (scope_decl *scope = (*i)->get_scope())
7449  add_decl_to_scope(subrange, scope);
7450  subranges.push_back(subrange);
7451  }
7452 
7453  array_type_def_sptr result
7454  (new array_type_def(array->get_element_type(),
7455  subranges, array->get_location()));
7456 
7457  return result;
7458 }
7459 
7460 /// Clone a typedef type.
7461 ///
7462 /// Note that the underlying type of the newly constructed typedef is
7463 /// shared with the old one.
7464 ///
7465 /// @param t the typedef to clone.
7466 ///
7467 /// @return the newly constructed typedef. Note that it needs to be
7468 /// added to a scope (e.g, using add_decl_to_scope) for its lifetime
7469 /// to be bound to the one of that scope. Otherwise, its lifetime is
7470 /// bound to the lifetime of its containing shared pointer.
7473 {
7474  if (!t)
7475  return t;
7476 
7477  typedef_decl_sptr result
7478  (new typedef_decl(t->get_name(), t->get_underlying_type(),
7479  t->get_location(), t->get_linkage_name(),
7480  t->get_visibility()));
7481  return result;
7482 }
7483 
7484 /// Clone a qualifiend type.
7485 ///
7486 /// Note that underlying type of the newly constructed qualified type
7487 /// is shared with the old one.
7488 ///
7489 /// @param t the qualified type to clone.
7490 ///
7491 /// @return the newly constructed qualified type. Note that it needs
7492 /// to be added to a scope (e.g, using add_decl_to_scope) for its
7493 /// lifetime to be bound to the one of that scope. Otherwise, its
7494 /// lifetime is bound to the lifetime of its containing shared
7495 /// pointer.
7496 qualified_type_def_sptr
7497 clone_qualified_type(const qualified_type_def_sptr& t)
7498 {
7499  if (!t)
7500  return t;
7501 
7502  qualified_type_def_sptr result
7503  (new qualified_type_def(t->get_underlying_type(),
7504  t->get_cv_quals(), t->get_location()));
7505 
7506  return result;
7507 }
7508 
7509 /// Clone a typedef, an array or a qualified tree.
7510 ///
7511 /// @param type the typedef, array or qualified tree to clone. any
7512 /// order.
7513 ///
7514 /// @return the cloned type, or NULL if @type was neither a typedef,
7515 /// array nor a qualified type.
7516 static type_base_sptr
7517 clone_typedef_array_qualified_type(type_base_sptr type)
7518 {
7519  if (!type)
7520  return type;
7521 
7522  scope_decl* scope = is_decl(type) ? is_decl(type)->get_scope() : 0;
7523  type_base_sptr result;
7524 
7525  if (typedef_decl_sptr t = is_typedef(type))
7526  result = clone_typedef(is_typedef(t));
7527  else if (qualified_type_def_sptr t = is_qualified_type(type))
7528  result = clone_qualified_type(t);
7529  else if (array_type_def_sptr t = is_array_type(type))
7530  result = clone_array(t);
7531  else
7532  return type_base_sptr();
7533 
7534  if (scope)
7535  add_decl_to_scope(is_decl(result), scope);
7536 
7537  return result;
7538 }
7539 
7540 /// Clone a type tree made of an array or a typedef of array.
7541 ///
7542 /// Note that this can be a tree which root node is a typedef an which
7543 /// sub-tree can be any arbitrary combination of typedef, qualified
7544 /// type and arrays.
7545 ///
7546 /// @param t the array or typedef of qualified array to consider.
7547 ///
7548 /// @return a clone of @p t.
7549 type_base_sptr
7550 clone_array_tree(const type_base_sptr t)
7551 {
7553 
7554  scope_decl* scope = is_decl(t)->get_scope();
7555  type_base_sptr result = clone_typedef_array_qualified_type(t);
7556  ABG_ASSERT(is_typedef_of_array(result) || is_array_type(result));
7557 
7558  type_base_sptr subtree;
7559  if (typedef_decl_sptr type = is_typedef(result))
7560  {
7561  type_base_sptr s =
7562  clone_typedef_array_qualified_type(type->get_underlying_type());
7563  if (s)
7564  {
7565  subtree = s;
7566  type->set_underlying_type(subtree);
7567  }
7568  }
7569  else if (array_type_def_sptr type = is_array_type(result))
7570  {
7571  type_base_sptr s =
7572  clone_typedef_array_qualified_type(type->get_element_type());
7573  if (s)
7574  {
7575  subtree = s;
7576  type->set_element_type(subtree);
7577  }
7578  }
7579  add_decl_to_scope(is_decl(subtree), scope);
7580 
7581  for (;;)
7582  {
7583  if (typedef_decl_sptr t = is_typedef(subtree))
7584  {
7585  type_base_sptr s =
7586  clone_typedef_array_qualified_type(t->get_underlying_type());
7587  if (s)
7588  {
7589  scope_decl* scope =
7590  is_decl(t->get_underlying_type())->get_scope();
7591  ABG_ASSERT(scope);
7592  add_decl_to_scope(is_decl(s), scope);
7593  t->set_underlying_type (s);
7594  subtree = s;
7595  }
7596  else
7597  break;
7598  }
7599  else if (qualified_type_def_sptr t = is_qualified_type(subtree))
7600  {
7601  type_base_sptr s =
7602  clone_typedef_array_qualified_type(t->get_underlying_type());
7603  if (s)
7604  {
7605  scope_decl* scope =
7606  is_decl(t->get_underlying_type())->get_scope();
7607  ABG_ASSERT(scope);
7608  add_decl_to_scope(is_decl(s), scope);
7609  t->set_underlying_type(s);
7610  subtree = s;
7611  }
7612  else
7613  break;
7614  }
7615  else if (array_type_def_sptr t = is_array_type(subtree))
7616  {
7617  type_base_sptr e = t->get_element_type();
7618  if (is_typedef(e) || is_qualified_type(e))
7619  {
7620  type_base_sptr s =
7621  clone_typedef_array_qualified_type(e);
7622  if (s)
7623  {
7624  scope_decl* scope = is_decl(e)->get_scope();
7625  ABG_ASSERT(scope);
7626  add_decl_to_scope(is_decl(s), scope);
7627  t->set_element_type(s);
7628  }
7629  else
7630  break;
7631  }
7632  break;
7633  }
7634  else
7635  break;
7636  }
7637  return result;
7638 }
7639 
7640 /// Update the qualified name of a given sub-tree.
7641 ///
7642 /// @param d the sub-tree for which to update the qualified name.
7643 static void
7644 update_qualified_name(decl_base * d)
7645 {
7646  ::qualified_name_setter setter;
7647  d->traverse(setter);
7648 }
7649 
7650 /// Update the qualified name of a given sub-tree.
7651 ///
7652 /// @param d the sub-tree for which to update the qualified name.
7653 static void
7654 update_qualified_name(decl_base_sptr d)
7655 {return update_qualified_name(d.get());}
7656 
7657 // <scope_decl stuff>
7658 
7659 /// Hash a type by returning the pointer value of its canonical type.
7660 ///
7661 /// @param l the type to hash.
7662 ///
7663 /// @return the the pointer value of the canonical type of @p l.
7664 size_t
7665 canonical_type_hash::operator()(const type_base_sptr& l) const
7666 {return operator()(l.get());}
7667 
7668 /// Hash a (canonical) type by returning its pointer value
7669 ///
7670 /// @param l the canonical type to hash.
7671 ///
7672 /// @return the pointer value of the canonical type of @p l.
7673 size_t
7675 {return reinterpret_cast<size_t>(l);}
7676 
7677 struct scope_decl::priv
7678 {
7679  declarations members_;
7680  declarations sorted_members_;
7681  type_base_sptrs_type member_types_;
7682  type_base_sptrs_type sorted_member_types_;
7683  scopes member_scopes_;
7684  canonical_type_sptr_set_type canonical_types_;
7685  type_base_sptrs_type sorted_canonical_types_;
7686 }; // end struct scope_decl::priv
7687 
7688 /// Constructor of the @ref scope_decl type.
7689 ///
7690 /// @param the environment to use for the new instance.
7691 ///
7692 /// @param the name of the scope decl.
7693 ///
7694 /// @param locus the source location where the scope_decl is defined.
7695 ///
7696 /// @param vis the visibility of the declaration.
7697 scope_decl::scope_decl(const environment& env,
7698  const string& name,
7699  const location& locus,
7700  visibility vis)
7701  : type_or_decl_base(env, ABSTRACT_SCOPE_DECL|ABSTRACT_DECL_BASE),
7702  decl_base(env, name, locus, /*mangled_name=*/name, vis),
7703  priv_(new priv)
7704 {}
7705 
7706 /// Constructor of the @ref scope_decl type.
7707 ///
7708 /// @param the environment to use for the new instance.
7709 ///
7710 /// @param l the source location where the scope_decl is defined.
7711 ///
7712 /// @param vis the visibility of the declaration.
7713 scope_decl::scope_decl(const environment& env, location& l)
7714  : type_or_decl_base(env, ABSTRACT_SCOPE_DECL|ABSTRACT_DECL_BASE),
7715  decl_base(env, "", l),
7716  priv_(new priv)
7717 {}
7718 
7719 /// @eturn the set of canonical types of the the current scope.
7722 {return priv_->canonical_types_;}
7723 
7724 /// @eturn the set of canonical types of the the current scope.
7727 {return const_cast<scope_decl*>(this)->get_canonical_types();}
7728 
7729 /// Return a vector of sorted canonical types of the current scope.
7730 ///
7731 /// The types are sorted "almost topologically". That means, they are
7732 /// sorted using the lexicographic order of the string representing
7733 /// the location their definition point. If a type doesn't have a
7734 /// location, then its pretty representation is used.
7735 ///
7736 /// @return a vector of sorted canonical types of the current scope.
7737 const type_base_sptrs_type&
7739 {
7740  if (priv_->sorted_canonical_types_.empty())
7741  {
7742  for (canonical_type_sptr_set_type::const_iterator e =
7743  get_canonical_types().begin();
7744  e != get_canonical_types().end();
7745  ++e)
7746  priv_->sorted_canonical_types_.push_back(*e);
7747 
7748  type_topo_comp comp;
7749  std::stable_sort(priv_->sorted_canonical_types_.begin(),
7750  priv_->sorted_canonical_types_.end(),
7751  comp);
7752  }
7753  return priv_->sorted_canonical_types_;
7754 }
7755 
7756 /// Getter for the member declarations carried by the current @ref
7757 /// scope_decl.
7758 ///
7759 /// @return the member declarations carried by the current @ref
7760 /// scope_decl.
7763 {return priv_->members_;}
7764 
7765 /// Getter for the member declarations carried by the current @ref
7766 /// scope_decl.
7767 ///
7768 /// @return the member declarations carried by the current @ref
7769 /// scope_decl.
7772 {return priv_->members_;}
7773 
7774 /// Getter for the sorted member declarations carried by the current
7775 /// @ref scope_decl.
7776 ///
7777 /// @return the sorted member declarations carried by the current @ref
7778 /// scope_decl. The declarations are sorted topologically.
7781 {
7782  decl_topo_comp comp;
7783  if (priv_->sorted_members_.empty())
7784  {
7785  for (declarations::const_iterator i = get_member_decls().begin();
7786  i != get_member_decls().end();
7787  ++i)
7788  priv_->sorted_members_.push_back(*i);
7789 
7790  std::stable_sort(priv_->sorted_members_.begin(),
7791  priv_->sorted_members_.end(),
7792  comp);
7793  }
7794  return priv_->sorted_members_;
7795 }
7796 
7797 /// Getter for the number of anonymous classes contained in this
7798 /// scope.
7799 ///
7800 /// @return the number of anonymous classes contained in this scope.
7801 size_t
7803 {
7804  int result = 0;
7805  for (declarations::const_iterator it = get_member_decls().begin();
7806  it != get_member_decls().end();
7807  ++it)
7808  if (class_decl_sptr t = is_class_type(*it))
7809  if (t->get_is_anonymous())
7810  ++result;
7811 
7812  return result;
7813 }
7814 
7815 /// Getter for the number of anonymous unions contained in this
7816 /// scope.
7817 ///
7818 /// @return the number of anonymous unions contained in this scope.
7819 size_t
7821 {
7822  int result = 0;
7823  for (declarations::const_iterator it = get_member_decls().begin();
7824  it != get_member_decls().end();
7825  ++it)
7826  if (union_decl_sptr t = is_union_type(*it))
7827  if (t->get_is_anonymous())
7828  ++result;
7829 
7830  return result;
7831 }
7832 
7833 /// Getter for the number of anonymous enums contained in this
7834 /// scope.
7835 ///
7836 /// @return the number of anonymous enums contained in this scope.
7837 size_t
7839 {
7840  int result = 0;
7841  for (declarations::const_iterator it = get_member_decls().begin();
7842  it != get_member_decls().end();
7843  ++it)
7844  if (enum_type_decl_sptr t = is_enum_type(*it))
7845  if (t->get_is_anonymous())
7846  ++result;
7847 
7848  return result;
7849 }
7850 
7851 /// Getter for the scopes carried by the current scope.
7852 ///
7853 /// @return the scopes carried by the current scope.
7856 {return priv_->member_scopes_;}
7857 
7858 /// Getter for the scopes carried by the current scope.
7859 ///
7860 /// @return the scopes carried by the current scope.
7861 const scope_decl::scopes&
7863 {return priv_->member_scopes_;}
7864 
7865 /// Test if the current scope is empty.
7866 ///
7867 /// @return true iff the current scope is empty.
7868 bool
7870 {
7871  return (get_member_decls().empty()
7872  && get_canonical_types().empty());
7873 }
7874 
7875 /// Add a member decl to this scope. Note that user code should not
7876 /// use this, but rather use add_decl_to_scope.
7877 ///
7878 /// Note that this function updates the qualified name of the member
7879 /// decl that is added. It also sets the scope of the member. Thus,
7880 /// it ABG_ASSERTs that member should not have its scope set, prior to
7881 /// calling this function.
7882 ///
7883 /// @param member the new member decl to add to this scope.
7884 decl_base_sptr
7885 scope_decl::add_member_decl(const decl_base_sptr& member)
7886 {
7887  ABG_ASSERT(!has_scope(member));
7888 
7889  member->set_scope(this);
7890  priv_->members_.push_back(member);
7891  if (is_type(member))
7892  priv_->member_types_.push_back(is_type(member));
7893 
7894  if (scope_decl_sptr m = dynamic_pointer_cast<scope_decl>(member))
7895  priv_->member_scopes_.push_back(m);
7896 
7897  update_qualified_name(member);
7898 
7900  {
7901  if (translation_unit* existing_tu = member->get_translation_unit())
7902  ABG_ASSERT(tu == existing_tu);
7903  else
7904  member->set_translation_unit(tu);
7905  }
7906 
7908 
7909  return member;
7910 }
7911 
7912 /// Get the member types of this @ref scope_decl.
7913 ///
7914 /// @return a vector of the member types of this ref class_or_union.
7915 const type_base_sptrs_type&
7917 {return priv_->member_types_;}
7918 
7919 /// Find a member type of a given name, inside the current @ref
7920 /// scope_decl.
7921 ///
7922 /// @param name the name of the member type to look for.
7923 ///
7924 /// @return a pointer to the @ref type_base that represents the member
7925 /// type of name @p name, for the current scope.
7926 type_base_sptr
7927 scope_decl::find_member_type(const string& name) const
7928 {
7929  for (auto t : get_member_types())
7930  if (get_type_name(t, /*qualified*/false) == name)
7931  return t;
7932  return type_base_sptr();
7933 }
7934 
7935 /// Insert a member type.
7936 ///
7937 /// @param t the type to insert in the @ref scope_decl type.
7938 ///
7939 /// @param an iterator right before which @p t has to be inserted.
7940 void
7942  declarations::iterator before)
7943 {
7944  decl_base_sptr d = get_type_declaration(t);
7945  ABG_ASSERT(d);
7946  ABG_ASSERT(!has_scope(d));
7947 
7948  priv_->member_types_.push_back(t);
7949  insert_member_decl(d, before);
7950 }
7951 
7952 /// Add a member type to the current instance of class_or_union.
7953 ///
7954 /// @param t the member type to add. It must not have been added to a
7955 /// scope, otherwise this will violate an ABG_ASSERTion.
7956 void
7958 {insert_member_type(t, get_member_decls().end());}
7959 
7960 /// Add a member type to the current instance of class_or_union.
7961 ///
7962 /// @param t the type to be added as a member type to the current
7963 /// instance of class_or_union. An instance of class_or_union::member_type
7964 /// will be created out of @p t and and added to the the class.
7965 ///
7966 /// @param a the access specifier for the member type to be created.
7967 type_base_sptr
7969 {
7970  decl_base_sptr d = get_type_declaration(t);
7971  ABG_ASSERT(d);
7973  add_member_type(t);
7975  return t;
7976 }
7977 
7978 /// Remove a member type from the current @ref class_or_union scope.
7979 ///
7980 /// @param t the type to remove.
7981 void
7983 {
7984  for (auto i = priv_->member_types_.begin();
7985  i != priv_->member_types_.end();
7986  ++i)
7987  {
7988  if (*((*i)) == *t)
7989  {
7990  priv_->member_types_.erase(i);
7991  return;
7992  }
7993  }
7994 }
7995 
7996 /// Get the sorted member types of this @ref scope_decl
7997 ///
7998 /// @return a vector of the sorted member types of this ref
7999 /// class_or_union.
8000 const type_base_sptrs_type&
8002 {
8003  if (priv_->sorted_member_types_.empty())
8004  {
8005  for (auto t : get_member_types())
8006  priv_->sorted_member_types_.push_back(t);
8007 
8008  type_topo_comp comp;
8009  std::stable_sort(priv_->sorted_member_types_.begin(),
8010  priv_->sorted_member_types_.end(),
8011  comp);
8012  }
8013  return priv_->sorted_member_types_;
8014 }
8015 
8016 /// Insert a member decl to this scope, right before an element
8017 /// pointed to by a given iterator. Note that user code should not
8018 /// use this, but rather use insert_decl_into_scope.
8019 ///
8020 /// Note that this function updates the qualified name of the inserted
8021 /// member.
8022 ///
8023 /// @param member the new member decl to add to this scope.
8024 ///
8025 /// @param before an interator pointing to the element before which
8026 /// the new member should be inserted.
8027 decl_base_sptr
8028 scope_decl::insert_member_decl(decl_base_sptr member,
8029  declarations::iterator before)
8030 {
8031  ABG_ASSERT(!member->get_scope());
8032 
8033  member->set_scope(this);
8034  priv_->members_.insert(before, member);
8035 
8036  if (scope_decl_sptr m = dynamic_pointer_cast<scope_decl>(member))
8037  priv_-> member_scopes_.push_back(m);
8038 
8039  update_qualified_name(member);
8040 
8042  {
8043  if (translation_unit* existing_tu = member->get_translation_unit())
8044  ABG_ASSERT(tu == existing_tu);
8045  else
8046  member->set_translation_unit(tu);
8047  }
8048 
8050 
8051  return member;
8052 }
8053 
8054 /// Remove a declaration from the current scope.
8055 ///
8056 /// @param member the declaration to remove from the scope.
8057 void
8058 scope_decl::remove_member_decl(decl_base_sptr member)
8059 {
8060  for (declarations::iterator i = priv_->members_.begin();
8061  i != priv_->members_.end();
8062  ++i)
8063  {
8064  if (**i == *member)
8065  {
8066  priv_->members_.erase(i);
8067  // Do not access i after this point as it's invalided by the
8068  // erase call.
8069  break;
8070  }
8071  }
8072 
8073  scope_decl_sptr scope = dynamic_pointer_cast<scope_decl>(member);
8074  if (scope)
8075  {
8076  for (scopes::iterator i = priv_->member_scopes_.begin();
8077  i != priv_->member_scopes_.end();
8078  ++i)
8079  {
8080  if (**i == *member)
8081  {
8082  priv_->member_scopes_.erase(i);
8083  break;
8084  }
8085  }
8086  }
8087 }
8088 
8089 /// Return the hash value for the current instance of scope_decl.
8090 ///
8091 /// This method can trigger the computing of the hash value, if need be.
8092 ///
8093 /// @return the hash value.
8094 size_t
8096 {
8097  scope_decl::hash hash_scope;
8098  return hash_scope(this);
8099 }
8100 
8101 /// Compares two instances of @ref scope_decl.
8102 ///
8103 /// If the two intances are different, set a bitfield to give some
8104 /// insight about the kind of differences there are.
8105 ///
8106 /// @param l the first artifact of the comparison.
8107 ///
8108 /// @param r the second artifact of the comparison.
8109 ///
8110 /// @param k a pointer to a bitfield that gives information about the
8111 /// kind of changes there are between @p l and @p r. This one is set
8112 /// iff @p k is non-null and the function returns false.
8113 ///
8114 /// Please note that setting k to a non-null value does have a
8115 /// negative performance impact because even if @p l and @p r are not
8116 /// equal, the function keeps up the comparison in order to determine
8117 /// the different kinds of ways in which they are different.
8118 ///
8119 /// @return true if @p l equals @p r, false otherwise.
8120 bool
8121 equals(const scope_decl& l, const scope_decl& r, change_kind* k)
8122 {
8123  bool result = true;
8124 
8125  if (!l.decl_base::operator==(r))
8126  {
8127  result = false;
8128  if (k)
8130  else
8132  }
8133 
8134  scope_decl::declarations::const_iterator i, j;
8135  for (i = l.get_member_decls().begin(), j = r.get_member_decls().begin();
8136  i != l.get_member_decls().end() && j != r.get_member_decls().end();
8137  ++i, ++j)
8138  {
8139  if (**i != **j)
8140  {
8141  result = false;
8142  if (k)
8143  {
8144  *k |= SUBTYPE_CHANGE_KIND;
8145  break;
8146  }
8147  else
8149  }
8150  }
8151 
8152  if (i != l.get_member_decls().end() || j != r.get_member_decls().end())
8153  {
8154  result = false;
8155  if (k)
8157  else
8159  }
8160 
8161  ABG_RETURN(result);
8162 }
8163 
8164 /// Return true iff both scopes have the same names and have the same
8165 /// member decls.
8166 ///
8167 /// This function doesn't check for equality of the scopes of its
8168 /// arguments.
8169 bool
8171 {
8172  const scope_decl* other = dynamic_cast<const scope_decl*>(&o);
8173  if (!other)
8174  return false;
8175 
8176  return equals(*this, *other, 0);
8177 }
8178 
8179 /// Equality operator for @ref scope_decl_sptr.
8180 ///
8181 /// @param l the left hand side operand of the equality operator.
8182 ///
8183 /// @pram r the right hand side operand of the equalify operator.
8184 ///
8185 /// @return true iff @p l equals @p r.
8186 bool
8188 {
8189  if (!!l != !!r)
8190  return false;
8191  if (l.get() == r.get())
8192  return true;
8193  return *l == *r;
8194 }
8195 
8196 /// Inequality operator for @ref scope_decl_sptr.
8197 ///
8198 /// @param l the left hand side operand of the equality operator.
8199 ///
8200 /// @pram r the right hand side operand of the equalify operator.
8201 ///
8202 /// @return true iff @p l equals @p r.
8203 bool
8205 {return !operator==(l, r);}
8206 
8207 /// Find a member of the current scope and return an iterator on it.
8208 ///
8209 /// @param decl the scope member to find.
8210 ///
8211 /// @param i the iterator to set to the member @p decl. This is set
8212 /// iff the function returns true.
8213 ///
8214 /// @return true if the member decl was found, false otherwise.
8215 bool
8217  declarations::iterator& i)
8218 {
8219  if (!decl)
8220  return false;
8221 
8222  if (get_member_decls().empty())
8223  {
8224  i = get_member_decls().end();
8225  return false;
8226  }
8227 
8228  for (declarations::iterator it = get_member_decls().begin();
8229  it != get_member_decls().end();
8230  ++it)
8231  {
8232  if ((*it).get() == decl)
8233  {
8234  i = it;
8235  return true;
8236  }
8237  }
8238 
8239  return false;
8240 }
8241 
8242 /// Find a member of the current scope and return an iterator on it.
8243 ///
8244 /// @param decl the scope member to find.
8245 ///
8246 /// @param i the iterator to set to the member @p decl. This is set
8247 /// iff the function returns true.
8248 ///
8249 /// @return true if the member decl was found, false otherwise.
8250 bool
8251 scope_decl::find_iterator_for_member(const decl_base_sptr decl,
8252  declarations::iterator& i)
8253 {return find_iterator_for_member(decl.get(), i);}
8254 
8255 /// This implements the ir_traversable_base::traverse pure virtual
8256 /// function.
8257 ///
8258 /// @param v the visitor used on the current instance of scope_decl
8259 /// and on its member nodes.
8260 ///
8261 /// @return true if the traversal of the tree should continue, false
8262 /// otherwise.
8263 bool
8265 {
8266  if (visiting())
8267  return true;
8268 
8269  if (v.visit_begin(this))
8270  {
8271  visiting(true);
8272  for (scope_decl::declarations::const_iterator i =
8273  get_member_decls().begin();
8274  i != get_member_decls ().end();
8275  ++i)
8276  if (!(*i)->traverse(v))
8277  break;
8278  visiting(false);
8279  }
8280  return v.visit_end(this);
8281 }
8282 
8283 scope_decl::~scope_decl()
8284 {}
8285 
8286 /// Appends a declaration to a given scope, if the declaration
8287 /// doesn't already belong to one.
8288 ///
8289 /// @param decl the declaration to add to the scope
8290 ///
8291 /// @param scope the scope to append the declaration to
8292 decl_base_sptr
8293 add_decl_to_scope(decl_base_sptr decl, scope_decl* scope)
8294 {
8295  ABG_ASSERT(scope);
8296 
8297  if (scope && decl && !decl->get_scope())
8298  decl = scope->add_member_decl(decl);
8299 
8300  return decl;
8301 }
8302 
8303 /// Appends a declaration to a given scope, if the declaration doesn't
8304 /// already belong to a scope.
8305 ///
8306 /// @param decl the declaration to add append to the scope
8307 ///
8308 /// @param scope the scope to append the decl to
8309 decl_base_sptr
8310 add_decl_to_scope(decl_base_sptr decl, const scope_decl_sptr& scope)
8311 {return add_decl_to_scope(decl, scope.get());}
8312 
8313 /// Remove a given decl from its scope
8314 ///
8315 /// @param decl the decl to remove from its scope.
8316 void
8317 remove_decl_from_scope(decl_base_sptr decl)
8318 {
8319  if (!decl)
8320  return;
8321 
8322  scope_decl* scope = decl->get_scope();
8323  scope->remove_member_decl(decl);
8324  decl->set_scope(0);
8325 }
8326 
8327 /// Inserts a declaration into a given scope, before a given IR child
8328 /// node of the scope.
8329 ///
8330 /// @param decl the declaration to insert into the scope.
8331 ///
8332 /// @param before an iterator pointing to the child IR node before
8333 /// which to insert the declaration.
8334 ///
8335 /// @param scope the scope into which to insert the declaration.
8336 decl_base_sptr
8337 insert_decl_into_scope(decl_base_sptr decl,
8338  scope_decl::declarations::iterator before,
8339  scope_decl* scope)
8340 {
8341  if (scope && decl && !decl->get_scope())
8342  {
8343  decl_base_sptr d = scope->insert_member_decl(decl, before);
8344  decl = d;
8345  }
8346  return decl;
8347 }
8348 
8349 /// Inserts a declaration into a given scope, before a given IR child
8350 /// node of the scope.
8351 ///
8352 /// @param decl the declaration to insert into the scope.
8353 ///
8354 /// @param before an iterator pointing to the child IR node before
8355 /// which to insert the declaration.
8356 ///
8357 /// @param scope the scope into which to insert the declaration.
8358 decl_base_sptr
8359 insert_decl_into_scope(decl_base_sptr decl,
8360  scope_decl::declarations::iterator before,
8361  scope_decl_sptr scope)
8362 {return insert_decl_into_scope(decl, before, scope.get());}
8363 
8364 /// Constructor of the @ref global_scope type.
8365 ///
8366 /// @param tu the translation unit the scope belongs to.
8367 global_scope::global_scope(translation_unit *tu)
8368  : type_or_decl_base(tu->get_environment(),
8369  GLOBAL_SCOPE_DECL
8370  | ABSTRACT_DECL_BASE
8371  | ABSTRACT_SCOPE_DECL),
8372  decl_base(tu->get_environment(), "", location()),
8373  scope_decl(tu->get_environment(), "", location()),
8374  translation_unit_(tu)
8375 {
8376  runtime_type_instance(this);
8377 }
8378 
8379 /// return the global scope as seen by a given declaration.
8380 ///
8381 /// @param decl the declaration to consider.
8382 ///
8383 /// @return the global scope of the decl, or a null pointer if the
8384 /// decl is not yet added to a translation_unit.
8385 const global_scope*
8387 {
8388  if (const global_scope* s = dynamic_cast<const global_scope*>(&decl))
8389  return s;
8390 
8391  scope_decl* scope = decl.get_scope();
8392  while (scope && !dynamic_cast<global_scope*>(scope))
8393  scope = scope->get_scope();
8394 
8395  return scope ? dynamic_cast<global_scope*> (scope) : 0;
8396 }
8397 
8398 /// return the global scope as seen by a given declaration.
8399 ///
8400 /// @param decl the declaration to consider.
8401 ///
8402 /// @return the global scope of the decl, or a null pointer if the
8403 /// decl is not yet added to a translation_unit.
8404 const global_scope*
8406 {return get_global_scope(*decl);}
8407 
8408 /// Return the global scope as seen by a given declaration.
8409 ///
8410 /// @param decl the declaration to consider.
8411 ///
8412 /// @return the global scope of the decl, or a null pointer if the
8413 /// decl is not yet added to a translation_unit.
8414 const global_scope*
8415 get_global_scope(const shared_ptr<decl_base> decl)
8416 {return get_global_scope(decl.get());}
8417 
8418 /// Return the a scope S containing a given declaration and that is
8419 /// right under a given scope P.
8420 ///
8421 /// Note that @p scope must come before @p decl in topological
8422 /// order.
8423 ///
8424 /// @param decl the decl for which to find a scope.
8425 ///
8426 /// @param scope the scope under which the resulting scope must be.
8427 ///
8428 /// @return the resulting scope.
8429 const scope_decl*
8431  const scope_decl* scope)
8432 {
8433  if (!decl)
8434  return 0;
8435 
8436  if (scope == 0)
8437  return get_global_scope(decl);
8438 
8439  // Handle the case where decl is a scope itself.
8440  const scope_decl* s = dynamic_cast<const scope_decl*>(decl);
8441  if (!s)
8442  s = decl->get_scope();
8443 
8444  if (is_global_scope(s))
8445  return scope;
8446 
8447  // Here, decl is in the scope 'scope', or decl and 'scope' are the
8448  // same. The caller needs to be prepared to deal with this case.
8449  if (s == scope)
8450  return s;
8451 
8452  while (s && !is_global_scope(s) && s->get_scope() != scope)
8453  s = s->get_scope();
8454 
8455  if (!s || is_global_scope(s))
8456  // SCOPE must come before decl in topological order, but I don't
8457  // know how to ensure that ...
8458  return scope;
8459  ABG_ASSERT(s);
8460 
8461  return s;
8462 }
8463 
8464 /// Return the a scope S containing a given declaration and that is
8465 /// right under a given scope P.
8466 ///
8467 /// @param decl the decl for which to find a scope.
8468 ///
8469 /// @param scope the scope under which the resulting scope must be.
8470 ///
8471 /// @return the resulting scope.
8472 const scope_decl*
8473 get_top_most_scope_under(const decl_base_sptr decl,
8474  const scope_decl* scope)
8475 {return get_top_most_scope_under(decl.get(), scope);}
8476 
8477 /// Return the a scope S containing a given declaration and that is
8478 /// right under a given scope P.
8479 ///
8480 /// @param decl the decl for which to find a scope.
8481 ///
8482 /// @param scope the scope under which the resulting scope must be.
8483 ///
8484 /// @return the resulting scope.
8485 const scope_decl*
8486 get_top_most_scope_under(const decl_base_sptr decl,
8487  const scope_decl_sptr scope)
8488 {return get_top_most_scope_under(decl, scope.get());}
8489 
8490 // </scope_decl stuff>
8491 
8492 
8493 /// Get the string representation of a CV qualifier bitmap.
8494 ///
8495 /// @param cv_quals the bitmap of CV qualifiers to consider.
8496 ///
8497 /// @return the string representation.
8498 string
8500 {
8501  string repr;
8502  if (cv_quals & qualified_type_def::CV_RESTRICT)
8503  repr = "restrict";
8504  if (cv_quals & qualified_type_def::CV_CONST)
8505  {
8506  if (!repr.empty())
8507  repr += ' ';
8508  repr += "const";
8509  }
8510  if (cv_quals & qualified_type_def::CV_VOLATILE)
8511  {
8512  if (!repr.empty())
8513  repr += ' ';
8514  repr += "volatile";
8515  }
8516  return repr;
8517 }
8518 
8519 /// Build and return a copy of the name of an ABI artifact that is
8520 /// either a type or a decl.
8521 ///
8522 /// @param tod the ABI artifact to get the name for.
8523 ///
8524 /// @param qualified if yes, return the qualified name of @p tod;
8525 /// otherwise, return the non-qualified name;
8526 ///
8527 /// @return the name of @p tod.
8528 string
8529 get_name(const type_or_decl_base *tod, bool qualified)
8530 {
8531  string result;
8532 
8533  type_or_decl_base* a = const_cast<type_or_decl_base*>(tod);
8534 
8535  if (type_base* t = dynamic_cast<type_base*>(a))
8536  result = get_type_name(t, qualified);
8537  else if (decl_base *d = dynamic_cast<decl_base*>(a))
8538  {
8539  if (qualified)
8540  result = d->get_qualified_name();
8541  else
8542  result = d->get_name();
8543  }
8544  else
8545  // We should never reach this point.
8546  abort();
8547 
8548  return result;
8549 }
8550 
8551 /// Build and return a copy of the name of an ABI artifact that is
8552 /// either a type of a decl.
8553 ///
8554 /// @param tod the ABI artifact to get the name for.
8555 ///
8556 /// @param qualified if yes, return the qualified name of @p tod;
8557 /// otherwise, return the non-qualified name;
8558 ///
8559 /// @return the name of @p tod.
8560 string
8561 get_name(const type_or_decl_base_sptr& tod, bool qualified)
8562 {return get_name(tod.get(), qualified);}
8563 
8564 /// Build and return a qualified name from a name and its scope.
8565 ///
8566 /// The name is supposed to be for an entity that is part of the
8567 /// scope.
8568 ///
8569 /// @param the scope to consider.
8570 ///
8571 /// @param name of the name to consider.
8572 ///
8573 /// @return a copy of the string that represents the qualified name.
8574 string
8575 build_qualified_name(const scope_decl* scope, const string& name)
8576 {
8577  if (name.empty())
8578  return "";
8579 
8580  string qualified_name;
8581  if (scope)
8582  qualified_name = scope->get_qualified_name();
8583 
8584  if (qualified_name.empty())
8585  qualified_name = name;
8586  else
8587  qualified_name = qualified_name + "::" + name;
8588 
8589  return qualified_name;
8590 }
8591 
8592 /// Build and return the qualified name of a type in its scope.
8593 ///
8594 /// @param scope the scope of the type to consider.
8595 ///
8596 /// @param type the type to consider.
8597 string
8598 build_qualified_name(const scope_decl* scope, const type_base_sptr& type)
8599 {return build_qualified_name(scope, get_name((type)));}
8600 
8601 // </scope_decl stuff>
8602 
8603 /// Get the location of the declaration of a given type.
8604 ///
8605 /// @param type the type to consider.
8606 ///
8607 /// @return the location of the declaration of type @p type.
8608 location
8609 get_location(const type_base_sptr& type)
8610 {
8611  if (decl_base_sptr decl = get_type_declaration(type))
8612  return get_location(decl);
8613  return location();
8614 }
8615 
8616 /// Get the location of a given declaration.
8617 ///
8618 /// @param decl the declaration to consider.
8619 ///
8620 /// @return the location of the declaration @p decl.
8621 location
8622 get_location(const decl_base_sptr& decl)
8623 {
8624  location loc = decl->get_location();
8625  if (!loc)
8626  {
8627  if (class_or_union_sptr c = is_class_or_union_type(decl))
8628  if (c->get_is_declaration_only() && c->get_definition_of_declaration())
8629  {
8630  c = is_class_or_union_type(c->get_definition_of_declaration());
8631  loc = c->get_location();
8632  }
8633  }
8634  return loc;
8635 }
8636 
8637 /// Get the scope of a given type.
8638 ///
8639 /// @param t the type to consider.
8640 ///
8641 /// @return the scope of type @p t or 0 if the type has no scope yet.
8642 scope_decl*
8644 {
8645  if (!t)
8646  return 0;
8647 
8649  if (d)
8650  return d->get_scope();
8651  return 0;
8652 }
8653 
8654 /// Get the scope of a given type.
8655 ///
8656 /// @param t the type to consider.
8657 ///
8658 /// @return the scope of type @p t or 0 if the type has no scope yet.
8659 scope_decl*
8660 get_type_scope(const type_base_sptr& t)
8661 {return get_type_scope(t.get());}
8662 
8663 /// Get the name of a given type and return a copy of it.
8664 ///
8665 /// @param t the type to consider.
8666 ///
8667 /// @param qualified if true then return the qualified name of the
8668 /// type.
8669 ///
8670 /// @param internal set to true if the call is intended for an
8671 /// internal use (for technical use inside the library itself), false
8672 /// otherwise. If you don't know what this is for, then set it to
8673 /// false.
8674 ///
8675 /// @return a copy of the type name if the type has a name, or the
8676 /// empty string if it does not.
8678 get_type_name(const type_base_sptr& t, bool qualified, bool internal)
8679 {return get_type_name(t.get(), qualified, internal);}
8680 
8681 /// Return true iff a decl is for a type type that has a generic
8682 /// anonymous internal type name.
8683 ///
8684 /// @param d the decl to considier.
8685 ///
8686 /// @return true iff @p d is for a type type that has a generic
8687 /// anonymous internal type name.
8688 static bool
8689 has_generic_anonymous_internal_type_name(const decl_base *d)
8690 {
8691  return (is_class_or_union_type(d)
8692  || is_enum_type(d)
8693  || is_subrange_type(d));
8694 }
8695 
8696 /// Return the generic internal name of an anonymous type.
8697 ///
8698 /// For internal purposes, we want to define a generic name for all
8699 /// anonymous types of a certain kind. For instance, all anonymous
8700 /// structs will be have a generic name of "__anonymous_struct__", all
8701 /// anonymous unions will have a generic name of
8702 /// "__anonymous_union__", etc.
8703 ///
8704 /// That generic name can be used as a hash to put all anonymous types
8705 /// of a certain kind in the same hash table bucket, for instance.
8706 static interned_string
8707 get_generic_anonymous_internal_type_name(const decl_base *d)
8708 {
8709  ABG_ASSERT(has_generic_anonymous_internal_type_name(d));
8710 
8711  const environment&env = d->get_environment();
8712 
8713  interned_string result;
8714  if (is_class_type(d))
8715  result =
8717  else if (is_union_type(d))
8718  result =
8720  else if (is_enum_type(d))
8721  result =
8723  else if (is_subrange_type(d))
8724  result =
8726  else
8728 
8729  return result;
8730 }
8731 
8732 /// Get the internal name for a given integral type.
8733 ///
8734 /// All integral types that have the modifiers 'short, long or long
8735 /// long' have the same internal name. This is so that they can all
8736 /// have the same canonical type if they are of the same size.
8737 /// Otherwise, 'long int' and 'long long int' would have different
8738 /// canonical types even though they are equivalent from an ABI point
8739 /// of view.
8740 ///
8741 /// @param t the integral type to consider
8742 ///
8743 /// @return the internal name for @p t if it's an integral type, or
8744 /// the empty string if @p t is not an integral type.
8745 static string
8746 get_internal_integral_type_name(const type_base* t)
8747 {
8748  string name;
8749  type_decl *type = is_integral_type(t);
8750 
8751  if (!type)
8752  return name;
8753 
8754  integral_type int_type;
8755  if (parse_integral_type(type->get_name(), int_type))
8756  name = int_type.to_string(/*internal=*/true);
8757 
8758  return name;
8759 }
8760 
8761 /// Get the name of a given type and return a copy of it.
8762 ///
8763 /// @param t the type to consider.
8764 ///
8765 /// @param qualified if true then return the qualified name of the
8766 /// type.
8767 ///
8768 /// @param internal set to true if the call is intended for an
8769 /// internal use (for technical use inside the library itself), false
8770 /// otherwise. If you don't know what this is for, then set it to
8771 /// false.
8772 ///
8773 /// @return a copy of the type name if the type has a name, or the
8774 /// empty string if it does not.
8775 interned_string
8776 get_type_name(const type_base* t, bool qualified, bool internal)
8777 {
8778  const decl_base* d = dynamic_cast<const decl_base*>(t);
8779  if (!d)
8780  {
8781  const function_type* fn_type = is_function_type(t);
8782  ABG_ASSERT(fn_type);
8783  return fn_type->get_cached_name(internal);
8784  }
8785 
8786  // All anonymous types of a given kind get to have the same internal
8787  // name for internal purpose. This to allow them to be compared
8788  // among themselves during type canonicalization.
8789  if (internal)
8790  {
8791  if (d->get_is_anonymous())
8792  {
8793  string r;
8794  r += get_generic_anonymous_internal_type_name(d);
8795  return t->get_environment().intern(r);
8796  }
8797 
8798  if (qualified)
8799  return d->get_qualified_name(internal);
8800 
8801  const environment&env = d->get_environment();
8802  return env.intern(get_internal_integral_type_name(t));
8803  }
8804 
8805  if (qualified)
8806  return d->get_qualified_name(internal);
8807  return d->get_name();
8808 }
8809 
8810 /// Get the name of a given type and return a copy of it.
8811 ///
8812 /// @param t the type to consider.
8813 ///
8814 /// @param qualified if true then return the qualified name of the
8815 /// type.
8816 ///
8817 /// @param internal set to true if the call is intended for an
8818 /// internal use (for technical use inside the library itself), false
8819 /// otherwise. If you don't know what this is for, then set it to
8820 /// false.
8821 ///
8822 /// @return a copy of the type name if the type has a name, or the
8823 /// empty string if it does not.
8825 get_type_name(const type_base& t, bool qualified, bool internal)
8826 {return get_type_name(&t, qualified, internal);}
8827 
8828 /// Get the name of the pointer to a given type.
8829 ///
8830 /// @param pointed_to_type the pointed-to-type to consider.
8831 ///
8832 /// @param qualified this is true if the resulting name should be of a
8833 /// pointer to a *fully-qualified* pointed-to-type.
8834 ///
8835 /// @param internal true if the name is for libabigail-internal
8836 /// purposes.
8837 ///
8838 /// @return the name (string representation) of the pointer.
8841  bool qualified, bool internal)
8842 {
8843  const environment& env = pointed_to_type.get_environment();
8844  string tn = get_type_name(pointed_to_type, qualified, internal);
8845  tn = tn + "*";
8846 
8847  return env.intern(tn);
8848 }
8849 
8850 /// Get the name of the reference to a given type.
8851 ///
8852 /// @param pointed_to_type the pointed-to-type to consider.
8853 ///
8854 /// @param qualified this is true if the resulting name should be of a
8855 /// reference to a *fully-qualified* pointed-to-type.
8856 ///
8857 /// @param internal true if the name is for libabigail-internal
8858 /// purposes.
8859 ///
8860 /// @return the name (string representation) of the reference.
8863  bool lvalue_reference,
8864  bool qualified, bool internal)
8865 {
8866  const environment& env = pointed_to_type.get_environment();
8867 
8868  string name = get_type_name(pointed_to_type, qualified, internal);
8869  if (lvalue_reference)
8870  name = name + "&";
8871  else
8872  name = name + "&&";
8873 
8874  return env.intern(name);
8875 }
8876 
8877 /// Get the name of a qualified type, given the underlying type and
8878 /// its qualifiers.
8879 ///
8880 /// @param underlying_type the underlying type to consider.
8881 ///
8882 /// @param quals the CV qualifiers of the name.
8883 ///
8884 /// @param qualified true if we should consider the fully qualified
8885 /// name of @p underlying_type.
8886 ///
8887 /// @param internal true if the result is to be used for
8888 /// libabigail-internal purposes.
8889 ///
8890 /// @return the name (string representation) of the qualified type.
8892 get_name_of_qualified_type(const type_base_sptr& underlying_type,
8893  qualified_type_def::CV quals,
8894  bool qualified, bool internal)
8895 {
8896  const environment& env = underlying_type->get_environment();
8897 
8898  string quals_repr = get_string_representation_of_cv_quals(quals);
8899  string name = get_type_name(underlying_type, qualified, internal);
8900 
8901  if (quals_repr.empty() && internal)
8902  // We are asked to return the internal name, that might be used
8903  // for type canonicalization. For that canonicalization, we need
8904  // to make a difference between a no-op qualified type which
8905  // underlying type is foo (the qualified type is named "none
8906  // foo"), and the name of foo, which is just "foo".
8907  //
8908  // Please remember that this has to be kept in sync with what is
8909  // done in die_qualified_name, in abg-dwarf-reader.cc. So if you
8910  // change this code here, please change that code there too.
8911  quals_repr = "";
8912 
8913  if (!quals_repr.empty())
8914  {
8915  if (is_pointer_type(peel_qualified_type(underlying_type))
8916  || is_reference_type(peel_qualified_type(underlying_type)))
8917  {
8918  name += " ";
8919  name += quals_repr;
8920  }
8921  else
8922  name = quals_repr + " " + name;
8923  }
8924 
8925  return env.intern(name);
8926 }
8927 
8928 /// Get the name of a given function type and return a copy of it.
8929 ///
8930 /// @param fn_type the function type to consider.
8931 ///
8932 /// @param internal set to true if the call is intended for an
8933 /// internal use (for technical use inside the library itself), false
8934 /// otherwise. If you don't know what this is for, then set it to
8935 /// false.
8936 ///
8937 /// @return a copy of the function type name
8940  bool internal)
8941 {return get_function_type_name(fn_type.get(), internal);}
8942 
8943 /// Get the name of a given function type and return a copy of it.
8944 ///
8945 /// @param fn_type the function type to consider.
8946 ///
8947 /// @param internal set to true if the call is intended for an
8948 /// internal use (for technical use inside the library itself), false
8949 /// otherwise. If you don't know what this is for, then set it to
8950 /// false.
8951 ///
8952 /// @return a copy of the function type name
8955  bool internal)
8956 {
8957  ABG_ASSERT(fn_type);
8958 
8959  if (const method_type* method = is_method_type(fn_type))
8960  return get_method_type_name(method, internal);
8961 
8962  return get_function_type_name(*fn_type, internal);
8963 }
8964 
8965 /// Get the name of a given function type and return a copy of it.
8966 ///
8967 /// @param fn_type the function type to consider.
8968 ///
8969 /// @param internal set to true if the call is intended for an
8970 /// internal use (for technical use inside the library itself), false
8971 /// otherwise. If you don't know what this is for, then set it to
8972 /// false.
8973 ///
8974 /// @return a copy of the function type name
8977  bool internal)
8978 {
8979  std::ostringstream o;
8980  // When the function name is used for internal purposes (e.g, for
8981  // canonicalization), we want its representation to stay the same,
8982  // regardless of typedefs. So let's strip typedefs from the return
8983  // type.
8984  type_base_sptr return_type =
8985  internal
8986  ? peel_typedef_type(fn_type.get_return_type())
8987  : fn_type.get_return_type();
8988  const environment& env = fn_type.get_environment();
8989 
8990  o << get_pretty_representation(return_type, internal);
8991 
8992  o << " (";
8993  type_base_sptr type;
8994  for (function_type::parameters::const_iterator i =
8995  fn_type.get_parameters().begin();
8996  i != fn_type.get_parameters().end();
8997  ++i)
8998  {
8999  if (i != fn_type.get_parameters().begin())
9000  o << ", ";
9001  type = (*i)->get_type();
9002  if (internal)
9003  type = peel_typedef_type(type);
9004  o << get_pretty_representation(type, internal);
9005  }
9006  o <<")";
9007 
9008  return env.intern(o.str());
9009 }
9010 
9011 /// Get the ID of a function, or, if the ID can designate several
9012 /// different functions, get its pretty representation.
9013 ///
9014 /// @param fn the function to consider
9015 ///
9016 /// @return the function ID of pretty representation of @p fn.
9019 {
9020  ABG_ASSERT(fn);
9021 
9022  interned_string result = fn->get_environment().intern(fn->get_id());
9023 
9024  if (corpus *c = fn->get_corpus())
9025  {
9027  c->get_exported_decls_builder();
9028  if (b->fn_id_maps_to_several_fns(fn))
9029  result = fn->get_environment().intern(fn->get_pretty_representation());
9030  }
9031 
9032  return result;
9033 }
9034 
9035 /// Get the name of a given method type and return a copy of it.
9036 ///
9037 /// @param fn_type the function type to consider.
9038 ///
9039 /// @param internal set to true if the call is intended for an
9040 /// internal use (for technical use inside the library itself), false
9041 /// otherwise. If you don't know what this is for, then set it to
9042 /// false.
9043 ///
9044 /// @return a copy of the function type name
9047  bool internal)
9048 {return get_method_type_name(fn_type.get(), internal);}
9049 
9050 /// Get the name of a given method type and return a copy of it.
9051 ///
9052 /// @param fn_type the function type to consider.
9053 ///
9054 /// @param internal set to true if the call is intended for an
9055 /// internal use (for technical use inside the library itself), false
9056 /// otherwise. If you don't know what this is for, then set it to
9057 /// false.
9058 ///
9059 /// @return a copy of the function type name
9062  bool internal)
9063 {
9064  if (fn_type)
9065  return get_method_type_name(*fn_type, internal);
9066 
9067  return interned_string();
9068 }
9069 
9070 /// Get the name of a given method type and return a copy of it.
9071 ///
9072 /// @param fn_type the function type to consider.
9073 ///
9074 /// @param internal set to true if the call is intended for an
9075 /// internal use (for technical use inside the library itself), false
9076 /// otherwise. If you don't know what this is for, then set it to
9077 /// false.
9078 ///
9079 /// @return a copy of the function type name
9082  bool internal)
9083 {
9084  std::ostringstream o;
9085  // When the function name is used for internal purposes (e.g, for
9086  // canonicalization), we want its representation to stay the same,
9087  // regardless of typedefs. So let's strip typedefs from the return
9088  // type.
9089  type_base_sptr return_type =
9090  internal
9091  ? peel_typedef_type(fn_type.get_return_type())
9092  : fn_type.get_return_type();
9093  const environment& env = fn_type.get_environment();
9094 
9095  if (return_type)
9096  o << return_type->get_cached_pretty_representation(internal);
9097  else
9098  // There are still some abixml files out there in which "void"
9099  // can be expressed as an empty type.
9100  o << "void";
9101 
9102  class_or_union_sptr class_type = fn_type.get_class_type();
9103  ABG_ASSERT(class_type);
9104 
9105  o << " (" << class_type->get_qualified_name(internal) << "::*)"
9106  << " (";
9107 
9108  type_base_sptr type;
9109  for (function_type::parameters::const_iterator i =
9110  fn_type.get_parameters().begin();
9111  i != fn_type.get_parameters().end();
9112  ++i)
9113  {
9114  if (i != fn_type.get_parameters().begin())
9115  o << ", ";
9116  type = (*i)->get_type();
9117  if (internal)
9118  type = peel_typedef_type(type);
9119  if (*i)
9120  o << type->get_cached_pretty_representation(internal);
9121  else
9122  // There are still some abixml files out there in which "void"
9123  // can be expressed as an empty type.
9124  o << "void";
9125  }
9126  o <<")";
9127 
9128  return env.intern(o.str());
9129 }
9130 
9131 /// Build and return a copy of the pretty representation of an ABI
9132 /// artifact that could be either a type of a decl.
9133 ///
9134 /// param tod the ABI artifact to consider.
9135 ///
9136 /// @param internal set to true if the call is intended for an
9137 /// internal use (for technical use inside the library itself), false
9138 /// otherwise. If you don't know what this is for, then set it to
9139 /// false.
9140 ///
9141 /// @return a copy of the pretty representation of an ABI artifact
9142 /// that could be either a type of a decl.
9143 string
9145 {
9146  string result;
9147 
9148  if (type_base* t = is_type(const_cast<type_or_decl_base*>(tod)))
9149  result = get_pretty_representation(t, internal);
9150  else if (decl_base* d = is_decl(const_cast<type_or_decl_base*>(tod)))
9151  result = get_pretty_representation(d, internal);
9152  else
9153  // We should never reach this point
9154  abort();
9155 
9156  return result;
9157 }
9158 
9159 /// Build and return a copy of the pretty representation of an ABI
9160 /// artifact that could be either a type of a decl.
9161 ///
9162 /// param tod the ABI artifact to consider.
9163 ///
9164 /// @param internal set to true if the call is intended for an
9165 /// internal use (for technical use inside the library itself), false
9166 /// otherwise. If you don't know what this is for, then set it to
9167 /// false.
9168 ///
9169 /// @return a copy of the pretty representation of an ABI artifact
9170 /// that could be either a type of a decl.
9171 string
9173 {return get_pretty_representation(tod.get(), internal);}
9174 
9175 /// Get a copy of the pretty representation of a decl.
9176 ///
9177 /// @param d the decl to consider.
9178 ///
9179 /// @param internal set to true if the call is intended for an
9180 /// internal use (for technical use inside the library itself), false
9181 /// otherwise. If you don't know what this is for, then set it to
9182 /// false.
9183 ///
9184 /// @return the pretty representation of the decl.
9185 string
9186 get_pretty_representation(const decl_base* d, bool internal)
9187 {
9188  if (!d)
9189  return "";
9190  return d->get_pretty_representation(internal);
9191 }
9192 
9193 /// Get a copy of the pretty representation of a type.
9194 ///
9195 /// @param d the type to consider.
9196 ///
9197 /// @param internal set to true if the call is intended for an
9198 /// internal use (for technical use inside the library itself), false
9199 /// otherwise. If you don't know what this is for, then set it to
9200 /// false.
9201 ///
9202 /// @return the pretty representation of the type.
9203 string
9204 get_pretty_representation(const type_base* t, bool internal)
9205 {
9206  if (!t)
9207  return "void";
9208  if (const function_type* fn_type = is_function_type(t))
9209  return get_pretty_representation(fn_type, internal);
9210 
9211  const decl_base* d = get_type_declaration(t);
9212  ABG_ASSERT(d);
9213  return get_pretty_representation(d, internal);
9214 }
9215 
9216 /// Get a copy of the pretty representation of a decl.
9217 ///
9218 /// @param d the decl to consider.
9219 ///
9220 /// @param internal set to true if the call is intended for an
9221 /// internal use (for technical use inside the library itself), false
9222 /// otherwise. If you don't know what this is for, then set it to
9223 /// false.
9224 ///
9225 /// @return the pretty representation of the decl.
9226 string
9227 get_pretty_representation(const decl_base_sptr& d, bool internal)
9228 {return get_pretty_representation(d.get(), internal);}
9229 
9230 /// Get a copy of the pretty representation of a type.
9231 ///
9232 /// @param d the type to consider.
9233 ///
9234 /// @param internal set to true if the call is intended for an
9235 /// internal use (for technical use inside the library itself), false
9236 /// otherwise. If you don't know what this is for, then set it to
9237 /// false.
9238 ///
9239 /// @return the pretty representation of the type.
9240 string
9241 get_pretty_representation(const type_base_sptr& t, bool internal)
9242 {return get_pretty_representation(t.get(), internal);}
9243 
9244 /// Get the pretty representation of a function type.
9245 ///
9246 /// @param fn_type the function type to consider.
9247 ///
9248 /// @param internal set to true if the call is intended for an
9249 /// internal use (for technical use inside the library itself), false
9250 /// otherwise. If you don't know what this is for, then set it to
9251 /// false.
9252 ///
9253 /// @return the string represenation of the function type.
9254 string
9256  bool internal)
9257 {return get_pretty_representation(fn_type.get(), internal);}
9258 
9259 /// Get the pretty representation of a function type.
9260 ///
9261 /// @param fn_type the function type to consider.
9262 ///
9263 /// @param internal set to true if the call is intended for an
9264 /// internal use (for technical use inside the library itself), false
9265 /// otherwise. If you don't know what this is for, then set it to
9266 /// false.
9267 ///
9268 /// @return the string represenation of the function type.
9269 string
9270 get_pretty_representation(const function_type* fn_type, bool internal)
9271 {
9272  if (!fn_type)
9273  return "void";
9274 
9275  if (const method_type* method = is_method_type(fn_type))
9276  return get_pretty_representation(method, internal);
9277 
9278  return get_pretty_representation(*fn_type, internal);
9279 }
9280 
9281 /// Get the pretty representation of a function type.
9282 ///
9283 /// @param fn_type the function type to consider.
9284 ///
9285 /// @param internal set to true if the call is intended for an
9286 /// internal use (for technical use inside the library itself), false
9287 /// otherwise. If you don't know what this is for, then set it to
9288 /// false.
9289 ///
9290 /// @return the string represenation of the function type.
9291 string
9292 get_pretty_representation(const function_type& fn_type, bool internal)
9293 {
9294  std::ostringstream o;
9295  o << "function type " << get_function_type_name(fn_type, internal);
9296  return o.str();
9297 }
9298 
9299 /// Get the pretty representation of a method type.
9300 ///
9301 /// @param method the method type to consider.
9302 ///
9303 /// @param internal set to true if the call is intended for an
9304 /// internal use (for technical use inside the library itself), false
9305 /// otherwise. If you don't know what this is for, then set it to
9306 /// false.
9307 ///
9308 /// @return the string represenation of the method type.
9309 string
9310 get_pretty_representation(const method_type& method, bool internal)
9311 {
9312  std::ostringstream o;
9313  o << "method type " << get_method_type_name(method, internal);
9314  return o.str();
9315 }
9316 
9317 /// Get the pretty representation of a method type.
9318 ///
9319 /// @param method the method type to consider.
9320 ///
9321 /// @param internal set to true if the call is intended for an
9322 /// internal use (for technical use inside the library itself), false
9323 /// otherwise. If you don't know what this is for, then set it to
9324 /// false.
9325 ///
9326 /// @return the string represenation of the method type.
9327 string
9328 get_pretty_representation(const method_type* method, bool internal)
9329 {
9330  if (!method)
9331  return "void";
9332  return get_pretty_representation(*method, internal);
9333 }
9334 
9335 /// Get the pretty representation of a method type.
9336 ///
9337 /// @param method the method type to consider.
9338 ///
9339 /// @param internal set to true if the call is intended for an
9340 /// internal use (for technical use inside the library itself), false
9341 /// otherwise. If you don't know what this is for, then set it to
9342 /// false.
9343 ///
9344 /// @return the string represenation of the method type.
9345 string
9346 get_pretty_representation(const method_type_sptr method, bool internal)
9347 {return get_pretty_representation(method.get(), internal);}
9348 
9349 /// Get the flat representation of an instance of @ref class_or_union
9350 /// type.
9351 ///
9352 /// The flat representation of a given @ref class_or_union type is the
9353 /// actual definition of the type, for instance:
9354 ///
9355 /// struct foo {int a; char b;}
9356 ///
9357 ///@param cou the instance of @ref class_or_union to consider.
9358 ///
9359 ///@param indent the identation spaces to use in the representation.
9360 ///
9361 ///@param one_line if true, then the flat representation stands on one
9362 ///line. Otherwise, it stands on multiple lines.
9363 ///
9364 ///@return the resulting flat representation.
9365 string
9367  const string& indent,
9368  bool one_line,
9369  bool internal,
9370  bool qualified_names)
9371 {
9372  string repr;
9373  string local_indent = " ";
9374 
9375  if (class_decl* clazz = is_class_type(&cou))
9376  {
9377  repr = indent;
9378  if (!internal && clazz->is_struct())
9379  repr += "struct";
9380  else
9381  repr += "class";
9382  }
9383  else if (is_union_type(cou))
9384  repr = indent + "union";
9385  else
9386  return "";
9387 
9388  repr += " ";
9389 
9390  string name = cou.get_qualified_name();
9391 
9392  if (!cou.get_is_anonymous())
9393  repr += name;
9394 
9395  repr += "{";
9396 
9397  if (!one_line)
9398  repr += "\n";
9399 
9400  string real_indent;
9401  const class_or_union::data_members &dmems = cou.get_data_members();
9402  for (class_or_union::data_members::const_iterator dm = dmems.begin();
9403  dm != dmems.end();
9404  ++dm)
9405  {
9406  if (dm != dmems.begin())
9407  {
9408  if (one_line)
9409  real_indent = " ";
9410  else
9411  real_indent = "\n" + indent + local_indent;
9412  }
9413 
9415  repr +=
9418  real_indent, one_line, internal, qualified_names);
9419  else
9420  {
9421  if (one_line)
9422  {
9423  if (dm != dmems.begin())
9424  repr += real_indent;
9425  repr += (*dm)->get_pretty_representation(internal,
9426  qualified_names);
9427  }
9428  else
9429  repr +=
9430  real_indent+ (*dm)->get_pretty_representation(internal,
9431  qualified_names);
9432  }
9433  repr += ";";
9434  }
9435 
9436  if (one_line)
9437  repr += "}";
9438  else
9439  repr += indent + "}";
9440 
9441  return repr;
9442 }
9443 
9444 /// Get the flat representation of an instance of @ref class_or_union
9445 /// type.
9446 ///
9447 /// The flat representation of a given @ref class_or_union type is the
9448 /// actual definition of the type, for instance:
9449 ///
9450 /// struct foo {int a; char b;}
9451 ///
9452 ///@param cou the instance of @ref class_or_union to consider.
9453 ///
9454 ///@param indent the identation spaces to use in the representation.
9455 ///
9456 ///@param one_line if true, then the flat representation stands on one
9457 ///line. Otherwise, it stands on multiple lines.
9458 ///
9459 ///@return the resulting flat representation.
9460 string
9462  const string& indent,
9463  bool one_line,
9464  bool internal,
9465  bool qualified_names)
9466 {
9467  if (cou)
9468  return get_class_or_union_flat_representation(*cou, indent, one_line,
9469  internal, qualified_names);
9470  return "";
9471 }
9472 
9473 /// Get the flat representation of an instance of @ref class_or_union
9474 /// type.
9475 ///
9476 /// The flat representation of a given @ref class_or_union type is the
9477 /// actual definition of the type, for instance:
9478 ///
9479 /// struct foo {int a; char b;}
9480 ///
9481 ///@param cou the instance of @ref class_or_union to consider.
9482 ///
9483 ///@param indent the identation spaces to use in the representation.
9484 ///
9485 ///@param one_line if true, then the flat representation stands on one
9486 ///line. Otherwise, it stands on multiple lines.
9487 ///
9488 ///@return the resulting flat representation.
9489 string
9490 get_class_or_union_flat_representation(const class_or_union_sptr& cou,
9491  const string& indent,
9492  bool one_line,
9493  bool internal,
9494  bool qualified_names)
9495 {return get_class_or_union_flat_representation(cou.get(),
9496  indent,
9497  one_line,
9498  internal,
9499  qualified_names);}
9500 
9501 /// Get the textual representation of a type for debugging purposes.
9502 ///
9503 /// If the type is a class/union, this shows the data members, virtual
9504 /// member functions, size, pointer value of its canonical type, etc.
9505 /// Otherwise, this just shows the name of the artifact as returned by
9506 /// type_or_decl_base:get_pretty_representation().
9507 ///
9508 /// @param artifact the artifact to show a debugging representation of.
9509 ///
9510 /// @return a debugging string representation of @p artifact.
9511 string
9513 {
9514  if (!artifact)
9515  return string("");
9516 
9517  class_or_union * c = is_class_or_union_type(artifact);
9518  if (c)
9519  {
9520  class_decl *clazz = is_class_type(c);
9521  string name = c->get_qualified_name();
9522  std::ostringstream o;
9523  if (clazz)
9524  {
9525  if (clazz->is_struct())
9526  o << "struct ";
9527  else
9528  o << "class ";
9529  }
9530  else if (is_union_type(c))
9531  o << "union ";
9532  o << name;
9533 
9534  if (clazz)
9535  {
9536  if (!clazz->get_base_specifiers().empty())
9537  o << " :" << std::endl;
9538  for (auto &b : clazz->get_base_specifiers())
9539  {
9540  o << " ";
9541  if (b->get_is_virtual())
9542  o << "virtual ";
9543  o << b->get_base_class()->get_qualified_name()
9544  << std::endl;
9545  }
9546  }
9547  o << std::endl
9548  << "{"
9549  << " // size in bits: " << c->get_size_in_bits() << "\n"
9550  << " // is-declaration-only: " << c->get_is_declaration_only() << "\n"
9551  << " // definition point: " << get_natural_or_artificial_location(c).expand() << "\n"
9552  << " // translation unit: " << c->get_translation_unit()->get_absolute_path() << std::endl
9553  << " // @: " << std::hex << is_type(c)
9554  << ", @canonical: " << c->get_canonical_type().get() << std::dec
9555  << "\n\n";
9556 
9557  for (auto m : c->get_data_members())
9558  {
9559  type_base_sptr t = m->get_type();
9561 
9562  o << " "
9563  << m->get_pretty_representation(/*internal=*/false,
9564  /*qualified=*/false)
9565  << ";";
9566 
9567  if (t && t->get_canonical_type())
9568  o << " // uses canonical type '@"
9569  << std::hex << t->get_canonical_type().get() << std::dec;
9570 
9571  o << "'" << std::endl;
9572  }
9573 
9574  if (clazz && clazz->has_vtable())
9575  {
9576  o << " // virtual member functions\n\n";
9577  for (auto f : clazz->get_virtual_mem_fns())
9578  o << " " << f->get_pretty_representation(/*internal=*/false,
9579  /*qualified=*/false)
9580  << ";" << std::endl;
9581  }
9582 
9583  o << "};" << std::endl;
9584 
9585  return o.str();
9586  }
9587  else if (const enum_type_decl* e = is_enum_type(artifact))
9588  {
9589  string name = e->get_qualified_name();
9590  std::ostringstream o;
9591  o << "union " << name
9592  << " : "
9593  << e->get_underlying_type()->get_pretty_representation(/*internal=*/false,
9594  true)
9595  << "\n"
9596  << "{\n"
9597  << " // size in bits: " << e->get_size_in_bits() << "\n"
9598  << " // is-declaration-only: " << e->get_is_declaration_only() << "\n"
9599  << " // definition point: " << get_natural_or_artificial_location(e).expand() << "\n"
9600  << " // translation unit: "
9601  << e->get_translation_unit()->get_absolute_path() << "\n"
9602  << " // @: " << std::hex << is_type(e)
9603  << ", @canonical: " << e->get_canonical_type().get() << std::dec
9604  << "\n\n";
9605 
9606  for (const auto &enom : e->get_enumerators())
9607  o << " " << enom.get_name() << " = " << enom.get_value() << ",\n";
9608 
9609  o << "};\n";
9610 
9611  return o.str();
9612  }
9613  return artifact->get_pretty_representation(/*internal=*/true,
9614  /*qualified=*/true);
9615 }
9616 
9617 /// Get a given data member, referred to by its name, of a class type.
9618 ///
9619 /// @param clazz the class to consider.
9620 ///
9621 /// @param member_name name of the data member to get.
9622 ///
9623 /// @return the resulting data member or nullptr if none was found.
9625 get_data_member(class_or_union *clazz, const char* member_name)
9626 {
9627  if (!clazz)
9628  return var_decl_sptr();
9629  return clazz->find_data_member(member_name);
9630 }
9631 
9632 /// Get a given data member, referred to by its name, of a class type.
9633 ///
9634 /// @param clazz the class to consider.
9635 ///
9636 /// @param member_name name of the data member to get.
9637 ///
9638 /// @return the resulting data member or nullptr if none was found.
9640 get_data_member(type_base *clazz, const char* member_name)
9641 {return get_data_member(is_class_or_union_type(clazz), member_name);}
9642 
9643 /// Get the non-artificial (natural) location of a decl.
9644 ///
9645 /// If the decl doesn't have a natural location then return its
9646 /// artificial one.
9647 ///
9648 /// @param decl the decl to consider.
9649 ///
9650 /// @return the natural location @p decl if it has one; otherwise,
9651 /// return its artificial one.
9652 const location&
9654 {
9655  ABG_ASSERT(decl);
9656 
9657  if (decl->get_location())
9658  return decl->get_location();
9659  return decl->get_artificial_location();
9660 }
9661 
9662 /// Get the artificial location of a decl.
9663 ///
9664 /// If the decl doesn't have an artificial location then return its
9665 /// natural one.
9666 ///
9667 /// @param decl the decl to consider.
9668 ///
9669 /// @return the artificial location @p decl if it has one; otherwise,
9670 /// return its natural one.
9671 const location&
9673 {
9674  ABG_ASSERT(decl);
9675 
9676  if (decl->has_artificial_location())
9677  return decl->get_artificial_location();
9678  return decl->get_location();
9679 }
9680 
9681 /// Emit a textual representation of an artifact to std error stream
9682 /// for debugging purposes.
9683 ///
9684 /// This is useful to invoke from within a command line debugger like
9685 /// GDB to help make sense of a given ABI artifact.
9686 ///
9687 /// @param artifact the ABI artifact to emit the debugging
9688 /// representation for.
9689 ///
9690 /// @return the artifact @p artifact.
9692 debug(const type_or_decl_base* artifact)
9693 {
9694  std::cerr << get_debug_representation(artifact) << std::endl;
9695  return const_cast<type_or_decl_base*>(artifact);
9696 }
9697 
9698 /// Emit a textual representation of an artifact to std error stream
9699 /// for debugging purposes.
9700 ///
9701 /// This is useful to invoke from within a command line debugger like
9702 /// GDB to help make sense of a given ABI artifact.
9703 ///
9704 /// @param artifact the ABI artifact to emit the debugging
9705 /// representation for.
9706 ///
9707 /// @return the artifact @p artifact.
9708 type_base*
9709 debug(const type_base* artifact)
9710 {
9711  debug(static_cast<const type_or_decl_base*>(artifact));
9712  return const_cast<type_base*>(artifact);
9713 }
9714 
9715 /// Emit a textual representation of an artifact to std error stream
9716 /// for debugging purposes.
9717 ///
9718 /// This is useful to invoke from within a command line debugger like
9719 /// GDB to help make sense of a given ABI artifact.
9720 ///
9721 /// @param artifact the ABI artifact to emit the debugging
9722 /// representation for.
9723 ///
9724 /// @return the artifact @p artifact.
9725 decl_base*
9726 debug(const decl_base* artifact)
9727 {
9728  debug(static_cast<const type_or_decl_base*>(artifact));
9729  return const_cast<decl_base*>(artifact);
9730 }
9731 
9732 /// Test if two ABI artifacts are equal.
9733 ///
9734 /// This can be useful when used from the command line of a debugger
9735 /// like GDB.
9736 ///
9737 /// @param l the first ABI artifact to consider in the comparison.
9738 ///
9739 /// @param r the second ABI artifact to consider in the comparison.
9740 ///
9741 /// @return true iff @p l equals @p r.
9742 bool
9744 {
9745  if (!!l != !!r)
9746  return false;
9747  if (!l && !r)
9748  return true;
9749 
9750  return (*l == *r);
9751 }
9752 
9753 /// Emit a trace of a comparison operand stack.
9754 ///
9755 /// @param vect the operand stack to emit the trace for.
9756 ///
9757 /// @param o the output stream to emit the trace to.
9758 static void
9759 debug_comp_vec(const vector<const type_base*>& vect, std::ostringstream& o)
9760 {
9761  for (auto t : vect)
9762  {
9763  o << "|" << t->get_pretty_representation()
9764  << "@" << std::hex << t << std::dec;
9765  }
9766  if (!vect.empty())
9767  o << "|";
9768 }
9769 
9770 /// Construct a trace of the two comparison operand stacks.
9771 ///
9772 /// @param the environment in which the comparison operand stacks are.
9773 ///
9774 /// @return a string representing the trace.
9775 static string
9776 print_comp_stack(const environment& env)
9777 {
9778  std::ostringstream o;
9779  o << "left-operands: ";
9780  debug_comp_vec(env.priv_->left_type_comp_operands_, o);
9781  o << "\n" << "right-operands: ";
9782  debug_comp_vec(env.priv_->right_type_comp_operands_, o);
9783  o << "\n";
9784  return o.str();
9785 }
9786 
9787 /// Emit a trace of the two comparison operands stack on the standard
9788 /// error stream.
9789 ///
9790 /// @param env the environment the comparison operands stack belong
9791 /// to.
9792 void
9794 {
9795  std::cerr << print_comp_stack(env);
9796  std::cerr << std::endl;
9797 }
9798 
9799 /// By looking at the language of the TU a given ABI artifact belongs
9800 /// to, test if the ONE Definition Rule should apply.
9801 ///
9802 /// To date, it applies to c++, java and ada.
9803 ///
9804 /// @param artifact the ABI artifact to consider.
9805 ///
9806 /// @return true iff the One Definition Rule should apply.
9807 bool
9809 {
9811  artifact.get_translation_unit()->get_language();
9812 
9813  if (is_cplus_plus_language(l)
9814  || is_java_language(l)
9815  || is_ada_language(l))
9816  return true;
9817 
9818  return false;
9819 }
9820 
9821 /// Get the declaration for a given type.
9822 ///
9823 /// @param t the type to consider.
9824 ///
9825 /// @return the declaration for the type to return.
9826 const decl_base*
9828 {return dynamic_cast<const decl_base*>(t);}
9829 
9830 /// Get the declaration for a given type.
9831 ///
9832 /// @param t the type to consider.
9833 ///
9834 /// @return the declaration for the type to return.
9835 decl_base*
9837 {return dynamic_cast<decl_base*>(t);}
9838 
9839 /// Get the declaration for a given type.
9840 ///
9841 /// @param t the type to consider.
9842 ///
9843 /// @return the declaration for the type to return.
9844 decl_base_sptr
9845 get_type_declaration(const type_base_sptr t)
9846 {return dynamic_pointer_cast<decl_base>(t);}
9847 
9848 /// Test if two types are equal modulo a typedef.
9849 ///
9850 /// Type A and B are compatible if
9851 ///
9852 /// - A and B are equal
9853 /// - or if one type is a typedef of the other one.
9854 ///
9855 /// @param type1 the first type to consider.
9856 ///
9857 /// @param type2 the second type to consider.
9858 ///
9859 /// @return true iff @p type1 and @p type2 are compatible.
9860 bool
9861 types_are_compatible(const type_base_sptr type1,
9862  const type_base_sptr type2)
9863 {
9864  if (!type1 || !type2)
9865  return false;
9866 
9867  if (type1 == type2)
9868  return true;
9869 
9870  // Normally we should strip typedefs entirely, but this is
9871  // potentially costly, especially on binaries with huge changesets
9872  // like the Linux Kernel. So we just get the leaf types for now.
9873  //
9874  // Maybe there should be an option by which users accepts to pay the
9875  // CPU usage toll in exchange for finer filtering?
9876 
9877  // type_base_sptr t1 = strip_typedef(type1);
9878  // type_base_sptr t2 = strip_typedef(type2);
9879 
9880  type_base_sptr t1 = peel_typedef_type(type1);
9881  type_base_sptr t2 = peel_typedef_type(type2);
9882 
9883  return t1 == t2;
9884 }
9885 
9886 /// Test if two types are equal modulo a typedef.
9887 ///
9888 /// Type A and B are compatible if
9889 ///
9890 /// - A and B are equal
9891 /// - or if one type is a typedef of the other one.
9892 ///
9893 /// @param type1 the declaration of the first type to consider.
9894 ///
9895 /// @param type2 the declaration of the second type to consider.
9896 ///
9897 /// @return true iff @p type1 and @p type2 are compatible.
9898 bool
9899 types_are_compatible(const decl_base_sptr d1,
9900  const decl_base_sptr d2)
9901 {return types_are_compatible(is_type(d1), is_type(d2));}
9902 
9903 /// Return the translation unit a declaration belongs to.
9904 ///
9905 /// @param decl the declaration to consider.
9906 ///
9907 /// @return the resulting translation unit, or null if the decl is not
9908 /// yet added to a translation unit.
9911 {return const_cast<translation_unit*>(decl.get_translation_unit());}
9912 
9913 /// Return the translation unit a declaration belongs to.
9914 ///
9915 /// @param decl the declaration to consider.
9916 ///
9917 /// @return the resulting translation unit, or null if the decl is not
9918 /// yet added to a translation unit.
9921 {return decl ? get_translation_unit(*decl) : 0;}
9922 
9923 /// Return the translation unit a declaration belongs to.
9924 ///
9925 /// @param decl the declaration to consider.
9926 ///
9927 /// @return the resulting translation unit, or null if the decl is not
9928 /// yet added to a translation unit.
9930 get_translation_unit(const shared_ptr<decl_base> decl)
9931 {return get_translation_unit(decl.get());}
9932 
9933 /// Tests whether if a given scope is the global scope.
9934 ///
9935 /// @param scope the scope to consider.
9936 ///
9937 /// @return true iff the current scope is the global one.
9938 bool
9940 {return !!dynamic_cast<const global_scope*>(&scope);}
9941 
9942 /// Tests whether if a given scope is the global scope.
9943 ///
9944 /// @param scope the scope to consider.
9945 ///
9946 /// @return the @ref global_scope* representing the scope @p scope or
9947 /// 0 if @p scope is not a global scope.
9948 const global_scope*
9950 {return dynamic_cast<const global_scope*>(scope);}
9951 
9952 /// Tests whether if a given scope is the global scope.
9953 ///
9954 /// @param scope the scope to consider.
9955 ///
9956 /// @return true iff the current scope is the global one.
9957 bool
9958 is_global_scope(const shared_ptr<scope_decl>scope)
9959 {return is_global_scope(scope.get());}
9960 
9961 /// Tests whether a given declaration is at global scope.
9962 ///
9963 /// @param decl the decl to consider.
9964 ///
9965 /// @return true iff decl is at global scope.
9966 bool
9968 {return (is_global_scope(decl.get_scope()));}
9969 
9970 /// Tests whether a given declaration is at global scope.
9971 ///
9972 /// @param decl the decl to consider.
9973 ///
9974 /// @return true iff decl is at global scope.
9975 bool
9976 is_at_global_scope(const decl_base_sptr decl)
9977 {return (decl && is_global_scope(decl->get_scope()));}
9978 
9979 /// Tests whether a given declaration is at global scope.
9980 ///
9981 /// @param decl the decl to consider.
9982 ///
9983 /// @return true iff decl is at global scope.
9984 bool
9986 {return is_at_global_scope(*decl);}
9987 
9988 /// Tests whether a given decl is at class scope.
9989 ///
9990 /// @param decl the decl to consider.
9991 ///
9992 /// @return true iff decl is at class scope.
9994 is_at_class_scope(const decl_base_sptr decl)
9995 {return is_at_class_scope(decl.get());}
9996 
9997 /// Tests whether a given decl is at class scope.
9998 ///
9999 /// @param decl the decl to consider.
10000 ///
10001 /// @return true iff decl is at class scope.
10004 {
10005  if (!decl)
10006  return 0;
10007 
10008  return is_at_class_scope(*decl);
10009 }
10010 
10011 /// Tests whether a given decl is at class scope.
10012 ///
10013 /// @param decl the decl to consider.
10014 ///
10015 /// @return true iff decl is at class scope.
10018 {
10019  scope_decl* scope = decl.get_scope();
10020  if (class_or_union* cl = is_class_type(scope))
10021  return cl;
10022  if (class_or_union* cl = is_union_type(scope))
10023  return cl;
10024  return 0;
10025 }
10026 
10027 /// Find a data member inside an anonymous data member.
10028 ///
10029 /// An anonymous data member has a type which is a class or union.
10030 /// This function looks for a data member inside the type of that
10031 /// anonymous data member.
10032 ///
10033 /// @param anon_dm the anonymous data member to consider.
10034 ///
10035 /// @param name the name of the data member to look for.
10038  const string& name)
10039 {
10040  const class_or_union* containing_class_or_union =
10042 
10043  if (!containing_class_or_union)
10044  return var_decl_sptr();
10045 
10046  var_decl_sptr result = containing_class_or_union->find_data_member(name);
10047  return result;
10048 }
10049 
10050 /// Tests whether a given decl is at template scope.
10051 ///
10052 /// Note that only template parameters , types that are compositions,
10053 /// and template patterns (function or class) can be at template scope.
10054 ///
10055 /// @param decl the decl to consider.
10056 ///
10057 /// @return true iff the decl is at template scope.
10058 bool
10059 is_at_template_scope(const shared_ptr<decl_base> decl)
10060 {return (decl && dynamic_cast<template_decl*>(decl->get_scope()));}
10061 
10062 /// Tests whether a decl is a template parameter.
10063 ///
10064 /// @param decl the decl to consider.
10065 ///
10066 /// @return true iff decl is a template parameter.
10067 bool
10068 is_template_parameter(const shared_ptr<decl_base> decl)
10069 {
10070  return (decl && (dynamic_pointer_cast<type_tparameter>(decl)
10071  || dynamic_pointer_cast<non_type_tparameter>(decl)
10072  || dynamic_pointer_cast<template_tparameter>(decl)));
10073 }
10074 
10075 /// Test whether a declaration is a @ref function_decl.
10076 ///
10077 /// @param d the declaration to test for.
10078 ///
10079 /// @return a shared pointer to @ref function_decl if @p d is a @ref
10080 /// function_decl. Otherwise, a nil shared pointer.
10083 {return dynamic_cast<function_decl*>(const_cast<type_or_decl_base*>(d));}
10084 
10085 /// Test whether a declaration is a @ref function_decl.
10086 ///
10087 /// @param d the declaration to test for.
10088 ///
10089 /// @return true if @p d is a function_decl.
10090 bool
10092 {return is_function_decl(&d);}
10093 
10094 /// Test whether a declaration is a @ref function_decl.
10095 ///
10096 /// @param d the declaration to test for.
10097 ///
10098 /// @return a shared pointer to @ref function_decl if @p d is a @ref
10099 /// function_decl. Otherwise, a nil shared pointer.
10102 {return dynamic_pointer_cast<function_decl>(d);}
10103 
10104 /// Test whether a declaration is a @ref function_decl.
10105 ///
10106 /// @param d the declaration to test for.
10107 ///
10108 /// @return a pointer to @ref function_decl if @p d is a @ref
10109 /// function_decl. Otherwise, a nil shared pointer.
10112 {
10113  return dynamic_cast<function_decl::parameter*>
10114  (const_cast<type_or_decl_base*>(tod));
10115 }
10116 
10117 /// Test whether an ABI artifact is a @ref function_decl.
10118 ///
10119 /// @param tod the declaration to test for.
10120 ///
10121 /// @return a pointer to @ref function_decl if @p d is a @ref
10122 /// function_decl. Otherwise, a nil shared pointer.
10125 {return dynamic_pointer_cast<function_decl::parameter>(tod);}
10126 
10127 /// Test if an ABI artifact is a declaration.
10128 ///
10129 /// @param d the artifact to consider.
10130 ///
10131 /// @param return the declaration sub-object of @p d if it's a
10132 /// declaration, or NULL if it is not.
10133 decl_base*
10135 {
10136  if (d && (d->kind() & type_or_decl_base::ABSTRACT_DECL_BASE))
10137  {
10138  if (!(d->kind() & type_or_decl_base::ABSTRACT_TYPE_BASE))
10139  // The artifact is a decl-only (like a function or a
10140  // variable). That is, it's not a type that also has a
10141  // declaration. In this case, we are in the fast path and we
10142  // have a pointer to the decl sub-object handy. Just return
10143  // it ...
10144  return reinterpret_cast<decl_base*>
10145  (const_cast<type_or_decl_base*>(d)->type_or_decl_base_pointer());
10146 
10147  // ... Otherwise, we are in the slow path, which is that the
10148  // artifact is a type which has a declaration. In that case,
10149  // let's use the slow dynamic_cast because we don't have the
10150  // pointer to the decl sub-object handily present.
10151  return dynamic_cast<decl_base*>(const_cast<type_or_decl_base*>(d));
10152  }
10153  return 0;
10154 }
10155 
10156 /// Test if an ABI artifact is a declaration.
10157 ///
10158 /// @param d the artifact to consider.
10159 ///
10160 /// @param return the declaration sub-object of @p d if it's a
10161 /// declaration, or NULL if it is not.
10162 decl_base_sptr
10164 {return dynamic_pointer_cast<decl_base>(d);}
10165 
10166 /// Test if an ABI artifact is a declaration.
10167 ///
10168 /// This is done using a slow path that uses dynamic_cast.
10169 ///
10170 /// @param d the artifact to consider.
10171 ///
10172 /// @param return the declaration sub-object of @p d if it's a
10173 decl_base*
10175 {return dynamic_cast<decl_base*>(const_cast<type_or_decl_base*>(t));}
10176 
10177 /// Test if an ABI artifact is a declaration.
10178 ///
10179 /// This is done using a slow path that uses dynamic_cast.
10180 ///
10181 /// @param d the artifact to consider.
10182 ///
10183 /// @param return the declaration sub-object of @p d if it's a
10184 decl_base_sptr
10186 {return dynamic_pointer_cast<decl_base>(t);}
10187 
10188 /// Test whether a declaration is a type.
10189 ///
10190 /// @param d the IR artefact to test for.
10191 ///
10192 /// @return true if the artifact is a type, false otherwise.
10193 bool
10195 {
10196  if (dynamic_cast<const type_base*>(&tod))
10197  return true;
10198  return false;
10199 }
10200 
10201 /// Test whether a declaration is a type.
10202 ///
10203 /// @param d the IR artefact to test for.
10204 ///
10205 /// @return true if the artifact is a type, false otherwise.
10206 type_base*
10208 {
10209  if (t && (t->kind() & type_or_decl_base::ABSTRACT_TYPE_BASE))
10210  return reinterpret_cast<type_base*>
10211  (const_cast<type_or_decl_base*>(t)->type_or_decl_base_pointer());
10212 
10213  return 0;
10214 }
10215 
10216 /// Test whether a declaration is a type.
10217 ///
10218 /// @param d the IR artefact to test for.
10219 ///
10220 /// @return true if the artifact is a type, false otherwise.
10221 type_base_sptr
10223 {return dynamic_pointer_cast<type_base>(tod);}
10224 
10225 /// Test whether a declaration is a type.
10226 ///
10227 /// @param d the declaration to test for.
10228 ///
10229 /// @return true if the declaration is a type, false otherwise.
10230 
10231 /// Test if a given type is anonymous.
10232 ///
10233 /// Note that this function considers that an anonymous class that is
10234 /// named by a typedef is not anonymous anymore. This is the C idiom:
10235 ///
10236 /// typedef struct {int member;} s_type;
10237 ///
10238 /// The typedef s_type becomes the name of the originally anonymous
10239 /// struct.
10240 ///
10241 /// @param t the type to consider.
10242 ///
10243 /// @return true iff @p t is anonymous.
10244 bool
10246 {
10247  const decl_base* d = get_type_declaration(t);
10248  if (d)
10249  if (d->get_is_anonymous())
10250  {
10251  if (class_or_union *cou = is_class_or_union_type(t))
10252  {
10253  // An anonymous class that is named by a typedef is not
10254  // considered anonymous anymore.
10255  if (!cou->get_naming_typedef())
10256  return true;
10257  }
10258  else
10259  return true;
10260  }
10261  return false;
10262 }
10263 
10264 /// Test if a given type is anonymous.
10265 ///
10266 /// @param t the type to consider.
10267 ///
10268 /// @return true iff @p t is anonymous.
10269 bool
10270 is_anonymous_type(const type_base_sptr& t)
10271 {return is_anonymous_type(t.get());}
10272 
10273 /// Test whether a type is a type_decl (a builtin type).
10274 ///
10275 /// @return the type_decl* for @t if it's type_decl, otherwise, return
10276 /// nil.
10277 const type_decl*
10279 {return dynamic_cast<const type_decl*>(t);}
10280 
10281 /// Test whether a type is a type_decl (a builtin type).
10282 ///
10283 /// @return the type_decl_sptr for @t if it's type_decl, otherwise,
10284 /// return nil.
10287 {return dynamic_pointer_cast<type_decl>(t);}
10288 
10289 /// Test if a type is an integral type.
10290 ///
10291 /// @param t the type to test.
10292 ///
10293 /// @return the integral type @p t can be converted to, or nil if @p
10294 /// is not an integral type.
10295 type_decl*
10297 {
10298  type_decl *type = const_cast<type_decl*>(is_type_decl(t));
10299  if (!type)
10300  return nullptr;
10301 
10302  integral_type int_type;
10303  if (!parse_integral_type(type->get_name(), int_type))
10304  return nullptr;
10305 
10306  return type;
10307 }
10308 
10309 /// Test if a type is an integral type.
10310 ///
10311 /// @param t the type to test.
10312 ///
10313 /// @return the integral type @p t can be converted to, or nil if @p
10314 /// is not an integral type.
10317 {
10318  const type_decl_sptr type = is_type_decl(t);
10319  if (!type)
10320  return type_decl_sptr();
10321 
10322  integral_type int_type;
10323  if (!parse_integral_type(type->get_name(), int_type))
10324  return type_decl_sptr();
10325 
10326  return type;
10327 }
10328 
10329 /// Test whether a type is a typedef.
10330 ///
10331 /// @param t the type to test for.
10332 ///
10333 /// @return the typedef declaration of the @p t, or NULL if it's not a
10334 /// typedef.
10337 {return dynamic_pointer_cast<typedef_decl>(t);}
10338 
10339 /// Test whether a type is a typedef.
10340 ///
10341 /// @param t the declaration of the type to test for.
10342 ///
10343 /// @return the typedef declaration of the @p t, or NULL if it's not a
10344 /// typedef.
10345 const typedef_decl*
10347 {return dynamic_cast<const typedef_decl*>(t);}
10348 
10349 /// Test whether a type is a typedef.
10350 ///
10351 /// @param t the declaration of the type to test for.
10352 ///
10353 /// @return the typedef declaration of the @p t, or NULL if it's not a
10354 /// typedef.
10355 typedef_decl*
10357 {return dynamic_cast<typedef_decl*>(t);}
10358 
10359 /// Test if a type is an enum. This function looks through typedefs.
10360 ///
10361 /// @parm t the type to consider.
10362 ///
10363 /// @return the enum_decl if @p t is an @ref enum_decl or null
10364 /// otherwise.
10366 is_compatible_with_enum_type(const type_base_sptr& t)
10367 {
10368  if (!t)
10369  return enum_type_decl_sptr();
10370 
10371  // Normally we should strip typedefs entirely, but this is
10372  // potentially costly, especially on binaries with huge changesets
10373  // like the Linux Kernel. So we just get the leaf types for now.
10374  //
10375  // Maybe there should be an option by which users accepts to pay the
10376  // CPU usage toll in exchange for finer filtering?
10377 
10378  // type_base_sptr ty = strip_typedef(t);
10379  type_base_sptr ty = peel_typedef_type(t);;
10380  return is_enum_type(ty);
10381 }
10382 
10383 /// Test if a type is an enum. This function looks through typedefs.
10384 ///
10385 /// @parm t the type to consider.
10386 ///
10387 /// @return the enum_decl if @p t is an @ref enum_decl or null
10388 /// otherwise.
10390 is_compatible_with_enum_type(const decl_base_sptr& t)
10392 
10393 /// Test if a decl is an enum_type_decl
10394 ///
10395 /// @param d the decl to test for.
10396 ///
10397 /// @return the enum_type_decl* if @p d is an enum, nil otherwise.
10398 const enum_type_decl*
10400 {return dynamic_cast<const enum_type_decl*>(d);}
10401 
10402 /// Test if a decl is an enum_type_decl
10403 ///
10404 /// @param d the decl to test for.
10405 ///
10406 /// @return the enum_type_decl_sptr if @p d is an enum, nil otherwise.
10409 {return dynamic_pointer_cast<enum_type_decl>(d);}
10410 
10411 /// Test if a type is a class. This function looks through typedefs.
10412 ///
10413 /// @parm t the type to consider.
10414 ///
10415 /// @return the class_decl if @p t is a class_decl or null otherwise.
10417 is_compatible_with_class_type(const type_base_sptr& t)
10418 {
10419  if (!t)
10420  return class_decl_sptr();
10421 
10422  // Normally we should strip typedefs entirely, but this is
10423  // potentially costly, especially on binaries with huge changesets
10424  // like the Linux Kernel. So we just get the leaf types for now.
10425  //
10426  // Maybe there should be an option by which users accepts to pay the
10427  // CPU usage toll in exchange for finer filtering?
10428 
10429  // type_base_sptr ty = strip_typedef(t);
10430  type_base_sptr ty = peel_typedef_type(t);
10431  return is_class_type(ty);
10432 }
10433 
10434 /// Test if a type is a class. This function looks through typedefs.
10435 ///
10436 /// @parm t the type to consider.
10437 ///
10438 /// @return the class_decl if @p t is a class_decl or null otherwise.
10440 is_compatible_with_class_type(const decl_base_sptr& t)
10442 
10443 /// Test whether a type is a class.
10444 ///
10445 /// @parm t the type to consider.
10446 ///
10447 /// @return true iff @p t is a class_decl.
10448 bool
10450 {return is_class_type(&t);}
10451 
10452 /// Test whether a type is a class.
10453 ///
10454 /// @parm t the type to consider.
10455 ///
10456 /// @return the class_decl if @p t is a class_decl or null otherwise.
10457 class_decl*
10459 {
10460  if (!t)
10461  return 0;
10462 
10463  if (t->kind() & type_or_decl_base::CLASS_TYPE)
10464  return reinterpret_cast<class_decl*>
10465  (const_cast<type_or_decl_base*>(t)->runtime_type_instance());
10466 
10467  return 0;
10468 }
10469 
10470 /// Test whether a type is a class.
10471 ///
10472 /// @parm t the type to consider.
10473 ///
10474 /// @return the class_decl if @p t is a class_decl or null otherwise.
10477 {return dynamic_pointer_cast<class_decl>(d);}
10478 
10479 
10480 /// Test wheter a type is a declaration-only class.
10481 ///
10482 /// @param t the type to considier.
10483 ///
10484 /// @return true iff @p t is a declaration-only class.
10485 bool
10487 {
10488  if (const class_or_union *klass = is_class_or_union_type(t))
10489  return klass->get_is_declaration_only();
10490  return false;
10491 }
10492 
10493 /// Test wheter a type is a declaration-only class.
10494 ///
10495 /// @param t the type to considier.
10496 ///
10497 /// @return true iff @p t is a declaration-only class.
10498 bool
10499 is_declaration_only_class_type(const type_base_sptr& t)
10500 {return is_declaration_only_class_or_union_type(t.get());}
10501 
10502 /// Test if a type is a @ref class_or_union.
10503 ///
10504 /// @param t the type to consider.
10505 ///
10506 /// @return the @ref class_or_union is @p is a @ref class_or_union, or
10507 /// nil otherwise.
10510 {return dynamic_cast<class_or_union*>(const_cast<type_or_decl_base*>(t));}
10511 
10512 /// Test if a type is a @ref class_or_union.
10513 ///
10514 /// @param t the type to consider.
10515 ///
10516 /// @return the @ref class_or_union is @p is a @ref class_or_union, or
10517 /// nil otherwise.
10518 shared_ptr<class_or_union>
10519 is_class_or_union_type(const shared_ptr<type_or_decl_base>& t)
10520 {return dynamic_pointer_cast<class_or_union>(t);}
10521 
10522 /// Test if a type is a @ref union_decl.
10523 ///
10524 /// @param t the type to consider.
10525 ///
10526 /// @return true iff @p t is a union_decl.
10527 bool
10529 {return is_union_type(&t);}
10530 
10531 /// Test if a type is a @ref union_decl.
10532 ///
10533 /// @param t the type to consider.
10534 ///
10535 /// @return the @ref union_decl is @p is a @ref union_decl, or nil
10536 /// otherwise.
10537 union_decl*
10539 {return dynamic_cast<union_decl*>(const_cast<type_or_decl_base*>(t));}
10540 
10541 /// Test if a type is a @ref union_decl.
10542 ///
10543 /// @param t the type to consider.
10544 ///
10545 /// @return the @ref union_decl is @p is a @ref union_decl, or nil
10546 /// otherwise.
10547 union_decl_sptr
10548 is_union_type(const shared_ptr<type_or_decl_base>& t)
10549 {return dynamic_pointer_cast<union_decl>(t);}
10550 
10551 /// Test whether a type is a pointer_type_def.
10552 ///
10553 /// @param t the type to test.
10554 ///
10555 /// @return the @ref pointer_type_def_sptr if @p t is a
10556 /// pointer_type_def, null otherwise.
10559 {
10560  if (!t)
10561  return 0;
10562 
10563  if (t->kind() & type_or_decl_base::POINTER_TYPE)
10564  return reinterpret_cast<pointer_type_def*>
10565  (const_cast<type_or_decl_base*>(t)->runtime_type_instance());
10566 
10567  return 0;
10568 }
10569 
10570 /// Test whether a type is a pointer_type_def.
10571 ///
10572 /// @param t the type to test.
10573 ///
10574 /// @return the @ref pointer_type_def_sptr if @p t is a
10575 /// pointer_type_def, null otherwise.
10576 const pointer_type_def*
10578 {
10579  return is_pointer_type(const_cast<type_or_decl_base*>(t));
10580 }
10581 
10582 /// Test whether a type is a pointer_type_def.
10583 ///
10584 /// @param t the type to test.
10585 ///
10586 /// @return the @ref pointer_type_def_sptr if @p t is a
10587 /// pointer_type_def, null otherwise.
10590 {return dynamic_pointer_cast<pointer_type_def>(t);}
10591 
10592 /// Test whether a type is a reference_type_def.
10593 ///
10594 /// @param t the type to test.
10595 ///
10596 /// @return the @ref reference_type_def_sptr if @p t is a
10597 /// reference_type_def, null otherwise.
10600 {return dynamic_cast<reference_type_def*>(t);}
10601 
10602 /// Test whether a type is a reference_type_def.
10603 ///
10604 /// @param t the type to test.
10605 ///
10606 /// @return the @ref reference_type_def_sptr if @p t is a
10607 /// reference_type_def, null otherwise.
10608 const reference_type_def*
10610 {return dynamic_cast<const reference_type_def*>(t);}
10611 
10612 /// Test whether a type is a reference_type_def.
10613 ///
10614 /// @param t the type to test.
10615 ///
10616 /// @return the @ref reference_type_def_sptr if @p t is a
10617 /// reference_type_def, null otherwise.
10620 {return dynamic_pointer_cast<reference_type_def>(t);}
10621 
10622 /// Test if a type is a pointer to void type.
10623 ///
10624 /// Note that this looks trough typedefs or CV qualifiers to look for
10625 /// the void pointer.
10626 ///
10627 /// @param type the type to consider.
10628 ///
10629 /// @return the actual void pointer if @p is a void pointer or NULL if
10630 /// it's not.
10631 const type_base*
10633 {
10634  type = peel_qualified_or_typedef_type(type);
10635 
10636  const pointer_type_def * t = is_pointer_type(type);
10637  if (!t)
10638  return 0;
10639 
10640  // Look through typedefs in the pointed-to type as well.
10641  type_base * ty = t->get_pointed_to_type().get();
10643  if (ty->get_environment().is_void_type(ty))
10644  return ty;
10645 
10646  return 0;
10647 }
10648 
10649 /// Test if a type is a pointer to void type.
10650 ///
10651 /// Note that this looks trough typedefs or CV qualifiers to look for
10652 /// the void pointer.
10653 ///
10654 /// @param type the type to consider.
10655 ///
10656 /// @return the actual void pointer if @p is a void pointer or NULL if
10657 /// it's not.
10658 const type_base*
10660 {return is_void_pointer_type(&type);}
10661 
10662 /// Test whether a type is a reference_type_def.
10663 ///
10664 /// @param t the type to test.
10665 ///
10666 /// @return the @ref reference_type_def_sptr if @p t is a
10667 /// reference_type_def, null otherwise.
10670 {return dynamic_cast<qualified_type_def*>(const_cast<type_or_decl_base*>(t));}
10671 
10672 /// Test whether a type is a qualified_type_def.
10673 ///
10674 /// @param t the type to test.
10675 ///
10676 /// @return the @ref qualified_type_def_sptr if @p t is a
10677 /// qualified_type_def, null otherwise.
10678 qualified_type_def_sptr
10680 {return dynamic_pointer_cast<qualified_type_def>(t);}
10681 
10682 /// Test whether a type is a function_type.
10683 ///
10684 /// @param t the type to test.
10685 ///
10686 /// @return the @ref function_type_sptr if @p t is a
10687 /// function_type, null otherwise.
10690 {return dynamic_pointer_cast<function_type>(t);}
10691 
10692 /// Test whether a type is a function_type.
10693 ///
10694 /// @param t the type to test.
10695 ///
10696 /// @return the @ref function_type_sptr if @p t is a
10697 /// function_type, null otherwise.
10700 {return dynamic_cast<function_type*>(t);}
10701 
10702 /// Test whether a type is a function_type.
10703 ///
10704 /// @param t the type to test.
10705 ///
10706 /// @return the @ref function_type_sptr if @p t is a
10707 /// function_type, null otherwise.
10708 const function_type*
10710 {return dynamic_cast<const function_type*>(t);}
10711 
10712 /// Test whether a type is a method_type.
10713 ///
10714 /// @param t the type to test.
10715 ///
10716 /// @return the @ref method_type_sptr if @p t is a
10717 /// method_type, null otherwise.
10720 {return dynamic_pointer_cast<method_type>(t);}
10721 
10722 /// Test whether a type is a method_type.
10723 ///
10724 /// @param t the type to test.
10725 ///
10726 /// @return the @ref method_type_sptr if @p t is a
10727 /// method_type, null otherwise.
10728 const method_type*
10730 {return dynamic_cast<const method_type*>(t);}
10731 
10732 /// Test whether a type is a method_type.
10733 ///
10734 /// @param t the type to test.
10735 ///
10736 /// @return the @ref method_type_sptr if @p t is a
10737 /// method_type, null otherwise.
10738 method_type*
10740 {return dynamic_cast<method_type*>(t);}
10741 
10742 /// If a class (or union) is a decl-only class, get its definition.
10743 /// Otherwise, just return the initial class.
10744 ///
10745 /// @param the_class the class (or union) to consider.
10746 ///
10747 /// @return either the definition of the class, or the class itself.
10750 {return is_class_or_union_type(look_through_decl_only(the_class));}
10751 
10752 /// If a class (or union) is a decl-only class, get its definition.
10753 /// Otherwise, just return the initial class.
10754 ///
10755 /// @param the_class the class (or union) to consider.
10756 ///
10757 /// @return either the definition of the class, or the class itself.
10758 class_or_union_sptr
10760 {return is_class_or_union_type(look_through_decl_only(the_class));}
10761 
10762 /// If a class (or union) is a decl-only class, get its definition.
10763 /// Otherwise, just return the initial class.
10764 ///
10765 /// @param klass the class (or union) to consider.
10766 ///
10767 /// @return either the definition of the class, or the class itself.
10768 class_or_union_sptr
10769 look_through_decl_only_class(class_or_union_sptr klass)
10771 
10772 /// If an enum is a decl-only enum, get its definition.
10773 /// Otherwise, just return the initial enum.
10774 ///
10775 /// @param the_enum the enum to consider.
10776 ///
10777 /// @return either the definition of the enum, or the enum itself.
10780 {return is_enum_type(look_through_decl_only(the_enum));}
10781 
10782 /// If an enum is a decl-only enum, get its definition.
10783 /// Otherwise, just return the initial enum.
10784 ///
10785 /// @param enom the enum to consider.
10786 ///
10787 /// @return either the definition of the enum, or the enum itself.
10790 {return is_enum_type(look_through_decl_only(enom));}
10791 
10792 /// If a decl is decl-only get its definition. Otherwise, just return nil.
10793 ///
10794 /// @param d the decl to consider.
10795 ///
10796 /// @return either the definition of the decl, or nil.
10797 decl_base_sptr
10799 {
10800  decl_base_sptr decl;
10801  if (d.get_is_declaration_only())
10802  decl = d.get_definition_of_declaration();
10803 
10804  if (!decl)
10805  return decl;
10806 
10807  while (decl->get_is_declaration_only()
10808  && decl->get_definition_of_declaration())
10809  decl = decl->get_definition_of_declaration();
10810 
10811  return decl;
10812 }
10813 
10814 /// If a decl is decl-only enum, get its definition. Otherwise, just
10815 /// return the initial decl.
10816 ///
10817 /// @param d the decl to consider.
10818 ///
10819 /// @return either the definition of the enum, or the decl itself.
10820 decl_base*
10822 {
10823  if (!d)
10824  return d;
10825 
10826  decl_base* result = look_through_decl_only(*d).get();
10827  if (!result)
10828  result = d;
10829 
10830  return result;
10831 }
10832 
10833 /// If a decl is decl-only get its definition. Otherwise, just return nil.
10834 ///
10835 /// @param d the decl to consider.
10836 ///
10837 /// @return either the definition of the decl, or nil.
10838 decl_base_sptr
10839 look_through_decl_only(const decl_base_sptr& d)
10840 {
10841  if (!d)
10842  return d;
10843 
10844  decl_base_sptr result = look_through_decl_only(*d);
10845  if (!result)
10846  result = d;
10847 
10848  return result;
10849 }
10850 
10851 /// If a type is is decl-only, then get its definition. Otherwise,
10852 /// just return the initial type.
10853 ///
10854 /// @param d the decl to consider.
10855 ///
10856 /// @return either the definition of the decl, or the initial type.
10857 type_base*
10859 {
10860  decl_base* d = is_decl(t);
10861  if (!d)
10862  return t;
10863  d = look_through_decl_only(d);
10864  return is_type(d);
10865 }
10866 
10867 /// If a type is is decl-only, then get its definition. Otherwise,
10868 /// just return the initial type.
10869 ///
10870 /// @param d the decl to consider.
10871 ///
10872 /// @return either the definition of the decl, or the initial type.
10873 type_base_sptr
10874 look_through_decl_only(const type_base_sptr& t)
10875 {
10876  decl_base_sptr d = is_decl(t);
10877  if (!d)
10878  return t;
10879  d = look_through_decl_only(d);
10880  return is_type(d);
10881 }
10882 
10883 /// Tests if a declaration is a variable declaration.
10884 ///
10885 /// @param decl the decl to test.
10886 ///
10887 /// @return the var_decl_sptr iff decl is a variable declaration; nil
10888 /// otherwise.
10889 var_decl*
10891 {return dynamic_cast<var_decl*>(const_cast<type_or_decl_base*>(tod));}
10892 
10893 /// Tests if a declaration is a variable declaration.
10894 ///
10895 /// @param decl the decl to test.
10896 ///
10897 /// @return the var_decl_sptr iff decl is a variable declaration; nil
10898 /// otherwise.
10901 {return dynamic_pointer_cast<var_decl>(decl);}
10902 
10903 /// Tests if a declaration is a namespace declaration.
10904 ///
10905 /// @param d the decalration to consider.
10906 ///
10907 /// @return the namespace declaration if @p d is a namespace.
10909 is_namespace(const decl_base_sptr& d)
10910 {return dynamic_pointer_cast<namespace_decl>(d);}
10911 
10912 /// Tests if a declaration is a namespace declaration.
10913 ///
10914 /// @param d the decalration to consider.
10915 ///
10916 /// @return the namespace declaration if @p d is a namespace.
10919 {return dynamic_cast<namespace_decl*>(const_cast<decl_base*>(d));}
10920 
10921 /// Tests whether a decl is a template parameter composition type.
10922 ///
10923 /// @param decl the declaration to consider.
10924 ///
10925 /// @return true iff decl is a template parameter composition type.
10926 bool
10927 is_template_parm_composition_type(const shared_ptr<decl_base> decl)
10928 {
10929  return (decl
10930  && is_at_template_scope(decl)
10931  && is_type(decl)
10932  && !is_template_parameter(decl));
10933 }
10934 
10935 /// Test whether a decl is the pattern of a function template.
10936 ///
10937 /// @param decl the decl to consider.
10938 ///
10939 /// @return true iff decl is the pattern of a function template.
10940 bool
10941 is_function_template_pattern(const shared_ptr<decl_base> decl)
10942 {
10943  return (decl
10944  && dynamic_pointer_cast<function_decl>(decl)
10945  && dynamic_cast<template_decl*>(decl->get_scope()));
10946 }
10947 
10948 /// Test if a type is an array_type_def.
10949 ///
10950 /// @param type the type to consider.
10951 ///
10952 /// @return true iff @p type is an array_type_def.
10955 {return dynamic_cast<array_type_def*>(const_cast<type_or_decl_base*>(type));}
10956 
10957 /// Test if a type is an array_type_def.
10958 ///
10959 /// @param type the type to consider.
10960 ///
10961 /// @return true iff @p type is an array_type_def.
10964 {return dynamic_pointer_cast<array_type_def>(type);}
10965 
10966 /// Tests if the element of a given array is a qualified type.
10967 ///
10968 /// @param array the array type to consider.
10969 ///
10970 /// @return the qualified element of the array iff it's a qualified
10971 /// type. Otherwise, return a nil object.
10972 qualified_type_def_sptr
10974 {
10975  if (!array)
10976  return qualified_type_def_sptr();
10977 
10978  return is_qualified_type(array->get_element_type());
10979 }
10980 
10981 /// Test if an array type is an array to a qualified element type.
10982 ///
10983 /// @param type the array type to consider.
10984 ///
10985 /// @return true the array @p type iff it's an array to a qualified
10986 /// element type.
10988 is_array_of_qualified_element(const type_base_sptr& type)
10989 {
10990  if (array_type_def_sptr array = is_array_type(type))
10991  if (is_array_of_qualified_element(array))
10992  return array;
10993 
10994  return array_type_def_sptr();
10995 }
10996 
10997 /// Test if a type is a typedef of an array.
10998 ///
10999 /// Note that the function looks through qualified and typedefs types
11000 /// of the underlying type of the current typedef. In other words, if
11001 /// we are looking at a typedef of a CV-qualified array, or at a
11002 /// typedef of a CV-qualified typedef of an array, this function will
11003 /// still return TRUE.
11004 ///
11005 /// @param t the type to consider.
11006 ///
11007 /// @return true if t is a typedef which underlying type is an array.
11008 /// That array might be either cv-qualified array or a typedef'ed
11009 /// array, or a combination of both.
11011 is_typedef_of_array(const type_base_sptr& t)
11012 {
11013  array_type_def_sptr result;
11014 
11015  if (typedef_decl_sptr typdef = is_typedef(t))
11016  {
11017  type_base_sptr u =
11018  peel_qualified_or_typedef_type(typdef->get_underlying_type());
11019  result = is_array_type(u);
11020  }
11021 
11022  return result;
11023 }
11024 
11025 /// Test if a type is an array_type_def::subrange_type.
11026 ///
11027 /// @param type the type to consider.
11028 ///
11029 /// @return the array_type_def::subrange_type which @p type is a type
11030 /// of, or nil if it's not of that type.
11031 array_type_def::subrange_type*
11033 {
11034  return dynamic_cast<array_type_def::subrange_type*>
11035  (const_cast<type_or_decl_base*>(type));
11036 }
11037 
11038 /// Test if a type is an array_type_def::subrange_type.
11039 ///
11040 /// @param type the type to consider.
11041 ///
11042 /// @return the array_type_def::subrange_type which @p type is a type
11043 /// of, or nil if it's not of that type.
11046 {return dynamic_pointer_cast<array_type_def::subrange_type>(type);}
11047 
11048 /// Tests whether a decl is a template.
11049 ///
11050 /// @param decl the decl to consider.
11051 ///
11052 /// @return true iff decl is a function template, class template, or
11053 /// template template parameter.
11054 bool
11055 is_template_decl(const shared_ptr<decl_base> decl)
11056 {return decl && dynamic_pointer_cast<template_decl>(decl);}
11057 
11058 /// This enum describe the kind of entity to lookup, while using the
11059 /// lookup API.
11061 {
11062  LOOKUP_ENTITY_TYPE,
11063  LOOKUP_ENTITY_VAR,
11064 };
11065 
11066 /// Find the first relevant delimiter (the "::" string) in a fully
11067 /// qualified C++ type name, starting from a given position. The
11068 /// delimiter returned separates a type name from the name of its
11069 /// context.
11070 ///
11071 /// This is supposed to work correctly on names in cases like this:
11072 ///
11073 /// foo<ns1::name1, ns2::name2>
11074 ///
11075 /// In that case when called with with parameter @p begin set to 0, no
11076 /// delimiter is returned, because the type name in this case is:
11077 /// 'foo<ns1::name1, ns2::name2>'.
11078 ///
11079 /// But in this case:
11080 ///
11081 /// foo<p1, bar::name>::some_type
11082 ///
11083 /// The "::" returned is the one right before 'some_type'.
11084 ///
11085 /// @param fqn the fully qualified name of the type to consider.
11086 ///
11087 /// @param begin the position from which to look for the delimiter.
11088 ///
11089 /// @param delim_pos out parameter. Is set to the position of the
11090 /// delimiter iff the function returned true.
11091 ///
11092 /// @return true iff the function found and returned the delimiter.
11093 static bool
11094 find_next_delim_in_cplus_type(const string& fqn,
11095  size_t begin,
11096  size_t& delim_pos)
11097 {
11098  int angle_count = 0;
11099  bool found = false;
11100  size_t i = begin;
11101  for (; i < fqn.size(); ++i)
11102  {
11103  if (fqn[i] == '<')
11104  ++angle_count;
11105  else if (fqn[i] == '>')
11106  --angle_count;
11107  else if (i + 1 < fqn.size()
11108  && !angle_count
11109  && fqn[i] == ':'
11110  && fqn[i+1] == ':')
11111  {
11112  delim_pos = i;
11113  found = true;
11114  break;
11115  }
11116  }
11117  return found;
11118 }
11119 
11120 /// Decompose a fully qualified name into the list of its components.
11121 ///
11122 /// @param fqn the fully qualified name to decompose.
11123 ///
11124 /// @param comps the resulting list of component to fill.
11125 void
11126 fqn_to_components(const string& fqn,
11127  list<string>& comps)
11128 {
11129  string::size_type fqn_size = fqn.size(), comp_begin = 0, comp_end = fqn_size;
11130  do
11131  {
11132  if (!find_next_delim_in_cplus_type(fqn, comp_begin, comp_end))
11133  comp_end = fqn_size;
11134 
11135  string comp = fqn.substr(comp_begin, comp_end - comp_begin);
11136  comps.push_back(comp);
11137 
11138  comp_begin = comp_end + 2;
11139  if (comp_begin >= fqn_size)
11140  break;
11141  } while (true);
11142 }
11143 
11144 /// Turn a set of qualified name components (that name a type) into a
11145 /// qualified name string.
11146 ///
11147 /// @param comps the name components
11148 ///
11149 /// @return the resulting string, which would be the qualified name of
11150 /// a type.
11151 string
11152 components_to_type_name(const list<string>& comps)
11153 {
11154  string result;
11155  for (list<string>::const_iterator c = comps.begin();
11156  c != comps.end();
11157  ++c)
11158  if (c == comps.begin())
11159  result = *c;
11160  else
11161  result += "::" + *c;
11162  return result;
11163 }
11164 
11165 /// This predicate returns true if a given container iterator points
11166 /// to the last element of the container, false otherwise.
11167 ///
11168 /// @tparam T the type of the container of the iterator.
11169 ///
11170 /// @param container the container the iterator points into.
11171 ///
11172 /// @param i the iterator to consider.
11173 ///
11174 /// @return true iff the iterator points to the last element of @p
11175 /// container.
11176 template<typename T>
11177 static bool
11178 iterator_is_last(T& container,
11179  typename T::const_iterator i)
11180 {
11181  typename T::const_iterator next = i;
11182  ++next;
11183  return (next == container.end());
11184 }
11185 
11186 //--------------------------------
11187 // <type and decls lookup stuff>
11188 // ------------------------------
11189 
11190 /// Lookup all the type*s* that have a given fully qualified name.
11191 ///
11192 /// @param type_name the fully qualified name of the type to
11193 /// lookup.
11194 ///
11195 /// @param type_map the map to look into.
11196 ///
11197 /// @return the vector containing the types named @p type_name. If
11198 /// the lookup didn't yield any type, then this function returns nil.
11199 static const type_base_wptrs_type*
11200 lookup_types_in_map(const interned_string& type_name,
11201  const istring_type_base_wptrs_map_type& type_map)
11202 {
11203  istring_type_base_wptrs_map_type::const_iterator i = type_map.find(type_name);
11204  if (i != type_map.end())
11205  return &i->second;
11206  return 0;
11207 }
11208 
11209 /// Lookup a type (with a given name) in a map that associates a type
11210 /// name to a type. If there are several types with a given name,
11211 /// then try to return the first one that is not decl-only.
11212 /// Otherwise, return the last of such types, that is, the last one
11213 /// that got registered.
11214 ///
11215 /// @tparam TypeKind the type of the type this function is supposed to
11216 /// return.
11217 ///
11218 /// @param type_name the name of the type to lookup.
11219 ///
11220 /// @param type_map the map in which to look.
11221 ///
11222 /// @return a shared_ptr to the type found. If no type was found or
11223 /// if the type found was not of type @p TypeKind then the function
11224 /// returns nil.
11225 template <class TypeKind>
11226 static shared_ptr<TypeKind>
11227 lookup_type_in_map(const interned_string& type_name,
11228  const istring_type_base_wptrs_map_type& type_map)
11229 {
11230  istring_type_base_wptrs_map_type::const_iterator i = type_map.find(type_name);
11231  if (i != type_map.end())
11232  {
11233  // Walk the types that have the name "type_name" and return the
11234  // first one that is not declaration-only ...
11235  for (auto j : i->second)
11236  {
11237  type_base_sptr t(j);
11238  decl_base_sptr d = is_decl(t);
11239  if (d && !d->get_is_declaration_only())
11240  return dynamic_pointer_cast<TypeKind>(type_base_sptr(j));
11241  }
11242  // ... or return the last type with the name "type_name" that
11243  // was recorded. It's likely to be declaration-only if we
11244  // reached this point.
11245  return dynamic_pointer_cast<TypeKind>(type_base_sptr(i->second.back()));
11246  }
11247  return shared_ptr<TypeKind>();
11248 }
11249 
11250 /// Lookup a basic type from a translation unit.
11251 ///
11252 /// This is done by looking the type up in the type map that is
11253 /// maintained in the translation unit. So this is as fast as
11254 /// possible.
11255 ///
11256 /// @param type_name the name of the basic type to look for.
11257 ///
11258 /// @param tu the translation unit to look into.
11259 ///
11260 /// @return the basic type found or nil if no basic type was found.
11263 {
11264  return lookup_type_in_map<type_decl>(type_name,
11265  tu.get_types().basic_types());
11266 }
11267 
11268 /// Lookup a basic type from a translation unit.
11269 ///
11270 /// This is done by looking the type up in the type map that is
11271 /// maintained in the translation unit. So this is as fast as
11272 /// possible.
11273 ///
11274 /// @param type_name the name of the basic type to look for.
11275 ///
11276 /// @param tu the translation unit to look into.
11277 ///
11278 /// @return the basic type found or nil if no basic type was found.
11280 lookup_basic_type(const string& type_name, const translation_unit& tu)
11281 {
11282  const environment& env = tu.get_environment();
11283 
11284  interned_string s = env.intern(type_name);
11285  return lookup_basic_type(s, tu);
11286 }
11287 
11288 /// Lookup a class type from a translation unit.
11289 ///
11290 /// This is done by looking the type up in the type map that is
11291 /// maintained in the translation unit. So this is as fast as
11292 /// possible.
11293 ///
11294 /// @param fqn the fully qualified name of the class type node to look
11295 /// up.
11296 ///
11297 /// @param tu the translation unit to perform lookup from.
11298 ///
11299 /// @return the declaration of the class type IR node found, NULL
11300 /// otherwise.
11302 lookup_class_type(const string& fqn, const translation_unit& tu)
11303 {
11304  const environment& env = tu.get_environment();
11305  interned_string s = env.intern(fqn);
11306  return lookup_class_type(s, tu);
11307 }
11308 
11309 /// Lookup a class type from a translation unit.
11310 ///
11311 /// This is done by looking the type up in the type map that is
11312 /// maintained in the translation unit. So this is as fast as
11313 /// possible.
11314 ///
11315 /// @param type_name the name of the class type to look for.
11316 ///
11317 /// @param tu the translation unit to look into.
11318 ///
11319 /// @return the class type found or nil if no class type was found.
11322 {
11323  return lookup_type_in_map<class_decl>(type_name,
11324  tu.get_types().class_types());
11325 }
11326 
11327 /// Lookup a union type from a translation unit.
11328 ///
11329 /// This is done by looking the type up in the type map that is
11330 /// maintained in the translation unit. So this is as fast as
11331 /// possible.
11332 ///
11333 /// @param type_name the name of the union type to look for.
11334 ///
11335 /// @param tu the translation unit to look into.
11336 ///
11337 /// @return the union type found or nil if no union type was found.
11338 union_decl_sptr
11340 {
11341  return lookup_type_in_map<union_decl>(type_name,
11342  tu.get_types().union_types());
11343 }
11344 
11345 /// Lookup a union type from a translation unit.
11346 ///
11347 /// This is done by looking the type up in the type map that is
11348 /// maintained in the translation unit. So this is as fast as
11349 /// possible.
11350 ///
11351 /// @param fqn the fully qualified name of the type to lookup.
11352 ///
11353 /// @param tu the translation unit to look into.
11354 ///
11355 /// @return the union type found or nil if no union type was found.
11356 union_decl_sptr
11357 lookup_union_type(const string& fqn, const translation_unit& tu)
11358 {
11359  const environment& env = tu.get_environment();
11360  interned_string s = env.intern(fqn);
11361  return lookup_union_type(s, tu);
11362 }
11363 
11364 /// Lookup a union type in a given corpus, from its location.
11365 ///
11366 /// @param loc the location of the union type to look for.
11367 ///
11368 /// @param corp the corpus to look it from.
11369 ///
11370 /// @return the resulting union_decl.
11371 union_decl_sptr
11373 {
11376  union_decl_sptr result = lookup_type_in_map<union_decl>(loc, m);
11377 
11378  return result;
11379 }
11380 
11381 /// Lookup a union type in a given corpus, from its location.
11382 ///
11383 /// @param loc the location of the union type to look for.
11384 ///
11385 /// @param corp the corpus to look it from.
11386 ///
11387 /// @return the resulting union_decl.
11388 union_decl_sptr
11389 lookup_union_type_per_location(const string& loc, const corpus& corp)
11390 {
11391  const environment& env = corp.get_environment();
11392  return lookup_union_type_per_location(env.intern(loc), corp);
11393 }
11394 
11395 /// Lookup an enum type from a translation unit.
11396 ///
11397 /// This is done by looking the type up in the type map that is
11398 /// maintained in the translation unit. So this is as fast as
11399 /// possible.
11400 ///
11401 /// @param type_name the name of the enum type to look for.
11402 ///
11403 /// @param tu the translation unit to look into.
11404 ///
11405 /// @return the enum type found or nil if no enum type was found.
11408 {
11409  return lookup_type_in_map<enum_type_decl>(type_name,
11410  tu.get_types().enum_types());
11411 }
11412 
11413 /// Lookup an enum type from a translation unit.
11414 ///
11415 /// This is done by looking the type up in the type map that is
11416 /// maintained in the translation unit. So this is as fast as
11417 /// possible.
11418 ///
11419 /// @param type_name the name of the enum type to look for.
11420 ///
11421 /// @param tu the translation unit to look into.
11422 ///
11423 /// @return the enum type found or nil if no enum type was found.
11425 lookup_enum_type(const string& type_name, const translation_unit& tu)
11426 {
11427  const environment& env = tu.get_environment();
11428  interned_string s = env.intern(type_name);
11429  return lookup_enum_type(s, tu);
11430 }
11431 
11432 /// Lookup a typedef type from a translation unit.
11433 ///
11434 /// This is done by looking the type up in the type map that is
11435 /// maintained in the translation unit. So this is as fast as
11436 /// possible.
11437 ///
11438 /// @param type_name the name of the typedef type to look for.
11439 ///
11440 /// @param tu the translation unit to look into.
11441 ///
11442 /// @return the typedef type found or nil if no typedef type was
11443 /// found.
11446  const translation_unit& tu)
11447 {
11448  return lookup_type_in_map<typedef_decl>(type_name,
11449  tu.get_types().typedef_types());
11450 }
11451 
11452 /// Lookup a typedef type from a translation unit.
11453 ///
11454 /// This is done by looking the type up in the type map that is
11455 /// maintained in the translation unit. So this is as fast as
11456 /// possible.
11457 ///
11458 /// @param type_name the name of the typedef type to look for.
11459 ///
11460 /// @param tu the translation unit to look into.
11461 ///
11462 /// @return the typedef type found or nil if no typedef type was
11463 /// found.
11465 lookup_typedef_type(const string& type_name, const translation_unit& tu)
11466 {
11467  const environment& env = tu.get_environment();
11468  interned_string s = env.intern(type_name);
11469  return lookup_typedef_type(s, tu);
11470 }
11471 
11472 /// Lookup a qualified type from a translation unit.
11473 ///
11474 /// This is done by looking the type up in the type map that is
11475 /// maintained in the translation unit. So this is as fast as
11476 /// possible.
11477 ///
11478 /// @param type_name the name of the qualified type to look for.
11479 ///
11480 /// @param tu the translation unit to look into.
11481 ///
11482 /// @return the qualified type found or nil if no qualified type was
11483 /// found.
11484 qualified_type_def_sptr
11486  const translation_unit& tu)
11487 {
11488  const type_maps& m = tu.get_types();
11489  return lookup_type_in_map<qualified_type_def>(type_name,
11490  m.qualified_types());
11491 }
11492 
11493 /// Lookup a qualified type from a translation unit.
11494 ///
11495 /// This is done by looking the type up in the type map that is
11496 /// maintained in the translation unit. So this is as fast as
11497 /// possible.
11498 ///
11499 /// @param underlying_type the underying type of the qualified type to
11500 /// look up.
11501 ///
11502 /// @param quals the CV-qualifiers of the qualified type to look for.
11503 ///
11504 /// @param tu the translation unit to look into.
11505 ///
11506 /// @return the qualified type found or nil if no qualified type was
11507 /// found.
11508 qualified_type_def_sptr
11509 lookup_qualified_type(const type_base_sptr& underlying_type,
11510  qualified_type_def::CV quals,
11511  const translation_unit& tu)
11512 {
11513  interned_string type_name = get_name_of_qualified_type(underlying_type,
11514  quals);
11515  return lookup_qualified_type(type_name, tu);
11516 }
11517 
11518 /// Lookup a pointer type from a translation unit.
11519 ///
11520 /// This is done by looking the type up in the type map that is
11521 /// maintained in the translation unit. So this is as fast as
11522 /// possible.
11523 ///
11524 /// @param type_name the name of the pointer type to look for.
11525 ///
11526 /// @param tu the translation unit to look into.
11527 ///
11528 /// @return the pointer type found or nil if no pointer type was
11529 /// found.
11532  const translation_unit& tu)
11533 {
11534  const type_maps& m = tu.get_types();
11535  return lookup_type_in_map<pointer_type_def>(type_name,
11536  m.pointer_types());
11537 }
11538 
11539 /// Lookup a pointer type from a translation unit.
11540 ///
11541 /// This is done by looking the type up in the type map that is
11542 /// maintained in the translation unit. So this is as fast as
11543 /// possible.
11544 ///
11545 /// @param type_name the name of the pointer type to look for.
11546 ///
11547 /// @param tu the translation unit to look into.
11548 ///
11549 /// @return the pointer type found or nil if no pointer type was
11550 /// found.
11552 lookup_pointer_type(const string& type_name, const translation_unit& tu)
11553 {
11554  const environment& env = tu.get_environment();
11555  interned_string s = env.intern(type_name);
11556  return lookup_pointer_type(s, tu);
11557 }
11558 
11559 /// Lookup a pointer type from a translation unit.
11560 ///
11561 /// This is done by looking the type up in the type map that is
11562 /// maintained in the translation unit. So this is as fast as
11563 /// possible.
11564 ///
11565 /// @param pointed_to_type the pointed-to-type of the pointer to look for.
11566 ///
11567 /// @param tu the translation unit to look into.
11568 ///
11569 /// @return the pointer type found or nil if no pointer type was
11570 /// found.
11572 lookup_pointer_type(const type_base_sptr& pointed_to_type,
11573  const translation_unit& tu)
11574 {
11575  type_base_sptr t = look_through_decl_only(pointed_to_type);
11577  return lookup_pointer_type(type_name, tu);
11578 }
11579 
11580 /// Lookup a reference type from a translation unit.
11581 ///
11582 /// This is done by looking the type up in the type map that is
11583 /// maintained in the translation unit. So this is as fast as
11584 /// possible.
11585 ///
11586 /// @param type_name the name of the reference type to look for.
11587 ///
11588 /// @param tu the translation unit to look into.
11589 ///
11590 /// @return the reference type found or nil if no reference type was
11591 /// found.
11594  const translation_unit& tu)
11595 {
11596  const type_maps& m = tu.get_types();
11597  return lookup_type_in_map<reference_type_def>(type_name,
11598  m.reference_types());
11599 }
11600 
11601 /// Lookup a reference type from a translation unit.
11602 ///
11603 /// This is done by looking the type up in the type map that is
11604 /// maintained in the translation unit. So this is as fast as
11605 /// possible.
11606 ///
11607 /// @param pointed_to_type the pointed-to-type of the reference to
11608 /// look up.
11609 ///
11610 /// @param tu the translation unit to look into.
11611 ///
11612 /// @return the reference type found or nil if no reference type was
11613 /// found.
11615 lookup_reference_type(const type_base_sptr& pointed_to_type,
11616  bool lvalue_reference,
11617  const translation_unit& tu)
11618 {
11619  interned_string type_name =
11621  lvalue_reference);
11622  return lookup_reference_type(type_name, tu);
11623 }
11624 
11625 /// Lookup an array type from a translation unit.
11626 ///
11627 /// This is done by looking the type up in the type map that is
11628 /// maintained in the translation unit. So this is as fast as
11629 /// possible.
11630 ///
11631 /// @param type_name the name of the array type to look for.
11632 ///
11633 /// @param tu the translation unit to look into.
11634 ///
11635 /// @return the array type found or nil if no array type was found.
11638  const translation_unit& tu)
11639 {
11640  const type_maps& m = tu.get_types();
11641  return lookup_type_in_map<array_type_def>(type_name,
11642  m.array_types());
11643 }
11644 
11645 /// Lookup a function type from a translation unit.
11646 ///
11647 /// This is done by looking the type up in the type map that is
11648 /// maintained in the translation unit. So this is as fast as
11649 /// possible.
11650 ///
11651 /// @param type_name the name of the type to lookup.
11652 ///
11653 /// @param tu the translation unit to look into.
11654 ///
11655 /// @return the function type found, or NULL of none was found.
11658  const translation_unit& tu)
11659 {
11660  const type_maps& m = tu.get_types();
11661  return lookup_type_in_map<function_type>(type_name,
11662  m.function_types());
11663 }
11664 
11665 /// Lookup a function type from a translation unit.
11666 ///
11667 /// This walks all the function types held by the translation unit and
11668 /// compare their sub-type *names*. If the names match then return
11669 /// the function type found in the translation unit.
11670 ///
11671 /// @param t the function type to look for.
11672 ///
11673 /// @param tu the translation unit to look into.
11674 ///
11675 /// @return the function type found, or NULL of none was found.
11678  const translation_unit& tu)
11679 {
11680  interned_string type_name = get_type_name(t);
11681  return lookup_function_type(type_name, tu);
11682 }
11683 
11684 /// Lookup a function type from a translation unit.
11685 ///
11686 /// This is done by looking the type up in the type map that is
11687 /// maintained in the translation unit. So this is as fast as
11688 /// possible.
11689 ///
11690 /// @param t the function type to look for.
11691 ///
11692 /// @param tu the translation unit to look into.
11693 ///
11694 /// @return the function type found, or NULL of none was found.
11697  const translation_unit& tu)
11698 {return lookup_function_type(*t, tu);}
11699 
11700 /// Lookup a type in a translation unit.
11701 ///
11702 /// @param fqn the fully qualified name of the type to lookup.
11703 ///
11704 /// @param tu the translation unit to consider.
11705 ///
11706 /// @return the declaration of the type if found, NULL otherwise.
11707 const type_base_sptr
11709  const translation_unit& tu)
11710 {
11711  type_base_sptr result;
11712  ((result = lookup_typedef_type(fqn, tu))
11713  || (result = lookup_class_type(fqn, tu))
11714  || (result = lookup_union_type(fqn, tu))
11715  || (result = lookup_enum_type(fqn, tu))
11716  || (result = lookup_qualified_type(fqn, tu))
11717  || (result = lookup_pointer_type(fqn, tu))
11718  || (result = lookup_reference_type(fqn, tu))
11719  || (result = lookup_array_type(fqn, tu))
11720  || (result = lookup_function_type(fqn, tu))
11721  || (result = lookup_basic_type(fqn, tu)));
11722 
11723  return result;
11724 }
11725 
11726 /// Lookup a type in a translation unit, starting from the global
11727 /// namespace.
11728 ///
11729 /// @param fqn the fully qualified name of the type to lookup.
11730 ///
11731 /// @param tu the translation unit to consider.
11732 ///
11733 /// @return the declaration of the type if found, NULL otherwise.
11734 type_base_sptr
11735 lookup_type(const string& fqn, const translation_unit& tu)
11736 {
11737  const environment&env = tu.get_environment();
11738  interned_string ifqn = env.intern(fqn);
11739  return lookup_type(ifqn, tu);
11740 }
11741 
11742 /// Lookup a type from a translation unit.
11743 ///
11744 /// @param fqn the components of the fully qualified name of the node
11745 /// to look up.
11746 ///
11747 /// @param tu the translation unit to perform lookup from.
11748 ///
11749 /// @return the declaration of the IR node found, NULL otherwise.
11750 const type_base_sptr
11751 lookup_type(const type_base_sptr type,
11752  const translation_unit& tu)
11753 {
11754  interned_string type_name = get_type_name(type);
11755  return lookup_type(type_name, tu);
11756 }
11757 
11758 /// Lookup a type in a scope.
11759 ///
11760 /// This is really slow as it walks the member types of the scope in
11761 /// sequence to find the type with a given name.
11762 ///
11763 /// If possible, users should prefer looking up types from the
11764 /// enclosing translation unit or even ABI corpus because both the
11765 /// translation unit and the corpus have a map of type, indexed by
11766 /// their name. Looking up a type from those maps is thus much
11767 /// faster.
11768 ///
11769 /// @param fqn the fully qualified name of the type to lookup.
11770 ///
11771 /// @param skope the scope to look into.
11772 ///
11773 /// @return the declaration of the type if found, NULL otherwise.
11774 const type_base_sptr
11775 lookup_type_in_scope(const string& fqn,
11776  const scope_decl_sptr& skope)
11777 {
11778  list<string> comps;
11779  fqn_to_components(fqn, comps);
11780  return lookup_type_in_scope(comps, skope);
11781 }
11782 
11783 /// Lookup a @ref var_decl in a scope.
11784 ///
11785 /// @param fqn the fuly qualified name of the @var_decl to lookup.
11786 ///
11787 /// @param skope the scope to look into.
11788 ///
11789 /// @return the declaration of the @ref var_decl if found, NULL
11790 /// otherwise.
11791 const decl_base_sptr
11792 lookup_var_decl_in_scope(const string& fqn,
11793  const scope_decl_sptr& skope)
11794 {
11795  list<string> comps;
11796  fqn_to_components(fqn, comps);
11797  return lookup_var_decl_in_scope(comps, skope);
11798 }
11799 
11800 /// A generic function (template) to get the name of a node, whatever
11801 /// node it is. This has to be specialized for the kind of node we
11802 /// want.
11803 ///
11804 /// Note that a node is a member of a scope.
11805 ///
11806 /// @tparam NodeKind the kind of node to consider.
11807 ///
11808 /// @param node the node to get the name from.
11809 ///
11810 /// @return the name of the node.
11811 template<typename NodeKind>
11812 static const interned_string&
11813 get_node_name(shared_ptr<NodeKind> node);
11814 
11815 /// Gets the name of a class_decl node.
11816 ///
11817 /// @param node the decl_base node to get the name from.
11818 ///
11819 /// @return the name of the node.
11820 template<>
11821 const interned_string&
11823 {return node->get_name();}
11824 
11825 /// Gets the name of a type_base node.
11826 ///
11827 /// @param node the type_base node to get the name from.
11828 ///
11829 /// @return the name of the node.
11830 template<>
11831 const interned_string&
11832 get_node_name(type_base_sptr node)
11833 {return get_type_declaration(node)->get_name();}
11834 
11835 /// Gets the name of a var_decl node.
11836 ///
11837 /// @param node the var_decl node to get the name from.
11838 ///
11839 /// @return the name of the node.
11840 template<>
11841 const interned_string&
11843 {return node->get_name();}
11844 
11845 /// Generic function to get the declaration of a given node, whatever
11846 /// it is. There has to be specializations for the kind of the nodes
11847 /// we want to support.
11848 ///
11849 /// @tparam NodeKind the type of the node we are looking at.
11850 ///
11851 /// @return the declaration.
11852 template<typename NodeKind>
11853 static decl_base_sptr
11854 convert_node_to_decl(shared_ptr<NodeKind> node);
11855 
11856 /// Lookup a node in a given scope.
11857 ///
11858 /// @tparam the type of the node to lookup.
11859 ///
11860 /// @param fqn the components of the fully qualified name of the node
11861 /// to lookup.
11862 ///
11863 /// @param skope the scope to look into.
11864 ///
11865 /// @return the declaration of the looked up node, or NULL if it
11866 /// wasn't found.
11867 template<typename NodeKind>
11868 static const type_or_decl_base_sptr
11869 lookup_node_in_scope(const list<string>& fqn,
11870  const scope_decl_sptr& skope)
11871 {
11872  type_or_decl_base_sptr resulting_decl;
11873  shared_ptr<NodeKind> node;
11874  bool it_is_last = false;
11875  scope_decl_sptr cur_scope = skope, new_scope, scope;
11876 
11877  for (list<string>::const_iterator c = fqn.begin(); c != fqn.end(); ++c)
11878  {
11879  new_scope.reset();
11880  it_is_last = iterator_is_last(fqn, c);
11881  for (scope_decl::declarations::const_iterator m =
11882  cur_scope->get_member_decls().begin();
11883  m != cur_scope->get_member_decls().end();
11884  ++m)
11885  {
11886  if (!it_is_last)
11887  {
11888  // looking for a scope
11889  scope = dynamic_pointer_cast<scope_decl>(*m);
11890  if (scope && scope->get_name() == *c)
11891  {
11892  new_scope = scope;
11893  break;
11894  }
11895  }
11896  else
11897  {
11898  //looking for a final type.
11899  node = dynamic_pointer_cast<NodeKind>(*m);
11900  if (node && get_node_name(node) == *c)
11901  {
11902  if (class_decl_sptr cl =
11903  dynamic_pointer_cast<class_decl>(node))
11904  if (cl->get_is_declaration_only()
11905  && !cl->get_definition_of_declaration())
11906  continue;
11907  resulting_decl = node;
11908  break;
11909  }
11910  }
11911  }
11912  if (!new_scope && !resulting_decl)
11913  return decl_base_sptr();
11914  cur_scope = new_scope;
11915  }
11916  ABG_ASSERT(resulting_decl);
11917  return resulting_decl;
11918 }
11919 
11920 /// lookup a type in a scope.
11921 ///
11922 ///
11923 /// This is really slow as it walks the member types of the scope in
11924 /// sequence to find the type with a given name.
11925 ///
11926 /// If possible, users should prefer looking up types from the
11927 /// enclosing translation unit or even ABI corpus because both the
11928 /// translation unit and the corpus have a map of type, indexed by
11929 /// their name. Looking up a type from those maps is thus much
11930 /// faster.
11931 ///
11932 /// @param comps the components of the fully qualified name of the
11933 /// type to lookup.
11934 ///
11935 /// @param skope the scope to look into.
11936 ///
11937 /// @return the declaration of the type found.
11938 const type_base_sptr
11939 lookup_type_in_scope(const list<string>& comps,
11940  const scope_decl_sptr& scope)
11941 {return is_type(lookup_node_in_scope<type_base>(comps, scope));}
11942 
11943 /// lookup a type in a scope.
11944 ///
11945 /// This is really slow as it walks the member types of the scope in
11946 /// sequence to find the type with a given name.
11947 ///
11948 /// If possible, users should prefer looking up types from the
11949 /// enclosing translation unit or even ABI corpus because both the
11950 /// translation unit and the corpus have a map of type, indexed by
11951 /// their name. Looking up a type from those maps is thus much
11952 /// faster.
11953 ///
11954 /// @param type the type to look for.
11955 ///
11956 /// @param access_path a vector of scopes the path of scopes to follow
11957 /// before reaching the scope into which to look for @p type. Note
11958 /// that the deepest scope (the one immediately containing @p type) is
11959 /// at index 0 of this vector, and the top-most scope is the last
11960 /// element of the vector.
11961 ///
11962 /// @param scope the top-most scope into which to look for @p type.
11963 ///
11964 /// @return the scope found in @p scope, or NULL if it wasn't found.
11965 static const type_base_sptr
11966 lookup_type_in_scope(const type_base& type,
11967  const vector<scope_decl*>& access_path,
11968  const scope_decl* scope)
11969 {
11970  vector<scope_decl*> a = access_path;
11971  type_base_sptr result;
11972 
11973  scope_decl* first_scope = 0;
11974  if (!a.empty())
11975  {
11976  first_scope = a.back();
11977  ABG_ASSERT(first_scope->get_name() == scope->get_name());
11978  a.pop_back();
11979  }
11980 
11981  if (a.empty())
11982  {
11983  interned_string n = get_type_name(type, false);
11984  for (scope_decl::declarations::const_iterator i =
11985  scope->get_member_decls().begin();
11986  i != scope->get_member_decls().end();
11987  ++i)
11988  if (is_type(*i) && (*i)->get_name() == n)
11989  {
11990  result = is_type(*i);
11991  break;
11992  }
11993  }
11994  else
11995  {
11996  first_scope = a.back();
11997  interned_string scope_name, cur_scope_name = first_scope->get_name();
11998  for (scope_decl::scopes::const_iterator i =
11999  scope->get_member_scopes().begin();
12000  i != scope->get_member_scopes().end();
12001  ++i)
12002  {
12003  scope_name = (*i)->get_name();
12004  if (scope_name == cur_scope_name)
12005  {
12006  result = lookup_type_in_scope(type, a, (*i).get());
12007  break;
12008  }
12009  }
12010  }
12011  return result;
12012 }
12013 
12014 /// lookup a type in a scope.
12015 ///
12016 /// This is really slow as it walks the member types of the scope in
12017 /// sequence to find the type with a given name.
12018 ///
12019 /// If possible, users should prefer looking up types from the
12020 /// enclosing translation unit or even ABI corpus because both the
12021 /// translation unit and the corpus have a map of type, indexed by
12022 /// their name. Looking up a type from those maps is thus much
12023 /// faster.
12024 ///
12025 /// @param type the type to look for.
12026 ///
12027 /// @param scope the top-most scope into which to look for @p type.
12028 ///
12029 /// @return the scope found in @p scope, or NULL if it wasn't found.
12030 static const type_base_sptr
12031 lookup_type_in_scope(const type_base_sptr type,
12032  const scope_decl* scope)
12033 {
12034  if (!type || is_function_type(type))
12035  return type_base_sptr();
12036 
12037  decl_base_sptr type_decl = get_type_declaration(type);
12038  ABG_ASSERT(type_decl);
12039  vector<scope_decl*> access_path;
12040  for (scope_decl* s = type_decl->get_scope(); s != 0; s = s->get_scope())
12041  {
12042  access_path.push_back(s);
12043  if (is_global_scope(s))
12044  break;
12045  }
12046  return lookup_type_in_scope(*type, access_path, scope);
12047 }
12048 
12049 /// Lookup a type from a translation unit by walking the scopes of the
12050 /// translation unit in sequence and looking into them.
12051 ///
12052 /// This is really slow as it walks the member types of the scopes in
12053 /// sequence to find the type with a given name.
12054 ///
12055 /// If possible, users should prefer looking up types from the
12056 /// translation unit or even ABI corpus in a more direct way, by using
12057 /// the lookup_type() functins.
12058 ///
12059 ///
12060 /// This is because both the translation unit and the corpus have a
12061 /// map of types, indexed by their name. Looking up a type from those
12062 /// maps is thus much faster. @param fqn the components of the fully
12063 /// qualified name of the node to look up.
12064 ///
12065 /// @param tu the translation unit to perform lookup from.
12066 ///
12067 /// @return the declaration of the IR node found, NULL otherwise.
12068 const type_base_sptr
12069 lookup_type_through_scopes(const type_base_sptr type,
12070  const translation_unit& tu)
12071 {
12072  if (function_type_sptr fn_type = is_function_type(type))
12073  return lookup_function_type(fn_type, tu);
12074  return lookup_type_in_scope(type, tu.get_global_scope().get());
12075 }
12076 
12077 /// lookup a var_decl in a scope.
12078 ///
12079 /// @param comps the components of the fully qualified name of the
12080 /// var_decl to lookup.
12081 ///
12082 /// @param skope the scope to look into.
12083 const decl_base_sptr
12084 lookup_var_decl_in_scope(const std::list<string>& comps,
12085  const scope_decl_sptr& skope)
12086 {return is_var_decl(lookup_node_in_scope<var_decl>(comps, skope));}
12087 
12088 /// Lookup an IR node from a translation unit.
12089 ///
12090 /// @tparam NodeKind the type of the IR node to lookup from the
12091 /// translation unit.
12092 ///
12093 /// @param fqn the components of the fully qualified name of the node
12094 /// to look up.
12095 ///
12096 /// @param tu the translation unit to perform lookup from.
12097 ///
12098 /// @return the declaration of the IR node found, NULL otherwise.
12099 template<typename NodeKind>
12100 static const type_or_decl_base_sptr
12101 lookup_node_in_translation_unit(const list<string>& fqn,
12102  const translation_unit& tu)
12103 {return lookup_node_in_scope<NodeKind>(fqn, tu.get_global_scope());}
12104 
12105 /// Lookup a type from a translation unit by walking its scopes in
12106 /// sequence and by looking into them.
12107 ///
12108 /// This is much slower than using the lookup_type() function.
12109 ///
12110 /// @param fqn the components of the fully qualified name of the node
12111 /// to look up.
12112 ///
12113 /// @param tu the translation unit to perform lookup from.
12114 ///
12115 /// @return the declaration of the IR node found, NULL otherwise.
12116 type_base_sptr
12117 lookup_type_through_scopes(const list<string>& fqn,
12118  const translation_unit& tu)
12119 {return is_type(lookup_node_in_translation_unit<type_base>(fqn, tu));}
12120 
12121 
12122 /// Lookup a class type from a translation unit by walking its scopes
12123 /// in sequence and by looking into them.
12124 ///
12125 /// This is much slower than using the lookup_class_type() function
12126 /// because it walks all the scopes of the translation unit in
12127 /// sequence and lookup the types to find one that has a given name.
12128 ///
12129 /// @param fqn the components of the fully qualified name of the class
12130 /// type node to look up.
12131 ///
12132 /// @param tu the translation unit to perform lookup from.
12133 ///
12134 /// @return the declaration of the class type IR node found, NULL
12135 /// otherwise.
12137 lookup_class_type_through_scopes(const list<string>& fqn,
12138  const translation_unit& tu)
12139 {return is_class_type(lookup_node_in_translation_unit<class_decl>(fqn, tu));}
12140 
12141 /// Lookup a basic type from all the translation units of a given
12142 /// corpus.
12143 ///
12144 /// @param fqn the components of the fully qualified name of the basic
12145 /// type node to look up.
12146 ///
12147 /// @param tu the translation unit to perform lookup from.
12148 ///
12149 /// @return the declaration of the basic type IR node found, NULL
12150 /// otherwise.
12151 static type_decl_sptr
12152 lookup_basic_type_through_translation_units(const interned_string& type_name,
12153  const corpus& abi_corpus)
12154 {
12155  type_decl_sptr result;
12156 
12157  for (translation_units::const_iterator tu =
12158  abi_corpus.get_translation_units().begin();
12159  tu != abi_corpus.get_translation_units().end();
12160  ++tu)
12161  if ((result = lookup_basic_type(type_name, **tu)))
12162  break;
12163 
12164  return result;
12165 }
12166 
12167 /// Lookup a union type from all the translation units of a given
12168 /// corpus.
12169 ///
12170 /// @param fqn the components of the fully qualified name of the union
12171 /// type node to look up.
12172 ///
12173 /// @param tu the translation unit to perform lookup from.
12174 ///
12175 /// @return the declaration of the union type IR node found, NULL
12176 /// otherwise.
12177 static union_decl_sptr
12178 lookup_union_type_through_translation_units(const interned_string& type_name,
12179  const corpus & abi_corpus)
12180 {
12181  union_decl_sptr result;
12182 
12183  for (translation_units::const_iterator tu =
12184  abi_corpus.get_translation_units().begin();
12185  tu != abi_corpus.get_translation_units().end();
12186  ++tu)
12187  if ((result = lookup_union_type(type_name, **tu)))
12188  break;
12189 
12190  return result;
12191 }
12192 
12193 /// Lookup an enum type from all the translation units of a given
12194 /// corpus.
12195 ///
12196 /// @param fqn the components of the fully qualified name of the enum
12197 /// type node to look up.
12198 ///
12199 /// @param tu the translation unit to perform lookup from.
12200 ///
12201 /// @return the declaration of the enum type IR node found, NULL
12202 /// otherwise.
12203 static enum_type_decl_sptr
12204 lookup_enum_type_through_translation_units(const interned_string& type_name,
12205  const corpus & abi_corpus)
12206 {
12207  enum_type_decl_sptr result;
12208 
12209  for (translation_units::const_iterator tu =
12210  abi_corpus.get_translation_units().begin();
12211  tu != abi_corpus.get_translation_units().end();
12212  ++tu)
12213  if ((result = lookup_enum_type(type_name, **tu)))
12214  break;
12215 
12216  return result;
12217 }
12218 
12219 /// Lookup a typedef type definition in all the translation units of a
12220 /// given ABI corpus.
12221 ///
12222 /// @param @param qn the fully qualified name of the typedef type to lookup.
12223 ///
12224 /// @param abi_corpus the ABI corpus which to look the type up in.
12225 ///
12226 /// @return the type definition if any was found, or a NULL pointer.
12227 static typedef_decl_sptr
12228 lookup_typedef_type_through_translation_units(const interned_string& type_name,
12229  const corpus & abi_corpus)
12230 {
12231  typedef_decl_sptr result;
12232 
12233  for (translation_units::const_iterator tu =
12234  abi_corpus.get_translation_units().begin();
12235  tu != abi_corpus.get_translation_units().end();
12236  ++tu)
12237  if ((result = lookup_typedef_type(type_name, **tu)))
12238  break;
12239 
12240  return result;
12241 }
12242 
12243 /// Lookup a qualified type definition in all the translation units of a
12244 /// given ABI corpus.
12245 ///
12246 /// @param @param qn the fully qualified name of the qualified type to
12247 /// lookup.
12248 ///
12249 /// @param abi_corpus the ABI corpus which to look the type up in.
12250 ///
12251 /// @return the type definition if any was found, or a NULL pointer.
12252 static qualified_type_def_sptr
12253 lookup_qualified_type_through_translation_units(const interned_string& t_name,
12254  const corpus & abi_corpus)
12255 {
12256  qualified_type_def_sptr result;
12257 
12258  for (translation_units::const_iterator tu =
12259  abi_corpus.get_translation_units().begin();
12260  tu != abi_corpus.get_translation_units().end();
12261  ++tu)
12262  if ((result = lookup_qualified_type(t_name, **tu)))
12263  break;
12264 
12265  return result;
12266 }
12267 
12268 /// Lookup a pointer type definition in all the translation units of a
12269 /// given ABI corpus.
12270 ///
12271 /// @param @param qn the fully qualified name of the pointer type to
12272 /// lookup.
12273 ///
12274 /// @param abi_corpus the ABI corpus which to look the type up in.
12275 ///
12276 /// @return the type definition if any was found, or a NULL pointer.
12277 static pointer_type_def_sptr
12278 lookup_pointer_type_through_translation_units(const interned_string& type_name,
12279  const corpus & abi_corpus)
12280 {
12281  pointer_type_def_sptr result;
12282 
12283  for (translation_units::const_iterator tu =
12284  abi_corpus.get_translation_units().begin();
12285  tu != abi_corpus.get_translation_units().end();
12286  ++tu)
12287  if ((result = lookup_pointer_type(type_name, **tu)))
12288  break;
12289 
12290  return result;
12291 }
12292 
12293 /// Lookup a reference type definition in all the translation units of a
12294 /// given ABI corpus.
12295 ///
12296 /// @param @param qn the fully qualified name of the reference type to
12297 /// lookup.
12298 ///
12299 /// @param abi_corpus the ABI corpus which to look the type up in.
12300 ///
12301 /// @return the type definition if any was found, or a NULL pointer.
12303 lookup_reference_type_through_translation_units(const interned_string& t_name,
12304  const corpus & abi_corpus)
12305 {
12306  reference_type_def_sptr result;
12307 
12308  for (translation_units::const_iterator tu =
12309  abi_corpus.get_translation_units().begin();
12310  tu != abi_corpus.get_translation_units().end();
12311  ++tu)
12312  if ((result = lookup_reference_type(t_name, **tu)))
12313  break;
12314 
12315  return result;
12316 }
12317 
12318 /// Lookup a array type definition in all the translation units of a
12319 /// given ABI corpus.
12320 ///
12321 /// @param @param qn the fully qualified name of the array type to
12322 /// lookup.
12323 ///
12324 /// @param abi_corpus the ABI corpus which to look the type up in.
12325 ///
12326 /// @return the type definition if any was found, or a NULL pointer.
12327 static array_type_def_sptr
12328 lookup_array_type_through_translation_units(const interned_string& type_name,
12329  const corpus & abi_corpus)
12330 {
12331  array_type_def_sptr result;
12332 
12333  for (translation_units::const_iterator tu =
12334  abi_corpus.get_translation_units().begin();
12335  tu != abi_corpus.get_translation_units().end();
12336  ++tu)
12337  if ((result = lookup_array_type(type_name, **tu)))
12338  break;
12339 
12340  return result;
12341 }
12342 
12343 /// Lookup a function type definition in all the translation units of
12344 /// a given ABI corpus.
12345 ///
12346 /// @param @param qn the fully qualified name of the function type to
12347 /// lookup.
12348 ///
12349 /// @param abi_corpus the ABI corpus which to look the type up in.
12350 ///
12351 /// @return the type definition if any was found, or a NULL pointer.
12352 static function_type_sptr
12353 lookup_function_type_through_translation_units(const interned_string& type_name,
12354  const corpus & abi_corpus)
12355 {
12356  function_type_sptr result;
12357 
12358  for (translation_units::const_iterator tu =
12359  abi_corpus.get_translation_units().begin();
12360  tu != abi_corpus.get_translation_units().end();
12361  ++tu)
12362  if ((result = lookup_function_type(type_name, **tu)))
12363  break;
12364 
12365  return result;
12366 }
12367 
12368 /// Lookup a type definition in all the translation units of a given
12369 /// ABI corpus.
12370 ///
12371 /// @param @param qn the fully qualified name of the type to lookup.
12372 ///
12373 /// @param abi_corpus the ABI corpus which to look the type up in.
12374 ///
12375 /// @return the type definition if any was found, or a NULL pointer.
12376 type_base_sptr
12378  const corpus& abi_corpus)
12379 {
12380  type_base_sptr result;
12381 
12382  for (translation_units::const_iterator tu =
12383  abi_corpus.get_translation_units().begin();
12384  tu != abi_corpus.get_translation_units().end();
12385  ++tu)
12386  if ((result = lookup_type(qn, **tu)))
12387  break;
12388 
12389  return result;
12390 }
12391 
12392 /// Lookup a type from a given translation unit present in a give corpus.
12393 ///
12394 /// @param type_name the name of the type to look for.
12395 ///
12396 /// @parm tu_path the path of the translation unit to consider.
12397 ///
12398 /// @param corp the corpus to consider.
12399 ///
12400 /// @return the resulting type, if any.
12401 type_base_sptr
12402 lookup_type_from_translation_unit(const string& type_name,
12403  const string& tu_path,
12404  const corpus& corp)
12405 {
12406  string_tu_map_type::const_iterator i = corp.priv_->path_tu_map.find(tu_path);
12407  if (i == corp.priv_->path_tu_map.end())
12408  return type_base_sptr();
12409 
12410  translation_unit_sptr tu = i->second;
12411  ABG_ASSERT(tu);
12412 
12413  type_base_sptr t = lookup_type(type_name, *tu);
12414  return t;
12415 }
12416 
12417 /// Look into an ABI corpus for a function type.
12418 ///
12419 /// @param fn_type the function type to be looked for in the ABI
12420 /// corpus.
12421 ///
12422 /// @param corpus the ABI corpus into which to look for the function
12423 /// type.
12424 ///
12425 /// @return the function type found in the corpus.
12428  const corpus& corpus)
12429 {
12430  ABG_ASSERT(fn_t);
12431 
12432  function_type_sptr result;
12433 
12434  if ((result = lookup_function_type(fn_t, corpus)))
12435  return result;
12436 
12437  for (translation_units::const_iterator i =
12438  corpus.get_translation_units().begin();
12439  i != corpus.get_translation_units().end();
12440  ++i)
12442  **i)))
12443  return result;
12444 
12445  return result;
12446 }
12447 
12448 /// Look into a given corpus to find a type which has the same
12449 /// qualified name as a giventype.
12450 ///
12451 /// If the per-corpus type map is non-empty (because the corpus allows
12452 /// the One Definition Rule) then the type islooked up in that
12453 /// per-corpus type map. Otherwise, the type is looked-up in each
12454 /// translation unit.
12455 ///
12456 /// @param t the type which has the same qualified name as the type we
12457 /// are looking for.
12458 ///
12459 /// @param corp the ABI corpus to look into for the type.
12461 lookup_basic_type(const type_decl& t, const corpus& corp)
12462 {return lookup_basic_type(t.get_name(), corp);}
12463 
12464 /// Look into a given corpus to find a basic type which has a given
12465 /// qualified name.
12466 ///
12467 /// If the per-corpus type map is non-empty (because the corpus allows
12468 /// the One Definition Rule) then the type islooked up in that
12469 /// per-corpus type map. Otherwise, the type is looked-up in each
12470 /// translation unit.
12471 ///
12472 /// @param qualified_name the qualified name of the basic type to look
12473 /// for.
12474 ///
12475 /// @param corp the corpus to look into.
12477 lookup_basic_type(const interned_string &qualified_name, const corpus& corp)
12478 {
12480  type_decl_sptr result;
12481 
12482  if (!m.empty())
12483  result = lookup_type_in_map<type_decl>(qualified_name, m);
12484  else
12485  result = lookup_basic_type_through_translation_units(qualified_name, corp);
12486 
12487  return result;
12488 }
12489 
12490 /// Lookup a @ref type_decl type from a given corpus, by its location.
12491 ///
12492 /// @param loc the location to consider.
12493 ///
12494 /// @param corp the corpus to consider.
12495 ///
12496 /// @return the resulting basic type, if any.
12499  const corpus &corp)
12500 {
12503  type_decl_sptr result;
12504 
12505  result = lookup_type_in_map<type_decl>(loc, m);
12506 
12507  return result;
12508 }
12509 
12510 /// Lookup a @ref type_decl type from a given corpus, by its location.
12511 ///
12512 /// @param loc the location to consider.
12513 ///
12514 /// @param corp the corpus to consider.
12515 ///
12516 /// @return the resulting basic type, if any.
12518 lookup_basic_type_per_location(const string &loc, const corpus &corp)
12519 {
12520  const environment& env = corp.get_environment();
12521  return lookup_basic_type_per_location(env.intern(loc), corp);
12522 }
12523 
12524 /// Look into a given corpus to find a basic type which has a given
12525 /// qualified name.
12526 ///
12527 /// If the per-corpus type map is non-empty (because the corpus allows
12528 /// the One Definition Rule) then the type islooked up in that
12529 /// per-corpus type map. Otherwise, the type is looked-up in each
12530 /// translation unit.
12531 ///
12532 /// @param qualified_name the qualified name of the basic type to look
12533 /// for.
12534 ///
12535 /// @param corp the corpus to look into.
12537 lookup_basic_type(const string& qualified_name, const corpus& corp)
12538 {
12539  return lookup_basic_type(corp.get_environment().intern(qualified_name),
12540  corp);
12541 }
12542 
12543 /// Look into a given corpus to find a class type which has the same
12544 /// qualified name as a given type.
12545 ///
12546 /// If the per-corpus type map is non-empty (because the corpus allows
12547 /// the One Definition Rule) then the type islooked up in that
12548 /// per-corpus type map. Otherwise, the type is looked-up in each
12549 /// translation unit.
12550 ///
12551 /// @param t the class decl type which has the same qualified name as
12552 /// the type we are looking for.
12553 ///
12554 /// @param corp the corpus to look into.
12556 lookup_class_type(const class_decl& t, const corpus& corp)
12557 {
12559  return lookup_class_type(s, corp);
12560 }
12561 
12562 /// Look into a given corpus to find a class type which has a given
12563 /// qualified name.
12564 ///
12565 /// If the per-corpus type map is non-empty (because the corpus allows
12566 /// the One Definition Rule) then the type islooked up in that
12567 /// per-corpus type map. Otherwise, the type is looked-up in each
12568 /// translation unit.
12569 ///
12570 /// @param qualified_name the qualified name of the type to look for.
12571 ///
12572 /// @param corp the corpus to look into.
12574 lookup_class_type(const string& qualified_name, const corpus& corp)
12575 {
12576  interned_string s = corp.get_environment().intern(qualified_name);
12577  return lookup_class_type(s, corp);
12578 }
12579 
12580 /// Look into a given corpus to find a class type which has a given
12581 /// qualified name.
12582 ///
12583 /// If the per-corpus type map is non-empty (because the corpus allows
12584 /// the One Definition Rule) then the type islooked up in that
12585 /// per-corpus type map. Otherwise, the type is looked-up in each
12586 /// translation unit.
12587 ///
12588 /// @param qualified_name the qualified name of the type to look for.
12589 ///
12590 /// @param corp the corpus to look into.
12592 lookup_class_type(const interned_string& qualified_name, const corpus& corp)
12593 {
12595 
12596  class_decl_sptr result = lookup_type_in_map<class_decl>(qualified_name, m);
12597 
12598  return result;
12599 }
12600 
12601 /// Look into a given corpus to find the class type*s* that have a
12602 /// given qualified name.
12603 ///
12604 /// @param qualified_name the qualified name of the type to look for.
12605 ///
12606 /// @param corp the corpus to look into.
12607 ///
12608 /// @return the vector of class types named @p qualified_name.
12609 const type_base_wptrs_type *
12610 lookup_class_types(const interned_string& qualified_name, const corpus& corp)
12611 {
12613 
12614  return lookup_types_in_map(qualified_name, m);
12615 }
12616 
12617 /// Look into a given corpus to find the class type*s* that have a
12618 /// given qualified name and that are declaration-only.
12619 ///
12620 /// @param qualified_name the qualified name of the type to look for.
12621 ///
12622 /// @param corp the corpus to look into.
12623 ///
12624 /// @param result the vector of decl-only class types named @p
12625 /// qualified_name. This is populated iff the function returns true.
12626 ///
12627 /// @return true iff @p result was populated with the decl-only
12628 /// classes named @p qualified_name.
12629 bool
12631  const corpus& corp,
12632  type_base_wptrs_type& result)
12633 {
12635 
12636  const type_base_wptrs_type *v = lookup_types_in_map(qualified_name, m);
12637  if (!v)
12638  return false;
12639 
12640  for (auto type : *v)
12641  {
12642  type_base_sptr t(type);
12644  if (c->get_is_declaration_only()
12645  && !c->get_definition_of_declaration())
12646  result.push_back(type);
12647  }
12648 
12649  return !result.empty();
12650 }
12651 
12652 /// Look into a given corpus to find the union type*s* that have a
12653 /// given qualified name.
12654 ///
12655 /// @param qualified_name the qualified name of the type to look for.
12656 ///
12657 /// @param corp the corpus to look into.
12658 ///
12659 /// @return the vector of union types named @p qualified_name.
12660 const type_base_wptrs_type *
12661 lookup_union_types(const interned_string& qualified_name, const corpus& corp)
12662 {
12664 
12665  return lookup_types_in_map(qualified_name, m);
12666 }
12667 
12668 /// Look into a given corpus to find the class type*s* that have a
12669 /// given qualified name.
12670 ///
12671 /// @param qualified_name the qualified name of the type to look for.
12672 ///
12673 /// @param corp the corpus to look into.
12674 ///
12675 /// @return the vector of class types which name is @p qualified_name.
12676 const type_base_wptrs_type*
12677 lookup_class_types(const string& qualified_name, const corpus& corp)
12678 {
12679  interned_string s = corp.get_environment().intern(qualified_name);
12680  return lookup_class_types(s, corp);
12681 }
12682 
12683 /// Look into a given corpus to find the union types that have a given
12684 /// qualified name.
12685 ///
12686 /// @param qualified_name the qualified name of the type to look for.
12687 ///
12688 /// @param corp the corpus to look into.
12689 ///
12690 /// @return the vector of union types which name is @p qualified_name.
12691 const type_base_wptrs_type *
12692 lookup_union_types(const string& qualified_name, const corpus& corp)
12693 {
12694  interned_string s = corp.get_environment().intern(qualified_name);
12695  return lookup_union_types(s, corp);
12696 }
12697 
12698 /// Look up a @ref class_decl from a given corpus by its location.
12699 ///
12700 /// @param loc the location to consider.
12701 ///
12702 /// @param corp the corpus to consider.
12703 ///
12704 /// @return the resulting class decl, if any.
12707  const corpus& corp)
12708 {
12711  class_decl_sptr result = lookup_type_in_map<class_decl>(loc, m);
12712 
12713  return result;
12714 }
12715 
12716 /// Look up a @ref class_decl from a given corpus by its location.
12717 ///
12718 /// @param loc the location to consider.
12719 ///
12720 /// @param corp the corpus to consider.
12721 ///
12722 /// @return the resulting class decl, if any.
12724 lookup_class_type_per_location(const string &loc, const corpus &corp)
12725 {
12726  const environment& env = corp.get_environment();
12727  return lookup_class_type_per_location(env.intern(loc), corp);
12728 }
12729 
12730 /// Look into a given corpus to find a union type which has a given
12731 /// qualified name.
12732 ///
12733 /// If the per-corpus type map is non-empty (because the corpus allows
12734 /// the One Definition Rule) then the type islooked up in that
12735 /// per-corpus type map. Otherwise, the type is looked-up in each
12736 /// translation unit.
12737 ///
12738 /// @param qualified_name the qualified name of the type to look for.
12739 ///
12740 /// @param corp the corpus to look into.
12741 union_decl_sptr
12742 lookup_union_type(const interned_string& type_name, const corpus& corp)
12743 {
12745 
12746  union_decl_sptr result = lookup_type_in_map<union_decl>(type_name, m);
12747  if (!result)
12748  result = lookup_union_type_through_translation_units(type_name, corp);
12749 
12750  return result;
12751 }
12752 
12753 /// Look into a given corpus to find a union type which has a given
12754 /// qualified name.
12755 ///
12756 /// If the per-corpus type map is non-empty (because the corpus allows
12757 /// the One Definition Rule) then the type islooked up in that
12758 /// per-corpus type map. Otherwise, the type is looked-up in each
12759 /// translation unit.
12760 ///
12761 /// @param qualified_name the qualified name of the type to look for.
12762 ///
12763 /// @param corp the corpus to look into.
12764 union_decl_sptr
12765 lookup_union_type(const string& type_name, const corpus& corp)
12766 {
12767  interned_string s = corp.get_environment().intern(type_name);
12768  return lookup_union_type(s, corp);
12769 }
12770 
12771 /// Look into a given corpus to find an enum type which has the same
12772 /// qualified name as a given enum type.
12773 ///
12774 /// If the per-corpus type map is non-empty (because the corpus allows
12775 /// the One Definition Rule) then the type islooked up in that
12776 /// per-corpus type map. Otherwise, the type is looked-up in each
12777 /// translation unit.
12778 ///
12779 /// @param t the enum type which has the same qualified name as the
12780 /// type we are looking for.
12781 ///
12782 /// @param corp the corpus to look into.
12785 {
12787  return lookup_enum_type(s, corp);
12788 }
12789 
12790 /// Look into a given corpus to find an enum type which has a given
12791 /// qualified name.
12792 ///
12793 /// If the per-corpus type map is non-empty (because the corpus allows
12794 /// the One Definition Rule) then the type islooked up in that
12795 /// per-corpus type map. Otherwise, the type is looked-up in each
12796 /// translation unit.
12797 ///
12798 /// @param qualified_name the qualified name of the enum type to look
12799 /// for.
12800 ///
12801 /// @param corp the corpus to look into.
12803 lookup_enum_type(const string& qualified_name, const corpus& corp)
12804 {
12805  interned_string s = corp.get_environment().intern(qualified_name);
12806  return lookup_enum_type(s, corp);
12807 }
12808 
12809 /// Look into a given corpus to find an enum type which has a given
12810 /// qualified name.
12811 ///
12812 /// If the per-corpus type map is non-empty (because the corpus allows
12813 /// the One Definition Rule) then the type islooked up in that
12814 /// per-corpus type map. Otherwise, the type is looked-up in each
12815 /// translation unit.
12816 ///
12817 /// @param qualified_name the qualified name of the enum type to look
12818 /// for.
12819 ///
12820 /// @param corp the corpus to look into.
12822 lookup_enum_type(const interned_string& qualified_name, const corpus& corp)
12823 {
12825 
12826  enum_type_decl_sptr result =
12827  lookup_type_in_map<enum_type_decl>(qualified_name, m);
12828  if (!result)
12829  result = lookup_enum_type_through_translation_units(qualified_name, corp);
12830 
12831  return result;
12832 }
12833 
12834 /// Look into a given corpus to find the enum type*s* that have a
12835 /// given qualified name.
12836 ///
12837 /// @param qualified_name the qualified name of the type to look for.
12838 ///
12839 /// @param corp the corpus to look into.
12840 ///
12841 /// @return the vector of enum types that which name is @p qualified_name.
12842 const type_base_wptrs_type *
12843 lookup_enum_types(const interned_string& qualified_name, const corpus& corp)
12844 {
12846 
12847  return lookup_types_in_map(qualified_name, m);
12848 }
12849 
12850 /// Look into a given corpus to find the enum type*s* that have a
12851 /// given qualified name.
12852 ///
12853 /// @param qualified_name the qualified name of the type to look for.
12854 ///
12855 /// @param corp the corpus to look into.
12856 ///
12857 /// @return the vector of enum types that which name is @p qualified_name.
12858 const type_base_wptrs_type*
12859 lookup_enum_types(const string& qualified_name, const corpus& corp)
12860 {
12861  interned_string s = corp.get_environment().intern(qualified_name);
12862  return lookup_enum_types(s, corp);
12863 }
12864 
12865 /// Look up an @ref enum_type_decl from a given corpus, by its location.
12866 ///
12867 /// @param loc the location to consider.
12868 ///
12869 /// @param corp the corpus to look the type from.
12870 ///
12871 /// @return the resulting enum type, if any.
12874 {
12877  enum_type_decl_sptr result = lookup_type_in_map<enum_type_decl>(loc, m);
12878 
12879  return result;
12880 }
12881 
12882 /// Look up an @ref enum_type_decl from a given corpus, by its location.
12883 ///
12884 /// @param loc the location to consider.
12885 ///
12886 /// @param corp the corpus to look the type from.
12887 ///
12888 /// @return the resulting enum type, if any.
12890 lookup_enum_type_per_location(const string &loc, const corpus &corp)
12891 {
12892  const environment& env = corp.get_environment();
12893  return lookup_enum_type_per_location(env.intern(loc), corp);
12894 }
12895 
12896 /// Look into a given corpus to find a typedef type which has the
12897 /// same qualified name as a given typedef type.
12898 ///
12899 /// If the per-corpus type map is non-empty (because the corpus allows
12900 /// the One Definition Rule) then the type islooked up in that
12901 /// per-corpus type map. Otherwise, the type is looked-up in each
12902 /// translation unit.
12903 ///
12904 /// @param t the typedef type which has the same qualified name as the
12905 /// typedef type we are looking for.
12906 ///
12907 /// @param corp the corpus to look into.
12910 {
12912  return lookup_typedef_type(s, corp);
12913 }
12914 
12915 /// Look into a given corpus to find a typedef type which has the
12916 /// same qualified name as a given typedef type.
12917 ///
12918 /// If the per-corpus type map is non-empty (because the corpus allows
12919 /// the One Definition Rule) then the type islooked up in that
12920 /// per-corpus type map. Otherwise, the type is looked-up in each
12921 /// translation unit.
12922 ///
12923 /// @param t the typedef type which has the same qualified name as the
12924 /// typedef type we are looking for.
12925 ///
12926 /// @param corp the corpus to look into.
12928 lookup_typedef_type(const string& qualified_name, const corpus& corp)
12929 {
12930  interned_string s = corp.get_environment().intern(qualified_name);
12931  return lookup_typedef_type(s, corp);
12932 }
12933 
12934 /// Look into a given corpus to find a typedef type which has a
12935 /// given qualified name.
12936 ///
12937 /// If the per-corpus type map is non-empty (because the corpus allows
12938 /// the One Definition Rule) then the type islooked up in that
12939 /// per-corpus type map. Otherwise, the type is looked-up in each
12940 /// translation unit.
12941 ///
12942 /// @param qualified_name the qualified name of the typedef type to
12943 /// look for.
12944 ///
12945 /// @param corp the corpus to look into.
12947 lookup_typedef_type(const interned_string& qualified_name, const corpus& corp)
12948 {
12950 
12951  typedef_decl_sptr result =
12952  lookup_type_in_map<typedef_decl>(qualified_name, m);
12953  if (!result)
12954  result = lookup_typedef_type_through_translation_units(qualified_name,
12955  corp);
12956 
12957  return result;
12958 }
12959 
12960 /// Lookup a @ref typedef_decl from a corpus, by its location.
12961 ///
12962 /// @param loc the location to consider.
12963 ///
12964 /// @param corp the corpus to consider.
12965 ///
12966 /// @return the typedef_decl found, if any.
12969 {
12972  typedef_decl_sptr result = lookup_type_in_map<typedef_decl>(loc, m);
12973 
12974  return result;
12975 }
12976 
12977 /// Lookup a @ref typedef_decl from a corpus, by its location.
12978 ///
12979 /// @param loc the location to consider.
12980 ///
12981 /// @param corp the corpus to consider.
12982 ///
12983 /// @return the typedef_decl found, if any.
12985 lookup_typedef_type_per_location(const string &loc, const corpus &corp)
12986 {
12987  const environment& env = corp.get_environment();
12988  return lookup_typedef_type_per_location(env.intern(loc), corp);
12989 }
12990 
12991 /// Look into a corpus to find a class, union or typedef type which
12992 /// has a given qualified name.
12993 ///
12994 /// If the per-corpus type map is non-empty (because the corpus allows
12995 /// the One Definition Rule) then the type islooked up in that
12996 /// per-corpus type map. Otherwise, the type is looked-up in each
12997 /// translation unit.
12998 ///
12999 /// @param qualified_name the name of the type to find.
13000 ///
13001 /// @param corp the corpus to look into.
13002 ///
13003 /// @return the typedef or class type found.
13004 type_base_sptr
13005 lookup_class_or_typedef_type(const string& qualified_name, const corpus& corp)
13006 {
13007  type_base_sptr result = lookup_class_type(qualified_name, corp);
13008  if (!result)
13009  result = lookup_union_type(qualified_name, corp);
13010 
13011  if (!result)
13012  result = lookup_typedef_type(qualified_name, corp);
13013  return result;
13014 }
13015 
13016 /// Look into a corpus to find a class, typedef or enum type which has
13017 /// a given qualified name.
13018 ///
13019 /// If the per-corpus type map is non-empty (because the corpus allows
13020 /// the One Definition Rule) then the type islooked up in that
13021 /// per-corpus type map. Otherwise, the type is looked-up in each
13022 /// translation unit.
13023 ///
13024 /// @param qualified_name the qualified name of the type to look for.
13025 ///
13026 /// @param corp the corpus to look into.
13027 ///
13028 /// @return the typedef, class or enum type found.
13029 type_base_sptr
13030 lookup_class_typedef_or_enum_type(const string& qualified_name,
13031  const corpus& corp)
13032 {
13033  type_base_sptr result = lookup_class_or_typedef_type(qualified_name, corp);
13034  if (!result)
13035  result = lookup_enum_type(qualified_name, corp);
13036 
13037  return result;
13038 }
13039 
13040 /// Look into a given corpus to find a qualified type which has the
13041 /// same qualified name as a given type.
13042 ///
13043 /// @param t the type which has the same qualified name as the
13044 /// qualified type we are looking for.
13045 ///
13046 /// @param corp the corpus to look into.
13047 ///
13048 /// @return the qualified type found.
13049 qualified_type_def_sptr
13051 {
13053  return lookup_qualified_type(s, corp);
13054 }
13055 
13056 /// Look into a given corpus to find a qualified type which has a
13057 /// given qualified name.
13058 ///
13059 /// @param qualified_name the qualified name of the type to look for.
13060 ///
13061 /// @param corp the corpus to look into.
13062 ///
13063 /// @return the type found.
13064 qualified_type_def_sptr
13065 lookup_qualified_type(const interned_string& qualified_name, const corpus& corp)
13066 {
13068  corp.get_types().qualified_types();
13069 
13070  qualified_type_def_sptr result =
13071  lookup_type_in_map<qualified_type_def>(qualified_name, m);
13072 
13073  if (!result)
13074  result = lookup_qualified_type_through_translation_units(qualified_name,
13075  corp);
13076 
13077  return result;
13078 }
13079 
13080 /// Look into a given corpus to find a pointer type which has the same
13081 /// qualified name as a given pointer type.
13082 ///
13083 /// @param t the pointer type which has the same qualified name as the
13084 /// type we are looking for.
13085 ///
13086 /// @param corp the corpus to look into.
13087 ///
13088 /// @return the pointer type found.
13091 {
13093  return lookup_pointer_type(s, corp);
13094 }
13095 
13096 /// Look into a given corpus to find a pointer type which has a given
13097 /// qualified name.
13098 ///
13099 /// If the per-corpus type map is non-empty (because the corpus allows
13100 /// the One Definition Rule) then the type islooked up in that
13101 /// per-corpus type map. Otherwise, the type is looked-up in each
13102 /// translation unit.
13103 ///
13104 /// @param qualified_name the qualified name of the pointer type to
13105 /// look for.
13106 ///
13107 /// @param corp the corpus to look into.
13108 ///
13109 /// @return the pointer type found.
13111 lookup_pointer_type(const interned_string& qualified_name, const corpus& corp)
13112 {
13114 
13115  pointer_type_def_sptr result =
13116  lookup_type_in_map<pointer_type_def>(qualified_name, m);
13117  if (!result)
13118  result = lookup_pointer_type_through_translation_units(qualified_name,
13119  corp);
13120 
13121  return result;
13122 }
13123 
13124 /// Look into a given corpus to find a reference type which has the
13125 /// same qualified name as a given reference type.
13126 ///
13127 /// If the per-corpus type map is non-empty (because the corpus allows
13128 /// the One Definition Rule) then the type islooked up in that
13129 /// per-corpus type map. Otherwise, the type is looked-up in each
13130 /// translation unit.
13131 ///
13132 /// @param t the reference type which has the same qualified name as
13133 /// the reference type we are looking for.
13134 ///
13135 /// @param corp the corpus to look into.
13136 ///
13137 /// @return the reference type found.
13140 {
13142  return lookup_reference_type(s, corp);
13143 }
13144 
13145 /// Look into a given corpus to find a reference type which has a
13146 /// given qualified name.
13147 ///
13148 /// If the per-corpus type map is non-empty (because the corpus allows
13149 /// the One Definition Rule) then the type islooked up in that
13150 /// per-corpus type map. Otherwise, the type is looked-up in each
13151 /// translation unit.
13152 ///
13153 /// @param qualified_name the qualified name of the reference type to
13154 /// look for.
13155 ///
13156 /// @param corp the corpus to look into.
13157 ///
13158 /// @return the reference type found.
13160 lookup_reference_type(const interned_string& qualified_name, const corpus& corp)
13161 {
13163  corp.get_types().reference_types();
13164 
13165  reference_type_def_sptr result =
13166  lookup_type_in_map<reference_type_def>(qualified_name, m);
13167  if (!result)
13168  result = lookup_reference_type_through_translation_units(qualified_name,
13169  corp);
13170 
13171  return result;
13172 }
13173 
13174 /// Look into a given corpus to find an array type which has a given
13175 /// qualified name.
13176 ///
13177 /// If the per-corpus type map is non-empty (because the corpus allows
13178 /// the One Definition Rule) then the type islooked up in that
13179 /// per-corpus type map. Otherwise, the type is looked-up in each
13180 /// translation unit.
13181 ///
13182 /// @param qualified_name the qualified name of the array type to look
13183 /// for.
13184 ///
13185 /// @param corp the corpus to look into.
13186 ///
13187 /// @return the array type found.
13190 {
13192  return lookup_array_type(s, corp);
13193 }
13194 
13195 /// Look into a given corpus to find an array type which has the same
13196 /// qualified name as a given array type.
13197 ///
13198 /// If the per-corpus type map is non-empty (because the corpus allows
13199 /// the One Definition Rule) then the type islooked up in that
13200 /// per-corpus type map. Otherwise, the type is looked-up in each
13201 /// translation unit.
13202 ///
13203 /// @param t the type which has the same qualified name as the type we
13204 /// are looking for.
13205 ///
13206 /// @param corp the corpus to look into.
13207 ///
13208 /// @return the type found.
13210 lookup_array_type(const interned_string& qualified_name, const corpus& corp)
13211 {
13213 
13214  array_type_def_sptr result =
13215  lookup_type_in_map<array_type_def>(qualified_name, m);
13216  if (!result)
13217  result = lookup_array_type_through_translation_units(qualified_name, corp);
13218 
13219  return result;
13220 }
13221 
13222 /// Look into a given corpus to find a function type which has the same
13223 /// qualified name as a given function type.
13224 ///
13225 /// If the per-corpus type map is non-empty (because the corpus allows
13226 /// the One Definition Rule) then the type islooked up in that
13227 /// per-corpus type map. Otherwise, the type is looked-up in each
13228 /// translation unit.
13229 ///
13230 /// @param t the function type which has the same qualified name as
13231 /// the function type we are looking for.
13232 ///
13233 /// @param corp the corpus to look into.
13234 ///
13235 /// @return the function type found.
13238 {
13239  interned_string type_name = get_type_name(t);
13240  return lookup_function_type(type_name, corp);
13241 }
13242 
13243 /// Look into a given corpus to find a function type which has the same
13244 /// qualified name as a given function type.
13245 ///
13246 /// If the per-corpus type map is non-empty (because the corpus allows
13247 /// the One Definition Rule) then the type islooked up in that
13248 /// per-corpus type map. Otherwise, the type is looked-up in each
13249 /// translation unit.
13250 ///
13251 /// @param t the function type which has the same qualified name as
13252 /// the function type we are looking for.
13253 ///
13254 /// @param corp the corpus to look into.
13255 ///
13256 /// @return the function type found.
13259  const corpus& corpus)
13260 {
13261  if (fn_t)
13262  return lookup_function_type(*fn_t, corpus);
13263  return function_type_sptr();
13264 }
13265 
13266 /// Look into a given corpus to find a function type which has a given
13267 /// qualified name.
13268 ///
13269 /// If the per-corpus type map is non-empty (because the corpus allows
13270 /// the One Definition Rule) then the type islooked up in that
13271 /// per-corpus type map. Otherwise, the type is looked-up in each
13272 /// translation unit.
13273 ///
13274 /// @param qualified_name the qualified name of the function type to
13275 /// look for.
13276 ///
13277 /// @param corp the corpus to look into.
13278 ///
13279 /// @return the function type found.
13281 lookup_function_type(const interned_string& qualified_name, const corpus& corp)
13282 {
13284 
13285  function_type_sptr result =
13286  lookup_type_in_map<function_type>(qualified_name, m);
13287  if (!result)
13288  result = lookup_function_type_through_translation_units(qualified_name,
13289  corp);
13290 
13291  return result;
13292 }
13293 
13294 /// Look into a given corpus to find a type which has a given
13295 /// qualified name.
13296 ///
13297 /// If the per-corpus type map is non-empty (because the corpus allows
13298 /// the One Definition Rule) then the type islooked up in that
13299 /// per-corpus type map. Otherwise, the type is looked-up in each
13300 /// translation unit.
13301 ///
13302 /// @param qualified_name the qualified name of the function type to
13303 /// look for.
13304 ///
13305 /// @param corp the corpus to look into.
13306 ///
13307 /// @return the function type found.
13308 type_base_sptr
13309 lookup_type(const interned_string& n, const corpus& corp)
13310 {
13311  type_base_sptr result;
13312 
13313  ((result = lookup_basic_type(n, corp))
13314  || (result = lookup_class_type(n, corp))
13315  || (result = lookup_union_type(n, corp))
13316  || (result = lookup_enum_type(n, corp))
13317  || (result = lookup_typedef_type(n, corp))
13318  || (result = lookup_qualified_type(n, corp))
13319  || (result = lookup_pointer_type(n, corp))
13320  || (result = lookup_reference_type(n, corp))
13321  || (result = lookup_array_type(n, corp))
13322  || (result= lookup_function_type(n, corp)));
13323 
13324  return result;
13325 }
13326 
13327 /// Lookup a type from a corpus, by its location.
13328 ///
13329 /// @param loc the location to consider.
13330 ///
13331 /// @param corp the corpus to look the type from.
13332 ///
13333 /// @return the resulting type, if any found.
13334 type_base_sptr
13336 {
13337  // TODO: finish this.
13338 
13339  //TODO: when we fully support types indexed by their location, this
13340  //function should return a vector of types because at each location,
13341  //there can be several types that are defined (yay, C and C++,
13342  //*sigh*).
13343 
13344  type_base_sptr result;
13345  ((result = lookup_basic_type_per_location(loc, corp))
13346  || (result = lookup_class_type_per_location(loc, corp))
13347  || (result = lookup_union_type_per_location(loc, corp))
13348  || (result = lookup_enum_type_per_location(loc, corp))
13349  || (result = lookup_typedef_type_per_location(loc, corp)));
13350 
13351  return result;
13352 }
13353 
13354 /// Look into a given corpus to find a type
13355 ///
13356 /// If the per-corpus type map is non-empty (because the corpus allows
13357 /// the One Definition Rule) then the type islooked up in that
13358 /// per-corpus type map. Otherwise, the type is looked-up in each
13359 /// translation unit.
13360 ///
13361 /// @param qualified_name the qualified name of the function type to
13362 /// look for.
13363 ///
13364 /// @param corp the corpus to look into.
13365 ///
13366 /// @return the function type found.
13367 type_base_sptr
13368 lookup_type(const type_base&t, const corpus& corp)
13369 {
13371  return lookup_type(n, corp);
13372 }
13373 
13374 /// Look into a given corpus to find a type
13375 ///
13376 /// If the per-corpus type map is non-empty (because the corpus allows
13377 /// the One Definition Rule) then the type islooked up in that
13378 /// per-corpus type map. Otherwise, the type is looked-up in each
13379 /// translation unit.
13380 ///
13381 /// @param qualified_name the qualified name of the function type to
13382 /// look for.
13383 ///
13384 /// @param corp the corpus to look into.
13385 ///
13386 /// @return the function type found.
13387 type_base_sptr
13388 lookup_type(const type_base_sptr&t, const corpus& corp)
13389 {
13390  if (t)
13391  return lookup_type(*t, corp);
13392  return type_base_sptr();
13393 }
13394 
13395 /// Update the map that associates a fully qualified name of a given
13396 /// type to that type.
13397 ///
13398 ///
13399 /// @param type the type we are considering.
13400 ///
13401 /// @param types_map the map to update. It's a map that assciates a
13402 /// fully qualified name of a type to the type itself.
13403 ///
13404 /// @param use_type_name_as_key if true, use the name of the type as
13405 /// the key to look it up later. If false, then use the location of
13406 /// the type as a key to look it up later.
13407 ///
13408 /// @return true iff the type was added to the map.
13409 template<typename TypeKind>
13410 bool
13411 maybe_update_types_lookup_map(const shared_ptr<TypeKind>& type,
13413  bool use_type_name_as_key = true)
13414 {
13415  interned_string s;
13416 
13417  if (use_type_name_as_key)
13418  s = get_type_name(type);
13419  else if (location l = type->get_location())
13420  {
13421  string str = l.expand();
13422  s = type->get_environment().intern(str);
13423  }
13424 
13425  istring_type_base_wptrs_map_type::iterator i = types_map.find(s);
13426  bool result = false;
13427 
13428  if (i == types_map.end())
13429  {
13430  types_map[s].push_back(type);
13431  result = true;
13432  }
13433  else
13434  i->second.push_back(type);
13435 
13436  return result;
13437 }
13438 
13439 /// This is the specialization for type @ref class_decl of the
13440 /// function template:
13441 ///
13442 /// maybe_update_types_lookup_map<T>(scope_decl*,
13443 /// const shared_ptr<T>&,
13444 /// istring_type_base_wptrs_map_type&)
13445 ///
13446 /// @param class_type the type to consider.
13447 ///
13448 /// @param types_map the type map to update.
13449 ///
13450 /// @return true iff the type was added to the map.
13451 template<>
13452 bool
13455  bool use_type_name_as_key)
13456 {
13457  class_decl_sptr type = class_type;
13458 
13459  bool update_qname_map = true;
13460  if (type->get_is_declaration_only())
13461  {
13462  // Let's try to look through decl-only classes to get their
13463  // definition. But if the class doesn't have a definition then
13464  // we'll keep it.
13465  if (class_decl_sptr def =
13466  is_class_type(class_type->get_definition_of_declaration()))
13467  type = def;
13468  }
13469 
13470  if (!update_qname_map)
13471  return false;
13472 
13473  interned_string s;
13474  if (use_type_name_as_key)
13475  {
13476  string qname = type->get_qualified_name();
13477  s = type->get_environment().intern(qname);
13478  }
13479  else if (location l = type->get_location())
13480  {
13481  string str = l.expand();
13482  s = type->get_environment().intern(str);
13483  }
13484 
13485  bool result = false;
13486  istring_type_base_wptrs_map_type::iterator i = map.find(s);
13487  if (i == map.end())
13488  {
13489  map[s].push_back(type);
13490  result = true;
13491  }
13492  else
13493  i->second.push_back(type);
13494 
13495  return result;
13496 }
13497 
13498 /// This is the specialization for type @ref function_type of the
13499 /// function template:
13500 ///
13501 /// maybe_update_types_lookup_map<T>(scope_decl*,
13502 /// const shared_ptr<T>&,
13503 /// istring_type_base_wptrs_map_type&)
13504 ///
13505 /// @param scope the scope of the type to consider.
13506 ///
13507 /// @param class_type the type to consider.
13508 ///
13509 /// @param types_map the type map to update.
13510 ///
13511 /// @return true iff the type was added to the map.
13512 template<>
13513 bool
13515 (const function_type_sptr& type,
13517  bool /*use_type_name_as_key*/)
13518 {
13519  bool result = false;
13520  interned_string s = get_type_name(type);
13521  istring_type_base_wptrs_map_type::iterator i = types_map.find(s);
13522  if (i == types_map.end())
13523  {
13524  types_map[s].push_back(type);
13525  result = true;
13526  }
13527  else
13528  i->second.push_back(type);
13529 
13530  return result;
13531 }
13532 
13533 /// Update the map that associates the fully qualified name of a basic
13534 /// type with the type itself.
13535 ///
13536 /// The per-translation unit type map is updated if no type with this
13537 /// name was already existing in that map.
13538 ///
13539 /// If no type with this name did already exist in the per-corpus type
13540 /// map, then that per-corpus type map is updated. Otherwise, that
13541 /// type is erased from that per-corpus map.
13542 ///
13543 /// @param basic_type the basic type to consider.
13544 void
13546 {
13547  if (translation_unit *tu = basic_type->get_translation_unit())
13548  maybe_update_types_lookup_map<type_decl>
13549  (basic_type, tu->get_types().basic_types());
13550 
13551  if (corpus *type_corpus = basic_type->get_corpus())
13552  {
13553  maybe_update_types_lookup_map<type_decl>
13554  (basic_type,
13555  type_corpus->priv_->get_types().basic_types());
13556 
13557  maybe_update_types_lookup_map<type_decl>
13558  (basic_type,
13559  type_corpus->get_type_per_loc_map().basic_types(),
13560  /*use_type_name_as_key*/false);
13561 
13562  if (corpus *group = type_corpus->get_group())
13563  {
13564  maybe_update_types_lookup_map<type_decl>
13565  (basic_type,
13566  group->priv_->get_types().basic_types());
13567 
13568  maybe_update_types_lookup_map<type_decl>
13569  (basic_type,
13570  group->get_type_per_loc_map().basic_types(),
13571  /*use_type_name_as_key*/false);
13572  }
13573  }
13574 
13575 }
13576 
13577 /// Update the map that associates the fully qualified name of a class
13578 /// type with the type itself.
13579 ///
13580 /// The per-translation unit type map is updated if no type with this
13581 /// name was already existing in that map.
13582 ///
13583 /// If no type with this name did already exist in the per-corpus type
13584 /// map, then that per-corpus type map is updated. Otherwise, that
13585 /// type is erased from that per-corpus map.
13586 ///
13587 /// @param class_type the class type to consider.
13588 void
13590 {
13591  if (translation_unit *tu = class_type->get_translation_unit())
13593  (class_type, tu->get_types().class_types());
13594 
13595  if (corpus *type_corpus = class_type->get_corpus())
13596  {
13598  (class_type,
13599  type_corpus->priv_->get_types().class_types());
13600 
13602  (class_type,
13603  type_corpus->get_type_per_loc_map().class_types(),
13604  /*use_type_name_as_key*/false);
13605 
13606  if (corpus *group = type_corpus->get_group())
13607  {
13609  (class_type,
13610  group->priv_->get_types().class_types());
13611 
13613  (class_type,
13614  group->get_type_per_loc_map().class_types(),
13615  /*use_type_name_as_key*/false);
13616  }
13617  }
13618 }
13619 
13620 /// Update the map that associates the fully qualified name of a union
13621 /// type with the type itself.
13622 ///
13623 /// The per-translation unit type map is updated if no type with this
13624 /// name was already existing in that map.
13625 ///
13626 /// If no type with this name did already exist in the per-corpus type
13627 /// map, then that per-corpus type map is updated. Otherwise, that
13628 /// type is erased from that per-corpus map.
13629 ///
13630 /// @param union_type the union type to consider.
13631 void
13632 maybe_update_types_lookup_map(const union_decl_sptr& union_type)
13633 {
13634  if (translation_unit *tu = union_type->get_translation_unit())
13635  maybe_update_types_lookup_map<union_decl>
13636  (union_type, tu->get_types().union_types());
13637 
13638  if (corpus *type_corpus = union_type->get_corpus())
13639  {
13640  maybe_update_types_lookup_map<union_decl>
13641  (union_type,
13642  type_corpus->priv_->get_types().union_types());
13643 
13644  maybe_update_types_lookup_map<union_decl>
13645  (union_type,
13646  type_corpus->get_type_per_loc_map().union_types(),
13647  /*use_type_name_as_key*/false);
13648 
13649  if (corpus *group = type_corpus->get_group())
13650  {
13651  maybe_update_types_lookup_map<union_decl>
13652  (union_type,
13653  group->priv_->get_types().union_types());
13654 
13655  maybe_update_types_lookup_map<union_decl>
13656  (union_type,
13657  group->get_type_per_loc_map().union_types(),
13658  /*use_type_name_as_key*/false);
13659  }
13660  }
13661 }
13662 
13663 /// Update the map that associates the fully qualified name of an enum
13664 /// type with the type itself.
13665 ///
13666 /// The per-translation unit type map is updated if no type with this
13667 /// name was already existing in that map.
13668 ///
13669 /// If no type with this name did already exist in the per-corpus type
13670 /// map, then that per-corpus type map is updated. Otherwise, that
13671 /// type is erased from that per-corpus map.
13672 ///
13673 /// @param enum_type the type to consider.
13674 void
13676 {
13677  if (translation_unit *tu = enum_type->get_translation_unit())
13678  maybe_update_types_lookup_map<enum_type_decl>
13679  (enum_type, tu->get_types().enum_types());
13680 
13681  if (corpus *type_corpus = enum_type->get_corpus())
13682  {
13683  maybe_update_types_lookup_map<enum_type_decl>
13684  (enum_type,
13685  type_corpus->priv_->get_types().enum_types());
13686 
13687  maybe_update_types_lookup_map<enum_type_decl>
13688  (enum_type,
13689  type_corpus->get_type_per_loc_map().enum_types(),
13690  /*use_type_name_as_key*/false);
13691 
13692  if (corpus *group = type_corpus->get_group())
13693  {
13694  maybe_update_types_lookup_map<enum_type_decl>
13695  (enum_type,
13696  group->priv_->get_types().enum_types());
13697 
13698  maybe_update_types_lookup_map<enum_type_decl>
13699  (enum_type,
13700  group->get_type_per_loc_map().enum_types(),
13701  /*use_type_name_as_key*/false);
13702  }
13703  }
13704 
13705 }
13706 
13707 /// Update the map that associates the fully qualified name of a
13708 /// typedef type with the type itself.
13709 ///
13710 /// The per-translation unit type map is updated if no type with this
13711 /// name was already existing in that map.
13712 ///
13713 /// If no type with this name did already exist in the per-corpus type
13714 /// map, then that per-corpus type map is updated. Otherwise, that
13715 /// type is erased from that per-corpus map.
13716 ///
13717 /// @param typedef_type the type to consider.
13718 void
13720 {
13721  if (translation_unit *tu = typedef_type->get_translation_unit())
13722  maybe_update_types_lookup_map<typedef_decl>
13723  (typedef_type, tu->get_types().typedef_types());
13724 
13725  if (corpus *type_corpus = typedef_type->get_corpus())
13726  {
13727  maybe_update_types_lookup_map<typedef_decl>
13728  (typedef_type,
13729  type_corpus->priv_->get_types().typedef_types());
13730 
13731  maybe_update_types_lookup_map<typedef_decl>
13732  (typedef_type,
13733  type_corpus->get_type_per_loc_map().typedef_types(),
13734  /*use_type_name_as_key*/false);
13735 
13736  if (corpus *group = type_corpus->get_group())
13737  {
13738  maybe_update_types_lookup_map<typedef_decl>
13739  (typedef_type,
13740  group->priv_->get_types().typedef_types());
13741 
13742  maybe_update_types_lookup_map<typedef_decl>
13743  (typedef_type,
13744  group->get_type_per_loc_map().typedef_types(),
13745  /*use_type_name_as_key*/false);
13746  }
13747  }
13748 }
13749 
13750 /// Update the map that associates the fully qualified name of a
13751 /// qualified type with the type itself.
13752 ///
13753 /// The per-translation unit type map is updated if no type with this
13754 /// name was already existing in that map.
13755 ///
13756 /// If no type with this name did already exist in the per-corpus type
13757 /// map, then that per-corpus type map is updated. Otherwise, that
13758 /// type is erased from that per-corpus map.
13759 ///
13760 /// @param qualified_type the type to consider.
13761 void
13762 maybe_update_types_lookup_map(const qualified_type_def_sptr& qualified_type)
13763 {
13764  if (translation_unit *tu = qualified_type->get_translation_unit())
13765  maybe_update_types_lookup_map<qualified_type_def>
13766  (qualified_type, tu->get_types().qualified_types());
13767 
13768  if (corpus *type_corpus = qualified_type->get_corpus())
13769  {
13770  maybe_update_types_lookup_map<qualified_type_def>
13771  (qualified_type,
13772  type_corpus->priv_->get_types().qualified_types());
13773 
13774  if (corpus *group = type_corpus->get_group())
13775  {
13776  maybe_update_types_lookup_map<qualified_type_def>
13777  (qualified_type,
13778  group->priv_->get_types().qualified_types());
13779  }
13780  }
13781 }
13782 
13783 /// Update the map that associates the fully qualified name of a
13784 /// pointer type with the type itself.
13785 ///
13786 /// The per-translation unit type map is updated if no type with this
13787 /// name was already existing in that map.
13788 ///
13789 /// If no type with this name did already exist in the per-corpus type
13790 /// map, then that per-corpus type map is updated. Otherwise, that
13791 /// type is erased from that per-corpus map.
13792 ///
13793 /// @param pointer_type the type to consider.
13794 void
13796 {
13797  if (translation_unit *tu = pointer_type->get_translation_unit())
13798  maybe_update_types_lookup_map<pointer_type_def>
13799  (pointer_type, tu->get_types().pointer_types());
13800 
13801  if (corpus *type_corpus = pointer_type->get_corpus())
13802  {
13803  maybe_update_types_lookup_map<pointer_type_def>
13804  (pointer_type,
13805  type_corpus->priv_->get_types().pointer_types());
13806 
13807  if (corpus *group = type_corpus->get_group())
13808  {
13809  maybe_update_types_lookup_map<pointer_type_def>
13810  (pointer_type,
13811  group->priv_->get_types().pointer_types());
13812  }
13813  }
13814 }
13815 
13816 /// Update the map that associates the fully qualified name of a
13817 /// reference type with the type itself.
13818 ///
13819 /// The per-translation unit type map is updated if no type with this
13820 /// name was already existing in that map.
13821 ///
13822 /// If no type with this name did already exist in the per-corpus type
13823 /// map, then that per-corpus type map is updated. Otherwise, that
13824 /// type is erased from that per-corpus map.
13825 ///
13826 /// @param reference_type the type to consider.
13827 void
13829 {
13830  if (translation_unit *tu = reference_type->get_translation_unit())
13831  maybe_update_types_lookup_map<reference_type_def>
13832  (reference_type, tu->get_types().reference_types());
13833 
13834  if (corpus *type_corpus = reference_type->get_corpus())
13835  {
13836  maybe_update_types_lookup_map<reference_type_def>
13837  (reference_type,
13838  type_corpus->priv_->get_types().reference_types());
13839 
13840  if (corpus *group = type_corpus->get_group())
13841  {
13842  maybe_update_types_lookup_map<reference_type_def>
13843  (reference_type,
13844  group->priv_->get_types().reference_types());
13845  }
13846  }
13847 }
13848 
13849 /// Update the map that associates the fully qualified name of a type
13850 /// with the type itself.
13851 ///
13852 /// The per-translation unit type map is updated if no type with this
13853 /// name was already existing in that map.
13854 ///
13855 /// If no type with this name did already exist in the per-corpus type
13856 /// map, then that per-corpus type map is updated. Otherwise, that
13857 /// type is erased from that per-corpus map.
13858 ///
13859 /// @param array_type the type to consider.
13860 void
13862 {
13863  if (translation_unit *tu = array_type->get_translation_unit())
13864  maybe_update_types_lookup_map<array_type_def>
13865  (array_type, tu->get_types().array_types());
13866 
13867  if (corpus *type_corpus = array_type->get_corpus())
13868  {
13869  maybe_update_types_lookup_map<array_type_def>
13870  (array_type,
13871  type_corpus->priv_->get_types().array_types());
13872 
13873  maybe_update_types_lookup_map<array_type_def>
13874  (array_type,
13875  type_corpus->get_type_per_loc_map().array_types(),
13876  /*use_type_name_as_key*/false);
13877 
13878  if (corpus *group = type_corpus->get_group())
13879  {
13880  maybe_update_types_lookup_map<array_type_def>
13881  (array_type,
13882  group->priv_->get_types().array_types());
13883 
13884  maybe_update_types_lookup_map<array_type_def>
13885  (array_type,
13886  group->get_type_per_loc_map().array_types(),
13887  /*use_type_name_as_key*/false);
13888  }
13889  }
13890 }
13891 
13892 /// Update the map that associates the fully qualified name of a type
13893 /// with the type itself.
13894 ///
13895 /// The per-translation unit type map is updated if no type with this
13896 /// name was already existing in that map.
13897 ///
13898 /// If no type with this name did already exist in the per-corpus type
13899 /// map, then that per-corpus type map is updated. Otherwise, that
13900 /// type is erased from that per-corpus map.
13901 ///
13902 /// @param subrange_type the type to consider.
13903 void
13905 (const array_type_def::subrange_sptr& subrange_type)
13906 {
13907  if (translation_unit *tu = subrange_type->get_translation_unit())
13908  maybe_update_types_lookup_map<array_type_def::subrange_type>
13909  (subrange_type, tu->get_types().subrange_types());
13910 
13911  if (corpus *type_corpus = subrange_type->get_corpus())
13912  {
13913  maybe_update_types_lookup_map<array_type_def::subrange_type>
13914  (subrange_type,
13915  type_corpus->priv_->get_types().subrange_types());
13916 
13917  maybe_update_types_lookup_map<array_type_def::subrange_type>
13918  (subrange_type,
13919  type_corpus->get_type_per_loc_map().subrange_types(),
13920  /*use_type_name_as_key*/false);
13921 
13922  if (corpus *group = subrange_type->get_corpus())
13923  {
13924  maybe_update_types_lookup_map<array_type_def::subrange_type>
13925  (subrange_type,
13926  group->priv_->get_types().subrange_types());
13927 
13928  maybe_update_types_lookup_map<array_type_def::subrange_type>
13929  (subrange_type,
13930  group->get_type_per_loc_map().subrange_types(),
13931  /*use_type_name_as_key*/false);
13932  }
13933  }
13934 }
13935 
13936 /// Update the map that associates the fully qualified name of a
13937 /// function type with the type itself.
13938 ///
13939 /// The per-translation unit type map is updated if no type with this
13940 /// name was already existing in that map.
13941 ///
13942 /// If no type with this name did already exist in the per-corpus type
13943 /// map, then that per-corpus type map is updated. Otherwise, that
13944 /// type is erased from that per-corpus map.
13945 ///
13946 /// @param scope the scope of the function type.
13947 /// @param fn_type the type to consider.
13948 void
13950 {
13951  if (translation_unit *tu = fn_type->get_translation_unit())
13953  (fn_type, tu->get_types().function_types());
13954 
13955  if (corpus *type_corpus = fn_type->get_corpus())
13956  {
13958  (fn_type,
13959  type_corpus->priv_->get_types().function_types());
13960 
13961  if (corpus *group = fn_type->get_corpus())
13962  {
13964  (fn_type,
13965  group->priv_->get_types().function_types());
13966  }
13967  }
13968 }
13969 
13970 /// Update the map that associates the fully qualified name of a type
13971 /// declaration with the type itself.
13972 ///
13973 /// The per-translation unit type map is updated if no type with this
13974 /// name was already existing in that map.
13975 ///
13976 /// If no type with this name did already exist in the per-corpus type
13977 /// map, then that per-corpus type map is updated. Otherwise, that
13978 /// type is erased from that per-corpus map.
13979 ///
13980 /// @param decl the declaration of the type to consider.
13981 void
13982 maybe_update_types_lookup_map(const decl_base_sptr& decl)
13983 {
13984  if (!is_type(decl))
13985  return;
13986 
13987  if (type_decl_sptr basic_type = is_type_decl(decl))
13988  maybe_update_types_lookup_map(basic_type);
13989  else if (class_decl_sptr class_type = is_class_type(decl))
13990  maybe_update_types_lookup_map(class_type);
13991  else if (union_decl_sptr union_type = is_union_type(decl))
13992  maybe_update_types_lookup_map(union_type);
13993  else if (enum_type_decl_sptr enum_type = is_enum_type(decl))
13994  maybe_update_types_lookup_map(enum_type);
13995  else if (typedef_decl_sptr typedef_type = is_typedef(decl))
13996  maybe_update_types_lookup_map(typedef_type);
13997  else if (qualified_type_def_sptr qualified_type = is_qualified_type(decl))
13998  maybe_update_types_lookup_map(qualified_type);
13999  else if (pointer_type_def_sptr pointer_type = is_pointer_type(decl))
14000  maybe_update_types_lookup_map(pointer_type);
14001  else if (reference_type_def_sptr reference_type = is_reference_type(decl))
14002  maybe_update_types_lookup_map(reference_type);
14003  else if (array_type_def_sptr array_type = is_array_type(decl))
14004  maybe_update_types_lookup_map(array_type);
14005  else if (array_type_def::subrange_sptr subrange_type = is_subrange_type(decl))
14006  maybe_update_types_lookup_map(subrange_type);
14007  else
14009 }
14010 
14011 /// Update the map that associates the fully qualified name of a type
14012 /// with the type itself.
14013 ///
14014 /// The per-translation unit type map is updated if no type with this
14015 /// name was already existing in that map.
14016 ///
14017 /// If no type with this name did already exist in the per-corpus type
14018 /// map, then that per-corpus type map is updated. Otherwise, that
14019 /// type is erased from that per-corpus map.
14020 ///
14021 /// @param type the type to consider.
14022 void
14023 maybe_update_types_lookup_map(const type_base_sptr& type)
14024 {
14025  if (decl_base_sptr decl = get_type_declaration(type))
14027  else
14029 }
14030 
14031 //--------------------------------
14032 // </type and decls lookup stuff>
14033 // ------------------------------
14034 
14035 /// In a translation unit, lookup a given type or synthesize it if
14036 /// it's a qualified type.
14037 ///
14038 /// So this function first looks the type up in the translation unit.
14039 /// If it's found, then OK, it's returned. Otherwise, if it's a
14040 /// qualified, reference or pointer or function type (a composite
14041 /// type), lookup the underlying type, synthesize the type we want
14042 /// from it and return it.
14043 ///
14044 /// If the underlying types is not not found, then give up and return
14045 /// nil.
14046 ///
14047 /// @return the type that was found or the synthesized type.
14048 type_base_sptr
14049 synthesize_type_from_translation_unit(const type_base_sptr& type,
14050  translation_unit& tu)
14051 {
14052  type_base_sptr result;
14053 
14054  result = lookup_type(type, tu);
14055 
14056  if (!result)
14057  {
14058  if (qualified_type_def_sptr qual = is_qualified_type(type))
14059  {
14060  type_base_sptr underlying_type =
14061  synthesize_type_from_translation_unit(qual->get_underlying_type(),
14062  tu);
14063  if (underlying_type)
14064  {
14065  result.reset(new qualified_type_def(underlying_type,
14066  qual->get_cv_quals(),
14067  qual->get_location()));
14068  }
14069  }
14070  else if (pointer_type_def_sptr p = is_pointer_type(type))
14071  {
14072  type_base_sptr pointed_to_type =
14073  synthesize_type_from_translation_unit(p->get_pointed_to_type(),
14074  tu);
14075  if (pointed_to_type)
14076  {
14077  result.reset(new pointer_type_def(pointed_to_type,
14078  p->get_size_in_bits(),
14079  p->get_alignment_in_bits(),
14080  p->get_location()));
14081  }
14082  }
14083  else if (reference_type_def_sptr r = is_reference_type(type))
14084  {
14085  type_base_sptr pointed_to_type =
14086  synthesize_type_from_translation_unit(r->get_pointed_to_type(), tu);
14087  if (pointed_to_type)
14088  {
14089  result.reset(new reference_type_def(pointed_to_type,
14090  r->is_lvalue(),
14091  r->get_size_in_bits(),
14092  r->get_alignment_in_bits(),
14093  r->get_location()));
14094  }
14095  }
14096  else if (function_type_sptr f = is_function_type(type))
14098 
14099  if (result)
14100  {
14102  canonicalize(result);
14103  }
14104  }
14105 
14106  if (result)
14107  tu.priv_->synthesized_types_.push_back(result);
14108 
14109  return result;
14110 }
14111 
14112 /// In a translation unit, lookup the sub-types that make up a given
14113 /// function type and if the sub-types are all found, synthesize and
14114 /// return a function_type with them.
14115 ///
14116 /// This function is like lookup_function_type_in_translation_unit()
14117 /// execept that it constructs the function type from the sub-types
14118 /// found in the translation, rather than just looking for the
14119 /// function types held by the translation unit. This can be useful
14120 /// if the translation unit doesnt hold the function type we are
14121 /// looking for (i.e, lookup_function_type_in_translation_unit()
14122 /// returned NULL) but we still want to see if the sub-types of the
14123 /// function types are present in the translation unit.
14124 ///
14125 /// @param fn_type the function type to consider.
14126 ///
14127 /// @param tu the translation unit to look into.
14128 ///
14129 /// @return the resulting synthesized function type if all its
14130 /// sub-types have been found, NULL otherwise.
14133  translation_unit& tu)
14134 {
14136 
14137  const environment& env = tu.get_environment();
14138 
14139  type_base_sptr return_type = fn_type.get_return_type();
14140  type_base_sptr result_return_type;
14141  if (!return_type || env.is_void_type(return_type))
14142  result_return_type = env.get_void_type();
14143  else
14144  result_return_type = synthesize_type_from_translation_unit(return_type, tu);
14145  if (!result_return_type)
14146  return nil;
14147 
14149  type_base_sptr parm_type;
14151  for (function_type::parameters::const_iterator i =
14152  fn_type.get_parameters().begin();
14153  i != fn_type.get_parameters().end();
14154  ++i)
14155  {
14156  type_base_sptr t = (*i)->get_type();
14157  parm_type = synthesize_type_from_translation_unit(t, tu);
14158  if (!parm_type)
14159  return nil;
14160  parm.reset(new function_decl::parameter(parm_type,
14161  (*i)->get_index(),
14162  (*i)->get_name(),
14163  (*i)->get_location(),
14164  (*i)->get_variadic_marker(),
14165  (*i)->get_is_artificial()));
14166  parms.push_back(parm);
14167  }
14168 
14169  class_or_union_sptr class_type;
14170  const method_type* method = is_method_type(&fn_type);
14171  if (method)
14172  {
14173  class_type = is_class_or_union_type
14175  ABG_ASSERT(class_type);
14176  }
14177 
14178  function_type_sptr result_fn_type;
14179 
14180  if (class_type)
14181  result_fn_type.reset(new method_type(result_return_type,
14182  class_type,
14183  parms,
14184  method->get_is_const(),
14185  fn_type.get_size_in_bits(),
14186  fn_type.get_alignment_in_bits()));
14187  else
14188  result_fn_type.reset(new function_type(result_return_type,
14189  parms,
14190  fn_type.get_size_in_bits(),
14191  fn_type.get_alignment_in_bits()));
14192 
14193  tu.priv_->synthesized_types_.push_back(result_fn_type);
14194  tu.bind_function_type_life_time(result_fn_type);
14195 
14196  canonicalize(result_fn_type);
14197  return result_fn_type;
14198 }
14199 
14200 /// Demangle a C++ mangled name and return the resulting string
14201 ///
14202 /// @param mangled_name the C++ mangled name to demangle.
14203 ///
14204 /// @return the resulting mangled name.
14205 string
14206 demangle_cplus_mangled_name(const string& mangled_name)
14207 {
14208  if (mangled_name.empty())
14209  return "";
14210 
14211  size_t l = 0;
14212  int status = 0;
14213  char * str = abi::__cxa_demangle(mangled_name.c_str(),
14214  NULL, &l, &status);
14215  string demangled_name = mangled_name;
14216  if (str)
14217  {
14218  ABG_ASSERT(status == 0);
14219  demangled_name = str;
14220  free(str);
14221  str = 0;
14222  }
14223  return demangled_name;
14224 }
14225 
14226 /// Return either the type given in parameter if it's non-null, or the
14227 /// void type.
14228 ///
14229 /// @param t the type to consider.
14230 ///
14231 /// @param env the environment to use. If NULL, just abort the
14232 /// process.
14233 ///
14234 /// @return either @p t if it is non-null, or the void type.
14235 type_base_sptr
14236 type_or_void(const type_base_sptr t, const environment& env)
14237 {
14238  type_base_sptr r;
14239 
14240  if (t)
14241  r = t;
14242  else
14243  r = type_base_sptr(env.get_void_type());
14244 
14245  return r;
14246 }
14247 
14248 global_scope::~global_scope()
14249 {
14250 }
14251 
14252 static bool
14253 maybe_propagate_canonical_type(const type_base& lhs_type,
14254  const type_base& rhs_type);
14255 
14256 /// Test if two types are eligible to the "Linux Kernel Fast Type
14257 /// Comparison Optimization", a.k.a LKFTCO.
14258 ///
14259 /// Two types T1 and T2 (who are presumably of the same name and kind)
14260 /// are eligible to the LKFTCO if they fulfill the following criteria/
14261 ///
14262 /// 1/ T1 and T2 come from the same Linux Kernel Corpus and they are
14263 /// either class, union or enums.
14264 ///
14265 /// 2/ They are defined in the same translation unit.
14266 ///
14267 /// @param t1 the first type to consider.
14268 ///
14269 /// @param t2 the second type to consider.
14270 ///
14271 /// @return true iff t1 and t2 are eligible to the LKFTCO.
14272 static bool
14273 types_defined_same_linux_kernel_corpus_public(const type_base& t1,
14274  const type_base& t2)
14275 {
14276  const corpus *t1_corpus = t1.get_corpus(), *t2_corpus = t2.get_corpus();
14277  string t1_file_path, t2_file_path;
14278 
14279  /// If the t1 (and t2) are classes/unions/enums from the same linux
14280  /// kernel corpus, let's move on. Otherwise bail out.
14281  if (!(t1_corpus && t2_corpus
14282  && t1_corpus == t2_corpus
14283  && (t1_corpus->get_origin() & corpus::LINUX_KERNEL_BINARY_ORIGIN)
14284  && (is_class_or_union_type(&t1)
14285  || is_enum_type(&t1))))
14286  return false;
14287 
14288  class_or_union *c1 = 0, *c2 = 0;
14289  c1 = is_class_or_union_type(&t1);
14290  c2 = is_class_or_union_type(&t2);
14291 
14292  // Two anonymous class types with no naming typedefs cannot be
14293  // eligible to this optimization.
14294  if ((c1 && c1->get_is_anonymous() && !c1->get_naming_typedef())
14295  || (c2 && c2->get_is_anonymous() && !c2->get_naming_typedef()))
14296  return false;
14297 
14298  // Two anonymous classes with naming typedefs should have the same
14299  // typedef name.
14300  if (c1
14301  && c2
14302  && c1->get_is_anonymous() && c1->get_naming_typedef()
14303  && c2->get_is_anonymous() && c2->get_naming_typedef())
14304  if (c1->get_naming_typedef()->get_name()
14305  != c2->get_naming_typedef()->get_name())
14306  return false;
14307 
14308  // Two anonymous enum types cannot be eligible to this optimization.
14309  if (const enum_type_decl *e1 = is_enum_type(&t1))
14310  if (const enum_type_decl *e2 = is_enum_type(&t2))
14311  if (e1->get_is_anonymous() || e2->get_is_anonymous())
14312  return false;
14313 
14314  // Look through declaration-only types. That is, get the associated
14315  // definition type.
14318 
14319  if (c1 && c2)
14320  {
14321  if (c1->get_is_declaration_only() != c2->get_is_declaration_only())
14322  {
14323  if (c1->get_environment().decl_only_class_equals_definition())
14324  // At least one of classes/union is declaration-only.
14325  // Because we are in a context in which a declaration-only
14326  // class/union is equal to all definitions of that
14327  // class/union, we can assume that the two types are
14328  // equal.
14329  return true;
14330  }
14331  }
14332 
14333  if (t1.get_size_in_bits() != t2.get_size_in_bits())
14334  return false;
14335 
14336  // Look at the file names of the locations of t1 and t2. If they
14337  // are equal, then t1 and t2 are defined in the same file.
14338  {
14339  location l;
14340 
14341  if (c1)
14342  l = c1->get_location();
14343  else
14344  l = dynamic_cast<const decl_base&>(t1).get_location();
14345 
14346  unsigned line = 0, col = 0;
14347  if (l)
14348  l.expand(t1_file_path, line, col);
14349  if (c2)
14350  l = c2->get_location();
14351  else
14352  l = dynamic_cast<const decl_base&>(t2).get_location();
14353  if (l)
14354  l.expand(t2_file_path, line, col);
14355  }
14356 
14357  if (t1_file_path.empty() || t2_file_path.empty())
14358  return false;
14359 
14360  if (t1_file_path == t2_file_path)
14361  return true;
14362 
14363  return false;
14364 }
14365 
14366 
14367 /// Compare a type T against a canonical type.
14368 ///
14369 /// This function is called during the canonicalization process of the
14370 /// type T. T is called the "candidate type" because it's in the
14371 /// process of being canonicalized. Meaning, it's going to be
14372 /// compared to a canonical type C. If T equals C, then the canonical
14373 /// type of T is C.
14374 ///
14375 /// The purpose of this function is to allow the debugging of the
14376 /// canonicalization of T, if that debugging is activated by
14377 /// configuring the libabigail package with
14378 /// --enable-debug-type-canonicalization and by running "abidw
14379 /// --debug-tc". In that case, T is going to be compared to C twice:
14380 /// once with canonical equality and once with structural equality.
14381 /// The two comparisons must be equal. Otherwise, the
14382 /// canonicalization process is said to be faulty and this function
14383 /// aborts.
14384 ///
14385 /// This is a sub-routine of type_base::get_canonical_type_for.
14386 ///
14387 /// @param canonical_type the canonical type to compare the candidate
14388 /// type against.
14389 ///
14390 /// @param candidate_type the candidate type to compare against the
14391 /// canonical type.
14392 ///
14393 /// @return true iff @p canonical_type equals @p candidate_type.
14394 ///
14395 static bool
14396 compare_types_during_canonicalization(const type_base& canonical_type,
14397  const type_base& candidate_type)
14398 {
14399 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
14400  const environment& env = canonical_type.get_environment();
14401  if (env.debug_type_canonicalization_is_on())
14402  {
14403  bool canonical_equality = false, structural_equality = false;
14404  env.priv_->use_canonical_type_comparison_ = false;
14405  structural_equality = canonical_type == candidate_type;
14406  env.priv_->use_canonical_type_comparison_ = true;
14407  canonical_equality = canonical_type == candidate_type;
14408  if (canonical_equality != structural_equality)
14409  {
14410  std::cerr << "structural & canonical equality different for type: "
14411  << canonical_type.get_pretty_representation(true, true)
14412  << std::endl;
14414  }
14415  return structural_equality;
14416  }
14417 #endif //end WITH_DEBUG_TYPE_CANONICALIZATION
14418  return canonical_type == candidate_type;
14419 }
14420 
14421 /// Compare a canonical type against a candidate canonical type.
14422 ///
14423 /// This is ultimately a sub-routine of the
14424 /// type_base::get_canonical_type_for().
14425 ///
14426 /// The goal of this function is to ease debugging because it can be
14427 /// called from within type_base::get_canonical_type_for() from the
14428 /// prompt of the debugger (with some breakpoint appropriately set) to
14429 /// debug the comparison that happens during type canonicalization,
14430 /// between a candidate type being canonicalized, and an existing
14431 /// canonical type that is registered in the system, in as returned by
14432 /// environment::get_canonical_types()
14433 ///
14434 /// @param canonical_type the canonical type to consider.
14435 ///
14436 /// @param candidate_type the candidate type that is being
14437 /// canonicalized, and thus compared to @p canonical_type.
14438 ///
14439 /// @return true iff @p canonical_type compares equal to @p
14440 /// candidate_type.
14441 static bool
14442 compare_canonical_type_against_candidate(const type_base& canonical_type,
14443  const type_base& candidate_type)
14444 {
14445  environment& env = const_cast<environment&>(canonical_type.get_environment());
14446 
14447  // Before the "*it == it" comparison below is done, let's
14448  // perform on-the-fly-canonicalization. For C types, let's
14449  // consider that an unresolved struct declaration 'struct S'
14450  // is different from a definition 'struct S'. This is
14451  // because normally, at this point all the declarations of
14452  // struct S that are compatible with the definition of
14453  // struct S have already been resolved to that definition,
14454  // during the DWARF parsing. The remaining unresolved
14455  // declaration are thus considered different. With this
14456  // setup we can properly handle cases of two *different*
14457  // struct S being defined in the same binary (in different
14458  // translation units), and a third struct S being only
14459  // declared as an opaque type in a third translation unit of
14460  // its own, with no definition in there. In that case, the
14461  // declaration-only struct S should be left alone and not
14462  // resolved to any of the two definitions of struct S.
14463  bool saved_decl_only_class_equals_definition =
14464  env.decl_only_class_equals_definition();
14465  env.do_on_the_fly_canonicalization(true);
14466  // Compare types by considering that decl-only classes don't
14467  // equal their definition.
14468  env.decl_only_class_equals_definition(false);
14469  env.priv_->allow_type_comparison_results_caching(true);
14470  bool equal = (types_defined_same_linux_kernel_corpus_public(canonical_type,
14471  candidate_type)
14472  || compare_types_during_canonicalization(canonical_type,
14473  candidate_type));
14474  // Restore the state of the on-the-fly-canonicalization and
14475  // the decl-only-class-being-equal-to-a-matching-definition
14476  // flags.
14477  env.priv_->allow_type_comparison_results_caching(false);
14478  env.do_on_the_fly_canonicalization(false);
14479  env.decl_only_class_equals_definition
14480  (saved_decl_only_class_equals_definition);
14481  return equal;
14482 }
14483 
14484 /// Compare a canonical type against a candidate canonical type.
14485 ///
14486 /// This is ultimately a sub-routine of the
14487 /// type_base::get_canonical_type_for().
14488 ///
14489 /// The goal of this function is to ease debugging because it can be
14490 /// called from within type_base::get_canonical_type_for() from the
14491 /// prompt of the debugger (with some breakpoint appropriately set) to
14492 /// debug the comparison that happens during type canonicalization,
14493 /// between a candidate type being canonicalized, and an existing
14494 /// canonical type that is registered in the system, in as returned by
14495 /// environment::get_canonical_types()
14496 ///
14497 /// @param canonical_type the canonical type to consider.
14498 ///
14499 /// @param candidate_type the candidate type that is being
14500 /// canonicalized, and thus compared to @p canonical_type.
14501 ///
14502 /// @return true iff @p canonical_type compares equal to @p
14503 /// candidate_type.
14504 static bool
14505 compare_canonical_type_against_candidate(const type_base* canonical_type,
14506  const type_base* candidate_type)
14507 {
14508  return compare_canonical_type_against_candidate(*canonical_type,
14509  *candidate_type);
14510 }
14511 
14512 /// Compare a canonical type against a candidate canonical type.
14513 ///
14514 /// This is ultimately a sub-routine of the
14515 /// type_base::get_canonical_type_for().
14516 ///
14517 /// The goal of this function is to ease debugging because it can be
14518 /// called from within type_base::get_canonical_type_for() from the
14519 /// prompt of the debugger (with some breakpoint appropriately set) to
14520 /// debug the comparison that happens during type canonicalization,
14521 /// between a candidate type being canonicalized, and an existing
14522 /// canonical type that is registered in the system, in as returned by
14523 /// environment::get_canonical_types()
14524 ///
14525 /// @param canonical_type the canonical type to consider.
14526 ///
14527 /// @param candidate_type the candidate type that is being
14528 /// canonicalized, and thus compared to @p canonical_type.
14529 ///
14530 /// @return true iff @p canonical_type compares equal to @p
14531 /// candidate_type.
14532 static bool
14533 compare_canonical_type_against_candidate(const type_base_sptr& canonical_type,
14534  const type_base_sptr& candidate_type)
14535 {
14536  return compare_canonical_type_against_candidate(canonical_type.get(),
14537  candidate_type.get());
14538 }
14539 
14540 /// Compute the canonical type for a given instance of @ref type_base.
14541 ///
14542 /// Consider two types T and T'. The canonical type of T, denoted
14543 /// C(T) is a type such as T == T' if and only if C(T) == C(T'). Said
14544 /// otherwise, to compare two types, one just needs to compare their
14545 /// canonical types using pointer equality. That makes type
14546 /// comparison faster than the structural comparison performed by the
14547 /// abigail::ir::equals() overloads.
14548 ///
14549 /// If there is not yet any canonical type for @p t, then @p t is its
14550 /// own canonical type. Otherwise, this function returns the
14551 /// canonical type of @p t which is the canonical type that has the
14552 /// same hash value as @p t and that structurally equals @p t. Note
14553 /// that after invoking this function, the life time of the returned
14554 /// canonical time is then equals to the life time of the current
14555 /// process.
14556 ///
14557 /// @param t a smart pointer to instance of @ref type_base we want to
14558 /// compute a canonical type for.
14559 ///
14560 /// @return the canonical type for the current instance of @ref
14561 /// type_base.
14562 type_base_sptr
14563 type_base::get_canonical_type_for(type_base_sptr t)
14564 {
14565  if (!t)
14566  return t;
14567 
14568  environment& env = const_cast<environment&>(t->get_environment());
14569 
14571  // This type should not be canonicalized!
14572  return type_base_sptr();
14573 
14574  if (is_decl(t))
14576 
14577  // Look through decl-only types (classes, unions and enums)
14578  bool decl_only_class_equals_definition =
14579  (odr_is_relevant(*t) || env.decl_only_class_equals_definition());
14580 
14581  class_or_union_sptr class_or_union = is_class_or_union_type(t);
14582 
14583  // In the context of types from C++ or languages where we assume the
14584  // "One Definition Rule", we assume that a declaration-only
14585  // non-anonymous class equals all fully defined classes of the same
14586  // name.
14587  //
14588  // Otherwise, all classes, including declaration-only classes are
14589  // canonicalized and only canonical comparison is going to be used
14590  // in the system.
14591  if (decl_only_class_equals_definition)
14592  if (class_or_union)
14593  if (class_or_union->get_is_declaration_only())
14594  return type_base_sptr();
14595 
14596  class_decl_sptr is_class = is_class_type(t);
14597  if (t->get_canonical_type())
14598  return t->get_canonical_type();
14599 
14600  // For classes and union, ensure that an anonymous class doesn't
14601  // have a linkage name. If it does in the future, then me must be
14602  // mindful that the linkage name respects the type identity
14603  // constraints which states that "if two linkage names are different
14604  // then the two types are different".
14605  ABG_ASSERT(!class_or_union
14606  || !class_or_union->get_is_anonymous()
14607  || class_or_union->get_linkage_name().empty());
14608 
14609  // We want the pretty representation of the type, but for an
14610  // internal use, not for a user-facing purpose.
14611  //
14612  // If two classe types Foo are declared, one as a class and the
14613  // other as a struct, but are otherwise equivalent, we want their
14614  // pretty representation to be the same. Hence the 'internal'
14615  // argument of ir::get_pretty_representation() is set to true here.
14616  // So in this case, the pretty representation of Foo is going to be
14617  // "class Foo", regardless of its struct-ness. This also applies to
14618  // composite types which would have "class Foo" as a sub-type.
14619  string repr = t->get_cached_pretty_representation(/*internal=*/true);
14620 
14621  // If 't' already has a canonical type 'inside' its corpus
14622  // (t_corpus), then this variable is going to contain that canonical
14623  // type.
14624  type_base_sptr canonical_type_present_in_corpus;
14626  env.get_canonical_types_map();
14627 
14628  type_base_sptr result;
14629  environment::canonical_types_map_type::iterator i = types.find(repr);
14630  if (i == types.end())
14631  {
14632  vector<type_base_sptr> v;
14633  v.push_back(t);
14634  types[repr] = v;
14635  result = t;
14636  }
14637  else
14638  {
14639  vector<type_base_sptr> &v = i->second;
14640  // Let's compare 't' structurally (i.e, compare its sub-types
14641  // recursively) against the canonical types of the system. If it
14642  // equals a given canonical type C, then it means C is the
14643  // canonical type of 't'. Otherwise, if 't' is different from
14644  // all the canonical types of the system, then it means 't' is a
14645  // canonical type itself.
14646  for (vector<type_base_sptr>::const_reverse_iterator it = v.rbegin();
14647  it != v.rend();
14648  ++it)
14649  {
14650  bool equal = compare_canonical_type_against_candidate(*it, t);
14651  if (equal)
14652  {
14653  result = *it;
14654  break;
14655  }
14656  }
14657 #ifdef WITH_DEBUG_SELF_COMPARISON
14658  if (env.self_comparison_debug_is_on())
14659  {
14660  // So we are debugging the canonicalization process,
14661  // possibly via the use of 'abidw --debug-abidiff <binary>'.
14662  corpus_sptr corp1, corp2;
14663  env.get_self_comparison_debug_inputs(corp1, corp2);
14664  if (corp1 && corp2 && t->get_corpus() == corp2.get())
14665  {
14666  // If 't' comes from the second corpus, then it *must*
14667  // be equal to its matching canonical type coming from
14668  // the first corpus because the second corpus is the
14669  // abixml representation of the first corpus. In other
14670  // words, all types coming from the second corpus must
14671  // have canonical types coming from the first corpus.
14672  if (result)
14673  {
14674  if (!env.priv_->
14675  check_canonical_type_from_abixml_during_self_comp(t,
14676  result))
14677  {
14678  // The canonical type of the type re-read from abixml
14679  // type doesn't match the canonical type that was
14680  // initially serialized down.
14681  uintptr_t should_have_canonical_type = 0;
14682  string type_id = env.get_type_id_from_type(t.get());
14683  if (type_id.empty())
14684  type_id = "type-id-<not-found>";
14685  else
14686  should_have_canonical_type =
14687  env.get_canonical_type_from_type_id(type_id.c_str());
14688  std::cerr << "error: wrong canonical type for '"
14689  << repr
14690  << "' / type: @"
14691  << std::hex
14692  << t.get()
14693  << "/ canon: @"
14694  << result.get()
14695  << ", type-id: '"
14696  << type_id
14697  << "'. Should have had canonical type: "
14698  << std::hex
14699  << should_have_canonical_type
14700  << std::endl;
14701  }
14702  }
14703  else //!result
14704  {
14705  uintptr_t ptr_val = reinterpret_cast<uintptr_t>(t.get());
14706  string type_id = env.get_type_id_from_pointer(ptr_val);
14707  if (type_id.empty())
14708  type_id = "type-id-<not-found>";
14709  // We are in the case where 't' is different from all
14710  // the canonical types of the same name that come from
14711  // the first corpus.
14712  //
14713  // If 't' indeed comes from the second corpus then this
14714  // clearly is a canonicalization failure.
14715  //
14716  // There was a problem either during the serialization
14717  // of 't' into abixml, or during the de-serialization
14718  // from abixml into abigail::ir. Further debugging is
14719  // needed to determine what that root cause problem is.
14720  //
14721  // Note that the first canonicalization problem of this
14722  // kind must be fixed before looking at the subsequent
14723  // ones, because the later might well just be
14724  // consequences of the former.
14725  std::cerr << "error: wrong induced canonical type for '"
14726  << repr
14727  << "' from second corpus"
14728  << ", ptr: " << std::hex << t.get()
14729  << " type-id: " << type_id
14730  << std::endl;
14731  }
14732  }
14733  }
14734 #endif //WITH_DEBUG_SELF_COMPARISON
14735 
14736  if (!result)
14737  {
14738  v.push_back(t);
14739  result = t;
14740  }
14741  }
14742 
14743  return result;
14744 }
14745 
14746 /// This method is invoked automatically right after the current
14747 /// instance of @ref class_decl has been canonicalized.
14748 void
14750 {}
14751 
14752 /// This is a subroutine of the canonicalize() function.
14753 ///
14754 /// When the canonical type C of type T has just been computed, there
14755 /// can be cases where T has member functions that C doesn't have.
14756 ///
14757 /// This is possible because non virtual member functions are not
14758 /// taken in account when comparing two types.
14759 ///
14760 /// In that case, this function updates C so that it contains the
14761 /// member functions.
14762 ///
14763 /// There can also be cases where C has a method M which is not linked
14764 /// to any underlying symbol, whereas in T, M is to link to an
14765 /// underlying symbol. In that case, this function updates M in C so
14766 /// that it's linked to the same underlying symbol as for M in T.
14767 static void
14768 maybe_adjust_canonical_type(const type_base_sptr& canonical,
14769  const type_base_sptr& type)
14770 {
14771  if (!canonical
14772  // If 'type' is *NOT* a newly canonicalized type ...
14773  || type->get_naked_canonical_type()
14774  // ... or if 'type' is it's own canonical type, then get out.
14775  || type.get() == canonical.get())
14776  return;
14777 
14778  if (class_decl_sptr cl = is_class_type(type))
14779  {
14780  class_decl_sptr canonical_class = is_class_type(canonical);
14781 
14782  if (canonical_class)
14783  {
14784  // Set symbols of member functions that might be missing
14785  // theirs.
14786  for (class_decl::member_functions::const_iterator i =
14787  cl->get_member_functions().begin();
14788  i != cl->get_member_functions().end();
14789  ++i)
14790  if ((*i)->get_symbol())
14791  {
14792  if (method_decl *m = canonical_class->
14793  find_member_function((*i)->get_linkage_name()))
14794  {
14795  elf_symbol_sptr s1 = (*i)->get_symbol();
14796  if (s1 && !m->get_symbol())
14797  // Method 'm' in the canonical type is not
14798  // linked to the underlying symbol of '*i'.
14799  // Let's link it now. have th
14800  m->set_symbol(s1);
14801  }
14802  else
14803  // There is a member function defined and publicly
14804  // exported in the other class, and the canonical
14805  // class doesn't have that member function. Let's
14806  // copy that member function to the canonical class
14807  // then.
14808  copy_member_function (canonical_class, *i);
14809  }
14810  }
14811  }
14812 
14813  // If an artificial function type equals a non-artfificial one in
14814  // the system, then the canonical type of both should be deemed
14815  // non-artificial. This is important because only non-artificial
14816  // canonical function types are emitted out into abixml, so if don't
14817  // do this we risk missing to emit some function types.
14818  if (is_function_type(type))
14819  if (type->get_is_artificial() != canonical->get_is_artificial())
14820  canonical->set_is_artificial(false);
14821 }
14822 
14823 /// Compute the canonical type of a given type.
14824 ///
14825 /// It means that after invoking this function, comparing the intance
14826 /// instance @ref type_base and another one (on which
14827 /// type_base::enable_canonical_equality() would have been invoked as
14828 /// well) is performed by just comparing the pointer values of the
14829 /// canonical types of both types. That equality comparison is
14830 /// supposedly faster than structural comparison of the types.
14831 ///
14832 /// @param t a smart pointer to the instance of @ref type_base for
14833 /// which to compute the canonical type. After this call,
14834 /// t->get_canonical_type() will return the newly computed canonical
14835 /// type.
14836 ///
14837 /// @return the canonical type computed for @p t.
14838 type_base_sptr
14839 canonicalize(type_base_sptr t)
14840 {
14841  if (!t)
14842  return t;
14843 
14844  if (t->get_canonical_type())
14845  return t->get_canonical_type();
14846 
14847  type_base_sptr canonical = type_base::get_canonical_type_for(t);
14848  maybe_adjust_canonical_type(canonical, t);
14849 
14850  t->priv_->canonical_type = canonical;
14851  t->priv_->naked_canonical_type = canonical.get();
14852 
14853  // So this type is now canonicalized.
14854  //
14855  // It means that:
14856  //
14857  // 1/ Either the canonical type was not propagated during the
14858  // comparison of another type that was being canonicalized
14859  //
14860  // 2/ Or the canonical type has been propagated during the
14861  // comparison of another type that was being canonicalized and
14862  // that propagated canonical type has been confirmed, because
14863  // it was depending on a recursive type which comparison
14864  // succeeded.
14865  ABG_ASSERT(!t->priv_->canonical_type_propagated()
14866  || t->priv_->propagated_canonical_type_confirmed());
14867 
14868  if (class_decl_sptr cl = is_class_type(t))
14869  if (type_base_sptr d = is_type(cl->get_earlier_declaration()))
14870  if ((canonical = d->get_canonical_type()))
14871  {
14872  d->priv_->canonical_type = canonical;
14873  d->priv_->naked_canonical_type = canonical.get();
14874  }
14875 
14876  if (canonical)
14877  {
14878  if (decl_base_sptr d = is_decl_slow(canonical))
14879  {
14880  scope_decl *scope = d->get_scope();
14881  // Add the canonical type to the set of canonical types
14882  // belonging to its scope.
14883  if (scope)
14884  {
14885  if (is_type(scope))
14886  // The scope in question is itself a type (e.g, a class
14887  // or union). Let's call that type ST. We want to add
14888  // 'canonical' to the set of canonical types belonging
14889  // to ST.
14890  if (type_base_sptr c = is_type(scope)->get_canonical_type())
14891  // We want to add 'canonical' to set of canonical
14892  // types belonging to the canonical type of ST. That
14893  // way, just looking at the canonical type of ST is
14894  // enough to get the types that belong to the scope of
14895  // the class of equivalence of ST.
14896  scope = is_scope_decl(is_decl(c)).get();
14897  scope->get_canonical_types().insert(canonical);
14898  }
14899  // else, if the type doesn't have a scope, it's not meant to be
14900  // emitted. This can be the case for the result of the
14901  // function strip_typedef, for instance.
14902  }
14903 
14904 #ifdef WITH_DEBUG_CT_PROPAGATION
14905  // Update the book-keeping of the set of the types which
14906  // propagated canonical type has been cleared.
14907  //
14908  // If this type 't' which has just been canonicalized was
14909  // previously in the set of types which propagated canonical
14910  // type has been cleared, then remove it from that set because
14911  // its canonical type is now computed and definitely set.
14912  const environment& env = t->get_environment();
14913  env.priv_->erase_type_with_cleared_propagated_canonical_type(t.get());
14914 #endif
14915  }
14916 
14917  t->on_canonical_type_set();
14918  return canonical;
14919 }
14920 
14921 /// Set the definition of this declaration-only @ref decl_base.
14922 ///
14923 /// @param d the new definition to set.
14924 void
14926 {
14928  priv_->definition_of_declaration_ = d;
14929  if (type_base *t = is_type(this))
14930  if (type_base_sptr canonical_type = is_type(d)->get_canonical_type())
14931  t->priv_->canonical_type = canonical_type;
14932 
14933  priv_->naked_definition_of_declaration_ = const_cast<decl_base*>(d.get());
14934 }
14935 
14936 /// The constructor of @ref type_base.
14937 ///
14938 /// @param s the size of the type, in bits.
14939 ///
14940 /// @param a the alignment of the type, in bits.
14941 type_base::type_base(const environment& e, size_t s, size_t a)
14942  : type_or_decl_base(e, ABSTRACT_TYPE_BASE|ABSTRACT_TYPE_BASE),
14943  priv_(new priv(s, a))
14944 {}
14945 
14946 /// Getter of the canonical type of the current instance of @ref
14947 /// type_base.
14948 ///
14949 /// @return a smart pointer to the canonical type of the current
14950 /// intance of @ref type_base, or an empty smart pointer if the
14951 /// current instance of @ref type_base doesn't have any canonical
14952 /// type.
14953 type_base_sptr
14955 {return priv_->canonical_type.lock();}
14956 
14957 /// Getter of the canonical type pointer.
14958 ///
14959 /// Note that this function doesn't return a smart pointer, but rather
14960 /// the underlying pointer managed by the smart pointer. So it's as
14961 /// fast as possible. This getter is to be used in code paths that
14962 /// are proven to be performance hot spots; especially, when comparing
14963 /// sensitive types like class, function, pointers and reference
14964 /// types. Those are compared extremely frequently and thus, their
14965 /// accessing the canonical type must be fast.
14966 ///
14967 /// @return the canonical type pointer, not managed by a smart
14968 /// pointer.
14969 type_base*
14971 {return priv_->naked_canonical_type;}
14972 
14973 /// Get the pretty representation of the current type.
14974 ///
14975 /// The pretty representation is retrieved from a cache. If the cache
14976 /// is empty, this function computes the pretty representation, put it
14977 /// in the cache and returns it.
14978 ///
14979 /// Note that if the type is *NOT* canonicalized, the pretty
14980 /// representation is never cached.
14981 ///
14982 /// @param internal if true, then the pretty representation is to be
14983 /// used for purpuses that are internal to the libabigail library
14984 /// itself. If you don't know what this means, then you probably
14985 /// should set this parameter to "false".
14986 const interned_string&
14988 {
14989  if (internal)
14990  {
14991  if (!get_naked_canonical_type() || priv_->internal_cached_repr_.empty())
14992  {
14993  string r = ir::get_pretty_representation(this, internal);
14994  priv_->internal_cached_repr_ = get_environment().intern(r);
14995  }
14996  return priv_->internal_cached_repr_;
14997  }
14998 
14999  if (!get_naked_canonical_type() || priv_->cached_repr_.empty())
15000  {
15001  string r = ir::get_pretty_representation(this, internal);
15002  priv_->cached_repr_ = get_environment().intern(r);
15003  }
15004 
15005  return priv_->cached_repr_;
15006 }
15007 
15008 /// Compares two instances of @ref type_base.
15009 ///
15010 /// If the two intances are different, set a bitfield to give some
15011 /// insight about the kind of differences there are.
15012 ///
15013 /// @param l the first artifact of the comparison.
15014 ///
15015 /// @param r the second artifact of the comparison.
15016 ///
15017 /// @param k a pointer to a bitfield that gives information about the
15018 /// kind of changes there are between @p l and @p r. This one is set
15019 /// iff @p is non-null and if the function returns false.
15020 ///
15021 /// Please note that setting k to a non-null value does have a
15022 /// negative performance impact because even if @p l and @p r are not
15023 /// equal, the function keeps up the comparison in order to determine
15024 /// the different kinds of ways in which they are different.
15025 ///
15026 /// @return true if @p l equals @p r, false otherwise.
15027 bool
15028 equals(const type_base& l, const type_base& r, change_kind* k)
15029 {
15030  bool result = (l.get_size_in_bits() == r.get_size_in_bits()
15032  if (!result)
15033  if (k)
15034  *k |= LOCAL_TYPE_CHANGE_KIND;
15035  ABG_RETURN(result);
15036 }
15037 
15038 /// Return true iff both type declarations are equal.
15039 ///
15040 /// Note that this doesn't test if the scopes of both types are equal.
15041 bool
15043 {return equals(*this, other, 0);}
15044 
15045 /// Inequality operator.
15046 ///
15047 ///@param other the instance of @ref type_base to compare the current
15048 /// instance against.
15049 ///
15050 /// @return true iff the current instance is different from @p other.
15051 bool
15053 {return !operator==(other);}
15054 
15055 /// Setter for the size of the type.
15056 ///
15057 /// @param s the new size -- in bits.
15058 void
15060 {priv_->size_in_bits = s;}
15061 
15062 /// Getter for the size of the type.
15063 ///
15064 /// @return the size in bits of the type.
15065 size_t
15067 {return priv_->size_in_bits;}
15068 
15069 /// Setter for the alignment of the type.
15070 ///
15071 /// @param a the new alignment -- in bits.
15072 void
15074 {priv_->alignment_in_bits = a;}
15075 
15076 /// Getter for the alignment of the type.
15077 ///
15078 /// @return the alignment of the type in bits.
15079 size_t
15081 {return priv_->alignment_in_bits;}
15082 
15083 /// Default implementation of traversal for types. This function does
15084 /// nothing. It must be implemented by every single new type that is
15085 /// written.
15086 ///
15087 /// Please look at e.g, class_decl::traverse() for an example of how
15088 /// to implement this.
15089 ///
15090 /// @param v the visitor used to visit the type.
15091 bool
15093 {
15094  if (v.type_node_has_been_visited(this))
15095  return true;
15096 
15097  v.visit_begin(this);
15098  bool result = v.visit_end(this);
15099  v.mark_type_node_as_visited(this);
15100 
15101  return result;
15102 }
15103 
15104 type_base::~type_base()
15105 {delete priv_;}
15106 
15107 // </type_base definitions>
15108 
15109 // <integral_type definitions>
15110 
15111 /// Bitwise OR operator for integral_type::modifiers_type.
15112 ///
15113 /// @param l the left-hand side operand.
15114 ///
15115 /// @param r the right-hand side operand.
15116 ///
15117 /// @return the result of the bitwise OR.
15120 {
15121  return static_cast<integral_type::modifiers_type>(static_cast<unsigned>(l)
15122  |
15123  static_cast<unsigned>(r));
15124 }
15125 
15126 /// Bitwise AND operator for integral_type::modifiers_type.
15127 ///
15128 /// @param l the left-hand side operand.
15129 ///
15130 /// @param r the right-hand side operand.
15131 ///
15132 /// @return the result of the bitwise AND.
15135 {
15136  return static_cast<integral_type::modifiers_type>(static_cast<unsigned>(l)
15137  &
15138  static_cast<unsigned>(r));
15139 }
15140 
15141 /// Bitwise one's complement operator for integral_type::modifiers_type.
15142 ///
15143 /// @param l the left-hand side operand.
15144 ///
15145 /// @param r the right-hand side operand.
15146 ///
15147 /// @return the result of the bitwise one's complement operator.
15150 {
15151  return static_cast<integral_type::modifiers_type>(~static_cast<unsigned>(l));
15152 }
15153 
15154 /// Bitwise |= operator for integral_type::modifiers_type.
15155 ///
15156 /// @param l the left-hand side operand.
15157 ///
15158 /// @param r the right-hand side operand.
15159 ///
15160 /// @return the result of the bitwise |=.
15163 {
15164  l = l | r;
15165  return l;
15166 }
15167 
15168 /// Bitwise &= operator for integral_type::modifiers_type.
15169 ///
15170 /// @param l the left-hand side operand.
15171 ///
15172 /// @param r the right-hand side operand.
15173 ///
15174 /// @return the result of the bitwise &=.
15177 {
15178  l = l & r;
15179  return l;
15180 }
15181 
15182 /// Parse a word containing one integral type modifier.
15183 ///
15184 /// A word is considered to be a string of characters that doesn't
15185 /// contain any white space.
15186 ///
15187 /// @param word the word to parse. It is considered to be a string of
15188 /// characters that doesn't contain any white space.
15189 ///
15190 /// @param modifiers out parameter. It's set by this function to the
15191 /// parsed modifier iff the function returned true.
15192 ///
15193 /// @return true iff @word was successfully parsed.
15194 static bool
15195 parse_integral_type_modifier(const string& word,
15196  integral_type::modifiers_type &modifiers)
15197 {
15198  if (word == "signed")
15199  modifiers |= integral_type::SIGNED_MODIFIER;
15200  else if (word == "unsigned")
15201  modifiers |= integral_type::UNSIGNED_MODIFIER;
15202  else if (word == "short")
15203  modifiers |= integral_type::SHORT_MODIFIER;
15204  else if (word == "long")
15205  modifiers |= integral_type::LONG_MODIFIER;
15206  else if (word == "long long")
15207  modifiers |= integral_type::LONG_LONG_MODIFIER;
15208  else
15209  return false;
15210 
15211  return true;
15212 }
15213 
15214 /// Parse a base type of an integral type from a string.
15215 ///
15216 /// @param type_name the type name to parse.
15217 ///
15218 /// @param base out parameter. This is set to the resulting base type
15219 /// parsed, iff the function returned true.
15220 ///
15221 /// @return true iff the function could successfully parse the base
15222 /// type.
15223 static bool
15224 parse_base_integral_type(const string& type_name,
15226 {
15227  if (type_name == "int")
15229  else if (type_name == "char")
15231  else if (type_name == "bool" || type_name == "_Bool")
15233  else if (type_name == "double")
15235  else if (type_name =="float")
15237  else if (type_name == "char16_t")
15239  else if (type_name == "char32_t")
15241  else if (type_name == "wchar_t")
15243  else
15244  return false;
15245 
15246  return true;
15247 }
15248 
15249 /// Parse an integral type from a string.
15250 ///
15251 /// @param type_name the string containing the integral type to parse.
15252 ///
15253 /// @param base out parameter. Is set by this function to the base
15254 /// type of the integral type, iff the function returned true.
15255 ///
15256 /// @param modifiers out parameter If set by this function to the
15257 /// modifier of the integral type, iff the function returned true.
15258 ///
15259 /// @return true iff the function could parse an integral type from @p
15260 /// type_name.
15261 static bool
15262 parse_integral_type(const string& type_name,
15264  integral_type::modifiers_type& modifiers)
15265 {
15266  string input = type_name;
15267  string::size_type len = input.length();
15268  string::size_type cur_pos = 0, prev_pos = 0;
15269  string cur_word, prev_word;
15270  bool ok = false;
15271 
15272  while (cur_pos < len)
15273  {
15274  if (cur_pos < len && isspace(input[cur_pos]))
15275  do
15276  ++cur_pos;
15277  while (cur_pos < len && isspace(input[cur_pos]));
15278 
15279  prev_pos = cur_pos;
15280  cur_pos = input.find(' ', prev_pos);
15281  prev_word = cur_word;
15282  cur_word = input.substr(prev_pos, cur_pos - prev_pos);
15283 
15284  if (cur_pos < len
15285  && cur_word == "long"
15286  && prev_word != "long")
15287  {
15288  if (cur_pos < len && isspace(input[cur_pos]))
15289  do
15290  ++cur_pos;
15291  while (cur_pos < len && isspace(input[cur_pos]));
15292  prev_pos = cur_pos;
15293 
15294  cur_pos = input.find(' ', prev_pos);
15295  string saved_prev_word = prev_word;
15296  prev_word = cur_word;
15297  cur_word = input.substr(prev_pos, cur_pos - prev_pos);
15298  if (cur_word == "long")
15299  cur_word = "long long";
15300  else
15301  {
15302  cur_pos = prev_pos;
15303  cur_word = prev_word;
15304  prev_word = saved_prev_word;
15305  }
15306  }
15307 
15308  if (!parse_integral_type_modifier(cur_word, modifiers))
15309  {
15310  if (!parse_base_integral_type(cur_word, base))
15311  return false;
15312  else
15313  ok = true;
15314  }
15315  else
15316  ok = true;
15317  }
15318 
15319  return ok;
15320 }
15321 
15322 /// Parse an integral type from a string.
15323 ///
15324 /// @param str the string containing the integral type to parse.
15325 ///
15326 ///@param type the resulting @ref integral_type. Is set to the result
15327 ///of the parse, iff the function returns true.
15328 ///
15329 /// @return true iff the function could parse an integral type from @p
15330 /// str.
15331 bool
15332 parse_integral_type(const string& str, integral_type& type)
15333 {
15335  integral_type::modifiers_type modifiers = integral_type::NO_MODIFIER;
15336 
15337  if (!parse_integral_type(str, base_type, modifiers))
15338  return false;
15339 
15340  // So this is an integral type.
15341  integral_type int_type(base_type, modifiers);
15342  type = int_type;
15343  return true;
15344 }
15345 
15346 /// Default constructor of the @ref integral_type.
15348  : base_(INT_BASE_TYPE),
15349  modifiers_(NO_MODIFIER)
15350 {}
15351 
15352 /// Constructor of the @ref integral_type.
15353 ///
15354 /// @param b the base type of the integral type.
15355 ///
15356 /// @param m the modifiers of the integral type.
15358  : base_(b), modifiers_(m)
15359 {}
15360 
15361 /// Constructor of the @ref integral_type.
15362 ///
15363 /// @param the name of the integral type to parse to initialize the
15364 /// current instance of @ref integral_type.
15365 integral_type::integral_type(const string& type_name)
15366  : base_(INT_BASE_TYPE),
15367  modifiers_(NO_MODIFIER)
15368 {
15369  bool could_parse = parse_integral_type(type_name, base_, modifiers_);
15370  ABG_ASSERT(could_parse);
15371 }
15372 
15373 /// Getter of the base type of the @ref integral_type.
15374 ///
15375 /// @return the base type of the @ref integral_type.
15378 {return base_;}
15379 
15380 /// Getter of the modifiers bitmap of the @ref integral_type.
15381 ///
15382 /// @return the modifiers bitmap of the @ref integral_type.
15385 {return modifiers_;}
15386 
15387 /// Setter of the modifiers bitmap of the @ref integral_type.
15388 ///
15389 /// @param m the new modifiers.
15390 void
15392 {modifiers_ = m;}
15393 
15394 /// Equality operator for the @ref integral_type.
15395 ///
15396 /// @param other the other integral type to compare against.
15397 ///
15398 /// @return true iff @p other equals the current instance of @ref
15399 /// integral_type.
15400 bool
15402 {return base_ == other.base_ && modifiers_ == other.modifiers_;}
15403 
15404 /// Return the string representation of the current instance of @ref
15405 /// integral_type.
15406 ///
15407 /// @param internal if true the string representation is to be used
15408 /// for internal purposes. In general, it means it's for type
15409 /// canonicalization purposes.
15410 ///
15411 /// @return the string representation of the current instance of @ref
15412 /// integral_type.
15413 string
15414 integral_type::to_string(bool internal) const
15415 {
15416  string result;
15417 
15418  // Look at modifiers ...
15419  if (modifiers_ & SIGNED_MODIFIER)
15420  result += "signed ";
15421  if (modifiers_ & UNSIGNED_MODIFIER)
15422  result += "unsigned ";
15423  if (!internal)
15424  {
15425  // For canonicalization purposes, we won't emit the "short, long, or
15426  // long long" modifiers. This is because on some platforms, "long
15427  // int" and "long long int" might have the same size. In those
15428  // cases, we want the two types to be equivalent if they have the
15429  // same size. If they don't have the same internal string
15430  // representation, they'd automatically have different canonical
15431  // types and thus be canonically different.
15432  if (modifiers_ & SHORT_MODIFIER)
15433  result += "short ";
15434  if (modifiers_ & LONG_MODIFIER)
15435  result += "long ";
15436  if (modifiers_ & LONG_LONG_MODIFIER)
15437  result += "long long ";
15438  }
15439 
15440  // ... and look at base types.
15441  if (base_ == INT_BASE_TYPE)
15442  result += "int";
15443  else if (base_ == CHAR_BASE_TYPE)
15444  result += "char";
15445  else if (base_ == BOOL_BASE_TYPE)
15446  result += "bool";
15447  else if (base_ == DOUBLE_BASE_TYPE)
15448  result += "double";
15449  else if (base_ == FLOAT_BASE_TYPE)
15450  result += "float";
15451  else if (base_ == CHAR16_T_BASE_TYPE)
15452  result += "char16_t";
15453  else if (base_ == CHAR32_T_BASE_TYPE)
15454  result += "char32_t";
15455  else if (base_ == WCHAR_T_BASE_TYPE)
15456  result += "wchar_t";
15457 
15458  return result;
15459 }
15460 
15461 /// Convert the current instance of @ref integral_type into its string
15462 /// representation.
15463 ///
15464 /// @return the string representation of the current instance of @ref
15465 /// integral_type.
15466 integral_type::operator string() const
15467 {return to_string();}
15468 
15469 // </integral_type definitions>
15470 
15471 //<type_decl definitions>
15472 
15473 /// Constructor.
15474 ///
15475 /// @param env the environment we are operating from.
15476 ///
15477 /// @param name the name of the type declaration.
15478 ///
15479 /// @param size_in_bits the size of the current type_decl, in bits.
15480 ///
15481 /// @param alignment_in_bits the alignment of the current typ, in
15482 /// bits.
15483 ///
15484 /// @param locus the source location of the current type declaration.
15485 ///
15486 /// @param linkage_name the linkage_name of the current type declaration.
15487 ///
15488 /// @param vis the visibility of the type declaration.
15489 type_decl::type_decl(const environment& env,
15490  const string& name,
15491  size_t size_in_bits,
15492  size_t alignment_in_bits,
15493  const location& locus,
15494  const string& linkage_name,
15495  visibility vis)
15496 
15497  : type_or_decl_base(env,
15498  BASIC_TYPE
15499  | ABSTRACT_TYPE_BASE
15500  | ABSTRACT_DECL_BASE),
15501  decl_base(env, name, locus, linkage_name, vis),
15502  type_base(env, size_in_bits, alignment_in_bits)
15503 {
15504  runtime_type_instance(this);
15505 
15507  integral_type::modifiers_type modifiers = integral_type::NO_MODIFIER;
15508  integral_type int_type(base_type, modifiers);
15509  if (parse_integral_type(name, int_type))
15510  {
15511  // Convert the integral_type into its canonical string
15512  // representation.
15513  string integral_type_name = int_type;
15514 
15515  // Set the name of this type_decl to the canonical string
15516  // representation above
15517  set_name(integral_type_name);
15519 
15520  if (!get_linkage_name().empty())
15521  set_linkage_name(integral_type_name);
15522  }
15523 }
15524 
15525 /// Compares two instances of @ref type_decl.
15526 ///
15527 /// If the two intances are different, set a bitfield to give some
15528 /// insight about the kind of differences there are.
15529 ///
15530 /// @param l the first artifact of the comparison.
15531 ///
15532 /// @param r the second artifact of the comparison.
15533 ///
15534 /// @param k a pointer to a bitfield that gives information about the
15535 /// kind of changes there are between @p l and @p r. This one is set
15536 /// iff @p k is non-null and the function returns false.
15537 ///
15538 /// Please note that setting k to a non-null value does have a
15539 /// negative performance impact because even if @p l and @p r are not
15540 /// equal, the function keeps up the comparison in order to determine
15541 /// the different kinds of ways in which they are different.
15542 ///
15543 /// @return true if @p l equals @p r, false otherwise.
15544 bool
15545 equals(const type_decl& l, const type_decl& r, change_kind* k)
15546 {
15547  bool result = false;
15548 
15549  // Consider the types as decls to compare their decls-related
15550  // properties.
15551  result = equals(static_cast<const decl_base&>(l),
15552  static_cast<const decl_base&>(r),
15553  k);
15554  if (!k && !result)
15556 
15557  // Now consider the types a "types' to compare their size-related
15558  // properties.
15559  result &= equals(static_cast<const type_base&>(l),
15560  static_cast<const type_base&>(r),
15561  k);
15562  ABG_RETURN(result);
15563 }
15564 
15565 /// Return true if both types equals.
15566 ///
15567 /// This operator re-uses the overload that takes a decl_base.
15568 ///
15569 /// Note that this does not check the scopes of any of the types.
15570 ///
15571 /// @param o the other type_decl to check agains.
15572 bool
15574 {
15575  const decl_base* other = dynamic_cast<const decl_base*>(&o);
15576  if (!other)
15577  return false;
15578  return *this == *other;
15579 }
15580 
15581 /// Return true if both types equals.
15582 ///
15583 /// Note that this does not check the scopes of any of the types.
15584 ///
15585 /// @param o the other type_decl to check against.
15586 bool
15588 {
15589  const type_decl* other = dynamic_cast<const type_decl*>(&o);
15590  if (!other)
15591  return false;
15592  return try_canonical_compare(this, other);
15593 }
15594 
15595 /// Return true if both types equals.
15596 ///
15597 /// Note that this does not check the scopes of any of the types.
15598 ///
15599 /// @param o the other type_decl to check against.
15600 ///
15601 /// @return true iff the current isntance equals @p o
15602 bool
15604 {
15605  const decl_base& other = o;
15606  return *this == other;
15607 }
15608 
15609 /// Return true if both types equals.
15610 ///
15611 /// Note that this does not check the scopes of any of the types.
15612 ///
15613 /// @param o the other type_decl to check against.
15614 ///
15615 /// @return true iff the current isntance equals @p o
15616 bool
15618 {return !operator==(o);}
15619 
15620 /// Return true if both types equals.
15621 ///
15622 /// Note that this does not check the scopes of any of the types.
15623 ///
15624 /// @param o the other type_decl to check against.
15625 ///
15626 /// @return true iff the current isntance equals @p o
15627 bool
15629 {return !operator==(o);}
15630 
15631 /// Inequality operator.
15632 ///
15633 /// @param o the other type to compare against.
15634 ///
15635 /// @return true iff the current instance is different from @p o.
15636 bool
15638 {return !operator==(o);}
15639 
15640 /// Equality operator for @ref type_decl_sptr.
15641 ///
15642 /// @param l the first operand to compare.
15643 ///
15644 /// @param r the second operand to compare.
15645 ///
15646 /// @return true iff @p l equals @p r.
15647 bool
15649 {
15650  if (!!l != !!r)
15651  return false;
15652  if (l.get() == r.get())
15653  return true;
15654  return *l == *r;
15655 }
15656 
15657 /// Inequality operator for @ref type_decl_sptr.
15658 ///
15659 /// @param l the first operand to compare.
15660 ///
15661 /// @param r the second operand to compare.
15662 ///
15663 /// @return true iff @p l is different from @p r.
15664 bool
15666 {return !operator==(l, r);}
15667 
15668 /// Implementation for the virtual qualified name builder for @ref
15669 /// type_decl.
15670 ///
15671 /// @param qualified_name the output parameter to hold the resulting
15672 /// qualified name.
15673 ///
15674 /// @param internal set to true if the call is intended for an
15675 /// internal use (for technical use inside the library itself), false
15676 /// otherwise. If you don't know what this is for, then set it to
15677 /// false.
15678 void
15680  bool internal) const
15681 {qualified_name = get_qualified_name(internal);}
15682 
15683 /// Implementation for the virtual qualified name builder for @ref
15684 /// type_decl.
15685 ///
15686 /// @param qualified_name the output parameter to hold the resulting
15687 /// qualified name.
15688 ///
15689 /// @param internal set to true if the call is intended for an
15690 /// internal use (for technical use inside the library itself), false
15691 /// otherwise. If you don't know what this is for, then set it to
15692 /// false.
15693 const interned_string&
15694 type_decl::get_qualified_name(bool internal) const
15695 {
15696  const environment& env = get_environment();
15697 
15698 
15699  if (internal)
15700  if (is_integral_type(this))
15701  {
15703  {
15704  if (decl_base::priv_->internal_qualified_name_.empty())
15705  decl_base::priv_->internal_qualified_name_ =
15706  env.intern(get_internal_integral_type_name(this));
15707  return decl_base::priv_->internal_qualified_name_;
15708  }
15709  else
15710  {
15711  decl_base::priv_->temporary_internal_qualified_name_ =
15712  env.intern(get_internal_integral_type_name(this));
15713  return decl_base::priv_->temporary_internal_qualified_name_;
15714  }
15715  }
15716 
15717  return decl_base::get_qualified_name(/*internal=*/false);
15718 }
15719 
15720 /// Get the pretty representation of the current instance of @ref
15721 /// type_decl.
15722 ///
15723 /// @param internal set to true if the call is intended to get a
15724 /// representation of the decl (or type) for the purpose of canonical
15725 /// type comparison. This is mainly used in the function
15726 /// type_base::get_canonical_type_for().
15727 ///
15728 /// In other words if the argument for this parameter is true then the
15729 /// call is meant for internal use (for technical use inside the
15730 /// library itself), false otherwise. If you don't know what this is
15731 /// for, then set it to false.
15732 ///
15733 /// @param qualified_name if true, names emitted in the pretty
15734 /// representation are fully qualified.
15735 ///
15736 /// @return the pretty representatin of the @ref type_decl.
15737 string
15739  bool qualified_name) const
15740 {
15741  if (internal)
15742  if (is_integral_type(this))
15743  return get_internal_integral_type_name(this);
15744 
15745  if (qualified_name)
15746  return get_qualified_name(internal);
15747  return get_name();
15748 }
15749 
15750 /// This implements the ir_traversable_base::traverse pure virtual
15751 /// function.
15752 ///
15753 /// @param v the visitor used on the current instance.
15754 ///
15755 /// @return true if the entire IR node tree got traversed, false
15756 /// otherwise.
15757 bool
15759 {
15760  if (v.type_node_has_been_visited(this))
15761  return true;
15762 
15763  v.visit_begin(this);
15764  bool result = v.visit_end(this);
15765  v.mark_type_node_as_visited(this);
15766 
15767  return result;
15768 }
15769 
15770 type_decl::~type_decl()
15771 {}
15772 //</type_decl definitions>
15773 
15774 // <scope_type_decl definitions>
15775 
15776 /// Constructor.
15777 ///
15778 /// @param env the environment we are operating from.
15779 ///
15780 /// @param name the name of the type.
15781 ///
15782 /// @param size_in_bits the size of the type, in bits.
15783 ///
15784 /// @param alignment_in_bits the alignment of the type, in bits.
15785 ///
15786 /// @param locus the source location where the type is defined.
15787 ///
15788 /// @param vis the visibility of the type.
15789 scope_type_decl::scope_type_decl(const environment& env,
15790  const string& name,
15791  size_t size_in_bits,
15792  size_t alignment_in_bits,
15793  const location& locus,
15794  visibility vis)
15795  : type_or_decl_base(env,
15796  ABSTRACT_SCOPE_TYPE_DECL
15797  | ABSTRACT_TYPE_BASE
15798  | ABSTRACT_DECL_BASE),
15799  decl_base(env, name, locus, "", vis),
15800  type_base(env, size_in_bits, alignment_in_bits),
15801  scope_decl(env, name, locus)
15802 {}
15803 
15804 /// Compares two instances of @ref scope_type_decl.
15805 ///
15806 /// If the two intances are different, set a bitfield to give some
15807 /// insight about the kind of differences there are.
15808 ///
15809 /// @param l the first artifact of the comparison.
15810 ///
15811 /// @param r the second artifact of the comparison.
15812 ///
15813 /// @param k a pointer to a bitfield that gives information about the
15814 /// kind of changes there are between @p l and @p r. This one is set
15815 /// iff @p k is non-null and the function returns false.
15816 ///
15817 /// Please note that setting k to a non-null value does have a
15818 /// negative performance impact because even if @p l and @p r are not
15819 /// equal, the function keeps up the comparison in order to determine
15820 /// the different kinds of ways in which they are different.
15821 ///
15822 /// @return true if @p l equals @p r, false otherwise.
15823 bool
15825 {
15826  bool result = equals(static_cast<const scope_decl&>(l),
15827  static_cast<const scope_decl&>(r),
15828  k);
15829 
15830  if (!k && !result)
15832 
15833  result &= equals(static_cast<const type_base&>(l),
15834  static_cast<const type_base&>(r),
15835  k);
15836 
15837  ABG_RETURN(result);
15838 }
15839 
15840 /// Equality operator between two scope_type_decl.
15841 ///
15842 /// Note that this function does not consider the scope of the scope
15843 /// types themselves.
15844 ///
15845 /// @return true iff both scope types are equal.
15846 bool
15848 {
15849  const scope_type_decl* other = dynamic_cast<const scope_type_decl*>(&o);
15850  if (!other)
15851  return false;
15852  return try_canonical_compare(this, other);
15853 }
15854 
15855 /// Equality operator between two scope_type_decl.
15856 ///
15857 /// This re-uses the equality operator that takes a decl_base.
15858 ///
15859 /// @param o the other scope_type_decl to compare against.
15860 ///
15861 /// @return true iff both scope types are equal.
15862 bool
15864 {
15865  const decl_base* other = dynamic_cast<const decl_base*>(&o);
15866  if (!other)
15867  return false;
15868 
15869  return *this == *other;
15870 }
15871 
15872 /// Traverses an instance of @ref scope_type_decl, visiting all the
15873 /// sub-types and decls that it might contain.
15874 ///
15875 /// @param v the visitor that is used to visit every IR sub-node of
15876 /// the current node.
15877 ///
15878 /// @return true if either
15879 /// - all the children nodes of the current IR node were traversed
15880 /// and the calling code should keep going with the traversing.
15881 /// - or the current IR node is already being traversed.
15882 /// Otherwise, returning false means that the calling code should not
15883 /// keep traversing the tree.
15884 bool
15886 {
15887  if (visiting())
15888  return true;
15889 
15890  if (v.type_node_has_been_visited(this))
15891  return true;
15892 
15893  if (v.visit_begin(this))
15894  {
15895  visiting(true);
15896  for (scope_decl::declarations::const_iterator i =
15897  get_member_decls().begin();
15898  i != get_member_decls ().end();
15899  ++i)
15900  if (!(*i)->traverse(v))
15901  break;
15902  visiting(false);
15903  }
15904 
15905  bool result = v.visit_end(this);
15906  v.mark_type_node_as_visited(this);
15907 
15908  return result;
15909 }
15910 
15911 scope_type_decl::~scope_type_decl()
15912 {}
15913 // </scope_type_decl definitions>
15914 
15915 // <namespace_decl>
15916 
15917 /// Constructor.
15918 ///
15919 /// @param the environment we are operatin from.
15920 ///
15921 /// @param name the name of the namespace.
15922 ///
15923 /// @param locus the source location where the namespace is defined.
15924 ///
15925 /// @param vis the visibility of the namespace.
15927  const string& name,
15928  const location& locus,
15929  visibility vis)
15930  // We need to call the constructor of decl_base directly here
15931  // because it is virtually inherited by scope_decl. Note that we
15932  // just implicitely call the default constructor for scope_decl
15933  // here, as what we really want is to initialize the decl_base
15934  // subobject. Wow, virtual inheritance is useful, but setting it
15935  // up is ugly.
15936  : type_or_decl_base(env,
15937  NAMESPACE_DECL
15938  | ABSTRACT_DECL_BASE
15939  | ABSTRACT_SCOPE_DECL),
15940  decl_base(env, name, locus, "", vis),
15941  scope_decl(env, name, locus)
15942 {
15943  runtime_type_instance(this);
15944 }
15945 
15946 /// Build and return a copy of the pretty representation of the
15947 /// namespace.
15948 ///
15949 /// @param internal set to true if the call is intended to get a
15950 /// representation of the decl (or type) for the purpose of canonical
15951 /// type comparison. This is mainly used in the function
15952 /// type_base::get_canonical_type_for().
15953 ///
15954 /// In other words if the argument for this parameter is true then the
15955 /// call is meant for internal use (for technical use inside the
15956 /// library itself), false otherwise. If you don't know what this is
15957 /// for, then set it to false.
15958 ///
15959 /// @param qualified_name if true, names emitted in the pretty
15960 /// representation are fully qualified.
15961 ///
15962 /// @return a copy of the pretty representation of the namespace.
15963 string
15965  bool qualified_name) const
15966 {
15967  string r =
15968  "namespace " + scope_decl::get_pretty_representation(internal,
15969  qualified_name);
15970  return r;
15971 }
15972 
15973 /// Return true iff both namespaces and their members are equal.
15974 ///
15975 /// Note that this function does not check if the scope of these
15976 /// namespaces are equal.
15977 bool
15979 {
15980  const namespace_decl* other = dynamic_cast<const namespace_decl*>(&o);
15981  if (!other)
15982  return false;
15983  return scope_decl::operator==(*other);
15984 }
15985 
15986 /// Test if the current namespace_decl is empty or contains empty
15987 /// namespaces itself.
15988 ///
15989 /// @return true iff the current namespace_decl is empty or contains
15990 /// empty itself.
15991 bool
15993 {
15994  if (is_empty())
15995  return true;
15996 
15997  for (declarations::const_iterator i = get_member_decls().begin();
15998  i != get_member_decls().end();
15999  ++i)
16000  {
16001  if (!is_namespace(*i))
16002  return false;
16003 
16005  ABG_ASSERT(ns);
16006 
16007  if (!ns->is_empty_or_has_empty_sub_namespaces())
16008  return false;
16009  }
16010 
16011  return true;
16012 }
16013 
16014 /// This implements the ir_traversable_base::traverse pure virtual
16015 /// function.
16016 ///
16017 /// @param v the visitor used on the current instance and on its
16018 /// member nodes.
16019 ///
16020 /// @return true if the entire IR node tree got traversed, false
16021 /// otherwise.
16022 bool
16024 {
16025  if (visiting())
16026  return true;
16027 
16028  if (v.visit_begin(this))
16029  {
16030  visiting(true);
16031  scope_decl::declarations::const_iterator i;
16032  for (i = get_member_decls().begin();
16033  i != get_member_decls ().end();
16034  ++i)
16035  {
16037  dynamic_pointer_cast<ir_traversable_base>(*i);
16038  if (t)
16039  if (!t->traverse (v))
16040  break;
16041  }
16042  visiting(false);
16043  }
16044  return v.visit_end(this);
16045 }
16046 
16047 namespace_decl::~namespace_decl()
16048 {
16049 }
16050 
16051 // </namespace_decl>
16052 
16053 // <qualified_type_def>
16054 
16055 /// Type of the private data of qualified_type_def.
16056 class qualified_type_def::priv
16057 {
16058  friend class qualified_type_def;
16059 
16060  qualified_type_def::CV cv_quals_;
16061  // Before the type is canonicalized, this is used as a temporary
16062  // internal name.
16063  interned_string temporary_internal_name_;
16064  // Once the type is canonicalized, this is used as the internal
16065  // name.
16066  interned_string internal_name_;
16067  weak_ptr<type_base> underlying_type_;
16068 
16069  priv()
16070  : cv_quals_(CV_NONE)
16071  {}
16072 
16073  priv(qualified_type_def::CV quals,
16074  type_base_sptr t)
16075  : cv_quals_(quals),
16076  underlying_type_(t)
16077  {}
16078 
16079  priv(qualified_type_def::CV quals)
16080  : cv_quals_(quals)
16081  {}
16082 };// end class qualified_type_def::priv
16083 
16084 /// Build the name of the current instance of qualified type.
16085 ///
16086 /// @param fully_qualified if true, build a fully qualified name.
16087 ///
16088 /// @param internal set to true if the call is intended for an
16089 /// internal use (for technical use inside the library itself), false
16090 /// otherwise. If you don't know what this is for, then set it to
16091 /// false.
16092 ///
16093 /// @return a copy of the newly-built name.
16094 string
16095 qualified_type_def::build_name(bool fully_qualified, bool internal) const
16096 {
16097  type_base_sptr t = get_underlying_type();
16098  if (!t)
16099  // The qualified type might temporarily have no underlying type,
16100  // especially during the construction of the type, while the
16101  // underlying type is not yet constructed. In that case, let's do
16102  // like if the underlying type is the 'void' type.
16104 
16106  fully_qualified,
16107  internal);
16108 }
16109 
16110 /// This function is automatically invoked whenever an instance of
16111 /// this type is canonicalized.
16112 ///
16113 /// It's an overload of the virtual type_base::on_canonical_type_set.
16114 ///
16115 /// We put here what is thus meant to be executed only at the point of
16116 /// type canonicalization.
16117 void
16120 
16121 /// Constructor of the qualified_type_def
16122 ///
16123 /// @param type the underlying type
16124 ///
16125 /// @param quals a bitfield representing the const/volatile qualifiers
16126 ///
16127 /// @param locus the location of the qualified type definition
16128 qualified_type_def::qualified_type_def(type_base_sptr type,
16129  CV quals,
16130  const location& locus)
16131  : type_or_decl_base(type->get_environment(),
16132  QUALIFIED_TYPE
16133  | ABSTRACT_TYPE_BASE
16134  | ABSTRACT_DECL_BASE),
16135  type_base(type->get_environment(), type->get_size_in_bits(),
16136  type->get_alignment_in_bits()),
16137  decl_base(type->get_environment(), "", locus, "",
16138  dynamic_pointer_cast<decl_base>(type)->get_visibility()),
16139  priv_(new priv(quals, type))
16140 {
16141  runtime_type_instance(this);
16142  interned_string name = type->get_environment().intern(build_name(false));
16143  set_name(name);
16144 }
16145 
16146 /// Constructor of the qualified_type_def
16147 ///
16148 /// @param env the environment of the type.
16149 ///
16150 /// @param quals a bitfield representing the const/volatile qualifiers
16151 ///
16152 /// @param locus the location of the qualified type definition
16153 qualified_type_def::qualified_type_def(const environment& env,
16154  CV quals,
16155  const location& locus)
16156  : type_or_decl_base(env,
16157  QUALIFIED_TYPE
16158  | ABSTRACT_TYPE_BASE
16159  | ABSTRACT_DECL_BASE),
16160  type_base(env, /*size_in_bits=*/0,
16161  /*alignment_in_bits=*/0),
16162  decl_base(env, "", locus, ""),
16163  priv_(new priv(quals))
16164 {
16165  runtime_type_instance(this);
16166  // We don't yet have an underlying type. So for naming purpose,
16167  // let's temporarily pretend the underlying type is 'void'.
16168  interned_string name = env.intern("void");
16169  set_name(name);
16170 }
16171 
16172 /// Get the size of the qualified type def.
16173 ///
16174 /// This is an overload for type_base::get_size_in_bits().
16175 ///
16176 /// @return the size of the qualified type.
16177 size_t
16179 {
16180  size_t s = 0;
16181  if (type_base_sptr ut = get_underlying_type())
16182  {
16183  // We do have the underlying type properly set, so let's make
16184  // the size of the qualified type match the size of its
16185  // underlying type.
16186  s = ut->get_size_in_bits();
16187  if (s != type_base::get_size_in_bits())
16188  const_cast<qualified_type_def*>(this)->set_size_in_bits(s);
16189  }
16190  return type_base::get_size_in_bits();
16191 }
16192 
16193 /// Compares two instances of @ref qualified_type_def.
16194 ///
16195 /// If the two intances are different, set a bitfield to give some
16196 /// insight about the kind of differences there are.
16197 ///
16198 /// @param l the first artifact of the comparison.
16199 ///
16200 /// @param r the second artifact of the comparison.
16201 ///
16202 /// @param k a pointer to a bitfield that gives information about the
16203 /// kind of changes there are between @p l and @p r. This one is set
16204 /// iff @p k is non-null and the function returns false.
16205 ///
16206 /// Please note that setting k to a non-null value does have a
16207 /// negative performance impact because even if @p l and @p r are not
16208 /// equal, the function keeps up the comparison in order to determine
16209 /// the different kinds of ways in which they are different.
16210 ///
16211 /// @return true if @p l equals @p r, false otherwise.
16212 bool
16214 {
16215  bool result = true;
16216  if (l.get_cv_quals() != r.get_cv_quals())
16217  {
16218  result = false;
16219  if (k)
16221  else
16223  }
16224 
16226  {
16227  result = false;
16228  if (k)
16229  {
16231  r.get_underlying_type().get()))
16232  // Underlying type changes in which the structure of the
16233  // type changed are considered local changes to the
16234  // qualified type.
16235  *k |= LOCAL_TYPE_CHANGE_KIND;
16236  else
16237  *k |= SUBTYPE_CHANGE_KIND;
16238  }
16239  else
16240  // okay strictly speaking this is not necessary, but I am
16241  // putting it here to maintenance; that is, so that adding
16242  // subsequent clauses needed to compare two qualified types
16243  // later still works.
16245  }
16246 
16247  ABG_RETURN(result);
16248 }
16249 
16250 /// Equality operator for qualified types.
16251 ///
16252 /// Note that this function does not check for equality of the scopes.
16253 ///
16254 ///@param o the other qualified type to compare against.
16255 ///
16256 /// @return true iff both qualified types are equal.
16257 bool
16259 {
16260  const qualified_type_def* other =
16261  dynamic_cast<const qualified_type_def*>(&o);
16262  if (!other)
16263  return false;
16264  return try_canonical_compare(this, other);
16265 }
16266 
16267 /// Equality operator for qualified types.
16268 ///
16269 /// Note that this function does not check for equality of the scopes.
16270 /// Also, this re-uses the equality operator above that takes a
16271 /// decl_base.
16272 ///
16273 ///@param o the other qualified type to compare against.
16274 ///
16275 /// @return true iff both qualified types are equal.
16276 bool
16278 {
16279  const decl_base* other = dynamic_cast<const decl_base*>(&o);
16280  if (!other)
16281  return false;
16282  return *this == *other;
16283 }
16284 
16285 /// Equality operator for qualified types.
16286 ///
16287 /// Note that this function does not check for equality of the scopes.
16288 /// Also, this re-uses the equality operator above that takes a
16289 /// decl_base.
16290 ///
16291 ///@param o the other qualified type to compare against.
16292 ///
16293 /// @return true iff both qualified types are equal.
16294 bool
16296 {
16297  const decl_base* other = dynamic_cast<const decl_base*>(&o);
16298  if (!other)
16299  return false;
16300  return *this == *other;
16301 }
16302 
16303 /// Implementation for the virtual qualified name builder for @ref
16304 /// qualified_type_def.
16305 ///
16306 /// @param qualified_name the output parameter to hold the resulting
16307 /// qualified name.
16308 ///
16309 /// @param internal set to true if the call is intended for an
16310 /// internal use (for technical use inside the library itself), false
16311 /// otherwise. If you don't know what this is for, then set it to
16312 /// false.
16313 void
16315  bool internal) const
16316 {qualified_name = get_qualified_name(internal);}
16317 
16318 /// Implementation of the virtual qualified name builder/getter.
16319 ///
16320 /// @param internal set to true if the call is intended for an
16321 /// internal use (for technical use inside the library itself), false
16322 /// otherwise. If you don't know what this is for, then set it to
16323 /// false.
16324 ///
16325 /// @return the resulting qualified name.
16326 const interned_string&
16328 {
16329  const environment& env = get_environment();
16330 
16331 
16332  if (!get_canonical_type())
16333  {
16334  // The type hasn't been canonicalized yet. We want to return a
16335  // temporary name that is not cached because the structure of
16336  // this type (and so its name) can change until its
16337  // canonicalized.
16338  if (internal)
16339  {
16340  // We are asked to return a temporary *internal* name.
16341  // Lets compute it and return a reference to where it's
16342  // stored.
16343  priv_->temporary_internal_name_ =
16344  env.intern(build_name(true, /*internal=*/true));
16345  return priv_->temporary_internal_name_;
16346  }
16347  else
16348  {
16349  // We are asked to return a temporary non-internal name.
16351  (env.intern(build_name(true, /*internal=*/false)));
16353  }
16354  }
16355  else
16356  {
16357  // The type has already been canonicalized. We want to return
16358  // the definitive name and cache it.
16359  if (internal)
16360  {
16361  if (priv_->internal_name_.empty())
16362  priv_->internal_name_ =
16363  env.intern(build_name(/*qualified=*/true,
16364  /*internal=*/true));
16365  return priv_->internal_name_;
16366  }
16367  else
16368  {
16369  if (peek_qualified_name().empty())
16371  (env.intern(build_name(/*qualified=*/true,
16372  /*internal=*/false)));
16373  return peek_qualified_name();
16374  }
16375  }
16376 }
16377 
16378 /// This implements the ir_traversable_base::traverse pure virtual
16379 /// function.
16380 ///
16381 /// @param v the visitor used on the current instance.
16382 ///
16383 /// @return true if the entire IR node tree got traversed, false
16384 /// otherwise.
16385 bool
16387 {
16388  if (v.type_node_has_been_visited(this))
16389  return true;
16390 
16391  if (visiting())
16392  return true;
16393 
16394  if (v.visit_begin(this))
16395  {
16396  visiting(true);
16397  if (type_base_sptr t = get_underlying_type())
16398  t->traverse(v);
16399  visiting(false);
16400  }
16401  bool result = v.visit_end(this);
16402  v.mark_type_node_as_visited(this);
16403  return result;
16404 }
16405 
16406 qualified_type_def::~qualified_type_def()
16407 {
16408 }
16409 
16410 /// Getter of the const/volatile qualifier bit field
16413 {return priv_->cv_quals_;}
16414 
16415 /// Setter of the const/value qualifiers bit field
16416 void
16418 {priv_->cv_quals_ = cv_quals;}
16419 
16420 /// Compute and return the string prefix or suffix representing the
16421 /// qualifiers hold by the current instance of @ref
16422 /// qualified_type_def.
16423 ///
16424 /// @return the newly-built cv string.
16425 string
16427 {return get_string_representation_of_cv_quals(priv_->cv_quals_);}
16428 
16429 /// Getter of the underlying type
16430 type_base_sptr
16432 {return priv_->underlying_type_.lock();}
16433 
16434 /// Setter of the underlying type.
16435 ///
16436 /// @param t the new underlying type.
16437 void
16439 {
16440  ABG_ASSERT(t);
16441  priv_->underlying_type_ = t;
16442  // Now we need to update other properties that depend on the new underlying type.
16443  set_size_in_bits(t->get_size_in_bits());
16444  set_alignment_in_bits(t->get_alignment_in_bits());
16446  set_name(name);
16447  if (scope_decl* s = get_scope())
16448  {
16449  // Now that the name has been updated, we need to update the
16450  // lookup maps accordingly.
16451  scope_decl::declarations::iterator i;
16452  if (s->find_iterator_for_member(this, i))
16454  else
16456  }
16457 }
16458 
16459 /// Non-member equality operator for @ref qualified_type_def
16460 ///
16461 /// @param l the left-hand side of the equality operator
16462 ///
16463 /// @param r the right-hand side of the equality operator
16464 ///
16465 /// @return true iff @p l and @p r equals.
16466 bool
16467 operator==(const qualified_type_def_sptr& l, const qualified_type_def_sptr& r)
16468 {
16469  if (l.get() == r.get())
16470  return true;
16471  if (!!l != !!r)
16472  return false;
16473 
16474  return *l == *r;
16475 }
16476 
16477 /// Non-member inequality operator for @ref qualified_type_def
16478 ///
16479 /// @param l the left-hand side of the equality operator
16480 ///
16481 /// @param r the right-hand side of the equality operator
16482 ///
16483 /// @return true iff @p l and @p r equals.
16484 bool
16485 operator!=(const qualified_type_def_sptr& l, const qualified_type_def_sptr& r)
16486 {return ! operator==(l, r);}
16487 
16488 /// Overloaded bitwise OR operator for cv qualifiers.
16491 {
16492  return static_cast<qualified_type_def::CV>
16493  (static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
16494 }
16495 
16496 /// Overloaded bitwise |= operator for cv qualifiers.
16499 {
16500  l = l | r;
16501  return l;
16502 }
16503 
16504 /// Overloaded bitwise &= operator for cv qualifiers.
16507 {
16508  l = l & r;
16509  return l;
16510 }
16511 
16512 /// Overloaded bitwise AND operator for CV qualifiers.
16515 {
16516  return static_cast<qualified_type_def::CV>
16517  (static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
16518 }
16519 
16520 /// Overloaded bitwise inverting operator for CV qualifiers.
16523 {return static_cast<qualified_type_def::CV>(~static_cast<unsigned>(q));}
16524 
16525 /// Streaming operator for qualified_type_decl::CV
16526 ///
16527 /// @param o the output stream to serialize the cv qualifier to.
16528 ///
16529 /// @param cv the cv qualifier to serialize.
16530 ///
16531 /// @return the output stream used.
16532 std::ostream&
16533 operator<<(std::ostream& o, qualified_type_def::CV cv)
16534 {
16535  string str;
16536 
16537  switch (cv)
16538  {
16539  case qualified_type_def::CV_NONE:
16540  str = "none";
16541  break;
16542  case qualified_type_def::CV_CONST:
16543  str = "const";
16544  break;
16545  case qualified_type_def::CV_VOLATILE:
16546  str = "volatile";
16547  break;
16548  case qualified_type_def::CV_RESTRICT:
16549  str = "restrict";
16550  break;
16551  }
16552 
16553  o << str;
16554  return o;
16555 }
16556 
16557 // </qualified_type_def>
16558 
16559 //<pointer_type_def definitions>
16560 
16561 /// Private data structure of the @ref pointer_type_def.
16562 struct pointer_type_def::priv
16563 {
16564  type_base_wptr pointed_to_type_;
16565  type_base* naked_pointed_to_type_;
16566  interned_string internal_qualified_name_;
16567  interned_string temp_internal_qualified_name_;
16568 
16569  priv(const type_base_sptr& t)
16570  : pointed_to_type_(type_or_void(t, t->get_environment())),
16571  naked_pointed_to_type_(t.get())
16572  {}
16573 
16574  priv()
16575  : naked_pointed_to_type_()
16576  {}
16577 }; //end struct pointer_type_def
16578 
16579 /// This function is automatically invoked whenever an instance of
16580 /// this type is canonicalized.
16581 ///
16582 /// It's an overload of the virtual type_base::on_canonical_type_set.
16583 ///
16584 /// We put here what is thus meant to be executed only at the point of
16585 /// type canonicalization.
16586 void
16589 
16590 
16591 ///Constructor of @ref pointer_type_def.
16592 ///
16593 /// @param pointed_to the pointed-to type.
16594 ///
16595 /// @param size_in_bits the size of the type, in bits.
16596 ///
16597 /// @param align_in_bits the alignment of the type, in bits.
16598 ///
16599 /// @param locus the source location where the type was defined.
16600 pointer_type_def::pointer_type_def(const type_base_sptr& pointed_to,
16601  size_t size_in_bits,
16602  size_t align_in_bits,
16603  const location& locus)
16604  : type_or_decl_base(pointed_to->get_environment(),
16605  POINTER_TYPE
16606  | ABSTRACT_TYPE_BASE
16607  | ABSTRACT_DECL_BASE),
16608  type_base(pointed_to->get_environment(), size_in_bits, align_in_bits),
16609  decl_base(pointed_to->get_environment(), "", locus, ""),
16610  priv_(new priv(pointed_to))
16611 {
16612  runtime_type_instance(this);
16613  try
16614  {
16615  ABG_ASSERT(pointed_to);
16616  const environment& env = pointed_to->get_environment();
16617  decl_base_sptr pto = dynamic_pointer_cast<decl_base>(pointed_to);
16618  string name = (pto ? pto->get_name() : string("void")) + "*";
16619  set_name(env.intern(name));
16620  if (pto)
16621  set_visibility(pto->get_visibility());
16622  }
16623  catch (...)
16624  {}
16625 }
16626 
16627 ///Constructor of @ref pointer_type_def.
16628 ///
16629 /// @param env the environment of the type.
16630 ///
16631 /// @param size_in_bits the size of the type, in bits.
16632 ///
16633 /// @param align_in_bits the alignment of the type, in bits.
16634 ///
16635 /// @param locus the source location where the type was defined.
16636 pointer_type_def::pointer_type_def(const environment& env, size_t size_in_bits,
16637  size_t alignment_in_bits,
16638  const location& locus)
16639  : type_or_decl_base(env,
16640  POINTER_TYPE
16641  | ABSTRACT_TYPE_BASE
16642  | ABSTRACT_DECL_BASE),
16643  type_base(env, size_in_bits, alignment_in_bits),
16644  decl_base(env, "", locus, ""),
16645  priv_(new priv())
16646 {
16647  runtime_type_instance(this);
16648  string name = string("void") + "*";
16649  set_name(env.intern(name));
16650 }
16651 
16652 /// Set the pointed-to type of the pointer.
16653 ///
16654 /// @param t the new pointed-to type.
16655 void
16657 {
16658  ABG_ASSERT(t);
16659  priv_->pointed_to_type_ = t;
16660  priv_->naked_pointed_to_type_ = t.get();
16661 
16662  try
16663  {
16664  const environment& env = t->get_environment();
16665  decl_base_sptr pto = dynamic_pointer_cast<decl_base>(t);
16666  string name = (pto ? pto->get_name() : string("void")) + "*";
16667  set_name(env.intern(name));
16668  if (pto)
16669  set_visibility(pto->get_visibility());
16670  }
16671  catch (...)
16672  {}
16673 }
16674 
16675 /// Compares two instances of @ref pointer_type_def.
16676 ///
16677 /// If the two intances are different, set a bitfield to give some
16678 /// insight about the kind of differences there are.
16679 ///
16680 /// @param l the first artifact of the comparison.
16681 ///
16682 /// @param r the second artifact of the comparison.
16683 ///
16684 /// @param k a pointer to a bitfield that gives information about the
16685 /// kind of changes there are between @p l and @p r. This one is set
16686 /// iff @p k is non-null and the function returns false.
16687 ///
16688 /// Please note that setting k to a non-null value does have a
16689 /// negative performance impact because even if @p l and @p r are not
16690 /// equal, the function keeps up the comparison in order to determine
16691 /// the different kinds of ways in which they are different.
16692 ///
16693 /// @return true if @p l equals @p r, false otherwise.
16694 bool
16696 {
16697  // In C and C++ languages, a pointer to void equals all other
16698  // pointers.
16699  if (l.get_translation_unit()
16700  && r.get_translation_unit()
16704  return true;
16705 
16706  bool result = l.get_pointed_to_type() == r.get_pointed_to_type();
16707  if (!result)
16708  if (k)
16709  {
16710  if (!types_have_similar_structure(&l, &r))
16711  // pointed-to type changes in which the structure of the
16712  // type changed are considered local changes to the pointer
16713  // type.
16714  *k |= LOCAL_TYPE_CHANGE_KIND;
16715  *k |= SUBTYPE_CHANGE_KIND;
16716  }
16717 
16718  ABG_RETURN(result);
16719 }
16720 
16721 /// Return true iff both instances of pointer_type_def are equal.
16722 ///
16723 /// Note that this function does not check for the scopes of the this
16724 /// types.
16725 bool
16727 {
16728  const pointer_type_def* other = is_pointer_type(&o);
16729  if (!other)
16730  return false;
16731  return try_canonical_compare(this, other);
16732 }
16733 
16734 /// Return true iff both instances of pointer_type_def are equal.
16735 ///
16736 /// Note that this function does not check for the scopes of the
16737 /// types.
16738 ///
16739 /// @param other the other type to compare against.
16740 ///
16741 /// @return true iff @p other equals the current instance.
16742 bool
16744 {
16745  const decl_base* o = is_decl(&other);
16746  if (!o)
16747  return false;
16748  return *this == *o;
16749 }
16750 
16751 /// Return true iff both instances of pointer_type_def are equal.
16752 ///
16753 /// Note that this function does not check for the scopes of the
16754 /// types.
16755 ///
16756 /// @param other the other type to compare against.
16757 ///
16758 /// @return true iff @p other equals the current instance.
16759 bool
16761 {
16762  const decl_base& o = other;
16763  return *this == o;
16764 }
16765 
16766 /// Getter of the pointed-to type.
16767 ///
16768 /// @return the pointed-to type.
16769 const type_base_sptr
16771 {return priv_->pointed_to_type_.lock();}
16772 
16773 /// Getter of a naked pointer to the pointed-to type.
16774 ///
16775 /// @return a naked pointed to the pointed-to type.
16776 type_base*
16778 {return priv_->naked_pointed_to_type_;}
16779 
16780 /// Build and return the qualified name of the current instance of
16781 /// @ref pointer_type_def.
16782 ///
16783 /// @param qn output parameter. The resulting qualified name.
16784 ///
16785 /// @param internal set to true if the call is intended for an
16786 /// internal use (for technical use inside the library itself), false
16787 /// otherwise. If you don't know what this is for, then set it to
16788 /// false.
16789 void
16791 {qn = get_qualified_name(internal);}
16792 
16793 /// Build, cache and return the qualified name of the current instance
16794 /// of @ref pointer_type_def. Subsequent invocations of this function
16795 /// return the cached value.
16796 ///
16797 /// Note that this function should work even if the underlying type is
16798 /// momentarily empty.
16799 ///
16800 /// @param internal set to true if the call is intended for an
16801 /// internal use (for technical use inside the library itself), false
16802 /// otherwise. If you don't know what this is for, then set it to
16803 /// false.
16804 ///
16805 /// @return the resulting qualified name.
16806 const interned_string&
16808 {
16809  type_base* pointed_to_type = get_naked_pointed_to_type();
16810  pointed_to_type = look_through_decl_only(pointed_to_type);
16811 
16812  if (internal)
16813  {
16814  if (get_canonical_type())
16815  {
16816  if (priv_->internal_qualified_name_.empty())
16817  if (pointed_to_type)
16818  priv_->internal_qualified_name_ =
16819  get_name_of_pointer_to_type(*pointed_to_type,
16820  /*qualified_name=*/true,
16821  /*internal=*/true);
16822  return priv_->internal_qualified_name_;
16823  }
16824  else
16825  {
16826  // As the type hasn't yet been canonicalized, its structure
16827  // (and so its name) can change. So let's invalidate the
16828  // cache where we store its name at each invocation of this
16829  // function.
16830  if (pointed_to_type)
16831  priv_->temp_internal_qualified_name_ =
16832  get_name_of_pointer_to_type(*pointed_to_type,
16833  /*qualified_name=*/true,
16834  /*internal=*/true);
16835  return priv_->temp_internal_qualified_name_;
16836  }
16837  }
16838  else
16839  {
16841  {
16842  if (decl_base::peek_qualified_name().empty())
16844  (get_name_of_pointer_to_type(*pointed_to_type,
16845  /*qualified_name=*/true,
16846  /*internal=*/false));
16848  }
16849  else
16850  {
16851  // As the type hasn't yet been canonicalized, its structure
16852  // (and so its name) can change. So let's invalidate the
16853  // cache where we store its name at each invocation of this
16854  // function.
16855  if (pointed_to_type)
16857  (get_name_of_pointer_to_type(*pointed_to_type,
16858  /*qualified_name=*/true,
16859  /*internal=*/false));
16861  }
16862  }
16863 }
16864 
16865 /// This implements the ir_traversable_base::traverse pure virtual
16866 /// function.
16867 ///
16868 /// @param v the visitor used on the current instance.
16869 ///
16870 /// @return true if the entire IR node tree got traversed, false
16871 /// otherwise.
16872 bool
16874 {
16875  if (v.type_node_has_been_visited(this))
16876  return true;
16877 
16878  if (visiting())
16879  return true;
16880 
16881  if (v.visit_begin(this))
16882  {
16883  visiting(true);
16884  if (type_base_sptr t = get_pointed_to_type())
16885  t->traverse(v);
16886  visiting(false);
16887  }
16888 
16889  bool result = v.visit_end(this);
16890  v.mark_type_node_as_visited(this);
16891  return result;
16892 }
16893 
16894 pointer_type_def::~pointer_type_def()
16895 {}
16896 
16897 /// Turn equality of shared_ptr of @ref pointer_type_def into a deep
16898 /// equality; that is, make it compare the pointed to objects too.
16899 ///
16900 /// @param l the shared_ptr of @ref pointer_type_def on left-hand-side
16901 /// of the equality.
16902 ///
16903 /// @param r the shared_ptr of @ref pointer_type_def on
16904 /// right-hand-side of the equality.
16905 ///
16906 /// @return true if the @ref pointer_type_def pointed to by the
16907 /// shared_ptrs are equal, false otherwise.
16908 bool
16910 {
16911  if (l.get() == r.get())
16912  return true;
16913  if (!!l != !!r)
16914  return false;
16915 
16916  return *l == *r;
16917 }
16918 
16919 /// Turn inequality of shared_ptr of @ref pointer_type_def into a deep
16920 /// equality; that is, make it compare the pointed to objects too.
16921 ///
16922 /// @param l the shared_ptr of @ref pointer_type_def on left-hand-side
16923 /// of the equality.
16924 ///
16925 /// @param r the shared_ptr of @ref pointer_type_def on
16926 /// right-hand-side of the equality.
16927 ///
16928 /// @return true iff the @ref pointer_type_def pointed to by the
16929 /// shared_ptrs are different.
16930 bool
16932 {return !operator==(l, r);}
16933 
16934 // </pointer_type_def definitions>
16935 
16936 // <reference_type_def definitions>
16937 
16938 /// This function is automatically invoked whenever an instance of
16939 /// this type is canonicalized.
16940 ///
16941 /// It's an overload of the virtual type_base::on_canonical_type_set.
16942 ///
16943 /// We put here what is thus meant to be executed only at the point of
16944 /// type canonicalization.
16945 void
16948 
16949 /// Constructor of the reference_type_def type.
16950 ///
16951 /// @param pointed_to the pointed to type.
16952 ///
16953 /// @param lvalue wether the reference is an lvalue reference. If
16954 /// false, the reference is an rvalue one.
16955 ///
16956 /// @param size_in_bits the size of the type, in bits.
16957 ///
16958 /// @param align_in_bits the alignment of the type, in bits.
16959 ///
16960 /// @param locus the source location of the type.
16961 reference_type_def::reference_type_def(const type_base_sptr pointed_to,
16962  bool lvalue,
16963  size_t size_in_bits,
16964  size_t align_in_bits,
16965  const location& locus)
16966  : type_or_decl_base(pointed_to->get_environment(),
16967  REFERENCE_TYPE
16968  | ABSTRACT_TYPE_BASE
16969  | ABSTRACT_DECL_BASE),
16970  type_base(pointed_to->get_environment(), size_in_bits, align_in_bits),
16971  decl_base(pointed_to->get_environment(), "", locus, ""),
16972  is_lvalue_(lvalue)
16973 {
16974  runtime_type_instance(this);
16975 
16976  try
16977  {
16978  decl_base_sptr pto = dynamic_pointer_cast<decl_base>(pointed_to);
16979  string name;
16980  if (pto)
16981  {
16982  set_visibility(pto->get_visibility());
16983  name = string(pto->get_name()) + "&";
16984  }
16985  else
16986  name = string(get_type_name(is_function_type(pointed_to),
16987  /*qualified_name=*/true)) + "&";
16988 
16989  if (!is_lvalue())
16990  name += "&";
16991  const environment& env = pointed_to->get_environment();
16992  set_name(env.intern(name));
16993 
16994  pointed_to_type_ =
16995  type_base_wptr(type_or_void(pointed_to,
16996  pointed_to->get_environment()));
16997  }
16998  catch (...)
16999  {}
17000 }
17001 
17002 /// Constructor of the reference_type_def type.
17003 ///
17004 /// This one creates a type that has no pointed-to type, temporarily.
17005 /// This is useful for cases where the underlying type is not yet
17006 /// available. It can be set later using
17007 /// reference_type_def::set_pointed_to_type().
17008 ///
17009 /// @param env the environment of the type.
17010 ///
17011 /// @param lvalue wether the reference is an lvalue reference. If
17012 /// false, the reference is an rvalue one.
17013 ///
17014 /// @param size_in_bits the size of the type, in bits.
17015 ///
17016 /// @param align_in_bits the alignment of the type, in bits.
17017 ///
17018 /// @param locus the source location of the type.
17019 reference_type_def::reference_type_def(const environment& env, bool lvalue,
17020  size_t size_in_bits,
17021  size_t alignment_in_bits,
17022  const location& locus)
17023  : type_or_decl_base(env,
17024  REFERENCE_TYPE
17025  | ABSTRACT_TYPE_BASE
17026  | ABSTRACT_DECL_BASE),
17027  type_base(env, size_in_bits, alignment_in_bits),
17028  decl_base(env, "", locus, ""),
17029  is_lvalue_(lvalue)
17030 {
17031  runtime_type_instance(this);
17032  string name = "void&";
17033  if (!is_lvalue())
17034  name += "&";
17035 
17036  set_name(env.intern(name));
17037  pointed_to_type_ = type_base_wptr(env.get_void_type());
17038 }
17039 
17040 /// Setter of the pointed_to type of the current reference type.
17041 ///
17042 /// @param pointed_to the new pointed to type.
17043 void
17044 reference_type_def::set_pointed_to_type(type_base_sptr& pointed_to_type)
17045 {
17046  ABG_ASSERT(pointed_to_type);
17047  pointed_to_type_ = pointed_to_type;
17048 
17049  decl_base_sptr pto;
17050  try
17051  {pto = dynamic_pointer_cast<decl_base>(pointed_to_type);}
17052  catch (...)
17053  {}
17054 
17055  if (pto)
17056  {
17057  set_visibility(pto->get_visibility());
17058  string name = string(pto->get_name()) + "&";
17059  if (!is_lvalue())
17060  name += "&";
17061  const environment& env = pto->get_environment();
17062  set_name(env.intern(name));
17063  }
17064 }
17065 
17066 /// Compares two instances of @ref reference_type_def.
17067 ///
17068 /// If the two intances are different, set a bitfield to give some
17069 /// insight about the kind of differences there are.
17070 ///
17071 /// @param l the first artifact of the comparison.
17072 ///
17073 /// @param r the second artifact of the comparison.
17074 ///
17075 /// @param k a pointer to a bitfield that gives information about the
17076 /// kind of changes there are between @p l and @p r. This one is set
17077 /// iff @p k is non-null and the function returns false.
17078 ///
17079 /// Please note that setting k to a non-null value does have a
17080 /// negative performance impact because even if @p l and @p r are not
17081 /// equal, the function keeps up the comparison in order to determine
17082 /// the different kinds of ways in which they are different.
17083 ///
17084 /// @return true if @p l equals @p r, false otherwise.
17085 bool
17087 {
17088  if (l.is_lvalue() != r.is_lvalue())
17089  {
17090  if (k)
17091  *k |= LOCAL_TYPE_CHANGE_KIND;
17093  }
17094 
17095  // Compare the pointed-to-types modulo the typedefs they might have
17096  bool result = (l.get_pointed_to_type() == r.get_pointed_to_type());
17097  if (!result)
17098  if (k)
17099  {
17100  if (!types_have_similar_structure(&l, &r))
17101  *k |= LOCAL_TYPE_CHANGE_KIND;
17102  *k |= SUBTYPE_CHANGE_KIND;
17103  }
17104  ABG_RETURN(result);
17105 }
17106 
17107 /// Equality operator of the @ref reference_type_def type.
17108 ///
17109 /// @param o the other instance of @ref reference_type_def to compare
17110 /// against.
17111 ///
17112 /// @return true iff the two instances are equal.
17113 bool
17115 {
17116  const reference_type_def* other =
17117  dynamic_cast<const reference_type_def*>(&o);
17118  if (!other)
17119  return false;
17120  return try_canonical_compare(this, other);
17121 }
17122 
17123 /// Equality operator of the @ref reference_type_def type.
17124 ///
17125 /// @param o the other instance of @ref reference_type_def to compare
17126 /// against.
17127 ///
17128 /// @return true iff the two instances are equal.
17129 bool
17131 {
17132  const decl_base* other = dynamic_cast<const decl_base*>(&o);
17133  if (!other)
17134  return false;
17135  return *this == *other;
17136 }
17137 
17138 /// Equality operator of the @ref reference_type_def type.
17139 ///
17140 /// @param o the other instance of @ref reference_type_def to compare
17141 /// against.
17142 ///
17143 /// @return true iff the two instances are equal.
17144 bool
17146 {
17147  const decl_base* other = dynamic_cast<const decl_base*>(&o);
17148  if (!other)
17149  return false;
17150  return *this == *other;
17151 }
17152 
17153 type_base_sptr
17154 reference_type_def::get_pointed_to_type() const
17155 {return pointed_to_type_.lock();}
17156 
17157 bool
17158 reference_type_def::is_lvalue() const
17159 {return is_lvalue_;}
17160 
17161 /// Build and return the qualified name of the current instance of the
17162 /// @ref reference_type_def.
17163 ///
17164 /// @param qn output parameter. Is set to the newly-built qualified
17165 /// name of the current instance of @ref reference_type_def.
17166 ///
17167 /// @param internal set to true if the call is intended for an
17168 /// internal use (for technical use inside the library itself), false
17169 /// otherwise. If you don't know what this is for, then set it to
17170 /// false.
17171 void
17173 {qn = get_qualified_name(internal);}
17174 
17175 /// Build, cache and return the qualified name of the current instance
17176 /// of the @ref reference_type_def. Subsequent invocations of this
17177 /// function return the cached value.
17178 ///
17179 /// @param internal set to true if the call is intended for an
17180 /// internal use (for technical use inside the library itself), false
17181 /// otherwise. If you don't know what this is for, then set it to
17182 /// false.
17183 ///
17184 /// @return the newly-built qualified name of the current instance of
17185 /// @ref reference_type_def.
17186 const interned_string&
17188 {
17189  if (peek_qualified_name().empty()
17190  || !get_canonical_type())
17192  (*look_through_decl_only(get_pointed_to_type()),
17193  is_lvalue(),
17194  /*qualified_name=*/true,
17195  internal));
17196  return peek_qualified_name();
17197 }
17198 
17199 /// Get the pretty representation of the current instance of @ref
17200 /// reference_type_def.
17201 ///
17202 /// @param internal set to true if the call is intended to get a
17203 /// representation of the decl (or type) for the purpose of canonical
17204 /// type comparison. This is mainly used in the function
17205 /// type_base::get_canonical_type_for().
17206 ///
17207 /// In other words if the argument for this parameter is true then the
17208 /// call is meant for internal use (for technical use inside the
17209 /// library itself), false otherwise. If you don't know what this is
17210 /// for, then set it to false.
17211 ///
17212 /// @param qualified_name if true, names emitted in the pretty
17213 /// representation are fully qualified.
17214 ///
17215 /// @return the pretty representatin of the @ref reference_type_def.
17216 string
17218  bool qualified_name) const
17219 {
17220  string result =
17222  (get_pointed_to_type()),
17223  is_lvalue(),
17224  qualified_name,
17225  internal);
17226 
17227  return result;
17228 }
17229 
17230 /// This implements the ir_traversable_base::traverse pure virtual
17231 /// function.
17232 ///
17233 /// @param v the visitor used on the current instance.
17234 ///
17235 /// @return true if the entire IR node tree got traversed, false
17236 /// otherwise.
17237 bool
17239 {
17240  if (v.type_node_has_been_visited(this))
17241  return true;
17242 
17243  if (visiting())
17244  return true;
17245 
17246  if (v.visit_begin(this))
17247  {
17248  visiting(true);
17249  if (type_base_sptr t = get_pointed_to_type())
17250  t->traverse(v);
17251  visiting(false);
17252  }
17253 
17254  bool result = v.visit_end(this);
17255  v.mark_type_node_as_visited(this);
17256  return result;
17257 }
17258 
17259 reference_type_def::~reference_type_def()
17260 {}
17261 
17262 /// Turn equality of shared_ptr of @ref reference_type_def into a deep
17263 /// equality; that is, make it compare the pointed to objects too.
17264 ///
17265 /// @param l the shared_ptr of @ref reference_type_def on left-hand-side
17266 /// of the equality.
17267 ///
17268 /// @param r the shared_ptr of @ref reference_type_def on
17269 /// right-hand-side of the equality.
17270 ///
17271 /// @return true if the @ref reference_type_def pointed to by the
17272 /// shared_ptrs are equal, false otherwise.
17273 bool
17275 {
17276  if (l.get() == r.get())
17277  return true;
17278  if (!!l != !!r)
17279  return false;
17280 
17281  return *l == *r;
17282 }
17283 
17284 /// Turn inequality of shared_ptr of @ref reference_type_def into a deep
17285 /// equality; that is, make it compare the pointed to objects too.
17286 ///
17287 /// @param l the shared_ptr of @ref reference_type_def on left-hand-side
17288 /// of the equality.
17289 ///
17290 /// @param r the shared_ptr of @ref reference_type_def on
17291 /// right-hand-side of the equality.
17292 ///
17293 /// @return true iff the @ref reference_type_def pointed to by the
17294 /// shared_ptrs are different.
17295 bool
17297 {return !operator==(l, r);}
17298 
17299 // </reference_type_def definitions>
17300 
17301 // <array_type_def definitions>
17302 
17303 // <array_type_def::subrange_type>
17304 array_type_def::subrange_type::~subrange_type() = default;
17305 
17306 // <array_type_def::subrante_type::bound_value>
17307 
17308 /// Default constructor of the @ref
17309 /// array_type_def::subrange_type::bound_value class.
17310 ///
17311 /// Constructs an unsigned bound_value of value zero.
17313  : s_(UNSIGNED_SIGNEDNESS)
17314 {
17315  v_.unsigned_ = 0;
17316 }
17317 
17318 /// Initialize an unsigned bound_value with a given value.
17319 ///
17320 /// @param v the initial bound value.
17322  : s_(UNSIGNED_SIGNEDNESS)
17323 {
17324  v_.unsigned_ = v;
17325 }
17326 
17327 /// Initialize a signed bound_value with a given value.
17328 ///
17329 /// @param v the initial bound value.
17331  : s_(SIGNED_SIGNEDNESS)
17332 {
17333  v_.signed_ = v;
17334 }
17335 
17336 /// Getter of the signedness (unsigned VS signed) of the bound value.
17337 ///
17338 /// @return the signedness of the bound value.
17339 enum array_type_def::subrange_type::bound_value::signedness
17341 {return s_;}
17342 
17343 /// Setter of the signedness (unsigned VS signed) of the bound value.
17344 ///
17345 /// @param s the new signedness of the bound value.
17346 void
17348 { s_ = s;}
17349 
17350 /// Getter of the bound value as a signed value.
17351 ///
17352 /// @return the bound value as signed.
17353 int64_t
17355 {return v_.signed_;
17356 }
17357 
17358 /// Getter of the bound value as an unsigned value.
17359 ///
17360 /// @return the bound value as unsigned.
17361 uint64_t
17363 {return v_.unsigned_;}
17364 
17365 /// Setter of the bound value as unsigned.
17366 ///
17367 /// @param v the new unsigned value.
17368 void
17370 {
17371  s_ = UNSIGNED_SIGNEDNESS;
17372  v_.unsigned_ = v;
17373 }
17374 
17375 /// Setter of the bound value as signed.
17376 ///
17377 /// @param v the new signed value.
17378 void
17380 {
17381  s_ = SIGNED_SIGNEDNESS;
17382  v_.signed_ = v;
17383 }
17384 
17385 /// Equality operator of the bound value.
17386 ///
17387 /// @param v the other bound value to compare with.
17388 ///
17389 /// @return true iff the current bound value equals @p v.
17390 bool
17392 {
17393  return s_ == v.s_ && v_.unsigned_ == v.v_.unsigned_;
17394 }
17395 
17396 // </array_type_def::subrante_type::bound_value>
17397 
17398 struct array_type_def::subrange_type::priv
17399 {
17400  bound_value lower_bound_;
17401  bound_value upper_bound_;
17402  type_base_wptr underlying_type_;
17403  translation_unit::language language_;
17404  bool infinite_;
17405 
17406  priv(bound_value ub,
17407  translation_unit::language l = translation_unit::LANG_C11)
17408  : upper_bound_(ub), language_(l), infinite_(false)
17409  {}
17410 
17411  priv(bound_value lb, bound_value ub,
17412  translation_unit::language l = translation_unit::LANG_C11)
17413  : lower_bound_(lb), upper_bound_(ub),
17414  language_(l), infinite_(false)
17415  {}
17416 
17417  priv(bound_value lb, bound_value ub, const type_base_sptr &u,
17418  translation_unit::language l = translation_unit::LANG_C11)
17419  : lower_bound_(lb), upper_bound_(ub), underlying_type_(u),
17420  language_(l), infinite_(false)
17421  {}
17422 };
17423 
17424 /// Constructor of an array_type_def::subrange_type type.
17425 ///
17426 /// @param env the environment this type was created from.
17427 ///
17428 /// @param name the name of the subrange type.
17429 ///
17430 /// @param lower_bound the lower bound of the array. This is
17431 /// generally zero (at least for C and C++).
17432 ///
17433 /// @param upper_bound the upper bound of the array.
17434 ///
17435 /// @param underlying_type the underlying type of the subrange type.
17436 ///
17437 /// @param loc the source location where the type is defined.
17438 array_type_def::subrange_type::subrange_type(const environment& env,
17439  const string& name,
17440  bound_value lower_bound,
17441  bound_value upper_bound,
17442  const type_base_sptr& utype,
17443  const location& loc,
17445  : type_or_decl_base(env, ABSTRACT_TYPE_BASE | ABSTRACT_DECL_BASE),
17446  type_base(env,
17447  upper_bound.get_unsigned_value()
17448  - lower_bound.get_unsigned_value(),
17449  0),
17450  decl_base(env, name, loc, ""),
17451  priv_(new priv(lower_bound, upper_bound, utype, l))
17452 {
17453  runtime_type_instance(this);
17454 }
17455 
17456 /// Constructor of the array_type_def::subrange_type type.
17457 ///
17458 /// @param env the environment this type is being created in.
17459 ///
17460 /// @param name the name of the subrange type.
17461 ///
17462 /// @param lower_bound the lower bound of the array. This is
17463 /// generally zero (at least for C and C++).
17464 ///
17465 /// @param upper_bound the upper bound of the array.
17466 ///
17467 /// @param loc the source location where the type is defined.
17468 ///
17469 /// @param l the language that generated this subrange.
17470 array_type_def::subrange_type::subrange_type(const environment& env,
17471  const string& name,
17472  bound_value lower_bound,
17473  bound_value upper_bound,
17474  const location& loc,
17476  : type_or_decl_base(env, ABSTRACT_TYPE_BASE | ABSTRACT_DECL_BASE),
17477  type_base(env,
17478  upper_bound.get_unsigned_value()
17479  - lower_bound.get_unsigned_value(), 0),
17480  decl_base(env, name, loc, ""),
17481  priv_(new priv(lower_bound, upper_bound, l))
17482 {
17483  runtime_type_instance(this);
17484 }
17485 
17486 /// Constructor of the array_type_def::subrange_type type.
17487 ///
17488 /// @param env the environment this type is being created from.
17489 ///
17490 /// @param name of the name of type.
17491 ///
17492 /// @param upper_bound the upper bound of the array. The lower bound
17493 /// is considered to be zero.
17494 ///
17495 /// @param loc the source location of the type.
17496 ///
17497 /// @param the language that generated this type.
17498 array_type_def::subrange_type::subrange_type(const environment& env,
17499  const string& name,
17500  bound_value upper_bound,
17501  const location& loc,
17503  : type_or_decl_base(env, ABSTRACT_TYPE_BASE | ABSTRACT_DECL_BASE),
17504  type_base(env, upper_bound.get_unsigned_value(), 0),
17505  decl_base(env, name, loc, ""),
17506  priv_(new priv(upper_bound, l))
17507 {
17508  runtime_type_instance(this);
17509 }
17510 
17511 /// Getter of the underlying type of the subrange, that is, the type
17512 /// that defines the range.
17513 ///
17514 /// @return the underlying type.
17515 type_base_sptr
17517 {return priv_->underlying_type_.lock();}
17518 
17519 /// Setter of the underlying type of the subrange, that is, the type
17520 /// that defines the range.
17521 ///
17522 /// @param u the new underlying type.
17523 void
17525 {
17526  ABG_ASSERT(priv_->underlying_type_.expired());
17527  priv_->underlying_type_ = u;
17528 }
17529 
17530 /// Getter of the upper bound of the subrange type.
17531 ///
17532 /// @return the upper bound of the subrange type.
17533 int64_t
17535 {return priv_->upper_bound_.get_signed_value();}
17536 
17537 /// Getter of the lower bound of the subrange type.
17538 ///
17539 /// @return the lower bound of the subrange type.
17540 int64_t
17542 {return priv_->lower_bound_.get_signed_value();}
17543 
17544 /// Setter of the upper bound of the subrange type.
17545 ///
17546 /// @param ub the new value of the upper bound.
17547 void
17549 {priv_->upper_bound_ = ub;}
17550 
17551 /// Setter of the lower bound.
17552 ///
17553 /// @param lb the new value of the lower bound.
17554 void
17556 {priv_->lower_bound_ = lb;}
17557 
17558 /// Getter of the length of the subrange type.
17559 ///
17560 /// Note that a length of zero means the array has an infinite (or
17561 /// rather a non-known) size.
17562 ///
17563 /// @return the length of the subrange type.
17564 uint64_t
17566 {
17567  if (is_infinite())
17568  return 0;
17569 
17570  // A subrange can have an upper bound that is lower than its lower
17571  // bound. This is possible in Ada for instance. In that case, the
17572  // length of the subrange is considered to be zero.
17573  if (get_upper_bound() >= get_lower_bound())
17574  return get_upper_bound() - get_lower_bound() + 1;
17575  return 0;
17576 }
17577 
17578 /// Test if the length of the subrange type is infinite.
17579 ///
17580 /// @return true iff the length of the subrange type is infinite.
17581 bool
17583 {return priv_->infinite_;}
17584 
17585 /// Set the infinite-ness status of the subrange type.
17586 ///
17587 /// @param f true iff the length of the subrange type should be set to
17588 /// being infinite.
17589 void
17591 {priv_->infinite_ = f;}
17592 
17593 /// Getter of the language that generated this type.
17594 ///
17595 /// @return the language of this type.
17598 {return priv_->language_;}
17599 
17600 /// Return a string representation of the sub range.
17601 ///
17602 /// @return the string representation of the sub range.
17603 string
17605 {
17606  std::ostringstream o;
17607 
17609  {
17610  type_base_sptr underlying_type = get_underlying_type();
17611  if (underlying_type)
17612  o << ir::get_pretty_representation(underlying_type, false) << " ";
17613  o << "range "<< get_lower_bound() << " .. " << get_upper_bound();
17614  }
17615  else if (is_infinite())
17616  o << "[]";
17617  else
17618  o << "[" << get_length() << "]";
17619 
17620  return o.str();
17621 }
17622 
17623 /// Return a string representation of a vector of subranges
17624 ///
17625 /// @return the string representation of a vector of sub ranges.
17626 string
17628 {
17629  if (v.empty())
17630  return "[]";
17631 
17632  string r;
17633  for (vector<subrange_sptr>::const_iterator i = v.begin();
17634  i != v.end();
17635  ++i)
17636  r += (*i)->as_string();
17637 
17638  return r;
17639 }
17640 
17641 /// Compares two isntances of @ref array_type_def::subrange_type.
17642 ///
17643 /// If the two intances are different, set a bitfield to give some
17644 /// insight about the kind of differences there are.
17645 ///
17646 /// @param l the first artifact of the comparison.
17647 ///
17648 /// @param r the second artifact of the comparison.
17649 ///
17650 /// @param k a pointer to a bitfield that gives information about the
17651 /// kind of changes there are between @p l and @p r. This one is set
17652 /// iff @p k is non-null and the function returns false.
17653 ///
17654 /// Please note that setting k to a non-null value does have a
17655 /// negative performance impact because even if @p l and @p r are not
17656 /// equal, the function keeps up the comparison in order to determine
17657 /// the different kinds of ways in which they are different.
17658 ///
17659 /// @return true if @p l equals @p r, false otherwise.
17660 bool
17663  change_kind* k)
17664 {
17665  bool result = true;
17666 
17667  if (l.get_lower_bound() != r.get_lower_bound()
17668  || l.get_upper_bound() != r.get_upper_bound()
17669  || l.get_name() != r.get_name())
17670  {
17671  result = false;
17672  if (k)
17673  *k |= LOCAL_TYPE_CHANGE_KIND;
17674  else
17675  ABG_RETURN(result);
17676  }
17677 
17678  ABG_RETURN(result);
17679 }
17680 
17681 /// Equality operator.
17682 ///
17683 /// @param o the other subrange to test against.
17684 ///
17685 /// @return true iff @p o equals the current instance of
17686 /// array_type_def::subrange_type.
17687 bool
17689 {
17690  const subrange_type* other =
17691  dynamic_cast<const subrange_type*>(&o);
17692  if (!other)
17693  return false;
17694  return try_canonical_compare(this, other);
17695 }
17696 
17697 /// Equality operator.
17698 ///
17699 /// @param o the other subrange to test against.
17700 ///
17701 /// @return true iff @p o equals the current instance of
17702 /// array_type_def::subrange_type.
17703 bool
17705 {
17706  const decl_base* other = dynamic_cast<const decl_base*>(&o);
17707  if (!other)
17708  return false;
17709  return *this == *other;
17710 }
17711 
17712 /// Equality operator.
17713 ///
17714 /// @param o the other subrange to test against.
17715 ///
17716 /// @return true iff @p o equals the current instance of
17717 /// array_type_def::subrange_type.
17718 bool
17720 {
17721  const type_base &t = o;
17722  return operator==(t);
17723 }
17724 
17725 /// Equality operator.
17726 ///
17727 /// @param o the other subrange to test against.
17728 ///
17729 /// @return true iff @p o equals the current instance of
17730 /// array_type_def::subrange_type.
17731 bool
17733 {return !operator==(o);}
17734 
17735 /// Equality operator.
17736 ///
17737 /// @param o the other subrange to test against.
17738 ///
17739 /// @return true iff @p o equals the current instance of
17740 /// array_type_def::subrange_type.
17741 bool
17743 {return !operator==(o);}
17744 
17745 /// Inequality operator.
17746 ///
17747 /// @param o the other subrange to test against.
17748 ///
17749 /// @return true iff @p o is different from the current instance of
17750 /// array_type_def::subrange_type.
17751 bool
17753 {return !operator==(o);}
17754 
17755 /// Build a pretty representation for an
17756 /// array_type_def::subrange_type.
17757 ///
17758 /// @param internal set to true if the call is intended to get a
17759 /// representation of the decl (or type) for the purpose of canonical
17760 /// type comparison. This is mainly used in the function
17761 /// type_base::get_canonical_type_for().
17762 ///
17763 /// In other words if the argument for this parameter is true then the
17764 /// call is meant for internal use (for technical use inside the
17765 /// library itself), false otherwise. If you don't know what this is
17766 /// for, then set it to false.
17767 ///
17768 /// @return a copy of the pretty representation of the current
17769 /// instance of typedef_decl.
17770 string
17772 {
17773  string name = get_name();
17774  string repr;
17775 
17776  if (name.empty())
17777  repr += "<anonymous range>";
17778  else
17779  repr += "<range " + get_name() + ">";
17780  repr += as_string();
17781 
17782  return repr;
17783 }
17784 
17785 /// This implements the ir_traversable_base::traverse pure virtual
17786 /// function.
17787 ///
17788 /// @param v the visitor used on the current instance.
17789 ///
17790 /// @return true if the entire IR node tree got traversed, false
17791 /// otherwise.
17792 bool
17794 {
17795  if (v.type_node_has_been_visited(this))
17796  return true;
17797 
17798  if (v.visit_begin(this))
17799  {
17800  visiting(true);
17801  if (type_base_sptr u = get_underlying_type())
17802  u->traverse(v);
17803  visiting(false);
17804  }
17805 
17806  bool result = v.visit_end(this);
17807  v.mark_type_node_as_visited(this);
17808  return result;
17809 }
17810 
17811 // </array_type_def::subrange_type>
17812 
17813 struct array_type_def::priv
17814 {
17815  type_base_wptr element_type_;
17816  subranges_type subranges_;
17817  interned_string temp_internal_qualified_name_;
17818  interned_string internal_qualified_name_;
17819 
17820  priv(type_base_sptr t)
17821  : element_type_(t)
17822  {}
17823 
17824  priv(type_base_sptr t, subranges_type subs)
17825  : element_type_(t), subranges_(subs)
17826  {}
17827 
17828  priv()
17829  {}
17830 };
17831 
17832 /// Constructor for the type array_type_def
17833 ///
17834 /// Note how the constructor expects a vector of subrange
17835 /// objects. Parsing of the array information always entails
17836 /// parsing the subrange info as well, thus the class subrange_type
17837 /// is defined inside class array_type_def and also parsed
17838 /// simultaneously.
17839 ///
17840 /// @param e_type the type of the elements contained in the array
17841 ///
17842 /// @param subs a vector of the array's subranges(dimensions)
17843 ///
17844 /// @param locus the source location of the array type definition.
17845 array_type_def::array_type_def(const type_base_sptr e_type,
17846  const std::vector<subrange_sptr>& subs,
17847  const location& locus)
17848  : type_or_decl_base(e_type->get_environment(),
17849  ARRAY_TYPE
17850  | ABSTRACT_TYPE_BASE
17851  | ABSTRACT_DECL_BASE),
17852  type_base(e_type->get_environment(), 0, e_type->get_alignment_in_bits()),
17853  decl_base(e_type->get_environment(), locus),
17854  priv_(new priv(e_type))
17855 {
17856  runtime_type_instance(this);
17857  append_subranges(subs);
17858 }
17859 
17860 /// Constructor for the type array_type_def
17861 ///
17862 /// This constructor builds a temporary array that has no element type
17863 /// associated. Later when the element type is available, it be set
17864 /// with the array_type_def::set_element_type() member function.
17865 ///
17866 /// Note how the constructor expects a vector of subrange
17867 /// objects. Parsing of the array information always entails
17868 /// parsing the subrange info as well, thus the class subrange_type
17869 /// is defined inside class array_type_def and also parsed
17870 /// simultaneously.
17871 ///
17872 /// @param env the environment of the array type.
17873 ///
17874 /// @param subs a vector of the array's subranges(dimensions)
17875 ///
17876 /// @param locus the source location of the array type definition.
17877 array_type_def::array_type_def(const environment& env,
17878  const std::vector<subrange_sptr>& subs,
17879  const location& locus)
17880  : type_or_decl_base(env,
17881  ARRAY_TYPE
17882  | ABSTRACT_TYPE_BASE
17883  | ABSTRACT_DECL_BASE),
17884  type_base(env, 0, 0),
17885  decl_base(env, locus),
17886  priv_(new priv)
17887 {
17888  runtime_type_instance(this);
17889  append_subranges(subs);
17890 }
17891 
17892 /// Update the size of the array.
17893 ///
17894 /// This function computes the size of the array and sets it using
17895 /// type_base::set_size_in_bits().
17896 void
17897 array_type_def::update_size()
17898 {
17899  type_base_sptr e = priv_->element_type_.lock();
17900  if (e)
17901  {
17902  size_t s = e->get_size_in_bits();
17903  if (s)
17904  {
17905  for (const auto &sub : get_subranges())
17906  s *= sub->get_length();
17907  set_size_in_bits(s);
17908  }
17909  set_alignment_in_bits(e->get_alignment_in_bits());
17910  }
17911 }
17912 
17913 string
17914 array_type_def::get_subrange_representation() const
17915 {
17917  return r;
17918 }
17919 
17920 /// Get the string representation of an @ref array_type_def.
17921 ///
17922 /// @param a the array type to consider.
17923 ///
17924 /// @param internal set to true if the call is intended for an
17925 /// internal use (for technical use inside the library itself), false
17926 /// otherwise. If you don't know what this is for, then set it to
17927 /// false.
17928 static string
17929 get_type_representation(const array_type_def& a, bool internal)
17930 {
17931  type_base_sptr e_type = a.get_element_type();
17932  decl_base_sptr d = get_type_declaration(e_type);
17933  string r;
17934 
17935  if (is_ada_language(a.get_language()))
17936  {
17937  std::ostringstream o;
17938  o << "array ("
17939  << a.get_subrange_representation()
17940  << ") of "
17941  << e_type ? e_type->get_pretty_representation(internal):string("void");
17942  }
17943  else
17944  {
17945  if (internal)
17946  r = (e_type
17947  ? get_type_name(e_type,
17948  /*qualified=*/true,
17949  /*internal=*/true)
17950  : string("void"))
17951  + a.get_subrange_representation();
17952  else
17953  r = (e_type
17954  ? get_type_name(e_type, /*qualified=*/true, /*internal=*/false)
17955  : string("void"))
17956  + a.get_subrange_representation();
17957  }
17958 
17959  return r;
17960 }
17961 
17962 /// Get the pretty representation of the current instance of @ref
17963 /// array_type_def.
17964 ///
17965 /// @param internal set to true if the call is intended to get a
17966 /// representation of the decl (or type) for the purpose of canonical
17967 /// type comparison. This is mainly used in the function
17968 /// type_base::get_canonical_type_for().
17969 ///
17970 /// In other words if the argument for this parameter is true then the
17971 /// call is meant for internal use (for technical use inside the
17972 /// library itself), false otherwise. If you don't know what this is
17973 /// for, then set it to false.
17974 /// @param internal set to true if the call is intended for an
17975 /// internal use (for technical use inside the library itself), false
17976 /// otherwise. If you don't know what this is for, then set it to
17977 /// false.
17978 ///
17979 /// @return the pretty representation of the ABI artifact.
17980 string
17982  bool /*qualified_name*/) const
17983 {return get_type_representation(*this, internal);}
17984 
17985 /// Compares two instances of @ref array_type_def.
17986 ///
17987 /// If the two intances are different, set a bitfield to give some
17988 /// insight about the kind of differences there are.
17989 ///
17990 /// @param l the first artifact of the comparison.
17991 ///
17992 /// @param r the second artifact of the comparison.
17993 ///
17994 /// @param k a pointer to a bitfield that gives information about the
17995 /// kind of changes there are between @p l and @p r. This one is set
17996 /// iff @p k is non-null and the function returns false.
17997 ///
17998 /// Please note that setting k to a non-null value does have a
17999 /// negative performance impact because even if @p l and @p r are not
18000 /// equal, the function keeps up the comparison in order to determine
18001 /// the different kinds of ways in which they are different.
18002 ///
18003 /// @return true if @p l equals @p r, false otherwise.
18004 bool
18006 {
18007  std::vector<array_type_def::subrange_sptr > this_subs = l.get_subranges();
18008  std::vector<array_type_def::subrange_sptr > other_subs = r.get_subranges();
18009 
18010  bool result = true;
18011  if (this_subs.size() != other_subs.size())
18012  {
18013  result = false;
18014  if (k)
18015  *k |= LOCAL_TYPE_CHANGE_KIND;
18016  else
18018  }
18019 
18020  std::vector<array_type_def::subrange_sptr >::const_iterator i,j;
18021  for (i = this_subs.begin(), j = other_subs.begin();
18022  i != this_subs.end() && j != other_subs.end();
18023  ++i, ++j)
18024  if (**i != **j)
18025  {
18026  result = false;
18027  if (k)
18028  {
18029  *k |= LOCAL_TYPE_CHANGE_KIND;
18030  break;
18031  }
18032  else
18034  }
18035 
18036  // Compare the element types modulo the typedefs they might have
18037  if (l.get_element_type() != r.get_element_type())
18038  {
18039  result = false;
18040  if (k)
18041  *k |= SUBTYPE_CHANGE_KIND;
18042  else
18044  }
18045 
18046  ABG_RETURN(result);
18047 }
18048 
18049 /// Test if two variables are equals modulo CV qualifiers.
18050 ///
18051 /// @param l the first array of the comparison.
18052 ///
18053 /// @param r the second array of the comparison.
18054 ///
18055 /// @return true iff @p l equals @p r or, if they are different, the
18056 /// difference between the too is just a matter of CV qualifiers.
18057 bool
18059 {
18060  if (l == r)
18061  return true;
18062 
18063  if (!l || !r)
18065 
18068 
18069  std::vector<array_type_def::subrange_sptr > this_subs = l->get_subranges();
18070  std::vector<array_type_def::subrange_sptr > other_subs = r->get_subranges();
18071 
18072  if (this_subs.size() != other_subs.size())
18074 
18075  std::vector<array_type_def::subrange_sptr >::const_iterator i,j;
18076  for (i = this_subs.begin(), j = other_subs.begin();
18077  i != this_subs.end() && j != other_subs.end();
18078  ++i, ++j)
18079  if (**i != **j)
18081 
18082  type_base *first_element_type =
18084  type_base *second_element_type =
18086 
18087  if (*first_element_type != *second_element_type)
18089 
18090  return true;
18091 }
18092 
18093 /// Get the language of the array.
18094 ///
18095 /// @return the language of the array.
18098 {
18099  const std::vector<subrange_sptr>& subranges =
18100  get_subranges();
18101 
18102  if (subranges.empty())
18103  return translation_unit::LANG_C11;
18104  return subranges.front()->get_language();
18105 }
18106 
18107 bool
18109 {
18110  const array_type_def* other =
18111  dynamic_cast<const array_type_def*>(&o);
18112  if (!other)
18113  return false;
18114  return try_canonical_compare(this, other);
18115 }
18116 
18117 bool
18119 {
18120  const decl_base* other = dynamic_cast<const decl_base*>(&o);
18121  if (!other)
18122  return false;
18123  return *this == *other;
18124 }
18125 
18126 /// Getter of the type of an array element.
18127 ///
18128 /// @return the type of an array element.
18129 const type_base_sptr
18131 {return priv_->element_type_.lock();}
18132 
18133 /// Setter of the type of array element.
18134 ///
18135 /// Beware that after using this function, one might want to
18136 /// re-compute the canonical type of the array, if one has already
18137 /// been computed.
18138 ///
18139 /// The intended use of this method is to permit in-place adjustment
18140 /// of the element type's qualifiers. In particular, the size of the
18141 /// element type should not be changed.
18142 ///
18143 /// @param element_type the new element type to set.
18144 void
18145 array_type_def::set_element_type(const type_base_sptr& element_type)
18146 {
18147  priv_->element_type_ = element_type;
18148  update_size();
18150 }
18151 
18152 /// Append subranges from the vector @param subs to the current
18153 /// vector of subranges.
18154 void
18155 array_type_def::append_subranges(const std::vector<subrange_sptr>& subs)
18156 {
18157 
18158  for (const auto &sub : subs)
18159  priv_->subranges_.push_back(sub);
18160 
18161  update_size();
18163 }
18164 
18165 /// @return true if one of the sub-ranges of the array is infinite, or
18166 /// if the array has no sub-range at all, also meaning that the size
18167 /// of the array is infinite.
18168 bool
18170 {
18171  if (priv_->subranges_.empty())
18172  return true;
18173 
18174  for (std::vector<shared_ptr<subrange_type> >::const_iterator i =
18175  priv_->subranges_.begin();
18176  i != priv_->subranges_.end();
18177  ++i)
18178  if ((*i)->is_infinite())
18179  return true;
18180 
18181  return false;
18182 }
18183 
18184 int
18185 array_type_def::get_dimension_count() const
18186 {return priv_->subranges_.size();}
18187 
18188 /// Build and return the qualified name of the current instance of the
18189 /// @ref array_type_def.
18190 ///
18191 /// @param qn output parameter. Is set to the newly-built qualified
18192 /// name of the current instance of @ref array_type_def.
18193 ///
18194 /// @param internal set to true if the call is intended for an
18195 /// internal use (for technical use inside the library itself), false
18196 /// otherwise. If you don't know what this is for, then set it to
18197 /// false.
18198 void
18200 {qn = get_qualified_name(internal);}
18201 
18202 /// Compute the qualified name of the array.
18203 ///
18204 /// @param internal set to true if the call is intended for an
18205 /// internal use (for technical use inside the library itself), false
18206 /// otherwise. If you don't know what this is for, then set it to
18207 /// false.
18208 ///
18209 /// @return the resulting qualified name.
18210 const interned_string&
18212 {
18213  const environment& env = get_environment();
18214 
18215 
18216  if (internal)
18217  {
18218  if (get_canonical_type())
18219  {
18220  if (priv_->internal_qualified_name_.empty())
18221  priv_->internal_qualified_name_ =
18222  env.intern(get_type_representation(*this, /*internal=*/true));
18223  return priv_->internal_qualified_name_;
18224  }
18225  else
18226  {
18227  priv_->temp_internal_qualified_name_ =
18228  env.intern(get_type_representation(*this, /*internal=*/true));
18229  return priv_->temp_internal_qualified_name_;
18230  }
18231  }
18232  else
18233  {
18234  if (get_canonical_type())
18235  {
18236  if (decl_base::peek_qualified_name().empty())
18237  set_qualified_name(env.intern(get_type_representation
18238  (*this, /*internal=*/false)));
18240  }
18241  else
18242  {
18243  set_temporary_qualified_name(env.intern(get_type_representation
18244  (*this,
18245  /*internal=*/false)));
18247  }
18248  }
18249 }
18250 
18251 /// This implements the ir_traversable_base::traverse pure virtual
18252 /// function.
18253 ///
18254 /// @param v the visitor used on the current instance.
18255 ///
18256 /// @return true if the entire IR node tree got traversed, false
18257 /// otherwise.
18258 bool
18260 {
18261  if (v.type_node_has_been_visited(this))
18262  return true;
18263 
18264  if (visiting())
18265  return true;
18266 
18267  if (v.visit_begin(this))
18268  {
18269  visiting(true);
18270  if (type_base_sptr t = get_element_type())
18271  t->traverse(v);
18272  visiting(false);
18273  }
18274 
18275  bool result = v.visit_end(this);
18276  v.mark_type_node_as_visited(this);
18277  return result;
18278 }
18279 
18280 const location&
18281 array_type_def::get_location() const
18282 {return decl_base::get_location();}
18283 
18284 /// Get the array's subranges
18285 const std::vector<array_type_def::subrange_sptr>&
18287 {return priv_->subranges_;}
18288 
18289 array_type_def::~array_type_def()
18290 {}
18291 
18292 // </array_type_def definitions>
18293 
18294 // <enum_type_decl definitions>
18295 
18296 class enum_type_decl::priv
18297 {
18298  type_base_sptr underlying_type_;
18299  enumerators enumerators_;
18300 
18301  friend class enum_type_decl;
18302 
18303  priv();
18304 
18305 public:
18306  priv(type_base_sptr underlying_type,
18308  : underlying_type_(underlying_type),
18309  enumerators_(enumerators)
18310  {}
18311 }; // end class enum_type_decl::priv
18312 
18313 /// Constructor.
18314 ///
18315 /// @param name the name of the type declaration.
18316 ///
18317 /// @param locus the source location where the type was defined.
18318 ///
18319 /// @param underlying_type the underlying type of the enum.
18320 ///
18321 /// @param enums the enumerators of this enum type.
18322 ///
18323 /// @param linkage_name the linkage name of the enum.
18324 ///
18325 /// @param vis the visibility of the enum type.
18326 enum_type_decl::enum_type_decl(const string& name,
18327  const location& locus,
18328  type_base_sptr underlying_type,
18329  enumerators& enums,
18330  const string& linkage_name,
18331  visibility vis)
18332  : type_or_decl_base(underlying_type->get_environment(),
18333  ENUM_TYPE
18334  | ABSTRACT_TYPE_BASE
18335  | ABSTRACT_DECL_BASE),
18336  type_base(underlying_type->get_environment(),
18337  underlying_type->get_size_in_bits(),
18338  underlying_type->get_alignment_in_bits()),
18339  decl_base(underlying_type->get_environment(),
18340  name, locus, linkage_name, vis),
18341  priv_(new priv(underlying_type, enums))
18342 {
18343  runtime_type_instance(this);
18344  for (enumerators::iterator e = get_enumerators().begin();
18345  e != get_enumerators().end();
18346  ++e)
18347  e->set_enum_type(this);
18348 }
18349 
18350 /// Return the underlying type of the enum.
18351 type_base_sptr
18353 {return priv_->underlying_type_;}
18354 
18355 /// @return the list of enumerators of the enum.
18358 {return priv_->enumerators_;}
18359 
18360 /// @return the list of enumerators of the enum.
18363 {return priv_->enumerators_;}
18364 
18365 /// Get the pretty representation of the current instance of @ref
18366 /// enum_type_decl.
18367 ///
18368 /// @param internal set to true if the call is intended to get a
18369 /// representation of the decl (or type) for the purpose of canonical
18370 /// type comparison. This is mainly used in the function
18371 /// type_base::get_canonical_type_for().
18372 ///
18373 /// In other words if the argument for this parameter is true then the
18374 /// call is meant for internal use (for technical use inside the
18375 /// library itself), false otherwise. If you don't know what this is
18376 /// for, then set it to false.
18377 ///
18378 /// @param qualified_name if true, names emitted in the pretty
18379 /// representation are fully qualified.
18380 ///
18381 /// @return the pretty representation of the enum type.
18382 string
18384  bool qualified_name) const
18385 {
18386  string r = "enum ";
18387 
18388  if (internal && get_is_anonymous())
18389  r += get_type_name(this, qualified_name, /*internal=*/true);
18390  else
18392  qualified_name);
18393  return r;
18394 }
18395 
18396 /// This implements the ir_traversable_base::traverse pure virtual
18397 /// function.
18398 ///
18399 /// @param v the visitor used on the current instance.
18400 ///
18401 /// @return true if the entire IR node tree got traversed, false
18402 /// otherwise.
18403 bool
18405 {
18406  if (v.type_node_has_been_visited(this))
18407  return true;
18408 
18409  if (visiting())
18410  return true;
18411 
18412  if (v.visit_begin(this))
18413  {
18414  visiting(true);
18415  if (type_base_sptr t = get_underlying_type())
18416  t->traverse(v);
18417  visiting(false);
18418  }
18419 
18420  bool result = v.visit_end(this);
18421  v.mark_type_node_as_visited(this);
18422  return result;
18423 }
18424 
18425 /// Destructor for the enum type declaration.
18427 {}
18428 
18429 /// Test if two enums differ, but not by a name change.
18430 ///
18431 /// @param l the first enum to consider.
18432 ///
18433 /// @param r the second enum to consider.
18434 ///
18435 /// @return true iff @p l differs from @p r by anything but a name
18436 /// change.
18437 bool
18439  const enum_type_decl& r,
18440  change_kind* k)
18441 {
18442  bool result = false;
18443  if (*l.get_underlying_type() != *r.get_underlying_type())
18444  {
18445  result = true;
18446  if (k)
18447  *k |= SUBTYPE_CHANGE_KIND;
18448  else
18449  return true;
18450  }
18451 
18452  enum_type_decl::enumerators::const_iterator i, j;
18453  for (i = l.get_enumerators().begin(), j = r.get_enumerators().begin();
18454  i != l.get_enumerators().end() && j != r.get_enumerators().end();
18455  ++i, ++j)
18456  if (*i != *j)
18457  {
18458  result = true;
18459  if (k)
18460  {
18461  *k |= LOCAL_TYPE_CHANGE_KIND;
18462  break;
18463  }
18464  else
18465  return true;
18466  }
18467 
18468  if (i != l.get_enumerators().end() || j != r.get_enumerators().end())
18469  {
18470  result = true;
18471  if (k)
18472  *k |= LOCAL_TYPE_CHANGE_KIND;
18473  else
18474  return true;
18475  }
18476 
18477  enum_type_decl &local_r = const_cast<enum_type_decl&>(r);
18480  string n_l = l.get_name();
18481  string n_r = r.get_name();
18482  local_r.set_qualified_name(qn_l);
18483  local_r.set_name(n_l);
18484 
18485  if (!(l.decl_base::operator==(r) && l.type_base::operator==(r)))
18486  {
18487  result = true;
18488  if (k)
18489  {
18490  if (!l.decl_base::operator==(r))
18492  if (!l.type_base::operator==(r))
18493  *k |= LOCAL_TYPE_CHANGE_KIND;
18494  }
18495  else
18496  {
18497  local_r.set_name(n_r);
18498  local_r.set_qualified_name(qn_r);
18499  return true;
18500  }
18501  }
18502  local_r.set_qualified_name(qn_r);
18503  local_r.set_name(n_r);
18504 
18505  return result;
18506 }
18507 
18508 /// Test if a given enumerator is found present in an enum.
18509 ///
18510 /// This is a subroutine of the equals function for enums.
18511 ///
18512 /// @param enr the enumerator to consider.
18513 ///
18514 /// @param enom the enum to consider.
18515 ///
18516 /// @return true iff the enumerator @p enr is present in the enum @p
18517 /// enom.
18518 static bool
18519 is_enumerator_present_in_enum(const enum_type_decl::enumerator &enr,
18520  const enum_type_decl &enom)
18521 {
18522  for (const auto &e : enom.get_enumerators())
18523  if (e == enr)
18524  return true;
18525  return false;
18526 }
18527 
18528 /// Check if two enumerators values are equal.
18529 ///
18530 /// This function doesn't check if the names of the enumerators are
18531 /// equal or not.
18532 ///
18533 /// @param enr the first enumerator to consider.
18534 ///
18535 /// @param enl the second enumerator to consider.
18536 ///
18537 /// @return true iff @p enr has the same value as @p enl.
18538 static bool
18539 enumerators_values_are_equal(const enum_type_decl::enumerator &enr,
18540  const enum_type_decl::enumerator &enl)
18541 {return enr.get_value() == enl.get_value();}
18542 
18543 /// Detect if a given enumerator value is present in an enum.
18544 ///
18545 /// This function looks inside the enumerators of a given enum and
18546 /// detect if the enum contains at least one enumerator or a given
18547 /// value. The function also detects if the enumerator value we are
18548 /// looking at is present in the enum with a different name. An
18549 /// enumerator with the same value but with a different name is named
18550 /// a "redundant enumerator". The function returns the set of
18551 /// enumerators that are redundant with the value we are looking at.
18552 ///
18553 /// @param enr the enumerator to consider.
18554 ///
18555 /// @param enom the enum to consider.
18556 ///
18557 /// @param redundant_enrs if the function returns true, then this
18558 /// vector is filled with enumerators that are redundant with the
18559 /// value of @p enr.
18560 ///
18561 /// @return true iff the function detects that @p enom contains
18562 /// enumerators with the same value as @p enr.
18563 static bool
18564 is_enumerator_value_present_in_enum(const enum_type_decl::enumerator &enr,
18565  const enum_type_decl &enom,
18566  vector<enum_type_decl::enumerator>& redundant_enrs)
18567 {
18568  bool found = false;
18569  for (const auto &e : enom.get_enumerators())
18570  if (enumerators_values_are_equal(e, enr))
18571  {
18572  found = true;
18573  if (e != enr)
18574  redundant_enrs.push_back(e);
18575  }
18576 
18577  return found;
18578 }
18579 
18580 /// Check if an enumerator value is redundant in a given enum.
18581 ///
18582 /// Given an enumerator value, this function detects if an enum
18583 /// contains at least one enumerator with the the same value but with
18584 /// a different name.
18585 ///
18586 /// @param enr the enumerator to consider.
18587 ///
18588 /// @param enom the enum to consider.
18589 ///
18590 /// @return true iff @p enr is a redundant enumerator in enom.
18591 static bool
18592 is_enumerator_value_redundant(const enum_type_decl::enumerator &enr,
18593  const enum_type_decl &enom)
18594 {
18595  vector<enum_type_decl::enumerator> redundant_enrs;
18596  if (is_enumerator_value_present_in_enum(enr, enom, redundant_enrs))
18597  {
18598  if (!redundant_enrs.empty())
18599  return true;
18600  }
18601  return false;
18602 }
18603 
18604 /// Compares two instances of @ref enum_type_decl.
18605 ///
18606 /// If the two intances are different, set a bitfield to give some
18607 /// insight about the kind of differences there are.
18608 ///
18609 /// @param l the first artifact of the comparison.
18610 ///
18611 /// @param r the second artifact of the comparison.
18612 ///
18613 /// @param k a pointer to a bitfield that gives information about the
18614 /// kind of changes there are between @p l and @p r. This one is set
18615 /// iff @p k is non-null and the function returns false.
18616 ///
18617 /// Please note that setting k to a non-null value does have a
18618 /// negative performance impact because even if @p l and @p r are not
18619 /// equal, the function keeps up the comparison in order to determine
18620 /// the different kinds of ways in which they are different.
18621 ///
18622 /// @return true if @p l equals @p r, false otherwise.
18623 bool
18625 {
18626  bool result = true;
18627 
18628  //
18629  // Look through decl-only-enum.
18630  //
18631 
18632  const enum_type_decl *def1 =
18635  : &l;
18636 
18637  const enum_type_decl *def2 =
18640  : &r;
18641 
18642  if (!!def1 != !!def2)
18643  {
18644  // One enum is decl-only while the other is not.
18645  // So the two enums are different.
18646  result = false;
18647  if (k)
18648  *k |= SUBTYPE_CHANGE_KIND;
18649  else
18651  }
18652 
18653  //
18654  // At this point, both enums have the same state of decl-only-ness.
18655  // So we can compare oranges to oranges.
18656  //
18657 
18658  if (!def1)
18659  def1 = &l;
18660  if (!def2)
18661  def2 = &r;
18662 
18663  if (def1->get_underlying_type() != def2->get_underlying_type())
18664  {
18665  result = false;
18666  if (k)
18667  *k |= SUBTYPE_CHANGE_KIND;
18668  else
18670  }
18671 
18672  if (!(def1->decl_base::operator==(*def2)
18673  && def1->type_base::operator==(*def2)))
18674  {
18675  result = false;
18676  if (k)
18677  {
18678  if (!def1->decl_base::operator==(*def2))
18680  if (!def1->type_base::operator==(*def2))
18681  *k |= LOCAL_TYPE_CHANGE_KIND;
18682  }
18683  else
18685  }
18686 
18687  // Now compare the enumerators. Note that the order of declaration
18688  // of enumerators should not matter in the comparison.
18689  //
18690  // Also if an enumerator value is redundant, that shouldn't impact
18691  // the comparison.
18692  //
18693  // In that case, note that the two enums below are considered equal:
18694  //
18695  // enum foo
18696  // {
18697  // e0 = 0;
18698  // e1 = 1;
18699  // e2 = 2;
18700  // };
18701  //
18702  // enum foo
18703  // {
18704  // e0 = 0;
18705  // e1 = 1;
18706  // e2 = 2;
18707  // e_added = 1; // <-- this value is redundant with the value
18708  // // of the enumerator e1.
18709  // };
18710  //
18711  // Note however that in the case below, the enums are different.
18712  //
18713  // enum foo
18714  // {
18715  // e0 = 0;
18716  // e1 = 1;
18717  // };
18718  //
18719  // enum foo
18720  // {
18721  // e0 = 0;
18722  // e2 = 1; // <-- this enum value is present in the first version
18723  // // of foo, but is not redundant with any enumerator
18724  // // in the second version of of enum foo.
18725  // };
18726  //
18727  // These two enums are considered equal.
18728 
18729  for(const auto &e : def1->get_enumerators())
18730  if (!is_enumerator_present_in_enum(e, *def2)
18731  && (!is_enumerator_value_redundant(e, *def2)
18732  || !is_enumerator_value_redundant(e, *def1)))
18733  {
18734  result = false;
18735  if (k)
18736  {
18737  *k |= LOCAL_TYPE_CHANGE_KIND;
18738  break;
18739  }
18740  else
18742  }
18743 
18744  for(const auto &e : def2->get_enumerators())
18745  if (!is_enumerator_present_in_enum(e, *def1)
18746  && (!is_enumerator_value_redundant(e, *def1)
18747  || !is_enumerator_value_redundant(e, *def2)))
18748  {
18749  result = false;
18750  if (k)
18751  {
18752  *k |= LOCAL_TYPE_CHANGE_KIND;
18753  break;
18754  }
18755  else
18757  }
18758 
18759  ABG_RETURN(result);
18760 }
18761 
18762 /// Equality operator.
18763 ///
18764 /// @param o the other enum to test against.
18765 ///
18766 /// @return true iff @p o equals the current instance of enum type
18767 /// decl.
18768 bool
18770 {
18771  const enum_type_decl* op = dynamic_cast<const enum_type_decl*>(&o);
18772  if (!op)
18773  return false;
18774  return try_canonical_compare(this, op);
18775 }
18776 
18777 /// Equality operator.
18778 ///
18779 /// @param o the other enum to test against.
18780 ///
18781 /// @return true iff @p o is equals the current instance of enum type
18782 /// decl.
18783 bool
18785 {
18786  const decl_base* other = dynamic_cast<const decl_base*>(&o);
18787  if (!other)
18788  return false;
18789  return *this == *other;
18790 }
18791 
18792 /// Equality operator for @ref enum_type_decl_sptr.
18793 ///
18794 /// @param l the first operand to compare.
18795 ///
18796 /// @param r the second operand to compare.
18797 ///
18798 /// @return true iff @p l equals @p r.
18799 bool
18801 {
18802  if (!!l != !!r)
18803  return false;
18804  if (l.get() == r.get())
18805  return true;
18806  decl_base_sptr o = r;
18807  return *l == *o;
18808 }
18809 
18810 /// Inequality operator for @ref enum_type_decl_sptr.
18811 ///
18812 /// @param l the first operand to compare.
18813 ///
18814 /// @param r the second operand to compare.
18815 ///
18816 /// @return true iff @p l equals @p r.
18817 bool
18819 {return !operator==(l, r);}
18820 
18821 /// The type of the private data of an @ref
18822 /// enum_type_decl::enumerator.
18823 class enum_type_decl::enumerator::priv
18824 {
18825  string name_;
18826  int64_t value_;
18827  string qualified_name_;
18828  enum_type_decl* enum_type_;
18829 
18830  friend class enum_type_decl::enumerator;
18831 
18832 public:
18833 
18834  priv()
18835  : enum_type_()
18836  {}
18837 
18838  priv(const string& name,
18839  int64_t value,
18840  enum_type_decl* e = 0)
18841  : name_(name),
18842  value_(value),
18843  enum_type_(e)
18844  {}
18845 }; // end class enum_type_def::enumerator::priv
18846 
18847 /// Default constructor of the @ref enum_type_decl::enumerator type.
18849  : priv_(new priv)
18850 {}
18851 
18852 enum_type_decl::enumerator::~enumerator() = default;
18853 
18854 /// Constructor of the @ref enum_type_decl::enumerator type.
18855 ///
18856 /// @param env the environment we are operating from.
18857 ///
18858 /// @param name the name of the enumerator.
18859 ///
18860 /// @param value the value of the enumerator.
18862  int64_t value)
18863  : priv_(new priv(name, value))
18864 {}
18865 
18866 /// Copy constructor of the @ref enum_type_decl::enumerator type.
18867 ///
18868 /// @param other enumerator to copy.
18870  : priv_(new priv(other.get_name(),
18871  other.get_value(),
18872  other.get_enum_type()))
18873 {}
18874 
18875 /// Assignment operator of the @ref enum_type_decl::enumerator type.
18876 ///
18877 /// @param o
18880 {
18881  priv_->name_ = o.get_name();
18882  priv_->value_ = o.get_value();
18883  priv_->enum_type_ = o.get_enum_type();
18884  return *this;
18885 }
18886 
18887 /// Equality operator
18888 ///
18889 /// @param other the enumerator to compare to the current
18890 /// instance of enum_type_decl::enumerator.
18891 ///
18892 /// @return true if @p other equals the current instance of
18893 /// enum_type_decl::enumerator.
18894 bool
18896 {
18897  bool names_equal = true;
18898  names_equal = (get_name() == other.get_name());
18899  return names_equal && (get_value() == other.get_value());
18900 }
18901 
18902 /// Inequality operator.
18903 ///
18904 /// @param other the other instance to compare against.
18905 ///
18906 /// @return true iff @p other is different from the current instance.
18907 bool
18909 {return !operator==(other);}
18910 
18911 /// Getter for the name of the current instance of
18912 /// enum_type_decl::enumerator.
18913 ///
18914 /// @return a reference to the name of the current instance of
18915 /// enum_type_decl::enumerator.
18916 const string&
18918 {return priv_->name_;}
18919 
18920 /// Getter for the qualified name of the current instance of
18921 /// enum_type_decl::enumerator. The first invocation of the method
18922 /// builds the qualified name, caches it and return a reference to the
18923 /// cached qualified name. Subsequent invocations just return the
18924 /// cached value.
18925 ///
18926 /// @param internal set to true if the call is intended for an
18927 /// internal use (for technical use inside the library itself), false
18928 /// otherwise. If you don't know what this is for, then set it to
18929 /// false.
18930 ///
18931 /// @return the qualified name of the current instance of
18932 /// enum_type_decl::enumerator.
18933 const string&
18935 {
18936  if (priv_->qualified_name_.empty())
18937  {
18938  priv_->qualified_name_ =
18939  get_enum_type()->get_qualified_name(internal)
18940  + "::"
18941  + get_name();
18942  }
18943  return priv_->qualified_name_;
18944 }
18945 
18946 /// Setter for the name of @ref enum_type_decl::enumerator.
18947 ///
18948 /// @param n the new name.
18949 void
18951 {priv_->name_ = n;}
18952 
18953 /// Getter for the value of @ref enum_type_decl::enumerator.
18954 ///
18955 /// @return the value of the current instance of
18956 /// enum_type_decl::enumerator.
18957 int64_t
18959 {return priv_->value_;}
18960 
18961 /// Setter for the value of @ref enum_type_decl::enumerator.
18962 ///
18963 /// @param v the new value of the enum_type_decl::enumerator.
18964 void
18966 {priv_->value_= v;}
18967 
18968 /// Getter for the enum type that this enumerator is for.
18969 ///
18970 /// @return the enum type that this enumerator is for.
18973 {return priv_->enum_type_;}
18974 
18975 /// Setter for the enum type that this enumerator is for.
18976 ///
18977 /// @param e the new enum type.
18978 void
18980 {priv_->enum_type_ = e;}
18981 // </enum_type_decl definitions>
18982 
18983 // <typedef_decl definitions>
18984 
18985 /// Private data structure of the @ref typedef_decl.
18986 struct typedef_decl::priv
18987 {
18988  type_base_wptr underlying_type_;
18989  string internal_qualified_name_;
18990  string temp_internal_qualified_name_;
18991 
18992  priv(const type_base_sptr& t)
18993  : underlying_type_(t)
18994  {}
18995 }; // end struct typedef_decl::priv
18996 
18997 /// Constructor of the typedef_decl type.
18998 ///
18999 /// @param name the name of the typedef.
19000 ///
19001 /// @param underlying_type the underlying type of the typedef.
19002 ///
19003 /// @param locus the source location of the typedef declaration.
19004 ///
19005 /// @param linkage_name the mangled name of the typedef.
19006 ///
19007 /// @param vis the visibility of the typedef type.
19008 typedef_decl::typedef_decl(const string& name,
19009  const type_base_sptr underlying_type,
19010  const location& locus,
19011  const string& linkage_name,
19012  visibility vis)
19013  : type_or_decl_base(underlying_type->get_environment(),
19014  TYPEDEF_TYPE
19015  | ABSTRACT_TYPE_BASE
19016  | ABSTRACT_DECL_BASE),
19017  type_base(underlying_type->get_environment(),
19018  underlying_type->get_size_in_bits(),
19019  underlying_type->get_alignment_in_bits()),
19020  decl_base(underlying_type->get_environment(),
19021  name, locus, linkage_name, vis),
19022  priv_(new priv(underlying_type))
19023 {
19024  runtime_type_instance(this);
19025 }
19026 
19027 /// Constructor of the typedef_decl type.
19028 ///
19029 /// @param name the name of the typedef.
19030 ///
19031 /// @param env the environment of the current typedef.
19032 ///
19033 /// @param locus the source location of the typedef declaration.
19034 ///
19035 /// @param mangled_name the mangled name of the typedef.
19036 ///
19037 /// @param vis the visibility of the typedef type.
19038 typedef_decl::typedef_decl(const string& name,
19039  const environment& env,
19040  const location& locus,
19041  const string& mangled_name,
19042  visibility vis)
19043  : type_or_decl_base(env,
19044  TYPEDEF_TYPE
19045  | ABSTRACT_TYPE_BASE
19046  | ABSTRACT_DECL_BASE),
19047  type_base(env, /*size_in_bits=*/0,
19048  /*alignment_in_bits=*/0),
19049  decl_base(env, name, locus, mangled_name, vis),
19050  priv_(new priv(nullptr))
19051 {
19052  runtime_type_instance(this);
19053 }
19054 
19055 /// Return the size of the typedef.
19056 ///
19057 /// This function looks at the size of the underlying type and ensures
19058 /// that it's the same as the size of the typedef.
19059 ///
19060 /// @return the size of the typedef.
19061 size_t
19063 {
19064  if (!get_underlying_type())
19065  return 0;
19066  size_t s = get_underlying_type()->get_size_in_bits();
19067  if (s != type_base::get_size_in_bits())
19068  const_cast<typedef_decl*>(this)->set_size_in_bits(s);
19069  return type_base::get_size_in_bits();
19070 }
19071 
19072 /// Return the alignment of the typedef.
19073 ///
19074 /// This function looks at the alignment of the underlying type and
19075 /// ensures that it's the same as the alignment of the typedef.
19076 ///
19077 /// @return the size of the typedef.
19078 size_t
19080 {
19081  if (!get_underlying_type())
19082  return 0;
19083  size_t s = get_underlying_type()->get_alignment_in_bits();
19085  const_cast<typedef_decl*>(this)->set_alignment_in_bits(s);
19087 }
19088 
19089 /// Compares two instances of @ref typedef_decl.
19090 ///
19091 /// If the two intances are different, set a bitfield to give some
19092 /// insight about the kind of differences there are.
19093 ///
19094 /// @param l the first artifact of the comparison.
19095 ///
19096 /// @param r the second artifact of the comparison.
19097 ///
19098 /// @param k a pointer to a bitfield that gives information about the
19099 /// kind of changes there are between @p l and @p r. This one is set
19100 /// iff @p k is non-null and the function returns false.
19101 ///
19102 /// Please note that setting k to a non-null value does have a
19103 /// negative performance impact because even if @p l and @p r are not
19104 /// equal, the function keeps up the comparison in order to determine
19105 /// the different kinds of ways in which they are different.
19106 ///
19107 /// @return true if @p l equals @p r, false otherwise.
19108 bool
19110 {
19111  bool result = true;
19112 
19113  // No need to go further if the types have different names or
19114  // different size / alignment.
19115  if (!(l.decl_base::operator==(r)))
19116  {
19117  result = false;
19118  if (k)
19119  *k |= LOCAL_TYPE_CHANGE_KIND;
19120  else
19122  }
19123 
19124  if (*l.get_underlying_type() != *r.get_underlying_type())
19125  {
19126  // Changes to the underlying type of a typedef are considered
19127  // local, a bit like for pointers.
19128  result = false;
19129  if (k)
19130  *k |= LOCAL_TYPE_CHANGE_KIND;
19131  else
19133  }
19134 
19135  ABG_RETURN(result);
19136 }
19137 
19138 /// Equality operator
19139 ///
19140 /// @param o the other typedef_decl to test against.
19141 bool
19143 {
19144  const typedef_decl* other = dynamic_cast<const typedef_decl*>(&o);
19145  if (!other)
19146  return false;
19147  return try_canonical_compare(this, other);
19148 }
19149 
19150 /// Equality operator
19151 ///
19152 /// @param o the other typedef_decl to test against.
19153 ///
19154 /// @return true if the current instance of @ref typedef_decl equals
19155 /// @p o.
19156 bool
19158 {
19159  const decl_base* other = dynamic_cast<const decl_base*>(&o);
19160  if (!other)
19161  return false;
19162  return *this == *other;
19163 }
19164 
19165 /// Build a pretty representation for a typedef_decl.
19166 ///
19167 /// @param internal set to true if the call is intended to get a
19168 /// representation of the decl (or type) for the purpose of canonical
19169 /// type comparison. This is mainly used in the function
19170 /// type_base::get_canonical_type_for().
19171 ///
19172 /// In other words if the argument for this parameter is true then the
19173 /// call is meant for internal use (for technical use inside the
19174 /// library itself), false otherwise. If you don't know what this is
19175 /// for, then set it to false.
19176 
19177 /// @param qualified_name if true, names emitted in the pretty
19178 /// representation are fully qualified.
19179 ///
19180 /// @return a copy of the pretty representation of the current
19181 /// instance of typedef_decl.
19182 string
19184  bool qualified_name) const
19185 {
19186 
19187  string result = "typedef ";
19188  if (qualified_name)
19189  result += get_qualified_name(internal);
19190  else
19191  result += get_name();
19192 
19193  return result;
19194 }
19195 
19196 /// Getter of the underlying type of the typedef.
19197 ///
19198 /// @return the underlying_type.
19199 type_base_sptr
19201 {return priv_->underlying_type_.lock();}
19202 
19203 /// Setter ofthe underlying type of the typedef.
19204 ///
19205 /// @param t the new underlying type of the typedef.
19206 void
19207 typedef_decl::set_underlying_type(const type_base_sptr& t)
19208 {
19209  priv_->underlying_type_ = t;
19210  set_size_in_bits(t->get_size_in_bits());
19211  set_alignment_in_bits(t->get_alignment_in_bits());
19212 }
19213 
19214 /// This implements the ir_traversable_base::traverse pure virtual
19215 /// function.
19216 ///
19217 /// @param v the visitor used on the current instance.
19218 ///
19219 /// @return true if the entire IR node tree got traversed, false
19220 /// otherwise.
19221 bool
19223 {
19224  if (v.type_node_has_been_visited(this))
19225  return true;
19226 
19227  if (visiting())
19228  return true;
19229 
19230  if (v.visit_begin(this))
19231  {
19232  visiting(true);
19233  if (type_base_sptr t = get_underlying_type())
19234  t->traverse(v);
19235  visiting(false);
19236  }
19237 
19238  bool result = v.visit_end(this);
19239  v.mark_type_node_as_visited(this);
19240  return result;
19241 }
19242 
19243 typedef_decl::~typedef_decl()
19244 {}
19245 // </typedef_decl definitions>
19246 
19247 // <var_decl definitions>
19248 
19249 struct var_decl::priv
19250 {
19251  type_base_wptr type_;
19252  type_base* naked_type_;
19253  decl_base::binding binding_;
19254  elf_symbol_sptr symbol_;
19255  interned_string id_;
19256 
19257  priv()
19258  : naked_type_(),
19259  binding_(decl_base::BINDING_GLOBAL)
19260  {}
19261 
19262  priv(type_base_sptr t,
19264  : type_(t),
19265  naked_type_(t.get()),
19266  binding_(b)
19267  {}
19268 
19269  /// Setter of the type of the variable.
19270  ///
19271  /// @param t the new variable type.
19272  void
19273  set_type(type_base_sptr t)
19274  {
19275  type_ = t;
19276  naked_type_ = t.get();
19277  }
19278 }; // end struct var_decl::priv
19279 
19280 /// Constructor of the @ref var_decl type.
19281 ///
19282 /// @param name the name of the variable declaration
19283 ///
19284 /// @param type the type of the variable declaration
19285 ///
19286 /// @param locus the source location where the variable was defined.
19287 ///
19288 /// @param linkage_name the linkage name of the variable.
19289 ///
19290 /// @param vis the visibility of of the variable.
19291 ///
19292 /// @param bind the binding kind of the variable.
19293 var_decl::var_decl(const string& name,
19294  type_base_sptr type,
19295  const location& locus,
19296  const string& linkage_name,
19297  visibility vis,
19298  binding bind)
19299  : type_or_decl_base(type->get_environment(),
19300  VAR_DECL | ABSTRACT_DECL_BASE),
19301  decl_base(type->get_environment(), name, locus, linkage_name, vis),
19302  priv_(new priv(type, bind))
19303 {
19304  runtime_type_instance(this);
19305 }
19306 
19307 /// Getter of the type of the variable.
19308 ///
19309 /// @return the type of the variable.
19310 const type_base_sptr
19312 {return priv_->type_.lock();}
19313 
19314 /// Setter of the type of the variable.
19315 ///
19316 /// @param the new type of the variable.
19317 void
19318 var_decl::set_type(type_base_sptr& t)
19319 {priv_->set_type(t);}
19320 
19321 /// Getter of the type of the variable.
19322 ///
19323 /// This getter returns a bare pointer, as opposed to a smart pointer.
19324 /// It's to be used on performance sensitive code paths identified by
19325 /// careful profiling.
19326 ///
19327 /// @return the type of the variable, as a bare pointer.
19328 const type_base*
19330 {return priv_->naked_type_;}
19331 
19332 /// Getter of the binding of the variable.
19333 ///
19334 /// @return the biding of the variable.
19337 {return priv_->binding_;}
19338 
19339 /// Setter of the binding of the variable.
19340 ///
19341 /// @param b the new binding value.
19342 void
19344 {priv_->binding_ = b;}
19345 
19346 /// Sets the underlying ELF symbol for the current variable.
19347 ///
19348 /// And underlyin$g ELF symbol for the current variable might exist
19349 /// only if the corpus that this variable originates from was
19350 /// constructed from an ELF binary file.
19351 ///
19352 /// Note that comparing two variables that have underlying ELF symbols
19353 /// involves comparing their underlying elf symbols. The decl name
19354 /// for the variable thus becomes irrelevant in the comparison.
19355 ///
19356 /// @param sym the new ELF symbol for this variable decl.
19357 void
19359 {
19360  priv_->symbol_ = sym;
19361  // The variable id cache that depends on the symbol must be
19362  // invalidated because the symbol changed.
19363  priv_->id_ = get_environment().intern("");
19364 }
19365 
19366 /// Gets the the underlying ELF symbol for the current variable,
19367 /// that was set using var_decl::set_symbol(). Please read the
19368 /// documentation for that member function for more information about
19369 /// "underlying ELF symbols".
19370 ///
19371 /// @return sym the underlying ELF symbol for this variable decl, if
19372 /// one exists.
19373 const elf_symbol_sptr&
19375 {return priv_->symbol_;}
19376 
19377 /// Create a new var_decl that is a clone of the current one.
19378 ///
19379 /// @return the cloned var_decl.
19382 {
19384  get_type(),
19385  get_location(),
19386  get_linkage_name(),
19387  get_visibility(),
19388  get_binding()));
19389 
19390  v->set_symbol(get_symbol());
19391 
19392  if (is_member_decl(*this))
19393  {
19397  get_member_is_static(*this),
19398  get_data_member_offset(*this));
19399  }
19400  else
19402 
19403  return v;
19404 }
19405 /// Setter of the scope of the current var_decl.
19406 ///
19407 /// Note that the decl won't hold a reference on the scope. It's
19408 /// rather the scope that holds a reference on its members.
19409 ///
19410 /// @param scope the new scope.
19411 void
19412 var_decl::set_scope(scope_decl* scope)
19413 {
19414  if (!get_context_rel())
19415  set_context_rel(new dm_context_rel(scope));
19416  else
19417  get_context_rel()->set_scope(scope);
19418 }
19419 
19420 /// Compares two instances of @ref var_decl without taking their type
19421 /// into account.
19422 ///
19423 /// If the two intances are different modulo their type, set a
19424 /// bitfield to give some insight about the kind of differences there
19425 /// are.
19426 ///
19427 /// @param l the first artifact of the comparison.
19428 ///
19429 /// @param r the second artifact of the comparison.
19430 ///
19431 /// @param k a pointer to a bitfield that gives information about the
19432 /// kind of changes there are between @p l and @p r. This one is set
19433 /// iff @p k is non-null and the function returns false.
19434 ///
19435 /// Please note that setting k to a non-null value does have a
19436 /// negative performance impact because even if @p l and @p r are not
19437 /// equal, the function keeps up the comparison in order to determine
19438 /// the different kinds of ways in which they are different.
19439 ///
19440 /// @return true if @p l equals @p r, false otherwise.
19441 bool
19443 {
19444  bool result = true;
19445 
19446  // If there are underlying elf symbols for these variables,
19447  // compare them. And then compare the other parts.
19448  const elf_symbol_sptr &s0 = l.get_symbol(), &s1 = r.get_symbol();
19449  if (!!s0 != !!s1)
19450  {
19451  result = false;
19452  if (k)
19454  else
19456  }
19457  else if (s0 && s0 != s1)
19458  {
19459  result = false;
19460  if (k)
19462  else
19464  }
19465  bool symbols_are_equal = (s0 && s1 && result);
19466 
19467  if (symbols_are_equal)
19468  {
19469  // The variables have underlying elf symbols that are equal, so
19470  // now, let's compare the decl_base part of the variables w/o
19471  // considering their decl names.
19472  const environment& env = l.get_environment();
19473  const interned_string n1 = l.get_qualified_name(), n2 = r.get_qualified_name();
19474  const_cast<var_decl&>(l).set_qualified_name(env.intern(""));
19475  const_cast<var_decl&>(r).set_qualified_name(env.intern(""));
19476  bool decl_bases_different = !l.decl_base::operator==(r);
19477  const_cast<var_decl&>(l).set_qualified_name(n1);
19478  const_cast<var_decl&>(r).set_qualified_name(n2);
19479 
19480  if (decl_bases_different)
19481  {
19482  result = false;
19483  if (k)
19485  else
19487  }
19488  }
19489  else
19490  if (!l.decl_base::operator==(r))
19491  {
19492  result = false;
19493  if (k)
19495  else
19497  }
19498 
19499  const dm_context_rel* c0 =
19500  dynamic_cast<const dm_context_rel*>(l.get_context_rel());
19501  const dm_context_rel* c1 =
19502  dynamic_cast<const dm_context_rel*>(r.get_context_rel());
19503  ABG_ASSERT(c0 && c1);
19504 
19505  if (*c0 != *c1)
19506  {
19507  result = false;
19508  if (k)
19510  else
19512  }
19513 
19514  ABG_RETURN(result);
19515 }
19516 
19517 /// Compares two instances of @ref var_decl.
19518 ///
19519 /// If the two intances are different, set a bitfield to give some
19520 /// insight about the kind of differences there are.
19521 ///
19522 /// @param l the first artifact of the comparison.
19523 ///
19524 /// @param r the second artifact of the comparison.
19525 ///
19526 /// @param k a pointer to a bitfield that gives information about the
19527 /// kind of changes there are between @p l and @p r. This one is set
19528 /// iff @p k is non-null and the function returns false.
19529 ///
19530 /// Please note that setting k to a non-null value does have a
19531 /// negative performance impact because even if @p l and @p r are not
19532 /// equal, the function keeps up the comparison in order to determine
19533 /// the different kinds of ways in which they are different.
19534 ///
19535 /// @return true if @p l equals @p r, false otherwise.
19536 bool
19537 equals(const var_decl& l, const var_decl& r, change_kind* k)
19538 {
19539  bool result = true;
19540 
19541  // First test types of variables. This should be fast because in
19542  // the general case, most types should be canonicalized.
19543  if (*l.get_naked_type() != *r.get_naked_type())
19544  {
19545  result = false;
19546  if (k)
19547  {
19549  r.get_naked_type()))
19550  *k |= (LOCAL_TYPE_CHANGE_KIND);
19551  else
19552  *k |= SUBTYPE_CHANGE_KIND;
19553  }
19554  else
19556  }
19557 
19558  result &= var_equals_modulo_types(l, r, k);
19559 
19560  ABG_RETURN(result);
19561 }
19562 
19563 /// Comparison operator of @ref var_decl.
19564 ///
19565 /// @param o the instance of @ref var_decl to compare against.
19566 ///
19567 /// @return true iff the current instance of @ref var_decl equals @p o.
19568 bool
19570 {
19571  const var_decl* other = dynamic_cast<const var_decl*>(&o);
19572  if (!other)
19573  return false;
19574 
19575  return equals(*this, *other, 0);
19576 }
19577 
19578 /// Return an ID that tries to uniquely identify the variable inside a
19579 /// program or a library.
19580 ///
19581 /// So if the variable has an underlying elf symbol, the ID is the
19582 /// concatenation of the symbol name and its version. Otherwise, the
19583 /// ID is the linkage name if its non-null. Otherwise, it's the
19584 /// pretty representation of the variable.
19585 ///
19586 /// @return the ID.
19589 {
19590  if (priv_->id_.empty())
19591  {
19592  string repr = get_name();
19593  string sym_str;
19594  if (elf_symbol_sptr s = get_symbol())
19595  sym_str = s->get_id_string();
19596  else if (!get_linkage_name().empty())
19597  sym_str = get_linkage_name();
19598 
19599  const environment& env = get_type()->get_environment();
19600  priv_->id_ = env.intern(repr);
19601  if (!sym_str.empty())
19602  priv_->id_ = env.intern(priv_->id_ + "{" + sym_str + "}");
19603  }
19604  return priv_->id_;
19605 }
19606 
19607 /// Return the hash value for the current instance.
19608 ///
19609 /// @return the hash value.
19610 size_t
19612 {
19613  var_decl::hash hash_var;
19614  return hash_var(this);
19615 }
19616 
19617 /// Get the qualified name of a given variable or data member.
19618 ///
19619 ///
19620 /// Note that if the current instance of @ref var_decl is an anonymous
19621 /// data member, then the qualified name is actually the flat
19622 /// representation (the definition) of the type of the anonymous data
19623 /// member. We chose the flat representation because otherwise, the
19624 /// name of an *anonymous* data member is empty, by construction, e.g:
19625 ///
19626 /// struct foo {
19627 /// int a;
19628 /// union {
19629 /// char b;
19630 /// char c;
19631 /// }; // <---- this data member is anonymous.
19632 /// int d;
19633 /// }
19634 ///
19635 /// The string returned for the anonymous member here is going to be:
19636 ///
19637 /// "union {char b; char c}"
19638 ///
19639 /// @param internal if true then this is for a purpose to the library,
19640 /// otherwise, it's for being displayed to users.
19641 ///
19642 /// @return the resulting qualified name.
19643 const interned_string&
19644 var_decl::get_qualified_name(bool internal) const
19645 {
19646  if (is_anonymous_data_member(this)
19647  && decl_base::get_qualified_name().empty())
19648  {
19649  // Display the anonymous data member in a way that makes sense.
19650  string r = get_pretty_representation(internal);
19651  set_qualified_name(get_environment().intern(r));
19652  }
19653 
19654  return decl_base::get_qualified_name(internal);
19655 }
19656 
19657 /// Build and return the pretty representation of this variable.
19658 ///
19659 /// @param internal set to true if the call is intended to get a
19660 /// representation of the decl (or type) for the purpose of canonical
19661 /// type comparison. This is mainly used in the function
19662 /// type_base::get_canonical_type_for().
19663 ///
19664 /// In other words if the argument for this parameter is true then the
19665 /// call is meant for internal use (for technical use inside the
19666 /// library itself), false otherwise. If you don't know what this is
19667 /// for, then set it to false.
19668 ///
19669 /// @param qualified_name if true, names emitted in the pretty
19670 /// representation are fully qualified.
19671 ///
19672 /// @return a copy of the pretty representation of this variable.
19673 string
19674 var_decl::get_pretty_representation(bool internal, bool qualified_name) const
19675 {
19676  string result;
19677 
19678  if (is_member_decl(this) && get_member_is_static(this))
19679  result = "static ";
19680 
19681  // Detect if the current instance of var_decl is a member of
19682  // an anonymous class or union.
19683  bool member_of_anonymous_class = false;
19684  if (class_or_union* scope = is_at_class_scope(this))
19685  if (scope->get_is_anonymous())
19686  member_of_anonymous_class = true;
19687 
19689  {
19690  string name;
19691  if (member_of_anonymous_class || !qualified_name)
19692  name = get_name();
19693  else
19694  name = get_qualified_name(internal);
19695 
19696  type_base_sptr et = t->get_element_type();
19697  ABG_ASSERT(et);
19698  decl_base_sptr decl = get_type_declaration(et);
19699  ABG_ASSERT(decl);
19700  result += decl->get_qualified_name(internal)
19701  + " " + name + t->get_subrange_representation();
19702  }
19703  else
19704  {
19705  if (/*The current var_decl is to be used as an anonymous data
19706  member. */
19707  get_name().empty())
19708  {
19709  // Display the anonymous data member in a way that
19710  // makes sense.
19711  result +=
19714  "", /*one_line=*/true, internal);
19715  }
19716  else if (data_member_has_anonymous_type(this))
19717  {
19720  "", /*one_line=*/true, internal);
19721  result += " ";
19722  if (!internal
19723  && (member_of_anonymous_class || !qualified_name))
19724  // It doesn't make sense to name the member of an
19725  // anonymous class or union like:
19726  // "__anonymous__::data_member_name". So let's just use
19727  // its non-qualified name.
19728  result += get_name();
19729  else
19730  result += get_qualified_name(internal);
19731  }
19732  else
19733  {
19734  result +=
19736  + " ";
19737 
19738  if (!internal
19739  && (member_of_anonymous_class || !qualified_name))
19740  // It doesn't make sense to name the member of an
19741  // anonymous class or union like:
19742  // "__anonymous__::data_member_name". So let's just use
19743  // its non-qualified name.
19744  result += get_name();
19745  else
19746  result += get_qualified_name(internal);
19747  }
19748  }
19749  return result;
19750 }
19751 
19752 /// Get a name that is valid even for an anonymous data member.
19753 ///
19754 /// If the current @ref var_decl is an anonymous data member, then
19755 /// return its pretty representation. As of now, that pretty
19756 /// representation is actually its flat representation as returned by
19757 /// get_class_or_union_flat_representation().
19758 ///
19759 /// Otherwise, just return the name of the current @ref var_decl.
19760 ///
19761 /// @param qualified if true, return the qualified name. This doesn't
19762 /// have an effet if the current @ref var_decl represents an anonymous
19763 /// data member.
19764 string
19766 {
19767  string name;
19768  if (is_anonymous_data_member(this))
19769  // This function is used in the comparison engine to determine
19770  // which anonymous data member was deleted. So it's not involved
19771  // in type comparison or canonicalization. We don't want to use
19772  // the 'internal' version of the pretty presentation.
19773  name = get_pretty_representation(/*internal=*/false, qualified);
19774  else
19775  name = get_name();
19776 
19777  return name;
19778 }
19779 
19780 /// This implements the ir_traversable_base::traverse pure virtual
19781 /// function.
19782 ///
19783 /// @param v the visitor used on the current instance.
19784 ///
19785 /// @return true if the entire IR node tree got traversed, false
19786 /// otherwise.
19787 bool
19789 {
19790  if (visiting())
19791  return true;
19792 
19793  if (v.visit_begin(this))
19794  {
19795  visiting(true);
19796  if (type_base_sptr t = get_type())
19797  t->traverse(v);
19798  visiting(false);
19799  }
19800  return v.visit_end(this);
19801 }
19802 
19803 var_decl::~var_decl()
19804 {}
19805 
19806 // </var_decl definitions>
19807 
19808 /// This function is automatically invoked whenever an instance of
19809 /// this type is canonicalized.
19810 ///
19811 /// It's an overload of the virtual type_base::on_canonical_type_set.
19812 ///
19813 /// We put here what is thus meant to be executed only at the point of
19814 /// type canonicalization.
19815 void
19817 {
19818  priv_->cached_name_.clear();
19819  priv_->internal_cached_name_.clear();
19820 }
19821 
19822 /// The most straightforward constructor for the function_type class.
19823 ///
19824 /// @param return_type the return type of the function type.
19825 ///
19826 /// @param parms the list of parameters of the function type.
19827 /// Stricto sensu, we just need a list of types; we are using a list
19828 /// of parameters (where each parameter also carries the name of the
19829 /// parameter and its source location) to try and provide better
19830 /// diagnostics whenever it makes sense. If it appears that this
19831 /// wasts too many resources, we can fall back to taking just a
19832 /// vector of types here.
19833 ///
19834 /// @param size_in_bits the size of this type, in bits.
19835 ///
19836 /// @param alignment_in_bits the alignment of this type, in bits.
19837 ///
19838 /// @param size_in_bits the size of this type.
19839 function_type::function_type(type_base_sptr return_type,
19840  const parameters& parms,
19841  size_t size_in_bits,
19842  size_t alignment_in_bits)
19843  : type_or_decl_base(return_type->get_environment(),
19844  FUNCTION_TYPE | ABSTRACT_TYPE_BASE),
19845  type_base(return_type->get_environment(), size_in_bits, alignment_in_bits),
19846  priv_(new priv(parms, return_type))
19847 {
19848  runtime_type_instance(this);
19849 
19850  for (parameters::size_type i = 0, j = 1;
19851  i < priv_->parms_.size();
19852  ++i, ++j)
19853  {
19854  if (i == 0 && priv_->parms_[i]->get_is_artificial())
19855  // If the first parameter is artificial, then it certainly
19856  // means that this is a member function, and the first
19857  // parameter is the implicit this pointer. In that case, set
19858  // the index of that implicit parameter to zero. Otherwise,
19859  // the index of the first parameter starts at one.
19860  j = 0;
19861  priv_->parms_[i]->set_index(j);
19862  }
19863 }
19864 
19865 /// A constructor for a function_type that takes no parameters.
19866 ///
19867 /// @param return_type the return type of this function_type.
19868 ///
19869 /// @param size_in_bits the size of this type, in bits.
19870 ///
19871 /// @param alignment_in_bits the alignment of this type, in bits.
19872 function_type::function_type(type_base_sptr return_type,
19873  size_t size_in_bits, size_t alignment_in_bits)
19874  : type_or_decl_base(return_type->get_environment(),
19875  FUNCTION_TYPE | ABSTRACT_TYPE_BASE),
19876  type_base(return_type->get_environment(), size_in_bits, alignment_in_bits),
19877  priv_(new priv(return_type))
19878 {
19879  runtime_type_instance(this);
19880 }
19881 
19882 /// A constructor for a function_type that takes no parameter and
19883 /// that has no return_type yet. These missing parts can (and must)
19884 /// be added later.
19885 ///
19886 /// @param env the environment we are operating from.
19887 ///
19888 /// @param size_in_bits the size of this type, in bits.
19889 ///
19890 /// @param alignment_in_bits the alignment of this type, in bits.
19891 function_type::function_type(const environment& env,
19892  size_t size_in_bits,
19893  size_t alignment_in_bits)
19894  : type_or_decl_base(env, FUNCTION_TYPE | ABSTRACT_TYPE_BASE),
19895  type_base(env, size_in_bits, alignment_in_bits),
19896  priv_(new priv)
19897 {
19898  runtime_type_instance(this);
19899 }
19900 
19901 /// Getter for the return type of the current instance of @ref
19902 /// function_type.
19903 ///
19904 /// @return the return type.
19905 type_base_sptr
19907 {return priv_->return_type_.lock();}
19908 
19909 /// Setter of the return type of the current instance of @ref
19910 /// function_type.
19911 ///
19912 /// @param t the new return type to set.
19913 void
19915 {priv_->return_type_ = t;}
19916 
19917 /// Getter for the set of parameters of the current intance of @ref
19918 /// function_type.
19919 ///
19920 /// @return the parameters of the current instance of @ref
19921 /// function_type.
19924 {return priv_->parms_;}
19925 
19926 /// Get the Ith parameter of the vector of parameters of the current
19927 /// instance of @ref function_type.
19928 ///
19929 /// Note that the first parameter is at index 0. That parameter is
19930 /// the first parameter that comes after the possible implicit "this"
19931 /// parameter, when the current instance @ref function_type is for a
19932 /// member function. Otherwise, if the current instance of @ref
19933 /// function_type is for a non-member function, the parameter at index
19934 /// 0 is the first parameter of the function.
19935 ///
19936 ///
19937 /// @param i the index of the parameter to return. If i is greater
19938 /// than the index of the last parameter, then this function returns
19939 /// an empty parameter (smart) pointer.
19940 ///
19941 /// @return the @p i th parameter that is not implicit.
19944 {
19945  parameter_sptr result;
19946  if (dynamic_cast<const method_type*>(this))
19947  {
19948  if (i + 1 < get_parameters().size())
19949  result = get_parameters()[i + 1];
19950  }
19951  else
19952  {
19953  if (i < get_parameters().size())
19954  result = get_parameters()[i];
19955  }
19956  return result;
19957 }
19958 
19959 /// Setter for the parameters of the current instance of @ref
19960 /// function_type.
19961 ///
19962 /// @param p the new vector of parameters to set.
19963 void
19965 {
19966  priv_->parms_ = p;
19967  for (parameters::size_type i = 0, j = 1;
19968  i < priv_->parms_.size();
19969  ++i, ++j)
19970  {
19971  if (i == 0 && priv_->parms_[i]->get_is_artificial())
19972  // If the first parameter is artificial, then it certainly
19973  // means that this is a member function, and the first
19974  // parameter is the implicit this pointer. In that case, set
19975  // the index of that implicit parameter to zero. Otherwise,
19976  // the index of the first parameter starts at one.
19977  j = 0;
19978  priv_->parms_[i]->set_index(j);
19979  }
19980 }
19981 
19982 /// Append a new parameter to the vector of parameters of the current
19983 /// instance of @ref function_type.
19984 ///
19985 /// @param parm the parameter to append.
19986 void
19988 {
19989  parm->set_index(priv_->parms_.size());
19990  priv_->parms_.push_back(parm);
19991 }
19992 
19993 /// Test if the current instance of @ref function_type is for a
19994 /// variadic function.
19995 ///
19996 /// A variadic function is a function that takes a variable number of
19997 /// arguments.
19998 ///
19999 /// @return true iff the current instance of @ref function_type is for
20000 /// a variadic function.
20001 bool
20003 {
20004  return (!priv_->parms_.empty()
20005  && priv_->parms_.back()->get_variadic_marker());
20006 }
20007 
20008 /// Compare two function types.
20009 ///
20010 /// In case these function types are actually method types, this
20011 /// function avoids comparing two parameters (of the function types)
20012 /// if the types of the parameters are actually the types of the
20013 /// classes of the method types. This prevents infinite recursion
20014 /// during the comparison of two classes that are structurally
20015 /// identical.
20016 ///
20017 /// This is a subroutine of the equality operator of function_type.
20018 ///
20019 /// @param lhs the first function type to consider
20020 ///
20021 /// @param rhs the second function type to consider
20022 ///
20023 /// @param k a pointer to a bitfield set by the function to give
20024 /// information about the kind of changes carried by @p lhs and @p
20025 /// rhs. It is set iff @p k is non-null and the function returns
20026 /// false.
20027 ///
20028 /// Please note that setting k to a non-null value does have a
20029 /// negative performance impact because even if @p l and @p r are not
20030 /// equal, the function keeps up the comparison in order to determine
20031 /// the different kinds of ways in which they are different.
20032 ///
20033 ///@return true if lhs == rhs, false otherwise.
20034 bool
20036 {
20037 #define RETURN(value) CACHE_AND_RETURN_COMPARISON_RESULT(value)
20038 
20040 
20041  {
20042  // First of all, let's see if these two function types haven't
20043  // already been compared. If so, and if the result of the
20044  // comparison has been cached, let's just re-use it, rather than
20045  // comparing them all over again.
20046  bool cached_result = false;
20047  if (l.get_environment().priv_->is_type_comparison_cached(l, r,
20048  cached_result))
20049  return cached_result;
20050  }
20051 
20053 
20054  bool result = true;
20055 
20056  if (!l.type_base::operator==(r))
20057  {
20058  result = false;
20059  if (k)
20060  *k |= LOCAL_TYPE_CHANGE_KIND;
20061  else
20062  RETURN(result);
20063  }
20064 
20065  class_or_union* l_class = 0, *r_class = 0;
20066  if (const method_type* m = dynamic_cast<const method_type*>(&l))
20067  l_class = m->get_class_type().get();
20068 
20069  if (const method_type* m = dynamic_cast<const method_type*>(&r))
20070  r_class = m->get_class_type().get();
20071 
20072  // Compare the names of the class of the method
20073 
20074  if (!!l_class != !!r_class)
20075  {
20076  result = false;
20077  if (k)
20078  *k |= LOCAL_TYPE_CHANGE_KIND;
20079  else
20080  RETURN(result);
20081  }
20082  else if (l_class
20083  && (l_class->get_qualified_name()
20084  != r_class->get_qualified_name()))
20085  {
20086  result = false;
20087  if (k)
20088  *k |= LOCAL_TYPE_CHANGE_KIND;
20089  else
20090  RETURN(result);
20091  }
20092 
20093  // Then compare the return type; Beware if it's t's a class type
20094  // that is the same as the method class name; we can recurse for
20095  // ever in that case.
20096 
20097  decl_base* l_return_type_decl =
20099  decl_base* r_return_type_decl =
20101  bool compare_result_types = true;
20102  string l_rt_name = l_return_type_decl
20103  ? l_return_type_decl->get_qualified_name()
20104  : string();
20105  string r_rt_name = r_return_type_decl
20106  ? r_return_type_decl->get_qualified_name()
20107  : string();
20108 
20109  if ((l_class && (l_class->get_qualified_name() == l_rt_name))
20110  ||
20111  (r_class && (r_class->get_qualified_name() == r_rt_name)))
20112  compare_result_types = false;
20113 
20114  if (compare_result_types)
20115  {
20116  // Let's not consider typedefs when comparing return types to
20117  // avoid spurious changes.
20118  //
20119  // TODO: We should also do this for parameter types, or rather,
20120  // we should teach the equality operators in the IR, at some
20121  // point, to peel typedefs off.
20123  !=
20125  {
20126  result = false;
20127  if (k)
20128  {
20130  r.get_return_type()))
20131  *k |= LOCAL_TYPE_CHANGE_KIND;
20132  else
20133  *k |= SUBTYPE_CHANGE_KIND;
20134  }
20135  else
20136  RETURN(result);
20137  }
20138  }
20139  else
20140  if (l_rt_name != r_rt_name)
20141  {
20142  result = false;
20143  if (k)
20144  *k |= SUBTYPE_CHANGE_KIND;
20145  else
20146  RETURN(result);
20147  }
20148 
20149  vector<shared_ptr<function_decl::parameter> >::const_iterator i,j;
20150  for (i = l.get_first_parm(), j = r.get_first_parm();
20151  i != l.get_parameters().end() && j != r.get_parameters().end();
20152  ++i, ++j)
20153  {
20154  if (**i != **j)
20155  {
20156  result = false;
20157  if (k)
20158  {
20159  if (!types_have_similar_structure((*i)->get_type(),
20160  (*j)->get_type()))
20161  *k |= LOCAL_TYPE_CHANGE_KIND;
20162  else
20163  *k |= SUBTYPE_CHANGE_KIND;
20164  }
20165  else
20166  RETURN(result);
20167  }
20168  }
20169 
20170  if ((i != l.get_parameters().end()
20171  || j != r.get_parameters().end()))
20172  {
20173  result = false;
20174  if (k)
20176  else
20177  RETURN(result);
20178  }
20179 
20180  RETURN(result);
20181 #undef RETURN
20182 }
20183 
20184 /// Get the first parameter of the function.
20185 ///
20186 /// If the function is a non-static member function, the parameter
20187 /// returned is the first one following the implicit 'this' parameter.
20188 ///
20189 /// @return the first non implicit parameter of the function.
20190 function_type::parameters::const_iterator
20192 {
20193  if (get_parameters().empty())
20194  return get_parameters().end();
20195 
20196  bool is_method = dynamic_cast<const method_type*>(this);
20197 
20198  parameters::const_iterator i = get_parameters().begin();
20199 
20200  if (is_method)
20201  ++i;
20202 
20203  return i;
20204 }
20205 
20206 /// Get the first parameter of the function.
20207 ///
20208 /// Note that if the function is a non-static member function, the
20209 /// parameter returned is the implicit 'this' parameter.
20210 ///
20211 /// @return the first parameter of the function.
20212 function_type::parameters::const_iterator
20214 {return get_parameters().begin();}
20215 
20216 /// Get the name of the current @ref function_type.
20217 ///
20218 /// The name is retrieved from a cache. If the cache is empty, this
20219 /// function computes the name of the type, stores it in the cache and
20220 /// returns it. Subsequent invocation of the function are going to
20221 /// just hit the cache.
20222 ///
20223 /// Note that if the type is *NOT* canonicalized then function type
20224 /// name is never cached.
20225 ///
20226 /// @param internal if true then it means the function type name is
20227 /// going to be used for purposes that are internal to libabigail
20228 /// itself. If you don't know what this is then you probably should
20229 /// set this parameter to 'false'.
20230 ///
20231 /// @return the name of the function type.
20232 const interned_string&
20234 {
20235  if (internal)
20236  {
20238  {
20239  if (priv_->internal_cached_name_.empty())
20240  priv_->internal_cached_name_ =
20241  get_function_type_name(this, /*internal=*/true);
20242  return priv_->internal_cached_name_;
20243  }
20244  else
20245  {
20246  priv_->temp_internal_cached_name_ =
20248  /*internal=*/true);
20249  return priv_->temp_internal_cached_name_;
20250  }
20251  }
20252  else
20253  {
20255  {
20256  if (priv_->cached_name_.empty())
20257  priv_->cached_name_ =
20258  get_function_type_name(this, /*internal=*/false);
20259  return priv_->cached_name_;
20260  }
20261  else
20262  {
20263  priv_->cached_name_ =
20264  get_function_type_name(this, /*internal=*/false);
20265  return priv_->cached_name_;
20266  }
20267  }
20268 }
20269 
20270 /// Equality operator for function_type.
20271 ///
20272 /// @param o the other function_type to compare against.
20273 ///
20274 /// @return true iff the two function_type are equal.
20275 bool
20277 {
20278  const function_type* o = dynamic_cast<const function_type*>(&other);
20279  if (!o)
20280  return false;
20281  return try_canonical_compare(this, o);
20282 }
20283 
20284 /// Return a copy of the pretty representation of the current @ref
20285 /// function_type.
20286 ///
20287 /// @param internal set to true if the call is intended to get a
20288 /// representation of the decl (or type) for the purpose of canonical
20289 /// type comparison. This is mainly used in the function
20290 /// type_base::get_canonical_type_for().
20291 ///
20292 /// In other words if the argument for this parameter is true then the
20293 /// call is meant for internal use (for technical use inside the
20294 /// library itself), false otherwise. If you don't know what this is
20295 /// for, then set it to false.
20296 ///
20297 /// @return a copy of the pretty representation of the current @ref
20298 /// function_type.
20299 string
20301  bool /*qualified_name*/) const
20302 {return ir::get_pretty_representation(this, internal);}
20303 
20304 /// Traverses an instance of @ref function_type, visiting all the
20305 /// sub-types and decls that it might contain.
20306 ///
20307 /// @param v the visitor that is used to visit every IR sub-node of
20308 /// the current node.
20309 ///
20310 /// @return true if either
20311 /// - all the children nodes of the current IR node were traversed
20312 /// and the calling code should keep going with the traversing.
20313 /// - or the current IR node is already being traversed.
20314 /// Otherwise, returning false means that the calling code should not
20315 /// keep traversing the tree.
20316 bool
20318 {
20319  // TODO: should we allow the walker to avoid visiting function type
20320  // twice? I think that if we do, then ir_node_visitor needs an
20321  // option to specifically disallow this feature for function types.
20322 
20323  if (visiting())
20324  return true;
20325 
20326  if (v.visit_begin(this))
20327  {
20328  visiting(true);
20329  bool keep_going = true;
20330 
20331  if (type_base_sptr t = get_return_type())
20332  {
20333  if (!t->traverse(v))
20334  keep_going = false;
20335  }
20336 
20337  if (keep_going)
20338  for (parameters::const_iterator i = get_parameters().begin();
20339  i != get_parameters().end();
20340  ++i)
20341  if (type_base_sptr parm_type = (*i)->get_type())
20342  if (!parm_type->traverse(v))
20343  break;
20344 
20345  visiting(false);
20346  }
20347  return v.visit_end(this);
20348 }
20349 
20350 function_type::~function_type()
20351 {}
20352 // </function_type>
20353 
20354 // <method_type>
20355 
20356 struct method_type::priv
20357 {
20358  class_or_union_wptr class_type_;
20359  bool is_const;
20360 
20361  priv()
20362  : is_const()
20363  {}
20364 }; // end struct method_type::priv
20365 
20366 /// Constructor for instances of method_type.
20367 ///
20368 /// Instances of method_decl must be of type method_type.
20369 ///
20370 /// @param return_type the type of the return value of the method.
20371 ///
20372 /// @param class_type the base type of the method type. That is, the
20373 /// type of the class the method belongs to.
20374 ///
20375 /// @param p the vector of the parameters of the method.
20376 ///
20377 /// @param is_const whether this method type is for a const method.
20378 /// Note that const-ness is a property of the method *type* and of the
20379 /// relationship between a method *declaration* and its scope.
20380 ///
20381 /// @param size_in_bits the size of an instance of method_type,
20382 /// expressed in bits.
20383 ///
20384 /// @param alignment_in_bits the alignment of an instance of
20385 /// method_type, expressed in bits.
20386 method_type::method_type (type_base_sptr return_type,
20387  class_or_union_sptr class_type,
20388  const std::vector<function_decl::parameter_sptr>& p,
20389  bool is_const,
20390  size_t size_in_bits,
20391  size_t alignment_in_bits)
20392  : type_or_decl_base(class_type->get_environment(),
20393  METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
20394  type_base(class_type->get_environment(), size_in_bits, alignment_in_bits),
20395  function_type(return_type, p, size_in_bits, alignment_in_bits),
20396  priv_(new priv)
20397 {
20398  runtime_type_instance(this);
20399  set_class_type(class_type);
20400  set_is_const(is_const);
20401 }
20402 
20403 /// Constructor of instances of method_type.
20404 ///
20405 ///Instances of method_decl must be of type method_type.
20406 ///
20407 /// @param return_type the type of the return value of the method.
20408 ///
20409 /// @param class_type the type of the class the method belongs to.
20410 /// The actual (dynamic) type of class_type must be a pointer
20411 /// class_type. We are setting it to pointer to type_base here to
20412 /// help client code that is compiled without rtti and thus cannot
20413 /// perform dynamic casts.
20414 ///
20415 /// @param p the vector of the parameters of the method type.
20416 ///
20417 /// @param is_const whether this method type is for a const method.
20418 /// Note that const-ness is a property of the method *type* and of the
20419 /// relationship between a method *declaration* and its scope.
20420 ///
20421 /// @param size_in_bits the size of an instance of method_type,
20422 /// expressed in bits.
20423 ///
20424 /// @param alignment_in_bits the alignment of an instance of
20425 /// method_type, expressed in bits.
20426 method_type::method_type(type_base_sptr return_type,
20427  type_base_sptr class_type,
20428  const std::vector<function_decl::parameter_sptr>& p,
20429  bool is_const,
20430  size_t size_in_bits,
20431  size_t alignment_in_bits)
20432  : type_or_decl_base(class_type->get_environment(),
20433  METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
20434  type_base(class_type->get_environment(), size_in_bits, alignment_in_bits),
20435  function_type(return_type, p, size_in_bits, alignment_in_bits),
20436  priv_(new priv)
20437 {
20438  runtime_type_instance(this);
20439  set_class_type(is_class_type(class_type));
20440  set_is_const(is_const);
20441 }
20442 
20443 /// Constructor of the qualified_type_def
20444 ///
20445 /// @param env the environment we are operating from.
20446 ///
20447 /// @param size_in_bits the size of the type, expressed in bits.
20448 ///
20449 /// @param alignment_in_bits the alignment of the type, expressed in bits
20450 method_type::method_type(const environment& env,
20451  size_t size_in_bits,
20452  size_t alignment_in_bits)
20453  : type_or_decl_base(env, METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
20454  type_base(env, size_in_bits, alignment_in_bits),
20455  function_type(env, size_in_bits, alignment_in_bits),
20456  priv_(new priv)
20457 {
20458  runtime_type_instance(this);
20459 }
20460 
20461 /// Constructor of instances of method_type.
20462 ///
20463 /// When constructed with this constructor, and instane of method_type
20464 /// must set a return type using method_type::set_return_type
20465 ///
20466 /// @param class_typ the base type of the method type. That is, the
20467 /// type of the class (or union) the method belongs to.
20468 ///
20469 /// @param size_in_bits the size of an instance of method_type,
20470 /// expressed in bits.
20471 ///
20472 /// @param alignment_in_bits the alignment of an instance of
20473 /// method_type, expressed in bits.
20474 method_type::method_type(class_or_union_sptr class_type,
20475  bool is_const,
20476  size_t size_in_bits,
20477  size_t alignment_in_bits)
20478  : type_or_decl_base(class_type->get_environment(),
20479  METHOD_TYPE | ABSTRACT_TYPE_BASE | FUNCTION_TYPE),
20480  type_base(class_type->get_environment(), size_in_bits, alignment_in_bits),
20481  function_type(class_type->get_environment(),
20482  size_in_bits,
20483  alignment_in_bits),
20484  priv_(new priv)
20485 {
20486  runtime_type_instance(this);
20487  set_class_type(class_type);
20488  set_is_const(is_const);
20489 }
20490 
20491 /// Get the class type this method belongs to.
20492 ///
20493 /// @return the class type.
20494 class_or_union_sptr
20496 {return class_or_union_sptr(priv_->class_type_);}
20497 
20498 /// Sets the class type of the current instance of method_type.
20499 ///
20500 /// The class type is the type of the class the method belongs to.
20501 ///
20502 /// @param t the new class type to set.
20503 void
20504 method_type::set_class_type(const class_or_union_sptr& t)
20505 {
20506  if (!t)
20507  return;
20508 
20509  priv_->class_type_ = t;
20510 }
20511 
20512 /// Return a copy of the pretty representation of the current @ref
20513 /// method_type.
20514 ///
20515 /// @param internal set to true if the call is intended to get a
20516 /// representation of the decl (or type) for the purpose of canonical
20517 /// type comparison. This is mainly used in the function
20518 /// type_base::get_canonical_type_for().
20519 ///
20520 /// In other words if the argument for this parameter is true then the
20521 /// call is meant for internal use (for technical use inside the
20522 /// library itself), false otherwise. If you don't know what this is
20523 /// for, then set it to false.
20524 ///
20525 /// @return a copy of the pretty representation of the current @ref
20526 /// method_type.
20527 string
20529  bool /*qualified_name*/) const
20530 {return ir::get_pretty_representation(*this, internal);}
20531 
20532 /// Setter of the "is-const" property of @ref method_type.
20533 ///
20534 /// @param the new value of the "is-const" property.
20535 void
20537 {priv_->is_const = f;}
20538 
20539 /// Getter of the "is-const" property of @ref method_type.
20540 ///
20541 /// @return true iff the "is-const" property was set.
20542 bool
20544 {return priv_->is_const;}
20545 
20546 /// The destructor of method_type
20548 {}
20549 
20550 // </method_type>
20551 
20552 // <function_decl definitions>
20553 
20554 struct function_decl::priv
20555 {
20556  bool declared_inline_;
20557  decl_base::binding binding_;
20558  function_type_wptr type_;
20559  function_type* naked_type_;
20560  elf_symbol_sptr symbol_;
20561  interned_string id_;
20562 
20563  priv()
20564  : declared_inline_(false),
20565  binding_(decl_base::BINDING_GLOBAL),
20566  naked_type_()
20567  {}
20568 
20569  priv(function_type_sptr t,
20570  bool declared_inline,
20572  : declared_inline_(declared_inline),
20573  binding_(binding),
20574  type_(t),
20575  naked_type_(t.get())
20576  {}
20577 
20578  priv(function_type_sptr t,
20579  bool declared_inline,
20581  elf_symbol_sptr s)
20582  : declared_inline_(declared_inline),
20583  binding_(binding),
20584  type_(t),
20585  naked_type_(t.get()),
20586  symbol_(s)
20587  {}
20588 }; // end sruct function_decl::priv
20589 
20590 /// Constructor of the @ref function_decl.
20591 ///
20592 /// @param name the name of the function.
20593 ///
20594 /// @param function_type the type of the function.
20595 ///
20596 /// @param declared_inline wether the function is declared inline.
20597 ///
20598 /// @param locus the source location of the function.
20599 ///
20600 /// @param mangled_name the linkage name of the function.
20601 ///
20602 /// @param vis the visibility of the function.
20603 ///
20604 /// @param bind the binding of the function.
20605 function_decl::function_decl(const string& name,
20607  bool declared_inline,
20608  const location& locus,
20609  const string& mangled_name,
20610  visibility vis,
20611  binding bind)
20612  : type_or_decl_base(function_type->get_environment(),
20613  FUNCTION_DECL | ABSTRACT_DECL_BASE),
20614  decl_base(function_type->get_environment(), name, locus, mangled_name, vis),
20615  priv_(new priv(function_type, declared_inline, bind))
20616 {
20617  runtime_type_instance(this);
20618 }
20619 
20620 /// Constructor of the function_decl type.
20621 ///
20622 /// This flavour of constructor is for when the pointer to the
20623 /// instance of function_type that the client code has is presented as
20624 /// a pointer to type_base. In that case, this constructor saves the
20625 /// client code from doing a dynamic_cast to get the function_type
20626 /// pointer.
20627 ///
20628 /// @param name the name of the function declaration.
20629 ///
20630 /// @param fn_type the type of the function declaration. The dynamic
20631 /// type of this parameter should be 'pointer to function_type'
20632 ///
20633 /// @param declared_inline whether this function was declared inline
20634 ///
20635 /// @param locus the source location of the function declaration.
20636 ///
20637 /// @param linkage_name the mangled name of the function declaration.
20638 ///
20639 /// @param vis the visibility of the function declaration.
20640 ///
20641 /// @param bind the kind of the binding of the function
20642 /// declaration.
20643 function_decl::function_decl(const string& name,
20644  type_base_sptr fn_type,
20645  bool declared_inline,
20646  const location& locus,
20647  const string& linkage_name,
20648  visibility vis,
20649  binding bind)
20650  : type_or_decl_base(fn_type->get_environment(),
20651  FUNCTION_DECL | ABSTRACT_DECL_BASE),
20652  decl_base(fn_type->get_environment(), name, locus, linkage_name, vis),
20653  priv_(new priv(dynamic_pointer_cast<function_type>(fn_type),
20654  declared_inline,
20655  bind))
20656 {
20657  runtime_type_instance(this);
20658 }
20659 
20660 /// Get the pretty representation of the current instance of @ref function_decl.
20661 ///
20662 /// @param internal set to true if the call is intended to get a
20663 /// representation of the decl (or type) for the purpose of canonical
20664 /// type comparison. This is mainly used in the function
20665 /// type_base::get_canonical_type_for().
20666 ///
20667 /// In other words if the argument for this parameter is true then the
20668 /// call is meant for internal use (for technical use inside the
20669 /// library itself), false otherwise. If you don't know what this is
20670 /// for, then set it to false.
20671 ///
20672 /// @return the pretty representation for a function.
20673 string
20675  bool /*qualified_name*/) const
20676 {
20677  const method_decl* mem_fn =
20678  dynamic_cast<const method_decl*>(this);
20679 
20680  string result = mem_fn ? "method ": "function ";
20681 
20682  if (mem_fn
20683  && is_member_function(mem_fn)
20684  && get_member_function_is_virtual(mem_fn))
20685  result += "virtual ";
20686 
20687  decl_base_sptr type;
20688  if ((mem_fn
20689  && is_member_function(mem_fn)
20690  && (get_member_function_is_dtor(*mem_fn)
20691  || get_member_function_is_ctor(*mem_fn))))
20692  /*cdtors do not have return types. */;
20693  else
20694  type = mem_fn
20695  ? get_type_declaration(mem_fn->get_type()->get_return_type())
20697 
20698  if (type)
20699  result += type->get_qualified_name(internal) + " ";
20700 
20701  result += get_pretty_representation_of_declarator(internal);
20702 
20703  return result;
20704 }
20705 
20706 /// Compute and return the pretty representation for the part of the
20707 /// function declaration that starts at the declarator. That is, the
20708 /// return type and the other specifiers of the beginning of the
20709 /// function's declaration ar omitted.
20710 ///
20711 /// @param internal set to true if the call is intended to get a
20712 /// representation of the decl (or type) for the purpose of canonical
20713 /// type comparison. This is mainly used in the function
20714 /// type_base::get_canonical_type_for().
20715 ///
20716 /// In other words if the argument for this parameter is true then the
20717 /// call is meant for internal use (for technical use inside the
20718 /// library itself), false otherwise. If you don't know what this is
20719 /// for, then set it to false.
20720 ///
20721 /// @return the pretty representation for the part of the function
20722 /// declaration that starts at the declarator.
20723 string
20725 {
20726  const method_decl* mem_fn =
20727  dynamic_cast<const method_decl*>(this);
20728 
20729  string result;
20730 
20731  if (mem_fn)
20732  {
20733  result += mem_fn->get_type()->get_class_type()->get_qualified_name()
20734  + "::" + mem_fn->get_name();
20735  }
20736  else
20737  result += get_qualified_name();
20738 
20739  result += "(";
20740 
20741  parameters::const_iterator i = get_parameters().begin(),
20742  end = get_parameters().end();
20743 
20744  // Skip the first parameter if this is a method.
20745  if (mem_fn && i != end)
20746  ++i;
20747  parameter_sptr parm;
20748  parameter_sptr first_parm;
20749  if (i != end)
20750  first_parm = *i;
20751  for (; i != end; ++i)
20752  {
20753  parm = *i;
20754  if (parm.get() != first_parm.get())
20755  result += ", ";
20756  if (parm->get_variadic_marker()
20757  || get_environment().is_variadic_parameter_type(parm->get_type()))
20758  result += "...";
20759  else
20760  {
20761  type_base_sptr type = parm->get_type();
20762  if (internal)
20763  type = peel_typedef_type(type);
20764  result += get_type_name(type, /*qualified=*/true, internal);
20765  }
20766  }
20767  result += ")";
20768 
20769  if (mem_fn
20770  &&((is_member_function(mem_fn) && get_member_function_is_const(*mem_fn))
20771  || is_method_type(mem_fn->get_type())->get_is_const()))
20772  result += " const";
20773 
20774  return result;
20775 }
20776 
20777 /// Getter for the first non-implicit parameter of a function decl.
20778 ///
20779 /// If the function is a non-static member function, the parameter
20780 /// returned is the first one following the implicit 'this' parameter.
20781 ///
20782 /// @return the first non implicit parm.
20783 function_decl::parameters::const_iterator
20785 {
20786  if (get_parameters().empty())
20787  return get_parameters().end();
20788 
20789  bool is_method = dynamic_cast<const method_decl*>(this);
20790 
20791  parameters::const_iterator i = get_parameters().begin();
20792  if (is_method)
20793  ++i;
20794 
20795  return i;
20796 }
20797 
20798 /// Return the type of the current instance of @ref function_decl.
20799 ///
20800 /// It's either a function_type or method_type.
20801 /// @return the type of the current instance of @ref function_decl.
20802 const shared_ptr<function_type>
20804 {return priv_->type_.lock();}
20805 
20806 /// Fast getter of the type of the current instance of @ref function_decl.
20807 ///
20808 /// Note that this function returns the underlying pointer managed by
20809 /// the smart pointer returned by function_decl::get_type(). It's
20810 /// faster than function_decl::get_type(). This getter is to be used
20811 /// in code paths that are proven to be performance hot spots;
20812 /// especially (for instance) when comparing function types. Those
20813 /// are compared extremely frequently when libabigail is used to
20814 /// handle huge binaries with a lot of functions.
20815 ///
20816 /// @return the type of the current instance of @ref function_decl.
20817 const function_type*
20819 {return priv_->naked_type_;}
20820 
20821 void
20822 function_decl::set_type(const function_type_sptr& fn_type)
20823 {
20824  priv_->type_ = fn_type;
20825  priv_->naked_type_ = fn_type.get();
20826 }
20827 
20828 /// This sets the underlying ELF symbol for the current function decl.
20829 ///
20830 /// And underlyin$g ELF symbol for the current function decl might
20831 /// exist only if the corpus that this function decl originates from
20832 /// was constructed from an ELF binary file.
20833 ///
20834 /// Note that comparing two function decls that have underlying ELF
20835 /// symbols involves comparing their underlying elf symbols. The decl
20836 /// name for the function thus becomes irrelevant in the comparison.
20837 ///
20838 /// @param sym the new ELF symbol for this function decl.
20839 void
20841 {
20842  priv_->symbol_ = sym;
20843  // The function id cache that depends on the symbol must be
20844  // invalidated because the symbol changed.
20845  priv_->id_ = get_environment().intern("");
20846 }
20847 
20848 /// Gets the the underlying ELF symbol for the current variable,
20849 /// that was set using function_decl::set_symbol(). Please read the
20850 /// documentation for that member function for more information about
20851 /// "underlying ELF symbols".
20852 ///
20853 /// @return sym the underlying ELF symbol for this function decl, if
20854 /// one exists.
20855 const elf_symbol_sptr&
20857 {return priv_->symbol_;}
20858 
20859 bool
20860 function_decl::is_declared_inline() const
20861 {return priv_->declared_inline_;}
20862 
20864 function_decl::get_binding() const
20865 {return priv_->binding_;}
20866 
20867 /// @return the return type of the current instance of function_decl.
20868 const shared_ptr<type_base>
20870 {return get_type()->get_return_type();}
20871 
20872 /// @return the parameters of the function.
20873 const std::vector<shared_ptr<function_decl::parameter> >&
20875 {return get_type()->get_parameters();}
20876 
20877 /// Append a parameter to the type of this function.
20878 ///
20879 /// @param parm the parameter to append.
20880 void
20881 function_decl::append_parameter(shared_ptr<parameter> parm)
20882 {get_type()->append_parameter(parm);}
20883 
20884 /// Append a vector of parameters to the type of this function.
20885 ///
20886 /// @param parms the vector of parameters to append.
20887 void
20888 function_decl::append_parameters(std::vector<shared_ptr<parameter> >& parms)
20889 {
20890  for (std::vector<shared_ptr<parameter> >::const_iterator i = parms.begin();
20891  i != parms.end();
20892  ++i)
20893  get_type()->append_parameter(*i);
20894 }
20895 
20896 /// Create a new instance of function_decl that is a clone of the
20897 /// current one.
20898 ///
20899 /// @return the new clone.
20902 {
20904  if (is_member_function(*this))
20905  {
20906  method_decl_sptr
20907  m(new method_decl(get_name(),
20908  get_type(),
20909  is_declared_inline(),
20910  get_location(),
20911  get_linkage_name(),
20912  get_visibility(),
20913  get_binding()));
20915  ABG_ASSERT(scope);
20919  get_member_is_static(*this),
20923  f = m;
20924  }
20925  else
20926  {
20927  f.reset(new function_decl(get_name(),
20928  get_type(),
20929  is_declared_inline(),
20930  get_location(),
20931  get_linkage_name(),
20932  get_visibility(),
20933  get_binding()));
20935  }
20936  f->set_symbol(get_symbol());
20937 
20938  return f;
20939 }
20940 
20941 /// Compares two instances of @ref function_decl.
20942 ///
20943 /// If the two intances are different, set a bitfield to give some
20944 /// insight about the kind of differences there are.
20945 ///
20946 /// @param l the first artifact of the comparison.
20947 ///
20948 /// @param r the second artifact of the comparison.
20949 ///
20950 /// @param k a pointer to a bitfield that gives information about the
20951 /// kind of changes there are between @p l and @p r. This one is set
20952 /// iff @p k is non-null and the function returns false.
20953 ///
20954 /// Please note that setting k to a non-null value does have a
20955 /// negative performance impact because even if @p l and @p r are not
20956 /// equal, the function keeps up the comparison in order to determine
20957 /// the different kinds of ways in which they are different.
20958 ///
20959 /// @return true if @p l equals @p r, false otherwise.
20960 bool
20962 {
20963  bool result = true;
20964 
20965  // Compare function types
20966  const type_base* t0 = l.get_naked_type(), *t1 = r.get_naked_type();
20967  if (t0 == t1 || *t0 == *t1)
20968  ; // the types are equal, let's move on to compare the other
20969  // properties of the functions.
20970  else
20971  {
20972  result = false;
20973  if (k)
20974  {
20975  if (!types_have_similar_structure(t0, t1))
20976  *k |= LOCAL_TYPE_CHANGE_KIND;
20977  else
20978  *k |= SUBTYPE_CHANGE_KIND;
20979  }
20980  else
20982  }
20983 
20984  const elf_symbol_sptr &s0 = l.get_symbol(), &s1 = r.get_symbol();
20985  if (!!s0 != !!s1)
20986  {
20987  result = false;
20988  if (k)
20990  else
20992  }
20993  else if (s0 && s0 != s1)
20994  {
20995  if (!elf_symbols_alias(s0, s1))
20996  {
20997  result = false;
20998  if (k)
21000  else
21002  }
21003  }
21004  bool symbols_are_equal = (s0 && s1 && result);
21005 
21006  if (symbols_are_equal)
21007  {
21008  // The functions have underlying elf symbols that are equal,
21009  // so now, let's compare the decl_base part of the functions
21010  // w/o considering their decl names.
21011  interned_string n1 = l.get_name(), n2 = r.get_name();
21012  interned_string ln1 = l.get_linkage_name(), ln2 = r.get_linkage_name();
21013  const_cast<function_decl&>(l).set_name("");
21014  const_cast<function_decl&>(l).set_linkage_name("");
21015  const_cast<function_decl&>(r).set_name("");
21016  const_cast<function_decl&>(r).set_linkage_name("");
21017 
21018  bool decl_bases_different = !l.decl_base::operator==(r);
21019 
21020  const_cast<function_decl&>(l).set_name(n1);
21021  const_cast<function_decl&>(l).set_linkage_name(ln1);
21022  const_cast<function_decl&>(r).set_name(n2);
21023  const_cast<function_decl&>(r).set_linkage_name(ln2);
21024 
21025  if (decl_bases_different)
21026  {
21027  result = false;
21028  if (k)
21030  else
21032  }
21033  }
21034  else
21035  if (!l.decl_base::operator==(r))
21036  {
21037  result = false;
21038  if (k)
21040  else
21042  }
21043 
21044  // Compare the remaining properties
21045  if (l.is_declared_inline() != r.is_declared_inline()
21046  || l.get_binding() != r.get_binding())
21047  {
21048  result = false;
21049  if (k)
21051  else
21053  }
21054 
21056  {
21057  result = false;
21058  if (k)
21060  else
21062  }
21063 
21065  {
21066  if (!((get_member_function_is_ctor(l)
21070  && (get_member_is_static(l)
21071  == get_member_is_static(r))
21078  {
21079  result = false;
21080  if (k)
21082  else
21084  }
21085  }
21086 
21087  ABG_RETURN(result);
21088 }
21089 
21090 /// Comparison operator for @ref function_decl.
21091 ///
21092 /// @param other the other instance of @ref function_decl to compare
21093 /// against.
21094 ///
21095 /// @return true iff the current instance of @ref function_decl equals
21096 /// @p other.
21097 bool
21099 {
21100  const function_decl* o = dynamic_cast<const function_decl*>(&other);
21101  if (!o)
21102  return false;
21103  return equals(*this, *o, 0);
21104 }
21105 
21106 /// Return true iff the function takes a variable number of
21107 /// parameters.
21108 ///
21109 /// @return true if the function taks a variable number
21110 /// of parameters.
21111 bool
21113 {
21114  return (!get_parameters().empty()
21115  && get_parameters().back()->get_variadic_marker());
21116 }
21117 
21118 /// The virtual implementation of 'get_hash' for a function_decl.
21119 ///
21120 /// This allows decl_base::get_hash to work for function_decls.
21121 ///
21122 /// @return the hash value for function decl.
21123 size_t
21125 {
21126  function_decl::hash hash_fn;
21127  return hash_fn(*this);
21128 }
21129 
21130 /// Return an ID that tries to uniquely identify the function inside a
21131 /// program or a library.
21132 ///
21133 /// So if the function has an underlying elf symbol, the ID is the
21134 /// concatenation of the symbol name and its version. Otherwise, the
21135 /// ID is the linkage name if its non-null. Otherwise, it's the
21136 /// pretty representation of the function.
21137 ///
21138 /// @return the ID.
21141 {
21142  if (priv_->id_.empty())
21143  {
21144  const environment& env = get_type()->get_environment();
21145  if (elf_symbol_sptr s = get_symbol())
21146  {
21147  if (s->has_aliases())
21148  // The symbol has several aliases, so let's use a scheme
21149  // that allows all aliased functions to have different
21150  // IDs.
21151  priv_->id_ = env.intern(get_name() + "/" + s->get_id_string());
21152  else
21153  // Let's use the full symbol name with its version as ID.
21154  priv_->id_ = env.intern(s->get_id_string());
21155  }
21156  else if (!get_linkage_name().empty())
21157  priv_->id_= env.intern(get_linkage_name());
21158  else
21159  priv_->id_ = env.intern(get_pretty_representation());
21160  }
21161  return priv_->id_;
21162 }
21163 
21164 /// Test if two function declarations are aliases.
21165 ///
21166 /// Two functions declarations are aliases if their symbols are
21167 /// aliases, in the ELF sense.
21168 ///
21169 /// @param f1 the first function to consider.
21170 ///
21171 /// @param f2 the second function to consider.
21172 ///
21173 /// @return true iff @p f1 is an alias of @p f2
21174 bool
21176 {
21177  elf_symbol_sptr s1 = f1.get_symbol(), s2 = f2.get_symbol();
21178 
21179  if (!s1 || !s2)
21180  return false;
21181 
21182  return elf_symbols_alias(s1, s2);
21183 }
21184 
21185 /// This implements the ir_traversable_base::traverse pure virtual
21186 /// function.
21187 ///
21188 /// @param v the visitor used on the current instance.
21189 ///
21190 /// @return true if the entire IR node tree got traversed, false
21191 /// otherwise.
21192 bool
21194 {
21195  if (visiting())
21196  return true;
21197 
21198  if (v.visit_begin(this))
21199  {
21200  visiting(true);
21201  if (type_base_sptr t = get_type())
21202  t->traverse(v);
21203  visiting(false);
21204  }
21205  return v.visit_end(this);
21206 }
21207 
21208 /// Destructor of the @ref function_decl type.
21210 {delete priv_;}
21211 
21212 /// A deep comparison operator for a shared pointer to @ref function_decl
21213 ///
21214 /// This function compares to shared pointers to @ref function_decl by
21215 /// looking at the pointed-to instances of @ref function_dec
21216 /// comparing them too. If the two pointed-to objects are equal then
21217 /// this function returns true.
21218 ///
21219 /// @param l the left-hand side argument of the equality operator.
21220 ///
21221 /// @param r the right-hand side argument of the equality operator.
21222 ///
21223 /// @return true iff @p l equals @p r.
21224 bool
21226 {
21227  if (l.get() == r.get())
21228  return true;
21229  if (!!l != !!r)
21230  return false;
21231 
21232  return *l == *r;
21233 }
21234 
21235 /// A deep inequality operator for smart pointers to functions.
21236 ///
21237 /// @param l the left-hand side argument of the inequality operator.
21238 ///
21239 /// @pram r the right-hand side argument of the inequality operator.
21240 ///
21241 /// @return true iff @p is not equal to @p r.
21242 bool
21244 {return !operator==(l, r);}
21245 
21246 // <function_decl definitions>
21247 
21248 // <function_decl::parameter definitions>
21249 
21250 struct function_decl::parameter::priv
21251 {
21252  type_base_wptr type_;
21253  unsigned index_;
21254  bool variadic_marker_;
21255 
21256  priv()
21257  : index_(),
21258  variadic_marker_()
21259  {}
21260 
21261  priv(type_base_sptr type,
21262  unsigned index,
21263  bool variadic_marker)
21264  : type_(type),
21265  index_(index),
21266  variadic_marker_(variadic_marker)
21267  {}
21268 };// end struct function_decl::parameter::priv
21269 
21270 function_decl::parameter::parameter(const type_base_sptr type,
21271  unsigned index,
21272  const string& name,
21273  const location& loc,
21274  bool is_variadic)
21275  : type_or_decl_base(type->get_environment(),
21276  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
21277  decl_base(type->get_environment(), name, loc),
21278  priv_(new priv(type, index, is_variadic))
21279 {
21280  runtime_type_instance(this);
21281 }
21282 
21283 function_decl::parameter::parameter(const type_base_sptr type,
21284  unsigned index,
21285  const string& name,
21286  const location& loc,
21287  bool is_variadic,
21288  bool is_artificial)
21289  : type_or_decl_base(type->get_environment(),
21290  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
21291  decl_base(type->get_environment(), name, loc),
21292  priv_(new priv(type, index, is_variadic))
21293 {
21294  runtime_type_instance(this);
21295  set_is_artificial(is_artificial);
21296 }
21297 
21298 function_decl::parameter::parameter(const type_base_sptr type,
21299  const string& name,
21300  const location& loc,
21301  bool is_variadic,
21302  bool is_artificial)
21303  : type_or_decl_base(type->get_environment(),
21304  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
21305  decl_base(type->get_environment(), name, loc),
21306  priv_(new priv(type, 0, is_variadic))
21307 {
21308  runtime_type_instance(this);
21309  set_is_artificial(is_artificial);
21310 }
21311 
21312 function_decl::parameter::parameter(const type_base_sptr type,
21313  unsigned index,
21314  bool variad)
21315  : type_or_decl_base(type->get_environment(),
21316  FUNCTION_PARAMETER_DECL | ABSTRACT_DECL_BASE),
21317  decl_base(type->get_environment(), "", location()),
21318  priv_(new priv(type, index, variad))
21319 {
21320  runtime_type_instance(this);
21321 }
21322 
21323 function_decl::parameter::~parameter() = default;
21324 
21325 const type_base_sptr
21326 function_decl::parameter::get_type()const
21327 {return priv_->type_.lock();}
21328 
21329 /// @return a copy of the type name of the parameter.
21330 interned_string
21332 {
21333  const environment& env = get_environment();
21334 
21335  type_base_sptr t = get_type();
21336  string str;
21337  if (get_variadic_marker() || env.is_variadic_parameter_type(t))
21338  str = "...";
21339  else
21340  {
21341  ABG_ASSERT(t);
21342  str = abigail::ir::get_type_name(t);
21343  }
21344  return env.intern(str);
21345 }
21346 
21347 /// @return a copy of the pretty representation of the type of the
21348 /// parameter.
21349 const string
21351 {
21352  type_base_sptr t = get_type();
21353  string str;
21354  if (get_variadic_marker()
21355  || get_environment().is_variadic_parameter_type(t))
21356  str = "...";
21357  else
21358  {
21359  ABG_ASSERT(t);
21361  }
21362  return str;
21363 }
21364 
21365 /// Get a name uniquely identifying the parameter in the function.
21366 ///
21367 ///@return the unique parm name id.
21370 {
21371  const environment& env = get_environment();
21372 
21373 
21374  std::ostringstream o;
21375  o << "parameter-" << get_index();
21376 
21377  return env.intern(o.str());
21378 }
21379 
21380 unsigned
21381 function_decl::parameter::get_index() const
21382 {return priv_->index_;}
21383 
21384 void
21385 function_decl::parameter::set_index(unsigned i)
21386 {priv_->index_ = i;}
21387 
21388 
21389 bool
21390 function_decl::parameter::get_variadic_marker() const
21391 {return priv_->variadic_marker_;}
21392 
21393 /// Compares two instances of @ref function_decl::parameter.
21394 ///
21395 /// If the two intances are different, set a bitfield to give some
21396 /// insight about the kind of differences there are.
21397 ///
21398 /// @param l the first artifact of the comparison.
21399 ///
21400 /// @param r the second artifact of the comparison.
21401 ///
21402 /// @param k a pointer to a bitfield that gives information about the
21403 /// kind of changes there are between @p l and @p r. This one is set
21404 /// iff @p k is non-null and the function returns false.
21405 ///
21406 /// Please note that setting k to a non-null value does have a
21407 /// negative performance impact because even if @p l and @p r are not
21408 /// equal, the function keeps up the comparison in order to determine
21409 /// the different kinds of ways in which they are different.
21410 ///
21411 /// @return true if @p l equals @p r, false otherwise.
21412 bool
21414  const function_decl::parameter& r,
21415  change_kind* k)
21416 {
21417  bool result = true;
21418 
21419  if ((l.get_variadic_marker() != r.get_variadic_marker())
21420  || (l.get_index() != r.get_index())
21421  || (!!l.get_type() != !!r.get_type()))
21422  {
21423  result = false;
21424  if (k)
21425  {
21426  if (l.get_index() != r.get_index())
21428  if (l.get_variadic_marker() != r.get_variadic_marker()
21429  || !!l.get_type() != !!r.get_type())
21430  *k |= LOCAL_TYPE_CHANGE_KIND;
21431  }
21432  else
21434  }
21435 
21436  type_base_sptr l_type = peel_typedef_type(l.get_type());
21437  type_base_sptr r_type = peel_typedef_type(r.get_type());
21438  if (l_type != r_type)
21439  {
21440  result = false;
21441  if (k)
21442  {
21443  if (!types_have_similar_structure(l_type, r_type))
21444  *k |= LOCAL_TYPE_CHANGE_KIND;
21445  else
21446  *k |= SUBTYPE_CHANGE_KIND;
21447  }
21448  else
21450  }
21451 
21452  ABG_RETURN(result);
21453 }
21454 
21455 bool
21456 function_decl::parameter::operator==(const parameter& o) const
21457 {return equals(*this, o, 0);}
21458 
21459 bool
21460 function_decl::parameter::operator==(const decl_base& o) const
21461 {
21462  const function_decl::parameter* p =
21463  dynamic_cast<const function_decl::parameter*>(&o);
21464  if (!p)
21465  return false;
21466  return function_decl::parameter::operator==(*p);
21467 }
21468 
21469 /// Non-member equality operator for @ref function_decl::parameter.
21470 ///
21471 /// @param l the left-hand side of the equality operator
21472 ///
21473 /// @param r the right-hand side of the equality operator
21474 ///
21475 /// @return true iff @p l and @p r equals.
21476 bool
21479 {
21480  if (!!l != !!r)
21481  return false;
21482  if (!l)
21483  return true;
21484  return *l == *r;
21485 }
21486 
21487 /// Non-member inequality operator for @ref function_decl::parameter.
21488 ///
21489 /// @param l the left-hand side of the equality operator
21490 ///
21491 /// @param r the right-hand side of the equality operator
21492 ///
21493 /// @return true iff @p l and @p r different.
21494 bool
21497 {return !operator==(l, r);}
21498 
21499 /// Traverse the diff sub-tree under the current instance
21500 /// function_decl.
21501 ///
21502 /// @param v the visitor to invoke on each diff node of the sub-tree.
21503 ///
21504 /// @return true if the traversing has to keep going on, false
21505 /// otherwise.
21506 bool
21508 {
21509  if (visiting())
21510  return true;
21511 
21512  if (v.visit_begin(this))
21513  {
21514  visiting(true);
21515  if (type_base_sptr t = get_type())
21516  t->traverse(v);
21517  visiting(false);
21518  }
21519  return v.visit_end(this);
21520 }
21521 
21522 /// Get the hash of a decl. If the hash hasn't been computed yet,
21523 /// compute it ans store its value; otherwise, just return the hash.
21524 ///
21525 /// @return the hash of the decl.
21526 size_t
21528 {
21529  function_decl::parameter::hash hash_fn_parm;
21530  return hash_fn_parm(this);
21531 }
21532 
21533 /// Compute the qualified name of the parameter.
21534 ///
21535 /// @param internal set to true if the call is intended for an
21536 /// internal use (for technical use inside the library itself), false
21537 /// otherwise. If you don't know what this is for, then set it to
21538 /// false.
21539 ///
21540 /// @param qn the resulting qualified name.
21541 void
21543  bool /*internal*/) const
21544 {qualified_name = get_name();}
21545 
21546 /// Compute and return a copy of the pretty representation of the
21547 /// current function parameter.
21548 ///
21549 /// @param internal set to true if the call is intended to get a
21550 /// representation of the decl (or type) for the purpose of canonical
21551 /// type comparison. This is mainly used in the function
21552 /// type_base::get_canonical_type_for().
21553 ///
21554 /// In other words if the argument for this parameter is true then the
21555 /// call is meant for internal use (for technical use inside the
21556 /// library itself), false otherwise. If you don't know what this is
21557 /// for, then set it to false.
21558 ///
21559 /// @return a copy of the textual representation of the current
21560 /// function parameter.
21561 string
21563  bool /*qualified_name*/) const
21564 {
21565  const environment& env = get_environment();
21566 
21567  string type_repr;
21568  type_base_sptr t = get_type();
21569  if (!t)
21570  type_repr = "void";
21571  else if (env.is_variadic_parameter_type(t))
21572  type_repr = "...";
21573  else
21574  type_repr = ir::get_pretty_representation(t, internal);
21575 
21576  string result = type_repr;
21577  string parm_name = get_name_id();
21578 
21579  if (!parm_name.empty())
21580  result += " " + parm_name;
21581 
21582  return result;
21583 }
21584 
21585 // </function_decl::parameter definitions>
21586 
21587 // <class_or_union definitions>
21588 
21589 /// A Constructor for instances of @ref class_or_union
21590 ///
21591 /// @param env the environment we are operating from.
21592 ///
21593 /// @param name the identifier of the class.
21594 ///
21595 /// @param size_in_bits the size of an instance of @ref
21596 /// class_or_union, expressed in bits
21597 ///
21598 /// @param align_in_bits the alignment of an instance of @ref class_or_union,
21599 /// expressed in bits.
21600 ///
21601 /// @param locus the source location of declaration point this class.
21602 ///
21603 /// @param vis the visibility of instances of @ref class_or_union.
21604 ///
21605 /// @param mem_types the vector of member types of this instance of
21606 /// @ref class_or_union.
21607 ///
21608 /// @param data_members the vector of data members of this instance of
21609 /// @ref class_or_union.
21610 ///
21611 /// @param member_fns the vector of member functions of this instance
21612 /// of @ref class_or_union.
21613 class_or_union::class_or_union(const environment& env, const string& name,
21614  size_t size_in_bits, size_t align_in_bits,
21615  const location& locus, visibility vis,
21616  member_types& mem_types,
21618  member_functions& member_fns)
21619  : type_or_decl_base(env,
21620  ABSTRACT_TYPE_BASE
21621  | ABSTRACT_DECL_BASE
21622  | ABSTRACT_SCOPE_TYPE_DECL
21623  | ABSTRACT_SCOPE_DECL),
21624  decl_base(env, name, locus, name, vis),
21625  type_base(env, size_in_bits, align_in_bits),
21626  scope_type_decl(env, name, size_in_bits, align_in_bits, locus, vis),
21627  priv_(new priv(data_members, member_fns))
21628 {
21629  for (member_types::iterator i = mem_types.begin();
21630  i != mem_types.end();
21631  ++i)
21632  if (!has_scope(get_type_declaration(*i)))
21634 
21635  for (data_members::iterator i = data_members.begin();
21636  i != data_members.end();
21637  ++i)
21638  if (!has_scope(*i))
21639  add_decl_to_scope(*i, this);
21640 
21641  for (member_functions::iterator i = member_fns.begin();
21642  i != member_fns.end();
21643  ++i)
21644  if (!has_scope(static_pointer_cast<decl_base>(*i)))
21645  add_decl_to_scope(*i, this);
21646 }
21647 
21648 /// A constructor for instances of @ref class_or_union.
21649 ///
21650 /// @param env the environment we are operating from.
21651 ///
21652 /// @param name the name of the class.
21653 ///
21654 /// @param size_in_bits the size of an instance of @ref
21655 /// class_or_union, expressed in bits
21656 ///
21657 /// @param align_in_bits the alignment of an instance of @ref class_or_union,
21658 /// expressed in bits.
21659 ///
21660 /// @param locus the source location of declaration point this class.
21661 ///
21662 /// @param vis the visibility of instances of @ref class_or_union.
21663 class_or_union::class_or_union(const environment& env, const string& name,
21664  size_t size_in_bits, size_t align_in_bits,
21665  const location& locus, visibility vis)
21666  : type_or_decl_base(env,
21667  ABSTRACT_TYPE_BASE
21668  | ABSTRACT_DECL_BASE
21669  | ABSTRACT_SCOPE_TYPE_DECL
21670  | ABSTRACT_SCOPE_DECL),
21671  decl_base(env, name, locus, name, vis),
21672  type_base(env, size_in_bits, align_in_bits),
21673  scope_type_decl(env, name, size_in_bits, align_in_bits, locus, vis),
21674  priv_(new priv)
21675 {}
21676 
21677 /// Constructor of the @ref class_or_union type.
21678 ///
21679 /// @param env the @ref environment we are operating from.
21680 ///
21681 /// @param name the name of the @ref class_or_union.
21682 ///
21683 /// @param is_declaration_only a boolean saying whether the instance
21684 /// represents a declaration only, or not.
21685 class_or_union::class_or_union(const environment& env, const string& name,
21686  bool is_declaration_only)
21687  : type_or_decl_base(env,
21688  ABSTRACT_TYPE_BASE
21689  | ABSTRACT_DECL_BASE
21690  | ABSTRACT_SCOPE_TYPE_DECL
21691  | ABSTRACT_SCOPE_DECL),
21692  decl_base(env, name, location(), name),
21693  type_base(env, 0, 0),
21694  scope_type_decl(env, name, 0, 0, location()),
21695  priv_(new priv)
21696 {
21697  set_is_declaration_only(is_declaration_only);
21698 }
21699 
21700 /// This implements the ir_traversable_base::traverse pure virtual
21701 /// function.
21702 ///
21703 /// @param v the visitor used on the member nodes of the translation
21704 /// unit during the traversal.
21705 ///
21706 /// @return true if the entire IR node tree got traversed, false
21707 /// otherwise.
21708 bool
21710 {
21711  if (v.type_node_has_been_visited(this))
21712  return true;
21713 
21714  if (visiting())
21715  return true;
21716 
21717  if (v.visit_begin(this))
21718  {
21719  visiting(true);
21720  bool stop = false;
21721 
21722  if (!stop)
21723  for (data_members::const_iterator i = get_data_members().begin();
21724  i != get_data_members().end();
21725  ++i)
21726  if (!(*i)->traverse(v))
21727  {
21728  stop = true;
21729  break;
21730  }
21731 
21732  if (!stop)
21733  for (member_functions::const_iterator i= get_member_functions().begin();
21734  i != get_member_functions().end();
21735  ++i)
21736  if (!(*i)->traverse(v))
21737  {
21738  stop = true;
21739  break;
21740  }
21741 
21742  if (!stop)
21743  for (member_types::const_iterator i = get_member_types().begin();
21744  i != get_member_types().end();
21745  ++i)
21746  if (!(*i)->traverse(v))
21747  {
21748  stop = true;
21749  break;
21750  }
21751 
21752  if (!stop)
21753  for (member_function_templates::const_iterator i =
21755  i != get_member_function_templates().end();
21756  ++i)
21757  if (!(*i)->traverse(v))
21758  {
21759  stop = true;
21760  break;
21761  }
21762 
21763  if (!stop)
21764  for (member_class_templates::const_iterator i =
21765  get_member_class_templates().begin();
21766  i != get_member_class_templates().end();
21767  ++i)
21768  if (!(*i)->traverse(v))
21769  {
21770  stop = true;
21771  break;
21772  }
21773  visiting(false);
21774  }
21775 
21776  bool result = v.visit_end(this);
21777  v.mark_type_node_as_visited(this);
21778  return result;
21779 }
21780 
21781 /// Destrcutor of the @ref class_or_union type.
21783 {delete priv_;}
21784 
21785 /// Add a member declaration to the current instance of class_or_union.
21786 /// The member declaration can be either a member type, data member,
21787 /// member function, or member template.
21788 ///
21789 /// @param d the member declaration to add.
21790 decl_base_sptr
21791 class_or_union::add_member_decl(const decl_base_sptr& d)
21792 {return insert_member_decl(d);}
21793 
21794 /// Remove a given decl from the current @ref class_or_union scope.
21795 ///
21796 /// Note that only type declarations are supported by this method for
21797 /// now. Support for the other kinds of declaration is left as an
21798 /// exercise for the interested reader of the code.
21799 ///
21800 /// @param decl the declaration to remove from this @ref
21801 /// class_or_union scope.
21802 void
21804 {
21805  type_base_sptr t = is_type(decl);
21806 
21807  // For now we want to support just removing types from classes. For
21808  // other kinds of IR node, we need more work.
21809  ABG_ASSERT(t);
21810 
21811  remove_member_type(t);
21812 }
21813 
21814 /// Fixup the members of the type of an anonymous data member.
21815 ///
21816 /// Walk all data members of (the type of) a given anonymous data
21817 /// member and set a particular property of the relationship between
21818 /// each data member and its containing type.
21819 ///
21820 /// That property records the fact that the data member belongs to the
21821 /// anonymous data member we consider.
21822 ///
21823 /// In the future, if there are other properties of this relationship
21824 /// to set in this manner, they ought to be added here.
21825 ///
21826 /// @param anon_dm the anonymous data member to consider.
21827 void
21829 {
21830  class_or_union * anon_dm_type =
21832  if (!anon_dm_type)
21833  return;
21834 
21835  for (class_or_union::data_members::const_iterator it =
21836  anon_dm_type->get_non_static_data_members().begin();
21837  it != anon_dm_type->get_non_static_data_members().end();
21838  ++it)
21839  {
21840  dm_context_rel *rel =
21841  dynamic_cast<dm_context_rel*>((*it)->get_context_rel());
21842  ABG_ASSERT(rel);
21843  rel->set_anonymous_data_member(anon_dm.get());
21844  }
21845 }
21846 
21847 /// Getter of the alignment of the @ref class_or_union type.
21848 ///
21849 /// If this @ref class_or_union is a declaration of a definition that
21850 /// is elsewhere, then the size of the definition is returned.
21851 ///
21852 /// @return the alignment of the @ref class_or_union type.
21853 size_t
21855 {
21857  return is_class_or_union_type
21859 
21861 }
21862 
21863 /// Setter of the alignment of the class type.
21864 ///
21865 /// If this class is a declaration of a definition that is elsewhere,
21866 /// then the new alignment is set to the definition.
21867 ///
21868 /// @param s the new alignment.
21869 void
21871 {
21875  else
21877 }
21878 
21879 /// Setter of the size of the @ref class_or_union type.
21880 ///
21881 /// If this @ref class_or_union is a declaration of a definition that
21882 /// is elsewhere, then the new size is set to the definition.
21883 ///
21884 /// @param s the new size.
21885 void
21887 {
21891  else
21893 }
21894 
21895 /// Getter of the size of the @ref class_or_union type.
21896 ///
21897 /// If this @ref class_or_union is a declaration of a definition that
21898 /// is elsewhere, then the size of the definition is returned.
21899 ///
21900 /// @return the size of the @ref class_or_union type.
21901 size_t
21903 {
21905  return is_class_or_union_type
21907 
21908  return type_base::get_size_in_bits();
21909 }
21910 
21911 /// Get the number of anonymous member classes contained in this
21912 /// class.
21913 ///
21914 /// @return the number of anonymous member classes contained in this
21915 /// class.
21916 size_t
21918 {
21919  int result = 0;
21920  for (member_types::const_iterator it = get_member_types().begin();
21921  it != get_member_types().end();
21922  ++it)
21923  if (class_decl_sptr t = is_class_type(*it))
21924  if (t->get_is_anonymous())
21925  ++result;
21926 
21927  return result;
21928 }
21929 
21930 /// Get the number of anonymous member unions contained in this class.
21931 ///
21932 /// @return the number of anonymous member unions contained in this
21933 /// class.
21934 size_t
21936 {
21937  int result = 0;
21938  for (member_types::const_iterator it = get_member_types().begin();
21939  it != get_member_types().end();
21940  ++it)
21941  if (union_decl_sptr t = is_union_type(*it))
21942  if (t->get_is_anonymous())
21943  ++result;
21944 
21945  return result;
21946 }
21947 
21948 /// Get the number of anonymous member enums contained in this class.
21949 ///
21950 /// @return the number of anonymous member enums contained in this
21951 /// class.
21952 size_t
21954 {
21955  int result = 0;
21956  for (member_types::const_iterator it = get_member_types().begin();
21957  it != get_member_types().end();
21958  ++it)
21959  if (enum_type_decl_sptr t = is_enum_type(*it))
21960  if (t->get_is_anonymous())
21961  ++result;
21962 
21963  return result;
21964 }
21965 
21966 /// Add a data member to the current instance of class_or_union.
21967 ///
21968 /// @param v a var_decl to add as a data member. A proper
21969 /// class_or_union::data_member is created from @p v and added to the
21970 /// class_or_union. This var_decl should not have been already added
21971 /// to a scope.
21972 ///
21973 /// @param access the access specifier for the data member.
21974 ///
21975 /// @param is_laid_out whether the data member was laid out. That is,
21976 /// if its offset has been computed. In the pattern of a class
21977 /// template for instance, this would be set to false.
21978 ///
21979 /// @param is_static whether the data memer is static.
21980 ///
21981 /// @param offset_in_bits if @p is_laid_out is true, this is the
21982 /// offset of the data member, expressed (oh, surprise) in bits.
21983 void
21985  bool is_laid_out, bool is_static,
21986  size_t offset_in_bits)
21987 {
21988  ABG_ASSERT(!has_scope(v));
21989 
21990  priv_->data_members_.push_back(v);
21992  set_data_member_is_laid_out(v, is_laid_out);
21993  set_data_member_offset(v, offset_in_bits);
21994  set_member_access_specifier(v, access);
21995  set_member_is_static(v, is_static);
21996 
21997  if (!is_static)
21998  {
21999  // If this is a non-static variable, add it to the set of
22000  // non-static variables, if it's not only in there.
22001  bool is_already_in = false;
22002  for (data_members::const_iterator i =
22003  priv_->non_static_data_members_.begin();
22004  i != priv_->non_static_data_members_.end();
22005  ++i)
22006  if (*i == v)
22007  {
22008  is_already_in = true;
22009  break;
22010  }
22011  if (!is_already_in)
22012  priv_->non_static_data_members_.push_back(v);
22013  }
22014 
22015  // If v is an anonymous data member, then fixup its data members.
22016  // For now, the only thing the fixup does is to make the data
22017  // members of the anonymous data member be aware of their containing
22018  // anonymous data member. That is helpful to compute the absolute
22019  // bit offset of each of the members of the anonymous data member.
22021 }
22022 
22023 /// Get the data members of this @ref class_or_union.
22024 ///
22025 /// @return a vector of the data members of this @ref class_or_union.
22028 {return priv_->data_members_;}
22029 
22030 /// Find a data member of a given name in the current @ref class_or_union.
22031 ///
22032 /// @param name the name of the data member to find in the current
22033 /// @ref class_or_union.
22034 ///
22035 /// @return a pointer to the @ref var_decl that represents the data
22036 /// member to find inside the current @ref class_or_union.
22037 const var_decl_sptr
22038 class_or_union::find_data_member(const string& name) const
22039 {
22040  for (data_members::const_iterator i = get_data_members().begin();
22041  i != get_data_members().end();
22042  ++i)
22043  if ((*i)->get_name() == name)
22044  return *i;
22045 
22046  // We haven't found a data member with the name 'name'. Let's look
22047  // closer again, this time in our anonymous data members.
22048  for (data_members::const_iterator i = get_data_members().begin();
22049  i != get_data_members().end();
22050  ++i)
22051  if (is_anonymous_data_member(*i))
22052  {
22053  class_or_union_sptr type = is_class_or_union_type((*i)->get_type());
22054  ABG_ASSERT(type);
22055  if (var_decl_sptr data_member = type->find_data_member(name))
22056  return data_member;
22057  }
22058 
22059  return var_decl_sptr();
22060 }
22061 
22062 /// Find an anonymous data member in the class.
22063 ///
22064 /// @param v the anonymous data member to find.
22065 ///
22066 /// @return the anonymous data member found, or nil if none was found.
22067 const var_decl_sptr
22069 {
22070  if (!v->get_name().empty())
22071  return var_decl_sptr();
22072 
22073  for (data_members::const_iterator it = get_non_static_data_members().begin();
22074  it != get_non_static_data_members().end();
22075  ++it)
22076  {
22077  if (is_anonymous_data_member(*it))
22078  if ((*it)->get_pretty_representation(/*internal=*/false, true)
22079  == v->get_pretty_representation(/*internal=*/false, true))
22080  return *it;
22081  }
22082 
22083  return var_decl_sptr();
22084 }
22085 
22086 /// Find a given data member.
22087 ///
22088 /// This function takes a @ref var_decl as an argument. If it has a
22089 /// non-empty name, then it tries to find a data member which has the
22090 /// same name as the argument.
22091 ///
22092 /// If it has an empty name, then the @ref var_decl is considered as
22093 /// an anonymous data member. In that case, this function tries to
22094 /// find an anonymous data member which type equals that of the @ref
22095 /// var_decl argument.
22096 ///
22097 /// @param v this carries either the name of the data member we need
22098 /// to look for, or the type of the anonymous data member we are
22099 /// looking for.
22100 const var_decl_sptr
22102 {
22103  if (!v)
22104  return var_decl_sptr();
22105 
22106  if (v->get_name().empty())
22107  return find_anonymous_data_member(v);
22108 
22109  return find_data_member(v->get_name());
22110 }
22111 
22112 
22113 /// Get the non-static data memebers of this @ref class_or_union.
22114 ///
22115 /// @return a vector of the non-static data members of this @ref
22116 /// class_or_union.
22119 {return priv_->non_static_data_members_;}
22120 
22121 /// Add a member function.
22122 ///
22123 /// @param f the new member function to add.
22124 ///
22125 /// @param a the access specifier to use for the new member function.
22126 ///
22127 /// @param is_static whether the new member function is static.
22128 ///
22129 /// @param is_ctor whether the new member function is a constructor.
22130 ///
22131 /// @param is_dtor whether the new member function is a destructor.
22132 ///
22133 /// @param is_const whether the new member function is const.
22134 void
22136  access_specifier a,
22137  bool is_static, bool is_ctor,
22138  bool is_dtor, bool is_const)
22139 {
22140  ABG_ASSERT(!has_scope(f));
22141 
22143 
22144  set_member_function_is_ctor(f, is_ctor);
22145  set_member_function_is_dtor(f, is_dtor);
22147  set_member_is_static(f, is_static);
22148  set_member_function_is_const(f, is_const);
22149 
22150  priv_->member_functions_.push_back(f);
22151 
22152  // Update the map of linkage name -> member functions. It's useful,
22153  // so that class_or_union::find_member_function() can function.
22154  if (!f->get_linkage_name().empty())
22155  priv_->mem_fns_map_[f->get_linkage_name()] = f;
22156 }
22157 
22158 /// Get the member functions of this @ref class_or_union.
22159 ///
22160 /// @return a vector of the member functions of this @ref
22161 /// class_or_union.
22164 {return priv_->member_functions_;}
22165 
22166 /// Find a method, using its linkage name as a key.
22167 ///
22168 /// @param linkage_name the linkage name of the method to find.
22169 ///
22170 /// @return the method found, or nil if none was found.
22171 const method_decl*
22172 class_or_union::find_member_function(const string& linkage_name) const
22173 {
22174  return const_cast<class_or_union*>(this)->find_member_function(linkage_name);
22175 }
22176 
22177 /// Find a method, using its linkage name as a key.
22178 ///
22179 /// @param linkage_name the linkage name of the method to find.
22180 ///
22181 /// @return the method found, or nil if none was found.
22182 method_decl*
22183 class_or_union::find_member_function(const string& linkage_name)
22184 {
22185  string_mem_fn_sptr_map_type::const_iterator i =
22186  priv_->mem_fns_map_.find(linkage_name);
22187  if (i == priv_->mem_fns_map_.end())
22188  return 0;
22189  return i->second.get();
22190 }
22191 
22192 /// Find a method, using its linkage name as a key.
22193 ///
22194 /// @param linkage_name the linkage name of the method to find.
22195 ///
22196 /// @return the method found, or nil if none was found.
22197 method_decl_sptr
22199 {
22200  string_mem_fn_sptr_map_type::const_iterator i =
22201  priv_->mem_fns_map_.find(linkage_name);
22202  if (i == priv_->mem_fns_map_.end())
22203  return 0;
22204  return i->second;
22205 }
22206 
22207 /// Find a method (member function) using its signature (pretty
22208 /// representation) as a key.
22209 ///
22210 /// @param s the signature of the method.
22211 ///
22212 /// @return the method found, or nil if none was found.
22213 const method_decl*
22215 {
22216  return const_cast<class_or_union*>(this)->find_member_function_from_signature(s);
22217 }
22218 
22219 /// Find a method (member function) using its signature (pretty
22220 /// representation) as a key.
22221 ///
22222 /// @param s the signature of the method.
22223 ///
22224 /// @return the method found, or nil if none was found.
22225 method_decl*
22227 {
22228  string_mem_fn_ptr_map_type::const_iterator i =
22229  priv_->signature_2_mem_fn_map_.find(s);
22230  if (i == priv_->signature_2_mem_fn_map_.end())
22231  return 0;
22232  return i->second;
22233 }
22234 
22235 /// Get the member function templates of this class.
22236 ///
22237 /// @return a vector of the member function templates of this class.
22238 const member_function_templates&
22240 {return priv_->member_function_templates_;}
22241 
22242 /// Get the member class templates of this class.
22243 ///
22244 /// @return a vector of the member class templates of this class.
22245 const member_class_templates&
22247 {return priv_->member_class_templates_;}
22248 
22249 /// Append a member function template to the @ref class_or_union.
22250 ///
22251 /// @param m the member function template to append.
22252 void
22253 class_or_union::add_member_function_template(member_function_template_sptr m)
22254 {
22255  decl_base* c = m->as_function_tdecl()->get_scope();
22256  /// TODO: use our own ABG_ASSERTion facility that adds a meaningful
22257  /// error message or something like a structured error.
22258  priv_->member_function_templates_.push_back(m);
22259  if (!c)
22260  scope_decl::add_member_decl(m->as_function_tdecl());
22261 }
22262 
22263 /// Append a member class template to the @ref class_or_union.
22264 ///
22265 /// @param m the member function template to append.
22266 void
22267 class_or_union::add_member_class_template(member_class_template_sptr m)
22268 {
22269  decl_base* c = m->as_class_tdecl()->get_scope();
22270  /// TODO: use our own ABG_ASSERTion facility that adds a meaningful
22271  /// error message or something like a structured error.
22272  m->set_scope(this);
22273  priv_->member_class_templates_.push_back(m);
22274  if (!c)
22275  scope_decl::add_member_decl(m->as_class_tdecl());
22276 }
22277 
22278 ///@return true iff the current instance has no member.
22279 bool
22281 {
22282  return (get_member_types().empty()
22283  && priv_->data_members_.empty()
22284  && priv_->member_functions_.empty()
22285  && priv_->member_function_templates_.empty()
22286  && priv_->member_class_templates_.empty());
22287 }
22288 
22289 /// Insert a data member to this @ref class_or_union type.
22290 ///
22291 /// @param d the data member to insert.
22292 ///
22293 /// @return the decl @p that got inserted.
22294 decl_base_sptr
22296 {
22297  if (var_decl_sptr v = dynamic_pointer_cast<var_decl>(d))
22298  {
22299  add_data_member(v, public_access,
22300  /*is_laid_out=*/false,
22301  /*is_static=*/true,
22302  /*offset_in_bits=*/0);
22303  d = v;
22304  }
22305  else if (method_decl_sptr f = dynamic_pointer_cast<method_decl>(d))
22306  add_member_function(f, public_access,
22307  /*is_static=*/false,
22308  /*is_ctor=*/false,
22309  /*is_dtor=*/false,
22310  /*is_const=*/false);
22311  else if (member_function_template_sptr f =
22312  dynamic_pointer_cast<member_function_template>(d))
22314  else if (member_class_template_sptr c =
22315  dynamic_pointer_cast<member_class_template>(d))
22317  else
22319 
22320  return d;
22321 }
22322 
22323 /// Equality operator.
22324 ///
22325 /// @param other the other @ref class_or_union to compare against.
22326 ///
22327 /// @return true iff @p other equals the current @ref class_or_union.
22328 bool
22330 {
22331  const class_or_union* op = dynamic_cast<const class_or_union*>(&other);
22332  if (!op)
22333  return false;
22334 
22335  // If this is a decl-only type (and thus with no canonical type),
22336  // use the canonical type of the definition, if any.
22337  const class_or_union *l = 0;
22339  l = dynamic_cast<const class_or_union*>(get_naked_definition_of_declaration());
22340  if (l == 0)
22341  l = this;
22342 
22343  // Likewise for the other class.
22344  const class_or_union *r = 0;
22345  if (op->get_is_declaration_only())
22346  r = dynamic_cast<const class_or_union*>(op->get_naked_definition_of_declaration());
22347  if (r == 0)
22348  r = op;
22349 
22350  return try_canonical_compare(l, r);
22351 }
22352 
22353 /// Equality operator.
22354 ///
22355 /// @param other the other @ref class_or_union to compare against.
22356 ///
22357 /// @return true iff @p other equals the current @ref class_or_union.
22358 bool
22360 {
22361  const decl_base* o = dynamic_cast<const decl_base*>(&other);
22362  if (!o)
22363  return false;
22364  return *this == *o;
22365 }
22366 
22367 /// Equality operator.
22368 ///
22369 /// @param other the other @ref class_or_union to compare against.
22370 ///
22371 /// @return true iff @p other equals the current @ref class_or_union.
22372 bool
22374 {
22375  const decl_base& o = other;
22376  return class_or_union::operator==(o);
22377 }
22378 
22379 /// Compares two instances of @ref class_or_union.
22380 ///
22381 /// If the two intances are different, set a bitfield to give some
22382 /// insight about the kind of differences there are.
22383 ///
22384 /// @param l the first artifact of the comparison.
22385 ///
22386 /// @param r the second artifact of the comparison.
22387 ///
22388 /// @param k a pointer to a bitfield that gives information about the
22389 /// kind of changes there are between @p l and @p r. This one is set
22390 /// iff it's non-null and if the function returns false.
22391 ///
22392 /// Please note that setting k to a non-null value does have a
22393 /// negative performance impact because even if @p l and @p r are not
22394 /// equal, the function keeps up the comparison in order to determine
22395 /// the different kinds of ways in which they are different.
22396 ///
22397 /// @return true if @p l equals @p r, false otherwise.
22398 bool
22400 {
22401  // if one of the classes is declaration-only, look through it to
22402  // get its definition.
22403  bool l_is_decl_only = l.get_is_declaration_only();
22404  bool r_is_decl_only = r.get_is_declaration_only();
22405  if (l_is_decl_only || r_is_decl_only)
22406  {
22407  const class_or_union* def1 = l_is_decl_only
22409  : &l;
22410 
22411  const class_or_union* def2 = r_is_decl_only
22413  : &r;
22414 
22415  if (!def1 || !def2)
22416  {
22417  if (!l.get_is_anonymous()
22418  && !r.get_is_anonymous()
22419  && l_is_decl_only && r_is_decl_only
22421  // The two decl-only classes differ from their size. A
22422  // true decl-only class should not have a size property to
22423  // begin with. This comes from a DWARF oddity and can
22424  // results in a false positive, so let's not consider that
22425  // change.
22426  return true;
22427 
22429  || ((odr_is_relevant(l) && !def1)
22430  || (odr_is_relevant(r) && !def2)))
22433  {
22434  const interned_string& q1 = l.get_scoped_name();
22435  const interned_string& q2 = r.get_scoped_name();
22436  if (q1 == q2)
22437  // Not using RETURN(true) here, because that causes
22438  // performance issues. We don't need to do
22439  // l.priv_->unmark_as_being_compared({l,r}) here because
22440  // we haven't marked l or r as being compared yet, and
22441  // doing so has a peformance cost that shows up on
22442  // performance profiles for *big* libraries.
22443  return true;
22444  else
22445  {
22446  if (k)
22447  *k |= LOCAL_TYPE_CHANGE_KIND;
22448  // Not using RETURN(true) here, because that causes
22449  // performance issues. We don't need to do
22450  // l.priv_->unmark_as_being_compared({l,r}) here because
22451  // we haven't marked l or r as being compared yet, and
22452  // doing so has a peformance cost that shows up on
22453  // performance profiles for *big* libraries.
22455  }
22456  }
22457  else // A decl-only class is considered different from a
22458  // class definition of the same name.
22459  {
22460  if (!!def1 != !!def2)
22461  {
22462  if (k)
22463  *k |= LOCAL_TYPE_CHANGE_KIND;
22465  }
22466 
22467  // both definitions are empty
22468  if (!(l.decl_base::operator==(r)
22469  && l.type_base::operator==(r)))
22470  {
22471  if (k)
22472  *k |= LOCAL_TYPE_CHANGE_KIND;
22474  }
22475 
22476  return true;
22477  }
22478  }
22479 
22480  bool val = *def1 == *def2;
22481  if (!val)
22482  if (k)
22483  *k |= LOCAL_TYPE_CHANGE_KIND;
22484  ABG_RETURN(val);
22485  }
22486 
22487  // No need to go further if the classes have different names or
22488  // different size / alignment.
22489  if (!(l.decl_base::operator==(r) && l.type_base::operator==(r)))
22490  {
22491  if (k)
22492  *k |= LOCAL_TYPE_CHANGE_KIND;
22494  }
22495 
22496  if (types_defined_same_linux_kernel_corpus_public(l, r))
22497  return true;
22498 
22499  //TODO: Maybe remove this (cycle detection and canonical type
22500  //propagation handling) from here and have it only in the equal
22501  //overload for class_decl and union_decl because this one ( the
22502  //equal overload for class_or_union) is just a sub-routine of these
22503  //two above.
22504 #define RETURN(value) \
22505  return return_comparison_result(l, r, value, \
22506  /*propagate_canonical_type=*/false);
22507 
22509 
22511 
22512  bool result = true;
22513 
22514  //compare data_members
22515  {
22516  if (l.get_non_static_data_members().size()
22517  != r.get_non_static_data_members().size())
22518  {
22519  result = false;
22520  if (k)
22521  *k |= LOCAL_TYPE_CHANGE_KIND;
22522  else
22523  RETURN(result);
22524  }
22525 
22526  for (class_or_union::data_members::const_iterator
22527  d0 = l.get_non_static_data_members().begin(),
22528  d1 = r.get_non_static_data_members().begin();
22529  (d0 != l.get_non_static_data_members().end()
22530  && d1 != r.get_non_static_data_members().end());
22531  ++d0, ++d1)
22532  if (**d0 != **d1)
22533  {
22534  result = false;
22535  if (k)
22536  {
22537  // Report any representation change as being local.
22538  if (!types_have_similar_structure((*d0)->get_type(),
22539  (*d1)->get_type())
22540  || (*d0)->get_type() == (*d1)->get_type())
22541  *k |= LOCAL_TYPE_CHANGE_KIND;
22542  else
22543  *k |= SUBTYPE_CHANGE_KIND;
22544  }
22545  else
22546  RETURN(result);
22547  }
22548  }
22549 
22550  // Do not compare member functions. DWARF does not necessarily
22551  // all the member functions, be they virtual or not, in all
22552  // translation units. So we cannot have a clear view of them, per
22553  // class
22554 
22555  // compare member function templates
22556  {
22557  if (l.get_member_function_templates().size()
22558  != r.get_member_function_templates().size())
22559  {
22560  result = false;
22561  if (k)
22563  else
22564  RETURN(result);
22565  }
22566 
22567  for (member_function_templates::const_iterator
22568  fn_tmpl_it0 = l.get_member_function_templates().begin(),
22569  fn_tmpl_it1 = r.get_member_function_templates().begin();
22570  fn_tmpl_it0 != l.get_member_function_templates().end()
22571  && fn_tmpl_it1 != r.get_member_function_templates().end();
22572  ++fn_tmpl_it0, ++fn_tmpl_it1)
22573  if (**fn_tmpl_it0 != **fn_tmpl_it1)
22574  {
22575  result = false;
22576  if (k)
22577  {
22579  break;
22580  }
22581  else
22582  RETURN(result);
22583  }
22584  }
22585 
22586  // compare member class templates
22587  {
22588  if (l.get_member_class_templates().size()
22589  != r.get_member_class_templates().size())
22590  {
22591  result = false;
22592  if (k)
22594  else
22595  RETURN(result);
22596  }
22597 
22598  for (member_class_templates::const_iterator
22599  cl_tmpl_it0 = l.get_member_class_templates().begin(),
22600  cl_tmpl_it1 = r.get_member_class_templates().begin();
22601  cl_tmpl_it0 != l.get_member_class_templates().end()
22602  && cl_tmpl_it1 != r.get_member_class_templates().end();
22603  ++cl_tmpl_it0, ++cl_tmpl_it1)
22604  if (**cl_tmpl_it0 != **cl_tmpl_it1)
22605  {
22606  result = false;
22607  if (k)
22608  {
22610  break;
22611  }
22612  else
22613  RETURN(result);
22614  }
22615  }
22616 
22617  RETURN(result);
22618 #undef RETURN
22619 }
22620 
22621 
22622 /// Copy a method of a @ref class_or_union into a new @ref
22623 /// class_or_union.
22624 ///
22625 /// @param t the @ref class_or_union into which the method is to be copied.
22626 ///
22627 /// @param method the method to copy into @p t.
22628 ///
22629 /// @return the resulting newly copied method.
22630 method_decl_sptr
22631 copy_member_function(const class_or_union_sptr& t,
22632  const method_decl_sptr& method)
22633 {return copy_member_function(t, method.get());}
22634 
22635 
22636 /// Copy a method of a @ref class_or_union into a new @ref
22637 /// class_or_union.
22638 ///
22639 /// @param t the @ref class_or_union into which the method is to be copied.
22640 ///
22641 /// @param method the method to copy into @p t.
22642 ///
22643 /// @return the resulting newly copied method.
22644 method_decl_sptr
22645 copy_member_function(const class_or_union_sptr& t, const method_decl* method)
22646 {
22647  ABG_ASSERT(t);
22648  ABG_ASSERT(method);
22649 
22650  method_type_sptr old_type = method->get_type();
22651  ABG_ASSERT(old_type);
22652  method_type_sptr new_type(new method_type(old_type->get_return_type(),
22653  t,
22654  old_type->get_parameters(),
22655  old_type->get_is_const(),
22656  old_type->get_size_in_bits(),
22657  old_type->get_alignment_in_bits()));
22658  keep_type_alive(new_type);
22659 
22660  method_decl_sptr
22661  new_method(new method_decl(method->get_name(),
22662  new_type,
22663  method->is_declared_inline(),
22664  method->get_location(),
22665  method->get_linkage_name(),
22666  method->get_visibility(),
22667  method->get_binding()));
22668  new_method->set_symbol(method->get_symbol());
22669 
22670  if (class_decl_sptr class_type = is_class_type(t))
22671  class_type->add_member_function(new_method,
22672  get_member_access_specifier(*method),
22675  get_member_is_static(*method),
22676  get_member_function_is_ctor(*method),
22677  get_member_function_is_dtor(*method),
22678  get_member_function_is_const(*method));
22679  else
22680  t->add_member_function(new_method,
22681  get_member_access_specifier(*method),
22682  get_member_is_static(*method),
22683  get_member_function_is_ctor(*method),
22684  get_member_function_is_dtor(*method),
22685  get_member_function_is_const(*method));
22686  return new_method;
22687 }
22688 
22689 // </class_or_union definitions>
22690 
22691 /// @defgroup OnTheFlyCanonicalization On-the-fly Canonicalization
22692 /// @{
22693 ///
22694 /// This optimization is also known as "canonical type propagation".
22695 ///
22696 /// During the canonicalization of a type T (which doesn't yet have a
22697 /// canonical type), T is compared structurally (member-wise) against
22698 /// a type C which already has a canonical type. The comparison
22699 /// expression is C == T.
22700 ///
22701 /// During that structural comparison, if a subtype of C (which also
22702 /// already has a canonical type) is structurally compared to a
22703 /// subtype of T (which doesn't yet have a canonical type) and if they
22704 /// are equal, then we can deduce that the canonical type of the
22705 /// subtype of C is the canonical type of the subtype of C.
22706 ///
22707 /// Thus, we can canonicalize the sub-type of the T, during the
22708 /// canonicalization of T itself. That canonicalization of the
22709 /// sub-type of T is what we call the "on-the-fly canonicalization".
22710 /// It's on the fly because it happens during a comparison -- which
22711 /// itself happens during the canonicalization of T.
22712 ///
22713 /// For now this on-the-fly canonicalization only happens when
22714 /// comparing @ref class_decl and @ref function_type.
22715 ///
22716 /// Note however that there is a case when a type is *NOT* eligible to
22717 /// this canonical type propagation optimization.
22718 ///
22719 /// The reason why a type is deemed NON-eligible to the canonical type
22720 /// propagation optimization is that it "depends" on recursively
22721 /// present type. Let me explain.
22722 ///
22723 /// Suppose we have a type T that has sub-types named ST0 and ST1.
22724 /// Suppose ST1 itself has a sub-type that is T itself. In this case,
22725 /// we say that T is a recursive type, because it has T (itself) as
22726 /// one of its sub-types:
22727 ///
22728 /// <PRE>
22729 /// T
22730 /// +-- ST0
22731 /// |
22732 /// +-- ST1
22733 /// | +
22734 /// | |
22735 /// | +-- T
22736 /// |
22737 /// +-- ST2
22738 /// </PRE>
22739 ///
22740 /// ST1 is said to "depend" on T because it has T as a sub-type. But
22741 /// because T is recursive, then ST1 is said to depend on a recursive
22742 /// type. Notice however that ST0 does not depend on any recursive
22743 /// type.
22744 ///
22745 /// Now suppose we are comparing T to a type T' that has the same
22746 /// structure with sub-types ST0', ST1' and ST2'. During the
22747 /// comparison of ST1 against ST1', their sub-type T is compared
22748 /// against T'. Because T (resp. T') is a recursive type that is
22749 /// already being compared, the comparison of T against T' (as a
22750 /// subtypes of ST1 and ST1') returns true, meaning they are
22751 /// considered equal. This is done so that we don't enter an infinite
22752 /// recursion.
22753 ///
22754 /// That means ST1 is also deemed equal to ST1'. If we are in the
22755 /// course of the canonicalization of T' and thus if T (as well as as
22756 /// all of its sub-types) is already canonicalized, then the canonical
22757 /// type propagation optimization will make us propagate the canonical
22758 /// type of ST1 onto ST1'. So the canonical type of ST1' will be
22759 /// equal to the canonical type of ST1 as a result of that
22760 /// optmization.
22761 ///
22762 /// But then, later down the road, when ST2 is compared against ST2',
22763 /// let's suppose that we find out that they are different. Meaning
22764 /// that ST2 != ST2'. This means that T != T', i.e, the
22765 /// canonicalization of T' failed for now. But most importantly, it
22766 /// means that the propagation of the canonical type of ST1 to ST1'
22767 /// must now be invalidated. Meaning, ST1' must now be considered as
22768 /// not having any canonical type.
22769 ///
22770 /// In other words, during type canonicalization, if ST1' depends on a
22771 /// recursive type T', its propagated canonical type must be
22772 /// invalidated (set to nullptr) if T' appears to be different from T,
22773 /// a.k.a, the canonicalization of T' temporarily failed.
22774 ///
22775 /// This means that any sub-type that depends on recursive types and
22776 /// that has been the target of the canonical type propagation
22777 /// optimization must be tracked. If the dependant recursive type
22778 /// fails its canonicalization, then the sub-type being compared must
22779 /// have its propagated canonical type cleared. In other words, its
22780 /// propagated canonical type must be cancelled.
22781 ///
22782 /// @}
22783 
22784 
22785 /// If on-the-fly canonicalization is turned on, then this function
22786 /// sets the canonical type of its second parameter to the canonical
22787 /// type of the first parameter.
22788 ///
22789 /// @param lhs_type the type which canonical type to propagate.
22790 ///
22791 /// @param rhs_type the type which canonical type to set.
22792 static bool
22793 maybe_propagate_canonical_type(const type_base& lhs_type,
22794  const type_base& rhs_type)
22795 {
22796  const environment& env = lhs_type.get_environment();
22797 #if WITH_DEBUG_TYPE_CANONICALIZATION
22798  if (!env.priv_->use_canonical_type_comparison_)
22799  return false;
22800 #endif
22801 
22803  if (type_base_sptr canonical_type = lhs_type.get_canonical_type())
22804  if (!rhs_type.get_canonical_type())
22805  if (env.priv_->propagate_ct(lhs_type, rhs_type))
22806  return true;
22807  return false;
22808 }
22809 
22810 // <class_decl definitions>
22811 
22812 static void
22813 sort_virtual_member_functions(class_decl::member_functions& mem_fns);
22814 
22815 /// The private data for the class_decl type.
22816 struct class_decl::priv
22817 {
22818  base_specs bases_;
22819  unordered_map<string, base_spec_sptr> bases_map_;
22820  member_functions virtual_mem_fns_;
22821  virtual_mem_fn_map_type virtual_mem_fns_map_;
22822  bool is_struct_;
22823 
22824  priv()
22825  : is_struct_(false)
22826  {}
22827 
22828  priv(bool is_struct, class_decl::base_specs& bases)
22829  : bases_(bases),
22830  is_struct_(is_struct)
22831  {
22832  }
22833 
22834  priv(bool is_struct)
22835  : is_struct_(is_struct)
22836  {}
22837 };// end struct class_decl::priv
22838 
22839 /// A Constructor for instances of \ref class_decl
22840 ///
22841 /// @param env the environment we are operating from.
22842 ///
22843 /// @param name the identifier of the class.
22844 ///
22845 /// @param size_in_bits the size of an instance of class_decl, expressed
22846 /// in bits
22847 ///
22848 /// @param align_in_bits the alignment of an instance of class_decl,
22849 /// expressed in bits.
22850 ///
22851 /// @param locus the source location of declaration point this class.
22852 ///
22853 /// @param vis the visibility of instances of class_decl.
22854 ///
22855 /// @param bases the vector of base classes for this instance of class_decl.
22856 ///
22857 /// @param mbrs the vector of member types of this instance of
22858 /// class_decl.
22859 ///
22860 /// @param data_mbrs the vector of data members of this instance of
22861 /// class_decl.
22862 ///
22863 /// @param mbr_fns the vector of member functions of this instance of
22864 /// class_decl.
22865 class_decl::class_decl(const environment& env, const string& name,
22866  size_t size_in_bits, size_t align_in_bits,
22867  bool is_struct, const location& locus,
22868  visibility vis, base_specs& bases,
22869  member_types& mbr_types,
22870  data_members& data_mbrs,
22871  member_functions& mbr_fns)
22872  : type_or_decl_base(env,
22873  CLASS_TYPE
22874  | ABSTRACT_TYPE_BASE
22875  | ABSTRACT_DECL_BASE
22876  | ABSTRACT_SCOPE_TYPE_DECL
22877  | ABSTRACT_SCOPE_DECL),
22878  decl_base(env, name, locus, name, vis),
22879  type_base(env, size_in_bits, align_in_bits),
22880  class_or_union(env, name, size_in_bits, align_in_bits,
22881  locus, vis, mbr_types, data_mbrs, mbr_fns),
22882  priv_(new priv(is_struct, bases))
22883 {
22884  runtime_type_instance(this);
22885 }
22886 
22887 /// A Constructor for instances of @ref class_decl
22888 ///
22889 /// @param env the environment we are operating from.
22890 ///
22891 /// @param name the identifier of the class.
22892 ///
22893 /// @param size_in_bits the size of an instance of class_decl, expressed
22894 /// in bits
22895 ///
22896 /// @param align_in_bits the alignment of an instance of class_decl,
22897 /// expressed in bits.
22898 ///
22899 /// @param locus the source location of declaration point this class.
22900 ///
22901 /// @param vis the visibility of instances of class_decl.
22902 ///
22903 /// @param bases the vector of base classes for this instance of class_decl.
22904 ///
22905 /// @param mbrs the vector of member types of this instance of
22906 /// class_decl.
22907 ///
22908 /// @param data_mbrs the vector of data members of this instance of
22909 /// class_decl.
22910 ///
22911 /// @param mbr_fns the vector of member functions of this instance of
22912 /// class_decl.
22913 ///
22914 /// @param is_anonymous whether the newly created instance is
22915 /// anonymous.
22916 class_decl::class_decl(const environment& env, const string& name,
22917  size_t size_in_bits, size_t align_in_bits,
22918  bool is_struct, const location& locus,
22919  visibility vis, base_specs& bases,
22920  member_types& mbr_types, data_members& data_mbrs,
22921  member_functions& mbr_fns, bool is_anonymous)
22922  : type_or_decl_base(env,
22923  CLASS_TYPE
22924  | ABSTRACT_TYPE_BASE
22925  | ABSTRACT_DECL_BASE
22926  | ABSTRACT_SCOPE_TYPE_DECL
22927  | ABSTRACT_SCOPE_DECL),
22928  decl_base(env, name, locus,
22929  // If the class is anonymous then by default it won't
22930  // have a linkage name. Also, the anonymous class does
22931  // have an internal-only unique name that is generally
22932  // not taken into account when comparing classes; such a
22933  // unique internal-only name, when used as a linkage
22934  // name might introduce spurious comparison false
22935  // negatives.
22936  /*linkage_name=*/is_anonymous ? string() : name,
22937  vis),
22938  type_base(env, size_in_bits, align_in_bits),
22939  class_or_union(env, name, size_in_bits, align_in_bits,
22940  locus, vis, mbr_types, data_mbrs, mbr_fns),
22941  priv_(new priv(is_struct, bases))
22942 {
22943  runtime_type_instance(this);
22944  set_is_anonymous(is_anonymous);
22945 }
22946 
22947 /// A constructor for instances of class_decl.
22948 ///
22949 /// @param env the environment we are operating from.
22950 ///
22951 /// @param name the name of the class.
22952 ///
22953 /// @param size_in_bits the size of an instance of class_decl, expressed
22954 /// in bits
22955 ///
22956 /// @param align_in_bits the alignment of an instance of class_decl,
22957 /// expressed in bits.
22958 ///
22959 /// @param locus the source location of declaration point this class.
22960 ///
22961 /// @param vis the visibility of instances of class_decl.
22962 class_decl::class_decl(const environment& env, const string& name,
22963  size_t size_in_bits, size_t align_in_bits,
22964  bool is_struct, const location& locus,
22965  visibility vis)
22966  : type_or_decl_base(env,
22967  CLASS_TYPE
22968  | ABSTRACT_TYPE_BASE
22969  | ABSTRACT_DECL_BASE
22970  | ABSTRACT_SCOPE_TYPE_DECL
22971  | ABSTRACT_SCOPE_DECL),
22972  decl_base(env, name, locus, name, vis),
22973  type_base(env, size_in_bits, align_in_bits),
22974  class_or_union(env, name, size_in_bits, align_in_bits,
22975  locus, vis),
22976  priv_(new priv(is_struct))
22977 {
22978  runtime_type_instance(this);
22979 }
22980 
22981 /// A constructor for instances of @ref class_decl.
22982 ///
22983 /// @param env the environment we are operating from.
22984 ///
22985 /// @param name the name of the class.
22986 ///
22987 /// @param size_in_bits the size of an instance of class_decl, expressed
22988 /// in bits
22989 ///
22990 /// @param align_in_bits the alignment of an instance of class_decl,
22991 /// expressed in bits.
22992 ///
22993 /// @param locus the source location of declaration point this class.
22994 ///
22995 /// @param vis the visibility of instances of class_decl.
22996 ///
22997 /// @param is_anonymous whether the newly created instance is
22998 /// anonymous.
22999 class_decl:: class_decl(const environment& env, const string& name,
23000  size_t size_in_bits, size_t align_in_bits,
23001  bool is_struct, const location& locus,
23002  visibility vis, bool is_anonymous)
23003  : type_or_decl_base(env,
23004  CLASS_TYPE
23005  | ABSTRACT_TYPE_BASE
23006  | ABSTRACT_DECL_BASE
23007  | ABSTRACT_SCOPE_TYPE_DECL
23008  | ABSTRACT_SCOPE_DECL),
23009  decl_base(env, name, locus,
23010  // If the class is anonymous then by default it won't
23011  // have a linkage name. Also, the anonymous class does
23012  // have an internal-only unique name that is generally
23013  // not taken into account when comparing classes; such a
23014  // unique internal-only name, when used as a linkage
23015  // name might introduce spurious comparison false
23016  // negatives.
23017  /*linkage_name=*/ is_anonymous ? string() : name,
23018  vis),
23019  type_base(env, size_in_bits, align_in_bits),
23020  class_or_union(env, name, size_in_bits, align_in_bits,
23021  locus, vis),
23022  priv_(new priv(is_struct))
23023 {
23024  runtime_type_instance(this);
23025  set_is_anonymous(is_anonymous);
23026 }
23027 
23028 /// A constuctor for instances of class_decl that represent a
23029 /// declaration without definition.
23030 ///
23031 /// @param env the environment we are operating from.
23032 ///
23033 /// @param name the name of the class.
23034 ///
23035 /// @param is_declaration_only a boolean saying whether the instance
23036 /// represents a declaration only, or not.
23037 class_decl::class_decl(const environment& env, const string& name,
23038  bool is_struct, bool is_declaration_only)
23039  : type_or_decl_base(env,
23040  CLASS_TYPE
23041  | ABSTRACT_TYPE_BASE
23042  | ABSTRACT_DECL_BASE
23043  | ABSTRACT_SCOPE_TYPE_DECL
23044  | ABSTRACT_SCOPE_DECL),
23045  decl_base(env, name, location(), name),
23046  type_base(env, 0, 0),
23047  class_or_union(env, name, is_declaration_only),
23048  priv_(new priv(is_struct))
23049 {
23050  runtime_type_instance(this);
23051 }
23052 
23053 /// This method is invoked automatically right after the current
23054 /// instance of @ref class_decl has been canonicalized.
23055 ///
23056 /// Currently, the only thing it does is to sort the virtual member
23057 /// functions vector.
23058 void
23060 {
23062 
23063  for (class_decl::virtual_mem_fn_map_type::iterator i =
23064  priv_->virtual_mem_fns_map_.begin();
23065  i != priv_->virtual_mem_fns_map_.end();
23066  ++i)
23067  sort_virtual_member_functions(i->second);
23068 }
23069 
23070 /// Set the "is-struct" flag of the class.
23071 ///
23072 /// @param f the new value of the flag.
23073 void
23075 {priv_->is_struct_ = f;}
23076 
23077 /// Test if the class is a struct.
23078 ///
23079 /// @return true iff the class is a struct.
23080 bool
23082 {return priv_->is_struct_;}
23083 
23084 /// Add a base specifier to this class.
23085 ///
23086 /// @param b the new base specifier.
23087 void
23089 {
23090  priv_->bases_.push_back(b);
23091  priv_->bases_map_[b->get_base_class()->get_qualified_name()] = b;
23092 }
23093 
23094 /// Get the base specifiers for this class.
23095 ///
23096 /// @return a vector of the base specifiers.
23099 {return priv_->bases_;}
23100 
23101 /// Find a base class of a given qualified name for the current class.
23102 ///
23103 /// @param qualified_name the qualified name of the base class to look for.
23104 ///
23105 /// @return a pointer to the @ref class_decl that represents the base
23106 /// class of name @p qualified_name, if found.
23108 class_decl::find_base_class(const string& qualified_name) const
23109 {
23110  unordered_map<string, base_spec_sptr>::iterator i =
23111  priv_->bases_map_.find(qualified_name);
23112 
23113  if (i != priv_->bases_map_.end())
23114  return i->second->get_base_class();
23115 
23116  return class_decl_sptr();
23117 }
23118 
23119 /// Get the virtual member functions of this class.
23120 ///
23121 /// @param return a vector of the virtual member functions of this
23122 /// class.
23125 {return priv_->virtual_mem_fns_;}
23126 
23127 /// Get the map that associates a virtual table offset to the virtual
23128 /// member functions with that virtual table offset.
23129 ///
23130 /// Usually, there should be a 1:1 mapping between a given vtable
23131 /// offset and virtual member functions of that vtable offset. But
23132 /// because of some implementation details, there can be several C++
23133 /// destructor functions that are *generated* by compilers, for a
23134 /// given destructor that is defined in the source code. If the
23135 /// destructor is virtual then those generated functions have some
23136 /// DWARF attributes in common with the constructor that the user
23137 /// actually defined in its source code. Among those attributes are
23138 /// the vtable offset of the destructor.
23139 ///
23140 /// @return the map that associates a virtual table offset to the
23141 /// virtual member functions with that virtual table offset.
23144 {return priv_->virtual_mem_fns_map_;}
23145 
23146 /// Sort the virtual member functions by their virtual index.
23147 void
23149 {sort_virtual_member_functions(priv_->virtual_mem_fns_);}
23150 
23151 /// Getter of the pretty representation of the current instance of
23152 /// @ref class_decl.
23153 ///
23154 /// @param internal set to true if the call is intended to get a
23155 /// representation of the decl (or type) for the purpose of canonical
23156 /// type comparison. This is mainly used in the function
23157 /// type_base::get_canonical_type_for().
23158 ///
23159 /// In other words if the argument for this parameter is true then the
23160 /// call is meant for internal use (for technical use inside the
23161 /// library itself), false otherwise. If you don't know what this is
23162 /// for, then set it to false.
23163 ///
23164 /// @param qualified_name if true, names emitted in the pretty
23165 /// representation are fully qualified.
23166 ///
23167 /// @return the pretty representaion for a class_decl.
23168 string
23170  bool qualified_name) const
23171 {
23172  string cl = "class ";
23173  if (!internal && is_struct())
23174  cl = "struct ";
23175 
23176  // When computing the pretty representation for internal purposes,
23177  // if an anonymous class is named by a typedef, then consider that
23178  // it has a name, which is the typedef name.
23179  if (get_is_anonymous())
23180  {
23181  if (internal && !get_name().empty())
23182  return cl + get_type_name(this, qualified_name, /*internal=*/true);
23184  /*one_line=*/true,
23185  internal);
23186 
23187  }
23188 
23189  string result = cl;
23190  if (qualified_name)
23191  result += get_qualified_name(internal);
23192  else
23193  result += get_name();
23194 
23195  return result;
23196 }
23197 
23198 decl_base_sptr
23199 class_decl::insert_member_decl(decl_base_sptr d)
23200 {
23201  if (method_decl_sptr f = dynamic_pointer_cast<method_decl>(d))
23202  add_member_function(f, public_access,
23203  /*is_virtual=*/false,
23204  /*vtable_offset=*/0,
23205  /*is_static=*/false,
23206  /*is_ctor=*/false,
23207  /*is_dtor=*/false,
23208  /*is_const=*/false);
23209  else
23211 
23212  return d;
23213 }
23214 
23215 /// The private data structure of class_decl::base_spec.
23216 struct class_decl::base_spec::priv
23217 {
23218  class_decl_wptr base_class_;
23219  long offset_in_bits_;
23220  bool is_virtual_;
23221 
23222  priv(const class_decl_sptr& cl,
23223  long offset_in_bits,
23224  bool is_virtual)
23225  : base_class_(cl),
23226  offset_in_bits_(offset_in_bits),
23227  is_virtual_(is_virtual)
23228  {}
23229 };
23230 
23231 /// Constructor for base_spec instances.
23232 ///
23233 /// @param base the base class to consider
23234 ///
23235 /// @param a the access specifier of the base class.
23236 ///
23237 /// @param offset_in_bits if positive or null, represents the offset
23238 /// of the base in the layout of its containing type.. If negative,
23239 /// means that the current base is not laid out in its containing type.
23240 ///
23241 /// @param is_virtual if true, means that the current base class is
23242 /// virtual in it's containing type.
23243 class_decl::base_spec::base_spec(const class_decl_sptr& base,
23244  access_specifier a,
23245  long offset_in_bits,
23246  bool is_virtual)
23247  : type_or_decl_base(base->get_environment(),
23248  ABSTRACT_DECL_BASE),
23249  decl_base(base->get_environment(), base->get_name(), base->get_location(),
23250  base->get_linkage_name(), base->get_visibility()),
23251  member_base(a),
23252  priv_(new priv(base, offset_in_bits, is_virtual))
23253 {
23254  runtime_type_instance(this);
23255  set_qualified_name(base->get_qualified_name());
23256 }
23257 
23258 /// Get the base class referred to by the current base class
23259 /// specifier.
23260 ///
23261 /// @return the base class.
23264 {return priv_->base_class_.lock();}
23265 
23266 /// Getter of the "is-virtual" proprerty of the base class specifier.
23267 ///
23268 /// @return true iff this specifies a virtual base class.
23269 bool
23271 {return priv_->is_virtual_;}
23272 
23273 /// Getter of the offset of the base.
23274 ///
23275 /// @return the offset of the base.
23276 long
23278 {return priv_->offset_in_bits_;}
23279 
23280 /// Calculate the hash value for a class_decl::base_spec.
23281 ///
23282 /// @return the hash value.
23283 size_t
23285 {
23286  base_spec::hash h;
23287  return h(*this);
23288 }
23289 
23290 /// Traverses an instance of @ref class_decl::base_spec, visiting all
23291 /// the sub-types and decls that it might contain.
23292 ///
23293 /// @param v the visitor that is used to visit every IR sub-node of
23294 /// the current node.
23295 ///
23296 /// @return true if either
23297 /// - all the children nodes of the current IR node were traversed
23298 /// and the calling code should keep going with the traversing.
23299 /// - or the current IR node is already being traversed.
23300 /// Otherwise, returning false means that the calling code should not
23301 /// keep traversing the tree.
23302 bool
23304 {
23305  if (visiting())
23306  return true;
23307 
23308  if (v.visit_begin(this))
23309  {
23310  visiting(true);
23311  get_base_class()->traverse(v);
23312  visiting(false);
23313  }
23314 
23315  return v.visit_end(this);
23316 }
23317 
23318 /// Constructor for base_spec instances.
23319 ///
23320 /// Note that this constructor is for clients that don't support RTTI
23321 /// and that have a base class of type_base, but of dynamic type
23322 /// class_decl.
23323 ///
23324 /// @param base the base class to consider. Must be a pointer to an
23325 /// instance of class_decl
23326 ///
23327 /// @param a the access specifier of the base class.
23328 ///
23329 /// @param offset_in_bits if positive or null, represents the offset
23330 /// of the base in the layout of its containing type.. If negative,
23331 /// means that the current base is not laid out in its containing type.
23332 ///
23333 /// @param is_virtual if true, means that the current base class is
23334 /// virtual in it's containing type.
23335 class_decl::base_spec::base_spec(const type_base_sptr& base,
23336  access_specifier a,
23337  long offset_in_bits,
23338  bool is_virtual)
23340  ABSTRACT_DECL_BASE),
23345  member_base(a),
23346  priv_(new priv(dynamic_pointer_cast<class_decl>(base),
23347  offset_in_bits,
23348  is_virtual))
23349 {
23350  runtime_type_instance(this);
23351 }
23352 
23353 class_decl::base_spec::~base_spec() = default;
23354 
23355 /// Compares two instances of @ref class_decl::base_spec.
23356 ///
23357 /// If the two intances are different, set a bitfield to give some
23358 /// insight about the kind of differences there are.
23359 ///
23360 /// @param l the first artifact of the comparison.
23361 ///
23362 /// @param r the second artifact of the comparison.
23363 ///
23364 /// @param k a pointer to a bitfield that gives information about the
23365 /// kind of changes there are between @p l and @p r. This one is set
23366 /// iff @p k is non-null and the function returns false.
23367 ///
23368 /// Please note that setting k to a non-null value does have a
23369 /// negative performance impact because even if @p l and @p r are not
23370 /// equal, the function keeps up the comparison in order to determine
23371 /// the different kinds of ways in which they are different.
23372 ///
23373 /// @return true if @p l equals @p r, false otherwise.
23374 bool
23376  const class_decl::base_spec& r,
23377  change_kind* k)
23378 {
23379  if (!l.member_base::operator==(r))
23380  {
23381  if (k)
23382  *k |= LOCAL_TYPE_CHANGE_KIND;
23384  }
23385 
23386  ABG_RETURN((*l.get_base_class() == *r.get_base_class()));
23387 }
23388 
23389 /// Comparison operator for @ref class_decl::base_spec.
23390 ///
23391 /// @param other the instance of @ref class_decl::base_spec to compare
23392 /// against.
23393 ///
23394 /// @return true if the current instance of @ref class_decl::base_spec
23395 /// equals @p other.
23396 bool
23398 {
23399  const class_decl::base_spec* o =
23400  dynamic_cast<const class_decl::base_spec*>(&other);
23401 
23402  if (!o)
23403  return false;
23404 
23405  return equals(*this, *o, 0);
23406 }
23407 
23408 /// Comparison operator for @ref class_decl::base_spec.
23409 ///
23410 /// @param other the instance of @ref class_decl::base_spec to compare
23411 /// against.
23412 ///
23413 /// @return true if the current instance of @ref class_decl::base_spec
23414 /// equals @p other.
23415 bool
23417 {
23418  const class_decl::base_spec* o =
23419  dynamic_cast<const class_decl::base_spec*>(&other);
23420  if (!o)
23421  return false;
23422 
23423  return operator==(static_cast<const decl_base&>(*o));
23424 }
23425 
23426 mem_fn_context_rel::~mem_fn_context_rel()
23427 {
23428 }
23429 
23430 /// A constructor for instances of method_decl.
23431 ///
23432 /// @param name the name of the method.
23433 ///
23434 /// @param type the type of the method.
23435 ///
23436 /// @param declared_inline whether the method was
23437 /// declared inline or not.
23438 ///
23439 /// @param locus the source location of the method.
23440 ///
23441 /// @param linkage_name the mangled name of the method.
23442 ///
23443 /// @param vis the visibility of the method.
23444 ///
23445 /// @param bind the binding of the method.
23446 method_decl::method_decl(const string& name,
23447  method_type_sptr type,
23448  bool declared_inline,
23449  const location& locus,
23450  const string& linkage_name,
23451  visibility vis,
23452  binding bind)
23454  METHOD_DECL
23455  | ABSTRACT_DECL_BASE
23456  |FUNCTION_DECL),
23457  decl_base(type->get_environment(), name, locus, linkage_name, vis),
23458  function_decl(name, static_pointer_cast<function_type>(type),
23459  declared_inline, locus, linkage_name, vis, bind)
23460 {
23461  runtime_type_instance(this);
23462  set_context_rel(new mem_fn_context_rel(0));
23463  set_member_function_is_const(*this, type->get_is_const());
23464 }
23465 
23466 /// A constructor for instances of method_decl.
23467 ///
23468 /// @param name the name of the method.
23469 ///
23470 /// @param type the type of the method. Must be an instance of
23471 /// method_type.
23472 ///
23473 /// @param declared_inline whether the method was
23474 /// declared inline or not.
23475 ///
23476 /// @param locus the source location of the method.
23477 ///
23478 /// @param linkage_name the mangled name of the method.
23479 ///
23480 /// @param vis the visibility of the method.
23481 ///
23482 /// @param bind the binding of the method.
23483 method_decl::method_decl(const string& name,
23484  function_type_sptr type,
23485  bool declared_inline,
23486  const location& locus,
23487  const string& linkage_name,
23488  visibility vis,
23489  binding bind)
23490  : type_or_decl_base(type->get_environment(),
23491  METHOD_DECL
23492  | ABSTRACT_DECL_BASE
23493  | FUNCTION_DECL),
23494  decl_base(type->get_environment(), name, locus, linkage_name, vis),
23495  function_decl(name, static_pointer_cast<function_type>
23496  (dynamic_pointer_cast<method_type>(type)),
23497  declared_inline, locus, linkage_name, vis, bind)
23498 {
23499  runtime_type_instance(this);
23500  set_context_rel(new mem_fn_context_rel(0));
23501 }
23502 
23503 /// A constructor for instances of method_decl.
23504 ///
23505 /// @param name the name of the method.
23506 ///
23507 /// @param type the type of the method. Must be an instance of
23508 /// method_type.
23509 ///
23510 /// @param declared_inline whether the method was
23511 /// declared inline or not.
23512 ///
23513 /// @param locus the source location of the method.
23514 ///
23515 /// @param linkage_name the mangled name of the method.
23516 ///
23517 /// @param vis the visibility of the method.
23518 ///
23519 /// @param bind the binding of the method.
23520 method_decl::method_decl(const string& name,
23521  type_base_sptr type,
23522  bool declared_inline,
23523  const location& locus,
23524  const string& linkage_name,
23525  visibility vis,
23526  binding bind)
23527  : type_or_decl_base(type->get_environment(),
23528  METHOD_DECL
23529  | ABSTRACT_DECL_BASE
23530  | FUNCTION_DECL),
23531  decl_base(type->get_environment(), name, locus, linkage_name, vis),
23532  function_decl(name, static_pointer_cast<function_type>
23533  (dynamic_pointer_cast<method_type>(type)),
23534  declared_inline, locus, linkage_name, vis, bind)
23535 {
23536  runtime_type_instance(this);
23537  set_context_rel(new mem_fn_context_rel(0));
23538 }
23539 
23540 /// Set the linkage name of the method.
23541 ///
23542 /// @param l the new linkage name of the method.
23543 void
23545 {
23547  // Update the linkage_name -> member function map of the containing
23548  // class declaration.
23549  if (!l.empty())
23550  {
23551  method_type_sptr t = get_type();
23552  class_or_union_sptr cl = t->get_class_type();
23553  method_decl_sptr m(this, sptr_utils::noop_deleter());
23554  cl->priv_->mem_fns_map_[l] = m;
23555  }
23556 }
23557 
23558 method_decl::~method_decl()
23559 {}
23560 
23561 const method_type_sptr
23563 {
23564  method_type_sptr result;
23566  result = dynamic_pointer_cast<method_type>(function_decl::get_type());
23567  return result;
23568 }
23569 
23570 /// Set the containing class of a method_decl.
23571 ///
23572 /// @param scope the new containing class_decl.
23573 void
23574 method_decl::set_scope(scope_decl* scope)
23575 {
23576  if (!get_context_rel())
23577  set_context_rel(new mem_fn_context_rel(scope));
23578  else
23579  get_context_rel()->set_scope(scope);
23580 }
23581 
23582 /// Equality operator for @ref method_decl_sptr.
23583 ///
23584 /// This is a deep equality operator, as it compares the @ref
23585 /// method_decl that is pointed-to by the smart pointer.
23586 ///
23587 /// @param l the left-hand side argument of the equality operator.
23588 ///
23589 /// @param r the righ-hand side argument of the equality operator.
23590 ///
23591 /// @return true iff @p l equals @p r.
23592 bool
23593 operator==(const method_decl_sptr& l, const method_decl_sptr& r)
23594 {
23595  if (l.get() == r.get())
23596  return true;
23597  if (!!l != !!r)
23598  return false;
23599 
23600  return *l == *r;
23601 }
23602 
23603 /// Inequality operator for @ref method_decl_sptr.
23604 ///
23605 /// This is a deep equality operator, as it compares the @ref
23606 /// method_decl that is pointed-to by the smart pointer.
23607 ///
23608 /// @param l the left-hand side argument of the equality operator.
23609 ///
23610 /// @param r the righ-hand side argument of the equality operator.
23611 ///
23612 /// @return true iff @p l differs from @p r.
23613 bool
23614 operator!=(const method_decl_sptr& l, const method_decl_sptr& r)
23615 {return !operator==(l, r);}
23616 
23617 /// Test if a function_decl is actually a method_decl.
23618 ///
23619 ///@param d the @ref function_decl to consider.
23620 ///
23621 /// @return the method_decl sub-object of @p d if inherits
23622 /// a method_decl type.
23623 method_decl*
23625 {
23626  return dynamic_cast<method_decl*>
23627  (const_cast<type_or_decl_base*>(d));
23628 }
23629 
23630 /// Test if a function_decl is actually a method_decl.
23631 ///
23632 ///@param d the @ref function_decl to consider.
23633 ///
23634 /// @return the method_decl sub-object of @p d if inherits
23635 /// a method_decl type.
23636 method_decl*
23638 {return is_method_decl(&d);}
23639 
23640 /// Test if a function_decl is actually a method_decl.
23641 ///
23642 ///@param d the @ref function_decl to consider.
23643 ///
23644 /// @return the method_decl sub-object of @p d if inherits
23645 /// a method_decl type.
23646 method_decl_sptr
23648 {return dynamic_pointer_cast<method_decl>(d);}
23649 
23650 /// A "less than" functor to sort a vector of instances of
23651 /// method_decl that are virtual.
23652 struct virtual_member_function_less_than
23653 {
23654  /// The less than operator. First, it sorts the methods by their
23655  /// vtable index. If they have the same vtable index, it sorts them
23656  /// by the name of their ELF symbol. If they don't have elf
23657  /// symbols, it sorts them by considering their pretty
23658  /// representation.
23659  ///
23660  /// Note that this method expects virtual methods.
23661  ///
23662  /// @param f the first method to consider.
23663  ///
23664  /// @param s the second method to consider.
23665  ///
23666  /// @return true if method @p is less than method @s.
23667  bool
23668  operator()(const method_decl& f,
23669  const method_decl& s)
23670  {
23673 
23674  ssize_t f_offset = get_member_function_vtable_offset(f);
23675  ssize_t s_offset = get_member_function_vtable_offset(s);
23676  if (f_offset != s_offset) return f_offset < s_offset;
23677 
23678  string fn, sn;
23679 
23680  // If the functions have symbols, then compare their symbol-id
23681  // string.
23682  elf_symbol_sptr f_sym = f.get_symbol();
23683  elf_symbol_sptr s_sym = s.get_symbol();
23684  if ((!f_sym) != (!s_sym)) return !f_sym;
23685  if (f_sym && s_sym)
23686  {
23687  fn = f_sym->get_id_string();
23688  sn = s_sym->get_id_string();
23689  if (fn != sn) return fn < sn;
23690  }
23691 
23692  // Try the linkage names (important for destructors).
23693  fn = f.get_linkage_name();
23694  sn = s.get_linkage_name();
23695  if (fn != sn) return fn < sn;
23696 
23697  // None of the functions have symbols or linkage names that
23698  // distinguish them, so compare their pretty representation.
23699  fn = f.get_pretty_representation();
23700  sn = s.get_pretty_representation();
23701  if (fn != sn) return fn < sn;
23702 
23703  /// If it's just the file paths that are different then sort them
23704  /// too.
23705  string fn_filepath, sn_filepath;
23706  unsigned line = 0, column = 0;
23707  location fn_loc = f.get_location(), sn_loc = s.get_location();
23708  if (fn_loc)
23709  fn_loc.expand(fn_filepath, line, column);
23710  if (sn_loc)
23711  sn_loc.expand(sn_filepath, line, column);
23712  return fn_filepath < sn_filepath;
23713  }
23714 
23715  /// The less than operator. First, it sorts the methods by their
23716  /// vtable index. If they have the same vtable index, it sorts them
23717  /// by the name of their ELF symbol. If they don't have elf
23718  /// symbols, it sorts them by considering their pretty
23719  /// representation.
23720  ///
23721  /// Note that this method expects to take virtual methods.
23722  ///
23723  /// @param f the first method to consider.
23724  ///
23725  /// @param s the second method to consider.
23726  bool
23727  operator()(const method_decl_sptr f,
23728  const method_decl_sptr s)
23729  {return operator()(*f, *s);}
23730 }; // end struct virtual_member_function_less_than
23731 
23732 /// Sort a vector of instances of virtual member functions.
23733 ///
23734 /// @param mem_fns the vector of member functions to sort.
23735 static void
23736 sort_virtual_member_functions(class_decl::member_functions& mem_fns)
23737 {
23738  virtual_member_function_less_than lt;
23739  std::stable_sort(mem_fns.begin(), mem_fns.end(), lt);
23740 }
23741 
23742 /// Add a member function to the current instance of @ref class_or_union.
23743 ///
23744 /// @param f a method_decl to add to the current class. This function
23745 /// should not have been already added to a scope.
23746 ///
23747 /// @param access the access specifier for the member function to add.
23748 ///
23749 /// @param is_virtual if this is true then it means the function @p f
23750 /// is a virtual function. That also means that the current instance
23751 /// of @ref class_or_union is actually an instance of @ref class_decl.
23752 ///
23753 /// @param vtable_offset the offset of the member function in the
23754 /// virtual table. This parameter is taken into account only if @p
23755 /// is_virtual is true.
23756 ///
23757 /// @param is_static whether the member function is static.
23758 ///
23759 /// @param is_ctor whether the member function is a constructor.
23760 ///
23761 /// @param is_dtor whether the member function is a destructor.
23762 ///
23763 /// @param is_const whether the member function is const.
23764 void
23766  access_specifier a,
23767  bool is_virtual,
23768  size_t vtable_offset,
23769  bool is_static, bool is_ctor,
23770  bool is_dtor, bool is_const)
23771 {
23772  add_member_function(f, a, is_static, is_ctor,
23773  is_dtor, is_const);
23774 
23775  if (class_decl* klass = is_class_type(this))
23776  {
23777  set_member_function_is_virtual(f, is_virtual);
23778  if (is_virtual)
23779  {
23780  set_member_function_vtable_offset(f, vtable_offset);
23781  sort_virtual_member_functions(klass->priv_->virtual_mem_fns_);
23782  }
23783  }
23784 }
23785 
23786 /// When a virtual member function has seen its virtualness set by
23787 /// set_member_function_is_virtual(), this function ensures that the
23788 /// member function is added to the specific vectors and maps of
23789 /// virtual member function of its class.
23790 ///
23791 /// @param method the method to fixup.
23792 void
23793 fixup_virtual_member_function(method_decl_sptr method)
23794 {
23795  if (!method || !get_member_function_is_virtual(method))
23796  return;
23797 
23798  class_decl_sptr klass = is_class_type(method->get_type()->get_class_type());
23799 
23800  class_decl::member_functions::const_iterator m;
23801  for (m = klass->priv_->virtual_mem_fns_.begin();
23802  m != klass->priv_->virtual_mem_fns_.end();
23803  ++m)
23804  if (m->get() == method.get())
23805  break;
23806  if (m == klass->priv_->virtual_mem_fns_.end())
23807  klass->priv_->virtual_mem_fns_.push_back(method);
23808 
23809  // Build or udpate the map that associates a vtable offset to the
23810  // number of virtual member functions that "point" to it.
23811  ssize_t voffset = get_member_function_vtable_offset(method);
23812  if (voffset == -1)
23813  return;
23814 
23815  class_decl::virtual_mem_fn_map_type::iterator i =
23816  klass->priv_->virtual_mem_fns_map_.find(voffset);
23817  if (i == klass->priv_->virtual_mem_fns_map_.end())
23818  {
23819  class_decl::member_functions virtual_mem_fns_at_voffset;
23820  virtual_mem_fns_at_voffset.push_back(method);
23821  klass->priv_->virtual_mem_fns_map_[voffset] = virtual_mem_fns_at_voffset;
23822  }
23823  else
23824  {
23825  for (m = i->second.begin() ; m != i->second.end(); ++m)
23826  if (m->get() == method.get())
23827  break;
23828  if (m == i->second.end())
23829  i->second.push_back(method);
23830  }
23831 }
23832 
23833 /// Return true iff the class has no entity in its scope.
23834 bool
23836 {return priv_->bases_.empty() && has_no_member();}
23837 
23838 /// Test if the current instance of @ref class_decl has virtual member
23839 /// functions.
23840 ///
23841 /// @return true iff the current instance of @ref class_decl has
23842 /// virtual member functions.
23843 bool
23845 {return !get_virtual_mem_fns().empty();}
23846 
23847 /// Test if the current instance of @ref class_decl has at least one
23848 /// virtual base.
23849 ///
23850 /// @return true iff the current instance of @ref class_decl has a
23851 /// virtual member function.
23852 bool
23854 {
23855  for (base_specs::const_iterator b = get_base_specifiers().begin();
23856  b != get_base_specifiers().end();
23857  ++b)
23858  if ((*b)->get_is_virtual()
23859  || (*b)->get_base_class()->has_virtual_bases())
23860  return true;
23861 
23862  return false;
23863 }
23864 
23865 /// Test if the current instance has a vtable.
23866 ///
23867 /// This is only valid for a C++ program.
23868 ///
23869 /// Basically this function checks if the class has either virtual
23870 /// functions, or virtual bases.
23871 bool
23873 {
23875  || has_virtual_bases())
23876  return true;
23877  return false;
23878 }
23879 
23880 /// Get the highest vtable offset of all the virtual methods of the
23881 /// class.
23882 ///
23883 /// @return the highest vtable offset of all the virtual methods of
23884 /// the class.
23885 ssize_t
23887 {
23888  ssize_t offset = -1;
23889  for (class_decl::virtual_mem_fn_map_type::const_iterator e =
23890  get_virtual_mem_fns_map().begin();
23891  e != get_virtual_mem_fns_map().end();
23892  ++e)
23893  if (e->first > offset)
23894  offset = e->first;
23895 
23896  return offset;
23897 }
23898 
23899 /// Return the hash value for the current instance.
23900 ///
23901 /// @return the hash value.
23902 size_t
23904 {
23905  class_decl::hash hash_class;
23906  return hash_class(this);
23907 }
23908 
23909 /// Test if two methods are equal without taking their symbol or
23910 /// linkage name into account.
23911 ///
23912 /// @param f the first method.
23913 ///
23914 /// @param s the second method.
23915 ///
23916 /// @return true iff @p f equals @p s without taking their linkage
23917 /// name or symbol into account.
23918 static bool
23919 methods_equal_modulo_elf_symbol(const method_decl_sptr& f,
23920  const method_decl_sptr& s)
23921 {
23922  method_decl_sptr first = f, second = s;
23923  elf_symbol_sptr saved_first_elf_symbol =
23924  first->get_symbol();
23925  elf_symbol_sptr saved_second_elf_symbol =
23926  second->get_symbol();
23927  interned_string saved_first_linkage_name =
23928  first->get_linkage_name();
23929  interned_string saved_second_linkage_name =
23930  second->get_linkage_name();
23931 
23932  first->set_symbol(elf_symbol_sptr());
23933  first->set_linkage_name("");
23934  second->set_symbol(elf_symbol_sptr());
23935  second->set_linkage_name("");
23936 
23937  bool equal = *first == *second;
23938 
23939  first->set_symbol(saved_first_elf_symbol);
23940  first->set_linkage_name(saved_first_linkage_name);
23941  second->set_symbol(saved_second_elf_symbol);
23942  second->set_linkage_name(saved_second_linkage_name);
23943 
23944  return equal;
23945 }
23946 
23947 /// Test if a given method is equivalent to at least of other method
23948 /// that is in a vector of methods.
23949 ///
23950 /// Note that "equivalent" here means being equal without taking the
23951 /// linkage name or the symbol of the methods into account.
23952 ///
23953 /// This is a sub-routine of the 'equals' function that compares @ref
23954 /// class_decl.
23955 ///
23956 /// @param method the method to compare.
23957 ///
23958 /// @param fns the vector of functions to compare @p method against.
23959 ///
23960 /// @return true iff @p is equivalent to at least one method in @p
23961 /// fns.
23962 static bool
23963 method_matches_at_least_one_in_vector(const method_decl_sptr& method,
23964  const class_decl::member_functions& fns)
23965 {
23966  for (class_decl::member_functions::const_iterator i = fns.begin();
23967  i != fns.end();
23968  ++i)
23969  // Note that the comparison must be done in this order: method ==
23970  // *i This is to keep the consistency of the comparison. It's
23971  // important especially when doing type canonicalization. The
23972  // already canonicalize type is the left operand, and the type
23973  // being canonicalized is the right operand. This comes from the
23974  // code in type_base::get_canonical_type_for().
23975  if (methods_equal_modulo_elf_symbol(method, *i))
23976  return true;
23977 
23978  return false;
23979 }
23980 
23981 /// Cancel the canonical type that was propagated.
23982 ///
23983 /// If we are in the process of comparing a type for the purpose of
23984 /// canonicalization, and if that type has been the target of the
23985 /// canonical type propagation optimization, then clear the propagated
23986 /// canonical type. See @ref OnTheFlyCanonicalization for more about
23987 /// the canonical type optimization
23988 ///
23989 /// @param t the type to consider.
23990 static bool
23991 maybe_cancel_propagated_canonical_type(const class_or_union& t)
23992 {
23993  const environment& env = t.get_environment();
23994  if (env.do_on_the_fly_canonicalization())
23995  if (is_type(&t)->priv_->canonical_type_propagated())
23996  {
23997  is_type(&t)->priv_->clear_propagated_canonical_type();
23998  env.priv_->remove_from_types_with_non_confirmed_propagated_ct(&t);
23999  return true;
24000  }
24001  return false;
24002 }
24003 
24004 /// Compares two instances of @ref class_decl.
24005 ///
24006 /// If the two intances are different, set a bitfield to give some
24007 /// insight about the kind of differences there are.
24008 ///
24009 /// @param l the first artifact of the comparison.
24010 ///
24011 /// @param r the second artifact of the comparison.
24012 ///
24013 /// @param k a pointer to a bitfield that gives information about the
24014 /// kind of changes there are between @p l and @p r. This one is set
24015 /// iff @p k is non-null and the function returns false.
24016 ///
24017 /// Please note that setting k to a non-null value does have a
24018 /// negative performance impact because even if @p l and @p r are not
24019 /// equal, the function keeps up the comparison in order to determine
24020 /// the different kinds of ways in which they are different.
24021 ///
24022 /// @return true if @p l equals @p r, false otherwise.
24023 bool
24024 equals(const class_decl& l, const class_decl& r, change_kind* k)
24025 {
24026  {
24027  // First of all, let's see if these two types haven't already been
24028  // compared. If so, and if the result of the comparison has been
24029  // cached, let's just re-use it, rather than comparing them all
24030  // over again.
24031  bool result = false;
24032  if (l.get_environment().priv_->is_type_comparison_cached(l, r, result))
24033  return result;
24034  }
24035 
24036  // if one of the classes is declaration-only then we take a fast
24037  // path here.
24039  ABG_RETURN(equals(static_cast<const class_or_union&>(l),
24040  static_cast<const class_or_union&>(r),
24041  k));
24042 
24043  bool had_canonical_type = !!r.get_naked_canonical_type();
24044  bool result = true;
24045  if (!equals(static_cast<const class_or_union&>(l),
24046  static_cast<const class_or_union&>(r),
24047  k))
24048  {
24049  result = false;
24050  if (!k)
24051  ABG_RETURN(result);
24052  }
24053 
24054  // If comparing the class_or_union 'part' of the type led to
24055  // canonical type propagation, then cancel that because it's too
24056  // early to do that at this point. We still need to compare bases
24057  // virtual members.
24058  if (!had_canonical_type)
24059  maybe_cancel_propagated_canonical_type(r);
24060 
24062 
24064 
24065 #define RETURN(value) CACHE_AND_RETURN_COMPARISON_RESULT(value)
24066 
24067  // Compare bases.
24068  if (l.get_base_specifiers().size() != r.get_base_specifiers().size())
24069  {
24070  result = false;
24071  if (k)
24072  *k |= LOCAL_TYPE_CHANGE_KIND;
24073  else
24074  RETURN(result);
24075  }
24076 
24077  for (class_decl::base_specs::const_iterator
24078  b0 = l.get_base_specifiers().begin(),
24079  b1 = r.get_base_specifiers().begin();
24080  (b0 != l.get_base_specifiers().end()
24081  && b1 != r.get_base_specifiers().end());
24082  ++b0, ++b1)
24083  if (*b0 != *b1)
24084  {
24085  result = false;
24086  if (k)
24087  {
24088  if (!types_have_similar_structure((*b0)->get_base_class().get(),
24089  (*b1)->get_base_class().get()))
24090  *k |= LOCAL_TYPE_CHANGE_KIND;
24091  else
24092  *k |= SUBTYPE_CHANGE_KIND;
24093  break;
24094  }
24095  RETURN(result);
24096  }
24097 
24098  // Compare virtual member functions
24099 
24100  // We look at the map that associates a given vtable offset to a
24101  // vector of virtual member functions that point to that offset.
24102  //
24103  // This is because there are cases where several functions can
24104  // point to the same virtual table offset.
24105  //
24106  // This is usually the case for virtual destructors. Even though
24107  // there can be only one virtual destructor declared in source
24108  // code, there are actually potentially up to three generated
24109  // functions for that destructor. Some of these generated
24110  // functions can be clones of other functions that are among those
24111  // generated ones. In any cases, they all have the same
24112  // properties, including the vtable offset property.
24113 
24114  // So, there should be the same number of different vtable
24115  // offsets, the size of two maps must be equals.
24116  if (l.get_virtual_mem_fns_map().size()
24117  != r.get_virtual_mem_fns_map().size())
24118  {
24119  result = false;
24120  if (k)
24122  else
24123  RETURN(result);
24124  }
24125 
24126  // Then, each virtual member function of a given vtable offset in
24127  // the first class type, must match an equivalent virtual member
24128  // function of a the same vtable offset in the second class type.
24129  //
24130  // By "match", I mean that the two virtual member function should
24131  // be equal if we don't take into account their symbol name or
24132  // their linkage name. This is because two destructor functions
24133  // clones (for instance) might have different linkage name, but
24134  // are still equivalent if their other properties are the same.
24135  for (class_decl::virtual_mem_fn_map_type::const_iterator first_v_fn_entry =
24136  l.get_virtual_mem_fns_map().begin();
24137  first_v_fn_entry != l.get_virtual_mem_fns_map().end();
24138  ++first_v_fn_entry)
24139  {
24140  unsigned voffset = first_v_fn_entry->first;
24141  const class_decl::member_functions& first_vfns =
24142  first_v_fn_entry->second;
24143 
24144  const class_decl::virtual_mem_fn_map_type::const_iterator
24145  second_v_fn_entry = r.get_virtual_mem_fns_map().find(voffset);
24146 
24147  if (second_v_fn_entry == r.get_virtual_mem_fns_map().end())
24148  {
24149  result = false;
24150  if (k)
24152  RETURN(result);
24153  }
24154 
24155  const class_decl::member_functions& second_vfns =
24156  second_v_fn_entry->second;
24157 
24158  bool matches = false;
24159  for (class_decl::member_functions::const_iterator i =
24160  first_vfns.begin();
24161  i != first_vfns.end();
24162  ++i)
24163  if (method_matches_at_least_one_in_vector(*i, second_vfns))
24164  {
24165  matches = true;
24166  break;
24167  }
24168 
24169  if (!matches)
24170  {
24171  result = false;
24172  if (k)
24173  *k |= SUBTYPE_CHANGE_KIND;
24174  else
24175  RETURN(result);
24176  }
24177  }
24178 
24179  RETURN(result);
24180 #undef RETURN
24181 }
24182 
24183 /// Copy a method of a class into a new class.
24184 ///
24185 /// @param klass the class into which the method is to be copied.
24186 ///
24187 /// @param method the method to copy into @p klass.
24188 ///
24189 /// @return the resulting newly copied method.
24190 method_decl_sptr
24191 copy_member_function(const class_decl_sptr& clazz, const method_decl_sptr& f)
24192 {return copy_member_function(static_pointer_cast<class_or_union>(clazz), f);}
24193 
24194 /// Copy a method of a class into a new class.
24195 ///
24196 /// @param klass the class into which the method is to be copied.
24197 ///
24198 /// @param method the method to copy into @p klass.
24199 ///
24200 /// @return the resulting newly copied method.
24201 method_decl_sptr
24203 {return copy_member_function(static_pointer_cast<class_or_union>(clazz), f);}
24204 
24205 /// Comparison operator for @ref class_decl.
24206 ///
24207 /// @param other the instance of @ref class_decl to compare against.
24208 ///
24209 /// @return true iff the current instance of @ref class_decl equals @p
24210 /// other.
24211 bool
24213 {
24214  const class_decl* op = is_class_type(&other);
24215  if (!op)
24216  return false;
24217 
24218  // If this is a decl-only type (and thus with no canonical type),
24219  // use the canonical type of the definition, if any.
24220  const class_decl *l = 0;
24222  l = dynamic_cast<const class_decl*>(get_naked_definition_of_declaration());
24223  if (l == 0)
24224  l = this;
24225 
24226  ABG_ASSERT(l);
24227 
24228  // Likewise for the other type.
24229  const class_decl *r = 0;
24230  if (op->get_is_declaration_only())
24231  r = dynamic_cast<const class_decl*>(op->get_naked_definition_of_declaration());
24232  if (r == 0)
24233  r = op;
24234 
24235  ABG_ASSERT(r);
24236 
24237  return try_canonical_compare(l, r);
24238 }
24239 
24240 /// Equality operator for class_decl.
24241 ///
24242 /// Re-uses the equality operator that takes a decl_base.
24243 ///
24244 /// @param other the other class_decl to compare against.
24245 ///
24246 /// @return true iff the current instance equals the other one.
24247 bool
24249 {
24250  const decl_base* o = is_decl(&other);
24251  if (!o)
24252  return false;
24253  return *this == *o;
24254 }
24255 
24256 /// Equality operator for class_decl.
24257 ///
24258 /// Re-uses the equality operator that takes a decl_base.
24259 ///
24260 /// @param other the other class_decl to compare against.
24261 ///
24262 /// @return true iff the current instance equals the other one.
24263 bool
24265 {
24266  const decl_base& o = other;
24267  return *this == o;
24268 }
24269 
24270 /// Comparison operator for @ref class_decl.
24271 ///
24272 /// @param other the instance of @ref class_decl to compare against.
24273 ///
24274 /// @return true iff the current instance of @ref class_decl equals @p
24275 /// other.
24276 bool
24278 {
24279  const decl_base& o = other;
24280  return *this == o;
24281 }
24282 
24283 /// Turn equality of shared_ptr of class_decl into a deep equality;
24284 /// that is, make it compare the pointed to objects too.
24285 ///
24286 /// @param l the shared_ptr of class_decl on left-hand-side of the
24287 /// equality.
24288 ///
24289 /// @param r the shared_ptr of class_decl on right-hand-side of the
24290 /// equality.
24291 ///
24292 /// @return true if the class_decl pointed to by the shared_ptrs are
24293 /// equal, false otherwise.
24294 bool
24296 {
24297  if (l.get() == r.get())
24298  return true;
24299  if (!!l != !!r)
24300  return false;
24301 
24302  return *l == *r;
24303 }
24304 
24305 /// Turn inequality of shared_ptr of class_decl into a deep equality;
24306 /// that is, make it compare the pointed to objects too.
24307 ///
24308 /// @param l the shared_ptr of class_decl on left-hand-side of the
24309 /// equality.
24310 ///
24311 /// @param r the shared_ptr of class_decl on right-hand-side of the
24312 /// equality.
24313 ///
24314 /// @return true if the class_decl pointed to by the shared_ptrs are
24315 /// different, false otherwise.
24316 bool
24318 {return !operator==(l, r);}
24319 
24320 /// Turn equality of shared_ptr of class_or_union into a deep
24321 /// equality; that is, make it compare the pointed to objects too.
24322 ///
24323 /// @param l the left-hand-side operand of the operator
24324 ///
24325 /// @param r the right-hand-side operand of the operator.
24326 ///
24327 /// @return true iff @p l equals @p r.
24328 bool
24329 operator==(const class_or_union_sptr& l, const class_or_union_sptr& r)
24330 {
24331  if (l.get() == r.get())
24332  return true;
24333  if (!!l != !!r)
24334  return false;
24335 
24336  return *l == *r;
24337 }
24338 
24339 /// Turn inequality of shared_ptr of class_or_union into a deep
24340 /// equality; that is, make it compare the pointed to objects too.
24341 ///
24342 /// @param l the left-hand-side operand of the operator
24343 ///
24344 /// @param r the right-hand-side operand of the operator.
24345 ///
24346 /// @return true iff @p l is different from @p r.
24347 bool
24348 operator!=(const class_or_union_sptr& l, const class_or_union_sptr& r)
24349 {return !operator==(l, r);}
24350 
24351 /// This implements the ir_traversable_base::traverse pure virtual
24352 /// function.
24353 ///
24354 /// @param v the visitor used on the current instance and on its
24355 /// members.
24356 ///
24357 /// @return true if the entire IR node tree got traversed, false
24358 /// otherwise.
24359 bool
24361 {
24362  if (v.type_node_has_been_visited(this))
24363  return true;
24364 
24365  if (visiting())
24366  return true;
24367 
24368  if (v.visit_begin(this))
24369  {
24370  visiting(true);
24371  bool stop = false;
24372 
24373  for (base_specs::const_iterator i = get_base_specifiers().begin();
24374  i != get_base_specifiers().end();
24375  ++i)
24376  {
24377  if (!(*i)->traverse(v))
24378  {
24379  stop = true;
24380  break;
24381  }
24382  }
24383 
24384  if (!stop)
24385  for (data_members::const_iterator i = get_data_members().begin();
24386  i != get_data_members().end();
24387  ++i)
24388  if (!(*i)->traverse(v))
24389  {
24390  stop = true;
24391  break;
24392  }
24393 
24394  if (!stop)
24395  for (member_functions::const_iterator i= get_member_functions().begin();
24396  i != get_member_functions().end();
24397  ++i)
24398  if (!(*i)->traverse(v))
24399  {
24400  stop = true;
24401  break;
24402  }
24403 
24404  if (!stop)
24405  for (member_types::const_iterator i = get_member_types().begin();
24406  i != get_member_types().end();
24407  ++i)
24408  if (!(*i)->traverse(v))
24409  {
24410  stop = true;
24411  break;
24412  }
24413 
24414  if (!stop)
24415  for (member_function_templates::const_iterator i =
24417  i != get_member_function_templates().end();
24418  ++i)
24419  if (!(*i)->traverse(v))
24420  {
24421  stop = true;
24422  break;
24423  }
24424 
24425  if (!stop)
24426  for (member_class_templates::const_iterator i =
24427  get_member_class_templates().begin();
24428  i != get_member_class_templates().end();
24429  ++i)
24430  if (!(*i)->traverse(v))
24431  {
24432  stop = true;
24433  break;
24434  }
24435  visiting(false);
24436  }
24437 
24438  bool result = v.visit_end(this);
24439  v.mark_type_node_as_visited(this);
24440  return result;
24441 }
24442 
24443 /// Destructor of the @ref class_decl type.
24445 {delete priv_;}
24446 
24447 context_rel::~context_rel()
24448 {}
24449 
24450 bool
24451 member_base::operator==(const member_base& o) const
24452 {
24454  && get_is_static() == o.get_is_static());
24455 }
24456 
24457 /// Equality operator for smart pointers to @ref
24458 /// class_decl::base_specs.
24459 ///
24460 /// This compares the pointed-to objects.
24461 ///
24462 /// @param l the first instance to consider.
24463 ///
24464 /// @param r the second instance to consider.
24465 ///
24466 /// @return true iff @p l equals @p r.
24467 bool
24469  const class_decl::base_spec_sptr& r)
24470 {
24471  if (l.get() == r.get())
24472  return true;
24473  if (!!l != !!r)
24474  return false;
24475 
24476  return *l == static_cast<const decl_base&>(*r);
24477 }
24478 
24479 /// Inequality operator for smart pointers to @ref
24480 /// class_decl::base_specs.
24481 ///
24482 /// This compares the pointed-to objects.
24483 ///
24484 /// @param l the first instance to consider.
24485 ///
24486 /// @param r the second instance to consider.
24487 ///
24488 /// @return true iff @p l is different from @p r.
24489 bool
24491  const class_decl::base_spec_sptr& r)
24492 {return !operator==(l, r);}
24493 
24494 /// Test if an ABI artifact is a class base specifier.
24495 ///
24496 /// @param tod the ABI artifact to consider.
24497 ///
24498 /// @return a pointer to the @ref class_decl::base_spec sub-object of
24499 /// @p tod iff it's a class base specifier.
24502 {
24503  return dynamic_cast<class_decl::base_spec*>
24504  (const_cast<type_or_decl_base*>(tod));
24505 }
24506 
24507 /// Test if an ABI artifact is a class base specifier.
24508 ///
24509 /// @param tod the ABI artifact to consider.
24510 ///
24511 /// @return a pointer to the @ref class_decl::base_spec sub-object of
24512 /// @p tod iff it's a class base specifier.
24515 {return dynamic_pointer_cast<class_decl::base_spec>(tod);}
24516 
24517 bool
24518 member_function_template::operator==(const member_base& other) const
24519 {
24520  try
24521  {
24522  const member_function_template& o =
24523  dynamic_cast<const member_function_template&>(other);
24524 
24525  if (!(is_constructor() == o.is_constructor()
24526  && is_const() == o.is_const()
24527  && member_base::operator==(o)))
24528  return false;
24529 
24530  if (function_tdecl_sptr ftdecl = as_function_tdecl())
24531  {
24532  function_tdecl_sptr other_ftdecl = o.as_function_tdecl();
24533  if (other_ftdecl)
24534  return ftdecl->function_tdecl::operator==(*other_ftdecl);
24535  }
24536  }
24537  catch(...)
24538  {}
24539  return false;
24540 }
24541 
24542 /// Equality operator for smart pointers to @ref
24543 /// member_function_template. This is compares the
24544 /// pointed-to instances.
24545 ///
24546 /// @param l the first instance to consider.
24547 ///
24548 /// @param r the second instance to consider.
24549 ///
24550 /// @return true iff @p l equals @p r.
24551 bool
24552 operator==(const member_function_template_sptr& l,
24553  const member_function_template_sptr& r)
24554 {
24555  if (l.get() == r.get())
24556  return true;
24557  if (!!l != !!r)
24558  return false;
24559 
24560  return *l == *r;
24561 }
24562 
24563 /// Inequality operator for smart pointers to @ref
24564 /// member_function_template. This is compares the pointed-to
24565 /// instances.
24566 ///
24567 /// @param l the first instance to consider.
24568 ///
24569 /// @param r the second instance to consider.
24570 ///
24571 /// @return true iff @p l equals @p r.
24572 bool
24573 operator!=(const member_function_template_sptr& l,
24574  const member_function_template_sptr& r)
24575 {return !operator==(l, r);}
24576 
24577 /// This implements the ir_traversable_base::traverse pure virtual
24578 /// function.
24579 ///
24580 /// @param v the visitor used on the current instance and on its
24581 /// underlying function template.
24582 ///
24583 /// @return true if the entire IR node tree got traversed, false
24584 /// otherwise.
24585 bool
24587 {
24588  if (visiting())
24589  return true;
24590 
24591  if (v.visit_begin(this))
24592  {
24593  visiting(true);
24594  if (function_tdecl_sptr f = as_function_tdecl())
24595  f->traverse(v);
24596  visiting(false);
24597  }
24598  return v.visit_end(this);
24599 }
24600 
24601 /// Equality operator of the the @ref member_class_template class.
24602 ///
24603 /// @param other the other @ref member_class_template to compare against.
24604 ///
24605 /// @return true iff the current instance equals @p other.
24606 bool
24608 {
24609  try
24610  {
24611  const member_class_template& o =
24612  dynamic_cast<const member_class_template&>(other);
24613 
24614  if (!member_base::operator==(o))
24615  return false;
24616 
24617  return as_class_tdecl()->class_tdecl::operator==(o);
24618  }
24619  catch(...)
24620  {return false;}
24621 }
24622 
24623 /// Equality operator of the the @ref member_class_template class.
24624 ///
24625 /// @param other the other @ref member_class_template to compare against.
24626 ///
24627 /// @return true iff the current instance equals @p other.
24628 bool
24630 {
24631  if (!decl_base::operator==(other))
24632  return false;
24633  return as_class_tdecl()->class_tdecl::operator==(other);
24634 }
24635 
24636 /// Comparison operator for the @ref member_class_template
24637 /// type.
24638 ///
24639 /// @param other the other instance of @ref
24640 /// member_class_template to compare against.
24641 ///
24642 /// @return true iff the two instances are equal.
24643 bool
24645 {
24646  const decl_base* o = dynamic_cast<const decl_base*>(&other);
24647  return *this == *o;
24648 }
24649 
24650 /// Comparison operator for the @ref member_class_template
24651 /// type.
24652 ///
24653 /// @param l the first argument of the operator.
24654 ///
24655 /// @param r the second argument of the operator.
24656 ///
24657 /// @return true iff the two instances are equal.
24658 bool
24659 operator==(const member_class_template_sptr& l,
24660  const member_class_template_sptr& r)
24661 {
24662  if (l.get() == r.get())
24663  return true;
24664  if (!!l != !!r)
24665  return false;
24666 
24667  return *l == *r;
24668 }
24669 
24670 /// Inequality operator for the @ref member_class_template
24671 /// type.
24672 ///
24673 /// @param l the first argument of the operator.
24674 ///
24675 /// @param r the second argument of the operator.
24676 ///
24677 /// @return true iff the two instances are equal.
24678 bool
24679 operator!=(const member_class_template_sptr& l,
24680  const member_class_template_sptr& r)
24681 {return !operator==(l, r);}
24682 
24683 /// This implements the ir_traversable_base::traverse pure virtual
24684 /// function.
24685 ///
24686 /// @param v the visitor used on the current instance and on the class
24687 /// pattern of the template.
24688 ///
24689 /// @return true if the entire IR node tree got traversed, false
24690 /// otherwise.
24691 bool
24693 {
24694  if (visiting())
24695  return true;
24696 
24697  if (v.visit_begin(this))
24698  {
24699  visiting(true);
24700  if (class_tdecl_sptr t = as_class_tdecl())
24701  t->traverse(v);
24702  visiting(false);
24703  }
24704  return v.visit_end(this);
24705 }
24706 
24707 /// Streaming operator for class_decl::access_specifier.
24708 ///
24709 /// @param o the output stream to serialize the access specifier to.
24710 ///
24711 /// @param a the access specifier to serialize.
24712 ///
24713 /// @return the output stream.
24714 std::ostream&
24715 operator<<(std::ostream& o, access_specifier a)
24716 {
24717  string r;
24718 
24719  switch (a)
24720  {
24721  case no_access:
24722  r = "none";
24723  break;
24724  case private_access:
24725  r = "private";
24726  break;
24727  case protected_access:
24728  r = "protected";
24729  break;
24730  case public_access:
24731  r= "public";
24732  break;
24733  };
24734  o << r;
24735  return o;
24736 }
24737 
24738 /// Sets the static-ness property of a class member.
24739 ///
24740 /// @param d the class member to set the static-ness property for.
24741 /// Note that this must be a class member otherwise the function
24742 /// aborts the current process.
24743 ///
24744 /// @param s this must be true if the member is to be static, false
24745 /// otherwise.
24746 void
24748 {
24750 
24751  context_rel* c = d.get_context_rel();
24752  ABG_ASSERT(c);
24753 
24754  c->set_is_static(s);
24755 
24756  scope_decl* scope = d.get_scope();
24757 
24758  if (class_or_union* cl = is_class_or_union_type(scope))
24759  {
24760  if (var_decl* v = is_var_decl(&d))
24761  {
24762  if (s)
24763  // remove from the non-static data members
24764  for (class_decl::data_members::iterator i =
24765  cl->priv_->non_static_data_members_.begin();
24766  i != cl->priv_->non_static_data_members_.end();
24767  ++i)
24768  {
24769  if ((*i)->get_name() == v->get_name())
24770  {
24771  cl->priv_->non_static_data_members_.erase(i);
24772  break;
24773  }
24774  }
24775  else
24776  {
24777  bool is_already_in_non_static_data_members = false;
24778  for (class_or_union::data_members::iterator i =
24779  cl->priv_->non_static_data_members_.begin();
24780  i != cl->priv_->non_static_data_members_.end();
24781  ++i)
24782  {
24783  if ((*i)->get_name() == v->get_name())
24784  {
24785  is_already_in_non_static_data_members = true;
24786  break;
24787  }
24788  }
24789  if (!is_already_in_non_static_data_members)
24790  {
24791  var_decl_sptr var;
24792  // add to non-static data members.
24793  for (class_or_union::data_members::const_iterator i =
24794  cl->priv_->data_members_.begin();
24795  i != cl->priv_->data_members_.end();
24796  ++i)
24797  {
24798  if ((*i)->get_name() == v->get_name())
24799  {
24800  var = *i;
24801  break;
24802  }
24803  }
24804  ABG_ASSERT(var);
24805  cl->priv_->non_static_data_members_.push_back(var);
24806  }
24807  }
24808  }
24809  }
24810 }
24811 
24812 /// Sets the static-ness property of a class member.
24813 ///
24814 /// @param d the class member to set the static-ness property for.
24815 /// Note that this must be a class member otherwise the function
24816 /// aborts the current process.
24817 ///
24818 /// @param s this must be true if the member is to be static, false
24819 /// otherwise.
24820 void
24821 set_member_is_static(const decl_base_sptr& d, bool s)
24822 {set_member_is_static(*d, s);}
24823 
24824 // </class_decl>
24825 
24826 // <union_decl>
24827 
24828 /// Constructor for the @ref union_decl type.
24829 ///
24830 /// @param env the @ref environment we are operating from.
24831 ///
24832 /// @param name the name of the union type.
24833 ///
24834 /// @param size_in_bits the size of the union, in bits.
24835 ///
24836 /// @param locus the location of the type.
24837 ///
24838 /// @param vis the visibility of instances of @ref union_decl.
24839 ///
24840 /// @param mbr_types the member types of the union.
24841 ///
24842 /// @param data_mbrs the data members of the union.
24843 ///
24844 /// @param member_fns the member functions of the union.
24845 union_decl::union_decl(const environment& env, const string& name,
24846  size_t size_in_bits, const location& locus,
24847  visibility vis, member_types& mbr_types,
24848  data_members& data_mbrs, member_functions& member_fns)
24849  : type_or_decl_base(env,
24850  UNION_TYPE
24851  | ABSTRACT_TYPE_BASE
24852  | ABSTRACT_DECL_BASE),
24853  decl_base(env, name, locus, name, vis),
24854  type_base(env, size_in_bits, 0),
24855  class_or_union(env, name, size_in_bits, 0,
24856  locus, vis, mbr_types, data_mbrs, member_fns)
24857 {
24858  runtime_type_instance(this);
24859 }
24860 
24861 /// Constructor for the @ref union_decl type.
24862 ///
24863 /// @param env the @ref environment we are operating from.
24864 ///
24865 /// @param name the name of the union type.
24866 ///
24867 /// @param size_in_bits the size of the union, in bits.
24868 ///
24869 /// @param locus the location of the type.
24870 ///
24871 /// @param vis the visibility of instances of @ref union_decl.
24872 ///
24873 /// @param mbr_types the member types of the union.
24874 ///
24875 /// @param data_mbrs the data members of the union.
24876 ///
24877 /// @param member_fns the member functions of the union.
24878 ///
24879 /// @param is_anonymous whether the newly created instance is
24880 /// anonymous.
24881 union_decl::union_decl(const environment& env, const string& name,
24882  size_t size_in_bits, const location& locus,
24883  visibility vis, member_types& mbr_types,
24884  data_members& data_mbrs, member_functions& member_fns,
24885  bool is_anonymous)
24886  : type_or_decl_base(env,
24887  UNION_TYPE
24888  | ABSTRACT_TYPE_BASE
24889  | ABSTRACT_DECL_BASE),
24890  decl_base(env, name, locus,
24891  // If the class is anonymous then by default it won't
24892  // have a linkage name. Also, the anonymous class does
24893  // have an internal-only unique name that is generally
24894  // not taken into account when comparing classes; such a
24895  // unique internal-only name, when used as a linkage
24896  // name might introduce spurious comparison false
24897  // negatives.
24898  /*linkage_name=*/is_anonymous ? string() : name,
24899  vis),
24900  type_base(env, size_in_bits, 0),
24901  class_or_union(env, name, size_in_bits, 0,
24902  locus, vis, mbr_types, data_mbrs, member_fns)
24903 {
24904  runtime_type_instance(this);
24905  set_is_anonymous(is_anonymous);
24906 }
24907 
24908 /// Constructor for the @ref union_decl type.
24909 ///
24910 /// @param env the @ref environment we are operating from.
24911 ///
24912 /// @param name the name of the union type.
24913 ///
24914 /// @param size_in_bits the size of the union, in bits.
24915 ///
24916 /// @param locus the location of the type.
24917 ///
24918 /// @param vis the visibility of instances of @ref union_decl.
24919 union_decl::union_decl(const environment& env, const string& name,
24920  size_t size_in_bits, const location& locus,
24921  visibility vis)
24922  : type_or_decl_base(env,
24923  UNION_TYPE
24924  | ABSTRACT_TYPE_BASE
24925  | ABSTRACT_DECL_BASE
24926  | ABSTRACT_SCOPE_TYPE_DECL
24927  | ABSTRACT_SCOPE_DECL),
24928  decl_base(env, name, locus, name, vis),
24929  type_base(env, size_in_bits, 0),
24930  class_or_union(env, name, size_in_bits,
24931  0, locus, vis)
24932 {
24933  runtime_type_instance(this);
24934 }
24935 
24936 /// Constructor for the @ref union_decl type.
24937 ///
24938 /// @param env the @ref environment we are operating from.
24939 ///
24940 /// @param name the name of the union type.
24941 ///
24942 /// @param size_in_bits the size of the union, in bits.
24943 ///
24944 /// @param locus the location of the type.
24945 ///
24946 /// @param vis the visibility of instances of @ref union_decl.
24947 ///
24948 /// @param is_anonymous whether the newly created instance is
24949 /// anonymous.
24950 union_decl::union_decl(const environment& env, const string& name,
24951  size_t size_in_bits, const location& locus,
24952  visibility vis, bool is_anonymous)
24953  : type_or_decl_base(env,
24954  UNION_TYPE
24955  | ABSTRACT_TYPE_BASE
24956  | ABSTRACT_DECL_BASE
24957  | ABSTRACT_SCOPE_TYPE_DECL
24958  | ABSTRACT_SCOPE_DECL),
24959  decl_base(env, name, locus,
24960  // If the class is anonymous then by default it won't
24961  // have a linkage name. Also, the anonymous class does
24962  // have an internal-only unique name that is generally
24963  // not taken into account when comparing classes; such a
24964  // unique internal-only name, when used as a linkage
24965  // name might introduce spurious comparison false
24966  // negatives.
24967  /*linkage_name=*/is_anonymous ? string() : name,
24968  vis),
24969  type_base(env, size_in_bits, 0),
24970  class_or_union(env, name, size_in_bits,
24971  0, locus, vis)
24972 {
24973  runtime_type_instance(this);
24974  set_is_anonymous(is_anonymous);
24975 }
24976 
24977 /// Constructor for the @ref union_decl type.
24978 ///
24979 /// @param env the @ref environment we are operating from.
24980 ///
24981 /// @param name the name of the union type.
24982 ///
24983 /// @param is_declaration_only a boolean saying whether the instance
24984 /// represents a declaration only, or not.
24985 union_decl::union_decl(const environment& env,
24986  const string& name,
24987  bool is_declaration_only)
24988  : type_or_decl_base(env,
24989  UNION_TYPE
24990  | ABSTRACT_TYPE_BASE
24991  | ABSTRACT_DECL_BASE
24992  | ABSTRACT_SCOPE_TYPE_DECL
24993  | ABSTRACT_SCOPE_DECL),
24994  decl_base(env, name, location(), name),
24995  type_base(env, 0, 0),
24996  class_or_union(env, name, is_declaration_only)
24997 {
24998  runtime_type_instance(this);
24999 }
25000 
25001 /// Getter of the pretty representation of the current instance of
25002 /// @ref union_decl.
25003 ///
25004 /// @param internal set to true if the call is intended to get a
25005 /// representation of the decl (or type) for the purpose of canonical
25006 /// type comparison. This is mainly used in the function
25007 /// type_base::get_canonical_type_for().
25008 ///
25009 /// In other words if the argument for this parameter is true then the
25010 /// call is meant for internal use (for technical use inside the
25011 /// library itself), false otherwise. If you don't know what this is
25012 /// for, then set it to false.
25013 ///
25014 /// @param qualified_name if true, names emitted in the pretty
25015 /// representation are fully qualified.
25016 ///
25017 /// @return the pretty representaion for a union_decl.
25018 string
25020  bool qualified_name) const
25021 {
25022  string repr;
25023  if (get_is_anonymous())
25024  {
25025  if (internal && !get_name().empty())
25026  repr = string("union ") +
25027  get_type_name(this, qualified_name, /*internal=*/true);
25028  else
25030  /*one_line=*/true,
25031  internal);
25032  }
25033  else
25034  {
25035  repr = "union ";
25036  if (qualified_name)
25037  repr += get_qualified_name(internal);
25038  else
25039  repr += get_name();
25040  }
25041 
25042  return repr;
25043 }
25044 
25045 /// Comparison operator for @ref union_decl.
25046 ///
25047 /// @param other the instance of @ref union_decl to compare against.
25048 ///
25049 /// @return true iff the current instance of @ref union_decl equals @p
25050 /// other.
25051 bool
25053 {
25054  const union_decl* op = dynamic_cast<const union_decl*>(&other);
25055  if (!op)
25056  return false;
25057  return try_canonical_compare(this, op);
25058 }
25059 
25060 /// Equality operator for union_decl.
25061 ///
25062 /// Re-uses the equality operator that takes a decl_base.
25063 ///
25064 /// @param other the other union_decl to compare against.
25065 ///
25066 /// @return true iff the current instance equals the other one.
25067 bool
25069 {
25070  const decl_base *o = dynamic_cast<const decl_base*>(&other);
25071  if (!o)
25072  return false;
25073  return *this == *o;
25074 }
25075 
25076 /// Equality operator for union_decl.
25077 ///
25078 /// Re-uses the equality operator that takes a decl_base.
25079 ///
25080 /// @param other the other union_decl to compare against.
25081 ///
25082 /// @return true iff the current instance equals the other one.
25083 bool
25085 {
25086  const decl_base *o = dynamic_cast<const decl_base*>(&other);
25087  return *this == *o;
25088 }
25089 
25090 /// Comparison operator for @ref union_decl.
25091 ///
25092 /// @param other the instance of @ref union_decl to compare against.
25093 ///
25094 /// @return true iff the current instance of @ref union_decl equals @p
25095 /// other.
25096 bool
25098 {
25099  const decl_base& o = other;
25100  return *this == o;
25101 }
25102 
25103 /// This implements the ir_traversable_base::traverse pure virtual
25104 /// function.
25105 ///
25106 /// @param v the visitor used on the current instance and on its
25107 /// members.
25108 ///
25109 /// @return true if the entire IR node tree got traversed, false
25110 /// otherwise.
25111 bool
25113 {
25114  if (v.type_node_has_been_visited(this))
25115  return true;
25116 
25117  if (visiting())
25118  return true;
25119 
25120  if (v.visit_begin(this))
25121  {
25122  visiting(true);
25123  bool stop = false;
25124 
25125  if (!stop)
25126  for (data_members::const_iterator i = get_data_members().begin();
25127  i != get_data_members().end();
25128  ++i)
25129  if (!(*i)->traverse(v))
25130  {
25131  stop = true;
25132  break;
25133  }
25134 
25135  if (!stop)
25136  for (member_functions::const_iterator i= get_member_functions().begin();
25137  i != get_member_functions().end();
25138  ++i)
25139  if (!(*i)->traverse(v))
25140  {
25141  stop = true;
25142  break;
25143  }
25144 
25145  if (!stop)
25146  for (member_types::const_iterator i = get_member_types().begin();
25147  i != get_member_types().end();
25148  ++i)
25149  if (!(*i)->traverse(v))
25150  {
25151  stop = true;
25152  break;
25153  }
25154 
25155  if (!stop)
25156  for (member_function_templates::const_iterator i =
25158  i != get_member_function_templates().end();
25159  ++i)
25160  if (!(*i)->traverse(v))
25161  {
25162  stop = true;
25163  break;
25164  }
25165 
25166  if (!stop)
25167  for (member_class_templates::const_iterator i =
25168  get_member_class_templates().begin();
25169  i != get_member_class_templates().end();
25170  ++i)
25171  if (!(*i)->traverse(v))
25172  {
25173  stop = true;
25174  break;
25175  }
25176  visiting(false);
25177  }
25178 
25179  bool result = v.visit_end(this);
25180  v.mark_type_node_as_visited(this);
25181  return result;
25182 }
25183 
25184 /// Destructor of the @ref union_decl type.
25186 {}
25187 
25188 /// Compares two instances of @ref union_decl.
25189 ///
25190 /// If the two intances are different, set a bitfield to give some
25191 /// insight about the kind of differences there are.
25192 ///
25193 /// @param l the first artifact of the comparison.
25194 ///
25195 /// @param r the second artifact of the comparison.
25196 ///
25197 /// @param k a pointer to a bitfield that gives information about the
25198 /// kind of changes there are between @p l and @p r. This one is set
25199 /// iff @p k is non-null and the function returns false.
25200 ///
25201 /// Please note that setting k to a non-null value does have a
25202 /// negative performance impact because even if @p l and @p r are not
25203 /// equal, the function keeps up the comparison in order to determine
25204 /// the different kinds of ways in which they are different.
25205 ///
25206 /// @return true if @p l equals @p r, false otherwise.
25207 bool
25208 equals(const union_decl& l, const union_decl& r, change_kind* k)
25209 {
25210 
25212 
25213  {
25214  // First of all, let's see if these two types haven't already been
25215  // compared. If so, and if the result of the comparison has been
25216  // cached, let's just re-use it, rather than comparing them all
25217  // over again.
25218  bool result = false;
25219  if (l.get_environment().priv_->is_type_comparison_cached(l, r, result))
25220  return result;
25221  }
25222 
25223  bool result = equals(static_cast<const class_or_union&>(l),
25224  static_cast<const class_or_union&>(r),
25225  k);
25226 
25228 }
25229 
25230 /// Copy a method of a @ref union_decl into a new @ref
25231 /// union_decl.
25232 ///
25233 /// @param t the @ref union_decl into which the method is to be copied.
25234 ///
25235 /// @param method the method to copy into @p t.
25236 ///
25237 /// @return the resulting newly copied method.
25238 method_decl_sptr
25239 copy_member_function(const union_decl_sptr& union_type,
25240  const method_decl_sptr& f)
25241 {return copy_member_function(union_type, f.get());}
25242 
25243 /// Copy a method of a @ref union_decl into a new @ref
25244 /// union_decl.
25245 ///
25246 /// @param t the @ref union_decl into which the method is to be copied.
25247 ///
25248 /// @param method the method to copy into @p t.
25249 ///
25250 /// @return the resulting newly copied method.
25251 method_decl_sptr
25252 copy_member_function(const union_decl_sptr& union_type,
25253  const method_decl* f)
25254 {
25255  const class_or_union_sptr t = union_type;
25256  return copy_member_function(t, f);
25257 }
25258 
25259 /// Turn equality of shared_ptr of union_decl into a deep equality;
25260 /// that is, make it compare the pointed to objects too.
25261 ///
25262 /// @param l the left-hand-side operand of the operator
25263 ///
25264 /// @param r the right-hand-side operand of the operator.
25265 ///
25266 /// @return true iff @p l equals @p r.
25267 bool
25268 operator==(const union_decl_sptr& l, const union_decl_sptr& r)
25269 {
25270  if (l.get() == r.get())
25271  return true;
25272  if (!!l != !!r)
25273  return false;
25274 
25275  return *l == *r;
25276 }
25277 
25278 /// Turn inequality of shared_ptr of union_decl into a deep equality;
25279 /// that is, make it compare the pointed to objects too.
25280 ///
25281 /// @param l the left-hand-side operand of the operator
25282 ///
25283 /// @param r the right-hand-side operand of the operator.
25284 ///
25285 /// @return true iff @p l is different from @p r.
25286 bool
25287 operator!=(const union_decl_sptr& l, const union_decl_sptr& r)
25288 {return !operator==(l, r);}
25289 // </union_decl>
25290 
25291 // <template_decl stuff>
25292 
25293 /// Data type of the private data of the @template_decl type.
25294 class template_decl::priv
25295 {
25296  friend class template_decl;
25297 
25298  std::list<template_parameter_sptr> parms_;
25299 public:
25300 
25301  priv()
25302  {}
25303 }; // end class template_decl::priv
25304 
25305 /// Add a new template parameter to the current instance of @ref
25306 /// template_decl.
25307 ///
25308 /// @param p the new template parameter to add.
25309 void
25311 {priv_->parms_.push_back(p);}
25312 
25313 /// Get the list of template parameters of the current instance of
25314 /// @ref template_decl.
25315 ///
25316 /// @return the list of template parameters.
25317 const std::list<template_parameter_sptr>&
25319 {return priv_->parms_;}
25320 
25321 /// Constructor.
25322 ///
25323 /// @param env the environment we are operating from.
25324 ///
25325 /// @param name the name of the template decl.
25326 ///
25327 /// @param locus the source location where the template declaration is
25328 /// defined.
25329 ///
25330 /// @param vis the visibility of the template declaration.
25331 template_decl::template_decl(const environment& env,
25332  const string& name,
25333  const location& locus,
25334  visibility vis)
25335  : type_or_decl_base(env, TEMPLATE_DECL | ABSTRACT_DECL_BASE),
25336  decl_base(env, name, locus, /*mangled_name=*/"", vis),
25337  priv_(new priv)
25338 {
25339  runtime_type_instance(this);
25340 }
25341 
25342 /// Destructor.
25344 {}
25345 
25346 /// Equality operator.
25347 ///
25348 /// @param o the other instance to compare against.
25349 ///
25350 /// @return true iff @p equals the current instance.
25351 bool
25353 {
25354  const template_decl* other = dynamic_cast<const template_decl*>(&o);
25355  if (!other)
25356  return false;
25357  return *this == *other;
25358 }
25359 
25360 /// Equality operator.
25361 ///
25362 /// @param o the other instance to compare against.
25363 ///
25364 /// @return true iff @p equals the current instance.
25365 bool
25367 {
25368  try
25369  {
25370  list<shared_ptr<template_parameter> >::const_iterator t0, t1;
25371  for (t0 = get_template_parameters().begin(),
25372  t1 = o.get_template_parameters().begin();
25373  (t0 != get_template_parameters().end()
25374  && t1 != o.get_template_parameters().end());
25375  ++t0, ++t1)
25376  {
25377  if (**t0 != **t1)
25378  return false;
25379  }
25380 
25381  if (t0 != get_template_parameters().end()
25382  || t1 != o.get_template_parameters().end())
25383  return false;
25384 
25385  return true;
25386  }
25387  catch(...)
25388  {return false;}
25389 }
25390 
25391 // </template_decl stuff>
25392 
25393 //<template_parameter>
25394 
25395 /// The type of the private data of the @ref template_parameter type.
25396 class template_parameter::priv
25397 {
25398  friend class template_parameter;
25399 
25400  unsigned index_;
25401  template_decl_wptr template_decl_;
25402  mutable bool hashing_started_;
25403  mutable bool comparison_started_;
25404 
25405  priv();
25406 
25407 public:
25408 
25409  priv(unsigned index, template_decl_sptr enclosing_template_decl)
25410  : index_(index),
25411  template_decl_(enclosing_template_decl),
25412  hashing_started_(),
25413  comparison_started_()
25414  {}
25415 }; // end class template_parameter::priv
25416 
25417 template_parameter::template_parameter(unsigned index,
25418  template_decl_sptr enclosing_template)
25419  : priv_(new priv(index, enclosing_template))
25420  {}
25421 
25422 unsigned
25423 template_parameter::get_index() const
25424 {return priv_->index_;}
25425 
25426 const template_decl_sptr
25427 template_parameter::get_enclosing_template_decl() const
25428 {return priv_->template_decl_.lock();}
25429 
25430 bool
25431 template_parameter::get_hashing_has_started() const
25432 {return priv_->hashing_started_;}
25433 
25434 void
25435 template_parameter::set_hashing_has_started(bool f) const
25436 {priv_->hashing_started_ = f;}
25437 
25438 bool
25439 template_parameter::operator==(const template_parameter& o) const
25440 {
25441  if (get_index() != o.get_index())
25442  return false;
25443 
25444  if (priv_->comparison_started_)
25445  return true;
25446 
25447  bool result = false;
25448 
25449  // Avoid inifite loops due to the fact that comparison the enclosing
25450  // template decl might lead to comparing this very same template
25451  // parameter with another one ...
25452  priv_->comparison_started_ = true;
25453 
25454  if (!!get_enclosing_template_decl() != !!o.get_enclosing_template_decl())
25455  ;
25456  else if (get_enclosing_template_decl()
25457  && (*get_enclosing_template_decl()
25458  != *o.get_enclosing_template_decl()))
25459  ;
25460  else
25461  result = true;
25462 
25463  priv_->comparison_started_ = false;
25464 
25465  return result;
25466 }
25467 
25468 /// Inequality operator.
25469 ///
25470 /// @param other the other instance to compare against.
25471 ///
25472 /// @return true iff the other instance is different from the current
25473 /// one.
25474 bool
25476 {return !operator==(other);}
25477 
25478 /// Destructor.
25480 {}
25481 
25482 /// The type of the private data of the @ref type_tparameter type.
25483 class type_tparameter::priv
25484 {
25485  friend class type_tparameter;
25486 }; // end class type_tparameter::priv
25487 
25488 /// Constructor of the @ref type_tparameter type.
25489 ///
25490 /// @param index the index the type template parameter.
25491 ///
25492 /// @param enclosing_tdecl the enclosing template declaration.
25493 ///
25494 /// @param name the name of the template parameter.
25495 ///
25496 /// @param locus the location of the declaration of this type template
25497 /// parameter.
25498 type_tparameter::type_tparameter(unsigned index,
25499  template_decl_sptr enclosing_tdecl,
25500  const string& name,
25501  const location& locus)
25502  : type_or_decl_base(enclosing_tdecl->get_environment(),
25503  ABSTRACT_DECL_BASE
25504  | ABSTRACT_TYPE_BASE
25505  | BASIC_TYPE),
25506  decl_base(enclosing_tdecl->get_environment(), name, locus),
25507  type_base(enclosing_tdecl->get_environment(), 0, 0),
25508  type_decl(enclosing_tdecl->get_environment(), name, 0, 0, locus),
25509  template_parameter(index, enclosing_tdecl),
25510  priv_(new priv)
25511 {
25512  runtime_type_instance(this);
25513 }
25514 
25515 /// Equality operator.
25516 ///
25517 /// @param other the other template type parameter to compare against.
25518 ///
25519 /// @return true iff @p other equals the current instance.
25520 bool
25522 {
25523  if (!type_decl::operator==(other))
25524  return false;
25525 
25526  try
25527  {
25528  const type_tparameter& o = dynamic_cast<const type_tparameter&>(other);
25530  }
25531  catch (...)
25532  {return false;}
25533 }
25534 
25535 /// Equality operator.
25536 ///
25537 /// @param other the other template type parameter to compare against.
25538 ///
25539 /// @return true iff @p other equals the current instance.
25540 bool
25542 {
25543  if (!type_decl::operator==(other))
25544  return false;
25545 
25546  try
25547  {
25548  const type_tparameter& o = dynamic_cast<const type_tparameter&>(other);
25550  }
25551  catch (...)
25552  {return false;}
25553 }
25554 
25555 /// Equality operator.
25556 ///
25557 /// @param other the other template type parameter to compare against.
25558 ///
25559 /// @return true iff @p other equals the current instance.
25560 bool
25562 {
25563  if (!decl_base::operator==(other))
25564  return false;
25565 
25566  try
25567  {
25568  const type_tparameter& o = dynamic_cast<const type_tparameter&>(other);
25570  }
25571  catch (...)
25572  {return false;}
25573 }
25574 
25575 /// Equality operator.
25576 ///
25577 /// @param other the other template type parameter to compare against.
25578 ///
25579 /// @return true iff @p other equals the current instance.
25580 bool
25582 {
25583  try
25584  {
25585  const type_base& o = dynamic_cast<const type_base&>(other);
25586  return *this == o;
25587  }
25588  catch(...)
25589  {return false;}
25590 }
25591 
25592 /// Equality operator.
25593 ///
25594 /// @param other the other template type parameter to compare against.
25595 ///
25596 /// @return true iff @p other equals the current instance.
25597 bool
25599 {return *this == static_cast<const type_base&>(other);}
25600 
25601 type_tparameter::~type_tparameter()
25602 {}
25603 
25604 /// The type of the private data of the @ref non_type_tparameter type.
25605 class non_type_tparameter::priv
25606 {
25607  friend class non_type_tparameter;
25608 
25609  type_base_wptr type_;
25610 
25611  priv();
25612 
25613 public:
25614 
25615  priv(type_base_sptr type)
25616  : type_(type)
25617  {}
25618 }; // end class non_type_tparameter::priv
25619 
25620 /// The constructor for the @ref non_type_tparameter type.
25621 ///
25622 /// @param index the index of the template parameter.
25623 ///
25624 /// @param enclosing_tdecl the enclosing template declaration that
25625 /// holds this parameter parameter.
25626 ///
25627 /// @param name the name of the template parameter.
25628 ///
25629 /// @param type the type of the template parameter.
25630 ///
25631 /// @param locus the location of the declaration of this template
25632 /// parameter.
25633 non_type_tparameter::non_type_tparameter(unsigned index,
25634  template_decl_sptr enclosing_tdecl,
25635  const string& name,
25636  type_base_sptr type,
25637  const location& locus)
25638  : type_or_decl_base(type->get_environment(), ABSTRACT_DECL_BASE),
25639  decl_base(type->get_environment(), name, locus, ""),
25640  template_parameter(index, enclosing_tdecl),
25641  priv_(new priv(type))
25642 {
25643  runtime_type_instance(this);
25644 }
25645 
25646 /// Getter for the type of the template parameter.
25647 ///
25648 /// @return the type of the template parameter.
25649 const type_base_sptr
25651 {return priv_->type_.lock();}
25652 
25653 /// Get the hash value of the current instance.
25654 ///
25655 /// @return the hash value.
25656 size_t
25658 {
25659  non_type_tparameter::hash hash_tparm;
25660  return hash_tparm(this);
25661 }
25662 
25663 bool
25665 {
25666  if (!decl_base::operator==(other))
25667  return false;
25668 
25669  try
25670  {
25671  const non_type_tparameter& o =
25672  dynamic_cast<const non_type_tparameter&>(other);
25673  return (template_parameter::operator==(o)
25674  && get_type() == o.get_type());
25675  }
25676  catch(...)
25677  {return false;}
25678 }
25679 
25680 bool
25682 {
25683  try
25684  {
25685  const decl_base& o = dynamic_cast<const decl_base&>(other);
25686  return *this == o;
25687  }
25688  catch(...)
25689  {return false;}
25690 }
25691 
25692 non_type_tparameter::~non_type_tparameter()
25693 {}
25694 
25695 // <template_tparameter stuff>
25696 
25697 /// Type of the private data of the @ref template_tparameter type.
25698 class template_tparameter::priv
25699 {
25700 }; //end class template_tparameter::priv
25701 
25702 /// Constructor for the @ref template_tparameter.
25703 ///
25704 /// @param index the index of the template parameter.
25705 ///
25706 /// @param enclosing_tdecl the enclosing template declaration.
25707 ///
25708 /// @param name the name of the template parameter.
25709 ///
25710 /// @param locus the location of the declaration of the template
25711 /// parameter.
25712 template_tparameter::template_tparameter(unsigned index,
25713  template_decl_sptr enclosing_tdecl,
25714  const string& name,
25715  const location& locus)
25716  : type_or_decl_base(enclosing_tdecl->get_environment(),
25717  ABSTRACT_DECL_BASE
25718  | ABSTRACT_TYPE_BASE
25719  | BASIC_TYPE),
25720  decl_base(enclosing_tdecl->get_environment(), name, locus),
25721  type_base(enclosing_tdecl->get_environment(), 0, 0),
25722  type_decl(enclosing_tdecl->get_environment(), name,
25723  0, 0, locus, name, VISIBILITY_DEFAULT),
25724  type_tparameter(index, enclosing_tdecl, name, locus),
25725  template_decl(enclosing_tdecl->get_environment(), name, locus),
25726  priv_(new priv)
25727 {
25728  runtime_type_instance(this);
25729 }
25730 
25731 /// Equality operator.
25732 ///
25733 /// @param other the other template parameter to compare against.
25734 ///
25735 /// @return true iff @p other equals the current instance.
25736 bool
25738 {
25739  try
25740  {
25741  const template_tparameter& o =
25742  dynamic_cast<const template_tparameter&>(other);
25743  return (type_tparameter::operator==(o)
25745  }
25746  catch(...)
25747  {return false;}
25748 }
25749 
25750 /// Equality operator.
25751 ///
25752 /// @param other the other template parameter to compare against.
25753 ///
25754 /// @return true iff @p other equals the current instance.
25755 bool
25757 {
25758  try
25759  {
25760  const template_tparameter& o =
25761  dynamic_cast<const template_tparameter&>(other);
25762  return (type_tparameter::operator==(o)
25764  }
25765  catch(...)
25766  {return false;}
25767 }
25768 
25769 bool
25771 {
25772  try
25773  {
25774  const template_tparameter& other =
25775  dynamic_cast<const template_tparameter&>(o);
25776  return *this == static_cast<const type_base&>(other);
25777  }
25778  catch(...)
25779  {return false;}
25780 }
25781 
25782 bool
25784 {
25785  try
25786  {
25787  const template_tparameter& other =
25788  dynamic_cast<const template_tparameter&>(o);
25789  return type_base::operator==(other);
25790  }
25791  catch(...)
25792  {return false;}
25793 }
25794 
25795 template_tparameter::~template_tparameter()
25796 {}
25797 
25798 // </template_tparameter stuff>
25799 
25800 // <type_composition stuff>
25801 
25802 /// The type of the private data of the @ref type_composition type.
25803 class type_composition::priv
25804 {
25805  friend class type_composition;
25806 
25807  type_base_wptr type_;
25808 
25809  // Forbid this.
25810  priv();
25811 
25812 public:
25813 
25814  priv(type_base_wptr type)
25815  : type_(type)
25816  {}
25817 }; //end class type_composition::priv
25818 
25819 /// Constructor for the @ref type_composition type.
25820 ///
25821 /// @param index the index of the template type composition.
25822 ///
25823 /// @param tdecl the enclosing template parameter that owns the
25824 /// composition.
25825 ///
25826 /// @param t the resulting type.
25827 type_composition::type_composition(unsigned index,
25828  template_decl_sptr tdecl,
25829  type_base_sptr t)
25830  : type_or_decl_base(tdecl->get_environment(),
25831  ABSTRACT_DECL_BASE),
25832  decl_base(tdecl->get_environment(), "", location()),
25833  template_parameter(index, tdecl),
25834  priv_(new priv(t))
25835 {
25836  runtime_type_instance(this);
25837 }
25838 
25839 /// Getter for the resulting composed type.
25840 ///
25841 /// @return the composed type.
25842 const type_base_sptr
25844 {return priv_->type_.lock();}
25845 
25846 /// Setter for the resulting composed type.
25847 ///
25848 /// @param t the composed type.
25849 void
25851 {priv_->type_ = t;}
25852 
25853 /// Get the hash value for the current instance.
25854 ///
25855 /// @return the hash value.
25856 size_t
25858 {
25859  type_composition::hash hash_type_composition;
25860  return hash_type_composition(this);
25861 }
25862 
25863 type_composition::~type_composition()
25864 {}
25865 
25866 // </type_composition stuff>
25867 
25868 //</template_parameter stuff>
25869 
25870 // <function_template>
25871 
25872 class function_tdecl::priv
25873 {
25874  friend class function_tdecl;
25875 
25876  function_decl_sptr pattern_;
25877  binding binding_;
25878 
25879  priv();
25880 
25881 public:
25882 
25883  priv(function_decl_sptr pattern, binding bind)
25884  : pattern_(pattern), binding_(bind)
25885  {}
25886 
25887  priv(binding bind)
25888  : binding_(bind)
25889  {}
25890 }; // end class function_tdecl::priv
25891 
25892 /// Constructor for a function template declaration.
25893 ///
25894 /// @param env the environment we are operating from.
25895 ///
25896 /// @param locus the location of the declaration.
25897 ///
25898 /// @param vis the visibility of the declaration. This is the
25899 /// visibility the functions instantiated from this template are going
25900 /// to have.
25901 ///
25902 /// @param bind the binding of the declaration. This is the binding
25903 /// the functions instantiated from this template are going to have.
25904 function_tdecl::function_tdecl(const environment& env,
25905  const location& locus,
25906  visibility vis,
25907  binding bind)
25908  : type_or_decl_base(env,
25909  ABSTRACT_DECL_BASE
25910  | TEMPLATE_DECL
25911  | ABSTRACT_SCOPE_DECL),
25912  decl_base(env, "", locus, "", vis),
25913  template_decl(env, "", locus, vis),
25914  scope_decl(env, "", locus),
25915  priv_(new priv(bind))
25916 {
25917  runtime_type_instance(this);
25918 }
25919 
25920 /// Constructor for a function template declaration.
25921 ///
25922 /// @param pattern the pattern of the template.
25923 ///
25924 /// @param locus the location of the declaration.
25925 ///
25926 /// @param vis the visibility of the declaration. This is the
25927 /// visibility the functions instantiated from this template are going
25928 /// to have.
25929 ///
25930 /// @param bind the binding of the declaration. This is the binding
25931 /// the functions instantiated from this template are going to have.
25932 function_tdecl::function_tdecl(function_decl_sptr pattern,
25933  const location& locus,
25934  visibility vis,
25935  binding bind)
25936  : type_or_decl_base(pattern->get_environment(),
25937  ABSTRACT_DECL_BASE
25938  | TEMPLATE_DECL
25939  | ABSTRACT_SCOPE_DECL),
25940  decl_base(pattern->get_environment(), pattern->get_name(), locus,
25941  pattern->get_name(), vis),
25942  template_decl(pattern->get_environment(), pattern->get_name(), locus, vis),
25943  scope_decl(pattern->get_environment(), pattern->get_name(), locus),
25944  priv_(new priv(pattern, bind))
25945 {
25946  runtime_type_instance(this);
25947 }
25948 
25949 /// Set a new pattern to the function template.
25950 ///
25951 /// @param p the new pattern.
25952 void
25954 {
25955  priv_->pattern_ = p;
25956  add_decl_to_scope(p, this);
25957  set_name(p->get_name());
25958 }
25959 
25960 /// Get the pattern of the function template.
25961 ///
25962 /// @return the pattern.
25965 {return priv_->pattern_;}
25966 
25967 /// Get the binding of the function template.
25968 ///
25969 /// @return the binding
25972 {return priv_->binding_;}
25973 
25974 /// Comparison operator for the @ref function_tdecl type.
25975 ///
25976 /// @param other the other instance of @ref function_tdecl to compare against.
25977 ///
25978 /// @return true iff the two instance are equal.
25979 bool
25981 {
25982  const function_tdecl* o = dynamic_cast<const function_tdecl*>(&other);
25983  if (o)
25984  return *this == *o;
25985  return false;
25986 }
25987 
25988 /// Comparison operator for the @ref function_tdecl type.
25989 ///
25990 /// @param other the other instance of @ref function_tdecl to compare against.
25991 ///
25992 /// @return true iff the two instance are equal.
25993 bool
25995 {
25996  const function_tdecl* o = dynamic_cast<const function_tdecl*>(&other);
25997  if (o)
25998  return *this == *o;
25999  return false;
26000 }
26001 
26002 /// Comparison operator for the @ref function_tdecl type.
26003 ///
26004 /// @param o the other instance of @ref function_tdecl to compare against.
26005 ///
26006 /// @return true iff the two instance are equal.
26007 bool
26009 {
26010  if (!(get_binding() == o.get_binding()
26011  && template_decl::operator==(o)
26012  && scope_decl::operator==(o)
26013  && !!get_pattern() == !!o.get_pattern()))
26014  return false;
26015 
26016  if (get_pattern())
26017  return (*get_pattern() == *o.get_pattern());
26018 
26019  return true;
26020 }
26021 
26022 /// This implements the ir_traversable_base::traverse pure virtual
26023 /// function.
26024 ///
26025 /// @param v the visitor used on the current instance and on the
26026 /// function pattern of the template.
26027 ///
26028 /// @return true if the entire IR node tree got traversed, false
26029 /// otherwise.
26030 bool
26032 {
26033  if (visiting())
26034  return true;
26035 
26036  if (!v.visit_begin(this))
26037  {
26038  visiting(true);
26039  if (get_pattern())
26040  get_pattern()->traverse(v);
26041  visiting(false);
26042  }
26043  return v.visit_end(this);
26044 }
26045 
26046 function_tdecl::~function_tdecl()
26047 {}
26048 
26049 // </function_template>
26050 
26051 // <class template>
26052 
26053 /// Type of the private data of the the @ref class_tdecl type.
26054 class class_tdecl::priv
26055 {
26056  friend class class_tdecl;
26057  class_decl_sptr pattern_;
26058 
26059 public:
26060 
26061  priv()
26062  {}
26063 
26064  priv(class_decl_sptr pattern)
26065  : pattern_(pattern)
26066  {}
26067 }; // end class class_tdecl::priv
26068 
26069 /// Constructor for the @ref class_tdecl type.
26070 ///
26071 /// @param env the environment we are operating from.
26072 ///
26073 /// @param locus the location of the declaration of the class_tdecl
26074 /// type.
26075 ///
26076 /// @param vis the visibility of the instance of class instantiated
26077 /// from this template.
26078 class_tdecl::class_tdecl(const environment& env,
26079  const location& locus,
26080  visibility vis)
26081  : type_or_decl_base(env,
26082  ABSTRACT_DECL_BASE
26083  | TEMPLATE_DECL
26084  | ABSTRACT_SCOPE_DECL),
26085  decl_base(env, "", locus, "", vis),
26086  template_decl(env, "", locus, vis),
26087  scope_decl(env, "", locus),
26088  priv_(new priv)
26089 {
26090  runtime_type_instance(this);
26091 }
26092 
26093 /// Constructor for the @ref class_tdecl type.
26094 ///
26095 /// @param pattern The details of the class template. This must NOT be a
26096 /// null pointer. If you really this to be null, please use the
26097 /// constructor above instead.
26098 ///
26099 /// @param locus the source location of the declaration of the type.
26100 ///
26101 /// @param vis the visibility of the instances of class instantiated
26102 /// from this template.
26103 class_tdecl::class_tdecl(class_decl_sptr pattern,
26104  const location& locus,
26105  visibility vis)
26106  : type_or_decl_base(pattern->get_environment(),
26107  ABSTRACT_DECL_BASE
26108  | TEMPLATE_DECL
26109  | ABSTRACT_SCOPE_DECL),
26110  decl_base(pattern->get_environment(), pattern->get_name(),
26111  locus, pattern->get_name(), vis),
26112  template_decl(pattern->get_environment(), pattern->get_name(), locus, vis),
26113  scope_decl(pattern->get_environment(), pattern->get_name(), locus),
26114  priv_(new priv(pattern))
26115 {
26116  runtime_type_instance(this);
26117 }
26118 
26119 /// Setter of the pattern of the template.
26120 ///
26121 /// @param p the new template.
26122 void
26124 {
26125  priv_->pattern_ = p;
26126  add_decl_to_scope(p, this);
26127  set_name(p->get_name());
26128 }
26129 
26130 /// Getter of the pattern of the template.
26131 ///
26132 /// @return p the new template.
26135 {return priv_->pattern_;}
26136 
26137 bool
26139 {
26140  try
26141  {
26142  const class_tdecl& o = dynamic_cast<const class_tdecl&>(other);
26143 
26144  if (!(template_decl::operator==(o)
26145  && scope_decl::operator==(o)
26146  && !!get_pattern() == !!o.get_pattern()))
26147  return false;
26148 
26149  if (!get_pattern() || !o.get_pattern())
26150  return true;
26151 
26152  return get_pattern()->decl_base::operator==(*o.get_pattern());
26153  }
26154  catch(...) {}
26155  return false;
26156 }
26157 
26158 bool
26160 {
26161  try
26162  {
26163  const class_tdecl& o = dynamic_cast<const class_tdecl&>(other);
26164  return *this == static_cast<const decl_base&>(o);
26165  }
26166  catch(...)
26167  {return false;}
26168 }
26169 
26170 bool
26171 class_tdecl::operator==(const class_tdecl& o) const
26172 {return *this == static_cast<const decl_base&>(o);}
26173 
26174 /// This implements the ir_traversable_base::traverse pure virtual
26175 /// function.
26176 ///
26177 /// @param v the visitor used on the current instance and on the class
26178 /// pattern of the template.
26179 ///
26180 /// @return true if the entire IR node tree got traversed, false
26181 /// otherwise.
26182 bool
26184 {
26185  if (visiting())
26186  return true;
26187 
26188  if (v.visit_begin(this))
26189  {
26190  visiting(true);
26191  if (class_decl_sptr pattern = get_pattern())
26192  pattern->traverse(v);
26193  visiting(false);
26194  }
26195  return v.visit_end(this);
26196 }
26197 
26198 class_tdecl::~class_tdecl()
26199 {}
26200 
26201 /// This visitor checks if a given type as non-canonicalized sub
26202 /// types.
26203 class non_canonicalized_subtype_detector : public ir::ir_node_visitor
26204 {
26205  type_base* type_;
26206  type_base* has_non_canonical_type_;
26207 
26208 private:
26209  non_canonicalized_subtype_detector();
26210 
26211 public:
26212  non_canonicalized_subtype_detector(type_base* type)
26213  : type_(type),
26214  has_non_canonical_type_()
26215  {}
26216 
26217  /// Return true if the visitor detected that there is a
26218  /// non-canonicalized sub-type.
26219  ///
26220  /// @return true if the visitor detected that there is a
26221  /// non-canonicalized sub-type.
26222  type_base*
26223  has_non_canonical_type() const
26224  {return has_non_canonical_type_;}
26225 
26226  /// The intent of this visitor handler is to avoid looking into
26227  /// sub-types of member functions of the type we are traversing.
26228  bool
26229  visit_begin(function_decl* f)
26230  {
26231  // Do not look at sub-types of non-virtual member functions.
26232  if (is_member_function(f)
26234  return false;
26235  return true;
26236  }
26237 
26238  /// When visiting a sub-type, if it's *NOT* been canonicalized, set
26239  /// the 'has_non_canonical_type' flag. And in any case, when
26240  /// visiting a sub-type, do not visit its children nodes. So this
26241  /// function only goes to the level below the level of the top-most
26242  /// type.
26243  ///
26244  /// @return true if we are at the same level as the top-most type,
26245  /// otherwise return false.
26246  bool
26247  visit_begin(type_base* t)
26248  {
26249  if (t != type_)
26250  {
26251  if (!t->get_canonical_type())
26252  // We are looking a sub-type of 'type_' which has no
26253  // canonical type. So tada! we found one! Get out right
26254  // now with the trophy.
26255  has_non_canonical_type_ = t;
26256 
26257  return false;
26258  }
26259  return true;
26260  }
26261 
26262  /// When we are done visiting a sub-type, if it's been flagged as
26263  /// been non-canonicalized, then stop the traversing.
26264  ///
26265  /// Otherwise, keep going.
26266  ///
26267  /// @return false iff the sub-type that has been visited is
26268  /// non-canonicalized.
26269  bool
26270  visit_end(type_base* )
26271  {
26272  if (has_non_canonical_type_)
26273  return false;
26274  return true;
26275  }
26276 }; //end class non_canonicalized_subtype_detector
26277 
26278 /// Test if a type has sub-types that are non-canonicalized.
26279 ///
26280 /// @param t the type which sub-types to consider.
26281 ///
26282 /// @return true if a type has sub-types that are non-canonicalized.
26283 type_base*
26285 {
26286  if (!t)
26287  return 0;
26288 
26289  non_canonicalized_subtype_detector v(t.get());
26290  t->traverse(v);
26291  return v.has_non_canonical_type();
26292 }
26293 
26294 /// Tests if the change of a given type effectively comes from just
26295 /// its sub-types. That is, if the type has changed but its type name
26296 /// hasn't changed, then the change of the type mostly likely is a
26297 /// sub-type change.
26298 ///
26299 /// @param t_v1 the first version of the type.
26300 ///
26301 /// @param t_v2 the second version of the type.
26302 ///
26303 /// @return true iff the type changed and the change is about its
26304 /// sub-types.
26305 bool
26306 type_has_sub_type_changes(const type_base_sptr t_v1,
26307  const type_base_sptr t_v2)
26308 {
26309  type_base_sptr t1 = strip_typedef(t_v1);
26310  type_base_sptr t2 = strip_typedef(t_v2);
26311 
26312  string repr1 = get_pretty_representation(t1, /*internal=*/false),
26313  repr2 = get_pretty_representation(t2, /*internal=*/false);
26314  return (t1 != t2 && repr1 == repr2);
26315 }
26316 
26317 /// Make sure that the life time of a given (smart pointer to a) type
26318 /// is the same as the life time of the libabigail library.
26319 ///
26320 /// @param t the type to consider.
26321 void
26322 keep_type_alive(type_base_sptr t)
26323 {
26324  const environment& env = t->get_environment();
26325  env.priv_->extra_live_types_.push_back(t);
26326 }
26327 
26328 /// Hash an ABI artifact that is either a type or a decl.
26329 ///
26330 /// This function intends to provides the fastest possible hashing for
26331 /// types and decls, while being completely correct.
26332 ///
26333 /// Note that if the artifact is a type and if it has a canonical
26334 /// type, the hash value is going to be the pointer value of the
26335 /// canonical type. Otherwise, this function computes a hash value
26336 /// for the type by recursively walking the type members. This last
26337 /// code path is possibly *very* slow and should only be used when
26338 /// only handful of types are going to be hashed.
26339 ///
26340 /// If the artifact is a decl, then a combination of the hash of its
26341 /// type and the hash of the other properties of the decl is computed.
26342 ///
26343 /// @param tod the type or decl to hash.
26344 ///
26345 /// @return the resulting hash value.
26346 size_t
26348 {
26349  size_t result = 0;
26350 
26351  if (tod == 0)
26352  ;
26353  else if (const type_base* t = is_type(tod))
26354  result = hash_type(t);
26355  else if (const decl_base* d = is_decl(tod))
26356  {
26357  if (var_decl* v = is_var_decl(d))
26358  {
26359  ABG_ASSERT(v->get_type());
26360  size_t h = hash_type_or_decl(v->get_type());
26361  string repr = v->get_pretty_representation(/*internal=*/true);
26362  std::hash<string> hash_string;
26363  h = hashing::combine_hashes(h, hash_string(repr));
26364  result = h;
26365  }
26366  else if (function_decl* f = is_function_decl(d))
26367  {
26368  ABG_ASSERT(f->get_type());
26369  size_t h = hash_type_or_decl(f->get_type());
26370  string repr = f->get_pretty_representation(/*internal=*/true);
26371  std::hash<string> hash_string;
26372  h = hashing::combine_hashes(h, hash_string(repr));
26373  result = h;
26374  }
26376  {
26377  type_base_sptr parm_type = p->get_type();
26378  ABG_ASSERT(parm_type);
26379  std::hash<bool> hash_bool;
26380  std::hash<unsigned> hash_unsigned;
26381  size_t h = hash_type_or_decl(parm_type);
26382  h = hashing::combine_hashes(h, hash_unsigned(p->get_index()));
26383  h = hashing::combine_hashes(h, hash_bool(p->get_variadic_marker()));
26384  result = h;
26385  }
26386  else if (class_decl::base_spec *bs = is_class_base_spec(d))
26387  {
26388  member_base::hash hash_member;
26389  std::hash<size_t> hash_size;
26390  std::hash<bool> hash_bool;
26391  type_base_sptr type = bs->get_base_class();
26392  size_t h = hash_type_or_decl(type);
26393  h = hashing::combine_hashes(h, hash_member(*bs));
26394  h = hashing::combine_hashes(h, hash_size(bs->get_offset_in_bits()));
26395  h = hashing::combine_hashes(h, hash_bool(bs->get_is_virtual()));
26396  result = h;
26397  }
26398  else
26399  // This is a *really* *SLOW* path. If it shows up in a
26400  // performance profile, I bet it'd be a good idea to try to
26401  // avoid it altogether.
26402  result = d->get_hash();
26403  }
26404  else
26405  // We should never get here.
26406  abort();
26407  return result;
26408 }
26409 
26410 /// Hash an ABI artifact that is either a type.
26411 ///
26412 /// This function intends to provides the fastest possible hashing for
26413 /// types while being completely correct.
26414 ///
26415 /// Note that if the type artifact has a canonical type, the hash
26416 /// value is going to be the pointer value of the canonical type.
26417 /// Otherwise, this function computes a hash value for the type by
26418 /// recursively walking the type members. This last code path is
26419 /// possibly *very* slow and should only be used when only handful of
26420 /// types are going to be hashed.
26421 ///
26422 /// @param t the type or decl to hash.
26423 ///
26424 /// @return the resulting hash value.
26425 size_t
26427 {return hash_as_canonical_type_or_constant(t);}
26428 
26429 /// Hash an ABI artifact that is either a type of a decl.
26430 ///
26431 /// @param tod the ABI artifact to hash.
26432 ///
26433 /// @return the hash value of the ABI artifact.
26434 size_t
26436 {return hash_type_or_decl(tod.get());}
26437 
26438 /// Test if a given type is allowed to be non canonicalized
26439 ///
26440 /// This is a subroutine of hash_as_canonical_type_or_constant.
26441 ///
26442 /// For now, the only types allowed to be non canonicalized in the
26443 /// system are decl-only class/union, the void type and variadic
26444 /// parameter types.
26445 ///
26446 /// @return true iff @p t is a one of the only types allowed to be
26447 /// non-canonicalized in the system.
26448 bool
26450 {
26451  if (!t)
26452  return true;
26453 
26454  const environment& env = t->get_environment();
26456  || is_void_pointer_type(t)
26457  || env.is_void_type(t)
26458  || env.is_variadic_parameter_type(t));
26459 }
26460 
26461 /// For a given type, return its exemplar type.
26462 ///
26463 /// For a given type, its exemplar type is either its canonical type
26464 /// or the canonical type of the definition type of a given
26465 /// declaration-only type. If the neither of those two types exist,
26466 /// then the exemplar type is the given type itself.
26467 ///
26468 /// @param type the input to consider.
26469 ///
26470 /// @return the exemplar type.
26471 type_base*
26473 {
26474  if (decl_base * decl = is_decl(type))
26475  {
26476  // Make sure we get the real definition of a decl-only type.
26477  decl = look_through_decl_only(decl);
26478  type = is_type(decl);
26479  ABG_ASSERT(type);
26480  }
26481  type_base *exemplar = type->get_naked_canonical_type();
26482  if (!exemplar)
26483  {
26484  // The type has no canonical type. Let's be sure that it's one
26485  // of those rare types that are allowed to be non canonicalized
26486  // in the system.
26487  exemplar = const_cast<type_base*>(type);
26489  }
26490  return exemplar;
26491 }
26492 
26493 /// Test if a given type is allowed to be non canonicalized
26494 ///
26495 /// This is a subroutine of hash_as_canonical_type_or_constant.
26496 ///
26497 /// For now, the only types allowed to be non canonicalized in the
26498 /// system are decl-only class/union and the void type.
26499 ///
26500 /// @return true iff @p t is a one of the only types allowed to be
26501 /// non-canonicalized in the system.
26502 bool
26503 is_non_canonicalized_type(const type_base_sptr& t)
26504 {return is_non_canonicalized_type(t.get());}
26505 
26506 /// Hash a type by either returning the pointer value of its canonical
26507 /// type or by returning a constant if the type doesn't have a
26508 /// canonical type.
26509 ///
26510 /// This is a subroutine of hash_type.
26511 ///
26512 /// @param t the type to consider.
26513 ///
26514 /// @return the hash value.
26515 static size_t
26516 hash_as_canonical_type_or_constant(const type_base *t)
26517 {
26518  type_base *canonical_type = 0;
26519 
26520  if (t)
26521  canonical_type = t->get_naked_canonical_type();
26522 
26523  if (!canonical_type)
26524  {
26525  // If the type doesn't have a canonical type, maybe it's because
26526  // it's a declaration-only type? If that's the case, let's try
26527  // to get the canonical type of the definition of this
26528  // declaration.
26529  decl_base *decl = is_decl(t);
26530  if (decl
26531  && decl->get_is_declaration_only()
26533  {
26534  type_base *definition =
26536  ABG_ASSERT(definition);
26537  canonical_type = definition->get_naked_canonical_type();
26538  }
26539  }
26540 
26541  if (canonical_type)
26542  return reinterpret_cast<size_t>(canonical_type);
26543 
26544  // If we reached this point, it means we are seeing a
26545  // non-canonicalized type. It must be a decl-only class or a void
26546  // type, otherwise it means that for some weird reason, the type
26547  // hasn't been canonicalized. It should be!
26549 
26550  return 0xDEADBABE;
26551 }
26552 
26553 /// Test if the pretty representation of a given @ref function_decl is
26554 /// lexicographically less then the pretty representation of another
26555 /// @ref function_decl.
26556 ///
26557 /// @param f the first @ref function_decl to consider for comparison.
26558 ///
26559 /// @param s the second @ref function_decl to consider for comparison.
26560 ///
26561 /// @return true iff the pretty representation of @p f is
26562 /// lexicographically less than the pretty representation of @p s.
26563 bool
26565 {
26568 
26569  if (fr != sr)
26570  return fr < sr;
26571 
26572  fr = f.get_pretty_representation(/*internal=*/true),
26573  sr = s.get_pretty_representation(/*internal=*/true);
26574 
26575  if (fr != sr)
26576  return fr < sr;
26577 
26578  if (f.get_symbol())
26579  fr = f.get_symbol()->get_id_string();
26580  else if (!f.get_linkage_name().empty())
26581  fr = f.get_linkage_name();
26582 
26583  if (s.get_symbol())
26584  sr = s.get_symbol()->get_id_string();
26585  else if (!s.get_linkage_name().empty())
26586  sr = s.get_linkage_name();
26587 
26588  return fr < sr;
26589 }
26590 
26591 /// Test if two types have similar structures, even though they are
26592 /// (or can be) different.
26593 ///
26594 /// const and volatile qualifiers are completely ignored.
26595 ///
26596 /// typedef are resolved to their definitions; their names are ignored.
26597 ///
26598 /// Two indirect types (pointers or references) have similar structure
26599 /// if their underlying types are of the same kind and have the same
26600 /// name. In the indirect types case, the size of the underlying type
26601 /// does not matter.
26602 ///
26603 /// Two direct types (i.e, non indirect) have a similar structure if
26604 /// they have the same kind, name and size. Two class types have
26605 /// similar structure if they have the same name, size, and if the
26606 /// types of their data members have similar types.
26607 ///
26608 /// @param first the first type to consider.
26609 ///
26610 /// @param second the second type to consider.
26611 ///
26612 /// @param indirect_type whether to do an indirect comparison
26613 ///
26614 /// @return true iff @p first and @p second have similar structures.
26615 bool
26616 types_have_similar_structure(const type_base_sptr& first,
26617  const type_base_sptr& second,
26618  bool indirect_type)
26619 {return types_have_similar_structure(first.get(), second.get(), indirect_type);}
26620 
26621 /// Test if two types have similar structures, even though they are
26622 /// (or can be) different.
26623 ///
26624 /// const and volatile qualifiers are completely ignored.
26625 ///
26626 /// typedef are resolved to their definitions; their names are ignored.
26627 ///
26628 /// Two indirect types (pointers, references or arrays) have similar
26629 /// structure if their underlying types are of the same kind and have
26630 /// the same name. In the indirect types case, the size of the
26631 /// underlying type does not matter.
26632 ///
26633 /// Two direct types (i.e, non indirect) have a similar structure if
26634 /// they have the same kind, name and size. Two class types have
26635 /// similar structure if they have the same name, size, and if the
26636 /// types of their data members have similar types.
26637 ///
26638 /// @param first the first type to consider.
26639 ///
26640 /// @param second the second type to consider.
26641 ///
26642 /// @param indirect_type if true, then consider @p first and @p
26643 /// second as being underlying types of indirect types. Meaning that
26644 /// their size does not matter.
26645 ///
26646 /// @return true iff @p first and @p second have similar structures.
26647 bool
26649  const type_base* second,
26650  bool indirect_type)
26651 {
26652  if (!!first != !!second)
26653  return false;
26654 
26655  if (!first)
26656  return false;
26657 
26658  // Treat typedefs purely as type aliases and ignore CV-qualifiers.
26659  first = peel_qualified_or_typedef_type(first);
26660  second = peel_qualified_or_typedef_type(second);
26661 
26662  // Eliminate all but N of the N^2 comparison cases. This also guarantees the
26663  // various ty2 below cannot be null.
26664  if (typeid(*first) != typeid(*second))
26665  return false;
26666 
26667  // Peel off matching pointers.
26668  if (const pointer_type_def* ty1 = is_pointer_type(first))
26669  {
26670  const pointer_type_def* ty2 = is_pointer_type(second);
26671  return types_have_similar_structure(ty1->get_pointed_to_type(),
26672  ty2->get_pointed_to_type(),
26673  /*indirect_type=*/true);
26674  }
26675 
26676  // Peel off matching references.
26677  if (const reference_type_def* ty1 = is_reference_type(first))
26678  {
26679  const reference_type_def* ty2 = is_reference_type(second);
26680  if (ty1->is_lvalue() != ty2->is_lvalue())
26681  return false;
26682  return types_have_similar_structure(ty1->get_pointed_to_type(),
26683  ty2->get_pointed_to_type(),
26684  /*indirect_type=*/true);
26685  }
26686 
26687  if (const type_decl* ty1 = is_type_decl(first))
26688  {
26689  const type_decl* ty2 = is_type_decl(second);
26690  if (!indirect_type)
26691  if (ty1->get_size_in_bits() != ty2->get_size_in_bits())
26692  return false;
26693 
26694  return ty1->get_name() == ty2->get_name();
26695  }
26696 
26697  if (const enum_type_decl* ty1 = is_enum_type(first))
26698  {
26699  const enum_type_decl* ty2 = is_enum_type(second);
26700  if (!indirect_type)
26701  if (ty1->get_size_in_bits() != ty2->get_size_in_bits())
26702  return false;
26703 
26704  return (get_name(ty1->get_underlying_type())
26705  == get_name(ty2->get_underlying_type()));
26706  }
26707 
26708  if (const class_decl* ty1 = is_class_type(first))
26709  {
26710  const class_decl* ty2 = is_class_type(second);
26711  if (!ty1->get_is_anonymous() && !ty2->get_is_anonymous()
26712  && ty1->get_name() != ty2->get_name())
26713  return false;
26714 
26715  if (!indirect_type)
26716  {
26717  if ((ty1->get_size_in_bits() != ty2->get_size_in_bits())
26718  || (ty1->get_non_static_data_members().size()
26719  != ty2->get_non_static_data_members().size()))
26720  return false;
26721 
26722  for (class_or_union::data_members::const_iterator
26723  i = ty1->get_non_static_data_members().begin(),
26724  j = ty2->get_non_static_data_members().begin();
26725  (i != ty1->get_non_static_data_members().end()
26726  && j != ty2->get_non_static_data_members().end());
26727  ++i, ++j)
26728  {
26729  var_decl_sptr dm1 = *i;
26730  var_decl_sptr dm2 = *j;
26731  if (!types_have_similar_structure(dm1->get_type().get(),
26732  dm2->get_type().get(),
26733  indirect_type))
26734  return false;
26735  }
26736  }
26737 
26738  return true;
26739  }
26740 
26741  if (const union_decl* ty1 = is_union_type(first))
26742  {
26743  const union_decl* ty2 = is_union_type(second);
26744  if (!ty1->get_is_anonymous() && !ty2->get_is_anonymous()
26745  && ty1->get_name() != ty2->get_name())
26746  return false;
26747 
26748  if (!indirect_type)
26749  return ty1->get_size_in_bits() == ty2->get_size_in_bits();
26750 
26751  return true;
26752  }
26753 
26754  if (const array_type_def* ty1 = is_array_type(first))
26755  {
26756  const array_type_def* ty2 = is_array_type(second);
26757  // TODO: Handle int[5][2] vs int[2][5] better.
26758  if (!indirect_type)
26759  {
26760  if (ty1->get_size_in_bits() != ty2->get_size_in_bits()
26761  || ty1->get_dimension_count() != ty2->get_dimension_count())
26762  return false;
26763  }
26764 
26765  if (!types_have_similar_structure(ty1->get_element_type(),
26766  ty2->get_element_type(),
26767  /*indirect_type=*/true))
26768  return false;
26769 
26770  return true;
26771  }
26772 
26773  if (const array_type_def::subrange_type *ty1 = is_subrange_type(first))
26774  {
26775  const array_type_def::subrange_type *ty2 = is_subrange_type(second);
26776  if (ty1->get_upper_bound() != ty2->get_upper_bound()
26777  || ty1->get_lower_bound() != ty2->get_lower_bound()
26778  || ty1->get_language() != ty2->get_language()
26779  || !types_have_similar_structure(ty1->get_underlying_type(),
26780  ty2->get_underlying_type(),
26781  indirect_type))
26782  return false;
26783 
26784  return true;
26785  }
26786 
26787  if (const function_type* ty1 = is_function_type(first))
26788  {
26789  const function_type* ty2 = is_function_type(second);
26790  if (!types_have_similar_structure(ty1->get_return_type(),
26791  ty2->get_return_type(),
26792  indirect_type))
26793  return false;
26794 
26795  if (ty1->get_parameters().size() != ty2->get_parameters().size())
26796  return false;
26797 
26798  for (function_type::parameters::const_iterator
26799  i = ty1->get_parameters().begin(),
26800  j = ty2->get_parameters().begin();
26801  (i != ty1->get_parameters().end()
26802  && j != ty2->get_parameters().end());
26803  ++i, ++j)
26804  if (!types_have_similar_structure((*i)->get_type(),
26805  (*j)->get_type(),
26806  indirect_type))
26807  return false;
26808 
26809  return true;
26810  }
26811 
26812  // All kinds of type should have been handled at this point.
26814 
26815  return false;
26816 }
26817 
26818 /// Look for a data member of a given class, struct or union type and
26819 /// return it.
26820 ///
26821 /// The data member is designated by its name.
26822 ///
26823 /// @param type the class, struct or union type to consider.
26824 ///
26825 /// @param dm_name the name of the data member to lookup.
26826 ///
26827 /// @return the data member iff it was found in @type or NULL if no
26828 /// data member with that name was found.
26829 const var_decl*
26831  const char* dm_name)
26832 
26833 {
26835  if (!cou)
26836  return 0;
26837 
26838  return cou->find_data_member(dm_name).get();
26839 }
26840 
26841 /// Get the function parameter designated by its index.
26842 ///
26843 /// Note that the first function parameter has index 0.
26844 ///
26845 /// @param fun the function to consider.
26846 ///
26847 /// @param parm_index the index of the function parameter to get.
26848 ///
26849 /// @return the function parameter designated by its index, of NULL if
26850 /// no function parameter with that index was found.
26853  unsigned parm_index)
26854 {
26855  function_decl* fn = is_function_decl(fun);
26856  if (!fn)
26857  return 0;
26858 
26859  const function_decl::parameters &parms = fn->get_type()->get_parameters();
26860  if (parms.size() <= parm_index)
26861  return 0;
26862 
26863  return parms[parm_index].get();
26864 }
26865 
26866 /// Build the internal name of the underlying type of an enum.
26867 ///
26868 /// @param base_name the (unqualified) name of the enum the underlying
26869 /// type is destined to.
26870 ///
26871 /// @param is_anonymous true if the underlying type of the enum is to
26872 /// be anonymous.
26873 string
26875  bool is_anonymous,
26876  uint64_t size)
26877 {
26878  std::ostringstream o;
26879 
26880  if (is_anonymous)
26881  o << "unnamed-enum";
26882  else
26883  o << "enum-" << base_name;
26884 
26885  o << "-underlying-type-" << size;
26886 
26887  return o.str();
26888 }
26889 
26890 /// Find the first data member of a class or union which name matches
26891 /// a regular expression.
26892 ///
26893 /// @param t the class or union to consider.
26894 ///
26895 /// @param r the regular expression to consider.
26896 ///
26897 /// @return the data member matched by @p r or nil if none was found.
26900  const regex::regex_t_sptr& r)
26901 {
26902  for (auto data_member : t.get_data_members())
26903  {
26904  if (regex::match(r, data_member->get_name()))
26905  return data_member;
26906  }
26907 
26908  return var_decl_sptr();
26909 }
26910 
26911 /// Find the last data member of a class or union which name matches
26912 /// a regular expression.
26913 ///
26914 /// @param t the class or union to consider.
26915 ///
26916 /// @param r the regular expression to consider.
26917 ///
26918 /// @return the data member matched by @p r or nil if none was found.
26921  const regex::regex_t_sptr& regex)
26922 {
26923  auto d = t.get_data_members().rbegin();
26924  auto e = t.get_data_members().rend();
26925  for (; d != e; ++d)
26926  {
26927  if (regex::match(regex, (*d)->get_name()))
26928  return *d;
26929  }
26930 
26931  return var_decl_sptr();
26932 }
26933 
26934 bool
26936 {return true;}
26937 
26938 // <ir_node_visitor stuff>
26939 
26940 /// The private data structure of the ir_node_visitor type.
26941 struct ir_node_visitor::priv
26942 {
26943  pointer_set visited_ir_nodes;
26945 
26946  priv()
26948  {}
26949 }; // end struct ir_node_visitory::priv
26950 
26951 /// Default Constructor of the ir_node_visitor type.
26953  : priv_(new priv)
26954 {}
26955 
26956 ir_node_visitor::~ir_node_visitor() = default;
26957 
26958 /// Set if the walker using this visitor is allowed to re-visit a type
26959 /// node that was previously visited or not.
26960 ///
26961 /// @param f if true, then the walker using this visitor is allowed to
26962 /// re-visit a type node that was previously visited.
26963 void
26965 {priv_->allow_visiting_already_visited_type_node = f;}
26966 
26967 /// Get if the walker using this visitor is allowed to re-visit a type
26968 /// node that was previously visited or not.
26969 ///
26970 /// @return true iff the walker using this visitor is allowed to
26971 /// re-visit a type node that was previously visited.
26972 bool
26974 {return priv_->allow_visiting_already_visited_type_node;}
26975 
26976 /// Mark a given type node as having been visited.
26977 ///
26978 /// Note that for this function to work, the type node must have been
26979 /// canonicalized. Otherwise the process is aborted.
26980 ///
26981 /// @param p the type to mark as having been visited.
26982 void
26984 {
26986  return;
26987 
26988  if (p == 0 || type_node_has_been_visited(p))
26989  return;
26990 
26991  type_base* canonical_type = p->get_naked_canonical_type();
26992  ABG_ASSERT(canonical_type);
26993 
26994  size_t canonical_ptr_value = reinterpret_cast<size_t>(canonical_type);
26995  priv_->visited_ir_nodes.insert(canonical_ptr_value);
26996 }
26997 
26998 /// Un-mark all visited type nodes.
26999 ///
27000 /// That is, no type node is going to be considered as having been
27001 /// visited anymore.
27002 ///
27003 /// In other words, after invoking this funciton,
27004 /// ir_node_visitor::type_node_has_been_visited() is going to return
27005 /// false on all type nodes.
27006 void
27008 {priv_->visited_ir_nodes.clear();}
27009 
27010 /// Test if a given type node has been marked as visited.
27011 ///
27012 /// @param p the type node to consider.
27013 ///
27014 /// @return true iff the type node @p p has been marked as visited by
27015 /// the function ir_node_visitor::mark_type_node_as_visited.
27016 bool
27018 {
27020  return false;
27021 
27022  if (p == 0)
27023  return false;
27024 
27025  type_base *canonical_type = p->get_naked_canonical_type();
27026  ABG_ASSERT(canonical_type);
27027 
27028  size_t ptr_value = reinterpret_cast<size_t>(canonical_type);
27029  pointer_set::iterator it = priv_->visited_ir_nodes.find(ptr_value);
27030  if (it == priv_->visited_ir_nodes.end())
27031  return false;
27032 
27033  return true;
27034 }
27035 
27036 bool
27037 ir_node_visitor::visit_begin(decl_base*)
27038 {return true;}
27039 
27040 bool
27041 ir_node_visitor::visit_end(decl_base*)
27042 {return true;}
27043 
27044 bool
27045 ir_node_visitor::visit_begin(scope_decl*)
27046 {return true;}
27047 
27048 bool
27049 ir_node_visitor::visit_end(scope_decl*)
27050 {return true;}
27051 
27052 bool
27053 ir_node_visitor::visit_begin(type_base*)
27054 {return true;}
27055 
27056 bool
27057 ir_node_visitor::visit_end(type_base*)
27058 {return true;}
27059 
27060 bool
27061 ir_node_visitor::visit_begin(scope_type_decl* t)
27062 {return visit_begin(static_cast<type_base*>(t));}
27063 
27064 bool
27065 ir_node_visitor::visit_end(scope_type_decl* t)
27066 {return visit_end(static_cast<type_base*>(t));}
27067 
27068 bool
27069 ir_node_visitor::visit_begin(type_decl* t)
27070 {return visit_begin(static_cast<type_base*>(t));}
27071 
27072 bool
27073 ir_node_visitor::visit_end(type_decl* t)
27074 {return visit_end(static_cast<type_base*>(t));}
27075 
27076 bool
27077 ir_node_visitor::visit_begin(namespace_decl* d)
27078 {return visit_begin(static_cast<decl_base*>(d));}
27079 
27080 bool
27081 ir_node_visitor::visit_end(namespace_decl* d)
27082 {return visit_end(static_cast<decl_base*>(d));}
27083 
27084 bool
27085 ir_node_visitor::visit_begin(qualified_type_def* t)
27086 {return visit_begin(static_cast<type_base*>(t));}
27087 
27088 bool
27089 ir_node_visitor::visit_end(qualified_type_def* t)
27090 {return visit_end(static_cast<type_base*>(t));}
27091 
27092 bool
27093 ir_node_visitor::visit_begin(pointer_type_def* t)
27094 {return visit_begin(static_cast<type_base*>(t));}
27095 
27096 bool
27097 ir_node_visitor::visit_end(pointer_type_def* t)
27098 {return visit_end(static_cast<type_base*>(t));}
27099 
27100 bool
27101 ir_node_visitor::visit_begin(reference_type_def* t)
27102 {return visit_begin(static_cast<type_base*>(t));}
27103 
27104 bool
27105 ir_node_visitor::visit_end(reference_type_def* t)
27106 {return visit_end(static_cast<type_base*>(t));}
27107 
27108 bool
27109 ir_node_visitor::visit_begin(array_type_def* t)
27110 {return visit_begin(static_cast<type_base*>(t));}
27111 
27112 bool
27113 ir_node_visitor::visit_end(array_type_def* t)
27114 {return visit_end(static_cast<type_base*>(t));}
27115 
27116 bool
27117 ir_node_visitor::visit_begin(array_type_def::subrange_type* t)
27118 {return visit_begin(static_cast<type_base*>(t));}
27119 
27120 bool
27121 ir_node_visitor::visit_end(array_type_def::subrange_type* t)
27122 {return visit_end(static_cast<type_base*>(t));}
27123 
27124 bool
27125 ir_node_visitor::visit_begin(enum_type_decl* t)
27126 {return visit_begin(static_cast<type_base*>(t));}
27127 
27128 bool
27129 ir_node_visitor::visit_end(enum_type_decl* t)
27130 {return visit_end(static_cast<type_base*>(t));}
27131 
27132 bool
27133 ir_node_visitor::visit_begin(typedef_decl* t)
27134 {return visit_begin(static_cast<type_base*>(t));}
27135 
27136 bool
27137 ir_node_visitor::visit_end(typedef_decl* t)
27138 {return visit_end(static_cast<type_base*>(t));}
27139 
27140 bool
27141 ir_node_visitor::visit_begin(function_type* t)
27142 {return visit_begin(static_cast<type_base*>(t));}
27143 
27144 bool
27145 ir_node_visitor::visit_end(function_type* t)
27146 {return visit_end(static_cast<type_base*>(t));}
27147 
27148 bool
27149 ir_node_visitor::visit_begin(var_decl* d)
27150 {return visit_begin(static_cast<decl_base*>(d));}
27151 
27152 bool
27153 ir_node_visitor::visit_end(var_decl* d)
27154 {return visit_end(static_cast<decl_base*>(d));}
27155 
27156 bool
27157 ir_node_visitor::visit_begin(function_decl* d)
27158 {return visit_begin(static_cast<decl_base*>(d));}
27159 
27160 bool
27161 ir_node_visitor::visit_end(function_decl* d)
27162 {return visit_end(static_cast<decl_base*>(d));}
27163 
27164 bool
27165 ir_node_visitor::visit_begin(function_decl::parameter* d)
27166 {return visit_begin(static_cast<decl_base*>(d));}
27167 
27168 bool
27169 ir_node_visitor::visit_end(function_decl::parameter* d)
27170 {return visit_end(static_cast<decl_base*>(d));}
27171 
27172 bool
27173 ir_node_visitor::visit_begin(function_tdecl* d)
27174 {return visit_begin(static_cast<decl_base*>(d));}
27175 
27176 bool
27177 ir_node_visitor::visit_end(function_tdecl* d)
27178 {return visit_end(static_cast<decl_base*>(d));}
27179 
27180 bool
27181 ir_node_visitor::visit_begin(class_tdecl* d)
27182 {return visit_begin(static_cast<decl_base*>(d));}
27183 
27184 bool
27185 ir_node_visitor::visit_end(class_tdecl* d)
27186 {return visit_end(static_cast<decl_base*>(d));}
27187 
27188 bool
27189 ir_node_visitor::visit_begin(class_or_union* t)
27190 {return visit_begin(static_cast<type_base*>(t));}
27191 
27192 bool
27193 ir_node_visitor::visit_end(class_or_union* t)
27194 {return visit_end(static_cast<type_base*>(t));}
27195 
27196 bool
27197 ir_node_visitor::visit_begin(class_decl* t)
27198 {return visit_begin(static_cast<type_base*>(t));}
27199 
27200 bool
27201 ir_node_visitor::visit_end(class_decl* t)
27202 {return visit_end(static_cast<type_base*>(t));}
27203 
27204 bool
27205 ir_node_visitor::visit_begin(union_decl* t)
27206 {return visit_begin(static_cast<type_base*>(t));}
27207 
27208 bool
27209 ir_node_visitor::visit_end(union_decl* t)
27210 {return visit_end(static_cast<type_base*>(t));}
27211 
27212 bool
27213 ir_node_visitor::visit_begin(class_decl::base_spec* d)
27214 {return visit_begin(static_cast<decl_base*>(d));}
27215 
27216 bool
27217 ir_node_visitor::visit_end(class_decl::base_spec* d)
27218 {return visit_end(static_cast<decl_base*>(d));}
27219 
27220 bool
27221 ir_node_visitor::visit_begin(member_function_template* d)
27222 {return visit_begin(static_cast<decl_base*>(d));}
27223 
27224 bool
27225 ir_node_visitor::visit_end(member_function_template* d)
27226 {return visit_end(static_cast<decl_base*>(d));}
27227 
27228 bool
27229 ir_node_visitor::visit_begin(member_class_template* d)
27230 {return visit_begin(static_cast<decl_base*>(d));}
27231 
27232 bool
27233 ir_node_visitor::visit_end(member_class_template* d)
27234 {return visit_end(static_cast<decl_base*>(d));}
27235 
27236 // </ir_node_visitor stuff>
27237 
27238 // <debugging facilities>
27239 
27240 /// Generate a different string at each invocation.
27241 ///
27242 /// @return the resulting string.
27243 static string
27244 get_next_string()
27245 {
27246  static __thread size_t counter;
27247  ++counter;
27248  std::ostringstream o;
27249  o << counter;
27250  return o.str();
27251 }
27252 
27253 /// Convenience typedef for a hash map of pointer to function_decl and
27254 /// string.
27255 typedef unordered_map<const function_decl*, string,
27256  function_decl::hash,
27258 
27259 /// Return a string associated to a given function. Two functions
27260 /// that compare equal would yield the same string, as far as this
27261 /// routine is concerned. And two functions that are different would
27262 /// yield different strings.
27263 ///
27264 /// This is used to debug core diffing issues on functions. The
27265 /// sequence of strings can be given to the 'testdiff2' program that
27266 /// is in the tests/ directory of the source tree, to reproduce core
27267 /// diffing issues on string and thus ease the debugging.
27268 ///
27269 /// @param fn the function to generate a string for.
27270 ///
27271 /// @param m the function_decl* <-> string map to be used by this
27272 /// function to generate strings associated to a function.
27273 ///
27274 /// @return the resulting string.
27275 static const string&
27276 fn_to_str(const function_decl* fn,
27278 {
27279  fns_to_str_map_type::const_iterator i = m.find(fn);
27280  if (i != m.end())
27281  return i->second;
27282  string s = get_next_string();
27283  return m[fn]= s;
27284 }
27285 
27286 /// Generate a sequence of string that matches a given sequence of
27287 /// function. In the resulting sequence, each function is "uniquely
27288 /// representated" by a string. For instance, if the same function "foo"
27289 /// appears at indexes 1 and 3, then the same string 'schmurf' (okay,
27290 /// we don't care about the actual string) would appear at index 1 and 3.
27291 ///
27292 /// @param begin the beginning of the sequence of functions to consider.
27293 ///
27294 /// @param end the end of the sequence of functions. This points to
27295 /// one-passed-the-end of the actual sequence.
27296 ///
27297 /// @param m the function_decl* <-> string map to be used by this
27298 /// function to generate strings associated to a function.
27299 ///
27300 /// @param o the output stream where to emit the generated list of
27301 /// strings to.
27302 static void
27303 fns_to_str(vector<function_decl*>::const_iterator begin,
27304  vector<function_decl*>::const_iterator end,
27306  std::ostream& o)
27307 {
27308  vector<function_decl*>::const_iterator i;
27309  for (i = begin; i != end; ++i)
27310  o << "'" << fn_to_str(*i, m) << "' ";
27311 }
27312 
27313 /// For each sequence of functions given in argument, generate a
27314 /// sequence of string that matches a given sequence of function. In
27315 /// the resulting sequence, each function is "uniquely representated"
27316 /// by a string. For instance, if the same function "foo" appears at
27317 /// indexes 1 and 3, then the same string 'schmurf' (okay, we don't
27318 /// care about the actual string) would appear at index 1 and 3.
27319 ///
27320 /// @param a_begin the beginning of the sequence of functions to consider.
27321 ///
27322 /// @param a_end the end of the sequence of functions. This points to
27323 /// one-passed-the-end of the actual sequence.
27324 ///
27325 /// @param b_begin the beginning of the second sequence of functions
27326 /// to consider.
27327 ///
27328 /// @param b_end the end of the second sequence of functions.
27329 ///
27330 /// @param m the function_decl* <-> string map to be used by this
27331 /// function to generate strings associated to a function.
27332 ///
27333 /// @param o the output stream where to emit the generated list of
27334 /// strings to.
27335 static void
27336 fns_to_str(vector<function_decl*>::const_iterator a_begin,
27337  vector<function_decl*>::const_iterator a_end,
27338  vector<function_decl*>::const_iterator b_begin,
27339  vector<function_decl*>::const_iterator b_end,
27341  std::ostream& o)
27342 {
27343  fns_to_str(a_begin, a_end, m, o);
27344  o << "->|<- ";
27345  fns_to_str(b_begin, b_end, m, o);
27346  o << "\n";
27347 }
27348 
27349 /// For each sequence of functions given in argument, generate a
27350 /// sequence of string that matches a given sequence of function. In
27351 /// the resulting sequence, each function is "uniquely representated"
27352 /// by a string. For instance, if the same function "foo" appears at
27353 /// indexes 1 and 3, then the same string 'schmurf' (okay, we don't
27354 /// care about the actual string) would appear at index 1 and 3.
27355 ///
27356 /// @param a_begin the beginning of the sequence of functions to consider.
27357 ///
27358 /// @param a_end the end of the sequence of functions. This points to
27359 /// one-passed-the-end of the actual sequence.
27360 ///
27361 /// @param b_begin the beginning of the second sequence of functions
27362 /// to consider.
27363 ///
27364 /// @param b_end the end of the second sequence of functions.
27365 ///
27366 /// @param o the output stream where to emit the generated list of
27367 /// strings to.
27368 void
27369 fns_to_str(vector<function_decl*>::const_iterator a_begin,
27370  vector<function_decl*>::const_iterator a_end,
27371  vector<function_decl*>::const_iterator b_begin,
27372  vector<function_decl*>::const_iterator b_end,
27373  std::ostream& o)
27374 {
27376  fns_to_str(a_begin, a_end, b_begin, b_end, m, o);
27377 }
27378 
27379 // </debugging facilities>
27380 
27381 // </class template>
27382 
27383 }// end namespace ir
27384 }//end namespace abigail
27385 
27386 namespace
27387 {
27388 
27389 /// Update the qualified parent name, qualified name and scoped name
27390 /// of a tree decl node.
27391 ///
27392 /// @return true if the tree walking should continue, false otherwise.
27393 ///
27394 /// @param d the tree node to take in account.
27395 bool
27396 qualified_name_setter::do_update(abigail::ir::decl_base* d)
27397 {
27398  std::string parent_qualified_name;
27399  abigail::ir::scope_decl* parent = d->get_scope();
27400  if (parent)
27401  d->priv_->qualified_parent_name_ = parent->get_qualified_name();
27402  else
27403  d->priv_->qualified_parent_name_ = abigail::interned_string();
27404 
27405  const abigail::ir::environment& env = d->get_environment();
27406 
27407  if (!d->priv_->qualified_parent_name_.empty())
27408  {
27409  if (d->get_name().empty())
27410  d->priv_->qualified_name_ = abigail::interned_string();
27411  else
27412  d->priv_->qualified_name_ =
27413  env.intern(d->priv_->qualified_parent_name_ + "::" + d->get_name());
27414  }
27415 
27416  if (d->priv_->scoped_name_.empty())
27417  {
27418  if (parent
27419  && !parent->get_is_anonymous()
27420  && !parent->get_name().empty())
27421  d->priv_->scoped_name_ =
27422  env.intern(parent->get_name() + "::" + d->get_name());
27423  else
27424  d->priv_->scoped_name_ =
27425  env.intern(d->get_name());
27426  }
27427 
27428  if (!is_scope_decl(d))
27429  return false;
27430 
27431  return true;
27432 }
27433 
27434 /// This is called when we start visiting a decl node, during the
27435 /// udpate of the qualified name of a given sub-tree.
27436 ///
27437 /// @param d the decl node we are visiting.
27438 ///
27439 /// @return true iff the traversal should keep going.
27440 bool
27441 qualified_name_setter::visit_begin(abigail::ir::decl_base* d)
27442 {return do_update(d);}
27443 
27444 /// This is called when we start visiting a type node, during the
27445 /// udpate of the qualified name of a given sub-tree.
27446 ///
27447 /// @param d the decl node we are visiting.
27448 ///
27449 /// @return true iff the traversal should keep going.
27450 bool
27451 qualified_name_setter::visit_begin(abigail::ir::type_base* t)
27452 {
27454  return do_update(d);
27455  return false;
27456 }
27457 }// 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:1589
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:1112
#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:909
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:97
const char * get_string(const char *s) const
Get a pointer to the interned string which has a given value.
Definition: abg-ir.cc:107
interned_string create_string(const std::string &)
Create an interned string with a given value.
Definition: abg-ir.cc:124
interned_string_pool()
Default constructor.
Definition: abg-ir.cc:84
~interned_string_pool()
Destructor.
Definition: abg-ir.cc:133
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:2487
void set_signed(int64_t v)
Setter of the bound value as signed.
Definition: abg-ir.cc:17379
void set_signedness(enum signedness s)
Setter of the signedness (unsigned VS signed) of the bound value.
Definition: abg-ir.cc:17347
enum signedness get_signedness() const
Getter of the signedness (unsigned VS signed) of the bound value.
Definition: abg-ir.cc:17340
int64_t get_signed_value() const
Getter of the bound value as a signed value.
Definition: abg-ir.cc:17354
bool operator==(const bound_value &) const
Equality operator of the bound value.
Definition: abg-ir.cc:17391
uint64_t get_unsigned_value()
Getter of the bound value as an unsigned value.
Definition: abg-ir.cc:17362
bound_value()
Default constructor of the array_type_def::subrange_type::bound_value class.
Definition: abg-ir.cc:17312
void set_unsigned(uint64_t v)
Setter of the bound value as unsigned.
Definition: abg-ir.cc:17369
Abstraction for an array range type, like in Ada, or just for an array dimension like in C or C++.
Definition: abg-ir.h:2472
void set_lower_bound(int64_t lb)
Setter of the lower bound.
Definition: abg-ir.cc:17555
void set_upper_bound(int64_t ub)
Setter of the upper bound of the subrange type.
Definition: abg-ir.cc:17548
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:17524
string as_string() const
Return a string representation of the sub range.
Definition: abg-ir.cc:17604
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:17793
bool is_infinite() const
Test if the length of the subrange type is infinite.
Definition: abg-ir.cc:17582
bool operator!=(const decl_base &o) const
Equality operator.
Definition: abg-ir.cc:17732
int64_t get_upper_bound() const
Getter of the upper bound of the subrange type.
Definition: abg-ir.cc:17534
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:17516
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:17688
int64_t get_lower_bound() const
Getter of the lower bound of the subrange type.
Definition: abg-ir.cc:17541
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:17771
static string vector_as_string(const vector< subrange_sptr > &)
Return a string representation of a vector of subranges.
Definition: abg-ir.cc:17627
uint64_t get_length() const
Getter of the length of the subrange type.
Definition: abg-ir.cc:17565
translation_unit::language get_language() const
Getter of the language that generated this type.
Definition: abg-ir.cc:17597
The abstraction of an array type.
Definition: abg-ir.h:2446
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:18199
const type_base_sptr get_element_type() const
Getter of the type of an array element.
Definition: abg-ir.cc:18130
virtual bool is_infinite() const
Definition: abg-ir.cc:18169
void set_element_type(const type_base_sptr &element_type)
Setter of the type of array element.
Definition: abg-ir.cc:18145
shared_ptr< subrange_type > subrange_sptr
Convenience typedef for a shared pointer on a function_decl::subrange.
Definition: abg-ir.h:2460
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:18259
const std::vector< subrange_sptr > & get_subranges() const
Get the array's subranges.
Definition: abg-ir.cc:18286
virtual bool operator==(const decl_base &) const
Return true iff the two decls have the same name.
Definition: abg-ir.cc:18108
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition: abg-ir.h:2467
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:17981
translation_unit::language get_language() const
Get the language of the array.
Definition: abg-ir.cc:18097
virtual void append_subranges(const std::vector< subrange_sptr > &subs)
Append subranges from the vector.
Definition: abg-ir.cc:18155
Abstraction of a base specifier in a class declaration.
Definition: abg-ir.h:4338
class_decl_sptr get_base_class() const
Get the base class referred to by the current base class specifier.
Definition: abg-ir.cc:23263
bool get_is_virtual() const
Getter of the "is-virtual" proprerty of the base class specifier.
Definition: abg-ir.cc:23270
long get_offset_in_bits() const
Getter of the offset of the base.
Definition: abg-ir.cc:23277
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:23303
virtual bool operator==(const decl_base &) const
Comparison operator for class_decl::base_spec.
Definition: abg-ir.cc:23397
virtual size_t get_hash() const
Calculate the hash value for a class_decl::base_spec.
Definition: abg-ir.cc:23284
Abstracts a class declaration.
Definition: abg-ir.h:4141
void is_struct(bool f)
Set the "is-struct" flag of the class.
Definition: abg-ir.cc:23074
bool has_virtual_member_functions() const
Test if the current instance of class_decl has virtual member functions.
Definition: abg-ir.cc:23844
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:23143
bool is_struct() const
Test if the class is a struct.
Definition: abg-ir.cc:23081
const base_specs & get_base_specifiers() const
Get the base specifiers for this class.
Definition: abg-ir.cc:23098
virtual ~class_decl()
Destructor of the class_decl type.
Definition: abg-ir.cc:24444
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:23059
bool has_vtable() const
Test if the current instance has a vtable.
Definition: abg-ir.cc:23872
ssize_t get_biggest_vtable_offset() const
Get the highest vtable offset of all the virtual methods of the class.
Definition: abg-ir.cc:23886
bool has_virtual_bases() const
Test if the current instance of class_decl has at least one virtual base.
Definition: abg-ir.cc:23853
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:24360
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition: abg-ir.h:4155
void add_base_specifier(shared_ptr< base_spec > b)
Add a base specifier to this class.
Definition: abg-ir.cc:23088
const member_functions & get_virtual_mem_fns() const
Get the virtual member functions of this class.
Definition: abg-ir.cc:23124
void sort_virtual_mem_fns()
Sort the virtual member functions by their virtual index.
Definition: abg-ir.cc:23148
friend bool equals(const class_decl &, const class_decl &, change_kind *)
Compares two instances of class_decl.
Definition: abg-ir.cc:24024
virtual bool operator==(const decl_base &) const
Comparison operator for class_decl.
Definition: abg-ir.cc:24212
bool has_no_base_nor_member() const
Return true iff the class has no entity in its scope.
Definition: abg-ir.cc:23835
virtual size_t get_hash() const
Return the hash value for the current instance.
Definition: abg-ir.cc:23903
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:23108
vector< base_spec_sptr > base_specs
Convenience typedef.
Definition: abg-ir.h:4160
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:23169
The base type of class_decl and union_decl.
Definition: abg-ir.h:3939
virtual size_t get_num_anonymous_member_classes() const
Get the number of anonymous member classes contained in this class.
Definition: abg-ir.cc:21917
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:22135
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:22068
const member_functions & get_member_functions() const
Get the member functions of this class_or_union.
Definition: abg-ir.cc:22163
virtual void remove_member_decl(decl_base_sptr)
Remove a given decl from the current class_or_union scope.
Definition: abg-ir.cc:21803
const member_function_templates & get_member_function_templates() const
Get the member function templates of this class.
Definition: abg-ir.cc:22239
virtual size_t get_size_in_bits() const
Getter of the size of the class_or_union type.
Definition: abg-ir.cc:21902
virtual size_t get_num_anonymous_member_unions() const
Get the number of anonymous member unions contained in this class.
Definition: abg-ir.cc:21935
void add_member_function_template(member_function_template_sptr)
Append a member function template to the class_or_union.
Definition: abg-ir.cc:22253
unordered_map< ssize_t, member_functions > virtual_mem_fn_map_type
Convenience typedef.
Definition: abg-ir.h:3971
vector< method_decl_sptr > member_functions
Convenience typedef.
Definition: abg-ir.h:3970
const data_members & get_data_members() const
Get the data members of this class_or_union.
Definition: abg-ir.cc:22027
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:21984
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:22214
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:22198
virtual void set_size_in_bits(size_t)
Setter of the size of the class_or_union type.
Definition: abg-ir.cc:21886
decl_base_sptr insert_member_decl(decl_base_sptr member)
Insert a data member to this class_or_union type.
Definition: abg-ir.cc:22295
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:21791
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:21709
void add_member_class_template(member_class_template_sptr m)
Append a member class template to the class_or_union.
Definition: abg-ir.cc:22267
const data_members & get_non_static_data_members() const
Get the non-static data memebers of this class_or_union.
Definition: abg-ir.cc:22118
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:22172
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:21828
vector< var_decl_sptr > data_members
Convenience typedef.
Definition: abg-ir.h:3969
virtual ~class_or_union()
Destrcutor of the class_or_union type.
Definition: abg-ir.cc:21782
bool has_no_member() const
Definition: abg-ir.cc:22280
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:22329
friend void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:24747
virtual size_t get_alignment_in_bits() const
Getter of the alignment of the class_or_union type.
Definition: abg-ir.cc:21854
const member_class_templates & get_member_class_templates() const
Get the member class templates of this class.
Definition: abg-ir.cc:22246
virtual void set_alignment_in_bits(size_t)
Setter of the alignment of the class type.
Definition: abg-ir.cc:21870
vector< type_base_sptr > member_types
Convenience typedef.
Definition: abg-ir.h:3964
virtual size_t get_num_anonymous_member_enums() const
Get the number of anonymous member enums contained in this class.
Definition: abg-ir.cc:21953
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:22038
Abstract a class template.
Definition: abg-ir.h:3742
shared_ptr< class_decl > get_pattern() const
Getter of the pattern of the template.
Definition: abg-ir.cc:26134
void set_pattern(class_decl_sptr p)
Setter of the pattern of the template.
Definition: abg-ir.cc:26123
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:26183
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:26138
The abstraction of the relationship between an entity and its containing scope (its context)....
Definition: abg-ir.h:1224
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:36
const translation_units & get_translation_units() const
Return the list of translation units of the current corpus.
Definition: abg-corpus.cc:700
type_maps & get_types()
Get the maps that associate a name to a certain kind of type.
Definition: abg-corpus.cc:732
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:841
const corpus_group * get_group() const
Getter of the group this corpus is a member of.
Definition: abg-corpus.cc:849
const environment & get_environment() const
Getter of the enviroment of the corpus.
Definition: abg-corpus.cc:655
The base type of all declarations.
Definition: abg-ir.h:1516
void set_definition_of_declaration(const decl_base_sptr &)
Set the definition of this declaration-only decl_base.
Definition: abg-ir.cc:14925
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:5036
virtual bool operator!=(const decl_base &) const
Inequality operator.
Definition: abg-ir.cc:5293
scope_decl * get_scope() const
Return the type containing the current decl, if any.
Definition: abg-ir.cc:4865
void set_qualified_name(const interned_string &) const
Setter for the qualified name.
Definition: abg-ir.cc:4574
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:4658
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:5648
const decl_base_sptr get_earlier_declaration() const
If this decl_base is a definition, get its earlier declaration.
Definition: abg-ir.cc:4984
virtual void set_linkage_name(const string &m)
Setter for the linkage name.
Definition: abg-ir.cc:4840
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:5020
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Compute the qualified name of the decl.
Definition: abg-ir.cc:4896
void clear_qualified_name()
Clear the qualified name of this decl.
Definition: abg-ir.cc:4567
void set_name(const string &n)
Setter for the name of the decl.
Definition: abg-ir.cc:4728
const location & get_location() const
Get the location of a given declaration.
Definition: abg-ir.cc:4678
binding
ELF binding.
Definition: abg-ir.h:1567
typedef_decl_sptr get_naming_typedef() const
Getter for the naming typedef of the current decl.
Definition: abg-ir.cc:4788
const interned_string & get_name() const
Getter for the name of the current decl.
Definition: abg-ir.cc:4884
virtual void set_scope(scope_decl *)
Setter of the scope of the current decl.
Definition: abg-ir.cc:5320
const interned_string & peek_qualified_name() const
Getter for the qualified name.
Definition: abg-ir.cc:4558
const context_rel * get_context_rel() const
Getter for the context relationship.
Definition: abg-ir.cc:4608
friend void set_member_function_is_virtual(function_decl &, bool)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6659
bool get_is_anonymous() const
Test if the current declaration is anonymous.
Definition: abg-ir.cc:4741
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.
Definition: abg-ir.cc:8293
virtual const interned_string & get_scoped_name() const
Return the scoped name of the decl.
Definition: abg-ir.cc:4976
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:5004
friend void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5617
void set_naming_typedef(const typedef_decl_sptr &)
Set the naming typedef of the current instance of decl_base.
Definition: abg-ir.cc:4806
void set_location(const location &l)
Set the location for a given declaration.
Definition: abg-ir.cc:4716
void set_is_anonymous(bool)
Set the "is_anonymous" flag of the current declaration.
Definition: abg-ir.cc:4751
void set_visibility(visibility v)
Setter for the visibility of the decl.
Definition: abg-ir.cc:4857
void set_temporary_qualified_name(const interned_string &) const
Setter for the temporary qualified name of the current declaration.
Definition: abg-ir.cc:4601
visibility get_visibility() const
Getter for the visibility of the decl.
Definition: abg-ir.cc:4850
visibility
ELF visibility.
Definition: abg-ir.h:1557
bool get_is_declaration_only() const
Test if a decl_base is a declaration-only decl.
Definition: abg-ir.cc:5027
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:5309
void set_earlier_declaration(const decl_base_sptr &)
set the earlier declaration of this decl_base definition.
Definition: abg-ir.cc:4992
const interned_string & get_linkage_name() const
Getter for the mangled name.
Definition: abg-ir.cc:4833
friend enum access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5588
friend bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6621
virtual ~decl_base()
Destructor of the decl_base type.
Definition: abg-ir.cc:5297
virtual bool operator==(const decl_base &) const
Return true iff the two decls have the same name.
Definition: abg-ir.cc:5282
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:4877
bool get_is_anonymous_or_has_anonymous_parent() const
Definition: abg-ir.cc:4774
bool get_has_anonymous_parent() const
Get the "has_anonymous_parent" flag of the current declaration.
Definition: abg-ir.cc:4763
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:4650
friend bool equals(const decl_base &, const decl_base &, change_kind *)
Compares two instances of decl_base.
Definition: abg-ir.cc:5199
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:4627
const interned_string & peek_temporary_qualified_name() const
Getter of the temporary qualified name of the current declaration.
Definition: abg-ir.cc:4587
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:4919
The abstraction for a data member context relationship. This relates a data member to its parent clas...
Definition: abg-ir.h:2867
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:3158
void set_anonymous_data_member(var_decl *)
Set the containing anonymous data member of this data member context relationship....
Definition: abg-ir.cc:3168
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1171
version & operator=(const version &o)
Assign a version to the current one.
Definition: abg-ir.cc:3071
bool operator==(const version &o) const
Compares the current version against another one.
Definition: abg-ir.cc:3053
bool is_default() const
Getter for the 'is_default' property of the version.
Definition: abg-ir.cc:3033
const string & str() const
Getter for the version name.
Definition: abg-ir.cc:3019
bool operator!=(const version &o) const
Inequality operator.
Definition: abg-ir.cc:3062
Abstraction of an elf symbol.
Definition: abg-ir.h:900
const abg_compat::optional< std::string > & get_namespace() const
Getter of the 'namespace' property.
Definition: abg-ir.cc:2156
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:2479
elf_symbol_sptr get_next_common_instance() const
Get the next common instance of the current common symbol.
Definition: abg-ir.cc:2374
type get_type() const
Getter for the type of the current instance of elf_symbol.
Definition: abg-ir.cc:1996
const elf_symbol_sptr get_main_symbol() const
Get the main symbol of an alias chain.
Definition: abg-ir.cc:2211
void set_is_in_ksymtab(bool is_in_ksymtab)
Setter of the 'is-in-ksymtab' property.
Definition: abg-ir.cc:2135
bool has_aliases() const
Check if the current elf_symbol has an alias.
Definition: abg-ir.cc:2240
void set_name(const string &n)
Setter for the name of the current intance of elf_symbol.
Definition: abg-ir.cc:1986
bool is_suppressed() const
Getter for the 'is-suppressed' property.
Definition: abg-ir.cc:2172
binding
The binding of a symbol.
Definition: abg-ir.h:917
int get_number_of_aliases() const
Get the number of aliases to this elf symbol.
Definition: abg-ir.cc:2247
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:2500
void set_binding(binding b)
Setter for the binding of the current instance of elf_symbol.
Definition: abg-ir.cc:2031
void add_common_instance(const elf_symbol_sptr &)
Add a common instance to the current common elf symbol.
Definition: abg-ir.cc:2385
void add_alias(const elf_symbol_sptr &)
Add an alias to the current elf symbol.
Definition: abg-ir.cc:2264
void set_is_suppressed(bool is_suppressed)
Setter for the 'is-suppressed' property.
Definition: abg-ir.cc:2181
bool is_variable() const
Test if the current instance of elf_symbol is a variable symbol or not.
Definition: abg-ir.cc:2119
elf_symbol_sptr update_main_symbol(const std::string &)
Update the main symbol for a group of aliased symbols.
Definition: abg-ir.cc:2310
void set_size(size_t)
Setter of the size of the symbol.
Definition: abg-ir.cc:2017
const string & get_name() const
Getter for the name of the elf_symbol.
Definition: abg-ir.cc:1979
binding get_binding() const
Getter for the binding of the current instance of elf_symbol.
Definition: abg-ir.cc:2024
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:2566
bool is_function() const
Test if the current instance of elf_symbol is a function symbol or not.
Definition: abg-ir.cc:2110
type
The type of a symbol.
Definition: abg-ir.h:904
void set_version(const version &v)
Setter for the version of the current instance of elf_symbol.
Definition: abg-ir.cc:2045
const abg_compat::optional< uint32_t > & get_crc() const
Getter of the 'crc' property.
Definition: abg-ir.cc:2142
void set_visibility(visibility v)
Setter of the visibility of the current instance of elf_symbol.
Definition: abg-ir.cc:2056
bool does_alias(const elf_symbol &) const
Test if the current symbol aliases another one.
Definition: abg-ir.cc:2625
bool is_main_symbol() const
Tests whether this symbol is the main symbol.
Definition: abg-ir.cc:2225
void set_crc(const abg_compat::optional< uint32_t > &crc)
Setter of the 'crc' property.
Definition: abg-ir.cc:2149
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:1902
visibility
The visibility of the symbol.
Definition: abg-ir.h:926
version & get_version() const
Getter for the version of the current instanc of elf_symbol.
Definition: abg-ir.cc:2038
bool is_common_symbol() const
Return true if the symbol is a common one.
Definition: abg-ir.cc:2343
void set_index(size_t)
Setter for the index.
Definition: abg-ir.cc:1972
visibility get_visibility() const
Getter of the visibility of the current instance of elf_symbol.
Definition: abg-ir.cc:2064
bool has_other_common_instances() const
Return true if this common common symbol has other common instances.
Definition: abg-ir.cc:2359
size_t get_index() const
Getter for the index.
Definition: abg-ir.cc:1965
const string & get_id_string() const
Get a string that is representative of a given elf_symbol.
Definition: abg-ir.cc:2430
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:2457
const environment & get_environment() const
Getter of the environment used by the current instance of elf_symbol.
Definition: abg-ir.cc:1958
void set_type(type t)
Setter for the type of the current instance of elf_symbol.
Definition: abg-ir.cc:2003
bool is_public() const
Test if the current instance of elf_symbol is public or not.
Definition: abg-ir.cc:2094
bool is_in_ksymtab() const
Getter of the 'is-in-ksymtab' property.
Definition: abg-ir.cc:2127
size_t get_size() const
Getter of the size of the symbol.
Definition: abg-ir.cc:2010
bool is_defined() const
Test if the current instance of elf_symbol is defined or not.
Definition: abg-ir.cc:2072
void set_namespace(const abg_compat::optional< std::string > &ns)
Setter of the 'namespace' property.
Definition: abg-ir.cc:2163
elf_symbol_sptr get_next_alias() const
Get the next alias of the current symbol.
Definition: abg-ir.cc:2232
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:2611
The abstraction of an enumerator.
Definition: abg-ir.h:2759
enumerator()
Default constructor of the enum_type_decl::enumerator type.
Definition: abg-ir.cc:18848
bool operator!=(const enumerator &other) const
Inequality operator.
Definition: abg-ir.cc:18908
void set_name(const string &n)
Setter for the name of enum_type_decl::enumerator.
Definition: abg-ir.cc:18950
enum_type_decl * get_enum_type() const
Getter for the enum type that this enumerator is for.
Definition: abg-ir.cc:18972
const string & get_name() const
Getter for the name of the current instance of enum_type_decl::enumerator.
Definition: abg-ir.cc:18917
void set_enum_type(enum_type_decl *)
Setter for the enum type that this enumerator is for.
Definition: abg-ir.cc:18979
void set_value(int64_t v)
Setter for the value of enum_type_decl::enumerator.
Definition: abg-ir.cc:18965
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:18934
int64_t get_value() const
Getter for the value of enum_type_decl::enumerator.
Definition: abg-ir.cc:18958
bool operator==(const enumerator &other) const
Equality operator.
Definition: abg-ir.cc:18895
enumerator & operator=(const enumerator &)
Assignment operator of the enum_type_decl::enumerator type.
Definition: abg-ir.cc:18879
Abstracts a declaration for an enum type.
Definition: abg-ir.h:2677
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition: abg-ir.h:2690
virtual ~enum_type_decl()
Destructor for the enum type declaration.
Definition: abg-ir.cc:18426
const enumerators & get_enumerators() const
Definition: abg-ir.cc:18357
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:18404
type_base_sptr get_underlying_type() const
Return the underlying type of the enum.
Definition: abg-ir.cc:18352
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:18769
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:18383
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:3619
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:3733
const type_base_sptr & get_void_type() const
Get a type_decl that represents a "void" type for the current environment.
Definition: abg-ir.cc:3523
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:3685
static string & get_variadic_parameter_type_name()
Getter of the name of the variadic parameter type.
Definition: abg-ir.cc:3551
const config & get_config() const
Getter of the general configuration object.
Definition: abg-ir.cc:3723
environment()
Default constructor of the environment type.
Definition: abg-ir.cc:3183
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:3870
bool canonicalization_is_done() const
Test if the canonicalization of types created out of the current environment is done.
Definition: abg-ir.cc:3563
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:3893
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:3538
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:3655
virtual ~environment()
Destructor for the environment type.
Definition: abg-ir.cc:3188
interned_string intern(const string &) const
Do intern a string.
Definition: abg-ir.cc:3716
bool do_on_the_fly_canonicalization() const
Getter for the "on-the-fly-canonicalization" flag.
Definition: abg-ir.cc:3586
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:3759
canonical_types_map_type & get_canonical_types_map()
Getter the map of canonical types.
Definition: abg-ir.cc:3196
Abstraction of a function parameter.
Definition: abg-ir.h:3196
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Compute the qualified name of the parameter.
Definition: abg-ir.cc:21542
interned_string get_type_name() const
Definition: abg-ir.cc:21331
interned_string get_name_id() const
Get a name uniquely identifying the parameter in the function.
Definition: abg-ir.cc:21369
const string get_type_pretty_representation() const
Definition: abg-ir.cc:21350
virtual bool traverse(ir_node_visitor &v)
Traverse the diff sub-tree under the current instance function_decl.
Definition: abg-ir.cc:21507
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:21527
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:21562
Abstraction for a function declaration.
Definition: abg-ir.h:3024
shared_ptr< parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition: abg-ir.h:3044
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:20724
const function_type * get_naked_type() const
Fast getter of the type of the current instance of function_decl.
Definition: abg-ir.cc:20818
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:21193
void append_parameters(std::vector< parameter_sptr > &parms)
Append a vector of parameters to the type of this function.
Definition: abg-ir.cc:20888
bool is_variadic() const
Return true iff the function takes a variable number of parameters.
Definition: abg-ir.cc:21112
parameters::const_iterator get_first_non_implicit_parm() const
Getter for the first non-implicit parameter of a function decl.
Definition: abg-ir.cc:20784
const function_type_sptr get_type() const
Return the type of the current instance of function_decl.
Definition: abg-ir.cc:20803
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:20605
const type_base_sptr get_return_type() const
Definition: abg-ir.cc:20869
function_decl_sptr clone() const
Create a new instance of function_decl that is a clone of the current one.
Definition: abg-ir.cc:20901
const std::vector< parameter_sptr > & get_parameters() const
Definition: abg-ir.cc:20874
void append_parameter(parameter_sptr parm)
Append a parameter to the type of this function.
Definition: abg-ir.cc:20881
void set_symbol(const elf_symbol_sptr &sym)
This sets the underlying ELF symbol for the current function decl.
Definition: abg-ir.cc:20840
virtual ~function_decl()
Destructor of the function_decl type.
Definition: abg-ir.cc:21209
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:20856
virtual bool operator==(const decl_base &o) const
Comparison operator for function_decl.
Definition: abg-ir.cc:21098
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3051
virtual size_t get_hash() const
The virtual implementation of 'get_hash' for a function_decl.
Definition: abg-ir.cc:21124
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:20674
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:21140
Abstract a function template declaration.
Definition: abg-ir.h:3693
binding get_binding() const
Get the binding of the function template.
Definition: abg-ir.cc:25971
void set_pattern(shared_ptr< function_decl > p)
Set a new pattern to the function template.
Definition: abg-ir.cc:25953
shared_ptr< function_decl > get_pattern() const
Get the pattern of the function template.
Definition: abg-ir.cc:25964
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:26031
virtual bool operator==(const decl_base &) const
Comparison operator for the function_tdecl type.
Definition: abg-ir.cc:25980
Abstraction of a function type.
Definition: abg-ir.h:3300
shared_ptr< function_decl::parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition: abg-ir.h:3306
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:20317
bool is_variadic() const
Test if the current instance of function_type is for a variadic function.
Definition: abg-ir.cc:20002
parameters::const_iterator get_first_parm() const
Get the first parameter of the function.
Definition: abg-ir.cc:20213
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:19816
virtual bool operator==(const type_base &) const
Equality operator for function_type.
Definition: abg-ir.cc:20276
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:19987
void set_parameters(const parameters &p)
Setter for the parameters of the current instance of function_type.
Definition: abg-ir.cc:19964
const interned_string & get_cached_name(bool internal=false) const
Get the name of the current function_type.
Definition: abg-ir.cc:20233
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:19943
type_base_sptr get_return_type() const
Getter for the return type of the current instance of function_type.
Definition: abg-ir.cc:19906
void set_return_type(type_base_sptr t)
Setter of the return type of the current instance of function_type.
Definition: abg-ir.cc:19914
parameters::const_iterator get_first_non_implicit_parm() const
Get the first parameter of the function.
Definition: abg-ir.cc:20191
const parameters & get_parameters() const
Getter for the set of parameters of the current intance of function_type.
Definition: abg-ir.cc:19923
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3312
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:20300
This abstracts the global scope of a given translation unit.
Definition: abg-ir.h:1930
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:15391
string to_string(bool internal=false) const
Return the string representation of the current instance of integral_type.
Definition: abg-ir.cc:15414
base_type get_base_type() const
Getter of the base type of the integral_type.
Definition: abg-ir.cc:15377
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:15401
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:15384
integral_type()
Default constructor of the integral_type.
Definition: abg-ir.cc:15347
The base class for the visitor type hierarchy used for traversing a translation unit.
Definition: abg-ir.h:4861
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:26973
bool type_node_has_been_visited(type_base *) const
Test if a given type node has been marked as visited.
Definition: abg-ir.cc:27017
void forget_visited_type_nodes()
Un-mark all visited type nodes.
Definition: abg-ir.cc:27007
ir_node_visitor()
Default Constructor of the ir_node_visitor type.
Definition: abg-ir.cc:26952
void mark_type_node_as_visited(type_base *)
Mark a given type node as having been visited.
Definition: abg-ir.cc:26983
The entry point to manage locations.
Definition: abg-ir.h:432
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:448
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:471
The source location of a token.
Definition: abg-ir.h:290
bool get_is_artificial() const
Test if the location is artificial.
Definition: abg-ir.h:331
unsigned get_value() const
Get the value of the location.
Definition: abg-ir.h:378
string expand(void) const
Expand the location into a string.
Definition: abg-ir.cc:413
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:393
Abstraction of a member function context relationship. This relates a member function to its parent c...
Definition: abg-ir.h:4466
bool is_constructor() const
Getter for the 'is-constructor' property.
Definition: abg-ir.h:4544
bool is_const() const
Getter for the 'is-const' property.
Definition: abg-ir.h:4579
size_t vtable_offset() const
Getter for the vtable offset property.
Definition: abg-ir.h:4524
bool is_destructor() const
Getter for the 'is-destructor' property.
Definition: abg-ir.h:4561
The base class for member types, data members and member functions. Its purpose is mainly to carry th...
Definition: abg-ir.h:3788
access_specifier get_access_specifier() const
Getter for the access specifier of this member.
Definition: abg-ir.h:3809
bool get_is_static() const
Definition: abg-ir.h:3821
Abstracts a member class template template.
Definition: abg-ir.h:4667
virtual bool operator==(const member_base &o) const
Equality operator of the the member_class_template class.
Definition: abg-ir.cc:24607
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:24692
Abstract a member function template.
Definition: abg-ir.h:4612
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:24586
Abstraction of the declaration of a method.
Definition: abg-ir.h:3837
friend void set_member_function_is_const(function_decl &, bool)
set the const-ness property of a member function.
Definition: abg-ir.cc:6518
virtual void set_linkage_name(const string &)
Set the linkage name of the method.
Definition: abg-ir.cc:23544
const method_type_sptr get_type() const
Definition: abg-ir.cc:23562
Abstracts the type of a class member function.
Definition: abg-ir.h:3396
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:20504
void set_is_const(bool)
Setter of the "is-const" property of method_type.
Definition: abg-ir.cc:20536
virtual ~method_type()
The destructor of method_type.
Definition: abg-ir.cc:20547
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:20528
class_or_union_sptr get_class_type() const
Get the class type this method belongs to.
Definition: abg-ir.cc:20495
bool get_is_const() const
Getter of the "is-const" property of method_type.
Definition: abg-ir.cc:20543
The abstraction of a namespace declaration.
Definition: abg-ir.h:2175
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:15992
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:16023
namespace_decl(const environment &env, const string &name, const location &locus, visibility vis=VISIBILITY_DEFAULT)
Constructor.
Definition: abg-ir.cc:15926
virtual bool operator==(const decl_base &) const
Return true iff both namespaces and their members are equal.
Definition: abg-ir.cc:15978
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:15964
Abstracts non type template parameters.
Definition: abg-ir.h:3570
const type_base_sptr get_type() const
Getter for the type of the template parameter.
Definition: abg-ir.cc:25650
virtual bool operator==(const decl_base &) const
Return true iff the two decls have the same name.
Definition: abg-ir.cc:25664
virtual size_t get_hash() const
Get the hash value of the current instance.
Definition: abg-ir.cc:25657
The abstraction of a pointer type.
Definition: abg-ir.h:2315
void set_pointed_to_type(const type_base_sptr &)
Set the pointed-to type of the pointer.
Definition: abg-ir.cc:16656
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:16790
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:16587
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:16873
virtual bool operator==(const decl_base &) const
Return true iff both instances of pointer_type_def are equal.
Definition: abg-ir.cc:16726
const type_base_sptr get_pointed_to_type() const
Getter of the pointed-to type.
Definition: abg-ir.cc:16770
type_base * get_naked_pointed_to_type() const
Getter of a naked pointer to the pointed-to type.
Definition: abg-ir.cc:16777
The abstraction of a qualified type.
Definition: abg-ir.h:2204
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:16314
void set_underlying_type(const type_base_sptr &)
Setter of the underlying type.
Definition: abg-ir.cc:16438
virtual size_t get_size_in_bits() const
Get the size of the qualified type def.
Definition: abg-ir.cc:16178
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:16426
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition: abg-ir.h:2223
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:16118
void set_cv_quals(CV cv_quals)
Setter of the const/value qualifiers bit field.
Definition: abg-ir.cc:16417
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:16386
CV get_cv_quals() const
Getter of the const/volatile qualifier bit field.
Definition: abg-ir.cc:16412
type_base_sptr get_underlying_type() const
Getter of the underlying type.
Definition: abg-ir.cc:16431
virtual bool operator==(const decl_base &) const
Equality operator for qualified types.
Definition: abg-ir.cc:16258
string build_name(bool, bool internal=false) const
Build the name of the current instance of qualified type.
Definition: abg-ir.cc:16095
Abstracts a reference type.
Definition: abg-ir.h:2378
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:17172
virtual void on_canonical_type_set()
This function is automatically invoked whenever an instance of this type is canonicalized.
Definition: abg-ir.cc:16946
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:17238
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:17044
virtual bool operator==(const decl_base &) const
Equality operator of the reference_type_def type.
Definition: abg-ir.cc:17114
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:17217
A declaration that introduces a scope.
Definition: abg-ir.h:1787
virtual size_t get_num_anonymous_member_classes() const
Getter for the number of anonymous classes contained in this scope.
Definition: abg-ir.cc:7802
void remove_member_type(type_base_sptr t)
Remove a member type from the current class_or_union scope.
Definition: abg-ir.cc:7982
void insert_member_type(type_base_sptr t, declarations::iterator before)
Insert a member type.
Definition: abg-ir.cc:7941
void add_member_type(type_base_sptr t)
Add a member type to the current instance of class_or_union.
Definition: abg-ir.cc:7957
virtual size_t get_num_anonymous_member_unions() const
Getter for the number of anonymous unions contained in this scope.
Definition: abg-ir.cc:7820
scopes & get_member_scopes()
Getter for the scopes carried by the current scope.
Definition: abg-ir.cc:7855
std::vector< scope_decl_sptr > scopes
Convenience typedef for a vector of scope_decl_sptr.
Definition: abg-ir.h:1798
bool is_empty() const
Test if the current scope is empty.
Definition: abg-ir.cc:7869
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:8264
const type_base_sptrs_type & get_member_types() const
Get the member types of this scope_decl.
Definition: abg-ir.cc:7916
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:8028
std::vector< decl_base_sptr > declarations
Convenience typedef for a vector of decl_base_sptr.
Definition: abg-ir.h:1794
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:7738
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:8216
virtual void remove_member_decl(decl_base_sptr member)
Remove a declaration from the current scope.
Definition: abg-ir.cc:8058
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:7885
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:7927
const declarations & get_member_decls() const
Getter for the member declarations carried by the current scope_decl.
Definition: abg-ir.cc:7762
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:7726
const type_base_sptrs_type & get_sorted_member_types() const
Get the sorted member types of this scope_decl.
Definition: abg-ir.cc:8001
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.
Definition: abg-ir.cc:8293
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:8170
const declarations & get_sorted_member_decls() const
Getter for the sorted member declarations carried by the current scope_decl.
Definition: abg-ir.cc:7780
virtual size_t get_num_anonymous_member_enums() const
Getter for the number of anonymous enums contained in this scope.
Definition: abg-ir.cc:7838
virtual size_t get_hash() const
Return the hash value for the current instance of scope_decl.
Definition: abg-ir.cc:8095
A type that introduces a scope.
Definition: abg-ir.h:2149
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:15885
virtual bool operator==(const decl_base &) const
Equality operator between two scope_type_decl.
Definition: abg-ir.cc:15847
The base class of templates.
Definition: abg-ir.h:3452
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:25318
virtual ~template_decl()
Destructor.
Definition: abg-ir.cc:25343
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:25310
virtual bool operator==(const decl_base &o) const
Equality operator.
Definition: abg-ir.cc:25352
Base class for a template parameter. Client code should use the more specialized type_template_parame...
Definition: abg-ir.h:3487
virtual ~template_parameter()
Destructor.
Definition: abg-ir.cc:25479
bool operator!=(const template_parameter &) const
Inequality operator.
Definition: abg-ir.cc:25475
Abstracts a template template parameter.
Definition: abg-ir.h:3617
virtual bool operator==(const type_base &) const
Equality operator.
Definition: abg-ir.cc:25737
This is the abstraction of the set of relevant artefacts (types, variable declarations,...
Definition: abg-ir.h:663
void set_address_size(char)
Setter of the address size in this translation unit.
Definition: abg-ir.cc:1393
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:1308
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:1425
bool operator==(const translation_unit &) const
Compare the current translation unit against another one.
Definition: abg-ir.cc:1435
const corpus * get_corpus() const
Get the corpus this translation unit is a member of.
Definition: abg-ir.cc:1351
char get_address_size() const
Getter of the address size in this translation unit.
Definition: abg-ir.cc:1386
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:1289
void set_corpus(corpus *)
Set the corpus this translation unit is a member of.
Definition: abg-ir.cc:1335
void set_language(language l)
Setter of the language of the source code of the translation unit.
Definition: abg-ir.cc:1252
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:1461
const scope_decl_sptr & get_global_scope() const
Getter of the the global scope of the translation unit.
Definition: abg-ir.cc:1188
bool is_empty() const
Tests whether if the current translation unit contains ABI artifacts or not.
Definition: abg-ir.cc:1375
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:1409
const std::string & get_path() const
Get the path of the current translation unit.
Definition: abg-ir.cc:1265
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:1300
location_manager & get_loc_mgr()
Getter of the location manager for the current translation unit.
Definition: abg-ir.cc:1359
void set_path(const string &)
Set the path associated to the current instance of translation_unit.
Definition: abg-ir.cc:1276
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse virtual function.
Definition: abg-ir.cc:1493
language
The language of the translation unit.
Definition: abg-ir.h:676
bool operator!=(const translation_unit &) const
Inequality operator.
Definition: abg-ir.cc:1450
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:1231
const environment & get_environment() const
Getter of the environment of the current translation_unit.
Definition: abg-ir.cc:1238
const type_maps & get_types() const
Getter of the types of the current translation_unit.
Definition: abg-ir.cc:1215
language get_language() const
Getter of the language of the source code of the translation unit.
Definition: abg-ir.cc:1245
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:1951
const interned_string & get_cached_pretty_representation(bool internal=false) const
Get the pretty representation of the current type.
Definition: abg-ir.cc:14987
type_base * get_naked_canonical_type() const
Getter of the canonical type pointer.
Definition: abg-ir.cc:14970
virtual size_t get_size_in_bits() const
Getter for the size of the type.
Definition: abg-ir.cc:15066
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:15092
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:14749
virtual void set_size_in_bits(size_t)
Setter for the size of the type.
Definition: abg-ir.cc:15059
virtual bool operator!=(const type_base &) const
Inequality operator.
Definition: abg-ir.cc:15052
virtual bool operator==(const type_base &) const
Return true iff both type declarations are equal.
Definition: abg-ir.cc:15042
virtual size_t get_alignment_in_bits() const
Getter for the alignment of the type.
Definition: abg-ir.cc:15080
virtual void set_alignment_in_bits(size_t)
Setter for the alignment of the type.
Definition: abg-ir.cc:15073
type_base_sptr get_canonical_type() const
Getter of the canonical type of the current instance of type_base.
Definition: abg-ir.cc:14954
This abstracts a composition of types based on template type parameters. The result of the compositio...
Definition: abg-ir.h:3655
const type_base_sptr get_composed_type() const
Getter for the resulting composed type.
Definition: abg-ir.cc:25843
void set_composed_type(type_base_sptr t)
Setter for the resulting composed type.
Definition: abg-ir.cc:25850
virtual size_t get_hash() const
Get the hash value for the current instance.
Definition: abg-ir.cc:25857
A basic type declaration that introduces no scope.
Definition: abg-ir.h:2086
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:15679
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:15758
virtual bool operator!=(const type_base &) const
Return true if both types equals.
Definition: abg-ir.cc:15617
virtual bool operator==(const type_base &) const
Return true if both types equals.
Definition: abg-ir.cc:15573
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:15738
This is a type that aggregates maps of all the kinds of types that are supported by libabigail.
Definition: abg-ir.h:576
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:591
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:680
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:631
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:549
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:563
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:645
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:577
bool empty() const
Test if the type_maps is empty.
Definition: abg-ir.cc:517
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:604
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:1124
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:617
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:535
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:666
The base class of both types and declarations.
Definition: abg-ir.h:1345
void set_translation_unit(translation_unit *)
Set the translation_unit this ABI artifact belongs to.
Definition: abg-ir.cc:4331
bool get_is_artificial() const
Getter of the flag that says if the artefact is artificial.
Definition: abg-ir.cc:4145
virtual ~type_or_decl_base()
The destructor of the type_or_decl_base type.
Definition: abg-ir.cc:4134
location & get_artificial_location() const
Getter of the artificial location of the artifact.
Definition: abg-ir.cc:4291
bool has_artificial_location() const
Test if the current ABI artifact carries an artificial location.
Definition: abg-ir.cc:4298
friend type_base * is_type(const type_or_decl_base *)
Test whether a declaration is a type.
Definition: abg-ir.cc:10207
const corpus * get_corpus() const
Get the corpus this ABI artifact belongs to.
Definition: abg-ir.cc:4323
friend class_decl * is_class_type(const type_or_decl_base *)
Test whether a type is a class.
Definition: abg-ir.cc:10458
enum type_or_decl_kind kind() const
Getter for the "kind" property of type_or_decl_base type.
Definition: abg-ir.cc:4168
void set_is_artificial(bool)
Setter of the flag that says if the artefact is artificial.
Definition: abg-ir.cc:4157
virtual bool traverse(ir_node_visitor &)
Traverse the the ABI artifact.
Definition: abg-ir.cc:4356
const void * runtime_type_instance() const
Getter of the pointer to the runtime type sub-object of the current instance.
Definition: abg-ir.cc:4188
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:4223
bool hashing_started() const
Getter for the 'hashing_started' property.
Definition: abg-ir.cc:4241
void set_artificial_location(const location &)
Setter of the artificial location of the artificat.
Definition: abg-ir.cc:4273
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:1359
const environment & get_environment() const
Getter of the environment of the current ABI artifact.
Definition: abg-ir.cc:4255
friend pointer_type_def * is_pointer_type(type_or_decl_base *)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:10558
friend decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10134
const translation_unit * get_translation_unit() const
Get the translation_unit this ABI artifact belongs to.
Definition: abg-ir.cc:4348
Abstracts a type template parameter.
Definition: abg-ir.h:3533
virtual bool operator==(const type_base &) const
Equality operator.
Definition: abg-ir.cc:25521
The abstraction of a typedef declaration.
Definition: abg-ir.h:2809
void set_underlying_type(const type_base_sptr &)
Setter ofthe underlying type of the typedef.
Definition: abg-ir.cc:19207
virtual size_t get_size_in_bits() const
Return the size of the typedef.
Definition: abg-ir.cc:19062
virtual bool traverse(ir_node_visitor &)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:19222
type_base_sptr get_underlying_type() const
Getter of the underlying type of the typedef.
Definition: abg-ir.cc:19200
virtual bool operator==(const decl_base &) const
Equality operator.
Definition: abg-ir.cc:19142
virtual size_t get_alignment_in_bits() const
Return the alignment of the typedef.
Definition: abg-ir.cc:19079
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:19183
Abstracts a union type declaration.
Definition: abg-ir.h:4396
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:25112
virtual bool operator==(const decl_base &) const
Comparison operator for union_decl.
Definition: abg-ir.cc:25052
virtual ~union_decl()
Destructor of the union_decl type.
Definition: abg-ir.cc:25185
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:25019
Abstracts a variable declaration.
Definition: abg-ir.h:2921
binding get_binding() const
Getter of the binding of the variable.
Definition: abg-ir.cc:19336
void set_type(type_base_sptr &)
Setter of the type of the variable.
Definition: abg-ir.cc:19318
void set_binding(binding b)
Setter of the binding of the variable.
Definition: abg-ir.cc:19343
friend uint64_t get_data_member_offset(const var_decl_sptr m)
Get the offset of a data member.
Definition: abg-ir.cc:6179
var_decl_sptr clone() const
Create a new var_decl that is a clone of the current one.
Definition: abg-ir.cc:19381
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:19644
const type_base * get_naked_type() const
Getter of the type of the variable.
Definition: abg-ir.cc:19329
friend bool get_data_member_is_laid_out(const var_decl &m)
Test whether a data member is laid out.
Definition: abg-ir.cc:6324
const type_base_sptr get_type() const
Getter of the type of the variable.
Definition: abg-ir.cc:19311
virtual bool traverse(ir_node_visitor &v)
This implements the ir_traversable_base::traverse pure virtual function.
Definition: abg-ir.cc:19788
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:19765
void set_symbol(const elf_symbol_sptr &sym)
Sets the underlying ELF symbol for the current variable.
Definition: abg-ir.cc:19358
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:19374
virtual bool operator==(const decl_base &) const
Comparison operator of var_decl.
Definition: abg-ir.cc:19569
virtual size_t get_hash() const
Return the hash value for the current instance.
Definition: abg-ir.cc:19611
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:19674
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:19588
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...
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition: abg-fwd.h:229
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:18438
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:12677
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:11775
bool is_non_canonicalized_type(const type_base *t)
Test if a given type is allowed to be non canonicalized.
Definition: abg-ir.cc:26449
bool get_member_function_is_dtor(const function_decl &f)
Test whether a member function is a destructor.
Definition: abg-ir.cc:6434
const type_base * peel_qualified_type(const type_base *type)
Return the leaf underlying type of a qualified type.
Definition: abg-ir.cc:7195
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition: abg-fwd.h:215
translation_unit * get_translation_unit(const shared_ptr< decl_base > decl)
Return the translation unit a declaration belongs to.
Definition: abg-ir.cc:9930
size_t hash_type(const type_base *t)
Hash an ABI artifact that is either a type.
Definition: abg-ir.cc:26426
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:5854
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:6134
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:8310
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:10101
class_decl_sptr is_class_type(const type_or_decl_base_sptr &d)
Test whether a type is a class.
Definition: abg-ir.cc:10476
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:5648
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:9743
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:9726
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition: abg-fwd.h:263
access_specifier
Access specifier for class members.
Definition: abg-ir.h:856
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:13111
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:12592
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:8825
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:13949
bool function_decls_alias(const function_decl &f1, const function_decl &f2)
Test if two function declarations are aliases.
Definition: abg-ir.cc:21175
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:213
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:13005
void set_member_function_is_virtual(function_decl &f, bool is_virtual)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6659
ssize_t get_member_function_vtable_offset(const function_decl &f)
Get the vtable offset of a member function.
Definition: abg-ir.cc:6558
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:13160
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:12518
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:10440
bool is_declaration_only_class_or_union_type(const type_base *t)
Test wheter a type is a declaration-only class.
Definition: abg-ir.cc:10486
corpus::origin operator|=(corpus::origin &l, corpus::origin r)
Bitwise |= operator for the corpus::origin type.
Definition: abg-corpus.cc:1616
vector< type_base_wptr > type_base_wptrs_type
A convenience typedef for a vector of type_base_wptr.
Definition: abg-fwd.h:143
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:25268
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:23793
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:266
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:18058
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:12859
qualified_type_def_sptr clone_qualified_type(const qualified_type_def_sptr &t)
Clone a qualifiend type.
Definition: abg-ir.cc:7497
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:9490
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10194
scope_decl * is_scope_decl(decl_base *d)
Test if a declaration is a scope_decl.
Definition: abg-ir.cc:5518
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:12765
bool is_anonymous_data_member(const decl_base &d)
Test if a decl is an anonymous data member.
Definition: abg-ir.cc:5877
bool is_anonymous_data_member(const var_decl &d)
Test if a var_decl is an anonymous data member.
Definition: abg-ir.cc:5979
string translation_unit_language_to_string(translation_unit::language l)
Converts a translation_unit::language enumerator into a string.
Definition: abg-ir.cc:1505
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:10316
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:7104
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5472
scope_decl * get_type_scope(const type_base_sptr &t)
Get the scope of a given type.
Definition: abg-ir.cc:8660
integral_type::modifiers_type operator~(integral_type::modifiers_type l)
Bitwise one's complement operator for integral_type::modifiers_type.
Definition: abg-ir.cc:15149
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:11032
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:863
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:12947
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:12402
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:9640
type_base * get_exemplar_type(const type_base *type)
For a given type, return its exemplar type.
Definition: abg-ir.cc:26472
location get_location(const decl_base_sptr &decl)
Get the location of a given declaration.
Definition: abg-ir.cc:8622
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8317
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:7268
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:9808
array_type_def_sptr clone_array(const array_type_def_sptr &array)
Clone an array type.
Definition: abg-ir.cc:7430
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:12890
change_kind
A bitfield that gives callers of abigail::ir::equals() some insight about how different two internal ...
Definition: abg-ir.h:1300
@ LOCAL_TYPE_CHANGE_KIND
This means that a given IR artifact has a local type change.
Definition: abg-ir.h:1304
@ 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:1320
@ 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:1309
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:26920
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:5791
bool is_user_defined_type(const type_base *t)
Test if a type is user-defined.
Definition: abg-ir.cc:5552
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:1688
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:6063
weak_ptr< class_decl > class_decl_wptr
Convenience typedef for a weak pointer on a class_decl.
Definition: abg-fwd.h:199
const interned_string & get_node_name(var_decl_sptr node)
Gets the name of a var_decl node.
Definition: abg-ir.cc:11842
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:9793
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition: abg-ir.cc:10449
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:14049
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:13515
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:234
method_type * is_method_type(type_or_decl_base *t)
Test whether a type is a method_type.
Definition: abg-ir.cc:10739
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:12427
pointer_type_def_sptr is_pointer_type(const type_or_decl_base_sptr &t)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:10589
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:13065
bool is_global_scope(const shared_ptr< scope_decl >scope)
Tests whether if a given scope is the global scope.
Definition: abg-ir.cc:9958
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:13388
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:9144
bool scope_anonymous_or_typedef_named(const decl_base &d)
Test if the scope of a given decl is anonymous or anonymous with a naming typedef.
Definition: abg-ir.cc:6120
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:24501
const type_base * is_void_pointer_type(const type_base &type)
Test if a type is a pointer to void type.
Definition: abg-ir.cc:10659
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:13030
void mark_types_as_being_compared(T &l, T &r)
Mark a pair of types as being compared.
Definition: abg-ir.cc:931
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:7217
array_type_def * is_array_type(const type_or_decl_base *type)
Test if a type is an array_type_def.
Definition: abg-ir.cc:10954
bool is_at_template_scope(const shared_ptr< decl_base > decl)
Tests whether a given decl is at template scope.
Definition: abg-ir.cc:10059
reference_type_def * is_reference_type(type_or_decl_base *t)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:10599
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:13281
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:6022
type_decl * is_integral_type(const type_or_decl_base *t)
Test if a type is an integral type.
Definition: abg-ir.cc:10296
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:6078
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6462
const function_type * is_function_type(const type_or_decl_base *t)
Test whether a type is a function_type.
Definition: abg-ir.cc:10709
const type_base_sptr peel_array_type(const type_base_sptr &type)
Return the leaf element type of an array.
Definition: abg-ir.cc:7153
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:13453
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:26616
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:10789
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:10509
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:10874
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition: abg-fwd.h:187
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:7291
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:6518
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:8561
bool is_anonymous_type(const type_base_sptr &t)
Test if a given type is anonymous.
Definition: abg-ir.cc:10270
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:6846
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:2878
namespace_decl_sptr is_namespace(const decl_base_sptr &d)
Tests if a declaration is a namespace declaration.
Definition: abg-ir.cc:10909
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:10988
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:5815
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:6405
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:10278
decl_base * is_decl_slow(const type_or_decl_base *t)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10174
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:10798
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:10689
scope_decl_sptr is_scope_decl(const decl_base_sptr &d)
Test if a declaration is a scope_decl.
Definition: abg-ir.cc:5528
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:8529
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5617
bool is_template_parameter(const shared_ptr< decl_base > decl)
Tests whether a decl is a template parameter.
Definition: abg-ir.cc:10068
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10336
type_base_sptr is_type(const type_or_decl_base_sptr &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10222
uint64_t get_var_size_in_bits(const var_decl_sptr &v)
Get the size of a given variable.
Definition: abg-ir.cc:6296
string get_debug_representation(const type_or_decl_base *artifact)
Get the textual representation of a type for debugging purposes.
Definition: abg-ir.cc:9512
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition: abg-fwd.h:205
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition: abg-fwd.h:161
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:14132
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:2750
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:7048
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:19442
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:12377
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:3508
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:7393
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:10185
corpus::origin operator|(corpus::origin l, corpus::origin r)
Bitwise | operator for the corpus::origin type.
Definition: abg-corpus.cc:1602
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:2959
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition: abg-ir.cc:1651
bool try_canonical_compare(const T *l, const T *r)
Compare two types by comparing their canonical types if present.
Definition: abg-ir.cc:818
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:10390
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:10124
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:10769
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:12630
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:6547
bool parse_integral_type(const string &type_name, integral_type &type)
Parse an integral type from a string.
Definition: abg-ir.cc:15332
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:7021
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:3179
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:12117
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:10399
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:8415
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:10519
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:26564
bool elf_symbols_alias(const elf_symbol &s1, const elf_symbol &s2)
Test if two symbols alias.
Definition: abg-ir.cc:2681
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:10037
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:246
const location & get_natural_or_artificial_location(const decl_base *decl)
Get the non-artificial (natural) location of a decl.
Definition: abg-ir.cc:9653
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:8598
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:26347
corpus::origin operator&(corpus::origin l, corpus::origin r)
Bitwise & operator for the corpus::origin type.
Definition: abg-corpus.cc:1630
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:27257
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition: abg-fwd.h:258
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:12724
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:2911
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
namespace_decl * is_namespace(const decl_base *d)
Tests if a declaration is a namespace declaration.
Definition: abg-ir.cc:10918
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:7354
lookup_entity_kind
This enum describe the kind of entity to lookup, while using the lookup API.
Definition: abg-ir.cc:11061
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:26284
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:10941
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:245
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition: abg-ir.cc:1665
function_decl::parameter * is_function_parameter(const type_or_decl_base *tod)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10111
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:12537
class_or_union * is_at_class_scope(const decl_base &decl)
Tests whether a given decl is at class scope.
Definition: abg-ir.cc:10017
bool equals(const decl_base &l, const decl_base &r, change_kind *k)
Compares two instances of decl_base.
Definition: abg-ir.cc:5199
bool get_data_member_is_laid_out(const var_decl &m)
Test whether a data member is laid out.
Definition: abg-ir.cc:6324
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:10927
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:12084
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:2936
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:11389
bool is_declaration_only_class_type(const type_base_sptr &t)
Test wheter a type is a declaration-only class.
Definition: abg-ir.cc:10499
type_base_sptr canonicalize(type_base_sptr t)
Compute the canonical type of a given type.
Definition: abg-ir.cc:14839
weak_ptr< elf_symbol > elf_symbol_wptr
A convenience typedef for a weak pointer to elf_symbol.
Definition: abg-ir.h:868
bool get_member_function_is_const(const function_decl &f)
Test whether a member function is const.
Definition: abg-ir.cc:6490
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:8862
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition: abg-fwd.h:220
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:13545
pointer_type_def * is_pointer_type(type_or_decl_base *t)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:10558
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:9081
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:1575
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:10408
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:13335
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:6209
uint64_t get_absolute_data_member_offset(const var_decl &m)
Get the absolute offset of a data member.
Definition: abg-ir.cc:6253
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:6348
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:26852
var_decl * is_var_decl(const type_or_decl_base *tod)
Tests if a declaration is a variable declaration.
Definition: abg-ir.cc:10890
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1637
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10134
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:26322
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:23624
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5537
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:9366
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:11045
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.
Definition: abg-ir.cc:8293
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:26874
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5588
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition: abg-fwd.h:169
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:6147
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:6711
uint64_t get_data_member_offset(const var_decl &m)
Get the offset of a data member.
Definition: abg-ir.cc:6164
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:10286
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:9994
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6621
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:9899
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:5994
location get_location(const type_base_sptr &type)
Get the location of the declaration of a given type.
Definition: abg-ir.cc:8609
weak_ptr< decl_base > decl_base_wptr
Convenience typedef for a weak pointer to a decl_base.
Definition: abg-fwd.h:178
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:8337
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:8939
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:1016
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:10749
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:10528
const location & get_artificial_or_natural_location(const decl_base *decl)
Get the artificial location of a decl.
Definition: abg-ir.cc:9672
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:12985
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:6994
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:8892
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:11011
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:8499
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:6310
array_type_def_sptr is_array_type(const type_or_decl_base_sptr &type)
Test if a type is an array_type_def.
Definition: abg-ir.cc:10963
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5686
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:26899
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:10548
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:9827
bool is_at_global_scope(const decl_base *decl)
Tests whether a given declaration is at global scope.
Definition: abg-ir.cc:9985
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:24747
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:26830
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition: abg-fwd.h:156
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:867
typedef_decl_sptr clone_typedef(const typedef_decl_sptr &t)
Clone a typedef type.
Definition: abg-ir.cc:7472
string get_pretty_representation(const method_type_sptr method, bool internal)
Get the pretty representation of a method type.
Definition: abg-ir.cc:9346
typedef_decl * is_typedef(type_base *t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10356
void unmark_types_as_being_compared(T &l, T &r)
Mark a pair of types as being not compared anymore.
Definition: abg-ir.cc:966
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
bool is_template_decl(const shared_ptr< decl_base > decl)
Tests whether a decl is a template.
Definition: abg-ir.cc:11055
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:13210
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:10900
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition: abg-ir.cc:1674
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:14206
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:11152
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:8678
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:330
corpus::origin operator&=(corpus::origin &l, corpus::origin r)
Bitwise &= operator for the corpus::origin type.
Definition: abg-corpus.cc:1644
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:7550
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:8976
decl_base_sptr is_decl(const type_or_decl_base_sptr &d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10163
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:1707
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:8840
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10082
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:2969
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:26306
reference_type_def_sptr is_reference_type(const type_or_decl_base_sptr &t)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:10619
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:10719
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:10669
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:8486
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:886
interned_string get_function_id_or_pretty_representation(function_decl *fn)
Get the ID of a function, or, if the ID can designate several different functions,...
Definition: abg-ir.cc:9018
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:8575
void set_member_function_vtable_offset(function_decl &f, ssize_t s)
Set the vtable offset of a member function.
Definition: abg-ir.cc:6591
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:22631
decl_base_sptr get_type_declaration(const type_base_sptr t)
Get the declaration for a given type.
Definition: abg-ir.cc:9845
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:7240
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:14236
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:12692
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5490
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:5100
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:10679
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:12137
const type_base * is_void_pointer_type(const type_base *type)
Test if a type is a pointer to void type.
Definition: abg-ir.cc:10632
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:11126
bool get_member_function_is_ctor(const function_decl &f)
Test whether a member function is a constructor.
Definition: abg-ir.cc:6375
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:12822
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:186
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition: abg-ir.cc:152
std::ostream & operator<<(std::ostream &o, const interned_string &s)
Streaming operator.
Definition: abg-ir.cc:169
unordered_map< string, string * > pool_map_type
Convenience typedef for a map of string -> string*.
Definition: abg-ir.cc:74
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:7665
The hashing functor for class_decl::base_spec.
Definition: abg-ir.h:4794
Hasher for the class_decl type.
Definition: abg-ir.h:4299
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:4760
A hashing functor for a function_decl::parameter.
Definition: abg-ir.h:3278
Equality functor for instances of function_decl.
Definition: abg-ir.h:4770
The hashing functor for function_type.
Definition: abg-ir.h:3383
The type of the private data of the function_type type.
Definition: abg-ir-priv.h:1519
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:26935
The hashing functor for member_base.
Definition: abg-ir.h:4801
Hasher for the non_type_tparameter type.
Definition: abg-ir.h:3605
Hasher for the scope_decl type.
Definition: abg-ir.h:1916
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:3682
A predicate for deep equality of instances of shared_ptr<type_base>
Definition: abg-ir.h:2064
A hashing functor for instances and pointers of var_decl.
Definition: abg-ir.h:4729
A deleter for shared pointers that ... doesn't delete the object managed by the shared pointer.