1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 Red Hat, Inc.
5 
6 /// @file
7 
8 #include <functional>
9 #include "abg-internal.h"
10 // <headers defining libabigail's API go under here>
11 ABG_BEGIN_EXPORT_DECLARATIONS
12 
13 #include "abg-hash.h"
14 #include "abg-ir.h"
15 
16 ABG_END_EXPORT_DECLARATIONS
17 // </headers defining libabigail's API>
18 
19 namespace abigail
20 {
21 
22 namespace hashing
23 {
24 
25 // Mix 3 32 bits values reversibly.  Borrowed from hashtab.c in gcc tree.
26 #define abigail_hash_mix(a, b, c) \
27 { \
28   a -= b; a -= c; a ^= (c>>13); \
29   b -= c; b -= a; b ^= (a<< 8); \
30   c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
31   a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
32   b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
33   c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
34   a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
35   b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
36   c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
37 }
38 
39 size_t
combine_hashes(size_t val1,size_t val2)40 combine_hashes(size_t val1, size_t val2)
41 {
42   /* the golden ratio; an arbitrary value.  */
43   size_t a = 0x9e3779b9;
44   abigail_hash_mix(a, val1, val2);
45   return val2;
46 }
47 
48 /// Compute a stable string hash.
49 ///
50 /// std::hash has no portability or stability guarantees so is
51 /// unsuitable where reproducibility is a requirement such as in XML
52 /// output.
53 ///
54 /// This is the 32-bit FNV-1a algorithm. The algorithm, reference code
55 /// and constants are all unencumbered. It is fast and has reasonable
56 /// distribution properties.
57 ///
58 /// https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function
59 ///
60 /// @param str the string to hash.
61 ///
62 /// @return an unsigned 32 bit hash value.
63 uint32_t
fnv_hash(const std::string & str)64 fnv_hash(const std::string& str)
65 {
66   const uint32_t prime = 0x01000193;
67   const uint32_t offset_basis = 0x811c9dc5;
68   uint32_t hash = offset_basis;
69   for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
70     {
71       uint8_t byte = *i;
72       hash = hash ^ byte;
73       hash = hash * prime;
74     }
75   return hash;
76 }
77 
78 }//end namespace hashing
79 
80 using std::list;
81 using std::vector;
82 
83 using namespace abigail::ir;
84 
85 // See forward declarations in abg-ir.h.
86 
87 // Definitions.
88 
89 /// Hash function for an instance of @ref type_base.
90 ///
91 /// @param t the type to hash.
92 ///
93 /// @return the type value.
94 size_t
operator ()(const type_base & t) const95 type_base::hash::operator()(const type_base& t) const
96 {
97   std::hash<size_t> size_t_hash;
98   std::hash<string> str_hash;
99 
100   size_t v = str_hash(typeid(t).name());
101   v = hashing::combine_hashes(v, size_t_hash(t.get_size_in_bits()));
102   v = hashing::combine_hashes(v, size_t_hash(t.get_alignment_in_bits()));
103 
104   return v;
105 }
106 
107 /// Hash function for an instance of @ref type_base.
108 ///
109 /// @param t the type to hash.
110 ///
111 /// @return the type value.
112 size_t
operator ()(const type_base * t) const113 type_base::hash::operator()(const type_base* t) const
114 {return operator()(*t);}
115 
116 /// Hash function for an instance of @ref type_base.
117 ///
118 /// @param t the type to hash.
119 ///
120 /// @return the type value.
121 size_t
operator ()(const type_base_sptr t) const122 type_base::hash::operator()(const type_base_sptr t) const
123 {return operator()(*t);}
124 
125 struct decl_base::hash
126 {
127   size_t
operator ()abigail::decl_base::hash128   operator()(const decl_base& d) const
129   {
130     std::hash<string> str_hash;
131 
132     size_t v = str_hash(typeid(d).name());
133     if (!d.get_linkage_name().empty())
134       v = hashing::combine_hashes(v, str_hash(d.get_linkage_name()));
135     if (!d.get_name().empty())
136       v = hashing::combine_hashes(v, str_hash(d.get_qualified_name()));
137     if (is_member_decl(d))
138       {
139 	v = hashing::combine_hashes(v, get_member_access_specifier(d));
140 	v = hashing::combine_hashes(v, get_member_is_static(d));
141       }
142     return v;
143   }
144 }; // end struct decl_base::hash
145 
146 struct type_decl::hash
147 {
148   size_t
operator ()abigail::type_decl::hash149   operator()(const type_decl& t) const
150   {
151     decl_base::hash decl_hash;
152     type_base::hash type_hash;
153     std::hash<string> str_hash;
154 
155     size_t v = str_hash(typeid(t).name());
156     v = hashing::combine_hashes(v, decl_hash(t));
157     v = hashing::combine_hashes(v, type_hash(t));
158 
159     return v;
160   }
161 };
162 
163 /// Hashing operator for the @ref scope_decl type.
164 ///
165 /// @param d the scope_decl to hash.
166 ///
167 /// @return the hash value.
168 size_t
operator ()(const scope_decl & d) const169 scope_decl::hash::operator()(const scope_decl& d) const
170 {
171   std::hash<string> hash_string;
172   size_t v = hash_string(typeid(d).name());
173   for (scope_decl::declarations::const_iterator i =
174 	 d.get_member_decls().begin();
175        i != d.get_member_decls().end();
176        ++i)
177     v = hashing::combine_hashes(v, (*i)->get_hash());
178 
179   return v;
180 }
181 
182 /// Hashing operator for the @ref scope_decl type.
183 ///
184 /// @param d the scope_decl to hash.
185 ///
186 /// @return the hash value.
187 size_t
operator ()(const scope_decl * d) const188 scope_decl::hash::operator()(const scope_decl* d) const
189 {return d? operator()(*d) : 0;}
190 
191 struct scope_type_decl::hash
192 {
193   size_t
operator ()abigail::scope_type_decl::hash194   operator()(const scope_type_decl& t) const
195   {
196     decl_base::hash decl_hash;
197     type_base::hash type_hash;
198     std::hash<string> str_hash;
199 
200     size_t v = str_hash(typeid(t).name());
201     v = hashing::combine_hashes(v, decl_hash(t));
202     v = hashing::combine_hashes(v, type_hash(t));
203 
204     return v;
205   }
206 };
207 
208 struct qualified_type_def::hash
209 {
210   size_t
operator ()abigail::qualified_type_def::hash211   operator()(const qualified_type_def& t) const
212   {
213     type_base::hash type_hash;
214     decl_base::hash decl_hash;
215     std::hash<string> str_hash;
216 
217     size_t v = str_hash(typeid(t).name());
218     v = hashing::combine_hashes(v, type_hash(t));
219     v = hashing::combine_hashes(v, decl_hash(t));
220     v = hashing::combine_hashes(v, t.get_cv_quals());
221     return v;
222   }
223 };
224 
225 struct pointer_type_def::hash
226 {
227   size_t
operator ()abigail::pointer_type_def::hash228   operator()(const pointer_type_def& t) const
229   {
230     std::hash<string> str_hash;
231     type_base::hash type_base_hash;
232     decl_base::hash decl_hash;
233     type_base::shared_ptr_hash hash_type_ptr;
234 
235     size_t v = str_hash(typeid(t).name());
236     v = hashing::combine_hashes(v, decl_hash(t));
237     v = hashing::combine_hashes(v, type_base_hash(t));
238     v = hashing::combine_hashes(v, hash_type_ptr(t.get_pointed_to_type()));
239     return v ;
240   }
241 };
242 
243 struct reference_type_def::hash
244 {
245   size_t
operator ()abigail::reference_type_def::hash246   operator()(const reference_type_def& t)
247   {
248     std::hash<string> hash_str;
249     type_base::hash hash_type_base;
250     decl_base::hash hash_decl;
251     type_base::shared_ptr_hash hash_type_ptr;
252 
253     size_t v = hash_str(typeid(t).name());
254     v = hashing::combine_hashes(v, hash_str(t.is_lvalue()
255 					    ? "lvalue"
256 					    : "rvalue"));
257     v = hashing::combine_hashes(v, hash_type_base(t));
258     v = hashing::combine_hashes(v, hash_decl(t));
259     v = hashing::combine_hashes(v, hash_type_ptr(t.get_pointed_to_type()));
260     return v;
261   }
262 };
263 
264 struct array_type_def::subrange_type::hash
265 {
266   size_t
operator ()abigail::array_type_def::subrange_type::hash267   operator()(const array_type_def::subrange_type& s) const
268   {
269     std::hash<int> hash_size_t;
270     size_t v = hash_size_t(hash_size_t(s.get_lower_bound()));
271     v = hashing::combine_hashes(v, hash_size_t(s.get_upper_bound()));
272     return v;
273   }
274 };
275 
276 struct array_type_def::hash
277 {
278   size_t
operator ()abigail::array_type_def::hash279   operator()(const array_type_def& t)
280   {
281     std::hash<string> hash_str;
282     type_base::hash hash_type_base;
283     decl_base::hash hash_decl;
284     type_base::shared_ptr_hash hash_type_ptr;
285     array_type_def::subrange_type::hash hash_subrange;
286 
287     size_t v = hash_str(typeid(t).name());
288 
289     v = hashing::combine_hashes(v, hash_type_base(t));
290     v = hashing::combine_hashes(v, hash_decl(t));
291     v = hashing::combine_hashes(v, hash_type_ptr(t.get_element_type()));
292 
293     for (vector<array_type_def::subrange_sptr >::const_iterator i =
294 	   t.get_subranges().begin();
295 	 i != t.get_subranges().end();
296 	 ++i)
297       v = hashing::combine_hashes(v, hash_subrange(**i));
298 
299     return v;
300   }
301 };
302 
303 struct enum_type_decl::hash
304 {
305   size_t
operator ()abigail::enum_type_decl::hash306   operator()(const enum_type_decl& t) const
307   {
308     std::hash<string> str_hash;
309     decl_base::hash decl_hash;
310     type_base::shared_ptr_hash type_ptr_hash;
311     std::hash<size_t> size_t_hash;
312 
313     size_t v = str_hash(typeid(t).name());
314     v = hashing::combine_hashes(v, decl_hash(t));
315     v = hashing::combine_hashes(v, type_ptr_hash(t.get_underlying_type()));
316     for (enum_type_decl::enumerators::const_iterator i =
317 	   t.get_enumerators().begin();
318 	 i != t.get_enumerators().end();
319 	 ++i)
320       {
321 	v = hashing::combine_hashes(v, str_hash(i->get_name()));
322 	v = hashing::combine_hashes(v, size_t_hash(i->get_value()));
323       }
324     return v;
325   }
326 };
327 
328 struct typedef_decl::hash
329 {
330   size_t
operator ()abigail::typedef_decl::hash331   operator()(const typedef_decl& t) const
332   {
333     std::hash<string> str_hash;
334     type_base::hash hash_type;
335     decl_base::hash decl_hash;
336     type_base::shared_ptr_hash type_ptr_hash;
337 
338     size_t v = str_hash(typeid(t).name());
339     v = hashing::combine_hashes(v, hash_type(t));
340     v = hashing::combine_hashes(v, decl_hash(t));
341     v = hashing::combine_hashes(v, type_ptr_hash(t.get_underlying_type()));
342     return v;
343   }
344  };
345 
346 /// Compute a hash for an instance @ref var_decl.
347 ///
348 /// Note that this function caches the hashing value the
349 /// decl_base::hash_ data member of the input instance and re-uses it
350 /// when it is already calculated.
351 ///
352 /// @param t the instance of @ref var_decl to compute the hash for.
353 ///
354 /// @return the calculated hash value, or the one that was previously
355 /// calculated.
356 size_t
operator ()(const var_decl & t) const357 var_decl::hash::operator()(const var_decl& t) const
358 {
359   std::hash<string> hash_string;
360   decl_base::hash hash_decl;
361   type_base::shared_ptr_hash hash_type_ptr;
362   std::hash<size_t> hash_size_t;
363 
364   size_t v = hash_string(typeid(t).name());
365   v = hashing::combine_hashes(v, hash_decl(t));
366   v = hashing::combine_hashes(v, hash_type_ptr(t.get_type()));
367 
368   if (is_data_member(t) && get_data_member_is_laid_out(t))
369     {
370       v = hashing::combine_hashes(v, hash_decl(*t.get_scope()));
371       v = hashing::combine_hashes(v, hash_size_t(get_data_member_offset(t)));
372     }
373 
374   return v;
375 }
376 
377 /// Compute a hash for a pointer to @ref var_decl.
378 ///
379 /// @param t the pointer to @ref var_decl to compute the hash for.
380 ///
381 /// @return the calculated hash value
382 size_t
operator ()(const var_decl * t) const383 var_decl::hash::operator()(const var_decl* t) const
384 {return operator()(*t);}
385 
386 /// Compute a hash value for an instance of @ref function_decl.
387 ///
388 /// Note that this function caches the resulting hash in the
389 /// decl_base::hash_ data member of the instance of @ref
390 /// function_decl, and just returns if it is already calculated.
391 ///
392 /// @param t the function to calculate the hash for.
393 ///
394 /// @return the hash value.
395 size_t
operator ()(const function_decl & t) const396 function_decl::hash::operator()(const function_decl& t) const
397 {
398   std::hash<int> hash_int;
399   std::hash<size_t> hash_size_t;
400   std::hash<bool> hash_bool;
401   std::hash<string> hash_string;
402   decl_base::hash hash_decl_base;
403   type_base::shared_ptr_hash hash_type_ptr;
404 
405   size_t v = hash_string(typeid(t).name());
406   v = hashing::combine_hashes(v, hash_decl_base(t));
407   v = hashing::combine_hashes(v, hash_type_ptr(t.get_type()));
408   v = hashing::combine_hashes(v, hash_bool(t.is_declared_inline()));
409   v = hashing::combine_hashes(v, hash_int(t.get_binding()));
410   if (is_member_function(t))
411     {
412       bool is_ctor = get_member_function_is_ctor(t),
413 	is_dtor = get_member_function_is_dtor(t),
414 	is_static = get_member_is_static(t),
415 	is_const = get_member_function_is_const(t);
416       size_t voffset = get_member_function_vtable_offset(t);
417 
418       v = hashing::combine_hashes(v, hash_bool(is_ctor));
419       v = hashing::combine_hashes(v, hash_bool(is_dtor));
420       v = hashing::combine_hashes(v, hash_bool(is_static));
421       v = hashing::combine_hashes(v, hash_bool(is_const));
422       if (!is_static && !is_ctor)
423 	v = hashing::combine_hashes(v, hash_size_t(voffset));
424     }
425 
426   return v;
427 }
428 
429 /// Compute a hash for a pointer to @ref function_decl.
430 ///
431 /// @param t the pointer to @ref function_decl to compute the hash for.
432 ///
433 /// @return the calculated hash value
434 size_t
operator ()(const function_decl * t) const435 function_decl::hash::operator()(const function_decl* t) const
436 {return operator()(*t);}
437 
438 size_t
operator ()(const function_decl::parameter & p) const439 function_decl::parameter::hash::operator()
440   (const function_decl::parameter& p) const
441 {
442   type_base::shared_ptr_hash hash_type_ptr;
443   std::hash<bool> hash_bool;
444   std::hash<unsigned> hash_unsigned;
445   size_t v = hash_type_ptr(p.get_type());
446   v = hashing::combine_hashes(v, hash_unsigned(p.get_index()));
447   v = hashing::combine_hashes(v, hash_bool(p.get_variadic_marker()));
448   return v;
449 }
450 
451 size_t
operator ()(const function_decl::parameter * p) const452 function_decl::parameter::hash::operator()
453   (const function_decl::parameter* p) const
454 {return operator()(*p);}
455 
456 size_t
operator ()(const function_decl::parameter_sptr p) const457 function_decl::parameter::hash::operator()
458   (const function_decl::parameter_sptr p) const
459 {return operator()(p.get());}
460 
461 /// Hashing functor for the @ref method_type type.
462 struct method_type::hash
463 {
464   size_t
operator ()abigail::method_type::hash465   operator()(const method_type& t) const
466   {
467     std::hash<string> hash_string;
468     type_base::shared_ptr_hash hash_type_ptr;
469     function_decl::parameter::hash hash_parameter;
470 
471     size_t v = hash_string(typeid(t).name());
472     string class_name = t.get_class_type()->get_qualified_name();
473     v = hashing::combine_hashes(v, hash_string(class_name));
474     v = hashing::combine_hashes(v, hash_type_ptr(t.get_return_type()));
475     vector<shared_ptr<function_decl::parameter> >::const_iterator i =
476       t.get_first_non_implicit_parm();
477 
478     for (; i != t.get_parameters().end(); ++i)
479       v = hashing::combine_hashes(v, hash_parameter(**i));
480 
481     return v;
482   }
483 
484   size_t
operator ()abigail::method_type::hash485   operator()(const method_type* t)
486   {return operator()(*t);}
487 
488   size_t
operator ()abigail::method_type::hash489   operator()(const method_type_sptr t)
490   {return operator()(t.get());}
491 }; // end struct method_type::hash
492 
493 // <struct function_type::hash stuff>
494 
495 /// Hashing function for @ref function_type.
496 ///
497 /// @param t the function type to hash.
498 ///
499 /// @return the resulting hash value.
500 size_t
operator ()(const function_type & t) const501 function_type::hash::operator()(const function_type& t) const
502 {
503   std::hash<string> hash_string;
504   type_base::shared_ptr_hash hash_type_ptr;
505   function_decl::parameter::hash hash_parameter;
506 
507   size_t v = hash_string(typeid(t).name());
508   v = hashing::combine_hashes(v, hash_type_ptr(t.get_return_type()));
509   for (vector<shared_ptr<function_decl::parameter> >::const_iterator i =
510 	 t.get_first_non_implicit_parm();
511        i != t.get_parameters().end();
512        ++i)
513     v = hashing::combine_hashes(v, hash_parameter(**i));
514   return v;
515 }
516 
517 /// Hashing function for a pointer to @ref function_type.
518 ///
519 /// @param t the pointer to @ref function_type to hash.
520 ///
521 /// @return the resulting hash value.
522 size_t
operator ()(const function_type * t) const523 function_type::hash::operator()(const function_type* t) const
524 {
525   if (const method_type* m = dynamic_cast<const method_type*>(t))
526     {
527       method_type::hash h;
528       return h(m);
529     }
530   return operator()(*t);
531 }
532 
533 /// Hashing function for a shared pointer to @ref function_type.
534 ///
535 /// @param t the pointer to @ref function_type to hash.
536 ///
537 /// @return the resulting hash value.
538 size_t
operator ()(const function_type_sptr t) const539 function_type::hash::operator()(const function_type_sptr t) const
540 {return operator()(t.get());}
541 
542 // </struct function_type::hash stuff>
543 
544 size_t
operator ()(const member_base & m) const545 member_base::hash::operator()(const member_base& m) const
546 {
547   std::hash<int> hash_int;
548   return hash_int(m.get_access_specifier());
549 }
550 
551 size_t
operator ()(const base_spec & t) const552 class_decl::base_spec::hash::operator()(const base_spec& t) const
553 {
554   member_base::hash hash_member;
555   type_base::shared_ptr_hash hash_type_ptr;
556   std::hash<size_t> hash_size;
557   std::hash<bool> hash_bool;
558   std::hash<string> hash_string;
559 
560   size_t v = hash_string(typeid(t).name());
561   v = hashing::combine_hashes(v, hash_member(t));
562   v = hashing::combine_hashes(v, hash_size(t.get_offset_in_bits()));
563   v = hashing::combine_hashes(v, hash_bool(t.get_is_virtual()));
564   v = hashing::combine_hashes(v, hash_type_ptr(t.get_base_class()));
565   return v;
566 }
567 
568 size_t
operator ()(const member_function_template & t) const569 member_function_template::hash::operator()
570   (const member_function_template& t) const
571 {
572   std::hash<bool> hash_bool;
573   function_tdecl::hash hash_function_tdecl;
574   member_base::hash hash_member;
575   std::hash<string> hash_string;
576 
577   size_t v = hash_member(t);
578   string n = t.get_qualified_name();
579   v = hashing::combine_hashes(v, hash_string(n));
580   v = hashing::combine_hashes(v, hash_function_tdecl(t));
581   v = hashing::combine_hashes(v, hash_bool(t.is_constructor()));
582   v = hashing::combine_hashes(v, hash_bool(t.is_const()));
583   return v;
584 }
585 
586 size_t
operator ()(const member_class_template & t) const587 member_class_template::hash::operator()
588   (const member_class_template& t) const
589 {
590   member_base::hash hash_member;
591   class_tdecl::hash hash_class_tdecl;
592   std::hash<string> hash_string;
593 
594   size_t v = hash_member(t);
595   string n = t.get_qualified_name();
596   v = hashing::combine_hashes(v, hash_string(n));
597   v = hashing::combine_hashes(v, hash_class_tdecl(t));
598   return v;
599 }
600 
601 /// Compute a hash for a @ref class_or_union
602 ///
603 /// @param t the class_or_union for which to compute the hash value.
604 ///
605 /// @return the computed hash value.
606 size_t
operator ()(const class_or_union & t) const607 class_or_union::hash::operator()(const class_or_union& t) const
608 {
609   if (t.hashing_started()
610       || (t.get_is_declaration_only() && !t.get_definition_of_declaration()))
611     // All non-resolved decl-only types have a hash of zero.  Their hash
612     // will differ from the resolved hash, but then at least, having
613     // it be zero will give a hint that we couldn't actually compute
614     // the hash.
615     return 0;
616 
617   // If the type is decl-only and now has a definition, then hash its
618   // definition instead.
619 
620   if (t.get_is_declaration_only())
621     {
622       ABG_ASSERT(t.get_definition_of_declaration());
623       size_t v = operator()
624 	(*is_class_or_union_type(t.get_definition_of_declaration()));
625       return v;
626     }
627 
628   ABG_ASSERT(!t.get_is_declaration_only());
629 
630   std::hash<string> hash_string;
631   scope_type_decl::hash hash_scope_type;
632   var_decl::hash hash_data_member;
633   member_function_template::hash hash_member_fn_tmpl;
634   member_class_template::hash hash_member_class_tmpl;
635 
636   size_t v = hash_string(typeid(t).name());
637   v = hashing::combine_hashes(v, hash_scope_type(t));
638 
639   t.hashing_started(true);
640 
641   // Hash data members.
642   for (class_decl::data_members::const_iterator d =
643 	 t.get_non_static_data_members().begin();
644        d != t.get_non_static_data_members().end();
645        ++d)
646     v = hashing::combine_hashes(v, hash_data_member(**d));
647 
648   // Do not hash member functions. All of them are not necessarily
649   // emitted per class, in a given TU so do not consider them when
650   // hashing a class.
651 
652   // Hash member function templates
653   for (member_function_templates::const_iterator f =
654 	 t.get_member_function_templates().begin();
655        f != t.get_member_function_templates().end();
656        ++f)
657     v = hashing::combine_hashes(v, hash_member_fn_tmpl(**f));
658 
659   // Hash member class templates
660   for (member_class_templates::const_iterator c =
661 	 t.get_member_class_templates().begin();
662        c != t.get_member_class_templates().end();
663        ++c)
664     v = hashing::combine_hashes(v, hash_member_class_tmpl(**c));
665 
666   t.hashing_started(false);
667 
668   return v;
669 };
670 
671 /// Compute a hash for a @ref class_or_union
672 ///
673 /// @param t the class_or_union for which to compute the hash value.
674 ///
675 /// @return the computed hash value.
676 size_t
operator ()(const class_or_union * t) const677 class_or_union::hash::operator()(const class_or_union *t) const
678 {return t ? operator()(*t) : 0;}
679 
680 /// Compute a hash for a @ref class_decl
681 ///
682 /// @param t the class_decl for which to compute the hash value.
683 ///
684 /// @return the computed hash value.
685 size_t
operator ()(const class_decl & t) const686 class_decl::hash::operator()(const class_decl& t) const
687 {
688   if (t.hashing_started()
689       || (t.get_is_declaration_only() && !t.get_definition_of_declaration()))
690     // All non-resolved decl-only types have a hash of zero.  Their hash
691     // will differ from the resolved hash, but then at least, having
692     // it be zero will give a hint that we couldn't actually compute
693     // the hash.
694     return 0;
695 
696 
697   // If the type is decl-only and now has a definition, then hash its
698   // definition instead.
699 
700   if (t.get_is_declaration_only())
701     {
702       ABG_ASSERT(t.get_definition_of_declaration());
703       size_t v = operator()(*is_class_type(t.get_definition_of_declaration()));
704       return v;
705     }
706 
707   ABG_ASSERT(!t.get_is_declaration_only());
708 
709   std::hash<string> hash_string;
710   class_decl::base_spec::hash hash_base;
711   class_or_union::hash hash_class_or_union;
712 
713   size_t v = hash_string(typeid(t).name());
714 
715   t.hashing_started(true);
716 
717   // Hash bases.
718   for (class_decl::base_specs::const_iterator b =
719 	 t.get_base_specifiers().begin();
720        b != t.get_base_specifiers().end();
721        ++b)
722     {
723       class_decl_sptr cl = (*b)->get_base_class();
724       v = hashing::combine_hashes(v, hash_base(**b));
725     }
726 
727   v = hashing::combine_hashes(v, hash_class_or_union(t));
728 
729   t.hashing_started(false);
730 
731   return v;
732 }
733 
734 /// Compute a hash for a @ref class_decl
735 ///
736 /// @param t the class_decl for which to compute the hash value.
737 ///
738 /// @return the computed hash value.
739 size_t
operator ()(const class_decl * t) const740 class_decl::hash::operator()(const class_decl* t) const
741 {return t ? operator()(*t) : 0;}
742 
743 struct template_parameter::hash
744 {
745   size_t
operator ()abigail::template_parameter::hash746   operator()(const template_parameter& t) const
747   {
748     // Let's avoid infinite recursion triggered from the fact that
749     // hashing a template parameter triggers hashing the enclosed
750     // template decl, which in turn triggers the hashing of its
751     // template parameters; so the initial template parameter that
752     // triggered the hashing could be hashed again ...
753     if (t.get_hashing_has_started())
754       return 0;
755 
756     t.set_hashing_has_started(true);
757 
758     std::hash<unsigned> hash_unsigned;
759     std::hash<std::string> hash_string;
760     template_decl::hash hash_template_decl;
761 
762     size_t v = hash_string(typeid(t).name());
763     v = hashing::combine_hashes(v, hash_unsigned(t.get_index()));
764     v = hashing::combine_hashes(v, hash_template_decl
765 				(*t.get_enclosing_template_decl()));
766 
767     t.set_hashing_has_started(false);
768 
769     return v;
770   }
771 };
772 
773 struct template_parameter::dynamic_hash
774 {
775   size_t
776   operator()(const template_parameter* t) const;
777 };
778 
779 struct template_parameter::shared_ptr_hash
780 {
781   size_t
operator ()abigail::template_parameter::shared_ptr_hash782   operator()(const shared_ptr<template_parameter> t) const
783   {return template_parameter::dynamic_hash()(t.get());}
784 };
785 
786 size_t
operator ()(const template_decl & t) const787 template_decl::hash::operator()(const template_decl& t) const
788 {
789   std::hash<string> hash_string;
790   template_parameter::shared_ptr_hash hash_template_parameter;
791 
792   size_t v = hash_string(typeid(t).name());
793   v = hashing::combine_hashes(v, hash_string(t.get_qualified_name()));
794 
795   for (list<template_parameter_sptr>::const_iterator p =
796 	 t.get_template_parameters().begin();
797        p != t.get_template_parameters().end();
798        ++p)
799     if (!(*p)->get_hashing_has_started())
800       v = hashing::combine_hashes(v, hash_template_parameter(*p));
801 
802   return v;
803 }
804 
805 struct type_tparameter::hash
806 {
807   size_t
operator ()abigail::type_tparameter::hash808   operator()(const type_tparameter& t) const
809   {
810     std::hash<string> hash_string;
811     template_parameter::hash hash_template_parameter;
812     type_decl::hash hash_type;
813 
814     size_t v = hash_string(typeid(t).name());
815     v = hashing::combine_hashes(v, hash_template_parameter(t));
816     v = hashing::combine_hashes(v, hash_type(t));
817 
818     return v;
819   }
820 };
821 
822 /// Compute a hash value for a @ref non_type_tparameter
823 ///
824 /// @param t the non_type_tparameter for which to compute the value.
825 ///
826 /// @return the computed hash value.
827 size_t
operator ()(const non_type_tparameter & t) const828 non_type_tparameter::hash::operator()(const non_type_tparameter& t) const
829 {
830   template_parameter::hash hash_template_parameter;
831   std::hash<string> hash_string;
832   type_base::shared_ptr_hash hash_type;
833 
834   size_t v = hash_string(typeid(t).name());
835   v = hashing::combine_hashes(v, hash_template_parameter(t));
836   v = hashing::combine_hashes(v, hash_string(t.get_name()));
837   v = hashing::combine_hashes(v, hash_type(t.get_type()));
838 
839   return v;
840 }
841 
842 /// Compute a hash value for a @ref non_type_tparameter
843 ///
844 /// @param t the non_type_tparameter to compute the hash value for.
845 ///
846 /// @return the computed hash value.
847 size_t
operator ()(const non_type_tparameter * t) const848 non_type_tparameter::hash::operator()(const non_type_tparameter* t) const
849 {return t ? operator()(*t) : 0;}
850 
851 struct template_tparameter::hash
852 {
853   size_t
operator ()abigail::template_tparameter::hash854   operator()(const template_tparameter& t) const
855   {
856     std::hash<string> hash_string;
857     type_tparameter::hash hash_template_type_parm;
858     template_decl::hash hash_template_decl;
859 
860     size_t v = hash_string(typeid(t).name());
861     v = hashing::combine_hashes(v, hash_template_type_parm(t));
862     v = hashing::combine_hashes(v, hash_template_decl(t));
863 
864     return v;
865   }
866 };
867 
868 size_t
869 template_parameter::dynamic_hash::
operator ()(const template_parameter * t) const870 operator()(const template_parameter* t) const
871 {
872   if (const template_tparameter* p =
873       dynamic_cast<const template_tparameter*>(t))
874     return template_tparameter::hash()(*p);
875   else if (const type_tparameter* p =
876 	   dynamic_cast<const type_tparameter*>(t))
877     return type_tparameter::hash()(*p);
878   if (const non_type_tparameter* p =
879       dynamic_cast<const non_type_tparameter*>(t))
880     return non_type_tparameter::hash()(*p);
881 
882   // Poor man's fallback.
883   return template_parameter::hash()(*t);
884 }
885 
886 /// Compute a hash value for a @ref type_composition type.
887 ///
888 /// @param t the type_composition to compute the hash value for.
889 ///
890 /// @return the computed hash value.
891 size_t
operator ()(const type_composition & t) const892 type_composition::hash::operator()(const type_composition& t) const
893 {
894   std::hash<string> hash_string;
895   type_base::dynamic_hash hash_type;
896 
897   size_t v = hash_string(typeid(t).name());
898   v = hashing::combine_hashes(v, hash_type(t.get_composed_type().get()));
899   return v;
900 }
901 
902 /// Compute a hash value for a @ref type_composition type.
903 ///
904 /// @param t the type_composition to compute the hash value for.
905 ///
906 /// @return the computed hash value.
907 size_t
operator ()(const type_composition * t) const908 type_composition::hash::operator()(const type_composition* t) const
909 {return t ? operator()(*t): 0;}
910 
911 size_t
912 function_tdecl::hash::
operator ()(const function_tdecl & t) const913 operator()(const function_tdecl& t) const
914 {
915   std::hash<string> hash_string;
916   decl_base::hash hash_decl_base;
917   template_decl::hash hash_template_decl;
918   function_decl::hash hash_function_decl;
919 
920   size_t v = hash_string(typeid(t).name());
921 
922   v = hashing::combine_hashes(v, hash_decl_base(t));
923   v = hashing::combine_hashes(v, hash_template_decl(t));
924   if (t.get_pattern())
925     v = hashing::combine_hashes(v, hash_function_decl(*t.get_pattern()));
926 
927   return v;
928 }
929 
930 size_t
931 function_tdecl::shared_ptr_hash::
operator ()(const shared_ptr<function_tdecl> f) const932 operator()(const shared_ptr<function_tdecl> f) const
933 {
934   function_tdecl::hash hash_fn_tmpl_decl;
935   if (f)
936     return hash_fn_tmpl_decl(*f);
937   return 0;
938 }
939 
940 size_t
941 class_tdecl::hash::
operator ()(const class_tdecl & t) const942 operator()(const class_tdecl& t) const
943 {
944   std::hash<string> hash_string;
945   decl_base::hash hash_decl_base;
946   template_decl::hash hash_template_decl;
947   class_decl::hash hash_class_decl;
948 
949   size_t v = hash_string(typeid(t).name());
950   v = hashing::combine_hashes(v, hash_decl_base(t));
951   v = hashing::combine_hashes(v, hash_template_decl(t));
952   if (t.get_pattern())
953     v = hashing::combine_hashes(v, hash_class_decl(*t.get_pattern()));
954 
955   return v;
956 }
957 
958 size_t
959 class_tdecl::shared_ptr_hash::
operator ()(const shared_ptr<class_tdecl> t) const960 operator()(const shared_ptr<class_tdecl> t) const
961 {
962   class_tdecl::hash hash_class_tmpl_decl;
963 
964   if (t)
965     return hash_class_tmpl_decl(*t);
966   return 0;
967 }
968 
969 /// A hashing function for type declarations.
970 ///
971 /// This function gets the dynamic type of the actual type
972 /// declaration and calls the right hashing function for that type.
973 ///
974 /// Note that each time a new type declaration kind is added to the
975 /// system, this function needs to be updated.  For a given
976 /// inheritance hierarchy, make sure to handle the most derived type
977 /// first.
978 ///
979 /// FIXME: This hashing function is not maintained and is surely
980 /// broken in subtle ways.  In pratice, the various *::hash functors
981 /// should be audited before they are used here.  They should all
982 /// match what is done in the 'equals' functions in abg-ir.cc.
983 ///
984 /// @param t a pointer to the type declaration to be hashed
985 ///
986 /// @return the resulting hash
987 size_t
operator ()(const type_base * t) const988 type_base::dynamic_hash::operator()(const type_base* t) const
989 {
990   if (t == 0)
991     return 0;
992 
993   if (const member_function_template* d =
994       dynamic_cast<const member_function_template*>(t))
995     return member_function_template::hash()(*d);
996   if (const member_class_template* d =
997       dynamic_cast<const member_class_template*>(t))
998     return member_class_template::hash()(*d);
999   if (const template_tparameter* d =
1000       dynamic_cast<const template_tparameter*>(t))
1001     return template_tparameter::hash()(*d);
1002   if (const type_tparameter* d =
1003       dynamic_cast<const type_tparameter*>(t))
1004     return type_tparameter::hash()(*d);
1005   if (const type_decl* d = dynamic_cast<const type_decl*> (t))
1006     return type_decl::hash()(*d);
1007   if (const qualified_type_def* d = dynamic_cast<const qualified_type_def*>(t))
1008     return qualified_type_def::hash()(*d);
1009   if (const pointer_type_def* d = dynamic_cast<const pointer_type_def*>(t))
1010     return pointer_type_def::hash()(*d);
1011   if (const reference_type_def* d = dynamic_cast<const reference_type_def*>(t))
1012     return reference_type_def::hash()(*d);
1013   if (const array_type_def* d = dynamic_cast<const array_type_def*>(t))
1014     return array_type_def::hash()(*d);
1015   if (const enum_type_decl* d = dynamic_cast<const enum_type_decl*>(t))
1016     return enum_type_decl::hash()(*d);
1017   if (const typedef_decl* d = dynamic_cast<const typedef_decl*>(t))
1018     return typedef_decl::hash()(*d);
1019   if (const class_decl* d = dynamic_cast<const class_decl*>(t))
1020     return class_decl::hash()(*d);
1021   if (const union_decl* d = dynamic_cast<const union_decl*>(t))
1022     return union_decl::hash()(*d);
1023   if (const scope_type_decl* d = dynamic_cast<const scope_type_decl*>(t))
1024     return scope_type_decl::hash()(*d);
1025   if (const method_type* d = dynamic_cast<const method_type*>(t))
1026     return method_type::hash()(*d);
1027   if (const function_type* d = dynamic_cast<const function_type*>(t))
1028     return function_type::hash()(*d);
1029 
1030   // Poor man's fallback case.
1031   return type_base::hash()(*t);
1032 }
1033 
1034 size_t
operator ()(const shared_ptr<type_base> t) const1035 type_base::shared_ptr_hash::operator()(const shared_ptr<type_base> t) const
1036 {return type_base::dynamic_hash()(t.get());}
1037 
1038 }//end namespace abigail
1039