1130803Smarcel/* C preprocessor macro tables for GDB.
2130803Smarcel   Copyright 2002 Free Software Foundation, Inc.
3130803Smarcel   Contributed by Red Hat, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel#include "gdb_obstack.h"
24130803Smarcel#include "splay-tree.h"
25130803Smarcel#include "symtab.h"
26130803Smarcel#include "symfile.h"
27130803Smarcel#include "objfiles.h"
28130803Smarcel#include "macrotab.h"
29130803Smarcel#include "gdb_assert.h"
30130803Smarcel#include "bcache.h"
31130803Smarcel#include "complaints.h"
32130803Smarcel
33130803Smarcel
34130803Smarcel/* The macro table structure.  */
35130803Smarcel
36130803Smarcelstruct macro_table
37130803Smarcel{
38130803Smarcel  /* The obstack this table's data should be allocated in, or zero if
39130803Smarcel     we should use xmalloc.  */
40130803Smarcel  struct obstack *obstack;
41130803Smarcel
42130803Smarcel  /* The bcache we should use to hold macro names, argument names, and
43130803Smarcel     definitions, or zero if we should use xmalloc.  */
44130803Smarcel  struct bcache *bcache;
45130803Smarcel
46130803Smarcel  /* The main source file for this compilation unit --- the one whose
47130803Smarcel     name was given to the compiler.  This is the root of the
48130803Smarcel     #inclusion tree; everything else is #included from here.  */
49130803Smarcel  struct macro_source_file *main_source;
50130803Smarcel
51130803Smarcel  /* The table of macro definitions.  This is a splay tree (an ordered
52130803Smarcel     binary tree that stays balanced, effectively), sorted by macro
53130803Smarcel     name.  Where a macro gets defined more than once (presumably with
54130803Smarcel     an #undefinition in between), we sort the definitions by the
55130803Smarcel     order they would appear in the preprocessor's output.  That is,
56130803Smarcel     if `a.c' #includes `m.h' and then #includes `n.h', and both
57130803Smarcel     header files #define X (with an #undef somewhere in between),
58130803Smarcel     then the definition from `m.h' appears in our splay tree before
59130803Smarcel     the one from `n.h'.
60130803Smarcel
61130803Smarcel     The splay tree's keys are `struct macro_key' pointers;
62130803Smarcel     the values are `struct macro_definition' pointers.
63130803Smarcel
64130803Smarcel     The splay tree, its nodes, and the keys and values are allocated
65130803Smarcel     in obstack, if it's non-zero, or with xmalloc otherwise.  The
66130803Smarcel     macro names, argument names, argument name arrays, and definition
67130803Smarcel     strings are all allocated in bcache, if non-zero, or with xmalloc
68130803Smarcel     otherwise.  */
69130803Smarcel  splay_tree definitions;
70130803Smarcel};
71130803Smarcel
72130803Smarcel
73130803Smarcel
74130803Smarcel/* Allocation and freeing functions.  */
75130803Smarcel
76130803Smarcel/* Allocate SIZE bytes of memory appropriately for the macro table T.
77130803Smarcel   This just checks whether T has an obstack, or whether its pieces
78130803Smarcel   should be allocated with xmalloc.  */
79130803Smarcelstatic void *
80130803Smarcelmacro_alloc (int size, struct macro_table *t)
81130803Smarcel{
82130803Smarcel  if (t->obstack)
83130803Smarcel    return obstack_alloc (t->obstack, size);
84130803Smarcel  else
85130803Smarcel    return xmalloc (size);
86130803Smarcel}
87130803Smarcel
88130803Smarcel
89130803Smarcelstatic void
90130803Smarcelmacro_free (void *object, struct macro_table *t)
91130803Smarcel{
92130803Smarcel  gdb_assert (! t->obstack);
93130803Smarcel  xfree (object);
94130803Smarcel}
95130803Smarcel
96130803Smarcel
97130803Smarcel/* If the macro table T has a bcache, then cache the LEN bytes at ADDR
98130803Smarcel   there, and return the cached copy.  Otherwise, just xmalloc a copy
99130803Smarcel   of the bytes, and return a pointer to that.  */
100130803Smarcelstatic const void *
101130803Smarcelmacro_bcache (struct macro_table *t, const void *addr, int len)
102130803Smarcel{
103130803Smarcel  if (t->bcache)
104130803Smarcel    return bcache (addr, len, t->bcache);
105130803Smarcel  else
106130803Smarcel    {
107130803Smarcel      void *copy = xmalloc (len);
108130803Smarcel      memcpy (copy, addr, len);
109130803Smarcel      return copy;
110130803Smarcel    }
111130803Smarcel}
112130803Smarcel
113130803Smarcel
114130803Smarcel/* If the macro table T has a bcache, cache the null-terminated string
115130803Smarcel   S there, and return a pointer to the cached copy.  Otherwise,
116130803Smarcel   xmalloc a copy and return that.  */
117130803Smarcelstatic const char *
118130803Smarcelmacro_bcache_str (struct macro_table *t, const char *s)
119130803Smarcel{
120130803Smarcel  return (char *) macro_bcache (t, s, strlen (s) + 1);
121130803Smarcel}
122130803Smarcel
123130803Smarcel
124130803Smarcel/* Free a possibly bcached object OBJ.  That is, if the macro table T
125130803Smarcel   has a bcache, it's an error; otherwise, xfree OBJ.  */
126130803Smarcelstatic void
127130803Smarcelmacro_bcache_free (struct macro_table *t, void *obj)
128130803Smarcel{
129130803Smarcel  gdb_assert (! t->bcache);
130130803Smarcel  xfree (obj);
131130803Smarcel}
132130803Smarcel
133130803Smarcel
134130803Smarcel
135130803Smarcel/* Macro tree keys, w/their comparison, allocation, and freeing functions.  */
136130803Smarcel
137130803Smarcel/* A key in the splay tree.  */
138130803Smarcelstruct macro_key
139130803Smarcel{
140130803Smarcel  /* The table we're in.  We only need this in order to free it, since
141130803Smarcel     the splay tree library's key and value freeing functions require
142130803Smarcel     that the key or value contain all the information needed to free
143130803Smarcel     themselves.  */
144130803Smarcel  struct macro_table *table;
145130803Smarcel
146130803Smarcel  /* The name of the macro.  This is in the table's bcache, if it has
147130803Smarcel     one. */
148130803Smarcel  const char *name;
149130803Smarcel
150130803Smarcel  /* The source file and line number where the definition's scope
151130803Smarcel     begins.  This is also the line of the definition itself.  */
152130803Smarcel  struct macro_source_file *start_file;
153130803Smarcel  int start_line;
154130803Smarcel
155130803Smarcel  /* The first source file and line after the definition's scope.
156130803Smarcel     (That is, the scope does not include this endpoint.)  If end_file
157130803Smarcel     is zero, then the definition extends to the end of the
158130803Smarcel     compilation unit.  */
159130803Smarcel  struct macro_source_file *end_file;
160130803Smarcel  int end_line;
161130803Smarcel};
162130803Smarcel
163130803Smarcel
164130803Smarcel/* Return the #inclusion depth of the source file FILE.  This is the
165130803Smarcel   number of #inclusions it took to reach this file.  For the main
166130803Smarcel   source file, the #inclusion depth is zero; for a file it #includes
167130803Smarcel   directly, the depth would be one; and so on.  */
168130803Smarcelstatic int
169130803Smarcelinclusion_depth (struct macro_source_file *file)
170130803Smarcel{
171130803Smarcel  int depth;
172130803Smarcel
173130803Smarcel  for (depth = 0; file->included_by; depth++)
174130803Smarcel    file = file->included_by;
175130803Smarcel
176130803Smarcel  return depth;
177130803Smarcel}
178130803Smarcel
179130803Smarcel
180130803Smarcel/* Compare two source locations (from the same compilation unit).
181130803Smarcel   This is part of the comparison function for the tree of
182130803Smarcel   definitions.
183130803Smarcel
184130803Smarcel   LINE1 and LINE2 are line numbers in the source files FILE1 and
185130803Smarcel   FILE2.  Return a value:
186130803Smarcel   - less than zero if {LINE,FILE}1 comes before {LINE,FILE}2,
187130803Smarcel   - greater than zero if {LINE,FILE}1 comes after {LINE,FILE}2, or
188130803Smarcel   - zero if they are equal.
189130803Smarcel
190130803Smarcel   When the two locations are in different source files --- perhaps
191130803Smarcel   one is in a header, while another is in the main source file --- we
192130803Smarcel   order them by where they would appear in the fully pre-processed
193130803Smarcel   sources, where all the #included files have been substituted into
194130803Smarcel   their places.  */
195130803Smarcelstatic int
196130803Smarcelcompare_locations (struct macro_source_file *file1, int line1,
197130803Smarcel                   struct macro_source_file *file2, int line2)
198130803Smarcel{
199130803Smarcel  /* We want to treat positions in an #included file as coming *after*
200130803Smarcel     the line containing the #include, but *before* the line after the
201130803Smarcel     include.  As we walk up the #inclusion tree toward the main
202130803Smarcel     source file, we update fileX and lineX as we go; includedX
203130803Smarcel     indicates whether the original position was from the #included
204130803Smarcel     file.  */
205130803Smarcel  int included1 = 0;
206130803Smarcel  int included2 = 0;
207130803Smarcel
208130803Smarcel  /* If a file is zero, that means "end of compilation unit."  Handle
209130803Smarcel     that specially.  */
210130803Smarcel  if (! file1)
211130803Smarcel    {
212130803Smarcel      if (! file2)
213130803Smarcel        return 0;
214130803Smarcel      else
215130803Smarcel        return 1;
216130803Smarcel    }
217130803Smarcel  else if (! file2)
218130803Smarcel    return -1;
219130803Smarcel
220130803Smarcel  /* If the two files are not the same, find their common ancestor in
221130803Smarcel     the #inclusion tree.  */
222130803Smarcel  if (file1 != file2)
223130803Smarcel    {
224130803Smarcel      /* If one file is deeper than the other, walk up the #inclusion
225130803Smarcel         chain until the two files are at least at the same *depth*.
226130803Smarcel         Then, walk up both files in synchrony until they're the same
227130803Smarcel         file.  That file is the common ancestor.  */
228130803Smarcel      int depth1 = inclusion_depth (file1);
229130803Smarcel      int depth2 = inclusion_depth (file2);
230130803Smarcel
231130803Smarcel      /* Only one of these while loops will ever execute in any given
232130803Smarcel         case.  */
233130803Smarcel      while (depth1 > depth2)
234130803Smarcel        {
235130803Smarcel          line1 = file1->included_at_line;
236130803Smarcel          file1 = file1->included_by;
237130803Smarcel          included1 = 1;
238130803Smarcel          depth1--;
239130803Smarcel        }
240130803Smarcel      while (depth2 > depth1)
241130803Smarcel        {
242130803Smarcel          line2 = file2->included_at_line;
243130803Smarcel          file2 = file2->included_by;
244130803Smarcel          included2 = 1;
245130803Smarcel          depth2--;
246130803Smarcel        }
247130803Smarcel
248130803Smarcel      /* Now both file1 and file2 are at the same depth.  Walk toward
249130803Smarcel         the root of the tree until we find where the branches meet.  */
250130803Smarcel      while (file1 != file2)
251130803Smarcel        {
252130803Smarcel          line1 = file1->included_at_line;
253130803Smarcel          file1 = file1->included_by;
254130803Smarcel          /* At this point, we know that the case the includedX flags
255130803Smarcel             are trying to deal with won't come up, but we'll just
256130803Smarcel             maintain them anyway.  */
257130803Smarcel          included1 = 1;
258130803Smarcel
259130803Smarcel          line2 = file2->included_at_line;
260130803Smarcel          file2 = file2->included_by;
261130803Smarcel          included2 = 1;
262130803Smarcel
263130803Smarcel          /* Sanity check.  If file1 and file2 are really from the
264130803Smarcel             same compilation unit, then they should both be part of
265130803Smarcel             the same tree, and this shouldn't happen.  */
266130803Smarcel          gdb_assert (file1 && file2);
267130803Smarcel        }
268130803Smarcel    }
269130803Smarcel
270130803Smarcel  /* Now we've got two line numbers in the same file.  */
271130803Smarcel  if (line1 == line2)
272130803Smarcel    {
273130803Smarcel      /* They can't both be from #included files.  Then we shouldn't
274130803Smarcel         have walked up this far.  */
275130803Smarcel      gdb_assert (! included1 || ! included2);
276130803Smarcel
277130803Smarcel      /* Any #included position comes after a non-#included position
278130803Smarcel         with the same line number in the #including file.  */
279130803Smarcel      if (included1)
280130803Smarcel        return 1;
281130803Smarcel      else if (included2)
282130803Smarcel        return -1;
283130803Smarcel      else
284130803Smarcel        return 0;
285130803Smarcel    }
286130803Smarcel  else
287130803Smarcel    return line1 - line2;
288130803Smarcel}
289130803Smarcel
290130803Smarcel
291130803Smarcel/* Compare a macro key KEY against NAME, the source file FILE, and
292130803Smarcel   line number LINE.
293130803Smarcel
294130803Smarcel   Sort definitions by name; for two definitions with the same name,
295130803Smarcel   place the one whose definition comes earlier before the one whose
296130803Smarcel   definition comes later.
297130803Smarcel
298130803Smarcel   Return -1, 0, or 1 if key comes before, is identical to, or comes
299130803Smarcel   after NAME, FILE, and LINE.  */
300130803Smarcelstatic int
301130803Smarcelkey_compare (struct macro_key *key,
302130803Smarcel             const char *name, struct macro_source_file *file, int line)
303130803Smarcel{
304130803Smarcel  int names = strcmp (key->name, name);
305130803Smarcel  if (names)
306130803Smarcel    return names;
307130803Smarcel
308130803Smarcel  return compare_locations (key->start_file, key->start_line,
309130803Smarcel                            file, line);
310130803Smarcel}
311130803Smarcel
312130803Smarcel
313130803Smarcel/* The macro tree comparison function, typed for the splay tree
314130803Smarcel   library's happiness.  */
315130803Smarcelstatic int
316130803Smarcelmacro_tree_compare (splay_tree_key untyped_key1,
317130803Smarcel                    splay_tree_key untyped_key2)
318130803Smarcel{
319130803Smarcel  struct macro_key *key1 = (struct macro_key *) untyped_key1;
320130803Smarcel  struct macro_key *key2 = (struct macro_key *) untyped_key2;
321130803Smarcel
322130803Smarcel  return key_compare (key1, key2->name, key2->start_file, key2->start_line);
323130803Smarcel}
324130803Smarcel
325130803Smarcel
326130803Smarcel/* Construct a new macro key node for a macro in table T whose name is
327130803Smarcel   NAME, and whose scope starts at LINE in FILE; register the name in
328130803Smarcel   the bcache.  */
329130803Smarcelstatic struct macro_key *
330130803Smarcelnew_macro_key (struct macro_table *t,
331130803Smarcel               const char *name,
332130803Smarcel               struct macro_source_file *file,
333130803Smarcel               int line)
334130803Smarcel{
335130803Smarcel  struct macro_key *k = macro_alloc (sizeof (*k), t);
336130803Smarcel
337130803Smarcel  memset (k, 0, sizeof (*k));
338130803Smarcel  k->table = t;
339130803Smarcel  k->name = macro_bcache_str (t, name);
340130803Smarcel  k->start_file = file;
341130803Smarcel  k->start_line = line;
342130803Smarcel  k->end_file = 0;
343130803Smarcel
344130803Smarcel  return k;
345130803Smarcel}
346130803Smarcel
347130803Smarcel
348130803Smarcelstatic void
349130803Smarcelmacro_tree_delete_key (void *untyped_key)
350130803Smarcel{
351130803Smarcel  struct macro_key *key = (struct macro_key *) untyped_key;
352130803Smarcel
353130803Smarcel  macro_bcache_free (key->table, (char *) key->name);
354130803Smarcel  macro_free (key, key->table);
355130803Smarcel}
356130803Smarcel
357130803Smarcel
358130803Smarcel
359130803Smarcel/* Building and querying the tree of #included files.  */
360130803Smarcel
361130803Smarcel
362130803Smarcel/* Allocate and initialize a new source file structure.  */
363130803Smarcelstatic struct macro_source_file *
364130803Smarcelnew_source_file (struct macro_table *t,
365130803Smarcel                 const char *filename)
366130803Smarcel{
367130803Smarcel  /* Get space for the source file structure itself.  */
368130803Smarcel  struct macro_source_file *f = macro_alloc (sizeof (*f), t);
369130803Smarcel
370130803Smarcel  memset (f, 0, sizeof (*f));
371130803Smarcel  f->table = t;
372130803Smarcel  f->filename = macro_bcache_str (t, filename);
373130803Smarcel  f->includes = 0;
374130803Smarcel
375130803Smarcel  return f;
376130803Smarcel}
377130803Smarcel
378130803Smarcel
379130803Smarcel/* Free a source file, and all the source files it #included.  */
380130803Smarcelstatic void
381130803Smarcelfree_macro_source_file (struct macro_source_file *src)
382130803Smarcel{
383130803Smarcel  struct macro_source_file *child, *next_child;
384130803Smarcel
385130803Smarcel  /* Free this file's children.  */
386130803Smarcel  for (child = src->includes; child; child = next_child)
387130803Smarcel    {
388130803Smarcel      next_child = child->next_included;
389130803Smarcel      free_macro_source_file (child);
390130803Smarcel    }
391130803Smarcel
392130803Smarcel  macro_bcache_free (src->table, (char *) src->filename);
393130803Smarcel  macro_free (src, src->table);
394130803Smarcel}
395130803Smarcel
396130803Smarcel
397130803Smarcelstruct macro_source_file *
398130803Smarcelmacro_set_main (struct macro_table *t,
399130803Smarcel                const char *filename)
400130803Smarcel{
401130803Smarcel  /* You can't change a table's main source file.  What would that do
402130803Smarcel     to the tree?  */
403130803Smarcel  gdb_assert (! t->main_source);
404130803Smarcel
405130803Smarcel  t->main_source = new_source_file (t, filename);
406130803Smarcel
407130803Smarcel  return t->main_source;
408130803Smarcel}
409130803Smarcel
410130803Smarcel
411130803Smarcelstruct macro_source_file *
412130803Smarcelmacro_main (struct macro_table *t)
413130803Smarcel{
414130803Smarcel  gdb_assert (t->main_source);
415130803Smarcel
416130803Smarcel  return t->main_source;
417130803Smarcel}
418130803Smarcel
419130803Smarcel
420130803Smarcelstruct macro_source_file *
421130803Smarcelmacro_include (struct macro_source_file *source,
422130803Smarcel               int line,
423130803Smarcel               const char *included)
424130803Smarcel{
425130803Smarcel  struct macro_source_file *new;
426130803Smarcel  struct macro_source_file **link;
427130803Smarcel
428130803Smarcel  /* Find the right position in SOURCE's `includes' list for the new
429130803Smarcel     file.  Skip inclusions at earlier lines, until we find one at the
430130803Smarcel     same line or later --- or until the end of the list.  */
431130803Smarcel  for (link = &source->includes;
432130803Smarcel       *link && (*link)->included_at_line < line;
433130803Smarcel       link = &(*link)->next_included)
434130803Smarcel    ;
435130803Smarcel
436130803Smarcel  /* Did we find another file already #included at the same line as
437130803Smarcel     the new one?  */
438130803Smarcel  if (*link && line == (*link)->included_at_line)
439130803Smarcel    {
440130803Smarcel      /* This means the compiler is emitting bogus debug info.  (GCC
441130803Smarcel         circa March 2002 did this.)  It also means that the splay
442130803Smarcel         tree ordering function, macro_tree_compare, will abort,
443130803Smarcel         because it can't tell which #inclusion came first.  But GDB
444130803Smarcel         should tolerate bad debug info.  So:
445130803Smarcel
446130803Smarcel         First, squawk.  */
447130803Smarcel      complaint (&symfile_complaints,
448130803Smarcel		 "both `%s' and `%s' allegedly #included at %s:%d", included,
449130803Smarcel		 (*link)->filename, source->filename, line);
450130803Smarcel
451130803Smarcel      /* Now, choose a new, unoccupied line number for this
452130803Smarcel         #inclusion, after the alleged #inclusion line.  */
453130803Smarcel      while (*link && line == (*link)->included_at_line)
454130803Smarcel        {
455130803Smarcel          /* This line number is taken, so try the next line.  */
456130803Smarcel          line++;
457130803Smarcel          link = &(*link)->next_included;
458130803Smarcel        }
459130803Smarcel    }
460130803Smarcel
461130803Smarcel  /* At this point, we know that LINE is an unused line number, and
462130803Smarcel     *LINK points to the entry an #inclusion at that line should
463130803Smarcel     precede.  */
464130803Smarcel  new = new_source_file (source->table, included);
465130803Smarcel  new->included_by = source;
466130803Smarcel  new->included_at_line = line;
467130803Smarcel  new->next_included = *link;
468130803Smarcel  *link = new;
469130803Smarcel
470130803Smarcel  return new;
471130803Smarcel}
472130803Smarcel
473130803Smarcel
474130803Smarcelstruct macro_source_file *
475130803Smarcelmacro_lookup_inclusion (struct macro_source_file *source, const char *name)
476130803Smarcel{
477130803Smarcel  /* Is SOURCE itself named NAME?  */
478130803Smarcel  if (strcmp (name, source->filename) == 0)
479130803Smarcel    return source;
480130803Smarcel
481130803Smarcel  /* The filename in the source structure is probably a full path, but
482130803Smarcel     NAME could be just the final component of the name.  */
483130803Smarcel  {
484130803Smarcel    int name_len = strlen (name);
485130803Smarcel    int src_name_len = strlen (source->filename);
486130803Smarcel
487130803Smarcel    /* We do mean < here, and not <=; if the lengths are the same,
488130803Smarcel       then the strcmp above should have triggered, and we need to
489130803Smarcel       check for a slash here.  */
490130803Smarcel    if (name_len < src_name_len
491130803Smarcel        && source->filename[src_name_len - name_len - 1] == '/'
492130803Smarcel        && strcmp (name, source->filename + src_name_len - name_len) == 0)
493130803Smarcel      return source;
494130803Smarcel  }
495130803Smarcel
496130803Smarcel  /* It's not us.  Try all our children, and return the lowest.  */
497130803Smarcel  {
498130803Smarcel    struct macro_source_file *child;
499130803Smarcel    struct macro_source_file *best = NULL;
500130803Smarcel    int best_depth = 0;
501130803Smarcel
502130803Smarcel    for (child = source->includes; child; child = child->next_included)
503130803Smarcel      {
504130803Smarcel        struct macro_source_file *result
505130803Smarcel          = macro_lookup_inclusion (child, name);
506130803Smarcel
507130803Smarcel        if (result)
508130803Smarcel          {
509130803Smarcel            int result_depth = inclusion_depth (result);
510130803Smarcel
511130803Smarcel            if (! best || result_depth < best_depth)
512130803Smarcel              {
513130803Smarcel                best = result;
514130803Smarcel                best_depth = result_depth;
515130803Smarcel              }
516130803Smarcel          }
517130803Smarcel      }
518130803Smarcel
519130803Smarcel    return best;
520130803Smarcel  }
521130803Smarcel}
522130803Smarcel
523130803Smarcel
524130803Smarcel
525130803Smarcel/* Registering and looking up macro definitions.  */
526130803Smarcel
527130803Smarcel
528130803Smarcel/* Construct a definition for a macro in table T.  Cache all strings,
529130803Smarcel   and the macro_definition structure itself, in T's bcache.  */
530130803Smarcelstatic struct macro_definition *
531130803Smarcelnew_macro_definition (struct macro_table *t,
532130803Smarcel                      enum macro_kind kind,
533130803Smarcel                      int argc, const char **argv,
534130803Smarcel                      const char *replacement)
535130803Smarcel{
536130803Smarcel  struct macro_definition *d = macro_alloc (sizeof (*d), t);
537130803Smarcel
538130803Smarcel  memset (d, 0, sizeof (*d));
539130803Smarcel  d->table = t;
540130803Smarcel  d->kind = kind;
541130803Smarcel  d->replacement = macro_bcache_str (t, replacement);
542130803Smarcel
543130803Smarcel  if (kind == macro_function_like)
544130803Smarcel    {
545130803Smarcel      int i;
546130803Smarcel      const char **cached_argv;
547130803Smarcel      int cached_argv_size = argc * sizeof (*cached_argv);
548130803Smarcel
549130803Smarcel      /* Bcache all the arguments.  */
550130803Smarcel      cached_argv = alloca (cached_argv_size);
551130803Smarcel      for (i = 0; i < argc; i++)
552130803Smarcel        cached_argv[i] = macro_bcache_str (t, argv[i]);
553130803Smarcel
554130803Smarcel      /* Now bcache the array of argument pointers itself.  */
555130803Smarcel      d->argv = macro_bcache (t, cached_argv, cached_argv_size);
556130803Smarcel      d->argc = argc;
557130803Smarcel    }
558130803Smarcel
559130803Smarcel  /* We don't bcache the entire definition structure because it's got
560130803Smarcel     a pointer to the macro table in it; since each compilation unit
561130803Smarcel     has its own macro table, you'd only get bcache hits for identical
562130803Smarcel     definitions within a compilation unit, which seems unlikely.
563130803Smarcel
564130803Smarcel     "So, why do macro definitions have pointers to their macro tables
565130803Smarcel     at all?"  Well, when the splay tree library wants to free a
566130803Smarcel     node's value, it calls the value freeing function with nothing
567130803Smarcel     but the value itself.  It makes the (apparently reasonable)
568130803Smarcel     assumption that the value carries enough information to free
569130803Smarcel     itself.  But not all macro tables have bcaches, so not all macro
570130803Smarcel     definitions would be bcached.  There's no way to tell whether a
571130803Smarcel     given definition is bcached without knowing which table the
572130803Smarcel     definition belongs to.  ...  blah.  The thing's only sixteen
573130803Smarcel     bytes anyway, and we can still bcache the name, args, and
574130803Smarcel     definition, so we just don't bother bcaching the definition
575130803Smarcel     structure itself.  */
576130803Smarcel  return d;
577130803Smarcel}
578130803Smarcel
579130803Smarcel
580130803Smarcel/* Free a macro definition.  */
581130803Smarcelstatic void
582130803Smarcelmacro_tree_delete_value (void *untyped_definition)
583130803Smarcel{
584130803Smarcel  struct macro_definition *d = (struct macro_definition *) untyped_definition;
585130803Smarcel  struct macro_table *t = d->table;
586130803Smarcel
587130803Smarcel  if (d->kind == macro_function_like)
588130803Smarcel    {
589130803Smarcel      int i;
590130803Smarcel
591130803Smarcel      for (i = 0; i < d->argc; i++)
592130803Smarcel        macro_bcache_free (t, (char *) d->argv[i]);
593130803Smarcel      macro_bcache_free (t, (char **) d->argv);
594130803Smarcel    }
595130803Smarcel
596130803Smarcel  macro_bcache_free (t, (char *) d->replacement);
597130803Smarcel  macro_free (d, t);
598130803Smarcel}
599130803Smarcel
600130803Smarcel
601130803Smarcel/* Find the splay tree node for the definition of NAME at LINE in
602130803Smarcel   SOURCE, or zero if there is none.  */
603130803Smarcelstatic splay_tree_node
604130803Smarcelfind_definition (const char *name,
605130803Smarcel                 struct macro_source_file *file,
606130803Smarcel                 int line)
607130803Smarcel{
608130803Smarcel  struct macro_table *t = file->table;
609130803Smarcel  splay_tree_node n;
610130803Smarcel
611130803Smarcel  /* Construct a macro_key object, just for the query.  */
612130803Smarcel  struct macro_key query;
613130803Smarcel
614130803Smarcel  query.name = name;
615130803Smarcel  query.start_file = file;
616130803Smarcel  query.start_line = line;
617130803Smarcel  query.end_file = NULL;
618130803Smarcel
619130803Smarcel  n = splay_tree_lookup (t->definitions, (splay_tree_key) &query);
620130803Smarcel  if (! n)
621130803Smarcel    {
622130803Smarcel      /* It's okay for us to do two queries like this: the real work
623130803Smarcel         of the searching is done when we splay, and splaying the tree
624130803Smarcel         a second time at the same key is a constant time operation.
625130803Smarcel         If this still bugs you, you could always just extend the
626130803Smarcel         splay tree library with a predecessor-or-equal operation, and
627130803Smarcel         use that.  */
628130803Smarcel      splay_tree_node pred = splay_tree_predecessor (t->definitions,
629130803Smarcel                                                     (splay_tree_key) &query);
630130803Smarcel
631130803Smarcel      if (pred)
632130803Smarcel        {
633130803Smarcel          /* Make sure this predecessor actually has the right name.
634130803Smarcel             We just want to search within a given name's definitions.  */
635130803Smarcel          struct macro_key *found = (struct macro_key *) pred->key;
636130803Smarcel
637130803Smarcel          if (strcmp (found->name, name) == 0)
638130803Smarcel            n = pred;
639130803Smarcel        }
640130803Smarcel    }
641130803Smarcel
642130803Smarcel  if (n)
643130803Smarcel    {
644130803Smarcel      struct macro_key *found = (struct macro_key *) n->key;
645130803Smarcel
646130803Smarcel      /* Okay, so this definition has the right name, and its scope
647130803Smarcel         begins before the given source location.  But does its scope
648130803Smarcel         end after the given source location?  */
649130803Smarcel      if (compare_locations (file, line, found->end_file, found->end_line) < 0)
650130803Smarcel        return n;
651130803Smarcel      else
652130803Smarcel        return 0;
653130803Smarcel    }
654130803Smarcel  else
655130803Smarcel    return 0;
656130803Smarcel}
657130803Smarcel
658130803Smarcel
659130803Smarcel/* If NAME already has a definition in scope at LINE in SOURCE, return
660130803Smarcel   the key.  If the old definition is different from the definition
661130803Smarcel   given by KIND, ARGC, ARGV, and REPLACEMENT, complain, too.
662130803Smarcel   Otherwise, return zero.  (ARGC and ARGV are meaningless unless KIND
663130803Smarcel   is `macro_function_like'.)  */
664130803Smarcelstatic struct macro_key *
665130803Smarcelcheck_for_redefinition (struct macro_source_file *source, int line,
666130803Smarcel                        const char *name, enum macro_kind kind,
667130803Smarcel                        int argc, const char **argv,
668130803Smarcel                        const char *replacement)
669130803Smarcel{
670130803Smarcel  splay_tree_node n = find_definition (name, source, line);
671130803Smarcel
672130803Smarcel  if (n)
673130803Smarcel    {
674130803Smarcel      struct macro_key *found_key = (struct macro_key *) n->key;
675130803Smarcel      struct macro_definition *found_def
676130803Smarcel        = (struct macro_definition *) n->value;
677130803Smarcel      int same = 1;
678130803Smarcel
679130803Smarcel      /* Is this definition the same as the existing one?
680130803Smarcel         According to the standard, this comparison needs to be done
681130803Smarcel         on lists of tokens, not byte-by-byte, as we do here.  But
682130803Smarcel         that's too hard for us at the moment, and comparing
683130803Smarcel         byte-by-byte will only yield false negatives (i.e., extra
684130803Smarcel         warning messages), not false positives (i.e., unnoticed
685130803Smarcel         definition changes).  */
686130803Smarcel      if (kind != found_def->kind)
687130803Smarcel        same = 0;
688130803Smarcel      else if (strcmp (replacement, found_def->replacement))
689130803Smarcel        same = 0;
690130803Smarcel      else if (kind == macro_function_like)
691130803Smarcel        {
692130803Smarcel          if (argc != found_def->argc)
693130803Smarcel            same = 0;
694130803Smarcel          else
695130803Smarcel            {
696130803Smarcel              int i;
697130803Smarcel
698130803Smarcel              for (i = 0; i < argc; i++)
699130803Smarcel                if (strcmp (argv[i], found_def->argv[i]))
700130803Smarcel                  same = 0;
701130803Smarcel            }
702130803Smarcel        }
703130803Smarcel
704130803Smarcel      if (! same)
705130803Smarcel        {
706130803Smarcel	  complaint (&symfile_complaints,
707130803Smarcel		     "macro `%s' redefined at %s:%d; original definition at %s:%d",
708130803Smarcel		     name, source->filename, line,
709130803Smarcel		     found_key->start_file->filename, found_key->start_line);
710130803Smarcel        }
711130803Smarcel
712130803Smarcel      return found_key;
713130803Smarcel    }
714130803Smarcel  else
715130803Smarcel    return 0;
716130803Smarcel}
717130803Smarcel
718130803Smarcel
719130803Smarcelvoid
720130803Smarcelmacro_define_object (struct macro_source_file *source, int line,
721130803Smarcel                     const char *name, const char *replacement)
722130803Smarcel{
723130803Smarcel  struct macro_table *t = source->table;
724130803Smarcel  struct macro_key *k;
725130803Smarcel  struct macro_definition *d;
726130803Smarcel
727130803Smarcel  k = check_for_redefinition (source, line,
728130803Smarcel                              name, macro_object_like,
729130803Smarcel                              0, 0,
730130803Smarcel                              replacement);
731130803Smarcel
732130803Smarcel  /* If we're redefining a symbol, and the existing key would be
733130803Smarcel     identical to our new key, then the splay_tree_insert function
734130803Smarcel     will try to delete the old definition.  When the definition is
735130803Smarcel     living on an obstack, this isn't a happy thing.
736130803Smarcel
737130803Smarcel     Since this only happens in the presence of questionable debug
738130803Smarcel     info, we just ignore all definitions after the first.  The only
739130803Smarcel     case I know of where this arises is in GCC's output for
740130803Smarcel     predefined macros, and all the definitions are the same in that
741130803Smarcel     case.  */
742130803Smarcel  if (k && ! key_compare (k, name, source, line))
743130803Smarcel    return;
744130803Smarcel
745130803Smarcel  k = new_macro_key (t, name, source, line);
746130803Smarcel  d = new_macro_definition (t, macro_object_like, 0, 0, replacement);
747130803Smarcel  splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
748130803Smarcel}
749130803Smarcel
750130803Smarcel
751130803Smarcelvoid
752130803Smarcelmacro_define_function (struct macro_source_file *source, int line,
753130803Smarcel                       const char *name, int argc, const char **argv,
754130803Smarcel                       const char *replacement)
755130803Smarcel{
756130803Smarcel  struct macro_table *t = source->table;
757130803Smarcel  struct macro_key *k;
758130803Smarcel  struct macro_definition *d;
759130803Smarcel
760130803Smarcel  k = check_for_redefinition (source, line,
761130803Smarcel                              name, macro_function_like,
762130803Smarcel                              argc, argv,
763130803Smarcel                              replacement);
764130803Smarcel
765130803Smarcel  /* See comments about duplicate keys in macro_define_object.  */
766130803Smarcel  if (k && ! key_compare (k, name, source, line))
767130803Smarcel    return;
768130803Smarcel
769130803Smarcel  /* We should also check here that all the argument names in ARGV are
770130803Smarcel     distinct.  */
771130803Smarcel
772130803Smarcel  k = new_macro_key (t, name, source, line);
773130803Smarcel  d = new_macro_definition (t, macro_function_like, argc, argv, replacement);
774130803Smarcel  splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
775130803Smarcel}
776130803Smarcel
777130803Smarcel
778130803Smarcelvoid
779130803Smarcelmacro_undef (struct macro_source_file *source, int line,
780130803Smarcel             const char *name)
781130803Smarcel{
782130803Smarcel  splay_tree_node n = find_definition (name, source, line);
783130803Smarcel
784130803Smarcel  if (n)
785130803Smarcel    {
786130803Smarcel      /* This function is the only place a macro's end-of-scope
787130803Smarcel         location gets set to anything other than "end of the
788130803Smarcel         compilation unit" (i.e., end_file is zero).  So if this macro
789130803Smarcel         already has its end-of-scope set, then we're probably seeing
790130803Smarcel         a second #undefinition for the same #definition.  */
791130803Smarcel      struct macro_key *key = (struct macro_key *) n->key;
792130803Smarcel
793130803Smarcel      if (key->end_file)
794130803Smarcel        {
795130803Smarcel	  complaint (&symfile_complaints,
796130803Smarcel		     "macro '%s' is #undefined twice, at %s:%d and %s:%d", name,
797130803Smarcel		     source->filename, line, key->end_file->filename,
798130803Smarcel		     key->end_line);
799130803Smarcel        }
800130803Smarcel
801130803Smarcel      /* Whatever the case, wipe out the old ending point, and
802130803Smarcel         make this the ending point.  */
803130803Smarcel      key->end_file = source;
804130803Smarcel      key->end_line = line;
805130803Smarcel    }
806130803Smarcel  else
807130803Smarcel    {
808130803Smarcel      /* According to the ISO C standard, an #undef for a symbol that
809130803Smarcel         has no macro definition in scope is ignored.  So we should
810130803Smarcel         ignore it too.  */
811130803Smarcel#if 0
812130803Smarcel      complaint (&symfile_complaints,
813130803Smarcel		 "no definition for macro `%s' in scope to #undef at %s:%d",
814130803Smarcel		 name, source->filename, line);
815130803Smarcel#endif
816130803Smarcel    }
817130803Smarcel}
818130803Smarcel
819130803Smarcel
820130803Smarcelstruct macro_definition *
821130803Smarcelmacro_lookup_definition (struct macro_source_file *source,
822130803Smarcel                         int line, const char *name)
823130803Smarcel{
824130803Smarcel  splay_tree_node n = find_definition (name, source, line);
825130803Smarcel
826130803Smarcel  if (n)
827130803Smarcel    return (struct macro_definition *) n->value;
828130803Smarcel  else
829130803Smarcel    return 0;
830130803Smarcel}
831130803Smarcel
832130803Smarcel
833130803Smarcelstruct macro_source_file *
834130803Smarcelmacro_definition_location (struct macro_source_file *source,
835130803Smarcel                           int line,
836130803Smarcel                           const char *name,
837130803Smarcel                           int *definition_line)
838130803Smarcel{
839130803Smarcel  splay_tree_node n = find_definition (name, source, line);
840130803Smarcel
841130803Smarcel  if (n)
842130803Smarcel    {
843130803Smarcel      struct macro_key *key = (struct macro_key *) n->key;
844130803Smarcel      *definition_line = key->start_line;
845130803Smarcel      return key->start_file;
846130803Smarcel    }
847130803Smarcel  else
848130803Smarcel    return 0;
849130803Smarcel}
850130803Smarcel
851130803Smarcel
852130803Smarcel
853130803Smarcel/* Creating and freeing macro tables.  */
854130803Smarcel
855130803Smarcel
856130803Smarcelstruct macro_table *
857130803Smarcelnew_macro_table (struct obstack *obstack,
858130803Smarcel                 struct bcache *b)
859130803Smarcel{
860130803Smarcel  struct macro_table *t;
861130803Smarcel
862130803Smarcel  /* First, get storage for the `struct macro_table' itself.  */
863130803Smarcel  if (obstack)
864130803Smarcel    t = obstack_alloc (obstack, sizeof (*t));
865130803Smarcel  else
866130803Smarcel    t = xmalloc (sizeof (*t));
867130803Smarcel
868130803Smarcel  memset (t, 0, sizeof (*t));
869130803Smarcel  t->obstack = obstack;
870130803Smarcel  t->bcache = b;
871130803Smarcel  t->main_source = NULL;
872130803Smarcel  t->definitions = (splay_tree_new_with_allocator
873130803Smarcel                    (macro_tree_compare,
874130803Smarcel                     ((splay_tree_delete_key_fn) macro_tree_delete_key),
875130803Smarcel                     ((splay_tree_delete_value_fn) macro_tree_delete_value),
876130803Smarcel                     ((splay_tree_allocate_fn) macro_alloc),
877130803Smarcel                     ((splay_tree_deallocate_fn) macro_free),
878130803Smarcel                     t));
879130803Smarcel
880130803Smarcel  return t;
881130803Smarcel}
882130803Smarcel
883130803Smarcel
884130803Smarcelvoid
885130803Smarcelfree_macro_table (struct macro_table *table)
886130803Smarcel{
887130803Smarcel  /* Free the source file tree.  */
888130803Smarcel  free_macro_source_file (table->main_source);
889130803Smarcel
890130803Smarcel  /* Free the table of macro definitions.  */
891130803Smarcel  splay_tree_delete (table->definitions);
892130803Smarcel}
893