OSDN Git Service

90a200fc7357ce5e9c36a0334e57ff386d0c942a
[pf3gnuchains/gcc-fork.git] / gcc / lto-symtab.c
1 /* LTO symbol table.
2    Copyright 2009 Free Software Foundation, Inc.
3    Contributed by CodeSourcery, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "toplev.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "ggc.h"        /* lambda.h needs this */
28 #include "lambda.h"     /* gcd */
29 #include "hashtab.h"
30 #include "plugin-api.h"
31 #include "lto-streamer.h"
32
33 /* Vector to keep track of external variables we've seen so far.  */
34 VEC(tree,gc) *lto_global_var_decls;
35
36 /* Base type for resolution map. It maps NODE to resolution.  */
37
38 struct GTY(()) lto_symtab_base_def
39 {
40   /* Key is either an IDENTIFIER or a DECL.  */
41   tree node;
42 };
43 typedef struct lto_symtab_base_def *lto_symtab_base_t;
44
45 struct GTY(()) lto_symtab_identifier_def
46 {
47   struct lto_symtab_base_def base;
48   tree decl;
49 };
50 typedef struct lto_symtab_identifier_def *lto_symtab_identifier_t;
51
52 struct GTY(()) lto_symtab_decl_def
53 {
54   struct lto_symtab_base_def base;
55   enum ld_plugin_symbol_resolution resolution;
56   struct lto_file_decl_data * GTY((skip (""))) file_data;
57 };
58 typedef struct lto_symtab_decl_def *lto_symtab_decl_t;
59
60 /* A poor man's symbol table. This hashes identifier to prevailing DECL
61    if there is one. */
62
63 static GTY ((if_marked ("lto_symtab_identifier_marked_p"),
64              param_is (struct lto_symtab_identifier_def)))
65   htab_t lto_symtab_identifiers;
66
67 static GTY ((if_marked ("lto_symtab_decl_marked_p"),
68              param_is (struct lto_symtab_decl_def)))
69   htab_t lto_symtab_decls;
70
71 /* Return the hash value of an lto_symtab_base_t object pointed to by P.  */
72
73 static hashval_t
74 lto_symtab_base_hash (const void *p)
75 {
76   const struct lto_symtab_base_def *base =
77     (const struct lto_symtab_base_def*) p;
78   return htab_hash_pointer (base->node);
79 }
80
81 /* Return non-zero if P1 and P2 points to lto_symtab_base_def structs
82    corresponding to the same tree node.  */
83
84 static int
85 lto_symtab_base_eq (const void *p1, const void *p2)
86 {
87   const struct lto_symtab_base_def *base1 =
88      (const struct lto_symtab_base_def *) p1;
89   const struct lto_symtab_base_def *base2 =
90      (const struct lto_symtab_base_def *) p2;
91   return (base1->node == base2->node);
92 }
93
94 /* Returns non-zero if P points to an lto_symtab_base_def struct that needs
95    to be marked for GC.  */ 
96
97 static int
98 lto_symtab_base_marked_p (const void *p)
99 {
100   const struct lto_symtab_base_def *base =
101      (const struct lto_symtab_base_def *) p;
102
103   /* Keep this only if the key node is marked.  */
104   return ggc_marked_p (base->node);
105 }
106
107 /* Returns non-zero if P points to an lto_symtab_identifier_def struct that
108    needs to be marked for GC.  */ 
109
110 static int
111 lto_symtab_identifier_marked_p (const void *p)
112 {
113   return lto_symtab_base_marked_p (p);
114 }
115
116 /* Returns non-zero if P points to an lto_symtab_decl_def struct that needs
117    to be marked for GC.  */ 
118
119 static int
120 lto_symtab_decl_marked_p (const void *p)
121 {
122   return lto_symtab_base_marked_p (p);
123 }
124
125 #define lto_symtab_identifier_eq        lto_symtab_base_eq
126 #define lto_symtab_identifier_hash      lto_symtab_base_hash
127 #define lto_symtab_decl_eq              lto_symtab_base_eq
128 #define lto_symtab_decl_hash            lto_symtab_base_hash
129
130 /* Lazily initialize resolution hash tables.  */
131
132 static void
133 lto_symtab_maybe_init_hash_tables (void)
134 {
135   if (!lto_symtab_identifiers)
136     {
137       lto_symtab_identifiers =
138         htab_create_ggc (1021, lto_symtab_identifier_hash,
139                          lto_symtab_identifier_eq, NULL);
140       lto_symtab_decls =
141         htab_create_ggc (1021, lto_symtab_decl_hash,
142                          lto_symtab_decl_eq, NULL);
143     }
144 }
145
146 /* Returns true iff the union of ATTRIBUTES_1 and ATTRIBUTES_2 can be
147    applied to DECL.  */
148 static bool
149 lto_compatible_attributes_p (tree decl ATTRIBUTE_UNUSED, 
150                              tree attributes_1, 
151                              tree attributes_2)
152 {
153 #if 0
154   /* ??? For now, assume two attribute sets are compatible only if they
155      are both empty.  */
156   return !attributes_1 && !attributes_2;
157 #else
158   /* FIXME.  For the moment, live dangerously, and assume the user knows
159      what he's doing. I don't think the linker would distinguish these cases.  */
160   return true || (!attributes_1 && !attributes_2);
161 #endif
162 }
163
164 /* Helper for lto_symtab_compatible. Return TRUE if DECL is an external
165    variable declaration of an aggregate type. */
166
167 static bool
168 external_aggregate_decl_p (tree decl)
169 {
170   return (TREE_CODE (decl) == VAR_DECL
171           && DECL_EXTERNAL (decl)
172           && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
173 }
174
175 static bool maybe_merge_incomplete_and_complete_type (tree, tree);
176
177 /* Try to merge an incomplete type INCOMPLETE with a complete type
178    COMPLETE of same kinds.
179    Return true if they were merged, false otherwise.  */
180
181 static bool
182 merge_incomplete_and_complete_type (tree incomplete, tree complete)
183 {
184   /* For merging array types do some extra sanity checking.  */
185   if (TREE_CODE (incomplete) == ARRAY_TYPE
186       && !maybe_merge_incomplete_and_complete_type (TREE_TYPE (incomplete),
187                                                     TREE_TYPE (complete))
188       && !gimple_types_compatible_p (TREE_TYPE (incomplete),
189                                      TREE_TYPE (complete)))
190     return false;
191
192   /* ??? Ideally we would do this by means of a common canonical type, but
193      that's difficult as we do not have links from the canonical type
194      back to all its children.  */
195   gimple_force_type_merge (incomplete, complete);
196
197   return true;
198 }
199
200 /* Try to merge a maybe complete / incomplete type pair TYPE1 and TYPE2.
201    Return true if they were merged, false otherwise.  */
202
203 static bool
204 maybe_merge_incomplete_and_complete_type (tree type1, tree type2)
205 {
206   bool res = false;
207
208   if (TREE_CODE (type1) != TREE_CODE (type2))
209     return false;
210
211   if (!COMPLETE_TYPE_P (type1) && COMPLETE_TYPE_P (type2))
212     res = merge_incomplete_and_complete_type (type1, type2);
213   else if (COMPLETE_TYPE_P (type1) && !COMPLETE_TYPE_P (type2))
214     res = merge_incomplete_and_complete_type (type2, type1);
215
216   /* Recurse on pointer targets.  */
217   if (!res
218       && POINTER_TYPE_P (type1)
219       && POINTER_TYPE_P (type2))
220     res = maybe_merge_incomplete_and_complete_type (TREE_TYPE (type1),
221                                                     TREE_TYPE (type2));
222
223   return res;
224 }
225
226 /* Check if OLD_DECL and NEW_DECL are compatible. */
227
228 static bool
229 lto_symtab_compatible (tree old_decl, tree new_decl)
230 {
231   tree merged_type = NULL_TREE;
232
233   if (TREE_CODE (old_decl) != TREE_CODE (new_decl))
234     {
235       switch (TREE_CODE (new_decl))
236         {
237         case VAR_DECL:
238           gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL);
239           error_at (DECL_SOURCE_LOCATION (new_decl),
240                     "function %qD redeclared as variable", new_decl);
241           inform (DECL_SOURCE_LOCATION (old_decl),
242                   "previously declared here");
243           return false;
244
245         case FUNCTION_DECL:
246           gcc_assert (TREE_CODE (old_decl) == VAR_DECL);
247           error_at (DECL_SOURCE_LOCATION (new_decl),
248                     "variable %qD redeclared as function", new_decl);
249           inform (DECL_SOURCE_LOCATION (old_decl),
250                   "previously declared here");
251           return false;
252
253         default:
254           gcc_unreachable ();
255         }
256     }
257
258   /* Handle external declarations with incomplete type or pointed-to
259      incomplete types by forcefully merging the types.
260      ???  In principle all types involved in the two decls should
261      be merged forcefully, for example without considering type or
262      field names.  */
263   if (TREE_CODE (old_decl) == VAR_DECL)
264     {
265       tree old_type = TREE_TYPE (old_decl);
266       tree new_type = TREE_TYPE (new_decl);
267
268       if (DECL_EXTERNAL (old_decl) || DECL_EXTERNAL (new_decl))
269         maybe_merge_incomplete_and_complete_type (old_type, new_type);
270       else if (POINTER_TYPE_P (old_type)
271                && POINTER_TYPE_P (new_type))
272         maybe_merge_incomplete_and_complete_type (TREE_TYPE (old_type),
273                                                   TREE_TYPE (new_type));
274
275       /* For array types we have to accept external declarations with
276          different sizes than the actual definition (164.gzip).
277          ???  We could emit a warning here.  */
278       if (TREE_CODE (old_type) == TREE_CODE (new_type)
279           && TREE_CODE (old_type) == ARRAY_TYPE
280           && COMPLETE_TYPE_P (old_type)
281           && COMPLETE_TYPE_P (new_type)
282           && tree_int_cst_compare (TYPE_SIZE (old_type),
283                                    TYPE_SIZE (new_type)) != 0
284           && gimple_types_compatible_p (TREE_TYPE (old_type),
285                                         TREE_TYPE (new_type)))
286         {
287           /* If only one is external use the type of the non-external decl.
288              Else use the larger one and also adjust the decl size.
289              ???  Directional merging would allow us to simply pick the
290              larger one instead of rewriting it.  */
291           if (DECL_EXTERNAL (old_decl) ^ DECL_EXTERNAL (new_decl))
292             {
293               if (DECL_EXTERNAL (old_decl))
294                 TREE_TYPE (old_decl) = new_type;
295               else if (DECL_EXTERNAL (new_decl))
296                 TREE_TYPE (new_decl) = old_type;
297             }
298           else
299             {
300               if (tree_int_cst_compare (TYPE_SIZE (old_type),
301                                         TYPE_SIZE (new_type)) < 0)
302                 {
303                   TREE_TYPE (old_decl) = new_type;
304                   DECL_SIZE (old_decl) = DECL_SIZE (new_decl);
305                   DECL_SIZE_UNIT (old_decl) = DECL_SIZE_UNIT (new_decl);
306                 }
307               else
308                 {
309                   TREE_TYPE (new_decl) = old_type;
310                   DECL_SIZE (new_decl) = DECL_SIZE (old_decl);
311                   DECL_SIZE_UNIT (new_decl) = DECL_SIZE_UNIT (old_decl);
312                 }
313             }
314         }
315     }
316
317   if (!gimple_types_compatible_p (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
318     {
319       if (TREE_CODE (new_decl) == FUNCTION_DECL)
320         {
321           if (!merged_type
322               /* We want either of the types to have argument types,
323                  but not both.  */
324               && ((TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != NULL)
325                   ^ (TYPE_ARG_TYPES (TREE_TYPE (new_decl)) != NULL)))
326             {
327               /* The situation here is that (in C) somebody was smart
328                  enough to use proper declarations in a header file, but
329                  the actual definition of the function uses
330                  non-ANSI-style argument lists.  Or we have a situation
331                  where declarations weren't used anywhere and we're
332                  merging the actual definition with a use.  One of the
333                  decls will then have a complete function type, whereas
334                  the other will only have a result type.  Assume that
335                  the more complete type is the right one and don't
336                  complain.  */
337               if (TYPE_ARG_TYPES (TREE_TYPE (old_decl)))
338                 {
339                   merged_type = TREE_TYPE (old_decl);
340                 }
341               else
342                 {
343                   merged_type = TREE_TYPE (new_decl);
344                 }
345             }
346
347           /* If we don't have a merged type yet...sigh.  The linker
348              wouldn't complain if the types were mismatched, so we
349              probably shouldn't either.  Just use the type from
350              whichever decl appears to be associated with the
351              definition.  If for some odd reason neither decl is, the
352              older one wins.  */
353           if (!merged_type)
354             {
355               if (!DECL_EXTERNAL (new_decl))
356                 {
357                   merged_type = TREE_TYPE (new_decl);
358                 }
359               else
360                 {
361                   merged_type = TREE_TYPE (old_decl);
362                 }
363             }
364         }
365
366       if (!merged_type)
367         {
368           if (warning_at (DECL_SOURCE_LOCATION (new_decl), 0,
369                           "type of %qD does not match original declaration",
370                           new_decl))
371             inform (DECL_SOURCE_LOCATION (old_decl),
372                     "previously declared here");
373           return false;
374         }
375     }
376
377   if (DECL_UNSIGNED (old_decl) != DECL_UNSIGNED (new_decl))
378     {
379       error_at (DECL_SOURCE_LOCATION (new_decl),
380                 "signedness of %qD does not match original declaration",
381                 new_decl);
382       inform (DECL_SOURCE_LOCATION (old_decl), "previously declared here");
383       return false;
384     }
385
386   if (!tree_int_cst_equal (DECL_SIZE (old_decl),
387                            DECL_SIZE (new_decl))
388       || !tree_int_cst_equal (DECL_SIZE_UNIT (old_decl),
389                               DECL_SIZE_UNIT (new_decl)))
390     {
391       /* Permit cases where we are declaring aggregates and at least one
392          of the decls is external and one of the decls has a size whereas
393          the other one does not.  This is perfectly legal in C:
394
395          struct s;
396          extern struct s x;
397
398          void*
399          f (void)
400          {
401            return &x;
402          }
403
404          There is no way a compiler can tell the size of x.  So we cannot
405          assume that external aggreates have complete types.  */
406
407       if (!((TREE_CODE (TREE_TYPE (old_decl))
408              == TREE_CODE (TREE_TYPE (new_decl)))
409             && ((external_aggregate_decl_p (old_decl)
410                  && DECL_SIZE (old_decl) == NULL_TREE)
411                 || (external_aggregate_decl_p (new_decl)
412                     && DECL_SIZE (new_decl) == NULL_TREE))))
413         {
414           error_at (DECL_SOURCE_LOCATION (new_decl),
415                     "size of %qD does not match original declaration",
416                     new_decl);
417           inform (DECL_SOURCE_LOCATION (old_decl),
418                   "previously declared here");
419           return false;
420         }
421     }
422
423   /* Report an error if user-specified alignments do not match.  */
424   if ((DECL_USER_ALIGN (old_decl) && DECL_USER_ALIGN (new_decl))
425       && DECL_ALIGN (old_decl) != DECL_ALIGN (new_decl))
426     {
427       error_at (DECL_SOURCE_LOCATION (new_decl),
428                 "alignment of %qD does not match original declaration",
429                 new_decl);
430       inform (DECL_SOURCE_LOCATION (old_decl), "previously declared here");
431       return false;
432     }
433
434   /* Do not compare the modes of the decls.  The type compatibility
435      checks or the completing of types has properly dealt with all issues.  */
436
437   if (!lto_compatible_attributes_p (old_decl,
438                                     DECL_ATTRIBUTES (old_decl),
439                                     DECL_ATTRIBUTES (new_decl)))
440     {
441       error_at (DECL_SOURCE_LOCATION (new_decl),
442                 "attributes applied to %qD are incompatible with original "
443                 "declaration", new_decl);
444       inform (DECL_SOURCE_LOCATION (old_decl), "previously declared here");
445       return false;
446     }
447
448   /* We do not require matches for:
449
450      - DECL_NAME
451
452        Only the name used in object files matters.
453
454      - DECL_CONTEXT  
455
456        An entity might be declared in a C++ namespace in one file and
457        with a C identifier in another file.  
458
459      - TREE_PRIVATE, TREE_PROTECTED
460
461        Access control is the problem of the front end that created the
462        object file.  
463        
464      Therefore, at this point we have decided to merge the declarations.  */
465   return true;
466 }
467
468
469 /* Marks decl DECL as having resolution RESOLUTION. */
470
471 static void
472 lto_symtab_set_resolution_and_file_data (tree decl,
473                                          ld_plugin_symbol_resolution_t
474                                          resolution,
475                                          struct lto_file_decl_data *file_data)
476 {
477   lto_symtab_decl_t new_entry;
478   void **slot;
479
480   gcc_assert (decl);
481
482   gcc_assert (TREE_PUBLIC (decl));
483   gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !DECL_ABSTRACT (decl));
484
485   new_entry = GGC_CNEW (struct lto_symtab_decl_def);
486   new_entry->base.node = decl;
487   new_entry->resolution = resolution;
488   new_entry->file_data = file_data;
489   
490   lto_symtab_maybe_init_hash_tables ();
491   slot = htab_find_slot (lto_symtab_decls, new_entry, INSERT);
492   gcc_assert (!*slot);
493   *slot = new_entry;
494 }
495
496 /* Get the lto_symtab_identifier_def struct associated with ID
497    if there is one.  If there is none and INSERT_P is true, create
498    a new one.  */
499
500 static lto_symtab_identifier_t
501 lto_symtab_get_identifier (tree id, bool insert_p)
502 {
503   struct lto_symtab_identifier_def temp;
504   lto_symtab_identifier_t symtab_id;
505   void **slot;
506
507   lto_symtab_maybe_init_hash_tables ();
508   temp.base.node = id;
509   slot = htab_find_slot (lto_symtab_identifiers, &temp,
510                          insert_p ? INSERT : NO_INSERT);
511   if (insert_p)
512     {
513       if (*slot)
514         return (lto_symtab_identifier_t) *slot;
515       else
516         {
517           symtab_id = GGC_CNEW (struct lto_symtab_identifier_def);
518           symtab_id->base.node = id;
519           *slot = symtab_id;
520           return symtab_id;
521         }
522     }
523   else
524     return slot ? (lto_symtab_identifier_t) *slot : NULL;
525 }
526
527 /* Return the DECL associated with an IDENTIFIER ID or return NULL_TREE
528    if there is none.  */
529
530 static tree
531 lto_symtab_get_identifier_decl (tree id)
532 {
533   lto_symtab_identifier_t symtab_id = lto_symtab_get_identifier (id, false);
534   return symtab_id ? symtab_id->decl : NULL_TREE;
535 }
536
537 /* SET the associated DECL of an IDENTIFIER ID to be DECL.  */
538
539 static void
540 lto_symtab_set_identifier_decl (tree id, tree decl)
541 {
542   lto_symtab_identifier_t symtab_id = lto_symtab_get_identifier (id, true);
543   symtab_id->decl = decl;
544 }
545
546 /* Common helper function for merging variable and function declarations.
547    NEW_DECL is the newly found decl. RESOLUTION is the decl's resolution
548    provided by the linker. */
549
550 static void
551 lto_symtab_merge_decl (tree new_decl,
552                        enum ld_plugin_symbol_resolution resolution,
553                        struct lto_file_decl_data *file_data)
554 {
555   tree old_decl;
556   tree name;
557   ld_plugin_symbol_resolution_t old_resolution;
558
559   gcc_assert (TREE_CODE (new_decl) == VAR_DECL
560               || TREE_CODE (new_decl) == FUNCTION_DECL);
561
562   gcc_assert (TREE_PUBLIC (new_decl));
563
564   gcc_assert (DECL_LANG_SPECIFIC (new_decl) == NULL);
565
566   /* Check that declarations reaching this function do not have
567      properties inconsistent with having external linkage.  If any of
568      these asertions fail, then the object file reader has failed to
569      detect these cases and issue appropriate error messages.  */
570   if (TREE_CODE (new_decl) == VAR_DECL)
571     gcc_assert (!(DECL_EXTERNAL (new_decl) && DECL_INITIAL (new_decl)));
572
573   /* Remember the resolution of this symbol. */
574   lto_symtab_set_resolution_and_file_data (new_decl, resolution, file_data);
575
576   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
577   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (new_decl));
578
579   /* Retrieve the previous declaration.  */
580   name = DECL_ASSEMBLER_NAME (new_decl);
581   old_decl = lto_symtab_get_identifier_decl (name);
582
583   /* If there was no previous declaration, then there is nothing to
584      merge.  */
585   if (!old_decl)
586     {
587       lto_symtab_set_identifier_decl (name, new_decl);
588       VEC_safe_push (tree, gc, lto_global_var_decls, new_decl);
589       return;
590     }
591
592   /* Give ODR violation errors.  */
593   old_resolution = lto_symtab_get_resolution (old_decl);
594   if (resolution == LDPR_PREVAILING_DEF
595       || resolution == LDPR_PREVAILING_DEF_IRONLY)
596     {
597       if ((old_resolution == LDPR_PREVAILING_DEF
598            || old_resolution == LDPR_PREVAILING_DEF_IRONLY)
599           && (old_resolution != resolution || flag_no_common))
600         {
601           error_at (DECL_SOURCE_LOCATION (new_decl),
602                     "%qD has already been defined", new_decl);
603           inform (DECL_SOURCE_LOCATION (old_decl),
604                   "previously defined here");
605           return;
606         }
607     }
608
609   /* The linker may ask us to combine two incompatible symbols.
610      Find a decl we can merge with or chain it in the list of decls
611      for that symbol.  */
612   while (old_decl
613          && !lto_symtab_compatible (old_decl, new_decl))
614     old_decl = (tree) DECL_LANG_SPECIFIC (old_decl);
615   if (!old_decl)
616     {
617       old_decl = lto_symtab_get_identifier_decl (name);
618       while (DECL_LANG_SPECIFIC (old_decl) != NULL)
619         old_decl = (tree) DECL_LANG_SPECIFIC (old_decl);
620       DECL_LANG_SPECIFIC (old_decl) = (struct lang_decl *) new_decl;
621       return;
622     }
623
624   /* Merge decl state in both directions, we may still end up using
625      the new decl.  */
626   TREE_ADDRESSABLE (old_decl) |= TREE_ADDRESSABLE (new_decl);
627   TREE_ADDRESSABLE (new_decl) |= TREE_ADDRESSABLE (old_decl);
628
629   gcc_assert (resolution != LDPR_UNKNOWN
630               && resolution != LDPR_UNDEF
631               && old_resolution != LDPR_UNKNOWN
632               && old_resolution != LDPR_UNDEF);
633
634   if (resolution == LDPR_PREVAILING_DEF
635       || resolution == LDPR_PREVAILING_DEF_IRONLY)
636     {
637       tree decl;
638       gcc_assert (old_resolution == LDPR_PREEMPTED_IR
639                   || old_resolution ==  LDPR_RESOLVED_IR
640                   || (old_resolution == resolution && !flag_no_common));
641       DECL_LANG_SPECIFIC (new_decl) = DECL_LANG_SPECIFIC (old_decl);
642       DECL_LANG_SPECIFIC (old_decl) = NULL;
643       decl = lto_symtab_get_identifier_decl (name);
644       if (decl == old_decl)
645         {
646           lto_symtab_set_identifier_decl (name, new_decl);
647           return;
648         }
649       while ((tree) DECL_LANG_SPECIFIC (decl) != old_decl)
650         decl = (tree) DECL_LANG_SPECIFIC (decl);
651       DECL_LANG_SPECIFIC (decl) = (struct lang_decl *) new_decl;
652       return;
653     }
654
655   if (resolution == LDPR_PREEMPTED_REG
656       || resolution == LDPR_RESOLVED_EXEC
657       || resolution == LDPR_RESOLVED_DYN)
658     gcc_assert (old_resolution == LDPR_PREEMPTED_REG
659                 || old_resolution == LDPR_RESOLVED_EXEC
660                 || old_resolution == LDPR_RESOLVED_DYN);
661
662   if (resolution == LDPR_PREEMPTED_IR
663       || resolution == LDPR_RESOLVED_IR)
664     gcc_assert (old_resolution == LDPR_PREVAILING_DEF
665                 || old_resolution == LDPR_PREVAILING_DEF_IRONLY
666                 || old_resolution == LDPR_PREEMPTED_IR
667                 || old_resolution == LDPR_RESOLVED_IR);
668
669   return;
670 }
671
672
673 /* Merge the VAR_DECL NEW_VAR with resolution RESOLUTION with any previous
674    declaration with the same name. */
675
676 void
677 lto_symtab_merge_var (tree new_var, enum ld_plugin_symbol_resolution resolution)
678 {
679   lto_symtab_merge_decl (new_var, resolution, NULL);
680 }
681
682 /* Merge the FUNCTION_DECL NEW_FN with resolution RESOLUTION with any previous
683    declaration with the same name. */
684
685 void
686 lto_symtab_merge_fn (tree new_fn, enum ld_plugin_symbol_resolution resolution,
687                      struct lto_file_decl_data *file_data)
688 {
689   lto_symtab_merge_decl (new_fn, resolution, file_data);
690 }
691
692 /* Given the decl DECL, return the prevailing decl with the same name. */
693
694 tree
695 lto_symtab_prevailing_decl (tree decl)
696 {
697   tree ret;
698   gcc_assert (decl);
699
700   /* Builtins and local symbols are their own prevailing decl.  */
701   if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
702     return decl;
703
704   /* DECL_ABSTRACTs are their own prevailng decl.  */
705   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
706     return decl;
707
708   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
709   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
710
711   /* Walk through the list of candidates and return the one we merged to.  */
712   ret = lto_symtab_get_identifier_decl (DECL_ASSEMBLER_NAME (decl));
713   if (!ret
714       || DECL_LANG_SPECIFIC (ret) == NULL)
715     return ret;
716
717   /* If there are multiple decls to choose from find the one we merged
718      with and return that.  */
719   while (ret)
720     {
721       if (gimple_types_compatible_p (TREE_TYPE (decl), TREE_TYPE (ret)))
722         return ret;
723
724       ret = (tree) DECL_LANG_SPECIFIC (ret);
725     }
726
727   gcc_unreachable ();
728 }
729
730 /* Return the hash table entry of DECL. */
731
732 static struct lto_symtab_decl_def *
733 lto_symtab_get_symtab_def (tree decl)
734 {
735   struct lto_symtab_decl_def temp, *symtab_decl;
736   void **slot;
737
738   gcc_assert (decl);
739
740   lto_symtab_maybe_init_hash_tables ();
741   temp.base.node = decl;
742   slot = htab_find_slot (lto_symtab_decls, &temp, NO_INSERT);
743   gcc_assert (slot && *slot);
744   symtab_decl = (struct lto_symtab_decl_def*) *slot;
745   return symtab_decl;
746 }
747
748 /* Return the resolution of DECL. */
749
750 enum ld_plugin_symbol_resolution
751 lto_symtab_get_resolution (tree decl)
752 {
753   gcc_assert (decl);
754
755   if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
756     return LDPR_PREVAILING_DEF_IRONLY;
757
758   /* FIXME lto: There should be no DECL_ABSTRACT in the middle end. */
759   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
760     return LDPR_PREVAILING_DEF_IRONLY;
761
762   return lto_symtab_get_symtab_def (decl)->resolution;
763 }
764
765 /* Return the file of DECL. */
766
767 struct lto_file_decl_data *
768 lto_symtab_get_file_data (tree decl)
769 {
770   return lto_symtab_get_symtab_def (decl)->file_data;
771 }
772
773 /* Remove any storage used to store resolution of DECL.  */
774
775 void
776 lto_symtab_clear_resolution (tree decl)
777 {
778   struct lto_symtab_decl_def temp;
779   gcc_assert (decl);
780
781   if (!TREE_PUBLIC (decl))
782     return;
783
784   /* LTO FIXME: There should be no DECL_ABSTRACT in the middle end. */
785   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
786     return;
787
788   lto_symtab_maybe_init_hash_tables ();
789   temp.base.node = decl;
790   htab_remove_elt (lto_symtab_decls, &temp);
791 }
792
793 #include "gt-lto-symtab.h"